treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / tools / testing / selftests / bpf / test_tcpbpf_user.c
blob3ae127620463da9cfbdd96b236e96f1a91a1c1ae
1 // SPDX-License-Identifier: GPL-2.0
2 #include <inttypes.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <errno.h>
7 #include <string.h>
8 #include <linux/bpf.h>
9 #include <sys/types.h>
10 #include <bpf/bpf.h>
11 #include <bpf/libbpf.h>
13 #include "bpf_rlimit.h"
14 #include "bpf_util.h"
15 #include "cgroup_helpers.h"
17 #include "test_tcpbpf.h"
19 /* 3 comes from one listening socket + both ends of the connection */
20 #define EXPECTED_CLOSE_EVENTS 3
22 #define EXPECT_EQ(expected, actual, fmt) \
23 do { \
24 if ((expected) != (actual)) { \
25 printf(" Value of: " #actual "\n" \
26 " Actual: %" fmt "\n" \
27 " Expected: %" fmt "\n", \
28 (actual), (expected)); \
29 ret--; \
30 } \
31 } while (0)
33 int verify_result(const struct tcpbpf_globals *result)
35 __u32 expected_events;
36 int ret = 0;
38 expected_events = ((1 << BPF_SOCK_OPS_TIMEOUT_INIT) |
39 (1 << BPF_SOCK_OPS_RWND_INIT) |
40 (1 << BPF_SOCK_OPS_TCP_CONNECT_CB) |
41 (1 << BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB) |
42 (1 << BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB) |
43 (1 << BPF_SOCK_OPS_NEEDS_ECN) |
44 (1 << BPF_SOCK_OPS_STATE_CB) |
45 (1 << BPF_SOCK_OPS_TCP_LISTEN_CB));
47 EXPECT_EQ(expected_events, result->event_map, "#" PRIx32);
48 EXPECT_EQ(501ULL, result->bytes_received, "llu");
49 EXPECT_EQ(1002ULL, result->bytes_acked, "llu");
50 EXPECT_EQ(1, result->data_segs_in, PRIu32);
51 EXPECT_EQ(1, result->data_segs_out, PRIu32);
52 EXPECT_EQ(0x80, result->bad_cb_test_rv, PRIu32);
53 EXPECT_EQ(0, result->good_cb_test_rv, PRIu32);
54 EXPECT_EQ(1, result->num_listen, PRIu32);
55 EXPECT_EQ(EXPECTED_CLOSE_EVENTS, result->num_close_events, PRIu32);
57 return ret;
60 int verify_sockopt_result(int sock_map_fd)
62 __u32 key = 0;
63 int ret = 0;
64 int res;
65 int rv;
67 /* check setsockopt for SAVE_SYN */
68 rv = bpf_map_lookup_elem(sock_map_fd, &key, &res);
69 EXPECT_EQ(0, rv, "d");
70 EXPECT_EQ(0, res, "d");
71 key = 1;
72 /* check getsockopt for SAVED_SYN */
73 rv = bpf_map_lookup_elem(sock_map_fd, &key, &res);
74 EXPECT_EQ(0, rv, "d");
75 EXPECT_EQ(1, res, "d");
76 return ret;
79 static int bpf_find_map(const char *test, struct bpf_object *obj,
80 const char *name)
82 struct bpf_map *map;
84 map = bpf_object__find_map_by_name(obj, name);
85 if (!map) {
86 printf("%s:FAIL:map '%s' not found\n", test, name);
87 return -1;
89 return bpf_map__fd(map);
92 int main(int argc, char **argv)
94 const char *file = "test_tcpbpf_kern.o";
95 int prog_fd, map_fd, sock_map_fd;
96 struct tcpbpf_globals g = {0};
97 const char *cg_path = "/foo";
98 int error = EXIT_FAILURE;
99 struct bpf_object *obj;
100 int cg_fd = -1;
101 int retry = 10;
102 __u32 key = 0;
103 int rv;
105 if (setup_cgroup_environment())
106 goto err;
108 cg_fd = create_and_get_cgroup(cg_path);
109 if (cg_fd < 0)
110 goto err;
112 if (join_cgroup(cg_path))
113 goto err;
115 if (bpf_prog_load(file, BPF_PROG_TYPE_SOCK_OPS, &obj, &prog_fd)) {
116 printf("FAILED: load_bpf_file failed for: %s\n", file);
117 goto err;
120 rv = bpf_prog_attach(prog_fd, cg_fd, BPF_CGROUP_SOCK_OPS, 0);
121 if (rv) {
122 printf("FAILED: bpf_prog_attach: %d (%s)\n",
123 error, strerror(errno));
124 goto err;
127 if (system("./tcp_server.py")) {
128 printf("FAILED: TCP server\n");
129 goto err;
132 map_fd = bpf_find_map(__func__, obj, "global_map");
133 if (map_fd < 0)
134 goto err;
136 sock_map_fd = bpf_find_map(__func__, obj, "sockopt_results");
137 if (sock_map_fd < 0)
138 goto err;
140 retry_lookup:
141 rv = bpf_map_lookup_elem(map_fd, &key, &g);
142 if (rv != 0) {
143 printf("FAILED: bpf_map_lookup_elem returns %d\n", rv);
144 goto err;
147 if (g.num_close_events != EXPECTED_CLOSE_EVENTS && retry--) {
148 printf("Unexpected number of close events (%d), retrying!\n",
149 g.num_close_events);
150 usleep(100);
151 goto retry_lookup;
154 if (verify_result(&g)) {
155 printf("FAILED: Wrong stats\n");
156 goto err;
159 if (verify_sockopt_result(sock_map_fd)) {
160 printf("FAILED: Wrong sockopt stats\n");
161 goto err;
164 printf("PASSED!\n");
165 error = 0;
166 err:
167 bpf_prog_detach(cg_fd, BPF_CGROUP_SOCK_OPS);
168 close(cg_fd);
169 cleanup_cgroup_environment();
170 return error;