WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / selftests / bpf / prog_tests / tcpbpf_user.c
blob87923d2865b7ba193bbfae9106d5b52d5ea8755b
1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include <network_helpers.h>
5 #include "test_tcpbpf.h"
6 #include "test_tcpbpf_kern.skel.h"
8 #define LO_ADDR6 "::1"
9 #define CG_NAME "/tcpbpf-user-test"
11 static __u32 duration;
13 static void verify_result(struct tcpbpf_globals *result)
15 __u32 expected_events = ((1 << BPF_SOCK_OPS_TIMEOUT_INIT) |
16 (1 << BPF_SOCK_OPS_RWND_INIT) |
17 (1 << BPF_SOCK_OPS_TCP_CONNECT_CB) |
18 (1 << BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB) |
19 (1 << BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB) |
20 (1 << BPF_SOCK_OPS_NEEDS_ECN) |
21 (1 << BPF_SOCK_OPS_STATE_CB) |
22 (1 << BPF_SOCK_OPS_TCP_LISTEN_CB));
24 /* check global map */
25 CHECK(expected_events != result->event_map, "event_map",
26 "unexpected event_map: actual 0x%08x != expected 0x%08x\n",
27 result->event_map, expected_events);
29 ASSERT_EQ(result->bytes_received, 501, "bytes_received");
30 ASSERT_EQ(result->bytes_acked, 1002, "bytes_acked");
31 ASSERT_EQ(result->data_segs_in, 1, "data_segs_in");
32 ASSERT_EQ(result->data_segs_out, 1, "data_segs_out");
33 ASSERT_EQ(result->bad_cb_test_rv, 0x80, "bad_cb_test_rv");
34 ASSERT_EQ(result->good_cb_test_rv, 0, "good_cb_test_rv");
35 ASSERT_EQ(result->num_listen, 1, "num_listen");
37 /* 3 comes from one listening socket + both ends of the connection */
38 ASSERT_EQ(result->num_close_events, 3, "num_close_events");
40 /* check setsockopt for SAVE_SYN */
41 ASSERT_EQ(result->tcp_save_syn, 0, "tcp_save_syn");
43 /* check getsockopt for SAVED_SYN */
44 ASSERT_EQ(result->tcp_saved_syn, 1, "tcp_saved_syn");
46 /* check getsockopt for window_clamp */
47 ASSERT_EQ(result->window_clamp_client, 9216, "window_clamp_client");
48 ASSERT_EQ(result->window_clamp_server, 9216, "window_clamp_server");
51 static void run_test(struct tcpbpf_globals *result)
53 int listen_fd = -1, cli_fd = -1, accept_fd = -1;
54 char buf[1000];
55 int err = -1;
56 int i, rv;
58 listen_fd = start_server(AF_INET6, SOCK_STREAM, LO_ADDR6, 0, 0);
59 if (CHECK(listen_fd == -1, "start_server", "listen_fd:%d errno:%d\n",
60 listen_fd, errno))
61 goto done;
63 cli_fd = connect_to_fd(listen_fd, 0);
64 if (CHECK(cli_fd == -1, "connect_to_fd(listen_fd)",
65 "cli_fd:%d errno:%d\n", cli_fd, errno))
66 goto done;
68 accept_fd = accept(listen_fd, NULL, NULL);
69 if (CHECK(accept_fd == -1, "accept(listen_fd)",
70 "accept_fd:%d errno:%d\n", accept_fd, errno))
71 goto done;
73 /* Send 1000B of '+'s from cli_fd -> accept_fd */
74 for (i = 0; i < 1000; i++)
75 buf[i] = '+';
77 rv = send(cli_fd, buf, 1000, 0);
78 if (CHECK(rv != 1000, "send(cli_fd)", "rv:%d errno:%d\n", rv, errno))
79 goto done;
81 rv = recv(accept_fd, buf, 1000, 0);
82 if (CHECK(rv != 1000, "recv(accept_fd)", "rv:%d errno:%d\n", rv, errno))
83 goto done;
85 /* Send 500B of '.'s from accept_fd ->cli_fd */
86 for (i = 0; i < 500; i++)
87 buf[i] = '.';
89 rv = send(accept_fd, buf, 500, 0);
90 if (CHECK(rv != 500, "send(accept_fd)", "rv:%d errno:%d\n", rv, errno))
91 goto done;
93 rv = recv(cli_fd, buf, 500, 0);
94 if (CHECK(rv != 500, "recv(cli_fd)", "rv:%d errno:%d\n", rv, errno))
95 goto done;
98 * shutdown accept first to guarantee correct ordering for
99 * bytes_received and bytes_acked when we go to verify the results.
101 shutdown(accept_fd, SHUT_WR);
102 err = recv(cli_fd, buf, 1, 0);
103 if (CHECK(err, "recv(cli_fd) for fin", "err:%d errno:%d\n", err, errno))
104 goto done;
106 shutdown(cli_fd, SHUT_WR);
107 err = recv(accept_fd, buf, 1, 0);
108 CHECK(err, "recv(accept_fd) for fin", "err:%d errno:%d\n", err, errno);
109 done:
110 if (accept_fd != -1)
111 close(accept_fd);
112 if (cli_fd != -1)
113 close(cli_fd);
114 if (listen_fd != -1)
115 close(listen_fd);
117 if (!err)
118 verify_result(result);
121 void test_tcpbpf_user(void)
123 struct test_tcpbpf_kern *skel;
124 int cg_fd = -1;
126 skel = test_tcpbpf_kern__open_and_load();
127 if (CHECK(!skel, "open and load skel", "failed"))
128 return;
130 cg_fd = test__join_cgroup(CG_NAME);
131 if (CHECK(cg_fd < 0, "test__join_cgroup(" CG_NAME ")",
132 "cg_fd:%d errno:%d", cg_fd, errno))
133 goto err;
135 skel->links.bpf_testcb = bpf_program__attach_cgroup(skel->progs.bpf_testcb, cg_fd);
136 if (!ASSERT_OK_PTR(skel->links.bpf_testcb, "attach_cgroup(bpf_testcb)"))
137 goto err;
139 run_test(&skel->bss->global);
141 err:
142 if (cg_fd != -1)
143 close(cg_fd);
144 test_tcpbpf_kern__destroy(skel);