1 // SPDX-License-Identifier: GPL-2.0
10 #include <linux/perf_event.h>
11 #include <linux/ptrace.h>
12 #include <linux/bpf.h>
13 #include <sys/ioctl.h>
14 #include <sys/types.h>
18 #include <bpf/libbpf.h>
20 #include <linux/perf_event.h>
21 #include "test_tcpbpf.h"
23 static int bpf_find_map(const char *test
, struct bpf_object
*obj
,
28 map
= bpf_object__find_map_by_name(obj
, name
);
30 printf("%s:FAIL:map '%s' not found\n", test
, name
);
33 return bpf_map__fd(map
);
39 printf("system(%s) FAILS!\n", CMD); \
43 int main(int argc
, char **argv
)
45 const char *file
= "test_tcpbpf_kern.o";
46 struct tcpbpf_globals g
= {0};
47 int cg_fd
, prog_fd
, map_fd
;
48 bool debug_flag
= false;
49 int error
= EXIT_FAILURE
;
50 struct bpf_object
*obj
;
57 if (argc
> 1 && strcmp(argv
[1], "-d") == 0)
60 dir
= "/tmp/cgroupv2/foo";
62 if (stat(dir
, &buffer
) != 0) {
63 SYSTEM("mkdir -p /tmp/cgroupv2");
64 SYSTEM("mount -t cgroup2 none /tmp/cgroupv2");
65 SYSTEM("mkdir -p /tmp/cgroupv2/foo");
68 sprintf(cmd
, "echo %d >> /tmp/cgroupv2/foo/cgroup.procs", pid
);
71 cg_fd
= open(dir
, O_DIRECTORY
, O_RDONLY
);
72 if (bpf_prog_load(file
, BPF_PROG_TYPE_SOCK_OPS
, &obj
, &prog_fd
)) {
73 printf("FAILED: load_bpf_file failed for: %s\n", file
);
77 rv
= bpf_prog_attach(prog_fd
, cg_fd
, BPF_CGROUP_SOCK_OPS
, 0);
79 printf("FAILED: bpf_prog_attach: %d (%s)\n",
80 error
, strerror(errno
));
84 SYSTEM("./tcp_server.py");
86 map_fd
= bpf_find_map(__func__
, obj
, "global_map");
90 rv
= bpf_map_lookup_elem(map_fd
, &key
, &g
);
92 printf("FAILED: bpf_map_lookup_elem returns %d\n", rv
);
96 if (g
.bytes_received
!= 501 || g
.bytes_acked
!= 1002 ||
97 g
.data_segs_in
!= 1 || g
.data_segs_out
!= 1 ||
98 (g
.event_map
^ 0x47e) != 0 || g
.bad_cb_test_rv
!= 0x80 ||
99 g
.good_cb_test_rv
!= 0) {
100 printf("FAILED: Wrong stats\n");
103 printf("bytes_received: %d (expecting 501)\n",
104 (int)g
.bytes_received
);
105 printf("bytes_acked: %d (expecting 1002)\n",
107 printf("data_segs_in: %d (expecting 1)\n",
109 printf("data_segs_out: %d (expecting 1)\n",
111 printf("event_map: 0x%x (at least 0x47e)\n",
113 printf("bad_cb_test_rv: 0x%x (expecting 0x80)\n",
115 printf("good_cb_test_rv:0x%x (expecting 0)\n",
123 bpf_prog_detach(cg_fd
, BPF_CGROUP_SOCK_OPS
);