Merge git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf
[linux-2.6-block.git] / samples / bpf / xdp_redirect_user.c
CommitLineData
5b497af4 1// SPDX-License-Identifier: GPL-2.0-only
832622e6 2/* Copyright (c) 2016 John Fastabend <john.r.fastabend@intel.com>
832622e6
JF
3 */
4#include <linux/bpf.h>
24251c26 5#include <linux/if_link.h>
832622e6
JF
6#include <assert.h>
7#include <errno.h>
8#include <signal.h>
9#include <stdio.h>
10#include <stdlib.h>
306da4e6 11#include <stdbool.h>
832622e6 12#include <string.h>
9e859e8f 13#include <net/if.h>
832622e6 14#include <unistd.h>
24251c26 15#include <libgen.h>
8ac2441e 16#include <sys/resource.h>
832622e6 17
832622e6 18#include "bpf_util.h"
2bf3e2ef 19#include <bpf/bpf.h>
4d18f6de 20#include "libbpf.h"
832622e6
JF
21
22static int ifindex_in;
23static int ifindex_out;
306da4e6 24static bool ifindex_out_xdp_dummy_attached = true;
3b7a8ec2
MF
25static __u32 prog_id;
26static __u32 dummy_prog_id;
832622e6 27
743e568c 28static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
bbaf6029 29static int rxcnt_map_fd;
24251c26 30
832622e6
JF
31static void int_exit(int sig)
32{
3b7a8ec2
MF
33 __u32 curr_prog_id = 0;
34
35 if (bpf_get_link_xdp_id(ifindex_in, &curr_prog_id, xdp_flags)) {
36 printf("bpf_get_link_xdp_id failed\n");
37 exit(1);
38 }
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");
43 else
44 printf("program on iface IN changed, not removing\n");
45
46 if (ifindex_out_xdp_dummy_attached) {
47 curr_prog_id = 0;
48 if (bpf_get_link_xdp_id(ifindex_out, &curr_prog_id,
49 xdp_flags)) {
50 printf("bpf_get_link_xdp_id failed\n");
51 exit(1);
52 }
915654fd 53 if (dummy_prog_id == curr_prog_id)
3b7a8ec2
MF
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");
57 else
58 printf("program on iface OUT changed, not removing\n");
59 }
832622e6
JF
60 exit(0);
61}
62
832622e6
JF
63static void poll_stats(int interval, int ifindex)
64{
65 unsigned int nr_cpus = bpf_num_possible_cpus();
66 __u64 values[nr_cpus], prev[nr_cpus];
67
68 memset(prev, 0, sizeof(prev));
69
70 while (1) {
71 __u64 sum = 0;
72 __u32 key = 0;
73 int i;
74
75 sleep(interval);
bbaf6029 76 assert(bpf_map_lookup_elem(rxcnt_map_fd, &key, values) == 0);
832622e6
JF
77 for (i = 0; i < nr_cpus; i++)
78 sum += (values[i] - prev[i]);
79 if (sum)
80 printf("ifindex %i: %10llu pkt/s\n",
81 ifindex, sum / interval);
82 memcpy(prev, values, sizeof(values));
83 }
84}
85
24251c26 86static void usage(const char *prog)
832622e6 87{
24251c26 88 fprintf(stderr,
9e859e8f 89 "usage: %s [OPTS] <IFNAME|IFINDEX>_IN <IFNAME|IFINDEX>_OUT\n\n"
24251c26
AG
90 "OPTS:\n"
91 " -S use skb-mode\n"
743e568c
MF
92 " -N enforce native mode\n"
93 " -F force loading prog\n",
24251c26
AG
94 prog);
95}
832622e6 96
832622e6 97
24251c26
AG
98int main(int argc, char **argv)
99{
8ac2441e 100 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
bbaf6029
MF
101 struct bpf_prog_load_attr prog_load_attr = {
102 .prog_type = BPF_PROG_TYPE_XDP,
103 };
104 struct bpf_program *prog, *dummy_prog;
105 int prog_fd, tx_port_map_fd, opt;
3b7a8ec2
MF
106 struct bpf_prog_info info = {};
107 __u32 info_len = sizeof(info);
743e568c 108 const char *optstr = "FSN";
bbaf6029 109 struct bpf_object *obj;
24251c26 110 char filename[256];
bbaf6029
MF
111 int dummy_prog_fd;
112 int ret, key = 0;
24251c26
AG
113
114 while ((opt = getopt(argc, argv, optstr)) != -1) {
115 switch (opt) {
116 case 'S':
117 xdp_flags |= XDP_FLAGS_SKB_MODE;
118 break;
119 case 'N':
120 xdp_flags |= XDP_FLAGS_DRV_MODE;
121 break;
743e568c
MF
122 case 'F':
123 xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
124 break;
24251c26
AG
125 default:
126 usage(basename(argv[0]));
127 return 1;
128 }
129 }
130
131 if (optind == argc) {
9e859e8f 132 printf("usage: %s <IFNAME|IFINDEX>_IN <IFNAME|IFINDEX>_OUT\n", argv[0]);
832622e6
JF
133 return 1;
134 }
135
8ac2441e
TD
136 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
137 perror("setrlimit(RLIMIT_MEMLOCK)");
138 return 1;
139 }
140
9e859e8f
DL
141 ifindex_in = if_nametoindex(argv[optind]);
142 if (!ifindex_in)
143 ifindex_in = strtoul(argv[optind], NULL, 0);
144
145 ifindex_out = if_nametoindex(argv[optind + 1]);
146 if (!ifindex_out)
147 ifindex_out = strtoul(argv[optind + 1], NULL, 0);
148
24251c26
AG
149 printf("input: %d output: %d\n", ifindex_in, ifindex_out);
150
151 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
bbaf6029 152 prog_load_attr.file = filename;
832622e6 153
bbaf6029
MF
154 if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
155 return 1;
156
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");
161 return 1;
162 }
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
165 */
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));
832622e6
JF
169 return 1;
170 }
171
bbaf6029
MF
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");
832622e6
JF
176 return 1;
177 }
178
bbaf6029 179 if (bpf_set_link_xdp_fd(ifindex_in, prog_fd, xdp_flags) < 0) {
306da4e6 180 printf("ERROR: link set xdp fd failed on %d\n", ifindex_in);
832622e6
JF
181 return 1;
182 }
183
3b7a8ec2
MF
184 ret = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
185 if (ret) {
186 printf("can't get prog info - %s\n", strerror(errno));
187 return ret;
188 }
189 prog_id = info.id;
190
306da4e6 191 /* Loading dummy XDP prog on out-device */
bbaf6029 192 if (bpf_set_link_xdp_fd(ifindex_out, dummy_prog_fd,
306da4e6
JDB
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;
196 }
197
3b7a8ec2 198 memset(&info, 0, sizeof(info));
20f6239d 199 ret = bpf_obj_get_info_by_fd(dummy_prog_fd, &info, &info_len);
3b7a8ec2
MF
200 if (ret) {
201 printf("can't get prog info - %s\n", strerror(errno));
202 return ret;
203 }
204 dummy_prog_id = info.id;
205
306da4e6
JDB
206 signal(SIGINT, int_exit);
207 signal(SIGTERM, int_exit);
208
832622e6 209 /* bpf redirect port */
bbaf6029 210 ret = bpf_map_update_elem(tx_port_map_fd, &key, &ifindex_out, 0);
832622e6
JF
211 if (ret) {
212 perror("bpf_update_elem");
213 goto out;
214 }
215
216 poll_stats(2, ifindex_out);
217
218out:
219 return 0;
220}