summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/prog_tests/test_xdp_veth.c
blob: 3e98a16659362a48202318a81033fe895647ad9c (plain)
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
// SPDX-License-Identifier: GPL-2.0

/* Create 3 namespaces with 3 veth peers, and forward packets in-between using
 * native XDP
 *
 * Network topology:
 *  ----------        ----------       ----------
 *  |  NS1   |        |  NS2   |       |  NS3   |
 *  | veth11 |        | veth22 |       | veth33 |
 *  ----|-----        -----|----       -----|----
 *      |                  |                |
 *  ----|------------------|----------------|----
 *  | veth1              veth2            veth3 |
 *  |                                           |
 *  |                     NSO                   |
 *  ---------------------------------------------
 *
 * Test cases:
 *  - [test_xdp_veth_redirect] : ping veth33 from veth11
 *
 *    veth11             veth22              veth33
 *  (XDP_PASS)          (XDP_TX)           (XDP_PASS)
 *       |                  |                  |
 *       |                  |                  |
 *     veth1             veth2              veth3
 * (XDP_REDIRECT)     (XDP_REDIRECT)     (XDP_REDIRECT)
 *      ^ |                ^ |                ^ |
 *      | |                | |                | |
 *      | ------------------ ------------------ |
 *      -----------------------------------------
 *
 * - [test_xdp_veth_broadcast_redirect]: broadcast from veth11
 *     - IPv4 ping : BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS
 *          -> echo request received by all except veth11
 *     - IPv4 ping : BPF_F_BROADCAST
 *          -> echo request received by all veth
 * - [test_xdp_veth_egress]:
 *     - all src mac should be the magic mac
 *
 *    veth11             veth22              veth33
 *  (XDP_PASS)         (XDP_PASS)          (XDP_PASS)
 *       |                  |                  |
 *       |                  |                  |
 *     veth1		  veth2              veth3
 * (XDP_REDIRECT)     (XDP_REDIRECT)     (XDP_REDIRECT)
 *      |                   ^                  ^
 *      |                   |                  |
 *      ----------------------------------------
 *
 */

#define _GNU_SOURCE
#include <net/if.h>
#include "test_progs.h"
#include "network_helpers.h"
#include "xdp_dummy.skel.h"
#include "xdp_redirect_map.skel.h"
#include "xdp_redirect_multi_kern.skel.h"
#include "xdp_tx.skel.h"
#include <uapi/linux/if_link.h>

#define VETH_PAIRS_COUNT	3
#define VETH_NAME_MAX_LEN	32
#define IP_MAX_LEN		16
#define IP_SRC				"10.1.1.11"
#define IP_DST				"10.1.1.33"
#define IP_NEIGH			"10.1.1.253"
#define PROG_NAME_MAX_LEN	128
#define NS_NAME_MAX_LEN		32

struct veth_configuration {
	char local_veth[VETH_NAME_MAX_LEN]; /* Interface in main namespace */
	char remote_veth[VETH_NAME_MAX_LEN]; /* Peer interface in dedicated namespace*/
	char namespace[NS_NAME_MAX_LEN]; /* Namespace for the remote veth */
	int next_veth; /* Local interface to redirect traffic to */
	char remote_addr[IP_MAX_LEN]; /* IP address of the remote veth */
};

struct net_configuration {
	char ns0_name[NS_NAME_MAX_LEN];
	struct veth_configuration veth_cfg[VETH_PAIRS_COUNT];
};

static const struct net_configuration default_config = {
	.ns0_name = "ns0-",
	{
		{
			.local_veth = "veth1-",
			.remote_veth = "veth11",
			.next_veth = 1,
			.remote_addr = IP_SRC,
			.namespace = "ns-veth11-"
		},
		{
			.local_veth = "veth2-",
			.remote_veth = "veth22",
			.next_veth = 2,
			.remote_addr = "",
			.namespace = "ns-veth22-"
		},
		{
			.local_veth = "veth3-",
			.remote_veth = "veth33",
			.next_veth = 0,
			.remote_addr = IP_DST,
			.namespace = "ns-veth33-"
		}
	}
};

struct prog_configuration {
	char local_name[PROG_NAME_MAX_LEN]; /* BPF prog to attach to local_veth */
	char remote_name[PROG_NAME_MAX_LEN]; /* BPF prog to attach to remote_veth */
	u32 local_flags; /* XDP flags to use on local_veth */
	u32 remote_flags; /* XDP flags to use on remote_veth */
};

static int attach_programs_to_veth_pair(struct bpf_object **objs, size_t nb_obj,
					struct net_configuration *net_config,
					struct prog_configuration *prog, int index)
{
	struct bpf_program *local_prog, *remote_prog;
	struct nstoken *nstoken;
	int interface, ret, i;

	for (i = 0; i < nb_obj; i++) {
		local_prog = bpf_object__find_program_by_name(objs[i], prog[index].local_name);
		if (local_prog)
			break;
	}
	if (!ASSERT_OK_PTR(local_prog, "find local program"))
		return -1;

	for (i = 0; i < nb_obj; i++) {
		remote_prog = bpf_object__find_program_by_name(objs[i], prog[index].remote_name);
		if (remote_prog)
			break;
	}
	if (!ASSERT_OK_PTR(remote_prog, "find remote program"))
		return -1;

	interface = if_nametoindex(net_config->veth_cfg[index].local_veth);
	if (!ASSERT_NEQ(interface, 0, "non zero interface index"))
		return -1;

	ret = bpf_xdp_attach(interface, bpf_program__fd(local_prog),
			     prog[index].local_flags, NULL);
	if (!ASSERT_OK(ret, "attach xdp program to local veth"))
		return -1;

	nstoken = open_netns(net_config->veth_cfg[index].namespace);
	if (!ASSERT_OK_PTR(nstoken, "switch to remote veth namespace"))
		return -1;

	interface = if_nametoindex(net_config->veth_cfg[index].remote_veth);
	if (!ASSERT_NEQ(interface, 0, "non zero interface index")) {
		close_netns(nstoken);
		return -1;
	}

	ret = bpf_xdp_attach(interface, bpf_program__fd(remote_prog),
			     prog[index].remote_flags, NULL);
	if (!ASSERT_OK(ret, "attach xdp program to remote veth")) {
		close_netns(nstoken);
		return -1;
	}

	close_netns(nstoken);
	return 0;
}

static int create_network(struct net_configuration *net_config)
{
	struct nstoken *nstoken = NULL;
	int i, err;

	memcpy(net_config, &default_config, sizeof(struct net_configuration));

	/* Create unique namespaces */
	err = append_tid(net_config->ns0_name, NS_NAME_MAX_LEN);
	if (!ASSERT_OK(err, "append TID to ns0 name"))
		goto fail;
	SYS(fail, "ip netns add %s", net_config->ns0_name);

	for (i = 0; i < VETH_PAIRS_COUNT; i++) {
		err = append_tid(net_config->veth_cfg[i].namespace, NS_NAME_MAX_LEN);
		if (!ASSERT_OK(err, "append TID to ns name"))
			goto fail;
		SYS(fail, "ip netns add %s", net_config->veth_cfg[i].namespace);
	}

	/* Create interfaces */
	nstoken = open_netns(net_config->ns0_name);
	if (!nstoken)
		goto fail;

	for (i = 0; i < VETH_PAIRS_COUNT; i++) {
		SYS(fail, "ip link add %s type veth peer name %s netns %s",
		    net_config->veth_cfg[i].local_veth, net_config->veth_cfg[i].remote_veth,
		    net_config->veth_cfg[i].namespace);
		SYS(fail, "ip link set dev %s up", net_config->veth_cfg[i].local_veth);
		if (net_config->veth_cfg[i].remote_addr[0])
			SYS(fail, "ip -n %s addr add %s/24 dev %s",
			    net_config->veth_cfg[i].namespace,
			    net_config->veth_cfg[i].remote_addr,
			    net_config->veth_cfg[i].remote_veth);
		SYS(fail, "ip -n %s link set dev %s up", net_config->veth_cfg[i].namespace,
		    net_config->veth_cfg[i].remote_veth);
	}

	close_netns(nstoken);
	return 0;

fail:
	close_netns(nstoken);
	return -1;
}

static void cleanup_network(struct net_configuration *net_config)
{
	int i;

	SYS_NOFAIL("ip netns del %s", net_config->ns0_name);
	for (i = 0; i < VETH_PAIRS_COUNT; i++)
		SYS_NOFAIL("ip netns del %s", net_config->veth_cfg[i].namespace);
}

#define VETH_REDIRECT_SKEL_NB	3
static void xdp_veth_redirect(u32 flags)
{
	struct prog_configuration ping_config[VETH_PAIRS_COUNT] = {
		{
			.local_name = "xdp_redirect_map_0",
			.remote_name = "xdp_dummy_prog",
			.local_flags = flags,
			.remote_flags = flags,
		},
		{
			.local_name = "xdp_redirect_map_1",
			.remote_name = "xdp_tx",
			.local_flags = flags,
			.remote_flags = flags,
		},
		{
			.local_name = "xdp_redirect_map_2",
			.remote_name = "xdp_dummy_prog",
			.local_flags = flags,
			.remote_flags = flags,
		}
	};
	struct bpf_object *bpf_objs[VETH_REDIRECT_SKEL_NB];
	struct xdp_redirect_map *xdp_redirect_map;
	struct net_configuration net_config;
	struct nstoken *nstoken = NULL;
	struct xdp_dummy *xdp_dummy;
	struct xdp_tx *xdp_tx;
	int map_fd;
	int i;

	xdp_dummy = xdp_dummy__open_and_load();
	if (!ASSERT_OK_PTR(xdp_dummy, "xdp_dummy__open_and_load"))
		return;

	xdp_tx = xdp_tx__open_and_load();
	if (!ASSERT_OK_PTR(xdp_tx, "xdp_tx__open_and_load"))
		goto destroy_xdp_dummy;

	xdp_redirect_map = xdp_redirect_map__open_and_load();
	if (!ASSERT_OK_PTR(xdp_redirect_map, "xdp_redirect_map__open_and_load"))
		goto destroy_xdp_tx;

	if (!ASSERT_OK(create_network(&net_config), "create network"))
		goto destroy_xdp_redirect_map;

	/* Then configure the redirect map and attach programs to interfaces */
	map_fd = bpf_map__fd(xdp_redirect_map->maps.tx_port);
	if (!ASSERT_OK_FD(map_fd, "open redirect map"))
		goto destroy_xdp_redirect_map;

	bpf_objs[0] = xdp_dummy->obj;
	bpf_objs[1] = xdp_tx->obj;
	bpf_objs[2] = xdp_redirect_map->obj;

	nstoken = open_netns(net_config.ns0_name);
	if (!ASSERT_OK_PTR(nstoken, "open NS0"))
		goto destroy_xdp_redirect_map;

	for (i = 0; i < VETH_PAIRS_COUNT; i++) {
		int next_veth = net_config.veth_cfg[i].next_veth;
		int interface_id;
		int err;

		interface_id = if_nametoindex(net_config.veth_cfg[next_veth].local_veth);
		if (!ASSERT_NEQ(interface_id, 0, "non zero interface index"))
			goto destroy_xdp_redirect_map;
		err = bpf_map_update_elem(map_fd, &i, &interface_id, BPF_ANY);
		if (!ASSERT_OK(err, "configure interface redirection through map"))
			goto destroy_xdp_redirect_map;
		if (attach_programs_to_veth_pair(bpf_objs, VETH_REDIRECT_SKEL_NB,
						 &net_config, ping_config, i))
			goto destroy_xdp_redirect_map;
	}

	/* Test: if all interfaces are properly configured, we must be able to ping
	 * veth33 from veth11
	 */
	ASSERT_OK(SYS_NOFAIL("ip netns exec %s ping -c 1 -W 1 %s > /dev/null",
			     net_config.veth_cfg[0].namespace, IP_DST), "ping");

destroy_xdp_redirect_map:
	close_netns(nstoken);
	xdp_redirect_map__destroy(xdp_redirect_map);
destroy_xdp_tx:
	xdp_tx__destroy(xdp_tx);
destroy_xdp_dummy:
	xdp_dummy__destroy(xdp_dummy);

	cleanup_network(&net_config);
}

#define BROADCAST_REDIRECT_SKEL_NB	2
static void xdp_veth_broadcast_redirect(u32 attach_flags, u64 redirect_flags)
{
	struct prog_configuration prog_cfg[VETH_PAIRS_COUNT] = {
		{
			.local_name = "xdp_redirect_map_multi_prog",
			.remote_name = "xdp_count_0",
			.local_flags = attach_flags,
			.remote_flags = attach_flags,
		},
		{
			.local_name = "xdp_redirect_map_multi_prog",
			.remote_name = "xdp_count_1",
			.local_flags = attach_flags,
			.remote_flags = attach_flags,
		},
		{
			.local_name = "xdp_redirect_map_multi_prog",
			.remote_name = "xdp_count_2",
			.local_flags = attach_flags,
			.remote_flags = attach_flags,
		}
	};
	struct bpf_object *bpf_objs[BROADCAST_REDIRECT_SKEL_NB];
	struct xdp_redirect_multi_kern *xdp_redirect_multi_kern;
	struct xdp_redirect_map *xdp_redirect_map;
	struct bpf_devmap_val devmap_val = {};
	struct net_configuration net_config;
	struct nstoken *nstoken = NULL;
	u16 protocol = ETH_P_IP;
	int group_map;
	int flags_map;
	int cnt_map;
	u64 cnt = 0;
	int i, err;

	xdp_redirect_multi_kern = xdp_redirect_multi_kern__open_and_load();
	if (!ASSERT_OK_PTR(xdp_redirect_multi_kern, "xdp_redirect_multi_kern__open_and_load"))
		return;

	xdp_redirect_map = xdp_redirect_map__open_and_load();
	if (!ASSERT_OK_PTR(xdp_redirect_map, "xdp_redirect_map__open_and_load"))
		goto destroy_xdp_redirect_multi_kern;

	if (!ASSERT_OK(create_network(&net_config), "create network"))
		goto destroy_xdp_redirect_map;

	group_map = bpf_map__fd(xdp_redirect_multi_kern->maps.map_all);
	if (!ASSERT_OK_FD(group_map, "open map_all"))
		goto destroy_xdp_redirect_map;

	flags_map = bpf_map__fd(xdp_redirect_multi_kern->maps.redirect_flags);
	if (!ASSERT_OK_FD(group_map, "open map_all"))
		goto destroy_xdp_redirect_map;

	err = bpf_map_update_elem(flags_map, &protocol, &redirect_flags, BPF_NOEXIST);
	if (!ASSERT_OK(err, "init IP count"))
		goto destroy_xdp_redirect_map;

	cnt_map = bpf_map__fd(xdp_redirect_map->maps.rxcnt);
	if (!ASSERT_OK_FD(cnt_map, "open rxcnt map"))
		goto destroy_xdp_redirect_map;

	bpf_objs[0] = xdp_redirect_multi_kern->obj;
	bpf_objs[1] = xdp_redirect_map->obj;

	nstoken = open_netns(net_config.ns0_name);
	if (!ASSERT_OK_PTR(nstoken, "open NS0"))
		goto destroy_xdp_redirect_map;

	for (i = 0; i < VETH_PAIRS_COUNT; i++) {
		int ifindex = if_nametoindex(net_config.veth_cfg[i].local_veth);

		if (attach_programs_to_veth_pair(bpf_objs, BROADCAST_REDIRECT_SKEL_NB,
						 &net_config, prog_cfg, i))
			goto destroy_xdp_redirect_map;

		SYS(destroy_xdp_redirect_map,
		    "ip -n %s neigh add %s lladdr 00:00:00:00:00:01 dev %s",
		    net_config.veth_cfg[i].namespace, IP_NEIGH, net_config.veth_cfg[i].remote_veth);

		devmap_val.ifindex = ifindex;
		err = bpf_map_update_elem(group_map, &ifindex, &devmap_val, 0);
		if (!ASSERT_OK(err, "bpf_map_update_elem"))
			goto destroy_xdp_redirect_map;

	}

	SYS_NOFAIL("ip netns exec %s ping %s -i 0.1 -c 4 -W1 > /dev/null ",
		    net_config.veth_cfg[0].namespace, IP_NEIGH);

	for (i = 0; i < VETH_PAIRS_COUNT; i++) {
		err =  bpf_map_lookup_elem(cnt_map, &i, &cnt);
		if (!ASSERT_OK(err, "get IP cnt"))
			goto destroy_xdp_redirect_map;

		if (redirect_flags & BPF_F_EXCLUDE_INGRESS)
			/* veth11 shouldn't receive the ICMP requests;
			 * others should
			 */
			ASSERT_EQ(cnt, i ? 4 : 0, "compare IP cnt");
		else
			/* All remote veth should receive the ICMP requests */
			ASSERT_EQ(cnt, 4, "compare IP cnt");
	}

destroy_xdp_redirect_map:
	close_netns(nstoken);
	xdp_redirect_map__destroy(xdp_redirect_map);
destroy_xdp_redirect_multi_kern:
	xdp_redirect_multi_kern__destroy(xdp_redirect_multi_kern);

	cleanup_network(&net_config);
}

#define VETH_EGRESS_SKEL_NB	3
static void xdp_veth_egress(u32 flags)
{
	struct prog_configuration prog_cfg[VETH_PAIRS_COUNT] = {
		{
			.local_name = "xdp_redirect_map_all_prog",
			.remote_name = "xdp_dummy_prog",
			.local_flags = flags,
			.remote_flags = flags,
		},
		{
			.local_name = "xdp_redirect_map_all_prog",
			.remote_name = "store_mac_1",
			.local_flags = flags,
			.remote_flags = flags,
		},
		{
			.local_name = "xdp_redirect_map_all_prog",
			.remote_name = "store_mac_2",
			.local_flags = flags,
			.remote_flags = flags,
		}
	};
	const char magic_mac[6] = { 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF};
	struct xdp_redirect_multi_kern *xdp_redirect_multi_kern;
	struct bpf_object *bpf_objs[VETH_EGRESS_SKEL_NB];
	struct xdp_redirect_map *xdp_redirect_map;
	struct bpf_devmap_val devmap_val = {};
	struct net_configuration net_config;
	int mac_map, egress_map, res_map;
	struct nstoken *nstoken = NULL;
	struct xdp_dummy *xdp_dummy;
	int err;
	int i;

	xdp_dummy = xdp_dummy__open_and_load();
	if (!ASSERT_OK_PTR(xdp_dummy, "xdp_dummy__open_and_load"))
		return;

	xdp_redirect_multi_kern = xdp_redirect_multi_kern__open_and_load();
	if (!ASSERT_OK_PTR(xdp_redirect_multi_kern, "xdp_redirect_multi_kern__open_and_load"))
		goto destroy_xdp_dummy;

	xdp_redirect_map = xdp_redirect_map__open_and_load();
	if (!ASSERT_OK_PTR(xdp_redirect_map, "xdp_redirect_map__open_and_load"))
		goto destroy_xdp_redirect_multi_kern;

	if (!ASSERT_OK(create_network(&net_config), "create network"))
		goto destroy_xdp_redirect_map;

	mac_map = bpf_map__fd(xdp_redirect_multi_kern->maps.mac_map);
	if (!ASSERT_OK_FD(mac_map, "open mac_map"))
		goto destroy_xdp_redirect_map;

	egress_map = bpf_map__fd(xdp_redirect_multi_kern->maps.map_egress);
	if (!ASSERT_OK_FD(egress_map, "open map_egress"))
		goto destroy_xdp_redirect_map;

	devmap_val.bpf_prog.fd = bpf_program__fd(xdp_redirect_multi_kern->progs.xdp_devmap_prog);

	bpf_objs[0] = xdp_dummy->obj;
	bpf_objs[1] = xdp_redirect_multi_kern->obj;
	bpf_objs[2] = xdp_redirect_map->obj;

	nstoken = open_netns(net_config.ns0_name);
	if (!ASSERT_OK_PTR(nstoken, "open NS0"))
		goto destroy_xdp_redirect_map;

	for (i = 0; i < VETH_PAIRS_COUNT; i++) {
		int ifindex = if_nametoindex(net_config.veth_cfg[i].local_veth);

		SYS(destroy_xdp_redirect_map,
		    "ip -n %s neigh add %s lladdr 00:00:00:00:00:01 dev %s",
		    net_config.veth_cfg[i].namespace, IP_NEIGH, net_config.veth_cfg[i].remote_veth);

		if (attach_programs_to_veth_pair(bpf_objs, VETH_REDIRECT_SKEL_NB,
						 &net_config, prog_cfg, i))
			goto destroy_xdp_redirect_map;

		err = bpf_map_update_elem(mac_map, &ifindex, magic_mac, 0);
		if (!ASSERT_OK(err, "bpf_map_update_elem"))
			goto destroy_xdp_redirect_map;

		devmap_val.ifindex = ifindex;
		err = bpf_map_update_elem(egress_map, &ifindex, &devmap_val, 0);
		if (!ASSERT_OK(err, "bpf_map_update_elem"))
			goto destroy_xdp_redirect_map;
	}

	SYS_NOFAIL("ip netns exec %s ping %s -i 0.1 -c 4 -W1 > /dev/null ",
		    net_config.veth_cfg[0].namespace, IP_NEIGH);

	res_map = bpf_map__fd(xdp_redirect_map->maps.rx_mac);
	if (!ASSERT_OK_FD(res_map, "open rx_map"))
		goto destroy_xdp_redirect_map;

	for (i = 0; i < 2; i++) {
		u32 key = i;
		u64 res;

		err = bpf_map_lookup_elem(res_map, &key, &res);
		if (!ASSERT_OK(err, "get MAC res"))
			goto destroy_xdp_redirect_map;

		ASSERT_STRNEQ((const char *)&res, magic_mac, ETH_ALEN, "compare mac");
	}

destroy_xdp_redirect_map:
	close_netns(nstoken);
	xdp_redirect_map__destroy(xdp_redirect_map);
destroy_xdp_redirect_multi_kern:
	xdp_redirect_multi_kern__destroy(xdp_redirect_multi_kern);
destroy_xdp_dummy:
	xdp_dummy__destroy(xdp_dummy);

	cleanup_network(&net_config);
}

void test_xdp_veth_redirect(void)
{
	if (test__start_subtest("0"))
		xdp_veth_redirect(0);

	if (test__start_subtest("DRV_MODE"))
		xdp_veth_redirect(XDP_FLAGS_DRV_MODE);

	if (test__start_subtest("SKB_MODE"))
		xdp_veth_redirect(XDP_FLAGS_SKB_MODE);
}

void test_xdp_veth_broadcast_redirect(void)
{
	if (test__start_subtest("0/BROADCAST"))
		xdp_veth_broadcast_redirect(0, BPF_F_BROADCAST);

	if (test__start_subtest("0/(BROADCAST | EXCLUDE_INGRESS)"))
		xdp_veth_broadcast_redirect(0, BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS);

	if (test__start_subtest("DRV_MODE/BROADCAST"))
		xdp_veth_broadcast_redirect(XDP_FLAGS_DRV_MODE, BPF_F_BROADCAST);

	if (test__start_subtest("DRV_MODE/(BROADCAST | EXCLUDE_INGRESS)"))
		xdp_veth_broadcast_redirect(XDP_FLAGS_DRV_MODE,
					    BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS);

	if (test__start_subtest("SKB_MODE/BROADCAST"))
		xdp_veth_broadcast_redirect(XDP_FLAGS_SKB_MODE, BPF_F_BROADCAST);

	if (test__start_subtest("SKB_MODE/(BROADCAST | EXCLUDE_INGRESS)"))
		xdp_veth_broadcast_redirect(XDP_FLAGS_SKB_MODE,
					    BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS);
}

void test_xdp_veth_egress(void)
{
	if (test__start_subtest("0/egress"))
		xdp_veth_egress(0);

	if (test__start_subtest("DRV_MODE/egress"))
		xdp_veth_egress(XDP_FLAGS_DRV_MODE);

	if (test__start_subtest("SKB_MODE/egress"))
		xdp_veth_egress(XDP_FLAGS_SKB_MODE);
}