Skip to content

Commit 0cf3c2f

Browse files
kkdwvdAlexei Starovoitov
authored andcommitted
samples: bpf: Add BPF support for cpumap tracepoints
These are invoked in two places, when the XDP frame or SKB (for generic XDP) enqueued to the ptr_ring (cpumap_enqueue) and when kthread processes the frame after invoking the CPUMAP program for it (returning stats for the batch). We use cpumap_map_id to filter on the map_id as a way to avoid printing incorrect stats for parallel sessions of xdp_redirect_cpu. Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20210821002010.845777-9-memxor@gmail.com
1 parent 82c4508 commit 0cf3c2f

1 file changed

Lines changed: 57 additions & 1 deletion

File tree

‎samples/bpf/xdp_sample.bpf.c‎

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
array_map rx_cnt SEC(".maps");
1010
array_map redir_err_cnt SEC(".maps");
11+
array_map cpumap_enqueue_cnt SEC(".maps");
12+
array_map cpumap_kthread_cnt SEC(".maps");
1113
array_map exception_cnt SEC(".maps");
1214

1315
const volatile int nr_cpus = 0;
@@ -19,6 +21,8 @@ const volatile int nr_cpus = 0;
1921
const volatile int from_match[32] = {};
2022
const volatile int to_match[32] = {};
2123

24+
int cpumap_map_id = 0;
25+
2226
/* Find if b is part of set a, but if a is empty set then evaluate to true */
2327
#define IN_SET(a, b) \
2428
({ \
@@ -112,6 +116,59 @@ int BPF_PROG(tp_xdp_redirect_map, const struct net_device *dev,
112116
return xdp_redirect_collect_stat(dev->ifindex, err);
113117
}
114118

119+
SEC("tp_btf/xdp_cpumap_enqueue")
120+
int BPF_PROG(tp_xdp_cpumap_enqueue, int map_id, unsigned int processed,
121+
unsigned int drops, int to_cpu)
122+
{
123+
u32 cpu = bpf_get_smp_processor_id();
124+
struct datarec *rec;
125+
u32 idx;
126+
127+
if (cpumap_map_id && cpumap_map_id != map_id)
128+
return 0;
129+
130+
idx = to_cpu * nr_cpus + cpu;
131+
rec = bpf_map_lookup_elem(&cpumap_enqueue_cnt, &idx);
132+
if (!rec)
133+
return 0;
134+
NO_TEAR_ADD(rec->processed, processed);
135+
NO_TEAR_ADD(rec->dropped, drops);
136+
/* Record bulk events, then userspace can calc average bulk size */
137+
if (processed > 0)
138+
NO_TEAR_INC(rec->issue);
139+
/* Inception: It's possible to detect overload situations, via
140+
* this tracepoint. This can be used for creating a feedback
141+
* loop to XDP, which can take appropriate actions to mitigate
142+
* this overload situation.
143+
*/
144+
return 0;
145+
}
146+
147+
SEC("tp_btf/xdp_cpumap_kthread")
148+
int BPF_PROG(tp_xdp_cpumap_kthread, int map_id, unsigned int processed,
149+
unsigned int drops, int sched, struct xdp_cpumap_stats *xdp_stats)
150+
{
151+
struct datarec *rec;
152+
u32 cpu;
153+
154+
if (cpumap_map_id && cpumap_map_id != map_id)
155+
return 0;
156+
157+
cpu = bpf_get_smp_processor_id();
158+
rec = bpf_map_lookup_elem(&cpumap_kthread_cnt, &cpu);
159+
if (!rec)
160+
return 0;
161+
NO_TEAR_ADD(rec->processed, processed);
162+
NO_TEAR_ADD(rec->dropped, drops);
163+
NO_TEAR_ADD(rec->xdp_pass, xdp_stats->pass);
164+
NO_TEAR_ADD(rec->xdp_drop, xdp_stats->drop);
165+
NO_TEAR_ADD(rec->xdp_redirect, xdp_stats->redirect);
166+
/* Count times kthread yielded CPU via schedule call */
167+
if (sched)
168+
NO_TEAR_INC(rec->issue);
169+
return 0;
170+
}
171+
115172
SEC("tp_btf/xdp_exception")
116173
int BPF_PROG(tp_xdp_exception, const struct net_device *dev,
117174
const struct bpf_prog *xdp, u32 act)
@@ -136,4 +193,3 @@ int BPF_PROG(tp_xdp_exception, const struct net_device *dev,
136193

137194
return 0;
138195
}
139-

0 commit comments

Comments
 (0)