1 /* Copyright (c) 2017 Covalent IO, Inc. http://covalent.io
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.
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
12 #include <linux/bpf.h>
13 #include <linux/if_link.h>
23 #include <sys/resource.h>
27 #include "bpf/libbpf.h"
29 static int ifindex_in;
30 static int ifindex_out;
31 static bool ifindex_out_xdp_dummy_attached = true;
33 static __u32 dummy_prog_id;
35 static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
36 static int rxcnt_map_fd;
38 static void int_exit(int sig)
40 __u32 curr_prog_id = 0;
42 if (bpf_get_link_xdp_id(ifindex_in, &curr_prog_id, xdp_flags)) {
43 printf("bpf_get_link_xdp_id failed\n");
46 if (prog_id == curr_prog_id)
47 bpf_set_link_xdp_fd(ifindex_in, -1, xdp_flags);
48 else if (!curr_prog_id)
49 printf("couldn't find a prog id on iface IN\n");
51 printf("program on iface IN changed, not removing\n");
53 if (ifindex_out_xdp_dummy_attached) {
55 if (bpf_get_link_xdp_id(ifindex_out, &curr_prog_id,
57 printf("bpf_get_link_xdp_id failed\n");
60 if (dummy_prog_id == curr_prog_id)
61 bpf_set_link_xdp_fd(ifindex_out, -1, xdp_flags);
62 else if (!curr_prog_id)
63 printf("couldn't find a prog id on iface OUT\n");
65 printf("program on iface OUT changed, not removing\n");
70 static void poll_stats(int interval, int ifindex)
72 unsigned int nr_cpus = bpf_num_possible_cpus();
73 __u64 values[nr_cpus], prev[nr_cpus];
75 memset(prev, 0, sizeof(prev));
83 assert(bpf_map_lookup_elem(rxcnt_map_fd, &key, values) == 0);
84 for (i = 0; i < nr_cpus; i++)
85 sum += (values[i] - prev[i]);
87 printf("ifindex %i: %10llu pkt/s\n",
88 ifindex, sum / interval);
89 memcpy(prev, values, sizeof(values));
93 static void usage(const char *prog)
96 "usage: %s [OPTS] IFINDEX_IN IFINDEX_OUT\n\n"
99 " -N enforce native mode\n"
100 " -F force loading prog\n",
104 int main(int argc, char **argv)
106 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
107 struct bpf_prog_load_attr prog_load_attr = {
108 .prog_type = BPF_PROG_TYPE_XDP,
110 struct bpf_program *prog, *dummy_prog;
111 struct bpf_prog_info info = {};
112 __u32 info_len = sizeof(info);
113 int prog_fd, dummy_prog_fd;
114 const char *optstr = "FSN";
115 struct bpf_object *obj;
116 int ret, opt, key = 0;
120 while ((opt = getopt(argc, argv, optstr)) != -1) {
123 xdp_flags |= XDP_FLAGS_SKB_MODE;
126 xdp_flags |= XDP_FLAGS_DRV_MODE;
129 xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
132 usage(basename(argv[0]));
137 if (optind == argc) {
138 printf("usage: %s IFINDEX_IN IFINDEX_OUT\n", argv[0]);
142 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
143 perror("setrlimit(RLIMIT_MEMLOCK)");
147 ifindex_in = strtoul(argv[optind], NULL, 0);
148 ifindex_out = strtoul(argv[optind + 1], NULL, 0);
149 printf("input: %d output: %d\n", ifindex_in, ifindex_out);
151 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
152 prog_load_attr.file = filename;
154 if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
157 prog = bpf_program__next(NULL, obj);
158 dummy_prog = bpf_program__next(prog, obj);
159 if (!prog || !dummy_prog) {
160 printf("finding a prog in obj file failed\n");
163 /* bpf_prog_load_xattr gives us the pointer to first prog's fd,
164 * so we're missing only the fd for dummy prog
166 dummy_prog_fd = bpf_program__fd(dummy_prog);
167 if (prog_fd < 0 || dummy_prog_fd < 0) {
168 printf("bpf_prog_load_xattr: %s\n", strerror(errno));
172 tx_port_map_fd = bpf_object__find_map_fd_by_name(obj, "tx_port");
173 rxcnt_map_fd = bpf_object__find_map_fd_by_name(obj, "rxcnt");
174 if (tx_port_map_fd < 0 || rxcnt_map_fd < 0) {
175 printf("bpf_object__find_map_fd_by_name failed\n");
179 if (bpf_set_link_xdp_fd(ifindex_in, prog_fd, xdp_flags) < 0) {
180 printf("ERROR: link set xdp fd failed on %d\n", ifindex_in);
184 ret = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
186 printf("can't get prog info - %s\n", strerror(errno));
191 /* Loading dummy XDP prog on out-device */
192 if (bpf_set_link_xdp_fd(ifindex_out, dummy_prog_fd,
193 (xdp_flags | XDP_FLAGS_UPDATE_IF_NOEXIST)) < 0) {
194 printf("WARN: link set xdp fd failed on %d\n", ifindex_out);
195 ifindex_out_xdp_dummy_attached = false;
198 memset(&info, 0, sizeof(info));
199 ret = bpf_obj_get_info_by_fd(dummy_prog_fd, &info, &info_len);
201 printf("can't get prog info - %s\n", strerror(errno));
204 dummy_prog_id = info.id;
206 signal(SIGINT, int_exit);
207 signal(SIGTERM, int_exit);
209 /* populate virtual to physical port map */
210 ret = bpf_map_update_elem(tx_port_map_fd, &key, &ifindex_out, 0);
212 perror("bpf_update_elem");
216 poll_stats(2, ifindex_out);