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"
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;
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",
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
))
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
))
73 /* Send 1000B of '+'s from cli_fd -> accept_fd */
74 for (i
= 0; i
< 1000; i
++)
77 rv
= send(cli_fd
, buf
, 1000, 0);
78 if (CHECK(rv
!= 1000, "send(cli_fd)", "rv:%d errno:%d\n", rv
, errno
))
81 rv
= recv(accept_fd
, buf
, 1000, 0);
82 if (CHECK(rv
!= 1000, "recv(accept_fd)", "rv:%d errno:%d\n", rv
, errno
))
85 /* Send 500B of '.'s from accept_fd ->cli_fd */
86 for (i
= 0; i
< 500; i
++)
89 rv
= send(accept_fd
, buf
, 500, 0);
90 if (CHECK(rv
!= 500, "send(accept_fd)", "rv:%d errno:%d\n", rv
, errno
))
93 rv
= recv(cli_fd
, buf
, 500, 0);
94 if (CHECK(rv
!= 500, "recv(cli_fd)", "rv:%d errno:%d\n", rv
, errno
))
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
))
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
);
118 verify_result(result
);
121 void test_tcpbpf_user(void)
123 struct test_tcpbpf_kern
*skel
;
126 skel
= test_tcpbpf_kern__open_and_load();
127 if (CHECK(!skel
, "open and load skel", "failed"))
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
))
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)"))
139 run_test(&skel
->bss
->global
);
144 test_tcpbpf_kern__destroy(skel
);