tools/lib/bpf/libbpf_internal.h
Source file repositories/reference/linux-study-clean/tools/lib/bpf/libbpf_internal.h
File Facts
- System
- Linux kernel
- Corpus path
tools/lib/bpf/libbpf_internal.h- Extension
.h- Size
- 22738 bytes
- Lines
- 776
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
stdlib.hbyteswap.hlimits.herrno.hlinux/err.hfcntl.hunistd.hsys/syscall.hlibelf.hrelo_core.hlibbpf.hbtf.h
Detected Declarations
struct bpf_linkstruct btfstruct btf_typestruct btf_map_defstruct kern_feature_cachestruct btf_ext_infostruct btf_ext_headerstruct btf_extstruct btf_ext_info_secstruct bpf_func_info_minstruct bpf_line_info_minstruct btf_field_descstruct btf_field_iterstruct elf_fdenum map_def_partsenum kern_feature_idenum kern_feature_resultenum btf_field_iter_kindfunction Copyrightfunction reallocarrayfunction libbpf_strlcpyfunction btf_func_linkagefunction btf_type_infofunction libbpf_is_mem_zeroedfunction libbpf_validate_optsfunction bpf_func_info_bswapfunction bpf_line_info_bswapfunction bpf_core_relo_bswapfunction libbpf_errfunction libbpf_err_errnofunction str_is_emptyfunction is_ldimm64_insnfunction bpf_insn_bswapfunction dup_good_fdfunction fcntlfunction sys_dup3function sys_memfd_createfunction reuse_fdfunction is_pow_of_2function ror32
Annotated Snippet
struct bpf_link {
int (*detach)(struct bpf_link *link);
void (*dealloc)(struct bpf_link *link);
char *pin_path; /* NULL, if not pinned */
int fd; /* hook FD, -1 if not applicable */
bool disconnected;
};
/*
* Re-implement glibc's reallocarray() for libbpf internal-only use.
* reallocarray(), unfortunately, is not available in all versions of glibc,
* so requires extra feature detection and using reallocarray() stub from
* <tools/libc_compat.h> and COMPAT_NEED_REALLOCARRAY. All this complicates
* build of libbpf unnecessarily and is just a maintenance burden. Instead,
* it's trivial to implement libbpf-specific internal version and use it
* throughout libbpf.
*/
static inline void *libbpf_reallocarray(void *ptr, size_t nmemb, size_t size)
{
size_t total;
#if __has_builtin(__builtin_mul_overflow)
if (unlikely(__builtin_mul_overflow(nmemb, size, &total)))
return NULL;
#else
if (size == 0 || nmemb > ULONG_MAX / size)
return NULL;
total = nmemb * size;
#endif
return realloc(ptr, total);
}
/* Copy up to sz - 1 bytes from zero-terminated src string and ensure that dst
* is zero-terminated string no matter what (unless sz == 0, in which case
* it's a no-op). It's conceptually close to FreeBSD's strlcpy(), but differs
* in what is returned. Given this is internal helper, it's trivial to extend
* this, when necessary. Use this instead of strncpy inside libbpf source code.
*/
static inline void libbpf_strlcpy(char *dst, const char *src, size_t sz)
{
size_t i;
if (sz == 0)
return;
sz--;
for (i = 0; i < sz && src[i]; i++)
dst[i] = src[i];
dst[i] = '\0';
}
__u32 get_kernel_version(void);
struct btf;
struct btf_type;
struct btf_type *btf_type_by_id(const struct btf *btf, __u32 type_id);
const char *btf_kind_str(const struct btf_type *t);
const struct btf_type *skip_mods_and_typedefs(const struct btf *btf, __u32 id, __u32 *res_id);
const struct btf_header *btf_header(const struct btf *btf);
void btf_set_base_btf(struct btf *btf, const struct btf *base_btf);
int btf_relocate(struct btf *btf, const struct btf *base_btf, __u32 **id_map);
bool btf_type_is_traceable_func(const struct btf *btf, const struct btf_type *t);
static inline enum btf_func_linkage btf_func_linkage(const struct btf_type *t)
{
return (enum btf_func_linkage)(int)btf_vlen(t);
}
static inline __u32 btf_type_info(int kind, int vlen, int kflag)
{
return (kflag << 31) | (kind << 24) | vlen;
}
enum map_def_parts {
MAP_DEF_MAP_TYPE = 0x001,
MAP_DEF_KEY_TYPE = 0x002,
MAP_DEF_KEY_SIZE = 0x004,
MAP_DEF_VALUE_TYPE = 0x008,
MAP_DEF_VALUE_SIZE = 0x010,
MAP_DEF_MAX_ENTRIES = 0x020,
MAP_DEF_MAP_FLAGS = 0x040,
MAP_DEF_NUMA_NODE = 0x080,
MAP_DEF_PINNING = 0x100,
MAP_DEF_INNER_MAP = 0x200,
MAP_DEF_MAP_EXTRA = 0x400,
MAP_DEF_ALL = 0x7ff, /* combination of all above */
};
Annotation
- Immediate include surface: `stdlib.h`, `byteswap.h`, `limits.h`, `errno.h`, `linux/err.h`, `fcntl.h`, `unistd.h`, `sys/syscall.h`.
- Detected declarations: `struct bpf_link`, `struct btf`, `struct btf_type`, `struct btf_map_def`, `struct kern_feature_cache`, `struct btf_ext_info`, `struct btf_ext_header`, `struct btf_ext`, `struct btf_ext_info_sec`, `struct bpf_func_info_min`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.