Linux Audio

Check our new training course

Loading...
Note: File does not exist in v5.4.
  1// SPDX-License-Identifier: GPL-2.0
  2/* Copyright (c) 2019 Facebook */
  3
  4#include <linux/err.h>
  5#include <netinet/tcp.h>
  6#include <test_progs.h>
  7#include "bpf_dctcp.skel.h"
  8#include "bpf_cubic.skel.h"
  9#include "bpf_tcp_nogpl.skel.h"
 10
 11#define min(a, b) ((a) < (b) ? (a) : (b))
 12
 13static const unsigned int total_bytes = 10 * 1024 * 1024;
 14static const struct timeval timeo_sec = { .tv_sec = 10 };
 15static const size_t timeo_optlen = sizeof(timeo_sec);
 16static int expected_stg = 0xeB9F;
 17static int stop, duration;
 18
 19static int settimeo(int fd)
 20{
 21	int err;
 22
 23	err = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeo_sec,
 24			 timeo_optlen);
 25	if (CHECK(err == -1, "setsockopt(fd, SO_RCVTIMEO)", "errno:%d\n",
 26		  errno))
 27		return -1;
 28
 29	err = setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeo_sec,
 30			 timeo_optlen);
 31	if (CHECK(err == -1, "setsockopt(fd, SO_SNDTIMEO)", "errno:%d\n",
 32		  errno))
 33		return -1;
 34
 35	return 0;
 36}
 37
 38static int settcpca(int fd, const char *tcp_ca)
 39{
 40	int err;
 41
 42	err = setsockopt(fd, IPPROTO_TCP, TCP_CONGESTION, tcp_ca, strlen(tcp_ca));
 43	if (CHECK(err == -1, "setsockopt(fd, TCP_CONGESTION)", "errno:%d\n",
 44		  errno))
 45		return -1;
 46
 47	return 0;
 48}
 49
 50static void *server(void *arg)
 51{
 52	int lfd = (int)(long)arg, err = 0, fd;
 53	ssize_t nr_sent = 0, bytes = 0;
 54	char batch[1500];
 55
 56	fd = accept(lfd, NULL, NULL);
 57	while (fd == -1) {
 58		if (errno == EINTR)
 59			continue;
 60		err = -errno;
 61		goto done;
 62	}
 63
 64	if (settimeo(fd)) {
 65		err = -errno;
 66		goto done;
 67	}
 68
 69	while (bytes < total_bytes && !READ_ONCE(stop)) {
 70		nr_sent = send(fd, &batch,
 71			       min(total_bytes - bytes, sizeof(batch)), 0);
 72		if (nr_sent == -1 && errno == EINTR)
 73			continue;
 74		if (nr_sent == -1) {
 75			err = -errno;
 76			break;
 77		}
 78		bytes += nr_sent;
 79	}
 80
 81	CHECK(bytes != total_bytes, "send", "%zd != %u nr_sent:%zd errno:%d\n",
 82	      bytes, total_bytes, nr_sent, errno);
 83
 84done:
 85	if (fd >= 0)
 86		close(fd);
 87	if (err) {
 88		WRITE_ONCE(stop, 1);
 89		return ERR_PTR(err);
 90	}
 91	return NULL;
 92}
 93
 94static void do_test(const char *tcp_ca, const struct bpf_map *sk_stg_map)
 95{
 96	struct sockaddr_in6 sa6 = {};
 97	ssize_t nr_recv = 0, bytes = 0;
 98	int lfd = -1, fd = -1;
 99	pthread_t srv_thread;
100	socklen_t addrlen = sizeof(sa6);
101	void *thread_ret;
102	char batch[1500];
103	int err;
104
105	WRITE_ONCE(stop, 0);
106
107	lfd = socket(AF_INET6, SOCK_STREAM, 0);
108	if (CHECK(lfd == -1, "socket", "errno:%d\n", errno))
109		return;
110	fd = socket(AF_INET6, SOCK_STREAM, 0);
111	if (CHECK(fd == -1, "socket", "errno:%d\n", errno)) {
112		close(lfd);
113		return;
114	}
115
116	if (settcpca(lfd, tcp_ca) || settcpca(fd, tcp_ca) ||
117	    settimeo(lfd) || settimeo(fd))
118		goto done;
119
120	/* bind, listen and start server thread to accept */
121	sa6.sin6_family = AF_INET6;
122	sa6.sin6_addr = in6addr_loopback;
123	err = bind(lfd, (struct sockaddr *)&sa6, addrlen);
124	if (CHECK(err == -1, "bind", "errno:%d\n", errno))
125		goto done;
126	err = getsockname(lfd, (struct sockaddr *)&sa6, &addrlen);
127	if (CHECK(err == -1, "getsockname", "errno:%d\n", errno))
128		goto done;
129	err = listen(lfd, 1);
130	if (CHECK(err == -1, "listen", "errno:%d\n", errno))
131		goto done;
132
133	if (sk_stg_map) {
134		err = bpf_map_update_elem(bpf_map__fd(sk_stg_map), &fd,
135					  &expected_stg, BPF_NOEXIST);
136		if (CHECK(err, "bpf_map_update_elem(sk_stg_map)",
137			  "err:%d errno:%d\n", err, errno))
138			goto done;
139	}
140
141	/* connect to server */
142	err = connect(fd, (struct sockaddr *)&sa6, addrlen);
143	if (CHECK(err == -1, "connect", "errno:%d\n", errno))
144		goto done;
145
146	if (sk_stg_map) {
147		int tmp_stg;
148
149		err = bpf_map_lookup_elem(bpf_map__fd(sk_stg_map), &fd,
150					  &tmp_stg);
151		if (CHECK(!err || errno != ENOENT,
152			  "bpf_map_lookup_elem(sk_stg_map)",
153			  "err:%d errno:%d\n", err, errno))
154			goto done;
155	}
156
157	err = pthread_create(&srv_thread, NULL, server, (void *)(long)lfd);
158	if (CHECK(err != 0, "pthread_create", "err:%d errno:%d\n", err, errno))
159		goto done;
160
161	/* recv total_bytes */
162	while (bytes < total_bytes && !READ_ONCE(stop)) {
163		nr_recv = recv(fd, &batch,
164			       min(total_bytes - bytes, sizeof(batch)), 0);
165		if (nr_recv == -1 && errno == EINTR)
166			continue;
167		if (nr_recv == -1)
168			break;
169		bytes += nr_recv;
170	}
171
172	CHECK(bytes != total_bytes, "recv", "%zd != %u nr_recv:%zd errno:%d\n",
173	      bytes, total_bytes, nr_recv, errno);
174
175	WRITE_ONCE(stop, 1);
176	pthread_join(srv_thread, &thread_ret);
177	CHECK(IS_ERR(thread_ret), "pthread_join", "thread_ret:%ld",
178	      PTR_ERR(thread_ret));
179done:
180	close(lfd);
181	close(fd);
182}
183
184static void test_cubic(void)
185{
186	struct bpf_cubic *cubic_skel;
187	struct bpf_link *link;
188
189	cubic_skel = bpf_cubic__open_and_load();
190	if (CHECK(!cubic_skel, "bpf_cubic__open_and_load", "failed\n"))
191		return;
192
193	link = bpf_map__attach_struct_ops(cubic_skel->maps.cubic);
194	if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops")) {
195		bpf_cubic__destroy(cubic_skel);
196		return;
197	}
198
199	do_test("bpf_cubic", NULL);
200
201	bpf_link__destroy(link);
202	bpf_cubic__destroy(cubic_skel);
203}
204
205static void test_dctcp(void)
206{
207	struct bpf_dctcp *dctcp_skel;
208	struct bpf_link *link;
209
210	dctcp_skel = bpf_dctcp__open_and_load();
211	if (CHECK(!dctcp_skel, "bpf_dctcp__open_and_load", "failed\n"))
212		return;
213
214	link = bpf_map__attach_struct_ops(dctcp_skel->maps.dctcp);
215	if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops")) {
216		bpf_dctcp__destroy(dctcp_skel);
217		return;
218	}
219
220	do_test("bpf_dctcp", dctcp_skel->maps.sk_stg_map);
221	CHECK(dctcp_skel->bss->stg_result != expected_stg,
222	      "Unexpected stg_result", "stg_result (%x) != expected_stg (%x)\n",
223	      dctcp_skel->bss->stg_result, expected_stg);
224
225	bpf_link__destroy(link);
226	bpf_dctcp__destroy(dctcp_skel);
227}
228
229static char *err_str;
230static bool found;
231
232static int libbpf_debug_print(enum libbpf_print_level level,
233			      const char *format, va_list args)
234{
235	char *log_buf;
236
237	if (level != LIBBPF_WARN ||
238	    strcmp(format, "libbpf: \n%s\n")) {
239		vprintf(format, args);
240		return 0;
241	}
242
243	log_buf = va_arg(args, char *);
244	if (!log_buf)
245		goto out;
246	if (err_str && strstr(log_buf, err_str) != NULL)
247		found = true;
248out:
249	printf(format, log_buf);
250	return 0;
251}
252
253static void test_invalid_license(void)
254{
255	libbpf_print_fn_t old_print_fn;
256	struct bpf_tcp_nogpl *skel;
257
258	err_str = "struct ops programs must have a GPL compatible license";
259	found = false;
260	old_print_fn = libbpf_set_print(libbpf_debug_print);
261
262	skel = bpf_tcp_nogpl__open_and_load();
263	ASSERT_NULL(skel, "bpf_tcp_nogpl");
264	ASSERT_EQ(found, true, "expected_err_msg");
265
266	bpf_tcp_nogpl__destroy(skel);
267	libbpf_set_print(old_print_fn);
268}
269
270void test_bpf_tcp_ca(void)
271{
272	if (test__start_subtest("dctcp"))
273		test_dctcp();
274	if (test__start_subtest("cubic"))
275		test_cubic();
276	if (test__start_subtest("invalid_license"))
277		test_invalid_license();
278}