Thomas Gleixner | 25763b3 | 2019-05-28 10:10:09 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Yonghong Song | 1da236b6b | 2017-08-04 16:00:10 -0700 | [diff] [blame] | 2 | /* Copyright (c) 2017 Facebook |
Yonghong Song | 1da236b6b | 2017-08-04 16:00:10 -0700 | [diff] [blame] | 3 | */ |
| 4 | #include <stdio.h> |
| 5 | #include <unistd.h> |
| 6 | #include <fcntl.h> |
| 7 | #include <stdlib.h> |
Yonghong Song | 1da236b6b | 2017-08-04 16:00:10 -0700 | [diff] [blame] | 8 | #include <string.h> |
| 9 | #include <linux/perf_event.h> |
| 10 | #include <errno.h> |
Yonghong Song | 1da236b6b | 2017-08-04 16:00:10 -0700 | [diff] [blame] | 11 | #include <sys/resource.h> |
Daniel T. Lee | f0c328f | 2020-08-23 17:53:34 +0900 | [diff] [blame] | 12 | #include <bpf/libbpf.h> |
Jakub Kicinski | 2bf3e2e | 2018-05-14 22:35:02 -0700 | [diff] [blame] | 13 | #include <bpf/bpf.h> |
Yonghong Song | 1da236b6b | 2017-08-04 16:00:10 -0700 | [diff] [blame] | 14 | |
| 15 | /* This program verifies bpf attachment to tracepoint sys_enter_* and sys_exit_*. |
| 16 | * This requires kernel CONFIG_FTRACE_SYSCALLS to be set. |
| 17 | */ |
| 18 | |
Yonghong Song | a678be5 | 2017-10-23 23:53:09 -0700 | [diff] [blame] | 19 | static void usage(const char *cmd) |
| 20 | { |
| 21 | printf("USAGE: %s [-i num_progs] [-h]\n", cmd); |
| 22 | printf(" -i num_progs # number of progs of the test\n"); |
| 23 | printf(" -h # help\n"); |
| 24 | } |
| 25 | |
Yonghong Song | 1da236b6b | 2017-08-04 16:00:10 -0700 | [diff] [blame] | 26 | static void verify_map(int map_id) |
| 27 | { |
| 28 | __u32 key = 0; |
| 29 | __u32 val; |
| 30 | |
| 31 | if (bpf_map_lookup_elem(map_id, &key, &val) != 0) { |
| 32 | fprintf(stderr, "map_lookup failed: %s\n", strerror(errno)); |
| 33 | return; |
| 34 | } |
Yonghong Song | a678be5 | 2017-10-23 23:53:09 -0700 | [diff] [blame] | 35 | if (val == 0) { |
Yonghong Song | 1da236b6b | 2017-08-04 16:00:10 -0700 | [diff] [blame] | 36 | fprintf(stderr, "failed: map #%d returns value 0\n", map_id); |
Yonghong Song | a678be5 | 2017-10-23 23:53:09 -0700 | [diff] [blame] | 37 | return; |
| 38 | } |
| 39 | val = 0; |
| 40 | if (bpf_map_update_elem(map_id, &key, &val, BPF_ANY) != 0) { |
| 41 | fprintf(stderr, "map_update failed: %s\n", strerror(errno)); |
| 42 | return; |
| 43 | } |
Yonghong Song | 1da236b6b | 2017-08-04 16:00:10 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Yonghong Song | a678be5 | 2017-10-23 23:53:09 -0700 | [diff] [blame] | 46 | static int test(char *filename, int num_progs) |
Yonghong Song | 1da236b6b | 2017-08-04 16:00:10 -0700 | [diff] [blame] | 47 | { |
Daniel T. Lee | f0c328f | 2020-08-23 17:53:34 +0900 | [diff] [blame] | 48 | int map0_fds[num_progs], map1_fds[num_progs], fd, i, j = 0; |
| 49 | struct bpf_link *links[num_progs * 4]; |
| 50 | struct bpf_object *objs[num_progs]; |
| 51 | struct bpf_program *prog; |
Yonghong Song | 1da236b6b | 2017-08-04 16:00:10 -0700 | [diff] [blame] | 52 | |
Yonghong Song | a678be5 | 2017-10-23 23:53:09 -0700 | [diff] [blame] | 53 | for (i = 0; i < num_progs; i++) { |
Daniel T. Lee | f0c328f | 2020-08-23 17:53:34 +0900 | [diff] [blame] | 54 | objs[i] = bpf_object__open_file(filename, NULL); |
| 55 | if (libbpf_get_error(objs[i])) { |
| 56 | fprintf(stderr, "opening BPF object file failed\n"); |
| 57 | objs[i] = NULL; |
| 58 | goto cleanup; |
Yonghong Song | a678be5 | 2017-10-23 23:53:09 -0700 | [diff] [blame] | 59 | } |
Daniel T. Lee | f0c328f | 2020-08-23 17:53:34 +0900 | [diff] [blame] | 60 | |
| 61 | /* load BPF program */ |
| 62 | if (bpf_object__load(objs[i])) { |
| 63 | fprintf(stderr, "loading BPF object file failed\n"); |
| 64 | goto cleanup; |
| 65 | } |
| 66 | |
| 67 | map0_fds[i] = bpf_object__find_map_fd_by_name(objs[i], |
| 68 | "enter_open_map"); |
| 69 | map1_fds[i] = bpf_object__find_map_fd_by_name(objs[i], |
| 70 | "exit_open_map"); |
| 71 | if (map0_fds[i] < 0 || map1_fds[i] < 0) { |
| 72 | fprintf(stderr, "finding a map in obj file failed\n"); |
| 73 | goto cleanup; |
| 74 | } |
| 75 | |
| 76 | bpf_object__for_each_program(prog, objs[i]) { |
| 77 | links[j] = bpf_program__attach(prog); |
| 78 | if (libbpf_get_error(links[j])) { |
| 79 | fprintf(stderr, "bpf_program__attach failed\n"); |
| 80 | links[j] = NULL; |
| 81 | goto cleanup; |
| 82 | } |
| 83 | j++; |
| 84 | } |
| 85 | printf("prog #%d: map ids %d %d\n", i, map0_fds[i], map1_fds[i]); |
Yonghong Song | 1da236b6b | 2017-08-04 16:00:10 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /* current load_bpf_file has perf_event_open default pid = -1 |
| 89 | * and cpu = 0, which permits attached bpf execution on |
| 90 | * all cpus for all pid's. bpf program execution ignores |
| 91 | * cpu affinity. |
| 92 | */ |
| 93 | /* trigger some "open" operations */ |
| 94 | fd = open(filename, O_RDONLY); |
| 95 | if (fd < 0) { |
| 96 | fprintf(stderr, "open failed: %s\n", strerror(errno)); |
| 97 | return 1; |
| 98 | } |
| 99 | close(fd); |
| 100 | |
| 101 | /* verify the map */ |
Yonghong Song | a678be5 | 2017-10-23 23:53:09 -0700 | [diff] [blame] | 102 | for (i = 0; i < num_progs; i++) { |
| 103 | verify_map(map0_fds[i]); |
| 104 | verify_map(map1_fds[i]); |
| 105 | } |
Yonghong Song | 1da236b6b | 2017-08-04 16:00:10 -0700 | [diff] [blame] | 106 | |
Daniel T. Lee | f0c328f | 2020-08-23 17:53:34 +0900 | [diff] [blame] | 107 | cleanup: |
| 108 | for (j--; j >= 0; j--) |
| 109 | bpf_link__destroy(links[j]); |
| 110 | |
| 111 | for (i--; i >= 0; i--) |
| 112 | bpf_object__close(objs[i]); |
Yonghong Song | 1da236b6b | 2017-08-04 16:00:10 -0700 | [diff] [blame] | 113 | return 0; |
| 114 | } |
Yonghong Song | a678be5 | 2017-10-23 23:53:09 -0700 | [diff] [blame] | 115 | |
| 116 | int main(int argc, char **argv) |
| 117 | { |
Yonghong Song | a678be5 | 2017-10-23 23:53:09 -0700 | [diff] [blame] | 118 | int opt, num_progs = 1; |
| 119 | char filename[256]; |
| 120 | |
| 121 | while ((opt = getopt(argc, argv, "i:h")) != -1) { |
| 122 | switch (opt) { |
| 123 | case 'i': |
| 124 | num_progs = atoi(optarg); |
| 125 | break; |
| 126 | case 'h': |
| 127 | default: |
| 128 | usage(argv[0]); |
| 129 | return 0; |
| 130 | } |
| 131 | } |
| 132 | |
Yonghong Song | a678be5 | 2017-10-23 23:53:09 -0700 | [diff] [blame] | 133 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
| 134 | |
| 135 | return test(filename, num_progs); |
| 136 | } |