Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 2 | #include <stdio.h> |
| 3 | #include <sys/types.h> |
| 4 | #include <sys/stat.h> |
| 5 | #include <fcntl.h> |
| 6 | #include <libelf.h> |
| 7 | #include <gelf.h> |
| 8 | #include <errno.h> |
| 9 | #include <unistd.h> |
| 10 | #include <string.h> |
| 11 | #include <stdbool.h> |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 12 | #include <stdlib.h> |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 13 | #include <linux/bpf.h> |
| 14 | #include <linux/filter.h> |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 15 | #include <linux/perf_event.h> |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 16 | #include <linux/netlink.h> |
| 17 | #include <linux/rtnetlink.h> |
Jesper Dangaard Brouer | 6387d01 | 2017-05-01 11:26:15 +0200 | [diff] [blame] | 18 | #include <linux/types.h> |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 19 | #include <sys/types.h> |
| 20 | #include <sys/socket.h> |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 21 | #include <sys/syscall.h> |
| 22 | #include <sys/ioctl.h> |
| 23 | #include <sys/mman.h> |
| 24 | #include <poll.h> |
Alexei Starovoitov | 5bacd78 | 2015-05-19 16:59:05 -0700 | [diff] [blame] | 25 | #include <ctype.h> |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 26 | #include <assert.h> |
Jakub Kicinski | 2bf3e2e | 2018-05-14 22:35:02 -0700 | [diff] [blame] | 27 | #include <bpf/bpf.h> |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 28 | #include "bpf_load.h" |
Joe Stringer | 205c8ad | 2016-12-08 18:46:19 -0800 | [diff] [blame] | 29 | #include "perf-sys.h" |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 30 | |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 31 | #define DEBUGFS "/sys/kernel/debug/tracing/" |
| 32 | |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 33 | static char license[128]; |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 34 | static int kern_version; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 35 | static bool processed_sec[128]; |
Joe Stringer | d40fc18 | 2016-12-14 14:43:38 -0800 | [diff] [blame] | 36 | char bpf_log_buf[BPF_LOG_BUF_SIZE]; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 37 | int map_fd[MAX_MAPS]; |
| 38 | int prog_fd[MAX_PROGS]; |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 39 | int event_fd[MAX_PROGS]; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 40 | int prog_cnt; |
Alexei Starovoitov | 5bacd78 | 2015-05-19 16:59:05 -0700 | [diff] [blame] | 41 | int prog_array_fd = -1; |
| 42 | |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 43 | struct bpf_map_data map_data[MAX_MAPS]; |
| 44 | int map_data_count = 0; |
| 45 | |
Alexei Starovoitov | 5bacd78 | 2015-05-19 16:59:05 -0700 | [diff] [blame] | 46 | static int populate_prog_array(const char *event, int prog_fd) |
| 47 | { |
| 48 | int ind = atoi(event), err; |
| 49 | |
Joe Stringer | d40fc18 | 2016-12-14 14:43:38 -0800 | [diff] [blame] | 50 | err = bpf_map_update_elem(prog_array_fd, &ind, &prog_fd, BPF_ANY); |
Alexei Starovoitov | 5bacd78 | 2015-05-19 16:59:05 -0700 | [diff] [blame] | 51 | if (err < 0) { |
| 52 | printf("failed to store prog_fd in prog_array\n"); |
| 53 | return -1; |
| 54 | } |
| 55 | return 0; |
| 56 | } |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 57 | |
| 58 | static int load_and_attach(const char *event, struct bpf_insn *prog, int size) |
| 59 | { |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 60 | bool is_socket = strncmp(event, "socket", 6) == 0; |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 61 | bool is_kprobe = strncmp(event, "kprobe/", 7) == 0; |
| 62 | bool is_kretprobe = strncmp(event, "kretprobe/", 10) == 0; |
Alexei Starovoitov | c076604 | 2016-04-06 18:43:29 -0700 | [diff] [blame] | 63 | bool is_tracepoint = strncmp(event, "tracepoint/", 11) == 0; |
Alexei Starovoitov | 4662a4e | 2018-03-28 12:05:39 -0700 | [diff] [blame] | 64 | bool is_raw_tracepoint = strncmp(event, "raw_tracepoint/", 15) == 0; |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 65 | bool is_xdp = strncmp(event, "xdp", 3) == 0; |
Alexei Starovoitov | 1c47910e | 2016-09-01 18:37:25 -0700 | [diff] [blame] | 66 | bool is_perf_event = strncmp(event, "perf_event", 10) == 0; |
David Ahern | 4f2e7ae | 2016-12-01 08:48:07 -0800 | [diff] [blame] | 67 | bool is_cgroup_skb = strncmp(event, "cgroup/skb", 10) == 0; |
| 68 | bool is_cgroup_sk = strncmp(event, "cgroup/sock", 11) == 0; |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 69 | bool is_sockops = strncmp(event, "sockops", 7) == 0; |
John Fastabend | 69e8cc1 | 2017-08-15 22:33:32 -0700 | [diff] [blame] | 70 | bool is_sk_skb = strncmp(event, "sk_skb", 6) == 0; |
John Fastabend | 4c4c3c2 | 2018-03-18 12:57:41 -0700 | [diff] [blame] | 71 | bool is_sk_msg = strncmp(event, "sk_msg", 6) == 0; |
Joe Stringer | 43371c8 | 2016-12-14 14:43:39 -0800 | [diff] [blame] | 72 | size_t insns_cnt = size / sizeof(struct bpf_insn); |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 73 | enum bpf_prog_type prog_type; |
| 74 | char buf[256]; |
| 75 | int fd, efd, err, id; |
| 76 | struct perf_event_attr attr = {}; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 77 | |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 78 | attr.type = PERF_TYPE_TRACEPOINT; |
| 79 | attr.sample_type = PERF_SAMPLE_RAW; |
| 80 | attr.sample_period = 1; |
| 81 | attr.wakeup_events = 1; |
| 82 | |
| 83 | if (is_socket) { |
| 84 | prog_type = BPF_PROG_TYPE_SOCKET_FILTER; |
| 85 | } else if (is_kprobe || is_kretprobe) { |
| 86 | prog_type = BPF_PROG_TYPE_KPROBE; |
Alexei Starovoitov | c076604 | 2016-04-06 18:43:29 -0700 | [diff] [blame] | 87 | } else if (is_tracepoint) { |
| 88 | prog_type = BPF_PROG_TYPE_TRACEPOINT; |
Alexei Starovoitov | 4662a4e | 2018-03-28 12:05:39 -0700 | [diff] [blame] | 89 | } else if (is_raw_tracepoint) { |
| 90 | prog_type = BPF_PROG_TYPE_RAW_TRACEPOINT; |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 91 | } else if (is_xdp) { |
| 92 | prog_type = BPF_PROG_TYPE_XDP; |
Alexei Starovoitov | 1c47910e | 2016-09-01 18:37:25 -0700 | [diff] [blame] | 93 | } else if (is_perf_event) { |
| 94 | prog_type = BPF_PROG_TYPE_PERF_EVENT; |
David Ahern | 4f2e7ae | 2016-12-01 08:48:07 -0800 | [diff] [blame] | 95 | } else if (is_cgroup_skb) { |
| 96 | prog_type = BPF_PROG_TYPE_CGROUP_SKB; |
| 97 | } else if (is_cgroup_sk) { |
| 98 | prog_type = BPF_PROG_TYPE_CGROUP_SOCK; |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 99 | } else if (is_sockops) { |
| 100 | prog_type = BPF_PROG_TYPE_SOCK_OPS; |
John Fastabend | 69e8cc1 | 2017-08-15 22:33:32 -0700 | [diff] [blame] | 101 | } else if (is_sk_skb) { |
| 102 | prog_type = BPF_PROG_TYPE_SK_SKB; |
John Fastabend | 4c4c3c2 | 2018-03-18 12:57:41 -0700 | [diff] [blame] | 103 | } else if (is_sk_msg) { |
| 104 | prog_type = BPF_PROG_TYPE_SK_MSG; |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 105 | } else { |
| 106 | printf("Unknown event '%s'\n", event); |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 107 | return -1; |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 108 | } |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 109 | |
Joe Stringer | 43371c8 | 2016-12-14 14:43:39 -0800 | [diff] [blame] | 110 | fd = bpf_load_program(prog_type, prog, insns_cnt, license, kern_version, |
Joe Stringer | d40fc18 | 2016-12-14 14:43:38 -0800 | [diff] [blame] | 111 | bpf_log_buf, BPF_LOG_BUF_SIZE); |
Alexei Starovoitov | 5bacd78 | 2015-05-19 16:59:05 -0700 | [diff] [blame] | 112 | if (fd < 0) { |
Joe Stringer | d40fc18 | 2016-12-14 14:43:38 -0800 | [diff] [blame] | 113 | printf("bpf_load_program() err=%d\n%s", errno, bpf_log_buf); |
Alexei Starovoitov | 5bacd78 | 2015-05-19 16:59:05 -0700 | [diff] [blame] | 114 | return -1; |
| 115 | } |
| 116 | |
| 117 | prog_fd[prog_cnt++] = fd; |
| 118 | |
David Ahern | 4f2e7ae | 2016-12-01 08:48:07 -0800 | [diff] [blame] | 119 | if (is_xdp || is_perf_event || is_cgroup_skb || is_cgroup_sk) |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 120 | return 0; |
| 121 | |
John Fastabend | 4c4c3c2 | 2018-03-18 12:57:41 -0700 | [diff] [blame] | 122 | if (is_socket || is_sockops || is_sk_skb || is_sk_msg) { |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 123 | if (is_socket) |
| 124 | event += 6; |
| 125 | else |
| 126 | event += 7; |
Alexei Starovoitov | 5bacd78 | 2015-05-19 16:59:05 -0700 | [diff] [blame] | 127 | if (*event != '/') |
| 128 | return 0; |
| 129 | event++; |
| 130 | if (!isdigit(*event)) { |
| 131 | printf("invalid prog number\n"); |
| 132 | return -1; |
| 133 | } |
| 134 | return populate_prog_array(event, fd); |
| 135 | } |
| 136 | |
Alexei Starovoitov | 4662a4e | 2018-03-28 12:05:39 -0700 | [diff] [blame] | 137 | if (is_raw_tracepoint) { |
| 138 | efd = bpf_raw_tracepoint_open(event + 15, fd); |
| 139 | if (efd < 0) { |
| 140 | printf("tracepoint %s %s\n", event + 15, strerror(errno)); |
| 141 | return -1; |
| 142 | } |
| 143 | event_fd[prog_cnt - 1] = efd; |
| 144 | return 0; |
| 145 | } |
| 146 | |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 147 | if (is_kprobe || is_kretprobe) { |
Yonghong Song | 34745ae | 2018-04-29 19:27:48 -0700 | [diff] [blame] | 148 | bool need_normal_check = true; |
| 149 | const char *event_prefix = ""; |
| 150 | |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 151 | if (is_kprobe) |
| 152 | event += 7; |
| 153 | else |
| 154 | event += 10; |
| 155 | |
Alexei Starovoitov | 5bacd78 | 2015-05-19 16:59:05 -0700 | [diff] [blame] | 156 | if (*event == 0) { |
| 157 | printf("event name cannot be empty\n"); |
| 158 | return -1; |
| 159 | } |
| 160 | |
| 161 | if (isdigit(*event)) |
| 162 | return populate_prog_array(event, fd); |
| 163 | |
Yonghong Song | 34745ae | 2018-04-29 19:27:48 -0700 | [diff] [blame] | 164 | #ifdef __x86_64__ |
| 165 | if (strncmp(event, "sys_", 4) == 0) { |
| 166 | snprintf(buf, sizeof(buf), |
| 167 | "echo '%c:__x64_%s __x64_%s' >> /sys/kernel/debug/tracing/kprobe_events", |
| 168 | is_kprobe ? 'p' : 'r', event, event); |
| 169 | err = system(buf); |
| 170 | if (err >= 0) { |
| 171 | need_normal_check = false; |
| 172 | event_prefix = "__x64_"; |
| 173 | } |
| 174 | } |
| 175 | #endif |
| 176 | if (need_normal_check) { |
| 177 | snprintf(buf, sizeof(buf), |
| 178 | "echo '%c:%s %s' >> /sys/kernel/debug/tracing/kprobe_events", |
| 179 | is_kprobe ? 'p' : 'r', event, event); |
| 180 | err = system(buf); |
| 181 | if (err < 0) { |
| 182 | printf("failed to create kprobe '%s' error '%s'\n", |
| 183 | event, strerror(errno)); |
| 184 | return -1; |
| 185 | } |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 186 | } |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 187 | |
Alexei Starovoitov | c076604 | 2016-04-06 18:43:29 -0700 | [diff] [blame] | 188 | strcpy(buf, DEBUGFS); |
| 189 | strcat(buf, "events/kprobes/"); |
Yonghong Song | 34745ae | 2018-04-29 19:27:48 -0700 | [diff] [blame] | 190 | strcat(buf, event_prefix); |
Alexei Starovoitov | c076604 | 2016-04-06 18:43:29 -0700 | [diff] [blame] | 191 | strcat(buf, event); |
| 192 | strcat(buf, "/id"); |
| 193 | } else if (is_tracepoint) { |
| 194 | event += 11; |
| 195 | |
| 196 | if (*event == 0) { |
| 197 | printf("event name cannot be empty\n"); |
| 198 | return -1; |
| 199 | } |
| 200 | strcpy(buf, DEBUGFS); |
| 201 | strcat(buf, "events/"); |
| 202 | strcat(buf, event); |
| 203 | strcat(buf, "/id"); |
| 204 | } |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 205 | |
| 206 | efd = open(buf, O_RDONLY, 0); |
| 207 | if (efd < 0) { |
| 208 | printf("failed to open event %s\n", event); |
| 209 | return -1; |
| 210 | } |
| 211 | |
| 212 | err = read(efd, buf, sizeof(buf)); |
| 213 | if (err < 0 || err >= sizeof(buf)) { |
| 214 | printf("read from '%s' failed '%s'\n", event, strerror(errno)); |
| 215 | return -1; |
| 216 | } |
| 217 | |
| 218 | close(efd); |
| 219 | |
| 220 | buf[err] = 0; |
| 221 | id = atoi(buf); |
| 222 | attr.config = id; |
| 223 | |
Joe Stringer | 205c8ad | 2016-12-08 18:46:19 -0800 | [diff] [blame] | 224 | efd = sys_perf_event_open(&attr, -1/*pid*/, 0/*cpu*/, -1/*group_fd*/, 0); |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 225 | if (efd < 0) { |
| 226 | printf("event %d fd %d err %s\n", id, efd, strerror(errno)); |
| 227 | return -1; |
| 228 | } |
| 229 | event_fd[prog_cnt - 1] = efd; |
Yonghong Song | 0ec9552 | 2017-11-30 13:47:55 -0800 | [diff] [blame] | 230 | err = ioctl(efd, PERF_EVENT_IOC_ENABLE, 0); |
| 231 | if (err < 0) { |
| 232 | printf("ioctl PERF_EVENT_IOC_ENABLE failed err %s\n", |
| 233 | strerror(errno)); |
| 234 | return -1; |
| 235 | } |
| 236 | err = ioctl(efd, PERF_EVENT_IOC_SET_BPF, fd); |
| 237 | if (err < 0) { |
| 238 | printf("ioctl PERF_EVENT_IOC_SET_BPF failed err %s\n", |
| 239 | strerror(errno)); |
| 240 | return -1; |
| 241 | } |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 242 | |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 243 | return 0; |
| 244 | } |
| 245 | |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 246 | static int load_maps(struct bpf_map_data *maps, int nr_maps, |
| 247 | fixup_map_cb fixup_map) |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 248 | { |
Martin KaFai Lau | ad17d0e | 2017-08-18 11:28:01 -0700 | [diff] [blame] | 249 | int i, numa_node; |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 250 | |
Jesper Dangaard Brouer | 5010e94 | 2017-04-28 16:25:04 +0200 | [diff] [blame] | 251 | for (i = 0; i < nr_maps; i++) { |
Jesper Dangaard Brouer | 6979bcc | 2017-05-02 14:32:01 +0200 | [diff] [blame] | 252 | if (fixup_map) { |
| 253 | fixup_map(&maps[i], i); |
| 254 | /* Allow userspace to assign map FD prior to creation */ |
| 255 | if (maps[i].fd != -1) { |
| 256 | map_fd[i] = maps[i].fd; |
| 257 | continue; |
| 258 | } |
| 259 | } |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 260 | |
Martin KaFai Lau | ad17d0e | 2017-08-18 11:28:01 -0700 | [diff] [blame] | 261 | numa_node = maps[i].def.map_flags & BPF_F_NUMA_NODE ? |
| 262 | maps[i].def.numa_node : -1; |
| 263 | |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 264 | if (maps[i].def.type == BPF_MAP_TYPE_ARRAY_OF_MAPS || |
| 265 | maps[i].def.type == BPF_MAP_TYPE_HASH_OF_MAPS) { |
| 266 | int inner_map_fd = map_fd[maps[i].def.inner_map_idx]; |
Martin KaFai Lau | fb30d4b | 2017-03-22 10:00:35 -0700 | [diff] [blame] | 267 | |
Martin KaFai Lau | ad17d0e | 2017-08-18 11:28:01 -0700 | [diff] [blame] | 268 | map_fd[i] = bpf_create_map_in_map_node(maps[i].def.type, |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 269 | maps[i].name, |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 270 | maps[i].def.key_size, |
| 271 | inner_map_fd, |
| 272 | maps[i].def.max_entries, |
Martin KaFai Lau | ad17d0e | 2017-08-18 11:28:01 -0700 | [diff] [blame] | 273 | maps[i].def.map_flags, |
| 274 | numa_node); |
Martin KaFai Lau | fb30d4b | 2017-03-22 10:00:35 -0700 | [diff] [blame] | 275 | } else { |
Martin KaFai Lau | ad17d0e | 2017-08-18 11:28:01 -0700 | [diff] [blame] | 276 | map_fd[i] = bpf_create_map_node(maps[i].def.type, |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 277 | maps[i].name, |
Martin KaFai Lau | ad17d0e | 2017-08-18 11:28:01 -0700 | [diff] [blame] | 278 | maps[i].def.key_size, |
| 279 | maps[i].def.value_size, |
| 280 | maps[i].def.max_entries, |
| 281 | maps[i].def.map_flags, |
| 282 | numa_node); |
Martin KaFai Lau | fb30d4b | 2017-03-22 10:00:35 -0700 | [diff] [blame] | 283 | } |
Alexei Starovoitov | 618ec9a7 | 2016-03-07 21:57:18 -0800 | [diff] [blame] | 284 | if (map_fd[i] < 0) { |
| 285 | printf("failed to create a map: %d %s\n", |
| 286 | errno, strerror(errno)); |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 287 | return 1; |
Alexei Starovoitov | 618ec9a7 | 2016-03-07 21:57:18 -0800 | [diff] [blame] | 288 | } |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 289 | maps[i].fd = map_fd[i]; |
Alexei Starovoitov | 5bacd78 | 2015-05-19 16:59:05 -0700 | [diff] [blame] | 290 | |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 291 | if (maps[i].def.type == BPF_MAP_TYPE_PROG_ARRAY) |
Alexei Starovoitov | 5bacd78 | 2015-05-19 16:59:05 -0700 | [diff] [blame] | 292 | prog_array_fd = map_fd[i]; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 293 | } |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | static int get_sec(Elf *elf, int i, GElf_Ehdr *ehdr, char **shname, |
| 298 | GElf_Shdr *shdr, Elf_Data **data) |
| 299 | { |
| 300 | Elf_Scn *scn; |
| 301 | |
| 302 | scn = elf_getscn(elf, i); |
| 303 | if (!scn) |
| 304 | return 1; |
| 305 | |
| 306 | if (gelf_getshdr(scn, shdr) != shdr) |
| 307 | return 2; |
| 308 | |
| 309 | *shname = elf_strptr(elf, ehdr->e_shstrndx, shdr->sh_name); |
| 310 | if (!*shname || !shdr->sh_size) |
| 311 | return 3; |
| 312 | |
| 313 | *data = elf_getdata(scn, 0); |
| 314 | if (!*data || elf_getdata(scn, *data) != NULL) |
| 315 | return 4; |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | static int parse_relo_and_apply(Elf_Data *data, Elf_Data *symbols, |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 321 | GElf_Shdr *shdr, struct bpf_insn *insn, |
| 322 | struct bpf_map_data *maps, int nr_maps) |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 323 | { |
| 324 | int i, nrels; |
| 325 | |
| 326 | nrels = shdr->sh_size / shdr->sh_entsize; |
| 327 | |
| 328 | for (i = 0; i < nrels; i++) { |
| 329 | GElf_Sym sym; |
| 330 | GElf_Rel rel; |
| 331 | unsigned int insn_idx; |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 332 | bool match = false; |
| 333 | int j, map_idx; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 334 | |
| 335 | gelf_getrel(data, i, &rel); |
| 336 | |
| 337 | insn_idx = rel.r_offset / sizeof(struct bpf_insn); |
| 338 | |
| 339 | gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &sym); |
| 340 | |
| 341 | if (insn[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) { |
| 342 | printf("invalid relo for insn[%d].code 0x%x\n", |
| 343 | insn_idx, insn[insn_idx].code); |
| 344 | return 1; |
| 345 | } |
| 346 | insn[insn_idx].src_reg = BPF_PSEUDO_MAP_FD; |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 347 | |
| 348 | /* Match FD relocation against recorded map_data[] offset */ |
| 349 | for (map_idx = 0; map_idx < nr_maps; map_idx++) { |
| 350 | if (maps[map_idx].elf_offset == sym.st_value) { |
| 351 | match = true; |
| 352 | break; |
| 353 | } |
| 354 | } |
| 355 | if (match) { |
| 356 | insn[insn_idx].imm = maps[map_idx].fd; |
| 357 | } else { |
| 358 | printf("invalid relo for insn[%d] no map_data match\n", |
| 359 | insn_idx); |
| 360 | return 1; |
| 361 | } |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | return 0; |
| 365 | } |
| 366 | |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 367 | static int cmp_symbols(const void *l, const void *r) |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 368 | { |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 369 | const GElf_Sym *lsym = (const GElf_Sym *)l; |
| 370 | const GElf_Sym *rsym = (const GElf_Sym *)r; |
| 371 | |
| 372 | if (lsym->st_value < rsym->st_value) |
| 373 | return -1; |
| 374 | else if (lsym->st_value > rsym->st_value) |
| 375 | return 1; |
| 376 | else |
| 377 | return 0; |
| 378 | } |
| 379 | |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 380 | static int load_elf_maps_section(struct bpf_map_data *maps, int maps_shndx, |
| 381 | Elf *elf, Elf_Data *symbols, int strtabidx) |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 382 | { |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 383 | int map_sz_elf, map_sz_copy; |
| 384 | bool validate_zero = false; |
| 385 | Elf_Data *data_maps; |
| 386 | int i, nr_maps; |
| 387 | GElf_Sym *sym; |
| 388 | Elf_Scn *scn; |
| 389 | int copy_sz; |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 390 | |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 391 | if (maps_shndx < 0) |
| 392 | return -EINVAL; |
| 393 | if (!symbols) |
| 394 | return -EINVAL; |
| 395 | |
| 396 | /* Get data for maps section via elf index */ |
| 397 | scn = elf_getscn(elf, maps_shndx); |
| 398 | if (scn) |
| 399 | data_maps = elf_getdata(scn, NULL); |
| 400 | if (!scn || !data_maps) { |
| 401 | printf("Failed to get Elf_Data from maps section %d\n", |
| 402 | maps_shndx); |
| 403 | return -EINVAL; |
| 404 | } |
| 405 | |
| 406 | /* For each map get corrosponding symbol table entry */ |
| 407 | sym = calloc(MAX_MAPS+1, sizeof(GElf_Sym)); |
| 408 | for (i = 0, nr_maps = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) { |
| 409 | assert(nr_maps < MAX_MAPS+1); |
| 410 | if (!gelf_getsym(symbols, i, &sym[nr_maps])) |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 411 | continue; |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 412 | if (sym[nr_maps].st_shndx != maps_shndx) |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 413 | continue; |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 414 | /* Only increment iif maps section */ |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 415 | nr_maps++; |
| 416 | } |
| 417 | |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 418 | /* Align to map_fd[] order, via sort on offset in sym.st_value */ |
| 419 | qsort(sym, nr_maps, sizeof(GElf_Sym), cmp_symbols); |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 420 | |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 421 | /* Keeping compatible with ELF maps section changes |
| 422 | * ------------------------------------------------ |
Jakub Kicinski | 74662ea | 2018-05-10 10:24:38 -0700 | [diff] [blame] | 423 | * The program size of struct bpf_load_map_def is known by loader |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 424 | * code, but struct stored in ELF file can be different. |
| 425 | * |
| 426 | * Unfortunately sym[i].st_size is zero. To calculate the |
| 427 | * struct size stored in the ELF file, assume all struct have |
| 428 | * the same size, and simply divide with number of map |
| 429 | * symbols. |
| 430 | */ |
| 431 | map_sz_elf = data_maps->d_size / nr_maps; |
Jakub Kicinski | 74662ea | 2018-05-10 10:24:38 -0700 | [diff] [blame] | 432 | map_sz_copy = sizeof(struct bpf_load_map_def); |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 433 | if (map_sz_elf < map_sz_copy) { |
| 434 | /* |
| 435 | * Backward compat, loading older ELF file with |
| 436 | * smaller struct, keeping remaining bytes zero. |
| 437 | */ |
| 438 | map_sz_copy = map_sz_elf; |
| 439 | } else if (map_sz_elf > map_sz_copy) { |
| 440 | /* |
| 441 | * Forward compat, loading newer ELF file with larger |
| 442 | * struct with unknown features. Assume zero means |
| 443 | * feature not used. Thus, validate rest of struct |
| 444 | * data is zero. |
| 445 | */ |
| 446 | validate_zero = true; |
| 447 | } |
| 448 | |
| 449 | /* Memcpy relevant part of ELF maps data to loader maps */ |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 450 | for (i = 0; i < nr_maps; i++) { |
Jakub Kicinski | 74662ea | 2018-05-10 10:24:38 -0700 | [diff] [blame] | 451 | struct bpf_load_map_def *def; |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 452 | unsigned char *addr, *end; |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 453 | const char *map_name; |
| 454 | size_t offset; |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 455 | |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 456 | map_name = elf_strptr(elf, strtabidx, sym[i].st_name); |
| 457 | maps[i].name = strdup(map_name); |
| 458 | if (!maps[i].name) { |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 459 | printf("strdup(%s): %s(%d)\n", map_name, |
| 460 | strerror(errno), errno); |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 461 | free(sym); |
| 462 | return -errno; |
| 463 | } |
| 464 | |
| 465 | /* Symbol value is offset into ELF maps section data area */ |
| 466 | offset = sym[i].st_value; |
Jakub Kicinski | 74662ea | 2018-05-10 10:24:38 -0700 | [diff] [blame] | 467 | def = (struct bpf_load_map_def *)(data_maps->d_buf + offset); |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 468 | maps[i].elf_offset = offset; |
Jakub Kicinski | 74662ea | 2018-05-10 10:24:38 -0700 | [diff] [blame] | 469 | memset(&maps[i].def, 0, sizeof(struct bpf_load_map_def)); |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 470 | memcpy(&maps[i].def, def, map_sz_copy); |
| 471 | |
| 472 | /* Verify no newer features were requested */ |
| 473 | if (validate_zero) { |
| 474 | addr = (unsigned char*) def + map_sz_copy; |
| 475 | end = (unsigned char*) def + map_sz_elf; |
| 476 | for (; addr < end; addr++) { |
| 477 | if (*addr != 0) { |
| 478 | free(sym); |
| 479 | return -EFBIG; |
| 480 | } |
| 481 | } |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 485 | free(sym); |
Jesper Dangaard Brouer | 5010e94 | 2017-04-28 16:25:04 +0200 | [diff] [blame] | 486 | return nr_maps; |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | static int do_load_bpf_file(const char *path, fixup_map_cb fixup_map) |
| 490 | { |
| 491 | int fd, i, ret, maps_shndx = -1, strtabidx = -1; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 492 | Elf *elf; |
| 493 | GElf_Ehdr ehdr; |
| 494 | GElf_Shdr shdr, shdr_prog; |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 495 | Elf_Data *data, *data_prog, *data_maps = NULL, *symbols = NULL; |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 496 | char *shname, *shname_prog; |
| 497 | int nr_maps = 0; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 498 | |
Mickaël Salaün | a734fb5 | 2017-02-08 21:27:43 +0100 | [diff] [blame] | 499 | /* reset global variables */ |
| 500 | kern_version = 0; |
| 501 | memset(license, 0, sizeof(license)); |
| 502 | memset(processed_sec, 0, sizeof(processed_sec)); |
| 503 | |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 504 | if (elf_version(EV_CURRENT) == EV_NONE) |
| 505 | return 1; |
| 506 | |
| 507 | fd = open(path, O_RDONLY, 0); |
| 508 | if (fd < 0) |
| 509 | return 1; |
| 510 | |
| 511 | elf = elf_begin(fd, ELF_C_READ, NULL); |
| 512 | |
| 513 | if (!elf) |
| 514 | return 1; |
| 515 | |
| 516 | if (gelf_getehdr(elf, &ehdr) != &ehdr) |
| 517 | return 1; |
| 518 | |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 519 | /* clear all kprobes */ |
| 520 | i = system("echo \"\" > /sys/kernel/debug/tracing/kprobe_events"); |
| 521 | |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 522 | /* scan over all elf sections to get license and map info */ |
| 523 | for (i = 1; i < ehdr.e_shnum; i++) { |
| 524 | |
| 525 | if (get_sec(elf, i, &ehdr, &shname, &shdr, &data)) |
| 526 | continue; |
| 527 | |
| 528 | if (0) /* helpful for llvm debugging */ |
| 529 | printf("section %d:%s data %p size %zd link %d flags %d\n", |
| 530 | i, shname, data->d_buf, data->d_size, |
| 531 | shdr.sh_link, (int) shdr.sh_flags); |
| 532 | |
| 533 | if (strcmp(shname, "license") == 0) { |
| 534 | processed_sec[i] = true; |
| 535 | memcpy(license, data->d_buf, data->d_size); |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 536 | } else if (strcmp(shname, "version") == 0) { |
| 537 | processed_sec[i] = true; |
| 538 | if (data->d_size != sizeof(int)) { |
| 539 | printf("invalid size of version section %zd\n", |
| 540 | data->d_size); |
| 541 | return 1; |
| 542 | } |
| 543 | memcpy(&kern_version, data->d_buf, sizeof(int)); |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 544 | } else if (strcmp(shname, "maps") == 0) { |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 545 | int j; |
| 546 | |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 547 | maps_shndx = i; |
| 548 | data_maps = data; |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 549 | for (j = 0; j < MAX_MAPS; j++) |
| 550 | map_data[j].fd = -1; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 551 | } else if (shdr.sh_type == SHT_SYMTAB) { |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 552 | strtabidx = shdr.sh_link; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 553 | symbols = data; |
| 554 | } |
| 555 | } |
| 556 | |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 557 | ret = 1; |
| 558 | |
| 559 | if (!symbols) { |
| 560 | printf("missing SHT_SYMTAB section\n"); |
| 561 | goto done; |
| 562 | } |
| 563 | |
| 564 | if (data_maps) { |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 565 | nr_maps = load_elf_maps_section(map_data, maps_shndx, |
| 566 | elf, symbols, strtabidx); |
| 567 | if (nr_maps < 0) { |
| 568 | printf("Error: Failed loading ELF maps (errno:%d):%s\n", |
| 569 | nr_maps, strerror(-nr_maps)); |
Jesper Dangaard Brouer | 5010e94 | 2017-04-28 16:25:04 +0200 | [diff] [blame] | 570 | goto done; |
| 571 | } |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 572 | if (load_maps(map_data, nr_maps, fixup_map)) |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 573 | goto done; |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 574 | map_data_count = nr_maps; |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 575 | |
| 576 | processed_sec[maps_shndx] = true; |
| 577 | } |
| 578 | |
Jesper Dangaard Brouer | 7bc5795 | 2017-05-30 14:37:51 +0200 | [diff] [blame] | 579 | /* process all relo sections, and rewrite bpf insns for maps */ |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 580 | for (i = 1; i < ehdr.e_shnum; i++) { |
Mickaël Salaün | 16ad132 | 2017-02-08 21:27:42 +0100 | [diff] [blame] | 581 | if (processed_sec[i]) |
| 582 | continue; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 583 | |
| 584 | if (get_sec(elf, i, &ehdr, &shname, &shdr, &data)) |
| 585 | continue; |
Jesper Dangaard Brouer | 7bc5795 | 2017-05-30 14:37:51 +0200 | [diff] [blame] | 586 | |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 587 | if (shdr.sh_type == SHT_REL) { |
| 588 | struct bpf_insn *insns; |
| 589 | |
Jesper Dangaard Brouer | 7bc5795 | 2017-05-30 14:37:51 +0200 | [diff] [blame] | 590 | /* locate prog sec that need map fixup (relocations) */ |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 591 | if (get_sec(elf, shdr.sh_info, &ehdr, &shname_prog, |
| 592 | &shdr_prog, &data_prog)) |
| 593 | continue; |
| 594 | |
Alexei Starovoitov | db6a71d | 2016-11-22 16:52:09 -0800 | [diff] [blame] | 595 | if (shdr_prog.sh_type != SHT_PROGBITS || |
| 596 | !(shdr_prog.sh_flags & SHF_EXECINSTR)) |
| 597 | continue; |
| 598 | |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 599 | insns = (struct bpf_insn *) data_prog->d_buf; |
Jesper Dangaard Brouer | 7bc5795 | 2017-05-30 14:37:51 +0200 | [diff] [blame] | 600 | processed_sec[i] = true; /* relo section */ |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 601 | |
Jesper Dangaard Brouer | 156450d | 2017-05-02 14:31:56 +0200 | [diff] [blame] | 602 | if (parse_relo_and_apply(data, symbols, &shdr, insns, |
| 603 | map_data, nr_maps)) |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 604 | continue; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | |
Jesper Dangaard Brouer | 7bc5795 | 2017-05-30 14:37:51 +0200 | [diff] [blame] | 608 | /* load programs */ |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 609 | for (i = 1; i < ehdr.e_shnum; i++) { |
| 610 | |
| 611 | if (processed_sec[i]) |
| 612 | continue; |
| 613 | |
| 614 | if (get_sec(elf, i, &ehdr, &shname, &shdr, &data)) |
| 615 | continue; |
| 616 | |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 617 | if (memcmp(shname, "kprobe/", 7) == 0 || |
| 618 | memcmp(shname, "kretprobe/", 10) == 0 || |
Alexei Starovoitov | c076604 | 2016-04-06 18:43:29 -0700 | [diff] [blame] | 619 | memcmp(shname, "tracepoint/", 11) == 0 || |
Alexei Starovoitov | 4662a4e | 2018-03-28 12:05:39 -0700 | [diff] [blame] | 620 | memcmp(shname, "raw_tracepoint/", 15) == 0 || |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 621 | memcmp(shname, "xdp", 3) == 0 || |
Alexei Starovoitov | 1c47910e | 2016-09-01 18:37:25 -0700 | [diff] [blame] | 622 | memcmp(shname, "perf_event", 10) == 0 || |
David Ahern | 4f2e7ae | 2016-12-01 08:48:07 -0800 | [diff] [blame] | 623 | memcmp(shname, "socket", 6) == 0 || |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 624 | memcmp(shname, "cgroup/", 7) == 0 || |
John Fastabend | 69e8cc1 | 2017-08-15 22:33:32 -0700 | [diff] [blame] | 625 | memcmp(shname, "sockops", 7) == 0 || |
John Fastabend | 4c4c3c2 | 2018-03-18 12:57:41 -0700 | [diff] [blame] | 626 | memcmp(shname, "sk_skb", 6) == 0 || |
| 627 | memcmp(shname, "sk_msg", 6) == 0) { |
Lawrence Brakmo | f856e46 | 2017-07-04 15:57:50 -0700 | [diff] [blame] | 628 | ret = load_and_attach(shname, data->d_buf, |
| 629 | data->d_size); |
| 630 | if (ret != 0) |
| 631 | goto done; |
| 632 | } |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 633 | } |
| 634 | |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 635 | done: |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 636 | close(fd); |
Martin KaFai Lau | 9fd63d0 | 2017-04-14 10:30:28 -0700 | [diff] [blame] | 637 | return ret; |
| 638 | } |
| 639 | |
| 640 | int load_bpf_file(char *path) |
| 641 | { |
| 642 | return do_load_bpf_file(path, NULL); |
| 643 | } |
| 644 | |
| 645 | int load_bpf_file_fixup_map(const char *path, fixup_map_cb fixup_map) |
| 646 | { |
| 647 | return do_load_bpf_file(path, fixup_map); |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 648 | } |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 649 | |
| 650 | void read_trace_pipe(void) |
| 651 | { |
| 652 | int trace_fd; |
| 653 | |
| 654 | trace_fd = open(DEBUGFS "trace_pipe", O_RDONLY, 0); |
| 655 | if (trace_fd < 0) |
| 656 | return; |
| 657 | |
| 658 | while (1) { |
| 659 | static char buf[4096]; |
| 660 | ssize_t sz; |
| 661 | |
| 662 | sz = read(trace_fd, buf, sizeof(buf)); |
| 663 | if (sz > 0) { |
| 664 | buf[sz] = 0; |
| 665 | puts(buf); |
| 666 | } |
| 667 | } |
| 668 | } |