1 // SPDX-License-Identifier: GPL-2.0 2 #define _GNU_SOURCE 3 #include <pthread.h> 4 #include <inttypes.h> 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <unistd.h> 8 #include <asm/types.h> 9 #include <sys/syscall.h> 10 #include <errno.h> 11 #include <string.h> 12 #include <linux/bpf.h> 13 #include <sys/socket.h> 14 #include <bpf/bpf.h> 15 #include <bpf/libbpf.h> 16 #include <sys/ioctl.h> 17 #include <linux/rtnetlink.h> 18 #include <linux/perf_event.h> 19 20 #include "cgroup_helpers.h" 21 22 #include "test_tcpnotify.h" 23 #include "testing_helpers.h" 24 25 #define SOCKET_BUFFER_SIZE (getpagesize() < 8192L ? getpagesize() : 8192L) 26 27 pthread_t tid; 28 static bool exit_thread; 29 30 int rx_callbacks; 31 32 static void dummyfn(void *ctx, int cpu, void *data, __u32 size) 33 { 34 struct tcp_notifier *t = data; 35 36 if (t->type != 0xde || t->subtype != 0xad || 37 t->source != 0xbe || t->hash != 0xef) 38 return; 39 rx_callbacks++; 40 } 41 42 void tcp_notifier_poller(struct perf_buffer *pb) 43 { 44 int err; 45 46 while (!exit_thread) { 47 err = perf_buffer__poll(pb, 100); 48 if (err < 0 && err != -EINTR) { 49 printf("failed perf_buffer__poll: %d\n", err); 50 return; 51 } 52 } 53 } 54 55 static void *poller_thread(void *arg) 56 { 57 struct perf_buffer *pb = arg; 58 59 tcp_notifier_poller(pb); 60 return arg; 61 } 62 63 int verify_result(const struct tcpnotify_globals *result) 64 { 65 return (result->ncalls > 0 && result->ncalls == rx_callbacks ? 0 : 1); 66 } 67 68 int main(int argc, char **argv) 69 { 70 const char *file = "test_tcpnotify_kern.bpf.o"; 71 struct bpf_map *perf_map, *global_map; 72 struct tcpnotify_globals g = {0}; 73 struct perf_buffer *pb = NULL; 74 const char *cg_path = "/foo"; 75 int prog_fd, rv, cg_fd = -1; 76 int error = EXIT_FAILURE; 77 struct bpf_object *obj; 78 char test_script[80]; 79 __u32 key = 0; 80 81 libbpf_set_strict_mode(LIBBPF_STRICT_ALL); 82 83 cg_fd = cgroup_setup_and_join(cg_path); 84 if (cg_fd < 0) 85 goto err; 86 87 if (bpf_prog_test_load(file, BPF_PROG_TYPE_SOCK_OPS, &obj, &prog_fd)) { 88 printf("FAILED: load_bpf_file failed for: %s\n", file); 89 goto err; 90 } 91 92 rv = bpf_prog_attach(prog_fd, cg_fd, BPF_CGROUP_SOCK_OPS, 0); 93 if (rv) { 94 printf("FAILED: bpf_prog_attach: %d (%s)\n", 95 error, strerror(errno)); 96 goto err; 97 } 98 99 perf_map = bpf_object__find_map_by_name(obj, "perf_event_map"); 100 if (!perf_map) { 101 printf("FAIL:map '%s' not found\n", "perf_event_map"); 102 goto err; 103 } 104 105 global_map = bpf_object__find_map_by_name(obj, "global_map"); 106 if (!global_map) { 107 printf("FAIL:map '%s' not found\n", "global_map"); 108 return -1; 109 } 110 111 pb = perf_buffer__new(bpf_map__fd(perf_map), 8, dummyfn, NULL, NULL, NULL); 112 if (!pb) 113 goto err; 114 115 pthread_create(&tid, NULL, poller_thread, pb); 116 117 sprintf(test_script, 118 "iptables -A INPUT -p tcp --dport %d -j DROP", 119 TESTPORT); 120 if (system(test_script)) { 121 printf("FAILED: execute command: %s, err %d\n", test_script, -errno); 122 goto err; 123 } 124 125 sprintf(test_script, 126 "nc 127.0.0.1 %d < /etc/passwd > /dev/null 2>&1 ", 127 TESTPORT); 128 if (system(test_script)) 129 printf("execute command: %s, err %d\n", test_script, -errno); 130 131 sprintf(test_script, 132 "iptables -D INPUT -p tcp --dport %d -j DROP", 133 TESTPORT); 134 if (system(test_script)) { 135 printf("FAILED: execute command: %s, err %d\n", test_script, -errno); 136 goto err; 137 } 138 139 rv = bpf_map_lookup_elem(bpf_map__fd(global_map), &key, &g); 140 if (rv != 0) { 141 printf("FAILED: bpf_map_lookup_elem returns %d\n", rv); 142 goto err; 143 } 144 145 sleep(10); 146 147 exit_thread = true; 148 int ret = pthread_join(tid, NULL); 149 if (ret) { 150 printf("FAILED: pthread_join\n"); 151 goto err; 152 } 153 154 if (verify_result(&g)) { 155 printf("FAILED: Wrong stats Expected %d calls, got %d\n", 156 g.ncalls, rx_callbacks); 157 goto err; 158 } 159 160 printf("PASSED!\n"); 161 error = 0; 162 err: 163 bpf_prog_detach(cg_fd, BPF_CGROUP_SOCK_OPS); 164 close(cg_fd); 165 cleanup_cgroup_environment(); 166 perf_buffer__free(pb); 167 return error; 168 } 169