blob: 749acf58a5da88f4d490e6e82c5e7e8968750406 [file] [log] [blame]
Eric Leblond6061a3d2018-01-30 21:55:03 +01001/* SPDX-License-Identifier: LGPL-2.1 */
2
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.
Wang Nan203d1ca2016-07-04 11:02:42 +00009 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation;
13 * version 2.1 of the License (not later!)
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this program; if not, see <http://www.gnu.org/licenses>
Wang Nan1b76c132015-07-01 02:13:51 +000022 */
23#ifndef __BPF_LIBBPF_H
24#define __BPF_LIBBPF_H
25
Wang Nan1a5e3fb2015-07-01 02:13:53 +000026#include <stdio.h>
Joe Stringere28ff1a2017-01-22 17:11:25 -080027#include <stdint.h>
Wang Nanaa9b1ac2015-07-01 02:14:08 +000028#include <stdbool.h>
Wang Nan5a6acad2016-11-26 07:03:27 +000029#include <sys/types.h> // for size_t
Alexei Starovoitovdd26b7f2017-03-30 21:45:40 -070030#include <linux/bpf.h>
Wang Nan6371ca3b2015-11-06 13:49:37 +000031
32enum libbpf_errno {
33 __LIBBPF_ERRNO__START = 4000,
34
35 /* Something wrong in libelf */
36 LIBBPF_ERRNO__LIBELF = __LIBBPF_ERRNO__START,
37 LIBBPF_ERRNO__FORMAT, /* BPF object format invalid */
38 LIBBPF_ERRNO__KVERSION, /* Incorrect or no 'version' section */
Colin Ian Kingde8a63b2016-06-28 13:23:37 +010039 LIBBPF_ERRNO__ENDIAN, /* Endian mismatch */
Wang Nan6371ca3b2015-11-06 13:49:37 +000040 LIBBPF_ERRNO__INTERNAL, /* Internal error in libbpf */
41 LIBBPF_ERRNO__RELOC, /* Relocation failed */
42 LIBBPF_ERRNO__LOAD, /* Load program failure for unknown reason */
43 LIBBPF_ERRNO__VERIFY, /* Kernel verifier blocks program loading */
44 LIBBPF_ERRNO__PROG2BIG, /* Program too big */
45 LIBBPF_ERRNO__KVER, /* Incorrect kernel version */
Wang Nan705fa212016-07-13 10:44:02 +000046 LIBBPF_ERRNO__PROGTYPE, /* Kernel doesn't support this program type */
Eric Leblond949abbe2018-01-30 21:55:01 +010047 LIBBPF_ERRNO__WRNGPID, /* Wrong pid in netlink message */
48 LIBBPF_ERRNO__INVSEQ, /* Invalid netlink sequence */
Wang Nan6371ca3b2015-11-06 13:49:37 +000049 __LIBBPF_ERRNO__END,
50};
51
52int libbpf_strerror(int err, char *buf, size_t size);
Wang Nan1a5e3fb2015-07-01 02:13:53 +000053
Wang Nanb3f59d62015-07-01 02:13:52 +000054/*
Jakub Kicinski2eb57bb2018-05-10 10:24:41 -070055 * __printf is defined in include/linux/compiler-gcc.h. However,
56 * it would be better if libbpf.h didn't depend on Linux header files.
Wang Nanb3f59d62015-07-01 02:13:52 +000057 * So instead of __printf, here we use gcc attribute directly.
58 */
59typedef int (*libbpf_print_fn_t)(const char *, ...)
60 __attribute__((format(printf, 1, 2)));
61
62void libbpf_set_print(libbpf_print_fn_t warn,
63 libbpf_print_fn_t info,
64 libbpf_print_fn_t debug);
65
Wang Nan1a5e3fb2015-07-01 02:13:53 +000066/* Hide internal to user */
67struct bpf_object;
68
69struct bpf_object *bpf_object__open(const char *path);
Wang Nan6c956392015-07-01 02:13:54 +000070struct bpf_object *bpf_object__open_buffer(void *obj_buf,
Wang Nanacf860a2015-08-27 02:30:55 +000071 size_t obj_buf_sz,
72 const char *name);
Joe Stringerd5148d82017-01-26 13:19:58 -080073int bpf_object__pin(struct bpf_object *object, const char *path);
Wang Nan1a5e3fb2015-07-01 02:13:53 +000074void bpf_object__close(struct bpf_object *object);
75
Wang Nan52d33522015-07-01 02:14:04 +000076/* Load/unload object into/from kernel */
77int bpf_object__load(struct bpf_object *obj);
78int bpf_object__unload(struct bpf_object *obj);
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -030079const char *bpf_object__name(struct bpf_object *obj);
80unsigned int bpf_object__kversion(struct bpf_object *obj);
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -070081int bpf_object__btf_fd(const struct bpf_object *obj);
Wang Nan52d33522015-07-01 02:14:04 +000082
Wang Nan9a208ef2015-07-01 02:14:10 +000083struct bpf_object *bpf_object__next(struct bpf_object *prev);
84#define bpf_object__for_each_safe(pos, tmp) \
85 for ((pos) = bpf_object__next(NULL), \
86 (tmp) = bpf_object__next(pos); \
87 (pos) != NULL; \
88 (pos) = (tmp), (tmp) = bpf_object__next(tmp))
89
Wang Nan10931d22016-11-26 07:03:26 +000090typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *);
91int bpf_object__set_priv(struct bpf_object *obj, void *priv,
92 bpf_object_clear_priv_t clear_priv);
93void *bpf_object__priv(struct bpf_object *prog);
94
Jakub Kicinskib60df2a2018-07-10 14:42:59 -070095int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
96 enum bpf_attach_type *expected_attach_type);
97
Jakub Kicinski2eb57bb2018-05-10 10:24:41 -070098/* Accessors of bpf_program */
Wang Nanaa9b1ac2015-07-01 02:14:08 +000099struct bpf_program;
100struct bpf_program *bpf_program__next(struct bpf_program *prog,
101 struct bpf_object *obj);
102
103#define bpf_object__for_each_program(pos, obj) \
104 for ((pos) = bpf_program__next(NULL, (obj)); \
105 (pos) != NULL; \
106 (pos) = bpf_program__next((pos), (obj)))
107
108typedef void (*bpf_program_clear_priv_t)(struct bpf_program *,
109 void *);
110
Arnaldo Carvalho de Meloedb13ed2016-06-03 12:38:21 -0300111int bpf_program__set_priv(struct bpf_program *prog, void *priv,
112 bpf_program_clear_priv_t clear_priv);
Wang Nanaa9b1ac2015-07-01 02:14:08 +0000113
Arnaldo Carvalho de Melobe834ff2016-06-03 12:36:39 -0300114void *bpf_program__priv(struct bpf_program *prog);
Jakub Kicinski9aba3612018-06-28 14:41:37 -0700115void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex);
Wang Nanaa9b1ac2015-07-01 02:14:08 +0000116
Namhyung Kim715f8db2015-11-03 20:21:05 +0900117const char *bpf_program__title(struct bpf_program *prog, bool needs_copy);
Wang Nanaa9b1ac2015-07-01 02:14:08 +0000118
119int bpf_program__fd(struct bpf_program *prog);
Joe Stringerf3675402017-01-26 13:19:56 -0800120int bpf_program__pin_instance(struct bpf_program *prog, const char *path,
121 int instance);
122int bpf_program__pin(struct bpf_program *prog, const char *path);
Wang Nanaa9b1ac2015-07-01 02:14:08 +0000123
Wang Nanb5805632015-11-16 12:10:09 +0000124struct bpf_insn;
125
126/*
127 * Libbpf allows callers to adjust BPF programs before being loaded
Jakub Kicinski2eb57bb2018-05-10 10:24:41 -0700128 * into kernel. One program in an object file can be transformed into
129 * multiple variants to be attached to different hooks.
Wang Nanb5805632015-11-16 12:10:09 +0000130 *
131 * bpf_program_prep_t, bpf_program__set_prep and bpf_program__nth_fd
Jakub Kicinski2eb57bb2018-05-10 10:24:41 -0700132 * form an API for this purpose.
Wang Nanb5805632015-11-16 12:10:09 +0000133 *
134 * - bpf_program_prep_t:
Jakub Kicinski2eb57bb2018-05-10 10:24:41 -0700135 * Defines a 'preprocessor', which is a caller defined function
Wang Nanb5805632015-11-16 12:10:09 +0000136 * passed to libbpf through bpf_program__set_prep(), and will be
137 * called before program is loaded. The processor should adjust
Jakub Kicinski2eb57bb2018-05-10 10:24:41 -0700138 * the program one time for each instance according to the instance id
Wang Nanb5805632015-11-16 12:10:09 +0000139 * passed to it.
140 *
141 * - bpf_program__set_prep:
Jakub Kicinski2eb57bb2018-05-10 10:24:41 -0700142 * Attaches a preprocessor to a BPF program. The number of instances
143 * that should be created is also passed through this function.
Wang Nanb5805632015-11-16 12:10:09 +0000144 *
145 * - bpf_program__nth_fd:
Jakub Kicinski2eb57bb2018-05-10 10:24:41 -0700146 * After the program is loaded, get resulting FD of a given instance
147 * of the BPF program.
Wang Nanb5805632015-11-16 12:10:09 +0000148 *
Jakub Kicinski2eb57bb2018-05-10 10:24:41 -0700149 * If bpf_program__set_prep() is not used, the program would be loaded
Wang Nanb5805632015-11-16 12:10:09 +0000150 * without adjustment during bpf_object__load(). The program has only
151 * one instance. In this case bpf_program__fd(prog) is equal to
152 * bpf_program__nth_fd(prog, 0).
153 */
154
155struct bpf_prog_prep_result {
156 /*
157 * If not NULL, load new instruction array.
158 * If set to NULL, don't load this instance.
159 */
160 struct bpf_insn *new_insn_ptr;
161 int new_insn_cnt;
162
Jakub Kicinski2eb57bb2018-05-10 10:24:41 -0700163 /* If not NULL, result FD is written to it. */
Wang Nanb5805632015-11-16 12:10:09 +0000164 int *pfd;
165};
166
167/*
168 * Parameters of bpf_program_prep_t:
169 * - prog: The bpf_program being loaded.
170 * - n: Index of instance being generated.
171 * - insns: BPF instructions array.
172 * - insns_cnt:Number of instructions in insns.
173 * - res: Output parameter, result of transformation.
174 *
175 * Return value:
Jakub Kicinski2eb57bb2018-05-10 10:24:41 -0700176 * - Zero: pre-processing success.
177 * - Non-zero: pre-processing error, stop loading.
Wang Nanb5805632015-11-16 12:10:09 +0000178 */
179typedef int (*bpf_program_prep_t)(struct bpf_program *prog, int n,
180 struct bpf_insn *insns, int insns_cnt,
181 struct bpf_prog_prep_result *res);
182
183int bpf_program__set_prep(struct bpf_program *prog, int nr_instance,
184 bpf_program_prep_t prep);
185
186int bpf_program__nth_fd(struct bpf_program *prog, int n);
187
Wang Nan34090912015-07-01 02:14:02 +0000188/*
Jakub Kicinski2eb57bb2018-05-10 10:24:41 -0700189 * Adjust type of BPF program. Default is kprobe.
Wang Nan5f44e4c82016-07-13 10:44:01 +0000190 */
Joe Stringer7803ba72017-01-22 17:11:24 -0800191int bpf_program__set_socket_filter(struct bpf_program *prog);
Wang Nan5f44e4c82016-07-13 10:44:01 +0000192int bpf_program__set_tracepoint(struct bpf_program *prog);
Andrey Ignatove14c93f2018-04-17 10:28:46 -0700193int bpf_program__set_raw_tracepoint(struct bpf_program *prog);
Wang Nan5f44e4c82016-07-13 10:44:01 +0000194int bpf_program__set_kprobe(struct bpf_program *prog);
Joe Stringer7803ba72017-01-22 17:11:24 -0800195int bpf_program__set_sched_cls(struct bpf_program *prog);
196int bpf_program__set_sched_act(struct bpf_program *prog);
197int bpf_program__set_xdp(struct bpf_program *prog);
198int bpf_program__set_perf_event(struct bpf_program *prog);
Alexei Starovoitovdd26b7f2017-03-30 21:45:40 -0700199void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type);
John Fastabend16962b22018-04-23 14:30:38 -0700200void bpf_program__set_expected_attach_type(struct bpf_program *prog,
201 enum bpf_attach_type type);
Wang Nan5f44e4c82016-07-13 10:44:01 +0000202
Joe Stringer7803ba72017-01-22 17:11:24 -0800203bool bpf_program__is_socket_filter(struct bpf_program *prog);
Wang Nan5f44e4c82016-07-13 10:44:01 +0000204bool bpf_program__is_tracepoint(struct bpf_program *prog);
Andrey Ignatove14c93f2018-04-17 10:28:46 -0700205bool bpf_program__is_raw_tracepoint(struct bpf_program *prog);
Wang Nan5f44e4c82016-07-13 10:44:01 +0000206bool bpf_program__is_kprobe(struct bpf_program *prog);
Joe Stringer7803ba72017-01-22 17:11:24 -0800207bool bpf_program__is_sched_cls(struct bpf_program *prog);
208bool bpf_program__is_sched_act(struct bpf_program *prog);
209bool bpf_program__is_xdp(struct bpf_program *prog);
210bool bpf_program__is_perf_event(struct bpf_program *prog);
Wang Nan5f44e4c82016-07-13 10:44:01 +0000211
212/*
Jakub Kicinski2eb57bb2018-05-10 10:24:41 -0700213 * No need for __attribute__((packed)), all members of 'bpf_map_def'
214 * are all aligned. In addition, using __attribute__((packed))
215 * would trigger a -Wpacked warning message, and lead to an error
216 * if -Werror is set.
Wang Nan34090912015-07-01 02:14:02 +0000217 */
218struct bpf_map_def {
219 unsigned int type;
220 unsigned int key_size;
221 unsigned int value_size;
222 unsigned int max_entries;
Craig Gallekfe9b5f72017-10-05 10:41:58 -0400223 unsigned int map_flags;
Wang Nan34090912015-07-01 02:14:02 +0000224};
225
Wang Nan9d759a92015-11-27 08:47:35 +0000226/*
Jakub Kicinski2eb57bb2018-05-10 10:24:41 -0700227 * The 'struct bpf_map' in include/linux/bpf.h is internal to the kernel,
228 * so no need to worry about a name clash.
Wang Nan9d759a92015-11-27 08:47:35 +0000229 */
230struct bpf_map;
Wang Nan561bbcc2015-11-27 08:47:36 +0000231struct bpf_map *
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -0300232bpf_object__find_map_by_name(struct bpf_object *obj, const char *name);
Wang Nan9d759a92015-11-27 08:47:35 +0000233
Wang Nan5a6acad2016-11-26 07:03:27 +0000234/*
235 * Get bpf_map through the offset of corresponding struct bpf_map_def
Jakub Kicinski2eb57bb2018-05-10 10:24:41 -0700236 * in the BPF object file.
Wang Nan5a6acad2016-11-26 07:03:27 +0000237 */
238struct bpf_map *
239bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset);
240
Wang Nan9d759a92015-11-27 08:47:35 +0000241struct bpf_map *
242bpf_map__next(struct bpf_map *map, struct bpf_object *obj);
243#define bpf_map__for_each(pos, obj) \
244 for ((pos) = bpf_map__next(NULL, (obj)); \
245 (pos) != NULL; \
246 (pos) = bpf_map__next((pos), (obj)))
247
Arnaldo Carvalho de Melo6e009e62016-06-03 12:15:52 -0300248int bpf_map__fd(struct bpf_map *map);
Arnaldo Carvalho de Melo53897a72016-06-02 14:21:06 -0300249const struct bpf_map_def *bpf_map__def(struct bpf_map *map);
Arnaldo Carvalho de Melo009ad5d2016-06-02 11:02:05 -0300250const char *bpf_map__name(struct bpf_map *map);
Martin KaFai Lau61746db2018-05-22 15:04:24 -0700251uint32_t bpf_map__btf_key_type_id(const struct bpf_map *map);
252uint32_t bpf_map__btf_value_type_id(const struct bpf_map *map);
Wang Nan9d759a92015-11-27 08:47:35 +0000253
254typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
Arnaldo Carvalho de Meloedb13ed2016-06-03 12:38:21 -0300255int bpf_map__set_priv(struct bpf_map *map, void *priv,
256 bpf_map_clear_priv_t clear_priv);
Arnaldo Carvalho de Melob4cbfa52016-06-02 10:51:59 -0300257void *bpf_map__priv(struct bpf_map *map);
Jakub Kicinskif83fb222018-07-10 14:43:01 -0700258bool bpf_map__is_offload_neutral(struct bpf_map *map);
Jakub Kicinski9aba3612018-06-28 14:41:37 -0700259void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
Joe Stringerb6989f32017-01-26 13:19:57 -0800260int bpf_map__pin(struct bpf_map *map, const char *path);
Wang Nan9d759a92015-11-27 08:47:35 +0000261
Joe Stringere28ff1a2017-01-22 17:11:25 -0800262long libbpf_get_error(const void *ptr);
263
Andrey Ignatovd7be1432018-03-30 15:08:01 -0700264struct bpf_prog_load_attr {
265 const char *file;
266 enum bpf_prog_type prog_type;
267 enum bpf_attach_type expected_attach_type;
David Beckettf0307a72018-05-16 14:02:49 -0700268 int ifindex;
Andrey Ignatovd7be1432018-03-30 15:08:01 -0700269};
270
271int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
272 struct bpf_object **pobj, int *prog_fd);
John Fastabend6f6d33f2017-08-15 22:34:22 -0700273int bpf_prog_load(const char *file, enum bpf_prog_type type,
274 struct bpf_object **pobj, int *prog_fd);
Eric Leblond949abbe2018-01-30 21:55:01 +0100275
276int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags);
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -0700277
278enum bpf_perf_event_ret {
279 LIBBPF_PERF_EVENT_DONE = 0,
280 LIBBPF_PERF_EVENT_ERROR = -1,
281 LIBBPF_PERF_EVENT_CONT = -2,
282};
283
284typedef enum bpf_perf_event_ret (*bpf_perf_event_print_t)(void *event,
285 void *priv);
286int bpf_perf_event_read_simple(void *mem, unsigned long size,
287 unsigned long page_size,
288 void **buf, size_t *buf_len,
289 bpf_perf_event_print_t fn, void *priv);
Wang Nan1b76c132015-07-01 02:13:51 +0000290#endif