1 /* Copyright (c) 2016 PLUMgrid
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
8 #include <linux/if_link.h>
23 static __u32 xdp_flags;
25 static void int_exit(int sig)
27 set_link_xdp_fd(ifindex, -1, xdp_flags);
31 /* simple per-protocol drop counter
33 static void poll_stats(int interval)
35 unsigned int nr_cpus = bpf_num_possible_cpus();
36 const unsigned int nr_keys = 256;
37 __u64 values[nr_cpus], prev[nr_keys][nr_cpus];
41 memset(prev, 0, sizeof(prev));
46 for (key = 0; key < nr_keys; key++) {
49 assert(bpf_map_lookup_elem(map_fd[0], &key, values) == 0);
50 for (i = 0; i < nr_cpus; i++)
51 sum += (values[i] - prev[key][i]);
53 printf("proto %u: %10llu pkt/s\n",
55 memcpy(prev[key], values, sizeof(values));
60 static void usage(const char *prog)
63 "usage: %s [OPTS] IFINDEX\n\n"
69 int main(int argc, char **argv)
71 const char *optstr = "S";
75 while ((opt = getopt(argc, argv, optstr)) != -1) {
78 xdp_flags |= XDP_FLAGS_SKB_MODE;
81 usage(basename(argv[0]));
87 usage(basename(argv[0]));
90 ifindex = strtoul(argv[optind], NULL, 0);
92 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
94 if (load_bpf_file(filename)) {
95 printf("%s", bpf_log_buf);
100 printf("load_bpf_file: %s\n", strerror(errno));
104 signal(SIGINT, int_exit);
106 if (set_link_xdp_fd(ifindex, prog_fd[0], xdp_flags) < 0) {
107 printf("link set xdp fd failed\n");