Loading...
1/* SPDX-License-Identifier: GPL-2.0
2 * Copyright (c) 2017 Jesper Dangaard Brouer, Red Hat Inc.
3 */
4static const char *__doc__ = " XDP RX-queue info extract example\n\n"
5 "Monitor how many packets per sec (pps) are received\n"
6 "per NIC RX queue index and which CPU processed the packet\n"
7 ;
8
9#include <errno.h>
10#include <signal.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <stdbool.h>
14#include <string.h>
15#include <unistd.h>
16#include <locale.h>
17#include <getopt.h>
18#include <net/if.h>
19#include <time.h>
20#include <limits.h>
21#include <arpa/inet.h>
22#include <linux/if_link.h>
23
24#include <bpf/bpf.h>
25#include <bpf/libbpf.h>
26#include "bpf_util.h"
27
28static int ifindex = -1;
29static char ifname_buf[IF_NAMESIZE];
30static char *ifname;
31static __u32 prog_id;
32
33static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
34
35static struct bpf_map *stats_global_map;
36static struct bpf_map *rx_queue_index_map;
37
38/* Exit return codes */
39#define EXIT_OK 0
40#define EXIT_FAIL 1
41#define EXIT_FAIL_OPTION 2
42#define EXIT_FAIL_XDP 3
43#define EXIT_FAIL_BPF 4
44#define EXIT_FAIL_MEM 5
45
46#define FAIL_MEM_SIG INT_MAX
47#define FAIL_STAT_SIG (INT_MAX - 1)
48
49static const struct option long_options[] = {
50 {"help", no_argument, NULL, 'h' },
51 {"dev", required_argument, NULL, 'd' },
52 {"skb-mode", no_argument, NULL, 'S' },
53 {"sec", required_argument, NULL, 's' },
54 {"no-separators", no_argument, NULL, 'z' },
55 {"action", required_argument, NULL, 'a' },
56 {"readmem", no_argument, NULL, 'r' },
57 {"swapmac", no_argument, NULL, 'm' },
58 {"force", no_argument, NULL, 'F' },
59 {0, 0, NULL, 0 }
60};
61
62static void int_exit(int sig)
63{
64 __u32 curr_prog_id = 0;
65
66 if (ifindex > -1) {
67 if (bpf_xdp_query_id(ifindex, xdp_flags, &curr_prog_id)) {
68 printf("bpf_xdp_query_id failed\n");
69 exit(EXIT_FAIL);
70 }
71 if (prog_id == curr_prog_id) {
72 fprintf(stderr,
73 "Interrupted: Removing XDP program on ifindex:%d device:%s\n",
74 ifindex, ifname);
75 bpf_xdp_detach(ifindex, xdp_flags, NULL);
76 } else if (!curr_prog_id) {
77 printf("couldn't find a prog id on a given iface\n");
78 } else {
79 printf("program on interface changed, not removing\n");
80 }
81 }
82
83 if (sig == FAIL_MEM_SIG)
84 exit(EXIT_FAIL_MEM);
85 else if (sig == FAIL_STAT_SIG)
86 exit(EXIT_FAIL);
87
88 exit(EXIT_OK);
89}
90
91struct config {
92 __u32 action;
93 int ifindex;
94 __u32 options;
95};
96enum cfg_options_flags {
97 NO_TOUCH = 0x0U,
98 READ_MEM = 0x1U,
99 SWAP_MAC = 0x2U,
100};
101#define XDP_ACTION_MAX (XDP_TX + 1)
102#define XDP_ACTION_MAX_STRLEN 11
103static const char *xdp_action_names[XDP_ACTION_MAX] = {
104 [XDP_ABORTED] = "XDP_ABORTED",
105 [XDP_DROP] = "XDP_DROP",
106 [XDP_PASS] = "XDP_PASS",
107 [XDP_TX] = "XDP_TX",
108};
109
110static const char *action2str(int action)
111{
112 if (action < XDP_ACTION_MAX)
113 return xdp_action_names[action];
114 return NULL;
115}
116
117static int parse_xdp_action(char *action_str)
118{
119 size_t maxlen;
120 __u64 action = -1;
121 int i;
122
123 for (i = 0; i < XDP_ACTION_MAX; i++) {
124 maxlen = XDP_ACTION_MAX_STRLEN;
125 if (strncmp(xdp_action_names[i], action_str, maxlen) == 0) {
126 action = i;
127 break;
128 }
129 }
130 return action;
131}
132
133static void list_xdp_actions(void)
134{
135 int i;
136
137 printf("Available XDP --action <options>\n");
138 for (i = 0; i < XDP_ACTION_MAX; i++)
139 printf("\t%s\n", xdp_action_names[i]);
140 printf("\n");
141}
142
143static char* options2str(enum cfg_options_flags flag)
144{
145 if (flag == NO_TOUCH)
146 return "no_touch";
147 if (flag & SWAP_MAC)
148 return "swapmac";
149 if (flag & READ_MEM)
150 return "read";
151 fprintf(stderr, "ERR: Unknown config option flags");
152 int_exit(FAIL_STAT_SIG);
153 return "unknown";
154}
155
156static void usage(char *argv[])
157{
158 int i;
159
160 printf("\nDOCUMENTATION:\n%s\n", __doc__);
161 printf(" Usage: %s (options-see-below)\n", argv[0]);
162 printf(" Listing options:\n");
163 for (i = 0; long_options[i].name != 0; i++) {
164 printf(" --%-12s", long_options[i].name);
165 if (long_options[i].flag != NULL)
166 printf(" flag (internal value:%d)",
167 *long_options[i].flag);
168 else
169 printf(" short-option: -%c",
170 long_options[i].val);
171 printf("\n");
172 }
173 printf("\n");
174 list_xdp_actions();
175}
176
177#define NANOSEC_PER_SEC 1000000000 /* 10^9 */
178static __u64 gettime(void)
179{
180 struct timespec t;
181 int res;
182
183 res = clock_gettime(CLOCK_MONOTONIC, &t);
184 if (res < 0) {
185 fprintf(stderr, "Error with gettimeofday! (%i)\n", res);
186 int_exit(FAIL_STAT_SIG);
187 }
188 return (__u64) t.tv_sec * NANOSEC_PER_SEC + t.tv_nsec;
189}
190
191/* Common stats data record shared with _kern.c */
192struct datarec {
193 __u64 processed;
194 __u64 issue;
195};
196struct record {
197 __u64 timestamp;
198 struct datarec total;
199 struct datarec *cpu;
200};
201struct stats_record {
202 struct record stats;
203 struct record *rxq;
204};
205
206static struct datarec *alloc_record_per_cpu(void)
207{
208 unsigned int nr_cpus = bpf_num_possible_cpus();
209 struct datarec *array;
210
211 array = calloc(nr_cpus, sizeof(struct datarec));
212 if (!array) {
213 fprintf(stderr, "Mem alloc error (nr_cpus:%u)\n", nr_cpus);
214 int_exit(FAIL_MEM_SIG);
215 }
216 return array;
217}
218
219static struct record *alloc_record_per_rxq(void)
220{
221 unsigned int nr_rxqs = bpf_map__max_entries(rx_queue_index_map);
222 struct record *array;
223
224 array = calloc(nr_rxqs, sizeof(struct record));
225 if (!array) {
226 fprintf(stderr, "Mem alloc error (nr_rxqs:%u)\n", nr_rxqs);
227 int_exit(FAIL_MEM_SIG);
228 }
229 return array;
230}
231
232static struct stats_record *alloc_stats_record(void)
233{
234 unsigned int nr_rxqs = bpf_map__max_entries(rx_queue_index_map);
235 struct stats_record *rec;
236 int i;
237
238 rec = calloc(1, sizeof(struct stats_record));
239 if (!rec) {
240 fprintf(stderr, "Mem alloc error\n");
241 int_exit(FAIL_MEM_SIG);
242 }
243 rec->rxq = alloc_record_per_rxq();
244 for (i = 0; i < nr_rxqs; i++)
245 rec->rxq[i].cpu = alloc_record_per_cpu();
246
247 rec->stats.cpu = alloc_record_per_cpu();
248 return rec;
249}
250
251static void free_stats_record(struct stats_record *r)
252{
253 unsigned int nr_rxqs = bpf_map__max_entries(rx_queue_index_map);
254 int i;
255
256 for (i = 0; i < nr_rxqs; i++)
257 free(r->rxq[i].cpu);
258
259 free(r->rxq);
260 free(r->stats.cpu);
261 free(r);
262}
263
264static bool map_collect_percpu(int fd, __u32 key, struct record *rec)
265{
266 /* For percpu maps, userspace gets a value per possible CPU */
267 unsigned int nr_cpus = bpf_num_possible_cpus();
268 struct datarec values[nr_cpus];
269 __u64 sum_processed = 0;
270 __u64 sum_issue = 0;
271 int i;
272
273 if ((bpf_map_lookup_elem(fd, &key, values)) != 0) {
274 fprintf(stderr,
275 "ERR: bpf_map_lookup_elem failed key:0x%X\n", key);
276 return false;
277 }
278 /* Get time as close as possible to reading map contents */
279 rec->timestamp = gettime();
280
281 /* Record and sum values from each CPU */
282 for (i = 0; i < nr_cpus; i++) {
283 rec->cpu[i].processed = values[i].processed;
284 sum_processed += values[i].processed;
285 rec->cpu[i].issue = values[i].issue;
286 sum_issue += values[i].issue;
287 }
288 rec->total.processed = sum_processed;
289 rec->total.issue = sum_issue;
290 return true;
291}
292
293static void stats_collect(struct stats_record *rec)
294{
295 int fd, i, max_rxqs;
296
297 fd = bpf_map__fd(stats_global_map);
298 map_collect_percpu(fd, 0, &rec->stats);
299
300 fd = bpf_map__fd(rx_queue_index_map);
301 max_rxqs = bpf_map__max_entries(rx_queue_index_map);
302 for (i = 0; i < max_rxqs; i++)
303 map_collect_percpu(fd, i, &rec->rxq[i]);
304}
305
306static double calc_period(struct record *r, struct record *p)
307{
308 double period_ = 0;
309 __u64 period = 0;
310
311 period = r->timestamp - p->timestamp;
312 if (period > 0)
313 period_ = ((double) period / NANOSEC_PER_SEC);
314
315 return period_;
316}
317
318static __u64 calc_pps(struct datarec *r, struct datarec *p, double period_)
319{
320 __u64 packets = 0;
321 __u64 pps = 0;
322
323 if (period_ > 0) {
324 packets = r->processed - p->processed;
325 pps = packets / period_;
326 }
327 return pps;
328}
329
330static __u64 calc_errs_pps(struct datarec *r,
331 struct datarec *p, double period_)
332{
333 __u64 packets = 0;
334 __u64 pps = 0;
335
336 if (period_ > 0) {
337 packets = r->issue - p->issue;
338 pps = packets / period_;
339 }
340 return pps;
341}
342
343static void stats_print(struct stats_record *stats_rec,
344 struct stats_record *stats_prev,
345 int action, __u32 cfg_opt)
346{
347 unsigned int nr_rxqs = bpf_map__max_entries(rx_queue_index_map);
348 unsigned int nr_cpus = bpf_num_possible_cpus();
349 double pps = 0, err = 0;
350 struct record *rec, *prev;
351 double t;
352 int rxq;
353 int i;
354
355 /* Header */
356 printf("\nRunning XDP on dev:%s (ifindex:%d) action:%s options:%s\n",
357 ifname, ifindex, action2str(action), options2str(cfg_opt));
358
359 /* stats_global_map */
360 {
361 char *fmt_rx = "%-15s %-7d %'-11.0f %'-10.0f %s\n";
362 char *fm2_rx = "%-15s %-7s %'-11.0f\n";
363 char *errstr = "";
364
365 printf("%-15s %-7s %-11s %-11s\n",
366 "XDP stats", "CPU", "pps", "issue-pps");
367
368 rec = &stats_rec->stats;
369 prev = &stats_prev->stats;
370 t = calc_period(rec, prev);
371 for (i = 0; i < nr_cpus; i++) {
372 struct datarec *r = &rec->cpu[i];
373 struct datarec *p = &prev->cpu[i];
374
375 pps = calc_pps (r, p, t);
376 err = calc_errs_pps(r, p, t);
377 if (err > 0)
378 errstr = "invalid-ifindex";
379 if (pps > 0)
380 printf(fmt_rx, "XDP-RX CPU",
381 i, pps, err, errstr);
382 }
383 pps = calc_pps (&rec->total, &prev->total, t);
384 err = calc_errs_pps(&rec->total, &prev->total, t);
385 printf(fm2_rx, "XDP-RX CPU", "total", pps, err);
386 }
387
388 /* rx_queue_index_map */
389 printf("\n%-15s %-7s %-11s %-11s\n",
390 "RXQ stats", "RXQ:CPU", "pps", "issue-pps");
391
392 for (rxq = 0; rxq < nr_rxqs; rxq++) {
393 char *fmt_rx = "%-15s %3d:%-3d %'-11.0f %'-10.0f %s\n";
394 char *fm2_rx = "%-15s %3d:%-3s %'-11.0f\n";
395 char *errstr = "";
396 int rxq_ = rxq;
397
398 /* Last RXQ in map catch overflows */
399 if (rxq_ == nr_rxqs - 1)
400 rxq_ = -1;
401
402 rec = &stats_rec->rxq[rxq];
403 prev = &stats_prev->rxq[rxq];
404 t = calc_period(rec, prev);
405 for (i = 0; i < nr_cpus; i++) {
406 struct datarec *r = &rec->cpu[i];
407 struct datarec *p = &prev->cpu[i];
408
409 pps = calc_pps (r, p, t);
410 err = calc_errs_pps(r, p, t);
411 if (err > 0) {
412 if (rxq_ == -1)
413 errstr = "map-overflow-RXQ";
414 else
415 errstr = "err";
416 }
417 if (pps > 0)
418 printf(fmt_rx, "rx_queue_index",
419 rxq_, i, pps, err, errstr);
420 }
421 pps = calc_pps (&rec->total, &prev->total, t);
422 err = calc_errs_pps(&rec->total, &prev->total, t);
423 if (pps || err)
424 printf(fm2_rx, "rx_queue_index", rxq_, "sum", pps, err);
425 }
426}
427
428
429/* Pointer swap trick */
430static inline void swap(struct stats_record **a, struct stats_record **b)
431{
432 struct stats_record *tmp;
433
434 tmp = *a;
435 *a = *b;
436 *b = tmp;
437}
438
439static void stats_poll(int interval, int action, __u32 cfg_opt)
440{
441 struct stats_record *record, *prev;
442
443 record = alloc_stats_record();
444 prev = alloc_stats_record();
445 stats_collect(record);
446
447 while (1) {
448 swap(&prev, &record);
449 stats_collect(record);
450 stats_print(record, prev, action, cfg_opt);
451 sleep(interval);
452 }
453
454 free_stats_record(record);
455 free_stats_record(prev);
456}
457
458
459int main(int argc, char **argv)
460{
461 __u32 cfg_options= NO_TOUCH ; /* Default: Don't touch packet memory */
462 struct bpf_prog_info info = {};
463 __u32 info_len = sizeof(info);
464 int prog_fd, map_fd, opt, err;
465 bool use_separators = true;
466 struct config cfg = { 0 };
467 struct bpf_program *prog;
468 struct bpf_object *obj;
469 struct bpf_map *map;
470 char filename[256];
471 int longindex = 0;
472 int interval = 2;
473 __u32 key = 0;
474
475
476 char action_str_buf[XDP_ACTION_MAX_STRLEN + 1 /* for \0 */] = { 0 };
477 int action = XDP_PASS; /* Default action */
478 char *action_str = NULL;
479
480 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
481
482 obj = bpf_object__open_file(filename, NULL);
483 if (libbpf_get_error(obj))
484 return EXIT_FAIL;
485
486 prog = bpf_object__next_program(obj, NULL);
487 bpf_program__set_type(prog, BPF_PROG_TYPE_XDP);
488
489 err = bpf_object__load(obj);
490 if (err)
491 return EXIT_FAIL;
492 prog_fd = bpf_program__fd(prog);
493
494 map = bpf_object__find_map_by_name(obj, "config_map");
495 stats_global_map = bpf_object__find_map_by_name(obj, "stats_global_map");
496 rx_queue_index_map = bpf_object__find_map_by_name(obj, "rx_queue_index_map");
497 if (!map || !stats_global_map || !rx_queue_index_map) {
498 printf("finding a map in obj file failed\n");
499 return EXIT_FAIL;
500 }
501 map_fd = bpf_map__fd(map);
502
503 if (!prog_fd) {
504 fprintf(stderr, "ERR: bpf_prog_load_xattr: %s\n", strerror(errno));
505 return EXIT_FAIL;
506 }
507
508 /* Parse commands line args */
509 while ((opt = getopt_long(argc, argv, "FhSrmzd:s:a:",
510 long_options, &longindex)) != -1) {
511 switch (opt) {
512 case 'd':
513 if (strlen(optarg) >= IF_NAMESIZE) {
514 fprintf(stderr, "ERR: --dev name too long\n");
515 goto error;
516 }
517 ifname = (char *)&ifname_buf;
518 strncpy(ifname, optarg, IF_NAMESIZE);
519 ifindex = if_nametoindex(ifname);
520 if (ifindex == 0) {
521 fprintf(stderr,
522 "ERR: --dev name unknown err(%d):%s\n",
523 errno, strerror(errno));
524 goto error;
525 }
526 break;
527 case 's':
528 interval = atoi(optarg);
529 break;
530 case 'S':
531 xdp_flags |= XDP_FLAGS_SKB_MODE;
532 break;
533 case 'z':
534 use_separators = false;
535 break;
536 case 'a':
537 action_str = (char *)&action_str_buf;
538 strncpy(action_str, optarg, XDP_ACTION_MAX_STRLEN);
539 break;
540 case 'r':
541 cfg_options |= READ_MEM;
542 break;
543 case 'm':
544 cfg_options |= SWAP_MAC;
545 break;
546 case 'F':
547 xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
548 break;
549 case 'h':
550 error:
551 default:
552 usage(argv);
553 return EXIT_FAIL_OPTION;
554 }
555 }
556
557 if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
558 xdp_flags |= XDP_FLAGS_DRV_MODE;
559
560 /* Required option */
561 if (ifindex == -1) {
562 fprintf(stderr, "ERR: required option --dev missing\n");
563 usage(argv);
564 return EXIT_FAIL_OPTION;
565 }
566 cfg.ifindex = ifindex;
567
568 /* Parse action string */
569 if (action_str) {
570 action = parse_xdp_action(action_str);
571 if (action < 0) {
572 fprintf(stderr, "ERR: Invalid XDP --action: %s\n",
573 action_str);
574 list_xdp_actions();
575 return EXIT_FAIL_OPTION;
576 }
577 }
578 cfg.action = action;
579
580 /* XDP_TX requires changing MAC-addrs, else HW may drop */
581 if (action == XDP_TX)
582 cfg_options |= SWAP_MAC;
583 cfg.options = cfg_options;
584
585 /* Trick to pretty printf with thousands separators use %' */
586 if (use_separators)
587 setlocale(LC_NUMERIC, "en_US");
588
589 /* User-side setup ifindex in config_map */
590 err = bpf_map_update_elem(map_fd, &key, &cfg, 0);
591 if (err) {
592 fprintf(stderr, "Store config failed (err:%d)\n", err);
593 exit(EXIT_FAIL_BPF);
594 }
595
596 /* Remove XDP program when program is interrupted or killed */
597 signal(SIGINT, int_exit);
598 signal(SIGTERM, int_exit);
599
600 if (bpf_xdp_attach(ifindex, prog_fd, xdp_flags, NULL) < 0) {
601 fprintf(stderr, "link set xdp fd failed\n");
602 return EXIT_FAIL_XDP;
603 }
604
605 err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
606 if (err) {
607 printf("can't get prog info - %s\n", strerror(errno));
608 return err;
609 }
610 prog_id = info.id;
611
612 stats_poll(interval, action, cfg_options);
613 return EXIT_OK;
614}
1/* SPDX-License-Identifier: GPL-2.0
2 * Copyright (c) 2017 Jesper Dangaard Brouer, Red Hat Inc.
3 */
4static const char *__doc__ = " XDP RX-queue info extract example\n\n"
5 "Monitor how many packets per sec (pps) are received\n"
6 "per NIC RX queue index and which CPU processed the packet\n"
7 ;
8
9#include <errno.h>
10#include <signal.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <stdbool.h>
14#include <string.h>
15#include <unistd.h>
16#include <locale.h>
17#include <sys/resource.h>
18#include <getopt.h>
19#include <net/if.h>
20#include <time.h>
21
22#include <arpa/inet.h>
23#include <linux/if_link.h>
24
25#include <bpf/bpf.h>
26#include <bpf/libbpf.h>
27#include "bpf_util.h"
28
29static int ifindex = -1;
30static char ifname_buf[IF_NAMESIZE];
31static char *ifname;
32static __u32 prog_id;
33
34static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
35
36static struct bpf_map *stats_global_map;
37static struct bpf_map *rx_queue_index_map;
38
39/* Exit return codes */
40#define EXIT_OK 0
41#define EXIT_FAIL 1
42#define EXIT_FAIL_OPTION 2
43#define EXIT_FAIL_XDP 3
44#define EXIT_FAIL_BPF 4
45#define EXIT_FAIL_MEM 5
46
47static const struct option long_options[] = {
48 {"help", no_argument, NULL, 'h' },
49 {"dev", required_argument, NULL, 'd' },
50 {"skb-mode", no_argument, NULL, 'S' },
51 {"sec", required_argument, NULL, 's' },
52 {"no-separators", no_argument, NULL, 'z' },
53 {"action", required_argument, NULL, 'a' },
54 {"readmem", no_argument, NULL, 'r' },
55 {"swapmac", no_argument, NULL, 'm' },
56 {"force", no_argument, NULL, 'F' },
57 {0, 0, NULL, 0 }
58};
59
60static void int_exit(int sig)
61{
62 __u32 curr_prog_id = 0;
63
64 if (ifindex > -1) {
65 if (bpf_get_link_xdp_id(ifindex, &curr_prog_id, xdp_flags)) {
66 printf("bpf_get_link_xdp_id failed\n");
67 exit(EXIT_FAIL);
68 }
69 if (prog_id == curr_prog_id) {
70 fprintf(stderr,
71 "Interrupted: Removing XDP program on ifindex:%d device:%s\n",
72 ifindex, ifname);
73 bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
74 } else if (!curr_prog_id) {
75 printf("couldn't find a prog id on a given iface\n");
76 } else {
77 printf("program on interface changed, not removing\n");
78 }
79 }
80 exit(EXIT_OK);
81}
82
83struct config {
84 __u32 action;
85 int ifindex;
86 __u32 options;
87};
88enum cfg_options_flags {
89 NO_TOUCH = 0x0U,
90 READ_MEM = 0x1U,
91 SWAP_MAC = 0x2U,
92};
93#define XDP_ACTION_MAX (XDP_TX + 1)
94#define XDP_ACTION_MAX_STRLEN 11
95static const char *xdp_action_names[XDP_ACTION_MAX] = {
96 [XDP_ABORTED] = "XDP_ABORTED",
97 [XDP_DROP] = "XDP_DROP",
98 [XDP_PASS] = "XDP_PASS",
99 [XDP_TX] = "XDP_TX",
100};
101
102static const char *action2str(int action)
103{
104 if (action < XDP_ACTION_MAX)
105 return xdp_action_names[action];
106 return NULL;
107}
108
109static int parse_xdp_action(char *action_str)
110{
111 size_t maxlen;
112 __u64 action = -1;
113 int i;
114
115 for (i = 0; i < XDP_ACTION_MAX; i++) {
116 maxlen = XDP_ACTION_MAX_STRLEN;
117 if (strncmp(xdp_action_names[i], action_str, maxlen) == 0) {
118 action = i;
119 break;
120 }
121 }
122 return action;
123}
124
125static void list_xdp_actions(void)
126{
127 int i;
128
129 printf("Available XDP --action <options>\n");
130 for (i = 0; i < XDP_ACTION_MAX; i++)
131 printf("\t%s\n", xdp_action_names[i]);
132 printf("\n");
133}
134
135static char* options2str(enum cfg_options_flags flag)
136{
137 if (flag == NO_TOUCH)
138 return "no_touch";
139 if (flag & SWAP_MAC)
140 return "swapmac";
141 if (flag & READ_MEM)
142 return "read";
143 fprintf(stderr, "ERR: Unknown config option flags");
144 exit(EXIT_FAIL);
145}
146
147static void usage(char *argv[])
148{
149 int i;
150
151 printf("\nDOCUMENTATION:\n%s\n", __doc__);
152 printf(" Usage: %s (options-see-below)\n", argv[0]);
153 printf(" Listing options:\n");
154 for (i = 0; long_options[i].name != 0; i++) {
155 printf(" --%-12s", long_options[i].name);
156 if (long_options[i].flag != NULL)
157 printf(" flag (internal value:%d)",
158 *long_options[i].flag);
159 else
160 printf(" short-option: -%c",
161 long_options[i].val);
162 printf("\n");
163 }
164 printf("\n");
165 list_xdp_actions();
166}
167
168#define NANOSEC_PER_SEC 1000000000 /* 10^9 */
169static __u64 gettime(void)
170{
171 struct timespec t;
172 int res;
173
174 res = clock_gettime(CLOCK_MONOTONIC, &t);
175 if (res < 0) {
176 fprintf(stderr, "Error with gettimeofday! (%i)\n", res);
177 exit(EXIT_FAIL);
178 }
179 return (__u64) t.tv_sec * NANOSEC_PER_SEC + t.tv_nsec;
180}
181
182/* Common stats data record shared with _kern.c */
183struct datarec {
184 __u64 processed;
185 __u64 issue;
186};
187struct record {
188 __u64 timestamp;
189 struct datarec total;
190 struct datarec *cpu;
191};
192struct stats_record {
193 struct record stats;
194 struct record *rxq;
195};
196
197static struct datarec *alloc_record_per_cpu(void)
198{
199 unsigned int nr_cpus = bpf_num_possible_cpus();
200 struct datarec *array;
201
202 array = calloc(nr_cpus, sizeof(struct datarec));
203 if (!array) {
204 fprintf(stderr, "Mem alloc error (nr_cpus:%u)\n", nr_cpus);
205 exit(EXIT_FAIL_MEM);
206 }
207 return array;
208}
209
210static struct record *alloc_record_per_rxq(void)
211{
212 unsigned int nr_rxqs = bpf_map__def(rx_queue_index_map)->max_entries;
213 struct record *array;
214
215 array = calloc(nr_rxqs, sizeof(struct record));
216 if (!array) {
217 fprintf(stderr, "Mem alloc error (nr_rxqs:%u)\n", nr_rxqs);
218 exit(EXIT_FAIL_MEM);
219 }
220 return array;
221}
222
223static struct stats_record *alloc_stats_record(void)
224{
225 unsigned int nr_rxqs = bpf_map__def(rx_queue_index_map)->max_entries;
226 struct stats_record *rec;
227 int i;
228
229 rec = calloc(1, sizeof(struct stats_record));
230 if (!rec) {
231 fprintf(stderr, "Mem alloc error\n");
232 exit(EXIT_FAIL_MEM);
233 }
234 rec->rxq = alloc_record_per_rxq();
235 for (i = 0; i < nr_rxqs; i++)
236 rec->rxq[i].cpu = alloc_record_per_cpu();
237
238 rec->stats.cpu = alloc_record_per_cpu();
239 return rec;
240}
241
242static void free_stats_record(struct stats_record *r)
243{
244 unsigned int nr_rxqs = bpf_map__def(rx_queue_index_map)->max_entries;
245 int i;
246
247 for (i = 0; i < nr_rxqs; i++)
248 free(r->rxq[i].cpu);
249
250 free(r->rxq);
251 free(r->stats.cpu);
252 free(r);
253}
254
255static bool map_collect_percpu(int fd, __u32 key, struct record *rec)
256{
257 /* For percpu maps, userspace gets a value per possible CPU */
258 unsigned int nr_cpus = bpf_num_possible_cpus();
259 struct datarec values[nr_cpus];
260 __u64 sum_processed = 0;
261 __u64 sum_issue = 0;
262 int i;
263
264 if ((bpf_map_lookup_elem(fd, &key, values)) != 0) {
265 fprintf(stderr,
266 "ERR: bpf_map_lookup_elem failed key:0x%X\n", key);
267 return false;
268 }
269 /* Get time as close as possible to reading map contents */
270 rec->timestamp = gettime();
271
272 /* Record and sum values from each CPU */
273 for (i = 0; i < nr_cpus; i++) {
274 rec->cpu[i].processed = values[i].processed;
275 sum_processed += values[i].processed;
276 rec->cpu[i].issue = values[i].issue;
277 sum_issue += values[i].issue;
278 }
279 rec->total.processed = sum_processed;
280 rec->total.issue = sum_issue;
281 return true;
282}
283
284static void stats_collect(struct stats_record *rec)
285{
286 int fd, i, max_rxqs;
287
288 fd = bpf_map__fd(stats_global_map);
289 map_collect_percpu(fd, 0, &rec->stats);
290
291 fd = bpf_map__fd(rx_queue_index_map);
292 max_rxqs = bpf_map__def(rx_queue_index_map)->max_entries;
293 for (i = 0; i < max_rxqs; i++)
294 map_collect_percpu(fd, i, &rec->rxq[i]);
295}
296
297static double calc_period(struct record *r, struct record *p)
298{
299 double period_ = 0;
300 __u64 period = 0;
301
302 period = r->timestamp - p->timestamp;
303 if (period > 0)
304 period_ = ((double) period / NANOSEC_PER_SEC);
305
306 return period_;
307}
308
309static __u64 calc_pps(struct datarec *r, struct datarec *p, double period_)
310{
311 __u64 packets = 0;
312 __u64 pps = 0;
313
314 if (period_ > 0) {
315 packets = r->processed - p->processed;
316 pps = packets / period_;
317 }
318 return pps;
319}
320
321static __u64 calc_errs_pps(struct datarec *r,
322 struct datarec *p, double period_)
323{
324 __u64 packets = 0;
325 __u64 pps = 0;
326
327 if (period_ > 0) {
328 packets = r->issue - p->issue;
329 pps = packets / period_;
330 }
331 return pps;
332}
333
334static void stats_print(struct stats_record *stats_rec,
335 struct stats_record *stats_prev,
336 int action, __u32 cfg_opt)
337{
338 unsigned int nr_rxqs = bpf_map__def(rx_queue_index_map)->max_entries;
339 unsigned int nr_cpus = bpf_num_possible_cpus();
340 double pps = 0, err = 0;
341 struct record *rec, *prev;
342 double t;
343 int rxq;
344 int i;
345
346 /* Header */
347 printf("\nRunning XDP on dev:%s (ifindex:%d) action:%s options:%s\n",
348 ifname, ifindex, action2str(action), options2str(cfg_opt));
349
350 /* stats_global_map */
351 {
352 char *fmt_rx = "%-15s %-7d %'-11.0f %'-10.0f %s\n";
353 char *fm2_rx = "%-15s %-7s %'-11.0f\n";
354 char *errstr = "";
355
356 printf("%-15s %-7s %-11s %-11s\n",
357 "XDP stats", "CPU", "pps", "issue-pps");
358
359 rec = &stats_rec->stats;
360 prev = &stats_prev->stats;
361 t = calc_period(rec, prev);
362 for (i = 0; i < nr_cpus; i++) {
363 struct datarec *r = &rec->cpu[i];
364 struct datarec *p = &prev->cpu[i];
365
366 pps = calc_pps (r, p, t);
367 err = calc_errs_pps(r, p, t);
368 if (err > 0)
369 errstr = "invalid-ifindex";
370 if (pps > 0)
371 printf(fmt_rx, "XDP-RX CPU",
372 i, pps, err, errstr);
373 }
374 pps = calc_pps (&rec->total, &prev->total, t);
375 err = calc_errs_pps(&rec->total, &prev->total, t);
376 printf(fm2_rx, "XDP-RX CPU", "total", pps, err);
377 }
378
379 /* rx_queue_index_map */
380 printf("\n%-15s %-7s %-11s %-11s\n",
381 "RXQ stats", "RXQ:CPU", "pps", "issue-pps");
382
383 for (rxq = 0; rxq < nr_rxqs; rxq++) {
384 char *fmt_rx = "%-15s %3d:%-3d %'-11.0f %'-10.0f %s\n";
385 char *fm2_rx = "%-15s %3d:%-3s %'-11.0f\n";
386 char *errstr = "";
387 int rxq_ = rxq;
388
389 /* Last RXQ in map catch overflows */
390 if (rxq_ == nr_rxqs - 1)
391 rxq_ = -1;
392
393 rec = &stats_rec->rxq[rxq];
394 prev = &stats_prev->rxq[rxq];
395 t = calc_period(rec, prev);
396 for (i = 0; i < nr_cpus; i++) {
397 struct datarec *r = &rec->cpu[i];
398 struct datarec *p = &prev->cpu[i];
399
400 pps = calc_pps (r, p, t);
401 err = calc_errs_pps(r, p, t);
402 if (err > 0) {
403 if (rxq_ == -1)
404 errstr = "map-overflow-RXQ";
405 else
406 errstr = "err";
407 }
408 if (pps > 0)
409 printf(fmt_rx, "rx_queue_index",
410 rxq_, i, pps, err, errstr);
411 }
412 pps = calc_pps (&rec->total, &prev->total, t);
413 err = calc_errs_pps(&rec->total, &prev->total, t);
414 if (pps || err)
415 printf(fm2_rx, "rx_queue_index", rxq_, "sum", pps, err);
416 }
417}
418
419
420/* Pointer swap trick */
421static inline void swap(struct stats_record **a, struct stats_record **b)
422{
423 struct stats_record *tmp;
424
425 tmp = *a;
426 *a = *b;
427 *b = tmp;
428}
429
430static void stats_poll(int interval, int action, __u32 cfg_opt)
431{
432 struct stats_record *record, *prev;
433
434 record = alloc_stats_record();
435 prev = alloc_stats_record();
436 stats_collect(record);
437
438 while (1) {
439 swap(&prev, &record);
440 stats_collect(record);
441 stats_print(record, prev, action, cfg_opt);
442 sleep(interval);
443 }
444
445 free_stats_record(record);
446 free_stats_record(prev);
447}
448
449
450int main(int argc, char **argv)
451{
452 __u32 cfg_options= NO_TOUCH ; /* Default: Don't touch packet memory */
453 struct bpf_prog_load_attr prog_load_attr = {
454 .prog_type = BPF_PROG_TYPE_XDP,
455 };
456 struct bpf_prog_info info = {};
457 __u32 info_len = sizeof(info);
458 int prog_fd, map_fd, opt, err;
459 bool use_separators = true;
460 struct config cfg = { 0 };
461 struct bpf_object *obj;
462 struct bpf_map *map;
463 char filename[256];
464 int longindex = 0;
465 int interval = 2;
466 __u32 key = 0;
467
468
469 char action_str_buf[XDP_ACTION_MAX_STRLEN + 1 /* for \0 */] = { 0 };
470 int action = XDP_PASS; /* Default action */
471 char *action_str = NULL;
472
473 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
474 prog_load_attr.file = filename;
475
476 if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
477 return EXIT_FAIL;
478
479 map = bpf_object__find_map_by_name(obj, "config_map");
480 stats_global_map = bpf_object__find_map_by_name(obj, "stats_global_map");
481 rx_queue_index_map = bpf_object__find_map_by_name(obj, "rx_queue_index_map");
482 if (!map || !stats_global_map || !rx_queue_index_map) {
483 printf("finding a map in obj file failed\n");
484 return EXIT_FAIL;
485 }
486 map_fd = bpf_map__fd(map);
487
488 if (!prog_fd) {
489 fprintf(stderr, "ERR: bpf_prog_load_xattr: %s\n", strerror(errno));
490 return EXIT_FAIL;
491 }
492
493 /* Parse commands line args */
494 while ((opt = getopt_long(argc, argv, "FhSrmzd:s:a:",
495 long_options, &longindex)) != -1) {
496 switch (opt) {
497 case 'd':
498 if (strlen(optarg) >= IF_NAMESIZE) {
499 fprintf(stderr, "ERR: --dev name too long\n");
500 goto error;
501 }
502 ifname = (char *)&ifname_buf;
503 strncpy(ifname, optarg, IF_NAMESIZE);
504 ifindex = if_nametoindex(ifname);
505 if (ifindex == 0) {
506 fprintf(stderr,
507 "ERR: --dev name unknown err(%d):%s\n",
508 errno, strerror(errno));
509 goto error;
510 }
511 break;
512 case 's':
513 interval = atoi(optarg);
514 break;
515 case 'S':
516 xdp_flags |= XDP_FLAGS_SKB_MODE;
517 break;
518 case 'z':
519 use_separators = false;
520 break;
521 case 'a':
522 action_str = (char *)&action_str_buf;
523 strncpy(action_str, optarg, XDP_ACTION_MAX_STRLEN);
524 break;
525 case 'r':
526 cfg_options |= READ_MEM;
527 break;
528 case 'm':
529 cfg_options |= SWAP_MAC;
530 break;
531 case 'F':
532 xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
533 break;
534 case 'h':
535 error:
536 default:
537 usage(argv);
538 return EXIT_FAIL_OPTION;
539 }
540 }
541
542 if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
543 xdp_flags |= XDP_FLAGS_DRV_MODE;
544
545 /* Required option */
546 if (ifindex == -1) {
547 fprintf(stderr, "ERR: required option --dev missing\n");
548 usage(argv);
549 return EXIT_FAIL_OPTION;
550 }
551 cfg.ifindex = ifindex;
552
553 /* Parse action string */
554 if (action_str) {
555 action = parse_xdp_action(action_str);
556 if (action < 0) {
557 fprintf(stderr, "ERR: Invalid XDP --action: %s\n",
558 action_str);
559 list_xdp_actions();
560 return EXIT_FAIL_OPTION;
561 }
562 }
563 cfg.action = action;
564
565 /* XDP_TX requires changing MAC-addrs, else HW may drop */
566 if (action == XDP_TX)
567 cfg_options |= SWAP_MAC;
568 cfg.options = cfg_options;
569
570 /* Trick to pretty printf with thousands separators use %' */
571 if (use_separators)
572 setlocale(LC_NUMERIC, "en_US");
573
574 /* User-side setup ifindex in config_map */
575 err = bpf_map_update_elem(map_fd, &key, &cfg, 0);
576 if (err) {
577 fprintf(stderr, "Store config failed (err:%d)\n", err);
578 exit(EXIT_FAIL_BPF);
579 }
580
581 /* Remove XDP program when program is interrupted or killed */
582 signal(SIGINT, int_exit);
583 signal(SIGTERM, int_exit);
584
585 if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) {
586 fprintf(stderr, "link set xdp fd failed\n");
587 return EXIT_FAIL_XDP;
588 }
589
590 err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
591 if (err) {
592 printf("can't get prog info - %s\n", strerror(errno));
593 return err;
594 }
595 prog_id = info.id;
596
597 stats_poll(interval, action, cfg_options);
598 return EXIT_OK;
599}