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/* 3 comes from one listening socket + both ends of the connection */ 20#define EXPECTED_CLOSE_EVENTS 3 21 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) 32 33int verify_result(const struct tcpbpf_globals *result) 34{ 35 __u32 expected_events; 36 int ret = 0; 37 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)); 46 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); 56 57 return ret; 58} 59 60int verify_sockopt_result(int sock_map_fd) 61{ 62 __u32 key = 0; 63 int ret = 0; 64 int res; 65 int rv; 66 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; 77} 78 79static int bpf_find_map(const char *test, struct bpf_object *obj, 80 const char *name) 81{ 82 struct bpf_map *map; 83 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; 88 } 89 return bpf_map__fd(map); 90} 91 92int main(int argc, char **argv) 93{ 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; 104 105 cg_fd = cgroup_setup_and_join(cg_path); 106 if (cg_fd < 0) 107 goto err; 108 109 if (bpf_prog_load(file, BPF_PROG_TYPE_SOCK_OPS, &obj, &prog_fd)) { 110 printf("FAILED: load_bpf_file failed for: %s\n", file); 111 goto err; 112 } 113 114 rv = bpf_prog_attach(prog_fd, cg_fd, BPF_CGROUP_SOCK_OPS, 0); 115 if (rv) { 116 printf("FAILED: bpf_prog_attach: %d (%s)\n", 117 error, strerror(errno)); 118 goto err; 119 } 120 121 if (system("./tcp_server.py")) { 122 printf("FAILED: TCP server\n"); 123 goto err; 124 } 125 126 map_fd = bpf_find_map(__func__, obj, "global_map"); 127 if (map_fd < 0) 128 goto err; 129 130 sock_map_fd = bpf_find_map(__func__, obj, "sockopt_results"); 131 if (sock_map_fd < 0) 132 goto err; 133 134retry_lookup: 135 rv = bpf_map_lookup_elem(map_fd, &key, &g); 136 if (rv != 0) { 137 printf("FAILED: bpf_map_lookup_elem returns %d\n", rv); 138 goto err; 139 } 140 141 if (g.num_close_events != EXPECTED_CLOSE_EVENTS && retry--) { 142 printf("Unexpected number of close events (%d), retrying!\n", 143 g.num_close_events); 144 usleep(100); 145 goto retry_lookup; 146 } 147 148 if (verify_result(&g)) { 149 printf("FAILED: Wrong stats\n"); 150 goto err; 151 } 152 153 if (verify_sockopt_result(sock_map_fd)) { 154 printf("FAILED: Wrong sockopt stats\n"); 155 goto err; 156 } 157 158 printf("PASSED!\n"); 159 error = 0; 160err: 161 bpf_prog_detach(cg_fd, BPF_CGROUP_SOCK_OPS); 162 close(cg_fd); 163 cleanup_cgroup_environment(); 164 return error; 165} 166