]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - tools/lib/bpf/libbpf.h
libbpf: Add BPF_EMBED_OBJ macro for embedding BPF .o files
[mirror_ubuntu-hirsute-kernel.git] / tools / lib / bpf / libbpf.h
1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2
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 */
10 #ifndef __LIBBPF_LIBBPF_H
11 #define __LIBBPF_LIBBPF_H
12
13 #include <stdarg.h>
14 #include <stdio.h>
15 #include <stdint.h>
16 #include <stdbool.h>
17 #include <sys/types.h> // for size_t
18 #include <linux/bpf.h>
19
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23
24 #ifndef LIBBPF_API
25 #define LIBBPF_API __attribute__((visibility("default")))
26 #endif
27
28 enum 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 */
35 LIBBPF_ERRNO__ENDIAN, /* Endian mismatch */
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 */
42 LIBBPF_ERRNO__PROGTYPE, /* Kernel doesn't support this program type */
43 LIBBPF_ERRNO__WRNGPID, /* Wrong pid in netlink message */
44 LIBBPF_ERRNO__INVSEQ, /* Invalid netlink sequence */
45 LIBBPF_ERRNO__NLPARSE, /* netlink parsing error */
46 __LIBBPF_ERRNO__END,
47 };
48
49 LIBBPF_API int libbpf_strerror(int err, char *buf, size_t size);
50
51 enum libbpf_print_level {
52 LIBBPF_WARN,
53 LIBBPF_INFO,
54 LIBBPF_DEBUG,
55 };
56
57 typedef int (*libbpf_print_fn_t)(enum libbpf_print_level level,
58 const char *, va_list ap);
59
60 LIBBPF_API libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn);
61
62 /* Hide internal to user */
63 struct bpf_object;
64
65 struct bpf_object_open_attr {
66 const char *file;
67 enum bpf_prog_type prog_type;
68 };
69
70 /* Helper macro to declare and initialize libbpf options struct
71 *
72 * This dance with uninitialized declaration, followed by memset to zero,
73 * followed by assignment using compound literal syntax is done to preserve
74 * ability to use a nice struct field initialization syntax and **hopefully**
75 * have all the padding bytes initialized to zero. It's not guaranteed though,
76 * when copying literal, that compiler won't copy garbage in literal's padding
77 * bytes, but that's the best way I've found and it seems to work in practice.
78 *
79 * Macro declares opts struct of given type and name, zero-initializes,
80 * including any extra padding, it with memset() and then assigns initial
81 * values provided by users in struct initializer-syntax as varargs.
82 */
83 #define DECLARE_LIBBPF_OPTS(TYPE, NAME, ...) \
84 struct TYPE NAME = ({ \
85 memset(&NAME, 0, sizeof(struct TYPE)); \
86 (struct TYPE) { \
87 .sz = sizeof(struct TYPE), \
88 __VA_ARGS__ \
89 }; \
90 })
91
92 struct bpf_object_open_opts {
93 /* size of this struct, for forward/backward compatiblity */
94 size_t sz;
95 /* object name override, if provided:
96 * - for object open from file, this will override setting object
97 * name from file path's base name;
98 * - for object open from memory buffer, this will specify an object
99 * name and will override default "<addr>-<buf-size>" name;
100 */
101 const char *object_name;
102 /* parse map definitions non-strictly, allowing extra attributes/data */
103 bool relaxed_maps;
104 /* process CO-RE relocations non-strictly, allowing them to fail */
105 bool relaxed_core_relocs;
106 /* maps that set the 'pinning' attribute in their definition will have
107 * their pin_path attribute set to a file in this directory, and be
108 * auto-pinned to that path on load; defaults to "/sys/fs/bpf".
109 */
110 const char *pin_root_path;
111 __u32 attach_prog_fd;
112 };
113 #define bpf_object_open_opts__last_field attach_prog_fd
114
115 LIBBPF_API struct bpf_object *bpf_object__open(const char *path);
116 LIBBPF_API struct bpf_object *
117 bpf_object__open_file(const char *path, struct bpf_object_open_opts *opts);
118 LIBBPF_API struct bpf_object *
119 bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz,
120 struct bpf_object_open_opts *opts);
121
122 /* deprecated bpf_object__open variants */
123 LIBBPF_API struct bpf_object *
124 bpf_object__open_buffer(const void *obj_buf, size_t obj_buf_sz,
125 const char *name);
126 LIBBPF_API struct bpf_object *
127 bpf_object__open_xattr(struct bpf_object_open_attr *attr);
128
129 enum libbpf_pin_type {
130 LIBBPF_PIN_NONE,
131 /* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */
132 LIBBPF_PIN_BY_NAME,
133 };
134
135 /* pin_maps and unpin_maps can both be called with a NULL path, in which case
136 * they will use the pin_path attribute of each map (and ignore all maps that
137 * don't have a pin_path set).
138 */
139 LIBBPF_API int bpf_object__pin_maps(struct bpf_object *obj, const char *path);
140 LIBBPF_API int bpf_object__unpin_maps(struct bpf_object *obj,
141 const char *path);
142 LIBBPF_API int bpf_object__pin_programs(struct bpf_object *obj,
143 const char *path);
144 LIBBPF_API int bpf_object__unpin_programs(struct bpf_object *obj,
145 const char *path);
146 LIBBPF_API int bpf_object__pin(struct bpf_object *object, const char *path);
147 LIBBPF_API void bpf_object__close(struct bpf_object *object);
148
149 struct bpf_object_load_attr {
150 struct bpf_object *obj;
151 int log_level;
152 const char *target_btf_path;
153 };
154
155 /* Load/unload object into/from kernel */
156 LIBBPF_API int bpf_object__load(struct bpf_object *obj);
157 LIBBPF_API int bpf_object__load_xattr(struct bpf_object_load_attr *attr);
158 LIBBPF_API int bpf_object__unload(struct bpf_object *obj);
159 LIBBPF_API const char *bpf_object__name(const struct bpf_object *obj);
160 LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj);
161
162 struct btf;
163 LIBBPF_API struct btf *bpf_object__btf(const struct bpf_object *obj);
164 LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj);
165
166 LIBBPF_API struct bpf_program *
167 bpf_object__find_program_by_title(const struct bpf_object *obj,
168 const char *title);
169
170 LIBBPF_API struct bpf_object *bpf_object__next(struct bpf_object *prev);
171 #define bpf_object__for_each_safe(pos, tmp) \
172 for ((pos) = bpf_object__next(NULL), \
173 (tmp) = bpf_object__next(pos); \
174 (pos) != NULL; \
175 (pos) = (tmp), (tmp) = bpf_object__next(tmp))
176
177 typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *);
178 LIBBPF_API int bpf_object__set_priv(struct bpf_object *obj, void *priv,
179 bpf_object_clear_priv_t clear_priv);
180 LIBBPF_API void *bpf_object__priv(const struct bpf_object *prog);
181
182 LIBBPF_API int
183 libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
184 enum bpf_attach_type *expected_attach_type);
185 LIBBPF_API int libbpf_attach_type_by_name(const char *name,
186 enum bpf_attach_type *attach_type);
187 LIBBPF_API int libbpf_find_vmlinux_btf_id(const char *name,
188 enum bpf_attach_type attach_type);
189
190 /* Accessors of bpf_program */
191 struct bpf_program;
192 LIBBPF_API struct bpf_program *bpf_program__next(struct bpf_program *prog,
193 const struct bpf_object *obj);
194
195 #define bpf_object__for_each_program(pos, obj) \
196 for ((pos) = bpf_program__next(NULL, (obj)); \
197 (pos) != NULL; \
198 (pos) = bpf_program__next((pos), (obj)))
199
200 LIBBPF_API struct bpf_program *bpf_program__prev(struct bpf_program *prog,
201 const struct bpf_object *obj);
202
203 typedef void (*bpf_program_clear_priv_t)(struct bpf_program *, void *);
204
205 LIBBPF_API int bpf_program__set_priv(struct bpf_program *prog, void *priv,
206 bpf_program_clear_priv_t clear_priv);
207
208 LIBBPF_API void *bpf_program__priv(const struct bpf_program *prog);
209 LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,
210 __u32 ifindex);
211
212 LIBBPF_API const char *bpf_program__title(const struct bpf_program *prog,
213 bool needs_copy);
214
215 /* returns program size in bytes */
216 LIBBPF_API size_t bpf_program__size(const struct bpf_program *prog);
217
218 LIBBPF_API int bpf_program__load(struct bpf_program *prog, char *license,
219 __u32 kern_version);
220 LIBBPF_API int bpf_program__fd(const struct bpf_program *prog);
221 LIBBPF_API int bpf_program__pin_instance(struct bpf_program *prog,
222 const char *path,
223 int instance);
224 LIBBPF_API int bpf_program__unpin_instance(struct bpf_program *prog,
225 const char *path,
226 int instance);
227 LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path);
228 LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path);
229 LIBBPF_API void bpf_program__unload(struct bpf_program *prog);
230
231 struct bpf_link;
232
233 LIBBPF_API int bpf_link__destroy(struct bpf_link *link);
234
235 LIBBPF_API struct bpf_link *
236 bpf_program__attach(struct bpf_program *prog);
237 LIBBPF_API struct bpf_link *
238 bpf_program__attach_perf_event(struct bpf_program *prog, int pfd);
239 LIBBPF_API struct bpf_link *
240 bpf_program__attach_kprobe(struct bpf_program *prog, bool retprobe,
241 const char *func_name);
242 LIBBPF_API struct bpf_link *
243 bpf_program__attach_uprobe(struct bpf_program *prog, bool retprobe,
244 pid_t pid, const char *binary_path,
245 size_t func_offset);
246 LIBBPF_API struct bpf_link *
247 bpf_program__attach_tracepoint(struct bpf_program *prog,
248 const char *tp_category,
249 const char *tp_name);
250 LIBBPF_API struct bpf_link *
251 bpf_program__attach_raw_tracepoint(struct bpf_program *prog,
252 const char *tp_name);
253
254 LIBBPF_API struct bpf_link *
255 bpf_program__attach_trace(struct bpf_program *prog);
256 struct bpf_insn;
257
258 /*
259 * Libbpf allows callers to adjust BPF programs before being loaded
260 * into kernel. One program in an object file can be transformed into
261 * multiple variants to be attached to different hooks.
262 *
263 * bpf_program_prep_t, bpf_program__set_prep and bpf_program__nth_fd
264 * form an API for this purpose.
265 *
266 * - bpf_program_prep_t:
267 * Defines a 'preprocessor', which is a caller defined function
268 * passed to libbpf through bpf_program__set_prep(), and will be
269 * called before program is loaded. The processor should adjust
270 * the program one time for each instance according to the instance id
271 * passed to it.
272 *
273 * - bpf_program__set_prep:
274 * Attaches a preprocessor to a BPF program. The number of instances
275 * that should be created is also passed through this function.
276 *
277 * - bpf_program__nth_fd:
278 * After the program is loaded, get resulting FD of a given instance
279 * of the BPF program.
280 *
281 * If bpf_program__set_prep() is not used, the program would be loaded
282 * without adjustment during bpf_object__load(). The program has only
283 * one instance. In this case bpf_program__fd(prog) is equal to
284 * bpf_program__nth_fd(prog, 0).
285 */
286
287 struct bpf_prog_prep_result {
288 /*
289 * If not NULL, load new instruction array.
290 * If set to NULL, don't load this instance.
291 */
292 struct bpf_insn *new_insn_ptr;
293 int new_insn_cnt;
294
295 /* If not NULL, result FD is written to it. */
296 int *pfd;
297 };
298
299 /*
300 * Parameters of bpf_program_prep_t:
301 * - prog: The bpf_program being loaded.
302 * - n: Index of instance being generated.
303 * - insns: BPF instructions array.
304 * - insns_cnt:Number of instructions in insns.
305 * - res: Output parameter, result of transformation.
306 *
307 * Return value:
308 * - Zero: pre-processing success.
309 * - Non-zero: pre-processing error, stop loading.
310 */
311 typedef int (*bpf_program_prep_t)(struct bpf_program *prog, int n,
312 struct bpf_insn *insns, int insns_cnt,
313 struct bpf_prog_prep_result *res);
314
315 LIBBPF_API int bpf_program__set_prep(struct bpf_program *prog, int nr_instance,
316 bpf_program_prep_t prep);
317
318 LIBBPF_API int bpf_program__nth_fd(const struct bpf_program *prog, int n);
319
320 /*
321 * Adjust type of BPF program. Default is kprobe.
322 */
323 LIBBPF_API int bpf_program__set_socket_filter(struct bpf_program *prog);
324 LIBBPF_API int bpf_program__set_tracepoint(struct bpf_program *prog);
325 LIBBPF_API int bpf_program__set_raw_tracepoint(struct bpf_program *prog);
326 LIBBPF_API int bpf_program__set_kprobe(struct bpf_program *prog);
327 LIBBPF_API int bpf_program__set_sched_cls(struct bpf_program *prog);
328 LIBBPF_API int bpf_program__set_sched_act(struct bpf_program *prog);
329 LIBBPF_API int bpf_program__set_xdp(struct bpf_program *prog);
330 LIBBPF_API int bpf_program__set_perf_event(struct bpf_program *prog);
331 LIBBPF_API int bpf_program__set_tracing(struct bpf_program *prog);
332
333 LIBBPF_API enum bpf_prog_type bpf_program__get_type(struct bpf_program *prog);
334 LIBBPF_API void bpf_program__set_type(struct bpf_program *prog,
335 enum bpf_prog_type type);
336
337 LIBBPF_API enum bpf_attach_type
338 bpf_program__get_expected_attach_type(struct bpf_program *prog);
339 LIBBPF_API void
340 bpf_program__set_expected_attach_type(struct bpf_program *prog,
341 enum bpf_attach_type type);
342
343 LIBBPF_API bool bpf_program__is_socket_filter(const struct bpf_program *prog);
344 LIBBPF_API bool bpf_program__is_tracepoint(const struct bpf_program *prog);
345 LIBBPF_API bool bpf_program__is_raw_tracepoint(const struct bpf_program *prog);
346 LIBBPF_API bool bpf_program__is_kprobe(const struct bpf_program *prog);
347 LIBBPF_API bool bpf_program__is_sched_cls(const struct bpf_program *prog);
348 LIBBPF_API bool bpf_program__is_sched_act(const struct bpf_program *prog);
349 LIBBPF_API bool bpf_program__is_xdp(const struct bpf_program *prog);
350 LIBBPF_API bool bpf_program__is_perf_event(const struct bpf_program *prog);
351 LIBBPF_API bool bpf_program__is_tracing(const struct bpf_program *prog);
352
353 /*
354 * No need for __attribute__((packed)), all members of 'bpf_map_def'
355 * are all aligned. In addition, using __attribute__((packed))
356 * would trigger a -Wpacked warning message, and lead to an error
357 * if -Werror is set.
358 */
359 struct bpf_map_def {
360 unsigned int type;
361 unsigned int key_size;
362 unsigned int value_size;
363 unsigned int max_entries;
364 unsigned int map_flags;
365 };
366
367 /*
368 * The 'struct bpf_map' in include/linux/bpf.h is internal to the kernel,
369 * so no need to worry about a name clash.
370 */
371 struct bpf_map;
372 LIBBPF_API struct bpf_map *
373 bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name);
374
375 LIBBPF_API int
376 bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name);
377
378 /*
379 * Get bpf_map through the offset of corresponding struct bpf_map_def
380 * in the BPF object file.
381 */
382 LIBBPF_API struct bpf_map *
383 bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset);
384
385 LIBBPF_API struct bpf_map *
386 bpf_map__next(const struct bpf_map *map, const struct bpf_object *obj);
387 #define bpf_object__for_each_map(pos, obj) \
388 for ((pos) = bpf_map__next(NULL, (obj)); \
389 (pos) != NULL; \
390 (pos) = bpf_map__next((pos), (obj)))
391 #define bpf_map__for_each bpf_object__for_each_map
392
393 LIBBPF_API struct bpf_map *
394 bpf_map__prev(const struct bpf_map *map, const struct bpf_object *obj);
395
396 LIBBPF_API int bpf_map__fd(const struct bpf_map *map);
397 LIBBPF_API const struct bpf_map_def *bpf_map__def(const struct bpf_map *map);
398 LIBBPF_API const char *bpf_map__name(const struct bpf_map *map);
399 LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map);
400 LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map);
401
402 typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
403 LIBBPF_API int bpf_map__set_priv(struct bpf_map *map, void *priv,
404 bpf_map_clear_priv_t clear_priv);
405 LIBBPF_API void *bpf_map__priv(const struct bpf_map *map);
406 LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);
407 LIBBPF_API int bpf_map__resize(struct bpf_map *map, __u32 max_entries);
408 LIBBPF_API bool bpf_map__is_offload_neutral(const struct bpf_map *map);
409 LIBBPF_API bool bpf_map__is_internal(const struct bpf_map *map);
410 LIBBPF_API void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
411 LIBBPF_API int bpf_map__set_pin_path(struct bpf_map *map, const char *path);
412 LIBBPF_API const char *bpf_map__get_pin_path(const struct bpf_map *map);
413 LIBBPF_API bool bpf_map__is_pinned(const struct bpf_map *map);
414 LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);
415 LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path);
416
417 LIBBPF_API int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd);
418
419 LIBBPF_API long libbpf_get_error(const void *ptr);
420
421 struct bpf_prog_load_attr {
422 const char *file;
423 enum bpf_prog_type prog_type;
424 enum bpf_attach_type expected_attach_type;
425 int ifindex;
426 int log_level;
427 int prog_flags;
428 };
429
430 LIBBPF_API int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
431 struct bpf_object **pobj, int *prog_fd);
432 LIBBPF_API int bpf_prog_load(const char *file, enum bpf_prog_type type,
433 struct bpf_object **pobj, int *prog_fd);
434
435 struct xdp_link_info {
436 __u32 prog_id;
437 __u32 drv_prog_id;
438 __u32 hw_prog_id;
439 __u32 skb_prog_id;
440 __u8 attach_mode;
441 };
442
443 LIBBPF_API int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags);
444 LIBBPF_API int bpf_get_link_xdp_id(int ifindex, __u32 *prog_id, __u32 flags);
445 LIBBPF_API int bpf_get_link_xdp_info(int ifindex, struct xdp_link_info *info,
446 size_t info_size, __u32 flags);
447
448 struct perf_buffer;
449
450 typedef void (*perf_buffer_sample_fn)(void *ctx, int cpu,
451 void *data, __u32 size);
452 typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt);
453
454 /* common use perf buffer options */
455 struct perf_buffer_opts {
456 /* if specified, sample_cb is called for each sample */
457 perf_buffer_sample_fn sample_cb;
458 /* if specified, lost_cb is called for each batch of lost samples */
459 perf_buffer_lost_fn lost_cb;
460 /* ctx is provided to sample_cb and lost_cb */
461 void *ctx;
462 };
463
464 LIBBPF_API struct perf_buffer *
465 perf_buffer__new(int map_fd, size_t page_cnt,
466 const struct perf_buffer_opts *opts);
467
468 enum bpf_perf_event_ret {
469 LIBBPF_PERF_EVENT_DONE = 0,
470 LIBBPF_PERF_EVENT_ERROR = -1,
471 LIBBPF_PERF_EVENT_CONT = -2,
472 };
473
474 struct perf_event_header;
475
476 typedef enum bpf_perf_event_ret
477 (*perf_buffer_event_fn)(void *ctx, int cpu, struct perf_event_header *event);
478
479 /* raw perf buffer options, giving most power and control */
480 struct perf_buffer_raw_opts {
481 /* perf event attrs passed directly into perf_event_open() */
482 struct perf_event_attr *attr;
483 /* raw event callback */
484 perf_buffer_event_fn event_cb;
485 /* ctx is provided to event_cb */
486 void *ctx;
487 /* if cpu_cnt == 0, open all on all possible CPUs (up to the number of
488 * max_entries of given PERF_EVENT_ARRAY map)
489 */
490 int cpu_cnt;
491 /* if cpu_cnt > 0, cpus is an array of CPUs to open ring buffers on */
492 int *cpus;
493 /* if cpu_cnt > 0, map_keys specify map keys to set per-CPU FDs for */
494 int *map_keys;
495 };
496
497 LIBBPF_API struct perf_buffer *
498 perf_buffer__new_raw(int map_fd, size_t page_cnt,
499 const struct perf_buffer_raw_opts *opts);
500
501 LIBBPF_API void perf_buffer__free(struct perf_buffer *pb);
502 LIBBPF_API int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms);
503
504 typedef enum bpf_perf_event_ret
505 (*bpf_perf_event_print_t)(struct perf_event_header *hdr,
506 void *private_data);
507 LIBBPF_API enum bpf_perf_event_ret
508 bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
509 void **copy_mem, size_t *copy_size,
510 bpf_perf_event_print_t fn, void *private_data);
511
512 struct bpf_prog_linfo;
513 struct bpf_prog_info;
514
515 LIBBPF_API void bpf_prog_linfo__free(struct bpf_prog_linfo *prog_linfo);
516 LIBBPF_API struct bpf_prog_linfo *
517 bpf_prog_linfo__new(const struct bpf_prog_info *info);
518 LIBBPF_API const struct bpf_line_info *
519 bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo,
520 __u64 addr, __u32 func_idx, __u32 nr_skip);
521 LIBBPF_API const struct bpf_line_info *
522 bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo,
523 __u32 insn_off, __u32 nr_skip);
524
525 /*
526 * Probe for supported system features
527 *
528 * Note that running many of these probes in a short amount of time can cause
529 * the kernel to reach the maximal size of lockable memory allowed for the
530 * user, causing subsequent probes to fail. In this case, the caller may want
531 * to adjust that limit with setrlimit().
532 */
533 LIBBPF_API bool bpf_probe_prog_type(enum bpf_prog_type prog_type,
534 __u32 ifindex);
535 LIBBPF_API bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex);
536 LIBBPF_API bool bpf_probe_helper(enum bpf_func_id id,
537 enum bpf_prog_type prog_type, __u32 ifindex);
538
539 /*
540 * Get bpf_prog_info in continuous memory
541 *
542 * struct bpf_prog_info has multiple arrays. The user has option to choose
543 * arrays to fetch from kernel. The following APIs provide an uniform way to
544 * fetch these data. All arrays in bpf_prog_info are stored in a single
545 * continuous memory region. This makes it easy to store the info in a
546 * file.
547 *
548 * Before writing bpf_prog_info_linear to files, it is necessary to
549 * translate pointers in bpf_prog_info to offsets. Helper functions
550 * bpf_program__bpil_addr_to_offs() and bpf_program__bpil_offs_to_addr()
551 * are introduced to switch between pointers and offsets.
552 *
553 * Examples:
554 * # To fetch map_ids and prog_tags:
555 * __u64 arrays = (1UL << BPF_PROG_INFO_MAP_IDS) |
556 * (1UL << BPF_PROG_INFO_PROG_TAGS);
557 * struct bpf_prog_info_linear *info_linear =
558 * bpf_program__get_prog_info_linear(fd, arrays);
559 *
560 * # To save data in file
561 * bpf_program__bpil_addr_to_offs(info_linear);
562 * write(f, info_linear, sizeof(*info_linear) + info_linear->data_len);
563 *
564 * # To read data from file
565 * read(f, info_linear, <proper_size>);
566 * bpf_program__bpil_offs_to_addr(info_linear);
567 */
568 enum bpf_prog_info_array {
569 BPF_PROG_INFO_FIRST_ARRAY = 0,
570 BPF_PROG_INFO_JITED_INSNS = 0,
571 BPF_PROG_INFO_XLATED_INSNS,
572 BPF_PROG_INFO_MAP_IDS,
573 BPF_PROG_INFO_JITED_KSYMS,
574 BPF_PROG_INFO_JITED_FUNC_LENS,
575 BPF_PROG_INFO_FUNC_INFO,
576 BPF_PROG_INFO_LINE_INFO,
577 BPF_PROG_INFO_JITED_LINE_INFO,
578 BPF_PROG_INFO_PROG_TAGS,
579 BPF_PROG_INFO_LAST_ARRAY,
580 };
581
582 struct bpf_prog_info_linear {
583 /* size of struct bpf_prog_info, when the tool is compiled */
584 __u32 info_len;
585 /* total bytes allocated for data, round up to 8 bytes */
586 __u32 data_len;
587 /* which arrays are included in data */
588 __u64 arrays;
589 struct bpf_prog_info info;
590 __u8 data[];
591 };
592
593 LIBBPF_API struct bpf_prog_info_linear *
594 bpf_program__get_prog_info_linear(int fd, __u64 arrays);
595
596 LIBBPF_API void
597 bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear *info_linear);
598
599 LIBBPF_API void
600 bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear);
601
602 /*
603 * A helper function to get the number of possible CPUs before looking up
604 * per-CPU maps. Negative errno is returned on failure.
605 *
606 * Example usage:
607 *
608 * int ncpus = libbpf_num_possible_cpus();
609 * if (ncpus < 0) {
610 * // error handling
611 * }
612 * long values[ncpus];
613 * bpf_map_lookup_elem(per_cpu_map_fd, key, values);
614 *
615 */
616 LIBBPF_API int libbpf_num_possible_cpus(void);
617
618 struct bpf_embed_data {
619 void *data;
620 size_t size;
621 };
622
623 #define BPF_EMBED_OBJ_DECLARE(NAME) \
624 extern struct bpf_embed_data NAME##_embed; \
625 extern char NAME##_data[]; \
626 extern char NAME##_data_end[];
627
628 #define __BPF_EMBED_OBJ(NAME, PATH, SZ, ASM_TYPE) \
629 asm ( \
630 " .pushsection \".rodata\", \"a\", @progbits \n" \
631 " .global "#NAME"_data \n" \
632 #NAME"_data: \n" \
633 " .incbin \"" PATH "\" \n" \
634 " .global "#NAME"_data_end \n" \
635 #NAME"_data_end: \n" \
636 " .global "#NAME"_embed \n" \
637 " .type "#NAME"_embed, @object \n" \
638 " .size "#NAME"_size, "#SZ" \n" \
639 " .align 8, \n" \
640 #NAME"_embed: \n" \
641 " "ASM_TYPE" "#NAME"_data \n" \
642 " "ASM_TYPE" "#NAME"_data_end - "#NAME"_data \n" \
643 " .popsection \n" \
644 ); \
645 BPF_EMBED_OBJ_DECLARE(NAME)
646
647 #if __SIZEOF_POINTER__ == 4
648 #define BPF_EMBED_OBJ(NAME, PATH) __BPF_EMBED_OBJ(NAME, PATH, 8, ".long")
649 #else
650 #define BPF_EMBED_OBJ(NAME, PATH) __BPF_EMBED_OBJ(NAME, PATH, 16, ".quad")
651 #endif
652
653 #ifdef __cplusplus
654 } /* extern "C" */
655 #endif
656
657 #endif /* __LIBBPF_LIBBPF_H */