1 /* XDP monitor tool, based on tracepoints
3 * Copyright(c) 2017 Jesper Dangaard Brouer, Red Hat Inc.
5 #include <uapi/linux/bpf.h>
6 #include "bpf_helpers.h"
8 struct bpf_map_def SEC("maps") redirect_err_cnt = {
9 .type = BPF_MAP_TYPE_PERCPU_ARRAY,
10 .key_size = sizeof(u32),
11 .value_size = sizeof(u64),
13 /* TODO: have entries for all possible errno's */
16 #define XDP_UNKNOWN XDP_REDIRECT + 1
17 struct bpf_map_def SEC("maps") exception_cnt = {
18 .type = BPF_MAP_TYPE_PERCPU_ARRAY,
19 .key_size = sizeof(u32),
20 .value_size = sizeof(u64),
21 .max_entries = XDP_UNKNOWN + 1,
24 /* Tracepoint format: /sys/kernel/debug/tracing/events/xdp/xdp_redirect/format
25 * Code in: kernel/include/trace/events/xdp.h
27 struct xdp_redirect_ctx {
28 u64 __pad; // First 8 bytes are not accessible by bpf code
29 int prog_id; // offset:8; size:4; signed:1;
30 u32 act; // offset:12 size:4; signed:0;
31 int ifindex; // offset:16 size:4; signed:1;
32 int err; // offset:20 size:4; signed:1;
33 int to_ifindex; // offset:24 size:4; signed:1;
34 u32 map_id; // offset:28 size:4; signed:0;
35 int map_index; // offset:32 size:4; signed:1;
39 XDP_REDIRECT_SUCCESS = 0,
40 XDP_REDIRECT_ERROR = 1
43 static __always_inline
44 int xdp_redirect_collect_stat(struct xdp_redirect_ctx *ctx)
46 u32 key = XDP_REDIRECT_ERROR;
51 key = XDP_REDIRECT_SUCCESS;
53 cnt = bpf_map_lookup_elem(&redirect_err_cnt, &key);
58 return 0; /* Indicate event was filtered (no further processing)*/
60 * Returning 1 here would allow e.g. a perf-record tracepoint
61 * to see and record these events, but it doesn't work well
62 * in-practice as stopping perf-record also unload this
63 * bpf_prog. Plus, there is additional overhead of doing so.
67 SEC("tracepoint/xdp/xdp_redirect_err")
68 int trace_xdp_redirect_err(struct xdp_redirect_ctx *ctx)
70 return xdp_redirect_collect_stat(ctx);
74 SEC("tracepoint/xdp/xdp_redirect_map_err")
75 int trace_xdp_redirect_map_err(struct xdp_redirect_ctx *ctx)
77 return xdp_redirect_collect_stat(ctx);
80 /* Likely unloaded when prog starts */
81 SEC("tracepoint/xdp/xdp_redirect")
82 int trace_xdp_redirect(struct xdp_redirect_ctx *ctx)
84 return xdp_redirect_collect_stat(ctx);
87 /* Likely unloaded when prog starts */
88 SEC("tracepoint/xdp/xdp_redirect_map")
89 int trace_xdp_redirect_map(struct xdp_redirect_ctx *ctx)
91 return xdp_redirect_collect_stat(ctx);
94 /* Tracepoint format: /sys/kernel/debug/tracing/events/xdp/xdp_exception/format
95 * Code in: kernel/include/trace/events/xdp.h
97 struct xdp_exception_ctx {
98 u64 __pad; // First 8 bytes are not accessible by bpf code
99 int prog_id; // offset:8; size:4; signed:1;
100 u32 act; // offset:12; size:4; signed:0;
101 int ifindex; // offset:16; size:4; signed:1;
104 SEC("tracepoint/xdp/xdp_exception")
105 int trace_xdp_exception(struct xdp_exception_ctx *ctx)
111 if (key > XDP_REDIRECT)
114 cnt = bpf_map_lookup_elem(&exception_cnt, &key);