1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include "cgroup_helpers.h"
5 #include "sockopt_multi.skel.h"
7 static int run_getsockopt_test(struct sockopt_multi *obj, int cg_parent,
8 int cg_child, int sock_fd)
10 struct bpf_link *link_parent = NULL;
11 struct bpf_link *link_child = NULL;
16 /* Set IP_TOS to the expected value (0x80). */
19 err = setsockopt(sock_fd, SOL_IP, IP_TOS, &buf, 1);
21 log_err("Failed to call setsockopt(IP_TOS)");
27 err = getsockopt(sock_fd, SOL_IP, IP_TOS, &buf, &optlen);
29 log_err("Failed to call getsockopt(IP_TOS)");
34 log_err("Unexpected getsockopt 0x%x != 0x80 without BPF", buf);
39 /* Attach child program and make sure it returns new value:
41 * - child: 0x80 -> 0x90
44 link_child = bpf_program__attach_cgroup(obj->progs._getsockopt_child,
46 if (!ASSERT_OK_PTR(link_child, "cg-attach-getsockopt_child"))
51 err = getsockopt(sock_fd, SOL_IP, IP_TOS, &buf, &optlen);
53 log_err("Failed to call getsockopt(IP_TOS)");
58 log_err("Unexpected getsockopt 0x%x != 0x90", buf);
63 /* Attach parent program and make sure it returns new value:
65 * - child: 0x80 -> 0x90
66 * - parent: 0x90 -> 0xA0
69 link_parent = bpf_program__attach_cgroup(obj->progs._getsockopt_parent,
71 if (!ASSERT_OK_PTR(link_parent, "cg-attach-getsockopt_parent"))
76 err = getsockopt(sock_fd, SOL_IP, IP_TOS, &buf, &optlen);
78 log_err("Failed to call getsockopt(IP_TOS)");
83 log_err("Unexpected getsockopt 0x%x != 0xA0", buf);
88 /* Setting unexpected initial sockopt should return EPERM:
90 * - child: unexpected 0x40, EPERM
91 * - parent: unexpected 0x40, EPERM
95 err = setsockopt(sock_fd, SOL_IP, IP_TOS, &buf, 1);
97 log_err("Failed to call setsockopt(IP_TOS)");
103 err = getsockopt(sock_fd, SOL_IP, IP_TOS, &buf, &optlen);
105 log_err("Unexpected success from getsockopt(IP_TOS)");
109 /* Detach child program and make sure we still get EPERM:
111 * - parent: unexpected 0x40, EPERM
114 bpf_link__destroy(link_child);
119 err = getsockopt(sock_fd, SOL_IP, IP_TOS, &buf, &optlen);
121 log_err("Unexpected success from getsockopt(IP_TOS)");
125 /* Set initial value to the one the parent program expects:
127 * - parent: 0x90 -> 0xA0
131 err = setsockopt(sock_fd, SOL_IP, IP_TOS, &buf, 1);
133 log_err("Failed to call setsockopt(IP_TOS)");
139 err = getsockopt(sock_fd, SOL_IP, IP_TOS, &buf, &optlen);
141 log_err("Failed to call getsockopt(IP_TOS)");
146 log_err("Unexpected getsockopt 0x%x != 0xA0", buf);
152 bpf_link__destroy(link_child);
153 bpf_link__destroy(link_parent);
158 static int run_setsockopt_test(struct sockopt_multi *obj, int cg_parent,
159 int cg_child, int sock_fd)
161 struct bpf_link *link_parent = NULL;
162 struct bpf_link *link_child = NULL;
167 /* Set IP_TOS to the expected value (0x80). */
170 err = setsockopt(sock_fd, SOL_IP, IP_TOS, &buf, 1);
172 log_err("Failed to call setsockopt(IP_TOS)");
178 err = getsockopt(sock_fd, SOL_IP, IP_TOS, &buf, &optlen);
180 log_err("Failed to call getsockopt(IP_TOS)");
185 log_err("Unexpected getsockopt 0x%x != 0x80 without BPF", buf);
190 /* Attach child program and make sure it adds 0x10. */
192 link_child = bpf_program__attach_cgroup(obj->progs._setsockopt,
194 if (!ASSERT_OK_PTR(link_child, "cg-attach-setsockopt_child"))
198 err = setsockopt(sock_fd, SOL_IP, IP_TOS, &buf, 1);
200 log_err("Failed to call setsockopt(IP_TOS)");
206 err = getsockopt(sock_fd, SOL_IP, IP_TOS, &buf, &optlen);
208 log_err("Failed to call getsockopt(IP_TOS)");
212 if (buf != 0x80 + 0x10) {
213 log_err("Unexpected getsockopt 0x%x != 0x80 + 0x10", buf);
218 /* Attach parent program and make sure it adds another 0x10. */
220 link_parent = bpf_program__attach_cgroup(obj->progs._setsockopt,
222 if (!ASSERT_OK_PTR(link_parent, "cg-attach-setsockopt_parent"))
226 err = setsockopt(sock_fd, SOL_IP, IP_TOS, &buf, 1);
228 log_err("Failed to call setsockopt(IP_TOS)");
234 err = getsockopt(sock_fd, SOL_IP, IP_TOS, &buf, &optlen);
236 log_err("Failed to call getsockopt(IP_TOS)");
240 if (buf != 0x80 + 2 * 0x10) {
241 log_err("Unexpected getsockopt 0x%x != 0x80 + 2 * 0x10", buf);
247 bpf_link__destroy(link_child);
248 bpf_link__destroy(link_parent);
253 void test_sockopt_multi(void)
255 int cg_parent = -1, cg_child = -1;
256 struct sockopt_multi *obj = NULL;
259 cg_parent = test__join_cgroup("/parent");
260 if (!ASSERT_GE(cg_parent, 0, "join_cgroup /parent"))
263 cg_child = test__join_cgroup("/parent/child");
264 if (!ASSERT_GE(cg_child, 0, "join_cgroup /parent/child"))
267 obj = sockopt_multi__open_and_load();
268 if (!ASSERT_OK_PTR(obj, "skel-load"))
271 obj->bss->page_size = sysconf(_SC_PAGESIZE);
273 sock_fd = socket(AF_INET, SOCK_STREAM, 0);
274 if (!ASSERT_GE(sock_fd, 0, "socket"))
277 ASSERT_OK(run_getsockopt_test(obj, cg_parent, cg_child, sock_fd), "getsockopt_test");
278 ASSERT_OK(run_setsockopt_test(obj, cg_parent, cg_child, sock_fd), "setsockopt_test");
282 sockopt_multi__destroy(obj);