Skip to content

Commit 7289e59

Browse files
bastien-curutchetMartin KaFai Lau
authored andcommitted
selftests/bpf: test_tunnel: Add ping helpers
All tests use more or less the same ping commands as final validation. Also test_ping()'s return value is checked with ASSERT_OK() while this check is already done by the SYS() macro inside test_ping(). Create helpers around test_ping() and use them in the tests to avoid code duplication. Remove the unnecessary ASSERT_OK() from the 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 6829f3c commit 7289e59

File tree

1 file changed

+24
-29
lines changed

1 file changed

+24
-29
lines changed

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

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

367+
static void ping_dev0(void)
368+
{
369+
/* ping from root namespace test */
370+
test_ping(AF_INET, IP4_ADDR_TUNL_DEV0);
371+
}
372+
373+
static void ping_dev1(void)
374+
{
375+
struct nstoken *nstoken;
376+
377+
/* ping from at_ns0 namespace test */
378+
nstoken = open_netns("at_ns0");
379+
if (!ASSERT_OK_PTR(nstoken, "setns"))
380+
return;
381+
382+
test_ping(AF_INET, IP4_ADDR_TUNL_DEV1);
383+
close_netns(nstoken);
384+
}
385+
367386
static int attach_tc_prog(int ifindex, int igr_fd, int egr_fd)
368387
{
369388
DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook, .ifindex = ifindex,
@@ -492,9 +511,7 @@ static void test_vxlan_tunnel(void)
492511
goto done;
493512

494513
/* ping test */
495-
err = test_ping(AF_INET, IP4_ADDR_TUNL_DEV0);
496-
if (!ASSERT_OK(err, "test_ping"))
497-
goto done;
514+
ping_dev0();
498515

499516
done:
500517
/* delete vxlan tunnel */
@@ -549,9 +566,7 @@ static void test_ip6vxlan_tunnel(void)
549566
goto done;
550567

551568
/* ping test */
552-
err = test_ping(AF_INET, IP4_ADDR_TUNL_DEV0);
553-
if (!ASSERT_OK(err, "test_ping"))
554-
goto done;
569+
ping_dev0();
555570

556571
done:
557572
/* delete ipv6 vxlan tunnel */
@@ -565,7 +580,6 @@ static void test_ip6vxlan_tunnel(void)
565580
static void test_ipip_tunnel(enum ipip_encap encap)
566581
{
567582
struct test_tunnel_kern *skel = NULL;
568-
struct nstoken *nstoken;
569583
int set_src_prog_fd, get_src_prog_fd;
570584
int err;
571585

@@ -602,19 +616,8 @@ static void test_ipip_tunnel(enum ipip_encap encap)
602616
if (generic_attach(IPIP_TUNL_DEV1, get_src_prog_fd, set_src_prog_fd))
603617
goto done;
604618

605-
/* ping from root namespace test */
606-
err = test_ping(AF_INET, IP4_ADDR_TUNL_DEV0);
607-
if (!ASSERT_OK(err, "test_ping"))
608-
goto done;
609-
610-
/* ping from at_ns0 namespace test */
611-
nstoken = open_netns("at_ns0");
612-
if (!ASSERT_OK_PTR(nstoken, "setns"))
613-
goto done;
614-
err = test_ping(AF_INET, IP4_ADDR_TUNL_DEV1);
615-
if (!ASSERT_OK(err, "test_ping"))
616-
goto done;
617-
close_netns(nstoken);
619+
ping_dev0();
620+
ping_dev1();
618621

619622
done:
620623
/* delete ipip tunnel */
@@ -627,7 +630,6 @@ static void test_xfrm_tunnel(void)
627630
{
628631
LIBBPF_OPTS(bpf_xdp_attach_opts, opts);
629632
struct test_tunnel_kern *skel = NULL;
630-
struct nstoken *nstoken;
631633
int xdp_prog_fd;
632634
int tc_prog_fd;
633635
int ifindex;
@@ -658,14 +660,7 @@ static void test_xfrm_tunnel(void)
658660
if (!ASSERT_OK(err, "bpf_xdp_attach"))
659661
goto done;
660662

661-
/* ping from at_ns0 namespace test */
662-
nstoken = open_netns("at_ns0");
663-
if (!ASSERT_OK_PTR(nstoken, "setns"))
664-
goto done;
665-
err = test_ping(AF_INET, IP4_ADDR_TUNL_DEV1);
666-
close_netns(nstoken);
667-
if (!ASSERT_OK(err, "test_ping"))
668-
goto done;
663+
ping_dev1();
669664

670665
if (!ASSERT_EQ(skel->bss->xfrm_reqid, 1, "req_id"))
671666
goto done;

0 commit comments

Comments
 (0)