Skip to content

Commit 6829f3c

Browse files
bastien-curutchetMartin KaFai Lau
authored andcommitted
selftests/bpf: test_tunnel: Add generic_attach* helpers
A fair amount of code duplication is present among tests to attach BPF programs. Create generic_attach* helpers that attach BPF programs to a given interface. Use ASSERT_OK_FD() instead of ASSERT_GE() to check fd's validity. Use these helpers in all the available tests. Signed-off-by: Bastien Curutchet (eBPF Foundation) <[email protected]> Signed-off-by: Martin KaFai Lau <[email protected]> Acked-by: Stanislav Fomichev <[email protected]> Link: https://patch.msgid.link/[email protected]
1 parent 0bf6c8a commit 6829f3c

File tree

1 file changed

+66
-74
lines changed

1 file changed

+66
-74
lines changed

tools/testing/selftests/bpf/prog_tests/test_tunnel.c

Lines changed: 66 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -364,51 +364,95 @@ static int test_ping(int family, const char *addr)
364364
return -1;
365365
}
366366

367-
static int attach_tc_prog(struct bpf_tc_hook *hook, int igr_fd, int egr_fd)
367+
static int attach_tc_prog(int ifindex, int igr_fd, int egr_fd)
368368
{
369+
DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook, .ifindex = ifindex,
370+
.attach_point = BPF_TC_INGRESS | BPF_TC_EGRESS);
369371
DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts1, .handle = 1,
370372
.priority = 1, .prog_fd = igr_fd);
371373
DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts2, .handle = 1,
372374
.priority = 1, .prog_fd = egr_fd);
373375
int ret;
374376

375-
ret = bpf_tc_hook_create(hook);
377+
ret = bpf_tc_hook_create(&hook);
376378
if (!ASSERT_OK(ret, "create tc hook"))
377379
return ret;
378380

379381
if (igr_fd >= 0) {
380-
hook->attach_point = BPF_TC_INGRESS;
381-
ret = bpf_tc_attach(hook, &opts1);
382+
hook.attach_point = BPF_TC_INGRESS;
383+
ret = bpf_tc_attach(&hook, &opts1);
382384
if (!ASSERT_OK(ret, "bpf_tc_attach")) {
383-
bpf_tc_hook_destroy(hook);
385+
bpf_tc_hook_destroy(&hook);
384386
return ret;
385387
}
386388
}
387389

388390
if (egr_fd >= 0) {
389-
hook->attach_point = BPF_TC_EGRESS;
390-
ret = bpf_tc_attach(hook, &opts2);
391+
hook.attach_point = BPF_TC_EGRESS;
392+
ret = bpf_tc_attach(&hook, &opts2);
391393
if (!ASSERT_OK(ret, "bpf_tc_attach")) {
392-
bpf_tc_hook_destroy(hook);
394+
bpf_tc_hook_destroy(&hook);
393395
return ret;
394396
}
395397
}
396398

397399
return 0;
398400
}
399401

402+
static int generic_attach(const char *dev, int igr_fd, int egr_fd)
403+
{
404+
int ifindex;
405+
406+
if (!ASSERT_OK_FD(igr_fd, "check ingress fd"))
407+
return -1;
408+
if (!ASSERT_OK_FD(egr_fd, "check egress fd"))
409+
return -1;
410+
411+
ifindex = if_nametoindex(dev);
412+
if (!ASSERT_NEQ(ifindex, 0, "get ifindex"))
413+
return -1;
414+
415+
return attach_tc_prog(ifindex, igr_fd, egr_fd);
416+
}
417+
418+
static int generic_attach_igr(const char *dev, int igr_fd)
419+
{
420+
int ifindex;
421+
422+
if (!ASSERT_OK_FD(igr_fd, "check ingress fd"))
423+
return -1;
424+
425+
ifindex = if_nametoindex(dev);
426+
if (!ASSERT_NEQ(ifindex, 0, "get ifindex"))
427+
return -1;
428+
429+
return attach_tc_prog(ifindex, igr_fd, -1);
430+
}
431+
432+
static int generic_attach_egr(const char *dev, int egr_fd)
433+
{
434+
int ifindex;
435+
436+
if (!ASSERT_OK_FD(egr_fd, "check egress fd"))
437+
return -1;
438+
439+
ifindex = if_nametoindex(dev);
440+
if (!ASSERT_NEQ(ifindex, 0, "get ifindex"))
441+
return -1;
442+
443+
return attach_tc_prog(ifindex, -1, egr_fd);
444+
}
445+
400446
static void test_vxlan_tunnel(void)
401447
{
402448
struct test_tunnel_kern *skel = NULL;
403449
struct nstoken *nstoken;
404450
int local_ip_map_fd = -1;
405451
int set_src_prog_fd, get_src_prog_fd;
406452
int set_dst_prog_fd;
407-
int key = 0, ifindex = -1;
453+
int key = 0;
408454
uint local_ip;
409455
int err;
410-
DECLARE_LIBBPF_OPTS(bpf_tc_hook, tc_hook,
411-
.attach_point = BPF_TC_INGRESS);
412456

413457
/* add vxlan tunnel */
414458
err = add_vxlan_tunnel();
@@ -419,42 +463,22 @@ static void test_vxlan_tunnel(void)
419463
skel = test_tunnel_kern__open_and_load();
420464
if (!ASSERT_OK_PTR(skel, "test_tunnel_kern__open_and_load"))
421465
goto done;
422-
ifindex = if_nametoindex(VXLAN_TUNL_DEV1);
423-
if (!ASSERT_NEQ(ifindex, 0, "vxlan11 ifindex"))
424-
goto done;
425-
tc_hook.ifindex = ifindex;
426466
get_src_prog_fd = bpf_program__fd(skel->progs.vxlan_get_tunnel_src);
427467
set_src_prog_fd = bpf_program__fd(skel->progs.vxlan_set_tunnel_src);
428-
if (!ASSERT_GE(get_src_prog_fd, 0, "bpf_program__fd"))
429-
goto done;
430-
if (!ASSERT_GE(set_src_prog_fd, 0, "bpf_program__fd"))
431-
goto done;
432-
if (attach_tc_prog(&tc_hook, get_src_prog_fd, set_src_prog_fd))
468+
if (generic_attach(VXLAN_TUNL_DEV1, get_src_prog_fd, set_src_prog_fd))
433469
goto done;
434470

435471
/* load and attach bpf prog to veth dev tc hook point */
436-
ifindex = if_nametoindex("veth1");
437-
if (!ASSERT_NEQ(ifindex, 0, "veth1 ifindex"))
438-
goto done;
439-
tc_hook.ifindex = ifindex;
440472
set_dst_prog_fd = bpf_program__fd(skel->progs.veth_set_outer_dst);
441-
if (!ASSERT_GE(set_dst_prog_fd, 0, "bpf_program__fd"))
442-
goto done;
443-
if (attach_tc_prog(&tc_hook, set_dst_prog_fd, -1))
473+
if (generic_attach_igr("veth1", set_dst_prog_fd))
444474
goto done;
445475

446476
/* load and attach prog set_md to tunnel dev tc hook point at_ns0 */
447477
nstoken = open_netns("at_ns0");
448478
if (!ASSERT_OK_PTR(nstoken, "setns src"))
449479
goto done;
450-
ifindex = if_nametoindex(VXLAN_TUNL_DEV0);
451-
if (!ASSERT_NEQ(ifindex, 0, "vxlan00 ifindex"))
452-
goto done;
453-
tc_hook.ifindex = ifindex;
454480
set_dst_prog_fd = bpf_program__fd(skel->progs.vxlan_set_tunnel_dst);
455-
if (!ASSERT_GE(set_dst_prog_fd, 0, "bpf_program__fd"))
456-
goto done;
457-
if (attach_tc_prog(&tc_hook, -1, set_dst_prog_fd))
481+
if (generic_attach_egr(VXLAN_TUNL_DEV0, set_dst_prog_fd))
458482
goto done;
459483
close_netns(nstoken);
460484

@@ -488,11 +512,9 @@ static void test_ip6vxlan_tunnel(void)
488512
int local_ip_map_fd = -1;
489513
int set_src_prog_fd, get_src_prog_fd;
490514
int set_dst_prog_fd;
491-
int key = 0, ifindex = -1;
515+
int key = 0;
492516
uint local_ip;
493517
int err;
494-
DECLARE_LIBBPF_OPTS(bpf_tc_hook, tc_hook,
495-
.attach_point = BPF_TC_INGRESS);
496518

497519
/* add vxlan tunnel */
498520
err = add_ip6vxlan_tunnel();
@@ -503,31 +525,17 @@ static void test_ip6vxlan_tunnel(void)
503525
skel = test_tunnel_kern__open_and_load();
504526
if (!ASSERT_OK_PTR(skel, "test_tunnel_kern__open_and_load"))
505527
goto done;
506-
ifindex = if_nametoindex(IP6VXLAN_TUNL_DEV1);
507-
if (!ASSERT_NEQ(ifindex, 0, "ip6vxlan11 ifindex"))
508-
goto done;
509-
tc_hook.ifindex = ifindex;
510528
get_src_prog_fd = bpf_program__fd(skel->progs.ip6vxlan_get_tunnel_src);
511529
set_src_prog_fd = bpf_program__fd(skel->progs.ip6vxlan_set_tunnel_src);
512-
if (!ASSERT_GE(set_src_prog_fd, 0, "bpf_program__fd"))
513-
goto done;
514-
if (!ASSERT_GE(get_src_prog_fd, 0, "bpf_program__fd"))
515-
goto done;
516-
if (attach_tc_prog(&tc_hook, get_src_prog_fd, set_src_prog_fd))
530+
if (generic_attach(IP6VXLAN_TUNL_DEV1, get_src_prog_fd, set_src_prog_fd))
517531
goto done;
518532

519533
/* load and attach prog set_md to tunnel dev tc hook point at_ns0 */
520534
nstoken = open_netns("at_ns0");
521535
if (!ASSERT_OK_PTR(nstoken, "setns src"))
522536
goto done;
523-
ifindex = if_nametoindex(IP6VXLAN_TUNL_DEV0);
524-
if (!ASSERT_NEQ(ifindex, 0, "ip6vxlan00 ifindex"))
525-
goto done;
526-
tc_hook.ifindex = ifindex;
527537
set_dst_prog_fd = bpf_program__fd(skel->progs.ip6vxlan_set_tunnel_dst);
528-
if (!ASSERT_GE(set_dst_prog_fd, 0, "bpf_program__fd"))
529-
goto done;
530-
if (attach_tc_prog(&tc_hook, -1, set_dst_prog_fd))
538+
if (generic_attach_egr(IP6VXLAN_TUNL_DEV0, set_dst_prog_fd))
531539
goto done;
532540
close_netns(nstoken);
533541

@@ -559,10 +567,7 @@ static void test_ipip_tunnel(enum ipip_encap encap)
559567
struct test_tunnel_kern *skel = NULL;
560568
struct nstoken *nstoken;
561569
int set_src_prog_fd, get_src_prog_fd;
562-
int ifindex = -1;
563570
int err;
564-
DECLARE_LIBBPF_OPTS(bpf_tc_hook, tc_hook,
565-
.attach_point = BPF_TC_INGRESS);
566571

567572
/* add ipip tunnel */
568573
err = add_ipip_tunnel(encap);
@@ -573,10 +578,6 @@ static void test_ipip_tunnel(enum ipip_encap encap)
573578
skel = test_tunnel_kern__open_and_load();
574579
if (!ASSERT_OK_PTR(skel, "test_tunnel_kern__open_and_load"))
575580
goto done;
576-
ifindex = if_nametoindex(IPIP_TUNL_DEV1);
577-
if (!ASSERT_NEQ(ifindex, 0, "ipip11 ifindex"))
578-
goto done;
579-
tc_hook.ifindex = ifindex;
580581

581582
switch (encap) {
582583
case FOU:
@@ -598,11 +599,7 @@ static void test_ipip_tunnel(enum ipip_encap encap)
598599
skel->progs.ipip_set_tunnel);
599600
}
600601

601-
if (!ASSERT_GE(set_src_prog_fd, 0, "bpf_program__fd"))
602-
goto done;
603-
if (!ASSERT_GE(get_src_prog_fd, 0, "bpf_program__fd"))
604-
goto done;
605-
if (attach_tc_prog(&tc_hook, get_src_prog_fd, set_src_prog_fd))
602+
if (generic_attach(IPIP_TUNL_DEV1, get_src_prog_fd, set_src_prog_fd))
606603
goto done;
607604

608605
/* ping from root namespace test */
@@ -628,8 +625,6 @@ static void test_ipip_tunnel(enum ipip_encap encap)
628625

629626
static void test_xfrm_tunnel(void)
630627
{
631-
DECLARE_LIBBPF_OPTS(bpf_tc_hook, tc_hook,
632-
.attach_point = BPF_TC_INGRESS);
633628
LIBBPF_OPTS(bpf_xdp_attach_opts, opts);
634629
struct test_tunnel_kern *skel = NULL;
635630
struct nstoken *nstoken;
@@ -646,19 +641,16 @@ static void test_xfrm_tunnel(void)
646641
if (!ASSERT_OK_PTR(skel, "test_tunnel_kern__open_and_load"))
647642
goto done;
648643

649-
ifindex = if_nametoindex("veth1");
650-
if (!ASSERT_NEQ(ifindex, 0, "veth1 ifindex"))
651-
goto done;
652644

653645
/* attach tc prog to tunnel dev */
654-
tc_hook.ifindex = ifindex;
655646
tc_prog_fd = bpf_program__fd(skel->progs.xfrm_get_state);
656-
if (!ASSERT_GE(tc_prog_fd, 0, "bpf_program__fd"))
657-
goto done;
658-
if (attach_tc_prog(&tc_hook, tc_prog_fd, -1))
647+
if (generic_attach_igr("veth1", tc_prog_fd))
659648
goto done;
660649

661650
/* attach xdp prog to tunnel dev */
651+
ifindex = if_nametoindex("veth1");
652+
if (!ASSERT_NEQ(ifindex, 0, "veth1 ifindex"))
653+
goto done;
662654
xdp_prog_fd = bpf_program__fd(skel->progs.xfrm_get_state_xdp);
663655
if (!ASSERT_GE(xdp_prog_fd, 0, "bpf_program__fd"))
664656
goto done;

0 commit comments

Comments
 (0)