1168de023SMartin KaFai Lau // SPDX-License-Identifier: GPL-2.0
2168de023SMartin KaFai Lau /* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
3168de023SMartin KaFai Lau
4168de023SMartin KaFai Lau #include <linux/rtnetlink.h>
5168de023SMartin KaFai Lau #include <sys/types.h>
6168de023SMartin KaFai Lau #include <net/if.h>
7168de023SMartin KaFai Lau
8168de023SMartin KaFai Lau #include "test_progs.h"
9168de023SMartin KaFai Lau #include "network_helpers.h"
10168de023SMartin KaFai Lau #include "fib_lookup.skel.h"
11168de023SMartin KaFai Lau
12168de023SMartin KaFai Lau #define NS_TEST "fib_lookup_ns"
13168de023SMartin KaFai Lau #define IPV6_IFACE_ADDR "face::face"
14168de023SMartin KaFai Lau #define IPV6_NUD_FAILED_ADDR "face::1"
15168de023SMartin KaFai Lau #define IPV6_NUD_STALE_ADDR "face::2"
16168de023SMartin KaFai Lau #define IPV4_IFACE_ADDR "10.0.0.254"
17168de023SMartin KaFai Lau #define IPV4_NUD_FAILED_ADDR "10.0.0.1"
18168de023SMartin KaFai Lau #define IPV4_NUD_STALE_ADDR "10.0.0.2"
19168de023SMartin KaFai Lau #define IPV4_TBID_ADDR "172.0.0.254"
20168de023SMartin KaFai Lau #define IPV4_TBID_NET "172.0.0.0"
21168de023SMartin KaFai Lau #define IPV4_TBID_DST "172.0.0.2"
22168de023SMartin KaFai Lau #define IPV6_TBID_ADDR "fd00::FFFF"
23168de023SMartin KaFai Lau #define IPV6_TBID_NET "fd00::"
24168de023SMartin KaFai Lau #define IPV6_TBID_DST "fd00::2"
25168de023SMartin KaFai Lau #define DMAC "11:11:11:11:11:11"
26168de023SMartin KaFai Lau #define DMAC_INIT { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, }
27168de023SMartin KaFai Lau #define DMAC2 "01:01:01:01:01:01"
28168de023SMartin KaFai Lau #define DMAC_INIT2 { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, }
29168de023SMartin KaFai Lau
30168de023SMartin KaFai Lau struct fib_lookup_test {
31168de023SMartin KaFai Lau const char *desc;
32168de023SMartin KaFai Lau const char *daddr;
33168de023SMartin KaFai Lau int expected_ret;
34168de023SMartin KaFai Lau int lookup_flags;
35168de023SMartin KaFai Lau __u32 tbid;
36168de023SMartin KaFai Lau __u8 dmac[6];
37168de023SMartin KaFai Lau };
38168de023SMartin KaFai Lau
39168de023SMartin KaFai Lau static const struct fib_lookup_test tests[] = {
40168de023SMartin KaFai Lau { .desc = "IPv6 failed neigh",
41168de023SMartin KaFai Lau .daddr = IPV6_NUD_FAILED_ADDR, .expected_ret = BPF_FIB_LKUP_RET_NO_NEIGH, },
42168de023SMartin KaFai Lau { .desc = "IPv6 stale neigh",
43168de023SMartin KaFai Lau .daddr = IPV6_NUD_STALE_ADDR, .expected_ret = BPF_FIB_LKUP_RET_SUCCESS,
44168de023SMartin KaFai Lau .dmac = DMAC_INIT, },
45168de023SMartin KaFai Lau { .desc = "IPv6 skip neigh",
46168de023SMartin KaFai Lau .daddr = IPV6_NUD_FAILED_ADDR, .expected_ret = BPF_FIB_LKUP_RET_SUCCESS,
47168de023SMartin KaFai Lau .lookup_flags = BPF_FIB_LOOKUP_SKIP_NEIGH, },
48168de023SMartin KaFai Lau { .desc = "IPv4 failed neigh",
49168de023SMartin KaFai Lau .daddr = IPV4_NUD_FAILED_ADDR, .expected_ret = BPF_FIB_LKUP_RET_NO_NEIGH, },
50168de023SMartin KaFai Lau { .desc = "IPv4 stale neigh",
51168de023SMartin KaFai Lau .daddr = IPV4_NUD_STALE_ADDR, .expected_ret = BPF_FIB_LKUP_RET_SUCCESS,
52168de023SMartin KaFai Lau .dmac = DMAC_INIT, },
53168de023SMartin KaFai Lau { .desc = "IPv4 skip neigh",
54b61987d3SHangbin Liu .daddr = IPV4_NUD_FAILED_ADDR, .expected_ret = BPF_FIB_LKUP_RET_SUCCESS,
55b61987d3SHangbin Liu .lookup_flags = BPF_FIB_LOOKUP_SKIP_NEIGH, },
56168de023SMartin KaFai Lau { .desc = "IPv4 TBID lookup failure",
57*a6865576SMartin KaFai Lau .daddr = IPV4_TBID_DST, .expected_ret = BPF_FIB_LKUP_RET_NOT_FWDED,
58*a6865576SMartin KaFai Lau .lookup_flags = BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_TBID,
59*a6865576SMartin KaFai Lau .tbid = RT_TABLE_MAIN, },
60*a6865576SMartin KaFai Lau { .desc = "IPv4 TBID lookup success",
61*a6865576SMartin KaFai Lau .daddr = IPV4_TBID_DST, .expected_ret = BPF_FIB_LKUP_RET_SUCCESS,
62*a6865576SMartin KaFai Lau .lookup_flags = BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_TBID, .tbid = 100,
63*a6865576SMartin KaFai Lau .dmac = DMAC_INIT2, },
64*a6865576SMartin KaFai Lau { .desc = "IPv6 TBID lookup failure",
65b61987d3SHangbin Liu .daddr = IPV6_TBID_DST, .expected_ret = BPF_FIB_LKUP_RET_NOT_FWDED,
66b61987d3SHangbin Liu .lookup_flags = BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_TBID,
67b61987d3SHangbin Liu .tbid = RT_TABLE_MAIN, },
68168de023SMartin KaFai Lau { .desc = "IPv6 TBID lookup success",
69*a6865576SMartin KaFai Lau .daddr = IPV6_TBID_DST, .expected_ret = BPF_FIB_LKUP_RET_SUCCESS,
70b61987d3SHangbin Liu .lookup_flags = BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_TBID, .tbid = 100,
71b61987d3SHangbin Liu .dmac = DMAC_INIT2, },
72168de023SMartin KaFai Lau };
73168de023SMartin KaFai Lau
74168de023SMartin KaFai Lau static int ifindex;
75168de023SMartin KaFai Lau
setup_netns(void)76168de023SMartin KaFai Lau static int setup_netns(void)
77168de023SMartin KaFai Lau {
78168de023SMartin KaFai Lau int err;
79168de023SMartin KaFai Lau
80168de023SMartin KaFai Lau SYS(fail, "ip link add veth1 type veth peer name veth2");
81168de023SMartin KaFai Lau SYS(fail, "ip link set dev veth1 up");
82168de023SMartin KaFai Lau SYS(fail, "ip link set dev veth2 up");
83168de023SMartin KaFai Lau
84168de023SMartin KaFai Lau err = write_sysctl("/proc/sys/net/ipv4/neigh/veth1/gc_stale_time", "900");
85168de023SMartin KaFai Lau if (!ASSERT_OK(err, "write_sysctl(net.ipv4.neigh.veth1.gc_stale_time)"))
86168de023SMartin KaFai Lau goto fail;
87168de023SMartin KaFai Lau
88168de023SMartin KaFai Lau err = write_sysctl("/proc/sys/net/ipv6/neigh/veth1/gc_stale_time", "900");
89168de023SMartin KaFai Lau if (!ASSERT_OK(err, "write_sysctl(net.ipv6.neigh.veth1.gc_stale_time)"))
90168de023SMartin KaFai Lau goto fail;
91168de023SMartin KaFai Lau
92168de023SMartin KaFai Lau SYS(fail, "ip addr add %s/64 dev veth1 nodad", IPV6_IFACE_ADDR);
93168de023SMartin KaFai Lau SYS(fail, "ip neigh add %s dev veth1 nud failed", IPV6_NUD_FAILED_ADDR);
94168de023SMartin KaFai Lau SYS(fail, "ip neigh add %s dev veth1 lladdr %s nud stale", IPV6_NUD_STALE_ADDR, DMAC);
95168de023SMartin KaFai Lau
96168de023SMartin KaFai Lau SYS(fail, "ip addr add %s/24 dev veth1", IPV4_IFACE_ADDR);
97168de023SMartin KaFai Lau SYS(fail, "ip neigh add %s dev veth1 nud failed", IPV4_NUD_FAILED_ADDR);
98168de023SMartin KaFai Lau SYS(fail, "ip neigh add %s dev veth1 lladdr %s nud stale", IPV4_NUD_STALE_ADDR, DMAC);
99168de023SMartin KaFai Lau
100168de023SMartin KaFai Lau /* Setup for tbid lookup tests */
101168de023SMartin KaFai Lau SYS(fail, "ip addr add %s/24 dev veth2", IPV4_TBID_ADDR);
102168de023SMartin KaFai Lau SYS(fail, "ip route del %s/24 dev veth2", IPV4_TBID_NET);
103168de023SMartin KaFai Lau SYS(fail, "ip route add table 100 %s/24 dev veth2", IPV4_TBID_NET);
104168de023SMartin KaFai Lau SYS(fail, "ip neigh add %s dev veth2 lladdr %s nud stale", IPV4_TBID_DST, DMAC2);
105168de023SMartin KaFai Lau
106168de023SMartin KaFai Lau SYS(fail, "ip addr add %s/64 dev veth2", IPV6_TBID_ADDR);
107168de023SMartin KaFai Lau SYS(fail, "ip -6 route del %s/64 dev veth2", IPV6_TBID_NET);
108168de023SMartin KaFai Lau SYS(fail, "ip -6 route add table 100 %s/64 dev veth2", IPV6_TBID_NET);
109168de023SMartin KaFai Lau SYS(fail, "ip neigh add %s dev veth2 lladdr %s nud stale", IPV6_TBID_DST, DMAC2);
110168de023SMartin KaFai Lau
111168de023SMartin KaFai Lau err = write_sysctl("/proc/sys/net/ipv4/conf/veth1/forwarding", "1");
112168de023SMartin KaFai Lau if (!ASSERT_OK(err, "write_sysctl(net.ipv4.conf.veth1.forwarding)"))
113168de023SMartin KaFai Lau goto fail;
114168de023SMartin KaFai Lau
115168de023SMartin KaFai Lau err = write_sysctl("/proc/sys/net/ipv6/conf/veth1/forwarding", "1");
116168de023SMartin KaFai Lau if (!ASSERT_OK(err, "write_sysctl(net.ipv6.conf.veth1.forwarding)"))
117168de023SMartin KaFai Lau goto fail;
118168de023SMartin KaFai Lau
119168de023SMartin KaFai Lau return 0;
120168de023SMartin KaFai Lau fail:
121168de023SMartin KaFai Lau return -1;
122168de023SMartin KaFai Lau }
123168de023SMartin KaFai Lau
set_lookup_params(struct bpf_fib_lookup * params,const struct fib_lookup_test * test)124168de023SMartin KaFai Lau static int set_lookup_params(struct bpf_fib_lookup *params, const struct fib_lookup_test *test)
125168de023SMartin KaFai Lau {
126168de023SMartin KaFai Lau int ret;
127168de023SMartin KaFai Lau
128168de023SMartin KaFai Lau memset(params, 0, sizeof(*params));
129168de023SMartin KaFai Lau
130168de023SMartin KaFai Lau params->l4_protocol = IPPROTO_TCP;
131168de023SMartin KaFai Lau params->ifindex = ifindex;
132168de023SMartin KaFai Lau params->tbid = test->tbid;
133168de023SMartin KaFai Lau
134168de023SMartin KaFai Lau if (inet_pton(AF_INET6, test->daddr, params->ipv6_dst) == 1) {
135168de023SMartin KaFai Lau params->family = AF_INET6;
136168de023SMartin KaFai Lau ret = inet_pton(AF_INET6, IPV6_IFACE_ADDR, params->ipv6_src);
137168de023SMartin KaFai Lau if (!ASSERT_EQ(ret, 1, "inet_pton(IPV6_IFACE_ADDR)"))
138168de023SMartin KaFai Lau return -1;
139168de023SMartin KaFai Lau return 0;
140168de023SMartin KaFai Lau }
141168de023SMartin KaFai Lau
142168de023SMartin KaFai Lau ret = inet_pton(AF_INET, test->daddr, ¶ms->ipv4_dst);
143b61987d3SHangbin Liu if (!ASSERT_EQ(ret, 1, "convert IP[46] address"))
144168de023SMartin KaFai Lau return -1;
145168de023SMartin KaFai Lau params->family = AF_INET;
146168de023SMartin KaFai Lau ret = inet_pton(AF_INET, IPV4_IFACE_ADDR, ¶ms->ipv4_src);
147168de023SMartin KaFai Lau if (!ASSERT_EQ(ret, 1, "inet_pton(IPV4_IFACE_ADDR)"))
148168de023SMartin KaFai Lau return -1;
149168de023SMartin KaFai Lau
150168de023SMartin KaFai Lau return 0;
151168de023SMartin KaFai Lau }
152168de023SMartin KaFai Lau
mac_str(char * b,const __u8 * mac)153168de023SMartin KaFai Lau static void mac_str(char *b, const __u8 *mac)
154168de023SMartin KaFai Lau {
155168de023SMartin KaFai Lau sprintf(b, "%02X:%02X:%02X:%02X:%02X:%02X",
156168de023SMartin KaFai Lau mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
157168de023SMartin KaFai Lau }
158168de023SMartin KaFai Lau
test_fib_lookup(void)159168de023SMartin KaFai Lau void test_fib_lookup(void)
160168de023SMartin KaFai Lau {
161168de023SMartin KaFai Lau struct bpf_fib_lookup *fib_params;
162168de023SMartin KaFai Lau struct nstoken *nstoken = NULL;
163168de023SMartin KaFai Lau struct __sk_buff skb = { };
164168de023SMartin KaFai Lau struct fib_lookup *skel;
165168de023SMartin KaFai Lau int prog_fd, err, ret, i;
166168de023SMartin KaFai Lau
167168de023SMartin KaFai Lau /* The test does not use the skb->data, so
168168de023SMartin KaFai Lau * use pkt_v6 for both v6 and v4 test.
169*a6865576SMartin KaFai Lau */
170168de023SMartin KaFai Lau LIBBPF_OPTS(bpf_test_run_opts, run_opts,
171168de023SMartin KaFai Lau .data_in = &pkt_v6,
172168de023SMartin KaFai Lau .data_size_in = sizeof(pkt_v6),
173168de023SMartin KaFai Lau .ctx_in = &skb,
174168de023SMartin KaFai Lau .ctx_size_in = sizeof(skb),
175168de023SMartin KaFai Lau );
176168de023SMartin KaFai Lau
177168de023SMartin KaFai Lau skel = fib_lookup__open_and_load();
178168de023SMartin KaFai Lau if (!ASSERT_OK_PTR(skel, "skel open_and_load"))
179168de023SMartin KaFai Lau return;
180168de023SMartin KaFai Lau prog_fd = bpf_program__fd(skel->progs.fib_lookup);
181168de023SMartin KaFai Lau
182168de023SMartin KaFai Lau SYS(fail, "ip netns add %s", NS_TEST);
183168de023SMartin KaFai Lau
184168de023SMartin KaFai Lau nstoken = open_netns(NS_TEST);
185b61987d3SHangbin Liu if (!ASSERT_OK_PTR(nstoken, "open_netns"))
186168de023SMartin KaFai Lau goto fail;
187168de023SMartin KaFai Lau
188 if (setup_netns())
189 goto fail;
190
191 ifindex = if_nametoindex("veth1");
192 skb.ifindex = ifindex;
193 fib_params = &skel->bss->fib_params;
194
195 for (i = 0; i < ARRAY_SIZE(tests); i++) {
196 printf("Testing %s ", tests[i].desc);
197
198 if (set_lookup_params(fib_params, &tests[i]))
199 continue;
200 skel->bss->fib_lookup_ret = -1;
201 skel->bss->lookup_flags = tests[i].lookup_flags;
202
203 err = bpf_prog_test_run_opts(prog_fd, &run_opts);
204 if (!ASSERT_OK(err, "bpf_prog_test_run_opts"))
205 continue;
206
207 ASSERT_EQ(skel->bss->fib_lookup_ret, tests[i].expected_ret,
208 "fib_lookup_ret");
209
210 ret = memcmp(tests[i].dmac, fib_params->dmac, sizeof(tests[i].dmac));
211 if (!ASSERT_EQ(ret, 0, "dmac not match")) {
212 char expected[18], actual[18];
213
214 mac_str(expected, tests[i].dmac);
215 mac_str(actual, fib_params->dmac);
216 printf("dmac expected %s actual %s ", expected, actual);
217 }
218
219 // ensure tbid is zero'd out after fib lookup.
220 if (tests[i].lookup_flags & BPF_FIB_LOOKUP_DIRECT) {
221 if (!ASSERT_EQ(skel->bss->fib_params.tbid, 0,
222 "expected fib_params.tbid to be zero"))
223 goto fail;
224 }
225 }
226
227 fail:
228 if (nstoken)
229 close_netns(nstoken);
230 SYS_NOFAIL("ip netns del " NS_TEST " &> /dev/null");
231 fib_lookup__destroy(skel);
232 }
233