xref: /linux/tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c (revision 5a8cd539ac19f7a68e68e1d25ef9ca2ff55b8500)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/pkt_sched.h>
4 #include <linux/rtnetlink.h>
5 #include <test_progs.h>
6 
7 #include "network_helpers.h"
8 #include "bpf_qdisc_fifo.skel.h"
9 #include "bpf_qdisc_fq.skel.h"
10 #include "bpf_qdisc_fail__incompl_ops.skel.h"
11 #include "bpf_qdisc_fail__invalid_dynptr.skel.h"
12 #include "bpf_qdisc_fail__invalid_dynptr_slice.skel.h"
13 #include "bpf_qdisc_fail__invalid_dynptr_cross_frame.skel.h"
14 #include "bpf_qdisc_fail__untrusted_write.skel.h"
15 #include "bpf_qdisc_dynptr_use_after_invalidate_clone.skel.h"
16 
17 #define LO_IFINDEX 1
18 
19 static const unsigned int total_bytes = 10 * 1024 * 1024;
20 
do_test(char * qdisc)21 static void do_test(char *qdisc)
22 {
23 	DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook, .ifindex = LO_IFINDEX,
24 			    .attach_point = BPF_TC_QDISC,
25 			    .parent = TC_H_ROOT,
26 			    .handle = 0x8000000,
27 			    .qdisc = qdisc);
28 	int srv_fd = -1, cli_fd = -1;
29 	int err;
30 
31 	err = bpf_tc_hook_create(&hook);
32 	if (!ASSERT_OK(err, "attach qdisc"))
33 		return;
34 
35 	srv_fd = start_server(AF_INET6, SOCK_STREAM, NULL, 0, 0);
36 	if (!ASSERT_OK_FD(srv_fd, "start server"))
37 		goto done;
38 
39 	cli_fd = connect_to_fd(srv_fd, 0);
40 	if (!ASSERT_OK_FD(cli_fd, "connect to client"))
41 		goto done;
42 
43 	err = send_recv_data(srv_fd, cli_fd, total_bytes);
44 	ASSERT_OK(err, "send_recv_data");
45 
46 done:
47 	if (srv_fd != -1)
48 		close(srv_fd);
49 	if (cli_fd != -1)
50 		close(cli_fd);
51 
52 	bpf_tc_hook_destroy(&hook);
53 }
54 
test_fifo(void)55 static void test_fifo(void)
56 {
57 	struct bpf_qdisc_fifo *fifo_skel;
58 
59 	fifo_skel = bpf_qdisc_fifo__open_and_load();
60 	if (!ASSERT_OK_PTR(fifo_skel, "bpf_qdisc_fifo__open_and_load"))
61 		return;
62 
63 	if (!ASSERT_OK(bpf_qdisc_fifo__attach(fifo_skel), "bpf_qdisc_fifo__attach"))
64 		goto out;
65 
66 	do_test("bpf_fifo");
67 out:
68 	bpf_qdisc_fifo__destroy(fifo_skel);
69 }
70 
test_fq(void)71 static void test_fq(void)
72 {
73 	struct bpf_qdisc_fq *fq_skel;
74 
75 	fq_skel = bpf_qdisc_fq__open_and_load();
76 	if (!ASSERT_OK_PTR(fq_skel, "bpf_qdisc_fq__open_and_load"))
77 		return;
78 
79 	if (!ASSERT_OK(bpf_qdisc_fq__attach(fq_skel), "bpf_qdisc_fq__attach"))
80 		goto out;
81 
82 	do_test("bpf_fq");
83 out:
84 	bpf_qdisc_fq__destroy(fq_skel);
85 }
86 
test_qdisc_attach_to_mq(void)87 static void test_qdisc_attach_to_mq(void)
88 {
89 	DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook,
90 			    .attach_point = BPF_TC_QDISC,
91 			    .parent = TC_H_MAKE(1 << 16, 1),
92 			    .handle = 0x11 << 16,
93 			    .qdisc = "bpf_fifo");
94 	struct bpf_qdisc_fifo *fifo_skel;
95 	int err;
96 
97 	fifo_skel = bpf_qdisc_fifo__open_and_load();
98 	if (!ASSERT_OK_PTR(fifo_skel, "bpf_qdisc_fifo__open_and_load"))
99 		return;
100 
101 	if (!ASSERT_OK(bpf_qdisc_fifo__attach(fifo_skel), "bpf_qdisc_fifo__attach"))
102 		goto out;
103 
104 	SYS(out, "ip link add veth0 type veth peer veth1");
105 	hook.ifindex = if_nametoindex("veth0");
106 	SYS(out, "tc qdisc add dev veth0 root handle 1: mq");
107 
108 	err = bpf_tc_hook_create(&hook);
109 	ASSERT_OK(err, "attach qdisc");
110 
111 	bpf_tc_hook_destroy(&hook);
112 
113 	SYS(out, "tc qdisc delete dev veth0 root mq");
114 out:
115 	bpf_qdisc_fifo__destroy(fifo_skel);
116 }
117 
test_qdisc_attach_to_non_root(void)118 static void test_qdisc_attach_to_non_root(void)
119 {
120 	DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook, .ifindex = LO_IFINDEX,
121 			    .attach_point = BPF_TC_QDISC,
122 			    .parent = TC_H_MAKE(1 << 16, 1),
123 			    .handle = 0x11 << 16,
124 			    .qdisc = "bpf_fifo");
125 	struct bpf_qdisc_fifo *fifo_skel;
126 	int err;
127 
128 	fifo_skel = bpf_qdisc_fifo__open_and_load();
129 	if (!ASSERT_OK_PTR(fifo_skel, "bpf_qdisc_fifo__open_and_load"))
130 		return;
131 
132 	if (!ASSERT_OK(bpf_qdisc_fifo__attach(fifo_skel), "bpf_qdisc_fifo__attach"))
133 		goto out;
134 
135 	SYS(out, "tc qdisc add dev lo root handle 1: htb");
136 	SYS(out_del_htb, "tc class add dev lo parent 1: classid 1:1 htb rate 75Kbit");
137 
138 	err = bpf_tc_hook_create(&hook);
139 	if (!ASSERT_ERR(err, "attach qdisc"))
140 		bpf_tc_hook_destroy(&hook);
141 
142 out_del_htb:
143 	SYS(out, "tc qdisc delete dev lo root htb");
144 out:
145 	bpf_qdisc_fifo__destroy(fifo_skel);
146 }
147 
test_incompl_ops(void)148 static void test_incompl_ops(void)
149 {
150 	struct bpf_qdisc_fail__incompl_ops *skel;
151 	struct bpf_link *link;
152 
153 	skel = bpf_qdisc_fail__incompl_ops__open_and_load();
154 	if (!ASSERT_OK_PTR(skel, "bpf_qdisc_fifo__open_and_load"))
155 		return;
156 
157 	link = bpf_map__attach_struct_ops(skel->maps.test);
158 	if (!ASSERT_ERR_PTR(link, "bpf_map__attach_struct_ops"))
159 		bpf_link__destroy(link);
160 
161 	bpf_qdisc_fail__incompl_ops__destroy(skel);
162 }
163 
get_default_qdisc(char * qdisc_name)164 static int get_default_qdisc(char *qdisc_name)
165 {
166 	FILE *f;
167 	int num;
168 
169 	f = fopen("/proc/sys/net/core/default_qdisc", "r");
170 	if (!f)
171 		return -errno;
172 
173 	num = fscanf(f, "%s", qdisc_name);
174 	fclose(f);
175 
176 	return num == 1 ? 0 : -EFAULT;
177 }
178 
test_default_qdisc_attach_to_mq(void)179 static void test_default_qdisc_attach_to_mq(void)
180 {
181 	char default_qdisc[IFNAMSIZ] = {};
182 	struct bpf_qdisc_fifo *fifo_skel;
183 	struct netns_obj *netns = NULL;
184 	int err;
185 
186 	fifo_skel = bpf_qdisc_fifo__open_and_load();
187 	if (!ASSERT_OK_PTR(fifo_skel, "bpf_qdisc_fifo__open_and_load"))
188 		return;
189 
190 	if (!ASSERT_OK(bpf_qdisc_fifo__attach(fifo_skel), "bpf_qdisc_fifo__attach"))
191 		goto out;
192 
193 	err = get_default_qdisc(default_qdisc);
194 	if (!ASSERT_OK(err, "read sysctl net.core.default_qdisc"))
195 		goto out;
196 
197 	err = write_sysctl("/proc/sys/net/core/default_qdisc", "bpf_fifo");
198 	if (!ASSERT_OK(err, "write sysctl net.core.default_qdisc"))
199 		goto out;
200 
201 	netns = netns_new("bpf_qdisc_ns", true);
202 	if (!ASSERT_OK_PTR(netns, "netns_new"))
203 		goto out;
204 
205 	SYS(out, "ip link add veth0 type veth peer veth1");
206 	SYS(out, "tc qdisc add dev veth0 root handle 1: mq");
207 
208 	ASSERT_EQ(fifo_skel->bss->init_called, true, "init_called");
209 
210 	SYS(out, "tc qdisc delete dev veth0 root mq");
211 out:
212 	netns_free(netns);
213 	if (default_qdisc[0])
214 		write_sysctl("/proc/sys/net/core/default_qdisc", default_qdisc);
215 
216 	bpf_qdisc_fifo__destroy(fifo_skel);
217 }
218 
test_ns_bpf_qdisc(void)219 void test_ns_bpf_qdisc(void)
220 {
221 	if (test__start_subtest("fifo"))
222 		test_fifo();
223 	if (test__start_subtest("fq"))
224 		test_fq();
225 	if (test__start_subtest("attach to mq"))
226 		test_qdisc_attach_to_mq();
227 	if (test__start_subtest("attach to non root"))
228 		test_qdisc_attach_to_non_root();
229 	if (test__start_subtest("incompl_ops"))
230 		test_incompl_ops();
231 	RUN_TESTS(bpf_qdisc_fail__invalid_dynptr);
232 	RUN_TESTS(bpf_qdisc_fail__invalid_dynptr_cross_frame);
233 	RUN_TESTS(bpf_qdisc_fail__invalid_dynptr_slice);
234 	RUN_TESTS(bpf_qdisc_fail__untrusted_write);
235 	RUN_TESTS(bpf_qdisc_dynptr_use_after_invalidate_clone);
236 }
237 
serial_test_bpf_qdisc_default(void)238 void serial_test_bpf_qdisc_default(void)
239 {
240 	test_default_qdisc_attach_to_mq();
241 }
242