1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2021 Facebook */
3 #include <test_progs.h>
4 #include <netinet/tcp.h>
5 #include "sockopt_qos_to_cc.skel.h"
6 
run_setsockopt_test(int cg_fd,int sock_fd)7 static void run_setsockopt_test(int cg_fd, int sock_fd)
8 {
9 	socklen_t optlen;
10 	char cc[16]; /* TCP_CA_NAME_MAX */
11 	int buf;
12 	int err = -1;
13 
14 	buf = 0x2D;
15 	err = setsockopt(sock_fd, SOL_IPV6, IPV6_TCLASS, &buf, sizeof(buf));
16 	if (!ASSERT_OK(err, "setsockopt(sock_fd, IPV6_TCLASS)"))
17 		return;
18 
19 	/* Verify the setsockopt cc change */
20 	optlen = sizeof(cc);
21 	err = getsockopt(sock_fd, SOL_TCP, TCP_CONGESTION, cc, &optlen);
22 	if (!ASSERT_OK(err, "getsockopt(sock_fd, TCP_CONGESTION)"))
23 		return;
24 
25 	if (!ASSERT_STREQ(cc, "reno", "getsockopt(sock_fd, TCP_CONGESTION)"))
26 		return;
27 }
28 
test_sockopt_qos_to_cc(void)29 void test_sockopt_qos_to_cc(void)
30 {
31 	struct sockopt_qos_to_cc *skel;
32 	char cc_cubic[16] = "cubic"; /* TCP_CA_NAME_MAX */
33 	int cg_fd = -1;
34 	int sock_fd = -1;
35 	int err;
36 
37 	cg_fd = test__join_cgroup("/sockopt_qos_to_cc");
38 	if (!ASSERT_GE(cg_fd, 0, "cg-join(sockopt_qos_to_cc)"))
39 		return;
40 
41 	skel = sockopt_qos_to_cc__open_and_load();
42 	if (!ASSERT_OK_PTR(skel, "skel"))
43 		goto done;
44 
45 	sock_fd = socket(AF_INET6, SOCK_STREAM, 0);
46 	if (!ASSERT_GE(sock_fd, 0, "v6 socket open"))
47 		goto done;
48 
49 	err = setsockopt(sock_fd, SOL_TCP, TCP_CONGESTION, &cc_cubic,
50 			 sizeof(cc_cubic));
51 	if (!ASSERT_OK(err, "setsockopt(sock_fd, TCP_CONGESTION)"))
52 		goto done;
53 
54 	skel->links.sockopt_qos_to_cc =
55 		bpf_program__attach_cgroup(skel->progs.sockopt_qos_to_cc,
56 					   cg_fd);
57 	if (!ASSERT_OK_PTR(skel->links.sockopt_qos_to_cc,
58 			   "prog_attach(sockopt_qos_to_cc)"))
59 		goto done;
60 
61 	run_setsockopt_test(cg_fd, sock_fd);
62 
63 done:
64 	if (sock_fd != -1)
65 		close(sock_fd);
66 	if (cg_fd != -1)
67 		close(cg_fd);
68 	/* destroy can take null and error pointer */
69 	sockopt_qos_to_cc__destroy(skel);
70 }
71