blob: 4f402dcdf3729b4d08b76d939252531bf16a9d56 [file] [log] [blame]
Wang Nan1b76c132015-07-01 02:13:51 +00001/*
2 * Common eBPF ELF object loading operations.
3 *
4 * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
5 * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
6 * Copyright (C) 2015 Huawei Inc.
Joe Stringerf3675402017-01-26 13:19:56 -08007 * Copyright (C) 2017 Nicira, Inc.
Wang Nan203d1ca2016-07-04 11:02:42 +00008 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation;
12 * version 2.1 of the License (not later!)
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this program; if not, see <http://www.gnu.org/licenses>
Wang Nan1b76c132015-07-01 02:13:51 +000021 */
22
23#include <stdlib.h>
Wang Nanb3f59d62015-07-01 02:13:52 +000024#include <stdio.h>
25#include <stdarg.h>
Joe Stringerf3675402017-01-26 13:19:56 -080026#include <libgen.h>
Wang Nan34090912015-07-01 02:14:02 +000027#include <inttypes.h>
Wang Nanb3f59d62015-07-01 02:13:52 +000028#include <string.h>
Wang Nan1b76c132015-07-01 02:13:51 +000029#include <unistd.h>
Wang Nan1a5e3fb2015-07-01 02:13:53 +000030#include <fcntl.h>
31#include <errno.h>
Wang Nan1b76c132015-07-01 02:13:51 +000032#include <asm/unistd.h>
Joe Stringere28ff1a2017-01-22 17:11:25 -080033#include <linux/err.h>
Wang Nancb1e5e92015-07-01 02:13:57 +000034#include <linux/kernel.h>
Wang Nan1b76c132015-07-01 02:13:51 +000035#include <linux/bpf.h>
Wang Nan9a208ef2015-07-01 02:14:10 +000036#include <linux/list.h>
Joe Stringerf3675402017-01-26 13:19:56 -080037#include <linux/limits.h>
38#include <sys/stat.h>
39#include <sys/types.h>
40#include <sys/vfs.h>
Wang Nan1a5e3fb2015-07-01 02:13:53 +000041#include <libelf.h>
42#include <gelf.h>
Wang Nan1b76c132015-07-01 02:13:51 +000043
44#include "libbpf.h"
Wang Nan52d33522015-07-01 02:14:04 +000045#include "bpf.h"
Wang Nanb3f59d62015-07-01 02:13:52 +000046
Wang Nan9b161372016-07-18 06:01:08 +000047#ifndef EM_BPF
48#define EM_BPF 247
49#endif
50
Joe Stringerf3675402017-01-26 13:19:56 -080051#ifndef BPF_FS_MAGIC
52#define BPF_FS_MAGIC 0xcafe4a11
53#endif
54
Wang Nanb3f59d62015-07-01 02:13:52 +000055#define __printf(a, b) __attribute__((format(printf, a, b)))
56
57__printf(1, 2)
58static int __base_pr(const char *format, ...)
59{
60 va_list args;
61 int err;
62
63 va_start(args, format);
64 err = vfprintf(stderr, format, args);
65 va_end(args);
66 return err;
67}
68
69static __printf(1, 2) libbpf_print_fn_t __pr_warning = __base_pr;
70static __printf(1, 2) libbpf_print_fn_t __pr_info = __base_pr;
71static __printf(1, 2) libbpf_print_fn_t __pr_debug;
72
73#define __pr(func, fmt, ...) \
74do { \
75 if ((func)) \
76 (func)("libbpf: " fmt, ##__VA_ARGS__); \
77} while (0)
78
79#define pr_warning(fmt, ...) __pr(__pr_warning, fmt, ##__VA_ARGS__)
80#define pr_info(fmt, ...) __pr(__pr_info, fmt, ##__VA_ARGS__)
81#define pr_debug(fmt, ...) __pr(__pr_debug, fmt, ##__VA_ARGS__)
82
83void libbpf_set_print(libbpf_print_fn_t warn,
84 libbpf_print_fn_t info,
85 libbpf_print_fn_t debug)
86{
87 __pr_warning = warn;
88 __pr_info = info;
89 __pr_debug = debug;
90}
Wang Nan1a5e3fb2015-07-01 02:13:53 +000091
Wang Nan6371ca3b2015-11-06 13:49:37 +000092#define STRERR_BUFSIZE 128
93
94#define ERRNO_OFFSET(e) ((e) - __LIBBPF_ERRNO__START)
95#define ERRCODE_OFFSET(c) ERRNO_OFFSET(LIBBPF_ERRNO__##c)
96#define NR_ERRNO (__LIBBPF_ERRNO__END - __LIBBPF_ERRNO__START)
97
98static const char *libbpf_strerror_table[NR_ERRNO] = {
99 [ERRCODE_OFFSET(LIBELF)] = "Something wrong in libelf",
100 [ERRCODE_OFFSET(FORMAT)] = "BPF object format invalid",
101 [ERRCODE_OFFSET(KVERSION)] = "'version' section incorrect or lost",
Colin Ian Kingde8a63b2016-06-28 13:23:37 +0100102 [ERRCODE_OFFSET(ENDIAN)] = "Endian mismatch",
Wang Nan6371ca3b2015-11-06 13:49:37 +0000103 [ERRCODE_OFFSET(INTERNAL)] = "Internal error in libbpf",
104 [ERRCODE_OFFSET(RELOC)] = "Relocation failed",
105 [ERRCODE_OFFSET(VERIFY)] = "Kernel verifier blocks program loading",
106 [ERRCODE_OFFSET(PROG2BIG)] = "Program too big",
107 [ERRCODE_OFFSET(KVER)] = "Incorrect kernel version",
Wang Nan705fa212016-07-13 10:44:02 +0000108 [ERRCODE_OFFSET(PROGTYPE)] = "Kernel doesn't support this program type",
Wang Nan6371ca3b2015-11-06 13:49:37 +0000109};
110
111int libbpf_strerror(int err, char *buf, size_t size)
112{
113 if (!buf || !size)
114 return -1;
115
116 err = err > 0 ? err : -err;
117
118 if (err < __LIBBPF_ERRNO__START) {
119 int ret;
120
121 ret = strerror_r(err, buf, size);
122 buf[size - 1] = '\0';
123 return ret;
124 }
125
126 if (err < __LIBBPF_ERRNO__END) {
127 const char *msg;
128
129 msg = libbpf_strerror_table[ERRNO_OFFSET(err)];
130 snprintf(buf, size, "%s", msg);
131 buf[size - 1] = '\0';
132 return 0;
133 }
134
135 snprintf(buf, size, "Unknown libbpf error %d", err);
136 buf[size - 1] = '\0';
137 return -1;
138}
139
140#define CHECK_ERR(action, err, out) do { \
141 err = action; \
142 if (err) \
143 goto out; \
144} while(0)
145
146
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000147/* Copied from tools/perf/util/util.h */
148#ifndef zfree
149# define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
150#endif
151
152#ifndef zclose
153# define zclose(fd) ({ \
154 int ___err = 0; \
155 if ((fd) >= 0) \
156 ___err = close((fd)); \
157 fd = -1; \
158 ___err; })
159#endif
160
161#ifdef HAVE_LIBELF_MMAP_SUPPORT
162# define LIBBPF_ELF_C_READ_MMAP ELF_C_READ_MMAP
163#else
164# define LIBBPF_ELF_C_READ_MMAP ELF_C_READ
165#endif
166
Wang Nana5b8bd42015-07-01 02:14:00 +0000167/*
168 * bpf_prog should be a better name but it has been used in
169 * linux/filter.h.
170 */
171struct bpf_program {
172 /* Index in elf obj file, for relocation use. */
173 int idx;
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700174 char *name;
Wang Nana5b8bd42015-07-01 02:14:00 +0000175 char *section_name;
176 struct bpf_insn *insns;
177 size_t insns_cnt;
Wang Nan5f44e4c82016-07-13 10:44:01 +0000178 enum bpf_prog_type type;
Wang Nan34090912015-07-01 02:14:02 +0000179
180 struct {
181 int insn_idx;
182 int map_idx;
183 } *reloc_desc;
184 int nr_reloc;
Wang Nan55cffde2015-07-01 02:14:07 +0000185
Wang Nanb5805632015-11-16 12:10:09 +0000186 struct {
187 int nr;
188 int *fds;
189 } instances;
190 bpf_program_prep_t preprocessor;
Wang Nanaa9b1ac2015-07-01 02:14:08 +0000191
192 struct bpf_object *obj;
193 void *priv;
194 bpf_program_clear_priv_t clear_priv;
Wang Nana5b8bd42015-07-01 02:14:00 +0000195};
196
Wang Nan9d759a92015-11-27 08:47:35 +0000197struct bpf_map {
198 int fd;
Wang Nan561bbcc2015-11-27 08:47:36 +0000199 char *name;
Eric Leblond4708bbd2016-11-15 04:05:47 +0000200 size_t offset;
Wang Nan9d759a92015-11-27 08:47:35 +0000201 struct bpf_map_def def;
202 void *priv;
203 bpf_map_clear_priv_t clear_priv;
204};
205
Wang Nan9a208ef2015-07-01 02:14:10 +0000206static LIST_HEAD(bpf_objects_list);
207
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000208struct bpf_object {
Wang Nancb1e5e92015-07-01 02:13:57 +0000209 char license[64];
210 u32 kern_version;
Wang Nan0b3d1ef2015-07-01 02:13:58 +0000211
Wang Nana5b8bd42015-07-01 02:14:00 +0000212 struct bpf_program *programs;
213 size_t nr_programs;
Wang Nan9d759a92015-11-27 08:47:35 +0000214 struct bpf_map *maps;
215 size_t nr_maps;
216
Wang Nan52d33522015-07-01 02:14:04 +0000217 bool loaded;
Wang Nana5b8bd42015-07-01 02:14:00 +0000218
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000219 /*
220 * Information when doing elf related work. Only valid if fd
221 * is valid.
222 */
223 struct {
224 int fd;
Wang Nan6c956392015-07-01 02:13:54 +0000225 void *obj_buf;
226 size_t obj_buf_sz;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000227 Elf *elf;
228 GElf_Ehdr ehdr;
Wang Nanbec7d682015-07-01 02:13:59 +0000229 Elf_Data *symbols;
Wang Nan77ba9a52015-12-08 02:25:30 +0000230 size_t strtabidx;
Wang Nanb62f06e2015-07-01 02:14:01 +0000231 struct {
232 GElf_Shdr shdr;
233 Elf_Data *data;
234 } *reloc;
235 int nr_reloc;
Wang Nan666810e2016-01-25 09:55:49 +0000236 int maps_shndx;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000237 } efile;
Wang Nan9a208ef2015-07-01 02:14:10 +0000238 /*
239 * All loaded bpf_object is linked in a list, which is
240 * hidden to caller. bpf_objects__<func> handlers deal with
241 * all objects.
242 */
243 struct list_head list;
Wang Nan10931d22016-11-26 07:03:26 +0000244
245 void *priv;
246 bpf_object_clear_priv_t clear_priv;
247
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000248 char path[];
249};
250#define obj_elf_valid(o) ((o)->efile.elf)
251
Wang Nan55cffde2015-07-01 02:14:07 +0000252static void bpf_program__unload(struct bpf_program *prog)
253{
Wang Nanb5805632015-11-16 12:10:09 +0000254 int i;
255
Wang Nan55cffde2015-07-01 02:14:07 +0000256 if (!prog)
257 return;
258
Wang Nanb5805632015-11-16 12:10:09 +0000259 /*
260 * If the object is opened but the program was never loaded,
261 * it is possible that prog->instances.nr == -1.
262 */
263 if (prog->instances.nr > 0) {
264 for (i = 0; i < prog->instances.nr; i++)
265 zclose(prog->instances.fds[i]);
266 } else if (prog->instances.nr != -1) {
267 pr_warning("Internal error: instances.nr is %d\n",
268 prog->instances.nr);
269 }
270
271 prog->instances.nr = -1;
272 zfree(&prog->instances.fds);
Wang Nan55cffde2015-07-01 02:14:07 +0000273}
274
Wang Nana5b8bd42015-07-01 02:14:00 +0000275static void bpf_program__exit(struct bpf_program *prog)
276{
277 if (!prog)
278 return;
279
Wang Nanaa9b1ac2015-07-01 02:14:08 +0000280 if (prog->clear_priv)
281 prog->clear_priv(prog, prog->priv);
282
283 prog->priv = NULL;
284 prog->clear_priv = NULL;
285
Wang Nan55cffde2015-07-01 02:14:07 +0000286 bpf_program__unload(prog);
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700287 zfree(&prog->name);
Wang Nana5b8bd42015-07-01 02:14:00 +0000288 zfree(&prog->section_name);
289 zfree(&prog->insns);
Wang Nan34090912015-07-01 02:14:02 +0000290 zfree(&prog->reloc_desc);
291
292 prog->nr_reloc = 0;
Wang Nana5b8bd42015-07-01 02:14:00 +0000293 prog->insns_cnt = 0;
294 prog->idx = -1;
295}
296
297static int
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700298bpf_program__init(void *data, size_t size, char *section_name, int idx,
299 struct bpf_program *prog)
Wang Nana5b8bd42015-07-01 02:14:00 +0000300{
301 if (size < sizeof(struct bpf_insn)) {
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700302 pr_warning("corrupted section '%s'\n", section_name);
Wang Nana5b8bd42015-07-01 02:14:00 +0000303 return -EINVAL;
304 }
305
306 bzero(prog, sizeof(*prog));
307
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700308 prog->section_name = strdup(section_name);
Wang Nana5b8bd42015-07-01 02:14:00 +0000309 if (!prog->section_name) {
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700310 pr_warning("failed to alloc name for prog under section %s\n",
311 section_name);
Wang Nana5b8bd42015-07-01 02:14:00 +0000312 goto errout;
313 }
314
315 prog->insns = malloc(size);
316 if (!prog->insns) {
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700317 pr_warning("failed to alloc insns for prog under section %s\n",
318 section_name);
Wang Nana5b8bd42015-07-01 02:14:00 +0000319 goto errout;
320 }
321 prog->insns_cnt = size / sizeof(struct bpf_insn);
322 memcpy(prog->insns, data,
323 prog->insns_cnt * sizeof(struct bpf_insn));
324 prog->idx = idx;
Wang Nanb5805632015-11-16 12:10:09 +0000325 prog->instances.fds = NULL;
326 prog->instances.nr = -1;
Wang Nan5f44e4c82016-07-13 10:44:01 +0000327 prog->type = BPF_PROG_TYPE_KPROBE;
Wang Nana5b8bd42015-07-01 02:14:00 +0000328
329 return 0;
330errout:
331 bpf_program__exit(prog);
332 return -ENOMEM;
333}
334
335static int
336bpf_object__add_program(struct bpf_object *obj, void *data, size_t size,
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700337 char *section_name, int idx)
Wang Nana5b8bd42015-07-01 02:14:00 +0000338{
339 struct bpf_program prog, *progs;
340 int nr_progs, err;
341
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700342 err = bpf_program__init(data, size, section_name, idx, &prog);
Wang Nana5b8bd42015-07-01 02:14:00 +0000343 if (err)
344 return err;
345
346 progs = obj->programs;
347 nr_progs = obj->nr_programs;
348
349 progs = realloc(progs, sizeof(progs[0]) * (nr_progs + 1));
350 if (!progs) {
351 /*
352 * In this case the original obj->programs
353 * is still valid, so don't need special treat for
354 * bpf_close_object().
355 */
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700356 pr_warning("failed to alloc a new program under section '%s'\n",
357 section_name);
Wang Nana5b8bd42015-07-01 02:14:00 +0000358 bpf_program__exit(&prog);
359 return -ENOMEM;
360 }
361
362 pr_debug("found program %s\n", prog.section_name);
363 obj->programs = progs;
364 obj->nr_programs = nr_progs + 1;
Wang Nanaa9b1ac2015-07-01 02:14:08 +0000365 prog.obj = obj;
Wang Nana5b8bd42015-07-01 02:14:00 +0000366 progs[nr_progs] = prog;
367 return 0;
368}
369
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700370static int
371bpf_object__init_prog_names(struct bpf_object *obj)
372{
373 Elf_Data *symbols = obj->efile.symbols;
374 struct bpf_program *prog;
375 size_t pi, si;
376
377 for (pi = 0; pi < obj->nr_programs; pi++) {
378 char *name = NULL;
379
380 prog = &obj->programs[pi];
381
382 for (si = 0; si < symbols->d_size / sizeof(GElf_Sym) && !name;
383 si++) {
384 GElf_Sym sym;
385
386 if (!gelf_getsym(symbols, si, &sym))
387 continue;
388 if (sym.st_shndx != prog->idx)
389 continue;
390
391 name = elf_strptr(obj->efile.elf,
392 obj->efile.strtabidx,
393 sym.st_name);
394 if (!name) {
395 pr_warning("failed to get sym name string for prog %s\n",
396 prog->section_name);
397 return -LIBBPF_ERRNO__LIBELF;
398 }
399 }
400
401 if (!name) {
402 pr_warning("failed to find sym for prog %s\n",
403 prog->section_name);
404 return -EINVAL;
405 }
406
407 prog->name = strdup(name);
408 if (!prog->name) {
409 pr_warning("failed to allocate memory for prog sym %s\n",
410 name);
411 return -ENOMEM;
412 }
413 }
414
415 return 0;
416}
417
Wang Nan6c956392015-07-01 02:13:54 +0000418static struct bpf_object *bpf_object__new(const char *path,
419 void *obj_buf,
420 size_t obj_buf_sz)
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000421{
422 struct bpf_object *obj;
423
424 obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1);
425 if (!obj) {
426 pr_warning("alloc memory failed for %s\n", path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000427 return ERR_PTR(-ENOMEM);
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000428 }
429
430 strcpy(obj->path, path);
431 obj->efile.fd = -1;
Wang Nan6c956392015-07-01 02:13:54 +0000432
433 /*
434 * Caller of this function should also calls
435 * bpf_object__elf_finish() after data collection to return
436 * obj_buf to user. If not, we should duplicate the buffer to
437 * avoid user freeing them before elf finish.
438 */
439 obj->efile.obj_buf = obj_buf;
440 obj->efile.obj_buf_sz = obj_buf_sz;
Wang Nan666810e2016-01-25 09:55:49 +0000441 obj->efile.maps_shndx = -1;
Wang Nan6c956392015-07-01 02:13:54 +0000442
Wang Nan52d33522015-07-01 02:14:04 +0000443 obj->loaded = false;
Wang Nan9a208ef2015-07-01 02:14:10 +0000444
445 INIT_LIST_HEAD(&obj->list);
446 list_add(&obj->list, &bpf_objects_list);
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000447 return obj;
448}
449
450static void bpf_object__elf_finish(struct bpf_object *obj)
451{
452 if (!obj_elf_valid(obj))
453 return;
454
455 if (obj->efile.elf) {
456 elf_end(obj->efile.elf);
457 obj->efile.elf = NULL;
458 }
Wang Nanbec7d682015-07-01 02:13:59 +0000459 obj->efile.symbols = NULL;
Wang Nanb62f06e2015-07-01 02:14:01 +0000460
461 zfree(&obj->efile.reloc);
462 obj->efile.nr_reloc = 0;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000463 zclose(obj->efile.fd);
Wang Nan6c956392015-07-01 02:13:54 +0000464 obj->efile.obj_buf = NULL;
465 obj->efile.obj_buf_sz = 0;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000466}
467
468static int bpf_object__elf_init(struct bpf_object *obj)
469{
470 int err = 0;
471 GElf_Ehdr *ep;
472
473 if (obj_elf_valid(obj)) {
474 pr_warning("elf init: internal error\n");
Wang Nan6371ca3b2015-11-06 13:49:37 +0000475 return -LIBBPF_ERRNO__LIBELF;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000476 }
477
Wang Nan6c956392015-07-01 02:13:54 +0000478 if (obj->efile.obj_buf_sz > 0) {
479 /*
480 * obj_buf should have been validated by
481 * bpf_object__open_buffer().
482 */
483 obj->efile.elf = elf_memory(obj->efile.obj_buf,
484 obj->efile.obj_buf_sz);
485 } else {
486 obj->efile.fd = open(obj->path, O_RDONLY);
487 if (obj->efile.fd < 0) {
488 pr_warning("failed to open %s: %s\n", obj->path,
489 strerror(errno));
490 return -errno;
491 }
492
493 obj->efile.elf = elf_begin(obj->efile.fd,
494 LIBBPF_ELF_C_READ_MMAP,
495 NULL);
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000496 }
497
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000498 if (!obj->efile.elf) {
499 pr_warning("failed to open %s as ELF file\n",
500 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000501 err = -LIBBPF_ERRNO__LIBELF;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000502 goto errout;
503 }
504
505 if (!gelf_getehdr(obj->efile.elf, &obj->efile.ehdr)) {
506 pr_warning("failed to get EHDR from %s\n",
507 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000508 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000509 goto errout;
510 }
511 ep = &obj->efile.ehdr;
512
Wang Nan9b161372016-07-18 06:01:08 +0000513 /* Old LLVM set e_machine to EM_NONE */
514 if ((ep->e_type != ET_REL) || (ep->e_machine && (ep->e_machine != EM_BPF))) {
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000515 pr_warning("%s is not an eBPF object file\n",
516 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000517 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000518 goto errout;
519 }
520
521 return 0;
522errout:
523 bpf_object__elf_finish(obj);
524 return err;
525}
526
Wang Nancc4228d2015-07-01 02:13:55 +0000527static int
528bpf_object__check_endianness(struct bpf_object *obj)
529{
530 static unsigned int const endian = 1;
531
532 switch (obj->efile.ehdr.e_ident[EI_DATA]) {
533 case ELFDATA2LSB:
534 /* We are big endian, BPF obj is little endian. */
535 if (*(unsigned char const *)&endian != 1)
536 goto mismatch;
537 break;
538
539 case ELFDATA2MSB:
540 /* We are little endian, BPF obj is big endian. */
541 if (*(unsigned char const *)&endian != 0)
542 goto mismatch;
543 break;
544 default:
Wang Nan6371ca3b2015-11-06 13:49:37 +0000545 return -LIBBPF_ERRNO__ENDIAN;
Wang Nancc4228d2015-07-01 02:13:55 +0000546 }
547
548 return 0;
549
550mismatch:
551 pr_warning("Error: endianness mismatch.\n");
Wang Nan6371ca3b2015-11-06 13:49:37 +0000552 return -LIBBPF_ERRNO__ENDIAN;
Wang Nancc4228d2015-07-01 02:13:55 +0000553}
554
Wang Nancb1e5e92015-07-01 02:13:57 +0000555static int
556bpf_object__init_license(struct bpf_object *obj,
557 void *data, size_t size)
558{
559 memcpy(obj->license, data,
560 min(size, sizeof(obj->license) - 1));
561 pr_debug("license of %s is %s\n", obj->path, obj->license);
562 return 0;
563}
564
565static int
566bpf_object__init_kversion(struct bpf_object *obj,
567 void *data, size_t size)
568{
569 u32 kver;
570
571 if (size != sizeof(kver)) {
572 pr_warning("invalid kver section in %s\n", obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000573 return -LIBBPF_ERRNO__FORMAT;
Wang Nancb1e5e92015-07-01 02:13:57 +0000574 }
575 memcpy(&kver, data, sizeof(kver));
576 obj->kern_version = kver;
577 pr_debug("kernel version of %s is %x\n", obj->path,
578 obj->kern_version);
579 return 0;
580}
581
Wang Nan0b3d1ef2015-07-01 02:13:58 +0000582static int
Eric Leblond4708bbd2016-11-15 04:05:47 +0000583bpf_object__validate_maps(struct bpf_object *obj)
Wang Nan0b3d1ef2015-07-01 02:13:58 +0000584{
Wang Nan9d759a92015-11-27 08:47:35 +0000585 int i;
586
Eric Leblond4708bbd2016-11-15 04:05:47 +0000587 /*
588 * If there's only 1 map, the only error case should have been
589 * catched in bpf_object__init_maps().
590 */
591 if (!obj->maps || !obj->nr_maps || (obj->nr_maps == 1))
Wang Nan0b3d1ef2015-07-01 02:13:58 +0000592 return 0;
Eric Leblond4708bbd2016-11-15 04:05:47 +0000593
594 for (i = 1; i < obj->nr_maps; i++) {
595 const struct bpf_map *a = &obj->maps[i - 1];
596 const struct bpf_map *b = &obj->maps[i];
597
598 if (b->offset - a->offset < sizeof(struct bpf_map_def)) {
599 pr_warning("corrupted map section in %s: map \"%s\" too small\n",
600 obj->path, a->name);
601 return -EINVAL;
602 }
603 }
604 return 0;
605}
606
607static int compare_bpf_map(const void *_a, const void *_b)
608{
609 const struct bpf_map *a = _a;
610 const struct bpf_map *b = _b;
611
612 return a->offset - b->offset;
613}
614
615static int
616bpf_object__init_maps(struct bpf_object *obj)
617{
618 int i, map_idx, nr_maps = 0;
619 Elf_Scn *scn;
620 Elf_Data *data;
621 Elf_Data *symbols = obj->efile.symbols;
622
623 if (obj->efile.maps_shndx < 0)
624 return -EINVAL;
625 if (!symbols)
626 return -EINVAL;
627
628 scn = elf_getscn(obj->efile.elf, obj->efile.maps_shndx);
629 if (scn)
630 data = elf_getdata(scn, NULL);
631 if (!scn || !data) {
632 pr_warning("failed to get Elf_Data from map section %d\n",
633 obj->efile.maps_shndx);
634 return -EINVAL;
Wang Nan0b3d1ef2015-07-01 02:13:58 +0000635 }
636
Eric Leblond4708bbd2016-11-15 04:05:47 +0000637 /*
638 * Count number of maps. Each map has a name.
639 * Array of maps is not supported: only the first element is
640 * considered.
641 *
642 * TODO: Detect array of map and report error.
643 */
644 for (i = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
645 GElf_Sym sym;
646
647 if (!gelf_getsym(symbols, i, &sym))
648 continue;
649 if (sym.st_shndx != obj->efile.maps_shndx)
650 continue;
651 nr_maps++;
652 }
653
654 /* Alloc obj->maps and fill nr_maps. */
655 pr_debug("maps in %s: %d maps in %zd bytes\n", obj->path,
656 nr_maps, data->d_size);
657
658 if (!nr_maps)
659 return 0;
Wang Nan9d759a92015-11-27 08:47:35 +0000660
661 obj->maps = calloc(nr_maps, sizeof(obj->maps[0]));
662 if (!obj->maps) {
663 pr_warning("alloc maps for object failed\n");
Wang Nan0b3d1ef2015-07-01 02:13:58 +0000664 return -ENOMEM;
665 }
Wang Nan9d759a92015-11-27 08:47:35 +0000666 obj->nr_maps = nr_maps;
Wang Nan0b3d1ef2015-07-01 02:13:58 +0000667
Eric Leblond4708bbd2016-11-15 04:05:47 +0000668 /*
669 * fill all fd with -1 so won't close incorrect
670 * fd (fd=0 is stdin) when failure (zclose won't close
671 * negative fd)).
672 */
673 for (i = 0; i < nr_maps; i++)
Wang Nan9d759a92015-11-27 08:47:35 +0000674 obj->maps[i].fd = -1;
675
Eric Leblond4708bbd2016-11-15 04:05:47 +0000676 /*
677 * Fill obj->maps using data in "maps" section.
678 */
679 for (i = 0, map_idx = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
Wang Nan561bbcc2015-11-27 08:47:36 +0000680 GElf_Sym sym;
Wang Nan561bbcc2015-11-27 08:47:36 +0000681 const char *map_name;
Eric Leblond4708bbd2016-11-15 04:05:47 +0000682 struct bpf_map_def *def;
Wang Nan561bbcc2015-11-27 08:47:36 +0000683
684 if (!gelf_getsym(symbols, i, &sym))
685 continue;
Wang Nan666810e2016-01-25 09:55:49 +0000686 if (sym.st_shndx != obj->efile.maps_shndx)
Wang Nan561bbcc2015-11-27 08:47:36 +0000687 continue;
688
689 map_name = elf_strptr(obj->efile.elf,
Wang Nan77ba9a52015-12-08 02:25:30 +0000690 obj->efile.strtabidx,
Wang Nan561bbcc2015-11-27 08:47:36 +0000691 sym.st_name);
Eric Leblond4708bbd2016-11-15 04:05:47 +0000692 obj->maps[map_idx].offset = sym.st_value;
693 if (sym.st_value + sizeof(struct bpf_map_def) > data->d_size) {
694 pr_warning("corrupted maps section in %s: last map \"%s\" too small\n",
695 obj->path, map_name);
696 return -EINVAL;
Wang Nan561bbcc2015-11-27 08:47:36 +0000697 }
Eric Leblond4708bbd2016-11-15 04:05:47 +0000698
Wang Nan561bbcc2015-11-27 08:47:36 +0000699 obj->maps[map_idx].name = strdup(map_name);
Wang Nan973170e2015-12-08 02:25:29 +0000700 if (!obj->maps[map_idx].name) {
701 pr_warning("failed to alloc map name\n");
702 return -ENOMEM;
703 }
Eric Leblond4708bbd2016-11-15 04:05:47 +0000704 pr_debug("map %d is \"%s\"\n", map_idx,
Wang Nan561bbcc2015-11-27 08:47:36 +0000705 obj->maps[map_idx].name);
Eric Leblond4708bbd2016-11-15 04:05:47 +0000706 def = (struct bpf_map_def *)(data->d_buf + sym.st_value);
707 obj->maps[map_idx].def = *def;
708 map_idx++;
Wang Nan561bbcc2015-11-27 08:47:36 +0000709 }
Eric Leblond4708bbd2016-11-15 04:05:47 +0000710
711 qsort(obj->maps, obj->nr_maps, sizeof(obj->maps[0]), compare_bpf_map);
712 return bpf_object__validate_maps(obj);
Wang Nan561bbcc2015-11-27 08:47:36 +0000713}
714
Wang Nan29603662015-07-01 02:13:56 +0000715static int bpf_object__elf_collect(struct bpf_object *obj)
716{
717 Elf *elf = obj->efile.elf;
718 GElf_Ehdr *ep = &obj->efile.ehdr;
719 Elf_Scn *scn = NULL;
Wang Nan666810e2016-01-25 09:55:49 +0000720 int idx = 0, err = 0;
Wang Nan29603662015-07-01 02:13:56 +0000721
722 /* Elf is corrupted/truncated, avoid calling elf_strptr. */
723 if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) {
724 pr_warning("failed to get e_shstrndx from %s\n",
725 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000726 return -LIBBPF_ERRNO__FORMAT;
Wang Nan29603662015-07-01 02:13:56 +0000727 }
728
729 while ((scn = elf_nextscn(elf, scn)) != NULL) {
730 char *name;
731 GElf_Shdr sh;
732 Elf_Data *data;
733
734 idx++;
735 if (gelf_getshdr(scn, &sh) != &sh) {
736 pr_warning("failed to get section header from %s\n",
737 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000738 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan29603662015-07-01 02:13:56 +0000739 goto out;
740 }
741
742 name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name);
743 if (!name) {
744 pr_warning("failed to get section name from %s\n",
745 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000746 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan29603662015-07-01 02:13:56 +0000747 goto out;
748 }
749
750 data = elf_getdata(scn, 0);
751 if (!data) {
752 pr_warning("failed to get section data from %s(%s)\n",
753 name, obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000754 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan29603662015-07-01 02:13:56 +0000755 goto out;
756 }
757 pr_debug("section %s, size %ld, link %d, flags %lx, type=%d\n",
758 name, (unsigned long)data->d_size,
759 (int)sh.sh_link, (unsigned long)sh.sh_flags,
760 (int)sh.sh_type);
Wang Nancb1e5e92015-07-01 02:13:57 +0000761
762 if (strcmp(name, "license") == 0)
763 err = bpf_object__init_license(obj,
764 data->d_buf,
765 data->d_size);
766 else if (strcmp(name, "version") == 0)
767 err = bpf_object__init_kversion(obj,
768 data->d_buf,
769 data->d_size);
Eric Leblond4708bbd2016-11-15 04:05:47 +0000770 else if (strcmp(name, "maps") == 0)
Wang Nan666810e2016-01-25 09:55:49 +0000771 obj->efile.maps_shndx = idx;
Eric Leblond4708bbd2016-11-15 04:05:47 +0000772 else if (sh.sh_type == SHT_SYMTAB) {
Wang Nanbec7d682015-07-01 02:13:59 +0000773 if (obj->efile.symbols) {
774 pr_warning("bpf: multiple SYMTAB in %s\n",
775 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000776 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan77ba9a52015-12-08 02:25:30 +0000777 } else {
Wang Nanbec7d682015-07-01 02:13:59 +0000778 obj->efile.symbols = data;
Wang Nan77ba9a52015-12-08 02:25:30 +0000779 obj->efile.strtabidx = sh.sh_link;
780 }
Wang Nana5b8bd42015-07-01 02:14:00 +0000781 } else if ((sh.sh_type == SHT_PROGBITS) &&
782 (sh.sh_flags & SHF_EXECINSTR) &&
783 (data->d_size > 0)) {
784 err = bpf_object__add_program(obj, data->d_buf,
785 data->d_size, name, idx);
786 if (err) {
Wang Nan6371ca3b2015-11-06 13:49:37 +0000787 char errmsg[STRERR_BUFSIZE];
788
Wang Nana5b8bd42015-07-01 02:14:00 +0000789 strerror_r(-err, errmsg, sizeof(errmsg));
790 pr_warning("failed to alloc program %s (%s): %s",
791 name, obj->path, errmsg);
792 }
Wang Nanb62f06e2015-07-01 02:14:01 +0000793 } else if (sh.sh_type == SHT_REL) {
794 void *reloc = obj->efile.reloc;
795 int nr_reloc = obj->efile.nr_reloc + 1;
796
797 reloc = realloc(reloc,
798 sizeof(*obj->efile.reloc) * nr_reloc);
799 if (!reloc) {
800 pr_warning("realloc failed\n");
801 err = -ENOMEM;
802 } else {
803 int n = nr_reloc - 1;
804
805 obj->efile.reloc = reloc;
806 obj->efile.nr_reloc = nr_reloc;
807
808 obj->efile.reloc[n].shdr = sh;
809 obj->efile.reloc[n].data = data;
810 }
Wang Nanbec7d682015-07-01 02:13:59 +0000811 }
Wang Nancb1e5e92015-07-01 02:13:57 +0000812 if (err)
813 goto out;
Wang Nan29603662015-07-01 02:13:56 +0000814 }
Wang Nan561bbcc2015-11-27 08:47:36 +0000815
Wang Nan77ba9a52015-12-08 02:25:30 +0000816 if (!obj->efile.strtabidx || obj->efile.strtabidx >= idx) {
817 pr_warning("Corrupted ELF file: index of strtab invalid\n");
818 return LIBBPF_ERRNO__FORMAT;
819 }
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700820 if (obj->efile.maps_shndx >= 0) {
Eric Leblond4708bbd2016-11-15 04:05:47 +0000821 err = bpf_object__init_maps(obj);
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700822 if (err)
823 goto out;
824 }
825 err = bpf_object__init_prog_names(obj);
Wang Nan29603662015-07-01 02:13:56 +0000826out:
827 return err;
828}
829
Wang Nan34090912015-07-01 02:14:02 +0000830static struct bpf_program *
831bpf_object__find_prog_by_idx(struct bpf_object *obj, int idx)
832{
833 struct bpf_program *prog;
834 size_t i;
835
836 for (i = 0; i < obj->nr_programs; i++) {
837 prog = &obj->programs[i];
838 if (prog->idx == idx)
839 return prog;
840 }
841 return NULL;
842}
843
844static int
845bpf_program__collect_reloc(struct bpf_program *prog,
846 size_t nr_maps, GElf_Shdr *shdr,
Wang Nan666810e2016-01-25 09:55:49 +0000847 Elf_Data *data, Elf_Data *symbols,
Joe Stringer94e5ade2017-01-22 17:11:22 -0800848 int maps_shndx, struct bpf_map *maps)
Wang Nan34090912015-07-01 02:14:02 +0000849{
850 int i, nrels;
851
852 pr_debug("collecting relocating info for: '%s'\n",
853 prog->section_name);
854 nrels = shdr->sh_size / shdr->sh_entsize;
855
856 prog->reloc_desc = malloc(sizeof(*prog->reloc_desc) * nrels);
857 if (!prog->reloc_desc) {
858 pr_warning("failed to alloc memory in relocation\n");
859 return -ENOMEM;
860 }
861 prog->nr_reloc = nrels;
862
863 for (i = 0; i < nrels; i++) {
864 GElf_Sym sym;
865 GElf_Rel rel;
866 unsigned int insn_idx;
867 struct bpf_insn *insns = prog->insns;
868 size_t map_idx;
869
870 if (!gelf_getrel(data, i, &rel)) {
871 pr_warning("relocation: failed to get %d reloc\n", i);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000872 return -LIBBPF_ERRNO__FORMAT;
Wang Nan34090912015-07-01 02:14:02 +0000873 }
874
Wang Nan34090912015-07-01 02:14:02 +0000875 if (!gelf_getsym(symbols,
876 GELF_R_SYM(rel.r_info),
877 &sym)) {
878 pr_warning("relocation: symbol %"PRIx64" not found\n",
879 GELF_R_SYM(rel.r_info));
Wang Nan6371ca3b2015-11-06 13:49:37 +0000880 return -LIBBPF_ERRNO__FORMAT;
Wang Nan34090912015-07-01 02:14:02 +0000881 }
882
Wang Nan666810e2016-01-25 09:55:49 +0000883 if (sym.st_shndx != maps_shndx) {
884 pr_warning("Program '%s' contains non-map related relo data pointing to section %u\n",
885 prog->section_name, sym.st_shndx);
886 return -LIBBPF_ERRNO__RELOC;
887 }
888
889 insn_idx = rel.r_offset / sizeof(struct bpf_insn);
890 pr_debug("relocation: insn_idx=%u\n", insn_idx);
891
Wang Nan34090912015-07-01 02:14:02 +0000892 if (insns[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
893 pr_warning("bpf: relocation: invalid relo for insns[%d].code 0x%x\n",
894 insn_idx, insns[insn_idx].code);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000895 return -LIBBPF_ERRNO__RELOC;
Wang Nan34090912015-07-01 02:14:02 +0000896 }
897
Joe Stringer94e5ade2017-01-22 17:11:22 -0800898 /* TODO: 'maps' is sorted. We can use bsearch to make it faster. */
899 for (map_idx = 0; map_idx < nr_maps; map_idx++) {
900 if (maps[map_idx].offset == sym.st_value) {
901 pr_debug("relocation: find map %zd (%s) for insn %u\n",
902 map_idx, maps[map_idx].name, insn_idx);
903 break;
904 }
905 }
906
Wang Nan34090912015-07-01 02:14:02 +0000907 if (map_idx >= nr_maps) {
908 pr_warning("bpf relocation: map_idx %d large than %d\n",
909 (int)map_idx, (int)nr_maps - 1);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000910 return -LIBBPF_ERRNO__RELOC;
Wang Nan34090912015-07-01 02:14:02 +0000911 }
912
913 prog->reloc_desc[i].insn_idx = insn_idx;
914 prog->reloc_desc[i].map_idx = map_idx;
915 }
916 return 0;
917}
918
Wang Nan52d33522015-07-01 02:14:04 +0000919static int
920bpf_object__create_maps(struct bpf_object *obj)
921{
922 unsigned int i;
Wang Nan52d33522015-07-01 02:14:04 +0000923
Wang Nan9d759a92015-11-27 08:47:35 +0000924 for (i = 0; i < obj->nr_maps; i++) {
925 struct bpf_map_def *def = &obj->maps[i].def;
926 int *pfd = &obj->maps[i].fd;
Wang Nan52d33522015-07-01 02:14:04 +0000927
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700928 *pfd = bpf_create_map_name(def->type,
929 obj->maps[i].name,
930 def->key_size,
931 def->value_size,
932 def->max_entries,
933 0);
Wang Nan52d33522015-07-01 02:14:04 +0000934 if (*pfd < 0) {
935 size_t j;
936 int err = *pfd;
937
Eric Leblond49bf4b32017-08-20 21:48:14 +0200938 pr_warning("failed to create map (name: '%s'): %s\n",
939 obj->maps[i].name,
Wang Nan52d33522015-07-01 02:14:04 +0000940 strerror(errno));
941 for (j = 0; j < i; j++)
Wang Nan9d759a92015-11-27 08:47:35 +0000942 zclose(obj->maps[j].fd);
Wang Nan52d33522015-07-01 02:14:04 +0000943 return err;
944 }
Eric Leblond4708bbd2016-11-15 04:05:47 +0000945 pr_debug("create map %s: fd=%d\n", obj->maps[i].name, *pfd);
Wang Nan52d33522015-07-01 02:14:04 +0000946 }
947
Wang Nan52d33522015-07-01 02:14:04 +0000948 return 0;
949}
950
Wang Nan8a47a6c2015-07-01 02:14:05 +0000951static int
Wang Nan9d759a92015-11-27 08:47:35 +0000952bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj)
Wang Nan8a47a6c2015-07-01 02:14:05 +0000953{
954 int i;
955
956 if (!prog || !prog->reloc_desc)
957 return 0;
958
959 for (i = 0; i < prog->nr_reloc; i++) {
960 int insn_idx, map_idx;
961 struct bpf_insn *insns = prog->insns;
962
963 insn_idx = prog->reloc_desc[i].insn_idx;
964 map_idx = prog->reloc_desc[i].map_idx;
965
966 if (insn_idx >= (int)prog->insns_cnt) {
967 pr_warning("relocation out of range: '%s'\n",
968 prog->section_name);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000969 return -LIBBPF_ERRNO__RELOC;
Wang Nan8a47a6c2015-07-01 02:14:05 +0000970 }
971 insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
Wang Nan9d759a92015-11-27 08:47:35 +0000972 insns[insn_idx].imm = obj->maps[map_idx].fd;
Wang Nan8a47a6c2015-07-01 02:14:05 +0000973 }
974
975 zfree(&prog->reloc_desc);
976 prog->nr_reloc = 0;
977 return 0;
978}
979
980
981static int
982bpf_object__relocate(struct bpf_object *obj)
983{
984 struct bpf_program *prog;
985 size_t i;
986 int err;
987
988 for (i = 0; i < obj->nr_programs; i++) {
989 prog = &obj->programs[i];
990
Wang Nan9d759a92015-11-27 08:47:35 +0000991 err = bpf_program__relocate(prog, obj);
Wang Nan8a47a6c2015-07-01 02:14:05 +0000992 if (err) {
993 pr_warning("failed to relocate '%s'\n",
994 prog->section_name);
995 return err;
996 }
997 }
998 return 0;
999}
1000
Wang Nan34090912015-07-01 02:14:02 +00001001static int bpf_object__collect_reloc(struct bpf_object *obj)
1002{
1003 int i, err;
1004
1005 if (!obj_elf_valid(obj)) {
1006 pr_warning("Internal error: elf object is closed\n");
Wang Nan6371ca3b2015-11-06 13:49:37 +00001007 return -LIBBPF_ERRNO__INTERNAL;
Wang Nan34090912015-07-01 02:14:02 +00001008 }
1009
1010 for (i = 0; i < obj->efile.nr_reloc; i++) {
1011 GElf_Shdr *shdr = &obj->efile.reloc[i].shdr;
1012 Elf_Data *data = obj->efile.reloc[i].data;
1013 int idx = shdr->sh_info;
1014 struct bpf_program *prog;
Wang Nan9d759a92015-11-27 08:47:35 +00001015 size_t nr_maps = obj->nr_maps;
Wang Nan34090912015-07-01 02:14:02 +00001016
1017 if (shdr->sh_type != SHT_REL) {
1018 pr_warning("internal error at %d\n", __LINE__);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001019 return -LIBBPF_ERRNO__INTERNAL;
Wang Nan34090912015-07-01 02:14:02 +00001020 }
1021
1022 prog = bpf_object__find_prog_by_idx(obj, idx);
1023 if (!prog) {
1024 pr_warning("relocation failed: no %d section\n",
1025 idx);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001026 return -LIBBPF_ERRNO__RELOC;
Wang Nan34090912015-07-01 02:14:02 +00001027 }
1028
1029 err = bpf_program__collect_reloc(prog, nr_maps,
1030 shdr, data,
Wang Nan666810e2016-01-25 09:55:49 +00001031 obj->efile.symbols,
Joe Stringer94e5ade2017-01-22 17:11:22 -08001032 obj->efile.maps_shndx,
1033 obj->maps);
Wang Nan34090912015-07-01 02:14:02 +00001034 if (err)
Wang Nan6371ca3b2015-11-06 13:49:37 +00001035 return err;
Wang Nan34090912015-07-01 02:14:02 +00001036 }
1037 return 0;
1038}
1039
Wang Nan55cffde2015-07-01 02:14:07 +00001040static int
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -07001041load_program(enum bpf_prog_type type, const char *name, struct bpf_insn *insns,
Wang Nan5f44e4c82016-07-13 10:44:01 +00001042 int insns_cnt, char *license, u32 kern_version, int *pfd)
Wang Nan55cffde2015-07-01 02:14:07 +00001043{
1044 int ret;
1045 char *log_buf;
1046
1047 if (!insns || !insns_cnt)
1048 return -EINVAL;
1049
1050 log_buf = malloc(BPF_LOG_BUF_SIZE);
1051 if (!log_buf)
1052 pr_warning("Alloc log buffer for bpf loader error, continue without log\n");
1053
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -07001054 ret = bpf_load_program_name(type, name, insns, insns_cnt, license,
1055 kern_version, log_buf, BPF_LOG_BUF_SIZE);
Wang Nan55cffde2015-07-01 02:14:07 +00001056
1057 if (ret >= 0) {
1058 *pfd = ret;
1059 ret = 0;
1060 goto out;
1061 }
1062
Wang Nan6371ca3b2015-11-06 13:49:37 +00001063 ret = -LIBBPF_ERRNO__LOAD;
Wang Nan55cffde2015-07-01 02:14:07 +00001064 pr_warning("load bpf program failed: %s\n", strerror(errno));
1065
Wang Nan6371ca3b2015-11-06 13:49:37 +00001066 if (log_buf && log_buf[0] != '\0') {
1067 ret = -LIBBPF_ERRNO__VERIFY;
Wang Nan55cffde2015-07-01 02:14:07 +00001068 pr_warning("-- BEGIN DUMP LOG ---\n");
1069 pr_warning("\n%s\n", log_buf);
1070 pr_warning("-- END LOG --\n");
Wang Nan705fa212016-07-13 10:44:02 +00001071 } else if (insns_cnt >= BPF_MAXINSNS) {
1072 pr_warning("Program too large (%d insns), at most %d insns\n",
1073 insns_cnt, BPF_MAXINSNS);
1074 ret = -LIBBPF_ERRNO__PROG2BIG;
Wang Nan6371ca3b2015-11-06 13:49:37 +00001075 } else {
Wang Nan705fa212016-07-13 10:44:02 +00001076 /* Wrong program type? */
1077 if (type != BPF_PROG_TYPE_KPROBE) {
1078 int fd;
1079
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -07001080 fd = bpf_load_program_name(BPF_PROG_TYPE_KPROBE, name,
1081 insns, insns_cnt, license,
1082 kern_version, NULL, 0);
Wang Nan705fa212016-07-13 10:44:02 +00001083 if (fd >= 0) {
1084 close(fd);
1085 ret = -LIBBPF_ERRNO__PROGTYPE;
1086 goto out;
1087 }
Wang Nan6371ca3b2015-11-06 13:49:37 +00001088 }
Wang Nan705fa212016-07-13 10:44:02 +00001089
1090 if (log_buf)
1091 ret = -LIBBPF_ERRNO__KVER;
Wang Nan55cffde2015-07-01 02:14:07 +00001092 }
1093
1094out:
1095 free(log_buf);
1096 return ret;
1097}
1098
1099static int
1100bpf_program__load(struct bpf_program *prog,
1101 char *license, u32 kern_version)
1102{
Wang Nanb5805632015-11-16 12:10:09 +00001103 int err = 0, fd, i;
Wang Nan55cffde2015-07-01 02:14:07 +00001104
Wang Nanb5805632015-11-16 12:10:09 +00001105 if (prog->instances.nr < 0 || !prog->instances.fds) {
1106 if (prog->preprocessor) {
1107 pr_warning("Internal error: can't load program '%s'\n",
1108 prog->section_name);
1109 return -LIBBPF_ERRNO__INTERNAL;
1110 }
Wang Nan55cffde2015-07-01 02:14:07 +00001111
Wang Nanb5805632015-11-16 12:10:09 +00001112 prog->instances.fds = malloc(sizeof(int));
1113 if (!prog->instances.fds) {
1114 pr_warning("Not enough memory for BPF fds\n");
1115 return -ENOMEM;
1116 }
1117 prog->instances.nr = 1;
1118 prog->instances.fds[0] = -1;
1119 }
1120
1121 if (!prog->preprocessor) {
1122 if (prog->instances.nr != 1) {
1123 pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n",
1124 prog->section_name, prog->instances.nr);
1125 }
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -07001126 err = load_program(prog->type, prog->name, prog->insns,
1127 prog->insns_cnt, license, kern_version, &fd);
Wang Nanb5805632015-11-16 12:10:09 +00001128 if (!err)
1129 prog->instances.fds[0] = fd;
1130 goto out;
1131 }
1132
1133 for (i = 0; i < prog->instances.nr; i++) {
1134 struct bpf_prog_prep_result result;
1135 bpf_program_prep_t preprocessor = prog->preprocessor;
1136
1137 bzero(&result, sizeof(result));
1138 err = preprocessor(prog, i, prog->insns,
1139 prog->insns_cnt, &result);
1140 if (err) {
1141 pr_warning("Preprocessing the %dth instance of program '%s' failed\n",
1142 i, prog->section_name);
1143 goto out;
1144 }
1145
1146 if (!result.new_insn_ptr || !result.new_insn_cnt) {
1147 pr_debug("Skip loading the %dth instance of program '%s'\n",
1148 i, prog->section_name);
1149 prog->instances.fds[i] = -1;
1150 if (result.pfd)
1151 *result.pfd = -1;
1152 continue;
1153 }
1154
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -07001155 err = load_program(prog->type, prog->name,
1156 result.new_insn_ptr,
Wang Nanb5805632015-11-16 12:10:09 +00001157 result.new_insn_cnt,
1158 license, kern_version, &fd);
1159
1160 if (err) {
1161 pr_warning("Loading the %dth instance of program '%s' failed\n",
1162 i, prog->section_name);
1163 goto out;
1164 }
1165
1166 if (result.pfd)
1167 *result.pfd = fd;
1168 prog->instances.fds[i] = fd;
1169 }
1170out:
Wang Nan55cffde2015-07-01 02:14:07 +00001171 if (err)
1172 pr_warning("failed to load program '%s'\n",
1173 prog->section_name);
1174 zfree(&prog->insns);
1175 prog->insns_cnt = 0;
1176 return err;
1177}
1178
1179static int
1180bpf_object__load_progs(struct bpf_object *obj)
1181{
1182 size_t i;
1183 int err;
1184
1185 for (i = 0; i < obj->nr_programs; i++) {
1186 err = bpf_program__load(&obj->programs[i],
1187 obj->license,
1188 obj->kern_version);
1189 if (err)
1190 return err;
1191 }
1192 return 0;
1193}
1194
Wang Nancb1e5e92015-07-01 02:13:57 +00001195static int bpf_object__validate(struct bpf_object *obj)
1196{
1197 if (obj->kern_version == 0) {
1198 pr_warning("%s doesn't provide kernel version\n",
1199 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001200 return -LIBBPF_ERRNO__KVERSION;
Wang Nancb1e5e92015-07-01 02:13:57 +00001201 }
1202 return 0;
1203}
1204
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001205static struct bpf_object *
Wang Nan6c956392015-07-01 02:13:54 +00001206__bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz)
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001207{
1208 struct bpf_object *obj;
Wang Nan6371ca3b2015-11-06 13:49:37 +00001209 int err;
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001210
1211 if (elf_version(EV_CURRENT) == EV_NONE) {
1212 pr_warning("failed to init libelf for %s\n", path);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001213 return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001214 }
1215
Wang Nan6c956392015-07-01 02:13:54 +00001216 obj = bpf_object__new(path, obj_buf, obj_buf_sz);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001217 if (IS_ERR(obj))
1218 return obj;
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001219
Wang Nan6371ca3b2015-11-06 13:49:37 +00001220 CHECK_ERR(bpf_object__elf_init(obj), err, out);
1221 CHECK_ERR(bpf_object__check_endianness(obj), err, out);
1222 CHECK_ERR(bpf_object__elf_collect(obj), err, out);
1223 CHECK_ERR(bpf_object__collect_reloc(obj), err, out);
1224 CHECK_ERR(bpf_object__validate(obj), err, out);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001225
1226 bpf_object__elf_finish(obj);
1227 return obj;
1228out:
1229 bpf_object__close(obj);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001230 return ERR_PTR(err);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001231}
1232
1233struct bpf_object *bpf_object__open(const char *path)
1234{
1235 /* param validation */
1236 if (!path)
1237 return NULL;
1238
1239 pr_debug("loading %s\n", path);
1240
Wang Nan6c956392015-07-01 02:13:54 +00001241 return __bpf_object__open(path, NULL, 0);
1242}
1243
1244struct bpf_object *bpf_object__open_buffer(void *obj_buf,
Wang Nanacf860a2015-08-27 02:30:55 +00001245 size_t obj_buf_sz,
1246 const char *name)
Wang Nan6c956392015-07-01 02:13:54 +00001247{
Wang Nanacf860a2015-08-27 02:30:55 +00001248 char tmp_name[64];
1249
Wang Nan6c956392015-07-01 02:13:54 +00001250 /* param validation */
1251 if (!obj_buf || obj_buf_sz <= 0)
1252 return NULL;
1253
Wang Nanacf860a2015-08-27 02:30:55 +00001254 if (!name) {
1255 snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx",
1256 (unsigned long)obj_buf,
1257 (unsigned long)obj_buf_sz);
1258 tmp_name[sizeof(tmp_name) - 1] = '\0';
1259 name = tmp_name;
1260 }
1261 pr_debug("loading object '%s' from buffer\n",
1262 name);
Wang Nan6c956392015-07-01 02:13:54 +00001263
Wang Nanacf860a2015-08-27 02:30:55 +00001264 return __bpf_object__open(name, obj_buf, obj_buf_sz);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001265}
1266
Wang Nan52d33522015-07-01 02:14:04 +00001267int bpf_object__unload(struct bpf_object *obj)
1268{
1269 size_t i;
1270
1271 if (!obj)
1272 return -EINVAL;
1273
Wang Nan9d759a92015-11-27 08:47:35 +00001274 for (i = 0; i < obj->nr_maps; i++)
1275 zclose(obj->maps[i].fd);
Wang Nan52d33522015-07-01 02:14:04 +00001276
Wang Nan55cffde2015-07-01 02:14:07 +00001277 for (i = 0; i < obj->nr_programs; i++)
1278 bpf_program__unload(&obj->programs[i]);
1279
Wang Nan52d33522015-07-01 02:14:04 +00001280 return 0;
1281}
1282
1283int bpf_object__load(struct bpf_object *obj)
1284{
Wang Nan6371ca3b2015-11-06 13:49:37 +00001285 int err;
1286
Wang Nan52d33522015-07-01 02:14:04 +00001287 if (!obj)
1288 return -EINVAL;
1289
1290 if (obj->loaded) {
1291 pr_warning("object should not be loaded twice\n");
1292 return -EINVAL;
1293 }
1294
1295 obj->loaded = true;
Wang Nan6371ca3b2015-11-06 13:49:37 +00001296
1297 CHECK_ERR(bpf_object__create_maps(obj), err, out);
1298 CHECK_ERR(bpf_object__relocate(obj), err, out);
1299 CHECK_ERR(bpf_object__load_progs(obj), err, out);
Wang Nan52d33522015-07-01 02:14:04 +00001300
1301 return 0;
1302out:
1303 bpf_object__unload(obj);
1304 pr_warning("failed to load object '%s'\n", obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001305 return err;
Wang Nan52d33522015-07-01 02:14:04 +00001306}
1307
Joe Stringerf3675402017-01-26 13:19:56 -08001308static int check_path(const char *path)
1309{
1310 struct statfs st_fs;
1311 char *dname, *dir;
1312 int err = 0;
1313
1314 if (path == NULL)
1315 return -EINVAL;
1316
1317 dname = strdup(path);
1318 if (dname == NULL)
1319 return -ENOMEM;
1320
1321 dir = dirname(dname);
1322 if (statfs(dir, &st_fs)) {
1323 pr_warning("failed to statfs %s: %s\n", dir, strerror(errno));
1324 err = -errno;
1325 }
1326 free(dname);
1327
1328 if (!err && st_fs.f_type != BPF_FS_MAGIC) {
1329 pr_warning("specified path %s is not on BPF FS\n", path);
1330 err = -EINVAL;
1331 }
1332
1333 return err;
1334}
1335
1336int bpf_program__pin_instance(struct bpf_program *prog, const char *path,
1337 int instance)
1338{
1339 int err;
1340
1341 err = check_path(path);
1342 if (err)
1343 return err;
1344
1345 if (prog == NULL) {
1346 pr_warning("invalid program pointer\n");
1347 return -EINVAL;
1348 }
1349
1350 if (instance < 0 || instance >= prog->instances.nr) {
1351 pr_warning("invalid prog instance %d of prog %s (max %d)\n",
1352 instance, prog->section_name, prog->instances.nr);
1353 return -EINVAL;
1354 }
1355
1356 if (bpf_obj_pin(prog->instances.fds[instance], path)) {
1357 pr_warning("failed to pin program: %s\n", strerror(errno));
1358 return -errno;
1359 }
1360 pr_debug("pinned program '%s'\n", path);
1361
1362 return 0;
1363}
1364
1365static int make_dir(const char *path)
1366{
1367 int err = 0;
1368
1369 if (mkdir(path, 0700) && errno != EEXIST)
1370 err = -errno;
1371
1372 if (err)
1373 pr_warning("failed to mkdir %s: %s\n", path, strerror(-err));
1374 return err;
1375}
1376
1377int bpf_program__pin(struct bpf_program *prog, const char *path)
1378{
1379 int i, err;
1380
1381 err = check_path(path);
1382 if (err)
1383 return err;
1384
1385 if (prog == NULL) {
1386 pr_warning("invalid program pointer\n");
1387 return -EINVAL;
1388 }
1389
1390 if (prog->instances.nr <= 0) {
1391 pr_warning("no instances of prog %s to pin\n",
1392 prog->section_name);
1393 return -EINVAL;
1394 }
1395
1396 err = make_dir(path);
1397 if (err)
1398 return err;
1399
1400 for (i = 0; i < prog->instances.nr; i++) {
1401 char buf[PATH_MAX];
1402 int len;
1403
1404 len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
1405 if (len < 0)
1406 return -EINVAL;
1407 else if (len >= PATH_MAX)
1408 return -ENAMETOOLONG;
1409
1410 err = bpf_program__pin_instance(prog, buf, i);
1411 if (err)
1412 return err;
1413 }
1414
1415 return 0;
1416}
1417
Joe Stringerb6989f32017-01-26 13:19:57 -08001418int bpf_map__pin(struct bpf_map *map, const char *path)
1419{
1420 int err;
1421
1422 err = check_path(path);
1423 if (err)
1424 return err;
1425
1426 if (map == NULL) {
1427 pr_warning("invalid map pointer\n");
1428 return -EINVAL;
1429 }
1430
1431 if (bpf_obj_pin(map->fd, path)) {
1432 pr_warning("failed to pin map: %s\n", strerror(errno));
1433 return -errno;
1434 }
1435
1436 pr_debug("pinned map '%s'\n", path);
1437 return 0;
1438}
1439
Joe Stringerd5148d82017-01-26 13:19:58 -08001440int bpf_object__pin(struct bpf_object *obj, const char *path)
1441{
1442 struct bpf_program *prog;
1443 struct bpf_map *map;
1444 int err;
1445
1446 if (!obj)
1447 return -ENOENT;
1448
1449 if (!obj->loaded) {
1450 pr_warning("object not yet loaded; load it first\n");
1451 return -ENOENT;
1452 }
1453
1454 err = make_dir(path);
1455 if (err)
1456 return err;
1457
1458 bpf_map__for_each(map, obj) {
1459 char buf[PATH_MAX];
1460 int len;
1461
1462 len = snprintf(buf, PATH_MAX, "%s/%s", path,
1463 bpf_map__name(map));
1464 if (len < 0)
1465 return -EINVAL;
1466 else if (len >= PATH_MAX)
1467 return -ENAMETOOLONG;
1468
1469 err = bpf_map__pin(map, buf);
1470 if (err)
1471 return err;
1472 }
1473
1474 bpf_object__for_each_program(prog, obj) {
1475 char buf[PATH_MAX];
1476 int len;
1477
1478 len = snprintf(buf, PATH_MAX, "%s/%s", path,
1479 prog->section_name);
1480 if (len < 0)
1481 return -EINVAL;
1482 else if (len >= PATH_MAX)
1483 return -ENAMETOOLONG;
1484
1485 err = bpf_program__pin(prog, buf);
1486 if (err)
1487 return err;
1488 }
1489
1490 return 0;
1491}
1492
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001493void bpf_object__close(struct bpf_object *obj)
1494{
Wang Nana5b8bd42015-07-01 02:14:00 +00001495 size_t i;
1496
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001497 if (!obj)
1498 return;
1499
Wang Nan10931d22016-11-26 07:03:26 +00001500 if (obj->clear_priv)
1501 obj->clear_priv(obj, obj->priv);
1502
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001503 bpf_object__elf_finish(obj);
Wang Nan52d33522015-07-01 02:14:04 +00001504 bpf_object__unload(obj);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001505
Wang Nan9d759a92015-11-27 08:47:35 +00001506 for (i = 0; i < obj->nr_maps; i++) {
Wang Nan561bbcc2015-11-27 08:47:36 +00001507 zfree(&obj->maps[i].name);
Wang Nan9d759a92015-11-27 08:47:35 +00001508 if (obj->maps[i].clear_priv)
1509 obj->maps[i].clear_priv(&obj->maps[i],
1510 obj->maps[i].priv);
1511 obj->maps[i].priv = NULL;
1512 obj->maps[i].clear_priv = NULL;
1513 }
1514 zfree(&obj->maps);
1515 obj->nr_maps = 0;
Wang Nana5b8bd42015-07-01 02:14:00 +00001516
1517 if (obj->programs && obj->nr_programs) {
1518 for (i = 0; i < obj->nr_programs; i++)
1519 bpf_program__exit(&obj->programs[i]);
1520 }
1521 zfree(&obj->programs);
1522
Wang Nan9a208ef2015-07-01 02:14:10 +00001523 list_del(&obj->list);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001524 free(obj);
1525}
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001526
Wang Nan9a208ef2015-07-01 02:14:10 +00001527struct bpf_object *
1528bpf_object__next(struct bpf_object *prev)
1529{
1530 struct bpf_object *next;
1531
1532 if (!prev)
1533 next = list_first_entry(&bpf_objects_list,
1534 struct bpf_object,
1535 list);
1536 else
1537 next = list_next_entry(prev, list);
1538
1539 /* Empty list is noticed here so don't need checking on entry. */
1540 if (&next->list == &bpf_objects_list)
1541 return NULL;
1542
1543 return next;
1544}
1545
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -03001546const char *bpf_object__name(struct bpf_object *obj)
Wang Nanacf860a2015-08-27 02:30:55 +00001547{
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -03001548 return obj ? obj->path : ERR_PTR(-EINVAL);
Wang Nanacf860a2015-08-27 02:30:55 +00001549}
1550
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -03001551unsigned int bpf_object__kversion(struct bpf_object *obj)
Wang Nan45825d82015-11-06 13:49:38 +00001552{
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -03001553 return obj ? obj->kern_version : 0;
Wang Nan45825d82015-11-06 13:49:38 +00001554}
1555
Wang Nan10931d22016-11-26 07:03:26 +00001556int bpf_object__set_priv(struct bpf_object *obj, void *priv,
1557 bpf_object_clear_priv_t clear_priv)
1558{
1559 if (obj->priv && obj->clear_priv)
1560 obj->clear_priv(obj, obj->priv);
1561
1562 obj->priv = priv;
1563 obj->clear_priv = clear_priv;
1564 return 0;
1565}
1566
1567void *bpf_object__priv(struct bpf_object *obj)
1568{
1569 return obj ? obj->priv : ERR_PTR(-EINVAL);
1570}
1571
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001572struct bpf_program *
1573bpf_program__next(struct bpf_program *prev, struct bpf_object *obj)
1574{
1575 size_t idx;
1576
1577 if (!obj->programs)
1578 return NULL;
1579 /* First handler */
1580 if (prev == NULL)
1581 return &obj->programs[0];
1582
1583 if (prev->obj != obj) {
1584 pr_warning("error: program handler doesn't match object\n");
1585 return NULL;
1586 }
1587
1588 idx = (prev - obj->programs) + 1;
1589 if (idx >= obj->nr_programs)
1590 return NULL;
1591 return &obj->programs[idx];
1592}
1593
Arnaldo Carvalho de Meloedb13ed2016-06-03 12:38:21 -03001594int bpf_program__set_priv(struct bpf_program *prog, void *priv,
1595 bpf_program_clear_priv_t clear_priv)
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001596{
1597 if (prog->priv && prog->clear_priv)
1598 prog->clear_priv(prog, prog->priv);
1599
1600 prog->priv = priv;
1601 prog->clear_priv = clear_priv;
1602 return 0;
1603}
1604
Arnaldo Carvalho de Melobe834ff2016-06-03 12:36:39 -03001605void *bpf_program__priv(struct bpf_program *prog)
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001606{
Arnaldo Carvalho de Melobe834ff2016-06-03 12:36:39 -03001607 return prog ? prog->priv : ERR_PTR(-EINVAL);
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001608}
1609
Namhyung Kim715f8db2015-11-03 20:21:05 +09001610const char *bpf_program__title(struct bpf_program *prog, bool needs_copy)
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001611{
1612 const char *title;
1613
1614 title = prog->section_name;
Namhyung Kim715f8db2015-11-03 20:21:05 +09001615 if (needs_copy) {
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001616 title = strdup(title);
1617 if (!title) {
1618 pr_warning("failed to strdup program title\n");
Wang Nan6371ca3b2015-11-06 13:49:37 +00001619 return ERR_PTR(-ENOMEM);
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001620 }
1621 }
1622
1623 return title;
1624}
1625
1626int bpf_program__fd(struct bpf_program *prog)
1627{
Wang Nanb5805632015-11-16 12:10:09 +00001628 return bpf_program__nth_fd(prog, 0);
1629}
1630
1631int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,
1632 bpf_program_prep_t prep)
1633{
1634 int *instances_fds;
1635
1636 if (nr_instances <= 0 || !prep)
1637 return -EINVAL;
1638
1639 if (prog->instances.nr > 0 || prog->instances.fds) {
1640 pr_warning("Can't set pre-processor after loading\n");
1641 return -EINVAL;
1642 }
1643
1644 instances_fds = malloc(sizeof(int) * nr_instances);
1645 if (!instances_fds) {
1646 pr_warning("alloc memory failed for fds\n");
1647 return -ENOMEM;
1648 }
1649
1650 /* fill all fd with -1 */
1651 memset(instances_fds, -1, sizeof(int) * nr_instances);
1652
1653 prog->instances.nr = nr_instances;
1654 prog->instances.fds = instances_fds;
1655 prog->preprocessor = prep;
1656 return 0;
1657}
1658
1659int bpf_program__nth_fd(struct bpf_program *prog, int n)
1660{
1661 int fd;
1662
1663 if (n >= prog->instances.nr || n < 0) {
1664 pr_warning("Can't get the %dth fd from program %s: only %d instances\n",
1665 n, prog->section_name, prog->instances.nr);
1666 return -EINVAL;
1667 }
1668
1669 fd = prog->instances.fds[n];
1670 if (fd < 0) {
1671 pr_warning("%dth instance of program '%s' is invalid\n",
1672 n, prog->section_name);
1673 return -ENOENT;
1674 }
1675
1676 return fd;
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001677}
Wang Nan9d759a92015-11-27 08:47:35 +00001678
Alexei Starovoitovdd26b7f2017-03-30 21:45:40 -07001679void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type)
Wang Nan5f44e4c82016-07-13 10:44:01 +00001680{
1681 prog->type = type;
1682}
1683
Wang Nan5f44e4c82016-07-13 10:44:01 +00001684static bool bpf_program__is_type(struct bpf_program *prog,
1685 enum bpf_prog_type type)
1686{
1687 return prog ? (prog->type == type) : false;
1688}
1689
Joe Stringered794072017-01-22 17:11:23 -08001690#define BPF_PROG_TYPE_FNS(NAME, TYPE) \
1691int bpf_program__set_##NAME(struct bpf_program *prog) \
1692{ \
1693 if (!prog) \
1694 return -EINVAL; \
1695 bpf_program__set_type(prog, TYPE); \
1696 return 0; \
1697} \
1698 \
1699bool bpf_program__is_##NAME(struct bpf_program *prog) \
1700{ \
1701 return bpf_program__is_type(prog, TYPE); \
1702} \
Wang Nan5f44e4c82016-07-13 10:44:01 +00001703
Joe Stringer7803ba72017-01-22 17:11:24 -08001704BPF_PROG_TYPE_FNS(socket_filter, BPF_PROG_TYPE_SOCKET_FILTER);
Joe Stringered794072017-01-22 17:11:23 -08001705BPF_PROG_TYPE_FNS(kprobe, BPF_PROG_TYPE_KPROBE);
Joe Stringer7803ba72017-01-22 17:11:24 -08001706BPF_PROG_TYPE_FNS(sched_cls, BPF_PROG_TYPE_SCHED_CLS);
1707BPF_PROG_TYPE_FNS(sched_act, BPF_PROG_TYPE_SCHED_ACT);
Joe Stringered794072017-01-22 17:11:23 -08001708BPF_PROG_TYPE_FNS(tracepoint, BPF_PROG_TYPE_TRACEPOINT);
Joe Stringer7803ba72017-01-22 17:11:24 -08001709BPF_PROG_TYPE_FNS(xdp, BPF_PROG_TYPE_XDP);
1710BPF_PROG_TYPE_FNS(perf_event, BPF_PROG_TYPE_PERF_EVENT);
Wang Nan5f44e4c82016-07-13 10:44:01 +00001711
Arnaldo Carvalho de Melo6e009e62016-06-03 12:15:52 -03001712int bpf_map__fd(struct bpf_map *map)
Wang Nan9d759a92015-11-27 08:47:35 +00001713{
Arnaldo Carvalho de Melo6e009e62016-06-03 12:15:52 -03001714 return map ? map->fd : -EINVAL;
Wang Nan9d759a92015-11-27 08:47:35 +00001715}
1716
Arnaldo Carvalho de Melo53897a72016-06-02 14:21:06 -03001717const struct bpf_map_def *bpf_map__def(struct bpf_map *map)
Wang Nan9d759a92015-11-27 08:47:35 +00001718{
Arnaldo Carvalho de Melo53897a72016-06-02 14:21:06 -03001719 return map ? &map->def : ERR_PTR(-EINVAL);
Wang Nan9d759a92015-11-27 08:47:35 +00001720}
1721
Arnaldo Carvalho de Melo009ad5d2016-06-02 11:02:05 -03001722const char *bpf_map__name(struct bpf_map *map)
Wang Nan561bbcc2015-11-27 08:47:36 +00001723{
Arnaldo Carvalho de Melo009ad5d2016-06-02 11:02:05 -03001724 return map ? map->name : NULL;
Wang Nan561bbcc2015-11-27 08:47:36 +00001725}
1726
Arnaldo Carvalho de Meloedb13ed2016-06-03 12:38:21 -03001727int bpf_map__set_priv(struct bpf_map *map, void *priv,
1728 bpf_map_clear_priv_t clear_priv)
Wang Nan9d759a92015-11-27 08:47:35 +00001729{
1730 if (!map)
1731 return -EINVAL;
1732
1733 if (map->priv) {
1734 if (map->clear_priv)
1735 map->clear_priv(map, map->priv);
1736 }
1737
1738 map->priv = priv;
1739 map->clear_priv = clear_priv;
1740 return 0;
1741}
1742
Arnaldo Carvalho de Melob4cbfa52016-06-02 10:51:59 -03001743void *bpf_map__priv(struct bpf_map *map)
Wang Nan9d759a92015-11-27 08:47:35 +00001744{
Arnaldo Carvalho de Melob4cbfa52016-06-02 10:51:59 -03001745 return map ? map->priv : ERR_PTR(-EINVAL);
Wang Nan9d759a92015-11-27 08:47:35 +00001746}
1747
1748struct bpf_map *
1749bpf_map__next(struct bpf_map *prev, struct bpf_object *obj)
1750{
1751 size_t idx;
1752 struct bpf_map *s, *e;
1753
1754 if (!obj || !obj->maps)
1755 return NULL;
1756
1757 s = obj->maps;
1758 e = obj->maps + obj->nr_maps;
1759
1760 if (prev == NULL)
1761 return s;
1762
1763 if ((prev < s) || (prev >= e)) {
1764 pr_warning("error in %s: map handler doesn't belong to object\n",
1765 __func__);
1766 return NULL;
1767 }
1768
1769 idx = (prev - obj->maps) + 1;
1770 if (idx >= obj->nr_maps)
1771 return NULL;
1772 return &obj->maps[idx];
1773}
Wang Nan561bbcc2015-11-27 08:47:36 +00001774
1775struct bpf_map *
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -03001776bpf_object__find_map_by_name(struct bpf_object *obj, const char *name)
Wang Nan561bbcc2015-11-27 08:47:36 +00001777{
1778 struct bpf_map *pos;
1779
1780 bpf_map__for_each(pos, obj) {
Wang Nan973170e2015-12-08 02:25:29 +00001781 if (pos->name && !strcmp(pos->name, name))
Wang Nan561bbcc2015-11-27 08:47:36 +00001782 return pos;
1783 }
1784 return NULL;
1785}
Wang Nan5a6acad2016-11-26 07:03:27 +00001786
1787struct bpf_map *
1788bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset)
1789{
1790 int i;
1791
1792 for (i = 0; i < obj->nr_maps; i++) {
1793 if (obj->maps[i].offset == offset)
1794 return &obj->maps[i];
1795 }
1796 return ERR_PTR(-ENOENT);
1797}
Joe Stringere28ff1a2017-01-22 17:11:25 -08001798
1799long libbpf_get_error(const void *ptr)
1800{
1801 if (IS_ERR(ptr))
1802 return PTR_ERR(ptr);
1803 return 0;
1804}
John Fastabend6f6d33f2017-08-15 22:34:22 -07001805
1806int bpf_prog_load(const char *file, enum bpf_prog_type type,
1807 struct bpf_object **pobj, int *prog_fd)
1808{
1809 struct bpf_program *prog;
1810 struct bpf_object *obj;
1811 int err;
1812
1813 obj = bpf_object__open(file);
1814 if (IS_ERR(obj))
1815 return -ENOENT;
1816
1817 prog = bpf_program__next(NULL, obj);
1818 if (!prog) {
1819 bpf_object__close(obj);
1820 return -ENOENT;
1821 }
1822
1823 bpf_program__set_type(prog, type);
1824 err = bpf_object__load(obj);
1825 if (err) {
1826 bpf_object__close(obj);
1827 return -EINVAL;
1828 }
1829
1830 *pobj = obj;
1831 *prog_fd = bpf_program__fd(prog);
1832 return 0;
1833}