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