1 /* Copyright (c) 2016 John Fastabend <john.r.fastabend@intel.com>
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",
105 int main(int argc
, char **argv
)
107 struct rlimit r
= {RLIM_INFINITY
, RLIM_INFINITY
};
108 struct bpf_prog_load_attr prog_load_attr
= {
109 .prog_type
= BPF_PROG_TYPE_XDP
,
111 struct bpf_program
*prog
, *dummy_prog
;
112 int prog_fd
, tx_port_map_fd
, opt
;
113 struct bpf_prog_info info
= {};
114 __u32 info_len
= sizeof(info
);
115 const char *optstr
= "FSN";
116 struct bpf_object
*obj
;
121 while ((opt
= getopt(argc
, argv
, optstr
)) != -1) {
124 xdp_flags
|= XDP_FLAGS_SKB_MODE
;
127 xdp_flags
|= XDP_FLAGS_DRV_MODE
;
130 xdp_flags
&= ~XDP_FLAGS_UPDATE_IF_NOEXIST
;
133 usage(basename(argv
[0]));
138 if (optind
== argc
) {
139 printf("usage: %s IFINDEX_IN IFINDEX_OUT\n", argv
[0]);
143 if (setrlimit(RLIMIT_MEMLOCK
, &r
)) {
144 perror("setrlimit(RLIMIT_MEMLOCK)");
148 ifindex_in
= strtoul(argv
[optind
], NULL
, 0);
149 ifindex_out
= strtoul(argv
[optind
+ 1], NULL
, 0);
150 printf("input: %d output: %d\n", ifindex_in
, ifindex_out
);
152 snprintf(filename
, sizeof(filename
), "%s_kern.o", argv
[0]);
153 prog_load_attr
.file
= filename
;
155 if (bpf_prog_load_xattr(&prog_load_attr
, &obj
, &prog_fd
))
158 prog
= bpf_program__next(NULL
, obj
);
159 dummy_prog
= bpf_program__next(prog
, obj
);
160 if (!prog
|| !dummy_prog
) {
161 printf("finding a prog in obj file failed\n");
164 /* bpf_prog_load_xattr gives us the pointer to first prog's fd,
165 * so we're missing only the fd for dummy prog
167 dummy_prog_fd
= bpf_program__fd(dummy_prog
);
168 if (prog_fd
< 0 || dummy_prog_fd
< 0) {
169 printf("bpf_prog_load_xattr: %s\n", strerror(errno
));
173 tx_port_map_fd
= bpf_object__find_map_fd_by_name(obj
, "tx_port");
174 rxcnt_map_fd
= bpf_object__find_map_fd_by_name(obj
, "rxcnt");
175 if (tx_port_map_fd
< 0 || rxcnt_map_fd
< 0) {
176 printf("bpf_object__find_map_fd_by_name failed\n");
180 if (bpf_set_link_xdp_fd(ifindex_in
, prog_fd
, xdp_flags
) < 0) {
181 printf("ERROR: link set xdp fd failed on %d\n", ifindex_in
);
185 ret
= bpf_obj_get_info_by_fd(prog_fd
, &info
, &info_len
);
187 printf("can't get prog info - %s\n", strerror(errno
));
192 /* Loading dummy XDP prog on out-device */
193 if (bpf_set_link_xdp_fd(ifindex_out
, dummy_prog_fd
,
194 (xdp_flags
| XDP_FLAGS_UPDATE_IF_NOEXIST
)) < 0) {
195 printf("WARN: link set xdp fd failed on %d\n", ifindex_out
);
196 ifindex_out_xdp_dummy_attached
= false;
199 memset(&info
, 0, sizeof(info
));
200 ret
= bpf_obj_get_info_by_fd(prog_fd
, &info
, &info_len
);
202 printf("can't get prog info - %s\n", strerror(errno
));
205 dummy_prog_id
= info
.id
;
207 signal(SIGINT
, int_exit
);
208 signal(SIGTERM
, int_exit
);
210 /* bpf redirect port */
211 ret
= bpf_map_update_elem(tx_port_map_fd
, &key
, &ifindex_out
, 0);
213 perror("bpf_update_elem");
217 poll_stats(2, ifindex_out
);