1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include <network_helpers.h>
4 #include "test_xdp_context_test_run.skel.h"
5 #include "test_xdp_meta.skel.h"
6
7 #define TX_ADDR "10.0.0.1"
8 #define RX_ADDR "10.0.0.2"
9 #define RX_NAME "veth0"
10 #define TX_NAME "veth1"
11 #define TX_NETNS "xdp_context_tx"
12 #define RX_NETNS "xdp_context_rx"
13
test_xdp_context_error(int prog_fd,struct bpf_test_run_opts opts,__u32 data_meta,__u32 data,__u32 data_end,__u32 ingress_ifindex,__u32 rx_queue_index,__u32 egress_ifindex)14 void test_xdp_context_error(int prog_fd, struct bpf_test_run_opts opts,
15 __u32 data_meta, __u32 data, __u32 data_end,
16 __u32 ingress_ifindex, __u32 rx_queue_index,
17 __u32 egress_ifindex)
18 {
19 struct xdp_md ctx = {
20 .data = data,
21 .data_end = data_end,
22 .data_meta = data_meta,
23 .ingress_ifindex = ingress_ifindex,
24 .rx_queue_index = rx_queue_index,
25 .egress_ifindex = egress_ifindex,
26 };
27 int err;
28
29 opts.ctx_in = &ctx;
30 opts.ctx_size_in = sizeof(ctx);
31 err = bpf_prog_test_run_opts(prog_fd, &opts);
32 ASSERT_EQ(errno, EINVAL, "errno-EINVAL");
33 ASSERT_ERR(err, "bpf_prog_test_run");
34 }
35
test_xdp_context_test_run(void)36 void test_xdp_context_test_run(void)
37 {
38 struct test_xdp_context_test_run *skel = NULL;
39 char data[sizeof(pkt_v4) + sizeof(__u32)];
40 char bad_ctx[sizeof(struct xdp_md) + 1];
41 struct xdp_md ctx_in, ctx_out;
42 DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts,
43 .data_in = &data,
44 .data_size_in = sizeof(data),
45 .ctx_out = &ctx_out,
46 .ctx_size_out = sizeof(ctx_out),
47 .repeat = 1,
48 );
49 int err, prog_fd;
50
51 skel = test_xdp_context_test_run__open_and_load();
52 if (!ASSERT_OK_PTR(skel, "skel"))
53 return;
54 prog_fd = bpf_program__fd(skel->progs.xdp_context);
55
56 /* Data past the end of the kernel's struct xdp_md must be 0 */
57 bad_ctx[sizeof(bad_ctx) - 1] = 1;
58 opts.ctx_in = bad_ctx;
59 opts.ctx_size_in = sizeof(bad_ctx);
60 err = bpf_prog_test_run_opts(prog_fd, &opts);
61 ASSERT_EQ(errno, E2BIG, "extradata-errno");
62 ASSERT_ERR(err, "bpf_prog_test_run(extradata)");
63
64 *(__u32 *)data = XDP_PASS;
65 *(struct ipv4_packet *)(data + sizeof(__u32)) = pkt_v4;
66 opts.ctx_in = &ctx_in;
67 opts.ctx_size_in = sizeof(ctx_in);
68 memset(&ctx_in, 0, sizeof(ctx_in));
69 ctx_in.data_meta = 0;
70 ctx_in.data = sizeof(__u32);
71 ctx_in.data_end = ctx_in.data + sizeof(pkt_v4);
72 err = bpf_prog_test_run_opts(prog_fd, &opts);
73 ASSERT_OK(err, "bpf_prog_test_run(valid)");
74 ASSERT_EQ(opts.retval, XDP_PASS, "valid-retval");
75 ASSERT_EQ(opts.data_size_out, sizeof(pkt_v4), "valid-datasize");
76 ASSERT_EQ(opts.ctx_size_out, opts.ctx_size_in, "valid-ctxsize");
77 ASSERT_EQ(ctx_out.data_meta, 0, "valid-datameta");
78 ASSERT_EQ(ctx_out.data, 0, "valid-data");
79 ASSERT_EQ(ctx_out.data_end, sizeof(pkt_v4), "valid-dataend");
80
81 /* Meta data's size must be a multiple of 4 */
82 test_xdp_context_error(prog_fd, opts, 0, 1, sizeof(data), 0, 0, 0);
83
84 /* data_meta must reference the start of data */
85 test_xdp_context_error(prog_fd, opts, 4, sizeof(__u32), sizeof(data),
86 0, 0, 0);
87
88 /* Meta data must be 255 bytes or smaller */
89 test_xdp_context_error(prog_fd, opts, 0, 256, sizeof(data), 0, 0, 0);
90
91 /* Total size of data must match data_end - data_meta */
92 test_xdp_context_error(prog_fd, opts, 0, sizeof(__u32),
93 sizeof(data) - 1, 0, 0, 0);
94 test_xdp_context_error(prog_fd, opts, 0, sizeof(__u32),
95 sizeof(data) + 1, 0, 0, 0);
96
97 /* RX queue cannot be specified without specifying an ingress */
98 test_xdp_context_error(prog_fd, opts, 0, sizeof(__u32), sizeof(data),
99 0, 1, 0);
100
101 /* Interface 1 is always the loopback interface which always has only
102 * one RX queue (index 0). This makes index 1 an invalid rx queue index
103 * for interface 1.
104 */
105 test_xdp_context_error(prog_fd, opts, 0, sizeof(__u32), sizeof(data),
106 1, 1, 0);
107
108 /* The egress cannot be specified */
109 test_xdp_context_error(prog_fd, opts, 0, sizeof(__u32), sizeof(data),
110 0, 0, 1);
111
112 test_xdp_context_test_run__destroy(skel);
113 }
114
test_xdp_context_functional(void)115 void test_xdp_context_functional(void)
116 {
117 LIBBPF_OPTS(bpf_tc_hook, tc_hook, .attach_point = BPF_TC_INGRESS);
118 LIBBPF_OPTS(bpf_tc_opts, tc_opts, .handle = 1, .priority = 1);
119 struct netns_obj *rx_ns = NULL, *tx_ns = NULL;
120 struct bpf_program *tc_prog, *xdp_prog;
121 struct test_xdp_meta *skel = NULL;
122 struct nstoken *nstoken = NULL;
123 int rx_ifindex;
124 int ret;
125
126 tx_ns = netns_new(TX_NETNS, false);
127 if (!ASSERT_OK_PTR(tx_ns, "create tx_ns"))
128 return;
129
130 rx_ns = netns_new(RX_NETNS, false);
131 if (!ASSERT_OK_PTR(rx_ns, "create rx_ns"))
132 goto close;
133
134 SYS(close, "ip link add " RX_NAME " netns " RX_NETNS
135 " type veth peer name " TX_NAME " netns " TX_NETNS);
136
137 nstoken = open_netns(RX_NETNS);
138 if (!ASSERT_OK_PTR(nstoken, "setns rx_ns"))
139 goto close;
140
141 SYS(close, "ip addr add " RX_ADDR "/24 dev " RX_NAME);
142 SYS(close, "ip link set dev " RX_NAME " up");
143
144 skel = test_xdp_meta__open_and_load();
145 if (!ASSERT_OK_PTR(skel, "open and load skeleton"))
146 goto close;
147
148 rx_ifindex = if_nametoindex(RX_NAME);
149 if (!ASSERT_GE(rx_ifindex, 0, "if_nametoindex rx"))
150 goto close;
151
152 tc_hook.ifindex = rx_ifindex;
153 ret = bpf_tc_hook_create(&tc_hook);
154 if (!ASSERT_OK(ret, "bpf_tc_hook_create"))
155 goto close;
156
157 tc_prog = bpf_object__find_program_by_name(skel->obj, "ing_cls");
158 if (!ASSERT_OK_PTR(tc_prog, "open ing_cls prog"))
159 goto close;
160
161 tc_opts.prog_fd = bpf_program__fd(tc_prog);
162 ret = bpf_tc_attach(&tc_hook, &tc_opts);
163 if (!ASSERT_OK(ret, "bpf_tc_attach"))
164 goto close;
165
166 xdp_prog = bpf_object__find_program_by_name(skel->obj, "ing_xdp");
167 if (!ASSERT_OK_PTR(xdp_prog, "open ing_xdp prog"))
168 goto close;
169
170 ret = bpf_xdp_attach(rx_ifindex,
171 bpf_program__fd(xdp_prog),
172 0, NULL);
173 if (!ASSERT_GE(ret, 0, "bpf_xdp_attach"))
174 goto close;
175
176 close_netns(nstoken);
177
178 nstoken = open_netns(TX_NETNS);
179 if (!ASSERT_OK_PTR(nstoken, "setns tx_ns"))
180 goto close;
181
182 SYS(close, "ip addr add " TX_ADDR "/24 dev " TX_NAME);
183 SYS(close, "ip link set dev " TX_NAME " up");
184 ASSERT_OK(SYS_NOFAIL("ping -c 1 " RX_ADDR), "ping");
185
186 close:
187 close_netns(nstoken);
188 test_xdp_meta__destroy(skel);
189 netns_free(rx_ns);
190 netns_free(tx_ns);
191 }
192
193