tools/lib/bpf/bpf.c
Source file repositories/reference/linux-study-clean/tools/lib/bpf/bpf.c
File Facts
- System
- Linux kernel
- Corpus path
tools/lib/bpf/bpf.c- Extension
.c- Size
- 42948 bytes
- Lines
- 1485
- 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.hstring.hmemory.hunistd.hasm/unistd.herrno.hlinux/bpf.hlinux/filter.hlinux/kernel.hlimits.hsys/resource.hbpf.hlibbpf.hlibbpf_internal.h
Detected Declarations
function ptr_to_u64function sys_bpf_extfunction sys_bpf_ext_fdfunction probe_sys_bpf_extfunction sys_bpffunction sys_bpf_fdfunction sys_bpf_prog_loadfunction bpf_ktime_get_coarse_nsfunction libbpf_set_memlock_rlimfunction bump_rlimit_memlockfunction bpf_map_createfunction alloc_zero_tailing_infofunction bpf_prog_loadfunction bpf_map_update_elemfunction bpf_map_lookup_elemfunction bpf_map_lookup_elem_flagsfunction bpf_map_lookup_and_delete_elemfunction bpf_map_lookup_and_delete_elem_flagsfunction bpf_map_delete_elemfunction bpf_map_delete_elem_flagsfunction bpf_map_get_next_keyfunction bpf_map_freezefunction bpf_map_batch_commonfunction bpf_map_delete_batchfunction bpf_map_lookup_batchfunction bpf_map_lookup_and_delete_batchfunction bpf_map_update_batchfunction bpf_obj_pin_optsfunction bpf_obj_pinfunction bpf_obj_getfunction bpf_obj_get_optsfunction bpf_prog_attachfunction bpf_prog_attach_optsfunction bpf_prog_detach_optsfunction bpf_prog_detachfunction bpf_prog_detach2function bpf_link_createfunction bpf_link_detachfunction bpf_link_updatefunction bpf_iter_createfunction bpf_prog_query_optsfunction bpf_prog_queryfunction bpf_prog_test_run_optsfunction bpf_obj_get_next_idfunction bpf_prog_get_next_idfunction bpf_map_get_next_idfunction bpf_btf_get_next_idfunction bpf_link_get_next_id
Annotated Snippet
if (!finfo) {
errno = E2BIG;
goto done;
}
attr.func_info = ptr_to_u64(finfo);
attr.func_info_rec_size = func_info_rec_size;
} else if (!linfo && attr.line_info_cnt &&
attr.line_info_rec_size < line_info_rec_size) {
linfo = alloc_zero_tailing_info(line_info,
attr.line_info_cnt,
line_info_rec_size,
attr.line_info_rec_size);
if (!linfo) {
errno = E2BIG;
goto done;
}
attr.line_info = ptr_to_u64(linfo);
attr.line_info_rec_size = line_info_rec_size;
} else {
break;
}
fd = sys_bpf_prog_load(&attr, attr_sz, attempts);
OPTS_SET(opts, log_true_size, attr.log_true_size);
if (fd >= 0)
goto done;
}
if (log_level == 0 && log_buf) {
/* log_level == 0 with non-NULL log_buf requires retrying on error
* with log_level == 1 and log_buf/log_buf_size set, to get details of
* failure
*/
attr.log_buf = ptr_to_u64(log_buf);
attr.log_size = log_size;
attr.log_level = 1;
fd = sys_bpf_prog_load(&attr, attr_sz, attempts);
OPTS_SET(opts, log_true_size, attr.log_true_size);
}
done:
/* free() doesn't affect errno, so we don't need to restore it */
free(finfo);
free(linfo);
return libbpf_err_errno(fd);
}
int bpf_map_update_elem(int fd, const void *key, const void *value,
__u64 flags)
{
const size_t attr_sz = offsetofend(union bpf_attr, flags);
union bpf_attr attr;
int ret;
memset(&attr, 0, attr_sz);
attr.map_fd = fd;
attr.key = ptr_to_u64(key);
attr.value = ptr_to_u64(value);
attr.flags = flags;
ret = sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, attr_sz);
return libbpf_err_errno(ret);
}
int bpf_map_lookup_elem(int fd, const void *key, void *value)
{
const size_t attr_sz = offsetofend(union bpf_attr, flags);
union bpf_attr attr;
int ret;
memset(&attr, 0, attr_sz);
attr.map_fd = fd;
attr.key = ptr_to_u64(key);
attr.value = ptr_to_u64(value);
ret = sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, attr_sz);
return libbpf_err_errno(ret);
}
int bpf_map_lookup_elem_flags(int fd, const void *key, void *value, __u64 flags)
{
const size_t attr_sz = offsetofend(union bpf_attr, flags);
union bpf_attr attr;
int ret;
memset(&attr, 0, attr_sz);
attr.map_fd = fd;
attr.key = ptr_to_u64(key);
Annotation
- Immediate include surface: `stdlib.h`, `string.h`, `memory.h`, `unistd.h`, `asm/unistd.h`, `errno.h`, `linux/bpf.h`, `linux/filter.h`.
- Detected declarations: `function ptr_to_u64`, `function sys_bpf_ext`, `function sys_bpf_ext_fd`, `function probe_sys_bpf_ext`, `function sys_bpf`, `function sys_bpf_fd`, `function sys_bpf_prog_load`, `function bpf_ktime_get_coarse_ns`, `function libbpf_set_memlock_rlim`, `function bump_rlimit_memlock`.
- 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.