blob: 95a370f3d3784574eec0787c45d2a849a98f5666 [file] [log] [blame]
Lawrence Brakmod6d4f602018-01-25 16:14:16 -08001// 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"
22
23static int bpf_find_map(const char *test, struct bpf_object *obj,
24 const char *name)
25{
26 struct bpf_map *map;
27
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;
32 }
33 return bpf_map__fd(map);
34}
35
36#define SYSTEM(CMD) \
37 do { \
38 if (system(CMD)) { \
39 printf("system(%s) FAILS!\n", CMD); \
40 } \
41 } while (0)
42
43int main(int argc, char **argv)
44{
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;
56
57 if (argc > 1 && strcmp(argv[1], "-d") == 0)
58 debug_flag = true;
59
60 dir = "/tmp/cgroupv2/foo";
61
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");
66 }
67 pid = (int) getpid();
68 sprintf(cmd, "echo %d >> /tmp/cgroupv2/foo/cgroup.procs", pid);
69 SYSTEM(cmd);
70
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;
75 }
76
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;
82 }
83
84 SYSTEM("./tcp_server.py");
85
86 map_fd = bpf_find_map(__func__, obj, "global_map");
87 if (map_fd < 0)
88 goto err;
89
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;
94 }
95
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);
117 }
118 goto err;
119 }
120 printf("PASSED!\n");
121 error = 0;
122err:
123 bpf_prog_detach(cg_fd, BPF_CGROUP_SOCK_OPS);
124 return error;
125
126}