1 /* SPDX-License-Identifier: GPL-2.0
2 * Copyright (c) 2017 Jesper Dangaard Brouer, Red Hat Inc.
4 static 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"
17 #include <sys/resource.h>
22 #include <arpa/inet.h>
23 #include <linux/if_link.h>
26 #include "bpf/libbpf.h"
29 static int ifindex
= -1;
30 static char ifname_buf
[IF_NAMESIZE
];
33 static __u32 xdp_flags
;
35 static struct bpf_map
*stats_global_map
;
36 static struct bpf_map
*rx_queue_index_map
;
38 /* Exit return codes */
41 #define EXIT_FAIL_OPTION 2
42 #define EXIT_FAIL_XDP 3
43 #define EXIT_FAIL_BPF 4
44 #define EXIT_FAIL_MEM 5
46 static const struct option long_options
[] = {
47 {"help", no_argument
, NULL
, 'h' },
48 {"dev", required_argument
, NULL
, 'd' },
49 {"skb-mode", no_argument
, NULL
, 'S' },
50 {"sec", required_argument
, NULL
, 's' },
51 {"no-separators", no_argument
, NULL
, 'z' },
52 {"action", required_argument
, NULL
, 'a' },
53 {"readmem", no_argument
, NULL
, 'r' },
54 {"swapmac", no_argument
, NULL
, 'm' },
58 static void int_exit(int sig
)
61 "Interrupted: Removing XDP program on ifindex:%d device:%s\n",
64 bpf_set_link_xdp_fd(ifindex
, -1, xdp_flags
);
73 enum cfg_options_flags
{
78 #define XDP_ACTION_MAX (XDP_TX + 1)
79 #define XDP_ACTION_MAX_STRLEN 11
80 static const char *xdp_action_names
[XDP_ACTION_MAX
] = {
81 [XDP_ABORTED
] = "XDP_ABORTED",
82 [XDP_DROP
] = "XDP_DROP",
83 [XDP_PASS
] = "XDP_PASS",
87 static const char *action2str(int action
)
89 if (action
< XDP_ACTION_MAX
)
90 return xdp_action_names
[action
];
94 static int parse_xdp_action(char *action_str
)
100 for (i
= 0; i
< XDP_ACTION_MAX
; i
++) {
101 maxlen
= XDP_ACTION_MAX_STRLEN
;
102 if (strncmp(xdp_action_names
[i
], action_str
, maxlen
) == 0) {
110 static void list_xdp_actions(void)
114 printf("Available XDP --action <options>\n");
115 for (i
= 0; i
< XDP_ACTION_MAX
; i
++)
116 printf("\t%s\n", xdp_action_names
[i
]);
120 static char* options2str(enum cfg_options_flags flag
)
122 if (flag
== NO_TOUCH
)
128 fprintf(stderr
, "ERR: Unknown config option flags");
132 static void usage(char *argv
[])
136 printf("\nDOCUMENTATION:\n%s\n", __doc__
);
137 printf(" Usage: %s (options-see-below)\n", argv
[0]);
138 printf(" Listing options:\n");
139 for (i
= 0; long_options
[i
].name
!= 0; i
++) {
140 printf(" --%-12s", long_options
[i
].name
);
141 if (long_options
[i
].flag
!= NULL
)
142 printf(" flag (internal value:%d)",
143 *long_options
[i
].flag
);
145 printf(" short-option: -%c",
146 long_options
[i
].val
);
153 #define NANOSEC_PER_SEC 1000000000 /* 10^9 */
154 static __u64
gettime(void)
159 res
= clock_gettime(CLOCK_MONOTONIC
, &t
);
161 fprintf(stderr
, "Error with gettimeofday! (%i)\n", res
);
164 return (__u64
) t
.tv_sec
* NANOSEC_PER_SEC
+ t
.tv_nsec
;
167 /* Common stats data record shared with _kern.c */
174 struct datarec total
;
177 struct stats_record
{
182 static struct datarec
*alloc_record_per_cpu(void)
184 unsigned int nr_cpus
= bpf_num_possible_cpus();
185 struct datarec
*array
;
188 size
= sizeof(struct datarec
) * nr_cpus
;
189 array
= malloc(size
);
190 memset(array
, 0, size
);
192 fprintf(stderr
, "Mem alloc error (nr_cpus:%u)\n", nr_cpus
);
198 static struct record
*alloc_record_per_rxq(void)
200 unsigned int nr_rxqs
= bpf_map__def(rx_queue_index_map
)->max_entries
;
201 struct record
*array
;
204 size
= sizeof(struct record
) * nr_rxqs
;
205 array
= malloc(size
);
206 memset(array
, 0, size
);
208 fprintf(stderr
, "Mem alloc error (nr_rxqs:%u)\n", nr_rxqs
);
214 static struct stats_record
*alloc_stats_record(void)
216 unsigned int nr_rxqs
= bpf_map__def(rx_queue_index_map
)->max_entries
;
217 struct stats_record
*rec
;
220 rec
= malloc(sizeof(*rec
));
221 memset(rec
, 0, sizeof(*rec
));
223 fprintf(stderr
, "Mem alloc error\n");
226 rec
->rxq
= alloc_record_per_rxq();
227 for (i
= 0; i
< nr_rxqs
; i
++)
228 rec
->rxq
[i
].cpu
= alloc_record_per_cpu();
230 rec
->stats
.cpu
= alloc_record_per_cpu();
234 static void free_stats_record(struct stats_record
*r
)
236 unsigned int nr_rxqs
= bpf_map__def(rx_queue_index_map
)->max_entries
;
239 for (i
= 0; i
< nr_rxqs
; i
++)
247 static bool map_collect_percpu(int fd
, __u32 key
, struct record
*rec
)
249 /* For percpu maps, userspace gets a value per possible CPU */
250 unsigned int nr_cpus
= bpf_num_possible_cpus();
251 struct datarec values
[nr_cpus
];
252 __u64 sum_processed
= 0;
256 if ((bpf_map_lookup_elem(fd
, &key
, values
)) != 0) {
258 "ERR: bpf_map_lookup_elem failed key:0x%X\n", key
);
261 /* Get time as close as possible to reading map contents */
262 rec
->timestamp
= gettime();
264 /* Record and sum values from each CPU */
265 for (i
= 0; i
< nr_cpus
; i
++) {
266 rec
->cpu
[i
].processed
= values
[i
].processed
;
267 sum_processed
+= values
[i
].processed
;
268 rec
->cpu
[i
].issue
= values
[i
].issue
;
269 sum_issue
+= values
[i
].issue
;
271 rec
->total
.processed
= sum_processed
;
272 rec
->total
.issue
= sum_issue
;
276 static void stats_collect(struct stats_record
*rec
)
280 fd
= bpf_map__fd(stats_global_map
);
281 map_collect_percpu(fd
, 0, &rec
->stats
);
283 fd
= bpf_map__fd(rx_queue_index_map
);
284 max_rxqs
= bpf_map__def(rx_queue_index_map
)->max_entries
;
285 for (i
= 0; i
< max_rxqs
; i
++)
286 map_collect_percpu(fd
, i
, &rec
->rxq
[i
]);
289 static double calc_period(struct record
*r
, struct record
*p
)
294 period
= r
->timestamp
- p
->timestamp
;
296 period_
= ((double) period
/ NANOSEC_PER_SEC
);
301 static __u64
calc_pps(struct datarec
*r
, struct datarec
*p
, double period_
)
307 packets
= r
->processed
- p
->processed
;
308 pps
= packets
/ period_
;
313 static __u64
calc_errs_pps(struct datarec
*r
,
314 struct datarec
*p
, double period_
)
320 packets
= r
->issue
- p
->issue
;
321 pps
= packets
/ period_
;
326 static void stats_print(struct stats_record
*stats_rec
,
327 struct stats_record
*stats_prev
,
328 int action
, __u32 cfg_opt
)
330 unsigned int nr_rxqs
= bpf_map__def(rx_queue_index_map
)->max_entries
;
331 unsigned int nr_cpus
= bpf_num_possible_cpus();
332 double pps
= 0, err
= 0;
333 struct record
*rec
, *prev
;
339 printf("\nRunning XDP on dev:%s (ifindex:%d) action:%s options:%s\n",
340 ifname
, ifindex
, action2str(action
), options2str(cfg_opt
));
342 /* stats_global_map */
344 char *fmt_rx
= "%-15s %-7d %'-11.0f %'-10.0f %s\n";
345 char *fm2_rx
= "%-15s %-7s %'-11.0f\n";
348 printf("%-15s %-7s %-11s %-11s\n",
349 "XDP stats", "CPU", "pps", "issue-pps");
351 rec
= &stats_rec
->stats
;
352 prev
= &stats_prev
->stats
;
353 t
= calc_period(rec
, prev
);
354 for (i
= 0; i
< nr_cpus
; i
++) {
355 struct datarec
*r
= &rec
->cpu
[i
];
356 struct datarec
*p
= &prev
->cpu
[i
];
358 pps
= calc_pps (r
, p
, t
);
359 err
= calc_errs_pps(r
, p
, t
);
361 errstr
= "invalid-ifindex";
363 printf(fmt_rx
, "XDP-RX CPU",
364 i
, pps
, err
, errstr
);
366 pps
= calc_pps (&rec
->total
, &prev
->total
, t
);
367 err
= calc_errs_pps(&rec
->total
, &prev
->total
, t
);
368 printf(fm2_rx
, "XDP-RX CPU", "total", pps
, err
);
371 /* rx_queue_index_map */
372 printf("\n%-15s %-7s %-11s %-11s\n",
373 "RXQ stats", "RXQ:CPU", "pps", "issue-pps");
375 for (rxq
= 0; rxq
< nr_rxqs
; rxq
++) {
376 char *fmt_rx
= "%-15s %3d:%-3d %'-11.0f %'-10.0f %s\n";
377 char *fm2_rx
= "%-15s %3d:%-3s %'-11.0f\n";
381 /* Last RXQ in map catch overflows */
382 if (rxq_
== nr_rxqs
- 1)
385 rec
= &stats_rec
->rxq
[rxq
];
386 prev
= &stats_prev
->rxq
[rxq
];
387 t
= calc_period(rec
, prev
);
388 for (i
= 0; i
< nr_cpus
; i
++) {
389 struct datarec
*r
= &rec
->cpu
[i
];
390 struct datarec
*p
= &prev
->cpu
[i
];
392 pps
= calc_pps (r
, p
, t
);
393 err
= calc_errs_pps(r
, p
, t
);
396 errstr
= "map-overflow-RXQ";
401 printf(fmt_rx
, "rx_queue_index",
402 rxq_
, i
, pps
, err
, errstr
);
404 pps
= calc_pps (&rec
->total
, &prev
->total
, t
);
405 err
= calc_errs_pps(&rec
->total
, &prev
->total
, t
);
407 printf(fm2_rx
, "rx_queue_index", rxq_
, "sum", pps
, err
);
412 /* Pointer swap trick */
413 static inline void swap(struct stats_record
**a
, struct stats_record
**b
)
415 struct stats_record
*tmp
;
422 static void stats_poll(int interval
, int action
, __u32 cfg_opt
)
424 struct stats_record
*record
, *prev
;
426 record
= alloc_stats_record();
427 prev
= alloc_stats_record();
428 stats_collect(record
);
431 swap(&prev
, &record
);
432 stats_collect(record
);
433 stats_print(record
, prev
, action
, cfg_opt
);
437 free_stats_record(record
);
438 free_stats_record(prev
);
442 int main(int argc
, char **argv
)
444 __u32 cfg_options
= NO_TOUCH
; /* Default: Don't touch packet memory */
445 struct rlimit r
= {10 * 1024 * 1024, RLIM_INFINITY
};
446 struct bpf_prog_load_attr prog_load_attr
= {
447 .prog_type
= BPF_PROG_TYPE_XDP
,
449 int prog_fd
, map_fd
, opt
, err
;
450 bool use_separators
= true;
451 struct config cfg
= { 0 };
452 struct bpf_object
*obj
;
460 char action_str_buf
[XDP_ACTION_MAX_STRLEN
+ 1 /* for \0 */] = { 0 };
461 int action
= XDP_PASS
; /* Default action */
462 char *action_str
= NULL
;
464 snprintf(filename
, sizeof(filename
), "%s_kern.o", argv
[0]);
465 prog_load_attr
.file
= filename
;
467 if (setrlimit(RLIMIT_MEMLOCK
, &r
)) {
468 perror("setrlimit(RLIMIT_MEMLOCK)");
472 if (bpf_prog_load_xattr(&prog_load_attr
, &obj
, &prog_fd
))
475 map
= bpf_object__find_map_by_name(obj
, "config_map");
476 stats_global_map
= bpf_object__find_map_by_name(obj
, "stats_global_map");
477 rx_queue_index_map
= bpf_object__find_map_by_name(obj
, "rx_queue_index_map");
478 if (!map
|| !stats_global_map
|| !rx_queue_index_map
) {
479 printf("finding a map in obj file failed\n");
482 map_fd
= bpf_map__fd(map
);
485 fprintf(stderr
, "ERR: load_bpf_file: %s\n", strerror(errno
));
489 /* Parse commands line args */
490 while ((opt
= getopt_long(argc
, argv
, "hSd:",
491 long_options
, &longindex
)) != -1) {
494 if (strlen(optarg
) >= IF_NAMESIZE
) {
495 fprintf(stderr
, "ERR: --dev name too long\n");
498 ifname
= (char *)&ifname_buf
;
499 strncpy(ifname
, optarg
, IF_NAMESIZE
);
500 ifindex
= if_nametoindex(ifname
);
503 "ERR: --dev name unknown err(%d):%s\n",
504 errno
, strerror(errno
));
509 interval
= atoi(optarg
);
512 xdp_flags
|= XDP_FLAGS_SKB_MODE
;
515 use_separators
= false;
518 action_str
= (char *)&action_str_buf
;
519 strncpy(action_str
, optarg
, XDP_ACTION_MAX_STRLEN
);
522 cfg_options
|= READ_MEM
;
525 cfg_options
|= SWAP_MAC
;
531 return EXIT_FAIL_OPTION
;
534 /* Required option */
536 fprintf(stderr
, "ERR: required option --dev missing\n");
538 return EXIT_FAIL_OPTION
;
540 cfg
.ifindex
= ifindex
;
542 /* Parse action string */
544 action
= parse_xdp_action(action_str
);
546 fprintf(stderr
, "ERR: Invalid XDP --action: %s\n",
549 return EXIT_FAIL_OPTION
;
554 /* XDP_TX requires changing MAC-addrs, else HW may drop */
555 if (action
== XDP_TX
)
556 cfg_options
|= SWAP_MAC
;
557 cfg
.options
= cfg_options
;
559 /* Trick to pretty printf with thousands separators use %' */
561 setlocale(LC_NUMERIC
, "en_US");
563 /* User-side setup ifindex in config_map */
564 err
= bpf_map_update_elem(map_fd
, &key
, &cfg
, 0);
566 fprintf(stderr
, "Store config failed (err:%d)\n", err
);
570 /* Remove XDP program when program is interrupted or killed */
571 signal(SIGINT
, int_exit
);
572 signal(SIGTERM
, int_exit
);
574 if (bpf_set_link_xdp_fd(ifindex
, prog_fd
, xdp_flags
) < 0) {
575 fprintf(stderr
, "link set xdp fd failed\n");
576 return EXIT_FAIL_XDP
;
579 stats_poll(interval
, action
, cfg_options
);