Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/tools/lib/bpf/libbpf.h
26285 views
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 <[email protected]>
7
* Copyright (C) 2015 Wang Nan <[email protected]>
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
#include "libbpf_common.h"
21
#include "libbpf_legacy.h"
22
23
#ifdef __cplusplus
24
extern "C" {
25
#endif
26
27
LIBBPF_API __u32 libbpf_major_version(void);
28
LIBBPF_API __u32 libbpf_minor_version(void);
29
LIBBPF_API const char *libbpf_version_string(void);
30
31
enum libbpf_errno {
32
__LIBBPF_ERRNO__START = 4000,
33
34
/* Something wrong in libelf */
35
LIBBPF_ERRNO__LIBELF = __LIBBPF_ERRNO__START,
36
LIBBPF_ERRNO__FORMAT, /* BPF object format invalid */
37
LIBBPF_ERRNO__KVERSION, /* Incorrect or no 'version' section */
38
LIBBPF_ERRNO__ENDIAN, /* Endian mismatch */
39
LIBBPF_ERRNO__INTERNAL, /* Internal error in libbpf */
40
LIBBPF_ERRNO__RELOC, /* Relocation failed */
41
LIBBPF_ERRNO__LOAD, /* Load program failure for unknown reason */
42
LIBBPF_ERRNO__VERIFY, /* Kernel verifier blocks program loading */
43
LIBBPF_ERRNO__PROG2BIG, /* Program too big */
44
LIBBPF_ERRNO__KVER, /* Incorrect kernel version */
45
LIBBPF_ERRNO__PROGTYPE, /* Kernel doesn't support this program type */
46
LIBBPF_ERRNO__WRNGPID, /* Wrong pid in netlink message */
47
LIBBPF_ERRNO__INVSEQ, /* Invalid netlink sequence */
48
LIBBPF_ERRNO__NLPARSE, /* netlink parsing error */
49
__LIBBPF_ERRNO__END,
50
};
51
52
LIBBPF_API int libbpf_strerror(int err, char *buf, size_t size);
53
54
/**
55
* @brief **libbpf_bpf_attach_type_str()** converts the provided attach type
56
* value into a textual representation.
57
* @param t The attach type.
58
* @return Pointer to a static string identifying the attach type. NULL is
59
* returned for unknown **bpf_attach_type** values.
60
*/
61
LIBBPF_API const char *libbpf_bpf_attach_type_str(enum bpf_attach_type t);
62
63
/**
64
* @brief **libbpf_bpf_link_type_str()** converts the provided link type value
65
* into a textual representation.
66
* @param t The link type.
67
* @return Pointer to a static string identifying the link type. NULL is
68
* returned for unknown **bpf_link_type** values.
69
*/
70
LIBBPF_API const char *libbpf_bpf_link_type_str(enum bpf_link_type t);
71
72
/**
73
* @brief **libbpf_bpf_map_type_str()** converts the provided map type value
74
* into a textual representation.
75
* @param t The map type.
76
* @return Pointer to a static string identifying the map type. NULL is
77
* returned for unknown **bpf_map_type** values.
78
*/
79
LIBBPF_API const char *libbpf_bpf_map_type_str(enum bpf_map_type t);
80
81
/**
82
* @brief **libbpf_bpf_prog_type_str()** converts the provided program type
83
* value into a textual representation.
84
* @param t The program type.
85
* @return Pointer to a static string identifying the program type. NULL is
86
* returned for unknown **bpf_prog_type** values.
87
*/
88
LIBBPF_API const char *libbpf_bpf_prog_type_str(enum bpf_prog_type t);
89
90
enum libbpf_print_level {
91
LIBBPF_WARN,
92
LIBBPF_INFO,
93
LIBBPF_DEBUG,
94
};
95
96
typedef int (*libbpf_print_fn_t)(enum libbpf_print_level level,
97
const char *, va_list ap);
98
99
/**
100
* @brief **libbpf_set_print()** sets user-provided log callback function to
101
* be used for libbpf warnings and informational messages. If the user callback
102
* is not set, messages are logged to stderr by default. The verbosity of these
103
* messages can be controlled by setting the environment variable
104
* LIBBPF_LOG_LEVEL to either warn, info, or debug.
105
* @param fn The log print function. If NULL, libbpf won't print anything.
106
* @return Pointer to old print function.
107
*
108
* This function is thread-safe.
109
*/
110
LIBBPF_API libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn);
111
112
/* Hide internal to user */
113
struct bpf_object;
114
115
struct bpf_object_open_opts {
116
/* size of this struct, for forward/backward compatibility */
117
size_t sz;
118
/* object name override, if provided:
119
* - for object open from file, this will override setting object
120
* name from file path's base name;
121
* - for object open from memory buffer, this will specify an object
122
* name and will override default "<addr>-<buf-size>" name;
123
*/
124
const char *object_name;
125
/* parse map definitions non-strictly, allowing extra attributes/data */
126
bool relaxed_maps;
127
/* maps that set the 'pinning' attribute in their definition will have
128
* their pin_path attribute set to a file in this directory, and be
129
* auto-pinned to that path on load; defaults to "/sys/fs/bpf".
130
*/
131
const char *pin_root_path;
132
133
__u32 :32; /* stub out now removed attach_prog_fd */
134
135
/* Additional kernel config content that augments and overrides
136
* system Kconfig for CONFIG_xxx externs.
137
*/
138
const char *kconfig;
139
/* Path to the custom BTF to be used for BPF CO-RE relocations.
140
* This custom BTF completely replaces the use of vmlinux BTF
141
* for the purpose of CO-RE relocations.
142
* NOTE: any other BPF feature (e.g., fentry/fexit programs,
143
* struct_ops, etc) will need actual kernel BTF at /sys/kernel/btf/vmlinux.
144
*/
145
const char *btf_custom_path;
146
/* Pointer to a buffer for storing kernel logs for applicable BPF
147
* commands. Valid kernel_log_size has to be specified as well and are
148
* passed-through to bpf() syscall. Keep in mind that kernel might
149
* fail operation with -ENOSPC error if provided buffer is too small
150
* to contain entire log output.
151
* See the comment below for kernel_log_level for interaction between
152
* log_buf and log_level settings.
153
*
154
* If specified, this log buffer will be passed for:
155
* - each BPF progral load (BPF_PROG_LOAD) attempt, unless overridden
156
* with bpf_program__set_log() on per-program level, to get
157
* BPF verifier log output.
158
* - during BPF object's BTF load into kernel (BPF_BTF_LOAD) to get
159
* BTF sanity checking log.
160
*
161
* Each BPF command (BPF_BTF_LOAD or BPF_PROG_LOAD) will overwrite
162
* previous contents, so if you need more fine-grained control, set
163
* per-program buffer with bpf_program__set_log_buf() to preserve each
164
* individual program's verification log. Keep using kernel_log_buf
165
* for BTF verification log, if necessary.
166
*/
167
char *kernel_log_buf;
168
size_t kernel_log_size;
169
/*
170
* Log level can be set independently from log buffer. Log_level=0
171
* means that libbpf will attempt loading BTF or program without any
172
* logging requested, but will retry with either its own or custom log
173
* buffer, if provided, and log_level=1 on any error.
174
* And vice versa, setting log_level>0 will request BTF or prog
175
* loading with verbose log from the first attempt (and as such also
176
* for successfully loaded BTF or program), and the actual log buffer
177
* could be either libbpf's own auto-allocated log buffer, if
178
* kernel_log_buffer is NULL, or user-provided custom kernel_log_buf.
179
* If user didn't provide custom log buffer, libbpf will emit captured
180
* logs through its print callback.
181
*/
182
__u32 kernel_log_level;
183
/* Path to BPF FS mount point to derive BPF token from.
184
*
185
* Created BPF token will be used for all bpf() syscall operations
186
* that accept BPF token (e.g., map creation, BTF and program loads,
187
* etc) automatically within instantiated BPF object.
188
*
189
* If bpf_token_path is not specified, libbpf will consult
190
* LIBBPF_BPF_TOKEN_PATH environment variable. If set, it will be
191
* taken as a value of bpf_token_path option and will force libbpf to
192
* either create BPF token from provided custom BPF FS path, or will
193
* disable implicit BPF token creation, if envvar value is an empty
194
* string. bpf_token_path overrides LIBBPF_BPF_TOKEN_PATH, if both are
195
* set at the same time.
196
*
197
* Setting bpf_token_path option to empty string disables libbpf's
198
* automatic attempt to create BPF token from default BPF FS mount
199
* point (/sys/fs/bpf), in case this default behavior is undesirable.
200
*/
201
const char *bpf_token_path;
202
203
size_t :0;
204
};
205
#define bpf_object_open_opts__last_field bpf_token_path
206
207
/**
208
* @brief **bpf_object__open()** creates a bpf_object by opening
209
* the BPF ELF object file pointed to by the passed path and loading it
210
* into memory.
211
* @param path BPF object file path.
212
* @return pointer to the new bpf_object; or NULL is returned on error,
213
* error code is stored in errno
214
*/
215
LIBBPF_API struct bpf_object *bpf_object__open(const char *path);
216
217
/**
218
* @brief **bpf_object__open_file()** creates a bpf_object by opening
219
* the BPF ELF object file pointed to by the passed path and loading it
220
* into memory.
221
* @param path BPF object file path
222
* @param opts options for how to load the bpf object, this parameter is
223
* optional and can be set to NULL
224
* @return pointer to the new bpf_object; or NULL is returned on error,
225
* error code is stored in errno
226
*/
227
LIBBPF_API struct bpf_object *
228
bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts);
229
230
/**
231
* @brief **bpf_object__open_mem()** creates a bpf_object by reading
232
* the BPF objects raw bytes from a memory buffer containing a valid
233
* BPF ELF object file.
234
* @param obj_buf pointer to the buffer containing ELF file bytes
235
* @param obj_buf_sz number of bytes in the buffer
236
* @param opts options for how to load the bpf object
237
* @return pointer to the new bpf_object; or NULL is returned on error,
238
* error code is stored in errno
239
*/
240
LIBBPF_API struct bpf_object *
241
bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz,
242
const struct bpf_object_open_opts *opts);
243
244
/**
245
* @brief **bpf_object__prepare()** prepares BPF object for loading:
246
* performs ELF processing, relocations, prepares final state of BPF program
247
* instructions (accessible with bpf_program__insns()), creates and
248
* (potentially) pins maps. Leaves BPF object in the state ready for program
249
* loading.
250
* @param obj Pointer to a valid BPF object instance returned by
251
* **bpf_object__open*()** API
252
* @return 0, on success; negative error code, otherwise, error code is
253
* stored in errno
254
*/
255
int bpf_object__prepare(struct bpf_object *obj);
256
257
/**
258
* @brief **bpf_object__load()** loads BPF object into kernel.
259
* @param obj Pointer to a valid BPF object instance returned by
260
* **bpf_object__open*()** APIs
261
* @return 0, on success; negative error code, otherwise, error code is
262
* stored in errno
263
*/
264
LIBBPF_API int bpf_object__load(struct bpf_object *obj);
265
266
/**
267
* @brief **bpf_object__close()** closes a BPF object and releases all
268
* resources.
269
* @param obj Pointer to a valid BPF object
270
*/
271
LIBBPF_API void bpf_object__close(struct bpf_object *obj);
272
273
/**
274
* @brief **bpf_object__pin_maps()** pins each map contained within
275
* the BPF object at the passed directory.
276
* @param obj Pointer to a valid BPF object
277
* @param path A directory where maps should be pinned.
278
* @return 0, on success; negative error code, otherwise
279
*
280
* If `path` is NULL `bpf_map__pin` (which is being used on each map)
281
* will use the pin_path attribute of each map. In this case, maps that
282
* don't have a pin_path set will be ignored.
283
*/
284
LIBBPF_API int bpf_object__pin_maps(struct bpf_object *obj, const char *path);
285
286
/**
287
* @brief **bpf_object__unpin_maps()** unpins each map contained within
288
* the BPF object found in the passed directory.
289
* @param obj Pointer to a valid BPF object
290
* @param path A directory where pinned maps should be searched for.
291
* @return 0, on success; negative error code, otherwise
292
*
293
* If `path` is NULL `bpf_map__unpin` (which is being used on each map)
294
* will use the pin_path attribute of each map. In this case, maps that
295
* don't have a pin_path set will be ignored.
296
*/
297
LIBBPF_API int bpf_object__unpin_maps(struct bpf_object *obj,
298
const char *path);
299
LIBBPF_API int bpf_object__pin_programs(struct bpf_object *obj,
300
const char *path);
301
LIBBPF_API int bpf_object__unpin_programs(struct bpf_object *obj,
302
const char *path);
303
LIBBPF_API int bpf_object__pin(struct bpf_object *object, const char *path);
304
LIBBPF_API int bpf_object__unpin(struct bpf_object *object, const char *path);
305
306
LIBBPF_API const char *bpf_object__name(const struct bpf_object *obj);
307
LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj);
308
LIBBPF_API int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version);
309
310
/**
311
* @brief **bpf_object__token_fd** is an accessor for BPF token FD associated
312
* with BPF object.
313
* @param obj Pointer to a valid BPF object
314
* @return BPF token FD or -1, if it wasn't set
315
*/
316
LIBBPF_API int bpf_object__token_fd(const struct bpf_object *obj);
317
318
struct btf;
319
LIBBPF_API struct btf *bpf_object__btf(const struct bpf_object *obj);
320
LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj);
321
322
LIBBPF_API struct bpf_program *
323
bpf_object__find_program_by_name(const struct bpf_object *obj,
324
const char *name);
325
326
LIBBPF_API int
327
libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
328
enum bpf_attach_type *expected_attach_type);
329
LIBBPF_API int libbpf_attach_type_by_name(const char *name,
330
enum bpf_attach_type *attach_type);
331
LIBBPF_API int libbpf_find_vmlinux_btf_id(const char *name,
332
enum bpf_attach_type attach_type);
333
334
/* Accessors of bpf_program */
335
struct bpf_program;
336
337
LIBBPF_API struct bpf_program *
338
bpf_object__next_program(const struct bpf_object *obj, struct bpf_program *prog);
339
340
#define bpf_object__for_each_program(pos, obj) \
341
for ((pos) = bpf_object__next_program((obj), NULL); \
342
(pos) != NULL; \
343
(pos) = bpf_object__next_program((obj), (pos)))
344
345
LIBBPF_API struct bpf_program *
346
bpf_object__prev_program(const struct bpf_object *obj, struct bpf_program *prog);
347
348
LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,
349
__u32 ifindex);
350
351
LIBBPF_API const char *bpf_program__name(const struct bpf_program *prog);
352
LIBBPF_API const char *bpf_program__section_name(const struct bpf_program *prog);
353
LIBBPF_API bool bpf_program__autoload(const struct bpf_program *prog);
354
LIBBPF_API int bpf_program__set_autoload(struct bpf_program *prog, bool autoload);
355
LIBBPF_API bool bpf_program__autoattach(const struct bpf_program *prog);
356
LIBBPF_API void bpf_program__set_autoattach(struct bpf_program *prog, bool autoattach);
357
358
struct bpf_insn;
359
360
/**
361
* @brief **bpf_program__insns()** gives read-only access to BPF program's
362
* underlying BPF instructions.
363
* @param prog BPF program for which to return instructions
364
* @return a pointer to an array of BPF instructions that belong to the
365
* specified BPF program
366
*
367
* Returned pointer is always valid and not NULL. Number of `struct bpf_insn`
368
* pointed to can be fetched using **bpf_program__insn_cnt()** API.
369
*
370
* Keep in mind, libbpf can modify and append/delete BPF program's
371
* instructions as it processes BPF object file and prepares everything for
372
* uploading into the kernel. So depending on the point in BPF object
373
* lifetime, **bpf_program__insns()** can return different sets of
374
* instructions. As an example, during BPF object load phase BPF program
375
* instructions will be CO-RE-relocated, BPF subprograms instructions will be
376
* appended, ldimm64 instructions will have FDs embedded, etc. So instructions
377
* returned before **bpf_object__load()** and after it might be quite
378
* different.
379
*/
380
LIBBPF_API const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog);
381
382
/**
383
* @brief **bpf_program__set_insns()** can set BPF program's underlying
384
* BPF instructions.
385
*
386
* WARNING: This is a very advanced libbpf API and users need to know
387
* what they are doing. This should be used from prog_prepare_load_fn
388
* callback only.
389
*
390
* @param prog BPF program for which to return instructions
391
* @param new_insns a pointer to an array of BPF instructions
392
* @param new_insn_cnt number of `struct bpf_insn`'s that form
393
* specified BPF program
394
* @return 0, on success; negative error code, otherwise
395
*/
396
LIBBPF_API int bpf_program__set_insns(struct bpf_program *prog,
397
struct bpf_insn *new_insns, size_t new_insn_cnt);
398
399
/**
400
* @brief **bpf_program__insn_cnt()** returns number of `struct bpf_insn`'s
401
* that form specified BPF program.
402
* @param prog BPF program for which to return number of BPF instructions
403
*
404
* See **bpf_program__insns()** documentation for notes on how libbpf can
405
* change instructions and their count during different phases of
406
* **bpf_object** lifetime.
407
*/
408
LIBBPF_API size_t bpf_program__insn_cnt(const struct bpf_program *prog);
409
410
LIBBPF_API int bpf_program__fd(const struct bpf_program *prog);
411
412
/**
413
* @brief **bpf_program__pin()** pins the BPF program to a file
414
* in the BPF FS specified by a path. This increments the programs
415
* reference count, allowing it to stay loaded after the process
416
* which loaded it has exited.
417
*
418
* @param prog BPF program to pin, must already be loaded
419
* @param path file path in a BPF file system
420
* @return 0, on success; negative error code, otherwise
421
*/
422
LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path);
423
424
/**
425
* @brief **bpf_program__unpin()** unpins the BPF program from a file
426
* in the BPFFS specified by a path. This decrements the programs
427
* reference count.
428
*
429
* The file pinning the BPF program can also be unlinked by a different
430
* process in which case this function will return an error.
431
*
432
* @param prog BPF program to unpin
433
* @param path file path to the pin in a BPF file system
434
* @return 0, on success; negative error code, otherwise
435
*/
436
LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path);
437
LIBBPF_API void bpf_program__unload(struct bpf_program *prog);
438
439
struct bpf_link;
440
441
LIBBPF_API struct bpf_link *bpf_link__open(const char *path);
442
LIBBPF_API int bpf_link__fd(const struct bpf_link *link);
443
LIBBPF_API const char *bpf_link__pin_path(const struct bpf_link *link);
444
/**
445
* @brief **bpf_link__pin()** pins the BPF link to a file
446
* in the BPF FS specified by a path. This increments the links
447
* reference count, allowing it to stay loaded after the process
448
* which loaded it has exited.
449
*
450
* @param link BPF link to pin, must already be loaded
451
* @param path file path in a BPF file system
452
* @return 0, on success; negative error code, otherwise
453
*/
454
455
LIBBPF_API int bpf_link__pin(struct bpf_link *link, const char *path);
456
457
/**
458
* @brief **bpf_link__unpin()** unpins the BPF link from a file
459
* in the BPFFS specified by a path. This decrements the links
460
* reference count.
461
*
462
* The file pinning the BPF link can also be unlinked by a different
463
* process in which case this function will return an error.
464
*
465
* @param prog BPF program to unpin
466
* @param path file path to the pin in a BPF file system
467
* @return 0, on success; negative error code, otherwise
468
*/
469
LIBBPF_API int bpf_link__unpin(struct bpf_link *link);
470
LIBBPF_API int bpf_link__update_program(struct bpf_link *link,
471
struct bpf_program *prog);
472
LIBBPF_API void bpf_link__disconnect(struct bpf_link *link);
473
LIBBPF_API int bpf_link__detach(struct bpf_link *link);
474
LIBBPF_API int bpf_link__destroy(struct bpf_link *link);
475
476
/**
477
* @brief **bpf_program__attach()** is a generic function for attaching
478
* a BPF program based on auto-detection of program type, attach type,
479
* and extra parameters, where applicable.
480
*
481
* @param prog BPF program to attach
482
* @return Reference to the newly created BPF link; or NULL is returned on error,
483
* error code is stored in errno
484
*
485
* This is supported for:
486
* - kprobe/kretprobe (depends on SEC() definition)
487
* - uprobe/uretprobe (depends on SEC() definition)
488
* - tracepoint
489
* - raw tracepoint
490
* - tracing programs (typed raw TP/fentry/fexit/fmod_ret)
491
*/
492
LIBBPF_API struct bpf_link *
493
bpf_program__attach(const struct bpf_program *prog);
494
495
struct bpf_perf_event_opts {
496
/* size of this struct, for forward/backward compatibility */
497
size_t sz;
498
/* custom user-provided value fetchable through bpf_get_attach_cookie() */
499
__u64 bpf_cookie;
500
/* don't use BPF link when attach BPF program */
501
bool force_ioctl_attach;
502
/* don't automatically enable the event */
503
bool dont_enable;
504
size_t :0;
505
};
506
#define bpf_perf_event_opts__last_field dont_enable
507
508
LIBBPF_API struct bpf_link *
509
bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd);
510
511
LIBBPF_API struct bpf_link *
512
bpf_program__attach_perf_event_opts(const struct bpf_program *prog, int pfd,
513
const struct bpf_perf_event_opts *opts);
514
515
/**
516
* enum probe_attach_mode - the mode to attach kprobe/uprobe
517
*
518
* force libbpf to attach kprobe/uprobe in specific mode, -ENOTSUP will
519
* be returned if it is not supported by the kernel.
520
*/
521
enum probe_attach_mode {
522
/* attach probe in latest supported mode by kernel */
523
PROBE_ATTACH_MODE_DEFAULT = 0,
524
/* attach probe in legacy mode, using debugfs/tracefs */
525
PROBE_ATTACH_MODE_LEGACY,
526
/* create perf event with perf_event_open() syscall */
527
PROBE_ATTACH_MODE_PERF,
528
/* attach probe with BPF link */
529
PROBE_ATTACH_MODE_LINK,
530
};
531
532
struct bpf_kprobe_opts {
533
/* size of this struct, for forward/backward compatibility */
534
size_t sz;
535
/* custom user-provided value fetchable through bpf_get_attach_cookie() */
536
__u64 bpf_cookie;
537
/* function's offset to install kprobe to */
538
size_t offset;
539
/* kprobe is return probe */
540
bool retprobe;
541
/* kprobe attach mode */
542
enum probe_attach_mode attach_mode;
543
size_t :0;
544
};
545
#define bpf_kprobe_opts__last_field attach_mode
546
547
LIBBPF_API struct bpf_link *
548
bpf_program__attach_kprobe(const struct bpf_program *prog, bool retprobe,
549
const char *func_name);
550
LIBBPF_API struct bpf_link *
551
bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
552
const char *func_name,
553
const struct bpf_kprobe_opts *opts);
554
555
struct bpf_kprobe_multi_opts {
556
/* size of this struct, for forward/backward compatibility */
557
size_t sz;
558
/* array of function symbols to attach */
559
const char **syms;
560
/* array of function addresses to attach */
561
const unsigned long *addrs;
562
/* array of user-provided values fetchable through bpf_get_attach_cookie */
563
const __u64 *cookies;
564
/* number of elements in syms/addrs/cookies arrays */
565
size_t cnt;
566
/* create return kprobes */
567
bool retprobe;
568
/* create session kprobes */
569
bool session;
570
/* enforce unique match */
571
bool unique_match;
572
size_t :0;
573
};
574
575
#define bpf_kprobe_multi_opts__last_field unique_match
576
577
LIBBPF_API struct bpf_link *
578
bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,
579
const char *pattern,
580
const struct bpf_kprobe_multi_opts *opts);
581
582
struct bpf_uprobe_multi_opts {
583
/* size of this struct, for forward/backward compatibility */
584
size_t sz;
585
/* array of function symbols to attach to */
586
const char **syms;
587
/* array of function addresses to attach to */
588
const unsigned long *offsets;
589
/* optional, array of associated ref counter offsets */
590
const unsigned long *ref_ctr_offsets;
591
/* optional, array of associated BPF cookies */
592
const __u64 *cookies;
593
/* number of elements in syms/addrs/cookies arrays */
594
size_t cnt;
595
/* create return uprobes */
596
bool retprobe;
597
/* create session kprobes */
598
bool session;
599
size_t :0;
600
};
601
602
#define bpf_uprobe_multi_opts__last_field session
603
604
/**
605
* @brief **bpf_program__attach_uprobe_multi()** attaches a BPF program
606
* to multiple uprobes with uprobe_multi link.
607
*
608
* User can specify 2 mutually exclusive set of inputs:
609
*
610
* 1) use only path/func_pattern/pid arguments
611
*
612
* 2) use path/pid with allowed combinations of
613
* syms/offsets/ref_ctr_offsets/cookies/cnt
614
*
615
* - syms and offsets are mutually exclusive
616
* - ref_ctr_offsets and cookies are optional
617
*
618
*
619
* @param prog BPF program to attach
620
* @param pid Process ID to attach the uprobe to, 0 for self (own process),
621
* -1 for all processes
622
* @param binary_path Path to binary
623
* @param func_pattern Regular expression to specify functions to attach
624
* BPF program to
625
* @param opts Additional options (see **struct bpf_uprobe_multi_opts**)
626
* @return 0, on success; negative error code, otherwise
627
*/
628
LIBBPF_API struct bpf_link *
629
bpf_program__attach_uprobe_multi(const struct bpf_program *prog,
630
pid_t pid,
631
const char *binary_path,
632
const char *func_pattern,
633
const struct bpf_uprobe_multi_opts *opts);
634
635
struct bpf_ksyscall_opts {
636
/* size of this struct, for forward/backward compatibility */
637
size_t sz;
638
/* custom user-provided value fetchable through bpf_get_attach_cookie() */
639
__u64 bpf_cookie;
640
/* attach as return probe? */
641
bool retprobe;
642
size_t :0;
643
};
644
#define bpf_ksyscall_opts__last_field retprobe
645
646
/**
647
* @brief **bpf_program__attach_ksyscall()** attaches a BPF program
648
* to kernel syscall handler of a specified syscall. Optionally it's possible
649
* to request to install retprobe that will be triggered at syscall exit. It's
650
* also possible to associate BPF cookie (though options).
651
*
652
* Libbpf automatically will determine correct full kernel function name,
653
* which depending on system architecture and kernel version/configuration
654
* could be of the form __<arch>_sys_<syscall> or __se_sys_<syscall>, and will
655
* attach specified program using kprobe/kretprobe mechanism.
656
*
657
* **bpf_program__attach_ksyscall()** is an API counterpart of declarative
658
* **SEC("ksyscall/<syscall>")** annotation of BPF programs.
659
*
660
* At the moment **SEC("ksyscall")** and **bpf_program__attach_ksyscall()** do
661
* not handle all the calling convention quirks for mmap(), clone() and compat
662
* syscalls. It also only attaches to "native" syscall interfaces. If host
663
* system supports compat syscalls or defines 32-bit syscalls in 64-bit
664
* kernel, such syscall interfaces won't be attached to by libbpf.
665
*
666
* These limitations may or may not change in the future. Therefore it is
667
* recommended to use SEC("kprobe") for these syscalls or if working with
668
* compat and 32-bit interfaces is required.
669
*
670
* @param prog BPF program to attach
671
* @param syscall_name Symbolic name of the syscall (e.g., "bpf")
672
* @param opts Additional options (see **struct bpf_ksyscall_opts**)
673
* @return Reference to the newly created BPF link; or NULL is returned on
674
* error, error code is stored in errno
675
*/
676
LIBBPF_API struct bpf_link *
677
bpf_program__attach_ksyscall(const struct bpf_program *prog,
678
const char *syscall_name,
679
const struct bpf_ksyscall_opts *opts);
680
681
struct bpf_uprobe_opts {
682
/* size of this struct, for forward/backward compatibility */
683
size_t sz;
684
/* offset of kernel reference counted USDT semaphore, added in
685
* a6ca88b241d5 ("trace_uprobe: support reference counter in fd-based uprobe")
686
*/
687
size_t ref_ctr_offset;
688
/* custom user-provided value fetchable through bpf_get_attach_cookie() */
689
__u64 bpf_cookie;
690
/* uprobe is return probe, invoked at function return time */
691
bool retprobe;
692
/* Function name to attach to. Could be an unqualified ("abc") or library-qualified
693
* "abc@LIBXYZ" name. To specify function entry, func_name should be set while
694
* func_offset argument to bpf_prog__attach_uprobe_opts() should be 0. To trace an
695
* offset within a function, specify func_name and use func_offset argument to specify
696
* offset within the function. Shared library functions must specify the shared library
697
* binary_path.
698
*/
699
const char *func_name;
700
/* uprobe attach mode */
701
enum probe_attach_mode attach_mode;
702
size_t :0;
703
};
704
#define bpf_uprobe_opts__last_field attach_mode
705
706
/**
707
* @brief **bpf_program__attach_uprobe()** attaches a BPF program
708
* to the userspace function which is found by binary path and
709
* offset. You can optionally specify a particular process to attach
710
* to. You can also optionally attach the program to the function
711
* exit instead of entry.
712
*
713
* @param prog BPF program to attach
714
* @param retprobe Attach to function exit
715
* @param pid Process ID to attach the uprobe to, 0 for self (own process),
716
* -1 for all processes
717
* @param binary_path Path to binary that contains the function symbol
718
* @param func_offset Offset within the binary of the function symbol
719
* @return Reference to the newly created BPF link; or NULL is returned on error,
720
* error code is stored in errno
721
*/
722
LIBBPF_API struct bpf_link *
723
bpf_program__attach_uprobe(const struct bpf_program *prog, bool retprobe,
724
pid_t pid, const char *binary_path,
725
size_t func_offset);
726
727
/**
728
* @brief **bpf_program__attach_uprobe_opts()** is just like
729
* bpf_program__attach_uprobe() except with a options struct
730
* for various configurations.
731
*
732
* @param prog BPF program to attach
733
* @param pid Process ID to attach the uprobe to, 0 for self (own process),
734
* -1 for all processes
735
* @param binary_path Path to binary that contains the function symbol
736
* @param func_offset Offset within the binary of the function symbol
737
* @param opts Options for altering program attachment
738
* @return Reference to the newly created BPF link; or NULL is returned on error,
739
* error code is stored in errno
740
*/
741
LIBBPF_API struct bpf_link *
742
bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
743
const char *binary_path, size_t func_offset,
744
const struct bpf_uprobe_opts *opts);
745
746
struct bpf_usdt_opts {
747
/* size of this struct, for forward/backward compatibility */
748
size_t sz;
749
/* custom user-provided value accessible through usdt_cookie() */
750
__u64 usdt_cookie;
751
size_t :0;
752
};
753
#define bpf_usdt_opts__last_field usdt_cookie
754
755
/**
756
* @brief **bpf_program__attach_usdt()** is just like
757
* bpf_program__attach_uprobe_opts() except it covers USDT (User-space
758
* Statically Defined Tracepoint) attachment, instead of attaching to
759
* user-space function entry or exit.
760
*
761
* @param prog BPF program to attach
762
* @param pid Process ID to attach the uprobe to, 0 for self (own process),
763
* -1 for all processes
764
* @param binary_path Path to binary that contains provided USDT probe
765
* @param usdt_provider USDT provider name
766
* @param usdt_name USDT probe name
767
* @param opts Options for altering program attachment
768
* @return Reference to the newly created BPF link; or NULL is returned on error,
769
* error code is stored in errno
770
*/
771
LIBBPF_API struct bpf_link *
772
bpf_program__attach_usdt(const struct bpf_program *prog,
773
pid_t pid, const char *binary_path,
774
const char *usdt_provider, const char *usdt_name,
775
const struct bpf_usdt_opts *opts);
776
777
struct bpf_tracepoint_opts {
778
/* size of this struct, for forward/backward compatibility */
779
size_t sz;
780
/* custom user-provided value fetchable through bpf_get_attach_cookie() */
781
__u64 bpf_cookie;
782
};
783
#define bpf_tracepoint_opts__last_field bpf_cookie
784
785
LIBBPF_API struct bpf_link *
786
bpf_program__attach_tracepoint(const struct bpf_program *prog,
787
const char *tp_category,
788
const char *tp_name);
789
LIBBPF_API struct bpf_link *
790
bpf_program__attach_tracepoint_opts(const struct bpf_program *prog,
791
const char *tp_category,
792
const char *tp_name,
793
const struct bpf_tracepoint_opts *opts);
794
795
struct bpf_raw_tracepoint_opts {
796
size_t sz; /* size of this struct for forward/backward compatibility */
797
__u64 cookie;
798
size_t :0;
799
};
800
#define bpf_raw_tracepoint_opts__last_field cookie
801
802
LIBBPF_API struct bpf_link *
803
bpf_program__attach_raw_tracepoint(const struct bpf_program *prog,
804
const char *tp_name);
805
LIBBPF_API struct bpf_link *
806
bpf_program__attach_raw_tracepoint_opts(const struct bpf_program *prog,
807
const char *tp_name,
808
struct bpf_raw_tracepoint_opts *opts);
809
810
struct bpf_trace_opts {
811
/* size of this struct, for forward/backward compatibility */
812
size_t sz;
813
/* custom user-provided value fetchable through bpf_get_attach_cookie() */
814
__u64 cookie;
815
};
816
#define bpf_trace_opts__last_field cookie
817
818
LIBBPF_API struct bpf_link *
819
bpf_program__attach_trace(const struct bpf_program *prog);
820
LIBBPF_API struct bpf_link *
821
bpf_program__attach_trace_opts(const struct bpf_program *prog, const struct bpf_trace_opts *opts);
822
823
LIBBPF_API struct bpf_link *
824
bpf_program__attach_lsm(const struct bpf_program *prog);
825
LIBBPF_API struct bpf_link *
826
bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd);
827
LIBBPF_API struct bpf_link *
828
bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd);
829
LIBBPF_API struct bpf_link *
830
bpf_program__attach_sockmap(const struct bpf_program *prog, int map_fd);
831
LIBBPF_API struct bpf_link *
832
bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex);
833
LIBBPF_API struct bpf_link *
834
bpf_program__attach_freplace(const struct bpf_program *prog,
835
int target_fd, const char *attach_func_name);
836
837
struct bpf_netfilter_opts {
838
/* size of this struct, for forward/backward compatibility */
839
size_t sz;
840
841
__u32 pf;
842
__u32 hooknum;
843
__s32 priority;
844
__u32 flags;
845
};
846
#define bpf_netfilter_opts__last_field flags
847
848
LIBBPF_API struct bpf_link *
849
bpf_program__attach_netfilter(const struct bpf_program *prog,
850
const struct bpf_netfilter_opts *opts);
851
852
struct bpf_tcx_opts {
853
/* size of this struct, for forward/backward compatibility */
854
size_t sz;
855
__u32 flags;
856
__u32 relative_fd;
857
__u32 relative_id;
858
__u64 expected_revision;
859
size_t :0;
860
};
861
#define bpf_tcx_opts__last_field expected_revision
862
863
LIBBPF_API struct bpf_link *
864
bpf_program__attach_tcx(const struct bpf_program *prog, int ifindex,
865
const struct bpf_tcx_opts *opts);
866
867
struct bpf_netkit_opts {
868
/* size of this struct, for forward/backward compatibility */
869
size_t sz;
870
__u32 flags;
871
__u32 relative_fd;
872
__u32 relative_id;
873
__u64 expected_revision;
874
size_t :0;
875
};
876
#define bpf_netkit_opts__last_field expected_revision
877
878
LIBBPF_API struct bpf_link *
879
bpf_program__attach_netkit(const struct bpf_program *prog, int ifindex,
880
const struct bpf_netkit_opts *opts);
881
882
struct bpf_cgroup_opts {
883
/* size of this struct, for forward/backward compatibility */
884
size_t sz;
885
__u32 flags;
886
__u32 relative_fd;
887
__u32 relative_id;
888
__u64 expected_revision;
889
size_t :0;
890
};
891
#define bpf_cgroup_opts__last_field expected_revision
892
893
LIBBPF_API struct bpf_link *
894
bpf_program__attach_cgroup_opts(const struct bpf_program *prog, int cgroup_fd,
895
const struct bpf_cgroup_opts *opts);
896
897
struct bpf_map;
898
899
LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map);
900
LIBBPF_API int bpf_link__update_map(struct bpf_link *link, const struct bpf_map *map);
901
902
struct bpf_iter_attach_opts {
903
size_t sz; /* size of this struct for forward/backward compatibility */
904
union bpf_iter_link_info *link_info;
905
__u32 link_info_len;
906
};
907
#define bpf_iter_attach_opts__last_field link_info_len
908
909
LIBBPF_API struct bpf_link *
910
bpf_program__attach_iter(const struct bpf_program *prog,
911
const struct bpf_iter_attach_opts *opts);
912
913
LIBBPF_API enum bpf_prog_type bpf_program__type(const struct bpf_program *prog);
914
915
/**
916
* @brief **bpf_program__set_type()** sets the program
917
* type of the passed BPF program.
918
* @param prog BPF program to set the program type for
919
* @param type program type to set the BPF map to have
920
* @return error code; or 0 if no error. An error occurs
921
* if the object is already loaded.
922
*
923
* This must be called before the BPF object is loaded,
924
* otherwise it has no effect and an error is returned.
925
*/
926
LIBBPF_API int bpf_program__set_type(struct bpf_program *prog,
927
enum bpf_prog_type type);
928
929
LIBBPF_API enum bpf_attach_type
930
bpf_program__expected_attach_type(const struct bpf_program *prog);
931
932
/**
933
* @brief **bpf_program__set_expected_attach_type()** sets the
934
* attach type of the passed BPF program. This is used for
935
* auto-detection of attachment when programs are loaded.
936
* @param prog BPF program to set the attach type for
937
* @param type attach type to set the BPF map to have
938
* @return error code; or 0 if no error. An error occurs
939
* if the object is already loaded.
940
*
941
* This must be called before the BPF object is loaded,
942
* otherwise it has no effect and an error is returned.
943
*/
944
LIBBPF_API int
945
bpf_program__set_expected_attach_type(struct bpf_program *prog,
946
enum bpf_attach_type type);
947
948
LIBBPF_API __u32 bpf_program__flags(const struct bpf_program *prog);
949
LIBBPF_API int bpf_program__set_flags(struct bpf_program *prog, __u32 flags);
950
951
/* Per-program log level and log buffer getters/setters.
952
* See bpf_object_open_opts comments regarding log_level and log_buf
953
* interactions.
954
*/
955
LIBBPF_API __u32 bpf_program__log_level(const struct bpf_program *prog);
956
LIBBPF_API int bpf_program__set_log_level(struct bpf_program *prog, __u32 log_level);
957
LIBBPF_API const char *bpf_program__log_buf(const struct bpf_program *prog, size_t *log_size);
958
LIBBPF_API int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log_size);
959
960
LIBBPF_API struct bpf_func_info *bpf_program__func_info(const struct bpf_program *prog);
961
LIBBPF_API __u32 bpf_program__func_info_cnt(const struct bpf_program *prog);
962
963
LIBBPF_API struct bpf_line_info *bpf_program__line_info(const struct bpf_program *prog);
964
LIBBPF_API __u32 bpf_program__line_info_cnt(const struct bpf_program *prog);
965
966
/**
967
* @brief **bpf_program__set_attach_target()** sets BTF-based attach target
968
* for supported BPF program types:
969
* - BTF-aware raw tracepoints (tp_btf);
970
* - fentry/fexit/fmod_ret;
971
* - lsm;
972
* - freplace.
973
* @param prog BPF program to set the attach type for
974
* @param type attach type to set the BPF map to have
975
* @return error code; or 0 if no error occurred.
976
*/
977
LIBBPF_API int
978
bpf_program__set_attach_target(struct bpf_program *prog, int attach_prog_fd,
979
const char *attach_func_name);
980
981
/**
982
* @brief **bpf_object__find_map_by_name()** returns BPF map of
983
* the given name, if it exists within the passed BPF object
984
* @param obj BPF object
985
* @param name name of the BPF map
986
* @return BPF map instance, if such map exists within the BPF object;
987
* or NULL otherwise.
988
*/
989
LIBBPF_API struct bpf_map *
990
bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name);
991
992
LIBBPF_API int
993
bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name);
994
995
LIBBPF_API struct bpf_map *
996
bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *map);
997
998
#define bpf_object__for_each_map(pos, obj) \
999
for ((pos) = bpf_object__next_map((obj), NULL); \
1000
(pos) != NULL; \
1001
(pos) = bpf_object__next_map((obj), (pos)))
1002
#define bpf_map__for_each bpf_object__for_each_map
1003
1004
LIBBPF_API struct bpf_map *
1005
bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *map);
1006
1007
/**
1008
* @brief **bpf_map__set_autocreate()** sets whether libbpf has to auto-create
1009
* BPF map during BPF object load phase.
1010
* @param map the BPF map instance
1011
* @param autocreate whether to create BPF map during BPF object load
1012
* @return 0 on success; -EBUSY if BPF object was already loaded
1013
*
1014
* **bpf_map__set_autocreate()** allows to opt-out from libbpf auto-creating
1015
* BPF map. By default, libbpf will attempt to create every single BPF map
1016
* defined in BPF object file using BPF_MAP_CREATE command of bpf() syscall
1017
* and fill in map FD in BPF instructions.
1018
*
1019
* This API allows to opt-out of this process for specific map instance. This
1020
* can be useful if host kernel doesn't support such BPF map type or used
1021
* combination of flags and user application wants to avoid creating such
1022
* a map in the first place. User is still responsible to make sure that their
1023
* BPF-side code that expects to use such missing BPF map is recognized by BPF
1024
* verifier as dead code, otherwise BPF verifier will reject such BPF program.
1025
*/
1026
LIBBPF_API int bpf_map__set_autocreate(struct bpf_map *map, bool autocreate);
1027
LIBBPF_API bool bpf_map__autocreate(const struct bpf_map *map);
1028
1029
/**
1030
* @brief **bpf_map__set_autoattach()** sets whether libbpf has to auto-attach
1031
* map during BPF skeleton attach phase.
1032
* @param map the BPF map instance
1033
* @param autoattach whether to attach map during BPF skeleton attach phase
1034
* @return 0 on success; negative error code, otherwise
1035
*/
1036
LIBBPF_API int bpf_map__set_autoattach(struct bpf_map *map, bool autoattach);
1037
1038
/**
1039
* @brief **bpf_map__autoattach()** returns whether BPF map is configured to
1040
* auto-attach during BPF skeleton attach phase.
1041
* @param map the BPF map instance
1042
* @return true if map is set to auto-attach during skeleton attach phase; false, otherwise
1043
*/
1044
LIBBPF_API bool bpf_map__autoattach(const struct bpf_map *map);
1045
1046
/**
1047
* @brief **bpf_map__fd()** gets the file descriptor of the passed
1048
* BPF map
1049
* @param map the BPF map instance
1050
* @return the file descriptor; or -EINVAL in case of an error
1051
*/
1052
LIBBPF_API int bpf_map__fd(const struct bpf_map *map);
1053
LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);
1054
/* get map name */
1055
LIBBPF_API const char *bpf_map__name(const struct bpf_map *map);
1056
/* get/set map type */
1057
LIBBPF_API enum bpf_map_type bpf_map__type(const struct bpf_map *map);
1058
LIBBPF_API int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type);
1059
/* get/set map size (max_entries) */
1060
LIBBPF_API __u32 bpf_map__max_entries(const struct bpf_map *map);
1061
LIBBPF_API int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries);
1062
/* get/set map flags */
1063
LIBBPF_API __u32 bpf_map__map_flags(const struct bpf_map *map);
1064
LIBBPF_API int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags);
1065
/* get/set map NUMA node */
1066
LIBBPF_API __u32 bpf_map__numa_node(const struct bpf_map *map);
1067
LIBBPF_API int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node);
1068
/* get/set map key size */
1069
LIBBPF_API __u32 bpf_map__key_size(const struct bpf_map *map);
1070
LIBBPF_API int bpf_map__set_key_size(struct bpf_map *map, __u32 size);
1071
/* get map value size */
1072
LIBBPF_API __u32 bpf_map__value_size(const struct bpf_map *map);
1073
/**
1074
* @brief **bpf_map__set_value_size()** sets map value size.
1075
* @param map the BPF map instance
1076
* @return 0, on success; negative error, otherwise
1077
*
1078
* There is a special case for maps with associated memory-mapped regions, like
1079
* the global data section maps (bss, data, rodata). When this function is used
1080
* on such a map, the mapped region is resized. Afterward, an attempt is made to
1081
* adjust the corresponding BTF info. This attempt is best-effort and can only
1082
* succeed if the last variable of the data section map is an array. The array
1083
* BTF type is replaced by a new BTF array type with a different length.
1084
* Any previously existing pointers returned from bpf_map__initial_value() or
1085
* corresponding data section skeleton pointer must be reinitialized.
1086
*/
1087
LIBBPF_API int bpf_map__set_value_size(struct bpf_map *map, __u32 size);
1088
/* get map key/value BTF type IDs */
1089
LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map);
1090
LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map);
1091
/* get/set map if_index */
1092
LIBBPF_API __u32 bpf_map__ifindex(const struct bpf_map *map);
1093
LIBBPF_API int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
1094
/* get/set map map_extra flags */
1095
LIBBPF_API __u64 bpf_map__map_extra(const struct bpf_map *map);
1096
LIBBPF_API int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra);
1097
1098
LIBBPF_API int bpf_map__set_initial_value(struct bpf_map *map,
1099
const void *data, size_t size);
1100
LIBBPF_API void *bpf_map__initial_value(const struct bpf_map *map, size_t *psize);
1101
1102
/**
1103
* @brief **bpf_map__is_internal()** tells the caller whether or not the
1104
* passed map is a special map created by libbpf automatically for things like
1105
* global variables, __ksym externs, Kconfig values, etc
1106
* @param map the bpf_map
1107
* @return true, if the map is an internal map; false, otherwise
1108
*/
1109
LIBBPF_API bool bpf_map__is_internal(const struct bpf_map *map);
1110
1111
/**
1112
* @brief **bpf_map__set_pin_path()** sets the path attribute that tells where the
1113
* BPF map should be pinned. This does not actually create the 'pin'.
1114
* @param map The bpf_map
1115
* @param path The path
1116
* @return 0, on success; negative error, otherwise
1117
*/
1118
LIBBPF_API int bpf_map__set_pin_path(struct bpf_map *map, const char *path);
1119
1120
/**
1121
* @brief **bpf_map__pin_path()** gets the path attribute that tells where the
1122
* BPF map should be pinned.
1123
* @param map The bpf_map
1124
* @return The path string; which can be NULL
1125
*/
1126
LIBBPF_API const char *bpf_map__pin_path(const struct bpf_map *map);
1127
1128
/**
1129
* @brief **bpf_map__is_pinned()** tells the caller whether or not the
1130
* passed map has been pinned via a 'pin' file.
1131
* @param map The bpf_map
1132
* @return true, if the map is pinned; false, otherwise
1133
*/
1134
LIBBPF_API bool bpf_map__is_pinned(const struct bpf_map *map);
1135
1136
/**
1137
* @brief **bpf_map__pin()** creates a file that serves as a 'pin'
1138
* for the BPF map. This increments the reference count on the
1139
* BPF map which will keep the BPF map loaded even after the
1140
* userspace process which loaded it has exited.
1141
* @param map The bpf_map to pin
1142
* @param path A file path for the 'pin'
1143
* @return 0, on success; negative error, otherwise
1144
*
1145
* If `path` is NULL the maps `pin_path` attribute will be used. If this is
1146
* also NULL, an error will be returned and the map will not be pinned.
1147
*/
1148
LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);
1149
1150
/**
1151
* @brief **bpf_map__unpin()** removes the file that serves as a
1152
* 'pin' for the BPF map.
1153
* @param map The bpf_map to unpin
1154
* @param path A file path for the 'pin'
1155
* @return 0, on success; negative error, otherwise
1156
*
1157
* The `path` parameter can be NULL, in which case the `pin_path`
1158
* map attribute is unpinned. If both the `path` parameter and
1159
* `pin_path` map attribute are set, they must be equal.
1160
*/
1161
LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path);
1162
1163
LIBBPF_API int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd);
1164
LIBBPF_API struct bpf_map *bpf_map__inner_map(struct bpf_map *map);
1165
1166
/**
1167
* @brief **bpf_map__lookup_elem()** allows to lookup BPF map value
1168
* corresponding to provided key.
1169
* @param map BPF map to lookup element in
1170
* @param key pointer to memory containing bytes of the key used for lookup
1171
* @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
1172
* @param value pointer to memory in which looked up value will be stored
1173
* @param value_sz size in byte of value data memory; it has to match BPF map
1174
* definition's **value_size**. For per-CPU BPF maps value size has to be
1175
* a product of BPF map value size and number of possible CPUs in the system
1176
* (could be fetched with **libbpf_num_possible_cpus()**). Note also that for
1177
* per-CPU values value size has to be aligned up to closest 8 bytes for
1178
* alignment reasons, so expected size is: `round_up(value_size, 8)
1179
* * libbpf_num_possible_cpus()`.
1180
* @flags extra flags passed to kernel for this operation
1181
* @return 0, on success; negative error, otherwise
1182
*
1183
* **bpf_map__lookup_elem()** is high-level equivalent of
1184
* **bpf_map_lookup_elem()** API with added check for key and value size.
1185
*/
1186
LIBBPF_API int bpf_map__lookup_elem(const struct bpf_map *map,
1187
const void *key, size_t key_sz,
1188
void *value, size_t value_sz, __u64 flags);
1189
1190
/**
1191
* @brief **bpf_map__update_elem()** allows to insert or update value in BPF
1192
* map that corresponds to provided key.
1193
* @param map BPF map to insert to or update element in
1194
* @param key pointer to memory containing bytes of the key
1195
* @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
1196
* @param value pointer to memory containing bytes of the value
1197
* @param value_sz size in byte of value data memory; it has to match BPF map
1198
* definition's **value_size**. For per-CPU BPF maps value size has to be
1199
* a product of BPF map value size and number of possible CPUs in the system
1200
* (could be fetched with **libbpf_num_possible_cpus()**). Note also that for
1201
* per-CPU values value size has to be aligned up to closest 8 bytes for
1202
* alignment reasons, so expected size is: `round_up(value_size, 8)
1203
* * libbpf_num_possible_cpus()`.
1204
* @flags extra flags passed to kernel for this operation
1205
* @return 0, on success; negative error, otherwise
1206
*
1207
* **bpf_map__update_elem()** is high-level equivalent of
1208
* **bpf_map_update_elem()** API with added check for key and value size.
1209
*/
1210
LIBBPF_API int bpf_map__update_elem(const struct bpf_map *map,
1211
const void *key, size_t key_sz,
1212
const void *value, size_t value_sz, __u64 flags);
1213
1214
/**
1215
* @brief **bpf_map__delete_elem()** allows to delete element in BPF map that
1216
* corresponds to provided key.
1217
* @param map BPF map to delete element from
1218
* @param key pointer to memory containing bytes of the key
1219
* @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
1220
* @flags extra flags passed to kernel for this operation
1221
* @return 0, on success; negative error, otherwise
1222
*
1223
* **bpf_map__delete_elem()** is high-level equivalent of
1224
* **bpf_map_delete_elem()** API with added check for key size.
1225
*/
1226
LIBBPF_API int bpf_map__delete_elem(const struct bpf_map *map,
1227
const void *key, size_t key_sz, __u64 flags);
1228
1229
/**
1230
* @brief **bpf_map__lookup_and_delete_elem()** allows to lookup BPF map value
1231
* corresponding to provided key and atomically delete it afterwards.
1232
* @param map BPF map to lookup element in
1233
* @param key pointer to memory containing bytes of the key used for lookup
1234
* @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
1235
* @param value pointer to memory in which looked up value will be stored
1236
* @param value_sz size in byte of value data memory; it has to match BPF map
1237
* definition's **value_size**. For per-CPU BPF maps value size has to be
1238
* a product of BPF map value size and number of possible CPUs in the system
1239
* (could be fetched with **libbpf_num_possible_cpus()**). Note also that for
1240
* per-CPU values value size has to be aligned up to closest 8 bytes for
1241
* alignment reasons, so expected size is: `round_up(value_size, 8)
1242
* * libbpf_num_possible_cpus()`.
1243
* @flags extra flags passed to kernel for this operation
1244
* @return 0, on success; negative error, otherwise
1245
*
1246
* **bpf_map__lookup_and_delete_elem()** is high-level equivalent of
1247
* **bpf_map_lookup_and_delete_elem()** API with added check for key and value size.
1248
*/
1249
LIBBPF_API int bpf_map__lookup_and_delete_elem(const struct bpf_map *map,
1250
const void *key, size_t key_sz,
1251
void *value, size_t value_sz, __u64 flags);
1252
1253
/**
1254
* @brief **bpf_map__get_next_key()** allows to iterate BPF map keys by
1255
* fetching next key that follows current key.
1256
* @param map BPF map to fetch next key from
1257
* @param cur_key pointer to memory containing bytes of current key or NULL to
1258
* fetch the first key
1259
* @param next_key pointer to memory to write next key into
1260
* @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
1261
* @return 0, on success; -ENOENT if **cur_key** is the last key in BPF map;
1262
* negative error, otherwise
1263
*
1264
* **bpf_map__get_next_key()** is high-level equivalent of
1265
* **bpf_map_get_next_key()** API with added check for key size.
1266
*/
1267
LIBBPF_API int bpf_map__get_next_key(const struct bpf_map *map,
1268
const void *cur_key, void *next_key, size_t key_sz);
1269
1270
struct bpf_xdp_set_link_opts {
1271
size_t sz;
1272
int old_fd;
1273
size_t :0;
1274
};
1275
#define bpf_xdp_set_link_opts__last_field old_fd
1276
1277
struct bpf_xdp_attach_opts {
1278
size_t sz;
1279
int old_prog_fd;
1280
size_t :0;
1281
};
1282
#define bpf_xdp_attach_opts__last_field old_prog_fd
1283
1284
struct bpf_xdp_query_opts {
1285
size_t sz;
1286
__u32 prog_id; /* output */
1287
__u32 drv_prog_id; /* output */
1288
__u32 hw_prog_id; /* output */
1289
__u32 skb_prog_id; /* output */
1290
__u8 attach_mode; /* output */
1291
__u64 feature_flags; /* output */
1292
__u32 xdp_zc_max_segs; /* output */
1293
size_t :0;
1294
};
1295
#define bpf_xdp_query_opts__last_field xdp_zc_max_segs
1296
1297
LIBBPF_API int bpf_xdp_attach(int ifindex, int prog_fd, __u32 flags,
1298
const struct bpf_xdp_attach_opts *opts);
1299
LIBBPF_API int bpf_xdp_detach(int ifindex, __u32 flags,
1300
const struct bpf_xdp_attach_opts *opts);
1301
LIBBPF_API int bpf_xdp_query(int ifindex, int flags, struct bpf_xdp_query_opts *opts);
1302
LIBBPF_API int bpf_xdp_query_id(int ifindex, int flags, __u32 *prog_id);
1303
1304
/* TC related API */
1305
enum bpf_tc_attach_point {
1306
BPF_TC_INGRESS = 1 << 0,
1307
BPF_TC_EGRESS = 1 << 1,
1308
BPF_TC_CUSTOM = 1 << 2,
1309
BPF_TC_QDISC = 1 << 3,
1310
};
1311
1312
#define BPF_TC_PARENT(a, b) \
1313
((((a) << 16) & 0xFFFF0000U) | ((b) & 0x0000FFFFU))
1314
1315
enum bpf_tc_flags {
1316
BPF_TC_F_REPLACE = 1 << 0,
1317
};
1318
1319
struct bpf_tc_hook {
1320
size_t sz;
1321
int ifindex;
1322
enum bpf_tc_attach_point attach_point;
1323
__u32 parent;
1324
__u32 handle;
1325
const char *qdisc;
1326
size_t :0;
1327
};
1328
#define bpf_tc_hook__last_field qdisc
1329
1330
struct bpf_tc_opts {
1331
size_t sz;
1332
int prog_fd;
1333
__u32 flags;
1334
__u32 prog_id;
1335
__u32 handle;
1336
__u32 priority;
1337
size_t :0;
1338
};
1339
#define bpf_tc_opts__last_field priority
1340
1341
LIBBPF_API int bpf_tc_hook_create(struct bpf_tc_hook *hook);
1342
LIBBPF_API int bpf_tc_hook_destroy(struct bpf_tc_hook *hook);
1343
LIBBPF_API int bpf_tc_attach(const struct bpf_tc_hook *hook,
1344
struct bpf_tc_opts *opts);
1345
LIBBPF_API int bpf_tc_detach(const struct bpf_tc_hook *hook,
1346
const struct bpf_tc_opts *opts);
1347
LIBBPF_API int bpf_tc_query(const struct bpf_tc_hook *hook,
1348
struct bpf_tc_opts *opts);
1349
1350
/* Ring buffer APIs */
1351
struct ring_buffer;
1352
struct ring;
1353
struct user_ring_buffer;
1354
1355
typedef int (*ring_buffer_sample_fn)(void *ctx, void *data, size_t size);
1356
1357
struct ring_buffer_opts {
1358
size_t sz; /* size of this struct, for forward/backward compatibility */
1359
};
1360
1361
#define ring_buffer_opts__last_field sz
1362
1363
LIBBPF_API struct ring_buffer *
1364
ring_buffer__new(int map_fd, ring_buffer_sample_fn sample_cb, void *ctx,
1365
const struct ring_buffer_opts *opts);
1366
LIBBPF_API void ring_buffer__free(struct ring_buffer *rb);
1367
LIBBPF_API int ring_buffer__add(struct ring_buffer *rb, int map_fd,
1368
ring_buffer_sample_fn sample_cb, void *ctx);
1369
LIBBPF_API int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms);
1370
LIBBPF_API int ring_buffer__consume(struct ring_buffer *rb);
1371
LIBBPF_API int ring_buffer__consume_n(struct ring_buffer *rb, size_t n);
1372
LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb);
1373
1374
/**
1375
* @brief **ring_buffer__ring()** returns the ringbuffer object inside a given
1376
* ringbuffer manager representing a single BPF_MAP_TYPE_RINGBUF map instance.
1377
*
1378
* @param rb A ringbuffer manager object.
1379
* @param idx An index into the ringbuffers contained within the ringbuffer
1380
* manager object. The index is 0-based and corresponds to the order in which
1381
* ring_buffer__add was called.
1382
* @return A ringbuffer object on success; NULL and errno set if the index is
1383
* invalid.
1384
*/
1385
LIBBPF_API struct ring *ring_buffer__ring(struct ring_buffer *rb,
1386
unsigned int idx);
1387
1388
/**
1389
* @brief **ring__consumer_pos()** returns the current consumer position in the
1390
* given ringbuffer.
1391
*
1392
* @param r A ringbuffer object.
1393
* @return The current consumer position.
1394
*/
1395
LIBBPF_API unsigned long ring__consumer_pos(const struct ring *r);
1396
1397
/**
1398
* @brief **ring__producer_pos()** returns the current producer position in the
1399
* given ringbuffer.
1400
*
1401
* @param r A ringbuffer object.
1402
* @return The current producer position.
1403
*/
1404
LIBBPF_API unsigned long ring__producer_pos(const struct ring *r);
1405
1406
/**
1407
* @brief **ring__avail_data_size()** returns the number of bytes in the
1408
* ringbuffer not yet consumed. This has no locking associated with it, so it
1409
* can be inaccurate if operations are ongoing while this is called. However, it
1410
* should still show the correct trend over the long-term.
1411
*
1412
* @param r A ringbuffer object.
1413
* @return The number of bytes not yet consumed.
1414
*/
1415
LIBBPF_API size_t ring__avail_data_size(const struct ring *r);
1416
1417
/**
1418
* @brief **ring__size()** returns the total size of the ringbuffer's map data
1419
* area (excluding special producer/consumer pages). Effectively this gives the
1420
* amount of usable bytes of data inside the ringbuffer.
1421
*
1422
* @param r A ringbuffer object.
1423
* @return The total size of the ringbuffer map data area.
1424
*/
1425
LIBBPF_API size_t ring__size(const struct ring *r);
1426
1427
/**
1428
* @brief **ring__map_fd()** returns the file descriptor underlying the given
1429
* ringbuffer.
1430
*
1431
* @param r A ringbuffer object.
1432
* @return The underlying ringbuffer file descriptor
1433
*/
1434
LIBBPF_API int ring__map_fd(const struct ring *r);
1435
1436
/**
1437
* @brief **ring__consume()** consumes available ringbuffer data without event
1438
* polling.
1439
*
1440
* @param r A ringbuffer object.
1441
* @return The number of records consumed (or INT_MAX, whichever is less), or
1442
* a negative number if any of the callbacks return an error.
1443
*/
1444
LIBBPF_API int ring__consume(struct ring *r);
1445
1446
/**
1447
* @brief **ring__consume_n()** consumes up to a requested amount of items from
1448
* a ringbuffer without event polling.
1449
*
1450
* @param r A ringbuffer object.
1451
* @param n Maximum amount of items to consume.
1452
* @return The number of items consumed, or a negative number if any of the
1453
* callbacks return an error.
1454
*/
1455
LIBBPF_API int ring__consume_n(struct ring *r, size_t n);
1456
1457
struct user_ring_buffer_opts {
1458
size_t sz; /* size of this struct, for forward/backward compatibility */
1459
};
1460
1461
#define user_ring_buffer_opts__last_field sz
1462
1463
/**
1464
* @brief **user_ring_buffer__new()** creates a new instance of a user ring
1465
* buffer.
1466
*
1467
* @param map_fd A file descriptor to a BPF_MAP_TYPE_USER_RINGBUF map.
1468
* @param opts Options for how the ring buffer should be created.
1469
* @return A user ring buffer on success; NULL and errno being set on a
1470
* failure.
1471
*/
1472
LIBBPF_API struct user_ring_buffer *
1473
user_ring_buffer__new(int map_fd, const struct user_ring_buffer_opts *opts);
1474
1475
/**
1476
* @brief **user_ring_buffer__reserve()** reserves a pointer to a sample in the
1477
* user ring buffer.
1478
* @param rb A pointer to a user ring buffer.
1479
* @param size The size of the sample, in bytes.
1480
* @return A pointer to an 8-byte aligned reserved region of the user ring
1481
* buffer; NULL, and errno being set if a sample could not be reserved.
1482
*
1483
* This function is *not* thread safe, and callers must synchronize accessing
1484
* this function if there are multiple producers. If a size is requested that
1485
* is larger than the size of the entire ring buffer, errno will be set to
1486
* E2BIG and NULL is returned. If the ring buffer could accommodate the size,
1487
* but currently does not have enough space, errno is set to ENOSPC and NULL is
1488
* returned.
1489
*
1490
* After initializing the sample, callers must invoke
1491
* **user_ring_buffer__submit()** to post the sample to the kernel. Otherwise,
1492
* the sample must be freed with **user_ring_buffer__discard()**.
1493
*/
1494
LIBBPF_API void *user_ring_buffer__reserve(struct user_ring_buffer *rb, __u32 size);
1495
1496
/**
1497
* @brief **user_ring_buffer__reserve_blocking()** reserves a record in the
1498
* ring buffer, possibly blocking for up to @timeout_ms until a sample becomes
1499
* available.
1500
* @param rb The user ring buffer.
1501
* @param size The size of the sample, in bytes.
1502
* @param timeout_ms The amount of time, in milliseconds, for which the caller
1503
* should block when waiting for a sample. -1 causes the caller to block
1504
* indefinitely.
1505
* @return A pointer to an 8-byte aligned reserved region of the user ring
1506
* buffer; NULL, and errno being set if a sample could not be reserved.
1507
*
1508
* This function is *not* thread safe, and callers must synchronize
1509
* accessing this function if there are multiple producers
1510
*
1511
* If **timeout_ms** is -1, the function will block indefinitely until a sample
1512
* becomes available. Otherwise, **timeout_ms** must be non-negative, or errno
1513
* is set to EINVAL, and NULL is returned. If **timeout_ms** is 0, no blocking
1514
* will occur and the function will return immediately after attempting to
1515
* reserve a sample.
1516
*
1517
* If **size** is larger than the size of the entire ring buffer, errno is set
1518
* to E2BIG and NULL is returned. If the ring buffer could accommodate
1519
* **size**, but currently does not have enough space, the caller will block
1520
* until at most **timeout_ms** has elapsed. If insufficient space is available
1521
* at that time, errno is set to ENOSPC, and NULL is returned.
1522
*
1523
* The kernel guarantees that it will wake up this thread to check if
1524
* sufficient space is available in the ring buffer at least once per
1525
* invocation of the **bpf_ringbuf_drain()** helper function, provided that at
1526
* least one sample is consumed, and the BPF program did not invoke the
1527
* function with BPF_RB_NO_WAKEUP. A wakeup may occur sooner than that, but the
1528
* kernel does not guarantee this. If the helper function is invoked with
1529
* BPF_RB_FORCE_WAKEUP, a wakeup event will be sent even if no sample is
1530
* consumed.
1531
*
1532
* When a sample of size **size** is found within **timeout_ms**, a pointer to
1533
* the sample is returned. After initializing the sample, callers must invoke
1534
* **user_ring_buffer__submit()** to post the sample to the ring buffer.
1535
* Otherwise, the sample must be freed with **user_ring_buffer__discard()**.
1536
*/
1537
LIBBPF_API void *user_ring_buffer__reserve_blocking(struct user_ring_buffer *rb,
1538
__u32 size,
1539
int timeout_ms);
1540
1541
/**
1542
* @brief **user_ring_buffer__submit()** submits a previously reserved sample
1543
* into the ring buffer.
1544
* @param rb The user ring buffer.
1545
* @param sample A reserved sample.
1546
*
1547
* It is not necessary to synchronize amongst multiple producers when invoking
1548
* this function.
1549
*/
1550
LIBBPF_API void user_ring_buffer__submit(struct user_ring_buffer *rb, void *sample);
1551
1552
/**
1553
* @brief **user_ring_buffer__discard()** discards a previously reserved sample.
1554
* @param rb The user ring buffer.
1555
* @param sample A reserved sample.
1556
*
1557
* It is not necessary to synchronize amongst multiple producers when invoking
1558
* this function.
1559
*/
1560
LIBBPF_API void user_ring_buffer__discard(struct user_ring_buffer *rb, void *sample);
1561
1562
/**
1563
* @brief **user_ring_buffer__free()** frees a ring buffer that was previously
1564
* created with **user_ring_buffer__new()**.
1565
* @param rb The user ring buffer being freed.
1566
*/
1567
LIBBPF_API void user_ring_buffer__free(struct user_ring_buffer *rb);
1568
1569
/* Perf buffer APIs */
1570
struct perf_buffer;
1571
1572
typedef void (*perf_buffer_sample_fn)(void *ctx, int cpu,
1573
void *data, __u32 size);
1574
typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt);
1575
1576
/* common use perf buffer options */
1577
struct perf_buffer_opts {
1578
size_t sz;
1579
__u32 sample_period;
1580
size_t :0;
1581
};
1582
#define perf_buffer_opts__last_field sample_period
1583
1584
/**
1585
* @brief **perf_buffer__new()** creates BPF perfbuf manager for a specified
1586
* BPF_PERF_EVENT_ARRAY map
1587
* @param map_fd FD of BPF_PERF_EVENT_ARRAY BPF map that will be used by BPF
1588
* code to send data over to user-space
1589
* @param page_cnt number of memory pages allocated for each per-CPU buffer
1590
* @param sample_cb function called on each received data record
1591
* @param lost_cb function called when record loss has occurred
1592
* @param ctx user-provided extra context passed into *sample_cb* and *lost_cb*
1593
* @return a new instance of struct perf_buffer on success, NULL on error with
1594
* *errno* containing an error code
1595
*/
1596
LIBBPF_API struct perf_buffer *
1597
perf_buffer__new(int map_fd, size_t page_cnt,
1598
perf_buffer_sample_fn sample_cb, perf_buffer_lost_fn lost_cb, void *ctx,
1599
const struct perf_buffer_opts *opts);
1600
1601
enum bpf_perf_event_ret {
1602
LIBBPF_PERF_EVENT_DONE = 0,
1603
LIBBPF_PERF_EVENT_ERROR = -1,
1604
LIBBPF_PERF_EVENT_CONT = -2,
1605
};
1606
1607
struct perf_event_header;
1608
1609
typedef enum bpf_perf_event_ret
1610
(*perf_buffer_event_fn)(void *ctx, int cpu, struct perf_event_header *event);
1611
1612
/* raw perf buffer options, giving most power and control */
1613
struct perf_buffer_raw_opts {
1614
size_t sz;
1615
long :0;
1616
long :0;
1617
/* if cpu_cnt == 0, open all on all possible CPUs (up to the number of
1618
* max_entries of given PERF_EVENT_ARRAY map)
1619
*/
1620
int cpu_cnt;
1621
/* if cpu_cnt > 0, cpus is an array of CPUs to open ring buffers on */
1622
int *cpus;
1623
/* if cpu_cnt > 0, map_keys specify map keys to set per-CPU FDs for */
1624
int *map_keys;
1625
};
1626
#define perf_buffer_raw_opts__last_field map_keys
1627
1628
struct perf_event_attr;
1629
1630
LIBBPF_API struct perf_buffer *
1631
perf_buffer__new_raw(int map_fd, size_t page_cnt, struct perf_event_attr *attr,
1632
perf_buffer_event_fn event_cb, void *ctx,
1633
const struct perf_buffer_raw_opts *opts);
1634
1635
LIBBPF_API void perf_buffer__free(struct perf_buffer *pb);
1636
LIBBPF_API int perf_buffer__epoll_fd(const struct perf_buffer *pb);
1637
LIBBPF_API int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms);
1638
LIBBPF_API int perf_buffer__consume(struct perf_buffer *pb);
1639
LIBBPF_API int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx);
1640
LIBBPF_API size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb);
1641
LIBBPF_API int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx);
1642
/**
1643
* @brief **perf_buffer__buffer()** returns the per-cpu raw mmap()'ed underlying
1644
* memory region of the ring buffer.
1645
* This ring buffer can be used to implement a custom events consumer.
1646
* The ring buffer starts with the *struct perf_event_mmap_page*, which
1647
* holds the ring buffer management fields, when accessing the header
1648
* structure it's important to be SMP aware.
1649
* You can refer to *perf_event_read_simple* for a simple example.
1650
* @param pb the perf buffer structure
1651
* @param buf_idx the buffer index to retrieve
1652
* @param buf (out) gets the base pointer of the mmap()'ed memory
1653
* @param buf_size (out) gets the size of the mmap()'ed region
1654
* @return 0 on success, negative error code for failure
1655
*/
1656
LIBBPF_API int perf_buffer__buffer(struct perf_buffer *pb, int buf_idx, void **buf,
1657
size_t *buf_size);
1658
1659
struct bpf_prog_linfo;
1660
struct bpf_prog_info;
1661
1662
LIBBPF_API void bpf_prog_linfo__free(struct bpf_prog_linfo *prog_linfo);
1663
LIBBPF_API struct bpf_prog_linfo *
1664
bpf_prog_linfo__new(const struct bpf_prog_info *info);
1665
LIBBPF_API const struct bpf_line_info *
1666
bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo,
1667
__u64 addr, __u32 func_idx, __u32 nr_skip);
1668
LIBBPF_API const struct bpf_line_info *
1669
bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo,
1670
__u32 insn_off, __u32 nr_skip);
1671
1672
/*
1673
* Probe for supported system features
1674
*
1675
* Note that running many of these probes in a short amount of time can cause
1676
* the kernel to reach the maximal size of lockable memory allowed for the
1677
* user, causing subsequent probes to fail. In this case, the caller may want
1678
* to adjust that limit with setrlimit().
1679
*/
1680
1681
/**
1682
* @brief **libbpf_probe_bpf_prog_type()** detects if host kernel supports
1683
* BPF programs of a given type.
1684
* @param prog_type BPF program type to detect kernel support for
1685
* @param opts reserved for future extensibility, should be NULL
1686
* @return 1, if given program type is supported; 0, if given program type is
1687
* not supported; negative error code if feature detection failed or can't be
1688
* performed
1689
*
1690
* Make sure the process has required set of CAP_* permissions (or runs as
1691
* root) when performing feature checking.
1692
*/
1693
LIBBPF_API int libbpf_probe_bpf_prog_type(enum bpf_prog_type prog_type, const void *opts);
1694
/**
1695
* @brief **libbpf_probe_bpf_map_type()** detects if host kernel supports
1696
* BPF maps of a given type.
1697
* @param map_type BPF map type to detect kernel support for
1698
* @param opts reserved for future extensibility, should be NULL
1699
* @return 1, if given map type is supported; 0, if given map type is
1700
* not supported; negative error code if feature detection failed or can't be
1701
* performed
1702
*
1703
* Make sure the process has required set of CAP_* permissions (or runs as
1704
* root) when performing feature checking.
1705
*/
1706
LIBBPF_API int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void *opts);
1707
/**
1708
* @brief **libbpf_probe_bpf_helper()** detects if host kernel supports the
1709
* use of a given BPF helper from specified BPF program type.
1710
* @param prog_type BPF program type used to check the support of BPF helper
1711
* @param helper_id BPF helper ID (enum bpf_func_id) to check support for
1712
* @param opts reserved for future extensibility, should be NULL
1713
* @return 1, if given combination of program type and helper is supported; 0,
1714
* if the combination is not supported; negative error code if feature
1715
* detection for provided input arguments failed or can't be performed
1716
*
1717
* Make sure the process has required set of CAP_* permissions (or runs as
1718
* root) when performing feature checking.
1719
*/
1720
LIBBPF_API int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type,
1721
enum bpf_func_id helper_id, const void *opts);
1722
1723
/**
1724
* @brief **libbpf_num_possible_cpus()** is a helper function to get the
1725
* number of possible CPUs that the host kernel supports and expects.
1726
* @return number of possible CPUs; or error code on failure
1727
*
1728
* Example usage:
1729
*
1730
* int ncpus = libbpf_num_possible_cpus();
1731
* if (ncpus < 0) {
1732
* // error handling
1733
* }
1734
* long values[ncpus];
1735
* bpf_map_lookup_elem(per_cpu_map_fd, key, values);
1736
*/
1737
LIBBPF_API int libbpf_num_possible_cpus(void);
1738
1739
struct bpf_map_skeleton {
1740
const char *name;
1741
struct bpf_map **map;
1742
void **mmaped;
1743
struct bpf_link **link;
1744
};
1745
1746
struct bpf_prog_skeleton {
1747
const char *name;
1748
struct bpf_program **prog;
1749
struct bpf_link **link;
1750
};
1751
1752
struct bpf_object_skeleton {
1753
size_t sz; /* size of this struct, for forward/backward compatibility */
1754
1755
const char *name;
1756
const void *data;
1757
size_t data_sz;
1758
1759
struct bpf_object **obj;
1760
1761
int map_cnt;
1762
int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */
1763
struct bpf_map_skeleton *maps;
1764
1765
int prog_cnt;
1766
int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */
1767
struct bpf_prog_skeleton *progs;
1768
};
1769
1770
LIBBPF_API int
1771
bpf_object__open_skeleton(struct bpf_object_skeleton *s,
1772
const struct bpf_object_open_opts *opts);
1773
LIBBPF_API int bpf_object__load_skeleton(struct bpf_object_skeleton *s);
1774
LIBBPF_API int bpf_object__attach_skeleton(struct bpf_object_skeleton *s);
1775
LIBBPF_API void bpf_object__detach_skeleton(struct bpf_object_skeleton *s);
1776
LIBBPF_API void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s);
1777
1778
struct bpf_var_skeleton {
1779
const char *name;
1780
struct bpf_map **map;
1781
void **addr;
1782
};
1783
1784
struct bpf_object_subskeleton {
1785
size_t sz; /* size of this struct, for forward/backward compatibility */
1786
1787
const struct bpf_object *obj;
1788
1789
int map_cnt;
1790
int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */
1791
struct bpf_map_skeleton *maps;
1792
1793
int prog_cnt;
1794
int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */
1795
struct bpf_prog_skeleton *progs;
1796
1797
int var_cnt;
1798
int var_skel_sz; /* sizeof(struct bpf_var_skeleton) */
1799
struct bpf_var_skeleton *vars;
1800
};
1801
1802
LIBBPF_API int
1803
bpf_object__open_subskeleton(struct bpf_object_subskeleton *s);
1804
LIBBPF_API void
1805
bpf_object__destroy_subskeleton(struct bpf_object_subskeleton *s);
1806
1807
struct gen_loader_opts {
1808
size_t sz; /* size of this struct, for forward/backward compatibility */
1809
const char *data;
1810
const char *insns;
1811
__u32 data_sz;
1812
__u32 insns_sz;
1813
};
1814
1815
#define gen_loader_opts__last_field insns_sz
1816
LIBBPF_API int bpf_object__gen_loader(struct bpf_object *obj,
1817
struct gen_loader_opts *opts);
1818
1819
enum libbpf_tristate {
1820
TRI_NO = 0,
1821
TRI_YES = 1,
1822
TRI_MODULE = 2,
1823
};
1824
1825
struct bpf_linker_opts {
1826
/* size of this struct, for forward/backward compatibility */
1827
size_t sz;
1828
};
1829
#define bpf_linker_opts__last_field sz
1830
1831
struct bpf_linker_file_opts {
1832
/* size of this struct, for forward/backward compatibility */
1833
size_t sz;
1834
};
1835
#define bpf_linker_file_opts__last_field sz
1836
1837
struct bpf_linker;
1838
1839
LIBBPF_API struct bpf_linker *bpf_linker__new(const char *filename, struct bpf_linker_opts *opts);
1840
LIBBPF_API struct bpf_linker *bpf_linker__new_fd(int fd, struct bpf_linker_opts *opts);
1841
LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker,
1842
const char *filename,
1843
const struct bpf_linker_file_opts *opts);
1844
LIBBPF_API int bpf_linker__add_fd(struct bpf_linker *linker, int fd,
1845
const struct bpf_linker_file_opts *opts);
1846
LIBBPF_API int bpf_linker__add_buf(struct bpf_linker *linker, void *buf, size_t buf_sz,
1847
const struct bpf_linker_file_opts *opts);
1848
LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker);
1849
LIBBPF_API void bpf_linker__free(struct bpf_linker *linker);
1850
1851
/*
1852
* Custom handling of BPF program's SEC() definitions
1853
*/
1854
1855
struct bpf_prog_load_opts; /* defined in bpf.h */
1856
1857
/* Called during bpf_object__open() for each recognized BPF program. Callback
1858
* can use various bpf_program__set_*() setters to adjust whatever properties
1859
* are necessary.
1860
*/
1861
typedef int (*libbpf_prog_setup_fn_t)(struct bpf_program *prog, long cookie);
1862
1863
/* Called right before libbpf performs bpf_prog_load() to load BPF program
1864
* into the kernel. Callback can adjust opts as necessary.
1865
*/
1866
typedef int (*libbpf_prog_prepare_load_fn_t)(struct bpf_program *prog,
1867
struct bpf_prog_load_opts *opts, long cookie);
1868
1869
/* Called during skeleton attach or through bpf_program__attach(). If
1870
* auto-attach is not supported, callback should return 0 and set link to
1871
* NULL (it's not considered an error during skeleton attach, but it will be
1872
* an error for bpf_program__attach() calls). On error, error should be
1873
* returned directly and link set to NULL. On success, return 0 and set link
1874
* to a valid struct bpf_link.
1875
*/
1876
typedef int (*libbpf_prog_attach_fn_t)(const struct bpf_program *prog, long cookie,
1877
struct bpf_link **link);
1878
1879
struct libbpf_prog_handler_opts {
1880
/* size of this struct, for forward/backward compatibility */
1881
size_t sz;
1882
/* User-provided value that is passed to prog_setup_fn,
1883
* prog_prepare_load_fn, and prog_attach_fn callbacks. Allows user to
1884
* register one set of callbacks for multiple SEC() definitions and
1885
* still be able to distinguish them, if necessary. For example,
1886
* libbpf itself is using this to pass necessary flags (e.g.,
1887
* sleepable flag) to a common internal SEC() handler.
1888
*/
1889
long cookie;
1890
/* BPF program initialization callback (see libbpf_prog_setup_fn_t).
1891
* Callback is optional, pass NULL if it's not necessary.
1892
*/
1893
libbpf_prog_setup_fn_t prog_setup_fn;
1894
/* BPF program loading callback (see libbpf_prog_prepare_load_fn_t).
1895
* Callback is optional, pass NULL if it's not necessary.
1896
*/
1897
libbpf_prog_prepare_load_fn_t prog_prepare_load_fn;
1898
/* BPF program attach callback (see libbpf_prog_attach_fn_t).
1899
* Callback is optional, pass NULL if it's not necessary.
1900
*/
1901
libbpf_prog_attach_fn_t prog_attach_fn;
1902
};
1903
#define libbpf_prog_handler_opts__last_field prog_attach_fn
1904
1905
/**
1906
* @brief **libbpf_register_prog_handler()** registers a custom BPF program
1907
* SEC() handler.
1908
* @param sec section prefix for which custom handler is registered
1909
* @param prog_type BPF program type associated with specified section
1910
* @param exp_attach_type Expected BPF attach type associated with specified section
1911
* @param opts optional cookie, callbacks, and other extra options
1912
* @return Non-negative handler ID is returned on success. This handler ID has
1913
* to be passed to *libbpf_unregister_prog_handler()* to unregister such
1914
* custom handler. Negative error code is returned on error.
1915
*
1916
* *sec* defines which SEC() definitions are handled by this custom handler
1917
* registration. *sec* can have few different forms:
1918
* - if *sec* is just a plain string (e.g., "abc"), it will match only
1919
* SEC("abc"). If BPF program specifies SEC("abc/whatever") it will result
1920
* in an error;
1921
* - if *sec* is of the form "abc/", proper SEC() form is
1922
* SEC("abc/something"), where acceptable "something" should be checked by
1923
* *prog_init_fn* callback, if there are additional restrictions;
1924
* - if *sec* is of the form "abc+", it will successfully match both
1925
* SEC("abc") and SEC("abc/whatever") forms;
1926
* - if *sec* is NULL, custom handler is registered for any BPF program that
1927
* doesn't match any of the registered (custom or libbpf's own) SEC()
1928
* handlers. There could be only one such generic custom handler registered
1929
* at any given time.
1930
*
1931
* All custom handlers (except the one with *sec* == NULL) are processed
1932
* before libbpf's own SEC() handlers. It is allowed to "override" libbpf's
1933
* SEC() handlers by registering custom ones for the same section prefix
1934
* (i.e., it's possible to have custom SEC("perf_event/LLC-load-misses")
1935
* handler).
1936
*
1937
* Note, like much of global libbpf APIs (e.g., libbpf_set_print(),
1938
* libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs
1939
* to ensure synchronization if there is a risk of running this API from
1940
* multiple threads simultaneously.
1941
*/
1942
LIBBPF_API int libbpf_register_prog_handler(const char *sec,
1943
enum bpf_prog_type prog_type,
1944
enum bpf_attach_type exp_attach_type,
1945
const struct libbpf_prog_handler_opts *opts);
1946
/**
1947
* @brief *libbpf_unregister_prog_handler()* unregisters previously registered
1948
* custom BPF program SEC() handler.
1949
* @param handler_id handler ID returned by *libbpf_register_prog_handler()*
1950
* after successful registration
1951
* @return 0 on success, negative error code if handler isn't found
1952
*
1953
* Note, like much of global libbpf APIs (e.g., libbpf_set_print(),
1954
* libbpf_set_strict_mode(), etc)) these APIs are not thread-safe. User needs
1955
* to ensure synchronization if there is a risk of running this API from
1956
* multiple threads simultaneously.
1957
*/
1958
LIBBPF_API int libbpf_unregister_prog_handler(int handler_id);
1959
1960
#ifdef __cplusplus
1961
} /* extern "C" */
1962
#endif
1963
1964
#endif /* __LIBBPF_LIBBPF_H */
1965
1966