blob: 2a1dbf52fc9a5326f08224fbce4c93946f05cbf1 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002
3/*
4 * Common eBPF ELF object loading operations.
5 *
6 * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
7 * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
8 * Copyright (C) 2015 Huawei Inc.
9 * Copyright (C) 2017 Nicira, Inc.
David Brazdil0f672f62019-12-10 10:32:29 +000010 * Copyright (C) 2019 Isovalent, Inc.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000011 */
12
David Brazdil0f672f62019-12-10 10:32:29 +000013#ifndef _GNU_SOURCE
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000014#define _GNU_SOURCE
David Brazdil0f672f62019-12-10 10:32:29 +000015#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000016#include <stdlib.h>
17#include <stdio.h>
18#include <stdarg.h>
19#include <libgen.h>
20#include <inttypes.h>
21#include <string.h>
22#include <unistd.h>
David Brazdil0f672f62019-12-10 10:32:29 +000023#include <endian.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000024#include <fcntl.h>
25#include <errno.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000026#include <asm/unistd.h>
27#include <linux/err.h>
28#include <linux/kernel.h>
29#include <linux/bpf.h>
30#include <linux/btf.h>
David Brazdil0f672f62019-12-10 10:32:29 +000031#include <linux/filter.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000032#include <linux/list.h>
33#include <linux/limits.h>
David Brazdil0f672f62019-12-10 10:32:29 +000034#include <linux/perf_event.h>
35#include <linux/ring_buffer.h>
36#include <sys/epoll.h>
37#include <sys/ioctl.h>
38#include <sys/mman.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000039#include <sys/stat.h>
40#include <sys/types.h>
41#include <sys/vfs.h>
David Brazdil0f672f62019-12-10 10:32:29 +000042#include <sys/utsname.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000043#include <tools/libc_compat.h>
44#include <libelf.h>
45#include <gelf.h>
46
47#include "libbpf.h"
48#include "bpf.h"
49#include "btf.h"
50#include "str_error.h"
David Brazdil0f672f62019-12-10 10:32:29 +000051#include "libbpf_internal.h"
52#include "hashmap.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000053
54#ifndef EM_BPF
55#define EM_BPF 247
56#endif
57
58#ifndef BPF_FS_MAGIC
59#define BPF_FS_MAGIC 0xcafe4a11
60#endif
61
David Brazdil0f672f62019-12-10 10:32:29 +000062/* vsprintf() in __base_pr() uses nonliteral format string. It may break
63 * compilation if user enables corresponding warning. Disable it explicitly.
64 */
65#pragma GCC diagnostic ignored "-Wformat-nonliteral"
66
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000067#define __printf(a, b) __attribute__((format(printf, a, b)))
68
David Brazdil0f672f62019-12-10 10:32:29 +000069static int __base_pr(enum libbpf_print_level level, const char *format,
70 va_list args)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000071{
David Brazdil0f672f62019-12-10 10:32:29 +000072 if (level == LIBBPF_DEBUG)
73 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000074
David Brazdil0f672f62019-12-10 10:32:29 +000075 return vfprintf(stderr, format, args);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000076}
77
David Brazdil0f672f62019-12-10 10:32:29 +000078static libbpf_print_fn_t __libbpf_pr = __base_pr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000079
David Brazdil0f672f62019-12-10 10:32:29 +000080libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000081{
David Brazdil0f672f62019-12-10 10:32:29 +000082 libbpf_print_fn_t old_print_fn = __libbpf_pr;
83
84 __libbpf_pr = fn;
85 return old_print_fn;
86}
87
88__printf(2, 3)
89void libbpf_print(enum libbpf_print_level level, const char *format, ...)
90{
91 va_list args;
92
93 if (!__libbpf_pr)
94 return;
95
96 va_start(args, format);
97 __libbpf_pr(level, format, args);
98 va_end(args);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000099}
100
101#define STRERR_BUFSIZE 128
102
103#define CHECK_ERR(action, err, out) do { \
104 err = action; \
105 if (err) \
106 goto out; \
107} while(0)
108
109
110/* Copied from tools/perf/util/util.h */
111#ifndef zfree
112# define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
113#endif
114
115#ifndef zclose
116# define zclose(fd) ({ \
117 int ___err = 0; \
118 if ((fd) >= 0) \
119 ___err = close((fd)); \
120 fd = -1; \
121 ___err; })
122#endif
123
124#ifdef HAVE_LIBELF_MMAP_SUPPORT
125# define LIBBPF_ELF_C_READ_MMAP ELF_C_READ_MMAP
126#else
127# define LIBBPF_ELF_C_READ_MMAP ELF_C_READ
128#endif
129
David Brazdil0f672f62019-12-10 10:32:29 +0000130static inline __u64 ptr_to_u64(const void *ptr)
131{
132 return (__u64) (unsigned long) ptr;
133}
134
135struct bpf_capabilities {
136 /* v4.14: kernel support for program & map names. */
137 __u32 name:1;
138 /* v5.2: kernel support for global data sections. */
139 __u32 global_data:1;
140 /* BTF_KIND_FUNC and BTF_KIND_FUNC_PROTO support */
141 __u32 btf_func:1;
142 /* BTF_KIND_VAR and BTF_KIND_DATASEC support */
143 __u32 btf_datasec:1;
144};
145
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000146/*
147 * bpf_prog should be a better name but it has been used in
148 * linux/filter.h.
149 */
150struct bpf_program {
151 /* Index in elf obj file, for relocation use. */
152 int idx;
153 char *name;
154 int prog_ifindex;
155 char *section_name;
David Brazdil0f672f62019-12-10 10:32:29 +0000156 /* section_name with / replaced by _; makes recursive pinning
157 * in bpf_object__pin_programs easier
158 */
159 char *pin_name;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000160 struct bpf_insn *insns;
161 size_t insns_cnt, main_prog_cnt;
162 enum bpf_prog_type type;
163
164 struct reloc_desc {
165 enum {
166 RELO_LD64,
167 RELO_CALL,
David Brazdil0f672f62019-12-10 10:32:29 +0000168 RELO_DATA,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000169 } type;
170 int insn_idx;
171 union {
172 int map_idx;
173 int text_off;
174 };
175 } *reloc_desc;
176 int nr_reloc;
David Brazdil0f672f62019-12-10 10:32:29 +0000177 int log_level;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000178
179 struct {
180 int nr;
181 int *fds;
182 } instances;
183 bpf_program_prep_t preprocessor;
184
185 struct bpf_object *obj;
186 void *priv;
187 bpf_program_clear_priv_t clear_priv;
188
189 enum bpf_attach_type expected_attach_type;
David Brazdil0f672f62019-12-10 10:32:29 +0000190 void *func_info;
191 __u32 func_info_rec_size;
192 __u32 func_info_cnt;
193
194 struct bpf_capabilities *caps;
195
196 void *line_info;
197 __u32 line_info_rec_size;
198 __u32 line_info_cnt;
199 __u32 prog_flags;
200};
201
202enum libbpf_map_type {
203 LIBBPF_MAP_UNSPEC,
204 LIBBPF_MAP_DATA,
205 LIBBPF_MAP_BSS,
206 LIBBPF_MAP_RODATA,
207};
208
209static const char * const libbpf_type_to_btf_name[] = {
210 [LIBBPF_MAP_DATA] = ".data",
211 [LIBBPF_MAP_BSS] = ".bss",
212 [LIBBPF_MAP_RODATA] = ".rodata",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000213};
214
215struct bpf_map {
216 int fd;
217 char *name;
David Brazdil0f672f62019-12-10 10:32:29 +0000218 int sec_idx;
219 size_t sec_offset;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000220 int map_ifindex;
David Brazdil0f672f62019-12-10 10:32:29 +0000221 int inner_map_fd;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000222 struct bpf_map_def def;
223 __u32 btf_key_type_id;
224 __u32 btf_value_type_id;
225 void *priv;
226 bpf_map_clear_priv_t clear_priv;
David Brazdil0f672f62019-12-10 10:32:29 +0000227 enum libbpf_map_type libbpf_type;
228};
229
230struct bpf_secdata {
231 void *rodata;
232 void *data;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000233};
234
235static LIST_HEAD(bpf_objects_list);
236
237struct bpf_object {
David Brazdil0f672f62019-12-10 10:32:29 +0000238 char name[BPF_OBJ_NAME_LEN];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000239 char license[64];
David Brazdil0f672f62019-12-10 10:32:29 +0000240 __u32 kern_version;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000241
242 struct bpf_program *programs;
243 size_t nr_programs;
244 struct bpf_map *maps;
245 size_t nr_maps;
David Brazdil0f672f62019-12-10 10:32:29 +0000246 size_t maps_cap;
247 struct bpf_secdata sections;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000248
249 bool loaded;
250 bool has_pseudo_calls;
251
252 /*
253 * Information when doing elf related work. Only valid if fd
254 * is valid.
255 */
256 struct {
257 int fd;
258 void *obj_buf;
259 size_t obj_buf_sz;
260 Elf *elf;
261 GElf_Ehdr ehdr;
262 Elf_Data *symbols;
David Brazdil0f672f62019-12-10 10:32:29 +0000263 Elf_Data *data;
264 Elf_Data *rodata;
265 Elf_Data *bss;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000266 size_t strtabidx;
267 struct {
268 GElf_Shdr shdr;
269 Elf_Data *data;
270 } *reloc;
271 int nr_reloc;
272 int maps_shndx;
David Brazdil0f672f62019-12-10 10:32:29 +0000273 int btf_maps_shndx;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000274 int text_shndx;
David Brazdil0f672f62019-12-10 10:32:29 +0000275 int data_shndx;
276 int rodata_shndx;
277 int bss_shndx;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000278 } efile;
279 /*
280 * All loaded bpf_object is linked in a list, which is
281 * hidden to caller. bpf_objects__<func> handlers deal with
282 * all objects.
283 */
284 struct list_head list;
285
286 struct btf *btf;
David Brazdil0f672f62019-12-10 10:32:29 +0000287 struct btf_ext *btf_ext;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000288
289 void *priv;
290 bpf_object_clear_priv_t clear_priv;
291
David Brazdil0f672f62019-12-10 10:32:29 +0000292 struct bpf_capabilities caps;
293
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000294 char path[];
295};
296#define obj_elf_valid(o) ((o)->efile.elf)
297
David Brazdil0f672f62019-12-10 10:32:29 +0000298void bpf_program__unload(struct bpf_program *prog)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000299{
300 int i;
301
302 if (!prog)
303 return;
304
305 /*
306 * If the object is opened but the program was never loaded,
307 * it is possible that prog->instances.nr == -1.
308 */
309 if (prog->instances.nr > 0) {
310 for (i = 0; i < prog->instances.nr; i++)
311 zclose(prog->instances.fds[i]);
312 } else if (prog->instances.nr != -1) {
313 pr_warning("Internal error: instances.nr is %d\n",
314 prog->instances.nr);
315 }
316
317 prog->instances.nr = -1;
318 zfree(&prog->instances.fds);
David Brazdil0f672f62019-12-10 10:32:29 +0000319
320 zfree(&prog->func_info);
321 zfree(&prog->line_info);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000322}
323
324static void bpf_program__exit(struct bpf_program *prog)
325{
326 if (!prog)
327 return;
328
329 if (prog->clear_priv)
330 prog->clear_priv(prog, prog->priv);
331
332 prog->priv = NULL;
333 prog->clear_priv = NULL;
334
335 bpf_program__unload(prog);
336 zfree(&prog->name);
337 zfree(&prog->section_name);
David Brazdil0f672f62019-12-10 10:32:29 +0000338 zfree(&prog->pin_name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000339 zfree(&prog->insns);
340 zfree(&prog->reloc_desc);
341
342 prog->nr_reloc = 0;
343 prog->insns_cnt = 0;
344 prog->idx = -1;
345}
346
David Brazdil0f672f62019-12-10 10:32:29 +0000347static char *__bpf_program__pin_name(struct bpf_program *prog)
348{
349 char *name, *p;
350
351 name = p = strdup(prog->section_name);
352 while ((p = strchr(p, '/')))
353 *p = '_';
354
355 return name;
356}
357
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000358static int
359bpf_program__init(void *data, size_t size, char *section_name, int idx,
360 struct bpf_program *prog)
361{
David Brazdil0f672f62019-12-10 10:32:29 +0000362 const size_t bpf_insn_sz = sizeof(struct bpf_insn);
363
364 if (size == 0 || size % bpf_insn_sz) {
365 pr_warning("corrupted section '%s', size: %zu\n",
366 section_name, size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000367 return -EINVAL;
368 }
369
David Brazdil0f672f62019-12-10 10:32:29 +0000370 memset(prog, 0, sizeof(*prog));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000371
372 prog->section_name = strdup(section_name);
373 if (!prog->section_name) {
374 pr_warning("failed to alloc name for prog under section(%d) %s\n",
375 idx, section_name);
376 goto errout;
377 }
378
David Brazdil0f672f62019-12-10 10:32:29 +0000379 prog->pin_name = __bpf_program__pin_name(prog);
380 if (!prog->pin_name) {
381 pr_warning("failed to alloc pin name for prog under section(%d) %s\n",
382 idx, section_name);
383 goto errout;
384 }
385
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000386 prog->insns = malloc(size);
387 if (!prog->insns) {
388 pr_warning("failed to alloc insns for prog under section %s\n",
389 section_name);
390 goto errout;
391 }
David Brazdil0f672f62019-12-10 10:32:29 +0000392 prog->insns_cnt = size / bpf_insn_sz;
393 memcpy(prog->insns, data, size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000394 prog->idx = idx;
395 prog->instances.fds = NULL;
396 prog->instances.nr = -1;
David Brazdil0f672f62019-12-10 10:32:29 +0000397 prog->type = BPF_PROG_TYPE_UNSPEC;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000398
399 return 0;
400errout:
401 bpf_program__exit(prog);
402 return -ENOMEM;
403}
404
405static int
406bpf_object__add_program(struct bpf_object *obj, void *data, size_t size,
407 char *section_name, int idx)
408{
409 struct bpf_program prog, *progs;
410 int nr_progs, err;
411
412 err = bpf_program__init(data, size, section_name, idx, &prog);
413 if (err)
414 return err;
415
David Brazdil0f672f62019-12-10 10:32:29 +0000416 prog.caps = &obj->caps;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000417 progs = obj->programs;
418 nr_progs = obj->nr_programs;
419
420 progs = reallocarray(progs, nr_progs + 1, sizeof(progs[0]));
421 if (!progs) {
422 /*
423 * In this case the original obj->programs
424 * is still valid, so don't need special treat for
425 * bpf_close_object().
426 */
427 pr_warning("failed to alloc a new program under section '%s'\n",
428 section_name);
429 bpf_program__exit(&prog);
430 return -ENOMEM;
431 }
432
433 pr_debug("found program %s\n", prog.section_name);
434 obj->programs = progs;
435 obj->nr_programs = nr_progs + 1;
436 prog.obj = obj;
437 progs[nr_progs] = prog;
438 return 0;
439}
440
441static int
442bpf_object__init_prog_names(struct bpf_object *obj)
443{
444 Elf_Data *symbols = obj->efile.symbols;
445 struct bpf_program *prog;
446 size_t pi, si;
447
448 for (pi = 0; pi < obj->nr_programs; pi++) {
449 const char *name = NULL;
450
451 prog = &obj->programs[pi];
452
453 for (si = 0; si < symbols->d_size / sizeof(GElf_Sym) && !name;
454 si++) {
455 GElf_Sym sym;
456
457 if (!gelf_getsym(symbols, si, &sym))
458 continue;
459 if (sym.st_shndx != prog->idx)
460 continue;
461 if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL)
462 continue;
463
464 name = elf_strptr(obj->efile.elf,
465 obj->efile.strtabidx,
466 sym.st_name);
467 if (!name) {
468 pr_warning("failed to get sym name string for prog %s\n",
469 prog->section_name);
470 return -LIBBPF_ERRNO__LIBELF;
471 }
472 }
473
474 if (!name && prog->idx == obj->efile.text_shndx)
475 name = ".text";
476
477 if (!name) {
478 pr_warning("failed to find sym for prog %s\n",
479 prog->section_name);
480 return -EINVAL;
481 }
482
483 prog->name = strdup(name);
484 if (!prog->name) {
485 pr_warning("failed to allocate memory for prog sym %s\n",
486 name);
487 return -ENOMEM;
488 }
489 }
490
491 return 0;
492}
493
494static struct bpf_object *bpf_object__new(const char *path,
495 void *obj_buf,
496 size_t obj_buf_sz)
497{
498 struct bpf_object *obj;
David Brazdil0f672f62019-12-10 10:32:29 +0000499 char *end;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000500
501 obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1);
502 if (!obj) {
503 pr_warning("alloc memory failed for %s\n", path);
504 return ERR_PTR(-ENOMEM);
505 }
506
507 strcpy(obj->path, path);
David Brazdil0f672f62019-12-10 10:32:29 +0000508 /* Using basename() GNU version which doesn't modify arg. */
509 strncpy(obj->name, basename((void *)path), sizeof(obj->name) - 1);
510 end = strchr(obj->name, '.');
511 if (end)
512 *end = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000513
David Brazdil0f672f62019-12-10 10:32:29 +0000514 obj->efile.fd = -1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000515 /*
David Brazdil0f672f62019-12-10 10:32:29 +0000516 * Caller of this function should also call
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000517 * bpf_object__elf_finish() after data collection to return
518 * obj_buf to user. If not, we should duplicate the buffer to
519 * avoid user freeing them before elf finish.
520 */
521 obj->efile.obj_buf = obj_buf;
522 obj->efile.obj_buf_sz = obj_buf_sz;
523 obj->efile.maps_shndx = -1;
David Brazdil0f672f62019-12-10 10:32:29 +0000524 obj->efile.btf_maps_shndx = -1;
525 obj->efile.data_shndx = -1;
526 obj->efile.rodata_shndx = -1;
527 obj->efile.bss_shndx = -1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000528
529 obj->loaded = false;
530
531 INIT_LIST_HEAD(&obj->list);
532 list_add(&obj->list, &bpf_objects_list);
533 return obj;
534}
535
536static void bpf_object__elf_finish(struct bpf_object *obj)
537{
538 if (!obj_elf_valid(obj))
539 return;
540
541 if (obj->efile.elf) {
542 elf_end(obj->efile.elf);
543 obj->efile.elf = NULL;
544 }
545 obj->efile.symbols = NULL;
David Brazdil0f672f62019-12-10 10:32:29 +0000546 obj->efile.data = NULL;
547 obj->efile.rodata = NULL;
548 obj->efile.bss = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000549
550 zfree(&obj->efile.reloc);
551 obj->efile.nr_reloc = 0;
552 zclose(obj->efile.fd);
553 obj->efile.obj_buf = NULL;
554 obj->efile.obj_buf_sz = 0;
555}
556
557static int bpf_object__elf_init(struct bpf_object *obj)
558{
559 int err = 0;
560 GElf_Ehdr *ep;
561
562 if (obj_elf_valid(obj)) {
563 pr_warning("elf init: internal error\n");
564 return -LIBBPF_ERRNO__LIBELF;
565 }
566
567 if (obj->efile.obj_buf_sz > 0) {
568 /*
569 * obj_buf should have been validated by
570 * bpf_object__open_buffer().
571 */
572 obj->efile.elf = elf_memory(obj->efile.obj_buf,
573 obj->efile.obj_buf_sz);
574 } else {
575 obj->efile.fd = open(obj->path, O_RDONLY);
576 if (obj->efile.fd < 0) {
David Brazdil0f672f62019-12-10 10:32:29 +0000577 char errmsg[STRERR_BUFSIZE], *cp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000578
David Brazdil0f672f62019-12-10 10:32:29 +0000579 err = -errno;
580 cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000581 pr_warning("failed to open %s: %s\n", obj->path, cp);
David Brazdil0f672f62019-12-10 10:32:29 +0000582 return err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000583 }
584
585 obj->efile.elf = elf_begin(obj->efile.fd,
David Brazdil0f672f62019-12-10 10:32:29 +0000586 LIBBPF_ELF_C_READ_MMAP, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000587 }
588
589 if (!obj->efile.elf) {
David Brazdil0f672f62019-12-10 10:32:29 +0000590 pr_warning("failed to open %s as ELF file\n", obj->path);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000591 err = -LIBBPF_ERRNO__LIBELF;
592 goto errout;
593 }
594
595 if (!gelf_getehdr(obj->efile.elf, &obj->efile.ehdr)) {
David Brazdil0f672f62019-12-10 10:32:29 +0000596 pr_warning("failed to get EHDR from %s\n", obj->path);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000597 err = -LIBBPF_ERRNO__FORMAT;
598 goto errout;
599 }
600 ep = &obj->efile.ehdr;
601
602 /* Old LLVM set e_machine to EM_NONE */
David Brazdil0f672f62019-12-10 10:32:29 +0000603 if (ep->e_type != ET_REL ||
604 (ep->e_machine && ep->e_machine != EM_BPF)) {
605 pr_warning("%s is not an eBPF object file\n", obj->path);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000606 err = -LIBBPF_ERRNO__FORMAT;
607 goto errout;
608 }
609
610 return 0;
611errout:
612 bpf_object__elf_finish(obj);
613 return err;
614}
615
David Brazdil0f672f62019-12-10 10:32:29 +0000616static int bpf_object__check_endianness(struct bpf_object *obj)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000617{
David Brazdil0f672f62019-12-10 10:32:29 +0000618#if __BYTE_ORDER == __LITTLE_ENDIAN
619 if (obj->efile.ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
620 return 0;
621#elif __BYTE_ORDER == __BIG_ENDIAN
622 if (obj->efile.ehdr.e_ident[EI_DATA] == ELFDATA2MSB)
623 return 0;
624#else
625# error "Unrecognized __BYTE_ORDER__"
626#endif
627 pr_warning("endianness mismatch.\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000628 return -LIBBPF_ERRNO__ENDIAN;
629}
630
631static int
David Brazdil0f672f62019-12-10 10:32:29 +0000632bpf_object__init_license(struct bpf_object *obj, void *data, size_t size)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000633{
David Brazdil0f672f62019-12-10 10:32:29 +0000634 memcpy(obj->license, data, min(size, sizeof(obj->license) - 1));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000635 pr_debug("license of %s is %s\n", obj->path, obj->license);
636 return 0;
637}
638
639static int
David Brazdil0f672f62019-12-10 10:32:29 +0000640bpf_object__init_kversion(struct bpf_object *obj, void *data, size_t size)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000641{
David Brazdil0f672f62019-12-10 10:32:29 +0000642 __u32 kver;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000643
644 if (size != sizeof(kver)) {
645 pr_warning("invalid kver section in %s\n", obj->path);
646 return -LIBBPF_ERRNO__FORMAT;
647 }
648 memcpy(&kver, data, sizeof(kver));
649 obj->kern_version = kver;
David Brazdil0f672f62019-12-10 10:32:29 +0000650 pr_debug("kernel version of %s is %x\n", obj->path, obj->kern_version);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000651 return 0;
652}
653
654static int compare_bpf_map(const void *_a, const void *_b)
655{
656 const struct bpf_map *a = _a;
657 const struct bpf_map *b = _b;
658
David Brazdil0f672f62019-12-10 10:32:29 +0000659 if (a->sec_idx != b->sec_idx)
660 return a->sec_idx - b->sec_idx;
661 return a->sec_offset - b->sec_offset;
662}
663
664static bool bpf_map_type__is_map_in_map(enum bpf_map_type type)
665{
666 if (type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
667 type == BPF_MAP_TYPE_HASH_OF_MAPS)
668 return true;
669 return false;
670}
671
672static int bpf_object_search_section_size(const struct bpf_object *obj,
673 const char *name, size_t *d_size)
674{
675 const GElf_Ehdr *ep = &obj->efile.ehdr;
676 Elf *elf = obj->efile.elf;
677 Elf_Scn *scn = NULL;
678 int idx = 0;
679
680 while ((scn = elf_nextscn(elf, scn)) != NULL) {
681 const char *sec_name;
682 Elf_Data *data;
683 GElf_Shdr sh;
684
685 idx++;
686 if (gelf_getshdr(scn, &sh) != &sh) {
687 pr_warning("failed to get section(%d) header from %s\n",
688 idx, obj->path);
689 return -EIO;
690 }
691
692 sec_name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name);
693 if (!sec_name) {
694 pr_warning("failed to get section(%d) name from %s\n",
695 idx, obj->path);
696 return -EIO;
697 }
698
699 if (strcmp(name, sec_name))
700 continue;
701
702 data = elf_getdata(scn, 0);
703 if (!data) {
704 pr_warning("failed to get section(%d) data from %s(%s)\n",
705 idx, name, obj->path);
706 return -EIO;
707 }
708
709 *d_size = data->d_size;
710 return 0;
711 }
712
713 return -ENOENT;
714}
715
716int bpf_object__section_size(const struct bpf_object *obj, const char *name,
717 __u32 *size)
718{
719 int ret = -ENOENT;
720 size_t d_size;
721
722 *size = 0;
723 if (!name) {
724 return -EINVAL;
725 } else if (!strcmp(name, ".data")) {
726 if (obj->efile.data)
727 *size = obj->efile.data->d_size;
728 } else if (!strcmp(name, ".bss")) {
729 if (obj->efile.bss)
730 *size = obj->efile.bss->d_size;
731 } else if (!strcmp(name, ".rodata")) {
732 if (obj->efile.rodata)
733 *size = obj->efile.rodata->d_size;
734 } else {
735 ret = bpf_object_search_section_size(obj, name, &d_size);
736 if (!ret)
737 *size = d_size;
738 }
739
740 return *size ? 0 : ret;
741}
742
743int bpf_object__variable_offset(const struct bpf_object *obj, const char *name,
744 __u32 *off)
745{
746 Elf_Data *symbols = obj->efile.symbols;
747 const char *sname;
748 size_t si;
749
750 if (!name || !off)
751 return -EINVAL;
752
753 for (si = 0; si < symbols->d_size / sizeof(GElf_Sym); si++) {
754 GElf_Sym sym;
755
756 if (!gelf_getsym(symbols, si, &sym))
757 continue;
758 if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL ||
759 GELF_ST_TYPE(sym.st_info) != STT_OBJECT)
760 continue;
761
762 sname = elf_strptr(obj->efile.elf, obj->efile.strtabidx,
763 sym.st_name);
764 if (!sname) {
765 pr_warning("failed to get sym name string for var %s\n",
766 name);
767 return -EIO;
768 }
769 if (strcmp(name, sname) == 0) {
770 *off = sym.st_value;
771 return 0;
772 }
773 }
774
775 return -ENOENT;
776}
777
778static struct bpf_map *bpf_object__add_map(struct bpf_object *obj)
779{
780 struct bpf_map *new_maps;
781 size_t new_cap;
782 int i;
783
784 if (obj->nr_maps < obj->maps_cap)
785 return &obj->maps[obj->nr_maps++];
786
787 new_cap = max((size_t)4, obj->maps_cap * 3 / 2);
788 new_maps = realloc(obj->maps, new_cap * sizeof(*obj->maps));
789 if (!new_maps) {
790 pr_warning("alloc maps for object failed\n");
791 return ERR_PTR(-ENOMEM);
792 }
793
794 obj->maps_cap = new_cap;
795 obj->maps = new_maps;
796
797 /* zero out new maps */
798 memset(obj->maps + obj->nr_maps, 0,
799 (obj->maps_cap - obj->nr_maps) * sizeof(*obj->maps));
800 /*
801 * fill all fd with -1 so won't close incorrect fd (fd=0 is stdin)
802 * when failure (zclose won't close negative fd)).
803 */
804 for (i = obj->nr_maps; i < obj->maps_cap; i++) {
805 obj->maps[i].fd = -1;
806 obj->maps[i].inner_map_fd = -1;
807 }
808
809 return &obj->maps[obj->nr_maps++];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000810}
811
812static int
David Brazdil0f672f62019-12-10 10:32:29 +0000813bpf_object__init_internal_map(struct bpf_object *obj, enum libbpf_map_type type,
814 int sec_idx, Elf_Data *data, void **data_buff)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000815{
David Brazdil0f672f62019-12-10 10:32:29 +0000816 char map_name[BPF_OBJ_NAME_LEN];
817 struct bpf_map_def *def;
818 struct bpf_map *map;
819
820 map = bpf_object__add_map(obj);
821 if (IS_ERR(map))
822 return PTR_ERR(map);
823
824 map->libbpf_type = type;
825 map->sec_idx = sec_idx;
826 map->sec_offset = 0;
827 snprintf(map_name, sizeof(map_name), "%.8s%.7s", obj->name,
828 libbpf_type_to_btf_name[type]);
829 map->name = strdup(map_name);
830 if (!map->name) {
831 pr_warning("failed to alloc map name\n");
832 return -ENOMEM;
833 }
834 pr_debug("map '%s' (global data): at sec_idx %d, offset %zu.\n",
835 map_name, map->sec_idx, map->sec_offset);
836
837 def = &map->def;
838 def->type = BPF_MAP_TYPE_ARRAY;
839 def->key_size = sizeof(int);
840 def->value_size = data->d_size;
841 def->max_entries = 1;
842 def->map_flags = type == LIBBPF_MAP_RODATA ? BPF_F_RDONLY_PROG : 0;
843 if (data_buff) {
844 *data_buff = malloc(data->d_size);
845 if (!*data_buff) {
846 zfree(&map->name);
847 pr_warning("failed to alloc map content buffer\n");
848 return -ENOMEM;
849 }
850 memcpy(*data_buff, data->d_buf, data->d_size);
851 }
852
853 pr_debug("map %td is \"%s\"\n", map - obj->maps, map->name);
854 return 0;
855}
856
857static int bpf_object__init_global_data_maps(struct bpf_object *obj)
858{
859 int err;
860
861 if (!obj->caps.global_data)
862 return 0;
863 /*
864 * Populate obj->maps with libbpf internal maps.
865 */
866 if (obj->efile.data_shndx >= 0) {
867 err = bpf_object__init_internal_map(obj, LIBBPF_MAP_DATA,
868 obj->efile.data_shndx,
869 obj->efile.data,
870 &obj->sections.data);
871 if (err)
872 return err;
873 }
874 if (obj->efile.rodata_shndx >= 0) {
875 err = bpf_object__init_internal_map(obj, LIBBPF_MAP_RODATA,
876 obj->efile.rodata_shndx,
877 obj->efile.rodata,
878 &obj->sections.rodata);
879 if (err)
880 return err;
881 }
882 if (obj->efile.bss_shndx >= 0) {
883 err = bpf_object__init_internal_map(obj, LIBBPF_MAP_BSS,
884 obj->efile.bss_shndx,
885 obj->efile.bss, NULL);
886 if (err)
887 return err;
888 }
889 return 0;
890}
891
892static int bpf_object__init_user_maps(struct bpf_object *obj, bool strict)
893{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000894 Elf_Data *symbols = obj->efile.symbols;
David Brazdil0f672f62019-12-10 10:32:29 +0000895 int i, map_def_sz = 0, nr_maps = 0, nr_syms;
896 Elf_Data *data = NULL;
897 Elf_Scn *scn;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000898
899 if (obj->efile.maps_shndx < 0)
David Brazdil0f672f62019-12-10 10:32:29 +0000900 return 0;
901
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000902 if (!symbols)
903 return -EINVAL;
904
905 scn = elf_getscn(obj->efile.elf, obj->efile.maps_shndx);
906 if (scn)
907 data = elf_getdata(scn, NULL);
908 if (!scn || !data) {
909 pr_warning("failed to get Elf_Data from map section %d\n",
910 obj->efile.maps_shndx);
911 return -EINVAL;
912 }
913
914 /*
915 * Count number of maps. Each map has a name.
916 * Array of maps is not supported: only the first element is
917 * considered.
918 *
919 * TODO: Detect array of map and report error.
920 */
David Brazdil0f672f62019-12-10 10:32:29 +0000921 nr_syms = symbols->d_size / sizeof(GElf_Sym);
922 for (i = 0; i < nr_syms; i++) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000923 GElf_Sym sym;
924
925 if (!gelf_getsym(symbols, i, &sym))
926 continue;
927 if (sym.st_shndx != obj->efile.maps_shndx)
928 continue;
929 nr_maps++;
930 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000931 /* Assume equally sized map definitions */
David Brazdil0f672f62019-12-10 10:32:29 +0000932 pr_debug("maps in %s: %d maps in %zd bytes\n",
933 obj->path, nr_maps, data->d_size);
934
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000935 map_def_sz = data->d_size / nr_maps;
936 if (!data->d_size || (data->d_size % nr_maps) != 0) {
937 pr_warning("unable to determine map definition size "
938 "section %s, %d maps in %zd bytes\n",
939 obj->path, nr_maps, data->d_size);
940 return -EINVAL;
941 }
942
David Brazdil0f672f62019-12-10 10:32:29 +0000943 /* Fill obj->maps using data in "maps" section. */
944 for (i = 0; i < nr_syms; i++) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000945 GElf_Sym sym;
946 const char *map_name;
947 struct bpf_map_def *def;
David Brazdil0f672f62019-12-10 10:32:29 +0000948 struct bpf_map *map;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000949
950 if (!gelf_getsym(symbols, i, &sym))
951 continue;
952 if (sym.st_shndx != obj->efile.maps_shndx)
953 continue;
954
David Brazdil0f672f62019-12-10 10:32:29 +0000955 map = bpf_object__add_map(obj);
956 if (IS_ERR(map))
957 return PTR_ERR(map);
958
959 map_name = elf_strptr(obj->efile.elf, obj->efile.strtabidx,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000960 sym.st_name);
David Brazdil0f672f62019-12-10 10:32:29 +0000961 if (!map_name) {
962 pr_warning("failed to get map #%d name sym string for obj %s\n",
963 i, obj->path);
964 return -LIBBPF_ERRNO__FORMAT;
965 }
966
967 map->libbpf_type = LIBBPF_MAP_UNSPEC;
968 map->sec_idx = sym.st_shndx;
969 map->sec_offset = sym.st_value;
970 pr_debug("map '%s' (legacy): at sec_idx %d, offset %zu.\n",
971 map_name, map->sec_idx, map->sec_offset);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000972 if (sym.st_value + map_def_sz > data->d_size) {
973 pr_warning("corrupted maps section in %s: last map \"%s\" too small\n",
974 obj->path, map_name);
975 return -EINVAL;
976 }
977
David Brazdil0f672f62019-12-10 10:32:29 +0000978 map->name = strdup(map_name);
979 if (!map->name) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000980 pr_warning("failed to alloc map name\n");
981 return -ENOMEM;
982 }
David Brazdil0f672f62019-12-10 10:32:29 +0000983 pr_debug("map %d is \"%s\"\n", i, map->name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000984 def = (struct bpf_map_def *)(data->d_buf + sym.st_value);
985 /*
986 * If the definition of the map in the object file fits in
987 * bpf_map_def, copy it. Any extra fields in our version
988 * of bpf_map_def will default to zero as a result of the
989 * calloc above.
990 */
991 if (map_def_sz <= sizeof(struct bpf_map_def)) {
David Brazdil0f672f62019-12-10 10:32:29 +0000992 memcpy(&map->def, def, map_def_sz);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000993 } else {
994 /*
995 * Here the map structure being read is bigger than what
996 * we expect, truncate if the excess bits are all zero.
997 * If they are not zero, reject this map as
998 * incompatible.
999 */
1000 char *b;
1001 for (b = ((char *)def) + sizeof(struct bpf_map_def);
1002 b < ((char *)def) + map_def_sz; b++) {
1003 if (*b != 0) {
1004 pr_warning("maps section in %s: \"%s\" "
1005 "has unrecognized, non-zero "
1006 "options\n",
1007 obj->path, map_name);
David Brazdil0f672f62019-12-10 10:32:29 +00001008 if (strict)
1009 return -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001010 }
1011 }
David Brazdil0f672f62019-12-10 10:32:29 +00001012 memcpy(&map->def, def, sizeof(struct bpf_map_def));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001013 }
David Brazdil0f672f62019-12-10 10:32:29 +00001014 }
1015 return 0;
1016}
1017
1018static const struct btf_type *
1019skip_mods_and_typedefs(const struct btf *btf, __u32 id, __u32 *res_id)
1020{
1021 const struct btf_type *t = btf__type_by_id(btf, id);
1022
1023 if (res_id)
1024 *res_id = id;
1025
1026 while (btf_is_mod(t) || btf_is_typedef(t)) {
1027 if (res_id)
1028 *res_id = t->type;
1029 t = btf__type_by_id(btf, t->type);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001030 }
1031
David Brazdil0f672f62019-12-10 10:32:29 +00001032 return t;
1033}
1034
1035/*
1036 * Fetch integer attribute of BTF map definition. Such attributes are
1037 * represented using a pointer to an array, in which dimensionality of array
1038 * encodes specified integer value. E.g., int (*type)[BPF_MAP_TYPE_ARRAY];
1039 * encodes `type => BPF_MAP_TYPE_ARRAY` key/value pair completely using BTF
1040 * type definition, while using only sizeof(void *) space in ELF data section.
1041 */
1042static bool get_map_field_int(const char *map_name, const struct btf *btf,
1043 const struct btf_type *def,
1044 const struct btf_member *m, __u32 *res) {
1045 const struct btf_type *t = skip_mods_and_typedefs(btf, m->type, NULL);
1046 const char *name = btf__name_by_offset(btf, m->name_off);
1047 const struct btf_array *arr_info;
1048 const struct btf_type *arr_t;
1049
1050 if (!btf_is_ptr(t)) {
1051 pr_warning("map '%s': attr '%s': expected PTR, got %u.\n",
1052 map_name, name, btf_kind(t));
1053 return false;
1054 }
1055
1056 arr_t = btf__type_by_id(btf, t->type);
1057 if (!arr_t) {
1058 pr_warning("map '%s': attr '%s': type [%u] not found.\n",
1059 map_name, name, t->type);
1060 return false;
1061 }
1062 if (!btf_is_array(arr_t)) {
1063 pr_warning("map '%s': attr '%s': expected ARRAY, got %u.\n",
1064 map_name, name, btf_kind(arr_t));
1065 return false;
1066 }
1067 arr_info = btf_array(arr_t);
1068 *res = arr_info->nelems;
1069 return true;
1070}
1071
1072static int bpf_object__init_user_btf_map(struct bpf_object *obj,
1073 const struct btf_type *sec,
1074 int var_idx, int sec_idx,
1075 const Elf_Data *data, bool strict)
1076{
1077 const struct btf_type *var, *def, *t;
1078 const struct btf_var_secinfo *vi;
1079 const struct btf_var *var_extra;
1080 const struct btf_member *m;
1081 const char *map_name;
1082 struct bpf_map *map;
1083 int vlen, i;
1084
1085 vi = btf_var_secinfos(sec) + var_idx;
1086 var = btf__type_by_id(obj->btf, vi->type);
1087 var_extra = btf_var(var);
1088 map_name = btf__name_by_offset(obj->btf, var->name_off);
1089 vlen = btf_vlen(var);
1090
1091 if (map_name == NULL || map_name[0] == '\0') {
1092 pr_warning("map #%d: empty name.\n", var_idx);
1093 return -EINVAL;
1094 }
1095 if ((__u64)vi->offset + vi->size > data->d_size) {
1096 pr_warning("map '%s' BTF data is corrupted.\n", map_name);
1097 return -EINVAL;
1098 }
1099 if (!btf_is_var(var)) {
1100 pr_warning("map '%s': unexpected var kind %u.\n",
1101 map_name, btf_kind(var));
1102 return -EINVAL;
1103 }
1104 if (var_extra->linkage != BTF_VAR_GLOBAL_ALLOCATED &&
1105 var_extra->linkage != BTF_VAR_STATIC) {
1106 pr_warning("map '%s': unsupported var linkage %u.\n",
1107 map_name, var_extra->linkage);
1108 return -EOPNOTSUPP;
1109 }
1110
1111 def = skip_mods_and_typedefs(obj->btf, var->type, NULL);
1112 if (!btf_is_struct(def)) {
1113 pr_warning("map '%s': unexpected def kind %u.\n",
1114 map_name, btf_kind(var));
1115 return -EINVAL;
1116 }
1117 if (def->size > vi->size) {
1118 pr_warning("map '%s': invalid def size.\n", map_name);
1119 return -EINVAL;
1120 }
1121
1122 map = bpf_object__add_map(obj);
1123 if (IS_ERR(map))
1124 return PTR_ERR(map);
1125 map->name = strdup(map_name);
1126 if (!map->name) {
1127 pr_warning("map '%s': failed to alloc map name.\n", map_name);
1128 return -ENOMEM;
1129 }
1130 map->libbpf_type = LIBBPF_MAP_UNSPEC;
1131 map->def.type = BPF_MAP_TYPE_UNSPEC;
1132 map->sec_idx = sec_idx;
1133 map->sec_offset = vi->offset;
1134 pr_debug("map '%s': at sec_idx %d, offset %zu.\n",
1135 map_name, map->sec_idx, map->sec_offset);
1136
1137 vlen = btf_vlen(def);
1138 m = btf_members(def);
1139 for (i = 0; i < vlen; i++, m++) {
1140 const char *name = btf__name_by_offset(obj->btf, m->name_off);
1141
1142 if (!name) {
1143 pr_warning("map '%s': invalid field #%d.\n",
1144 map_name, i);
1145 return -EINVAL;
1146 }
1147 if (strcmp(name, "type") == 0) {
1148 if (!get_map_field_int(map_name, obj->btf, def, m,
1149 &map->def.type))
1150 return -EINVAL;
1151 pr_debug("map '%s': found type = %u.\n",
1152 map_name, map->def.type);
1153 } else if (strcmp(name, "max_entries") == 0) {
1154 if (!get_map_field_int(map_name, obj->btf, def, m,
1155 &map->def.max_entries))
1156 return -EINVAL;
1157 pr_debug("map '%s': found max_entries = %u.\n",
1158 map_name, map->def.max_entries);
1159 } else if (strcmp(name, "map_flags") == 0) {
1160 if (!get_map_field_int(map_name, obj->btf, def, m,
1161 &map->def.map_flags))
1162 return -EINVAL;
1163 pr_debug("map '%s': found map_flags = %u.\n",
1164 map_name, map->def.map_flags);
1165 } else if (strcmp(name, "key_size") == 0) {
1166 __u32 sz;
1167
1168 if (!get_map_field_int(map_name, obj->btf, def, m,
1169 &sz))
1170 return -EINVAL;
1171 pr_debug("map '%s': found key_size = %u.\n",
1172 map_name, sz);
1173 if (map->def.key_size && map->def.key_size != sz) {
1174 pr_warning("map '%s': conflicting key size %u != %u.\n",
1175 map_name, map->def.key_size, sz);
1176 return -EINVAL;
1177 }
1178 map->def.key_size = sz;
1179 } else if (strcmp(name, "key") == 0) {
1180 __s64 sz;
1181
1182 t = btf__type_by_id(obj->btf, m->type);
1183 if (!t) {
1184 pr_warning("map '%s': key type [%d] not found.\n",
1185 map_name, m->type);
1186 return -EINVAL;
1187 }
1188 if (!btf_is_ptr(t)) {
1189 pr_warning("map '%s': key spec is not PTR: %u.\n",
1190 map_name, btf_kind(t));
1191 return -EINVAL;
1192 }
1193 sz = btf__resolve_size(obj->btf, t->type);
1194 if (sz < 0) {
1195 pr_warning("map '%s': can't determine key size for type [%u]: %lld.\n",
1196 map_name, t->type, sz);
1197 return sz;
1198 }
1199 pr_debug("map '%s': found key [%u], sz = %lld.\n",
1200 map_name, t->type, sz);
1201 if (map->def.key_size && map->def.key_size != sz) {
1202 pr_warning("map '%s': conflicting key size %u != %lld.\n",
1203 map_name, map->def.key_size, sz);
1204 return -EINVAL;
1205 }
1206 map->def.key_size = sz;
1207 map->btf_key_type_id = t->type;
1208 } else if (strcmp(name, "value_size") == 0) {
1209 __u32 sz;
1210
1211 if (!get_map_field_int(map_name, obj->btf, def, m,
1212 &sz))
1213 return -EINVAL;
1214 pr_debug("map '%s': found value_size = %u.\n",
1215 map_name, sz);
1216 if (map->def.value_size && map->def.value_size != sz) {
1217 pr_warning("map '%s': conflicting value size %u != %u.\n",
1218 map_name, map->def.value_size, sz);
1219 return -EINVAL;
1220 }
1221 map->def.value_size = sz;
1222 } else if (strcmp(name, "value") == 0) {
1223 __s64 sz;
1224
1225 t = btf__type_by_id(obj->btf, m->type);
1226 if (!t) {
1227 pr_warning("map '%s': value type [%d] not found.\n",
1228 map_name, m->type);
1229 return -EINVAL;
1230 }
1231 if (!btf_is_ptr(t)) {
1232 pr_warning("map '%s': value spec is not PTR: %u.\n",
1233 map_name, btf_kind(t));
1234 return -EINVAL;
1235 }
1236 sz = btf__resolve_size(obj->btf, t->type);
1237 if (sz < 0) {
1238 pr_warning("map '%s': can't determine value size for type [%u]: %lld.\n",
1239 map_name, t->type, sz);
1240 return sz;
1241 }
1242 pr_debug("map '%s': found value [%u], sz = %lld.\n",
1243 map_name, t->type, sz);
1244 if (map->def.value_size && map->def.value_size != sz) {
1245 pr_warning("map '%s': conflicting value size %u != %lld.\n",
1246 map_name, map->def.value_size, sz);
1247 return -EINVAL;
1248 }
1249 map->def.value_size = sz;
1250 map->btf_value_type_id = t->type;
1251 } else {
1252 if (strict) {
1253 pr_warning("map '%s': unknown field '%s'.\n",
1254 map_name, name);
1255 return -ENOTSUP;
1256 }
1257 pr_debug("map '%s': ignoring unknown field '%s'.\n",
1258 map_name, name);
1259 }
1260 }
1261
1262 if (map->def.type == BPF_MAP_TYPE_UNSPEC) {
1263 pr_warning("map '%s': map type isn't specified.\n", map_name);
1264 return -EINVAL;
1265 }
1266
1267 return 0;
1268}
1269
1270static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict)
1271{
1272 const struct btf_type *sec = NULL;
1273 int nr_types, i, vlen, err;
1274 const struct btf_type *t;
1275 const char *name;
1276 Elf_Data *data;
1277 Elf_Scn *scn;
1278
1279 if (obj->efile.btf_maps_shndx < 0)
1280 return 0;
1281
1282 scn = elf_getscn(obj->efile.elf, obj->efile.btf_maps_shndx);
1283 if (scn)
1284 data = elf_getdata(scn, NULL);
1285 if (!scn || !data) {
1286 pr_warning("failed to get Elf_Data from map section %d (%s)\n",
1287 obj->efile.maps_shndx, MAPS_ELF_SEC);
1288 return -EINVAL;
1289 }
1290
1291 nr_types = btf__get_nr_types(obj->btf);
1292 for (i = 1; i <= nr_types; i++) {
1293 t = btf__type_by_id(obj->btf, i);
1294 if (!btf_is_datasec(t))
1295 continue;
1296 name = btf__name_by_offset(obj->btf, t->name_off);
1297 if (strcmp(name, MAPS_ELF_SEC) == 0) {
1298 sec = t;
1299 break;
1300 }
1301 }
1302
1303 if (!sec) {
1304 pr_warning("DATASEC '%s' not found.\n", MAPS_ELF_SEC);
1305 return -ENOENT;
1306 }
1307
1308 vlen = btf_vlen(sec);
1309 for (i = 0; i < vlen; i++) {
1310 err = bpf_object__init_user_btf_map(obj, sec, i,
1311 obj->efile.btf_maps_shndx,
1312 data, strict);
1313 if (err)
1314 return err;
1315 }
1316
1317 return 0;
1318}
1319
1320static int bpf_object__init_maps(struct bpf_object *obj, int flags)
1321{
1322 bool strict = !(flags & MAPS_RELAX_COMPAT);
1323 int err;
1324
1325 err = bpf_object__init_user_maps(obj, strict);
1326 if (err)
1327 return err;
1328
1329 err = bpf_object__init_user_btf_maps(obj, strict);
1330 if (err)
1331 return err;
1332
1333 err = bpf_object__init_global_data_maps(obj);
1334 if (err)
1335 return err;
1336
1337 if (obj->nr_maps) {
1338 qsort(obj->maps, obj->nr_maps, sizeof(obj->maps[0]),
1339 compare_bpf_map);
1340 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001341 return 0;
1342}
1343
1344static bool section_have_execinstr(struct bpf_object *obj, int idx)
1345{
1346 Elf_Scn *scn;
1347 GElf_Shdr sh;
1348
1349 scn = elf_getscn(obj->efile.elf, idx);
1350 if (!scn)
1351 return false;
1352
1353 if (gelf_getshdr(scn, &sh) != &sh)
1354 return false;
1355
1356 if (sh.sh_flags & SHF_EXECINSTR)
1357 return true;
1358
1359 return false;
1360}
1361
David Brazdil0f672f62019-12-10 10:32:29 +00001362static void bpf_object__sanitize_btf(struct bpf_object *obj)
1363{
1364 bool has_datasec = obj->caps.btf_datasec;
1365 bool has_func = obj->caps.btf_func;
1366 struct btf *btf = obj->btf;
1367 struct btf_type *t;
1368 int i, j, vlen;
1369
1370 if (!obj->btf || (has_func && has_datasec))
1371 return;
1372
1373 for (i = 1; i <= btf__get_nr_types(btf); i++) {
1374 t = (struct btf_type *)btf__type_by_id(btf, i);
1375
1376 if (!has_datasec && btf_is_var(t)) {
1377 /* replace VAR with INT */
1378 t->info = BTF_INFO_ENC(BTF_KIND_INT, 0, 0);
1379 /*
1380 * using size = 1 is the safest choice, 4 will be too
1381 * big and cause kernel BTF validation failure if
1382 * original variable took less than 4 bytes
1383 */
1384 t->size = 1;
1385 *(int *)(t + 1) = BTF_INT_ENC(0, 0, 8);
1386 } else if (!has_datasec && btf_is_datasec(t)) {
1387 /* replace DATASEC with STRUCT */
1388 const struct btf_var_secinfo *v = btf_var_secinfos(t);
1389 struct btf_member *m = btf_members(t);
1390 struct btf_type *vt;
1391 char *name;
1392
1393 name = (char *)btf__name_by_offset(btf, t->name_off);
1394 while (*name) {
1395 if (*name == '.')
1396 *name = '_';
1397 name++;
1398 }
1399
1400 vlen = btf_vlen(t);
1401 t->info = BTF_INFO_ENC(BTF_KIND_STRUCT, 0, vlen);
1402 for (j = 0; j < vlen; j++, v++, m++) {
1403 /* order of field assignments is important */
1404 m->offset = v->offset * 8;
1405 m->type = v->type;
1406 /* preserve variable name as member name */
1407 vt = (void *)btf__type_by_id(btf, v->type);
1408 m->name_off = vt->name_off;
1409 }
1410 } else if (!has_func && btf_is_func_proto(t)) {
1411 /* replace FUNC_PROTO with ENUM */
1412 vlen = btf_vlen(t);
1413 t->info = BTF_INFO_ENC(BTF_KIND_ENUM, 0, vlen);
1414 t->size = sizeof(__u32); /* kernel enforced */
1415 } else if (!has_func && btf_is_func(t)) {
1416 /* replace FUNC with TYPEDEF */
1417 t->info = BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0);
1418 }
1419 }
1420}
1421
1422static void bpf_object__sanitize_btf_ext(struct bpf_object *obj)
1423{
1424 if (!obj->btf_ext)
1425 return;
1426
1427 if (!obj->caps.btf_func) {
1428 btf_ext__free(obj->btf_ext);
1429 obj->btf_ext = NULL;
1430 }
1431}
1432
1433static bool bpf_object__is_btf_mandatory(const struct bpf_object *obj)
1434{
1435 return obj->efile.btf_maps_shndx >= 0;
1436}
1437
1438static int bpf_object__init_btf(struct bpf_object *obj,
1439 Elf_Data *btf_data,
1440 Elf_Data *btf_ext_data)
1441{
1442 bool btf_required = bpf_object__is_btf_mandatory(obj);
1443 int err = 0;
1444
1445 if (btf_data) {
1446 obj->btf = btf__new(btf_data->d_buf, btf_data->d_size);
1447 if (IS_ERR(obj->btf)) {
1448 pr_warning("Error loading ELF section %s: %d.\n",
1449 BTF_ELF_SEC, err);
1450 goto out;
1451 }
1452 err = btf__finalize_data(obj, obj->btf);
1453 if (err) {
1454 pr_warning("Error finalizing %s: %d.\n",
1455 BTF_ELF_SEC, err);
1456 goto out;
1457 }
1458 }
1459 if (btf_ext_data) {
1460 if (!obj->btf) {
1461 pr_debug("Ignore ELF section %s because its depending ELF section %s is not found.\n",
1462 BTF_EXT_ELF_SEC, BTF_ELF_SEC);
1463 goto out;
1464 }
1465 obj->btf_ext = btf_ext__new(btf_ext_data->d_buf,
1466 btf_ext_data->d_size);
1467 if (IS_ERR(obj->btf_ext)) {
1468 pr_warning("Error loading ELF section %s: %ld. Ignored and continue.\n",
1469 BTF_EXT_ELF_SEC, PTR_ERR(obj->btf_ext));
1470 obj->btf_ext = NULL;
1471 goto out;
1472 }
1473 }
1474out:
1475 if (err || IS_ERR(obj->btf)) {
1476 if (btf_required)
1477 err = err ? : PTR_ERR(obj->btf);
1478 else
1479 err = 0;
1480 if (!IS_ERR_OR_NULL(obj->btf))
1481 btf__free(obj->btf);
1482 obj->btf = NULL;
1483 }
1484 if (btf_required && !obj->btf) {
1485 pr_warning("BTF is required, but is missing or corrupted.\n");
1486 return err == 0 ? -ENOENT : err;
1487 }
1488 return 0;
1489}
1490
1491static int bpf_object__sanitize_and_load_btf(struct bpf_object *obj)
1492{
1493 int err = 0;
1494
1495 if (!obj->btf)
1496 return 0;
1497
1498 bpf_object__sanitize_btf(obj);
1499 bpf_object__sanitize_btf_ext(obj);
1500
1501 err = btf__load(obj->btf);
1502 if (err) {
1503 pr_warning("Error loading %s into kernel: %d.\n",
1504 BTF_ELF_SEC, err);
1505 btf__free(obj->btf);
1506 obj->btf = NULL;
1507 /* btf_ext can't exist without btf, so free it as well */
1508 if (obj->btf_ext) {
1509 btf_ext__free(obj->btf_ext);
1510 obj->btf_ext = NULL;
1511 }
1512
1513 if (bpf_object__is_btf_mandatory(obj))
1514 return err;
1515 }
1516 return 0;
1517}
1518
1519static int bpf_object__elf_collect(struct bpf_object *obj, int flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001520{
1521 Elf *elf = obj->efile.elf;
1522 GElf_Ehdr *ep = &obj->efile.ehdr;
David Brazdil0f672f62019-12-10 10:32:29 +00001523 Elf_Data *btf_ext_data = NULL;
1524 Elf_Data *btf_data = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001525 Elf_Scn *scn = NULL;
1526 int idx = 0, err = 0;
1527
1528 /* Elf is corrupted/truncated, avoid calling elf_strptr. */
1529 if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) {
David Brazdil0f672f62019-12-10 10:32:29 +00001530 pr_warning("failed to get e_shstrndx from %s\n", obj->path);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001531 return -LIBBPF_ERRNO__FORMAT;
1532 }
1533
1534 while ((scn = elf_nextscn(elf, scn)) != NULL) {
1535 char *name;
1536 GElf_Shdr sh;
1537 Elf_Data *data;
1538
1539 idx++;
1540 if (gelf_getshdr(scn, &sh) != &sh) {
1541 pr_warning("failed to get section(%d) header from %s\n",
1542 idx, obj->path);
David Brazdil0f672f62019-12-10 10:32:29 +00001543 return -LIBBPF_ERRNO__FORMAT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001544 }
1545
1546 name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name);
1547 if (!name) {
1548 pr_warning("failed to get section(%d) name from %s\n",
1549 idx, obj->path);
David Brazdil0f672f62019-12-10 10:32:29 +00001550 return -LIBBPF_ERRNO__FORMAT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001551 }
1552
1553 data = elf_getdata(scn, 0);
1554 if (!data) {
1555 pr_warning("failed to get section(%d) data from %s(%s)\n",
1556 idx, name, obj->path);
David Brazdil0f672f62019-12-10 10:32:29 +00001557 return -LIBBPF_ERRNO__FORMAT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001558 }
1559 pr_debug("section(%d) %s, size %ld, link %d, flags %lx, type=%d\n",
1560 idx, name, (unsigned long)data->d_size,
1561 (int)sh.sh_link, (unsigned long)sh.sh_flags,
1562 (int)sh.sh_type);
1563
David Brazdil0f672f62019-12-10 10:32:29 +00001564 if (strcmp(name, "license") == 0) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001565 err = bpf_object__init_license(obj,
1566 data->d_buf,
1567 data->d_size);
David Brazdil0f672f62019-12-10 10:32:29 +00001568 if (err)
1569 return err;
1570 } else if (strcmp(name, "version") == 0) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001571 err = bpf_object__init_kversion(obj,
1572 data->d_buf,
1573 data->d_size);
David Brazdil0f672f62019-12-10 10:32:29 +00001574 if (err)
1575 return err;
1576 } else if (strcmp(name, "maps") == 0) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001577 obj->efile.maps_shndx = idx;
David Brazdil0f672f62019-12-10 10:32:29 +00001578 } else if (strcmp(name, MAPS_ELF_SEC) == 0) {
1579 obj->efile.btf_maps_shndx = idx;
1580 } else if (strcmp(name, BTF_ELF_SEC) == 0) {
1581 btf_data = data;
1582 } else if (strcmp(name, BTF_EXT_ELF_SEC) == 0) {
1583 btf_ext_data = data;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001584 } else if (sh.sh_type == SHT_SYMTAB) {
1585 if (obj->efile.symbols) {
1586 pr_warning("bpf: multiple SYMTAB in %s\n",
1587 obj->path);
David Brazdil0f672f62019-12-10 10:32:29 +00001588 return -LIBBPF_ERRNO__FORMAT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001589 }
David Brazdil0f672f62019-12-10 10:32:29 +00001590 obj->efile.symbols = data;
1591 obj->efile.strtabidx = sh.sh_link;
1592 } else if (sh.sh_type == SHT_PROGBITS && data->d_size > 0) {
1593 if (sh.sh_flags & SHF_EXECINSTR) {
1594 if (strcmp(name, ".text") == 0)
1595 obj->efile.text_shndx = idx;
1596 err = bpf_object__add_program(obj, data->d_buf,
1597 data->d_size, name, idx);
1598 if (err) {
1599 char errmsg[STRERR_BUFSIZE];
1600 char *cp = libbpf_strerror_r(-err, errmsg,
1601 sizeof(errmsg));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001602
David Brazdil0f672f62019-12-10 10:32:29 +00001603 pr_warning("failed to alloc program %s (%s): %s",
1604 name, obj->path, cp);
1605 return err;
1606 }
1607 } else if (strcmp(name, ".data") == 0) {
1608 obj->efile.data = data;
1609 obj->efile.data_shndx = idx;
1610 } else if (strcmp(name, ".rodata") == 0) {
1611 obj->efile.rodata = data;
1612 obj->efile.rodata_shndx = idx;
1613 } else {
1614 pr_debug("skip section(%d) %s\n", idx, name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001615 }
1616 } else if (sh.sh_type == SHT_REL) {
David Brazdil0f672f62019-12-10 10:32:29 +00001617 int nr_reloc = obj->efile.nr_reloc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001618 void *reloc = obj->efile.reloc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001619 int sec = sh.sh_info; /* points to other section */
1620
1621 /* Only do relo for section with exec instructions */
1622 if (!section_have_execinstr(obj, sec)) {
1623 pr_debug("skip relo %s(%d) for section(%d)\n",
1624 name, idx, sec);
1625 continue;
1626 }
1627
David Brazdil0f672f62019-12-10 10:32:29 +00001628 reloc = reallocarray(reloc, nr_reloc + 1,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001629 sizeof(*obj->efile.reloc));
1630 if (!reloc) {
1631 pr_warning("realloc failed\n");
David Brazdil0f672f62019-12-10 10:32:29 +00001632 return -ENOMEM;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001633 }
David Brazdil0f672f62019-12-10 10:32:29 +00001634
1635 obj->efile.reloc = reloc;
1636 obj->efile.nr_reloc++;
1637
1638 obj->efile.reloc[nr_reloc].shdr = sh;
1639 obj->efile.reloc[nr_reloc].data = data;
1640 } else if (sh.sh_type == SHT_NOBITS && strcmp(name, ".bss") == 0) {
1641 obj->efile.bss = data;
1642 obj->efile.bss_shndx = idx;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001643 } else {
1644 pr_debug("skip section(%d) %s\n", idx, name);
1645 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001646 }
1647
1648 if (!obj->efile.strtabidx || obj->efile.strtabidx >= idx) {
1649 pr_warning("Corrupted ELF file: index of strtab invalid\n");
David Brazdil0f672f62019-12-10 10:32:29 +00001650 return -LIBBPF_ERRNO__FORMAT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001651 }
David Brazdil0f672f62019-12-10 10:32:29 +00001652 err = bpf_object__init_btf(obj, btf_data, btf_ext_data);
1653 if (!err)
1654 err = bpf_object__init_maps(obj, flags);
1655 if (!err)
1656 err = bpf_object__sanitize_and_load_btf(obj);
1657 if (!err)
1658 err = bpf_object__init_prog_names(obj);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001659 return err;
1660}
1661
1662static struct bpf_program *
1663bpf_object__find_prog_by_idx(struct bpf_object *obj, int idx)
1664{
1665 struct bpf_program *prog;
1666 size_t i;
1667
1668 for (i = 0; i < obj->nr_programs; i++) {
1669 prog = &obj->programs[i];
1670 if (prog->idx == idx)
1671 return prog;
1672 }
1673 return NULL;
1674}
1675
1676struct bpf_program *
David Brazdil0f672f62019-12-10 10:32:29 +00001677bpf_object__find_program_by_title(const struct bpf_object *obj,
1678 const char *title)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001679{
1680 struct bpf_program *pos;
1681
1682 bpf_object__for_each_program(pos, obj) {
1683 if (pos->section_name && !strcmp(pos->section_name, title))
1684 return pos;
1685 }
1686 return NULL;
1687}
1688
David Brazdil0f672f62019-12-10 10:32:29 +00001689static bool bpf_object__shndx_is_data(const struct bpf_object *obj,
1690 int shndx)
1691{
1692 return shndx == obj->efile.data_shndx ||
1693 shndx == obj->efile.bss_shndx ||
1694 shndx == obj->efile.rodata_shndx;
1695}
1696
1697static bool bpf_object__shndx_is_maps(const struct bpf_object *obj,
1698 int shndx)
1699{
1700 return shndx == obj->efile.maps_shndx ||
1701 shndx == obj->efile.btf_maps_shndx;
1702}
1703
1704static bool bpf_object__relo_in_known_section(const struct bpf_object *obj,
1705 int shndx)
1706{
1707 return shndx == obj->efile.text_shndx ||
1708 bpf_object__shndx_is_maps(obj, shndx) ||
1709 bpf_object__shndx_is_data(obj, shndx);
1710}
1711
1712static enum libbpf_map_type
1713bpf_object__section_to_libbpf_map_type(const struct bpf_object *obj, int shndx)
1714{
1715 if (shndx == obj->efile.data_shndx)
1716 return LIBBPF_MAP_DATA;
1717 else if (shndx == obj->efile.bss_shndx)
1718 return LIBBPF_MAP_BSS;
1719 else if (shndx == obj->efile.rodata_shndx)
1720 return LIBBPF_MAP_RODATA;
1721 else
1722 return LIBBPF_MAP_UNSPEC;
1723}
1724
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001725static int
1726bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr,
1727 Elf_Data *data, struct bpf_object *obj)
1728{
1729 Elf_Data *symbols = obj->efile.symbols;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001730 struct bpf_map *maps = obj->maps;
1731 size_t nr_maps = obj->nr_maps;
1732 int i, nrels;
1733
David Brazdil0f672f62019-12-10 10:32:29 +00001734 pr_debug("collecting relocating info for: '%s'\n", prog->section_name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001735 nrels = shdr->sh_size / shdr->sh_entsize;
1736
1737 prog->reloc_desc = malloc(sizeof(*prog->reloc_desc) * nrels);
1738 if (!prog->reloc_desc) {
1739 pr_warning("failed to alloc memory in relocation\n");
1740 return -ENOMEM;
1741 }
1742 prog->nr_reloc = nrels;
1743
1744 for (i = 0; i < nrels; i++) {
David Brazdil0f672f62019-12-10 10:32:29 +00001745 struct bpf_insn *insns = prog->insns;
1746 enum libbpf_map_type type;
1747 unsigned int insn_idx;
1748 unsigned int shdr_idx;
1749 const char *name;
1750 size_t map_idx;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001751 GElf_Sym sym;
1752 GElf_Rel rel;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001753
1754 if (!gelf_getrel(data, i, &rel)) {
1755 pr_warning("relocation: failed to get %d reloc\n", i);
1756 return -LIBBPF_ERRNO__FORMAT;
1757 }
1758
David Brazdil0f672f62019-12-10 10:32:29 +00001759 if (!gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &sym)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001760 pr_warning("relocation: symbol %"PRIx64" not found\n",
1761 GELF_R_SYM(rel.r_info));
1762 return -LIBBPF_ERRNO__FORMAT;
1763 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001764
David Brazdil0f672f62019-12-10 10:32:29 +00001765 name = elf_strptr(obj->efile.elf, obj->efile.strtabidx,
1766 sym.st_name) ? : "<?>";
1767
1768 pr_debug("relo for %lld value %lld name %d (\'%s\')\n",
1769 (long long) (rel.r_info >> 32),
1770 (long long) sym.st_value, sym.st_name, name);
1771
1772 shdr_idx = sym.st_shndx;
1773 insn_idx = rel.r_offset / sizeof(struct bpf_insn);
1774 pr_debug("relocation: insn_idx=%u, shdr_idx=%u\n",
1775 insn_idx, shdr_idx);
1776
1777 if (shdr_idx >= SHN_LORESERVE) {
1778 pr_warning("relocation: not yet supported relo for non-static global \'%s\' variable in special section (0x%x) found in insns[%d].code 0x%x\n",
1779 name, shdr_idx, insn_idx,
1780 insns[insn_idx].code);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001781 return -LIBBPF_ERRNO__RELOC;
1782 }
David Brazdil0f672f62019-12-10 10:32:29 +00001783 if (!bpf_object__relo_in_known_section(obj, shdr_idx)) {
1784 pr_warning("Program '%s' contains unrecognized relo data pointing to section %u\n",
1785 prog->section_name, shdr_idx);
1786 return -LIBBPF_ERRNO__RELOC;
1787 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001788
1789 if (insns[insn_idx].code == (BPF_JMP | BPF_CALL)) {
1790 if (insns[insn_idx].src_reg != BPF_PSEUDO_CALL) {
1791 pr_warning("incorrect bpf_call opcode\n");
1792 return -LIBBPF_ERRNO__RELOC;
1793 }
1794 prog->reloc_desc[i].type = RELO_CALL;
1795 prog->reloc_desc[i].insn_idx = insn_idx;
1796 prog->reloc_desc[i].text_off = sym.st_value;
1797 obj->has_pseudo_calls = true;
1798 continue;
1799 }
1800
1801 if (insns[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
1802 pr_warning("bpf: relocation: invalid relo for insns[%d].code 0x%x\n",
1803 insn_idx, insns[insn_idx].code);
1804 return -LIBBPF_ERRNO__RELOC;
1805 }
1806
David Brazdil0f672f62019-12-10 10:32:29 +00001807 if (bpf_object__shndx_is_maps(obj, shdr_idx) ||
1808 bpf_object__shndx_is_data(obj, shdr_idx)) {
1809 type = bpf_object__section_to_libbpf_map_type(obj, shdr_idx);
1810 if (type != LIBBPF_MAP_UNSPEC) {
1811 if (GELF_ST_BIND(sym.st_info) == STB_GLOBAL) {
1812 pr_warning("bpf: relocation: not yet supported relo for non-static global \'%s\' variable found in insns[%d].code 0x%x\n",
1813 name, insn_idx, insns[insn_idx].code);
1814 return -LIBBPF_ERRNO__RELOC;
1815 }
1816 if (!obj->caps.global_data) {
1817 pr_warning("bpf: relocation: kernel does not support global \'%s\' variable access in insns[%d]\n",
1818 name, insn_idx);
1819 return -LIBBPF_ERRNO__RELOC;
1820 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001821 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001822
David Brazdil0f672f62019-12-10 10:32:29 +00001823 for (map_idx = 0; map_idx < nr_maps; map_idx++) {
1824 if (maps[map_idx].libbpf_type != type)
1825 continue;
1826 if (type != LIBBPF_MAP_UNSPEC ||
1827 (maps[map_idx].sec_idx == sym.st_shndx &&
1828 maps[map_idx].sec_offset == sym.st_value)) {
1829 pr_debug("relocation: found map %zd (%s, sec_idx %d, offset %zu) for insn %u\n",
1830 map_idx, maps[map_idx].name,
1831 maps[map_idx].sec_idx,
1832 maps[map_idx].sec_offset,
1833 insn_idx);
1834 break;
1835 }
1836 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001837
David Brazdil0f672f62019-12-10 10:32:29 +00001838 if (map_idx >= nr_maps) {
1839 pr_warning("bpf relocation: map_idx %d larger than %d\n",
1840 (int)map_idx, (int)nr_maps - 1);
1841 return -LIBBPF_ERRNO__RELOC;
1842 }
1843
1844 prog->reloc_desc[i].type = type != LIBBPF_MAP_UNSPEC ?
1845 RELO_DATA : RELO_LD64;
1846 prog->reloc_desc[i].insn_idx = insn_idx;
1847 prog->reloc_desc[i].map_idx = map_idx;
1848 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001849 }
1850 return 0;
1851}
1852
David Brazdil0f672f62019-12-10 10:32:29 +00001853static int bpf_map_find_btf_info(struct bpf_object *obj, struct bpf_map *map)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001854{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001855 struct bpf_map_def *def = &map->def;
David Brazdil0f672f62019-12-10 10:32:29 +00001856 __u32 key_type_id = 0, value_type_id = 0;
1857 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001858
David Brazdil0f672f62019-12-10 10:32:29 +00001859 /* if it's BTF-defined map, we don't need to search for type IDs */
1860 if (map->sec_idx == obj->efile.btf_maps_shndx)
1861 return 0;
1862
1863 if (!bpf_map__is_internal(map)) {
1864 ret = btf__get_map_kv_tids(obj->btf, map->name, def->key_size,
1865 def->value_size, &key_type_id,
1866 &value_type_id);
1867 } else {
1868 /*
1869 * LLVM annotates global data differently in BTF, that is,
1870 * only as '.data', '.bss' or '.rodata'.
1871 */
1872 ret = btf__find_by_name(obj->btf,
1873 libbpf_type_to_btf_name[map->libbpf_type]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001874 }
David Brazdil0f672f62019-12-10 10:32:29 +00001875 if (ret < 0)
1876 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001877
David Brazdil0f672f62019-12-10 10:32:29 +00001878 map->btf_key_type_id = key_type_id;
1879 map->btf_value_type_id = bpf_map__is_internal(map) ?
1880 ret : value_type_id;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001881 return 0;
1882}
1883
1884int bpf_map__reuse_fd(struct bpf_map *map, int fd)
1885{
1886 struct bpf_map_info info = {};
1887 __u32 len = sizeof(info);
1888 int new_fd, err;
1889 char *new_name;
1890
1891 err = bpf_obj_get_info_by_fd(fd, &info, &len);
1892 if (err)
1893 return err;
1894
1895 new_name = strdup(info.name);
1896 if (!new_name)
1897 return -errno;
1898
1899 new_fd = open("/", O_RDONLY | O_CLOEXEC);
Olivier Deprez0e641232021-09-23 10:07:05 +02001900 if (new_fd < 0) {
1901 err = -errno;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001902 goto err_free_new_name;
Olivier Deprez0e641232021-09-23 10:07:05 +02001903 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001904
1905 new_fd = dup3(fd, new_fd, O_CLOEXEC);
Olivier Deprez0e641232021-09-23 10:07:05 +02001906 if (new_fd < 0) {
1907 err = -errno;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001908 goto err_close_new_fd;
Olivier Deprez0e641232021-09-23 10:07:05 +02001909 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001910
1911 err = zclose(map->fd);
Olivier Deprez0e641232021-09-23 10:07:05 +02001912 if (err) {
1913 err = -errno;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001914 goto err_close_new_fd;
Olivier Deprez0e641232021-09-23 10:07:05 +02001915 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001916 free(map->name);
1917
1918 map->fd = new_fd;
1919 map->name = new_name;
1920 map->def.type = info.type;
1921 map->def.key_size = info.key_size;
1922 map->def.value_size = info.value_size;
1923 map->def.max_entries = info.max_entries;
1924 map->def.map_flags = info.map_flags;
1925 map->btf_key_type_id = info.btf_key_type_id;
1926 map->btf_value_type_id = info.btf_value_type_id;
1927
1928 return 0;
1929
1930err_close_new_fd:
1931 close(new_fd);
1932err_free_new_name:
1933 free(new_name);
Olivier Deprez0e641232021-09-23 10:07:05 +02001934 return err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001935}
1936
David Brazdil0f672f62019-12-10 10:32:29 +00001937int bpf_map__resize(struct bpf_map *map, __u32 max_entries)
1938{
1939 if (!map || !max_entries)
1940 return -EINVAL;
1941
1942 /* If map already created, its attributes can't be changed. */
1943 if (map->fd >= 0)
1944 return -EBUSY;
1945
1946 map->def.max_entries = max_entries;
1947
1948 return 0;
1949}
1950
1951static int
1952bpf_object__probe_name(struct bpf_object *obj)
1953{
1954 struct bpf_load_program_attr attr;
1955 char *cp, errmsg[STRERR_BUFSIZE];
1956 struct bpf_insn insns[] = {
1957 BPF_MOV64_IMM(BPF_REG_0, 0),
1958 BPF_EXIT_INSN(),
1959 };
1960 int ret;
1961
1962 /* make sure basic loading works */
1963
1964 memset(&attr, 0, sizeof(attr));
1965 attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
1966 attr.insns = insns;
1967 attr.insns_cnt = ARRAY_SIZE(insns);
1968 attr.license = "GPL";
1969
1970 ret = bpf_load_program_xattr(&attr, NULL, 0);
1971 if (ret < 0) {
1972 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
1973 pr_warning("Error in %s():%s(%d). Couldn't load basic 'r0 = 0' BPF program.\n",
1974 __func__, cp, errno);
1975 return -errno;
1976 }
1977 close(ret);
1978
1979 /* now try the same program, but with the name */
1980
1981 attr.name = "test";
1982 ret = bpf_load_program_xattr(&attr, NULL, 0);
1983 if (ret >= 0) {
1984 obj->caps.name = 1;
1985 close(ret);
1986 }
1987
1988 return 0;
1989}
1990
1991static int
1992bpf_object__probe_global_data(struct bpf_object *obj)
1993{
1994 struct bpf_load_program_attr prg_attr;
1995 struct bpf_create_map_attr map_attr;
1996 char *cp, errmsg[STRERR_BUFSIZE];
1997 struct bpf_insn insns[] = {
1998 BPF_LD_MAP_VALUE(BPF_REG_1, 0, 16),
1999 BPF_ST_MEM(BPF_DW, BPF_REG_1, 0, 42),
2000 BPF_MOV64_IMM(BPF_REG_0, 0),
2001 BPF_EXIT_INSN(),
2002 };
2003 int ret, map;
2004
2005 memset(&map_attr, 0, sizeof(map_attr));
2006 map_attr.map_type = BPF_MAP_TYPE_ARRAY;
2007 map_attr.key_size = sizeof(int);
2008 map_attr.value_size = 32;
2009 map_attr.max_entries = 1;
2010
2011 map = bpf_create_map_xattr(&map_attr);
2012 if (map < 0) {
2013 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
2014 pr_warning("Error in %s():%s(%d). Couldn't create simple array map.\n",
2015 __func__, cp, errno);
2016 return -errno;
2017 }
2018
2019 insns[0].imm = map;
2020
2021 memset(&prg_attr, 0, sizeof(prg_attr));
2022 prg_attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
2023 prg_attr.insns = insns;
2024 prg_attr.insns_cnt = ARRAY_SIZE(insns);
2025 prg_attr.license = "GPL";
2026
2027 ret = bpf_load_program_xattr(&prg_attr, NULL, 0);
2028 if (ret >= 0) {
2029 obj->caps.global_data = 1;
2030 close(ret);
2031 }
2032
2033 close(map);
2034 return 0;
2035}
2036
2037static int bpf_object__probe_btf_func(struct bpf_object *obj)
2038{
2039 const char strs[] = "\0int\0x\0a";
2040 /* void x(int a) {} */
2041 __u32 types[] = {
2042 /* int */
2043 BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2044 /* FUNC_PROTO */ /* [2] */
2045 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 1), 0),
2046 BTF_PARAM_ENC(7, 1),
2047 /* FUNC x */ /* [3] */
2048 BTF_TYPE_ENC(5, BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0), 2),
2049 };
2050 int btf_fd;
2051
2052 btf_fd = libbpf__load_raw_btf((char *)types, sizeof(types),
2053 strs, sizeof(strs));
2054 if (btf_fd >= 0) {
2055 obj->caps.btf_func = 1;
2056 close(btf_fd);
2057 return 1;
2058 }
2059
2060 return 0;
2061}
2062
2063static int bpf_object__probe_btf_datasec(struct bpf_object *obj)
2064{
2065 const char strs[] = "\0x\0.data";
2066 /* static int a; */
2067 __u32 types[] = {
2068 /* int */
2069 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2070 /* VAR x */ /* [2] */
2071 BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), 1),
2072 BTF_VAR_STATIC,
2073 /* DATASEC val */ /* [3] */
2074 BTF_TYPE_ENC(3, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
2075 BTF_VAR_SECINFO_ENC(2, 0, 4),
2076 };
2077 int btf_fd;
2078
2079 btf_fd = libbpf__load_raw_btf((char *)types, sizeof(types),
2080 strs, sizeof(strs));
2081 if (btf_fd >= 0) {
2082 obj->caps.btf_datasec = 1;
2083 close(btf_fd);
2084 return 1;
2085 }
2086
2087 return 0;
2088}
2089
2090static int
2091bpf_object__probe_caps(struct bpf_object *obj)
2092{
2093 int (*probe_fn[])(struct bpf_object *obj) = {
2094 bpf_object__probe_name,
2095 bpf_object__probe_global_data,
2096 bpf_object__probe_btf_func,
2097 bpf_object__probe_btf_datasec,
2098 };
2099 int i, ret;
2100
2101 for (i = 0; i < ARRAY_SIZE(probe_fn); i++) {
2102 ret = probe_fn[i](obj);
2103 if (ret < 0)
2104 pr_debug("Probe #%d failed with %d.\n", i, ret);
2105 }
2106
2107 return 0;
2108}
2109
2110static int
2111bpf_object__populate_internal_map(struct bpf_object *obj, struct bpf_map *map)
2112{
2113 char *cp, errmsg[STRERR_BUFSIZE];
2114 int err, zero = 0;
2115 __u8 *data;
2116
2117 /* Nothing to do here since kernel already zero-initializes .bss map. */
2118 if (map->libbpf_type == LIBBPF_MAP_BSS)
2119 return 0;
2120
2121 data = map->libbpf_type == LIBBPF_MAP_DATA ?
2122 obj->sections.data : obj->sections.rodata;
2123
2124 err = bpf_map_update_elem(map->fd, &zero, data, 0);
2125 /* Freeze .rodata map as read-only from syscall side. */
2126 if (!err && map->libbpf_type == LIBBPF_MAP_RODATA) {
2127 err = bpf_map_freeze(map->fd);
2128 if (err) {
2129 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
2130 pr_warning("Error freezing map(%s) as read-only: %s\n",
2131 map->name, cp);
2132 err = 0;
2133 }
2134 }
2135 return err;
2136}
2137
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002138static int
2139bpf_object__create_maps(struct bpf_object *obj)
2140{
2141 struct bpf_create_map_attr create_attr = {};
David Brazdil0f672f62019-12-10 10:32:29 +00002142 int nr_cpus = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002143 unsigned int i;
2144 int err;
2145
2146 for (i = 0; i < obj->nr_maps; i++) {
2147 struct bpf_map *map = &obj->maps[i];
2148 struct bpf_map_def *def = &map->def;
2149 char *cp, errmsg[STRERR_BUFSIZE];
2150 int *pfd = &map->fd;
2151
2152 if (map->fd >= 0) {
2153 pr_debug("skip map create (preset) %s: fd=%d\n",
2154 map->name, map->fd);
2155 continue;
2156 }
2157
David Brazdil0f672f62019-12-10 10:32:29 +00002158 if (obj->caps.name)
2159 create_attr.name = map->name;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002160 create_attr.map_ifindex = map->map_ifindex;
2161 create_attr.map_type = def->type;
2162 create_attr.map_flags = def->map_flags;
2163 create_attr.key_size = def->key_size;
2164 create_attr.value_size = def->value_size;
David Brazdil0f672f62019-12-10 10:32:29 +00002165 if (def->type == BPF_MAP_TYPE_PERF_EVENT_ARRAY &&
2166 !def->max_entries) {
2167 if (!nr_cpus)
2168 nr_cpus = libbpf_num_possible_cpus();
2169 if (nr_cpus < 0) {
2170 pr_warning("failed to determine number of system CPUs: %d\n",
2171 nr_cpus);
2172 err = nr_cpus;
2173 goto err_out;
2174 }
2175 pr_debug("map '%s': setting size to %d\n",
2176 map->name, nr_cpus);
2177 create_attr.max_entries = nr_cpus;
2178 } else {
2179 create_attr.max_entries = def->max_entries;
2180 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002181 create_attr.btf_fd = 0;
2182 create_attr.btf_key_type_id = 0;
2183 create_attr.btf_value_type_id = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00002184 if (bpf_map_type__is_map_in_map(def->type) &&
2185 map->inner_map_fd >= 0)
2186 create_attr.inner_map_fd = map->inner_map_fd;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002187
David Brazdil0f672f62019-12-10 10:32:29 +00002188 if (obj->btf && !bpf_map_find_btf_info(obj, map)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002189 create_attr.btf_fd = btf__fd(obj->btf);
2190 create_attr.btf_key_type_id = map->btf_key_type_id;
2191 create_attr.btf_value_type_id = map->btf_value_type_id;
2192 }
2193
2194 *pfd = bpf_create_map_xattr(&create_attr);
David Brazdil0f672f62019-12-10 10:32:29 +00002195 if (*pfd < 0 && (create_attr.btf_key_type_id ||
2196 create_attr.btf_value_type_id)) {
2197 err = -errno;
2198 cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002199 pr_warning("Error in bpf_create_map_xattr(%s):%s(%d). Retrying without BTF.\n",
David Brazdil0f672f62019-12-10 10:32:29 +00002200 map->name, cp, err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002201 create_attr.btf_fd = 0;
2202 create_attr.btf_key_type_id = 0;
2203 create_attr.btf_value_type_id = 0;
2204 map->btf_key_type_id = 0;
2205 map->btf_value_type_id = 0;
2206 *pfd = bpf_create_map_xattr(&create_attr);
2207 }
2208
2209 if (*pfd < 0) {
2210 size_t j;
2211
David Brazdil0f672f62019-12-10 10:32:29 +00002212 err = -errno;
2213err_out:
2214 cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
2215 pr_warning("failed to create map (name: '%s'): %s(%d)\n",
2216 map->name, cp, err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002217 for (j = 0; j < i; j++)
2218 zclose(obj->maps[j].fd);
2219 return err;
2220 }
David Brazdil0f672f62019-12-10 10:32:29 +00002221
2222 if (bpf_map__is_internal(map)) {
2223 err = bpf_object__populate_internal_map(obj, map);
2224 if (err < 0) {
2225 zclose(*pfd);
2226 goto err_out;
2227 }
2228 }
2229
2230 pr_debug("created map %s: fd=%d\n", map->name, *pfd);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002231 }
2232
2233 return 0;
2234}
2235
2236static int
David Brazdil0f672f62019-12-10 10:32:29 +00002237check_btf_ext_reloc_err(struct bpf_program *prog, int err,
2238 void *btf_prog_info, const char *info_name)
2239{
2240 if (err != -ENOENT) {
2241 pr_warning("Error in loading %s for sec %s.\n",
2242 info_name, prog->section_name);
2243 return err;
2244 }
2245
2246 /* err == -ENOENT (i.e. prog->section_name not found in btf_ext) */
2247
2248 if (btf_prog_info) {
2249 /*
2250 * Some info has already been found but has problem
2251 * in the last btf_ext reloc. Must have to error out.
2252 */
2253 pr_warning("Error in relocating %s for sec %s.\n",
2254 info_name, prog->section_name);
2255 return err;
2256 }
2257
2258 /* Have problem loading the very first info. Ignore the rest. */
2259 pr_warning("Cannot find %s for main program sec %s. Ignore all %s.\n",
2260 info_name, prog->section_name, info_name);
2261 return 0;
2262}
2263
2264static int
2265bpf_program_reloc_btf_ext(struct bpf_program *prog, struct bpf_object *obj,
2266 const char *section_name, __u32 insn_offset)
2267{
2268 int err;
2269
2270 if (!insn_offset || prog->func_info) {
2271 /*
2272 * !insn_offset => main program
2273 *
2274 * For sub prog, the main program's func_info has to
2275 * be loaded first (i.e. prog->func_info != NULL)
2276 */
2277 err = btf_ext__reloc_func_info(obj->btf, obj->btf_ext,
2278 section_name, insn_offset,
2279 &prog->func_info,
2280 &prog->func_info_cnt);
2281 if (err)
2282 return check_btf_ext_reloc_err(prog, err,
2283 prog->func_info,
2284 "bpf_func_info");
2285
2286 prog->func_info_rec_size = btf_ext__func_info_rec_size(obj->btf_ext);
2287 }
2288
2289 if (!insn_offset || prog->line_info) {
2290 err = btf_ext__reloc_line_info(obj->btf, obj->btf_ext,
2291 section_name, insn_offset,
2292 &prog->line_info,
2293 &prog->line_info_cnt);
2294 if (err)
2295 return check_btf_ext_reloc_err(prog, err,
2296 prog->line_info,
2297 "bpf_line_info");
2298
2299 prog->line_info_rec_size = btf_ext__line_info_rec_size(obj->btf_ext);
2300 }
2301
2302 return 0;
2303}
2304
2305#define BPF_CORE_SPEC_MAX_LEN 64
2306
2307/* represents BPF CO-RE field or array element accessor */
2308struct bpf_core_accessor {
2309 __u32 type_id; /* struct/union type or array element type */
2310 __u32 idx; /* field index or array index */
2311 const char *name; /* field name or NULL for array accessor */
2312};
2313
2314struct bpf_core_spec {
2315 const struct btf *btf;
2316 /* high-level spec: named fields and array indices only */
2317 struct bpf_core_accessor spec[BPF_CORE_SPEC_MAX_LEN];
2318 /* high-level spec length */
2319 int len;
2320 /* raw, low-level spec: 1-to-1 with accessor spec string */
2321 int raw_spec[BPF_CORE_SPEC_MAX_LEN];
2322 /* raw spec length */
2323 int raw_len;
2324 /* field byte offset represented by spec */
2325 __u32 offset;
2326};
2327
2328static bool str_is_empty(const char *s)
2329{
2330 return !s || !s[0];
2331}
2332
2333/*
2334 * Turn bpf_offset_reloc into a low- and high-level spec representation,
2335 * validating correctness along the way, as well as calculating resulting
2336 * field offset (in bytes), specified by accessor string. Low-level spec
2337 * captures every single level of nestedness, including traversing anonymous
2338 * struct/union members. High-level one only captures semantically meaningful
2339 * "turning points": named fields and array indicies.
2340 * E.g., for this case:
2341 *
2342 * struct sample {
2343 * int __unimportant;
2344 * struct {
2345 * int __1;
2346 * int __2;
2347 * int a[7];
2348 * };
2349 * };
2350 *
2351 * struct sample *s = ...;
2352 *
2353 * int x = &s->a[3]; // access string = '0:1:2:3'
2354 *
2355 * Low-level spec has 1:1 mapping with each element of access string (it's
2356 * just a parsed access string representation): [0, 1, 2, 3].
2357 *
2358 * High-level spec will capture only 3 points:
2359 * - intial zero-index access by pointer (&s->... is the same as &s[0]...);
2360 * - field 'a' access (corresponds to '2' in low-level spec);
2361 * - array element #3 access (corresponds to '3' in low-level spec).
2362 *
2363 */
2364static int bpf_core_spec_parse(const struct btf *btf,
2365 __u32 type_id,
2366 const char *spec_str,
2367 struct bpf_core_spec *spec)
2368{
2369 int access_idx, parsed_len, i;
2370 const struct btf_type *t;
2371 const char *name;
2372 __u32 id;
2373 __s64 sz;
2374
2375 if (str_is_empty(spec_str) || *spec_str == ':')
2376 return -EINVAL;
2377
2378 memset(spec, 0, sizeof(*spec));
2379 spec->btf = btf;
2380
2381 /* parse spec_str="0:1:2:3:4" into array raw_spec=[0, 1, 2, 3, 4] */
2382 while (*spec_str) {
2383 if (*spec_str == ':')
2384 ++spec_str;
2385 if (sscanf(spec_str, "%d%n", &access_idx, &parsed_len) != 1)
2386 return -EINVAL;
2387 if (spec->raw_len == BPF_CORE_SPEC_MAX_LEN)
2388 return -E2BIG;
2389 spec_str += parsed_len;
2390 spec->raw_spec[spec->raw_len++] = access_idx;
2391 }
2392
2393 if (spec->raw_len == 0)
2394 return -EINVAL;
2395
2396 /* first spec value is always reloc type array index */
2397 t = skip_mods_and_typedefs(btf, type_id, &id);
2398 if (!t)
2399 return -EINVAL;
2400
2401 access_idx = spec->raw_spec[0];
2402 spec->spec[0].type_id = id;
2403 spec->spec[0].idx = access_idx;
2404 spec->len++;
2405
2406 sz = btf__resolve_size(btf, id);
2407 if (sz < 0)
2408 return sz;
2409 spec->offset = access_idx * sz;
2410
2411 for (i = 1; i < spec->raw_len; i++) {
2412 t = skip_mods_and_typedefs(btf, id, &id);
2413 if (!t)
2414 return -EINVAL;
2415
2416 access_idx = spec->raw_spec[i];
2417
2418 if (btf_is_composite(t)) {
2419 const struct btf_member *m;
2420 __u32 offset;
2421
2422 if (access_idx >= btf_vlen(t))
2423 return -EINVAL;
2424 if (btf_member_bitfield_size(t, access_idx))
2425 return -EINVAL;
2426
2427 offset = btf_member_bit_offset(t, access_idx);
2428 if (offset % 8)
2429 return -EINVAL;
2430 spec->offset += offset / 8;
2431
2432 m = btf_members(t) + access_idx;
2433 if (m->name_off) {
2434 name = btf__name_by_offset(btf, m->name_off);
2435 if (str_is_empty(name))
2436 return -EINVAL;
2437
2438 spec->spec[spec->len].type_id = id;
2439 spec->spec[spec->len].idx = access_idx;
2440 spec->spec[spec->len].name = name;
2441 spec->len++;
2442 }
2443
2444 id = m->type;
2445 } else if (btf_is_array(t)) {
2446 const struct btf_array *a = btf_array(t);
2447
2448 t = skip_mods_and_typedefs(btf, a->type, &id);
2449 if (!t || access_idx >= a->nelems)
2450 return -EINVAL;
2451
2452 spec->spec[spec->len].type_id = id;
2453 spec->spec[spec->len].idx = access_idx;
2454 spec->len++;
2455
2456 sz = btf__resolve_size(btf, id);
2457 if (sz < 0)
2458 return sz;
2459 spec->offset += access_idx * sz;
2460 } else {
2461 pr_warning("relo for [%u] %s (at idx %d) captures type [%d] of unexpected kind %d\n",
2462 type_id, spec_str, i, id, btf_kind(t));
2463 return -EINVAL;
2464 }
2465 }
2466
2467 return 0;
2468}
2469
2470static bool bpf_core_is_flavor_sep(const char *s)
2471{
2472 /* check X___Y name pattern, where X and Y are not underscores */
2473 return s[0] != '_' && /* X */
2474 s[1] == '_' && s[2] == '_' && s[3] == '_' && /* ___ */
2475 s[4] != '_'; /* Y */
2476}
2477
2478/* Given 'some_struct_name___with_flavor' return the length of a name prefix
2479 * before last triple underscore. Struct name part after last triple
2480 * underscore is ignored by BPF CO-RE relocation during relocation matching.
2481 */
2482static size_t bpf_core_essential_name_len(const char *name)
2483{
2484 size_t n = strlen(name);
2485 int i;
2486
2487 for (i = n - 5; i >= 0; i--) {
2488 if (bpf_core_is_flavor_sep(name + i))
2489 return i + 1;
2490 }
2491 return n;
2492}
2493
2494/* dynamically sized list of type IDs */
2495struct ids_vec {
2496 __u32 *data;
2497 int len;
2498};
2499
2500static void bpf_core_free_cands(struct ids_vec *cand_ids)
2501{
2502 free(cand_ids->data);
2503 free(cand_ids);
2504}
2505
2506static struct ids_vec *bpf_core_find_cands(const struct btf *local_btf,
2507 __u32 local_type_id,
2508 const struct btf *targ_btf)
2509{
2510 size_t local_essent_len, targ_essent_len;
2511 const char *local_name, *targ_name;
2512 const struct btf_type *t;
2513 struct ids_vec *cand_ids;
2514 __u32 *new_ids;
2515 int i, err, n;
2516
2517 t = btf__type_by_id(local_btf, local_type_id);
2518 if (!t)
2519 return ERR_PTR(-EINVAL);
2520
2521 local_name = btf__name_by_offset(local_btf, t->name_off);
2522 if (str_is_empty(local_name))
2523 return ERR_PTR(-EINVAL);
2524 local_essent_len = bpf_core_essential_name_len(local_name);
2525
2526 cand_ids = calloc(1, sizeof(*cand_ids));
2527 if (!cand_ids)
2528 return ERR_PTR(-ENOMEM);
2529
2530 n = btf__get_nr_types(targ_btf);
2531 for (i = 1; i <= n; i++) {
2532 t = btf__type_by_id(targ_btf, i);
2533 targ_name = btf__name_by_offset(targ_btf, t->name_off);
2534 if (str_is_empty(targ_name))
2535 continue;
2536
2537 targ_essent_len = bpf_core_essential_name_len(targ_name);
2538 if (targ_essent_len != local_essent_len)
2539 continue;
2540
2541 if (strncmp(local_name, targ_name, local_essent_len) == 0) {
2542 pr_debug("[%d] %s: found candidate [%d] %s\n",
2543 local_type_id, local_name, i, targ_name);
Olivier Deprez0e641232021-09-23 10:07:05 +02002544 new_ids = reallocarray(cand_ids->data,
2545 cand_ids->len + 1,
2546 sizeof(*cand_ids->data));
David Brazdil0f672f62019-12-10 10:32:29 +00002547 if (!new_ids) {
2548 err = -ENOMEM;
2549 goto err_out;
2550 }
2551 cand_ids->data = new_ids;
2552 cand_ids->data[cand_ids->len++] = i;
2553 }
2554 }
2555 return cand_ids;
2556err_out:
2557 bpf_core_free_cands(cand_ids);
2558 return ERR_PTR(err);
2559}
2560
2561/* Check two types for compatibility, skipping const/volatile/restrict and
2562 * typedefs, to ensure we are relocating offset to the compatible entities:
2563 * - any two STRUCTs/UNIONs are compatible and can be mixed;
2564 * - any two FWDs are compatible;
2565 * - any two PTRs are always compatible;
2566 * - for ENUMs, check sizes, names are ignored;
2567 * - for INT, size and bitness should match, signedness is ignored;
2568 * - for ARRAY, dimensionality is ignored, element types are checked for
2569 * compatibility recursively;
2570 * - everything else shouldn't be ever a target of relocation.
2571 * These rules are not set in stone and probably will be adjusted as we get
2572 * more experience with using BPF CO-RE relocations.
2573 */
2574static int bpf_core_fields_are_compat(const struct btf *local_btf,
2575 __u32 local_id,
2576 const struct btf *targ_btf,
2577 __u32 targ_id)
2578{
2579 const struct btf_type *local_type, *targ_type;
2580
2581recur:
2582 local_type = skip_mods_and_typedefs(local_btf, local_id, &local_id);
2583 targ_type = skip_mods_and_typedefs(targ_btf, targ_id, &targ_id);
2584 if (!local_type || !targ_type)
2585 return -EINVAL;
2586
2587 if (btf_is_composite(local_type) && btf_is_composite(targ_type))
2588 return 1;
2589 if (btf_kind(local_type) != btf_kind(targ_type))
2590 return 0;
2591
2592 switch (btf_kind(local_type)) {
2593 case BTF_KIND_FWD:
2594 case BTF_KIND_PTR:
2595 return 1;
2596 case BTF_KIND_ENUM:
2597 return local_type->size == targ_type->size;
2598 case BTF_KIND_INT:
2599 return btf_int_offset(local_type) == 0 &&
2600 btf_int_offset(targ_type) == 0 &&
2601 local_type->size == targ_type->size &&
2602 btf_int_bits(local_type) == btf_int_bits(targ_type);
2603 case BTF_KIND_ARRAY:
2604 local_id = btf_array(local_type)->type;
2605 targ_id = btf_array(targ_type)->type;
2606 goto recur;
2607 default:
2608 pr_warning("unexpected kind %d relocated, local [%d], target [%d]\n",
2609 btf_kind(local_type), local_id, targ_id);
2610 return 0;
2611 }
2612}
2613
2614/*
2615 * Given single high-level named field accessor in local type, find
2616 * corresponding high-level accessor for a target type. Along the way,
2617 * maintain low-level spec for target as well. Also keep updating target
2618 * offset.
2619 *
2620 * Searching is performed through recursive exhaustive enumeration of all
2621 * fields of a struct/union. If there are any anonymous (embedded)
2622 * structs/unions, they are recursively searched as well. If field with
2623 * desired name is found, check compatibility between local and target types,
2624 * before returning result.
2625 *
2626 * 1 is returned, if field is found.
2627 * 0 is returned if no compatible field is found.
2628 * <0 is returned on error.
2629 */
2630static int bpf_core_match_member(const struct btf *local_btf,
2631 const struct bpf_core_accessor *local_acc,
2632 const struct btf *targ_btf,
2633 __u32 targ_id,
2634 struct bpf_core_spec *spec,
2635 __u32 *next_targ_id)
2636{
2637 const struct btf_type *local_type, *targ_type;
2638 const struct btf_member *local_member, *m;
2639 const char *local_name, *targ_name;
2640 __u32 local_id;
2641 int i, n, found;
2642
2643 targ_type = skip_mods_and_typedefs(targ_btf, targ_id, &targ_id);
2644 if (!targ_type)
2645 return -EINVAL;
2646 if (!btf_is_composite(targ_type))
2647 return 0;
2648
2649 local_id = local_acc->type_id;
2650 local_type = btf__type_by_id(local_btf, local_id);
2651 local_member = btf_members(local_type) + local_acc->idx;
2652 local_name = btf__name_by_offset(local_btf, local_member->name_off);
2653
2654 n = btf_vlen(targ_type);
2655 m = btf_members(targ_type);
2656 for (i = 0; i < n; i++, m++) {
2657 __u32 offset;
2658
2659 /* bitfield relocations not supported */
2660 if (btf_member_bitfield_size(targ_type, i))
2661 continue;
2662 offset = btf_member_bit_offset(targ_type, i);
2663 if (offset % 8)
2664 continue;
2665
2666 /* too deep struct/union/array nesting */
2667 if (spec->raw_len == BPF_CORE_SPEC_MAX_LEN)
2668 return -E2BIG;
2669
2670 /* speculate this member will be the good one */
2671 spec->offset += offset / 8;
2672 spec->raw_spec[spec->raw_len++] = i;
2673
2674 targ_name = btf__name_by_offset(targ_btf, m->name_off);
2675 if (str_is_empty(targ_name)) {
2676 /* embedded struct/union, we need to go deeper */
2677 found = bpf_core_match_member(local_btf, local_acc,
2678 targ_btf, m->type,
2679 spec, next_targ_id);
2680 if (found) /* either found or error */
2681 return found;
2682 } else if (strcmp(local_name, targ_name) == 0) {
2683 /* matching named field */
2684 struct bpf_core_accessor *targ_acc;
2685
2686 targ_acc = &spec->spec[spec->len++];
2687 targ_acc->type_id = targ_id;
2688 targ_acc->idx = i;
2689 targ_acc->name = targ_name;
2690
2691 *next_targ_id = m->type;
2692 found = bpf_core_fields_are_compat(local_btf,
2693 local_member->type,
2694 targ_btf, m->type);
2695 if (!found)
2696 spec->len--; /* pop accessor */
2697 return found;
2698 }
2699 /* member turned out not to be what we looked for */
2700 spec->offset -= offset / 8;
2701 spec->raw_len--;
2702 }
2703
2704 return 0;
2705}
2706
2707/*
2708 * Try to match local spec to a target type and, if successful, produce full
2709 * target spec (high-level, low-level + offset).
2710 */
2711static int bpf_core_spec_match(struct bpf_core_spec *local_spec,
2712 const struct btf *targ_btf, __u32 targ_id,
2713 struct bpf_core_spec *targ_spec)
2714{
2715 const struct btf_type *targ_type;
2716 const struct bpf_core_accessor *local_acc;
2717 struct bpf_core_accessor *targ_acc;
2718 int i, sz, matched;
2719
2720 memset(targ_spec, 0, sizeof(*targ_spec));
2721 targ_spec->btf = targ_btf;
2722
2723 local_acc = &local_spec->spec[0];
2724 targ_acc = &targ_spec->spec[0];
2725
2726 for (i = 0; i < local_spec->len; i++, local_acc++, targ_acc++) {
2727 targ_type = skip_mods_and_typedefs(targ_spec->btf, targ_id,
2728 &targ_id);
2729 if (!targ_type)
2730 return -EINVAL;
2731
2732 if (local_acc->name) {
2733 matched = bpf_core_match_member(local_spec->btf,
2734 local_acc,
2735 targ_btf, targ_id,
2736 targ_spec, &targ_id);
2737 if (matched <= 0)
2738 return matched;
2739 } else {
2740 /* for i=0, targ_id is already treated as array element
2741 * type (because it's the original struct), for others
2742 * we should find array element type first
2743 */
2744 if (i > 0) {
2745 const struct btf_array *a;
2746
2747 if (!btf_is_array(targ_type))
2748 return 0;
2749
2750 a = btf_array(targ_type);
2751 if (local_acc->idx >= a->nelems)
2752 return 0;
2753 if (!skip_mods_and_typedefs(targ_btf, a->type,
2754 &targ_id))
2755 return -EINVAL;
2756 }
2757
2758 /* too deep struct/union/array nesting */
2759 if (targ_spec->raw_len == BPF_CORE_SPEC_MAX_LEN)
2760 return -E2BIG;
2761
2762 targ_acc->type_id = targ_id;
2763 targ_acc->idx = local_acc->idx;
2764 targ_acc->name = NULL;
2765 targ_spec->len++;
2766 targ_spec->raw_spec[targ_spec->raw_len] = targ_acc->idx;
2767 targ_spec->raw_len++;
2768
2769 sz = btf__resolve_size(targ_btf, targ_id);
2770 if (sz < 0)
2771 return sz;
2772 targ_spec->offset += local_acc->idx * sz;
2773 }
2774 }
2775
2776 return 1;
2777}
2778
2779/*
2780 * Patch relocatable BPF instruction.
2781 * Expected insn->imm value is provided for validation, as well as the new
2782 * relocated value.
2783 *
2784 * Currently three kinds of BPF instructions are supported:
2785 * 1. rX = <imm> (assignment with immediate operand);
2786 * 2. rX += <imm> (arithmetic operations with immediate operand);
2787 * 3. *(rX) = <imm> (indirect memory assignment with immediate operand).
2788 *
2789 * If actual insn->imm value is wrong, bail out.
2790 */
2791static int bpf_core_reloc_insn(struct bpf_program *prog, int insn_off,
2792 __u32 orig_off, __u32 new_off)
2793{
2794 struct bpf_insn *insn;
2795 int insn_idx;
2796 __u8 class;
2797
2798 if (insn_off % sizeof(struct bpf_insn))
2799 return -EINVAL;
2800 insn_idx = insn_off / sizeof(struct bpf_insn);
2801
2802 insn = &prog->insns[insn_idx];
2803 class = BPF_CLASS(insn->code);
2804
2805 if (class == BPF_ALU || class == BPF_ALU64) {
2806 if (BPF_SRC(insn->code) != BPF_K)
2807 return -EINVAL;
2808 if (insn->imm != orig_off)
2809 return -EINVAL;
2810 insn->imm = new_off;
2811 pr_debug("prog '%s': patched insn #%d (ALU/ALU64) imm %d -> %d\n",
2812 bpf_program__title(prog, false),
2813 insn_idx, orig_off, new_off);
2814 } else {
2815 pr_warning("prog '%s': trying to relocate unrecognized insn #%d, code:%x, src:%x, dst:%x, off:%x, imm:%x\n",
2816 bpf_program__title(prog, false),
2817 insn_idx, insn->code, insn->src_reg, insn->dst_reg,
2818 insn->off, insn->imm);
2819 return -EINVAL;
2820 }
2821 return 0;
2822}
2823
2824static struct btf *btf_load_raw(const char *path)
2825{
2826 struct btf *btf;
2827 size_t read_cnt;
2828 struct stat st;
2829 void *data;
2830 FILE *f;
2831
2832 if (stat(path, &st))
2833 return ERR_PTR(-errno);
2834
2835 data = malloc(st.st_size);
2836 if (!data)
2837 return ERR_PTR(-ENOMEM);
2838
2839 f = fopen(path, "rb");
2840 if (!f) {
2841 btf = ERR_PTR(-errno);
2842 goto cleanup;
2843 }
2844
2845 read_cnt = fread(data, 1, st.st_size, f);
2846 fclose(f);
2847 if (read_cnt < st.st_size) {
2848 btf = ERR_PTR(-EBADF);
2849 goto cleanup;
2850 }
2851
2852 btf = btf__new(data, read_cnt);
2853
2854cleanup:
2855 free(data);
2856 return btf;
2857}
2858
2859/*
2860 * Probe few well-known locations for vmlinux kernel image and try to load BTF
2861 * data out of it to use for target BTF.
2862 */
2863static struct btf *bpf_core_find_kernel_btf(void)
2864{
2865 struct {
2866 const char *path_fmt;
2867 bool raw_btf;
2868 } locations[] = {
2869 /* try canonical vmlinux BTF through sysfs first */
2870 { "/sys/kernel/btf/vmlinux", true /* raw BTF */ },
2871 /* fall back to trying to find vmlinux ELF on disk otherwise */
2872 { "/boot/vmlinux-%1$s" },
2873 { "/lib/modules/%1$s/vmlinux-%1$s" },
2874 { "/lib/modules/%1$s/build/vmlinux" },
2875 { "/usr/lib/modules/%1$s/kernel/vmlinux" },
2876 { "/usr/lib/debug/boot/vmlinux-%1$s" },
2877 { "/usr/lib/debug/boot/vmlinux-%1$s.debug" },
2878 { "/usr/lib/debug/lib/modules/%1$s/vmlinux" },
2879 };
2880 char path[PATH_MAX + 1];
2881 struct utsname buf;
2882 struct btf *btf;
2883 int i;
2884
2885 uname(&buf);
2886
2887 for (i = 0; i < ARRAY_SIZE(locations); i++) {
2888 snprintf(path, PATH_MAX, locations[i].path_fmt, buf.release);
2889
2890 if (access(path, R_OK))
2891 continue;
2892
2893 if (locations[i].raw_btf)
2894 btf = btf_load_raw(path);
2895 else
2896 btf = btf__parse_elf(path, NULL);
2897
2898 pr_debug("loading kernel BTF '%s': %ld\n",
2899 path, IS_ERR(btf) ? PTR_ERR(btf) : 0);
2900 if (IS_ERR(btf))
2901 continue;
2902
2903 return btf;
2904 }
2905
2906 pr_warning("failed to find valid kernel BTF\n");
2907 return ERR_PTR(-ESRCH);
2908}
2909
2910/* Output spec definition in the format:
2911 * [<type-id>] (<type-name>) + <raw-spec> => <offset>@<spec>,
2912 * where <spec> is a C-syntax view of recorded field access, e.g.: x.a[3].b
2913 */
2914static void bpf_core_dump_spec(int level, const struct bpf_core_spec *spec)
2915{
2916 const struct btf_type *t;
2917 const char *s;
2918 __u32 type_id;
2919 int i;
2920
2921 type_id = spec->spec[0].type_id;
2922 t = btf__type_by_id(spec->btf, type_id);
2923 s = btf__name_by_offset(spec->btf, t->name_off);
2924 libbpf_print(level, "[%u] %s + ", type_id, s);
2925
2926 for (i = 0; i < spec->raw_len; i++)
2927 libbpf_print(level, "%d%s", spec->raw_spec[i],
2928 i == spec->raw_len - 1 ? " => " : ":");
2929
2930 libbpf_print(level, "%u @ &x", spec->offset);
2931
2932 for (i = 0; i < spec->len; i++) {
2933 if (spec->spec[i].name)
2934 libbpf_print(level, ".%s", spec->spec[i].name);
2935 else
2936 libbpf_print(level, "[%u]", spec->spec[i].idx);
2937 }
2938
2939}
2940
2941static size_t bpf_core_hash_fn(const void *key, void *ctx)
2942{
2943 return (size_t)key;
2944}
2945
2946static bool bpf_core_equal_fn(const void *k1, const void *k2, void *ctx)
2947{
2948 return k1 == k2;
2949}
2950
2951static void *u32_as_hash_key(__u32 x)
2952{
2953 return (void *)(uintptr_t)x;
2954}
2955
2956/*
2957 * CO-RE relocate single instruction.
2958 *
2959 * The outline and important points of the algorithm:
2960 * 1. For given local type, find corresponding candidate target types.
2961 * Candidate type is a type with the same "essential" name, ignoring
2962 * everything after last triple underscore (___). E.g., `sample`,
2963 * `sample___flavor_one`, `sample___flavor_another_one`, are all candidates
2964 * for each other. Names with triple underscore are referred to as
2965 * "flavors" and are useful, among other things, to allow to
2966 * specify/support incompatible variations of the same kernel struct, which
2967 * might differ between different kernel versions and/or build
2968 * configurations.
2969 *
2970 * N.B. Struct "flavors" could be generated by bpftool's BTF-to-C
2971 * converter, when deduplicated BTF of a kernel still contains more than
2972 * one different types with the same name. In that case, ___2, ___3, etc
2973 * are appended starting from second name conflict. But start flavors are
2974 * also useful to be defined "locally", in BPF program, to extract same
2975 * data from incompatible changes between different kernel
2976 * versions/configurations. For instance, to handle field renames between
2977 * kernel versions, one can use two flavors of the struct name with the
2978 * same common name and use conditional relocations to extract that field,
2979 * depending on target kernel version.
2980 * 2. For each candidate type, try to match local specification to this
2981 * candidate target type. Matching involves finding corresponding
2982 * high-level spec accessors, meaning that all named fields should match,
2983 * as well as all array accesses should be within the actual bounds. Also,
2984 * types should be compatible (see bpf_core_fields_are_compat for details).
2985 * 3. It is supported and expected that there might be multiple flavors
2986 * matching the spec. As long as all the specs resolve to the same set of
2987 * offsets across all candidates, there is not error. If there is any
2988 * ambiguity, CO-RE relocation will fail. This is necessary to accomodate
2989 * imprefection of BTF deduplication, which can cause slight duplication of
2990 * the same BTF type, if some directly or indirectly referenced (by
2991 * pointer) type gets resolved to different actual types in different
2992 * object files. If such situation occurs, deduplicated BTF will end up
2993 * with two (or more) structurally identical types, which differ only in
2994 * types they refer to through pointer. This should be OK in most cases and
2995 * is not an error.
2996 * 4. Candidate types search is performed by linearly scanning through all
2997 * types in target BTF. It is anticipated that this is overall more
2998 * efficient memory-wise and not significantly worse (if not better)
2999 * CPU-wise compared to prebuilding a map from all local type names to
3000 * a list of candidate type names. It's also sped up by caching resolved
3001 * list of matching candidates per each local "root" type ID, that has at
3002 * least one bpf_offset_reloc associated with it. This list is shared
3003 * between multiple relocations for the same type ID and is updated as some
3004 * of the candidates are pruned due to structural incompatibility.
3005 */
3006static int bpf_core_reloc_offset(struct bpf_program *prog,
3007 const struct bpf_offset_reloc *relo,
3008 int relo_idx,
3009 const struct btf *local_btf,
3010 const struct btf *targ_btf,
3011 struct hashmap *cand_cache)
3012{
3013 const char *prog_name = bpf_program__title(prog, false);
3014 struct bpf_core_spec local_spec, cand_spec, targ_spec;
3015 const void *type_key = u32_as_hash_key(relo->type_id);
3016 const struct btf_type *local_type, *cand_type;
3017 const char *local_name, *cand_name;
3018 struct ids_vec *cand_ids;
3019 __u32 local_id, cand_id;
3020 const char *spec_str;
3021 int i, j, err;
3022
3023 local_id = relo->type_id;
3024 local_type = btf__type_by_id(local_btf, local_id);
3025 if (!local_type)
3026 return -EINVAL;
3027
3028 local_name = btf__name_by_offset(local_btf, local_type->name_off);
3029 if (str_is_empty(local_name))
3030 return -EINVAL;
3031
3032 spec_str = btf__name_by_offset(local_btf, relo->access_str_off);
3033 if (str_is_empty(spec_str))
3034 return -EINVAL;
3035
3036 err = bpf_core_spec_parse(local_btf, local_id, spec_str, &local_spec);
3037 if (err) {
3038 pr_warning("prog '%s': relo #%d: parsing [%d] %s + %s failed: %d\n",
3039 prog_name, relo_idx, local_id, local_name, spec_str,
3040 err);
3041 return -EINVAL;
3042 }
3043
3044 pr_debug("prog '%s': relo #%d: spec is ", prog_name, relo_idx);
3045 bpf_core_dump_spec(LIBBPF_DEBUG, &local_spec);
3046 libbpf_print(LIBBPF_DEBUG, "\n");
3047
3048 if (!hashmap__find(cand_cache, type_key, (void **)&cand_ids)) {
3049 cand_ids = bpf_core_find_cands(local_btf, local_id, targ_btf);
3050 if (IS_ERR(cand_ids)) {
3051 pr_warning("prog '%s': relo #%d: target candidate search failed for [%d] %s: %ld",
3052 prog_name, relo_idx, local_id, local_name,
3053 PTR_ERR(cand_ids));
3054 return PTR_ERR(cand_ids);
3055 }
3056 err = hashmap__set(cand_cache, type_key, cand_ids, NULL, NULL);
3057 if (err) {
3058 bpf_core_free_cands(cand_ids);
3059 return err;
3060 }
3061 }
3062
3063 for (i = 0, j = 0; i < cand_ids->len; i++) {
3064 cand_id = cand_ids->data[i];
3065 cand_type = btf__type_by_id(targ_btf, cand_id);
3066 cand_name = btf__name_by_offset(targ_btf, cand_type->name_off);
3067
3068 err = bpf_core_spec_match(&local_spec, targ_btf,
3069 cand_id, &cand_spec);
3070 pr_debug("prog '%s': relo #%d: matching candidate #%d %s against spec ",
3071 prog_name, relo_idx, i, cand_name);
3072 bpf_core_dump_spec(LIBBPF_DEBUG, &cand_spec);
3073 libbpf_print(LIBBPF_DEBUG, ": %d\n", err);
3074 if (err < 0) {
3075 pr_warning("prog '%s': relo #%d: matching error: %d\n",
3076 prog_name, relo_idx, err);
3077 return err;
3078 }
3079 if (err == 0)
3080 continue;
3081
3082 if (j == 0) {
3083 targ_spec = cand_spec;
3084 } else if (cand_spec.offset != targ_spec.offset) {
3085 /* if there are many candidates, they should all
3086 * resolve to the same offset
3087 */
3088 pr_warning("prog '%s': relo #%d: offset ambiguity: %u != %u\n",
3089 prog_name, relo_idx, cand_spec.offset,
3090 targ_spec.offset);
3091 return -EINVAL;
3092 }
3093
3094 cand_ids->data[j++] = cand_spec.spec[0].type_id;
3095 }
3096
3097 cand_ids->len = j;
3098 if (cand_ids->len == 0) {
3099 pr_warning("prog '%s': relo #%d: no matching targets found for [%d] %s + %s\n",
3100 prog_name, relo_idx, local_id, local_name, spec_str);
3101 return -ESRCH;
3102 }
3103
3104 err = bpf_core_reloc_insn(prog, relo->insn_off,
3105 local_spec.offset, targ_spec.offset);
3106 if (err) {
3107 pr_warning("prog '%s': relo #%d: failed to patch insn at offset %d: %d\n",
3108 prog_name, relo_idx, relo->insn_off, err);
3109 return -EINVAL;
3110 }
3111
3112 return 0;
3113}
3114
3115static int
3116bpf_core_reloc_offsets(struct bpf_object *obj, const char *targ_btf_path)
3117{
3118 const struct btf_ext_info_sec *sec;
3119 const struct bpf_offset_reloc *rec;
3120 const struct btf_ext_info *seg;
3121 struct hashmap_entry *entry;
3122 struct hashmap *cand_cache = NULL;
3123 struct bpf_program *prog;
3124 struct btf *targ_btf;
3125 const char *sec_name;
3126 int i, err = 0;
3127
3128 if (targ_btf_path)
3129 targ_btf = btf__parse_elf(targ_btf_path, NULL);
3130 else
3131 targ_btf = bpf_core_find_kernel_btf();
3132 if (IS_ERR(targ_btf)) {
3133 pr_warning("failed to get target BTF: %ld\n",
3134 PTR_ERR(targ_btf));
3135 return PTR_ERR(targ_btf);
3136 }
3137
3138 cand_cache = hashmap__new(bpf_core_hash_fn, bpf_core_equal_fn, NULL);
3139 if (IS_ERR(cand_cache)) {
3140 err = PTR_ERR(cand_cache);
3141 goto out;
3142 }
3143
3144 seg = &obj->btf_ext->offset_reloc_info;
3145 for_each_btf_ext_sec(seg, sec) {
3146 sec_name = btf__name_by_offset(obj->btf, sec->sec_name_off);
3147 if (str_is_empty(sec_name)) {
3148 err = -EINVAL;
3149 goto out;
3150 }
3151 prog = bpf_object__find_program_by_title(obj, sec_name);
3152 if (!prog) {
3153 pr_warning("failed to find program '%s' for CO-RE offset relocation\n",
3154 sec_name);
3155 err = -EINVAL;
3156 goto out;
3157 }
3158
3159 pr_debug("prog '%s': performing %d CO-RE offset relocs\n",
3160 sec_name, sec->num_info);
3161
3162 for_each_btf_ext_rec(seg, sec, i, rec) {
3163 err = bpf_core_reloc_offset(prog, rec, i, obj->btf,
3164 targ_btf, cand_cache);
3165 if (err) {
3166 pr_warning("prog '%s': relo #%d: failed to relocate: %d\n",
3167 sec_name, i, err);
3168 goto out;
3169 }
3170 }
3171 }
3172
3173out:
3174 btf__free(targ_btf);
3175 if (!IS_ERR_OR_NULL(cand_cache)) {
3176 hashmap__for_each_entry(cand_cache, entry, i) {
3177 bpf_core_free_cands(entry->value);
3178 }
3179 hashmap__free(cand_cache);
3180 }
3181 return err;
3182}
3183
3184static int
3185bpf_object__relocate_core(struct bpf_object *obj, const char *targ_btf_path)
3186{
3187 int err = 0;
3188
3189 if (obj->btf_ext->offset_reloc_info.len)
3190 err = bpf_core_reloc_offsets(obj, targ_btf_path);
3191
3192 return err;
3193}
3194
3195static int
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003196bpf_program__reloc_text(struct bpf_program *prog, struct bpf_object *obj,
3197 struct reloc_desc *relo)
3198{
3199 struct bpf_insn *insn, *new_insn;
3200 struct bpf_program *text;
3201 size_t new_cnt;
David Brazdil0f672f62019-12-10 10:32:29 +00003202 int err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003203
3204 if (relo->type != RELO_CALL)
3205 return -LIBBPF_ERRNO__RELOC;
3206
3207 if (prog->idx == obj->efile.text_shndx) {
3208 pr_warning("relo in .text insn %d into off %d\n",
3209 relo->insn_idx, relo->text_off);
3210 return -LIBBPF_ERRNO__RELOC;
3211 }
3212
3213 if (prog->main_prog_cnt == 0) {
3214 text = bpf_object__find_prog_by_idx(obj, obj->efile.text_shndx);
3215 if (!text) {
3216 pr_warning("no .text section found yet relo into text exist\n");
3217 return -LIBBPF_ERRNO__RELOC;
3218 }
3219 new_cnt = prog->insns_cnt + text->insns_cnt;
3220 new_insn = reallocarray(prog->insns, new_cnt, sizeof(*insn));
3221 if (!new_insn) {
3222 pr_warning("oom in prog realloc\n");
3223 return -ENOMEM;
3224 }
Olivier Deprez0e641232021-09-23 10:07:05 +02003225 prog->insns = new_insn;
David Brazdil0f672f62019-12-10 10:32:29 +00003226
3227 if (obj->btf_ext) {
3228 err = bpf_program_reloc_btf_ext(prog, obj,
3229 text->section_name,
3230 prog->insns_cnt);
3231 if (err)
3232 return err;
3233 }
3234
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003235 memcpy(new_insn + prog->insns_cnt, text->insns,
3236 text->insns_cnt * sizeof(*insn));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003237 prog->main_prog_cnt = prog->insns_cnt;
3238 prog->insns_cnt = new_cnt;
3239 pr_debug("added %zd insn from %s to prog %s\n",
3240 text->insns_cnt, text->section_name,
3241 prog->section_name);
3242 }
3243 insn = &prog->insns[relo->insn_idx];
3244 insn->imm += prog->main_prog_cnt - relo->insn_idx;
3245 return 0;
3246}
3247
3248static int
3249bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj)
3250{
3251 int i, err;
3252
David Brazdil0f672f62019-12-10 10:32:29 +00003253 if (!prog)
3254 return 0;
3255
3256 if (obj->btf_ext) {
3257 err = bpf_program_reloc_btf_ext(prog, obj,
3258 prog->section_name, 0);
3259 if (err)
3260 return err;
3261 }
3262
3263 if (!prog->reloc_desc)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003264 return 0;
3265
3266 for (i = 0; i < prog->nr_reloc; i++) {
David Brazdil0f672f62019-12-10 10:32:29 +00003267 if (prog->reloc_desc[i].type == RELO_LD64 ||
3268 prog->reloc_desc[i].type == RELO_DATA) {
3269 bool relo_data = prog->reloc_desc[i].type == RELO_DATA;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003270 struct bpf_insn *insns = prog->insns;
3271 int insn_idx, map_idx;
3272
3273 insn_idx = prog->reloc_desc[i].insn_idx;
3274 map_idx = prog->reloc_desc[i].map_idx;
3275
David Brazdil0f672f62019-12-10 10:32:29 +00003276 if (insn_idx + 1 >= (int)prog->insns_cnt) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003277 pr_warning("relocation out of range: '%s'\n",
3278 prog->section_name);
3279 return -LIBBPF_ERRNO__RELOC;
3280 }
David Brazdil0f672f62019-12-10 10:32:29 +00003281
3282 if (!relo_data) {
3283 insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
3284 } else {
3285 insns[insn_idx].src_reg = BPF_PSEUDO_MAP_VALUE;
3286 insns[insn_idx + 1].imm = insns[insn_idx].imm;
3287 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003288 insns[insn_idx].imm = obj->maps[map_idx].fd;
David Brazdil0f672f62019-12-10 10:32:29 +00003289 } else if (prog->reloc_desc[i].type == RELO_CALL) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003290 err = bpf_program__reloc_text(prog, obj,
3291 &prog->reloc_desc[i]);
3292 if (err)
3293 return err;
3294 }
3295 }
3296
3297 zfree(&prog->reloc_desc);
3298 prog->nr_reloc = 0;
3299 return 0;
3300}
3301
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003302static int
David Brazdil0f672f62019-12-10 10:32:29 +00003303bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_path)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003304{
3305 struct bpf_program *prog;
3306 size_t i;
3307 int err;
3308
David Brazdil0f672f62019-12-10 10:32:29 +00003309 if (obj->btf_ext) {
3310 err = bpf_object__relocate_core(obj, targ_btf_path);
3311 if (err) {
3312 pr_warning("failed to perform CO-RE relocations: %d\n",
3313 err);
3314 return err;
3315 }
3316 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003317 for (i = 0; i < obj->nr_programs; i++) {
3318 prog = &obj->programs[i];
3319
3320 err = bpf_program__relocate(prog, obj);
3321 if (err) {
3322 pr_warning("failed to relocate '%s'\n",
3323 prog->section_name);
3324 return err;
3325 }
3326 }
3327 return 0;
3328}
3329
3330static int bpf_object__collect_reloc(struct bpf_object *obj)
3331{
3332 int i, err;
3333
3334 if (!obj_elf_valid(obj)) {
3335 pr_warning("Internal error: elf object is closed\n");
3336 return -LIBBPF_ERRNO__INTERNAL;
3337 }
3338
3339 for (i = 0; i < obj->efile.nr_reloc; i++) {
3340 GElf_Shdr *shdr = &obj->efile.reloc[i].shdr;
3341 Elf_Data *data = obj->efile.reloc[i].data;
3342 int idx = shdr->sh_info;
3343 struct bpf_program *prog;
3344
3345 if (shdr->sh_type != SHT_REL) {
3346 pr_warning("internal error at %d\n", __LINE__);
3347 return -LIBBPF_ERRNO__INTERNAL;
3348 }
3349
3350 prog = bpf_object__find_prog_by_idx(obj, idx);
3351 if (!prog) {
3352 pr_warning("relocation failed: no section(%d)\n", idx);
3353 return -LIBBPF_ERRNO__RELOC;
3354 }
3355
David Brazdil0f672f62019-12-10 10:32:29 +00003356 err = bpf_program__collect_reloc(prog, shdr, data, obj);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003357 if (err)
3358 return err;
3359 }
3360 return 0;
3361}
3362
3363static int
David Brazdil0f672f62019-12-10 10:32:29 +00003364load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt,
3365 char *license, __u32 kern_version, int *pfd)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003366{
3367 struct bpf_load_program_attr load_attr;
3368 char *cp, errmsg[STRERR_BUFSIZE];
David Brazdil0f672f62019-12-10 10:32:29 +00003369 int log_buf_size = BPF_LOG_BUF_SIZE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003370 char *log_buf;
David Brazdil0f672f62019-12-10 10:32:29 +00003371 int btf_fd, ret;
3372
3373 if (!insns || !insns_cnt)
3374 return -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003375
3376 memset(&load_attr, 0, sizeof(struct bpf_load_program_attr));
David Brazdil0f672f62019-12-10 10:32:29 +00003377 load_attr.prog_type = prog->type;
3378 load_attr.expected_attach_type = prog->expected_attach_type;
3379 if (prog->caps->name)
3380 load_attr.name = prog->name;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003381 load_attr.insns = insns;
3382 load_attr.insns_cnt = insns_cnt;
3383 load_attr.license = license;
3384 load_attr.kern_version = kern_version;
David Brazdil0f672f62019-12-10 10:32:29 +00003385 load_attr.prog_ifindex = prog->prog_ifindex;
3386 /* if .BTF.ext was loaded, kernel supports associated BTF for prog */
3387 if (prog->obj->btf_ext)
3388 btf_fd = bpf_object__btf_fd(prog->obj);
3389 else
3390 btf_fd = -1;
3391 load_attr.prog_btf_fd = btf_fd >= 0 ? btf_fd : 0;
3392 load_attr.func_info = prog->func_info;
3393 load_attr.func_info_rec_size = prog->func_info_rec_size;
3394 load_attr.func_info_cnt = prog->func_info_cnt;
3395 load_attr.line_info = prog->line_info;
3396 load_attr.line_info_rec_size = prog->line_info_rec_size;
3397 load_attr.line_info_cnt = prog->line_info_cnt;
3398 load_attr.log_level = prog->log_level;
3399 load_attr.prog_flags = prog->prog_flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003400
David Brazdil0f672f62019-12-10 10:32:29 +00003401retry_load:
3402 log_buf = malloc(log_buf_size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003403 if (!log_buf)
3404 pr_warning("Alloc log buffer for bpf loader error, continue without log\n");
3405
David Brazdil0f672f62019-12-10 10:32:29 +00003406 ret = bpf_load_program_xattr(&load_attr, log_buf, log_buf_size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003407
3408 if (ret >= 0) {
David Brazdil0f672f62019-12-10 10:32:29 +00003409 if (load_attr.log_level)
3410 pr_debug("verifier log:\n%s", log_buf);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003411 *pfd = ret;
3412 ret = 0;
3413 goto out;
3414 }
3415
David Brazdil0f672f62019-12-10 10:32:29 +00003416 if (errno == ENOSPC) {
3417 log_buf_size <<= 1;
3418 free(log_buf);
3419 goto retry_load;
3420 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003421 ret = -LIBBPF_ERRNO__LOAD;
David Brazdil0f672f62019-12-10 10:32:29 +00003422 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003423 pr_warning("load bpf program failed: %s\n", cp);
3424
3425 if (log_buf && log_buf[0] != '\0') {
3426 ret = -LIBBPF_ERRNO__VERIFY;
3427 pr_warning("-- BEGIN DUMP LOG ---\n");
3428 pr_warning("\n%s\n", log_buf);
3429 pr_warning("-- END LOG --\n");
3430 } else if (load_attr.insns_cnt >= BPF_MAXINSNS) {
3431 pr_warning("Program too large (%zu insns), at most %d insns\n",
3432 load_attr.insns_cnt, BPF_MAXINSNS);
3433 ret = -LIBBPF_ERRNO__PROG2BIG;
3434 } else {
3435 /* Wrong program type? */
3436 if (load_attr.prog_type != BPF_PROG_TYPE_KPROBE) {
3437 int fd;
3438
3439 load_attr.prog_type = BPF_PROG_TYPE_KPROBE;
3440 load_attr.expected_attach_type = 0;
3441 fd = bpf_load_program_xattr(&load_attr, NULL, 0);
3442 if (fd >= 0) {
3443 close(fd);
3444 ret = -LIBBPF_ERRNO__PROGTYPE;
3445 goto out;
3446 }
3447 }
3448
3449 if (log_buf)
3450 ret = -LIBBPF_ERRNO__KVER;
3451 }
3452
3453out:
3454 free(log_buf);
3455 return ret;
3456}
3457
David Brazdil0f672f62019-12-10 10:32:29 +00003458int
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003459bpf_program__load(struct bpf_program *prog,
David Brazdil0f672f62019-12-10 10:32:29 +00003460 char *license, __u32 kern_version)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003461{
3462 int err = 0, fd, i;
3463
3464 if (prog->instances.nr < 0 || !prog->instances.fds) {
3465 if (prog->preprocessor) {
3466 pr_warning("Internal error: can't load program '%s'\n",
3467 prog->section_name);
3468 return -LIBBPF_ERRNO__INTERNAL;
3469 }
3470
3471 prog->instances.fds = malloc(sizeof(int));
3472 if (!prog->instances.fds) {
3473 pr_warning("Not enough memory for BPF fds\n");
3474 return -ENOMEM;
3475 }
3476 prog->instances.nr = 1;
3477 prog->instances.fds[0] = -1;
3478 }
3479
3480 if (!prog->preprocessor) {
3481 if (prog->instances.nr != 1) {
3482 pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n",
3483 prog->section_name, prog->instances.nr);
3484 }
David Brazdil0f672f62019-12-10 10:32:29 +00003485 err = load_program(prog, prog->insns, prog->insns_cnt,
3486 license, kern_version, &fd);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003487 if (!err)
3488 prog->instances.fds[0] = fd;
3489 goto out;
3490 }
3491
3492 for (i = 0; i < prog->instances.nr; i++) {
3493 struct bpf_prog_prep_result result;
3494 bpf_program_prep_t preprocessor = prog->preprocessor;
3495
David Brazdil0f672f62019-12-10 10:32:29 +00003496 memset(&result, 0, sizeof(result));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003497 err = preprocessor(prog, i, prog->insns,
3498 prog->insns_cnt, &result);
3499 if (err) {
3500 pr_warning("Preprocessing the %dth instance of program '%s' failed\n",
3501 i, prog->section_name);
3502 goto out;
3503 }
3504
3505 if (!result.new_insn_ptr || !result.new_insn_cnt) {
3506 pr_debug("Skip loading the %dth instance of program '%s'\n",
3507 i, prog->section_name);
3508 prog->instances.fds[i] = -1;
3509 if (result.pfd)
3510 *result.pfd = -1;
3511 continue;
3512 }
3513
David Brazdil0f672f62019-12-10 10:32:29 +00003514 err = load_program(prog, result.new_insn_ptr,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003515 result.new_insn_cnt,
David Brazdil0f672f62019-12-10 10:32:29 +00003516 license, kern_version, &fd);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003517
3518 if (err) {
3519 pr_warning("Loading the %dth instance of program '%s' failed\n",
3520 i, prog->section_name);
3521 goto out;
3522 }
3523
3524 if (result.pfd)
3525 *result.pfd = fd;
3526 prog->instances.fds[i] = fd;
3527 }
3528out:
3529 if (err)
3530 pr_warning("failed to load program '%s'\n",
3531 prog->section_name);
3532 zfree(&prog->insns);
3533 prog->insns_cnt = 0;
3534 return err;
3535}
3536
David Brazdil0f672f62019-12-10 10:32:29 +00003537static bool bpf_program__is_function_storage(const struct bpf_program *prog,
3538 const struct bpf_object *obj)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003539{
3540 return prog->idx == obj->efile.text_shndx && obj->has_pseudo_calls;
3541}
3542
3543static int
David Brazdil0f672f62019-12-10 10:32:29 +00003544bpf_object__load_progs(struct bpf_object *obj, int log_level)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003545{
3546 size_t i;
3547 int err;
3548
3549 for (i = 0; i < obj->nr_programs; i++) {
3550 if (bpf_program__is_function_storage(&obj->programs[i], obj))
3551 continue;
David Brazdil0f672f62019-12-10 10:32:29 +00003552 obj->programs[i].log_level |= log_level;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003553 err = bpf_program__load(&obj->programs[i],
3554 obj->license,
3555 obj->kern_version);
3556 if (err)
3557 return err;
3558 }
3559 return 0;
3560}
3561
3562static bool bpf_prog_type__needs_kver(enum bpf_prog_type type)
3563{
3564 switch (type) {
3565 case BPF_PROG_TYPE_SOCKET_FILTER:
3566 case BPF_PROG_TYPE_SCHED_CLS:
3567 case BPF_PROG_TYPE_SCHED_ACT:
3568 case BPF_PROG_TYPE_XDP:
3569 case BPF_PROG_TYPE_CGROUP_SKB:
3570 case BPF_PROG_TYPE_CGROUP_SOCK:
3571 case BPF_PROG_TYPE_LWT_IN:
3572 case BPF_PROG_TYPE_LWT_OUT:
3573 case BPF_PROG_TYPE_LWT_XMIT:
3574 case BPF_PROG_TYPE_LWT_SEG6LOCAL:
3575 case BPF_PROG_TYPE_SOCK_OPS:
3576 case BPF_PROG_TYPE_SK_SKB:
3577 case BPF_PROG_TYPE_CGROUP_DEVICE:
3578 case BPF_PROG_TYPE_SK_MSG:
3579 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3580 case BPF_PROG_TYPE_LIRC_MODE2:
3581 case BPF_PROG_TYPE_SK_REUSEPORT:
David Brazdil0f672f62019-12-10 10:32:29 +00003582 case BPF_PROG_TYPE_FLOW_DISSECTOR:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003583 case BPF_PROG_TYPE_UNSPEC:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003584 case BPF_PROG_TYPE_TRACEPOINT:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003585 case BPF_PROG_TYPE_RAW_TRACEPOINT:
David Brazdil0f672f62019-12-10 10:32:29 +00003586 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
3587 case BPF_PROG_TYPE_PERF_EVENT:
3588 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3589 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3590 return false;
3591 case BPF_PROG_TYPE_KPROBE:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003592 default:
3593 return true;
3594 }
3595}
3596
3597static int bpf_object__validate(struct bpf_object *obj, bool needs_kver)
3598{
3599 if (needs_kver && obj->kern_version == 0) {
3600 pr_warning("%s doesn't provide kernel version\n",
3601 obj->path);
3602 return -LIBBPF_ERRNO__KVERSION;
3603 }
3604 return 0;
3605}
3606
3607static struct bpf_object *
3608__bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz,
David Brazdil0f672f62019-12-10 10:32:29 +00003609 bool needs_kver, int flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003610{
3611 struct bpf_object *obj;
3612 int err;
3613
3614 if (elf_version(EV_CURRENT) == EV_NONE) {
3615 pr_warning("failed to init libelf for %s\n", path);
3616 return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
3617 }
3618
3619 obj = bpf_object__new(path, obj_buf, obj_buf_sz);
3620 if (IS_ERR(obj))
3621 return obj;
3622
3623 CHECK_ERR(bpf_object__elf_init(obj), err, out);
3624 CHECK_ERR(bpf_object__check_endianness(obj), err, out);
David Brazdil0f672f62019-12-10 10:32:29 +00003625 CHECK_ERR(bpf_object__probe_caps(obj), err, out);
3626 CHECK_ERR(bpf_object__elf_collect(obj, flags), err, out);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003627 CHECK_ERR(bpf_object__collect_reloc(obj), err, out);
3628 CHECK_ERR(bpf_object__validate(obj, needs_kver), err, out);
3629
3630 bpf_object__elf_finish(obj);
3631 return obj;
3632out:
3633 bpf_object__close(obj);
3634 return ERR_PTR(err);
3635}
3636
David Brazdil0f672f62019-12-10 10:32:29 +00003637struct bpf_object *__bpf_object__open_xattr(struct bpf_object_open_attr *attr,
3638 int flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003639{
3640 /* param validation */
3641 if (!attr->file)
3642 return NULL;
3643
3644 pr_debug("loading %s\n", attr->file);
3645
3646 return __bpf_object__open(attr->file, NULL, 0,
David Brazdil0f672f62019-12-10 10:32:29 +00003647 bpf_prog_type__needs_kver(attr->prog_type),
3648 flags);
3649}
3650
3651struct bpf_object *bpf_object__open_xattr(struct bpf_object_open_attr *attr)
3652{
3653 return __bpf_object__open_xattr(attr, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003654}
3655
3656struct bpf_object *bpf_object__open(const char *path)
3657{
3658 struct bpf_object_open_attr attr = {
3659 .file = path,
3660 .prog_type = BPF_PROG_TYPE_UNSPEC,
3661 };
3662
3663 return bpf_object__open_xattr(&attr);
3664}
3665
3666struct bpf_object *bpf_object__open_buffer(void *obj_buf,
3667 size_t obj_buf_sz,
3668 const char *name)
3669{
3670 char tmp_name[64];
3671
3672 /* param validation */
3673 if (!obj_buf || obj_buf_sz <= 0)
3674 return NULL;
3675
3676 if (!name) {
3677 snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx",
3678 (unsigned long)obj_buf,
3679 (unsigned long)obj_buf_sz);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003680 name = tmp_name;
3681 }
David Brazdil0f672f62019-12-10 10:32:29 +00003682 pr_debug("loading object '%s' from buffer\n", name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003683
David Brazdil0f672f62019-12-10 10:32:29 +00003684 return __bpf_object__open(name, obj_buf, obj_buf_sz, true, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003685}
3686
3687int bpf_object__unload(struct bpf_object *obj)
3688{
3689 size_t i;
3690
3691 if (!obj)
3692 return -EINVAL;
3693
3694 for (i = 0; i < obj->nr_maps; i++)
3695 zclose(obj->maps[i].fd);
3696
3697 for (i = 0; i < obj->nr_programs; i++)
3698 bpf_program__unload(&obj->programs[i]);
3699
3700 return 0;
3701}
3702
David Brazdil0f672f62019-12-10 10:32:29 +00003703int bpf_object__load_xattr(struct bpf_object_load_attr *attr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003704{
David Brazdil0f672f62019-12-10 10:32:29 +00003705 struct bpf_object *obj;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003706 int err;
3707
David Brazdil0f672f62019-12-10 10:32:29 +00003708 if (!attr)
3709 return -EINVAL;
3710 obj = attr->obj;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003711 if (!obj)
3712 return -EINVAL;
3713
3714 if (obj->loaded) {
3715 pr_warning("object should not be loaded twice\n");
3716 return -EINVAL;
3717 }
3718
3719 obj->loaded = true;
3720
3721 CHECK_ERR(bpf_object__create_maps(obj), err, out);
David Brazdil0f672f62019-12-10 10:32:29 +00003722 CHECK_ERR(bpf_object__relocate(obj, attr->target_btf_path), err, out);
3723 CHECK_ERR(bpf_object__load_progs(obj, attr->log_level), err, out);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003724
3725 return 0;
3726out:
3727 bpf_object__unload(obj);
3728 pr_warning("failed to load object '%s'\n", obj->path);
3729 return err;
3730}
3731
David Brazdil0f672f62019-12-10 10:32:29 +00003732int bpf_object__load(struct bpf_object *obj)
3733{
3734 struct bpf_object_load_attr attr = {
3735 .obj = obj,
3736 };
3737
3738 return bpf_object__load_xattr(&attr);
3739}
3740
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003741static int check_path(const char *path)
3742{
3743 char *cp, errmsg[STRERR_BUFSIZE];
3744 struct statfs st_fs;
3745 char *dname, *dir;
3746 int err = 0;
3747
3748 if (path == NULL)
3749 return -EINVAL;
3750
3751 dname = strdup(path);
3752 if (dname == NULL)
3753 return -ENOMEM;
3754
3755 dir = dirname(dname);
3756 if (statfs(dir, &st_fs)) {
David Brazdil0f672f62019-12-10 10:32:29 +00003757 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003758 pr_warning("failed to statfs %s: %s\n", dir, cp);
3759 err = -errno;
3760 }
3761 free(dname);
3762
3763 if (!err && st_fs.f_type != BPF_FS_MAGIC) {
3764 pr_warning("specified path %s is not on BPF FS\n", path);
3765 err = -EINVAL;
3766 }
3767
3768 return err;
3769}
3770
3771int bpf_program__pin_instance(struct bpf_program *prog, const char *path,
3772 int instance)
3773{
3774 char *cp, errmsg[STRERR_BUFSIZE];
3775 int err;
3776
3777 err = check_path(path);
3778 if (err)
3779 return err;
3780
3781 if (prog == NULL) {
3782 pr_warning("invalid program pointer\n");
3783 return -EINVAL;
3784 }
3785
3786 if (instance < 0 || instance >= prog->instances.nr) {
3787 pr_warning("invalid prog instance %d of prog %s (max %d)\n",
3788 instance, prog->section_name, prog->instances.nr);
3789 return -EINVAL;
3790 }
3791
3792 if (bpf_obj_pin(prog->instances.fds[instance], path)) {
David Brazdil0f672f62019-12-10 10:32:29 +00003793 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003794 pr_warning("failed to pin program: %s\n", cp);
3795 return -errno;
3796 }
3797 pr_debug("pinned program '%s'\n", path);
3798
3799 return 0;
3800}
3801
David Brazdil0f672f62019-12-10 10:32:29 +00003802int bpf_program__unpin_instance(struct bpf_program *prog, const char *path,
3803 int instance)
3804{
3805 int err;
3806
3807 err = check_path(path);
3808 if (err)
3809 return err;
3810
3811 if (prog == NULL) {
3812 pr_warning("invalid program pointer\n");
3813 return -EINVAL;
3814 }
3815
3816 if (instance < 0 || instance >= prog->instances.nr) {
3817 pr_warning("invalid prog instance %d of prog %s (max %d)\n",
3818 instance, prog->section_name, prog->instances.nr);
3819 return -EINVAL;
3820 }
3821
3822 err = unlink(path);
3823 if (err != 0)
3824 return -errno;
3825 pr_debug("unpinned program '%s'\n", path);
3826
3827 return 0;
3828}
3829
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003830static int make_dir(const char *path)
3831{
3832 char *cp, errmsg[STRERR_BUFSIZE];
3833 int err = 0;
3834
3835 if (mkdir(path, 0700) && errno != EEXIST)
3836 err = -errno;
3837
3838 if (err) {
David Brazdil0f672f62019-12-10 10:32:29 +00003839 cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003840 pr_warning("failed to mkdir %s: %s\n", path, cp);
3841 }
3842 return err;
3843}
3844
3845int bpf_program__pin(struct bpf_program *prog, const char *path)
3846{
3847 int i, err;
3848
3849 err = check_path(path);
3850 if (err)
3851 return err;
3852
3853 if (prog == NULL) {
3854 pr_warning("invalid program pointer\n");
3855 return -EINVAL;
3856 }
3857
3858 if (prog->instances.nr <= 0) {
3859 pr_warning("no instances of prog %s to pin\n",
3860 prog->section_name);
3861 return -EINVAL;
3862 }
3863
David Brazdil0f672f62019-12-10 10:32:29 +00003864 if (prog->instances.nr == 1) {
3865 /* don't create subdirs when pinning single instance */
3866 return bpf_program__pin_instance(prog, path, 0);
3867 }
3868
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003869 err = make_dir(path);
3870 if (err)
3871 return err;
3872
3873 for (i = 0; i < prog->instances.nr; i++) {
3874 char buf[PATH_MAX];
3875 int len;
3876
3877 len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
David Brazdil0f672f62019-12-10 10:32:29 +00003878 if (len < 0) {
3879 err = -EINVAL;
3880 goto err_unpin;
3881 } else if (len >= PATH_MAX) {
3882 err = -ENAMETOOLONG;
3883 goto err_unpin;
3884 }
3885
3886 err = bpf_program__pin_instance(prog, buf, i);
3887 if (err)
3888 goto err_unpin;
3889 }
3890
3891 return 0;
3892
3893err_unpin:
3894 for (i = i - 1; i >= 0; i--) {
3895 char buf[PATH_MAX];
3896 int len;
3897
3898 len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
3899 if (len < 0)
3900 continue;
3901 else if (len >= PATH_MAX)
3902 continue;
3903
3904 bpf_program__unpin_instance(prog, buf, i);
3905 }
3906
3907 rmdir(path);
3908
3909 return err;
3910}
3911
3912int bpf_program__unpin(struct bpf_program *prog, const char *path)
3913{
3914 int i, err;
3915
3916 err = check_path(path);
3917 if (err)
3918 return err;
3919
3920 if (prog == NULL) {
3921 pr_warning("invalid program pointer\n");
3922 return -EINVAL;
3923 }
3924
3925 if (prog->instances.nr <= 0) {
3926 pr_warning("no instances of prog %s to pin\n",
3927 prog->section_name);
3928 return -EINVAL;
3929 }
3930
3931 if (prog->instances.nr == 1) {
3932 /* don't create subdirs when pinning single instance */
3933 return bpf_program__unpin_instance(prog, path, 0);
3934 }
3935
3936 for (i = 0; i < prog->instances.nr; i++) {
3937 char buf[PATH_MAX];
3938 int len;
3939
3940 len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003941 if (len < 0)
3942 return -EINVAL;
3943 else if (len >= PATH_MAX)
3944 return -ENAMETOOLONG;
3945
David Brazdil0f672f62019-12-10 10:32:29 +00003946 err = bpf_program__unpin_instance(prog, buf, i);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003947 if (err)
3948 return err;
3949 }
3950
David Brazdil0f672f62019-12-10 10:32:29 +00003951 err = rmdir(path);
3952 if (err)
3953 return -errno;
3954
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003955 return 0;
3956}
3957
3958int bpf_map__pin(struct bpf_map *map, const char *path)
3959{
3960 char *cp, errmsg[STRERR_BUFSIZE];
3961 int err;
3962
3963 err = check_path(path);
3964 if (err)
3965 return err;
3966
3967 if (map == NULL) {
3968 pr_warning("invalid map pointer\n");
3969 return -EINVAL;
3970 }
3971
3972 if (bpf_obj_pin(map->fd, path)) {
David Brazdil0f672f62019-12-10 10:32:29 +00003973 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003974 pr_warning("failed to pin map: %s\n", cp);
3975 return -errno;
3976 }
3977
3978 pr_debug("pinned map '%s'\n", path);
David Brazdil0f672f62019-12-10 10:32:29 +00003979
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003980 return 0;
3981}
3982
David Brazdil0f672f62019-12-10 10:32:29 +00003983int bpf_map__unpin(struct bpf_map *map, const char *path)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003984{
David Brazdil0f672f62019-12-10 10:32:29 +00003985 int err;
3986
3987 err = check_path(path);
3988 if (err)
3989 return err;
3990
3991 if (map == NULL) {
3992 pr_warning("invalid map pointer\n");
3993 return -EINVAL;
3994 }
3995
3996 err = unlink(path);
3997 if (err != 0)
3998 return -errno;
3999 pr_debug("unpinned map '%s'\n", path);
4000
4001 return 0;
4002}
4003
4004int bpf_object__pin_maps(struct bpf_object *obj, const char *path)
4005{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004006 struct bpf_map *map;
4007 int err;
4008
4009 if (!obj)
4010 return -ENOENT;
4011
4012 if (!obj->loaded) {
4013 pr_warning("object not yet loaded; load it first\n");
4014 return -ENOENT;
4015 }
4016
4017 err = make_dir(path);
4018 if (err)
4019 return err;
4020
David Brazdil0f672f62019-12-10 10:32:29 +00004021 bpf_object__for_each_map(map, obj) {
4022 char buf[PATH_MAX];
4023 int len;
4024
4025 len = snprintf(buf, PATH_MAX, "%s/%s", path,
4026 bpf_map__name(map));
4027 if (len < 0) {
4028 err = -EINVAL;
4029 goto err_unpin_maps;
4030 } else if (len >= PATH_MAX) {
4031 err = -ENAMETOOLONG;
4032 goto err_unpin_maps;
4033 }
4034
4035 err = bpf_map__pin(map, buf);
4036 if (err)
4037 goto err_unpin_maps;
4038 }
4039
4040 return 0;
4041
4042err_unpin_maps:
4043 while ((map = bpf_map__prev(map, obj))) {
4044 char buf[PATH_MAX];
4045 int len;
4046
4047 len = snprintf(buf, PATH_MAX, "%s/%s", path,
4048 bpf_map__name(map));
4049 if (len < 0)
4050 continue;
4051 else if (len >= PATH_MAX)
4052 continue;
4053
4054 bpf_map__unpin(map, buf);
4055 }
4056
4057 return err;
4058}
4059
4060int bpf_object__unpin_maps(struct bpf_object *obj, const char *path)
4061{
4062 struct bpf_map *map;
4063 int err;
4064
4065 if (!obj)
4066 return -ENOENT;
4067
4068 bpf_object__for_each_map(map, obj) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004069 char buf[PATH_MAX];
4070 int len;
4071
4072 len = snprintf(buf, PATH_MAX, "%s/%s", path,
4073 bpf_map__name(map));
4074 if (len < 0)
4075 return -EINVAL;
4076 else if (len >= PATH_MAX)
4077 return -ENAMETOOLONG;
4078
David Brazdil0f672f62019-12-10 10:32:29 +00004079 err = bpf_map__unpin(map, buf);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004080 if (err)
4081 return err;
4082 }
4083
David Brazdil0f672f62019-12-10 10:32:29 +00004084 return 0;
4085}
4086
4087int bpf_object__pin_programs(struct bpf_object *obj, const char *path)
4088{
4089 struct bpf_program *prog;
4090 int err;
4091
4092 if (!obj)
4093 return -ENOENT;
4094
4095 if (!obj->loaded) {
4096 pr_warning("object not yet loaded; load it first\n");
4097 return -ENOENT;
4098 }
4099
4100 err = make_dir(path);
4101 if (err)
4102 return err;
4103
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004104 bpf_object__for_each_program(prog, obj) {
4105 char buf[PATH_MAX];
4106 int len;
4107
4108 len = snprintf(buf, PATH_MAX, "%s/%s", path,
David Brazdil0f672f62019-12-10 10:32:29 +00004109 prog->pin_name);
4110 if (len < 0) {
4111 err = -EINVAL;
4112 goto err_unpin_programs;
4113 } else if (len >= PATH_MAX) {
4114 err = -ENAMETOOLONG;
4115 goto err_unpin_programs;
4116 }
4117
4118 err = bpf_program__pin(prog, buf);
4119 if (err)
4120 goto err_unpin_programs;
4121 }
4122
4123 return 0;
4124
4125err_unpin_programs:
4126 while ((prog = bpf_program__prev(prog, obj))) {
4127 char buf[PATH_MAX];
4128 int len;
4129
4130 len = snprintf(buf, PATH_MAX, "%s/%s", path,
4131 prog->pin_name);
4132 if (len < 0)
4133 continue;
4134 else if (len >= PATH_MAX)
4135 continue;
4136
4137 bpf_program__unpin(prog, buf);
4138 }
4139
4140 return err;
4141}
4142
4143int bpf_object__unpin_programs(struct bpf_object *obj, const char *path)
4144{
4145 struct bpf_program *prog;
4146 int err;
4147
4148 if (!obj)
4149 return -ENOENT;
4150
4151 bpf_object__for_each_program(prog, obj) {
4152 char buf[PATH_MAX];
4153 int len;
4154
4155 len = snprintf(buf, PATH_MAX, "%s/%s", path,
4156 prog->pin_name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004157 if (len < 0)
4158 return -EINVAL;
4159 else if (len >= PATH_MAX)
4160 return -ENAMETOOLONG;
4161
David Brazdil0f672f62019-12-10 10:32:29 +00004162 err = bpf_program__unpin(prog, buf);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004163 if (err)
4164 return err;
4165 }
4166
4167 return 0;
4168}
4169
David Brazdil0f672f62019-12-10 10:32:29 +00004170int bpf_object__pin(struct bpf_object *obj, const char *path)
4171{
4172 int err;
4173
4174 err = bpf_object__pin_maps(obj, path);
4175 if (err)
4176 return err;
4177
4178 err = bpf_object__pin_programs(obj, path);
4179 if (err) {
4180 bpf_object__unpin_maps(obj, path);
4181 return err;
4182 }
4183
4184 return 0;
4185}
4186
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004187void bpf_object__close(struct bpf_object *obj)
4188{
4189 size_t i;
4190
4191 if (!obj)
4192 return;
4193
4194 if (obj->clear_priv)
4195 obj->clear_priv(obj, obj->priv);
4196
4197 bpf_object__elf_finish(obj);
4198 bpf_object__unload(obj);
4199 btf__free(obj->btf);
David Brazdil0f672f62019-12-10 10:32:29 +00004200 btf_ext__free(obj->btf_ext);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004201
4202 for (i = 0; i < obj->nr_maps; i++) {
4203 zfree(&obj->maps[i].name);
4204 if (obj->maps[i].clear_priv)
4205 obj->maps[i].clear_priv(&obj->maps[i],
4206 obj->maps[i].priv);
4207 obj->maps[i].priv = NULL;
4208 obj->maps[i].clear_priv = NULL;
4209 }
David Brazdil0f672f62019-12-10 10:32:29 +00004210
4211 zfree(&obj->sections.rodata);
4212 zfree(&obj->sections.data);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004213 zfree(&obj->maps);
4214 obj->nr_maps = 0;
4215
4216 if (obj->programs && obj->nr_programs) {
4217 for (i = 0; i < obj->nr_programs; i++)
4218 bpf_program__exit(&obj->programs[i]);
4219 }
4220 zfree(&obj->programs);
4221
4222 list_del(&obj->list);
4223 free(obj);
4224}
4225
4226struct bpf_object *
4227bpf_object__next(struct bpf_object *prev)
4228{
4229 struct bpf_object *next;
4230
4231 if (!prev)
4232 next = list_first_entry(&bpf_objects_list,
4233 struct bpf_object,
4234 list);
4235 else
4236 next = list_next_entry(prev, list);
4237
4238 /* Empty list is noticed here so don't need checking on entry. */
4239 if (&next->list == &bpf_objects_list)
4240 return NULL;
4241
4242 return next;
4243}
4244
David Brazdil0f672f62019-12-10 10:32:29 +00004245const char *bpf_object__name(const struct bpf_object *obj)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004246{
4247 return obj ? obj->path : ERR_PTR(-EINVAL);
4248}
4249
David Brazdil0f672f62019-12-10 10:32:29 +00004250unsigned int bpf_object__kversion(const struct bpf_object *obj)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004251{
4252 return obj ? obj->kern_version : 0;
4253}
4254
David Brazdil0f672f62019-12-10 10:32:29 +00004255struct btf *bpf_object__btf(const struct bpf_object *obj)
4256{
4257 return obj ? obj->btf : NULL;
4258}
4259
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004260int bpf_object__btf_fd(const struct bpf_object *obj)
4261{
4262 return obj->btf ? btf__fd(obj->btf) : -1;
4263}
4264
4265int bpf_object__set_priv(struct bpf_object *obj, void *priv,
4266 bpf_object_clear_priv_t clear_priv)
4267{
4268 if (obj->priv && obj->clear_priv)
4269 obj->clear_priv(obj, obj->priv);
4270
4271 obj->priv = priv;
4272 obj->clear_priv = clear_priv;
4273 return 0;
4274}
4275
David Brazdil0f672f62019-12-10 10:32:29 +00004276void *bpf_object__priv(const struct bpf_object *obj)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004277{
4278 return obj ? obj->priv : ERR_PTR(-EINVAL);
4279}
4280
4281static struct bpf_program *
David Brazdil0f672f62019-12-10 10:32:29 +00004282__bpf_program__iter(const struct bpf_program *p, const struct bpf_object *obj,
4283 bool forward)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004284{
David Brazdil0f672f62019-12-10 10:32:29 +00004285 size_t nr_programs = obj->nr_programs;
4286 ssize_t idx;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004287
David Brazdil0f672f62019-12-10 10:32:29 +00004288 if (!nr_programs)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004289 return NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004290
David Brazdil0f672f62019-12-10 10:32:29 +00004291 if (!p)
4292 /* Iter from the beginning */
4293 return forward ? &obj->programs[0] :
4294 &obj->programs[nr_programs - 1];
4295
4296 if (p->obj != obj) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004297 pr_warning("error: program handler doesn't match object\n");
4298 return NULL;
4299 }
4300
David Brazdil0f672f62019-12-10 10:32:29 +00004301 idx = (p - obj->programs) + (forward ? 1 : -1);
4302 if (idx >= obj->nr_programs || idx < 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004303 return NULL;
4304 return &obj->programs[idx];
4305}
4306
4307struct bpf_program *
David Brazdil0f672f62019-12-10 10:32:29 +00004308bpf_program__next(struct bpf_program *prev, const struct bpf_object *obj)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004309{
4310 struct bpf_program *prog = prev;
4311
4312 do {
David Brazdil0f672f62019-12-10 10:32:29 +00004313 prog = __bpf_program__iter(prog, obj, true);
4314 } while (prog && bpf_program__is_function_storage(prog, obj));
4315
4316 return prog;
4317}
4318
4319struct bpf_program *
4320bpf_program__prev(struct bpf_program *next, const struct bpf_object *obj)
4321{
4322 struct bpf_program *prog = next;
4323
4324 do {
4325 prog = __bpf_program__iter(prog, obj, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004326 } while (prog && bpf_program__is_function_storage(prog, obj));
4327
4328 return prog;
4329}
4330
4331int bpf_program__set_priv(struct bpf_program *prog, void *priv,
4332 bpf_program_clear_priv_t clear_priv)
4333{
4334 if (prog->priv && prog->clear_priv)
4335 prog->clear_priv(prog, prog->priv);
4336
4337 prog->priv = priv;
4338 prog->clear_priv = clear_priv;
4339 return 0;
4340}
4341
David Brazdil0f672f62019-12-10 10:32:29 +00004342void *bpf_program__priv(const struct bpf_program *prog)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004343{
4344 return prog ? prog->priv : ERR_PTR(-EINVAL);
4345}
4346
4347void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex)
4348{
4349 prog->prog_ifindex = ifindex;
4350}
4351
David Brazdil0f672f62019-12-10 10:32:29 +00004352const char *bpf_program__title(const struct bpf_program *prog, bool needs_copy)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004353{
4354 const char *title;
4355
4356 title = prog->section_name;
4357 if (needs_copy) {
4358 title = strdup(title);
4359 if (!title) {
4360 pr_warning("failed to strdup program title\n");
4361 return ERR_PTR(-ENOMEM);
4362 }
4363 }
4364
4365 return title;
4366}
4367
David Brazdil0f672f62019-12-10 10:32:29 +00004368int bpf_program__fd(const struct bpf_program *prog)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004369{
4370 return bpf_program__nth_fd(prog, 0);
4371}
4372
4373int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,
4374 bpf_program_prep_t prep)
4375{
4376 int *instances_fds;
4377
4378 if (nr_instances <= 0 || !prep)
4379 return -EINVAL;
4380
4381 if (prog->instances.nr > 0 || prog->instances.fds) {
4382 pr_warning("Can't set pre-processor after loading\n");
4383 return -EINVAL;
4384 }
4385
4386 instances_fds = malloc(sizeof(int) * nr_instances);
4387 if (!instances_fds) {
4388 pr_warning("alloc memory failed for fds\n");
4389 return -ENOMEM;
4390 }
4391
4392 /* fill all fd with -1 */
4393 memset(instances_fds, -1, sizeof(int) * nr_instances);
4394
4395 prog->instances.nr = nr_instances;
4396 prog->instances.fds = instances_fds;
4397 prog->preprocessor = prep;
4398 return 0;
4399}
4400
David Brazdil0f672f62019-12-10 10:32:29 +00004401int bpf_program__nth_fd(const struct bpf_program *prog, int n)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004402{
4403 int fd;
4404
4405 if (!prog)
4406 return -EINVAL;
4407
4408 if (n >= prog->instances.nr || n < 0) {
4409 pr_warning("Can't get the %dth fd from program %s: only %d instances\n",
4410 n, prog->section_name, prog->instances.nr);
4411 return -EINVAL;
4412 }
4413
4414 fd = prog->instances.fds[n];
4415 if (fd < 0) {
4416 pr_warning("%dth instance of program '%s' is invalid\n",
4417 n, prog->section_name);
4418 return -ENOENT;
4419 }
4420
4421 return fd;
4422}
4423
4424void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type)
4425{
4426 prog->type = type;
4427}
4428
David Brazdil0f672f62019-12-10 10:32:29 +00004429static bool bpf_program__is_type(const struct bpf_program *prog,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004430 enum bpf_prog_type type)
4431{
4432 return prog ? (prog->type == type) : false;
4433}
4434
David Brazdil0f672f62019-12-10 10:32:29 +00004435#define BPF_PROG_TYPE_FNS(NAME, TYPE) \
4436int bpf_program__set_##NAME(struct bpf_program *prog) \
4437{ \
4438 if (!prog) \
4439 return -EINVAL; \
4440 bpf_program__set_type(prog, TYPE); \
4441 return 0; \
4442} \
4443 \
4444bool bpf_program__is_##NAME(const struct bpf_program *prog) \
4445{ \
4446 return bpf_program__is_type(prog, TYPE); \
4447} \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004448
4449BPF_PROG_TYPE_FNS(socket_filter, BPF_PROG_TYPE_SOCKET_FILTER);
4450BPF_PROG_TYPE_FNS(kprobe, BPF_PROG_TYPE_KPROBE);
4451BPF_PROG_TYPE_FNS(sched_cls, BPF_PROG_TYPE_SCHED_CLS);
4452BPF_PROG_TYPE_FNS(sched_act, BPF_PROG_TYPE_SCHED_ACT);
4453BPF_PROG_TYPE_FNS(tracepoint, BPF_PROG_TYPE_TRACEPOINT);
4454BPF_PROG_TYPE_FNS(raw_tracepoint, BPF_PROG_TYPE_RAW_TRACEPOINT);
4455BPF_PROG_TYPE_FNS(xdp, BPF_PROG_TYPE_XDP);
4456BPF_PROG_TYPE_FNS(perf_event, BPF_PROG_TYPE_PERF_EVENT);
4457
4458void bpf_program__set_expected_attach_type(struct bpf_program *prog,
4459 enum bpf_attach_type type)
4460{
4461 prog->expected_attach_type = type;
4462}
4463
David Brazdil0f672f62019-12-10 10:32:29 +00004464#define BPF_PROG_SEC_IMPL(string, ptype, eatype, is_attachable, atype) \
4465 { string, sizeof(string) - 1, ptype, eatype, is_attachable, atype }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004466
David Brazdil0f672f62019-12-10 10:32:29 +00004467/* Programs that can NOT be attached. */
4468#define BPF_PROG_SEC(string, ptype) BPF_PROG_SEC_IMPL(string, ptype, 0, 0, 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004469
David Brazdil0f672f62019-12-10 10:32:29 +00004470/* Programs that can be attached. */
4471#define BPF_APROG_SEC(string, ptype, atype) \
4472 BPF_PROG_SEC_IMPL(string, ptype, 0, 1, atype)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004473
David Brazdil0f672f62019-12-10 10:32:29 +00004474/* Programs that must specify expected attach type at load time. */
4475#define BPF_EAPROG_SEC(string, ptype, eatype) \
4476 BPF_PROG_SEC_IMPL(string, ptype, eatype, 1, eatype)
4477
4478/* Programs that can be attached but attach type can't be identified by section
4479 * name. Kept for backward compatibility.
4480 */
4481#define BPF_APROG_COMPAT(string, ptype) BPF_PROG_SEC(string, ptype)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004482
4483static const struct {
4484 const char *sec;
4485 size_t len;
4486 enum bpf_prog_type prog_type;
4487 enum bpf_attach_type expected_attach_type;
David Brazdil0f672f62019-12-10 10:32:29 +00004488 int is_attachable;
4489 enum bpf_attach_type attach_type;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004490} section_names[] = {
David Brazdil0f672f62019-12-10 10:32:29 +00004491 BPF_PROG_SEC("socket", BPF_PROG_TYPE_SOCKET_FILTER),
4492 BPF_PROG_SEC("kprobe/", BPF_PROG_TYPE_KPROBE),
4493 BPF_PROG_SEC("kretprobe/", BPF_PROG_TYPE_KPROBE),
4494 BPF_PROG_SEC("classifier", BPF_PROG_TYPE_SCHED_CLS),
4495 BPF_PROG_SEC("action", BPF_PROG_TYPE_SCHED_ACT),
4496 BPF_PROG_SEC("tracepoint/", BPF_PROG_TYPE_TRACEPOINT),
4497 BPF_PROG_SEC("raw_tracepoint/", BPF_PROG_TYPE_RAW_TRACEPOINT),
4498 BPF_PROG_SEC("xdp", BPF_PROG_TYPE_XDP),
4499 BPF_PROG_SEC("perf_event", BPF_PROG_TYPE_PERF_EVENT),
4500 BPF_PROG_SEC("lwt_in", BPF_PROG_TYPE_LWT_IN),
4501 BPF_PROG_SEC("lwt_out", BPF_PROG_TYPE_LWT_OUT),
4502 BPF_PROG_SEC("lwt_xmit", BPF_PROG_TYPE_LWT_XMIT),
4503 BPF_PROG_SEC("lwt_seg6local", BPF_PROG_TYPE_LWT_SEG6LOCAL),
4504 BPF_APROG_SEC("cgroup_skb/ingress", BPF_PROG_TYPE_CGROUP_SKB,
4505 BPF_CGROUP_INET_INGRESS),
4506 BPF_APROG_SEC("cgroup_skb/egress", BPF_PROG_TYPE_CGROUP_SKB,
4507 BPF_CGROUP_INET_EGRESS),
4508 BPF_APROG_COMPAT("cgroup/skb", BPF_PROG_TYPE_CGROUP_SKB),
4509 BPF_APROG_SEC("cgroup/sock", BPF_PROG_TYPE_CGROUP_SOCK,
4510 BPF_CGROUP_INET_SOCK_CREATE),
4511 BPF_EAPROG_SEC("cgroup/post_bind4", BPF_PROG_TYPE_CGROUP_SOCK,
4512 BPF_CGROUP_INET4_POST_BIND),
4513 BPF_EAPROG_SEC("cgroup/post_bind6", BPF_PROG_TYPE_CGROUP_SOCK,
4514 BPF_CGROUP_INET6_POST_BIND),
4515 BPF_APROG_SEC("cgroup/dev", BPF_PROG_TYPE_CGROUP_DEVICE,
4516 BPF_CGROUP_DEVICE),
4517 BPF_APROG_SEC("sockops", BPF_PROG_TYPE_SOCK_OPS,
4518 BPF_CGROUP_SOCK_OPS),
4519 BPF_APROG_SEC("sk_skb/stream_parser", BPF_PROG_TYPE_SK_SKB,
4520 BPF_SK_SKB_STREAM_PARSER),
4521 BPF_APROG_SEC("sk_skb/stream_verdict", BPF_PROG_TYPE_SK_SKB,
4522 BPF_SK_SKB_STREAM_VERDICT),
4523 BPF_APROG_COMPAT("sk_skb", BPF_PROG_TYPE_SK_SKB),
4524 BPF_APROG_SEC("sk_msg", BPF_PROG_TYPE_SK_MSG,
4525 BPF_SK_MSG_VERDICT),
4526 BPF_APROG_SEC("lirc_mode2", BPF_PROG_TYPE_LIRC_MODE2,
4527 BPF_LIRC_MODE2),
4528 BPF_APROG_SEC("flow_dissector", BPF_PROG_TYPE_FLOW_DISSECTOR,
4529 BPF_FLOW_DISSECTOR),
4530 BPF_EAPROG_SEC("cgroup/bind4", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
4531 BPF_CGROUP_INET4_BIND),
4532 BPF_EAPROG_SEC("cgroup/bind6", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
4533 BPF_CGROUP_INET6_BIND),
4534 BPF_EAPROG_SEC("cgroup/connect4", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
4535 BPF_CGROUP_INET4_CONNECT),
4536 BPF_EAPROG_SEC("cgroup/connect6", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
4537 BPF_CGROUP_INET6_CONNECT),
4538 BPF_EAPROG_SEC("cgroup/sendmsg4", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
4539 BPF_CGROUP_UDP4_SENDMSG),
4540 BPF_EAPROG_SEC("cgroup/sendmsg6", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
4541 BPF_CGROUP_UDP6_SENDMSG),
4542 BPF_EAPROG_SEC("cgroup/recvmsg4", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
4543 BPF_CGROUP_UDP4_RECVMSG),
4544 BPF_EAPROG_SEC("cgroup/recvmsg6", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
4545 BPF_CGROUP_UDP6_RECVMSG),
4546 BPF_EAPROG_SEC("cgroup/sysctl", BPF_PROG_TYPE_CGROUP_SYSCTL,
4547 BPF_CGROUP_SYSCTL),
4548 BPF_EAPROG_SEC("cgroup/getsockopt", BPF_PROG_TYPE_CGROUP_SOCKOPT,
4549 BPF_CGROUP_GETSOCKOPT),
4550 BPF_EAPROG_SEC("cgroup/setsockopt", BPF_PROG_TYPE_CGROUP_SOCKOPT,
4551 BPF_CGROUP_SETSOCKOPT),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004552};
4553
David Brazdil0f672f62019-12-10 10:32:29 +00004554#undef BPF_PROG_SEC_IMPL
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004555#undef BPF_PROG_SEC
David Brazdil0f672f62019-12-10 10:32:29 +00004556#undef BPF_APROG_SEC
4557#undef BPF_EAPROG_SEC
4558#undef BPF_APROG_COMPAT
4559
4560#define MAX_TYPE_NAME_SIZE 32
4561
4562static char *libbpf_get_type_names(bool attach_type)
4563{
4564 int i, len = ARRAY_SIZE(section_names) * MAX_TYPE_NAME_SIZE;
4565 char *buf;
4566
4567 buf = malloc(len);
4568 if (!buf)
4569 return NULL;
4570
4571 buf[0] = '\0';
4572 /* Forge string buf with all available names */
4573 for (i = 0; i < ARRAY_SIZE(section_names); i++) {
4574 if (attach_type && !section_names[i].is_attachable)
4575 continue;
4576
4577 if (strlen(buf) + strlen(section_names[i].sec) + 2 > len) {
4578 free(buf);
4579 return NULL;
4580 }
4581 strcat(buf, " ");
4582 strcat(buf, section_names[i].sec);
4583 }
4584
4585 return buf;
4586}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004587
4588int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
4589 enum bpf_attach_type *expected_attach_type)
4590{
David Brazdil0f672f62019-12-10 10:32:29 +00004591 char *type_names;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004592 int i;
4593
4594 if (!name)
4595 return -EINVAL;
4596
4597 for (i = 0; i < ARRAY_SIZE(section_names); i++) {
4598 if (strncmp(name, section_names[i].sec, section_names[i].len))
4599 continue;
4600 *prog_type = section_names[i].prog_type;
4601 *expected_attach_type = section_names[i].expected_attach_type;
4602 return 0;
4603 }
David Brazdil0f672f62019-12-10 10:32:29 +00004604 pr_warning("failed to guess program type based on ELF section name '%s'\n", name);
4605 type_names = libbpf_get_type_names(false);
4606 if (type_names != NULL) {
4607 pr_info("supported section(type) names are:%s\n", type_names);
4608 free(type_names);
4609 }
4610
4611 return -EINVAL;
4612}
4613
4614int libbpf_attach_type_by_name(const char *name,
4615 enum bpf_attach_type *attach_type)
4616{
4617 char *type_names;
4618 int i;
4619
4620 if (!name)
4621 return -EINVAL;
4622
4623 for (i = 0; i < ARRAY_SIZE(section_names); i++) {
4624 if (strncmp(name, section_names[i].sec, section_names[i].len))
4625 continue;
4626 if (!section_names[i].is_attachable)
4627 return -EINVAL;
4628 *attach_type = section_names[i].attach_type;
4629 return 0;
4630 }
4631 pr_warning("failed to guess attach type based on ELF section name '%s'\n", name);
4632 type_names = libbpf_get_type_names(true);
4633 if (type_names != NULL) {
4634 pr_info("attachable section(type) names are:%s\n", type_names);
4635 free(type_names);
4636 }
4637
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004638 return -EINVAL;
4639}
4640
4641static int
4642bpf_program__identify_section(struct bpf_program *prog,
4643 enum bpf_prog_type *prog_type,
4644 enum bpf_attach_type *expected_attach_type)
4645{
4646 return libbpf_prog_type_by_name(prog->section_name, prog_type,
4647 expected_attach_type);
4648}
4649
David Brazdil0f672f62019-12-10 10:32:29 +00004650int bpf_map__fd(const struct bpf_map *map)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004651{
4652 return map ? map->fd : -EINVAL;
4653}
4654
David Brazdil0f672f62019-12-10 10:32:29 +00004655const struct bpf_map_def *bpf_map__def(const struct bpf_map *map)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004656{
4657 return map ? &map->def : ERR_PTR(-EINVAL);
4658}
4659
David Brazdil0f672f62019-12-10 10:32:29 +00004660const char *bpf_map__name(const struct bpf_map *map)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004661{
4662 return map ? map->name : NULL;
4663}
4664
4665__u32 bpf_map__btf_key_type_id(const struct bpf_map *map)
4666{
4667 return map ? map->btf_key_type_id : 0;
4668}
4669
4670__u32 bpf_map__btf_value_type_id(const struct bpf_map *map)
4671{
4672 return map ? map->btf_value_type_id : 0;
4673}
4674
4675int bpf_map__set_priv(struct bpf_map *map, void *priv,
4676 bpf_map_clear_priv_t clear_priv)
4677{
4678 if (!map)
4679 return -EINVAL;
4680
4681 if (map->priv) {
4682 if (map->clear_priv)
4683 map->clear_priv(map, map->priv);
4684 }
4685
4686 map->priv = priv;
4687 map->clear_priv = clear_priv;
4688 return 0;
4689}
4690
David Brazdil0f672f62019-12-10 10:32:29 +00004691void *bpf_map__priv(const struct bpf_map *map)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004692{
4693 return map ? map->priv : ERR_PTR(-EINVAL);
4694}
4695
David Brazdil0f672f62019-12-10 10:32:29 +00004696bool bpf_map__is_offload_neutral(const struct bpf_map *map)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004697{
4698 return map->def.type == BPF_MAP_TYPE_PERF_EVENT_ARRAY;
4699}
4700
David Brazdil0f672f62019-12-10 10:32:29 +00004701bool bpf_map__is_internal(const struct bpf_map *map)
4702{
4703 return map->libbpf_type != LIBBPF_MAP_UNSPEC;
4704}
4705
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004706void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex)
4707{
4708 map->map_ifindex = ifindex;
4709}
4710
David Brazdil0f672f62019-12-10 10:32:29 +00004711int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004712{
David Brazdil0f672f62019-12-10 10:32:29 +00004713 if (!bpf_map_type__is_map_in_map(map->def.type)) {
4714 pr_warning("error: unsupported map type\n");
4715 return -EINVAL;
4716 }
4717 if (map->inner_map_fd != -1) {
4718 pr_warning("error: inner_map_fd already specified\n");
4719 return -EINVAL;
4720 }
4721 map->inner_map_fd = fd;
4722 return 0;
4723}
4724
4725static struct bpf_map *
4726__bpf_map__iter(const struct bpf_map *m, const struct bpf_object *obj, int i)
4727{
4728 ssize_t idx;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004729 struct bpf_map *s, *e;
4730
4731 if (!obj || !obj->maps)
4732 return NULL;
4733
4734 s = obj->maps;
4735 e = obj->maps + obj->nr_maps;
4736
David Brazdil0f672f62019-12-10 10:32:29 +00004737 if ((m < s) || (m >= e)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004738 pr_warning("error in %s: map handler doesn't belong to object\n",
4739 __func__);
4740 return NULL;
4741 }
4742
David Brazdil0f672f62019-12-10 10:32:29 +00004743 idx = (m - obj->maps) + i;
4744 if (idx >= obj->nr_maps || idx < 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004745 return NULL;
4746 return &obj->maps[idx];
4747}
4748
4749struct bpf_map *
David Brazdil0f672f62019-12-10 10:32:29 +00004750bpf_map__next(const struct bpf_map *prev, const struct bpf_object *obj)
4751{
4752 if (prev == NULL)
4753 return obj->maps;
4754
4755 return __bpf_map__iter(prev, obj, 1);
4756}
4757
4758struct bpf_map *
4759bpf_map__prev(const struct bpf_map *next, const struct bpf_object *obj)
4760{
4761 if (next == NULL) {
4762 if (!obj->nr_maps)
4763 return NULL;
4764 return obj->maps + obj->nr_maps - 1;
4765 }
4766
4767 return __bpf_map__iter(next, obj, -1);
4768}
4769
4770struct bpf_map *
4771bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004772{
4773 struct bpf_map *pos;
4774
David Brazdil0f672f62019-12-10 10:32:29 +00004775 bpf_object__for_each_map(pos, obj) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004776 if (pos->name && !strcmp(pos->name, name))
4777 return pos;
4778 }
4779 return NULL;
4780}
4781
David Brazdil0f672f62019-12-10 10:32:29 +00004782int
4783bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name)
4784{
4785 return bpf_map__fd(bpf_object__find_map_by_name(obj, name));
4786}
4787
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004788struct bpf_map *
4789bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset)
4790{
David Brazdil0f672f62019-12-10 10:32:29 +00004791 return ERR_PTR(-ENOTSUP);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004792}
4793
4794long libbpf_get_error(const void *ptr)
4795{
David Brazdil0f672f62019-12-10 10:32:29 +00004796 return PTR_ERR_OR_ZERO(ptr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004797}
4798
4799int bpf_prog_load(const char *file, enum bpf_prog_type type,
4800 struct bpf_object **pobj, int *prog_fd)
4801{
4802 struct bpf_prog_load_attr attr;
4803
4804 memset(&attr, 0, sizeof(struct bpf_prog_load_attr));
4805 attr.file = file;
4806 attr.prog_type = type;
4807 attr.expected_attach_type = 0;
4808
4809 return bpf_prog_load_xattr(&attr, pobj, prog_fd);
4810}
4811
4812int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
4813 struct bpf_object **pobj, int *prog_fd)
4814{
David Brazdil0f672f62019-12-10 10:32:29 +00004815 struct bpf_object_open_attr open_attr = {};
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004816 struct bpf_program *prog, *first_prog = NULL;
4817 enum bpf_attach_type expected_attach_type;
4818 enum bpf_prog_type prog_type;
4819 struct bpf_object *obj;
4820 struct bpf_map *map;
4821 int err;
4822
4823 if (!attr)
4824 return -EINVAL;
4825 if (!attr->file)
4826 return -EINVAL;
4827
David Brazdil0f672f62019-12-10 10:32:29 +00004828 open_attr.file = attr->file;
4829 open_attr.prog_type = attr->prog_type;
4830
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004831 obj = bpf_object__open_xattr(&open_attr);
4832 if (IS_ERR_OR_NULL(obj))
4833 return -ENOENT;
4834
4835 bpf_object__for_each_program(prog, obj) {
4836 /*
4837 * If type is not specified, try to guess it based on
4838 * section name.
4839 */
4840 prog_type = attr->prog_type;
4841 prog->prog_ifindex = attr->ifindex;
4842 expected_attach_type = attr->expected_attach_type;
4843 if (prog_type == BPF_PROG_TYPE_UNSPEC) {
4844 err = bpf_program__identify_section(prog, &prog_type,
4845 &expected_attach_type);
4846 if (err < 0) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004847 bpf_object__close(obj);
4848 return -EINVAL;
4849 }
4850 }
4851
4852 bpf_program__set_type(prog, prog_type);
4853 bpf_program__set_expected_attach_type(prog,
4854 expected_attach_type);
4855
David Brazdil0f672f62019-12-10 10:32:29 +00004856 prog->log_level = attr->log_level;
4857 prog->prog_flags = attr->prog_flags;
4858 if (!first_prog)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004859 first_prog = prog;
4860 }
4861
David Brazdil0f672f62019-12-10 10:32:29 +00004862 bpf_object__for_each_map(map, obj) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004863 if (!bpf_map__is_offload_neutral(map))
4864 map->map_ifindex = attr->ifindex;
4865 }
4866
4867 if (!first_prog) {
4868 pr_warning("object file doesn't contain bpf program\n");
4869 bpf_object__close(obj);
4870 return -ENOENT;
4871 }
4872
4873 err = bpf_object__load(obj);
4874 if (err) {
4875 bpf_object__close(obj);
4876 return -EINVAL;
4877 }
4878
4879 *pobj = obj;
4880 *prog_fd = bpf_program__fd(first_prog);
4881 return 0;
4882}
4883
David Brazdil0f672f62019-12-10 10:32:29 +00004884struct bpf_link {
4885 int (*destroy)(struct bpf_link *link);
4886};
4887
4888int bpf_link__destroy(struct bpf_link *link)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004889{
David Brazdil0f672f62019-12-10 10:32:29 +00004890 int err;
4891
4892 if (!link)
4893 return 0;
4894
4895 err = link->destroy(link);
4896 free(link);
4897
4898 return err;
4899}
4900
4901struct bpf_link_fd {
4902 struct bpf_link link; /* has to be at the top of struct */
4903 int fd; /* hook FD */
4904};
4905
4906static int bpf_link__destroy_perf_event(struct bpf_link *link)
4907{
4908 struct bpf_link_fd *l = (void *)link;
4909 int err;
4910
4911 err = ioctl(l->fd, PERF_EVENT_IOC_DISABLE, 0);
4912 if (err)
4913 err = -errno;
4914
4915 close(l->fd);
4916 return err;
4917}
4918
4919struct bpf_link *bpf_program__attach_perf_event(struct bpf_program *prog,
4920 int pfd)
4921{
4922 char errmsg[STRERR_BUFSIZE];
4923 struct bpf_link_fd *link;
4924 int prog_fd, err;
4925
4926 if (pfd < 0) {
4927 pr_warning("program '%s': invalid perf event FD %d\n",
4928 bpf_program__title(prog, false), pfd);
4929 return ERR_PTR(-EINVAL);
4930 }
4931 prog_fd = bpf_program__fd(prog);
4932 if (prog_fd < 0) {
4933 pr_warning("program '%s': can't attach BPF program w/o FD (did you load it?)\n",
4934 bpf_program__title(prog, false));
4935 return ERR_PTR(-EINVAL);
4936 }
4937
4938 link = malloc(sizeof(*link));
4939 if (!link)
4940 return ERR_PTR(-ENOMEM);
4941 link->link.destroy = &bpf_link__destroy_perf_event;
4942 link->fd = pfd;
4943
4944 if (ioctl(pfd, PERF_EVENT_IOC_SET_BPF, prog_fd) < 0) {
4945 err = -errno;
4946 free(link);
4947 pr_warning("program '%s': failed to attach to pfd %d: %s\n",
4948 bpf_program__title(prog, false), pfd,
4949 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
4950 return ERR_PTR(err);
4951 }
4952 if (ioctl(pfd, PERF_EVENT_IOC_ENABLE, 0) < 0) {
4953 err = -errno;
4954 free(link);
4955 pr_warning("program '%s': failed to enable pfd %d: %s\n",
4956 bpf_program__title(prog, false), pfd,
4957 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
4958 return ERR_PTR(err);
4959 }
4960 return (struct bpf_link *)link;
4961}
4962
4963/*
4964 * this function is expected to parse integer in the range of [0, 2^31-1] from
4965 * given file using scanf format string fmt. If actual parsed value is
4966 * negative, the result might be indistinguishable from error
4967 */
4968static int parse_uint_from_file(const char *file, const char *fmt)
4969{
4970 char buf[STRERR_BUFSIZE];
4971 int err, ret;
4972 FILE *f;
4973
4974 f = fopen(file, "r");
4975 if (!f) {
4976 err = -errno;
4977 pr_debug("failed to open '%s': %s\n", file,
4978 libbpf_strerror_r(err, buf, sizeof(buf)));
4979 return err;
4980 }
4981 err = fscanf(f, fmt, &ret);
4982 if (err != 1) {
4983 err = err == EOF ? -EIO : -errno;
4984 pr_debug("failed to parse '%s': %s\n", file,
4985 libbpf_strerror_r(err, buf, sizeof(buf)));
4986 fclose(f);
4987 return err;
4988 }
4989 fclose(f);
4990 return ret;
4991}
4992
4993static int determine_kprobe_perf_type(void)
4994{
4995 const char *file = "/sys/bus/event_source/devices/kprobe/type";
4996
4997 return parse_uint_from_file(file, "%d\n");
4998}
4999
5000static int determine_uprobe_perf_type(void)
5001{
5002 const char *file = "/sys/bus/event_source/devices/uprobe/type";
5003
5004 return parse_uint_from_file(file, "%d\n");
5005}
5006
5007static int determine_kprobe_retprobe_bit(void)
5008{
5009 const char *file = "/sys/bus/event_source/devices/kprobe/format/retprobe";
5010
5011 return parse_uint_from_file(file, "config:%d\n");
5012}
5013
5014static int determine_uprobe_retprobe_bit(void)
5015{
5016 const char *file = "/sys/bus/event_source/devices/uprobe/format/retprobe";
5017
5018 return parse_uint_from_file(file, "config:%d\n");
5019}
5020
5021static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,
5022 uint64_t offset, int pid)
5023{
5024 struct perf_event_attr attr = {};
5025 char errmsg[STRERR_BUFSIZE];
5026 int type, pfd, err;
5027
5028 type = uprobe ? determine_uprobe_perf_type()
5029 : determine_kprobe_perf_type();
5030 if (type < 0) {
5031 pr_warning("failed to determine %s perf type: %s\n",
5032 uprobe ? "uprobe" : "kprobe",
5033 libbpf_strerror_r(type, errmsg, sizeof(errmsg)));
5034 return type;
5035 }
5036 if (retprobe) {
5037 int bit = uprobe ? determine_uprobe_retprobe_bit()
5038 : determine_kprobe_retprobe_bit();
5039
5040 if (bit < 0) {
5041 pr_warning("failed to determine %s retprobe bit: %s\n",
5042 uprobe ? "uprobe" : "kprobe",
5043 libbpf_strerror_r(bit, errmsg,
5044 sizeof(errmsg)));
5045 return bit;
5046 }
5047 attr.config |= 1 << bit;
5048 }
5049 attr.size = sizeof(attr);
5050 attr.type = type;
5051 attr.config1 = ptr_to_u64(name); /* kprobe_func or uprobe_path */
5052 attr.config2 = offset; /* kprobe_addr or probe_offset */
5053
5054 /* pid filter is meaningful only for uprobes */
5055 pfd = syscall(__NR_perf_event_open, &attr,
5056 pid < 0 ? -1 : pid /* pid */,
5057 pid == -1 ? 0 : -1 /* cpu */,
5058 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC);
5059 if (pfd < 0) {
5060 err = -errno;
5061 pr_warning("%s perf_event_open() failed: %s\n",
5062 uprobe ? "uprobe" : "kprobe",
5063 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
5064 return err;
5065 }
5066 return pfd;
5067}
5068
5069struct bpf_link *bpf_program__attach_kprobe(struct bpf_program *prog,
5070 bool retprobe,
5071 const char *func_name)
5072{
5073 char errmsg[STRERR_BUFSIZE];
5074 struct bpf_link *link;
5075 int pfd, err;
5076
5077 pfd = perf_event_open_probe(false /* uprobe */, retprobe, func_name,
5078 0 /* offset */, -1 /* pid */);
5079 if (pfd < 0) {
5080 pr_warning("program '%s': failed to create %s '%s' perf event: %s\n",
5081 bpf_program__title(prog, false),
5082 retprobe ? "kretprobe" : "kprobe", func_name,
5083 libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
5084 return ERR_PTR(pfd);
5085 }
5086 link = bpf_program__attach_perf_event(prog, pfd);
5087 if (IS_ERR(link)) {
5088 close(pfd);
5089 err = PTR_ERR(link);
5090 pr_warning("program '%s': failed to attach to %s '%s': %s\n",
5091 bpf_program__title(prog, false),
5092 retprobe ? "kretprobe" : "kprobe", func_name,
5093 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
5094 return link;
5095 }
5096 return link;
5097}
5098
5099struct bpf_link *bpf_program__attach_uprobe(struct bpf_program *prog,
5100 bool retprobe, pid_t pid,
5101 const char *binary_path,
5102 size_t func_offset)
5103{
5104 char errmsg[STRERR_BUFSIZE];
5105 struct bpf_link *link;
5106 int pfd, err;
5107
5108 pfd = perf_event_open_probe(true /* uprobe */, retprobe,
5109 binary_path, func_offset, pid);
5110 if (pfd < 0) {
5111 pr_warning("program '%s': failed to create %s '%s:0x%zx' perf event: %s\n",
5112 bpf_program__title(prog, false),
5113 retprobe ? "uretprobe" : "uprobe",
5114 binary_path, func_offset,
5115 libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
5116 return ERR_PTR(pfd);
5117 }
5118 link = bpf_program__attach_perf_event(prog, pfd);
5119 if (IS_ERR(link)) {
5120 close(pfd);
5121 err = PTR_ERR(link);
5122 pr_warning("program '%s': failed to attach to %s '%s:0x%zx': %s\n",
5123 bpf_program__title(prog, false),
5124 retprobe ? "uretprobe" : "uprobe",
5125 binary_path, func_offset,
5126 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
5127 return link;
5128 }
5129 return link;
5130}
5131
5132static int determine_tracepoint_id(const char *tp_category,
5133 const char *tp_name)
5134{
5135 char file[PATH_MAX];
5136 int ret;
5137
5138 ret = snprintf(file, sizeof(file),
5139 "/sys/kernel/debug/tracing/events/%s/%s/id",
5140 tp_category, tp_name);
5141 if (ret < 0)
5142 return -errno;
5143 if (ret >= sizeof(file)) {
5144 pr_debug("tracepoint %s/%s path is too long\n",
5145 tp_category, tp_name);
5146 return -E2BIG;
5147 }
5148 return parse_uint_from_file(file, "%d\n");
5149}
5150
5151static int perf_event_open_tracepoint(const char *tp_category,
5152 const char *tp_name)
5153{
5154 struct perf_event_attr attr = {};
5155 char errmsg[STRERR_BUFSIZE];
5156 int tp_id, pfd, err;
5157
5158 tp_id = determine_tracepoint_id(tp_category, tp_name);
5159 if (tp_id < 0) {
5160 pr_warning("failed to determine tracepoint '%s/%s' perf event ID: %s\n",
5161 tp_category, tp_name,
5162 libbpf_strerror_r(tp_id, errmsg, sizeof(errmsg)));
5163 return tp_id;
5164 }
5165
5166 attr.type = PERF_TYPE_TRACEPOINT;
5167 attr.size = sizeof(attr);
5168 attr.config = tp_id;
5169
5170 pfd = syscall(__NR_perf_event_open, &attr, -1 /* pid */, 0 /* cpu */,
5171 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC);
5172 if (pfd < 0) {
5173 err = -errno;
5174 pr_warning("tracepoint '%s/%s' perf_event_open() failed: %s\n",
5175 tp_category, tp_name,
5176 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
5177 return err;
5178 }
5179 return pfd;
5180}
5181
5182struct bpf_link *bpf_program__attach_tracepoint(struct bpf_program *prog,
5183 const char *tp_category,
5184 const char *tp_name)
5185{
5186 char errmsg[STRERR_BUFSIZE];
5187 struct bpf_link *link;
5188 int pfd, err;
5189
5190 pfd = perf_event_open_tracepoint(tp_category, tp_name);
5191 if (pfd < 0) {
5192 pr_warning("program '%s': failed to create tracepoint '%s/%s' perf event: %s\n",
5193 bpf_program__title(prog, false),
5194 tp_category, tp_name,
5195 libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
5196 return ERR_PTR(pfd);
5197 }
5198 link = bpf_program__attach_perf_event(prog, pfd);
5199 if (IS_ERR(link)) {
5200 close(pfd);
5201 err = PTR_ERR(link);
5202 pr_warning("program '%s': failed to attach to tracepoint '%s/%s': %s\n",
5203 bpf_program__title(prog, false),
5204 tp_category, tp_name,
5205 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
5206 return link;
5207 }
5208 return link;
5209}
5210
5211static int bpf_link__destroy_fd(struct bpf_link *link)
5212{
5213 struct bpf_link_fd *l = (void *)link;
5214
5215 return close(l->fd);
5216}
5217
5218struct bpf_link *bpf_program__attach_raw_tracepoint(struct bpf_program *prog,
5219 const char *tp_name)
5220{
5221 char errmsg[STRERR_BUFSIZE];
5222 struct bpf_link_fd *link;
5223 int prog_fd, pfd;
5224
5225 prog_fd = bpf_program__fd(prog);
5226 if (prog_fd < 0) {
5227 pr_warning("program '%s': can't attach before loaded\n",
5228 bpf_program__title(prog, false));
5229 return ERR_PTR(-EINVAL);
5230 }
5231
5232 link = malloc(sizeof(*link));
5233 if (!link)
5234 return ERR_PTR(-ENOMEM);
5235 link->link.destroy = &bpf_link__destroy_fd;
5236
5237 pfd = bpf_raw_tracepoint_open(tp_name, prog_fd);
5238 if (pfd < 0) {
5239 pfd = -errno;
5240 free(link);
5241 pr_warning("program '%s': failed to attach to raw tracepoint '%s': %s\n",
5242 bpf_program__title(prog, false), tp_name,
5243 libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
5244 return ERR_PTR(pfd);
5245 }
5246 link->fd = pfd;
5247 return (struct bpf_link *)link;
5248}
5249
5250enum bpf_perf_event_ret
5251bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
5252 void **copy_mem, size_t *copy_size,
5253 bpf_perf_event_print_t fn, void *private_data)
5254{
5255 struct perf_event_mmap_page *header = mmap_mem;
5256 __u64 data_head = ring_buffer_read_head(header);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005257 __u64 data_tail = header->data_tail;
David Brazdil0f672f62019-12-10 10:32:29 +00005258 void *base = ((__u8 *)header) + page_size;
5259 int ret = LIBBPF_PERF_EVENT_CONT;
5260 struct perf_event_header *ehdr;
5261 size_t ehdr_size;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005262
David Brazdil0f672f62019-12-10 10:32:29 +00005263 while (data_head != data_tail) {
5264 ehdr = base + (data_tail & (mmap_size - 1));
5265 ehdr_size = ehdr->size;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005266
David Brazdil0f672f62019-12-10 10:32:29 +00005267 if (((void *)ehdr) + ehdr_size > base + mmap_size) {
5268 void *copy_start = ehdr;
5269 size_t len_first = base + mmap_size - copy_start;
5270 size_t len_secnd = ehdr_size - len_first;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005271
David Brazdil0f672f62019-12-10 10:32:29 +00005272 if (*copy_size < ehdr_size) {
5273 free(*copy_mem);
5274 *copy_mem = malloc(ehdr_size);
5275 if (!*copy_mem) {
5276 *copy_size = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005277 ret = LIBBPF_PERF_EVENT_ERROR;
5278 break;
5279 }
David Brazdil0f672f62019-12-10 10:32:29 +00005280 *copy_size = ehdr_size;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005281 }
5282
David Brazdil0f672f62019-12-10 10:32:29 +00005283 memcpy(*copy_mem, copy_start, len_first);
5284 memcpy(*copy_mem + len_first, base, len_secnd);
5285 ehdr = *copy_mem;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005286 }
5287
David Brazdil0f672f62019-12-10 10:32:29 +00005288 ret = fn(ehdr, private_data);
5289 data_tail += ehdr_size;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005290 if (ret != LIBBPF_PERF_EVENT_CONT)
5291 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005292 }
5293
David Brazdil0f672f62019-12-10 10:32:29 +00005294 ring_buffer_write_tail(header, data_tail);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005295 return ret;
5296}
David Brazdil0f672f62019-12-10 10:32:29 +00005297
5298struct perf_buffer;
5299
5300struct perf_buffer_params {
5301 struct perf_event_attr *attr;
5302 /* if event_cb is specified, it takes precendence */
5303 perf_buffer_event_fn event_cb;
5304 /* sample_cb and lost_cb are higher-level common-case callbacks */
5305 perf_buffer_sample_fn sample_cb;
5306 perf_buffer_lost_fn lost_cb;
5307 void *ctx;
5308 int cpu_cnt;
5309 int *cpus;
5310 int *map_keys;
5311};
5312
5313struct perf_cpu_buf {
5314 struct perf_buffer *pb;
5315 void *base; /* mmap()'ed memory */
5316 void *buf; /* for reconstructing segmented data */
5317 size_t buf_size;
5318 int fd;
5319 int cpu;
5320 int map_key;
5321};
5322
5323struct perf_buffer {
5324 perf_buffer_event_fn event_cb;
5325 perf_buffer_sample_fn sample_cb;
5326 perf_buffer_lost_fn lost_cb;
5327 void *ctx; /* passed into callbacks */
5328
5329 size_t page_size;
5330 size_t mmap_size;
5331 struct perf_cpu_buf **cpu_bufs;
5332 struct epoll_event *events;
5333 int cpu_cnt;
5334 int epoll_fd; /* perf event FD */
5335 int map_fd; /* BPF_MAP_TYPE_PERF_EVENT_ARRAY BPF map FD */
5336};
5337
5338static void perf_buffer__free_cpu_buf(struct perf_buffer *pb,
5339 struct perf_cpu_buf *cpu_buf)
5340{
5341 if (!cpu_buf)
5342 return;
5343 if (cpu_buf->base &&
5344 munmap(cpu_buf->base, pb->mmap_size + pb->page_size))
5345 pr_warning("failed to munmap cpu_buf #%d\n", cpu_buf->cpu);
5346 if (cpu_buf->fd >= 0) {
5347 ioctl(cpu_buf->fd, PERF_EVENT_IOC_DISABLE, 0);
5348 close(cpu_buf->fd);
5349 }
5350 free(cpu_buf->buf);
5351 free(cpu_buf);
5352}
5353
5354void perf_buffer__free(struct perf_buffer *pb)
5355{
5356 int i;
5357
5358 if (!pb)
5359 return;
5360 if (pb->cpu_bufs) {
Olivier Deprez0e641232021-09-23 10:07:05 +02005361 for (i = 0; i < pb->cpu_cnt; i++) {
David Brazdil0f672f62019-12-10 10:32:29 +00005362 struct perf_cpu_buf *cpu_buf = pb->cpu_bufs[i];
5363
Olivier Deprez0e641232021-09-23 10:07:05 +02005364 if (!cpu_buf)
5365 continue;
5366
David Brazdil0f672f62019-12-10 10:32:29 +00005367 bpf_map_delete_elem(pb->map_fd, &cpu_buf->map_key);
5368 perf_buffer__free_cpu_buf(pb, cpu_buf);
5369 }
5370 free(pb->cpu_bufs);
5371 }
5372 if (pb->epoll_fd >= 0)
5373 close(pb->epoll_fd);
5374 free(pb->events);
5375 free(pb);
5376}
5377
5378static struct perf_cpu_buf *
5379perf_buffer__open_cpu_buf(struct perf_buffer *pb, struct perf_event_attr *attr,
5380 int cpu, int map_key)
5381{
5382 struct perf_cpu_buf *cpu_buf;
5383 char msg[STRERR_BUFSIZE];
5384 int err;
5385
5386 cpu_buf = calloc(1, sizeof(*cpu_buf));
5387 if (!cpu_buf)
5388 return ERR_PTR(-ENOMEM);
5389
5390 cpu_buf->pb = pb;
5391 cpu_buf->cpu = cpu;
5392 cpu_buf->map_key = map_key;
5393
5394 cpu_buf->fd = syscall(__NR_perf_event_open, attr, -1 /* pid */, cpu,
5395 -1, PERF_FLAG_FD_CLOEXEC);
5396 if (cpu_buf->fd < 0) {
5397 err = -errno;
5398 pr_warning("failed to open perf buffer event on cpu #%d: %s\n",
5399 cpu, libbpf_strerror_r(err, msg, sizeof(msg)));
5400 goto error;
5401 }
5402
5403 cpu_buf->base = mmap(NULL, pb->mmap_size + pb->page_size,
5404 PROT_READ | PROT_WRITE, MAP_SHARED,
5405 cpu_buf->fd, 0);
5406 if (cpu_buf->base == MAP_FAILED) {
5407 cpu_buf->base = NULL;
5408 err = -errno;
5409 pr_warning("failed to mmap perf buffer on cpu #%d: %s\n",
5410 cpu, libbpf_strerror_r(err, msg, sizeof(msg)));
5411 goto error;
5412 }
5413
5414 if (ioctl(cpu_buf->fd, PERF_EVENT_IOC_ENABLE, 0) < 0) {
5415 err = -errno;
5416 pr_warning("failed to enable perf buffer event on cpu #%d: %s\n",
5417 cpu, libbpf_strerror_r(err, msg, sizeof(msg)));
5418 goto error;
5419 }
5420
5421 return cpu_buf;
5422
5423error:
5424 perf_buffer__free_cpu_buf(pb, cpu_buf);
5425 return (struct perf_cpu_buf *)ERR_PTR(err);
5426}
5427
5428static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt,
5429 struct perf_buffer_params *p);
5430
5431struct perf_buffer *perf_buffer__new(int map_fd, size_t page_cnt,
5432 const struct perf_buffer_opts *opts)
5433{
5434 struct perf_buffer_params p = {};
5435 struct perf_event_attr attr = { 0, };
5436
5437 attr.config = PERF_COUNT_SW_BPF_OUTPUT,
5438 attr.type = PERF_TYPE_SOFTWARE;
5439 attr.sample_type = PERF_SAMPLE_RAW;
5440 attr.sample_period = 1;
5441 attr.wakeup_events = 1;
5442
5443 p.attr = &attr;
5444 p.sample_cb = opts ? opts->sample_cb : NULL;
5445 p.lost_cb = opts ? opts->lost_cb : NULL;
5446 p.ctx = opts ? opts->ctx : NULL;
5447
5448 return __perf_buffer__new(map_fd, page_cnt, &p);
5449}
5450
5451struct perf_buffer *
5452perf_buffer__new_raw(int map_fd, size_t page_cnt,
5453 const struct perf_buffer_raw_opts *opts)
5454{
5455 struct perf_buffer_params p = {};
5456
5457 p.attr = opts->attr;
5458 p.event_cb = opts->event_cb;
5459 p.ctx = opts->ctx;
5460 p.cpu_cnt = opts->cpu_cnt;
5461 p.cpus = opts->cpus;
5462 p.map_keys = opts->map_keys;
5463
5464 return __perf_buffer__new(map_fd, page_cnt, &p);
5465}
5466
5467static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt,
5468 struct perf_buffer_params *p)
5469{
5470 struct bpf_map_info map = {};
5471 char msg[STRERR_BUFSIZE];
5472 struct perf_buffer *pb;
5473 __u32 map_info_len;
5474 int err, i;
5475
5476 if (page_cnt & (page_cnt - 1)) {
5477 pr_warning("page count should be power of two, but is %zu\n",
5478 page_cnt);
5479 return ERR_PTR(-EINVAL);
5480 }
5481
5482 map_info_len = sizeof(map);
5483 err = bpf_obj_get_info_by_fd(map_fd, &map, &map_info_len);
5484 if (err) {
5485 err = -errno;
5486 pr_warning("failed to get map info for map FD %d: %s\n",
5487 map_fd, libbpf_strerror_r(err, msg, sizeof(msg)));
5488 return ERR_PTR(err);
5489 }
5490
5491 if (map.type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) {
5492 pr_warning("map '%s' should be BPF_MAP_TYPE_PERF_EVENT_ARRAY\n",
5493 map.name);
5494 return ERR_PTR(-EINVAL);
5495 }
5496
5497 pb = calloc(1, sizeof(*pb));
5498 if (!pb)
5499 return ERR_PTR(-ENOMEM);
5500
5501 pb->event_cb = p->event_cb;
5502 pb->sample_cb = p->sample_cb;
5503 pb->lost_cb = p->lost_cb;
5504 pb->ctx = p->ctx;
5505
5506 pb->page_size = getpagesize();
5507 pb->mmap_size = pb->page_size * page_cnt;
5508 pb->map_fd = map_fd;
5509
5510 pb->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
5511 if (pb->epoll_fd < 0) {
5512 err = -errno;
5513 pr_warning("failed to create epoll instance: %s\n",
5514 libbpf_strerror_r(err, msg, sizeof(msg)));
5515 goto error;
5516 }
5517
5518 if (p->cpu_cnt > 0) {
5519 pb->cpu_cnt = p->cpu_cnt;
5520 } else {
5521 pb->cpu_cnt = libbpf_num_possible_cpus();
5522 if (pb->cpu_cnt < 0) {
5523 err = pb->cpu_cnt;
5524 goto error;
5525 }
5526 if (map.max_entries < pb->cpu_cnt)
5527 pb->cpu_cnt = map.max_entries;
5528 }
5529
5530 pb->events = calloc(pb->cpu_cnt, sizeof(*pb->events));
5531 if (!pb->events) {
5532 err = -ENOMEM;
5533 pr_warning("failed to allocate events: out of memory\n");
5534 goto error;
5535 }
5536 pb->cpu_bufs = calloc(pb->cpu_cnt, sizeof(*pb->cpu_bufs));
5537 if (!pb->cpu_bufs) {
5538 err = -ENOMEM;
5539 pr_warning("failed to allocate buffers: out of memory\n");
5540 goto error;
5541 }
5542
5543 for (i = 0; i < pb->cpu_cnt; i++) {
5544 struct perf_cpu_buf *cpu_buf;
5545 int cpu, map_key;
5546
5547 cpu = p->cpu_cnt > 0 ? p->cpus[i] : i;
5548 map_key = p->cpu_cnt > 0 ? p->map_keys[i] : i;
5549
5550 cpu_buf = perf_buffer__open_cpu_buf(pb, p->attr, cpu, map_key);
5551 if (IS_ERR(cpu_buf)) {
5552 err = PTR_ERR(cpu_buf);
5553 goto error;
5554 }
5555
5556 pb->cpu_bufs[i] = cpu_buf;
5557
5558 err = bpf_map_update_elem(pb->map_fd, &map_key,
5559 &cpu_buf->fd, 0);
5560 if (err) {
5561 err = -errno;
5562 pr_warning("failed to set cpu #%d, key %d -> perf FD %d: %s\n",
5563 cpu, map_key, cpu_buf->fd,
5564 libbpf_strerror_r(err, msg, sizeof(msg)));
5565 goto error;
5566 }
5567
5568 pb->events[i].events = EPOLLIN;
5569 pb->events[i].data.ptr = cpu_buf;
5570 if (epoll_ctl(pb->epoll_fd, EPOLL_CTL_ADD, cpu_buf->fd,
5571 &pb->events[i]) < 0) {
5572 err = -errno;
5573 pr_warning("failed to epoll_ctl cpu #%d perf FD %d: %s\n",
5574 cpu, cpu_buf->fd,
5575 libbpf_strerror_r(err, msg, sizeof(msg)));
5576 goto error;
5577 }
5578 }
5579
5580 return pb;
5581
5582error:
5583 if (pb)
5584 perf_buffer__free(pb);
5585 return ERR_PTR(err);
5586}
5587
5588struct perf_sample_raw {
5589 struct perf_event_header header;
5590 uint32_t size;
5591 char data[0];
5592};
5593
5594struct perf_sample_lost {
5595 struct perf_event_header header;
5596 uint64_t id;
5597 uint64_t lost;
5598 uint64_t sample_id;
5599};
5600
5601static enum bpf_perf_event_ret
5602perf_buffer__process_record(struct perf_event_header *e, void *ctx)
5603{
5604 struct perf_cpu_buf *cpu_buf = ctx;
5605 struct perf_buffer *pb = cpu_buf->pb;
5606 void *data = e;
5607
5608 /* user wants full control over parsing perf event */
5609 if (pb->event_cb)
5610 return pb->event_cb(pb->ctx, cpu_buf->cpu, e);
5611
5612 switch (e->type) {
5613 case PERF_RECORD_SAMPLE: {
5614 struct perf_sample_raw *s = data;
5615
5616 if (pb->sample_cb)
5617 pb->sample_cb(pb->ctx, cpu_buf->cpu, s->data, s->size);
5618 break;
5619 }
5620 case PERF_RECORD_LOST: {
5621 struct perf_sample_lost *s = data;
5622
5623 if (pb->lost_cb)
5624 pb->lost_cb(pb->ctx, cpu_buf->cpu, s->lost);
5625 break;
5626 }
5627 default:
5628 pr_warning("unknown perf sample type %d\n", e->type);
5629 return LIBBPF_PERF_EVENT_ERROR;
5630 }
5631 return LIBBPF_PERF_EVENT_CONT;
5632}
5633
5634static int perf_buffer__process_records(struct perf_buffer *pb,
5635 struct perf_cpu_buf *cpu_buf)
5636{
5637 enum bpf_perf_event_ret ret;
5638
5639 ret = bpf_perf_event_read_simple(cpu_buf->base, pb->mmap_size,
5640 pb->page_size, &cpu_buf->buf,
5641 &cpu_buf->buf_size,
5642 perf_buffer__process_record, cpu_buf);
5643 if (ret != LIBBPF_PERF_EVENT_CONT)
5644 return ret;
5645 return 0;
5646}
5647
5648int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms)
5649{
5650 int i, cnt, err;
5651
5652 cnt = epoll_wait(pb->epoll_fd, pb->events, pb->cpu_cnt, timeout_ms);
5653 for (i = 0; i < cnt; i++) {
5654 struct perf_cpu_buf *cpu_buf = pb->events[i].data.ptr;
5655
5656 err = perf_buffer__process_records(pb, cpu_buf);
5657 if (err) {
5658 pr_warning("error while processing records: %d\n", err);
5659 return err;
5660 }
5661 }
5662 return cnt < 0 ? -errno : cnt;
5663}
5664
5665struct bpf_prog_info_array_desc {
5666 int array_offset; /* e.g. offset of jited_prog_insns */
5667 int count_offset; /* e.g. offset of jited_prog_len */
5668 int size_offset; /* > 0: offset of rec size,
5669 * < 0: fix size of -size_offset
5670 */
5671};
5672
5673static struct bpf_prog_info_array_desc bpf_prog_info_array_desc[] = {
5674 [BPF_PROG_INFO_JITED_INSNS] = {
5675 offsetof(struct bpf_prog_info, jited_prog_insns),
5676 offsetof(struct bpf_prog_info, jited_prog_len),
5677 -1,
5678 },
5679 [BPF_PROG_INFO_XLATED_INSNS] = {
5680 offsetof(struct bpf_prog_info, xlated_prog_insns),
5681 offsetof(struct bpf_prog_info, xlated_prog_len),
5682 -1,
5683 },
5684 [BPF_PROG_INFO_MAP_IDS] = {
5685 offsetof(struct bpf_prog_info, map_ids),
5686 offsetof(struct bpf_prog_info, nr_map_ids),
5687 -(int)sizeof(__u32),
5688 },
5689 [BPF_PROG_INFO_JITED_KSYMS] = {
5690 offsetof(struct bpf_prog_info, jited_ksyms),
5691 offsetof(struct bpf_prog_info, nr_jited_ksyms),
5692 -(int)sizeof(__u64),
5693 },
5694 [BPF_PROG_INFO_JITED_FUNC_LENS] = {
5695 offsetof(struct bpf_prog_info, jited_func_lens),
5696 offsetof(struct bpf_prog_info, nr_jited_func_lens),
5697 -(int)sizeof(__u32),
5698 },
5699 [BPF_PROG_INFO_FUNC_INFO] = {
5700 offsetof(struct bpf_prog_info, func_info),
5701 offsetof(struct bpf_prog_info, nr_func_info),
5702 offsetof(struct bpf_prog_info, func_info_rec_size),
5703 },
5704 [BPF_PROG_INFO_LINE_INFO] = {
5705 offsetof(struct bpf_prog_info, line_info),
5706 offsetof(struct bpf_prog_info, nr_line_info),
5707 offsetof(struct bpf_prog_info, line_info_rec_size),
5708 },
5709 [BPF_PROG_INFO_JITED_LINE_INFO] = {
5710 offsetof(struct bpf_prog_info, jited_line_info),
5711 offsetof(struct bpf_prog_info, nr_jited_line_info),
5712 offsetof(struct bpf_prog_info, jited_line_info_rec_size),
5713 },
5714 [BPF_PROG_INFO_PROG_TAGS] = {
5715 offsetof(struct bpf_prog_info, prog_tags),
5716 offsetof(struct bpf_prog_info, nr_prog_tags),
5717 -(int)sizeof(__u8) * BPF_TAG_SIZE,
5718 },
5719
5720};
5721
5722static __u32 bpf_prog_info_read_offset_u32(struct bpf_prog_info *info, int offset)
5723{
5724 __u32 *array = (__u32 *)info;
5725
5726 if (offset >= 0)
5727 return array[offset / sizeof(__u32)];
5728 return -(int)offset;
5729}
5730
5731static __u64 bpf_prog_info_read_offset_u64(struct bpf_prog_info *info, int offset)
5732{
5733 __u64 *array = (__u64 *)info;
5734
5735 if (offset >= 0)
5736 return array[offset / sizeof(__u64)];
5737 return -(int)offset;
5738}
5739
5740static void bpf_prog_info_set_offset_u32(struct bpf_prog_info *info, int offset,
5741 __u32 val)
5742{
5743 __u32 *array = (__u32 *)info;
5744
5745 if (offset >= 0)
5746 array[offset / sizeof(__u32)] = val;
5747}
5748
5749static void bpf_prog_info_set_offset_u64(struct bpf_prog_info *info, int offset,
5750 __u64 val)
5751{
5752 __u64 *array = (__u64 *)info;
5753
5754 if (offset >= 0)
5755 array[offset / sizeof(__u64)] = val;
5756}
5757
5758struct bpf_prog_info_linear *
5759bpf_program__get_prog_info_linear(int fd, __u64 arrays)
5760{
5761 struct bpf_prog_info_linear *info_linear;
5762 struct bpf_prog_info info = {};
5763 __u32 info_len = sizeof(info);
5764 __u32 data_len = 0;
5765 int i, err;
5766 void *ptr;
5767
5768 if (arrays >> BPF_PROG_INFO_LAST_ARRAY)
5769 return ERR_PTR(-EINVAL);
5770
5771 /* step 1: get array dimensions */
5772 err = bpf_obj_get_info_by_fd(fd, &info, &info_len);
5773 if (err) {
5774 pr_debug("can't get prog info: %s", strerror(errno));
5775 return ERR_PTR(-EFAULT);
5776 }
5777
5778 /* step 2: calculate total size of all arrays */
5779 for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
5780 bool include_array = (arrays & (1UL << i)) > 0;
5781 struct bpf_prog_info_array_desc *desc;
5782 __u32 count, size;
5783
5784 desc = bpf_prog_info_array_desc + i;
5785
5786 /* kernel is too old to support this field */
5787 if (info_len < desc->array_offset + sizeof(__u32) ||
5788 info_len < desc->count_offset + sizeof(__u32) ||
5789 (desc->size_offset > 0 && info_len < desc->size_offset))
5790 include_array = false;
5791
5792 if (!include_array) {
5793 arrays &= ~(1UL << i); /* clear the bit */
5794 continue;
5795 }
5796
5797 count = bpf_prog_info_read_offset_u32(&info, desc->count_offset);
5798 size = bpf_prog_info_read_offset_u32(&info, desc->size_offset);
5799
5800 data_len += count * size;
5801 }
5802
5803 /* step 3: allocate continuous memory */
5804 data_len = roundup(data_len, sizeof(__u64));
5805 info_linear = malloc(sizeof(struct bpf_prog_info_linear) + data_len);
5806 if (!info_linear)
5807 return ERR_PTR(-ENOMEM);
5808
5809 /* step 4: fill data to info_linear->info */
5810 info_linear->arrays = arrays;
5811 memset(&info_linear->info, 0, sizeof(info));
5812 ptr = info_linear->data;
5813
5814 for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
5815 struct bpf_prog_info_array_desc *desc;
5816 __u32 count, size;
5817
5818 if ((arrays & (1UL << i)) == 0)
5819 continue;
5820
5821 desc = bpf_prog_info_array_desc + i;
5822 count = bpf_prog_info_read_offset_u32(&info, desc->count_offset);
5823 size = bpf_prog_info_read_offset_u32(&info, desc->size_offset);
5824 bpf_prog_info_set_offset_u32(&info_linear->info,
5825 desc->count_offset, count);
5826 bpf_prog_info_set_offset_u32(&info_linear->info,
5827 desc->size_offset, size);
5828 bpf_prog_info_set_offset_u64(&info_linear->info,
5829 desc->array_offset,
5830 ptr_to_u64(ptr));
5831 ptr += count * size;
5832 }
5833
5834 /* step 5: call syscall again to get required arrays */
5835 err = bpf_obj_get_info_by_fd(fd, &info_linear->info, &info_len);
5836 if (err) {
5837 pr_debug("can't get prog info: %s", strerror(errno));
5838 free(info_linear);
5839 return ERR_PTR(-EFAULT);
5840 }
5841
5842 /* step 6: verify the data */
5843 for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
5844 struct bpf_prog_info_array_desc *desc;
5845 __u32 v1, v2;
5846
5847 if ((arrays & (1UL << i)) == 0)
5848 continue;
5849
5850 desc = bpf_prog_info_array_desc + i;
5851 v1 = bpf_prog_info_read_offset_u32(&info, desc->count_offset);
5852 v2 = bpf_prog_info_read_offset_u32(&info_linear->info,
5853 desc->count_offset);
5854 if (v1 != v2)
5855 pr_warning("%s: mismatch in element count\n", __func__);
5856
5857 v1 = bpf_prog_info_read_offset_u32(&info, desc->size_offset);
5858 v2 = bpf_prog_info_read_offset_u32(&info_linear->info,
5859 desc->size_offset);
5860 if (v1 != v2)
5861 pr_warning("%s: mismatch in rec size\n", __func__);
5862 }
5863
5864 /* step 7: update info_len and data_len */
5865 info_linear->info_len = sizeof(struct bpf_prog_info);
5866 info_linear->data_len = data_len;
5867
5868 return info_linear;
5869}
5870
5871void bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear *info_linear)
5872{
5873 int i;
5874
5875 for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
5876 struct bpf_prog_info_array_desc *desc;
5877 __u64 addr, offs;
5878
5879 if ((info_linear->arrays & (1UL << i)) == 0)
5880 continue;
5881
5882 desc = bpf_prog_info_array_desc + i;
5883 addr = bpf_prog_info_read_offset_u64(&info_linear->info,
5884 desc->array_offset);
5885 offs = addr - ptr_to_u64(info_linear->data);
5886 bpf_prog_info_set_offset_u64(&info_linear->info,
5887 desc->array_offset, offs);
5888 }
5889}
5890
5891void bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear)
5892{
5893 int i;
5894
5895 for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
5896 struct bpf_prog_info_array_desc *desc;
5897 __u64 addr, offs;
5898
5899 if ((info_linear->arrays & (1UL << i)) == 0)
5900 continue;
5901
5902 desc = bpf_prog_info_array_desc + i;
5903 offs = bpf_prog_info_read_offset_u64(&info_linear->info,
5904 desc->array_offset);
5905 addr = offs + ptr_to_u64(info_linear->data);
5906 bpf_prog_info_set_offset_u64(&info_linear->info,
5907 desc->array_offset, addr);
5908 }
5909}
5910
Olivier Deprez0e641232021-09-23 10:07:05 +02005911int parse_cpu_mask_str(const char *s, bool **mask, int *mask_sz)
5912{
5913 int err = 0, n, len, start, end = -1;
5914 bool *tmp;
5915
5916 *mask = NULL;
5917 *mask_sz = 0;
5918
5919 /* Each sub string separated by ',' has format \d+-\d+ or \d+ */
5920 while (*s) {
5921 if (*s == ',' || *s == '\n') {
5922 s++;
5923 continue;
5924 }
5925 n = sscanf(s, "%d%n-%d%n", &start, &len, &end, &len);
5926 if (n <= 0 || n > 2) {
5927 pr_warning("Failed to get CPU range %s: %d\n", s, n);
5928 err = -EINVAL;
5929 goto cleanup;
5930 } else if (n == 1) {
5931 end = start;
5932 }
5933 if (start < 0 || start > end) {
5934 pr_warning("Invalid CPU range [%d,%d] in %s\n",
5935 start, end, s);
5936 err = -EINVAL;
5937 goto cleanup;
5938 }
5939 tmp = realloc(*mask, end + 1);
5940 if (!tmp) {
5941 err = -ENOMEM;
5942 goto cleanup;
5943 }
5944 *mask = tmp;
5945 memset(tmp + *mask_sz, 0, start - *mask_sz);
5946 memset(tmp + start, 1, end - start + 1);
5947 *mask_sz = end + 1;
5948 s += len;
5949 }
5950 if (!*mask_sz) {
5951 pr_warning("Empty CPU range\n");
5952 return -EINVAL;
5953 }
5954 return 0;
5955cleanup:
5956 free(*mask);
5957 *mask = NULL;
5958 return err;
5959}
5960
5961int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz)
5962{
5963 int fd, err = 0, len;
5964 char buf[128];
5965
5966 fd = open(fcpu, O_RDONLY);
5967 if (fd < 0) {
5968 err = -errno;
5969 pr_warning("Failed to open cpu mask file %s: %d\n", fcpu, err);
5970 return err;
5971 }
5972 len = read(fd, buf, sizeof(buf));
5973 close(fd);
5974 if (len <= 0) {
5975 err = len ? -errno : -EINVAL;
5976 pr_warning("Failed to read cpu mask from %s: %d\n", fcpu, err);
5977 return err;
5978 }
5979 if (len >= sizeof(buf)) {
5980 pr_warning("CPU mask is too big in file %s\n", fcpu);
5981 return -E2BIG;
5982 }
5983 buf[len] = '\0';
5984
5985 return parse_cpu_mask_str(buf, mask, mask_sz);
5986}
5987
David Brazdil0f672f62019-12-10 10:32:29 +00005988int libbpf_num_possible_cpus(void)
5989{
5990 static const char *fcpu = "/sys/devices/system/cpu/possible";
David Brazdil0f672f62019-12-10 10:32:29 +00005991 static int cpus;
Olivier Deprez0e641232021-09-23 10:07:05 +02005992 int err, n, i, tmp_cpus;
5993 bool *mask;
David Brazdil0f672f62019-12-10 10:32:29 +00005994
5995 tmp_cpus = READ_ONCE(cpus);
5996 if (tmp_cpus > 0)
5997 return tmp_cpus;
5998
Olivier Deprez0e641232021-09-23 10:07:05 +02005999 err = parse_cpu_mask_file(fcpu, &mask, &n);
6000 if (err)
6001 return err;
David Brazdil0f672f62019-12-10 10:32:29 +00006002
Olivier Deprez0e641232021-09-23 10:07:05 +02006003 tmp_cpus = 0;
6004 for (i = 0; i < n; i++) {
6005 if (mask[i])
6006 tmp_cpus++;
David Brazdil0f672f62019-12-10 10:32:29 +00006007 }
Olivier Deprez0e641232021-09-23 10:07:05 +02006008 free(mask);
David Brazdil0f672f62019-12-10 10:32:29 +00006009
6010 WRITE_ONCE(cpus, tmp_cpus);
6011 return tmp_cpus;
6012}