WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / selftests / bpf / prog_tests / cgroup_skb_sk_lookup.c
blob464edc1c1708667199f9cc4eede87851503d0606
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2020 Facebook
4 #include <test_progs.h>
6 #include "network_helpers.h"
7 #include "cgroup_skb_sk_lookup_kern.skel.h"
9 static void run_lookup_test(__u16 *g_serv_port, int out_sk)
11 int serv_sk = -1, in_sk = -1, serv_in_sk = -1, err;
12 struct sockaddr_in6 addr = {};
13 socklen_t addr_len = sizeof(addr);
14 __u32 duration = 0;
16 serv_sk = start_server(AF_INET6, SOCK_STREAM, NULL, 0, 0);
17 if (CHECK(serv_sk < 0, "start_server", "failed to start server\n"))
18 return;
20 err = getsockname(serv_sk, (struct sockaddr *)&addr, &addr_len);
21 if (CHECK(err, "getsockname", "errno %d\n", errno))
22 goto cleanup;
24 *g_serv_port = addr.sin6_port;
26 /* Client outside of test cgroup should fail to connect by timeout. */
27 err = connect_fd_to_fd(out_sk, serv_sk, 1000);
28 if (CHECK(!err || errno != EINPROGRESS, "connect_fd_to_fd",
29 "unexpected result err %d errno %d\n", err, errno))
30 goto cleanup;
32 /* Client inside test cgroup should connect just fine. */
33 in_sk = connect_to_fd(serv_sk, 0);
34 if (CHECK(in_sk < 0, "connect_to_fd", "errno %d\n", errno))
35 goto cleanup;
37 serv_in_sk = accept(serv_sk, NULL, NULL);
38 if (CHECK(serv_in_sk < 0, "accept", "errno %d\n", errno))
39 goto cleanup;
41 cleanup:
42 close(serv_in_sk);
43 close(in_sk);
44 close(serv_sk);
47 static void run_cgroup_bpf_test(const char *cg_path, int out_sk)
49 struct cgroup_skb_sk_lookup_kern *skel;
50 struct bpf_link *link;
51 __u32 duration = 0;
52 int cgfd = -1;
54 skel = cgroup_skb_sk_lookup_kern__open_and_load();
55 if (CHECK(!skel, "skel_open_load", "open_load failed\n"))
56 return;
58 cgfd = test__join_cgroup(cg_path);
59 if (CHECK(cgfd < 0, "cgroup_join", "cgroup setup failed\n"))
60 goto cleanup;
62 link = bpf_program__attach_cgroup(skel->progs.ingress_lookup, cgfd);
63 if (CHECK(IS_ERR(link), "cgroup_attach", "err: %ld\n", PTR_ERR(link)))
64 goto cleanup;
66 run_lookup_test(&skel->bss->g_serv_port, out_sk);
68 bpf_link__destroy(link);
70 cleanup:
71 close(cgfd);
72 cgroup_skb_sk_lookup_kern__destroy(skel);
75 void test_cgroup_skb_sk_lookup(void)
77 const char *cg_path = "/foo";
78 int out_sk;
80 /* Create a socket before joining testing cgroup so that its cgroup id
81 * differs from that of testing cgroup. Moving selftests process to
82 * testing cgroup won't change cgroup id of an already created socket.
84 out_sk = socket(AF_INET6, SOCK_STREAM, 0);
85 if (CHECK_FAIL(out_sk < 0))
86 return;
88 run_cgroup_bpf_test(cg_path, out_sk);
90 close(out_sk);