selftests/bpf: test_tunnel: Move gre tunnel test to test_progs

gre tunnels are tested in the test_tunnel.sh but not in the test_progs
framework.

Add a new test in test_progs to test gre tunnels. It uses the same
network topology and the same BPF programs than the script.
Remove test_gre() and test_gre_no_tunnel_key() from the script.

Signed-off-by: Bastien Curutchet (eBPF Foundation) <bastien.curutchet@bootlin.com>
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
Acked-by: Stanislav Fomichev <sdf@fomichev.me>
Link: https://patch.msgid.link/20250303-tunnels-v2-3-8329f38f0678@bootlin.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
Bastien Curutchet (eBPF Foundation)
2025-03-03 09:22:51 +01:00
committed by Alexei Starovoitov
parent fcb39996a2
commit 257dfd1c6b
2 changed files with 97 additions and 79 deletions

View File

@@ -98,6 +98,9 @@
#define XFRM_SPI_IN_TO_OUT 0x1
#define XFRM_SPI_OUT_TO_IN 0x2
#define GRE_TUNL_DEV0 "gre00"
#define GRE_TUNL_DEV1 "gre11"
#define PING_ARGS "-i 0.01 -c 3 -w 10 -q"
static int config_device(void)
@@ -216,6 +219,18 @@ static int set_ipip_encap(const char *ipproto, const char *type)
return -1;
}
static int set_ipv4_addr(const char *dev0, const char *dev1)
{
SYS(fail, "ip -n at_ns0 link set dev %s up", dev0);
SYS(fail, "ip -n at_ns0 addr add dev %s %s/24", dev0, IP4_ADDR_TUNL_DEV0);
SYS(fail, "ip link set dev %s up", dev1);
SYS(fail, "ip addr add dev %s %s/24", dev1, IP4_ADDR_TUNL_DEV1);
return 0;
fail:
return 1;
}
static int add_ipip_tunnel(enum ipip_encap encap)
{
int err;
@@ -356,6 +371,31 @@ static void delete_xfrm_tunnel(void)
IP4_ADDR1_VETH1, IP4_ADDR_VETH0, XFRM_SPI_OUT_TO_IN);
}
static int add_ipv4_tunnel(const char *dev0, const char *dev1,
const char *type, const char *opt)
{
if (!type || !opt || !dev0 || !dev1)
return -1;
SYS(fail, "ip -n at_ns0 link add dev %s type %s %s local %s remote %s",
dev0, type, opt, IP4_ADDR_VETH0, IP4_ADDR1_VETH1);
SYS(fail, "ip link add dev %s type %s external", dev1, type);
return set_ipv4_addr(dev0, dev1);
fail:
return -1;
}
static void delete_tunnel(const char *dev0, const char *dev1)
{
if (!dev0 || !dev1)
return;
SYS_NOFAIL("ip netns exec at_ns0 ip link delete dev %s", dev0);
SYS_NOFAIL("ip link delete dev %s", dev1);
}
static int test_ping(int family, const char *addr)
{
SYS(fail, "%s %s %s > /dev/null", ping_command(family), PING_ARGS, addr);
@@ -677,6 +717,59 @@ static void test_xfrm_tunnel(void)
test_tunnel_kern__destroy(skel);
}
enum gre_test {
GRE,
GRE_NOKEY,
GRETAP,
GRETAP_NOKEY,
};
static void test_gre_tunnel(enum gre_test test)
{
struct test_tunnel_kern *skel;
int set_fd, get_fd;
int err;
skel = test_tunnel_kern__open_and_load();
if (!ASSERT_OK_PTR(skel, "test_tunnel_kern__open_and_load"))
return;
switch (test) {
case GRE:
err = add_ipv4_tunnel(GRE_TUNL_DEV0, GRE_TUNL_DEV1, "gre", "seq");
set_fd = bpf_program__fd(skel->progs.gre_set_tunnel_no_key);
get_fd = bpf_program__fd(skel->progs.gre_get_tunnel);
break;
case GRE_NOKEY:
err = add_ipv4_tunnel(GRE_TUNL_DEV0, GRE_TUNL_DEV1, "gre", "seq key 2");
set_fd = bpf_program__fd(skel->progs.gre_set_tunnel);
get_fd = bpf_program__fd(skel->progs.gre_get_tunnel);
break;
case GRETAP:
err = add_ipv4_tunnel(GRE_TUNL_DEV0, GRE_TUNL_DEV1, "gretap", "seq");
set_fd = bpf_program__fd(skel->progs.gre_set_tunnel_no_key);
get_fd = bpf_program__fd(skel->progs.gre_get_tunnel);
break;
case GRETAP_NOKEY:
err = add_ipv4_tunnel(GRE_TUNL_DEV0, GRE_TUNL_DEV1, "gretap", "seq key 2");
set_fd = bpf_program__fd(skel->progs.gre_set_tunnel);
get_fd = bpf_program__fd(skel->progs.gre_get_tunnel);
break;
}
if (!ASSERT_OK(err, "add tunnel"))
goto done;
if (generic_attach(GRE_TUNL_DEV1, get_fd, set_fd))
goto done;
ping_dev0();
ping_dev1();
done:
delete_tunnel(GRE_TUNL_DEV0, GRE_TUNL_DEV1);
test_tunnel_kern__destroy(skel);
}
#define RUN_TEST(name, ...) \
({ \
if (test__start_subtest(#name)) { \
@@ -694,6 +787,10 @@ static void *test_tunnel_run_tests(void *arg)
RUN_TEST(ipip_tunnel, FOU);
RUN_TEST(ipip_tunnel, GUE);
RUN_TEST(xfrm_tunnel);
RUN_TEST(gre_tunnel, GRE);
RUN_TEST(gre_tunnel, GRE_NOKEY);
RUN_TEST(gre_tunnel, GRETAP);
RUN_TEST(gre_tunnel, GRETAP_NOKEY);
return NULL;
}

View File

@@ -64,26 +64,6 @@ config_device()
ip addr add dev veth1 172.16.1.200/24
}
add_gre_tunnel()
{
tun_key=
if [ -n "$1" ]; then
tun_key="key $1"
fi
# at_ns0 namespace
ip netns exec at_ns0 \
ip link add dev $DEV_NS type $TYPE seq $tun_key \
local 172.16.1.100 remote 172.16.1.200
ip netns exec at_ns0 ip link set dev $DEV_NS up
ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
# root namespace
ip link add dev $DEV type $TYPE $tun_key external
ip link set dev $DEV up
ip addr add dev $DEV 10.1.1.200/24
}
add_ip6gretap_tunnel()
{
@@ -234,54 +214,6 @@ add_ip6tnl_tunnel()
ip link set dev $DEV up
}
test_gre()
{
TYPE=gretap
DEV_NS=gretap00
DEV=gretap11
ret=0
check $TYPE
config_device
add_gre_tunnel 2
attach_bpf $DEV gre_set_tunnel gre_get_tunnel
ping $PING_ARG 10.1.1.100
check_err $?
ip netns exec at_ns0 ping $PING_ARG 10.1.1.200
check_err $?
cleanup
if [ $ret -ne 0 ]; then
echo -e ${RED}"FAIL: $TYPE"${NC}
return 1
fi
echo -e ${GREEN}"PASS: $TYPE"${NC}
}
test_gre_no_tunnel_key()
{
TYPE=gre
DEV_NS=gre00
DEV=gre11
ret=0
check $TYPE
config_device
add_gre_tunnel
attach_bpf $DEV gre_set_tunnel_no_key gre_get_tunnel
ping $PING_ARG 10.1.1.100
check_err $?
ip netns exec at_ns0 ping $PING_ARG 10.1.1.200
check_err $?
cleanup
if [ $ret -ne 0 ]; then
echo -e ${RED}"FAIL: $TYPE"${NC}
return 1
fi
echo -e ${GREEN}"PASS: $TYPE"${NC}
}
test_ip6gre()
{
TYPE=ip6gre
@@ -538,8 +470,6 @@ cleanup()
ip link del ipip11 2> /dev/null
ip link del ipip6tnl11 2> /dev/null
ip link del ip6ip6tnl11 2> /dev/null
ip link del gretap11 2> /dev/null
ip link del gre11 2> /dev/null
ip link del ip6gre11 2> /dev/null
ip link del ip6gretap11 2> /dev/null
ip link del geneve11 2> /dev/null
@@ -567,7 +497,6 @@ check()
enable_debug()
{
echo 'file ip_gre.c +p' > /sys/kernel/debug/dynamic_debug/control
echo 'file ip6_gre.c +p' > /sys/kernel/debug/dynamic_debug/control
echo 'file geneve.c +p' > /sys/kernel/debug/dynamic_debug/control
echo 'file ipip.c +p' > /sys/kernel/debug/dynamic_debug/control
@@ -584,14 +513,6 @@ bpf_tunnel_test()
{
local errors=0
echo "Testing GRE tunnel..."
test_gre
errors=$(( $errors + $? ))
echo "Testing GRE tunnel (without tunnel keys)..."
test_gre_no_tunnel_key
errors=$(( $errors + $? ))
echo "Testing IP6GRE tunnel..."
test_ip6gre
errors=$(( $errors + $? ))