Linux 5.1.15
[linux/fpc-iii.git] / samples / bpf / xdp_redirect_user.c
blobe9054c0269ff5dcdbf3d0c4f08c37ed6b43f9e5f
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>
14 #include <assert.h>
15 #include <errno.h>
16 #include <signal.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <stdbool.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <libgen.h>
23 #include <sys/resource.h>
25 #include "bpf_util.h"
26 #include <bpf/bpf.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;
32 static __u32 prog_id;
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");
44 exit(1);
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");
50 else
51 printf("program on iface IN changed, not removing\n");
53 if (ifindex_out_xdp_dummy_attached) {
54 curr_prog_id = 0;
55 if (bpf_get_link_xdp_id(ifindex_out, &curr_prog_id,
56 xdp_flags)) {
57 printf("bpf_get_link_xdp_id failed\n");
58 exit(1);
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");
64 else
65 printf("program on iface OUT changed, not removing\n");
67 exit(0);
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));
77 while (1) {
78 __u64 sum = 0;
79 __u32 key = 0;
80 int i;
82 sleep(interval);
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]);
86 if (sum)
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)
95 fprintf(stderr,
96 "usage: %s [OPTS] IFINDEX_IN IFINDEX_OUT\n\n"
97 "OPTS:\n"
98 " -S use skb-mode\n"
99 " -N enforce native mode\n"
100 " -F force loading prog\n",
101 prog);
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;
117 char filename[256];
118 int dummy_prog_fd;
119 int ret, key = 0;
121 while ((opt = getopt(argc, argv, optstr)) != -1) {
122 switch (opt) {
123 case 'S':
124 xdp_flags |= XDP_FLAGS_SKB_MODE;
125 break;
126 case 'N':
127 xdp_flags |= XDP_FLAGS_DRV_MODE;
128 break;
129 case 'F':
130 xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
131 break;
132 default:
133 usage(basename(argv[0]));
134 return 1;
138 if (optind == argc) {
139 printf("usage: %s IFINDEX_IN IFINDEX_OUT\n", argv[0]);
140 return 1;
143 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
144 perror("setrlimit(RLIMIT_MEMLOCK)");
145 return 1;
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))
156 return 1;
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");
162 return 1;
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));
170 return 1;
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");
177 return 1;
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);
182 return 1;
185 ret = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
186 if (ret) {
187 printf("can't get prog info - %s\n", strerror(errno));
188 return ret;
190 prog_id = info.id;
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);
201 if (ret) {
202 printf("can't get prog info - %s\n", strerror(errno));
203 return ret;
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);
212 if (ret) {
213 perror("bpf_update_elem");
214 goto out;
217 poll_stats(2, ifindex_out);
219 out:
220 return 0;