170a00e2fSMartin KaFai Lau // SPDX-License-Identifier: GPL-2.0
270a00e2fSMartin KaFai Lau /* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
370a00e2fSMartin KaFai Lau
470a00e2fSMartin KaFai Lau #include <sys/types.h>
570a00e2fSMartin KaFai Lau #include <sys/socket.h>
670a00e2fSMartin KaFai Lau #include <net/if.h>
770a00e2fSMartin KaFai Lau
870a00e2fSMartin KaFai Lau #include "test_progs.h"
970a00e2fSMartin KaFai Lau #include "network_helpers.h"
1070a00e2fSMartin KaFai Lau #include "decap_sanity.skel.h"
1170a00e2fSMartin KaFai Lau
1270a00e2fSMartin KaFai Lau #define NS_TEST "decap_sanity_ns"
1370a00e2fSMartin KaFai Lau #define IPV6_IFACE_ADDR "face::1"
1470a00e2fSMartin KaFai Lau #define UDP_TEST_PORT 7777
1570a00e2fSMartin KaFai Lau
test_decap_sanity(void)1670a00e2fSMartin KaFai Lau void test_decap_sanity(void)
1770a00e2fSMartin KaFai Lau {
1870a00e2fSMartin KaFai Lau LIBBPF_OPTS(bpf_tc_hook, qdisc_hook, .attach_point = BPF_TC_EGRESS);
1970a00e2fSMartin KaFai Lau LIBBPF_OPTS(bpf_tc_opts, tc_attach);
2070a00e2fSMartin KaFai Lau struct nstoken *nstoken = NULL;
2170a00e2fSMartin KaFai Lau struct decap_sanity *skel;
2270a00e2fSMartin KaFai Lau struct sockaddr_in6 addr;
2370a00e2fSMartin KaFai Lau socklen_t addrlen;
2470a00e2fSMartin KaFai Lau char buf[128] = {};
2570a00e2fSMartin KaFai Lau int sockfd, err;
2670a00e2fSMartin KaFai Lau
2770a00e2fSMartin KaFai Lau skel = decap_sanity__open_and_load();
2870a00e2fSMartin KaFai Lau if (!ASSERT_OK_PTR(skel, "skel open_and_load"))
2970a00e2fSMartin KaFai Lau return;
3070a00e2fSMartin KaFai Lau
31b61987d3SHangbin Liu SYS(fail, "ip netns add %s", NS_TEST);
32b61987d3SHangbin Liu SYS(fail, "ip -net %s -6 addr add %s/128 dev lo nodad", NS_TEST, IPV6_IFACE_ADDR);
33b61987d3SHangbin Liu SYS(fail, "ip -net %s link set dev lo up", NS_TEST);
3470a00e2fSMartin KaFai Lau
3570a00e2fSMartin KaFai Lau nstoken = open_netns(NS_TEST);
3670a00e2fSMartin KaFai Lau if (!ASSERT_OK_PTR(nstoken, "open_netns"))
3770a00e2fSMartin KaFai Lau goto fail;
3870a00e2fSMartin KaFai Lau
3970a00e2fSMartin KaFai Lau qdisc_hook.ifindex = if_nametoindex("lo");
4070a00e2fSMartin KaFai Lau if (!ASSERT_GT(qdisc_hook.ifindex, 0, "if_nametoindex lo"))
4170a00e2fSMartin KaFai Lau goto fail;
4270a00e2fSMartin KaFai Lau
4370a00e2fSMartin KaFai Lau err = bpf_tc_hook_create(&qdisc_hook);
4470a00e2fSMartin KaFai Lau if (!ASSERT_OK(err, "create qdisc hook"))
4570a00e2fSMartin KaFai Lau goto fail;
4670a00e2fSMartin KaFai Lau
4770a00e2fSMartin KaFai Lau tc_attach.prog_fd = bpf_program__fd(skel->progs.decap_sanity);
4870a00e2fSMartin KaFai Lau err = bpf_tc_attach(&qdisc_hook, &tc_attach);
4970a00e2fSMartin KaFai Lau if (!ASSERT_OK(err, "attach filter"))
5070a00e2fSMartin KaFai Lau goto fail;
5170a00e2fSMartin KaFai Lau
5270a00e2fSMartin KaFai Lau addrlen = sizeof(addr);
5370a00e2fSMartin KaFai Lau err = make_sockaddr(AF_INET6, IPV6_IFACE_ADDR, UDP_TEST_PORT,
5470a00e2fSMartin KaFai Lau (void *)&addr, &addrlen);
5570a00e2fSMartin KaFai Lau if (!ASSERT_OK(err, "make_sockaddr"))
5670a00e2fSMartin KaFai Lau goto fail;
5770a00e2fSMartin KaFai Lau sockfd = socket(AF_INET6, SOCK_DGRAM, 0);
5870a00e2fSMartin KaFai Lau if (!ASSERT_NEQ(sockfd, -1, "socket"))
5970a00e2fSMartin KaFai Lau goto fail;
6070a00e2fSMartin KaFai Lau err = sendto(sockfd, buf, sizeof(buf), 0, (void *)&addr, addrlen);
6170a00e2fSMartin KaFai Lau close(sockfd);
6270a00e2fSMartin KaFai Lau if (!ASSERT_EQ(err, sizeof(buf), "send"))
6370a00e2fSMartin KaFai Lau goto fail;
6470a00e2fSMartin KaFai Lau
6570a00e2fSMartin KaFai Lau ASSERT_TRUE(skel->bss->init_csum_partial, "init_csum_partial");
6670a00e2fSMartin KaFai Lau ASSERT_TRUE(skel->bss->final_csum_none, "final_csum_none");
6770a00e2fSMartin KaFai Lau ASSERT_FALSE(skel->bss->broken_csum_start, "broken_csum_start");
6870a00e2fSMartin KaFai Lau
6970a00e2fSMartin KaFai Lau fail:
7070a00e2fSMartin KaFai Lau if (nstoken) {
7170a00e2fSMartin KaFai Lau bpf_tc_hook_destroy(&qdisc_hook);
7270a00e2fSMartin KaFai Lau close_netns(nstoken);
7370a00e2fSMartin KaFai Lau }
74*fbaf59a9SMartin KaFai Lau SYS_NOFAIL("ip netns del " NS_TEST);
7570a00e2fSMartin KaFai Lau decap_sanity__destroy(skel);
7670a00e2fSMartin KaFai Lau }
77