tools/testing/selftests/bpf/prog_tests/bpf_iter.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/prog_tests/bpf_iter.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/prog_tests/bpf_iter.c- Extension
.c- Size
- 46261 bytes
- Lines
- 1798
- 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.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
test_progs.hunistd.hsys/syscall.htask_local_storage_helpers.hbpf_iter_ipv6_route.skel.hbpf_iter_netlink.skel.hbpf_iter_bpf_map.skel.hbpf_iter_tasks.skel.hbpf_iter_task_stack.skel.hbpf_iter_task_file.skel.hbpf_iter_task_vmas.skel.hbpf_iter_task_btf.skel.hbpf_iter_tcp4.skel.hbpf_iter_tcp6.skel.hbpf_iter_udp4.skel.hbpf_iter_udp6.skel.hbpf_iter_unix.skel.hbpf_iter_vma_offset.skel.hbpf_iter_test_kern1.skel.hbpf_iter_test_kern2.skel.hbpf_iter_test_kern3.skel.hbpf_iter_test_kern4.skel.hbpf_iter_bpf_hash_map.skel.hbpf_iter_bpf_percpu_hash_map.skel.hbpf_iter_bpf_array_map.skel.hbpf_iter_bpf_percpu_array_map.skel.hbpf_iter_bpf_sk_storage_helpers.skel.hbpf_iter_bpf_sk_storage_map.skel.hbpf_iter_test_kern5.skel.hbpf_iter_test_kern6.skel.hbpf_iter_bpf_link.skel.hbpf_iter_ksym.skel.h
Detected Declarations
struct key_tstruct key_tfunction test_btf_id_or_nullfunction do_dummy_read_optsfunction do_dummy_readfunction do_read_map_iter_fdfunction read_fd_into_bufferfunction test_ipv6_routefunction test_netlinkfunction test_bpf_mapfunction check_bpf_link_infofunction test_task_common_nocheckfunction test_task_commonfunction test_task_tidfunction test_task_pidfunction test_task_pidfdfunction test_task_sleepablefunction test_task_stackfunction test_task_filefunction do_btf_readfunction test_task_btffunction test_tcp4function test_tcp6function test_udp4function test_udp6function test_unixfunction do_read_with_fdfunction test_anon_iterfunction do_readfunction test_file_iterfunction test_overflowfunction test_bpf_hash_mapfunction test_bpf_percpu_hash_mapfunction test_bpf_array_mapfunction test_bpf_array_map_iter_fdfunction test_bpf_percpu_array_mapfunction test_bpf_sk_storage_deletefunction test_bpf_sk_storage_getfunction test_bpf_sk_storage_map_iter_fdfunction test_bpf_sk_storage_mapfunction test_rdonly_buf_out_of_boundfunction test_buf_neg_offsetfunction test_link_iterfunction test_ksym_iterfunction str_strip_first_linefunction test_task_vma_commonfunction test_task_vma_dead_taskfunction test_bpf_sockmap_map_iter_fd
Annotated Snippet
struct key_t {
int a;
int b;
int c;
} key;
char buf[64];
skel = bpf_iter_bpf_hash_map__open();
if (!ASSERT_OK_PTR(skel, "bpf_iter_bpf_hash_map__open"))
return;
skel->bss->in_test_mode = true;
err = bpf_iter_bpf_hash_map__load(skel);
if (!ASSERT_OK(err, "bpf_iter_bpf_hash_map__load"))
goto out;
/* iterator with hashmap2 and hashmap3 should fail */
memset(&linfo, 0, sizeof(linfo));
linfo.map.map_fd = bpf_map__fd(skel->maps.hashmap2);
opts.link_info = &linfo;
opts.link_info_len = sizeof(linfo);
link = bpf_program__attach_iter(skel->progs.dump_bpf_hash_map, &opts);
if (!ASSERT_ERR_PTR(link, "attach_iter"))
goto out;
linfo.map.map_fd = bpf_map__fd(skel->maps.hashmap3);
link = bpf_program__attach_iter(skel->progs.dump_bpf_hash_map, &opts);
if (!ASSERT_ERR_PTR(link, "attach_iter"))
goto out;
/* hashmap1 should be good, update map values here */
map_fd = bpf_map__fd(skel->maps.hashmap1);
for (i = 0; i < bpf_map__max_entries(skel->maps.hashmap1); i++) {
key.a = i + 1;
key.b = i + 2;
key.c = i + 3;
val = i + 4;
expected_key_a += key.a;
expected_key_b += key.b;
expected_val += val;
err = bpf_map_update_elem(map_fd, &key, &val, BPF_ANY);
if (!ASSERT_OK(err, "map_update"))
goto out;
}
/* Sleepable program is prohibited for hash map iterator */
linfo.map.map_fd = map_fd;
link = bpf_program__attach_iter(skel->progs.sleepable_dummy_dump, &opts);
if (!ASSERT_ERR_PTR(link, "attach_sleepable_prog_to_iter"))
goto out;
linfo.map.map_fd = map_fd;
link = bpf_program__attach_iter(skel->progs.dump_bpf_hash_map, &opts);
if (!ASSERT_OK_PTR(link, "attach_iter"))
goto out;
iter_fd = bpf_iter_create(bpf_link__fd(link));
if (!ASSERT_GE(iter_fd, 0, "create_iter"))
goto free_link;
/* do some tests */
while ((len = read(iter_fd, buf, sizeof(buf))) > 0)
;
if (!ASSERT_GE(len, 0, "read"))
goto close_iter;
/* test results */
if (!ASSERT_EQ(skel->bss->key_sum_a, expected_key_a, "key_sum_a"))
goto close_iter;
if (!ASSERT_EQ(skel->bss->key_sum_b, expected_key_b, "key_sum_b"))
goto close_iter;
if (!ASSERT_EQ(skel->bss->val_sum, expected_val, "val_sum"))
goto close_iter;
close_iter:
close(iter_fd);
free_link:
bpf_link__destroy(link);
out:
bpf_iter_bpf_hash_map__destroy(skel);
}
static void test_bpf_percpu_hash_map(void)
{
__u32 expected_key_a = 0, expected_key_b = 0;
DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts);
struct bpf_iter_bpf_percpu_hash_map *skel;
int err, i, j, len, map_fd, iter_fd;
Annotation
- Immediate include surface: `test_progs.h`, `unistd.h`, `sys/syscall.h`, `task_local_storage_helpers.h`, `bpf_iter_ipv6_route.skel.h`, `bpf_iter_netlink.skel.h`, `bpf_iter_bpf_map.skel.h`, `bpf_iter_tasks.skel.h`.
- Detected declarations: `struct key_t`, `struct key_t`, `function test_btf_id_or_null`, `function do_dummy_read_opts`, `function do_dummy_read`, `function do_read_map_iter_fd`, `function read_fd_into_buffer`, `function test_ipv6_route`, `function test_netlink`, `function test_bpf_map`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
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.