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>
22 #include "bpf/libbpf.h"
25 static __u32 xdp_flags
;
27 static void int_exit(int sig
)
29 bpf_set_link_xdp_fd(ifindex
, -1, xdp_flags
);
33 /* simple per-protocol drop counter
35 static void poll_stats(int map_fd
, int interval
)
37 unsigned int nr_cpus
= bpf_num_possible_cpus();
38 __u64 values
[nr_cpus
], prev
[UINT8_MAX
] = { 0 };
42 __u32 key
= UINT32_MAX
;
46 while (bpf_map_get_next_key(map_fd
, &key
, &key
) != -1) {
49 assert(bpf_map_lookup_elem(map_fd
, &key
, values
) == 0);
50 for (i
= 0; i
< nr_cpus
; i
++)
53 printf("proto %u: %10llu pkt/s\n",
54 key
, (sum
- prev
[key
]) / interval
);
60 static void usage(const char *prog
)
63 "usage: %s [OPTS] IFACE\n\n"
66 " -N enforce native mode\n",
70 int main(int argc
, char **argv
)
72 struct rlimit r
= {RLIM_INFINITY
, RLIM_INFINITY
};
73 struct bpf_prog_load_attr prog_load_attr
= {
74 .prog_type
= BPF_PROG_TYPE_XDP
,
76 const char *optstr
= "SN";
77 int prog_fd
, map_fd
, opt
;
78 struct bpf_object
*obj
;
82 while ((opt
= getopt(argc
, argv
, optstr
)) != -1) {
85 xdp_flags
|= XDP_FLAGS_SKB_MODE
;
88 xdp_flags
|= XDP_FLAGS_DRV_MODE
;
91 usage(basename(argv
[0]));
97 usage(basename(argv
[0]));
101 if (setrlimit(RLIMIT_MEMLOCK
, &r
)) {
102 perror("setrlimit(RLIMIT_MEMLOCK)");
106 ifindex
= if_nametoindex(argv
[1]);
108 perror("if_nametoindex");
112 snprintf(filename
, sizeof(filename
), "%s_kern.o", argv
[0]);
113 prog_load_attr
.file
= filename
;
115 if (bpf_prog_load_xattr(&prog_load_attr
, &obj
, &prog_fd
))
118 map
= bpf_map__next(NULL
, obj
);
120 printf("finding a map in obj file failed\n");
123 map_fd
= bpf_map__fd(map
);
126 printf("load_bpf_file: %s\n", strerror(errno
));
130 signal(SIGINT
, int_exit
);
131 signal(SIGTERM
, int_exit
);
133 if (bpf_set_link_xdp_fd(ifindex
, prog_fd
, xdp_flags
) < 0) {
134 printf("link set xdp fd failed\n");
138 poll_stats(map_fd
, 2);