1 // SPDX-License-Identifier: GPL-2.0
9 #include <sys/syscall.h>
12 #include <linux/bpf.h>
13 #include <sys/socket.h>
15 #include <bpf/libbpf.h>
16 #include <sys/ioctl.h>
17 #include <linux/rtnetlink.h>
19 #include <linux/perf_event.h>
21 #include "bpf_rlimit.h"
23 #include "cgroup_helpers.h"
25 #include "test_tcpnotify.h"
26 #include "trace_helpers.h"
28 #define SOCKET_BUFFER_SIZE (getpagesize() < 8192L ? getpagesize() : 8192L)
33 static int dummyfn(void *data
, int size
)
35 struct tcp_notifier
*t
= data
;
37 if (t
->type
!= 0xde || t
->subtype
!= 0xad ||
38 t
->source
!= 0xbe || t
->hash
!= 0xef)
44 void tcp_notifier_poller(int fd
)
47 perf_event_poller(fd
, dummyfn
);
50 static void *poller_thread(void *arg
)
54 tcp_notifier_poller(fd
);
58 int verify_result(const struct tcpnotify_globals
*result
)
60 return (result
->ncalls
> 0 && result
->ncalls
== rx_callbacks
? 0 : 1);
63 static int bpf_find_map(const char *test
, struct bpf_object
*obj
,
68 map
= bpf_object__find_map_by_name(obj
, name
);
70 printf("%s:FAIL:map '%s' not found\n", test
, name
);
73 return bpf_map__fd(map
);
76 static int setup_bpf_perf_event(int mapfd
)
78 struct perf_event_attr attr
= {
79 .sample_type
= PERF_SAMPLE_RAW
,
80 .type
= PERF_TYPE_SOFTWARE
,
81 .config
= PERF_COUNT_SW_BPF_OUTPUT
,
86 pmu_fd
= syscall(__NR_perf_event_open
, &attr
, -1, 0, -1, 0);
89 bpf_map_update_elem(mapfd
, &key
, &pmu_fd
, BPF_ANY
);
91 ioctl(pmu_fd
, PERF_EVENT_IOC_ENABLE
, 0);
95 int main(int argc
, char **argv
)
97 const char *file
= "test_tcpnotify_kern.o";
98 int prog_fd
, map_fd
, perf_event_fd
;
99 struct tcpnotify_globals g
= {0};
100 const char *cg_path
= "/foo";
101 int error
= EXIT_FAILURE
;
102 struct bpf_object
*obj
;
106 char test_script
[80];
112 pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t
), &cpuset
);
114 if (setup_cgroup_environment())
117 cg_fd
= create_and_get_cgroup(cg_path
);
121 if (join_cgroup(cg_path
))
124 if (bpf_prog_load(file
, BPF_PROG_TYPE_SOCK_OPS
, &obj
, &prog_fd
)) {
125 printf("FAILED: load_bpf_file failed for: %s\n", file
);
129 rv
= bpf_prog_attach(prog_fd
, cg_fd
, BPF_CGROUP_SOCK_OPS
, 0);
131 printf("FAILED: bpf_prog_attach: %d (%s)\n",
132 error
, strerror(errno
));
136 perf_event_fd
= bpf_find_map(__func__
, obj
, "perf_event_map");
137 if (perf_event_fd
< 0)
140 map_fd
= bpf_find_map(__func__
, obj
, "global_map");
144 pmu_fd
= setup_bpf_perf_event(perf_event_fd
);
145 if (pmu_fd
< 0 || perf_event_mmap(pmu_fd
) < 0)
148 pthread_create(&tid
, NULL
, poller_thread
, (void *)&pmu_fd
);
151 "iptables -A INPUT -p tcp --dport %d -j DROP",
156 "nc 127.0.0.1 %d < /etc/passwd > /dev/null 2>&1 ",
161 "iptables -D INPUT -p tcp --dport %d -j DROP",
165 rv
= bpf_map_lookup_elem(map_fd
, &key
, &g
);
167 printf("FAILED: bpf_map_lookup_elem returns %d\n", rv
);
173 if (verify_result(&g
)) {
174 printf("FAILED: Wrong stats Expected %d calls, got %d\n",
175 g
.ncalls
, rx_callbacks
);
182 bpf_prog_detach(cg_fd
, BPF_CGROUP_SOCK_OPS
);
184 cleanup_cgroup_environment();