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