blob: 5d73db4164607da6577444bfe03719e09954a9a9 [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>
Yonghong Song615a9472018-02-13 10:35:05 -080014#include <sys/time.h>
15#include <sys/resource.h>
Lawrence Brakmod6d4f602018-01-25 16:14:16 -080016#include <sys/types.h>
17#include <sys/stat.h>
18#include <fcntl.h>
19#include <bpf/bpf.h>
20#include <bpf/libbpf.h>
21#include "bpf_util.h"
22#include <linux/perf_event.h>
23#include "test_tcpbpf.h"
24
25static int bpf_find_map(const char *test, struct bpf_object *obj,
26 const char *name)
27{
28 struct bpf_map *map;
29
30 map = bpf_object__find_map_by_name(obj, name);
31 if (!map) {
32 printf("%s:FAIL:map '%s' not found\n", test, name);
33 return -1;
34 }
35 return bpf_map__fd(map);
36}
37
38#define SYSTEM(CMD) \
39 do { \
40 if (system(CMD)) { \
41 printf("system(%s) FAILS!\n", CMD); \
42 } \
43 } while (0)
44
45int main(int argc, char **argv)
46{
Yonghong Song615a9472018-02-13 10:35:05 -080047 struct rlimit limit = { RLIM_INFINITY, RLIM_INFINITY };
Lawrence Brakmod6d4f602018-01-25 16:14:16 -080048 const char *file = "test_tcpbpf_kern.o";
49 struct tcpbpf_globals g = {0};
50 int cg_fd, prog_fd, map_fd;
51 bool debug_flag = false;
52 int error = EXIT_FAILURE;
53 struct bpf_object *obj;
54 char cmd[100], *dir;
55 struct stat buffer;
56 __u32 key = 0;
57 int pid;
58 int rv;
59
Yonghong Song615a9472018-02-13 10:35:05 -080060 if (setrlimit(RLIMIT_MEMLOCK, &limit) < 0)
61 perror("Unable to lift memlock rlimit");
62
Lawrence Brakmod6d4f602018-01-25 16:14:16 -080063 if (argc > 1 && strcmp(argv[1], "-d") == 0)
64 debug_flag = true;
65
66 dir = "/tmp/cgroupv2/foo";
67
68 if (stat(dir, &buffer) != 0) {
69 SYSTEM("mkdir -p /tmp/cgroupv2");
70 SYSTEM("mount -t cgroup2 none /tmp/cgroupv2");
71 SYSTEM("mkdir -p /tmp/cgroupv2/foo");
72 }
73 pid = (int) getpid();
74 sprintf(cmd, "echo %d >> /tmp/cgroupv2/foo/cgroup.procs", pid);
75 SYSTEM(cmd);
76
77 cg_fd = open(dir, O_DIRECTORY, O_RDONLY);
78 if (bpf_prog_load(file, BPF_PROG_TYPE_SOCK_OPS, &obj, &prog_fd)) {
79 printf("FAILED: load_bpf_file failed for: %s\n", file);
80 goto err;
81 }
82
83 rv = bpf_prog_attach(prog_fd, cg_fd, BPF_CGROUP_SOCK_OPS, 0);
84 if (rv) {
85 printf("FAILED: bpf_prog_attach: %d (%s)\n",
86 error, strerror(errno));
87 goto err;
88 }
89
90 SYSTEM("./tcp_server.py");
91
92 map_fd = bpf_find_map(__func__, obj, "global_map");
93 if (map_fd < 0)
94 goto err;
95
96 rv = bpf_map_lookup_elem(map_fd, &key, &g);
97 if (rv != 0) {
98 printf("FAILED: bpf_map_lookup_elem returns %d\n", rv);
99 goto err;
100 }
101
102 if (g.bytes_received != 501 || g.bytes_acked != 1002 ||
103 g.data_segs_in != 1 || g.data_segs_out != 1 ||
104 (g.event_map ^ 0x47e) != 0 || g.bad_cb_test_rv != 0x80 ||
105 g.good_cb_test_rv != 0) {
106 printf("FAILED: Wrong stats\n");
107 if (debug_flag) {
108 printf("\n");
109 printf("bytes_received: %d (expecting 501)\n",
110 (int)g.bytes_received);
111 printf("bytes_acked: %d (expecting 1002)\n",
112 (int)g.bytes_acked);
113 printf("data_segs_in: %d (expecting 1)\n",
114 g.data_segs_in);
115 printf("data_segs_out: %d (expecting 1)\n",
116 g.data_segs_out);
117 printf("event_map: 0x%x (at least 0x47e)\n",
118 g.event_map);
119 printf("bad_cb_test_rv: 0x%x (expecting 0x80)\n",
120 g.bad_cb_test_rv);
121 printf("good_cb_test_rv:0x%x (expecting 0)\n",
122 g.good_cb_test_rv);
123 }
124 goto err;
125 }
126 printf("PASSED!\n");
127 error = 0;
128err:
129 bpf_prog_detach(cg_fd, BPF_CGROUP_SOCK_OPS);
130 return error;
131
132}