File: test_xdp_devbound.c

package info (click to toggle)
xdp-tools 1.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,256 kB
  • sloc: ansic: 24,705; sh: 2,627; makefile: 422; python: 337; lisp: 53
file content (432 lines) | stat: -rw-r--r-- 9,464 bytes parent folder | download
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/* 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 <unistd.h>

#include "test_utils.h"

#include <xdp/libxdp.h>
#include <bpf/libbpf.h>

#define ARRAY_SIZE(_x) (sizeof(_x) / sizeof((_x)[0]))
#define EXIT_SKIPPED 249

static bool kern_compat;

static struct xdp_program *load_prog(void)
{
	DECLARE_LIBXDP_OPTS(xdp_program_opts, opts, .prog_name = "xdp_pass",
			    .find_filename = "xdp-dispatcher.o", );
	return xdp_program__create(&opts);
}

static int check_attached_progs(int ifindex, int count, bool devbound)
{
	struct xdp_multiprog *mp;
	int ret;

	mp = xdp_multiprog__get_from_ifindex(ifindex);
	ret = libxdp_get_error(mp);
	if (ret) {
		fprintf(stderr, "Couldn't get multiprog on ifindex %d: %s\n",
			ifindex, strerror(-ret));
		return ret;
	}

	ret = -EINVAL;

	if (xdp_multiprog__is_legacy(mp)) {
		fprintf(stderr, "Found legacy prog on ifindex %d\n", ifindex);
		goto out;
	}

	if (xdp_multiprog__program_count(mp) != count) {
		fprintf(stderr,
			"Expected %d programs loaded on ifindex %d, found %d\n",
			count, ifindex, xdp_multiprog__program_count(mp));
		goto out;
	}

	if (xdp_multiprog__xdp_dev_bound(mp) != devbound) {
		fprintf(stderr,
			"Multiprog on ifindex %d %s device binding, expected %s\n",
			ifindex,
			xdp_multiprog__xdp_dev_bound(mp) ? "supports" :
							   "does not support",
			devbound ? "support" : "no support");
		goto out;
	}

	ret = 0;

out:
	xdp_multiprog__close(mp);
	return ret;
}

static void print_test_result(const char *func, int ret)
{
	fflush(stderr);
	fprintf(stderr, "%s:\t%s\n", func,
		ret ? (ret == EXIT_SKIPPED ? "SKIPPED" : "FAILED") : "PASSED");
	fflush(stdout);
}

static int load_attach_prog(struct xdp_program **prog, int ifindex,
			    bool devbound)
{
	int ret;

	*prog = load_prog();
	if (!*prog) {
		ret = -errno;
		fprintf(stderr, "Couldn't load program: %s\n", strerror(-ret));
		return ret;
	}

	return xdp_program__attach(*prog, ifindex, XDP_MODE_NATIVE,
				   devbound ? XDP_ATTACH_DEVBIND : 0);
}

static int _check_load(int ifindex, bool devbound, bool should_succeed)
{
	struct xdp_program *prog = NULL;
	bool attached = false;
	int ret;

	if (!kern_compat && devbound) {
		ret = EXIT_SKIPPED;
		goto out;
	}

	ret = load_attach_prog(&prog, ifindex, devbound);
	attached = !ret;

	if (attached != should_succeed) {
		ret = -EINVAL;
		goto out;
	}

	if (should_succeed)
		ret = check_attached_progs(ifindex, 1, devbound);
	else
		ret = 0;

out:
	if (attached)
		xdp_program__detach(prog, ifindex, XDP_MODE_NATIVE, 0);
	xdp_program__close(prog);
	return ret;
}

static int check_load_devbound(int ifindex)
{
	int ret = _check_load(ifindex, true, true);
	print_test_result(__func__, ret);
	return ret;
}

static int check_load_nodevbound_success(int ifindex)
{
	int ret = _check_load(ifindex, false, true);
	print_test_result(__func__, ret);
	return ret;
}

static int check_load_devbound_multi(int ifindex)
{
	struct xdp_program *prog1 = NULL, *prog2 = NULL;
	int ret;

	if (!kern_compat) {
		ret = EXIT_SKIPPED;
		goto out;
	}

	ret = load_attach_prog(&prog1, ifindex, true);
	if (ret)
		goto out;

	ret = load_attach_prog(&prog2, ifindex, true);
	if (ret)
		goto out_prog1;

	ret = check_attached_progs(ifindex, 2, true);

	xdp_program__detach(prog2, ifindex, XDP_MODE_NATIVE, 0);
out_prog1:
	xdp_program__detach(prog1, ifindex, XDP_MODE_NATIVE, 0);
out:
	xdp_program__close(prog2);
	xdp_program__close(prog1);
	print_test_result(__func__, ret);
	return ret;
}

static int _check_load_mix(int ifindex, bool devbound1, bool devbound2)
{
	struct xdp_program *prog1 = NULL, *prog2 = NULL;
	int ret;

	if (!kern_compat && (devbound1 || devbound2)) {
		ret = EXIT_SKIPPED;
		goto out;
	}

	ret = load_attach_prog(&prog1, ifindex, devbound1);
	if (ret)
		goto out;

	/* First program attached, dispatcher supports device binding */
	ret = check_attached_progs(ifindex, 1, devbound1);
	if (ret)
		goto out;

	ret = load_attach_prog(&prog2, ifindex, devbound2);
	if (!ret) {
		xdp_program__detach(prog2, ifindex, XDP_MODE_NATIVE, 0);
		ret = -EINVAL;
		goto out_prog1;
	}

	/* Still only a single program loaded, with device binding */
	ret = check_attached_progs(ifindex, 1, devbound1);

out_prog1:
	xdp_program__detach(prog1, ifindex, XDP_MODE_NATIVE, 0);

out:
	xdp_program__close(prog2);
	xdp_program__close(prog1);
	return ret;
}

static int check_load_mix_devbound_nodevbound(int ifindex)
{
	int ret = _check_load_mix(ifindex, true, false);
	print_test_result(__func__, ret);
	return ret;
}

static int check_load_mix_nodevbound_devbound(int ifindex)
{
	int ret = _check_load_mix(ifindex, false, true);
	print_test_result(__func__, ret);
	return ret;
}

static int check_load_devbound_multiple_ifindex(int ifindex1, int ifindex2)
{
	struct xdp_program *prog = NULL;
	int ret;

	if (!kern_compat) {
		ret = EXIT_SKIPPED;
		goto out;
	}

	prog = load_prog();

	ret = xdp_program__attach(prog, ifindex1, XDP_MODE_NATIVE,
				  XDP_ATTACH_DEVBIND);
	if (ret) {
		ret = -EINVAL;
		goto out;
	}

	/* Still only a single program loaded, with device binding */
	ret = check_attached_progs(ifindex1, 1, true);
	if (ret)
		goto out;

	ret = xdp_program__attach(prog, ifindex2, XDP_MODE_NATIVE,
				  XDP_ATTACH_DEVBIND);
	if (!ret) {
		xdp_program__detach(prog, ifindex2, XDP_MODE_NATIVE, 0);
		ret = -EINVAL;
		goto out;
	}

out:
	xdp_program__detach(prog, ifindex1, XDP_MODE_NATIVE, 0);
	xdp_program__close(prog);
	print_test_result(__func__, ret == EXIT_SKIPPED ? ret : !ret);
	return !ret;
}

static int check_load_mixed_multiple_ifindex(int ifindex1, int ifindex2)
{
	struct xdp_program *prog = NULL;
	int ret;

	if (!kern_compat) {
		ret = EXIT_SKIPPED;
		goto out;
	}

	prog = load_prog();

	ret = xdp_program__attach(prog, ifindex1, XDP_MODE_NATIVE,
				  XDP_ATTACH_DEVBIND);
	if (ret)
		goto out;

	/* Still only a single program loaded, with device binding */
	ret = check_attached_progs(ifindex1, 1, true);
	if (ret)
		goto out_prog1;

	ret = xdp_program__attach(prog, ifindex2, XDP_MODE_NATIVE, 0);
	if (!ret) {
		xdp_program__detach(prog, ifindex2, XDP_MODE_NATIVE, 0);
		ret = -EINVAL;
	}

out_prog1:
	xdp_program__detach(prog, ifindex1, XDP_MODE_NATIVE, 0);
out:
	xdp_program__close(prog);
	print_test_result(__func__, ret == EXIT_SKIPPED ? ret : !ret);
	return !ret;
}

static int check_load2_mixed_multiple_ifindex(int ifindex1, int ifindex2)
{
	struct xdp_program *prog1 = NULL, *prog2 = NULL;
	int ret;

	if (!kern_compat) {
		ret = EXIT_SKIPPED;
		goto out;
	}

	ret = load_attach_prog(&prog1, ifindex1, true);
	if (ret)
		goto out;

	/* First program attached, dispatcher supports device binding */
	ret = check_attached_progs(ifindex1, 1, true);
	if (ret)
		goto out_prog1;

	ret = load_attach_prog(&prog2, ifindex2, false);
	if (ret)
		goto out_prog1;

	/* Still only a single program loaded, with device binding */
	ret = check_attached_progs(ifindex2, 1, false);

out_prog1:
	xdp_program__detach(prog1, ifindex1, XDP_MODE_NATIVE, 0);

out:
	xdp_program__detach(prog2, ifindex2, XDP_MODE_NATIVE, 0);
	xdp_program__close(prog2);
	xdp_program__close(prog1);
	print_test_result(__func__, ret);

	return ret;
}

static bool check_devbound_compat(void)
{
#ifdef HAVE_LIBBPF_BPF_PROGRAM__FLAGS
	struct xdp_program *test_prog;
	struct bpf_program *prog;
	struct bpf_object *obj;
	bool ret = false;
	int err;

	test_prog = load_prog();
	if (!test_prog)
		return false;

	obj = xdp_program__bpf_obj(test_prog);
	if (!obj)
		goto out;

	prog = bpf_object__find_program_by_name(obj, "xdp_pass");
	if (!prog)
		goto out;

	bpf_program__set_flags(prog, BPF_F_XDP_DEV_BOUND_ONLY);
	err = bpf_object__load(obj);
	if (!err) {
		printf("Kernel supports XDP programs with device binding\n");
		ret = true;
	} else {
		printf("Kernel DOES NOT support XDP programs with device binding\n");
	}
	fflush(stdout);

out:
	xdp_program__close(test_prog);
	return ret;
#else
        return -EOPNOTSUPP;
#endif
}

static void usage(char *progname)
{
	fprintf(stderr, "Usage: %s <ifname1> <ifname2>\n", progname);
	exit(EXIT_FAILURE);
}

int main(int argc, char **argv)
{
	struct rlimit r = { RLIM_INFINITY, RLIM_INFINITY };
	int ifindex1, ifindex2, ret = 0;

	if (setrlimit(RLIMIT_MEMLOCK, &r)) {
		fprintf(stderr, "ERROR: setrlimit(RLIMIT_MEMLOCK) \"%s\"\n",
			strerror(errno));
		exit(EXIT_FAILURE);
	}

	char *envval;

	envval = secure_getenv("VERBOSE_TESTS");

	silence_libbpf_logging();
	if (envval && envval[0] == '1')
		verbose_libxdp_logging();
	else
		silence_libxdp_logging();

	if (argc != 3)
		usage(argv[0]);

	ifindex1 = if_nametoindex(argv[1]);
	if (!ifindex1) {
		fprintf(stderr, "Interface '%s' not found.\n", argv[1]);
		usage(argv[0]);
	}

	ifindex2 = if_nametoindex(argv[2]);
	if (!ifindex2) {
		fprintf(stderr, "Interface '%s' not found.\n", argv[1]);
		usage(argv[0]);
	}

	kern_compat = check_devbound_compat();
	ret = check_load_devbound(ifindex1);
	ret |= check_load_nodevbound_success(ifindex1);
	ret |= check_load_devbound_multi(ifindex1);
	ret |= check_load_mix_devbound_nodevbound(ifindex1);
	ret |= check_load_mix_nodevbound_devbound(ifindex1);
	ret |= check_load_devbound_multiple_ifindex(ifindex1, ifindex2);
	ret |= check_load_mixed_multiple_ifindex(ifindex1, ifindex2);
	ret |= check_load2_mixed_multiple_ifindex(ifindex1, ifindex2);

	return ret == EXIT_SKIPPED ? 0 : ret;
}