]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - samples/bpf/xdp_redirect_map_user.c
Merge tag 'rtc-5.1' of git://git.kernel.org/pub/scm/linux/kernel/git/abelloni/linux
[mirror_ubuntu-jammy-kernel.git] / samples / bpf / xdp_redirect_map_user.c
1 /* Copyright (c) 2017 Covalent IO, Inc. http://covalent.io
2 *
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.
6 *
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.
11 */
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>
24
25 #include "bpf_util.h"
26 #include <bpf/bpf.h>
27 #include "bpf/libbpf.h"
28
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;
34
35 static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
36 static int rxcnt_map_fd;
37
38 static void int_exit(int sig)
39 {
40 __u32 curr_prog_id = 0;
41
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);
45 }
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");
52
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);
59 }
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");
66 }
67 exit(0);
68 }
69
70 static void poll_stats(int interval, int ifindex)
71 {
72 unsigned int nr_cpus = bpf_num_possible_cpus();
73 __u64 values[nr_cpus], prev[nr_cpus];
74
75 memset(prev, 0, sizeof(prev));
76
77 while (1) {
78 __u64 sum = 0;
79 __u32 key = 0;
80 int i;
81
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));
90 }
91 }
92
93 static void usage(const char *prog)
94 {
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);
102 }
103
104 int main(int argc, char **argv)
105 {
106 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
107 struct bpf_prog_load_attr prog_load_attr = {
108 .prog_type = BPF_PROG_TYPE_XDP,
109 };
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;
117 char filename[256];
118 int tx_port_map_fd;
119
120 while ((opt = getopt(argc, argv, optstr)) != -1) {
121 switch (opt) {
122 case 'S':
123 xdp_flags |= XDP_FLAGS_SKB_MODE;
124 break;
125 case 'N':
126 xdp_flags |= XDP_FLAGS_DRV_MODE;
127 break;
128 case 'F':
129 xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
130 break;
131 default:
132 usage(basename(argv[0]));
133 return 1;
134 }
135 }
136
137 if (optind == argc) {
138 printf("usage: %s IFINDEX_IN IFINDEX_OUT\n", argv[0]);
139 return 1;
140 }
141
142 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
143 perror("setrlimit(RLIMIT_MEMLOCK)");
144 return 1;
145 }
146
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);
150
151 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
152 prog_load_attr.file = filename;
153
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));
169 return 1;
170 }
171
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");
176 return 1;
177 }
178
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);
181 return 1;
182 }
183
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
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;
196 }
197
198 memset(&info, 0, sizeof(info));
199 ret = bpf_obj_get_info_by_fd(dummy_prog_fd, &info, &info_len);
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
206 signal(SIGINT, int_exit);
207 signal(SIGTERM, int_exit);
208
209 /* populate virtual to physical port map */
210 ret = bpf_map_update_elem(tx_port_map_fd, &key, &ifindex_out, 0);
211 if (ret) {
212 perror("bpf_update_elem");
213 goto out;
214 }
215
216 poll_stats(2, ifindex_out);
217
218 out:
219 return 0;
220 }