162306a36Sopenharmony_ci/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci/*
462306a36Sopenharmony_ci * Libbpf legacy APIs (either discouraged or deprecated, as mentioned in [0])
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci *   [0] https://docs.google.com/document/d/1UyjTZuPFWiPFyKk1tV5an11_iaRuec6U-ZESZ54nNTY
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci * Copyright (C) 2021 Facebook
962306a36Sopenharmony_ci */
1062306a36Sopenharmony_ci#ifndef __LIBBPF_LEGACY_BPF_H
1162306a36Sopenharmony_ci#define __LIBBPF_LEGACY_BPF_H
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#include <linux/bpf.h>
1462306a36Sopenharmony_ci#include <stdbool.h>
1562306a36Sopenharmony_ci#include <stddef.h>
1662306a36Sopenharmony_ci#include <stdint.h>
1762306a36Sopenharmony_ci#include "libbpf_common.h"
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci#ifdef __cplusplus
2062306a36Sopenharmony_ciextern "C" {
2162306a36Sopenharmony_ci#endif
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci/* As of libbpf 1.0 libbpf_set_strict_mode() and enum libbpf_struct_mode have
2462306a36Sopenharmony_ci * no effect. But they are left in libbpf_legacy.h so that applications that
2562306a36Sopenharmony_ci * prepared for libbpf 1.0 before final release by using
2662306a36Sopenharmony_ci * libbpf_set_strict_mode() still work with libbpf 1.0+ without any changes.
2762306a36Sopenharmony_ci */
2862306a36Sopenharmony_cienum libbpf_strict_mode {
2962306a36Sopenharmony_ci	/* Turn on all supported strict features of libbpf to simulate libbpf
3062306a36Sopenharmony_ci	 * v1.0 behavior.
3162306a36Sopenharmony_ci	 * This will be the default behavior in libbpf v1.0.
3262306a36Sopenharmony_ci	 */
3362306a36Sopenharmony_ci	LIBBPF_STRICT_ALL = 0xffffffff,
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci	/*
3662306a36Sopenharmony_ci	 * Disable any libbpf 1.0 behaviors. This is the default before libbpf
3762306a36Sopenharmony_ci	 * v1.0. It won't be supported anymore in v1.0, please update your
3862306a36Sopenharmony_ci	 * code so that it handles LIBBPF_STRICT_ALL mode before libbpf v1.0.
3962306a36Sopenharmony_ci	 */
4062306a36Sopenharmony_ci	LIBBPF_STRICT_NONE = 0x00,
4162306a36Sopenharmony_ci	/*
4262306a36Sopenharmony_ci	 * Return NULL pointers on error, not ERR_PTR(err).
4362306a36Sopenharmony_ci	 * Additionally, libbpf also always sets errno to corresponding Exx
4462306a36Sopenharmony_ci	 * (positive) error code.
4562306a36Sopenharmony_ci	 */
4662306a36Sopenharmony_ci	LIBBPF_STRICT_CLEAN_PTRS = 0x01,
4762306a36Sopenharmony_ci	/*
4862306a36Sopenharmony_ci	 * Return actual error codes from low-level APIs directly, not just -1.
4962306a36Sopenharmony_ci	 * Additionally, libbpf also always sets errno to corresponding Exx
5062306a36Sopenharmony_ci	 * (positive) error code.
5162306a36Sopenharmony_ci	 */
5262306a36Sopenharmony_ci	LIBBPF_STRICT_DIRECT_ERRS = 0x02,
5362306a36Sopenharmony_ci	/*
5462306a36Sopenharmony_ci	 * Enforce strict BPF program section (SEC()) names.
5562306a36Sopenharmony_ci	 * E.g., while prefiously SEC("xdp_whatever") or SEC("perf_event_blah") were
5662306a36Sopenharmony_ci	 * allowed, with LIBBPF_STRICT_SEC_PREFIX this will become
5762306a36Sopenharmony_ci	 * unrecognized by libbpf and would have to be just SEC("xdp") and
5862306a36Sopenharmony_ci	 * SEC("xdp") and SEC("perf_event").
5962306a36Sopenharmony_ci	 *
6062306a36Sopenharmony_ci	 * Note, in this mode the program pin path will be based on the
6162306a36Sopenharmony_ci	 * function name instead of section name.
6262306a36Sopenharmony_ci	 *
6362306a36Sopenharmony_ci	 * Additionally, routines in the .text section are always considered
6462306a36Sopenharmony_ci	 * sub-programs. Legacy behavior allows for a single routine in .text
6562306a36Sopenharmony_ci	 * to be a program.
6662306a36Sopenharmony_ci	 */
6762306a36Sopenharmony_ci	LIBBPF_STRICT_SEC_NAME = 0x04,
6862306a36Sopenharmony_ci	/*
6962306a36Sopenharmony_ci	 * Disable the global 'bpf_objects_list'. Maintaining this list adds
7062306a36Sopenharmony_ci	 * a race condition to bpf_object__open() and bpf_object__close().
7162306a36Sopenharmony_ci	 * Clients can maintain it on their own if it is valuable for them.
7262306a36Sopenharmony_ci	 */
7362306a36Sopenharmony_ci	LIBBPF_STRICT_NO_OBJECT_LIST = 0x08,
7462306a36Sopenharmony_ci	/*
7562306a36Sopenharmony_ci	 * Automatically bump RLIMIT_MEMLOCK using setrlimit() before the
7662306a36Sopenharmony_ci	 * first BPF program or map creation operation. This is done only if
7762306a36Sopenharmony_ci	 * kernel is too old to support memcg-based memory accounting for BPF
7862306a36Sopenharmony_ci	 * subsystem. By default, RLIMIT_MEMLOCK limit is set to RLIM_INFINITY,
7962306a36Sopenharmony_ci	 * but it can be overriden with libbpf_set_memlock_rlim() API.
8062306a36Sopenharmony_ci	 * Note that libbpf_set_memlock_rlim() needs to be called before
8162306a36Sopenharmony_ci	 * the very first bpf_prog_load(), bpf_map_create() or bpf_object__load()
8262306a36Sopenharmony_ci	 * operation.
8362306a36Sopenharmony_ci	 */
8462306a36Sopenharmony_ci	LIBBPF_STRICT_AUTO_RLIMIT_MEMLOCK = 0x10,
8562306a36Sopenharmony_ci	/*
8662306a36Sopenharmony_ci	 * Error out on any SEC("maps") map definition, which are deprecated
8762306a36Sopenharmony_ci	 * in favor of BTF-defined map definitions in SEC(".maps").
8862306a36Sopenharmony_ci	 */
8962306a36Sopenharmony_ci	LIBBPF_STRICT_MAP_DEFINITIONS = 0x20,
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci	__LIBBPF_STRICT_LAST,
9262306a36Sopenharmony_ci};
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ciLIBBPF_API int libbpf_set_strict_mode(enum libbpf_strict_mode mode);
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci/**
9762306a36Sopenharmony_ci * @brief **libbpf_get_error()** extracts the error code from the passed
9862306a36Sopenharmony_ci * pointer
9962306a36Sopenharmony_ci * @param ptr pointer returned from libbpf API function
10062306a36Sopenharmony_ci * @return error code; or 0 if no error occured
10162306a36Sopenharmony_ci *
10262306a36Sopenharmony_ci * Note, as of libbpf 1.0 this function is not necessary and not recommended
10362306a36Sopenharmony_ci * to be used. Libbpf doesn't return error code embedded into the pointer
10462306a36Sopenharmony_ci * itself. Instead, NULL is returned on error and error code is passed through
10562306a36Sopenharmony_ci * thread-local errno variable. **libbpf_get_error()** is just returning -errno
10662306a36Sopenharmony_ci * value if it receives NULL, which is correct only if errno hasn't been
10762306a36Sopenharmony_ci * modified between libbpf API call and corresponding **libbpf_get_error()**
10862306a36Sopenharmony_ci * call. Prefer to check return for NULL and use errno directly.
10962306a36Sopenharmony_ci *
11062306a36Sopenharmony_ci * This API is left in libbpf 1.0 to allow applications that were 1.0-ready
11162306a36Sopenharmony_ci * before final libbpf 1.0 without needing to change them.
11262306a36Sopenharmony_ci */
11362306a36Sopenharmony_ciLIBBPF_API long libbpf_get_error(const void *ptr);
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci#define DECLARE_LIBBPF_OPTS LIBBPF_OPTS
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci/* "Discouraged" APIs which don't follow consistent libbpf naming patterns.
11862306a36Sopenharmony_ci * They are normally a trivial aliases or wrappers for proper APIs and are
11962306a36Sopenharmony_ci * left to minimize unnecessary disruption for users of libbpf. But they
12062306a36Sopenharmony_ci * shouldn't be used going forward.
12162306a36Sopenharmony_ci */
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_cistruct bpf_program;
12462306a36Sopenharmony_cistruct bpf_map;
12562306a36Sopenharmony_cistruct btf;
12662306a36Sopenharmony_cistruct btf_ext;
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ciLIBBPF_API struct btf *libbpf_find_kernel_btf(void);
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ciLIBBPF_API enum bpf_prog_type bpf_program__get_type(const struct bpf_program *prog);
13162306a36Sopenharmony_ciLIBBPF_API enum bpf_attach_type bpf_program__get_expected_attach_type(const struct bpf_program *prog);
13262306a36Sopenharmony_ciLIBBPF_API const char *bpf_map__get_pin_path(const struct bpf_map *map);
13362306a36Sopenharmony_ciLIBBPF_API const void *btf__get_raw_data(const struct btf *btf, __u32 *size);
13462306a36Sopenharmony_ciLIBBPF_API const void *btf_ext__get_raw_data(const struct btf_ext *btf_ext, __u32 *size);
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci#ifdef __cplusplus
13762306a36Sopenharmony_ci} /* extern "C" */
13862306a36Sopenharmony_ci#endif
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci#endif /* __LIBBPF_LEGACY_BPF_H */
141