Skip to content

Commit 6a1962e

Browse files
committed
netstacklat: convert bpf_printk to optional debug feature
For production we need a way to disable any use of bpf_printk. To track errors in production introduce a map for counting these errors, as that will be exposed as a Prometheus counter naming it netstacklat_errors_total. The new "dbg" macro handled/hides if bpf_printk or counters are enabled. Signed-off-by: Jesper Dangaard Brouer <hawk@kernel.org>
1 parent 1443f7b commit 6a1962e

File tree

2 files changed

+82
-11
lines changed

2 files changed

+82
-11
lines changed

examples/netstacklat.bpf.c

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,56 @@ static void record_latency_since(ktime_t tstamp, const struct hist_key *key)
265265
}
266266
#endif /* !CONFIG_MAP_MACROS */
267267

268+
/* Debug facility to count errors */
269+
#define MAX_ERROR_TYPES 8
270+
enum error_types {
271+
ERR_UNKNOWN = 0,
272+
ERR_sk_storage = 1,
273+
ERR_READ_TCP_rcv_wup = 2,
274+
ERR_READ_TCP_rcv_wnd = 3,
275+
ERR_READ_TCP_rcv_nxt = 4,
276+
ERR_READ_TCP_last_skb_cb = 5,
277+
ERR_READ_TCP_cp_seq = 6,
278+
ERR_READ_TCP_rcv_ooopack = 7,
279+
};
280+
struct {
281+
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
282+
__uint(max_entries, MAX_ERROR_TYPES);
283+
__type(key, u32);
284+
__type(value, u64);
285+
} netstacklat_errors_total SEC(".maps");
286+
287+
/* This provide easy way to disable debug feature for errors.
288+
* Disabling this reduces BPF code size.
289+
*/
290+
#define CONFIG_TRACK_ERRORS 1
291+
//#define CONFIG_PRINT_ERRORS 1
292+
293+
void record_errors(u32 err)
294+
{
295+
#ifdef CONFIG_TRACK_ERRORS
296+
u32 key = ERR_UNKNOWN;
297+
298+
if (err < MAX_ERROR_TYPES)
299+
key = err;
300+
301+
increment_map_nosync(&netstacklat_errors_total, &key, 1);
302+
#endif /* CONFIG_TRACK_ERRORS */
303+
}
304+
305+
#ifdef CONFIG_PRINT_ERRORS
306+
#define my_printk(fmt, ...) bpf_printk(fmt, ##__VA_ARGS__)
307+
#else /* !CONFIG_PRINT_ERRORS */
308+
#define my_printk(fmt, ...)
309+
#endif
310+
311+
/* Debug macro that can be disabled compile time */
312+
#define dbg(__ERR_NR, fmt, ...) \
313+
({ \
314+
record_errors(__ERR_NR); \
315+
my_printk(fmt, ##__VA_ARGS__); \
316+
})
317+
268318
static inline bool filter_nth_packet(const enum netstacklat_hook hook)
269319
{
270320
u32 key = hook;
@@ -516,13 +566,15 @@ static int get_current_rcv_wnd_seq(struct tcp_sock *tp, u32 rcv_nxt, u32 *seq)
516566

517567
err = bpf_core_read(&rcv_wup, sizeof(rcv_wup), &tp->rcv_wup);
518568
if (err) {
519-
bpf_printk("failed to read tcp_sock->rcv_wup, err=%d", err);
569+
dbg(ERR_READ_TCP_rcv_wup,
570+
"failed to read tcp_sock->rcv_wup, err=%d", err);
520571
goto exit;
521572
}
522573

523574
err = bpf_core_read(&rcv_wnd, sizeof(rcv_wnd), &tp->rcv_wnd);
524575
if (err) {
525-
bpf_printk("failed to read tcp_sock->rcv_wnd, err=%d", err);
576+
dbg(ERR_READ_TCP_rcv_wnd,
577+
"failed to read tcp_sock->rcv_wnd, err=%d", err);
526578
goto exit;
527579
}
528580

@@ -543,7 +595,8 @@ static int current_max_possible_ooo_seq(struct tcp_sock *tp, u32 *seq)
543595

544596
err = bpf_core_read(&rcv_nxt, sizeof(rcv_nxt), &tp->rcv_nxt);
545597
if (err) {
546-
bpf_printk("failed reading tcp_sock->rcv_nxt, err=%d", err);
598+
dbg(ERR_READ_TCP_rcv_nxt,
599+
"failed reading tcp_sock->rcv_nxt, err=%d", err);
547600
goto exit;
548601
}
549602

@@ -562,9 +615,8 @@ static int current_max_possible_ooo_seq(struct tcp_sock *tp, u32 *seq)
562615
*/
563616
err = BPF_CORE_READ_INTO(&cb, tp, ooo_last_skb, cb);
564617
if (err) {
565-
bpf_printk(
566-
"failed to read tcp_sock->ooo_last_skb->cb, err=%d",
567-
err);
618+
dbg(ERR_READ_TCP_last_skb_cb,
619+
"failed to read tcp_sock->ooo_last_skb->cb, err=%d", err);
568620
goto exit;
569621
}
570622

@@ -598,7 +650,8 @@ static bool tcp_read_in_ooo_range(struct tcp_sock *tp,
598650

599651
err = bpf_core_read(&read_seq, sizeof(read_seq), &tp->copied_seq);
600652
if (err) {
601-
bpf_printk("failed to read tcp_sock->copied_seq, err=%d", err);
653+
dbg(ERR_READ_TCP_cp_seq,
654+
"failed to read tcp_sock->copied_seq, err=%d", err);
602655
return true; // Assume we may be in ooo-range
603656
}
604657

@@ -619,8 +672,8 @@ static bool tcp_read_maybe_holblocked(struct sock *sk)
619672

620673
err = bpf_core_read(&n_ooopkts, sizeof(n_ooopkts), &tp->rcv_ooopack);
621674
if (err) {
622-
bpf_printk("failed to read tcp_sock->rcv_ooopack, err=%d\n",
623-
err);
675+
dbg(ERR_READ_TCP_rcv_ooopack,
676+
"failed to read tcp_sock->rcv_ooopack, err=%d\n", err);
624677
return true; // Assume we may be in ooo-range
625678
}
626679

@@ -630,8 +683,8 @@ static bool tcp_read_maybe_holblocked(struct sock *sk)
630683
ooo_range = bpf_sk_storage_get(&netstack_tcp_ooo_range, sk, NULL,
631684
BPF_SK_STORAGE_GET_F_CREATE);
632685
if (!ooo_range) {
633-
bpf_printk(
634-
"failed getting ooo-range socket storage for tcp socket");
686+
dbg(ERR_sk_storage,
687+
"failed getting ooo-range socket storage for tcp socket");
635688
return true; // Assume we may be in ooo-range
636689
}
637690

examples/netstacklat.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,24 @@ metrics:
3737
size: 2
3838
decoders:
3939
- name: uint
40+
counters:
41+
- name: netstacklat_errors_total
42+
help: Counter for bpf_core_read errors in code (can be disabled in code)
43+
labels:
44+
- name: type
45+
size: 4
46+
decoders:
47+
- name: uint
48+
- name: static_map
49+
static_map:
50+
0: unknown
51+
1: err_sk_storage
52+
2: err_read_tcp_rcv_wup
53+
3: err_read_tcp_rcv_wnd
54+
4: err_read_tcp_rcv_nxt
55+
5: err_read_tcp_last_skb_cb
56+
6: err_read_tcp_cp_seq
57+
7: err_read_tcp_rcv_ooopack
4058

4159
# Remember to update #define N_CGROUPS in code when adding more matches
4260
cgroup_id_map:

0 commit comments

Comments
 (0)