blob: 1f3468dad8b2c300c3e8eeda9e94ca6f973d0faa [file] [log] [blame]
Alexei Starovoitov1bc38b82018-10-05 16:40:00 -07001/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
Eric Leblond6061a3d2018-01-30 21:55:03 +01002
Wang Nan1b76c132015-07-01 02:13:51 +00003/*
4 * Common eBPF ELF object loading operations.
5 *
6 * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
7 * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
8 * Copyright (C) 2015 Huawei Inc.
9 */
Andrey Ignatoveff81902018-10-03 15:26:42 -070010#ifndef __LIBBPF_LIBBPF_H
11#define __LIBBPF_LIBBPF_H
Wang Nan1b76c132015-07-01 02:13:51 +000012
Wang Nan1a5e3fb2015-07-01 02:13:53 +000013#include <stdio.h>
Joe Stringere28ff1a2017-01-22 17:11:25 -080014#include <stdint.h>
Wang Nanaa9b1ac2015-07-01 02:14:08 +000015#include <stdbool.h>
Wang Nan5a6acad2016-11-26 07:03:27 +000016#include <sys/types.h> // for size_t
Alexei Starovoitovdd26b7f2017-03-30 21:45:40 -070017#include <linux/bpf.h>
Wang Nan6371ca3b2015-11-06 13:49:37 +000018
Andrey Ignatovab9e0842018-10-15 22:50:34 -070019#ifndef LIBBPF_API
20#define LIBBPF_API __attribute__((visibility("default")))
21#endif
22
Wang Nan6371ca3b2015-11-06 13:49:37 +000023enum libbpf_errno {
24 __LIBBPF_ERRNO__START = 4000,
25
26 /* Something wrong in libelf */
27 LIBBPF_ERRNO__LIBELF = __LIBBPF_ERRNO__START,
28 LIBBPF_ERRNO__FORMAT, /* BPF object format invalid */
29 LIBBPF_ERRNO__KVERSION, /* Incorrect or no 'version' section */
Colin Ian Kingde8a63b2016-06-28 13:23:37 +010030 LIBBPF_ERRNO__ENDIAN, /* Endian mismatch */
Wang Nan6371ca3b2015-11-06 13:49:37 +000031 LIBBPF_ERRNO__INTERNAL, /* Internal error in libbpf */
32 LIBBPF_ERRNO__RELOC, /* Relocation failed */
33 LIBBPF_ERRNO__LOAD, /* Load program failure for unknown reason */
34 LIBBPF_ERRNO__VERIFY, /* Kernel verifier blocks program loading */
35 LIBBPF_ERRNO__PROG2BIG, /* Program too big */
36 LIBBPF_ERRNO__KVER, /* Incorrect kernel version */
Wang Nan705fa212016-07-13 10:44:02 +000037 LIBBPF_ERRNO__PROGTYPE, /* Kernel doesn't support this program type */
Eric Leblond949abbe2018-01-30 21:55:01 +010038 LIBBPF_ERRNO__WRNGPID, /* Wrong pid in netlink message */
39 LIBBPF_ERRNO__INVSEQ, /* Invalid netlink sequence */
Yonghong Song36f16782018-09-05 16:58:05 -070040 LIBBPF_ERRNO__NLPARSE, /* netlink parsing error */
Wang Nan6371ca3b2015-11-06 13:49:37 +000041 __LIBBPF_ERRNO__END,
42};
43
Andrey Ignatovab9e0842018-10-15 22:50:34 -070044LIBBPF_API int libbpf_strerror(int err, char *buf, size_t size);
Wang Nan1a5e3fb2015-07-01 02:13:53 +000045
Wang Nanb3f59d62015-07-01 02:13:52 +000046/*
Jakub Kicinski2eb57bb2018-05-10 10:24:41 -070047 * __printf is defined in include/linux/compiler-gcc.h. However,
48 * it would be better if libbpf.h didn't depend on Linux header files.
Wang Nanb3f59d62015-07-01 02:13:52 +000049 * So instead of __printf, here we use gcc attribute directly.
50 */
51typedef int (*libbpf_print_fn_t)(const char *, ...)
52 __attribute__((format(printf, 1, 2)));
53
Andrey Ignatovab9e0842018-10-15 22:50:34 -070054LIBBPF_API void libbpf_set_print(libbpf_print_fn_t warn,
55 libbpf_print_fn_t info,
56 libbpf_print_fn_t debug);
Wang Nanb3f59d62015-07-01 02:13:52 +000057
Wang Nan1a5e3fb2015-07-01 02:13:53 +000058/* Hide internal to user */
59struct bpf_object;
60
Jakub Kicinski07f2d4e2018-07-10 14:43:02 -070061struct bpf_object_open_attr {
62 const char *file;
63 enum bpf_prog_type prog_type;
64};
65
Andrey Ignatovab9e0842018-10-15 22:50:34 -070066LIBBPF_API struct bpf_object *bpf_object__open(const char *path);
67LIBBPF_API struct bpf_object *
68bpf_object__open_xattr(struct bpf_object_open_attr *attr);
John Fastabendc034a172018-10-15 11:19:55 -070069struct bpf_object *__bpf_object__open_xattr(struct bpf_object_open_attr *attr,
70 int flags);
Andrey Ignatovab9e0842018-10-15 22:50:34 -070071LIBBPF_API struct bpf_object *bpf_object__open_buffer(void *obj_buf,
72 size_t obj_buf_sz,
73 const char *name);
74LIBBPF_API int bpf_object__pin(struct bpf_object *object, const char *path);
75LIBBPF_API void bpf_object__close(struct bpf_object *object);
Wang Nan1a5e3fb2015-07-01 02:13:53 +000076
Wang Nan52d33522015-07-01 02:14:04 +000077/* Load/unload object into/from kernel */
Andrey Ignatovab9e0842018-10-15 22:50:34 -070078LIBBPF_API int bpf_object__load(struct bpf_object *obj);
79LIBBPF_API int bpf_object__unload(struct bpf_object *obj);
80LIBBPF_API const char *bpf_object__name(struct bpf_object *obj);
81LIBBPF_API unsigned int bpf_object__kversion(struct bpf_object *obj);
82LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj);
Wang Nan52d33522015-07-01 02:14:04 +000083
Andrey Ignatovab9e0842018-10-15 22:50:34 -070084LIBBPF_API struct bpf_program *
Jakub Kicinski6d4b1982018-07-26 14:32:19 -070085bpf_object__find_program_by_title(struct bpf_object *obj, const char *title);
86
Andrey Ignatovab9e0842018-10-15 22:50:34 -070087LIBBPF_API struct bpf_object *bpf_object__next(struct bpf_object *prev);
Wang Nan9a208ef2015-07-01 02:14:10 +000088#define bpf_object__for_each_safe(pos, tmp) \
89 for ((pos) = bpf_object__next(NULL), \
90 (tmp) = bpf_object__next(pos); \
91 (pos) != NULL; \
92 (pos) = (tmp), (tmp) = bpf_object__next(tmp))
93
Wang Nan10931d22016-11-26 07:03:26 +000094typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *);
Andrey Ignatovab9e0842018-10-15 22:50:34 -070095LIBBPF_API int bpf_object__set_priv(struct bpf_object *obj, void *priv,
96 bpf_object_clear_priv_t clear_priv);
97LIBBPF_API void *bpf_object__priv(struct bpf_object *prog);
Wang Nan10931d22016-11-26 07:03:26 +000098
Andrey Ignatovab9e0842018-10-15 22:50:34 -070099LIBBPF_API int
100libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
101 enum bpf_attach_type *expected_attach_type);
102LIBBPF_API int libbpf_attach_type_by_name(const char *name,
103 enum bpf_attach_type *attach_type);
Jakub Kicinskib60df2a2018-07-10 14:42:59 -0700104
Jakub Kicinski2eb57bb2018-05-10 10:24:41 -0700105/* Accessors of bpf_program */
Wang Nanaa9b1ac2015-07-01 02:14:08 +0000106struct bpf_program;
Andrey Ignatovab9e0842018-10-15 22:50:34 -0700107LIBBPF_API struct bpf_program *bpf_program__next(struct bpf_program *prog,
108 struct bpf_object *obj);
Wang Nanaa9b1ac2015-07-01 02:14:08 +0000109
110#define bpf_object__for_each_program(pos, obj) \
111 for ((pos) = bpf_program__next(NULL, (obj)); \
112 (pos) != NULL; \
113 (pos) = bpf_program__next((pos), (obj)))
114
115typedef void (*bpf_program_clear_priv_t)(struct bpf_program *,
116 void *);
117
Andrey Ignatovab9e0842018-10-15 22:50:34 -0700118LIBBPF_API int bpf_program__set_priv(struct bpf_program *prog, void *priv,
119 bpf_program_clear_priv_t clear_priv);
Wang Nanaa9b1ac2015-07-01 02:14:08 +0000120
Andrey Ignatovab9e0842018-10-15 22:50:34 -0700121LIBBPF_API void *bpf_program__priv(struct bpf_program *prog);
122LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,
123 __u32 ifindex);
Wang Nanaa9b1ac2015-07-01 02:14:08 +0000124
Andrey Ignatovab9e0842018-10-15 22:50:34 -0700125LIBBPF_API const char *bpf_program__title(struct bpf_program *prog,
126 bool needs_copy);
Wang Nanaa9b1ac2015-07-01 02:14:08 +0000127
Andrey Ignatovab9e0842018-10-15 22:50:34 -0700128LIBBPF_API int bpf_program__load(struct bpf_program *prog, char *license,
129 __u32 kern_version);
130LIBBPF_API int bpf_program__fd(struct bpf_program *prog);
131LIBBPF_API int bpf_program__pin_instance(struct bpf_program *prog,
132 const char *path,
133 int instance);
134LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path);
135LIBBPF_API void bpf_program__unload(struct bpf_program *prog);
Wang Nanaa9b1ac2015-07-01 02:14:08 +0000136
Wang Nanb5805632015-11-16 12:10:09 +0000137struct bpf_insn;
138
139/*
140 * Libbpf allows callers to adjust BPF programs before being loaded
Jakub Kicinski2eb57bb2018-05-10 10:24:41 -0700141 * into kernel. One program in an object file can be transformed into
142 * multiple variants to be attached to different hooks.
Wang Nanb5805632015-11-16 12:10:09 +0000143 *
144 * bpf_program_prep_t, bpf_program__set_prep and bpf_program__nth_fd
Jakub Kicinski2eb57bb2018-05-10 10:24:41 -0700145 * form an API for this purpose.
Wang Nanb5805632015-11-16 12:10:09 +0000146 *
147 * - bpf_program_prep_t:
Jakub Kicinski2eb57bb2018-05-10 10:24:41 -0700148 * Defines a 'preprocessor', which is a caller defined function
Wang Nanb5805632015-11-16 12:10:09 +0000149 * passed to libbpf through bpf_program__set_prep(), and will be
150 * called before program is loaded. The processor should adjust
Jakub Kicinski2eb57bb2018-05-10 10:24:41 -0700151 * the program one time for each instance according to the instance id
Wang Nanb5805632015-11-16 12:10:09 +0000152 * passed to it.
153 *
154 * - bpf_program__set_prep:
Jakub Kicinski2eb57bb2018-05-10 10:24:41 -0700155 * Attaches a preprocessor to a BPF program. The number of instances
156 * that should be created is also passed through this function.
Wang Nanb5805632015-11-16 12:10:09 +0000157 *
158 * - bpf_program__nth_fd:
Jakub Kicinski2eb57bb2018-05-10 10:24:41 -0700159 * After the program is loaded, get resulting FD of a given instance
160 * of the BPF program.
Wang Nanb5805632015-11-16 12:10:09 +0000161 *
Jakub Kicinski2eb57bb2018-05-10 10:24:41 -0700162 * If bpf_program__set_prep() is not used, the program would be loaded
Wang Nanb5805632015-11-16 12:10:09 +0000163 * without adjustment during bpf_object__load(). The program has only
164 * one instance. In this case bpf_program__fd(prog) is equal to
165 * bpf_program__nth_fd(prog, 0).
166 */
167
168struct bpf_prog_prep_result {
169 /*
170 * If not NULL, load new instruction array.
171 * If set to NULL, don't load this instance.
172 */
173 struct bpf_insn *new_insn_ptr;
174 int new_insn_cnt;
175
Jakub Kicinski2eb57bb2018-05-10 10:24:41 -0700176 /* If not NULL, result FD is written to it. */
Wang Nanb5805632015-11-16 12:10:09 +0000177 int *pfd;
178};
179
180/*
181 * Parameters of bpf_program_prep_t:
182 * - prog: The bpf_program being loaded.
183 * - n: Index of instance being generated.
184 * - insns: BPF instructions array.
185 * - insns_cnt:Number of instructions in insns.
186 * - res: Output parameter, result of transformation.
187 *
188 * Return value:
Jakub Kicinski2eb57bb2018-05-10 10:24:41 -0700189 * - Zero: pre-processing success.
190 * - Non-zero: pre-processing error, stop loading.
Wang Nanb5805632015-11-16 12:10:09 +0000191 */
192typedef int (*bpf_program_prep_t)(struct bpf_program *prog, int n,
193 struct bpf_insn *insns, int insns_cnt,
194 struct bpf_prog_prep_result *res);
195
Andrey Ignatovab9e0842018-10-15 22:50:34 -0700196LIBBPF_API int bpf_program__set_prep(struct bpf_program *prog, int nr_instance,
197 bpf_program_prep_t prep);
Wang Nanb5805632015-11-16 12:10:09 +0000198
Andrey Ignatovab9e0842018-10-15 22:50:34 -0700199LIBBPF_API int bpf_program__nth_fd(struct bpf_program *prog, int n);
Wang Nanb5805632015-11-16 12:10:09 +0000200
Wang Nan34090912015-07-01 02:14:02 +0000201/*
Jakub Kicinski2eb57bb2018-05-10 10:24:41 -0700202 * Adjust type of BPF program. Default is kprobe.
Wang Nan5f44e4c82016-07-13 10:44:01 +0000203 */
Andrey Ignatovab9e0842018-10-15 22:50:34 -0700204LIBBPF_API int bpf_program__set_socket_filter(struct bpf_program *prog);
205LIBBPF_API int bpf_program__set_tracepoint(struct bpf_program *prog);
206LIBBPF_API int bpf_program__set_raw_tracepoint(struct bpf_program *prog);
207LIBBPF_API int bpf_program__set_kprobe(struct bpf_program *prog);
208LIBBPF_API int bpf_program__set_sched_cls(struct bpf_program *prog);
209LIBBPF_API int bpf_program__set_sched_act(struct bpf_program *prog);
210LIBBPF_API int bpf_program__set_xdp(struct bpf_program *prog);
211LIBBPF_API int bpf_program__set_perf_event(struct bpf_program *prog);
212LIBBPF_API void bpf_program__set_type(struct bpf_program *prog,
213 enum bpf_prog_type type);
214LIBBPF_API void
215bpf_program__set_expected_attach_type(struct bpf_program *prog,
216 enum bpf_attach_type type);
Wang Nan5f44e4c82016-07-13 10:44:01 +0000217
Andrey Ignatovab9e0842018-10-15 22:50:34 -0700218LIBBPF_API bool bpf_program__is_socket_filter(struct bpf_program *prog);
219LIBBPF_API bool bpf_program__is_tracepoint(struct bpf_program *prog);
220LIBBPF_API bool bpf_program__is_raw_tracepoint(struct bpf_program *prog);
221LIBBPF_API bool bpf_program__is_kprobe(struct bpf_program *prog);
222LIBBPF_API bool bpf_program__is_sched_cls(struct bpf_program *prog);
223LIBBPF_API bool bpf_program__is_sched_act(struct bpf_program *prog);
224LIBBPF_API bool bpf_program__is_xdp(struct bpf_program *prog);
225LIBBPF_API bool bpf_program__is_perf_event(struct bpf_program *prog);
Wang Nan5f44e4c82016-07-13 10:44:01 +0000226
227/*
Jakub Kicinski2eb57bb2018-05-10 10:24:41 -0700228 * No need for __attribute__((packed)), all members of 'bpf_map_def'
229 * are all aligned. In addition, using __attribute__((packed))
230 * would trigger a -Wpacked warning message, and lead to an error
231 * if -Werror is set.
Wang Nan34090912015-07-01 02:14:02 +0000232 */
233struct bpf_map_def {
234 unsigned int type;
235 unsigned int key_size;
236 unsigned int value_size;
237 unsigned int max_entries;
Craig Gallekfe9b5f72017-10-05 10:41:58 -0400238 unsigned int map_flags;
Wang Nan34090912015-07-01 02:14:02 +0000239};
240
Wang Nan9d759a92015-11-27 08:47:35 +0000241/*
Jakub Kicinski2eb57bb2018-05-10 10:24:41 -0700242 * The 'struct bpf_map' in include/linux/bpf.h is internal to the kernel,
243 * so no need to worry about a name clash.
Wang Nan9d759a92015-11-27 08:47:35 +0000244 */
245struct bpf_map;
Andrey Ignatovab9e0842018-10-15 22:50:34 -0700246LIBBPF_API struct bpf_map *
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -0300247bpf_object__find_map_by_name(struct bpf_object *obj, const char *name);
Wang Nan9d759a92015-11-27 08:47:35 +0000248
Wang Nan5a6acad2016-11-26 07:03:27 +0000249/*
250 * Get bpf_map through the offset of corresponding struct bpf_map_def
Jakub Kicinski2eb57bb2018-05-10 10:24:41 -0700251 * in the BPF object file.
Wang Nan5a6acad2016-11-26 07:03:27 +0000252 */
Andrey Ignatovab9e0842018-10-15 22:50:34 -0700253LIBBPF_API struct bpf_map *
Wang Nan5a6acad2016-11-26 07:03:27 +0000254bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset);
255
Andrey Ignatovab9e0842018-10-15 22:50:34 -0700256LIBBPF_API struct bpf_map *
Wang Nan9d759a92015-11-27 08:47:35 +0000257bpf_map__next(struct bpf_map *map, struct bpf_object *obj);
258#define bpf_map__for_each(pos, obj) \
259 for ((pos) = bpf_map__next(NULL, (obj)); \
260 (pos) != NULL; \
261 (pos) = bpf_map__next((pos), (obj)))
262
Andrey Ignatovab9e0842018-10-15 22:50:34 -0700263LIBBPF_API int bpf_map__fd(struct bpf_map *map);
264LIBBPF_API const struct bpf_map_def *bpf_map__def(struct bpf_map *map);
265LIBBPF_API const char *bpf_map__name(struct bpf_map *map);
266LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map);
267LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map);
Wang Nan9d759a92015-11-27 08:47:35 +0000268
269typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
Andrey Ignatovab9e0842018-10-15 22:50:34 -0700270LIBBPF_API int bpf_map__set_priv(struct bpf_map *map, void *priv,
271 bpf_map_clear_priv_t clear_priv);
272LIBBPF_API void *bpf_map__priv(struct bpf_map *map);
273LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);
274LIBBPF_API bool bpf_map__is_offload_neutral(struct bpf_map *map);
275LIBBPF_API void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
276LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);
Wang Nan9d759a92015-11-27 08:47:35 +0000277
Andrey Ignatovab9e0842018-10-15 22:50:34 -0700278LIBBPF_API long libbpf_get_error(const void *ptr);
Joe Stringere28ff1a2017-01-22 17:11:25 -0800279
Andrey Ignatovd7be1432018-03-30 15:08:01 -0700280struct bpf_prog_load_attr {
281 const char *file;
282 enum bpf_prog_type prog_type;
283 enum bpf_attach_type expected_attach_type;
David Beckettf0307a72018-05-16 14:02:49 -0700284 int ifindex;
Andrey Ignatovd7be1432018-03-30 15:08:01 -0700285};
286
Andrey Ignatovab9e0842018-10-15 22:50:34 -0700287LIBBPF_API int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
288 struct bpf_object **pobj, int *prog_fd);
289LIBBPF_API int bpf_prog_load(const char *file, enum bpf_prog_type type,
290 struct bpf_object **pobj, int *prog_fd);
Eric Leblond949abbe2018-01-30 21:55:01 +0100291
Andrey Ignatovab9e0842018-10-15 22:50:34 -0700292LIBBPF_API int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags);
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -0700293
294enum bpf_perf_event_ret {
295 LIBBPF_PERF_EVENT_DONE = 0,
296 LIBBPF_PERF_EVENT_ERROR = -1,
297 LIBBPF_PERF_EVENT_CONT = -2,
298};
299
Daniel Borkmann3dca2112018-10-21 02:09:28 +0200300struct perf_event_header;
301typedef enum bpf_perf_event_ret
302 (*bpf_perf_event_print_t)(struct perf_event_header *hdr,
303 void *private_data);
304LIBBPF_API enum bpf_perf_event_ret
305bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
306 void **copy_mem, size_t *copy_size,
307 bpf_perf_event_print_t fn, void *private_data);
Yonghong Song36f16782018-09-05 16:58:05 -0700308
Yonghong Song36f16782018-09-05 16:58:05 -0700309struct nlattr;
Andrey Ignatovaae57782018-10-03 15:26:39 -0700310typedef int (*libbpf_dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb);
311int libbpf_netlink_open(unsigned int *nl_pid);
312int libbpf_nl_get_link(int sock, unsigned int nl_pid,
313 libbpf_dump_nlmsg_t dump_link_nlmsg, void *cookie);
314int libbpf_nl_get_class(int sock, unsigned int nl_pid, int ifindex,
315 libbpf_dump_nlmsg_t dump_class_nlmsg, void *cookie);
316int libbpf_nl_get_qdisc(int sock, unsigned int nl_pid, int ifindex,
317 libbpf_dump_nlmsg_t dump_qdisc_nlmsg, void *cookie);
318int libbpf_nl_get_filter(int sock, unsigned int nl_pid, int ifindex, int handle,
319 libbpf_dump_nlmsg_t dump_filter_nlmsg, void *cookie);
Andrey Ignatoveff81902018-10-03 15:26:42 -0700320#endif /* __LIBBPF_LIBBPF_H */