libbpf: add "prog_flags" to bpf_program/bpf_prog_load_attr/bpf_load_program_attr
[linux-2.6-block.git] / tools / lib / bpf / libbpf.h
CommitLineData
1bc38b8f 1/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
6061a3d6 2
1b76c13e
WN
3/*
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 */
eff81908
AI
10#ifndef __LIBBPF_LIBBPF_H
11#define __LIBBPF_LIBBPF_H
1b76c13e 12
dfcbc2f2 13#include <stdarg.h>
1a5e3fb1 14#include <stdio.h>
e28ff1a8 15#include <stdint.h>
aa9b1ac3 16#include <stdbool.h>
5a6acad1 17#include <sys/types.h> // for size_t
dd26b7f5 18#include <linux/bpf.h>
6371ca3b 19
8c4905b9
SF
20#ifdef __cplusplus
21extern "C" {
22#endif
23
ab9e0848
AI
24#ifndef LIBBPF_API
25#define LIBBPF_API __attribute__((visibility("default")))
26#endif
27
6371ca3b
WN
28enum libbpf_errno {
29 __LIBBPF_ERRNO__START = 4000,
30
31 /* Something wrong in libelf */
32 LIBBPF_ERRNO__LIBELF = __LIBBPF_ERRNO__START,
33 LIBBPF_ERRNO__FORMAT, /* BPF object format invalid */
34 LIBBPF_ERRNO__KVERSION, /* Incorrect or no 'version' section */
de8a63bd 35 LIBBPF_ERRNO__ENDIAN, /* Endian mismatch */
6371ca3b
WN
36 LIBBPF_ERRNO__INTERNAL, /* Internal error in libbpf */
37 LIBBPF_ERRNO__RELOC, /* Relocation failed */
38 LIBBPF_ERRNO__LOAD, /* Load program failure for unknown reason */
39 LIBBPF_ERRNO__VERIFY, /* Kernel verifier blocks program loading */
40 LIBBPF_ERRNO__PROG2BIG, /* Program too big */
41 LIBBPF_ERRNO__KVER, /* Incorrect kernel version */
705fa219 42 LIBBPF_ERRNO__PROGTYPE, /* Kernel doesn't support this program type */
949abbe8
EL
43 LIBBPF_ERRNO__WRNGPID, /* Wrong pid in netlink message */
44 LIBBPF_ERRNO__INVSEQ, /* Invalid netlink sequence */
36f1678d 45 LIBBPF_ERRNO__NLPARSE, /* netlink parsing error */
6371ca3b
WN
46 __LIBBPF_ERRNO__END,
47};
48
ab9e0848 49LIBBPF_API int libbpf_strerror(int err, char *buf, size_t size);
1a5e3fb1 50
8461ef8b
YS
51enum libbpf_print_level {
52 LIBBPF_WARN,
53 LIBBPF_INFO,
54 LIBBPF_DEBUG,
55};
56
6f1ae8b6 57typedef int (*libbpf_print_fn_t)(enum libbpf_print_level level,
a8a1f7d0 58 const char *, va_list ap);
b3f59d66 59
6f1ae8b6 60LIBBPF_API void libbpf_set_print(libbpf_print_fn_t fn);
b3f59d66 61
1a5e3fb1
WN
62/* Hide internal to user */
63struct bpf_object;
64
07f2d4ea
JK
65struct bpf_object_open_attr {
66 const char *file;
67 enum bpf_prog_type prog_type;
68};
69
ab9e0848
AI
70LIBBPF_API struct bpf_object *bpf_object__open(const char *path);
71LIBBPF_API struct bpf_object *
72bpf_object__open_xattr(struct bpf_object_open_attr *attr);
c034a177
JF
73struct bpf_object *__bpf_object__open_xattr(struct bpf_object_open_attr *attr,
74 int flags);
ab9e0848
AI
75LIBBPF_API struct bpf_object *bpf_object__open_buffer(void *obj_buf,
76 size_t obj_buf_sz,
77 const char *name);
1713d68b
DB
78int bpf_object__section_size(const struct bpf_object *obj, const char *name,
79 __u32 *size);
80int bpf_object__variable_offset(const struct bpf_object *obj, const char *name,
81 __u32 *off);
0c19a9fb
SF
82LIBBPF_API int bpf_object__pin_maps(struct bpf_object *obj, const char *path);
83LIBBPF_API int bpf_object__unpin_maps(struct bpf_object *obj,
84 const char *path);
85LIBBPF_API int bpf_object__pin_programs(struct bpf_object *obj,
86 const char *path);
87LIBBPF_API int bpf_object__unpin_programs(struct bpf_object *obj,
88 const char *path);
ab9e0848
AI
89LIBBPF_API int bpf_object__pin(struct bpf_object *object, const char *path);
90LIBBPF_API void bpf_object__close(struct bpf_object *object);
1a5e3fb1 91
52d3352e 92/* Load/unload object into/from kernel */
ab9e0848
AI
93LIBBPF_API int bpf_object__load(struct bpf_object *obj);
94LIBBPF_API int bpf_object__unload(struct bpf_object *obj);
95LIBBPF_API const char *bpf_object__name(struct bpf_object *obj);
96LIBBPF_API unsigned int bpf_object__kversion(struct bpf_object *obj);
789f6bab
AI
97
98struct btf;
99LIBBPF_API struct btf *bpf_object__btf(struct bpf_object *obj);
ab9e0848 100LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj);
52d3352e 101
ab9e0848 102LIBBPF_API struct bpf_program *
6d4b198b
JK
103bpf_object__find_program_by_title(struct bpf_object *obj, const char *title);
104
ab9e0848 105LIBBPF_API struct bpf_object *bpf_object__next(struct bpf_object *prev);
9a208eff
WN
106#define bpf_object__for_each_safe(pos, tmp) \
107 for ((pos) = bpf_object__next(NULL), \
108 (tmp) = bpf_object__next(pos); \
109 (pos) != NULL; \
110 (pos) = (tmp), (tmp) = bpf_object__next(tmp))
111
10931d24 112typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *);
ab9e0848
AI
113LIBBPF_API int bpf_object__set_priv(struct bpf_object *obj, void *priv,
114 bpf_object_clear_priv_t clear_priv);
115LIBBPF_API void *bpf_object__priv(struct bpf_object *prog);
10931d24 116
ab9e0848
AI
117LIBBPF_API int
118libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
119 enum bpf_attach_type *expected_attach_type);
120LIBBPF_API int libbpf_attach_type_by_name(const char *name,
121 enum bpf_attach_type *attach_type);
b60df2a0 122
2eb57bb8 123/* Accessors of bpf_program */
aa9b1ac3 124struct bpf_program;
ab9e0848
AI
125LIBBPF_API struct bpf_program *bpf_program__next(struct bpf_program *prog,
126 struct bpf_object *obj);
aa9b1ac3
WN
127
128#define bpf_object__for_each_program(pos, obj) \
129 for ((pos) = bpf_program__next(NULL, (obj)); \
130 (pos) != NULL; \
131 (pos) = bpf_program__next((pos), (obj)))
132
0c19a9fb
SF
133LIBBPF_API struct bpf_program *bpf_program__prev(struct bpf_program *prog,
134 struct bpf_object *obj);
135
aa9b1ac3
WN
136typedef void (*bpf_program_clear_priv_t)(struct bpf_program *,
137 void *);
138
ab9e0848
AI
139LIBBPF_API int bpf_program__set_priv(struct bpf_program *prog, void *priv,
140 bpf_program_clear_priv_t clear_priv);
aa9b1ac3 141
ab9e0848
AI
142LIBBPF_API void *bpf_program__priv(struct bpf_program *prog);
143LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,
144 __u32 ifindex);
aa9b1ac3 145
ab9e0848
AI
146LIBBPF_API const char *bpf_program__title(struct bpf_program *prog,
147 bool needs_copy);
aa9b1ac3 148
ab9e0848
AI
149LIBBPF_API int bpf_program__load(struct bpf_program *prog, char *license,
150 __u32 kern_version);
151LIBBPF_API int bpf_program__fd(struct bpf_program *prog);
152LIBBPF_API int bpf_program__pin_instance(struct bpf_program *prog,
153 const char *path,
154 int instance);
0c19a9fb
SF
155LIBBPF_API int bpf_program__unpin_instance(struct bpf_program *prog,
156 const char *path,
157 int instance);
ab9e0848 158LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path);
0c19a9fb 159LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path);
ab9e0848 160LIBBPF_API void bpf_program__unload(struct bpf_program *prog);
aa9b1ac3 161
b580563e
WN
162struct bpf_insn;
163
164/*
165 * Libbpf allows callers to adjust BPF programs before being loaded
2eb57bb8
JK
166 * into kernel. One program in an object file can be transformed into
167 * multiple variants to be attached to different hooks.
b580563e
WN
168 *
169 * bpf_program_prep_t, bpf_program__set_prep and bpf_program__nth_fd
2eb57bb8 170 * form an API for this purpose.
b580563e
WN
171 *
172 * - bpf_program_prep_t:
2eb57bb8 173 * Defines a 'preprocessor', which is a caller defined function
b580563e
WN
174 * passed to libbpf through bpf_program__set_prep(), and will be
175 * called before program is loaded. The processor should adjust
2eb57bb8 176 * the program one time for each instance according to the instance id
b580563e
WN
177 * passed to it.
178 *
179 * - bpf_program__set_prep:
2eb57bb8
JK
180 * Attaches a preprocessor to a BPF program. The number of instances
181 * that should be created is also passed through this function.
b580563e
WN
182 *
183 * - bpf_program__nth_fd:
2eb57bb8
JK
184 * After the program is loaded, get resulting FD of a given instance
185 * of the BPF program.
b580563e 186 *
2eb57bb8 187 * If bpf_program__set_prep() is not used, the program would be loaded
b580563e
WN
188 * without adjustment during bpf_object__load(). The program has only
189 * one instance. In this case bpf_program__fd(prog) is equal to
190 * bpf_program__nth_fd(prog, 0).
191 */
192
193struct bpf_prog_prep_result {
194 /*
195 * If not NULL, load new instruction array.
196 * If set to NULL, don't load this instance.
197 */
198 struct bpf_insn *new_insn_ptr;
199 int new_insn_cnt;
200
2eb57bb8 201 /* If not NULL, result FD is written to it. */
b580563e
WN
202 int *pfd;
203};
204
205/*
206 * Parameters of bpf_program_prep_t:
207 * - prog: The bpf_program being loaded.
208 * - n: Index of instance being generated.
209 * - insns: BPF instructions array.
210 * - insns_cnt:Number of instructions in insns.
211 * - res: Output parameter, result of transformation.
212 *
213 * Return value:
2eb57bb8
JK
214 * - Zero: pre-processing success.
215 * - Non-zero: pre-processing error, stop loading.
b580563e
WN
216 */
217typedef int (*bpf_program_prep_t)(struct bpf_program *prog, int n,
218 struct bpf_insn *insns, int insns_cnt,
219 struct bpf_prog_prep_result *res);
220
ab9e0848
AI
221LIBBPF_API int bpf_program__set_prep(struct bpf_program *prog, int nr_instance,
222 bpf_program_prep_t prep);
b580563e 223
ab9e0848 224LIBBPF_API int bpf_program__nth_fd(struct bpf_program *prog, int n);
b580563e 225
5f44e4c8 226/*
2eb57bb8 227 * Adjust type of BPF program. Default is kprobe.
5f44e4c8 228 */
ab9e0848
AI
229LIBBPF_API int bpf_program__set_socket_filter(struct bpf_program *prog);
230LIBBPF_API int bpf_program__set_tracepoint(struct bpf_program *prog);
231LIBBPF_API int bpf_program__set_raw_tracepoint(struct bpf_program *prog);
232LIBBPF_API int bpf_program__set_kprobe(struct bpf_program *prog);
233LIBBPF_API int bpf_program__set_sched_cls(struct bpf_program *prog);
234LIBBPF_API int bpf_program__set_sched_act(struct bpf_program *prog);
235LIBBPF_API int bpf_program__set_xdp(struct bpf_program *prog);
236LIBBPF_API int bpf_program__set_perf_event(struct bpf_program *prog);
237LIBBPF_API void bpf_program__set_type(struct bpf_program *prog,
238 enum bpf_prog_type type);
239LIBBPF_API void
240bpf_program__set_expected_attach_type(struct bpf_program *prog,
241 enum bpf_attach_type type);
242
243LIBBPF_API bool bpf_program__is_socket_filter(struct bpf_program *prog);
244LIBBPF_API bool bpf_program__is_tracepoint(struct bpf_program *prog);
245LIBBPF_API bool bpf_program__is_raw_tracepoint(struct bpf_program *prog);
246LIBBPF_API bool bpf_program__is_kprobe(struct bpf_program *prog);
247LIBBPF_API bool bpf_program__is_sched_cls(struct bpf_program *prog);
248LIBBPF_API bool bpf_program__is_sched_act(struct bpf_program *prog);
249LIBBPF_API bool bpf_program__is_xdp(struct bpf_program *prog);
250LIBBPF_API bool bpf_program__is_perf_event(struct bpf_program *prog);
5f44e4c8 251
34090915 252/*
2eb57bb8
JK
253 * No need for __attribute__((packed)), all members of 'bpf_map_def'
254 * are all aligned. In addition, using __attribute__((packed))
255 * would trigger a -Wpacked warning message, and lead to an error
256 * if -Werror is set.
34090915
WN
257 */
258struct bpf_map_def {
259 unsigned int type;
260 unsigned int key_size;
261 unsigned int value_size;
262 unsigned int max_entries;
fe9b5f77 263 unsigned int map_flags;
34090915
WN
264};
265
9d759a9b 266/*
2eb57bb8
JK
267 * The 'struct bpf_map' in include/linux/bpf.h is internal to the kernel,
268 * so no need to worry about a name clash.
9d759a9b
WN
269 */
270struct bpf_map;
ab9e0848 271LIBBPF_API struct bpf_map *
a7fe0450 272bpf_object__find_map_by_name(struct bpf_object *obj, const char *name);
9d759a9b 273
f3cea32d
MF
274LIBBPF_API int
275bpf_object__find_map_fd_by_name(struct bpf_object *obj, const char *name);
276
5a6acad1
WN
277/*
278 * Get bpf_map through the offset of corresponding struct bpf_map_def
2eb57bb8 279 * in the BPF object file.
5a6acad1 280 */
ab9e0848 281LIBBPF_API struct bpf_map *
5a6acad1
WN
282bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset);
283
ab9e0848 284LIBBPF_API struct bpf_map *
9d759a9b 285bpf_map__next(struct bpf_map *map, struct bpf_object *obj);
f74a53d9 286#define bpf_object__for_each_map(pos, obj) \
9d759a9b
WN
287 for ((pos) = bpf_map__next(NULL, (obj)); \
288 (pos) != NULL; \
289 (pos) = bpf_map__next((pos), (obj)))
f74a53d9 290#define bpf_map__for_each bpf_object__for_each_map
9d759a9b 291
0c19a9fb
SF
292LIBBPF_API struct bpf_map *
293bpf_map__prev(struct bpf_map *map, struct bpf_object *obj);
294
ab9e0848
AI
295LIBBPF_API int bpf_map__fd(struct bpf_map *map);
296LIBBPF_API const struct bpf_map_def *bpf_map__def(struct bpf_map *map);
297LIBBPF_API const char *bpf_map__name(struct bpf_map *map);
298LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map);
299LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map);
9d759a9b
WN
300
301typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
ab9e0848
AI
302LIBBPF_API int bpf_map__set_priv(struct bpf_map *map, void *priv,
303 bpf_map_clear_priv_t clear_priv);
304LIBBPF_API void *bpf_map__priv(struct bpf_map *map);
305LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);
1a11a4c7 306LIBBPF_API int bpf_map__resize(struct bpf_map *map, __u32 max_entries);
ab9e0848 307LIBBPF_API bool bpf_map__is_offload_neutral(struct bpf_map *map);
d859900c 308LIBBPF_API bool bpf_map__is_internal(struct bpf_map *map);
ab9e0848
AI
309LIBBPF_API void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
310LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);
0c19a9fb 311LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path);
9d759a9b 312
addb9fc9
NS
313LIBBPF_API int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd);
314
ab9e0848 315LIBBPF_API long libbpf_get_error(const void *ptr);
e28ff1a8 316
d7be143b
AI
317struct bpf_prog_load_attr {
318 const char *file;
319 enum bpf_prog_type prog_type;
320 enum bpf_attach_type expected_attach_type;
f0307a7e 321 int ifindex;
da11b417 322 int log_level;
04656198 323 int prog_flags;
d7be143b
AI
324};
325
ab9e0848
AI
326LIBBPF_API int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
327 struct bpf_object **pobj, int *prog_fd);
328LIBBPF_API int bpf_prog_load(const char *file, enum bpf_prog_type type,
329 struct bpf_object **pobj, int *prog_fd);
949abbe8 330
ab9e0848 331LIBBPF_API int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags);
50db9f07 332LIBBPF_API int bpf_get_link_xdp_id(int ifindex, __u32 *prog_id, __u32 flags);
d0cabbb0
JK
333
334enum bpf_perf_event_ret {
335 LIBBPF_PERF_EVENT_DONE = 0,
336 LIBBPF_PERF_EVENT_ERROR = -1,
337 LIBBPF_PERF_EVENT_CONT = -2,
338};
339
3dca2115
DB
340struct perf_event_header;
341typedef enum bpf_perf_event_ret
342 (*bpf_perf_event_print_t)(struct perf_event_header *hdr,
343 void *private_data);
344LIBBPF_API enum bpf_perf_event_ret
345bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
346 void **copy_mem, size_t *copy_size,
347 bpf_perf_event_print_t fn, void *private_data);
36f1678d 348
36f1678d 349struct nlattr;
aae57780
AI
350typedef int (*libbpf_dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb);
351int libbpf_netlink_open(unsigned int *nl_pid);
352int libbpf_nl_get_link(int sock, unsigned int nl_pid,
353 libbpf_dump_nlmsg_t dump_link_nlmsg, void *cookie);
354int libbpf_nl_get_class(int sock, unsigned int nl_pid, int ifindex,
355 libbpf_dump_nlmsg_t dump_class_nlmsg, void *cookie);
356int libbpf_nl_get_qdisc(int sock, unsigned int nl_pid, int ifindex,
357 libbpf_dump_nlmsg_t dump_qdisc_nlmsg, void *cookie);
358int libbpf_nl_get_filter(int sock, unsigned int nl_pid, int ifindex, int handle,
359 libbpf_dump_nlmsg_t dump_filter_nlmsg, void *cookie);
8c4905b9 360
b053b439
MKL
361struct bpf_prog_linfo;
362struct bpf_prog_info;
363
364LIBBPF_API void bpf_prog_linfo__free(struct bpf_prog_linfo *prog_linfo);
365LIBBPF_API struct bpf_prog_linfo *
366bpf_prog_linfo__new(const struct bpf_prog_info *info);
367LIBBPF_API const struct bpf_line_info *
368bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo,
369 __u64 addr, __u32 func_idx, __u32 nr_skip);
370LIBBPF_API const struct bpf_line_info *
371bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo,
372 __u32 insn_off, __u32 nr_skip);
373
1bf4b058
QM
374/*
375 * Probe for supported system features
376 *
377 * Note that running many of these probes in a short amount of time can cause
378 * the kernel to reach the maximal size of lockable memory allowed for the
379 * user, causing subsequent probes to fail. In this case, the caller may want
380 * to adjust that limit with setrlimit().
381 */
382LIBBPF_API bool bpf_probe_prog_type(enum bpf_prog_type prog_type,
383 __u32 ifindex);
f99e1663 384LIBBPF_API bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex);
2d3ea5e8
QM
385LIBBPF_API bool bpf_probe_helper(enum bpf_func_id id,
386 enum bpf_prog_type prog_type, __u32 ifindex);
1bf4b058 387
34be1646
SL
388/*
389 * Get bpf_prog_info in continuous memory
390 *
391 * struct bpf_prog_info has multiple arrays. The user has option to choose
392 * arrays to fetch from kernel. The following APIs provide an uniform way to
393 * fetch these data. All arrays in bpf_prog_info are stored in a single
394 * continuous memory region. This makes it easy to store the info in a
395 * file.
396 *
397 * Before writing bpf_prog_info_linear to files, it is necessary to
398 * translate pointers in bpf_prog_info to offsets. Helper functions
399 * bpf_program__bpil_addr_to_offs() and bpf_program__bpil_offs_to_addr()
400 * are introduced to switch between pointers and offsets.
401 *
402 * Examples:
403 * # To fetch map_ids and prog_tags:
404 * __u64 arrays = (1UL << BPF_PROG_INFO_MAP_IDS) |
405 * (1UL << BPF_PROG_INFO_PROG_TAGS);
406 * struct bpf_prog_info_linear *info_linear =
407 * bpf_program__get_prog_info_linear(fd, arrays);
408 *
409 * # To save data in file
410 * bpf_program__bpil_addr_to_offs(info_linear);
411 * write(f, info_linear, sizeof(*info_linear) + info_linear->data_len);
412 *
413 * # To read data from file
414 * read(f, info_linear, <proper_size>);
415 * bpf_program__bpil_offs_to_addr(info_linear);
416 */
417enum bpf_prog_info_array {
418 BPF_PROG_INFO_FIRST_ARRAY = 0,
419 BPF_PROG_INFO_JITED_INSNS = 0,
420 BPF_PROG_INFO_XLATED_INSNS,
421 BPF_PROG_INFO_MAP_IDS,
422 BPF_PROG_INFO_JITED_KSYMS,
423 BPF_PROG_INFO_JITED_FUNC_LENS,
424 BPF_PROG_INFO_FUNC_INFO,
425 BPF_PROG_INFO_LINE_INFO,
426 BPF_PROG_INFO_JITED_LINE_INFO,
427 BPF_PROG_INFO_PROG_TAGS,
428 BPF_PROG_INFO_LAST_ARRAY,
429};
430
431struct bpf_prog_info_linear {
432 /* size of struct bpf_prog_info, when the tool is compiled */
433 __u32 info_len;
434 /* total bytes allocated for data, round up to 8 bytes */
435 __u32 data_len;
436 /* which arrays are included in data */
437 __u64 arrays;
438 struct bpf_prog_info info;
439 __u8 data[];
440};
441
442LIBBPF_API struct bpf_prog_info_linear *
443bpf_program__get_prog_info_linear(int fd, __u64 arrays);
444
445LIBBPF_API void
446bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear *info_linear);
447
448LIBBPF_API void
449bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear);
450
8c4905b9
SF
451#ifdef __cplusplus
452} /* extern "C" */
453#endif
454
eff81908 455#endif /* __LIBBPF_LIBBPF_H */