1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Copyright (c) 2017 Facebook 3 */ 4 #include <stdio.h> 5 #include <unistd.h> 6 #include <fcntl.h> 7 #include <stdlib.h> 8 #include <string.h> 9 #include <linux/perf_event.h> 10 #include <errno.h> 11 #include <sys/resource.h> 12 #include <bpf/libbpf.h> 13 #include <bpf/bpf.h> 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 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 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 } 35 if (val == 0) { 36 fprintf(stderr, "failed: map #%d returns value 0\n", map_id); 37 return; 38 } 39 40 printf("verify map:%d val: %d\n", map_id, val); 41 42 val = 0; 43 if (bpf_map_update_elem(map_id, &key, &val, BPF_ANY) != 0) { 44 fprintf(stderr, "map_update failed: %s\n", strerror(errno)); 45 return; 46 } 47 } 48 49 static int test(char *filename, int num_progs) 50 { 51 int map0_fds[num_progs], map1_fds[num_progs], fd, i, j = 0; 52 struct bpf_link *links[num_progs * 4]; 53 struct bpf_object *objs[num_progs]; 54 struct bpf_program *prog; 55 56 for (i = 0; i < num_progs; i++) { 57 objs[i] = bpf_object__open_file(filename, NULL); 58 if (libbpf_get_error(objs[i])) { 59 fprintf(stderr, "opening BPF object file failed\n"); 60 objs[i] = NULL; 61 goto cleanup; 62 } 63 64 /* load BPF program */ 65 if (bpf_object__load(objs[i])) { 66 fprintf(stderr, "loading BPF object file failed\n"); 67 goto cleanup; 68 } 69 70 map0_fds[i] = bpf_object__find_map_fd_by_name(objs[i], 71 "enter_open_map"); 72 map1_fds[i] = bpf_object__find_map_fd_by_name(objs[i], 73 "exit_open_map"); 74 if (map0_fds[i] < 0 || map1_fds[i] < 0) { 75 fprintf(stderr, "finding a map in obj file failed\n"); 76 goto cleanup; 77 } 78 79 bpf_object__for_each_program(prog, objs[i]) { 80 links[j] = bpf_program__attach(prog); 81 if (libbpf_get_error(links[j])) { 82 fprintf(stderr, "bpf_program__attach failed\n"); 83 links[j] = NULL; 84 goto cleanup; 85 } 86 j++; 87 } 88 printf("prog #%d: map ids %d %d\n", i, map0_fds[i], map1_fds[i]); 89 } 90 91 /* current load_bpf_file has perf_event_open default pid = -1 92 * and cpu = 0, which permits attached bpf execution on 93 * all cpus for all pid's. bpf program execution ignores 94 * cpu affinity. 95 */ 96 /* trigger some "open" operations */ 97 fd = open(filename, O_RDONLY); 98 if (fd < 0) { 99 fprintf(stderr, "open failed: %s\n", strerror(errno)); 100 return 1; 101 } 102 close(fd); 103 104 /* verify the map */ 105 for (i = 0; i < num_progs; i++) { 106 verify_map(map0_fds[i]); 107 verify_map(map1_fds[i]); 108 } 109 110 cleanup: 111 for (j--; j >= 0; j--) 112 bpf_link__destroy(links[j]); 113 114 for (i--; i >= 0; i--) 115 bpf_object__close(objs[i]); 116 return 0; 117 } 118 119 int main(int argc, char **argv) 120 { 121 int opt, num_progs = 1; 122 char filename[256]; 123 124 while ((opt = getopt(argc, argv, "i:h")) != -1) { 125 switch (opt) { 126 case 'i': 127 num_progs = atoi(optarg); 128 break; 129 case 'h': 130 default: 131 usage(argv[0]); 132 return 0; 133 } 134 } 135 136 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); 137 138 return test(filename, num_progs); 139 } 140