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>
29 static int ifindex
= -1;
30 static char ifname_buf
[IF_NAMESIZE
];
33 static __u32 xdp_flags
;
35 /* Exit return codes */
38 #define EXIT_FAIL_OPTION 2
39 #define EXIT_FAIL_XDP 3
40 #define EXIT_FAIL_BPF 4
41 #define EXIT_FAIL_MEM 5
43 static const struct option long_options
[] = {
44 {"help", no_argument
, NULL
, 'h' },
45 {"dev", required_argument
, NULL
, 'd' },
46 {"skb-mode", no_argument
, NULL
, 'S' },
47 {"sec", required_argument
, NULL
, 's' },
48 {"no-separators", no_argument
, NULL
, 'z' },
49 {"action", required_argument
, NULL
, 'a' },
53 static void int_exit(int sig
)
56 "Interrupted: Removing XDP program on ifindex:%d device:%s\n",
59 bpf_set_link_xdp_fd(ifindex
, -1, xdp_flags
);
67 #define XDP_ACTION_MAX (XDP_TX + 1)
68 #define XDP_ACTION_MAX_STRLEN 11
69 static const char *xdp_action_names
[XDP_ACTION_MAX
] = {
70 [XDP_ABORTED
] = "XDP_ABORTED",
71 [XDP_DROP
] = "XDP_DROP",
72 [XDP_PASS
] = "XDP_PASS",
76 static const char *action2str(int action
)
78 if (action
< XDP_ACTION_MAX
)
79 return xdp_action_names
[action
];
83 static int parse_xdp_action(char *action_str
)
89 for (i
= 0; i
< XDP_ACTION_MAX
; i
++) {
90 maxlen
= XDP_ACTION_MAX_STRLEN
;
91 if (strncmp(xdp_action_names
[i
], action_str
, maxlen
) == 0) {
99 static void list_xdp_actions(void)
103 printf("Available XDP --action <options>\n");
104 for (i
= 0; i
< XDP_ACTION_MAX
; i
++)
105 printf("\t%s\n", xdp_action_names
[i
]);
109 static void usage(char *argv
[])
113 printf("\nDOCUMENTATION:\n%s\n", __doc__
);
114 printf(" Usage: %s (options-see-below)\n", argv
[0]);
115 printf(" Listing options:\n");
116 for (i
= 0; long_options
[i
].name
!= 0; i
++) {
117 printf(" --%-12s", long_options
[i
].name
);
118 if (long_options
[i
].flag
!= NULL
)
119 printf(" flag (internal value:%d)",
120 *long_options
[i
].flag
);
122 printf(" short-option: -%c",
123 long_options
[i
].val
);
130 #define NANOSEC_PER_SEC 1000000000 /* 10^9 */
131 static __u64
gettime(void)
136 res
= clock_gettime(CLOCK_MONOTONIC
, &t
);
138 fprintf(stderr
, "Error with gettimeofday! (%i)\n", res
);
141 return (__u64
) t
.tv_sec
* NANOSEC_PER_SEC
+ t
.tv_nsec
;
144 /* Common stats data record shared with _kern.c */
151 struct datarec total
;
154 struct stats_record
{
159 static struct datarec
*alloc_record_per_cpu(void)
161 unsigned int nr_cpus
= bpf_num_possible_cpus();
162 struct datarec
*array
;
165 size
= sizeof(struct datarec
) * nr_cpus
;
166 array
= malloc(size
);
167 memset(array
, 0, size
);
169 fprintf(stderr
, "Mem alloc error (nr_cpus:%u)\n", nr_cpus
);
175 static struct record
*alloc_record_per_rxq(void)
177 unsigned int nr_rxqs
= map_data
[2].def
.max_entries
;
178 struct record
*array
;
181 size
= sizeof(struct record
) * nr_rxqs
;
182 array
= malloc(size
);
183 memset(array
, 0, size
);
185 fprintf(stderr
, "Mem alloc error (nr_rxqs:%u)\n", nr_rxqs
);
191 static struct stats_record
*alloc_stats_record(void)
193 unsigned int nr_rxqs
= map_data
[2].def
.max_entries
;
194 struct stats_record
*rec
;
197 rec
= malloc(sizeof(*rec
));
198 memset(rec
, 0, sizeof(*rec
));
200 fprintf(stderr
, "Mem alloc error\n");
203 rec
->rxq
= alloc_record_per_rxq();
204 for (i
= 0; i
< nr_rxqs
; i
++)
205 rec
->rxq
[i
].cpu
= alloc_record_per_cpu();
207 rec
->stats
.cpu
= alloc_record_per_cpu();
211 static void free_stats_record(struct stats_record
*r
)
213 unsigned int nr_rxqs
= map_data
[2].def
.max_entries
;
216 for (i
= 0; i
< nr_rxqs
; i
++)
224 static bool map_collect_percpu(int fd
, __u32 key
, struct record
*rec
)
226 /* For percpu maps, userspace gets a value per possible CPU */
227 unsigned int nr_cpus
= bpf_num_possible_cpus();
228 struct datarec values
[nr_cpus
];
229 __u64 sum_processed
= 0;
233 if ((bpf_map_lookup_elem(fd
, &key
, values
)) != 0) {
235 "ERR: bpf_map_lookup_elem failed key:0x%X\n", key
);
238 /* Get time as close as possible to reading map contents */
239 rec
->timestamp
= gettime();
241 /* Record and sum values from each CPU */
242 for (i
= 0; i
< nr_cpus
; i
++) {
243 rec
->cpu
[i
].processed
= values
[i
].processed
;
244 sum_processed
+= values
[i
].processed
;
245 rec
->cpu
[i
].issue
= values
[i
].issue
;
246 sum_issue
+= values
[i
].issue
;
248 rec
->total
.processed
= sum_processed
;
249 rec
->total
.issue
= sum_issue
;
253 static void stats_collect(struct stats_record
*rec
)
257 fd
= map_data
[1].fd
; /* map: stats_global_map */
258 map_collect_percpu(fd
, 0, &rec
->stats
);
260 fd
= map_data
[2].fd
; /* map: rx_queue_index_map */
261 max_rxqs
= map_data
[2].def
.max_entries
;
262 for (i
= 0; i
< max_rxqs
; i
++)
263 map_collect_percpu(fd
, i
, &rec
->rxq
[i
]);
266 static double calc_period(struct record
*r
, struct record
*p
)
271 period
= r
->timestamp
- p
->timestamp
;
273 period_
= ((double) period
/ NANOSEC_PER_SEC
);
278 static __u64
calc_pps(struct datarec
*r
, struct datarec
*p
, double period_
)
284 packets
= r
->processed
- p
->processed
;
285 pps
= packets
/ period_
;
290 static __u64
calc_errs_pps(struct datarec
*r
,
291 struct datarec
*p
, double period_
)
297 packets
= r
->issue
- p
->issue
;
298 pps
= packets
/ period_
;
303 static void stats_print(struct stats_record
*stats_rec
,
304 struct stats_record
*stats_prev
,
307 unsigned int nr_cpus
= bpf_num_possible_cpus();
308 unsigned int nr_rxqs
= map_data
[2].def
.max_entries
;
309 double pps
= 0, err
= 0;
310 struct record
*rec
, *prev
;
316 printf("\nRunning XDP on dev:%s (ifindex:%d) action:%s\n",
317 ifname
, ifindex
, action2str(action
));
319 /* stats_global_map */
321 char *fmt_rx
= "%-15s %-7d %'-11.0f %'-10.0f %s\n";
322 char *fm2_rx
= "%-15s %-7s %'-11.0f\n";
325 printf("%-15s %-7s %-11s %-11s\n",
326 "XDP stats", "CPU", "pps", "issue-pps");
328 rec
= &stats_rec
->stats
;
329 prev
= &stats_prev
->stats
;
330 t
= calc_period(rec
, prev
);
331 for (i
= 0; i
< nr_cpus
; i
++) {
332 struct datarec
*r
= &rec
->cpu
[i
];
333 struct datarec
*p
= &prev
->cpu
[i
];
335 pps
= calc_pps (r
, p
, t
);
336 err
= calc_errs_pps(r
, p
, t
);
338 errstr
= "invalid-ifindex";
340 printf(fmt_rx
, "XDP-RX CPU",
341 i
, pps
, err
, errstr
);
343 pps
= calc_pps (&rec
->total
, &prev
->total
, t
);
344 err
= calc_errs_pps(&rec
->total
, &prev
->total
, t
);
345 printf(fm2_rx
, "XDP-RX CPU", "total", pps
, err
);
348 /* rx_queue_index_map */
349 printf("\n%-15s %-7s %-11s %-11s\n",
350 "RXQ stats", "RXQ:CPU", "pps", "issue-pps");
352 for (rxq
= 0; rxq
< nr_rxqs
; rxq
++) {
353 char *fmt_rx
= "%-15s %3d:%-3d %'-11.0f %'-10.0f %s\n";
354 char *fm2_rx
= "%-15s %3d:%-3s %'-11.0f\n";
358 /* Last RXQ in map catch overflows */
359 if (rxq_
== nr_rxqs
- 1)
362 rec
= &stats_rec
->rxq
[rxq
];
363 prev
= &stats_prev
->rxq
[rxq
];
364 t
= calc_period(rec
, prev
);
365 for (i
= 0; i
< nr_cpus
; i
++) {
366 struct datarec
*r
= &rec
->cpu
[i
];
367 struct datarec
*p
= &prev
->cpu
[i
];
369 pps
= calc_pps (r
, p
, t
);
370 err
= calc_errs_pps(r
, p
, t
);
373 errstr
= "map-overflow-RXQ";
378 printf(fmt_rx
, "rx_queue_index",
379 rxq_
, i
, pps
, err
, errstr
);
381 pps
= calc_pps (&rec
->total
, &prev
->total
, t
);
382 err
= calc_errs_pps(&rec
->total
, &prev
->total
, t
);
384 printf(fm2_rx
, "rx_queue_index", rxq_
, "sum", pps
, err
);
389 /* Pointer swap trick */
390 static inline void swap(struct stats_record
**a
, struct stats_record
**b
)
392 struct stats_record
*tmp
;
399 static void stats_poll(int interval
, int action
)
401 struct stats_record
*record
, *prev
;
403 record
= alloc_stats_record();
404 prev
= alloc_stats_record();
405 stats_collect(record
);
408 swap(&prev
, &record
);
409 stats_collect(record
);
410 stats_print(record
, prev
, action
);
414 free_stats_record(record
);
415 free_stats_record(prev
);
419 int main(int argc
, char **argv
)
421 struct rlimit r
= {10 * 1024 * 1024, RLIM_INFINITY
};
422 bool use_separators
= true;
423 struct config cfg
= { 0 };
430 char action_str_buf
[XDP_ACTION_MAX_STRLEN
+ 1 /* for \0 */] = { 0 };
431 int action
= XDP_PASS
; /* Default action */
432 char *action_str
= NULL
;
434 snprintf(filename
, sizeof(filename
), "%s_kern.o", argv
[0]);
436 if (setrlimit(RLIMIT_MEMLOCK
, &r
)) {
437 perror("setrlimit(RLIMIT_MEMLOCK)");
441 if (load_bpf_file(filename
)) {
442 fprintf(stderr
, "ERR in load_bpf_file(): %s", bpf_log_buf
);
447 fprintf(stderr
, "ERR: load_bpf_file: %s\n", strerror(errno
));
451 /* Parse commands line args */
452 while ((opt
= getopt_long(argc
, argv
, "hSd:",
453 long_options
, &longindex
)) != -1) {
456 if (strlen(optarg
) >= IF_NAMESIZE
) {
457 fprintf(stderr
, "ERR: --dev name too long\n");
460 ifname
= (char *)&ifname_buf
;
461 strncpy(ifname
, optarg
, IF_NAMESIZE
);
462 ifindex
= if_nametoindex(ifname
);
465 "ERR: --dev name unknown err(%d):%s\n",
466 errno
, strerror(errno
));
471 interval
= atoi(optarg
);
474 xdp_flags
|= XDP_FLAGS_SKB_MODE
;
477 use_separators
= false;
480 action_str
= (char *)&action_str_buf
;
481 strncpy(action_str
, optarg
, XDP_ACTION_MAX_STRLEN
);
487 return EXIT_FAIL_OPTION
;
490 /* Required option */
492 fprintf(stderr
, "ERR: required option --dev missing\n");
494 return EXIT_FAIL_OPTION
;
496 cfg
.ifindex
= ifindex
;
498 /* Parse action string */
500 action
= parse_xdp_action(action_str
);
502 fprintf(stderr
, "ERR: Invalid XDP --action: %s\n",
505 return EXIT_FAIL_OPTION
;
510 /* Trick to pretty printf with thousands separators use %' */
512 setlocale(LC_NUMERIC
, "en_US");
514 /* User-side setup ifindex in config_map */
515 err
= bpf_map_update_elem(map_fd
[0], &key
, &cfg
, 0);
517 fprintf(stderr
, "Store config failed (err:%d)\n", err
);
521 /* Remove XDP program when program is interrupted */
522 signal(SIGINT
, int_exit
);
524 if (bpf_set_link_xdp_fd(ifindex
, prog_fd
[0], xdp_flags
) < 0) {
525 fprintf(stderr
, "link set xdp fd failed\n");
526 return EXIT_FAIL_XDP
;
529 stats_poll(interval
, action
);