Skip to content

Add netstacklat tool #125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
5 changes: 5 additions & 0 deletions headers/vmlinux/vmlinux_net.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ struct sk_buff {
__u8 nf_trace: 1;
__u8 ip_summed: 2;
__u8 ooo_okay: 1;
__u8 tstamp_type: 2;
__u8 l4_hash: 1;
__u8 sw_hash: 1;
__u8 wifi_acked_valid: 1;
Expand Down Expand Up @@ -145,4 +146,8 @@ enum ip_conntrack_status {
IPS_CONFIRMED = (1 << IPS_CONFIRMED_BIT),
};

struct scm_timestamping_internal {
struct timespec64 ts[3];
};

#endif /* __VMLINUX_NET_H__ */
7 changes: 7 additions & 0 deletions headers/vmlinux/vmlinux_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,11 @@ typedef __u64 u64;

typedef s64 ktime_t;

typedef __s64 time64_t;

struct timespec64 {
time64_t tv_sec;
long int tv_nsec;
};

#endif /* __VMLINUX_TYPES_H__ */
1 change: 1 addition & 0 deletions netstacklat/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
netstacklat
13 changes: 13 additions & 0 deletions netstacklat/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)

USER_TARGETS := netstacklat
BPF_TARGETS := netstacklat.bpf
BPF_SKEL_OBJ := netstacklat.bpf.o

EXTRA_DEPS += netstacklat.h bits.bpf.h
LDLIBS += -lm

LIB_DIR = ../lib

include $(LIB_DIR)/common.mk

58 changes: 58 additions & 0 deletions netstacklat/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Netstacklat - Monitor latency within the network stack
Netstacklat is a simple tool for monitoring latency within the Linux
network stack for ingress traffic. The tool relies on the kernel time
stamping received packets (`SOF_TIMESTAMPING_RX_SOFTWARE`),
specifically setting `sk_buff->tstamp`. It then reports when packets
arrive at various hooks relative to this timestamp, i.e. the time
between the packet being timestamped by the kernel and reaching a
specific hook.

The tool is based on the following bpftrace script from Jesper
Dangaard Brouer:
```console
sudo bpftrace -e '
kfunc:tcp_v4_do_rcv,
kfunc:tcp_data_queue,
kfunc:udp_queue_rcv_one_skb
{
$tai_offset=37000000000;
$now=nsecs(tai)-$tai_offset; @cnt[probe]=count(); @total[probe]=count();
$ts=args->skb->tstamp; $delta=$now-(uint64)$ts;
@hist_ns[probe]=hist($delta);
@stats[probe]=stats($delta);
//printf("now:%llu - ts:%llu = delta:%llu\n", $now, $ts, $delta);
}
interval:s:10 {time("\n%H:%M:%S\n");
print(@cnt); clear(@cnt);
print(@total);
print(@stats);
print(@hist_ns);
}'
```

The eBPF part of the tool (`netstacklat.bpf.c`) is designed to be
compatible with
[ebpf_exporter](https://github.com/cloudflare/ebpf_exporter), so that
the data can easily be exported to Prometheus. The easiest way to use
netstacklat together with ebpf-exporter is simply to point it to this
directory, i.e.
```console
$ ebpf_exporter --config.dir=<path>/bpf-examples/netstacklat --config.names=netstacklat
```

Alternatively, you can copy over the files to ebpf-exporter's example
repository.
```console
$ cp netstacklat.{bpf.c,h,yaml} -t <path>/ebpf_exporter/examples/
# Fix up some header includes (e.g. "vmlinux_local.h" -> <vmlinux.h>
$ make -C <path>/ebpf_exporter/examples build
$ ebpf_exporter --config.dir=<path>/ebpf_exporter/examples --config.names
```

Note that when using together with ebpf-exporter, some of the
functionality handled by netstacklat's userspace program will not be
available. This includes setting the `TAI_OFFSET` constant in
`netstacklat.bpf.c` to match your system's TAI offset (you can do this
manually instead), and enabling RX timestamping by the kernel (see the
`enable_sw_rx_tstamps()` function in `netstacklat.c` for an example of
how to do this).
29 changes: 29 additions & 0 deletions netstacklat/bits.bpf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
/* From https://github.com/iovisor/bcc/blob/v0.25.0/libbpf-tools/bits.bpf.h*/

#ifndef __BITS_BPF_H
#define __BITS_BPF_H

static __always_inline u64 log2(u32 v)
{
u32 shift, r;

r = (v > 0xFFFF) << 4; v >>= r;
shift = (v > 0xFF) << 3; v >>= shift; r |= shift;
shift = (v > 0xF) << 2; v >>= shift; r |= shift;
shift = (v > 0x3) << 1; v >>= shift; r |= shift;
r |= (v >> 1);

return r;
}

static __always_inline u64 log2l(u64 v)
{
u32 hi = v >> 32;
if (hi)
return log2(hi) + 32;
else
return log2(v);
}

#endif /* __BITS_BPF_H */
Loading