Lawrence Brakmo | d6d4f60 | 2018-01-25 16:14:16 -0800 | [diff] [blame] | 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> |
Yonghong Song | 615a947 | 2018-02-13 10:35:05 -0800 | [diff] [blame] | 14 | #include <sys/time.h> |
Lawrence Brakmo | d6d4f60 | 2018-01-25 16:14:16 -0800 | [diff] [blame] | 15 | #include <sys/types.h> |
| 16 | #include <sys/stat.h> |
| 17 | #include <fcntl.h> |
| 18 | #include <bpf/bpf.h> |
| 19 | #include <bpf/libbpf.h> |
| 20 | #include "bpf_util.h" |
Daniel Borkmann | fe8d662 | 2018-02-26 22:34:32 +0100 | [diff] [blame] | 21 | #include "bpf_rlimit.h" |
Lawrence Brakmo | d6d4f60 | 2018-01-25 16:14:16 -0800 | [diff] [blame] | 22 | #include <linux/perf_event.h> |
| 23 | #include "test_tcpbpf.h" |
| 24 | |
| 25 | static 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 | |
| 45 | int main(int argc, char **argv) |
| 46 | { |
| 47 | const char *file = "test_tcpbpf_kern.o"; |
| 48 | struct tcpbpf_globals g = {0}; |
| 49 | int cg_fd, prog_fd, map_fd; |
| 50 | bool debug_flag = false; |
| 51 | int error = EXIT_FAILURE; |
| 52 | struct bpf_object *obj; |
| 53 | char cmd[100], *dir; |
| 54 | struct stat buffer; |
| 55 | __u32 key = 0; |
| 56 | int pid; |
| 57 | int rv; |
| 58 | |
Lawrence Brakmo | d6d4f60 | 2018-01-25 16:14:16 -0800 | [diff] [blame] | 59 | if (argc > 1 && strcmp(argv[1], "-d") == 0) |
| 60 | debug_flag = true; |
| 61 | |
| 62 | dir = "/tmp/cgroupv2/foo"; |
| 63 | |
| 64 | if (stat(dir, &buffer) != 0) { |
| 65 | SYSTEM("mkdir -p /tmp/cgroupv2"); |
| 66 | SYSTEM("mount -t cgroup2 none /tmp/cgroupv2"); |
| 67 | SYSTEM("mkdir -p /tmp/cgroupv2/foo"); |
| 68 | } |
| 69 | pid = (int) getpid(); |
| 70 | sprintf(cmd, "echo %d >> /tmp/cgroupv2/foo/cgroup.procs", pid); |
| 71 | SYSTEM(cmd); |
| 72 | |
| 73 | cg_fd = open(dir, O_DIRECTORY, O_RDONLY); |
| 74 | if (bpf_prog_load(file, BPF_PROG_TYPE_SOCK_OPS, &obj, &prog_fd)) { |
| 75 | printf("FAILED: load_bpf_file failed for: %s\n", file); |
| 76 | goto err; |
| 77 | } |
| 78 | |
| 79 | rv = bpf_prog_attach(prog_fd, cg_fd, BPF_CGROUP_SOCK_OPS, 0); |
| 80 | if (rv) { |
| 81 | printf("FAILED: bpf_prog_attach: %d (%s)\n", |
| 82 | error, strerror(errno)); |
| 83 | goto err; |
| 84 | } |
| 85 | |
| 86 | SYSTEM("./tcp_server.py"); |
| 87 | |
| 88 | map_fd = bpf_find_map(__func__, obj, "global_map"); |
| 89 | if (map_fd < 0) |
| 90 | goto err; |
| 91 | |
| 92 | rv = bpf_map_lookup_elem(map_fd, &key, &g); |
| 93 | if (rv != 0) { |
| 94 | printf("FAILED: bpf_map_lookup_elem returns %d\n", rv); |
| 95 | goto err; |
| 96 | } |
| 97 | |
| 98 | if (g.bytes_received != 501 || g.bytes_acked != 1002 || |
| 99 | g.data_segs_in != 1 || g.data_segs_out != 1 || |
| 100 | (g.event_map ^ 0x47e) != 0 || g.bad_cb_test_rv != 0x80 || |
| 101 | g.good_cb_test_rv != 0) { |
| 102 | printf("FAILED: Wrong stats\n"); |
| 103 | if (debug_flag) { |
| 104 | printf("\n"); |
| 105 | printf("bytes_received: %d (expecting 501)\n", |
| 106 | (int)g.bytes_received); |
| 107 | printf("bytes_acked: %d (expecting 1002)\n", |
| 108 | (int)g.bytes_acked); |
| 109 | printf("data_segs_in: %d (expecting 1)\n", |
| 110 | g.data_segs_in); |
| 111 | printf("data_segs_out: %d (expecting 1)\n", |
| 112 | g.data_segs_out); |
| 113 | printf("event_map: 0x%x (at least 0x47e)\n", |
| 114 | g.event_map); |
| 115 | printf("bad_cb_test_rv: 0x%x (expecting 0x80)\n", |
| 116 | g.bad_cb_test_rv); |
| 117 | printf("good_cb_test_rv:0x%x (expecting 0)\n", |
| 118 | g.good_cb_test_rv); |
| 119 | } |
| 120 | goto err; |
| 121 | } |
| 122 | printf("PASSED!\n"); |
| 123 | error = 0; |
| 124 | err: |
| 125 | bpf_prog_detach(cg_fd, BPF_CGROUP_SOCK_OPS); |
| 126 | return error; |
| 127 | |
| 128 | } |