2 * Common eBPF ELF object loading operations.
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.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation;
11 * version 2.1 of the License (not later!)
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this program; if not, see <http://www.gnu.org/licenses>
21 #ifndef __BPF_LIBBPF_H
22 #define __BPF_LIBBPF_H
26 #include <linux/err.h>
27 #include <sys/types.h> // for size_t
30 __LIBBPF_ERRNO__START
= 4000,
32 /* Something wrong in libelf */
33 LIBBPF_ERRNO__LIBELF
= __LIBBPF_ERRNO__START
,
34 LIBBPF_ERRNO__FORMAT
, /* BPF object format invalid */
35 LIBBPF_ERRNO__KVERSION
, /* Incorrect or no 'version' section */
36 LIBBPF_ERRNO__ENDIAN
, /* Endian mismatch */
37 LIBBPF_ERRNO__INTERNAL
, /* Internal error in libbpf */
38 LIBBPF_ERRNO__RELOC
, /* Relocation failed */
39 LIBBPF_ERRNO__LOAD
, /* Load program failure for unknown reason */
40 LIBBPF_ERRNO__VERIFY
, /* Kernel verifier blocks program loading */
41 LIBBPF_ERRNO__PROG2BIG
, /* Program too big */
42 LIBBPF_ERRNO__KVER
, /* Incorrect kernel version */
43 LIBBPF_ERRNO__PROGTYPE
, /* Kernel doesn't support this program type */
47 int libbpf_strerror(int err
, char *buf
, size_t size
);
50 * In include/linux/compiler-gcc.h, __printf is defined. However
51 * it should be better if libbpf.h doesn't depend on Linux header file.
52 * So instead of __printf, here we use gcc attribute directly.
54 typedef int (*libbpf_print_fn_t
)(const char *, ...)
55 __attribute__((format(printf
, 1, 2)));
57 void libbpf_set_print(libbpf_print_fn_t warn
,
58 libbpf_print_fn_t info
,
59 libbpf_print_fn_t debug
);
61 /* Hide internal to user */
64 struct bpf_object
*bpf_object__open(const char *path
);
65 struct bpf_object
*bpf_object__open_buffer(void *obj_buf
,
68 void bpf_object__close(struct bpf_object
*object
);
70 /* Load/unload object into/from kernel */
71 int bpf_object__load(struct bpf_object
*obj
);
72 int bpf_object__unload(struct bpf_object
*obj
);
73 const char *bpf_object__name(struct bpf_object
*obj
);
74 unsigned int bpf_object__kversion(struct bpf_object
*obj
);
76 struct bpf_object
*bpf_object__next(struct bpf_object
*prev
);
77 #define bpf_object__for_each_safe(pos, tmp) \
78 for ((pos) = bpf_object__next(NULL), \
79 (tmp) = bpf_object__next(pos); \
81 (pos) = (tmp), (tmp) = bpf_object__next(tmp))
83 typedef void (*bpf_object_clear_priv_t
)(struct bpf_object
*, void *);
84 int bpf_object__set_priv(struct bpf_object
*obj
, void *priv
,
85 bpf_object_clear_priv_t clear_priv
);
86 void *bpf_object__priv(struct bpf_object
*prog
);
88 /* Accessors of bpf_program. */
90 struct bpf_program
*bpf_program__next(struct bpf_program
*prog
,
91 struct bpf_object
*obj
);
93 #define bpf_object__for_each_program(pos, obj) \
94 for ((pos) = bpf_program__next(NULL, (obj)); \
96 (pos) = bpf_program__next((pos), (obj)))
98 typedef void (*bpf_program_clear_priv_t
)(struct bpf_program
*,
101 int bpf_program__set_priv(struct bpf_program
*prog
, void *priv
,
102 bpf_program_clear_priv_t clear_priv
);
104 void *bpf_program__priv(struct bpf_program
*prog
);
106 const char *bpf_program__title(struct bpf_program
*prog
, bool needs_copy
);
108 int bpf_program__fd(struct bpf_program
*prog
);
113 * Libbpf allows callers to adjust BPF programs before being loaded
114 * into kernel. One program in an object file can be transform into
115 * multiple variants to be attached to different code.
117 * bpf_program_prep_t, bpf_program__set_prep and bpf_program__nth_fd
118 * are APIs for this propose.
120 * - bpf_program_prep_t:
121 * It defines 'preprocessor', which is a caller defined function
122 * passed to libbpf through bpf_program__set_prep(), and will be
123 * called before program is loaded. The processor should adjust
124 * the program one time for each instances according to the number
127 * - bpf_program__set_prep:
128 * Attachs a preprocessor to a BPF program. The number of instances
129 * whould be created is also passed through this function.
131 * - bpf_program__nth_fd:
132 * After the program is loaded, get resuling fds from bpf program for
135 * If bpf_program__set_prep() is not used, the program whould be loaded
136 * without adjustment during bpf_object__load(). The program has only
137 * one instance. In this case bpf_program__fd(prog) is equal to
138 * bpf_program__nth_fd(prog, 0).
141 struct bpf_prog_prep_result
{
143 * If not NULL, load new instruction array.
144 * If set to NULL, don't load this instance.
146 struct bpf_insn
*new_insn_ptr
;
149 /* If not NULL, result fd is set to it */
154 * Parameters of bpf_program_prep_t:
155 * - prog: The bpf_program being loaded.
156 * - n: Index of instance being generated.
157 * - insns: BPF instructions array.
158 * - insns_cnt:Number of instructions in insns.
159 * - res: Output parameter, result of transformation.
162 * - Zero: pre-processing success.
163 * - Non-zero: pre-processing, stop loading.
165 typedef int (*bpf_program_prep_t
)(struct bpf_program
*prog
, int n
,
166 struct bpf_insn
*insns
, int insns_cnt
,
167 struct bpf_prog_prep_result
*res
);
169 int bpf_program__set_prep(struct bpf_program
*prog
, int nr_instance
,
170 bpf_program_prep_t prep
);
172 int bpf_program__nth_fd(struct bpf_program
*prog
, int n
);
175 * Adjust type of bpf program. Default is kprobe.
177 int bpf_program__set_tracepoint(struct bpf_program
*prog
);
178 int bpf_program__set_kprobe(struct bpf_program
*prog
);
180 bool bpf_program__is_tracepoint(struct bpf_program
*prog
);
181 bool bpf_program__is_kprobe(struct bpf_program
*prog
);
184 * We don't need __attribute__((packed)) now since it is
185 * unnecessary for 'bpf_map_def' because they are all aligned.
186 * In addition, using it will trigger -Wpacked warning message,
187 * and will be treated as an error due to -Werror.
191 unsigned int key_size
;
192 unsigned int value_size
;
193 unsigned int max_entries
;
197 * There is another 'struct bpf_map' in include/linux/map.h. However,
198 * it is not a uapi header so no need to consider name clash.
202 bpf_object__find_map_by_name(struct bpf_object
*obj
, const char *name
);
205 * Get bpf_map through the offset of corresponding struct bpf_map_def
206 * in the bpf object file.
209 bpf_object__find_map_by_offset(struct bpf_object
*obj
, size_t offset
);
212 bpf_map__next(struct bpf_map
*map
, struct bpf_object
*obj
);
213 #define bpf_map__for_each(pos, obj) \
214 for ((pos) = bpf_map__next(NULL, (obj)); \
216 (pos) = bpf_map__next((pos), (obj)))
218 int bpf_map__fd(struct bpf_map
*map
);
219 const struct bpf_map_def
*bpf_map__def(struct bpf_map
*map
);
220 const char *bpf_map__name(struct bpf_map
*map
);
222 typedef void (*bpf_map_clear_priv_t
)(struct bpf_map
*, void *);
223 int bpf_map__set_priv(struct bpf_map
*map
, void *priv
,
224 bpf_map_clear_priv_t clear_priv
);
225 void *bpf_map__priv(struct bpf_map
*map
);