-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy pathxdp_redirect_devmap.c
More file actions
245 lines (212 loc) · 6.94 KB
/
xdp_redirect_devmap.c
File metadata and controls
245 lines (212 loc) · 6.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// SPDX-License-Identifier: GPL-2.0-only
/* Copyright (c) 2017 Covalent IO, Inc. http://covalent.io
*/
#include <errno.h>
#include <stdio.h>
#include <assert.h>
#include <getopt.h>
#include <libgen.h>
#include <net/if.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <bpf/bpf.h>
#include <stdbool.h>
#include <linux/bpf.h>
#include <bpf/libbpf.h>
#include <xdp/libxdp.h>
#include <linux/if_link.h>
#include "logging.h"
#include "xdp-bench.h"
#include "xdp_sample.h"
#include "xdp_redirect_devmap.skel.h"
static int mask = SAMPLE_RX_CNT |
SAMPLE_EXCEPTION_CNT | SAMPLE_DEVMAP_XMIT_CNT_MULTI;
DEFINE_SAMPLE_INIT(xdp_redirect_devmap);
const struct devmap_opts defaults_redirect_devmap = { .mode = XDP_MODE_NATIVE,
.interval = 2 };
static struct bpf_program *egress_prog(struct xdp_redirect_devmap *skel,
enum devmap_egress_action action)
{
switch (action) {
case DEVMAP_EGRESS_DROP:
return skel->progs.xdp_redirect_devmap_egress_drop;
case DEVMAP_EGRESS_NONE:
case DEVMAP_EGRESS_FORWARD:
default:
return skel->progs.xdp_redirect_devmap_egress;
}
}
int do_redirect_devmap(const void *cfg, __unused const char *pin_root_path)
{
const struct devmap_opts *opt = cfg;
struct xdp_program *xdp_prog = NULL, *dummy_prog = NULL;
const char *prog_name = "redir_devmap_native";
DECLARE_LIBBPF_OPTS(xdp_program_opts, opts);
struct bpf_devmap_val devmap_val = {};
struct bpf_map *tx_port_map = NULL;
struct xdp_redirect_devmap *skel;
struct bpf_program *prog = NULL;
char str[2 * IF_NAMESIZE + 1];
int ret = EXIT_FAIL_OPTION;
bool tried = false;
int key = 0;
if (opt->extended)
sample_switch_mode();
if (opt->mode == XDP_MODE_SKB)
/* devmap_xmit tracepoint not available */
mask &= ~(SAMPLE_DEVMAP_XMIT_CNT |
SAMPLE_DEVMAP_XMIT_CNT_MULTI);
if (opt->stats)
mask |= SAMPLE_REDIRECT_CNT;
if (!opt->load_egress && opt->egress_action != DEVMAP_EGRESS_NONE) {
pr_warn("egress-action option was set without load-egress\n");
goto end;
}
restart:
skel = xdp_redirect_devmap__open();
if (!skel) {
pr_warn("Failed to xdp_redirect_devmap__open: %s\n",
strerror(errno));
ret = EXIT_FAIL_BPF;
goto end;
}
/* Make sure we only load the one XDP program we are interested in */
while ((prog = bpf_object__next_program(skel->obj, prog)) != NULL)
if (bpf_program__type(prog) == BPF_PROG_TYPE_XDP &&
bpf_program__expected_attach_type(prog) == BPF_XDP)
bpf_program__set_autoload(prog, false);
if (tried) {
tx_port_map = skel->maps.tx_port_general;
bpf_program__set_autoload(egress_prog(skel, opt->egress_action), false);
#ifdef HAVE_LIBBPF_BPF_MAP__SET_AUTOCREATE
bpf_map__set_autocreate(skel->maps.tx_port_native, false);
#else
pr_warn("Libbpf is missing bpf_map__set_autocreate(), fallback won't work\n");
ret = EXIT_FAIL_BPF;
goto end_destroy;
#endif
} else {
#ifdef HAVE_LIBBPF_BPF_MAP__SET_AUTOCREATE
bpf_map__set_autocreate(skel->maps.tx_port_general, false);
#endif
tx_port_map = skel->maps.tx_port_native;
}
ret = sample_init_pre_load(skel, opt->iface_in.ifname);
if (ret < 0) {
pr_warn("Failed to sample_init_pre_load: %s\n", strerror(-ret));
ret = EXIT_FAIL_BPF;
goto end_destroy;
}
/* Load 2nd xdp prog on egress. */
if (opt->load_egress) {
ret = get_mac_addr(opt->iface_out.ifindex, skel->rodata->tx_mac_addr);
if (ret < 0) {
pr_warn("Failed to get interface %s mac address: %s\n",
opt->iface_out.ifname, strerror(-ret));
ret = EXIT_FAIL;
goto end_destroy;
}
}
skel->rodata->from_match[0] = opt->iface_in.ifindex;
skel->rodata->to_match[0] = opt->iface_out.ifindex;
opts.obj = skel->obj;
opts.prog_name = prog_name;
xdp_prog = xdp_program__create(&opts);
if (!xdp_prog) {
ret = -errno;
pr_warn("Couldn't open XDP program: %s\n",
strerror(-ret));
goto end_destroy;
}
/* We always set the frags support bit: nothing the program does is
* incompatible with multibuf, and it's perfectly fine to load a program
* with frags support on an interface with a small MTU. We don't risk
* setting any flags the kernel will balk at, either, since libxdp will
* do the feature probing for us and skip the flag if the kernel doesn't
* support it.
*
* The function below returns EOPNOTSUPP it libbpf is too old to support
* setting the flags, but we just ignore that, since in such a case the
* best we can do is just attempt to run without the frags support.
*/
xdp_program__set_xdp_frags_support(xdp_prog, true);
ret = xdp_program__attach(xdp_prog, opt->iface_in.ifindex, opt->mode, 0);
if (ret < 0) {
/* First try with struct bpf_devmap_val as value for generic
* mode, then fallback to sizeof(int) for older kernels.
*/
if (!opt->load_egress && !tried) {
pr_warn("Attempting fallback to int-sized devmap\n");
prog_name = "redir_devmap_general";
tried = true;
xdp_program__close(xdp_prog);
xdp_redirect_devmap__destroy(skel);
sample_teardown();
xdp_prog = NULL;
goto restart;
}
pr_warn("Failed to attach XDP program: %s\n",
strerror(-ret));
ret = EXIT_FAIL_XDP;
goto end_destroy;
}
ret = sample_init(skel, mask, opt->iface_in.ifindex, opt->iface_out.ifindex);
if (ret < 0) {
pr_warn("Failed to initialize sample: %s\n", strerror(-ret));
ret = EXIT_FAIL;
goto end_detach;
}
opts.obj = NULL;
opts.prog_name = "xdp_pass";
opts.find_filename = "xdp-dispatcher.o";
dummy_prog = xdp_program__create(&opts);
if (!dummy_prog) {
pr_warn("Failed to load dummy program: %s\n", strerror(errno));
ret = EXIT_FAIL_BPF;
goto end_detach;
}
xdp_program__set_xdp_frags_support(dummy_prog, true);
ret = xdp_program__attach(dummy_prog, opt->iface_out.ifindex, opt->mode, 0);
if (ret < 0) {
pr_warn("Failed to attach dummy program: %s\n", strerror(-ret));
ret = EXIT_FAIL_BPF;
goto end_detach;
}
devmap_val.ifindex = opt->iface_out.ifindex;
if (opt->load_egress) {
struct bpf_program *prog = egress_prog(skel, opt->egress_action);
devmap_val.bpf_prog.fd = bpf_program__fd(prog);
}
ret = bpf_map_update_elem(bpf_map__fd(tx_port_map), &key, &devmap_val, 0);
if (ret < 0) {
pr_warn("Failed to update devmap value: %s\n",
strerror(errno));
ret = EXIT_FAIL_BPF;
goto end_detach;
}
ret = EXIT_FAIL;
safe_strncpy(str, get_driver_name(opt->iface_in.ifindex), sizeof(str));
pr_info("Redirecting from %s (ifindex %d; driver %s) to %s (ifindex %d; driver %s)\n",
opt->iface_in.ifname, opt->iface_in.ifindex, str,
opt->iface_out.ifname, opt->iface_out.ifindex, get_driver_name(opt->iface_out.ifindex));
ret = sample_run(opt->interval, NULL, NULL);
if (ret < 0) {
pr_warn("Failed during sample run: %s\n", strerror(-ret));
ret = EXIT_FAIL;
goto end_destroy;
}
ret = EXIT_OK;
end_detach:
if (dummy_prog)
xdp_program__detach(dummy_prog, opt->iface_out.ifindex, opt->mode, 0);
xdp_program__detach(xdp_prog, opt->iface_in.ifindex, opt->mode, 0);
end_destroy:
xdp_program__close(xdp_prog);
xdp_program__close(dummy_prog);
xdp_redirect_devmap__destroy(skel);
end:
sample_teardown();
return ret;
}