Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
0002 
0003 /*
0004  * Libbpf legacy APIs (either discouraged or deprecated, as mentioned in [0])
0005  *
0006  *   [0] https://docs.google.com/document/d/1UyjTZuPFWiPFyKk1tV5an11_iaRuec6U-ZESZ54nNTY
0007  *
0008  * Copyright (C) 2021 Facebook
0009  */
0010 #ifndef __LIBBPF_LEGACY_BPF_H
0011 #define __LIBBPF_LEGACY_BPF_H
0012 
0013 #include <linux/bpf.h>
0014 #include <stdbool.h>
0015 #include <stddef.h>
0016 #include <stdint.h>
0017 #include "libbpf_common.h"
0018 
0019 #ifdef __cplusplus
0020 extern "C" {
0021 #endif
0022 
0023 /* As of libbpf 1.0 libbpf_set_strict_mode() and enum libbpf_struct_mode have
0024  * no effect. But they are left in libbpf_legacy.h so that applications that
0025  * prepared for libbpf 1.0 before final release by using
0026  * libbpf_set_strict_mode() still work with libbpf 1.0+ without any changes.
0027  */
0028 enum libbpf_strict_mode {
0029     /* Turn on all supported strict features of libbpf to simulate libbpf
0030      * v1.0 behavior.
0031      * This will be the default behavior in libbpf v1.0.
0032      */
0033     LIBBPF_STRICT_ALL = 0xffffffff,
0034 
0035     /*
0036      * Disable any libbpf 1.0 behaviors. This is the default before libbpf
0037      * v1.0. It won't be supported anymore in v1.0, please update your
0038      * code so that it handles LIBBPF_STRICT_ALL mode before libbpf v1.0.
0039      */
0040     LIBBPF_STRICT_NONE = 0x00,
0041     /*
0042      * Return NULL pointers on error, not ERR_PTR(err).
0043      * Additionally, libbpf also always sets errno to corresponding Exx
0044      * (positive) error code.
0045      */
0046     LIBBPF_STRICT_CLEAN_PTRS = 0x01,
0047     /*
0048      * Return actual error codes from low-level APIs directly, not just -1.
0049      * Additionally, libbpf also always sets errno to corresponding Exx
0050      * (positive) error code.
0051      */
0052     LIBBPF_STRICT_DIRECT_ERRS = 0x02,
0053     /*
0054      * Enforce strict BPF program section (SEC()) names.
0055      * E.g., while prefiously SEC("xdp_whatever") or SEC("perf_event_blah") were
0056      * allowed, with LIBBPF_STRICT_SEC_PREFIX this will become
0057      * unrecognized by libbpf and would have to be just SEC("xdp") and
0058      * SEC("xdp") and SEC("perf_event").
0059      *
0060      * Note, in this mode the program pin path will be based on the
0061      * function name instead of section name.
0062      *
0063      * Additionally, routines in the .text section are always considered
0064      * sub-programs. Legacy behavior allows for a single routine in .text
0065      * to be a program.
0066      */
0067     LIBBPF_STRICT_SEC_NAME = 0x04,
0068     /*
0069      * Disable the global 'bpf_objects_list'. Maintaining this list adds
0070      * a race condition to bpf_object__open() and bpf_object__close().
0071      * Clients can maintain it on their own if it is valuable for them.
0072      */
0073     LIBBPF_STRICT_NO_OBJECT_LIST = 0x08,
0074     /*
0075      * Automatically bump RLIMIT_MEMLOCK using setrlimit() before the
0076      * first BPF program or map creation operation. This is done only if
0077      * kernel is too old to support memcg-based memory accounting for BPF
0078      * subsystem. By default, RLIMIT_MEMLOCK limit is set to RLIM_INFINITY,
0079      * but it can be overriden with libbpf_set_memlock_rlim() API.
0080      * Note that libbpf_set_memlock_rlim() needs to be called before
0081      * the very first bpf_prog_load(), bpf_map_create() or bpf_object__load()
0082      * operation.
0083      */
0084     LIBBPF_STRICT_AUTO_RLIMIT_MEMLOCK = 0x10,
0085     /*
0086      * Error out on any SEC("maps") map definition, which are deprecated
0087      * in favor of BTF-defined map definitions in SEC(".maps").
0088      */
0089     LIBBPF_STRICT_MAP_DEFINITIONS = 0x20,
0090 
0091     __LIBBPF_STRICT_LAST,
0092 };
0093 
0094 LIBBPF_API int libbpf_set_strict_mode(enum libbpf_strict_mode mode);
0095 
0096 /**
0097  * @brief **libbpf_get_error()** extracts the error code from the passed
0098  * pointer
0099  * @param ptr pointer returned from libbpf API function
0100  * @return error code; or 0 if no error occured
0101  *
0102  * Note, as of libbpf 1.0 this function is not necessary and not recommended
0103  * to be used. Libbpf doesn't return error code embedded into the pointer
0104  * itself. Instead, NULL is returned on error and error code is passed through
0105  * thread-local errno variable. **libbpf_get_error()** is just returning -errno
0106  * value if it receives NULL, which is correct only if errno hasn't been
0107  * modified between libbpf API call and corresponding **libbpf_get_error()**
0108  * call. Prefer to check return for NULL and use errno directly.
0109  *
0110  * This API is left in libbpf 1.0 to allow applications that were 1.0-ready
0111  * before final libbpf 1.0 without needing to change them.
0112  */
0113 LIBBPF_API long libbpf_get_error(const void *ptr);
0114 
0115 #define DECLARE_LIBBPF_OPTS LIBBPF_OPTS
0116 
0117 /* "Discouraged" APIs which don't follow consistent libbpf naming patterns.
0118  * They are normally a trivial aliases or wrappers for proper APIs and are
0119  * left to minimize unnecessary disruption for users of libbpf. But they
0120  * shouldn't be used going forward.
0121  */
0122 
0123 struct bpf_program;
0124 struct bpf_map;
0125 struct btf;
0126 struct btf_ext;
0127 
0128 LIBBPF_API enum bpf_prog_type bpf_program__get_type(const struct bpf_program *prog);
0129 LIBBPF_API enum bpf_attach_type bpf_program__get_expected_attach_type(const struct bpf_program *prog);
0130 LIBBPF_API const char *bpf_map__get_pin_path(const struct bpf_map *map);
0131 LIBBPF_API const void *btf__get_raw_data(const struct btf *btf, __u32 *size);
0132 LIBBPF_API const void *btf_ext__get_raw_data(const struct btf_ext *btf_ext, __u32 *size);
0133 
0134 #ifdef __cplusplus
0135 } /* extern "C" */
0136 #endif
0137 
0138 #endif /* __LIBBPF_LEGACY_BPF_H */