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>
12 
13 #include "bpf_rlimit.h"
14 #include "bpf_util.h"
15 #include "cgroup_helpers.h"
16 
17 #include "test_tcpbpf.h"
18 
19 #define EXPECT_EQ(expected, actual, fmt)			\
20 	do {							\
21 		if ((expected) != (actual)) {			\
22 			printf("  Value of: " #actual "\n"	\
23 			       "    Actual: %" fmt "\n"		\
24 			       "  Expected: %" fmt "\n",	\
25 			       (actual), (expected));		\
26 			goto err;				\
27 		}						\
28 	} while (0)
29 
verify_result(const struct tcpbpf_globals * result)30 int verify_result(const struct tcpbpf_globals *result)
31 {
32 	__u32 expected_events;
33 
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));
42 
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);
51 
52 	return 0;
53 err:
54 	return -1;
55 }
56 
bpf_find_map(const char * test,struct bpf_object * obj,const char * name)57 static int bpf_find_map(const char *test, struct bpf_object *obj,
58 			const char *name)
59 {
60 	struct bpf_map *map;
61 
62 	map = bpf_object__find_map_by_name(obj, name);
63 	if (!map) {
64 		printf("%s:FAIL:map '%s' not found\n", test, name);
65 		return -1;
66 	}
67 	return bpf_map__fd(map);
68 }
69 
main(int argc,char ** argv)70 int main(int argc, char **argv)
71 {
72 	const char *file = "test_tcpbpf_kern.o";
73 	struct tcpbpf_globals g = {0};
74 	const char *cg_path = "/foo";
75 	int error = EXIT_FAILURE;
76 	struct bpf_object *obj;
77 	int prog_fd, map_fd;
78 	int cg_fd = -1;
79 	__u32 key = 0;
80 	int rv;
81 
82 	if (setup_cgroup_environment())
83 		goto err;
84 
85 	cg_fd = create_and_get_cgroup(cg_path);
86 	if (!cg_fd)
87 		goto err;
88 
89 	if (join_cgroup(cg_path))
90 		goto err;
91 
92 	if (bpf_prog_load(file, BPF_PROG_TYPE_SOCK_OPS, &obj, &prog_fd)) {
93 		printf("FAILED: load_bpf_file failed for: %s\n", file);
94 		goto err;
95 	}
96 
97 	rv = bpf_prog_attach(prog_fd, cg_fd, BPF_CGROUP_SOCK_OPS, 0);
98 	if (rv) {
99 		printf("FAILED: bpf_prog_attach: %d (%s)\n",
100 		       error, strerror(errno));
101 		goto err;
102 	}
103 
104 	if (system("./tcp_server.py")) {
105 		printf("FAILED: TCP server\n");
106 		goto err;
107 	}
108 
109 	map_fd = bpf_find_map(__func__, obj, "global_map");
110 	if (map_fd < 0)
111 		goto err;
112 
113 	rv = bpf_map_lookup_elem(map_fd, &key, &g);
114 	if (rv != 0) {
115 		printf("FAILED: bpf_map_lookup_elem returns %d\n", rv);
116 		goto err;
117 	}
118 
119 	if (verify_result(&g)) {
120 		printf("FAILED: Wrong stats\n");
121 		goto err;
122 	}
123 
124 	printf("PASSED!\n");
125 	error = 0;
126 err:
127 	bpf_prog_detach(cg_fd, BPF_CGROUP_SOCK_OPS);
128 	close(cg_fd);
129 	cleanup_cgroup_environment();
130 	return error;
131 }
132