1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include <network_helpers.h>
4 #include <net/if.h>
5 #include <linux/if_ether.h>
6 #include <linux/if_packet.h>
7 #include <linux/if_link.h>
8 #include <linux/ipv6.h>
9 #include <linux/in6.h>
10 #include <linux/udp.h>
11 #include <bpf/bpf_endian.h>
12 #include <uapi/linux/netdev.h>
13 #include "test_xdp_do_redirect.skel.h"
14
15 #define SYS(fmt, ...) \
16 ({ \
17 char cmd[1024]; \
18 snprintf(cmd, sizeof(cmd), fmt, ##__VA_ARGS__); \
19 if (!ASSERT_OK(system(cmd), cmd)) \
20 goto out; \
21 })
22
23 struct udp_packet {
24 struct ethhdr eth;
25 struct ipv6hdr iph;
26 struct udphdr udp;
27 __u8 payload[64 - sizeof(struct udphdr)
28 - sizeof(struct ethhdr) - sizeof(struct ipv6hdr)];
29 } __packed;
30
31 static struct udp_packet pkt_udp = {
32 .eth.h_proto = __bpf_constant_htons(ETH_P_IPV6),
33 .eth.h_dest = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55},
34 .eth.h_source = {0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb},
35 .iph.version = 6,
36 .iph.nexthdr = IPPROTO_UDP,
37 .iph.payload_len = bpf_htons(sizeof(struct udp_packet)
38 - offsetof(struct udp_packet, udp)),
39 .iph.hop_limit = 2,
40 .iph.saddr.s6_addr16 = {bpf_htons(0xfc00), 0, 0, 0, 0, 0, 0, bpf_htons(1)},
41 .iph.daddr.s6_addr16 = {bpf_htons(0xfc00), 0, 0, 0, 0, 0, 0, bpf_htons(2)},
42 .udp.source = bpf_htons(1),
43 .udp.dest = bpf_htons(1),
44 .udp.len = bpf_htons(sizeof(struct udp_packet)
45 - offsetof(struct udp_packet, udp)),
46 .payload = {0x42}, /* receiver XDP program matches on this */
47 };
48
attach_tc_prog(struct bpf_tc_hook * hook,int fd)49 static int attach_tc_prog(struct bpf_tc_hook *hook, int fd)
50 {
51 DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, .handle = 1, .priority = 1, .prog_fd = fd);
52 int ret;
53
54 ret = bpf_tc_hook_create(hook);
55 if (!ASSERT_OK(ret, "create tc hook"))
56 return ret;
57
58 ret = bpf_tc_attach(hook, &opts);
59 if (!ASSERT_OK(ret, "bpf_tc_attach")) {
60 bpf_tc_hook_destroy(hook);
61 return ret;
62 }
63
64 return 0;
65 }
66
67 /* The maximum permissible size is: PAGE_SIZE - sizeof(struct xdp_page_head) -
68 * SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) - XDP_PACKET_HEADROOM =
69 * 3408 bytes for 64-byte cacheline and 3216 for 256-byte one.
70 */
71 #if defined(__s390x__)
72 #define MAX_PKT_SIZE 3216
73 #else
74 #define MAX_PKT_SIZE 3408
75 #endif
test_max_pkt_size(int fd)76 static void test_max_pkt_size(int fd)
77 {
78 char data[MAX_PKT_SIZE + 1] = {};
79 int err;
80 DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts,
81 .data_in = &data,
82 .data_size_in = MAX_PKT_SIZE,
83 .flags = BPF_F_TEST_XDP_LIVE_FRAMES,
84 .repeat = 1,
85 );
86 err = bpf_prog_test_run_opts(fd, &opts);
87 ASSERT_OK(err, "prog_run_max_size");
88
89 opts.data_size_in += 1;
90 err = bpf_prog_test_run_opts(fd, &opts);
91 ASSERT_EQ(err, -EINVAL, "prog_run_too_big");
92 }
93
94 #define NUM_PKTS 10000
test_xdp_do_redirect(void)95 void test_xdp_do_redirect(void)
96 {
97 int err, xdp_prog_fd, tc_prog_fd, ifindex_src, ifindex_dst;
98 char data[sizeof(pkt_udp) + sizeof(__u32)];
99 struct test_xdp_do_redirect *skel = NULL;
100 struct nstoken *nstoken = NULL;
101 struct bpf_link *link;
102 LIBBPF_OPTS(bpf_xdp_query_opts, query_opts);
103 struct xdp_md ctx_in = { .data = sizeof(__u32),
104 .data_end = sizeof(data) };
105 DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts,
106 .data_in = &data,
107 .data_size_in = sizeof(data),
108 .ctx_in = &ctx_in,
109 .ctx_size_in = sizeof(ctx_in),
110 .flags = BPF_F_TEST_XDP_LIVE_FRAMES,
111 .repeat = NUM_PKTS,
112 .batch_size = 64,
113 );
114 DECLARE_LIBBPF_OPTS(bpf_tc_hook, tc_hook,
115 .attach_point = BPF_TC_INGRESS);
116
117 memcpy(&data[sizeof(__u32)], &pkt_udp, sizeof(pkt_udp));
118 *((__u32 *)data) = 0x42; /* metadata test value */
119
120 skel = test_xdp_do_redirect__open();
121 if (!ASSERT_OK_PTR(skel, "skel"))
122 return;
123
124 /* The XDP program we run with bpf_prog_run() will cycle through all
125 * three xmit (PASS/TX/REDIRECT) return codes starting from above, and
126 * ending up with PASS, so we should end up with two packets on the dst
127 * iface and NUM_PKTS-2 in the TC hook. We match the packets on the UDP
128 * payload.
129 */
130 SYS("ip netns add testns");
131 nstoken = open_netns("testns");
132 if (!ASSERT_OK_PTR(nstoken, "setns"))
133 goto out;
134
135 SYS("ip link add veth_src type veth peer name veth_dst");
136 SYS("ip link set dev veth_src address 00:11:22:33:44:55");
137 SYS("ip link set dev veth_dst address 66:77:88:99:aa:bb");
138 SYS("ip link set dev veth_src up");
139 SYS("ip link set dev veth_dst up");
140 SYS("ip addr add dev veth_src fc00::1/64");
141 SYS("ip addr add dev veth_dst fc00::2/64");
142 SYS("ip neigh add fc00::2 dev veth_src lladdr 66:77:88:99:aa:bb");
143
144 /* We enable forwarding in the test namespace because that will cause
145 * the packets that go through the kernel stack (with XDP_PASS) to be
146 * forwarded back out the same interface (because of the packet dst
147 * combined with the interface addresses). When this happens, the
148 * regular forwarding path will end up going through the same
149 * veth_xdp_xmit() call as the XDP_REDIRECT code, which can cause a
150 * deadlock if it happens on the same CPU. There's a local_bh_disable()
151 * in the test_run code to prevent this, but an earlier version of the
152 * code didn't have this, so we keep the test behaviour to make sure the
153 * bug doesn't resurface.
154 */
155 SYS("sysctl -qw net.ipv6.conf.all.forwarding=1");
156
157 ifindex_src = if_nametoindex("veth_src");
158 ifindex_dst = if_nametoindex("veth_dst");
159 if (!ASSERT_NEQ(ifindex_src, 0, "ifindex_src") ||
160 !ASSERT_NEQ(ifindex_dst, 0, "ifindex_dst"))
161 goto out;
162
163 /* Check xdp features supported by veth driver */
164 err = bpf_xdp_query(ifindex_src, XDP_FLAGS_DRV_MODE, &query_opts);
165 if (!ASSERT_OK(err, "veth_src bpf_xdp_query"))
166 goto out;
167
168 if (!ASSERT_EQ(query_opts.feature_flags,
169 NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
170 NETDEV_XDP_ACT_NDO_XMIT | NETDEV_XDP_ACT_RX_SG |
171 NETDEV_XDP_ACT_NDO_XMIT_SG,
172 "veth_src query_opts.feature_flags"))
173 goto out;
174
175 err = bpf_xdp_query(ifindex_dst, XDP_FLAGS_DRV_MODE, &query_opts);
176 if (!ASSERT_OK(err, "veth_dst bpf_xdp_query"))
177 goto out;
178
179 if (!ASSERT_EQ(query_opts.feature_flags,
180 NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
181 NETDEV_XDP_ACT_NDO_XMIT | NETDEV_XDP_ACT_RX_SG |
182 NETDEV_XDP_ACT_NDO_XMIT_SG,
183 "veth_dst query_opts.feature_flags"))
184 goto out;
185
186 memcpy(skel->rodata->expect_dst, &pkt_udp.eth.h_dest, ETH_ALEN);
187 skel->rodata->ifindex_out = ifindex_src; /* redirect back to the same iface */
188 skel->rodata->ifindex_in = ifindex_src;
189 ctx_in.ingress_ifindex = ifindex_src;
190 tc_hook.ifindex = ifindex_src;
191
192 if (!ASSERT_OK(test_xdp_do_redirect__load(skel), "load"))
193 goto out;
194
195 link = bpf_program__attach_xdp(skel->progs.xdp_count_pkts, ifindex_dst);
196 if (!ASSERT_OK_PTR(link, "prog_attach"))
197 goto out;
198 skel->links.xdp_count_pkts = link;
199
200 tc_prog_fd = bpf_program__fd(skel->progs.tc_count_pkts);
201 if (attach_tc_prog(&tc_hook, tc_prog_fd))
202 goto out;
203
204 xdp_prog_fd = bpf_program__fd(skel->progs.xdp_redirect);
205 err = bpf_prog_test_run_opts(xdp_prog_fd, &opts);
206 if (!ASSERT_OK(err, "prog_run"))
207 goto out_tc;
208
209 /* wait for the packets to be flushed */
210 kern_sync_rcu();
211
212 /* There will be one packet sent through XDP_REDIRECT and one through
213 * XDP_TX; these will show up on the XDP counting program, while the
214 * rest will be counted at the TC ingress hook (and the counting program
215 * resets the packet payload so they don't get counted twice even though
216 * they are re-xmited out the veth device
217 */
218 ASSERT_EQ(skel->bss->pkts_seen_xdp, 2, "pkt_count_xdp");
219 ASSERT_EQ(skel->bss->pkts_seen_zero, 2, "pkt_count_zero");
220 ASSERT_EQ(skel->bss->pkts_seen_tc, NUM_PKTS - 2, "pkt_count_tc");
221
222 test_max_pkt_size(bpf_program__fd(skel->progs.xdp_count_pkts));
223
224 out_tc:
225 bpf_tc_hook_destroy(&tc_hook);
226 out:
227 if (nstoken)
228 close_netns(nstoken);
229 system("ip netns del testns");
230 test_xdp_do_redirect__destroy(skel);
231 }
232