Skip to content

Commit 780984d

Browse files
committed
netstacklat: take sk_backlog into account
Our checks for empty or almost empty sockets were wrong, because sockets also have a backlog queue. Signed-off-by: Jesper Dangaard Brouer <[email protected]>
1 parent 593f76b commit 780984d

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

examples/netstacklat.bpf.c

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* SPDX-License-Identifier: GPL-2.0-or-later */
22
/*
3-
* This is a ebpf_exporter variant of the netstacklat tool
3+
* This is an ebpf_exporter variant of the netstacklat tool
44
*
55
* Netstacklat - is a tool that "Monitor RX latency within the network stack"
66
* - https://github.com/xdp-project/bpf-examples/tree/main/netstacklat
@@ -29,7 +29,7 @@ char LICENSE[] SEC("license") = "GPL";
2929
const __s64 TAI_OFFSET = (37LL * NS_PER_S);
3030
const struct netstacklat_bpf_config user_config = {
3131
.network_ns = 0,
32-
.filter_queue_len = 3, /* zero means filter is inactive */
32+
.filter_queue_len = 1, /* zero means filter is inactive */
3333
.filter_pid = false,
3434
.filter_ifindex = true,
3535
.filter_cgroup = true,
@@ -390,12 +390,24 @@ static inline int skb_queue_empty(const struct sk_buff_head *list)
390390
return READ_ONCE(list->next) == (const struct sk_buff *)list;
391391
}
392392

393+
static inline bool sk_backlog_empty(const struct sock *sk)
394+
{
395+
return READ_ONCE(sk->sk_backlog.tail) == NULL;
396+
}
397+
393398
static bool filter_nonempty_sockqueue(struct sock *sk)
394399
{
395400
if (!user_config.filter_nonempty_sockqueue)
396401
return true;
397402

398-
return !skb_queue_empty(&sk->sk_receive_queue);
403+
if (!skb_queue_empty(&sk->sk_receive_queue))
404+
return true;
405+
406+
/* Packets can also be on the sk_backlog */
407+
if (!sk_backlog_empty(sk))
408+
return true;
409+
410+
return false;
399411
}
400412

401413
/* To lower runtime overhead, skip recording timestamps for sockets with very
@@ -416,6 +428,14 @@ static bool filter_queue_len(struct sock *sk)
416428

417429
if (sk_queue_len(&sk->sk_receive_queue) > above_len)
418430
return true;
431+
432+
/* Packets can also be on the sk_backlog, but we don't know the number
433+
* of SKBs on the queue, because sk_backlog.len is in bytes (based on
434+
* skb->truesize). Thus, if any backlog exists we don't filter.
435+
*/
436+
if (!sk_backlog_empty(sk))
437+
return true;
438+
419439
return false;
420440
}
421441

@@ -526,7 +546,6 @@ int BPF_PROG(netstacklat_udp_enqueue_schedule_skb, struct sock *sk,
526546
#endif /* CONFIG_HOOKS_ENQUEUE */
527547

528548
#ifdef CONFIG_HOOKS_DEQUEUE
529-
530549
SEC("fentry/tcp_recv_timestamp")
531550
int BPF_PROG(netstacklat_tcp_recv_timestamp, void *msg, struct sock *sk,
532551
struct scm_timestamping_internal *tss)

0 commit comments

Comments
 (0)