This source file includes following definitions.
- xdp_redirect_collect_stat
- SEC
- SEC
- SEC
- SEC
- SEC
- SEC
- SEC
- SEC
1
2
3
4
5
6 #include <uapi/linux/bpf.h>
7 #include "bpf_helpers.h"
8
9 struct bpf_map_def SEC("maps") redirect_err_cnt = {
10 .type = BPF_MAP_TYPE_PERCPU_ARRAY,
11 .key_size = sizeof(u32),
12 .value_size = sizeof(u64),
13 .max_entries = 2,
14
15 };
16
17 #define XDP_UNKNOWN XDP_REDIRECT + 1
18 struct bpf_map_def SEC("maps") exception_cnt = {
19 .type = BPF_MAP_TYPE_PERCPU_ARRAY,
20 .key_size = sizeof(u32),
21 .value_size = sizeof(u64),
22 .max_entries = XDP_UNKNOWN + 1,
23 };
24
25
26
27
28 struct xdp_redirect_ctx {
29 u64 __pad;
30 int prog_id;
31 u32 act;
32 int ifindex;
33 int err;
34 int to_ifindex;
35 u32 map_id;
36 int map_index;
37 };
38
39 enum {
40 XDP_REDIRECT_SUCCESS = 0,
41 XDP_REDIRECT_ERROR = 1
42 };
43
44 static __always_inline
45 int xdp_redirect_collect_stat(struct xdp_redirect_ctx *ctx)
46 {
47 u32 key = XDP_REDIRECT_ERROR;
48 int err = ctx->err;
49 u64 *cnt;
50
51 if (!err)
52 key = XDP_REDIRECT_SUCCESS;
53
54 cnt = bpf_map_lookup_elem(&redirect_err_cnt, &key);
55 if (!cnt)
56 return 1;
57 *cnt += 1;
58
59 return 0;
60
61
62
63
64
65
66 }
67
68 SEC("tracepoint/xdp/xdp_redirect_err")
69 int trace_xdp_redirect_err(struct xdp_redirect_ctx *ctx)
70 {
71 return xdp_redirect_collect_stat(ctx);
72 }
73
74
75 SEC("tracepoint/xdp/xdp_redirect_map_err")
76 int trace_xdp_redirect_map_err(struct xdp_redirect_ctx *ctx)
77 {
78 return xdp_redirect_collect_stat(ctx);
79 }
80
81
82 SEC("tracepoint/xdp/xdp_redirect")
83 int trace_xdp_redirect(struct xdp_redirect_ctx *ctx)
84 {
85 return xdp_redirect_collect_stat(ctx);
86 }
87
88
89 SEC("tracepoint/xdp/xdp_redirect_map")
90 int trace_xdp_redirect_map(struct xdp_redirect_ctx *ctx)
91 {
92 return xdp_redirect_collect_stat(ctx);
93 }
94
95
96
97
98 struct xdp_exception_ctx {
99 u64 __pad;
100 int prog_id;
101 u32 act;
102 int ifindex;
103 };
104
105 SEC("tracepoint/xdp/xdp_exception")
106 int trace_xdp_exception(struct xdp_exception_ctx *ctx)
107 {
108 u64 *cnt;
109 u32 key;
110
111 key = ctx->act;
112 if (key > XDP_REDIRECT)
113 key = XDP_UNKNOWN;
114
115 cnt = bpf_map_lookup_elem(&exception_cnt, &key);
116 if (!cnt)
117 return 1;
118 *cnt += 1;
119
120 return 0;
121 }
122
123
124 struct datarec {
125 u64 processed;
126 u64 dropped;
127 u64 info;
128 u64 err;
129 };
130 #define MAX_CPUS 64
131
132 struct bpf_map_def SEC("maps") cpumap_enqueue_cnt = {
133 .type = BPF_MAP_TYPE_PERCPU_ARRAY,
134 .key_size = sizeof(u32),
135 .value_size = sizeof(struct datarec),
136 .max_entries = MAX_CPUS,
137 };
138
139 struct bpf_map_def SEC("maps") cpumap_kthread_cnt = {
140 .type = BPF_MAP_TYPE_PERCPU_ARRAY,
141 .key_size = sizeof(u32),
142 .value_size = sizeof(struct datarec),
143 .max_entries = 1,
144 };
145
146
147
148
149 struct cpumap_enqueue_ctx {
150 u64 __pad;
151 int map_id;
152 u32 act;
153 int cpu;
154 unsigned int drops;
155 unsigned int processed;
156 int to_cpu;
157 };
158
159 SEC("tracepoint/xdp/xdp_cpumap_enqueue")
160 int trace_xdp_cpumap_enqueue(struct cpumap_enqueue_ctx *ctx)
161 {
162 u32 to_cpu = ctx->to_cpu;
163 struct datarec *rec;
164
165 if (to_cpu >= MAX_CPUS)
166 return 1;
167
168 rec = bpf_map_lookup_elem(&cpumap_enqueue_cnt, &to_cpu);
169 if (!rec)
170 return 0;
171 rec->processed += ctx->processed;
172 rec->dropped += ctx->drops;
173
174
175 if (ctx->processed > 0)
176 rec->info += 1;
177
178 return 0;
179 }
180
181
182
183
184 struct cpumap_kthread_ctx {
185 u64 __pad;
186 int map_id;
187 u32 act;
188 int cpu;
189 unsigned int drops;
190 unsigned int processed;
191 int sched;
192 };
193
194 SEC("tracepoint/xdp/xdp_cpumap_kthread")
195 int trace_xdp_cpumap_kthread(struct cpumap_kthread_ctx *ctx)
196 {
197 struct datarec *rec;
198 u32 key = 0;
199
200 rec = bpf_map_lookup_elem(&cpumap_kthread_cnt, &key);
201 if (!rec)
202 return 0;
203 rec->processed += ctx->processed;
204 rec->dropped += ctx->drops;
205
206
207 if (ctx->sched)
208 rec->info++;
209
210 return 0;
211 }
212
213 struct bpf_map_def SEC("maps") devmap_xmit_cnt = {
214 .type = BPF_MAP_TYPE_PERCPU_ARRAY,
215 .key_size = sizeof(u32),
216 .value_size = sizeof(struct datarec),
217 .max_entries = 1,
218 };
219
220
221
222
223 struct devmap_xmit_ctx {
224 u64 __pad;
225 int map_id;
226 u32 act;
227 u32 map_index;
228 int drops;
229 int sent;
230 int from_ifindex;
231 int to_ifindex;
232 int err;
233 };
234
235 SEC("tracepoint/xdp/xdp_devmap_xmit")
236 int trace_xdp_devmap_xmit(struct devmap_xmit_ctx *ctx)
237 {
238 struct datarec *rec;
239 u32 key = 0;
240
241 rec = bpf_map_lookup_elem(&devmap_xmit_cnt, &key);
242 if (!rec)
243 return 0;
244 rec->processed += ctx->sent;
245 rec->dropped += ctx->drops;
246
247
248 rec->info += 1;
249
250
251 if (ctx->err)
252 rec->err++;
253
254
255 if (ctx->drops < 0)
256 rec->err++;
257
258 return 1;
259 }