1 /* SPDX-License-Identifier: GPL-2.0
2 * Copyright(c) 2017 Jesper Dangaard Brouer, Red Hat, Inc.
4 static const char *__doc__
=
5 "XDP monitor tool, based on tracepoints\n"
8 static const char *__doc_err_only__
=
9 " NOTICE: Only tracking XDP redirect errors\n"
10 " Enable TX success stats via '--stats'\n"
11 " (which comes with a per packet processing overhead)\n"
24 #include <sys/resource.h>
33 static int verbose
= 1;
34 static bool debug
= false;
36 static const struct option long_options
[] = {
37 {"help", no_argument
, NULL
, 'h' },
38 {"debug", no_argument
, NULL
, 'D' },
39 {"stats", no_argument
, NULL
, 'S' },
40 {"sec", required_argument
, NULL
, 's' },
44 /* C standard specifies two constants, EXIT_SUCCESS(0) and EXIT_FAILURE(1) */
45 #define EXIT_FAIL_MEM 5
47 static void usage(char *argv
[])
50 printf("\nDOCUMENTATION:\n%s\n", __doc__
);
52 printf(" Usage: %s (options-see-below)\n",
54 printf(" Listing options:\n");
55 for (i
= 0; long_options
[i
].name
!= 0; i
++) {
56 printf(" --%-15s", long_options
[i
].name
);
57 if (long_options
[i
].flag
!= NULL
)
58 printf(" flag (internal value:%d)",
59 *long_options
[i
].flag
);
61 printf("(internal short-option: -%c)",
68 #define NANOSEC_PER_SEC 1000000000 /* 10^9 */
69 static __u64
gettime(void)
74 res
= clock_gettime(CLOCK_MONOTONIC
, &t
);
76 fprintf(stderr
, "Error with gettimeofday! (%i)\n", res
);
79 return (__u64
) t
.tv_sec
* NANOSEC_PER_SEC
+ t
.tv_nsec
;
86 #define REDIR_RES_MAX 2
87 static const char *redir_names
[REDIR_RES_MAX
] = {
88 [REDIR_SUCCESS
] = "Success",
89 [REDIR_ERROR
] = "Error",
91 static const char *err2str(int err
)
93 if (err
< REDIR_RES_MAX
)
94 return redir_names
[err
];
98 #define XDP_UNKNOWN XDP_REDIRECT + 1
99 #define XDP_ACTION_MAX (XDP_UNKNOWN + 1)
100 static const char *xdp_action_names
[XDP_ACTION_MAX
] = {
101 [XDP_ABORTED
] = "XDP_ABORTED",
102 [XDP_DROP
] = "XDP_DROP",
103 [XDP_PASS
] = "XDP_PASS",
105 [XDP_REDIRECT
] = "XDP_REDIRECT",
106 [XDP_UNKNOWN
] = "XDP_UNKNOWN",
108 static const char *action2str(int action
)
110 if (action
< XDP_ACTION_MAX
)
111 return xdp_action_names
[action
];
115 /* Common stats data record shared with _kern.c */
123 /* Userspace structs for collection of stats from maps */
126 struct datarec total
;
133 /* record for _kern side __u64 values */
139 struct stats_record
{
140 struct record_u64 xdp_redirect
[REDIR_RES_MAX
];
141 struct record_u64 xdp_exception
[XDP_ACTION_MAX
];
142 struct record xdp_cpumap_kthread
;
143 struct record xdp_cpumap_enqueue
[MAX_CPUS
];
146 static bool map_collect_record(int fd
, __u32 key
, struct record
*rec
)
148 /* For percpu maps, userspace gets a value per possible CPU */
149 unsigned int nr_cpus
= bpf_num_possible_cpus();
150 struct datarec values
[nr_cpus
];
151 __u64 sum_processed
= 0;
152 __u64 sum_dropped
= 0;
156 if ((bpf_map_lookup_elem(fd
, &key
, values
)) != 0) {
158 "ERR: bpf_map_lookup_elem failed key:0x%X\n", key
);
161 /* Get time as close as possible to reading map contents */
162 rec
->timestamp
= gettime();
164 /* Record and sum values from each CPU */
165 for (i
= 0; i
< nr_cpus
; i
++) {
166 rec
->cpu
[i
].processed
= values
[i
].processed
;
167 sum_processed
+= values
[i
].processed
;
168 rec
->cpu
[i
].dropped
= values
[i
].dropped
;
169 sum_dropped
+= values
[i
].dropped
;
170 rec
->cpu
[i
].info
= values
[i
].info
;
171 sum_info
+= values
[i
].info
;
173 rec
->total
.processed
= sum_processed
;
174 rec
->total
.dropped
= sum_dropped
;
175 rec
->total
.info
= sum_info
;
179 static bool map_collect_record_u64(int fd
, __u32 key
, struct record_u64
*rec
)
181 /* For percpu maps, userspace gets a value per possible CPU */
182 unsigned int nr_cpus
= bpf_num_possible_cpus();
183 struct u64rec values
[nr_cpus
];
187 if ((bpf_map_lookup_elem(fd
, &key
, values
)) != 0) {
189 "ERR: bpf_map_lookup_elem failed key:0x%X\n", key
);
192 /* Get time as close as possible to reading map contents */
193 rec
->timestamp
= gettime();
195 /* Record and sum values from each CPU */
196 for (i
= 0; i
< nr_cpus
; i
++) {
197 rec
->cpu
[i
].processed
= values
[i
].processed
;
198 sum_total
+= values
[i
].processed
;
200 rec
->total
.processed
= sum_total
;
204 static double calc_period(struct record
*r
, struct record
*p
)
209 period
= r
->timestamp
- p
->timestamp
;
211 period_
= ((double) period
/ NANOSEC_PER_SEC
);
216 static double calc_period_u64(struct record_u64
*r
, struct record_u64
*p
)
221 period
= r
->timestamp
- p
->timestamp
;
223 period_
= ((double) period
/ NANOSEC_PER_SEC
);
228 static double calc_pps(struct datarec
*r
, struct datarec
*p
, double period
)
234 packets
= r
->processed
- p
->processed
;
235 pps
= packets
/ period
;
240 static double calc_pps_u64(struct u64rec
*r
, struct u64rec
*p
, double period
)
246 packets
= r
->processed
- p
->processed
;
247 pps
= packets
/ period
;
252 static double calc_drop(struct datarec
*r
, struct datarec
*p
, double period
)
258 packets
= r
->dropped
- p
->dropped
;
259 pps
= packets
/ period
;
264 static double calc_info(struct datarec
*r
, struct datarec
*p
, double period
)
270 packets
= r
->info
- p
->info
;
271 pps
= packets
/ period
;
276 static void stats_print(struct stats_record
*stats_rec
,
277 struct stats_record
*stats_prev
,
280 unsigned int nr_cpus
= bpf_num_possible_cpus();
281 int rec_i
= 0, i
, to_cpu
;
282 double t
= 0, pps
= 0;
285 printf("%-15s %-7s %-12s %-12s %-9s\n",
286 "XDP-event", "CPU:to", "pps", "drop-pps", "extra-info");
288 /* tracepoint: xdp:xdp_redirect_* */
292 for (; rec_i
< REDIR_RES_MAX
; rec_i
++) {
293 struct record_u64
*rec
, *prev
;
294 char *fmt1
= "%-15s %-7d %'-12.0f %'-12.0f %s\n";
295 char *fmt2
= "%-15s %-7s %'-12.0f %'-12.0f %s\n";
297 rec
= &stats_rec
->xdp_redirect
[rec_i
];
298 prev
= &stats_prev
->xdp_redirect
[rec_i
];
299 t
= calc_period_u64(rec
, prev
);
301 for (i
= 0; i
< nr_cpus
; i
++) {
302 struct u64rec
*r
= &rec
->cpu
[i
];
303 struct u64rec
*p
= &prev
->cpu
[i
];
305 pps
= calc_pps_u64(r
, p
, t
);
307 printf(fmt1
, "XDP_REDIRECT", i
,
308 rec_i
? 0.0: pps
, rec_i
? pps
: 0.0,
311 pps
= calc_pps_u64(&rec
->total
, &prev
->total
, t
);
312 printf(fmt2
, "XDP_REDIRECT", "total",
313 rec_i
? 0.0: pps
, rec_i
? pps
: 0.0, err2str(rec_i
));
316 /* tracepoint: xdp:xdp_exception */
317 for (rec_i
= 0; rec_i
< XDP_ACTION_MAX
; rec_i
++) {
318 struct record_u64
*rec
, *prev
;
319 char *fmt1
= "%-15s %-7d %'-12.0f %'-12.0f %s\n";
320 char *fmt2
= "%-15s %-7s %'-12.0f %'-12.0f %s\n";
322 rec
= &stats_rec
->xdp_exception
[rec_i
];
323 prev
= &stats_prev
->xdp_exception
[rec_i
];
324 t
= calc_period_u64(rec
, prev
);
326 for (i
= 0; i
< nr_cpus
; i
++) {
327 struct u64rec
*r
= &rec
->cpu
[i
];
328 struct u64rec
*p
= &prev
->cpu
[i
];
330 pps
= calc_pps_u64(r
, p
, t
);
332 printf(fmt1
, "Exception", i
,
333 0.0, pps
, err2str(rec_i
));
335 pps
= calc_pps_u64(&rec
->total
, &prev
->total
, t
);
337 printf(fmt2
, "Exception", "total",
338 0.0, pps
, action2str(rec_i
));
341 /* cpumap enqueue stats */
342 for (to_cpu
= 0; to_cpu
< MAX_CPUS
; to_cpu
++) {
343 char *fmt1
= "%-15s %3d:%-3d %'-12.0f %'-12.0f %'-10.2f %s\n";
344 char *fmt2
= "%-15s %3s:%-3d %'-12.0f %'-12.0f %'-10.2f %s\n";
345 struct record
*rec
, *prev
;
349 rec
= &stats_rec
->xdp_cpumap_enqueue
[to_cpu
];
350 prev
= &stats_prev
->xdp_cpumap_enqueue
[to_cpu
];
351 t
= calc_period(rec
, prev
);
352 for (i
= 0; i
< nr_cpus
; i
++) {
353 struct datarec
*r
= &rec
->cpu
[i
];
354 struct datarec
*p
= &prev
->cpu
[i
];
356 pps
= calc_pps(r
, p
, t
);
357 drop
= calc_drop(r
, p
, t
);
358 info
= calc_info(r
, p
, t
);
360 info_str
= "bulk-average";
361 info
= pps
/ info
; /* calc average bulk size */
364 printf(fmt1
, "cpumap-enqueue",
365 i
, to_cpu
, pps
, drop
, info
, info_str
);
367 pps
= calc_pps(&rec
->total
, &prev
->total
, t
);
369 drop
= calc_drop(&rec
->total
, &prev
->total
, t
);
370 info
= calc_info(&rec
->total
, &prev
->total
, t
);
372 info_str
= "bulk-average";
373 info
= pps
/ info
; /* calc average bulk size */
375 printf(fmt2
, "cpumap-enqueue",
376 "sum", to_cpu
, pps
, drop
, info
, info_str
);
380 /* cpumap kthread stats */
382 char *fmt1
= "%-15s %-7d %'-12.0f %'-12.0f %'-10.0f %s\n";
383 char *fmt2
= "%-15s %-7s %'-12.0f %'-12.0f %'-10.0f %s\n";
384 struct record
*rec
, *prev
;
388 rec
= &stats_rec
->xdp_cpumap_kthread
;
389 prev
= &stats_prev
->xdp_cpumap_kthread
;
390 t
= calc_period(rec
, prev
);
391 for (i
= 0; i
< nr_cpus
; i
++) {
392 struct datarec
*r
= &rec
->cpu
[i
];
393 struct datarec
*p
= &prev
->cpu
[i
];
395 pps
= calc_pps(r
, p
, t
);
396 drop
= calc_drop(r
, p
, t
);
397 info
= calc_info(r
, p
, t
);
401 printf(fmt1
, "cpumap-kthread",
402 i
, pps
, drop
, info
, i_str
);
404 pps
= calc_pps(&rec
->total
, &prev
->total
, t
);
405 drop
= calc_drop(&rec
->total
, &prev
->total
, t
);
406 info
= calc_info(&rec
->total
, &prev
->total
, t
);
409 printf(fmt2
, "cpumap-kthread", "total", pps
, drop
, info
, i_str
);
415 static bool stats_collect(struct stats_record
*rec
)
420 /* TODO: Detect if someone unloaded the perf event_fd's, as
421 * this can happen by someone running perf-record -e
424 fd
= map_data
[0].fd
; /* map0: redirect_err_cnt */
425 for (i
= 0; i
< REDIR_RES_MAX
; i
++)
426 map_collect_record_u64(fd
, i
, &rec
->xdp_redirect
[i
]);
428 fd
= map_data
[1].fd
; /* map1: exception_cnt */
429 for (i
= 0; i
< XDP_ACTION_MAX
; i
++) {
430 map_collect_record_u64(fd
, i
, &rec
->xdp_exception
[i
]);
433 fd
= map_data
[2].fd
; /* map2: cpumap_enqueue_cnt */
434 for (i
= 0; i
< MAX_CPUS
; i
++)
435 map_collect_record(fd
, i
, &rec
->xdp_cpumap_enqueue
[i
]);
437 fd
= map_data
[3].fd
; /* map3: cpumap_kthread_cnt */
438 map_collect_record(fd
, 0, &rec
->xdp_cpumap_kthread
);
443 static void *alloc_rec_per_cpu(int record_size
)
445 unsigned int nr_cpus
= bpf_num_possible_cpus();
449 size
= record_size
* nr_cpus
;
450 array
= malloc(size
);
451 memset(array
, 0, size
);
453 fprintf(stderr
, "Mem alloc error (nr_cpus:%u)\n", nr_cpus
);
459 static struct stats_record
*alloc_stats_record(void)
461 struct stats_record
*rec
;
465 /* Alloc main stats_record structure */
466 rec
= malloc(sizeof(*rec
));
467 memset(rec
, 0, sizeof(*rec
));
469 fprintf(stderr
, "Mem alloc error\n");
473 /* Alloc stats stored per CPU for each record */
474 rec_sz
= sizeof(struct u64rec
);
475 for (i
= 0; i
< REDIR_RES_MAX
; i
++)
476 rec
->xdp_redirect
[i
].cpu
= alloc_rec_per_cpu(rec_sz
);
478 for (i
= 0; i
< XDP_ACTION_MAX
; i
++)
479 rec
->xdp_exception
[i
].cpu
= alloc_rec_per_cpu(rec_sz
);
481 rec_sz
= sizeof(struct datarec
);
482 rec
->xdp_cpumap_kthread
.cpu
= alloc_rec_per_cpu(rec_sz
);
484 for (i
= 0; i
< MAX_CPUS
; i
++)
485 rec
->xdp_cpumap_enqueue
[i
].cpu
= alloc_rec_per_cpu(rec_sz
);
490 static void free_stats_record(struct stats_record
*r
)
494 for (i
= 0; i
< REDIR_RES_MAX
; i
++)
495 free(r
->xdp_redirect
[i
].cpu
);
497 for (i
= 0; i
< XDP_ACTION_MAX
; i
++)
498 free(r
->xdp_exception
[i
].cpu
);
500 free(r
->xdp_cpumap_kthread
.cpu
);
502 for (i
= 0; i
< MAX_CPUS
; i
++)
503 free(r
->xdp_cpumap_enqueue
[i
].cpu
);
508 /* Pointer swap trick */
509 static inline void swap(struct stats_record
**a
, struct stats_record
**b
)
511 struct stats_record
*tmp
;
518 static void stats_poll(int interval
, bool err_only
)
520 struct stats_record
*rec
, *prev
;
522 rec
= alloc_stats_record();
523 prev
= alloc_stats_record();
527 printf("\n%s\n", __doc_err_only__
);
529 /* Trick to pretty printf with thousands separators use %' */
530 setlocale(LC_NUMERIC
, "en_US");
534 printf("\n%s", __doc__
);
536 /* TODO Need more advanced stats on error types */
538 printf(" - Stats map0: %s\n", map_data
[0].name
);
539 printf(" - Stats map1: %s\n", map_data
[1].name
);
547 stats_print(rec
, prev
, err_only
);
552 free_stats_record(rec
);
553 free_stats_record(prev
);
556 static void print_bpf_prog_info(void)
561 printf("Loaded BPF prog have %d bpf program(s)\n", prog_cnt
);
562 for (i
= 0; i
< prog_cnt
; i
++) {
563 printf(" - prog_fd[%d] = fd(%d)\n", i
, prog_fd
[i
]);
567 printf("Loaded BPF prog have %d map(s)\n", map_data_count
);
568 for (i
= 0; i
< map_data_count
; i
++) {
569 char *name
= map_data
[i
].name
;
570 int fd
= map_data
[i
].fd
;
572 printf(" - map_data[%d] = fd(%d) name:%s\n", i
, fd
, name
);
576 printf("Searching for (max:%d) event file descriptor(s)\n", prog_cnt
);
577 for (i
= 0; i
< prog_cnt
; i
++) {
578 if (event_fd
[i
] != -1)
579 printf(" - event_fd[%d] = fd(%d)\n", i
, event_fd
[i
]);
583 int main(int argc
, char **argv
)
585 struct rlimit r
= {RLIM_INFINITY
, RLIM_INFINITY
};
586 int longindex
= 0, opt
;
587 int ret
= EXIT_SUCCESS
;
588 char bpf_obj_file
[256];
590 /* Default settings: */
591 bool errors_only
= true;
594 snprintf(bpf_obj_file
, sizeof(bpf_obj_file
), "%s_kern.o", argv
[0]);
596 /* Parse commands line args */
597 while ((opt
= getopt_long(argc
, argv
, "h",
598 long_options
, &longindex
)) != -1) {
607 interval
= atoi(optarg
);
616 if (setrlimit(RLIMIT_MEMLOCK
, &r
)) {
617 perror("setrlimit(RLIMIT_MEMLOCK)");
621 if (load_bpf_file(bpf_obj_file
)) {
622 printf("ERROR - bpf_log_buf: %s", bpf_log_buf
);
626 printf("ERROR - load_bpf_file: %s\n", strerror(errno
));
631 print_bpf_prog_info();
634 /* Unload/stop tracepoint event by closing fd's */
636 /* The prog_fd[i] and event_fd[i] depend on the
637 * order the functions was defined in _kern.c
639 close(event_fd
[2]); /* tracepoint/xdp/xdp_redirect */
640 close(prog_fd
[2]); /* func: trace_xdp_redirect */
641 close(event_fd
[3]); /* tracepoint/xdp/xdp_redirect_map */
642 close(prog_fd
[3]); /* func: trace_xdp_redirect_map */
645 stats_poll(interval
, errors_only
);