1 // SPDX-License-Identifier: GPL-2.0-only
6 #include <bpf/libbpf.h>
10 #include <linux/ethtool.h>
11 #include <linux/hashtable.h>
12 #include <linux/if_link.h>
13 #include <linux/jhash.h>
14 #include <linux/limits.h>
15 #include <linux/list.h>
16 #include <linux/sockios.h>
26 #include <sys/ioctl.h>
28 #include <sys/signalfd.h>
29 #include <sys/sysinfo.h>
30 #include <sys/timerfd.h>
31 #include <sys/utsname.h>
36 #include "xdp_sample_user.h"
38 #define __sample_print(fmt, cond, ...) \
41 printf(fmt, ##__VA_ARGS__); \
44 #define print_always(fmt, ...) __sample_print(fmt, 1, ##__VA_ARGS__)
45 #define print_default(fmt, ...) \
46 __sample_print(fmt, sample_log_level & LL_DEFAULT, ##__VA_ARGS__)
47 #define __print_err(err, fmt, ...) \
49 __sample_print(fmt, err > 0 || sample_log_level & LL_DEFAULT, \
51 sample_err_exp = sample_err_exp ? true : err > 0; \
53 #define print_err(err, fmt, ...) __print_err(err, fmt, ##__VA_ARGS__)
55 #define __COLUMN(x) "%'10" x " %-13s"
56 #define FMT_COLUMNf __COLUMN(".0f")
57 #define FMT_COLUMNd __COLUMN("d")
58 #define FMT_COLUMNl __COLUMN("llu")
59 #define RX(rx) rx, "rx/s"
60 #define PPS(pps) pps, "pkt/s"
61 #define DROP(drop) drop, "drop/s"
62 #define ERR(err) err, "error/s"
63 #define HITS(hits) hits, "hit/s"
64 #define XMIT(xmit) xmit, "xmit/s"
65 #define PASS(pass) pass, "pass/s"
66 #define REDIR(redir) redir, "redir/s"
67 #define NANOSEC_PER_SEC 1000000000 /* 10^9 */
69 #define XDP_UNKNOWN (XDP_REDIRECT + 1)
70 #define XDP_ACTION_MAX (XDP_UNKNOWN + 1)
71 #define XDP_REDIRECT_ERR_MAX 7
80 MAP_DEVMAP_XMIT_MULTI
,
97 struct hlist_node node
;
102 struct stats_record
{
103 struct record rx_cnt
;
104 struct record redir_err
[XDP_REDIRECT_ERR_MAX
];
105 struct record kthread
;
106 struct record exception
[XDP_ACTION_MAX
];
107 struct record devmap_xmit
;
108 DECLARE_HASHTABLE(xmit_map
, 5);
112 struct sample_output
{
148 } sample_xdp_progs
[32];
150 struct datarec
*sample_mmap
[NUM_MAP
];
151 struct bpf_map
*sample_map
[NUM_MAP
];
152 size_t sample_map_count
[NUM_MAP
];
153 enum log_level sample_log_level
;
154 struct sample_output sample_out
;
155 unsigned long sample_interval
;
162 static const char *xdp_redirect_err_names
[XDP_REDIRECT_ERR_MAX
] = {
163 /* Key=1 keeps unknown errors */
173 /* Keyed from Unknown */
174 static const char *xdp_redirect_err_help
[XDP_REDIRECT_ERR_MAX
- 1] = {
176 "Invalid redirection",
177 "Device being redirected to is down",
178 "Packet length too large for device",
179 "Operation not supported",
180 "No space in ptr_ring of cpumap kthread",
183 static const char *xdp_action_names
[XDP_ACTION_MAX
] = {
184 [XDP_ABORTED
] = "XDP_ABORTED",
185 [XDP_DROP
] = "XDP_DROP",
186 [XDP_PASS
] = "XDP_PASS",
188 [XDP_REDIRECT
] = "XDP_REDIRECT",
189 [XDP_UNKNOWN
] = "XDP_UNKNOWN",
192 static __u64
gettime(void)
197 res
= clock_gettime(CLOCK_MONOTONIC
, &t
);
199 fprintf(stderr
, "Error with gettimeofday! (%i)\n", res
);
202 return (__u64
)t
.tv_sec
* NANOSEC_PER_SEC
+ t
.tv_nsec
;
205 static const char *action2str(int action
)
207 if (action
< XDP_ACTION_MAX
)
208 return xdp_action_names
[action
];
212 static void sample_print_help(int mask
)
214 printf("Output format description\n\n"
215 "By default, redirect success statistics are disabled, use -s to enable.\n"
216 "The terse output mode is default, verbose mode can be activated using -v\n"
217 "Use SIGQUIT (Ctrl + \\) to switch the mode dynamically at runtime\n\n"
218 "Terse mode displays at most the following fields:\n"
219 " rx/s Number of packets received per second\n"
220 " redir/s Number of packets successfully redirected per second\n"
221 " err,drop/s Aggregated count of errors per second (including dropped packets)\n"
222 " xmit/s Number of packets transmitted on the output device per second\n\n"
223 "Output description for verbose mode:\n"
224 " FIELD DESCRIPTION\n");
226 if (mask
& SAMPLE_RX_CNT
) {
227 printf(" receive\t\tDisplays the number of packets received & errors encountered\n"
228 " \t\t\tWhenever an error or packet drop occurs, details of per CPU error\n"
229 " \t\t\tand drop statistics will be expanded inline in terse mode.\n"
230 " \t\t\t\tpkt/s - Packets received per second\n"
231 " \t\t\t\tdrop/s - Packets dropped per second\n"
232 " \t\t\t\terror/s - Errors encountered per second\n\n");
234 if (mask
& (SAMPLE_REDIRECT_CNT
| SAMPLE_REDIRECT_ERR_CNT
)) {
235 printf(" redirect\t\tDisplays the number of packets successfully redirected\n"
236 " \t\t\tErrors encountered are expanded under redirect_err field\n"
237 " \t\t\tNote that passing -s to enable it has a per packet overhead\n"
238 " \t\t\t\tredir/s - Packets redirected successfully per second\n\n"
239 " redirect_err\t\tDisplays the number of packets that failed redirection\n"
240 " \t\t\tThe errno is expanded under this field with per CPU count\n"
241 " \t\t\tThe recognized errors are:\n");
243 for (int i
= 2; i
< XDP_REDIRECT_ERR_MAX
; i
++)
244 printf("\t\t\t %s: %s\n", xdp_redirect_err_names
[i
],
245 xdp_redirect_err_help
[i
- 1]);
247 printf(" \n\t\t\t\terror/s - Packets that failed redirection per second\n\n");
250 if (mask
& SAMPLE_CPUMAP_ENQUEUE_CNT
) {
251 printf(" enqueue to cpu N\tDisplays the number of packets enqueued to bulk queue of CPU N\n"
252 " \t\t\tExpands to cpu:FROM->N to display enqueue stats for each CPU enqueuing to CPU N\n"
253 " \t\t\tReceived packets can be associated with the CPU redirect program is enqueuing \n"
254 " \t\t\tpackets to.\n"
255 " \t\t\t\tpkt/s - Packets enqueued per second from other CPU to CPU N\n"
256 " \t\t\t\tdrop/s - Packets dropped when trying to enqueue to CPU N\n"
257 " \t\t\t\tbulk-avg - Average number of packets processed for each event\n\n");
260 if (mask
& SAMPLE_CPUMAP_KTHREAD_CNT
) {
261 printf(" kthread\t\tDisplays the number of packets processed in CPUMAP kthread for each CPU\n"
262 " \t\t\tPackets consumed from ptr_ring in kthread, and its xdp_stats (after calling \n"
263 " \t\t\tCPUMAP bpf prog) are expanded below this. xdp_stats are expanded as a total and\n"
264 " \t\t\tthen per-CPU to associate it to each CPU's pinned CPUMAP kthread.\n"
265 " \t\t\t\tpkt/s - Packets consumed per second from ptr_ring\n"
266 " \t\t\t\tdrop/s - Packets dropped per second in kthread\n"
267 " \t\t\t\tsched - Number of times kthread called schedule()\n\n"
268 " \t\t\txdp_stats (also expands to per-CPU counts)\n"
269 " \t\t\t\tpass/s - XDP_PASS count for CPUMAP program execution\n"
270 " \t\t\t\tdrop/s - XDP_DROP count for CPUMAP program execution\n"
271 " \t\t\t\tredir/s - XDP_REDIRECT count for CPUMAP program execution\n\n");
274 if (mask
& SAMPLE_EXCEPTION_CNT
) {
275 printf(" xdp_exception\t\tDisplays xdp_exception tracepoint events\n"
276 " \t\t\tThis can occur due to internal driver errors, unrecognized\n"
277 " \t\t\tXDP actions and due to explicit user trigger by use of XDP_ABORTED\n"
278 " \t\t\tEach action is expanded below this field with its count\n"
279 " \t\t\t\thit/s - Number of times the tracepoint was hit per second\n\n");
282 if (mask
& SAMPLE_DEVMAP_XMIT_CNT
) {
283 printf(" devmap_xmit\t\tDisplays devmap_xmit tracepoint events\n"
284 " \t\t\tThis tracepoint is invoked for successful transmissions on output\n"
285 " \t\t\tdevice but these statistics are not available for generic XDP mode,\n"
286 " \t\t\thence they will be omitted from the output when using SKB mode\n"
287 " \t\t\t\txmit/s - Number of packets that were transmitted per second\n"
288 " \t\t\t\tdrop/s - Number of packets that failed transmissions per second\n"
289 " \t\t\t\tdrv_err/s - Number of internal driver errors per second\n"
290 " \t\t\t\tbulk-avg - Average number of packets processed for each event\n\n");
294 void sample_usage(char *argv
[], const struct option
*long_options
,
295 const char *doc
, int mask
, bool error
)
300 sample_print_help(mask
);
302 printf("\n%s\nOption for %s:\n", doc
, argv
[0]);
303 for (i
= 0; long_options
[i
].name
!= 0; i
++) {
304 printf(" --%-15s", long_options
[i
].name
);
305 if (long_options
[i
].flag
!= NULL
)
306 printf(" flag (internal value: %d)",
307 *long_options
[i
].flag
);
309 printf("\t short-option: -%c", long_options
[i
].val
);
315 static struct datarec
*alloc_record_per_cpu(void)
317 unsigned int nr_cpus
= libbpf_num_possible_cpus();
318 struct datarec
*array
;
320 array
= calloc(nr_cpus
, sizeof(*array
));
322 fprintf(stderr
, "Failed to allocate memory (nr_cpus: %u)\n",
329 static int map_entry_init(struct map_entry
*e
, __u64 pair
)
332 INIT_HLIST_NODE(&e
->node
);
333 e
->val
.timestamp
= gettime();
334 e
->val
.cpu
= alloc_record_per_cpu();
340 static void map_collect_percpu(struct datarec
*values
, struct record
*rec
)
342 /* For percpu maps, userspace gets a value per possible CPU */
343 unsigned int nr_cpus
= libbpf_num_possible_cpus();
344 __u64 sum_xdp_redirect
= 0;
345 __u64 sum_processed
= 0;
346 __u64 sum_xdp_pass
= 0;
347 __u64 sum_xdp_drop
= 0;
348 __u64 sum_dropped
= 0;
352 /* Get time as close as possible to reading map contents */
353 rec
->timestamp
= gettime();
355 /* Record and sum values from each CPU */
356 for (i
= 0; i
< nr_cpus
; i
++) {
357 rec
->cpu
[i
].processed
= READ_ONCE(values
[i
].processed
);
358 rec
->cpu
[i
].dropped
= READ_ONCE(values
[i
].dropped
);
359 rec
->cpu
[i
].issue
= READ_ONCE(values
[i
].issue
);
360 rec
->cpu
[i
].xdp_pass
= READ_ONCE(values
[i
].xdp_pass
);
361 rec
->cpu
[i
].xdp_drop
= READ_ONCE(values
[i
].xdp_drop
);
362 rec
->cpu
[i
].xdp_redirect
= READ_ONCE(values
[i
].xdp_redirect
);
364 sum_processed
+= rec
->cpu
[i
].processed
;
365 sum_dropped
+= rec
->cpu
[i
].dropped
;
366 sum_issue
+= rec
->cpu
[i
].issue
;
367 sum_xdp_pass
+= rec
->cpu
[i
].xdp_pass
;
368 sum_xdp_drop
+= rec
->cpu
[i
].xdp_drop
;
369 sum_xdp_redirect
+= rec
->cpu
[i
].xdp_redirect
;
372 rec
->total
.processed
= sum_processed
;
373 rec
->total
.dropped
= sum_dropped
;
374 rec
->total
.issue
= sum_issue
;
375 rec
->total
.xdp_pass
= sum_xdp_pass
;
376 rec
->total
.xdp_drop
= sum_xdp_drop
;
377 rec
->total
.xdp_redirect
= sum_xdp_redirect
;
380 static int map_collect_percpu_devmap(int map_fd
, struct stats_record
*rec
)
382 unsigned int nr_cpus
= bpf_num_possible_cpus();
383 __u32 batch
, count
= 32;
384 struct datarec
*values
;
389 keys
= calloc(count
, sizeof(__u64
));
392 values
= calloc(count
* nr_cpus
, sizeof(struct datarec
));
401 ret
= bpf_map_lookup_batch(map_fd
, init
? &batch
: NULL
, &batch
,
402 keys
, values
, &count
, NULL
);
403 if (ret
< 0 && errno
!= ENOENT
)
409 for (i
= 0; i
< count
; i
++) {
410 struct map_entry
*e
, *x
= NULL
;
411 __u64 pair
= keys
[i
];
414 arr
= &values
[i
* nr_cpus
];
415 hash_for_each_possible(rec
->xmit_map
, e
, node
, pair
) {
416 if (e
->pair
== pair
) {
422 x
= calloc(1, sizeof(*x
));
425 if (map_entry_init(x
, pair
) < 0) {
429 hash_add(rec
->xmit_map
, &x
->node
, pair
);
431 map_collect_percpu(arr
, &x
->val
);
448 static struct stats_record
*alloc_stats_record(void)
450 struct stats_record
*rec
;
453 rec
= calloc(1, sizeof(*rec
) + sample_n_cpus
* sizeof(struct record
));
455 fprintf(stderr
, "Failed to allocate memory\n");
459 if (sample_mask
& SAMPLE_RX_CNT
) {
460 rec
->rx_cnt
.cpu
= alloc_record_per_cpu();
461 if (!rec
->rx_cnt
.cpu
) {
463 "Failed to allocate rx_cnt per-CPU array\n");
467 if (sample_mask
& (SAMPLE_REDIRECT_CNT
| SAMPLE_REDIRECT_ERR_CNT
)) {
468 for (i
= 0; i
< XDP_REDIRECT_ERR_MAX
; i
++) {
469 rec
->redir_err
[i
].cpu
= alloc_record_per_cpu();
470 if (!rec
->redir_err
[i
].cpu
) {
472 "Failed to allocate redir_err per-CPU array for "
474 xdp_redirect_err_names
[i
]);
476 free(rec
->redir_err
[i
].cpu
);
481 if (sample_mask
& SAMPLE_CPUMAP_KTHREAD_CNT
) {
482 rec
->kthread
.cpu
= alloc_record_per_cpu();
483 if (!rec
->kthread
.cpu
) {
485 "Failed to allocate kthread per-CPU array\n");
489 if (sample_mask
& SAMPLE_EXCEPTION_CNT
) {
490 for (i
= 0; i
< XDP_ACTION_MAX
; i
++) {
491 rec
->exception
[i
].cpu
= alloc_record_per_cpu();
492 if (!rec
->exception
[i
].cpu
) {
494 "Failed to allocate exception per-CPU array for "
498 free(rec
->exception
[i
].cpu
);
503 if (sample_mask
& SAMPLE_DEVMAP_XMIT_CNT
) {
504 rec
->devmap_xmit
.cpu
= alloc_record_per_cpu();
505 if (!rec
->devmap_xmit
.cpu
) {
507 "Failed to allocate devmap_xmit per-CPU array\n");
511 if (sample_mask
& SAMPLE_DEVMAP_XMIT_CNT_MULTI
)
512 hash_init(rec
->xmit_map
);
513 if (sample_mask
& SAMPLE_CPUMAP_ENQUEUE_CNT
) {
514 for (i
= 0; i
< sample_n_cpus
; i
++) {
515 rec
->enq
[i
].cpu
= alloc_record_per_cpu();
516 if (!rec
->enq
[i
].cpu
) {
518 "Failed to allocate enqueue per-CPU array for "
522 free(rec
->enq
[i
].cpu
);
523 goto end_devmap_xmit
;
531 free(rec
->devmap_xmit
.cpu
);
533 for (i
= 0; i
< XDP_ACTION_MAX
; i
++)
534 free(rec
->exception
[i
].cpu
);
536 free(rec
->kthread
.cpu
);
538 for (i
= 0; i
< XDP_REDIRECT_ERR_MAX
; i
++)
539 free(rec
->redir_err
[i
].cpu
);
541 free(rec
->rx_cnt
.cpu
);
547 static void free_stats_record(struct stats_record
*r
)
549 struct hlist_node
*tmp
;
553 for (i
= 0; i
< sample_n_cpus
; i
++)
555 hash_for_each_safe(r
->xmit_map
, i
, tmp
, e
, node
) {
560 free(r
->devmap_xmit
.cpu
);
561 for (i
= 0; i
< XDP_ACTION_MAX
; i
++)
562 free(r
->exception
[i
].cpu
);
563 free(r
->kthread
.cpu
);
564 for (i
= 0; i
< XDP_REDIRECT_ERR_MAX
; i
++)
565 free(r
->redir_err
[i
].cpu
);
570 static double calc_period(struct record
*r
, struct record
*p
)
575 period
= r
->timestamp
- p
->timestamp
;
577 period_
= ((double)period
/ NANOSEC_PER_SEC
);
582 static double sample_round(double val
)
584 if (val
- floor(val
) < 0.5)
589 static __u64
calc_pps(struct datarec
*r
, struct datarec
*p
, double period_
)
595 packets
= r
->processed
- p
->processed
;
596 pps
= sample_round(packets
/ period_
);
601 static __u64
calc_drop_pps(struct datarec
*r
, struct datarec
*p
, double period_
)
607 packets
= r
->dropped
- p
->dropped
;
608 pps
= sample_round(packets
/ period_
);
613 static __u64
calc_errs_pps(struct datarec
*r
, struct datarec
*p
, double period_
)
619 packets
= r
->issue
- p
->issue
;
620 pps
= sample_round(packets
/ period_
);
625 static __u64
calc_info_pps(struct datarec
*r
, struct datarec
*p
, double period_
)
631 packets
= r
->info
- p
->info
;
632 pps
= sample_round(packets
/ period_
);
637 static void calc_xdp_pps(struct datarec
*r
, struct datarec
*p
, double *xdp_pass
,
638 double *xdp_drop
, double *xdp_redirect
, double period_
)
640 *xdp_pass
= 0, *xdp_drop
= 0, *xdp_redirect
= 0;
642 *xdp_redirect
= (r
->xdp_redirect
- p
->xdp_redirect
) / period_
;
643 *xdp_pass
= (r
->xdp_pass
- p
->xdp_pass
) / period_
;
644 *xdp_drop
= (r
->xdp_drop
- p
->xdp_drop
) / period_
;
648 static void stats_get_rx_cnt(struct stats_record
*stats_rec
,
649 struct stats_record
*stats_prev
,
650 unsigned int nr_cpus
, struct sample_output
*out
)
652 struct record
*rec
, *prev
;
653 double t
, pps
, drop
, err
;
656 rec
= &stats_rec
->rx_cnt
;
657 prev
= &stats_prev
->rx_cnt
;
658 t
= calc_period(rec
, prev
);
660 for (i
= 0; i
< nr_cpus
; i
++) {
661 struct datarec
*r
= &rec
->cpu
[i
];
662 struct datarec
*p
= &prev
->cpu
[i
];
665 pps
= calc_pps(r
, p
, t
);
666 drop
= calc_drop_pps(r
, p
, t
);
667 err
= calc_errs_pps(r
, p
, t
);
668 if (!pps
&& !drop
&& !err
)
671 snprintf(str
, sizeof(str
), "cpu:%d", i
);
672 print_default(" %-18s " FMT_COLUMNf FMT_COLUMNf FMT_COLUMNf
674 str
, PPS(pps
), DROP(drop
), ERR(err
));
678 pps
= calc_pps(&rec
->total
, &prev
->total
, t
);
679 drop
= calc_drop_pps(&rec
->total
, &prev
->total
, t
);
680 err
= calc_errs_pps(&rec
->total
, &prev
->total
, t
);
682 out
->rx_cnt
.pps
= pps
;
683 out
->rx_cnt
.drop
= drop
;
684 out
->rx_cnt
.err
= err
;
685 out
->totals
.rx
+= pps
;
686 out
->totals
.drop
+= drop
;
687 out
->totals
.err
+= err
;
691 static void stats_get_cpumap_enqueue(struct stats_record
*stats_rec
,
692 struct stats_record
*stats_prev
,
693 unsigned int nr_cpus
)
695 struct record
*rec
, *prev
;
696 double t
, pps
, drop
, err
;
699 /* cpumap enqueue stats */
700 for (to_cpu
= 0; to_cpu
< sample_n_cpus
; to_cpu
++) {
701 rec
= &stats_rec
->enq
[to_cpu
];
702 prev
= &stats_prev
->enq
[to_cpu
];
703 t
= calc_period(rec
, prev
);
705 pps
= calc_pps(&rec
->total
, &prev
->total
, t
);
706 drop
= calc_drop_pps(&rec
->total
, &prev
->total
, t
);
707 err
= calc_errs_pps(&rec
->total
, &prev
->total
, t
);
709 if (pps
> 0 || drop
> 0) {
712 snprintf(str
, sizeof(str
), "enqueue to cpu %d", to_cpu
);
715 err
= pps
/ err
; /* calc average bulk size */
718 " %-20s " FMT_COLUMNf FMT_COLUMNf
__COLUMN(
720 str
, PPS(pps
), DROP(drop
), err
, "bulk-avg");
723 for (i
= 0; i
< nr_cpus
; i
++) {
724 struct datarec
*r
= &rec
->cpu
[i
];
725 struct datarec
*p
= &prev
->cpu
[i
];
728 pps
= calc_pps(r
, p
, t
);
729 drop
= calc_drop_pps(r
, p
, t
);
730 err
= calc_errs_pps(r
, p
, t
);
731 if (!pps
&& !drop
&& !err
)
734 snprintf(str
, sizeof(str
), "cpu:%d->%d", i
, to_cpu
);
736 err
= pps
/ err
; /* calc average bulk size */
738 " %-18s " FMT_COLUMNf FMT_COLUMNf
__COLUMN(
740 str
, PPS(pps
), DROP(drop
), err
, "bulk-avg");
745 static void stats_get_cpumap_remote(struct stats_record
*stats_rec
,
746 struct stats_record
*stats_prev
,
747 unsigned int nr_cpus
)
749 double xdp_pass
, xdp_drop
, xdp_redirect
;
750 struct record
*rec
, *prev
;
754 rec
= &stats_rec
->kthread
;
755 prev
= &stats_prev
->kthread
;
756 t
= calc_period(rec
, prev
);
758 calc_xdp_pps(&rec
->total
, &prev
->total
, &xdp_pass
, &xdp_drop
,
760 if (xdp_pass
|| xdp_drop
|| xdp_redirect
) {
762 " %-18s " FMT_COLUMNf FMT_COLUMNf FMT_COLUMNf
"\n",
763 "xdp_stats", PASS(xdp_pass
), DROP(xdp_drop
),
764 REDIR(xdp_redirect
));
767 for (i
= 0; i
< nr_cpus
; i
++) {
768 struct datarec
*r
= &rec
->cpu
[i
];
769 struct datarec
*p
= &prev
->cpu
[i
];
772 calc_xdp_pps(r
, p
, &xdp_pass
, &xdp_drop
, &xdp_redirect
, t
);
773 if (!xdp_pass
&& !xdp_drop
&& !xdp_redirect
)
776 snprintf(str
, sizeof(str
), "cpu:%d", i
);
777 print_default(" %-16s " FMT_COLUMNf FMT_COLUMNf FMT_COLUMNf
779 str
, PASS(xdp_pass
), DROP(xdp_drop
),
780 REDIR(xdp_redirect
));
784 static void stats_get_cpumap_kthread(struct stats_record
*stats_rec
,
785 struct stats_record
*stats_prev
,
786 unsigned int nr_cpus
)
788 struct record
*rec
, *prev
;
789 double t
, pps
, drop
, err
;
792 rec
= &stats_rec
->kthread
;
793 prev
= &stats_prev
->kthread
;
794 t
= calc_period(rec
, prev
);
796 pps
= calc_pps(&rec
->total
, &prev
->total
, t
);
797 drop
= calc_drop_pps(&rec
->total
, &prev
->total
, t
);
798 err
= calc_errs_pps(&rec
->total
, &prev
->total
, t
);
800 print_err(drop
, " %-20s " FMT_COLUMNf FMT_COLUMNf FMT_COLUMNf
"\n",
801 pps
? "kthread total" : "kthread", PPS(pps
), DROP(drop
), err
,
804 for (i
= 0; i
< nr_cpus
; i
++) {
805 struct datarec
*r
= &rec
->cpu
[i
];
806 struct datarec
*p
= &prev
->cpu
[i
];
809 pps
= calc_pps(r
, p
, t
);
810 drop
= calc_drop_pps(r
, p
, t
);
811 err
= calc_errs_pps(r
, p
, t
);
812 if (!pps
&& !drop
&& !err
)
815 snprintf(str
, sizeof(str
), "cpu:%d", i
);
816 print_default(" %-18s " FMT_COLUMNf FMT_COLUMNf FMT_COLUMNf
818 str
, PPS(pps
), DROP(drop
), err
, "sched");
822 static void stats_get_redirect_cnt(struct stats_record
*stats_rec
,
823 struct stats_record
*stats_prev
,
824 unsigned int nr_cpus
,
825 struct sample_output
*out
)
827 struct record
*rec
, *prev
;
831 rec
= &stats_rec
->redir_err
[0];
832 prev
= &stats_prev
->redir_err
[0];
833 t
= calc_period(rec
, prev
);
834 for (i
= 0; i
< nr_cpus
; i
++) {
835 struct datarec
*r
= &rec
->cpu
[i
];
836 struct datarec
*p
= &prev
->cpu
[i
];
839 pps
= calc_pps(r
, p
, t
);
843 snprintf(str
, sizeof(str
), "cpu:%d", i
);
844 print_default(" %-18s " FMT_COLUMNf
"\n", str
, REDIR(pps
));
848 pps
= calc_pps(&rec
->total
, &prev
->total
, t
);
849 out
->redir_cnt
.suc
= pps
;
850 out
->totals
.redir
+= pps
;
854 static void stats_get_redirect_err_cnt(struct stats_record
*stats_rec
,
855 struct stats_record
*stats_prev
,
856 unsigned int nr_cpus
,
857 struct sample_output
*out
)
859 struct record
*rec
, *prev
;
860 double t
, drop
, sum
= 0;
863 for (rec_i
= 1; rec_i
< XDP_REDIRECT_ERR_MAX
; rec_i
++) {
866 rec
= &stats_rec
->redir_err
[rec_i
];
867 prev
= &stats_prev
->redir_err
[rec_i
];
868 t
= calc_period(rec
, prev
);
870 drop
= calc_drop_pps(&rec
->total
, &prev
->total
, t
);
871 if (drop
> 0 && !out
) {
872 snprintf(str
, sizeof(str
),
873 sample_log_level
& LL_DEFAULT
? "%s total" :
875 xdp_redirect_err_names
[rec_i
]);
876 print_err(drop
, " %-18s " FMT_COLUMNf
"\n", str
,
880 for (i
= 0; i
< nr_cpus
; i
++) {
881 struct datarec
*r
= &rec
->cpu
[i
];
882 struct datarec
*p
= &prev
->cpu
[i
];
885 drop
= calc_drop_pps(r
, p
, t
);
889 snprintf(str
, sizeof(str
), "cpu:%d", i
);
890 print_default(" %-16s" FMT_COLUMNf
"\n", str
,
898 out
->redir_cnt
.err
= sum
;
899 out
->totals
.err
+= sum
;
903 static void stats_get_exception_cnt(struct stats_record
*stats_rec
,
904 struct stats_record
*stats_prev
,
905 unsigned int nr_cpus
,
906 struct sample_output
*out
)
908 double t
, drop
, sum
= 0;
909 struct record
*rec
, *prev
;
912 for (rec_i
= 0; rec_i
< XDP_ACTION_MAX
; rec_i
++) {
913 rec
= &stats_rec
->exception
[rec_i
];
914 prev
= &stats_prev
->exception
[rec_i
];
915 t
= calc_period(rec
, prev
);
917 drop
= calc_drop_pps(&rec
->total
, &prev
->total
, t
);
918 /* Fold out errors after heading */
921 if (drop
> 0 && !out
) {
922 print_always(" %-18s " FMT_COLUMNf
"\n",
923 action2str(rec_i
), ERR(drop
));
925 for (i
= 0; i
< nr_cpus
; i
++) {
926 struct datarec
*r
= &rec
->cpu
[i
];
927 struct datarec
*p
= &prev
->cpu
[i
];
931 drop
= calc_drop_pps(r
, p
, t
);
935 snprintf(str
, sizeof(str
), "cpu:%d", i
);
936 print_default(" %-16s" FMT_COLUMNf
"\n",
943 out
->except_cnt
.hits
= sum
;
944 out
->totals
.err
+= sum
;
948 static void stats_get_devmap_xmit(struct stats_record
*stats_rec
,
949 struct stats_record
*stats_prev
,
950 unsigned int nr_cpus
,
951 struct sample_output
*out
)
953 double pps
, drop
, info
, err
;
954 struct record
*rec
, *prev
;
958 rec
= &stats_rec
->devmap_xmit
;
959 prev
= &stats_prev
->devmap_xmit
;
960 t
= calc_period(rec
, prev
);
961 for (i
= 0; i
< nr_cpus
; i
++) {
962 struct datarec
*r
= &rec
->cpu
[i
];
963 struct datarec
*p
= &prev
->cpu
[i
];
966 pps
= calc_pps(r
, p
, t
);
967 drop
= calc_drop_pps(r
, p
, t
);
968 err
= calc_errs_pps(r
, p
, t
);
970 if (!pps
&& !drop
&& !err
)
973 snprintf(str
, sizeof(str
), "cpu:%d", i
);
974 info
= calc_info_pps(r
, p
, t
);
976 info
= (pps
+ drop
) / info
; /* calc avg bulk */
977 print_default(" %-18s" FMT_COLUMNf FMT_COLUMNf FMT_COLUMNf
978 __COLUMN(".2f") "\n",
979 str
, XMIT(pps
), DROP(drop
), err
, "drv_err/s",
983 pps
= calc_pps(&rec
->total
, &prev
->total
, t
);
984 drop
= calc_drop_pps(&rec
->total
, &prev
->total
, t
);
985 info
= calc_info_pps(&rec
->total
, &prev
->total
, t
);
987 info
= (pps
+ drop
) / info
; /* calc avg bulk */
988 err
= calc_errs_pps(&rec
->total
, &prev
->total
, t
);
990 out
->xmit_cnt
.pps
= pps
;
991 out
->xmit_cnt
.drop
= drop
;
992 out
->xmit_cnt
.bavg
= info
;
993 out
->xmit_cnt
.err
= err
;
994 out
->totals
.xmit
+= pps
;
995 out
->totals
.drop_xmit
+= drop
;
996 out
->totals
.err
+= err
;
1000 static void stats_get_devmap_xmit_multi(struct stats_record
*stats_rec
,
1001 struct stats_record
*stats_prev
,
1002 unsigned int nr_cpus
,
1003 struct sample_output
*out
,
1006 double pps
, drop
, info
, err
;
1007 struct map_entry
*entry
;
1008 struct record
*r
, *p
;
1012 hash_for_each(stats_rec
->xmit_map
, bkt
, entry
, node
) {
1013 struct map_entry
*e
, *x
= NULL
;
1014 char ifname_from
[IFNAMSIZ
];
1015 char ifname_to
[IFNAMSIZ
];
1016 const char *fstr
, *tstr
;
1017 unsigned long prev_time
;
1018 struct record beg
= {};
1019 __u32 from_idx
, to_idx
;
1024 prev_time
= sample_interval
* NANOSEC_PER_SEC
;
1027 from_idx
= pair
>> 32;
1028 to_idx
= pair
& 0xFFFFFFFF;
1031 beg
.timestamp
= r
->timestamp
- prev_time
;
1033 /* Find matching entry from stats_prev map */
1034 hash_for_each_possible(stats_prev
->xmit_map
, e
, node
, pair
) {
1035 if (e
->pair
== pair
) {
1044 t
= calc_period(r
, p
);
1045 pps
= calc_pps(&r
->total
, &p
->total
, t
);
1046 drop
= calc_drop_pps(&r
->total
, &p
->total
, t
);
1047 info
= calc_info_pps(&r
->total
, &p
->total
, t
);
1049 info
= (pps
+ drop
) / info
; /* calc avg bulk */
1050 err
= calc_errs_pps(&r
->total
, &p
->total
, t
);
1053 /* We are responsible for filling out totals */
1054 out
->totals
.xmit
+= pps
;
1055 out
->totals
.drop_xmit
+= drop
;
1056 out
->totals
.err
+= err
;
1061 if (if_indextoname(from_idx
, ifname_from
))
1063 if (if_indextoname(to_idx
, ifname_to
))
1066 snprintf(str
, sizeof(str
), "xmit %s->%s", fstr
?: "?",
1068 /* Skip idle streams of redirection */
1069 if (pps
|| drop
|| err
) {
1071 " %-20s " FMT_COLUMNf FMT_COLUMNf FMT_COLUMNf
1072 __COLUMN(".2f") "\n", str
, XMIT(pps
), DROP(drop
),
1073 err
, "drv_err/s", info
, "bulk-avg");
1076 for (i
= 0; i
< nr_cpus
; i
++) {
1077 struct datarec
*rc
= &r
->cpu
[i
];
1078 struct datarec
*pc
, p_beg
= {};
1081 pc
= p
== &beg
? &p_beg
: &p
->cpu
[i
];
1083 pps
= calc_pps(rc
, pc
, t
);
1084 drop
= calc_drop_pps(rc
, pc
, t
);
1085 err
= calc_errs_pps(rc
, pc
, t
);
1087 if (!pps
&& !drop
&& !err
)
1090 snprintf(str
, sizeof(str
), "cpu:%d", i
);
1091 info
= calc_info_pps(rc
, pc
, t
);
1093 info
= (pps
+ drop
) / info
; /* calc avg bulk */
1095 print_default(" %-18s" FMT_COLUMNf FMT_COLUMNf FMT_COLUMNf
1096 __COLUMN(".2f") "\n", str
, XMIT(pps
),
1097 DROP(drop
), err
, "drv_err/s", info
, "bulk-avg");
1102 static void stats_print(const char *prefix
, int mask
, struct stats_record
*r
,
1103 struct stats_record
*p
, struct sample_output
*out
)
1105 int nr_cpus
= libbpf_num_possible_cpus();
1108 print_always("%-23s", prefix
?: "Summary");
1109 if (mask
& SAMPLE_RX_CNT
)
1110 print_always(FMT_COLUMNl
, RX(out
->totals
.rx
));
1111 if (mask
& SAMPLE_REDIRECT_CNT
)
1112 print_always(FMT_COLUMNl
, REDIR(out
->totals
.redir
));
1114 out
->totals
.err
+ out
->totals
.drop
+ out
->totals
.drop_xmit
,
1116 if (mask
& SAMPLE_DEVMAP_XMIT_CNT
||
1117 mask
& SAMPLE_DEVMAP_XMIT_CNT_MULTI
)
1118 printf(FMT_COLUMNl
, XMIT(out
->totals
.xmit
));
1121 if (mask
& SAMPLE_RX_CNT
) {
1122 str
= (sample_log_level
& LL_DEFAULT
) && out
->rx_cnt
.pps
?
1125 print_err((out
->rx_cnt
.err
|| out
->rx_cnt
.drop
),
1126 " %-20s " FMT_COLUMNl FMT_COLUMNl FMT_COLUMNl
"\n",
1127 str
, PPS(out
->rx_cnt
.pps
), DROP(out
->rx_cnt
.drop
),
1128 ERR(out
->rx_cnt
.err
));
1130 stats_get_rx_cnt(r
, p
, nr_cpus
, NULL
);
1133 if (mask
& SAMPLE_CPUMAP_ENQUEUE_CNT
)
1134 stats_get_cpumap_enqueue(r
, p
, nr_cpus
);
1136 if (mask
& SAMPLE_CPUMAP_KTHREAD_CNT
) {
1137 stats_get_cpumap_kthread(r
, p
, nr_cpus
);
1138 stats_get_cpumap_remote(r
, p
, nr_cpus
);
1141 if (mask
& SAMPLE_REDIRECT_CNT
) {
1142 str
= out
->redir_cnt
.suc
? "redirect total" : "redirect";
1143 print_default(" %-20s " FMT_COLUMNl
"\n", str
,
1144 REDIR(out
->redir_cnt
.suc
));
1146 stats_get_redirect_cnt(r
, p
, nr_cpus
, NULL
);
1149 if (mask
& SAMPLE_REDIRECT_ERR_CNT
) {
1150 str
= (sample_log_level
& LL_DEFAULT
) && out
->redir_cnt
.err
?
1151 "redirect_err total" :
1153 print_err(out
->redir_cnt
.err
, " %-20s " FMT_COLUMNl
"\n", str
,
1154 ERR(out
->redir_cnt
.err
));
1156 stats_get_redirect_err_cnt(r
, p
, nr_cpus
, NULL
);
1159 if (mask
& SAMPLE_EXCEPTION_CNT
) {
1160 str
= out
->except_cnt
.hits
? "xdp_exception total" :
1163 print_err(out
->except_cnt
.hits
, " %-20s " FMT_COLUMNl
"\n", str
,
1164 HITS(out
->except_cnt
.hits
));
1166 stats_get_exception_cnt(r
, p
, nr_cpus
, NULL
);
1169 if (mask
& SAMPLE_DEVMAP_XMIT_CNT
) {
1170 str
= (sample_log_level
& LL_DEFAULT
) && out
->xmit_cnt
.pps
?
1171 "devmap_xmit total" :
1174 print_err(out
->xmit_cnt
.err
|| out
->xmit_cnt
.drop
,
1175 " %-20s " FMT_COLUMNl FMT_COLUMNl FMT_COLUMNl
1176 __COLUMN(".2f") "\n",
1177 str
, XMIT(out
->xmit_cnt
.pps
),
1178 DROP(out
->xmit_cnt
.drop
), out
->xmit_cnt
.err
,
1179 "drv_err/s", out
->xmit_cnt
.bavg
, "bulk-avg");
1181 stats_get_devmap_xmit(r
, p
, nr_cpus
, NULL
);
1184 if (mask
& SAMPLE_DEVMAP_XMIT_CNT_MULTI
)
1185 stats_get_devmap_xmit_multi(r
, p
, nr_cpus
, NULL
,
1186 mask
& SAMPLE_DEVMAP_XMIT_CNT
);
1188 if (sample_log_level
& LL_DEFAULT
||
1189 ((sample_log_level
& LL_SIMPLE
) && sample_err_exp
)) {
1190 sample_err_exp
= false;
1195 int sample_setup_maps(struct bpf_map
**maps
)
1197 sample_n_cpus
= libbpf_num_possible_cpus();
1199 for (int i
= 0; i
< MAP_DEVMAP_XMIT_MULTI
; i
++) {
1200 sample_map
[i
] = maps
[i
];
1204 case MAP_CPUMAP_KTHREAD
:
1205 case MAP_DEVMAP_XMIT
:
1206 sample_map_count
[i
] = sample_n_cpus
;
1208 case MAP_REDIRECT_ERR
:
1209 sample_map_count
[i
] =
1210 XDP_REDIRECT_ERR_MAX
* sample_n_cpus
;
1213 sample_map_count
[i
] = XDP_ACTION_MAX
* sample_n_cpus
;
1214 case MAP_CPUMAP_ENQUEUE
:
1215 sample_map_count
[i
] = sample_n_cpus
* sample_n_cpus
;
1220 if (bpf_map__set_max_entries(sample_map
[i
], sample_map_count
[i
]) < 0)
1223 sample_map
[MAP_DEVMAP_XMIT_MULTI
] = maps
[MAP_DEVMAP_XMIT_MULTI
];
1227 static int sample_setup_maps_mappings(void)
1229 for (int i
= 0; i
< MAP_DEVMAP_XMIT_MULTI
; i
++) {
1230 size_t size
= sample_map_count
[i
] * sizeof(struct datarec
);
1232 sample_mmap
[i
] = mmap(NULL
, size
, PROT_READ
| PROT_WRITE
,
1233 MAP_SHARED
, bpf_map__fd(sample_map
[i
]), 0);
1234 if (sample_mmap
[i
] == MAP_FAILED
)
1240 int __sample_init(int mask
)
1245 sigaddset(&st
, SIGQUIT
);
1246 sigaddset(&st
, SIGINT
);
1247 sigaddset(&st
, SIGTERM
);
1249 if (sigprocmask(SIG_BLOCK
, &st
, NULL
) < 0)
1252 sample_sig_fd
= signalfd(-1, &st
, SFD_CLOEXEC
| SFD_NONBLOCK
);
1253 if (sample_sig_fd
< 0)
1258 return sample_setup_maps_mappings();
1261 static int __sample_remove_xdp(int ifindex
, __u32 prog_id
, int xdp_flags
)
1263 __u32 cur_prog_id
= 0;
1267 ret
= bpf_xdp_query_id(ifindex
, xdp_flags
, &cur_prog_id
);
1271 if (prog_id
!= cur_prog_id
) {
1273 "Program on ifindex %d does not match installed "
1274 "program, skipping unload\n",
1280 return bpf_xdp_detach(ifindex
, xdp_flags
, NULL
);
1283 int sample_install_xdp(struct bpf_program
*xdp_prog
, int ifindex
, bool generic
,
1286 int ret
, xdp_flags
= 0;
1289 if (sample_xdp_cnt
== 32) {
1291 "Total limit for installed XDP programs in a sample reached\n");
1295 xdp_flags
|= !force
? XDP_FLAGS_UPDATE_IF_NOEXIST
: 0;
1296 xdp_flags
|= generic
? XDP_FLAGS_SKB_MODE
: XDP_FLAGS_DRV_MODE
;
1297 ret
= bpf_xdp_attach(ifindex
, bpf_program__fd(xdp_prog
), xdp_flags
, NULL
);
1301 "Failed to install program \"%s\" on ifindex %d, mode = %s, "
1303 bpf_program__name(xdp_prog
), ifindex
,
1304 generic
? "skb" : "native", force
? "true" : "false",
1309 ret
= bpf_xdp_query_id(ifindex
, xdp_flags
, &prog_id
);
1313 "Failed to get XDP program id for ifindex %d, removing program: %s\n",
1314 ifindex
, strerror(errno
));
1315 __sample_remove_xdp(ifindex
, 0, xdp_flags
);
1318 sample_xdp_progs
[sample_xdp_cnt
++] =
1319 (struct xdp_desc
){ ifindex
, prog_id
, xdp_flags
};
1324 static void sample_summary_print(void)
1326 double num
= sample_out
.rx_cnt
.num
;
1328 if (sample_out
.totals
.rx
) {
1329 double pkts
= sample_out
.totals
.rx
;
1331 print_always(" Packets received : %'-10llu\n",
1332 sample_out
.totals
.rx
);
1333 print_always(" Average packets/s : %'-10.0f\n",
1334 sample_round(pkts
/ num
));
1336 if (sample_out
.totals
.redir
) {
1337 double pkts
= sample_out
.totals
.redir
;
1339 print_always(" Packets redirected : %'-10llu\n",
1340 sample_out
.totals
.redir
);
1341 print_always(" Average redir/s : %'-10.0f\n",
1342 sample_round(pkts
/ num
));
1344 if (sample_out
.totals
.drop
)
1345 print_always(" Rx dropped : %'-10llu\n",
1346 sample_out
.totals
.drop
);
1347 if (sample_out
.totals
.drop_xmit
)
1348 print_always(" Tx dropped : %'-10llu\n",
1349 sample_out
.totals
.drop_xmit
);
1350 if (sample_out
.totals
.err
)
1351 print_always(" Errors recorded : %'-10llu\n",
1352 sample_out
.totals
.err
);
1353 if (sample_out
.totals
.xmit
) {
1354 double pkts
= sample_out
.totals
.xmit
;
1356 print_always(" Packets transmitted : %'-10llu\n",
1357 sample_out
.totals
.xmit
);
1358 print_always(" Average transmit/s : %'-10.0f\n",
1359 sample_round(pkts
/ num
));
1363 void sample_exit(int status
)
1367 for (int i
= 0; i
< NUM_MAP
; i
++) {
1368 size
= sample_map_count
[i
] * sizeof(**sample_mmap
);
1369 munmap(sample_mmap
[i
], size
);
1371 while (sample_xdp_cnt
--) {
1372 int i
= sample_xdp_cnt
, ifindex
, xdp_flags
;
1375 prog_id
= sample_xdp_progs
[i
].prog_id
;
1376 ifindex
= sample_xdp_progs
[i
].ifindex
;
1377 xdp_flags
= sample_xdp_progs
[i
].flags
;
1379 __sample_remove_xdp(ifindex
, prog_id
, xdp_flags
);
1381 sample_summary_print();
1382 close(sample_sig_fd
);
1386 static int sample_stats_collect(struct stats_record
*rec
)
1390 if (sample_mask
& SAMPLE_RX_CNT
)
1391 map_collect_percpu(sample_mmap
[MAP_RX
], &rec
->rx_cnt
);
1393 if (sample_mask
& SAMPLE_REDIRECT_CNT
)
1394 map_collect_percpu(sample_mmap
[MAP_REDIRECT_ERR
], &rec
->redir_err
[0]);
1396 if (sample_mask
& SAMPLE_REDIRECT_ERR_CNT
) {
1397 for (i
= 1; i
< XDP_REDIRECT_ERR_MAX
; i
++)
1398 map_collect_percpu(&sample_mmap
[MAP_REDIRECT_ERR
][i
* sample_n_cpus
],
1399 &rec
->redir_err
[i
]);
1402 if (sample_mask
& SAMPLE_CPUMAP_ENQUEUE_CNT
)
1403 for (i
= 0; i
< sample_n_cpus
; i
++)
1404 map_collect_percpu(&sample_mmap
[MAP_CPUMAP_ENQUEUE
][i
* sample_n_cpus
],
1407 if (sample_mask
& SAMPLE_CPUMAP_KTHREAD_CNT
)
1408 map_collect_percpu(sample_mmap
[MAP_CPUMAP_KTHREAD
],
1411 if (sample_mask
& SAMPLE_EXCEPTION_CNT
)
1412 for (i
= 0; i
< XDP_ACTION_MAX
; i
++)
1413 map_collect_percpu(&sample_mmap
[MAP_EXCEPTION
][i
* sample_n_cpus
],
1414 &rec
->exception
[i
]);
1416 if (sample_mask
& SAMPLE_DEVMAP_XMIT_CNT
)
1417 map_collect_percpu(sample_mmap
[MAP_DEVMAP_XMIT
], &rec
->devmap_xmit
);
1419 if (sample_mask
& SAMPLE_DEVMAP_XMIT_CNT_MULTI
) {
1420 if (map_collect_percpu_devmap(bpf_map__fd(sample_map
[MAP_DEVMAP_XMIT_MULTI
]), rec
) < 0)
1426 static void sample_summary_update(struct sample_output
*out
)
1428 sample_out
.totals
.rx
+= out
->totals
.rx
;
1429 sample_out
.totals
.redir
+= out
->totals
.redir
;
1430 sample_out
.totals
.drop
+= out
->totals
.drop
;
1431 sample_out
.totals
.drop_xmit
+= out
->totals
.drop_xmit
;
1432 sample_out
.totals
.err
+= out
->totals
.err
;
1433 sample_out
.totals
.xmit
+= out
->totals
.xmit
;
1434 sample_out
.rx_cnt
.num
++;
1437 static void sample_stats_print(int mask
, struct stats_record
*cur
,
1438 struct stats_record
*prev
, char *prog_name
)
1440 struct sample_output out
= {};
1442 if (mask
& SAMPLE_RX_CNT
)
1443 stats_get_rx_cnt(cur
, prev
, 0, &out
);
1444 if (mask
& SAMPLE_REDIRECT_CNT
)
1445 stats_get_redirect_cnt(cur
, prev
, 0, &out
);
1446 if (mask
& SAMPLE_REDIRECT_ERR_CNT
)
1447 stats_get_redirect_err_cnt(cur
, prev
, 0, &out
);
1448 if (mask
& SAMPLE_EXCEPTION_CNT
)
1449 stats_get_exception_cnt(cur
, prev
, 0, &out
);
1450 if (mask
& SAMPLE_DEVMAP_XMIT_CNT
)
1451 stats_get_devmap_xmit(cur
, prev
, 0, &out
);
1452 else if (mask
& SAMPLE_DEVMAP_XMIT_CNT_MULTI
)
1453 stats_get_devmap_xmit_multi(cur
, prev
, 0, &out
,
1454 mask
& SAMPLE_DEVMAP_XMIT_CNT
);
1455 sample_summary_update(&out
);
1457 stats_print(prog_name
, mask
, cur
, prev
, &out
);
1460 void sample_switch_mode(void)
1462 sample_log_level
^= LL_DEBUG
- 1;
1465 static int sample_signal_cb(void)
1467 struct signalfd_siginfo si
;
1470 r
= read(sample_sig_fd
, &si
, sizeof(si
));
1474 switch (si
.ssi_signo
) {
1476 sample_switch_mode();
1487 /* Pointer swap trick */
1488 static void swap(struct stats_record
**a
, struct stats_record
**b
)
1490 struct stats_record
*tmp
;
1497 static int sample_timer_cb(int timerfd
, struct stats_record
**rec
,
1498 struct stats_record
**prev
)
1500 char line
[64] = "Summary";
1504 ret
= read(timerfd
, &t
, sizeof(t
));
1509 ret
= sample_stats_collect(*rec
);
1513 if (sample_xdp_cnt
== 2 && !(sample_mask
& SAMPLE_SKIP_HEADING
)) {
1519 if (if_indextoname(sample_xdp_progs
[0].ifindex
, fi
))
1521 if (if_indextoname(sample_xdp_progs
[1].ifindex
, to
))
1524 snprintf(line
, sizeof(line
), "%s->%s", f
?: "?", t
?: "?");
1527 sample_stats_print(sample_mask
, *rec
, *prev
, line
);
1531 int sample_run(int interval
, void (*post_cb
)(void *), void *ctx
)
1533 struct timespec ts
= { interval
, 0 };
1534 struct itimerspec its
= { ts
, ts
};
1535 struct stats_record
*rec
, *prev
;
1536 struct pollfd pfd
[2] = {};
1540 fprintf(stderr
, "Incorrect interval 0\n");
1543 sample_interval
= interval
;
1544 /* Pretty print numbers */
1545 setlocale(LC_NUMERIC
, "en_US.UTF-8");
1547 timerfd
= timerfd_create(CLOCK_MONOTONIC
, TFD_CLOEXEC
| TFD_NONBLOCK
);
1550 timerfd_settime(timerfd
, 0, &its
, NULL
);
1552 pfd
[0].fd
= sample_sig_fd
;
1553 pfd
[0].events
= POLLIN
;
1555 pfd
[1].fd
= timerfd
;
1556 pfd
[1].events
= POLLIN
;
1559 rec
= alloc_stats_record();
1562 prev
= alloc_stats_record();
1566 ret
= sample_stats_collect(rec
);
1571 ret
= poll(pfd
, 2, -1);
1579 if (pfd
[0].revents
& POLLIN
)
1580 ret
= sample_signal_cb();
1581 else if (pfd
[1].revents
& POLLIN
)
1582 ret
= sample_timer_cb(timerfd
, &rec
, &prev
);
1592 free_stats_record(prev
);
1594 free_stats_record(rec
);
1601 const char *get_driver_name(int ifindex
)
1603 struct ethtool_drvinfo drv
= {};
1604 char ifname
[IF_NAMESIZE
];
1605 static char drvname
[32];
1606 struct ifreq ifr
= {};
1609 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
1613 if (!if_indextoname(ifindex
, ifname
))
1616 drv
.cmd
= ETHTOOL_GDRVINFO
;
1617 safe_strncpy(ifr
.ifr_name
, ifname
, sizeof(ifr
.ifr_name
));
1618 ifr
.ifr_data
= (void *)&drv
;
1620 r
= ioctl(fd
, SIOCETHTOOL
, &ifr
);
1624 safe_strncpy(drvname
, drv
.driver
, sizeof(drvname
));
1632 return r
== EOPNOTSUPP
? "loopback" : "[error]";
1635 int get_mac_addr(int ifindex
, void *mac_addr
)
1637 char ifname
[IF_NAMESIZE
];
1638 struct ifreq ifr
= {};
1641 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
1645 if (!if_indextoname(ifindex
, ifname
)) {
1650 safe_strncpy(ifr
.ifr_name
, ifname
, sizeof(ifr
.ifr_name
));
1652 r
= ioctl(fd
, SIOCGIFHWADDR
, &ifr
);
1658 memcpy(mac_addr
, ifr
.ifr_hwaddr
.sa_data
, 6 * sizeof(char));
1665 __attribute__((constructor
)) static void sample_ctor(void)
1667 if (libbpf_set_strict_mode(LIBBPF_STRICT_ALL
) < 0) {
1668 fprintf(stderr
, "Failed to set libbpf strict mode: %s\n",
1670 /* Just exit, nothing to cleanup right now */
1671 exit(EXIT_FAIL_BPF
);