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