tools/testing/selftests/bpf/prog_tests/sockmap_basic.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/prog_tests/sockmap_basic.c- Extension
.c- Size
- 40950 bytes
- Lines
- 1455
- 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
error.hlinux/tcp.hlinux/socket.hsys/epoll.htest_progs.htest_skmsg_load_helpers.skel.htest_sockmap_update.skel.htest_sockmap_invalid_update.skel.htest_sockmap_skb_verdict_attach.skel.htest_sockmap_progs_query.skel.htest_sockmap_pass_prog.skel.htest_sockmap_drop_prog.skel.htest_sockmap_change_tail.skel.htest_sockmap_msg_pop_data.skel.hbpf_iter_sockmap.skel.hsockmap_helpers.h
Detected Declarations
function connected_socket_v4function compare_cookiesfunction test_sockmap_create_update_freefunction test_sockmap_vsock_delete_on_closefunction test_skmsg_helpersfunction test_skmsg_helpers_with_linkfunction test_sockmap_updatefunction test_sockmap_invalid_updatefunction test_sockmap_copyfunction test_sockmap_skb_verdict_attachfunction test_sockmap_skb_verdict_attach_with_linkfunction query_prog_idfunction test_sockmap_progs_queryfunction test_sockmap_skb_verdict_shutdownfunction do_test_sockmap_skb_verdict_fionreadfunction test_sockmap_skb_verdict_fionreadfunction test_sockmap_skb_verdict_change_tailfunction test_sockmap_msg_verdict_pop_datafunction test_sockmap_skb_verdict_peek_helperfunction test_sockmap_skb_verdict_peekfunction test_sockmap_skb_verdict_peek_with_linkfunction test_sockmap_unconnected_unixfunction test_sockmap_many_socketfunction test_sockmap_many_mapsfunction test_sockmap_same_sockfunction test_sockmap_skb_verdict_vsock_pollfunction test_sockmap_vsock_unconnectedfunction test_sockmap_zcfunction test_sockmap_copied_seqfunction wait_for_fionreadfunction test_sockmap_multi_channelsfunction test_sockmap_basic
Annotated Snippet
if (err && errno == ENOENT) {
err = bpf_map_lookup_elem(dst_fd, &i, &dst_cookie);
ASSERT_ERR(err, "map_lookup_elem(dst)");
ASSERT_EQ(errno, ENOENT, "map_lookup_elem(dst)");
continue;
}
if (!ASSERT_OK(err, "lookup_elem(src)"))
continue;
err = bpf_map_lookup_elem(dst_fd, &i, &dst_cookie);
if (!ASSERT_OK(err, "lookup_elem(dst)"))
continue;
ASSERT_EQ(dst_cookie, src_cookie, "cookie mismatch");
}
}
/* Create a map, populate it with one socket, and free the map. */
static void test_sockmap_create_update_free(enum bpf_map_type map_type)
{
const int zero = 0;
int s, map, err;
s = connected_socket_v4();
if (!ASSERT_GE(s, 0, "connected_socket_v4"))
return;
map = bpf_map_create(map_type, NULL, sizeof(int), sizeof(int), 1, NULL);
if (!ASSERT_GE(map, 0, "bpf_map_create"))
goto out;
err = bpf_map_update_elem(map, &zero, &s, BPF_NOEXIST);
if (!ASSERT_OK(err, "bpf_map_update"))
goto out;
out:
close(map);
close(s);
}
static void test_sockmap_vsock_delete_on_close(void)
{
int map, c, p, err, zero = 0;
map = bpf_map_create(BPF_MAP_TYPE_SOCKMAP, NULL, sizeof(int),
sizeof(int), 1, NULL);
if (!ASSERT_OK_FD(map, "bpf_map_create"))
return;
err = create_pair(AF_VSOCK, SOCK_STREAM, &c, &p);
if (!ASSERT_OK(err, "create_pair"))
goto close_map;
if (xbpf_map_update_elem(map, &zero, &c, BPF_NOEXIST))
goto close_socks;
xclose(c);
xclose(p);
err = create_pair(AF_VSOCK, SOCK_STREAM, &c, &p);
if (!ASSERT_OK(err, "create_pair"))
goto close_map;
err = bpf_map_update_elem(map, &zero, &c, BPF_NOEXIST);
ASSERT_OK(err, "after close(), bpf_map_update");
close_socks:
xclose(c);
xclose(p);
close_map:
xclose(map);
}
static void test_skmsg_helpers(enum bpf_map_type map_type)
{
struct test_skmsg_load_helpers *skel;
int err, map, verdict;
skel = test_skmsg_load_helpers__open_and_load();
if (!ASSERT_OK_PTR(skel, "test_skmsg_load_helpers__open_and_load"))
return;
verdict = bpf_program__fd(skel->progs.prog_msg_verdict);
map = bpf_map__fd(skel->maps.sock_map);
err = bpf_prog_attach(verdict, map, BPF_SK_MSG_VERDICT, 0);
if (!ASSERT_OK(err, "bpf_prog_attach"))
goto out;
err = bpf_prog_detach2(verdict, map, BPF_SK_MSG_VERDICT);
Annotation
- Immediate include surface: `error.h`, `linux/tcp.h`, `linux/socket.h`, `sys/epoll.h`, `test_progs.h`, `test_skmsg_load_helpers.skel.h`, `test_sockmap_update.skel.h`, `test_sockmap_invalid_update.skel.h`.
- Detected declarations: `function connected_socket_v4`, `function compare_cookies`, `function test_sockmap_create_update_free`, `function test_sockmap_vsock_delete_on_close`, `function test_skmsg_helpers`, `function test_skmsg_helpers_with_link`, `function test_sockmap_update`, `function test_sockmap_invalid_update`, `function test_sockmap_copy`, `function test_sockmap_skb_verdict_attach`.
- 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.