xref: /linux/tools/testing/selftests/bpf/prog_tests/xdp_devmap_attach.c (revision 55d0969c451159cff86949b38c39171cab962069)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <arpa/inet.h>
3 #include <uapi/linux/bpf.h>
4 #include <linux/if_link.h>
5 #include <network_helpers.h>
6 #include <net/if.h>
7 #include <test_progs.h>
8 
9 #include "test_xdp_devmap_helpers.skel.h"
10 #include "test_xdp_with_devmap_frags_helpers.skel.h"
11 #include "test_xdp_with_devmap_helpers.skel.h"
12 
13 #define IFINDEX_LO 1
14 #define TEST_NS "devmap_attach_ns"
15 
16 static void test_xdp_with_devmap_helpers(void)
17 {
18 	struct test_xdp_with_devmap_helpers *skel = NULL;
19 	struct bpf_prog_info info = {};
20 	struct bpf_devmap_val val = {
21 		.ifindex = IFINDEX_LO,
22 	};
23 	__u32 len = sizeof(info);
24 	int err, dm_fd, dm_fd_redir, map_fd;
25 	struct nstoken *nstoken = NULL;
26 	char data[10] = {};
27 	__u32 idx = 0;
28 
29 	SYS(out_close, "ip netns add %s", TEST_NS);
30 	nstoken = open_netns(TEST_NS);
31 	if (!ASSERT_OK_PTR(nstoken, "open_netns"))
32 		goto out_close;
33 	SYS(out_close, "ip link set dev lo up");
34 
35 	skel = test_xdp_with_devmap_helpers__open_and_load();
36 	if (!ASSERT_OK_PTR(skel, "test_xdp_with_devmap_helpers__open_and_load"))
37 		goto out_close;
38 
39 	dm_fd_redir = bpf_program__fd(skel->progs.xdp_redir_prog);
40 	err = bpf_xdp_attach(IFINDEX_LO, dm_fd_redir, XDP_FLAGS_SKB_MODE, NULL);
41 	if (!ASSERT_OK(err, "Generic attach of program with 8-byte devmap"))
42 		goto out_close;
43 
44 	dm_fd = bpf_program__fd(skel->progs.xdp_dummy_dm);
45 	map_fd = bpf_map__fd(skel->maps.dm_ports);
46 	err = bpf_prog_get_info_by_fd(dm_fd, &info, &len);
47 	if (!ASSERT_OK(err, "bpf_prog_get_info_by_fd"))
48 		goto out_close;
49 
50 	val.bpf_prog.fd = dm_fd;
51 	err = bpf_map_update_elem(map_fd, &idx, &val, 0);
52 	ASSERT_OK(err, "Add program to devmap entry");
53 
54 	err = bpf_map_lookup_elem(map_fd, &idx, &val);
55 	ASSERT_OK(err, "Read devmap entry");
56 	ASSERT_EQ(info.id, val.bpf_prog.id, "Match program id to devmap entry prog_id");
57 
58 	/* send a packet to trigger any potential bugs in there */
59 	DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts,
60 			    .data_in = &data,
61 			    .data_size_in = 10,
62 			    .flags = BPF_F_TEST_XDP_LIVE_FRAMES,
63 			    .repeat = 1,
64 		);
65 	err = bpf_prog_test_run_opts(dm_fd_redir, &opts);
66 	ASSERT_OK(err, "XDP test run");
67 
68 	/* wait for the packets to be flushed */
69 	kern_sync_rcu();
70 
71 	err = bpf_xdp_detach(IFINDEX_LO, XDP_FLAGS_SKB_MODE, NULL);
72 	ASSERT_OK(err, "XDP program detach");
73 
74 	/* can not attach BPF_XDP_DEVMAP program to a device */
75 	err = bpf_xdp_attach(IFINDEX_LO, dm_fd, XDP_FLAGS_SKB_MODE, NULL);
76 	if (!ASSERT_NEQ(err, 0, "Attach of BPF_XDP_DEVMAP program"))
77 		bpf_xdp_detach(IFINDEX_LO, XDP_FLAGS_SKB_MODE, NULL);
78 
79 	val.ifindex = 1;
80 	val.bpf_prog.fd = bpf_program__fd(skel->progs.xdp_dummy_prog);
81 	err = bpf_map_update_elem(map_fd, &idx, &val, 0);
82 	ASSERT_NEQ(err, 0, "Add non-BPF_XDP_DEVMAP program to devmap entry");
83 
84 	/* Try to attach BPF_XDP program with frags to devmap when we have
85 	 * already loaded a BPF_XDP program on the map
86 	 */
87 	idx = 1;
88 	val.ifindex = 1;
89 	val.bpf_prog.fd = bpf_program__fd(skel->progs.xdp_dummy_dm_frags);
90 	err = bpf_map_update_elem(map_fd, &idx, &val, 0);
91 	ASSERT_NEQ(err, 0, "Add BPF_XDP program with frags to devmap entry");
92 
93 out_close:
94 	close_netns(nstoken);
95 	SYS_NOFAIL("ip netns del %s", TEST_NS);
96 	test_xdp_with_devmap_helpers__destroy(skel);
97 }
98 
99 static void test_neg_xdp_devmap_helpers(void)
100 {
101 	struct test_xdp_devmap_helpers *skel;
102 
103 	skel = test_xdp_devmap_helpers__open_and_load();
104 	if (!ASSERT_EQ(skel, NULL,
105 		    "Load of XDP program accessing egress ifindex without attach type")) {
106 		test_xdp_devmap_helpers__destroy(skel);
107 	}
108 }
109 
110 static void test_xdp_with_devmap_frags_helpers(void)
111 {
112 	struct test_xdp_with_devmap_frags_helpers *skel;
113 	struct bpf_prog_info info = {};
114 	struct bpf_devmap_val val = {
115 		.ifindex = IFINDEX_LO,
116 	};
117 	__u32 len = sizeof(info);
118 	int err, dm_fd_frags, map_fd;
119 	__u32 idx = 0;
120 
121 	skel = test_xdp_with_devmap_frags_helpers__open_and_load();
122 	if (!ASSERT_OK_PTR(skel, "test_xdp_with_devmap_helpers__open_and_load"))
123 		return;
124 
125 	dm_fd_frags = bpf_program__fd(skel->progs.xdp_dummy_dm_frags);
126 	map_fd = bpf_map__fd(skel->maps.dm_ports);
127 	err = bpf_prog_get_info_by_fd(dm_fd_frags, &info, &len);
128 	if (!ASSERT_OK(err, "bpf_prog_get_info_by_fd"))
129 		goto out_close;
130 
131 	val.bpf_prog.fd = dm_fd_frags;
132 	err = bpf_map_update_elem(map_fd, &idx, &val, 0);
133 	ASSERT_OK(err, "Add frags program to devmap entry");
134 
135 	err = bpf_map_lookup_elem(map_fd, &idx, &val);
136 	ASSERT_OK(err, "Read devmap entry");
137 	ASSERT_EQ(info.id, val.bpf_prog.id,
138 		  "Match program id to devmap entry prog_id");
139 
140 	/* Try to attach BPF_XDP program to devmap when we have
141 	 * already loaded a BPF_XDP program with frags on the map
142 	 */
143 	idx = 1;
144 	val.ifindex = 1;
145 	val.bpf_prog.fd = bpf_program__fd(skel->progs.xdp_dummy_dm);
146 	err = bpf_map_update_elem(map_fd, &idx, &val, 0);
147 	ASSERT_NEQ(err, 0, "Add BPF_XDP program to devmap entry");
148 
149 out_close:
150 	test_xdp_with_devmap_frags_helpers__destroy(skel);
151 }
152 
153 static void test_xdp_with_devmap_helpers_veth(void)
154 {
155 	struct test_xdp_with_devmap_helpers *skel = NULL;
156 	struct bpf_prog_info info = {};
157 	struct bpf_devmap_val val = {};
158 	struct nstoken *nstoken = NULL;
159 	__u32 len = sizeof(info);
160 	int err, dm_fd, dm_fd_redir, map_fd, ifindex_dst;
161 	char data[10] = {};
162 	__u32 idx = 0;
163 
164 	SYS(out_close, "ip netns add %s", TEST_NS);
165 	nstoken = open_netns(TEST_NS);
166 	if (!ASSERT_OK_PTR(nstoken, "open_netns"))
167 		goto out_close;
168 
169 	SYS(out_close, "ip link add veth_src type veth peer name veth_dst");
170 	SYS(out_close, "ip link set dev veth_src up");
171 	SYS(out_close, "ip link set dev veth_dst up");
172 
173 	val.ifindex = if_nametoindex("veth_src");
174 	ifindex_dst = if_nametoindex("veth_dst");
175 	if (!ASSERT_NEQ(val.ifindex, 0, "val.ifindex") ||
176 	    !ASSERT_NEQ(ifindex_dst, 0, "ifindex_dst"))
177 		goto out_close;
178 
179 	skel = test_xdp_with_devmap_helpers__open_and_load();
180 	if (!ASSERT_OK_PTR(skel, "test_xdp_with_devmap_helpers__open_and_load"))
181 		goto out_close;
182 
183 	dm_fd_redir = bpf_program__fd(skel->progs.xdp_redir_prog);
184 	err = bpf_xdp_attach(val.ifindex, dm_fd_redir, XDP_FLAGS_DRV_MODE, NULL);
185 	if (!ASSERT_OK(err, "Attach of program with 8-byte devmap"))
186 		goto out_close;
187 
188 	dm_fd = bpf_program__fd(skel->progs.xdp_dummy_dm);
189 	map_fd = bpf_map__fd(skel->maps.dm_ports);
190 	err = bpf_prog_get_info_by_fd(dm_fd, &info, &len);
191 	if (!ASSERT_OK(err, "bpf_prog_get_info_by_fd"))
192 		goto out_close;
193 
194 	val.bpf_prog.fd = dm_fd;
195 	err = bpf_map_update_elem(map_fd, &idx, &val, 0);
196 	ASSERT_OK(err, "Add program to devmap entry");
197 
198 	err = bpf_map_lookup_elem(map_fd, &idx, &val);
199 	ASSERT_OK(err, "Read devmap entry");
200 	ASSERT_EQ(info.id, val.bpf_prog.id, "Match program id to devmap entry prog_id");
201 
202 	/* attach dummy to other side to enable reception */
203 	dm_fd = bpf_program__fd(skel->progs.xdp_dummy_prog);
204 	err = bpf_xdp_attach(ifindex_dst, dm_fd, XDP_FLAGS_DRV_MODE, NULL);
205 	if (!ASSERT_OK(err, "Attach of dummy XDP"))
206 		goto out_close;
207 
208 	/* send a packet to trigger any potential bugs in there */
209 	DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts,
210 			    .data_in = &data,
211 			    .data_size_in = 10,
212 			    .flags = BPF_F_TEST_XDP_LIVE_FRAMES,
213 			    .repeat = 1,
214 		);
215 	err = bpf_prog_test_run_opts(dm_fd_redir, &opts);
216 	ASSERT_OK(err, "XDP test run");
217 
218 	/* wait for the packets to be flushed */
219 	kern_sync_rcu();
220 
221 	err = bpf_xdp_detach(val.ifindex, XDP_FLAGS_DRV_MODE, NULL);
222 	ASSERT_OK(err, "XDP program detach");
223 
224 	err = bpf_xdp_detach(ifindex_dst, XDP_FLAGS_DRV_MODE, NULL);
225 	ASSERT_OK(err, "XDP program detach");
226 
227 out_close:
228 	close_netns(nstoken);
229 	SYS_NOFAIL("ip netns del %s", TEST_NS);
230 	test_xdp_with_devmap_helpers__destroy(skel);
231 }
232 
233 void serial_test_xdp_devmap_attach(void)
234 {
235 	if (test__start_subtest("DEVMAP with programs in entries"))
236 		test_xdp_with_devmap_helpers();
237 
238 	if (test__start_subtest("DEVMAP with frags programs in entries"))
239 		test_xdp_with_devmap_frags_helpers();
240 
241 	if (test__start_subtest("Verifier check of DEVMAP programs"))
242 		test_neg_xdp_devmap_helpers();
243 
244 	if (test__start_subtest("DEVMAP with programs in entries on veth"))
245 		test_xdp_with_devmap_helpers_veth();
246 }
247