blob: e3f4c3379f14a266304a2c236e8fca0af87e2230 [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.
7 */
8
9#include <stdlib.h>
Wang Nanb3f59d62015-07-01 02:13:52 +000010#include <stdio.h>
11#include <stdarg.h>
Wang Nan34090912015-07-01 02:14:02 +000012#include <inttypes.h>
Wang Nanb3f59d62015-07-01 02:13:52 +000013#include <string.h>
Wang Nan1b76c132015-07-01 02:13:51 +000014#include <unistd.h>
Wang Nan1a5e3fb2015-07-01 02:13:53 +000015#include <fcntl.h>
16#include <errno.h>
Wang Nan1b76c132015-07-01 02:13:51 +000017#include <asm/unistd.h>
Wang Nancb1e5e92015-07-01 02:13:57 +000018#include <linux/kernel.h>
Wang Nan1b76c132015-07-01 02:13:51 +000019#include <linux/bpf.h>
Wang Nan9a208ef2015-07-01 02:14:10 +000020#include <linux/list.h>
Wang Nan1a5e3fb2015-07-01 02:13:53 +000021#include <libelf.h>
22#include <gelf.h>
Wang Nan1b76c132015-07-01 02:13:51 +000023
24#include "libbpf.h"
Wang Nan52d33522015-07-01 02:14:04 +000025#include "bpf.h"
Wang Nanb3f59d62015-07-01 02:13:52 +000026
27#define __printf(a, b) __attribute__((format(printf, a, b)))
28
29__printf(1, 2)
30static int __base_pr(const char *format, ...)
31{
32 va_list args;
33 int err;
34
35 va_start(args, format);
36 err = vfprintf(stderr, format, args);
37 va_end(args);
38 return err;
39}
40
41static __printf(1, 2) libbpf_print_fn_t __pr_warning = __base_pr;
42static __printf(1, 2) libbpf_print_fn_t __pr_info = __base_pr;
43static __printf(1, 2) libbpf_print_fn_t __pr_debug;
44
45#define __pr(func, fmt, ...) \
46do { \
47 if ((func)) \
48 (func)("libbpf: " fmt, ##__VA_ARGS__); \
49} while (0)
50
51#define pr_warning(fmt, ...) __pr(__pr_warning, fmt, ##__VA_ARGS__)
52#define pr_info(fmt, ...) __pr(__pr_info, fmt, ##__VA_ARGS__)
53#define pr_debug(fmt, ...) __pr(__pr_debug, fmt, ##__VA_ARGS__)
54
55void libbpf_set_print(libbpf_print_fn_t warn,
56 libbpf_print_fn_t info,
57 libbpf_print_fn_t debug)
58{
59 __pr_warning = warn;
60 __pr_info = info;
61 __pr_debug = debug;
62}
Wang Nan1a5e3fb2015-07-01 02:13:53 +000063
Wang Nan6371ca3b2015-11-06 13:49:37 +000064#define STRERR_BUFSIZE 128
65
66#define ERRNO_OFFSET(e) ((e) - __LIBBPF_ERRNO__START)
67#define ERRCODE_OFFSET(c) ERRNO_OFFSET(LIBBPF_ERRNO__##c)
68#define NR_ERRNO (__LIBBPF_ERRNO__END - __LIBBPF_ERRNO__START)
69
70static const char *libbpf_strerror_table[NR_ERRNO] = {
71 [ERRCODE_OFFSET(LIBELF)] = "Something wrong in libelf",
72 [ERRCODE_OFFSET(FORMAT)] = "BPF object format invalid",
73 [ERRCODE_OFFSET(KVERSION)] = "'version' section incorrect or lost",
74 [ERRCODE_OFFSET(ENDIAN)] = "Endian missmatch",
75 [ERRCODE_OFFSET(INTERNAL)] = "Internal error in libbpf",
76 [ERRCODE_OFFSET(RELOC)] = "Relocation failed",
77 [ERRCODE_OFFSET(VERIFY)] = "Kernel verifier blocks program loading",
78 [ERRCODE_OFFSET(PROG2BIG)] = "Program too big",
79 [ERRCODE_OFFSET(KVER)] = "Incorrect kernel version",
80};
81
82int libbpf_strerror(int err, char *buf, size_t size)
83{
84 if (!buf || !size)
85 return -1;
86
87 err = err > 0 ? err : -err;
88
89 if (err < __LIBBPF_ERRNO__START) {
90 int ret;
91
92 ret = strerror_r(err, buf, size);
93 buf[size - 1] = '\0';
94 return ret;
95 }
96
97 if (err < __LIBBPF_ERRNO__END) {
98 const char *msg;
99
100 msg = libbpf_strerror_table[ERRNO_OFFSET(err)];
101 snprintf(buf, size, "%s", msg);
102 buf[size - 1] = '\0';
103 return 0;
104 }
105
106 snprintf(buf, size, "Unknown libbpf error %d", err);
107 buf[size - 1] = '\0';
108 return -1;
109}
110
111#define CHECK_ERR(action, err, out) do { \
112 err = action; \
113 if (err) \
114 goto out; \
115} while(0)
116
117
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000118/* Copied from tools/perf/util/util.h */
119#ifndef zfree
120# define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
121#endif
122
123#ifndef zclose
124# define zclose(fd) ({ \
125 int ___err = 0; \
126 if ((fd) >= 0) \
127 ___err = close((fd)); \
128 fd = -1; \
129 ___err; })
130#endif
131
132#ifdef HAVE_LIBELF_MMAP_SUPPORT
133# define LIBBPF_ELF_C_READ_MMAP ELF_C_READ_MMAP
134#else
135# define LIBBPF_ELF_C_READ_MMAP ELF_C_READ
136#endif
137
Wang Nana5b8bd42015-07-01 02:14:00 +0000138/*
139 * bpf_prog should be a better name but it has been used in
140 * linux/filter.h.
141 */
142struct bpf_program {
143 /* Index in elf obj file, for relocation use. */
144 int idx;
145 char *section_name;
146 struct bpf_insn *insns;
147 size_t insns_cnt;
Wang Nan34090912015-07-01 02:14:02 +0000148
149 struct {
150 int insn_idx;
151 int map_idx;
152 } *reloc_desc;
153 int nr_reloc;
Wang Nan55cffde2015-07-01 02:14:07 +0000154
Wang Nanb5805632015-11-16 12:10:09 +0000155 struct {
156 int nr;
157 int *fds;
158 } instances;
159 bpf_program_prep_t preprocessor;
Wang Nanaa9b1ac2015-07-01 02:14:08 +0000160
161 struct bpf_object *obj;
162 void *priv;
163 bpf_program_clear_priv_t clear_priv;
Wang Nana5b8bd42015-07-01 02:14:00 +0000164};
165
Wang Nan9a208ef2015-07-01 02:14:10 +0000166static LIST_HEAD(bpf_objects_list);
167
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000168struct bpf_object {
Wang Nancb1e5e92015-07-01 02:13:57 +0000169 char license[64];
170 u32 kern_version;
Wang Nan0b3d1ef2015-07-01 02:13:58 +0000171 void *maps_buf;
172 size_t maps_buf_sz;
173
Wang Nana5b8bd42015-07-01 02:14:00 +0000174 struct bpf_program *programs;
175 size_t nr_programs;
Wang Nan52d33522015-07-01 02:14:04 +0000176 int *map_fds;
177 /*
178 * This field is required because maps_buf will be freed and
179 * maps_buf_sz will be set to 0 after loaded.
180 */
181 size_t nr_map_fds;
182 bool loaded;
Wang Nana5b8bd42015-07-01 02:14:00 +0000183
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000184 /*
185 * Information when doing elf related work. Only valid if fd
186 * is valid.
187 */
188 struct {
189 int fd;
Wang Nan6c956392015-07-01 02:13:54 +0000190 void *obj_buf;
191 size_t obj_buf_sz;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000192 Elf *elf;
193 GElf_Ehdr ehdr;
Wang Nanbec7d682015-07-01 02:13:59 +0000194 Elf_Data *symbols;
Wang Nanb62f06e2015-07-01 02:14:01 +0000195 struct {
196 GElf_Shdr shdr;
197 Elf_Data *data;
198 } *reloc;
199 int nr_reloc;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000200 } efile;
Wang Nan9a208ef2015-07-01 02:14:10 +0000201 /*
202 * All loaded bpf_object is linked in a list, which is
203 * hidden to caller. bpf_objects__<func> handlers deal with
204 * all objects.
205 */
206 struct list_head list;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000207 char path[];
208};
209#define obj_elf_valid(o) ((o)->efile.elf)
210
Wang Nan55cffde2015-07-01 02:14:07 +0000211static void bpf_program__unload(struct bpf_program *prog)
212{
Wang Nanb5805632015-11-16 12:10:09 +0000213 int i;
214
Wang Nan55cffde2015-07-01 02:14:07 +0000215 if (!prog)
216 return;
217
Wang Nanb5805632015-11-16 12:10:09 +0000218 /*
219 * If the object is opened but the program was never loaded,
220 * it is possible that prog->instances.nr == -1.
221 */
222 if (prog->instances.nr > 0) {
223 for (i = 0; i < prog->instances.nr; i++)
224 zclose(prog->instances.fds[i]);
225 } else if (prog->instances.nr != -1) {
226 pr_warning("Internal error: instances.nr is %d\n",
227 prog->instances.nr);
228 }
229
230 prog->instances.nr = -1;
231 zfree(&prog->instances.fds);
Wang Nan55cffde2015-07-01 02:14:07 +0000232}
233
Wang Nana5b8bd42015-07-01 02:14:00 +0000234static void bpf_program__exit(struct bpf_program *prog)
235{
236 if (!prog)
237 return;
238
Wang Nanaa9b1ac2015-07-01 02:14:08 +0000239 if (prog->clear_priv)
240 prog->clear_priv(prog, prog->priv);
241
242 prog->priv = NULL;
243 prog->clear_priv = NULL;
244
Wang Nan55cffde2015-07-01 02:14:07 +0000245 bpf_program__unload(prog);
Wang Nana5b8bd42015-07-01 02:14:00 +0000246 zfree(&prog->section_name);
247 zfree(&prog->insns);
Wang Nan34090912015-07-01 02:14:02 +0000248 zfree(&prog->reloc_desc);
249
250 prog->nr_reloc = 0;
Wang Nana5b8bd42015-07-01 02:14:00 +0000251 prog->insns_cnt = 0;
252 prog->idx = -1;
253}
254
255static int
256bpf_program__init(void *data, size_t size, char *name, int idx,
257 struct bpf_program *prog)
258{
259 if (size < sizeof(struct bpf_insn)) {
260 pr_warning("corrupted section '%s'\n", name);
261 return -EINVAL;
262 }
263
264 bzero(prog, sizeof(*prog));
265
266 prog->section_name = strdup(name);
267 if (!prog->section_name) {
268 pr_warning("failed to alloc name for prog %s\n",
269 name);
270 goto errout;
271 }
272
273 prog->insns = malloc(size);
274 if (!prog->insns) {
275 pr_warning("failed to alloc insns for %s\n", name);
276 goto errout;
277 }
278 prog->insns_cnt = size / sizeof(struct bpf_insn);
279 memcpy(prog->insns, data,
280 prog->insns_cnt * sizeof(struct bpf_insn));
281 prog->idx = idx;
Wang Nanb5805632015-11-16 12:10:09 +0000282 prog->instances.fds = NULL;
283 prog->instances.nr = -1;
Wang Nana5b8bd42015-07-01 02:14:00 +0000284
285 return 0;
286errout:
287 bpf_program__exit(prog);
288 return -ENOMEM;
289}
290
291static int
292bpf_object__add_program(struct bpf_object *obj, void *data, size_t size,
293 char *name, int idx)
294{
295 struct bpf_program prog, *progs;
296 int nr_progs, err;
297
298 err = bpf_program__init(data, size, name, idx, &prog);
299 if (err)
300 return err;
301
302 progs = obj->programs;
303 nr_progs = obj->nr_programs;
304
305 progs = realloc(progs, sizeof(progs[0]) * (nr_progs + 1));
306 if (!progs) {
307 /*
308 * In this case the original obj->programs
309 * is still valid, so don't need special treat for
310 * bpf_close_object().
311 */
312 pr_warning("failed to alloc a new program '%s'\n",
313 name);
314 bpf_program__exit(&prog);
315 return -ENOMEM;
316 }
317
318 pr_debug("found program %s\n", prog.section_name);
319 obj->programs = progs;
320 obj->nr_programs = nr_progs + 1;
Wang Nanaa9b1ac2015-07-01 02:14:08 +0000321 prog.obj = obj;
Wang Nana5b8bd42015-07-01 02:14:00 +0000322 progs[nr_progs] = prog;
323 return 0;
324}
325
Wang Nan6c956392015-07-01 02:13:54 +0000326static struct bpf_object *bpf_object__new(const char *path,
327 void *obj_buf,
328 size_t obj_buf_sz)
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000329{
330 struct bpf_object *obj;
331
332 obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1);
333 if (!obj) {
334 pr_warning("alloc memory failed for %s\n", path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000335 return ERR_PTR(-ENOMEM);
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000336 }
337
338 strcpy(obj->path, path);
339 obj->efile.fd = -1;
Wang Nan6c956392015-07-01 02:13:54 +0000340
341 /*
342 * Caller of this function should also calls
343 * bpf_object__elf_finish() after data collection to return
344 * obj_buf to user. If not, we should duplicate the buffer to
345 * avoid user freeing them before elf finish.
346 */
347 obj->efile.obj_buf = obj_buf;
348 obj->efile.obj_buf_sz = obj_buf_sz;
349
Wang Nan52d33522015-07-01 02:14:04 +0000350 obj->loaded = false;
Wang Nan9a208ef2015-07-01 02:14:10 +0000351
352 INIT_LIST_HEAD(&obj->list);
353 list_add(&obj->list, &bpf_objects_list);
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000354 return obj;
355}
356
357static void bpf_object__elf_finish(struct bpf_object *obj)
358{
359 if (!obj_elf_valid(obj))
360 return;
361
362 if (obj->efile.elf) {
363 elf_end(obj->efile.elf);
364 obj->efile.elf = NULL;
365 }
Wang Nanbec7d682015-07-01 02:13:59 +0000366 obj->efile.symbols = NULL;
Wang Nanb62f06e2015-07-01 02:14:01 +0000367
368 zfree(&obj->efile.reloc);
369 obj->efile.nr_reloc = 0;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000370 zclose(obj->efile.fd);
Wang Nan6c956392015-07-01 02:13:54 +0000371 obj->efile.obj_buf = NULL;
372 obj->efile.obj_buf_sz = 0;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000373}
374
375static int bpf_object__elf_init(struct bpf_object *obj)
376{
377 int err = 0;
378 GElf_Ehdr *ep;
379
380 if (obj_elf_valid(obj)) {
381 pr_warning("elf init: internal error\n");
Wang Nan6371ca3b2015-11-06 13:49:37 +0000382 return -LIBBPF_ERRNO__LIBELF;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000383 }
384
Wang Nan6c956392015-07-01 02:13:54 +0000385 if (obj->efile.obj_buf_sz > 0) {
386 /*
387 * obj_buf should have been validated by
388 * bpf_object__open_buffer().
389 */
390 obj->efile.elf = elf_memory(obj->efile.obj_buf,
391 obj->efile.obj_buf_sz);
392 } else {
393 obj->efile.fd = open(obj->path, O_RDONLY);
394 if (obj->efile.fd < 0) {
395 pr_warning("failed to open %s: %s\n", obj->path,
396 strerror(errno));
397 return -errno;
398 }
399
400 obj->efile.elf = elf_begin(obj->efile.fd,
401 LIBBPF_ELF_C_READ_MMAP,
402 NULL);
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000403 }
404
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000405 if (!obj->efile.elf) {
406 pr_warning("failed to open %s as ELF file\n",
407 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000408 err = -LIBBPF_ERRNO__LIBELF;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000409 goto errout;
410 }
411
412 if (!gelf_getehdr(obj->efile.elf, &obj->efile.ehdr)) {
413 pr_warning("failed to get EHDR from %s\n",
414 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000415 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000416 goto errout;
417 }
418 ep = &obj->efile.ehdr;
419
420 if ((ep->e_type != ET_REL) || (ep->e_machine != 0)) {
421 pr_warning("%s is not an eBPF object file\n",
422 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000423 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000424 goto errout;
425 }
426
427 return 0;
428errout:
429 bpf_object__elf_finish(obj);
430 return err;
431}
432
Wang Nancc4228d2015-07-01 02:13:55 +0000433static int
434bpf_object__check_endianness(struct bpf_object *obj)
435{
436 static unsigned int const endian = 1;
437
438 switch (obj->efile.ehdr.e_ident[EI_DATA]) {
439 case ELFDATA2LSB:
440 /* We are big endian, BPF obj is little endian. */
441 if (*(unsigned char const *)&endian != 1)
442 goto mismatch;
443 break;
444
445 case ELFDATA2MSB:
446 /* We are little endian, BPF obj is big endian. */
447 if (*(unsigned char const *)&endian != 0)
448 goto mismatch;
449 break;
450 default:
Wang Nan6371ca3b2015-11-06 13:49:37 +0000451 return -LIBBPF_ERRNO__ENDIAN;
Wang Nancc4228d2015-07-01 02:13:55 +0000452 }
453
454 return 0;
455
456mismatch:
457 pr_warning("Error: endianness mismatch.\n");
Wang Nan6371ca3b2015-11-06 13:49:37 +0000458 return -LIBBPF_ERRNO__ENDIAN;
Wang Nancc4228d2015-07-01 02:13:55 +0000459}
460
Wang Nancb1e5e92015-07-01 02:13:57 +0000461static int
462bpf_object__init_license(struct bpf_object *obj,
463 void *data, size_t size)
464{
465 memcpy(obj->license, data,
466 min(size, sizeof(obj->license) - 1));
467 pr_debug("license of %s is %s\n", obj->path, obj->license);
468 return 0;
469}
470
471static int
472bpf_object__init_kversion(struct bpf_object *obj,
473 void *data, size_t size)
474{
475 u32 kver;
476
477 if (size != sizeof(kver)) {
478 pr_warning("invalid kver section in %s\n", obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000479 return -LIBBPF_ERRNO__FORMAT;
Wang Nancb1e5e92015-07-01 02:13:57 +0000480 }
481 memcpy(&kver, data, sizeof(kver));
482 obj->kern_version = kver;
483 pr_debug("kernel version of %s is %x\n", obj->path,
484 obj->kern_version);
485 return 0;
486}
487
Wang Nan0b3d1ef2015-07-01 02:13:58 +0000488static int
489bpf_object__init_maps(struct bpf_object *obj, void *data,
490 size_t size)
491{
492 if (size == 0) {
493 pr_debug("%s doesn't need map definition\n",
494 obj->path);
495 return 0;
496 }
497
498 obj->maps_buf = malloc(size);
499 if (!obj->maps_buf) {
500 pr_warning("malloc maps failed: %s\n", obj->path);
501 return -ENOMEM;
502 }
503
504 obj->maps_buf_sz = size;
505 memcpy(obj->maps_buf, data, size);
506 pr_debug("maps in %s: %ld bytes\n", obj->path, (long)size);
507 return 0;
508}
509
Wang Nan29603662015-07-01 02:13:56 +0000510static int bpf_object__elf_collect(struct bpf_object *obj)
511{
512 Elf *elf = obj->efile.elf;
513 GElf_Ehdr *ep = &obj->efile.ehdr;
514 Elf_Scn *scn = NULL;
515 int idx = 0, err = 0;
516
517 /* Elf is corrupted/truncated, avoid calling elf_strptr. */
518 if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) {
519 pr_warning("failed to get e_shstrndx from %s\n",
520 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000521 return -LIBBPF_ERRNO__FORMAT;
Wang Nan29603662015-07-01 02:13:56 +0000522 }
523
524 while ((scn = elf_nextscn(elf, scn)) != NULL) {
525 char *name;
526 GElf_Shdr sh;
527 Elf_Data *data;
528
529 idx++;
530 if (gelf_getshdr(scn, &sh) != &sh) {
531 pr_warning("failed to get section header from %s\n",
532 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000533 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan29603662015-07-01 02:13:56 +0000534 goto out;
535 }
536
537 name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name);
538 if (!name) {
539 pr_warning("failed to get section name from %s\n",
540 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000541 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan29603662015-07-01 02:13:56 +0000542 goto out;
543 }
544
545 data = elf_getdata(scn, 0);
546 if (!data) {
547 pr_warning("failed to get section data from %s(%s)\n",
548 name, obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000549 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan29603662015-07-01 02:13:56 +0000550 goto out;
551 }
552 pr_debug("section %s, size %ld, link %d, flags %lx, type=%d\n",
553 name, (unsigned long)data->d_size,
554 (int)sh.sh_link, (unsigned long)sh.sh_flags,
555 (int)sh.sh_type);
Wang Nancb1e5e92015-07-01 02:13:57 +0000556
557 if (strcmp(name, "license") == 0)
558 err = bpf_object__init_license(obj,
559 data->d_buf,
560 data->d_size);
561 else if (strcmp(name, "version") == 0)
562 err = bpf_object__init_kversion(obj,
563 data->d_buf,
564 data->d_size);
Wang Nan0b3d1ef2015-07-01 02:13:58 +0000565 else if (strcmp(name, "maps") == 0)
566 err = bpf_object__init_maps(obj, data->d_buf,
567 data->d_size);
Wang Nanbec7d682015-07-01 02:13:59 +0000568 else if (sh.sh_type == SHT_SYMTAB) {
569 if (obj->efile.symbols) {
570 pr_warning("bpf: multiple SYMTAB in %s\n",
571 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000572 err = -LIBBPF_ERRNO__FORMAT;
Wang Nanbec7d682015-07-01 02:13:59 +0000573 } else
574 obj->efile.symbols = data;
Wang Nana5b8bd42015-07-01 02:14:00 +0000575 } else if ((sh.sh_type == SHT_PROGBITS) &&
576 (sh.sh_flags & SHF_EXECINSTR) &&
577 (data->d_size > 0)) {
578 err = bpf_object__add_program(obj, data->d_buf,
579 data->d_size, name, idx);
580 if (err) {
Wang Nan6371ca3b2015-11-06 13:49:37 +0000581 char errmsg[STRERR_BUFSIZE];
582
Wang Nana5b8bd42015-07-01 02:14:00 +0000583 strerror_r(-err, errmsg, sizeof(errmsg));
584 pr_warning("failed to alloc program %s (%s): %s",
585 name, obj->path, errmsg);
586 }
Wang Nanb62f06e2015-07-01 02:14:01 +0000587 } else if (sh.sh_type == SHT_REL) {
588 void *reloc = obj->efile.reloc;
589 int nr_reloc = obj->efile.nr_reloc + 1;
590
591 reloc = realloc(reloc,
592 sizeof(*obj->efile.reloc) * nr_reloc);
593 if (!reloc) {
594 pr_warning("realloc failed\n");
595 err = -ENOMEM;
596 } else {
597 int n = nr_reloc - 1;
598
599 obj->efile.reloc = reloc;
600 obj->efile.nr_reloc = nr_reloc;
601
602 obj->efile.reloc[n].shdr = sh;
603 obj->efile.reloc[n].data = data;
604 }
Wang Nanbec7d682015-07-01 02:13:59 +0000605 }
Wang Nancb1e5e92015-07-01 02:13:57 +0000606 if (err)
607 goto out;
Wang Nan29603662015-07-01 02:13:56 +0000608 }
609out:
610 return err;
611}
612
Wang Nan34090912015-07-01 02:14:02 +0000613static struct bpf_program *
614bpf_object__find_prog_by_idx(struct bpf_object *obj, int idx)
615{
616 struct bpf_program *prog;
617 size_t i;
618
619 for (i = 0; i < obj->nr_programs; i++) {
620 prog = &obj->programs[i];
621 if (prog->idx == idx)
622 return prog;
623 }
624 return NULL;
625}
626
627static int
628bpf_program__collect_reloc(struct bpf_program *prog,
629 size_t nr_maps, GElf_Shdr *shdr,
630 Elf_Data *data, Elf_Data *symbols)
631{
632 int i, nrels;
633
634 pr_debug("collecting relocating info for: '%s'\n",
635 prog->section_name);
636 nrels = shdr->sh_size / shdr->sh_entsize;
637
638 prog->reloc_desc = malloc(sizeof(*prog->reloc_desc) * nrels);
639 if (!prog->reloc_desc) {
640 pr_warning("failed to alloc memory in relocation\n");
641 return -ENOMEM;
642 }
643 prog->nr_reloc = nrels;
644
645 for (i = 0; i < nrels; i++) {
646 GElf_Sym sym;
647 GElf_Rel rel;
648 unsigned int insn_idx;
649 struct bpf_insn *insns = prog->insns;
650 size_t map_idx;
651
652 if (!gelf_getrel(data, i, &rel)) {
653 pr_warning("relocation: failed to get %d reloc\n", i);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000654 return -LIBBPF_ERRNO__FORMAT;
Wang Nan34090912015-07-01 02:14:02 +0000655 }
656
657 insn_idx = rel.r_offset / sizeof(struct bpf_insn);
658 pr_debug("relocation: insn_idx=%u\n", insn_idx);
659
660 if (!gelf_getsym(symbols,
661 GELF_R_SYM(rel.r_info),
662 &sym)) {
663 pr_warning("relocation: symbol %"PRIx64" not found\n",
664 GELF_R_SYM(rel.r_info));
Wang Nan6371ca3b2015-11-06 13:49:37 +0000665 return -LIBBPF_ERRNO__FORMAT;
Wang Nan34090912015-07-01 02:14:02 +0000666 }
667
668 if (insns[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
669 pr_warning("bpf: relocation: invalid relo for insns[%d].code 0x%x\n",
670 insn_idx, insns[insn_idx].code);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000671 return -LIBBPF_ERRNO__RELOC;
Wang Nan34090912015-07-01 02:14:02 +0000672 }
673
674 map_idx = sym.st_value / sizeof(struct bpf_map_def);
675 if (map_idx >= nr_maps) {
676 pr_warning("bpf relocation: map_idx %d large than %d\n",
677 (int)map_idx, (int)nr_maps - 1);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000678 return -LIBBPF_ERRNO__RELOC;
Wang Nan34090912015-07-01 02:14:02 +0000679 }
680
681 prog->reloc_desc[i].insn_idx = insn_idx;
682 prog->reloc_desc[i].map_idx = map_idx;
683 }
684 return 0;
685}
686
Wang Nan52d33522015-07-01 02:14:04 +0000687static int
688bpf_object__create_maps(struct bpf_object *obj)
689{
690 unsigned int i;
691 size_t nr_maps;
692 int *pfd;
693
694 nr_maps = obj->maps_buf_sz / sizeof(struct bpf_map_def);
695 if (!obj->maps_buf || !nr_maps) {
696 pr_debug("don't need create maps for %s\n",
697 obj->path);
698 return 0;
699 }
700
701 obj->map_fds = malloc(sizeof(int) * nr_maps);
702 if (!obj->map_fds) {
703 pr_warning("realloc perf_bpf_map_fds failed\n");
704 return -ENOMEM;
705 }
706 obj->nr_map_fds = nr_maps;
707
708 /* fill all fd with -1 */
709 memset(obj->map_fds, -1, sizeof(int) * nr_maps);
710
711 pfd = obj->map_fds;
712 for (i = 0; i < nr_maps; i++) {
713 struct bpf_map_def def;
714
715 def = *(struct bpf_map_def *)(obj->maps_buf +
716 i * sizeof(struct bpf_map_def));
717
718 *pfd = bpf_create_map(def.type,
719 def.key_size,
720 def.value_size,
721 def.max_entries);
722 if (*pfd < 0) {
723 size_t j;
724 int err = *pfd;
725
726 pr_warning("failed to create map: %s\n",
727 strerror(errno));
728 for (j = 0; j < i; j++)
729 zclose(obj->map_fds[j]);
730 obj->nr_map_fds = 0;
731 zfree(&obj->map_fds);
732 return err;
733 }
734 pr_debug("create map: fd=%d\n", *pfd);
735 pfd++;
736 }
737
738 zfree(&obj->maps_buf);
739 obj->maps_buf_sz = 0;
740 return 0;
741}
742
Wang Nan8a47a6c2015-07-01 02:14:05 +0000743static int
744bpf_program__relocate(struct bpf_program *prog, int *map_fds)
745{
746 int i;
747
748 if (!prog || !prog->reloc_desc)
749 return 0;
750
751 for (i = 0; i < prog->nr_reloc; i++) {
752 int insn_idx, map_idx;
753 struct bpf_insn *insns = prog->insns;
754
755 insn_idx = prog->reloc_desc[i].insn_idx;
756 map_idx = prog->reloc_desc[i].map_idx;
757
758 if (insn_idx >= (int)prog->insns_cnt) {
759 pr_warning("relocation out of range: '%s'\n",
760 prog->section_name);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000761 return -LIBBPF_ERRNO__RELOC;
Wang Nan8a47a6c2015-07-01 02:14:05 +0000762 }
763 insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
764 insns[insn_idx].imm = map_fds[map_idx];
765 }
766
767 zfree(&prog->reloc_desc);
768 prog->nr_reloc = 0;
769 return 0;
770}
771
772
773static int
774bpf_object__relocate(struct bpf_object *obj)
775{
776 struct bpf_program *prog;
777 size_t i;
778 int err;
779
780 for (i = 0; i < obj->nr_programs; i++) {
781 prog = &obj->programs[i];
782
783 err = bpf_program__relocate(prog, obj->map_fds);
784 if (err) {
785 pr_warning("failed to relocate '%s'\n",
786 prog->section_name);
787 return err;
788 }
789 }
790 return 0;
791}
792
Wang Nan34090912015-07-01 02:14:02 +0000793static int bpf_object__collect_reloc(struct bpf_object *obj)
794{
795 int i, err;
796
797 if (!obj_elf_valid(obj)) {
798 pr_warning("Internal error: elf object is closed\n");
Wang Nan6371ca3b2015-11-06 13:49:37 +0000799 return -LIBBPF_ERRNO__INTERNAL;
Wang Nan34090912015-07-01 02:14:02 +0000800 }
801
802 for (i = 0; i < obj->efile.nr_reloc; i++) {
803 GElf_Shdr *shdr = &obj->efile.reloc[i].shdr;
804 Elf_Data *data = obj->efile.reloc[i].data;
805 int idx = shdr->sh_info;
806 struct bpf_program *prog;
807 size_t nr_maps = obj->maps_buf_sz /
808 sizeof(struct bpf_map_def);
809
810 if (shdr->sh_type != SHT_REL) {
811 pr_warning("internal error at %d\n", __LINE__);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000812 return -LIBBPF_ERRNO__INTERNAL;
Wang Nan34090912015-07-01 02:14:02 +0000813 }
814
815 prog = bpf_object__find_prog_by_idx(obj, idx);
816 if (!prog) {
817 pr_warning("relocation failed: no %d section\n",
818 idx);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000819 return -LIBBPF_ERRNO__RELOC;
Wang Nan34090912015-07-01 02:14:02 +0000820 }
821
822 err = bpf_program__collect_reloc(prog, nr_maps,
823 shdr, data,
824 obj->efile.symbols);
825 if (err)
Wang Nan6371ca3b2015-11-06 13:49:37 +0000826 return err;
Wang Nan34090912015-07-01 02:14:02 +0000827 }
828 return 0;
829}
830
Wang Nan55cffde2015-07-01 02:14:07 +0000831static int
832load_program(struct bpf_insn *insns, int insns_cnt,
833 char *license, u32 kern_version, int *pfd)
834{
835 int ret;
836 char *log_buf;
837
838 if (!insns || !insns_cnt)
839 return -EINVAL;
840
841 log_buf = malloc(BPF_LOG_BUF_SIZE);
842 if (!log_buf)
843 pr_warning("Alloc log buffer for bpf loader error, continue without log\n");
844
845 ret = bpf_load_program(BPF_PROG_TYPE_KPROBE, insns,
846 insns_cnt, license, kern_version,
847 log_buf, BPF_LOG_BUF_SIZE);
848
849 if (ret >= 0) {
850 *pfd = ret;
851 ret = 0;
852 goto out;
853 }
854
Wang Nan6371ca3b2015-11-06 13:49:37 +0000855 ret = -LIBBPF_ERRNO__LOAD;
Wang Nan55cffde2015-07-01 02:14:07 +0000856 pr_warning("load bpf program failed: %s\n", strerror(errno));
857
Wang Nan6371ca3b2015-11-06 13:49:37 +0000858 if (log_buf && log_buf[0] != '\0') {
859 ret = -LIBBPF_ERRNO__VERIFY;
Wang Nan55cffde2015-07-01 02:14:07 +0000860 pr_warning("-- BEGIN DUMP LOG ---\n");
861 pr_warning("\n%s\n", log_buf);
862 pr_warning("-- END LOG --\n");
Wang Nan6371ca3b2015-11-06 13:49:37 +0000863 } else {
864 if (insns_cnt >= BPF_MAXINSNS) {
865 pr_warning("Program too large (%d insns), at most %d insns\n",
866 insns_cnt, BPF_MAXINSNS);
867 ret = -LIBBPF_ERRNO__PROG2BIG;
868 } else if (log_buf) {
869 pr_warning("log buffer is empty\n");
870 ret = -LIBBPF_ERRNO__KVER;
871 }
Wang Nan55cffde2015-07-01 02:14:07 +0000872 }
873
874out:
875 free(log_buf);
876 return ret;
877}
878
879static int
880bpf_program__load(struct bpf_program *prog,
881 char *license, u32 kern_version)
882{
Wang Nanb5805632015-11-16 12:10:09 +0000883 int err = 0, fd, i;
Wang Nan55cffde2015-07-01 02:14:07 +0000884
Wang Nanb5805632015-11-16 12:10:09 +0000885 if (prog->instances.nr < 0 || !prog->instances.fds) {
886 if (prog->preprocessor) {
887 pr_warning("Internal error: can't load program '%s'\n",
888 prog->section_name);
889 return -LIBBPF_ERRNO__INTERNAL;
890 }
Wang Nan55cffde2015-07-01 02:14:07 +0000891
Wang Nanb5805632015-11-16 12:10:09 +0000892 prog->instances.fds = malloc(sizeof(int));
893 if (!prog->instances.fds) {
894 pr_warning("Not enough memory for BPF fds\n");
895 return -ENOMEM;
896 }
897 prog->instances.nr = 1;
898 prog->instances.fds[0] = -1;
899 }
900
901 if (!prog->preprocessor) {
902 if (prog->instances.nr != 1) {
903 pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n",
904 prog->section_name, prog->instances.nr);
905 }
906 err = load_program(prog->insns, prog->insns_cnt,
907 license, kern_version, &fd);
908 if (!err)
909 prog->instances.fds[0] = fd;
910 goto out;
911 }
912
913 for (i = 0; i < prog->instances.nr; i++) {
914 struct bpf_prog_prep_result result;
915 bpf_program_prep_t preprocessor = prog->preprocessor;
916
917 bzero(&result, sizeof(result));
918 err = preprocessor(prog, i, prog->insns,
919 prog->insns_cnt, &result);
920 if (err) {
921 pr_warning("Preprocessing the %dth instance of program '%s' failed\n",
922 i, prog->section_name);
923 goto out;
924 }
925
926 if (!result.new_insn_ptr || !result.new_insn_cnt) {
927 pr_debug("Skip loading the %dth instance of program '%s'\n",
928 i, prog->section_name);
929 prog->instances.fds[i] = -1;
930 if (result.pfd)
931 *result.pfd = -1;
932 continue;
933 }
934
935 err = load_program(result.new_insn_ptr,
936 result.new_insn_cnt,
937 license, kern_version, &fd);
938
939 if (err) {
940 pr_warning("Loading the %dth instance of program '%s' failed\n",
941 i, prog->section_name);
942 goto out;
943 }
944
945 if (result.pfd)
946 *result.pfd = fd;
947 prog->instances.fds[i] = fd;
948 }
949out:
Wang Nan55cffde2015-07-01 02:14:07 +0000950 if (err)
951 pr_warning("failed to load program '%s'\n",
952 prog->section_name);
953 zfree(&prog->insns);
954 prog->insns_cnt = 0;
955 return err;
956}
957
958static int
959bpf_object__load_progs(struct bpf_object *obj)
960{
961 size_t i;
962 int err;
963
964 for (i = 0; i < obj->nr_programs; i++) {
965 err = bpf_program__load(&obj->programs[i],
966 obj->license,
967 obj->kern_version);
968 if (err)
969 return err;
970 }
971 return 0;
972}
973
Wang Nancb1e5e92015-07-01 02:13:57 +0000974static int bpf_object__validate(struct bpf_object *obj)
975{
976 if (obj->kern_version == 0) {
977 pr_warning("%s doesn't provide kernel version\n",
978 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000979 return -LIBBPF_ERRNO__KVERSION;
Wang Nancb1e5e92015-07-01 02:13:57 +0000980 }
981 return 0;
982}
983
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000984static struct bpf_object *
Wang Nan6c956392015-07-01 02:13:54 +0000985__bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz)
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000986{
987 struct bpf_object *obj;
Wang Nan6371ca3b2015-11-06 13:49:37 +0000988 int err;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000989
990 if (elf_version(EV_CURRENT) == EV_NONE) {
991 pr_warning("failed to init libelf for %s\n", path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000992 return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000993 }
994
Wang Nan6c956392015-07-01 02:13:54 +0000995 obj = bpf_object__new(path, obj_buf, obj_buf_sz);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000996 if (IS_ERR(obj))
997 return obj;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000998
Wang Nan6371ca3b2015-11-06 13:49:37 +0000999 CHECK_ERR(bpf_object__elf_init(obj), err, out);
1000 CHECK_ERR(bpf_object__check_endianness(obj), err, out);
1001 CHECK_ERR(bpf_object__elf_collect(obj), err, out);
1002 CHECK_ERR(bpf_object__collect_reloc(obj), err, out);
1003 CHECK_ERR(bpf_object__validate(obj), err, out);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001004
1005 bpf_object__elf_finish(obj);
1006 return obj;
1007out:
1008 bpf_object__close(obj);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001009 return ERR_PTR(err);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001010}
1011
1012struct bpf_object *bpf_object__open(const char *path)
1013{
1014 /* param validation */
1015 if (!path)
1016 return NULL;
1017
1018 pr_debug("loading %s\n", path);
1019
Wang Nan6c956392015-07-01 02:13:54 +00001020 return __bpf_object__open(path, NULL, 0);
1021}
1022
1023struct bpf_object *bpf_object__open_buffer(void *obj_buf,
Wang Nanacf860a2015-08-27 02:30:55 +00001024 size_t obj_buf_sz,
1025 const char *name)
Wang Nan6c956392015-07-01 02:13:54 +00001026{
Wang Nanacf860a2015-08-27 02:30:55 +00001027 char tmp_name[64];
1028
Wang Nan6c956392015-07-01 02:13:54 +00001029 /* param validation */
1030 if (!obj_buf || obj_buf_sz <= 0)
1031 return NULL;
1032
Wang Nanacf860a2015-08-27 02:30:55 +00001033 if (!name) {
1034 snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx",
1035 (unsigned long)obj_buf,
1036 (unsigned long)obj_buf_sz);
1037 tmp_name[sizeof(tmp_name) - 1] = '\0';
1038 name = tmp_name;
1039 }
1040 pr_debug("loading object '%s' from buffer\n",
1041 name);
Wang Nan6c956392015-07-01 02:13:54 +00001042
Wang Nanacf860a2015-08-27 02:30:55 +00001043 return __bpf_object__open(name, obj_buf, obj_buf_sz);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001044}
1045
Wang Nan52d33522015-07-01 02:14:04 +00001046int bpf_object__unload(struct bpf_object *obj)
1047{
1048 size_t i;
1049
1050 if (!obj)
1051 return -EINVAL;
1052
1053 for (i = 0; i < obj->nr_map_fds; i++)
1054 zclose(obj->map_fds[i]);
1055 zfree(&obj->map_fds);
1056 obj->nr_map_fds = 0;
1057
Wang Nan55cffde2015-07-01 02:14:07 +00001058 for (i = 0; i < obj->nr_programs; i++)
1059 bpf_program__unload(&obj->programs[i]);
1060
Wang Nan52d33522015-07-01 02:14:04 +00001061 return 0;
1062}
1063
1064int bpf_object__load(struct bpf_object *obj)
1065{
Wang Nan6371ca3b2015-11-06 13:49:37 +00001066 int err;
1067
Wang Nan52d33522015-07-01 02:14:04 +00001068 if (!obj)
1069 return -EINVAL;
1070
1071 if (obj->loaded) {
1072 pr_warning("object should not be loaded twice\n");
1073 return -EINVAL;
1074 }
1075
1076 obj->loaded = true;
Wang Nan6371ca3b2015-11-06 13:49:37 +00001077
1078 CHECK_ERR(bpf_object__create_maps(obj), err, out);
1079 CHECK_ERR(bpf_object__relocate(obj), err, out);
1080 CHECK_ERR(bpf_object__load_progs(obj), err, out);
Wang Nan52d33522015-07-01 02:14:04 +00001081
1082 return 0;
1083out:
1084 bpf_object__unload(obj);
1085 pr_warning("failed to load object '%s'\n", obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001086 return err;
Wang Nan52d33522015-07-01 02:14:04 +00001087}
1088
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001089void bpf_object__close(struct bpf_object *obj)
1090{
Wang Nana5b8bd42015-07-01 02:14:00 +00001091 size_t i;
1092
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001093 if (!obj)
1094 return;
1095
1096 bpf_object__elf_finish(obj);
Wang Nan52d33522015-07-01 02:14:04 +00001097 bpf_object__unload(obj);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001098
Wang Nan0b3d1ef2015-07-01 02:13:58 +00001099 zfree(&obj->maps_buf);
Wang Nana5b8bd42015-07-01 02:14:00 +00001100
1101 if (obj->programs && obj->nr_programs) {
1102 for (i = 0; i < obj->nr_programs; i++)
1103 bpf_program__exit(&obj->programs[i]);
1104 }
1105 zfree(&obj->programs);
1106
Wang Nan9a208ef2015-07-01 02:14:10 +00001107 list_del(&obj->list);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001108 free(obj);
1109}
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001110
Wang Nan9a208ef2015-07-01 02:14:10 +00001111struct bpf_object *
1112bpf_object__next(struct bpf_object *prev)
1113{
1114 struct bpf_object *next;
1115
1116 if (!prev)
1117 next = list_first_entry(&bpf_objects_list,
1118 struct bpf_object,
1119 list);
1120 else
1121 next = list_next_entry(prev, list);
1122
1123 /* Empty list is noticed here so don't need checking on entry. */
1124 if (&next->list == &bpf_objects_list)
1125 return NULL;
1126
1127 return next;
1128}
1129
Wang Nanacf860a2015-08-27 02:30:55 +00001130const char *
1131bpf_object__get_name(struct bpf_object *obj)
1132{
1133 if (!obj)
Wang Nan6371ca3b2015-11-06 13:49:37 +00001134 return ERR_PTR(-EINVAL);
Wang Nanacf860a2015-08-27 02:30:55 +00001135 return obj->path;
1136}
1137
Wang Nan45825d82015-11-06 13:49:38 +00001138unsigned int
1139bpf_object__get_kversion(struct bpf_object *obj)
1140{
1141 if (!obj)
1142 return 0;
1143 return obj->kern_version;
1144}
1145
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001146struct bpf_program *
1147bpf_program__next(struct bpf_program *prev, struct bpf_object *obj)
1148{
1149 size_t idx;
1150
1151 if (!obj->programs)
1152 return NULL;
1153 /* First handler */
1154 if (prev == NULL)
1155 return &obj->programs[0];
1156
1157 if (prev->obj != obj) {
1158 pr_warning("error: program handler doesn't match object\n");
1159 return NULL;
1160 }
1161
1162 idx = (prev - obj->programs) + 1;
1163 if (idx >= obj->nr_programs)
1164 return NULL;
1165 return &obj->programs[idx];
1166}
1167
1168int bpf_program__set_private(struct bpf_program *prog,
1169 void *priv,
1170 bpf_program_clear_priv_t clear_priv)
1171{
1172 if (prog->priv && prog->clear_priv)
1173 prog->clear_priv(prog, prog->priv);
1174
1175 prog->priv = priv;
1176 prog->clear_priv = clear_priv;
1177 return 0;
1178}
1179
1180int bpf_program__get_private(struct bpf_program *prog, void **ppriv)
1181{
1182 *ppriv = prog->priv;
1183 return 0;
1184}
1185
Namhyung Kim715f8db2015-11-03 20:21:05 +09001186const char *bpf_program__title(struct bpf_program *prog, bool needs_copy)
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001187{
1188 const char *title;
1189
1190 title = prog->section_name;
Namhyung Kim715f8db2015-11-03 20:21:05 +09001191 if (needs_copy) {
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001192 title = strdup(title);
1193 if (!title) {
1194 pr_warning("failed to strdup program title\n");
Wang Nan6371ca3b2015-11-06 13:49:37 +00001195 return ERR_PTR(-ENOMEM);
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001196 }
1197 }
1198
1199 return title;
1200}
1201
1202int bpf_program__fd(struct bpf_program *prog)
1203{
Wang Nanb5805632015-11-16 12:10:09 +00001204 return bpf_program__nth_fd(prog, 0);
1205}
1206
1207int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,
1208 bpf_program_prep_t prep)
1209{
1210 int *instances_fds;
1211
1212 if (nr_instances <= 0 || !prep)
1213 return -EINVAL;
1214
1215 if (prog->instances.nr > 0 || prog->instances.fds) {
1216 pr_warning("Can't set pre-processor after loading\n");
1217 return -EINVAL;
1218 }
1219
1220 instances_fds = malloc(sizeof(int) * nr_instances);
1221 if (!instances_fds) {
1222 pr_warning("alloc memory failed for fds\n");
1223 return -ENOMEM;
1224 }
1225
1226 /* fill all fd with -1 */
1227 memset(instances_fds, -1, sizeof(int) * nr_instances);
1228
1229 prog->instances.nr = nr_instances;
1230 prog->instances.fds = instances_fds;
1231 prog->preprocessor = prep;
1232 return 0;
1233}
1234
1235int bpf_program__nth_fd(struct bpf_program *prog, int n)
1236{
1237 int fd;
1238
1239 if (n >= prog->instances.nr || n < 0) {
1240 pr_warning("Can't get the %dth fd from program %s: only %d instances\n",
1241 n, prog->section_name, prog->instances.nr);
1242 return -EINVAL;
1243 }
1244
1245 fd = prog->instances.fds[n];
1246 if (fd < 0) {
1247 pr_warning("%dth instance of program '%s' is invalid\n",
1248 n, prog->section_name);
1249 return -ENOENT;
1250 }
1251
1252 return fd;
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001253}