1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2017 Covalent IO, Inc. http://covalent.io
5 #include <linux/if_link.h>
16 #include <sys/resource.h>
20 #include <bpf/libbpf.h>
22 static int ifindex_in;
23 static int ifindex_out;
24 static bool ifindex_out_xdp_dummy_attached = true;
26 static __u32 dummy_prog_id;
28 static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
29 static int rxcnt_map_fd;
31 static void int_exit(int sig)
33 __u32 curr_prog_id = 0;
35 if (bpf_get_link_xdp_id(ifindex_in, &curr_prog_id, xdp_flags)) {
36 printf("bpf_get_link_xdp_id failed\n");
39 if (prog_id == curr_prog_id)
40 bpf_set_link_xdp_fd(ifindex_in, -1, xdp_flags);
41 else if (!curr_prog_id)
42 printf("couldn't find a prog id on iface IN\n");
44 printf("program on iface IN changed, not removing\n");
46 if (ifindex_out_xdp_dummy_attached) {
48 if (bpf_get_link_xdp_id(ifindex_out, &curr_prog_id,
50 printf("bpf_get_link_xdp_id failed\n");
53 if (dummy_prog_id == curr_prog_id)
54 bpf_set_link_xdp_fd(ifindex_out, -1, xdp_flags);
55 else if (!curr_prog_id)
56 printf("couldn't find a prog id on iface OUT\n");
58 printf("program on iface OUT changed, not removing\n");
63 static void poll_stats(int interval, int ifindex)
65 unsigned int nr_cpus = bpf_num_possible_cpus();
66 __u64 values[nr_cpus], prev[nr_cpus];
68 memset(prev, 0, sizeof(prev));
76 assert(bpf_map_lookup_elem(rxcnt_map_fd, &key, values) == 0);
77 for (i = 0; i < nr_cpus; i++)
78 sum += (values[i] - prev[i]);
80 printf("ifindex %i: %10llu pkt/s\n",
81 ifindex, sum / interval);
82 memcpy(prev, values, sizeof(values));
86 static void usage(const char *prog)
89 "usage: %s [OPTS] <IFNAME|IFINDEX>_IN <IFNAME|IFINDEX>_OUT\n\n"
92 " -N enforce native mode\n"
93 " -F force loading prog\n",
97 int main(int argc, char **argv)
99 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
100 struct bpf_prog_load_attr prog_load_attr = {
101 .prog_type = BPF_PROG_TYPE_XDP,
103 struct bpf_program *prog, *dummy_prog;
104 struct bpf_prog_info info = {};
105 __u32 info_len = sizeof(info);
106 int prog_fd, dummy_prog_fd;
107 const char *optstr = "FSN";
108 struct bpf_object *obj;
109 int ret, opt, key = 0;
113 while ((opt = getopt(argc, argv, optstr)) != -1) {
116 xdp_flags |= XDP_FLAGS_SKB_MODE;
119 /* default, set below */
122 xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
125 usage(basename(argv[0]));
130 if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
131 xdp_flags |= XDP_FLAGS_DRV_MODE;
133 if (optind == argc) {
134 printf("usage: %s <IFNAME|IFINDEX>_IN <IFNAME|IFINDEX>_OUT\n", argv[0]);
138 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
139 perror("setrlimit(RLIMIT_MEMLOCK)");
143 ifindex_in = if_nametoindex(argv[optind]);
145 ifindex_in = strtoul(argv[optind], NULL, 0);
147 ifindex_out = if_nametoindex(argv[optind + 1]);
149 ifindex_out = strtoul(argv[optind + 1], NULL, 0);
151 printf("input: %d output: %d\n", ifindex_in, ifindex_out);
153 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
154 prog_load_attr.file = filename;
156 if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
159 prog = bpf_program__next(NULL, obj);
160 dummy_prog = bpf_program__next(prog, obj);
161 if (!prog || !dummy_prog) {
162 printf("finding a prog in obj file failed\n");
165 /* bpf_prog_load_xattr gives us the pointer to first prog's fd,
166 * so we're missing only the fd for dummy prog
168 dummy_prog_fd = bpf_program__fd(dummy_prog);
169 if (prog_fd < 0 || dummy_prog_fd < 0) {
170 printf("bpf_prog_load_xattr: %s\n", strerror(errno));
174 tx_port_map_fd = bpf_object__find_map_fd_by_name(obj, "tx_port");
175 rxcnt_map_fd = bpf_object__find_map_fd_by_name(obj, "rxcnt");
176 if (tx_port_map_fd < 0 || rxcnt_map_fd < 0) {
177 printf("bpf_object__find_map_fd_by_name failed\n");
181 if (bpf_set_link_xdp_fd(ifindex_in, prog_fd, xdp_flags) < 0) {
182 printf("ERROR: link set xdp fd failed on %d\n", ifindex_in);
186 ret = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
188 printf("can't get prog info - %s\n", strerror(errno));
193 /* Loading dummy XDP prog on out-device */
194 if (bpf_set_link_xdp_fd(ifindex_out, dummy_prog_fd,
195 (xdp_flags | XDP_FLAGS_UPDATE_IF_NOEXIST)) < 0) {
196 printf("WARN: link set xdp fd failed on %d\n", ifindex_out);
197 ifindex_out_xdp_dummy_attached = false;
200 memset(&info, 0, sizeof(info));
201 ret = bpf_obj_get_info_by_fd(dummy_prog_fd, &info, &info_len);
203 printf("can't get prog info - %s\n", strerror(errno));
206 dummy_prog_id = info.id;
208 signal(SIGINT, int_exit);
209 signal(SIGTERM, int_exit);
211 /* populate virtual to physical port map */
212 ret = bpf_map_update_elem(tx_port_map_fd, &key, &ifindex_out, 0);
214 perror("bpf_update_elem");
218 poll_stats(2, ifindex_out);