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>
21 #include "bpf/libbpf.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 map_fd, int interval)
36 unsigned int nr_cpus = bpf_num_possible_cpus();
37 __u64 values[nr_cpus], prev[UINT8_MAX] = { 0 };
41 __u32 key = UINT32_MAX;
45 while (bpf_map_get_next_key(map_fd, &key, &key) != -1) {
48 assert(bpf_map_lookup_elem(map_fd, &key, values) == 0);
49 for (i = 0; i < nr_cpus; i++)
52 printf("proto %u: %10llu pkt/s\n",
53 key, (sum - prev[key]) / interval);
59 static void usage(const char *prog)
62 "usage: %s [OPTS] IFINDEX\n\n"
65 " -N enforce native mode\n",
69 int main(int argc, char **argv)
71 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
72 struct bpf_prog_load_attr prog_load_attr = {
73 .prog_type = BPF_PROG_TYPE_XDP,
75 const char *optstr = "SN";
76 int prog_fd, map_fd, opt;
77 struct bpf_object *obj;
81 while ((opt = getopt(argc, argv, optstr)) != -1) {
84 xdp_flags |= XDP_FLAGS_SKB_MODE;
87 xdp_flags |= XDP_FLAGS_DRV_MODE;
90 usage(basename(argv[0]));
96 usage(basename(argv[0]));
100 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
101 perror("setrlimit(RLIMIT_MEMLOCK)");
105 ifindex = strtoul(argv[optind], NULL, 0);
107 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
108 prog_load_attr.file = filename;
110 if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
113 map = bpf_map__next(NULL, obj);
115 printf("finding a map in obj file failed\n");
118 map_fd = bpf_map__fd(map);
121 printf("load_bpf_file: %s\n", strerror(errno));
125 signal(SIGINT, int_exit);
126 signal(SIGTERM, int_exit);
128 if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) {
129 printf("link set xdp fd failed\n");
133 poll_stats(map_fd, 2);