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>
17 #include <sys/resource.h>
24 static __u32 xdp_flags
;
26 static void int_exit(int sig
)
28 bpf_set_link_xdp_fd(ifindex
, -1, xdp_flags
);
32 /* simple per-protocol drop counter
34 static void poll_stats(int interval
)
36 unsigned int nr_cpus
= bpf_num_possible_cpus();
37 const unsigned int nr_keys
= 256;
38 __u64 values
[nr_cpus
], prev
[nr_keys
][nr_cpus
];
42 memset(prev
, 0, sizeof(prev
));
47 for (key
= 0; key
< nr_keys
; key
++) {
50 assert(bpf_map_lookup_elem(map_fd
[0], &key
, values
) == 0);
51 for (i
= 0; i
< nr_cpus
; i
++)
52 sum
+= (values
[i
] - prev
[key
][i
]);
54 printf("proto %u: %10llu pkt/s\n",
56 memcpy(prev
[key
], values
, sizeof(values
));
61 static void usage(const char *prog
)
64 "usage: %s [OPTS] IFINDEX\n\n"
67 " -N enforce native mode\n",
71 int main(int argc
, char **argv
)
73 struct rlimit r
= {RLIM_INFINITY
, RLIM_INFINITY
};
74 const char *optstr
= "SN";
78 while ((opt
= getopt(argc
, argv
, optstr
)) != -1) {
81 xdp_flags
|= XDP_FLAGS_SKB_MODE
;
84 xdp_flags
|= XDP_FLAGS_DRV_MODE
;
87 usage(basename(argv
[0]));
93 usage(basename(argv
[0]));
97 if (setrlimit(RLIMIT_MEMLOCK
, &r
)) {
98 perror("setrlimit(RLIMIT_MEMLOCK)");
102 ifindex
= strtoul(argv
[optind
], NULL
, 0);
104 snprintf(filename
, sizeof(filename
), "%s_kern.o", argv
[0]);
106 if (load_bpf_file(filename
)) {
107 printf("%s", bpf_log_buf
);
112 printf("load_bpf_file: %s\n", strerror(errno
));
116 signal(SIGINT
, int_exit
);
117 signal(SIGTERM
, int_exit
);
119 if (bpf_set_link_xdp_fd(ifindex
, prog_fd
[0], xdp_flags
) < 0) {
120 printf("link set xdp fd failed\n");