blob: bebe4188b4b36cbf42fe39f1f5c61c43ea1bf8a9 [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>
Alexei Starovoitov249b8122014-12-01 15:06:37 -080027#include "libbpf.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
Joe Stringer43371c82016-12-14 14:43:39 -0800110 fd = bpf_load_program(prog_type, prog, insns_cnt, license, kern_version,
Joe Stringerd40fc182016-12-14 14:43:38 -0800111 bpf_log_buf, BPF_LOG_BUF_SIZE);
Alexei Starovoitov5bacd782015-05-19 16:59:05 -0700112 if (fd < 0) {
Joe Stringerd40fc182016-12-14 14:43:38 -0800113 printf("bpf_load_program() err=%d\n%s", errno, bpf_log_buf);
Alexei Starovoitov5bacd782015-05-19 16:59:05 -0700114 return -1;
115 }
116
117 prog_fd[prog_cnt++] = fd;
118
David Ahern4f2e7ae2016-12-01 08:48:07 -0800119 if (is_xdp || is_perf_event || is_cgroup_skb || is_cgroup_sk)
Brenden Blanco86af8b42016-07-19 12:16:51 -0700120 return 0;
121
John Fastabend4c4c3c22018-03-18 12:57:41 -0700122 if (is_socket || is_sockops || is_sk_skb || is_sk_msg) {
Lawrence Brakmo40304b22017-06-30 20:02:40 -0700123 if (is_socket)
124 event += 6;
125 else
126 event += 7;
Alexei Starovoitov5bacd782015-05-19 16:59:05 -0700127 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 Starovoitov4662a4e2018-03-28 12:05:39 -0700137 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 Starovoitovb896c4f2015-03-25 12:49:23 -0700147 if (is_kprobe || is_kretprobe) {
148 if (is_kprobe)
149 event += 7;
150 else
151 event += 10;
152
Alexei Starovoitov5bacd782015-05-19 16:59:05 -0700153 if (*event == 0) {
154 printf("event name cannot be empty\n");
155 return -1;
156 }
157
158 if (isdigit(*event))
159 return populate_prog_array(event, fd);
160
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700161 snprintf(buf, sizeof(buf),
162 "echo '%c:%s %s' >> /sys/kernel/debug/tracing/kprobe_events",
163 is_kprobe ? 'p' : 'r', event, event);
164 err = system(buf);
165 if (err < 0) {
166 printf("failed to create kprobe '%s' error '%s'\n",
167 event, strerror(errno));
168 return -1;
169 }
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700170
Alexei Starovoitovc0766042016-04-06 18:43:29 -0700171 strcpy(buf, DEBUGFS);
172 strcat(buf, "events/kprobes/");
173 strcat(buf, event);
174 strcat(buf, "/id");
175 } else if (is_tracepoint) {
176 event += 11;
177
178 if (*event == 0) {
179 printf("event name cannot be empty\n");
180 return -1;
181 }
182 strcpy(buf, DEBUGFS);
183 strcat(buf, "events/");
184 strcat(buf, event);
185 strcat(buf, "/id");
186 }
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700187
188 efd = open(buf, O_RDONLY, 0);
189 if (efd < 0) {
190 printf("failed to open event %s\n", event);
191 return -1;
192 }
193
194 err = read(efd, buf, sizeof(buf));
195 if (err < 0 || err >= sizeof(buf)) {
196 printf("read from '%s' failed '%s'\n", event, strerror(errno));
197 return -1;
198 }
199
200 close(efd);
201
202 buf[err] = 0;
203 id = atoi(buf);
204 attr.config = id;
205
Joe Stringer205c8ad2016-12-08 18:46:19 -0800206 efd = sys_perf_event_open(&attr, -1/*pid*/, 0/*cpu*/, -1/*group_fd*/, 0);
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700207 if (efd < 0) {
208 printf("event %d fd %d err %s\n", id, efd, strerror(errno));
209 return -1;
210 }
211 event_fd[prog_cnt - 1] = efd;
Yonghong Song0ec95522017-11-30 13:47:55 -0800212 err = ioctl(efd, PERF_EVENT_IOC_ENABLE, 0);
213 if (err < 0) {
214 printf("ioctl PERF_EVENT_IOC_ENABLE failed err %s\n",
215 strerror(errno));
216 return -1;
217 }
218 err = ioctl(efd, PERF_EVENT_IOC_SET_BPF, fd);
219 if (err < 0) {
220 printf("ioctl PERF_EVENT_IOC_SET_BPF failed err %s\n",
221 strerror(errno));
222 return -1;
223 }
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700224
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800225 return 0;
226}
227
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200228static int load_maps(struct bpf_map_data *maps, int nr_maps,
229 fixup_map_cb fixup_map)
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800230{
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -0700231 int i, numa_node;
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200232
Jesper Dangaard Brouer5010e942017-04-28 16:25:04 +0200233 for (i = 0; i < nr_maps; i++) {
Jesper Dangaard Brouer6979bcc2017-05-02 14:32:01 +0200234 if (fixup_map) {
235 fixup_map(&maps[i], i);
236 /* Allow userspace to assign map FD prior to creation */
237 if (maps[i].fd != -1) {
238 map_fd[i] = maps[i].fd;
239 continue;
240 }
241 }
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800242
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -0700243 numa_node = maps[i].def.map_flags & BPF_F_NUMA_NODE ?
244 maps[i].def.numa_node : -1;
245
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200246 if (maps[i].def.type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
247 maps[i].def.type == BPF_MAP_TYPE_HASH_OF_MAPS) {
248 int inner_map_fd = map_fd[maps[i].def.inner_map_idx];
Martin KaFai Laufb30d4b2017-03-22 10:00:35 -0700249
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -0700250 map_fd[i] = bpf_create_map_in_map_node(maps[i].def.type,
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700251 maps[i].name,
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200252 maps[i].def.key_size,
253 inner_map_fd,
254 maps[i].def.max_entries,
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -0700255 maps[i].def.map_flags,
256 numa_node);
Martin KaFai Laufb30d4b2017-03-22 10:00:35 -0700257 } else {
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -0700258 map_fd[i] = bpf_create_map_node(maps[i].def.type,
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700259 maps[i].name,
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -0700260 maps[i].def.key_size,
261 maps[i].def.value_size,
262 maps[i].def.max_entries,
263 maps[i].def.map_flags,
264 numa_node);
Martin KaFai Laufb30d4b2017-03-22 10:00:35 -0700265 }
Alexei Starovoitov618ec9a72016-03-07 21:57:18 -0800266 if (map_fd[i] < 0) {
267 printf("failed to create a map: %d %s\n",
268 errno, strerror(errno));
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800269 return 1;
Alexei Starovoitov618ec9a72016-03-07 21:57:18 -0800270 }
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200271 maps[i].fd = map_fd[i];
Alexei Starovoitov5bacd782015-05-19 16:59:05 -0700272
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200273 if (maps[i].def.type == BPF_MAP_TYPE_PROG_ARRAY)
Alexei Starovoitov5bacd782015-05-19 16:59:05 -0700274 prog_array_fd = map_fd[i];
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800275 }
276 return 0;
277}
278
279static int get_sec(Elf *elf, int i, GElf_Ehdr *ehdr, char **shname,
280 GElf_Shdr *shdr, Elf_Data **data)
281{
282 Elf_Scn *scn;
283
284 scn = elf_getscn(elf, i);
285 if (!scn)
286 return 1;
287
288 if (gelf_getshdr(scn, shdr) != shdr)
289 return 2;
290
291 *shname = elf_strptr(elf, ehdr->e_shstrndx, shdr->sh_name);
292 if (!*shname || !shdr->sh_size)
293 return 3;
294
295 *data = elf_getdata(scn, 0);
296 if (!*data || elf_getdata(scn, *data) != NULL)
297 return 4;
298
299 return 0;
300}
301
302static int parse_relo_and_apply(Elf_Data *data, Elf_Data *symbols,
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200303 GElf_Shdr *shdr, struct bpf_insn *insn,
304 struct bpf_map_data *maps, int nr_maps)
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800305{
306 int i, nrels;
307
308 nrels = shdr->sh_size / shdr->sh_entsize;
309
310 for (i = 0; i < nrels; i++) {
311 GElf_Sym sym;
312 GElf_Rel rel;
313 unsigned int insn_idx;
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200314 bool match = false;
315 int j, map_idx;
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800316
317 gelf_getrel(data, i, &rel);
318
319 insn_idx = rel.r_offset / sizeof(struct bpf_insn);
320
321 gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &sym);
322
323 if (insn[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
324 printf("invalid relo for insn[%d].code 0x%x\n",
325 insn_idx, insn[insn_idx].code);
326 return 1;
327 }
328 insn[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200329
330 /* Match FD relocation against recorded map_data[] offset */
331 for (map_idx = 0; map_idx < nr_maps; map_idx++) {
332 if (maps[map_idx].elf_offset == sym.st_value) {
333 match = true;
334 break;
335 }
336 }
337 if (match) {
338 insn[insn_idx].imm = maps[map_idx].fd;
339 } else {
340 printf("invalid relo for insn[%d] no map_data match\n",
341 insn_idx);
342 return 1;
343 }
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800344 }
345
346 return 0;
347}
348
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700349static int cmp_symbols(const void *l, const void *r)
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800350{
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700351 const GElf_Sym *lsym = (const GElf_Sym *)l;
352 const GElf_Sym *rsym = (const GElf_Sym *)r;
353
354 if (lsym->st_value < rsym->st_value)
355 return -1;
356 else if (lsym->st_value > rsym->st_value)
357 return 1;
358 else
359 return 0;
360}
361
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200362static int load_elf_maps_section(struct bpf_map_data *maps, int maps_shndx,
363 Elf *elf, Elf_Data *symbols, int strtabidx)
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700364{
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200365 int map_sz_elf, map_sz_copy;
366 bool validate_zero = false;
367 Elf_Data *data_maps;
368 int i, nr_maps;
369 GElf_Sym *sym;
370 Elf_Scn *scn;
371 int copy_sz;
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700372
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200373 if (maps_shndx < 0)
374 return -EINVAL;
375 if (!symbols)
376 return -EINVAL;
377
378 /* Get data for maps section via elf index */
379 scn = elf_getscn(elf, maps_shndx);
380 if (scn)
381 data_maps = elf_getdata(scn, NULL);
382 if (!scn || !data_maps) {
383 printf("Failed to get Elf_Data from maps section %d\n",
384 maps_shndx);
385 return -EINVAL;
386 }
387
388 /* For each map get corrosponding symbol table entry */
389 sym = calloc(MAX_MAPS+1, sizeof(GElf_Sym));
390 for (i = 0, nr_maps = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
391 assert(nr_maps < MAX_MAPS+1);
392 if (!gelf_getsym(symbols, i, &sym[nr_maps]))
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700393 continue;
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200394 if (sym[nr_maps].st_shndx != maps_shndx)
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700395 continue;
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200396 /* Only increment iif maps section */
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700397 nr_maps++;
398 }
399
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200400 /* Align to map_fd[] order, via sort on offset in sym.st_value */
401 qsort(sym, nr_maps, sizeof(GElf_Sym), cmp_symbols);
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700402
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200403 /* Keeping compatible with ELF maps section changes
404 * ------------------------------------------------
405 * The program size of struct bpf_map_def is known by loader
406 * code, but struct stored in ELF file can be different.
407 *
408 * Unfortunately sym[i].st_size is zero. To calculate the
409 * struct size stored in the ELF file, assume all struct have
410 * the same size, and simply divide with number of map
411 * symbols.
412 */
413 map_sz_elf = data_maps->d_size / nr_maps;
414 map_sz_copy = sizeof(struct bpf_map_def);
415 if (map_sz_elf < map_sz_copy) {
416 /*
417 * Backward compat, loading older ELF file with
418 * smaller struct, keeping remaining bytes zero.
419 */
420 map_sz_copy = map_sz_elf;
421 } else if (map_sz_elf > map_sz_copy) {
422 /*
423 * Forward compat, loading newer ELF file with larger
424 * struct with unknown features. Assume zero means
425 * feature not used. Thus, validate rest of struct
426 * data is zero.
427 */
428 validate_zero = true;
429 }
430
431 /* Memcpy relevant part of ELF maps data to loader maps */
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700432 for (i = 0; i < nr_maps; i++) {
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200433 unsigned char *addr, *end;
434 struct bpf_map_def *def;
435 const char *map_name;
436 size_t offset;
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700437
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200438 map_name = elf_strptr(elf, strtabidx, sym[i].st_name);
439 maps[i].name = strdup(map_name);
440 if (!maps[i].name) {
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700441 printf("strdup(%s): %s(%d)\n", map_name,
442 strerror(errno), errno);
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200443 free(sym);
444 return -errno;
445 }
446
447 /* Symbol value is offset into ELF maps section data area */
448 offset = sym[i].st_value;
449 def = (struct bpf_map_def *)(data_maps->d_buf + offset);
450 maps[i].elf_offset = offset;
451 memset(&maps[i].def, 0, sizeof(struct bpf_map_def));
452 memcpy(&maps[i].def, def, map_sz_copy);
453
454 /* Verify no newer features were requested */
455 if (validate_zero) {
456 addr = (unsigned char*) def + map_sz_copy;
457 end = (unsigned char*) def + map_sz_elf;
458 for (; addr < end; addr++) {
459 if (*addr != 0) {
460 free(sym);
461 return -EFBIG;
462 }
463 }
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700464 }
465 }
466
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200467 free(sym);
Jesper Dangaard Brouer5010e942017-04-28 16:25:04 +0200468 return nr_maps;
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700469}
470
471static int do_load_bpf_file(const char *path, fixup_map_cb fixup_map)
472{
473 int fd, i, ret, maps_shndx = -1, strtabidx = -1;
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800474 Elf *elf;
475 GElf_Ehdr ehdr;
476 GElf_Shdr shdr, shdr_prog;
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700477 Elf_Data *data, *data_prog, *data_maps = NULL, *symbols = NULL;
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200478 char *shname, *shname_prog;
479 int nr_maps = 0;
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800480
Mickaël Salaüna734fb52017-02-08 21:27:43 +0100481 /* reset global variables */
482 kern_version = 0;
483 memset(license, 0, sizeof(license));
484 memset(processed_sec, 0, sizeof(processed_sec));
485
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800486 if (elf_version(EV_CURRENT) == EV_NONE)
487 return 1;
488
489 fd = open(path, O_RDONLY, 0);
490 if (fd < 0)
491 return 1;
492
493 elf = elf_begin(fd, ELF_C_READ, NULL);
494
495 if (!elf)
496 return 1;
497
498 if (gelf_getehdr(elf, &ehdr) != &ehdr)
499 return 1;
500
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700501 /* clear all kprobes */
502 i = system("echo \"\" > /sys/kernel/debug/tracing/kprobe_events");
503
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800504 /* scan over all elf sections to get license and map info */
505 for (i = 1; i < ehdr.e_shnum; i++) {
506
507 if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
508 continue;
509
510 if (0) /* helpful for llvm debugging */
511 printf("section %d:%s data %p size %zd link %d flags %d\n",
512 i, shname, data->d_buf, data->d_size,
513 shdr.sh_link, (int) shdr.sh_flags);
514
515 if (strcmp(shname, "license") == 0) {
516 processed_sec[i] = true;
517 memcpy(license, data->d_buf, data->d_size);
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700518 } else if (strcmp(shname, "version") == 0) {
519 processed_sec[i] = true;
520 if (data->d_size != sizeof(int)) {
521 printf("invalid size of version section %zd\n",
522 data->d_size);
523 return 1;
524 }
525 memcpy(&kern_version, data->d_buf, sizeof(int));
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800526 } else if (strcmp(shname, "maps") == 0) {
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200527 int j;
528
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700529 maps_shndx = i;
530 data_maps = data;
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200531 for (j = 0; j < MAX_MAPS; j++)
532 map_data[j].fd = -1;
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800533 } else if (shdr.sh_type == SHT_SYMTAB) {
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700534 strtabidx = shdr.sh_link;
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800535 symbols = data;
536 }
537 }
538
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700539 ret = 1;
540
541 if (!symbols) {
542 printf("missing SHT_SYMTAB section\n");
543 goto done;
544 }
545
546 if (data_maps) {
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200547 nr_maps = load_elf_maps_section(map_data, maps_shndx,
548 elf, symbols, strtabidx);
549 if (nr_maps < 0) {
550 printf("Error: Failed loading ELF maps (errno:%d):%s\n",
551 nr_maps, strerror(-nr_maps));
Jesper Dangaard Brouer5010e942017-04-28 16:25:04 +0200552 ret = 1;
553 goto done;
554 }
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200555 if (load_maps(map_data, nr_maps, fixup_map))
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700556 goto done;
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200557 map_data_count = nr_maps;
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700558
559 processed_sec[maps_shndx] = true;
560 }
561
Jesper Dangaard Brouer7bc57952017-05-30 14:37:51 +0200562 /* process all relo sections, and rewrite bpf insns for maps */
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800563 for (i = 1; i < ehdr.e_shnum; i++) {
Mickaël Salaün16ad1322017-02-08 21:27:42 +0100564 if (processed_sec[i])
565 continue;
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800566
567 if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
568 continue;
Jesper Dangaard Brouer7bc57952017-05-30 14:37:51 +0200569
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800570 if (shdr.sh_type == SHT_REL) {
571 struct bpf_insn *insns;
572
Jesper Dangaard Brouer7bc57952017-05-30 14:37:51 +0200573 /* locate prog sec that need map fixup (relocations) */
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800574 if (get_sec(elf, shdr.sh_info, &ehdr, &shname_prog,
575 &shdr_prog, &data_prog))
576 continue;
577
Alexei Starovoitovdb6a71d2016-11-22 16:52:09 -0800578 if (shdr_prog.sh_type != SHT_PROGBITS ||
579 !(shdr_prog.sh_flags & SHF_EXECINSTR))
580 continue;
581
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800582 insns = (struct bpf_insn *) data_prog->d_buf;
Jesper Dangaard Brouer7bc57952017-05-30 14:37:51 +0200583 processed_sec[i] = true; /* relo section */
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800584
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200585 if (parse_relo_and_apply(data, symbols, &shdr, insns,
586 map_data, nr_maps))
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800587 continue;
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800588 }
589 }
590
Jesper Dangaard Brouer7bc57952017-05-30 14:37:51 +0200591 /* load programs */
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800592 for (i = 1; i < ehdr.e_shnum; i++) {
593
594 if (processed_sec[i])
595 continue;
596
597 if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
598 continue;
599
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700600 if (memcmp(shname, "kprobe/", 7) == 0 ||
601 memcmp(shname, "kretprobe/", 10) == 0 ||
Alexei Starovoitovc0766042016-04-06 18:43:29 -0700602 memcmp(shname, "tracepoint/", 11) == 0 ||
Alexei Starovoitov4662a4e2018-03-28 12:05:39 -0700603 memcmp(shname, "raw_tracepoint/", 15) == 0 ||
Brenden Blanco86af8b42016-07-19 12:16:51 -0700604 memcmp(shname, "xdp", 3) == 0 ||
Alexei Starovoitov1c47910e2016-09-01 18:37:25 -0700605 memcmp(shname, "perf_event", 10) == 0 ||
David Ahern4f2e7ae2016-12-01 08:48:07 -0800606 memcmp(shname, "socket", 6) == 0 ||
Lawrence Brakmo40304b22017-06-30 20:02:40 -0700607 memcmp(shname, "cgroup/", 7) == 0 ||
John Fastabend69e8cc12017-08-15 22:33:32 -0700608 memcmp(shname, "sockops", 7) == 0 ||
John Fastabend4c4c3c22018-03-18 12:57:41 -0700609 memcmp(shname, "sk_skb", 6) == 0 ||
610 memcmp(shname, "sk_msg", 6) == 0) {
Lawrence Brakmof856e462017-07-04 15:57:50 -0700611 ret = load_and_attach(shname, data->d_buf,
612 data->d_size);
613 if (ret != 0)
614 goto done;
615 }
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800616 }
617
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700618 ret = 0;
619done:
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800620 close(fd);
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700621 return ret;
622}
623
624int load_bpf_file(char *path)
625{
626 return do_load_bpf_file(path, NULL);
627}
628
629int load_bpf_file_fixup_map(const char *path, fixup_map_cb fixup_map)
630{
631 return do_load_bpf_file(path, fixup_map);
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800632}
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700633
634void read_trace_pipe(void)
635{
636 int trace_fd;
637
638 trace_fd = open(DEBUGFS "trace_pipe", O_RDONLY, 0);
639 if (trace_fd < 0)
640 return;
641
642 while (1) {
643 static char buf[4096];
644 ssize_t sz;
645
646 sz = read(trace_fd, buf, sizeof(buf));
647 if (sz > 0) {
648 buf[sz] = 0;
649 puts(buf);
650 }
651 }
652}
Alexei Starovoitov3622e7e2016-03-07 21:57:19 -0800653
654#define MAX_SYMS 300000
655static struct ksym syms[MAX_SYMS];
656static int sym_cnt;
657
658static int ksym_cmp(const void *p1, const void *p2)
659{
660 return ((struct ksym *)p1)->addr - ((struct ksym *)p2)->addr;
661}
662
663int load_kallsyms(void)
664{
665 FILE *f = fopen("/proc/kallsyms", "r");
666 char func[256], buf[256];
667 char symbol;
668 void *addr;
669 int i = 0;
670
671 if (!f)
672 return -ENOENT;
673
674 while (!feof(f)) {
675 if (!fgets(buf, sizeof(buf), f))
676 break;
677 if (sscanf(buf, "%p %c %s", &addr, &symbol, func) != 3)
678 break;
679 if (!addr)
680 continue;
681 syms[i].addr = (long) addr;
682 syms[i].name = strdup(func);
683 i++;
684 }
685 sym_cnt = i;
686 qsort(syms, sym_cnt, sizeof(struct ksym), ksym_cmp);
687 return 0;
688}
689
690struct ksym *ksym_search(long key)
691{
692 int start = 0, end = sym_cnt;
693 int result;
694
695 while (start < end) {
696 size_t mid = start + (end - start) / 2;
697
698 result = key - syms[mid].addr;
699 if (result < 0)
700 end = mid;
701 else if (result > 0)
702 start = mid + 1;
703 else
704 return &syms[mid];
705 }
706
707 if (start >= 1 && syms[start - 1].addr < key &&
708 key < syms[start].addr)
709 /* valid ksym */
710 return &syms[start - 1];
711
712 /* out of range. return _stext */
713 return &syms[0];
714}
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -0800715