forked from xdp-project/xdp-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxdp_load_and_stats.c
337 lines (278 loc) · 8.71 KB
/
xdp_load_and_stats.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/* SPDX-License-Identifier: GPL-2.0 */
static const char *__doc__ = "XDP loader and stats program\n"
" - Allows selecting BPF section --progsec name to XDP-attach to --dev\n";
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <getopt.h>
#include <locale.h>
#include <unistd.h>
#include <time.h>
#include <bpf/bpf.h>
#include <bpf/libbpf.h>
#include <net/if.h>
#include <linux/if_link.h> /* depend on kernel-headers installed */
#include "../common/common_params.h"
#include "../common/common_user_bpf_xdp.h"
#include "common_kern_user.h"
#include "bpf_util.h" /* bpf_num_possible_cpus */
static const char *default_filename = "xdp_prog_kern.o";
static const char *default_progsec = "xdp_stats1";
static const struct option_wrapper long_options[] = {
{{"help", no_argument, NULL, 'h' },
"Show help", false},
{{"dev", required_argument, NULL, 'd' },
"Operate on device <ifname>", "<ifname>", true},
{{"skb-mode", no_argument, NULL, 'S' },
"Install XDP program in SKB (AKA generic) mode"},
{{"native-mode", no_argument, NULL, 'N' },
"Install XDP program in native mode"},
{{"auto-mode", no_argument, NULL, 'A' },
"Auto-detect SKB or native mode"},
{{"force", no_argument, NULL, 'F' },
"Force install, replacing existing program on interface"},
{{"unload", no_argument, NULL, 'U' },
"Unload XDP program instead of loading"},
{{"quiet", no_argument, NULL, 'q' },
"Quiet mode (no output)"},
{{"filename", required_argument, NULL, 1 },
"Load program from <file>", "<file>"},
{{"progsec", required_argument, NULL, 2 },
"Load program in <section> of the ELF file", "<section>"},
{{0, 0, NULL, 0 }}
};
int find_map_fd(struct bpf_object *bpf_obj, const char *mapname)
{
struct bpf_map *map;
int map_fd = -1;
/* Lesson#3: bpf_object to bpf_map */
map = bpf_object__find_map_by_name(bpf_obj, mapname);
if (!map) {
fprintf(stderr, "ERR: cannot find map by name: %s\n", mapname);
goto out;
}
map_fd = bpf_map__fd(map);
out:
return map_fd;
}
#define NANOSEC_PER_SEC 1000000000 /* 10^9 */
static __u64 gettime(void)
{
struct timespec t;
int res;
res = clock_gettime(CLOCK_MONOTONIC, &t);
if (res < 0) {
fprintf(stderr, "Error with gettimeofday! (%i)\n", res);
exit(EXIT_FAIL);
}
return (__u64) t.tv_sec * NANOSEC_PER_SEC + t.tv_nsec;
}
struct record {
__u64 timestamp;
struct datarec total; /* defined in common_kern_user.h */
};
struct stats_record {
struct record stats[1]; /* Assignment#2: Hint */
};
static double calc_period(struct record *r, struct record *p)
{
double period_ = 0;
__u64 period = 0;
period = r->timestamp - p->timestamp;
if (period > 0)
period_ = ((double) period / NANOSEC_PER_SEC);
return period_;
}
static void stats_print(struct stats_record *stats_rec,
struct stats_record *stats_prev)
{
struct record *rec, *prev;
double period;
__u64 packets;
double pps; /* packets per sec */
/* Assignment#2: Print other XDP actions stats */
{
char *fmt = "%-12s %'11lld pkts (%'10.0f pps)"
//" %'11lld Kbytes (%'6.0f Mbits/s)"
" period:%f\n";
const char *action = action2str(XDP_PASS);
rec = &stats_rec->stats[0];
prev = &stats_prev->stats[0];
period = calc_period(rec, prev);
if (period == 0)
return;
packets = rec->total.rx_packets - prev->total.rx_packets;
pps = packets / period;
printf(fmt, action, rec->total.rx_packets, pps, period);
}
}
/* BPF_MAP_TYPE_ARRAY */
void map_get_value_array(int fd, __u32 key, struct datarec *value)
{
if ((bpf_map_lookup_elem(fd, &key, value)) != 0) {
fprintf(stderr,
"ERR: bpf_map_lookup_elem failed key:0x%X\n", key);
}
}
/* BPF_MAP_TYPE_PERCPU_ARRAY */
void map_get_value_percpu_array(int fd, __u32 key, struct datarec *value)
{
/* For percpu maps, userspace gets a value per possible CPU */
// unsigned int nr_cpus = bpf_num_possible_cpus();
// struct datarec values[nr_cpus];
fprintf(stderr, "ERR: %s() not impl. see assignment#3", __func__);
}
static bool map_collect(int fd, __u32 map_type, __u32 key, struct record *rec)
{
struct datarec value;
/* Get time as close as possible to reading map contents */
rec->timestamp = gettime();
switch (map_type) {
case BPF_MAP_TYPE_ARRAY:
map_get_value_array(fd, key, &value);
break;
case BPF_MAP_TYPE_PERCPU_ARRAY:
/* fall-through */
default:
fprintf(stderr, "ERR: Unknown map_type(%u) cannot handle\n",
map_type);
return false;
break;
}
/* Assignment#1: Add byte counters */
rec->total.rx_packets = value.rx_packets;
return true;
}
static void stats_collect(int map_fd, __u32 map_type,
struct stats_record *stats_rec)
{
/* Assignment#2: Collect other XDP actions stats */
__u32 key = XDP_PASS;
map_collect(map_fd, map_type, key, &stats_rec->stats[0]);
}
static void stats_poll(int map_fd, __u32 map_type, int interval)
{
struct stats_record prev, record = { 0 };
/* Trick to pretty printf with thousands separators use %' */
setlocale(LC_NUMERIC, "en_US");
/* Print stats "header" */
if (verbose) {
printf("\n");
printf("%-12s\n", "XDP-action");
}
/* Get initial reading quickly */
stats_collect(map_fd, map_type, &record);
usleep(1000000/4);
while (1) {
prev = record; /* struct copy */
stats_collect(map_fd, map_type, &record);
stats_print(&record, &prev);
sleep(interval);
}
}
/* Lesson#4: It is userspace responsibility to known what map it is reading and
* know the value size. Here get bpf_map_info and check if it match our expected
* values.
*/
static int __check_map_fd_info(int map_fd, struct bpf_map_info *info,
struct bpf_map_info *exp)
{
__u32 info_len = sizeof(*info);
int err;
if (map_fd < 0)
return EXIT_FAIL;
/* BPF-info via bpf-syscall */
err = bpf_obj_get_info_by_fd(map_fd, info, &info_len);
if (err) {
fprintf(stderr, "ERR: %s() can't get info - %s\n",
__func__, strerror(errno));
return EXIT_FAIL_BPF;
}
if (exp->key_size && exp->key_size != info->key_size) {
fprintf(stderr, "ERR: %s() "
"Map key size(%d) mismatch expected size(%d)\n",
__func__, info->key_size, exp->key_size);
return EXIT_FAIL;
}
if (exp->value_size && exp->value_size != info->value_size) {
fprintf(stderr, "ERR: %s() "
"Map value size(%d) mismatch expected size(%d)\n",
__func__, info->value_size, exp->value_size);
return EXIT_FAIL;
}
if (exp->max_entries && exp->max_entries != info->max_entries) {
fprintf(stderr, "ERR: %s() "
"Map max_entries(%d) mismatch expected size(%d)\n",
__func__, info->max_entries, exp->max_entries);
return EXIT_FAIL;
}
if (exp->type && exp->type != info->type) {
fprintf(stderr, "ERR: %s() "
"Map type(%d) mismatch expected type(%d)\n",
__func__, info->type, exp->type);
return EXIT_FAIL;
}
return 0;
}
int main(int argc, char **argv)
{
struct bpf_map_info map_expect = { 0 };
struct bpf_map_info info = { 0 };
struct bpf_object *bpf_obj;
int stats_map_fd;
int interval = 2;
int err;
struct config cfg = {
.xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST | XDP_FLAGS_DRV_MODE,
.ifindex = -1,
.do_unload = false,
};
/* Set default BPF-ELF object file and BPF program name */
strncpy(cfg.filename, default_filename, sizeof(cfg.filename));
strncpy(cfg.progsec, default_progsec, sizeof(cfg.progsec));
/* Cmdline options can change progsec */
parse_cmdline_args(argc, argv, long_options, &cfg, __doc__);
/* Required option */
if (cfg.ifindex == -1) {
fprintf(stderr, "ERR: required option --dev missing\n");
usage(argv[0], __doc__, long_options, (argc == 1));
return EXIT_FAIL_OPTION;
}
if (cfg.do_unload)
return xdp_link_detach(cfg.ifindex, cfg.xdp_flags, 0);
bpf_obj = load_bpf_and_xdp_attach(&cfg);
if (!bpf_obj)
return EXIT_FAIL_BPF;
if (verbose) {
printf("Success: Loaded BPF-object(%s) and used section(%s)\n",
cfg.filename, cfg.progsec);
printf(" - XDP prog attached on device:%s(ifindex:%d)\n",
cfg.ifname, cfg.ifindex);
}
/* Lesson#3: Locate map file descriptor */
stats_map_fd = find_map_fd(bpf_obj, "xdp_stats_map");
if (stats_map_fd < 0) {
xdp_link_detach(cfg.ifindex, cfg.xdp_flags, 0);
return EXIT_FAIL_BPF;
}
/* Lesson#4: check map info, e.g. datarec is expected size */
map_expect.key_size = sizeof(__u32);
map_expect.value_size = sizeof(struct datarec);
map_expect.max_entries = XDP_ACTION_MAX;
err = __check_map_fd_info(stats_map_fd, &info, &map_expect);
if (err) {
fprintf(stderr, "ERR: map via FD not compatible\n");
return err;
}
if (verbose) {
printf("\nCollecting stats from BPF map\n");
printf(" - BPF map (bpf_map_type:%d) id:%d name:%s"
" key_size:%d value_size:%d max_entries:%d\n",
info.type, info.id, info.name,
info.key_size, info.value_size, info.max_entries
);
}
stats_poll(stats_map_fd, info.type, interval);
return EXIT_OK;
}