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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
/* SPDX-License-Identifier: GPL-2.0 */
#define _GNU_SOURCE
#include <errno.h>
#include <linux/err.h>
#include <net/if.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <unistd.h>
#include "test_utils.h"
#include "../libxdp_internal.h"
#include "xdp_dispatcher_v1.h"
#include <xdp/libxdp.h>
#include <bpf/libbpf.h>
#include <bpf/btf.h>
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
#define BPFFS_DIR "/sys/fs/bpf/xdp"
#define PROG_RUN_PRIO 42
#define PROG_CHAIN_CALL_ACTIONS (1 << XDP_DROP)
int get_prog_id(int prog_fd)
{
struct bpf_prog_info info = {};
__u32 len = sizeof(info);
int err;
err = bpf_obj_get_info_by_fd(prog_fd, &info, &len);
if (err)
return -errno;
return info.id;
}
int load_dispatcher_v1(int ifindex)
{
struct xdp_dispatcher_config_v1 dispatcher_config = {};
struct bpf_object *obj_dispatcher, *obj_prog = NULL;
DECLARE_LIBBPF_OPTS(bpf_link_create_opts, opts);
struct bpf_program *dispatcher_prog, *xdp_prog;
int ret, btf_id, lfd = -1, dispatcher_id;
char pin_path[PATH_MAX], buf[PATH_MAX];
const char *attach_func = "prog0";
struct bpf_map *map;
if (!ifindex)
return -ENOENT;
obj_dispatcher = bpf_object__open("xdp_dispatcher_v1.o");
if (!obj_dispatcher)
return -errno;
btf_id = btf__find_by_name_kind(bpf_object__btf(obj_dispatcher),
attach_func, BTF_KIND_FUNC);
if (btf_id <= 0) {
ret = -ENOENT;
goto out;
}
opts.target_btf_id = btf_id;
map = bpf_object__next_map(obj_dispatcher, NULL);
if (!map) {
ret = -ENOENT;
goto out;
}
dispatcher_prog = bpf_object__find_program_by_name(obj_dispatcher,
"xdp_dispatcher");
if (!dispatcher_prog) {
ret = -errno;
goto out;
}
dispatcher_config.num_progs_enabled = 1;
dispatcher_config.chain_call_actions[0] = PROG_CHAIN_CALL_ACTIONS;
dispatcher_config.run_prios[0] = PROG_RUN_PRIO;
ret = bpf_map__set_initial_value(map, &dispatcher_config,
sizeof(dispatcher_config));
if (ret)
goto out;
ret = bpf_object__load(obj_dispatcher);
if (ret)
goto out;
dispatcher_id = get_prog_id(bpf_program__fd(dispatcher_prog));
if (dispatcher_id < 0) {
ret = dispatcher_id;
goto out;
}
obj_prog = bpf_object__open("xdp_pass.o");
if (!obj_prog) {
ret = -errno;
goto out;
}
xdp_prog = bpf_object__find_program_by_name(obj_prog, "xdp_pass");
if (!xdp_prog) {
ret = -errno;
goto out;
}
ret = bpf_program__set_attach_target(xdp_prog,
bpf_program__fd(dispatcher_prog),
attach_func);
if (ret)
goto out;
bpf_program__set_type(xdp_prog, BPF_PROG_TYPE_EXT);
bpf_program__set_expected_attach_type(xdp_prog, 0);
ret = bpf_object__load(obj_prog);
if (ret)
goto out;
lfd = bpf_link_create(bpf_program__fd(xdp_prog),
bpf_program__fd(dispatcher_prog), 0, &opts);
if (lfd < 0) {
ret = -errno;
goto out;
}
ret = try_snprintf(pin_path, sizeof(pin_path), "%s/dispatch-%d-%d",
BPFFS_DIR, ifindex, dispatcher_id);
if (ret)
goto out;
ret = mkdir(BPFFS_DIR, S_IRWXU);
if (ret && errno != EEXIST) {
ret = -errno;
printf("mkdir err (%s): %s\n", BPFFS_DIR, strerror(-ret));
goto out;
}
ret = mkdir(pin_path, S_IRWXU);
if (ret) {
ret = -errno;
printf("mkdir err (%s): %s\n", pin_path, strerror(-ret));
goto out;
}
ret = try_snprintf(buf, sizeof(buf), "%s/prog0-link", pin_path);
if (ret)
goto err_unpin;
ret = bpf_obj_pin(lfd, buf);
if (ret)
goto err_unpin;
ret = try_snprintf(buf, sizeof(buf), "%s/prog0-prog", pin_path);
if (ret)
goto err_unpin;
ret = bpf_obj_pin(bpf_program__fd(xdp_prog), buf);
if (ret)
goto err_unpin;
ret = xdp_attach_fd(bpf_program__fd(dispatcher_prog), -1, ifindex,
XDP_MODE_NATIVE);
if (ret)
goto err_unpin;
out:
if (lfd >= 0)
close(lfd);
bpf_object__close(obj_dispatcher);
bpf_object__close(obj_prog);
return ret;
err_unpin:
if (!try_snprintf(buf, sizeof(buf), "%s/prog0-link", pin_path))
unlink(buf);
if (!try_snprintf(buf, sizeof(buf), "%s/prog0-prog", pin_path))
unlink(buf);
rmdir(pin_path);
goto out;
}
int check_old_dispatcher(int ifindex)
{
struct xdp_multiprog *mp = NULL;
struct xdp_program *xdp_prog;
char buf[100];
int ret;
ret = load_dispatcher_v1(ifindex);
if (ret)
goto out;
mp = xdp_multiprog__get_from_ifindex(ifindex);
ret = libxdp_get_error(mp);
if (ret)
goto out;
if (xdp_multiprog__is_legacy(mp)) {
printf("Got unexpected legacy multiprog\n");
ret = -EINVAL;
goto out;
}
if (xdp_multiprog__program_count(mp) != 1) {
printf("Expected 1 attached program, got %d\n",
xdp_multiprog__program_count(mp));
ret = -EINVAL;
goto out;
}
xdp_prog = xdp_multiprog__next_prog(NULL, mp);
if (!xdp_prog) {
ret = -errno;
goto out;
}
if (strcmp(xdp_program__name(xdp_prog), "xdp_pass")) {
printf("Expected xdp_pass program, got %s\n",
xdp_program__name(xdp_prog));
ret = -EINVAL;
goto out;
}
if (xdp_program__run_prio(xdp_prog) != PROG_RUN_PRIO) {
printf("Expected run prio %d got %d\n", PROG_RUN_PRIO,
xdp_program__run_prio(xdp_prog));
ret = -EINVAL;
goto out;
}
ret = xdp_program__print_chain_call_actions(xdp_prog, buf, sizeof(buf));
if (ret)
goto out;
if (strcmp(buf, "XDP_DROP")) {
printf("Expected actions XDP_PASS, got %s\n", buf);
ret = -EINVAL;
goto out;
}
xdp_prog = xdp_program__open_file("xdp_pass.o", "xdp", NULL);
ret = libxdp_get_error(xdp_prog);
if (ret)
goto out;
ret = xdp_program__attach(xdp_prog, ifindex, XDP_MODE_NATIVE, 0);
xdp_program__close(xdp_prog);
if (!ret) {
printf("Shouldn't have been able to attach a new program to ifindex!\n");
ret = -EINVAL;
goto out;
}
ret = 0;
out:
if (mp)
xdp_multiprog__detach(mp);
xdp_multiprog__close(mp);
return ret;
}
static void usage(char *progname)
{
fprintf(stderr, "Usage: %s <ifname>\n", progname);
exit(EXIT_FAILURE);
}
int main(int argc, char **argv)
{
int ifindex, ret;
char *envval;
envval = secure_getenv("VERBOSE_TESTS");
silence_libbpf_logging();
if (envval && envval[0] == '1')
verbose_libxdp_logging();
else
silence_libxdp_logging();
if (argc != 2)
usage(argv[0]);
ifindex = if_nametoindex(argv[1]);
ret = check_old_dispatcher(ifindex);
return ret;
}
|