blob: 904e775d1a44c5f9cd0582bf4db8af4b3b0a011a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Alexei Starovoitov249b8122014-12-01 15:06:37 -08002#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 Starovoitovb896c4f2015-03-25 12:49:23 -070012#include <stdlib.h>
Alexei Starovoitov249b8122014-12-01 15:06:37 -080013#include <linux/bpf.h>
14#include <linux/filter.h>
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070015#include <linux/perf_event.h>
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -080016#include <linux/netlink.h>
17#include <linux/rtnetlink.h>
Jesper Dangaard Brouer6387d012017-05-01 11:26:15 +020018#include <linux/types.h>
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -080019#include <sys/types.h>
20#include <sys/socket.h>
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070021#include <sys/syscall.h>
22#include <sys/ioctl.h>
23#include <sys/mman.h>
24#include <poll.h>
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070025#include <ctype.h>
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -070026#include <assert.h>
Jakub Kicinski2bf3e2e2018-05-14 22:35:02 -070027#include <bpf/bpf.h>
Alexei Starovoitov249b8122014-12-01 15:06:37 -080028#include "bpf_load.h"
Joe Stringer205c8ad2016-12-08 18:46:19 -080029#include "perf-sys.h"
Alexei Starovoitov249b8122014-12-01 15:06:37 -080030
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070031#define DEBUGFS "/sys/kernel/debug/tracing/"
32
Alexei Starovoitov249b8122014-12-01 15:06:37 -080033static char license[128];
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070034static int kern_version;
Alexei Starovoitov249b8122014-12-01 15:06:37 -080035static bool processed_sec[128];
Joe Stringerd40fc182016-12-14 14:43:38 -080036char bpf_log_buf[BPF_LOG_BUF_SIZE];
Alexei Starovoitov249b8122014-12-01 15:06:37 -080037int map_fd[MAX_MAPS];
38int prog_fd[MAX_PROGS];
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070039int event_fd[MAX_PROGS];
Alexei Starovoitov249b8122014-12-01 15:06:37 -080040int prog_cnt;
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070041int prog_array_fd = -1;
42
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +020043struct bpf_map_data map_data[MAX_MAPS];
44int map_data_count = 0;
45
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070046static int populate_prog_array(const char *event, int prog_fd)
47{
48 int ind = atoi(event), err;
49
Joe Stringerd40fc182016-12-14 14:43:38 -080050 err = bpf_map_update_elem(prog_array_fd, &ind, &prog_fd, BPF_ANY);
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070051 if (err < 0) {
52 printf("failed to store prog_fd in prog_array\n");
53 return -1;
54 }
55 return 0;
56}
Alexei Starovoitov249b8122014-12-01 15:06:37 -080057
58static int load_and_attach(const char *event, struct bpf_insn *prog, int size)
59{
Alexei Starovoitov249b8122014-12-01 15:06:37 -080060 bool is_socket = strncmp(event, "socket", 6) == 0;
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070061 bool is_kprobe = strncmp(event, "kprobe/", 7) == 0;
62 bool is_kretprobe = strncmp(event, "kretprobe/", 10) == 0;
Alexei Starovoitovc0766042016-04-06 18:43:29 -070063 bool is_tracepoint = strncmp(event, "tracepoint/", 11) == 0;
Alexei Starovoitov4662a4e2018-03-28 12:05:39 -070064 bool is_raw_tracepoint = strncmp(event, "raw_tracepoint/", 15) == 0;
Brenden Blanco86af8b42016-07-19 12:16:51 -070065 bool is_xdp = strncmp(event, "xdp", 3) == 0;
Alexei Starovoitov1c47910e2016-09-01 18:37:25 -070066 bool is_perf_event = strncmp(event, "perf_event", 10) == 0;
David Ahern4f2e7ae2016-12-01 08:48:07 -080067 bool is_cgroup_skb = strncmp(event, "cgroup/skb", 10) == 0;
68 bool is_cgroup_sk = strncmp(event, "cgroup/sock", 11) == 0;
Lawrence Brakmo40304b22017-06-30 20:02:40 -070069 bool is_sockops = strncmp(event, "sockops", 7) == 0;
John Fastabend69e8cc12017-08-15 22:33:32 -070070 bool is_sk_skb = strncmp(event, "sk_skb", 6) == 0;
John Fastabend4c4c3c22018-03-18 12:57:41 -070071 bool is_sk_msg = strncmp(event, "sk_msg", 6) == 0;
Joe Stringer43371c82016-12-14 14:43:39 -080072 size_t insns_cnt = size / sizeof(struct bpf_insn);
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070073 enum bpf_prog_type prog_type;
74 char buf[256];
75 int fd, efd, err, id;
76 struct perf_event_attr attr = {};
Alexei Starovoitov249b8122014-12-01 15:06:37 -080077
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070078 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 Starovoitovc0766042016-04-06 18:43:29 -070087 } else if (is_tracepoint) {
88 prog_type = BPF_PROG_TYPE_TRACEPOINT;
Alexei Starovoitov4662a4e2018-03-28 12:05:39 -070089 } else if (is_raw_tracepoint) {
90 prog_type = BPF_PROG_TYPE_RAW_TRACEPOINT;
Brenden Blanco86af8b42016-07-19 12:16:51 -070091 } else if (is_xdp) {
92 prog_type = BPF_PROG_TYPE_XDP;
Alexei Starovoitov1c47910e2016-09-01 18:37:25 -070093 } else if (is_perf_event) {
94 prog_type = BPF_PROG_TYPE_PERF_EVENT;
David Ahern4f2e7ae2016-12-01 08:48:07 -080095 } 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 Brakmo40304b22017-06-30 20:02:40 -070099 } else if (is_sockops) {
100 prog_type = BPF_PROG_TYPE_SOCK_OPS;
John Fastabend69e8cc12017-08-15 22:33:32 -0700101 } else if (is_sk_skb) {
102 prog_type = BPF_PROG_TYPE_SK_SKB;
John Fastabend4c4c3c22018-03-18 12:57:41 -0700103 } else if (is_sk_msg) {
104 prog_type = BPF_PROG_TYPE_SK_MSG;
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700105 } else {
106 printf("Unknown event '%s'\n", event);
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800107 return -1;
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700108 }
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800109
Dan Carpenterb0294bc2018-07-13 18:11:39 +0300110 if (prog_cnt == MAX_PROGS)
111 return -1;
112
Joe Stringer43371c82016-12-14 14:43:39 -0800113 fd = bpf_load_program(prog_type, prog, insns_cnt, license, kern_version,
Joe Stringerd40fc182016-12-14 14:43:38 -0800114 bpf_log_buf, BPF_LOG_BUF_SIZE);
Alexei Starovoitov5bacd782015-05-19 16:59:05 -0700115 if (fd < 0) {
Joe Stringerd40fc182016-12-14 14:43:38 -0800116 printf("bpf_load_program() err=%d\n%s", errno, bpf_log_buf);
Alexei Starovoitov5bacd782015-05-19 16:59:05 -0700117 return -1;
118 }
119
120 prog_fd[prog_cnt++] = fd;
121
David Ahern4f2e7ae2016-12-01 08:48:07 -0800122 if (is_xdp || is_perf_event || is_cgroup_skb || is_cgroup_sk)
Brenden Blanco86af8b42016-07-19 12:16:51 -0700123 return 0;
124
John Fastabend4c4c3c22018-03-18 12:57:41 -0700125 if (is_socket || is_sockops || is_sk_skb || is_sk_msg) {
Lawrence Brakmo40304b22017-06-30 20:02:40 -0700126 if (is_socket)
127 event += 6;
128 else
129 event += 7;
Alexei Starovoitov5bacd782015-05-19 16:59:05 -0700130 if (*event != '/')
131 return 0;
132 event++;
133 if (!isdigit(*event)) {
134 printf("invalid prog number\n");
135 return -1;
136 }
137 return populate_prog_array(event, fd);
138 }
139
Alexei Starovoitov4662a4e2018-03-28 12:05:39 -0700140 if (is_raw_tracepoint) {
141 efd = bpf_raw_tracepoint_open(event + 15, fd);
142 if (efd < 0) {
143 printf("tracepoint %s %s\n", event + 15, strerror(errno));
144 return -1;
145 }
146 event_fd[prog_cnt - 1] = efd;
147 return 0;
148 }
149
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700150 if (is_kprobe || is_kretprobe) {
Yonghong Song34745ae2018-04-29 19:27:48 -0700151 bool need_normal_check = true;
152 const char *event_prefix = "";
153
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700154 if (is_kprobe)
155 event += 7;
156 else
157 event += 10;
158
Alexei Starovoitov5bacd782015-05-19 16:59:05 -0700159 if (*event == 0) {
160 printf("event name cannot be empty\n");
161 return -1;
162 }
163
164 if (isdigit(*event))
165 return populate_prog_array(event, fd);
166
Yonghong Song34745ae2018-04-29 19:27:48 -0700167#ifdef __x86_64__
168 if (strncmp(event, "sys_", 4) == 0) {
169 snprintf(buf, sizeof(buf),
170 "echo '%c:__x64_%s __x64_%s' >> /sys/kernel/debug/tracing/kprobe_events",
171 is_kprobe ? 'p' : 'r', event, event);
172 err = system(buf);
173 if (err >= 0) {
174 need_normal_check = false;
175 event_prefix = "__x64_";
176 }
177 }
178#endif
179 if (need_normal_check) {
180 snprintf(buf, sizeof(buf),
181 "echo '%c:%s %s' >> /sys/kernel/debug/tracing/kprobe_events",
182 is_kprobe ? 'p' : 'r', event, event);
183 err = system(buf);
184 if (err < 0) {
185 printf("failed to create kprobe '%s' error '%s'\n",
186 event, strerror(errno));
187 return -1;
188 }
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700189 }
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700190
Alexei Starovoitovc0766042016-04-06 18:43:29 -0700191 strcpy(buf, DEBUGFS);
192 strcat(buf, "events/kprobes/");
Yonghong Song34745ae2018-04-29 19:27:48 -0700193 strcat(buf, event_prefix);
Alexei Starovoitovc0766042016-04-06 18:43:29 -0700194 strcat(buf, event);
195 strcat(buf, "/id");
196 } else if (is_tracepoint) {
197 event += 11;
198
199 if (*event == 0) {
200 printf("event name cannot be empty\n");
201 return -1;
202 }
203 strcpy(buf, DEBUGFS);
204 strcat(buf, "events/");
205 strcat(buf, event);
206 strcat(buf, "/id");
207 }
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700208
209 efd = open(buf, O_RDONLY, 0);
210 if (efd < 0) {
211 printf("failed to open event %s\n", event);
212 return -1;
213 }
214
215 err = read(efd, buf, sizeof(buf));
216 if (err < 0 || err >= sizeof(buf)) {
217 printf("read from '%s' failed '%s'\n", event, strerror(errno));
218 return -1;
219 }
220
221 close(efd);
222
223 buf[err] = 0;
224 id = atoi(buf);
225 attr.config = id;
226
Joe Stringer205c8ad2016-12-08 18:46:19 -0800227 efd = sys_perf_event_open(&attr, -1/*pid*/, 0/*cpu*/, -1/*group_fd*/, 0);
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700228 if (efd < 0) {
229 printf("event %d fd %d err %s\n", id, efd, strerror(errno));
230 return -1;
231 }
232 event_fd[prog_cnt - 1] = efd;
Yonghong Song0ec95522017-11-30 13:47:55 -0800233 err = ioctl(efd, PERF_EVENT_IOC_ENABLE, 0);
234 if (err < 0) {
235 printf("ioctl PERF_EVENT_IOC_ENABLE failed err %s\n",
236 strerror(errno));
237 return -1;
238 }
239 err = ioctl(efd, PERF_EVENT_IOC_SET_BPF, fd);
240 if (err < 0) {
241 printf("ioctl PERF_EVENT_IOC_SET_BPF failed err %s\n",
242 strerror(errno));
243 return -1;
244 }
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700245
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800246 return 0;
247}
248
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200249static int load_maps(struct bpf_map_data *maps, int nr_maps,
250 fixup_map_cb fixup_map)
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800251{
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -0700252 int i, numa_node;
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200253
Jesper Dangaard Brouer5010e942017-04-28 16:25:04 +0200254 for (i = 0; i < nr_maps; i++) {
Jesper Dangaard Brouer6979bcc2017-05-02 14:32:01 +0200255 if (fixup_map) {
256 fixup_map(&maps[i], i);
257 /* Allow userspace to assign map FD prior to creation */
258 if (maps[i].fd != -1) {
259 map_fd[i] = maps[i].fd;
260 continue;
261 }
262 }
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800263
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -0700264 numa_node = maps[i].def.map_flags & BPF_F_NUMA_NODE ?
265 maps[i].def.numa_node : -1;
266
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200267 if (maps[i].def.type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
268 maps[i].def.type == BPF_MAP_TYPE_HASH_OF_MAPS) {
269 int inner_map_fd = map_fd[maps[i].def.inner_map_idx];
Martin KaFai Laufb30d4b2017-03-22 10:00:35 -0700270
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -0700271 map_fd[i] = bpf_create_map_in_map_node(maps[i].def.type,
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700272 maps[i].name,
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200273 maps[i].def.key_size,
274 inner_map_fd,
275 maps[i].def.max_entries,
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -0700276 maps[i].def.map_flags,
277 numa_node);
Martin KaFai Laufb30d4b2017-03-22 10:00:35 -0700278 } else {
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -0700279 map_fd[i] = bpf_create_map_node(maps[i].def.type,
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700280 maps[i].name,
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -0700281 maps[i].def.key_size,
282 maps[i].def.value_size,
283 maps[i].def.max_entries,
284 maps[i].def.map_flags,
285 numa_node);
Martin KaFai Laufb30d4b2017-03-22 10:00:35 -0700286 }
Alexei Starovoitov618ec9a72016-03-07 21:57:18 -0800287 if (map_fd[i] < 0) {
288 printf("failed to create a map: %d %s\n",
289 errno, strerror(errno));
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800290 return 1;
Alexei Starovoitov618ec9a72016-03-07 21:57:18 -0800291 }
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200292 maps[i].fd = map_fd[i];
Alexei Starovoitov5bacd782015-05-19 16:59:05 -0700293
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200294 if (maps[i].def.type == BPF_MAP_TYPE_PROG_ARRAY)
Alexei Starovoitov5bacd782015-05-19 16:59:05 -0700295 prog_array_fd = map_fd[i];
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800296 }
297 return 0;
298}
299
300static int get_sec(Elf *elf, int i, GElf_Ehdr *ehdr, char **shname,
301 GElf_Shdr *shdr, Elf_Data **data)
302{
303 Elf_Scn *scn;
304
305 scn = elf_getscn(elf, i);
306 if (!scn)
307 return 1;
308
309 if (gelf_getshdr(scn, shdr) != shdr)
310 return 2;
311
312 *shname = elf_strptr(elf, ehdr->e_shstrndx, shdr->sh_name);
313 if (!*shname || !shdr->sh_size)
314 return 3;
315
316 *data = elf_getdata(scn, 0);
317 if (!*data || elf_getdata(scn, *data) != NULL)
318 return 4;
319
320 return 0;
321}
322
323static int parse_relo_and_apply(Elf_Data *data, Elf_Data *symbols,
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200324 GElf_Shdr *shdr, struct bpf_insn *insn,
325 struct bpf_map_data *maps, int nr_maps)
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800326{
327 int i, nrels;
328
329 nrels = shdr->sh_size / shdr->sh_entsize;
330
331 for (i = 0; i < nrels; i++) {
332 GElf_Sym sym;
333 GElf_Rel rel;
334 unsigned int insn_idx;
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200335 bool match = false;
336 int j, map_idx;
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800337
338 gelf_getrel(data, i, &rel);
339
340 insn_idx = rel.r_offset / sizeof(struct bpf_insn);
341
342 gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &sym);
343
344 if (insn[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
345 printf("invalid relo for insn[%d].code 0x%x\n",
346 insn_idx, insn[insn_idx].code);
347 return 1;
348 }
349 insn[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200350
351 /* Match FD relocation against recorded map_data[] offset */
352 for (map_idx = 0; map_idx < nr_maps; map_idx++) {
353 if (maps[map_idx].elf_offset == sym.st_value) {
354 match = true;
355 break;
356 }
357 }
358 if (match) {
359 insn[insn_idx].imm = maps[map_idx].fd;
360 } else {
361 printf("invalid relo for insn[%d] no map_data match\n",
362 insn_idx);
363 return 1;
364 }
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800365 }
366
367 return 0;
368}
369
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700370static int cmp_symbols(const void *l, const void *r)
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800371{
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700372 const GElf_Sym *lsym = (const GElf_Sym *)l;
373 const GElf_Sym *rsym = (const GElf_Sym *)r;
374
375 if (lsym->st_value < rsym->st_value)
376 return -1;
377 else if (lsym->st_value > rsym->st_value)
378 return 1;
379 else
380 return 0;
381}
382
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200383static int load_elf_maps_section(struct bpf_map_data *maps, int maps_shndx,
384 Elf *elf, Elf_Data *symbols, int strtabidx)
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700385{
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200386 int map_sz_elf, map_sz_copy;
387 bool validate_zero = false;
388 Elf_Data *data_maps;
389 int i, nr_maps;
390 GElf_Sym *sym;
391 Elf_Scn *scn;
392 int copy_sz;
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700393
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200394 if (maps_shndx < 0)
395 return -EINVAL;
396 if (!symbols)
397 return -EINVAL;
398
399 /* Get data for maps section via elf index */
400 scn = elf_getscn(elf, maps_shndx);
401 if (scn)
402 data_maps = elf_getdata(scn, NULL);
403 if (!scn || !data_maps) {
404 printf("Failed to get Elf_Data from maps section %d\n",
405 maps_shndx);
406 return -EINVAL;
407 }
408
409 /* For each map get corrosponding symbol table entry */
410 sym = calloc(MAX_MAPS+1, sizeof(GElf_Sym));
411 for (i = 0, nr_maps = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
412 assert(nr_maps < MAX_MAPS+1);
413 if (!gelf_getsym(symbols, i, &sym[nr_maps]))
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700414 continue;
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200415 if (sym[nr_maps].st_shndx != maps_shndx)
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700416 continue;
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200417 /* Only increment iif maps section */
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700418 nr_maps++;
419 }
420
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200421 /* Align to map_fd[] order, via sort on offset in sym.st_value */
422 qsort(sym, nr_maps, sizeof(GElf_Sym), cmp_symbols);
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700423
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200424 /* Keeping compatible with ELF maps section changes
425 * ------------------------------------------------
Jakub Kicinski74662ea2018-05-10 10:24:38 -0700426 * The program size of struct bpf_load_map_def is known by loader
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200427 * code, but struct stored in ELF file can be different.
428 *
429 * Unfortunately sym[i].st_size is zero. To calculate the
430 * struct size stored in the ELF file, assume all struct have
431 * the same size, and simply divide with number of map
432 * symbols.
433 */
434 map_sz_elf = data_maps->d_size / nr_maps;
Jakub Kicinski74662ea2018-05-10 10:24:38 -0700435 map_sz_copy = sizeof(struct bpf_load_map_def);
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200436 if (map_sz_elf < map_sz_copy) {
437 /*
438 * Backward compat, loading older ELF file with
439 * smaller struct, keeping remaining bytes zero.
440 */
441 map_sz_copy = map_sz_elf;
442 } else if (map_sz_elf > map_sz_copy) {
443 /*
444 * Forward compat, loading newer ELF file with larger
445 * struct with unknown features. Assume zero means
446 * feature not used. Thus, validate rest of struct
447 * data is zero.
448 */
449 validate_zero = true;
450 }
451
452 /* Memcpy relevant part of ELF maps data to loader maps */
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700453 for (i = 0; i < nr_maps; i++) {
Jakub Kicinski74662ea2018-05-10 10:24:38 -0700454 struct bpf_load_map_def *def;
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200455 unsigned char *addr, *end;
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200456 const char *map_name;
457 size_t offset;
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700458
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200459 map_name = elf_strptr(elf, strtabidx, sym[i].st_name);
460 maps[i].name = strdup(map_name);
461 if (!maps[i].name) {
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700462 printf("strdup(%s): %s(%d)\n", map_name,
463 strerror(errno), errno);
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200464 free(sym);
465 return -errno;
466 }
467
468 /* Symbol value is offset into ELF maps section data area */
469 offset = sym[i].st_value;
Jakub Kicinski74662ea2018-05-10 10:24:38 -0700470 def = (struct bpf_load_map_def *)(data_maps->d_buf + offset);
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200471 maps[i].elf_offset = offset;
Jakub Kicinski74662ea2018-05-10 10:24:38 -0700472 memset(&maps[i].def, 0, sizeof(struct bpf_load_map_def));
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200473 memcpy(&maps[i].def, def, map_sz_copy);
474
475 /* Verify no newer features were requested */
476 if (validate_zero) {
477 addr = (unsigned char*) def + map_sz_copy;
478 end = (unsigned char*) def + map_sz_elf;
479 for (; addr < end; addr++) {
480 if (*addr != 0) {
481 free(sym);
482 return -EFBIG;
483 }
484 }
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700485 }
486 }
487
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200488 free(sym);
Jesper Dangaard Brouer5010e942017-04-28 16:25:04 +0200489 return nr_maps;
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700490}
491
492static int do_load_bpf_file(const char *path, fixup_map_cb fixup_map)
493{
494 int fd, i, ret, maps_shndx = -1, strtabidx = -1;
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800495 Elf *elf;
496 GElf_Ehdr ehdr;
497 GElf_Shdr shdr, shdr_prog;
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700498 Elf_Data *data, *data_prog, *data_maps = NULL, *symbols = NULL;
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200499 char *shname, *shname_prog;
500 int nr_maps = 0;
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800501
Mickaël Salaüna734fb52017-02-08 21:27:43 +0100502 /* reset global variables */
503 kern_version = 0;
504 memset(license, 0, sizeof(license));
505 memset(processed_sec, 0, sizeof(processed_sec));
506
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800507 if (elf_version(EV_CURRENT) == EV_NONE)
508 return 1;
509
510 fd = open(path, O_RDONLY, 0);
511 if (fd < 0)
512 return 1;
513
514 elf = elf_begin(fd, ELF_C_READ, NULL);
515
516 if (!elf)
517 return 1;
518
519 if (gelf_getehdr(elf, &ehdr) != &ehdr)
520 return 1;
521
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700522 /* clear all kprobes */
523 i = system("echo \"\" > /sys/kernel/debug/tracing/kprobe_events");
524
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800525 /* scan over all elf sections to get license and map info */
526 for (i = 1; i < ehdr.e_shnum; i++) {
527
528 if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
529 continue;
530
531 if (0) /* helpful for llvm debugging */
532 printf("section %d:%s data %p size %zd link %d flags %d\n",
533 i, shname, data->d_buf, data->d_size,
534 shdr.sh_link, (int) shdr.sh_flags);
535
536 if (strcmp(shname, "license") == 0) {
537 processed_sec[i] = true;
538 memcpy(license, data->d_buf, data->d_size);
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700539 } else if (strcmp(shname, "version") == 0) {
540 processed_sec[i] = true;
541 if (data->d_size != sizeof(int)) {
542 printf("invalid size of version section %zd\n",
543 data->d_size);
544 return 1;
545 }
546 memcpy(&kern_version, data->d_buf, sizeof(int));
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800547 } else if (strcmp(shname, "maps") == 0) {
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200548 int j;
549
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700550 maps_shndx = i;
551 data_maps = data;
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200552 for (j = 0; j < MAX_MAPS; j++)
553 map_data[j].fd = -1;
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800554 } else if (shdr.sh_type == SHT_SYMTAB) {
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700555 strtabidx = shdr.sh_link;
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800556 symbols = data;
557 }
558 }
559
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700560 ret = 1;
561
562 if (!symbols) {
563 printf("missing SHT_SYMTAB section\n");
564 goto done;
565 }
566
567 if (data_maps) {
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200568 nr_maps = load_elf_maps_section(map_data, maps_shndx,
569 elf, symbols, strtabidx);
570 if (nr_maps < 0) {
571 printf("Error: Failed loading ELF maps (errno:%d):%s\n",
572 nr_maps, strerror(-nr_maps));
Jesper Dangaard Brouer5010e942017-04-28 16:25:04 +0200573 goto done;
574 }
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200575 if (load_maps(map_data, nr_maps, fixup_map))
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700576 goto done;
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200577 map_data_count = nr_maps;
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700578
579 processed_sec[maps_shndx] = true;
580 }
581
Jesper Dangaard Brouer7bc57952017-05-30 14:37:51 +0200582 /* process all relo sections, and rewrite bpf insns for maps */
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800583 for (i = 1; i < ehdr.e_shnum; i++) {
Mickaël Salaün16ad1322017-02-08 21:27:42 +0100584 if (processed_sec[i])
585 continue;
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800586
587 if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
588 continue;
Jesper Dangaard Brouer7bc57952017-05-30 14:37:51 +0200589
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800590 if (shdr.sh_type == SHT_REL) {
591 struct bpf_insn *insns;
592
Jesper Dangaard Brouer7bc57952017-05-30 14:37:51 +0200593 /* locate prog sec that need map fixup (relocations) */
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800594 if (get_sec(elf, shdr.sh_info, &ehdr, &shname_prog,
595 &shdr_prog, &data_prog))
596 continue;
597
Alexei Starovoitovdb6a71d2016-11-22 16:52:09 -0800598 if (shdr_prog.sh_type != SHT_PROGBITS ||
599 !(shdr_prog.sh_flags & SHF_EXECINSTR))
600 continue;
601
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800602 insns = (struct bpf_insn *) data_prog->d_buf;
Jesper Dangaard Brouer7bc57952017-05-30 14:37:51 +0200603 processed_sec[i] = true; /* relo section */
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800604
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200605 if (parse_relo_and_apply(data, symbols, &shdr, insns,
606 map_data, nr_maps))
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800607 continue;
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800608 }
609 }
610
Jesper Dangaard Brouer7bc57952017-05-30 14:37:51 +0200611 /* load programs */
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800612 for (i = 1; i < ehdr.e_shnum; i++) {
613
614 if (processed_sec[i])
615 continue;
616
617 if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
618 continue;
619
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700620 if (memcmp(shname, "kprobe/", 7) == 0 ||
621 memcmp(shname, "kretprobe/", 10) == 0 ||
Alexei Starovoitovc0766042016-04-06 18:43:29 -0700622 memcmp(shname, "tracepoint/", 11) == 0 ||
Alexei Starovoitov4662a4e2018-03-28 12:05:39 -0700623 memcmp(shname, "raw_tracepoint/", 15) == 0 ||
Brenden Blanco86af8b42016-07-19 12:16:51 -0700624 memcmp(shname, "xdp", 3) == 0 ||
Alexei Starovoitov1c47910e2016-09-01 18:37:25 -0700625 memcmp(shname, "perf_event", 10) == 0 ||
David Ahern4f2e7ae2016-12-01 08:48:07 -0800626 memcmp(shname, "socket", 6) == 0 ||
Lawrence Brakmo40304b22017-06-30 20:02:40 -0700627 memcmp(shname, "cgroup/", 7) == 0 ||
John Fastabend69e8cc12017-08-15 22:33:32 -0700628 memcmp(shname, "sockops", 7) == 0 ||
John Fastabend4c4c3c22018-03-18 12:57:41 -0700629 memcmp(shname, "sk_skb", 6) == 0 ||
630 memcmp(shname, "sk_msg", 6) == 0) {
Lawrence Brakmof856e462017-07-04 15:57:50 -0700631 ret = load_and_attach(shname, data->d_buf,
632 data->d_size);
633 if (ret != 0)
634 goto done;
635 }
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800636 }
637
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700638done:
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800639 close(fd);
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700640 return ret;
641}
642
643int load_bpf_file(char *path)
644{
645 return do_load_bpf_file(path, NULL);
646}
647
648int load_bpf_file_fixup_map(const char *path, fixup_map_cb fixup_map)
649{
650 return do_load_bpf_file(path, fixup_map);
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800651}
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700652
653void read_trace_pipe(void)
654{
655 int trace_fd;
656
657 trace_fd = open(DEBUGFS "trace_pipe", O_RDONLY, 0);
658 if (trace_fd < 0)
659 return;
660
661 while (1) {
662 static char buf[4096];
663 ssize_t sz;
664
665 sz = read(trace_fd, buf, sizeof(buf));
666 if (sz > 0) {
667 buf[sz] = 0;
668 puts(buf);
669 }
670 }
671}