blob: c9a9aef2806cbfbef033668920ab8cae02ca2e8d [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#ifndef __BPF_LIBBPF_H
9#define __BPF_LIBBPF_H
10
Wang Nan1a5e3fb2015-07-01 02:13:53 +000011#include <stdio.h>
Wang Nanaa9b1ac2015-07-01 02:14:08 +000012#include <stdbool.h>
Wang Nan6371ca3b2015-11-06 13:49:37 +000013#include <linux/err.h>
14
15enum libbpf_errno {
16 __LIBBPF_ERRNO__START = 4000,
17
18 /* Something wrong in libelf */
19 LIBBPF_ERRNO__LIBELF = __LIBBPF_ERRNO__START,
20 LIBBPF_ERRNO__FORMAT, /* BPF object format invalid */
21 LIBBPF_ERRNO__KVERSION, /* Incorrect or no 'version' section */
22 LIBBPF_ERRNO__ENDIAN, /* Endian missmatch */
23 LIBBPF_ERRNO__INTERNAL, /* Internal error in libbpf */
24 LIBBPF_ERRNO__RELOC, /* Relocation failed */
25 LIBBPF_ERRNO__LOAD, /* Load program failure for unknown reason */
26 LIBBPF_ERRNO__VERIFY, /* Kernel verifier blocks program loading */
27 LIBBPF_ERRNO__PROG2BIG, /* Program too big */
28 LIBBPF_ERRNO__KVER, /* Incorrect kernel version */
29 __LIBBPF_ERRNO__END,
30};
31
32int libbpf_strerror(int err, char *buf, size_t size);
Wang Nan1a5e3fb2015-07-01 02:13:53 +000033
Wang Nanb3f59d62015-07-01 02:13:52 +000034/*
35 * In include/linux/compiler-gcc.h, __printf is defined. However
36 * it should be better if libbpf.h doesn't depend on Linux header file.
37 * So instead of __printf, here we use gcc attribute directly.
38 */
39typedef int (*libbpf_print_fn_t)(const char *, ...)
40 __attribute__((format(printf, 1, 2)));
41
42void libbpf_set_print(libbpf_print_fn_t warn,
43 libbpf_print_fn_t info,
44 libbpf_print_fn_t debug);
45
Wang Nan1a5e3fb2015-07-01 02:13:53 +000046/* Hide internal to user */
47struct bpf_object;
48
49struct bpf_object *bpf_object__open(const char *path);
Wang Nan6c956392015-07-01 02:13:54 +000050struct bpf_object *bpf_object__open_buffer(void *obj_buf,
Wang Nanacf860a2015-08-27 02:30:55 +000051 size_t obj_buf_sz,
52 const char *name);
Wang Nan1a5e3fb2015-07-01 02:13:53 +000053void bpf_object__close(struct bpf_object *object);
54
Wang Nan52d33522015-07-01 02:14:04 +000055/* Load/unload object into/from kernel */
56int bpf_object__load(struct bpf_object *obj);
57int bpf_object__unload(struct bpf_object *obj);
Wang Nanacf860a2015-08-27 02:30:55 +000058const char *bpf_object__get_name(struct bpf_object *obj);
Wang Nan45825d82015-11-06 13:49:38 +000059unsigned int bpf_object__get_kversion(struct bpf_object *obj);
Wang Nan52d33522015-07-01 02:14:04 +000060
Wang Nan9a208ef2015-07-01 02:14:10 +000061struct bpf_object *bpf_object__next(struct bpf_object *prev);
62#define bpf_object__for_each_safe(pos, tmp) \
63 for ((pos) = bpf_object__next(NULL), \
64 (tmp) = bpf_object__next(pos); \
65 (pos) != NULL; \
66 (pos) = (tmp), (tmp) = bpf_object__next(tmp))
67
Wang Nanaa9b1ac2015-07-01 02:14:08 +000068/* Accessors of bpf_program. */
69struct bpf_program;
70struct bpf_program *bpf_program__next(struct bpf_program *prog,
71 struct bpf_object *obj);
72
73#define bpf_object__for_each_program(pos, obj) \
74 for ((pos) = bpf_program__next(NULL, (obj)); \
75 (pos) != NULL; \
76 (pos) = bpf_program__next((pos), (obj)))
77
78typedef void (*bpf_program_clear_priv_t)(struct bpf_program *,
79 void *);
80
81int bpf_program__set_private(struct bpf_program *prog, void *priv,
82 bpf_program_clear_priv_t clear_priv);
83
84int bpf_program__get_private(struct bpf_program *prog,
85 void **ppriv);
86
Namhyung Kim715f8db2015-11-03 20:21:05 +090087const char *bpf_program__title(struct bpf_program *prog, bool needs_copy);
Wang Nanaa9b1ac2015-07-01 02:14:08 +000088
89int bpf_program__fd(struct bpf_program *prog);
90
Wang Nan34090912015-07-01 02:14:02 +000091/*
92 * We don't need __attribute__((packed)) now since it is
93 * unnecessary for 'bpf_map_def' because they are all aligned.
94 * In addition, using it will trigger -Wpacked warning message,
95 * and will be treated as an error due to -Werror.
96 */
97struct bpf_map_def {
98 unsigned int type;
99 unsigned int key_size;
100 unsigned int value_size;
101 unsigned int max_entries;
102};
103
Wang Nan1b76c132015-07-01 02:13:51 +0000104#endif