Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / tools / testing / selftests / bpf / test_tcpbpf_user.c
blob95a370f3d3784574eec0787c45d2a849a98f5666
1 // SPDX-License-Identifier: GPL-2.0
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <unistd.h>
6 #include <errno.h>
7 #include <signal.h>
8 #include <string.h>
9 #include <assert.h>
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>
15 #include <sys/stat.h>
16 #include <fcntl.h>
17 #include <bpf/bpf.h>
18 #include <bpf/libbpf.h>
19 #include "bpf_util.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,
24 const char *name)
26 struct bpf_map *map;
28 map = bpf_object__find_map_by_name(obj, name);
29 if (!map) {
30 printf("%s:FAIL:map '%s' not found\n", test, name);
31 return -1;
33 return bpf_map__fd(map);
36 #define SYSTEM(CMD) \
37 do { \
38 if (system(CMD)) { \
39 printf("system(%s) FAILS!\n", CMD); \
40 } \
41 } while (0)
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;
51 char cmd[100], *dir;
52 struct stat buffer;
53 __u32 key = 0;
54 int pid;
55 int rv;
57 if (argc > 1 && strcmp(argv[1], "-d") == 0)
58 debug_flag = true;
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");
67 pid = (int) getpid();
68 sprintf(cmd, "echo %d >> /tmp/cgroupv2/foo/cgroup.procs", pid);
69 SYSTEM(cmd);
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);
74 goto err;
77 rv = bpf_prog_attach(prog_fd, cg_fd, BPF_CGROUP_SOCK_OPS, 0);
78 if (rv) {
79 printf("FAILED: bpf_prog_attach: %d (%s)\n",
80 error, strerror(errno));
81 goto err;
84 SYSTEM("./tcp_server.py");
86 map_fd = bpf_find_map(__func__, obj, "global_map");
87 if (map_fd < 0)
88 goto err;
90 rv = bpf_map_lookup_elem(map_fd, &key, &g);
91 if (rv != 0) {
92 printf("FAILED: bpf_map_lookup_elem returns %d\n", rv);
93 goto err;
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");
101 if (debug_flag) {
102 printf("\n");
103 printf("bytes_received: %d (expecting 501)\n",
104 (int)g.bytes_received);
105 printf("bytes_acked: %d (expecting 1002)\n",
106 (int)g.bytes_acked);
107 printf("data_segs_in: %d (expecting 1)\n",
108 g.data_segs_in);
109 printf("data_segs_out: %d (expecting 1)\n",
110 g.data_segs_out);
111 printf("event_map: 0x%x (at least 0x47e)\n",
112 g.event_map);
113 printf("bad_cb_test_rv: 0x%x (expecting 0x80)\n",
114 g.bad_cb_test_rv);
115 printf("good_cb_test_rv:0x%x (expecting 0)\n",
116 g.good_cb_test_rv);
118 goto err;
120 printf("PASSED!\n");
121 error = 0;
122 err:
123 bpf_prog_detach(cg_fd, BPF_CGROUP_SOCK_OPS);
124 return error;