1 // SPDX-License-Identifier: GPL-2.0
11 #include <bpf/libbpf.h>
13 #include "bpf_rlimit.h"
15 #include "cgroup_helpers.h"
17 #include "test_tcpbpf.h"
19 #define EXPECT_EQ(expected, actual, fmt) \
21 if ((expected) != (actual)) { \
22 printf(" Value of: " #actual "\n" \
23 " Actual: %" fmt "\n" \
24 " Expected: %" fmt "\n", \
25 (actual), (expected)); \
30 int verify_result(const struct tcpbpf_globals
*result
)
32 __u32 expected_events
;
34 expected_events
= ((1 << BPF_SOCK_OPS_TIMEOUT_INIT
) |
35 (1 << BPF_SOCK_OPS_RWND_INIT
) |
36 (1 << BPF_SOCK_OPS_TCP_CONNECT_CB
) |
37 (1 << BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB
) |
38 (1 << BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB
) |
39 (1 << BPF_SOCK_OPS_NEEDS_ECN
) |
40 (1 << BPF_SOCK_OPS_STATE_CB
) |
41 (1 << BPF_SOCK_OPS_TCP_LISTEN_CB
));
43 EXPECT_EQ(expected_events
, result
->event_map
, "#" PRIx32
);
44 EXPECT_EQ(501ULL, result
->bytes_received
, "llu");
45 EXPECT_EQ(1002ULL, result
->bytes_acked
, "llu");
46 EXPECT_EQ(1, result
->data_segs_in
, PRIu32
);
47 EXPECT_EQ(1, result
->data_segs_out
, PRIu32
);
48 EXPECT_EQ(0x80, result
->bad_cb_test_rv
, PRIu32
);
49 EXPECT_EQ(0, result
->good_cb_test_rv
, PRIu32
);
50 EXPECT_EQ(1, result
->num_listen
, PRIu32
);
57 int verify_sockopt_result(int sock_map_fd
)
63 /* check setsockopt for SAVE_SYN */
64 rv
= bpf_map_lookup_elem(sock_map_fd
, &key
, &res
);
65 EXPECT_EQ(0, rv
, "d");
66 EXPECT_EQ(0, res
, "d");
68 /* check getsockopt for SAVED_SYN */
69 rv
= bpf_map_lookup_elem(sock_map_fd
, &key
, &res
);
70 EXPECT_EQ(0, rv
, "d");
71 EXPECT_EQ(1, res
, "d");
77 static int bpf_find_map(const char *test
, struct bpf_object
*obj
,
82 map
= bpf_object__find_map_by_name(obj
, name
);
84 printf("%s:FAIL:map '%s' not found\n", test
, name
);
87 return bpf_map__fd(map
);
90 int main(int argc
, char **argv
)
92 const char *file
= "test_tcpbpf_kern.o";
93 int prog_fd
, map_fd
, sock_map_fd
;
94 struct tcpbpf_globals g
= {0};
95 const char *cg_path
= "/foo";
96 int error
= EXIT_FAILURE
;
97 struct bpf_object
*obj
;
102 if (setup_cgroup_environment())
105 cg_fd
= create_and_get_cgroup(cg_path
);
109 if (join_cgroup(cg_path
))
112 if (bpf_prog_load(file
, BPF_PROG_TYPE_SOCK_OPS
, &obj
, &prog_fd
)) {
113 printf("FAILED: load_bpf_file failed for: %s\n", file
);
117 rv
= bpf_prog_attach(prog_fd
, cg_fd
, BPF_CGROUP_SOCK_OPS
, 0);
119 printf("FAILED: bpf_prog_attach: %d (%s)\n",
120 error
, strerror(errno
));
124 if (system("./tcp_server.py")) {
125 printf("FAILED: TCP server\n");
129 map_fd
= bpf_find_map(__func__
, obj
, "global_map");
133 sock_map_fd
= bpf_find_map(__func__
, obj
, "sockopt_results");
137 rv
= bpf_map_lookup_elem(map_fd
, &key
, &g
);
139 printf("FAILED: bpf_map_lookup_elem returns %d\n", rv
);
143 if (verify_result(&g
)) {
144 printf("FAILED: Wrong stats\n");
148 if (verify_sockopt_result(sock_map_fd
)) {
149 printf("FAILED: Wrong sockopt stats\n");
156 bpf_prog_detach(cg_fd
, BPF_CGROUP_SOCK_OPS
);
158 cleanup_cgroup_environment();