blob: 4014d1ba5e3d8e74df963ef46e1ea8af90defb84 [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.
Wang Nan203d1ca2016-07-04 11:02:42 +00007 *
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!)
12 *
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.
17 *
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>
Wang Nan1b76c132015-07-01 02:13:51 +000020 */
21#ifndef __BPF_LIBBPF_H
22#define __BPF_LIBBPF_H
23
Wang Nan1a5e3fb2015-07-01 02:13:53 +000024#include <stdio.h>
Joe Stringere28ff1a2017-01-22 17:11:25 -080025#include <stdint.h>
Wang Nanaa9b1ac2015-07-01 02:14:08 +000026#include <stdbool.h>
Wang Nan5a6acad2016-11-26 07:03:27 +000027#include <sys/types.h> // for size_t
Wang Nan6371ca32015-11-06 13:49:37 +000028
29enum libbpf_errno {
30 __LIBBPF_ERRNO__START = 4000,
31
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 */
Colin Ian Kingde8a63b2016-06-28 13:23:37 +010036 LIBBPF_ERRNO__ENDIAN, /* Endian mismatch */
Wang Nan6371ca32015-11-06 13:49:37 +000037 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 */
Wang Nan705fa212016-07-13 10:44:02 +000043 LIBBPF_ERRNO__PROGTYPE, /* Kernel doesn't support this program type */
Wang Nan6371ca32015-11-06 13:49:37 +000044 __LIBBPF_ERRNO__END,
45};
46
47int libbpf_strerror(int err, char *buf, size_t size);
Wang Nan1a5e3fb2015-07-01 02:13:53 +000048
Wang Nanb3f59d62015-07-01 02:13:52 +000049/*
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.
53 */
54typedef int (*libbpf_print_fn_t)(const char *, ...)
55 __attribute__((format(printf, 1, 2)));
56
57void libbpf_set_print(libbpf_print_fn_t warn,
58 libbpf_print_fn_t info,
59 libbpf_print_fn_t debug);
60
Wang Nan1a5e3fb2015-07-01 02:13:53 +000061/* Hide internal to user */
62struct bpf_object;
63
64struct bpf_object *bpf_object__open(const char *path);
Wang Nan6c956392015-07-01 02:13:54 +000065struct bpf_object *bpf_object__open_buffer(void *obj_buf,
Wang Nanacf860a2015-08-27 02:30:55 +000066 size_t obj_buf_sz,
67 const char *name);
Wang Nan1a5e3fb2015-07-01 02:13:53 +000068void bpf_object__close(struct bpf_object *object);
69
Wang Nan52d33522015-07-01 02:14:04 +000070/* Load/unload object into/from kernel */
71int bpf_object__load(struct bpf_object *obj);
72int bpf_object__unload(struct bpf_object *obj);
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -030073const char *bpf_object__name(struct bpf_object *obj);
74unsigned int bpf_object__kversion(struct bpf_object *obj);
Wang Nan52d33522015-07-01 02:14:04 +000075
Wang Nan9a208ef2015-07-01 02:14:10 +000076struct 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); \
80 (pos) != NULL; \
81 (pos) = (tmp), (tmp) = bpf_object__next(tmp))
82
Wang Nan10931d22016-11-26 07:03:26 +000083typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *);
84int bpf_object__set_priv(struct bpf_object *obj, void *priv,
85 bpf_object_clear_priv_t clear_priv);
86void *bpf_object__priv(struct bpf_object *prog);
87
Wang Nanaa9b1ac2015-07-01 02:14:08 +000088/* Accessors of bpf_program. */
89struct bpf_program;
90struct bpf_program *bpf_program__next(struct bpf_program *prog,
91 struct bpf_object *obj);
92
93#define bpf_object__for_each_program(pos, obj) \
94 for ((pos) = bpf_program__next(NULL, (obj)); \
95 (pos) != NULL; \
96 (pos) = bpf_program__next((pos), (obj)))
97
98typedef void (*bpf_program_clear_priv_t)(struct bpf_program *,
99 void *);
100
Arnaldo Carvalho de Meloedb13ed2016-06-03 12:38:21 -0300101int bpf_program__set_priv(struct bpf_program *prog, void *priv,
102 bpf_program_clear_priv_t clear_priv);
Wang Nanaa9b1ac2015-07-01 02:14:08 +0000103
Arnaldo Carvalho de Melobe834ff2016-06-03 12:36:39 -0300104void *bpf_program__priv(struct bpf_program *prog);
Wang Nanaa9b1ac2015-07-01 02:14:08 +0000105
Namhyung Kim715f8db2015-11-03 20:21:05 +0900106const char *bpf_program__title(struct bpf_program *prog, bool needs_copy);
Wang Nanaa9b1ac2015-07-01 02:14:08 +0000107
108int bpf_program__fd(struct bpf_program *prog);
109
Wang Nanb5805632015-11-16 12:10:09 +0000110struct bpf_insn;
111
112/*
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.
116 *
117 * bpf_program_prep_t, bpf_program__set_prep and bpf_program__nth_fd
118 * are APIs for this propose.
119 *
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
125 * passed to it.
126 *
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.
130 *
131 * - bpf_program__nth_fd:
132 * After the program is loaded, get resuling fds from bpf program for
133 * each instances.
134 *
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).
139 */
140
141struct bpf_prog_prep_result {
142 /*
143 * If not NULL, load new instruction array.
144 * If set to NULL, don't load this instance.
145 */
146 struct bpf_insn *new_insn_ptr;
147 int new_insn_cnt;
148
149 /* If not NULL, result fd is set to it */
150 int *pfd;
151};
152
153/*
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.
160 *
161 * Return value:
162 * - Zero: pre-processing success.
163 * - Non-zero: pre-processing, stop loading.
164 */
165typedef 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);
168
169int bpf_program__set_prep(struct bpf_program *prog, int nr_instance,
170 bpf_program_prep_t prep);
171
172int bpf_program__nth_fd(struct bpf_program *prog, int n);
173
Wang Nan34090912015-07-01 02:14:02 +0000174/*
Wang Nan5f44e4c82016-07-13 10:44:01 +0000175 * Adjust type of bpf program. Default is kprobe.
176 */
Joe Stringer7803ba72017-01-22 17:11:24 -0800177int bpf_program__set_socket_filter(struct bpf_program *prog);
Wang Nan5f44e4c82016-07-13 10:44:01 +0000178int bpf_program__set_tracepoint(struct bpf_program *prog);
179int bpf_program__set_kprobe(struct bpf_program *prog);
Joe Stringer7803ba72017-01-22 17:11:24 -0800180int bpf_program__set_sched_cls(struct bpf_program *prog);
181int bpf_program__set_sched_act(struct bpf_program *prog);
182int bpf_program__set_xdp(struct bpf_program *prog);
183int bpf_program__set_perf_event(struct bpf_program *prog);
Wang Nan5f44e4c82016-07-13 10:44:01 +0000184
Joe Stringer7803ba72017-01-22 17:11:24 -0800185bool bpf_program__is_socket_filter(struct bpf_program *prog);
Wang Nan5f44e4c82016-07-13 10:44:01 +0000186bool bpf_program__is_tracepoint(struct bpf_program *prog);
187bool bpf_program__is_kprobe(struct bpf_program *prog);
Joe Stringer7803ba72017-01-22 17:11:24 -0800188bool bpf_program__is_sched_cls(struct bpf_program *prog);
189bool bpf_program__is_sched_act(struct bpf_program *prog);
190bool bpf_program__is_xdp(struct bpf_program *prog);
191bool bpf_program__is_perf_event(struct bpf_program *prog);
Wang Nan5f44e4c82016-07-13 10:44:01 +0000192
193/*
Wang Nan34090912015-07-01 02:14:02 +0000194 * We don't need __attribute__((packed)) now since it is
195 * unnecessary for 'bpf_map_def' because they are all aligned.
196 * In addition, using it will trigger -Wpacked warning message,
197 * and will be treated as an error due to -Werror.
198 */
199struct bpf_map_def {
200 unsigned int type;
201 unsigned int key_size;
202 unsigned int value_size;
203 unsigned int max_entries;
204};
205
Wang Nan9d759a92015-11-27 08:47:35 +0000206/*
207 * There is another 'struct bpf_map' in include/linux/map.h. However,
208 * it is not a uapi header so no need to consider name clash.
209 */
210struct bpf_map;
Wang Nan561bbcc2015-11-27 08:47:36 +0000211struct bpf_map *
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -0300212bpf_object__find_map_by_name(struct bpf_object *obj, const char *name);
Wang Nan9d759a92015-11-27 08:47:35 +0000213
Wang Nan5a6acad2016-11-26 07:03:27 +0000214/*
215 * Get bpf_map through the offset of corresponding struct bpf_map_def
216 * in the bpf object file.
217 */
218struct bpf_map *
219bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset);
220
Wang Nan9d759a92015-11-27 08:47:35 +0000221struct bpf_map *
222bpf_map__next(struct bpf_map *map, struct bpf_object *obj);
223#define bpf_map__for_each(pos, obj) \
224 for ((pos) = bpf_map__next(NULL, (obj)); \
225 (pos) != NULL; \
226 (pos) = bpf_map__next((pos), (obj)))
227
Arnaldo Carvalho de Melo6e009e62016-06-03 12:15:52 -0300228int bpf_map__fd(struct bpf_map *map);
Arnaldo Carvalho de Melo53897a72016-06-02 14:21:06 -0300229const struct bpf_map_def *bpf_map__def(struct bpf_map *map);
Arnaldo Carvalho de Melo009ad5d2016-06-02 11:02:05 -0300230const char *bpf_map__name(struct bpf_map *map);
Wang Nan9d759a92015-11-27 08:47:35 +0000231
232typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
Arnaldo Carvalho de Meloedb13ed2016-06-03 12:38:21 -0300233int bpf_map__set_priv(struct bpf_map *map, void *priv,
234 bpf_map_clear_priv_t clear_priv);
Arnaldo Carvalho de Melob4cbfa52016-06-02 10:51:59 -0300235void *bpf_map__priv(struct bpf_map *map);
Wang Nan9d759a92015-11-27 08:47:35 +0000236
Joe Stringere28ff1a2017-01-22 17:11:25 -0800237long libbpf_get_error(const void *ptr);
238
Wang Nan1b76c132015-07-01 02:13:51 +0000239#endif