tools/testing/selftests/bpf/prog_tests/sock_iter_batch.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/prog_tests/sock_iter_batch.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/prog_tests/sock_iter_batch.c- Extension
.c- Size
- 29020 bytes
- Lines
- 995
- 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
poll.htest_progs.hnetwork_helpers.hsock_iter_batch.skel.h
Detected Declarations
struct iter_outstruct sock_countstruct test_casefunction insertfunction read_nfunction socket_cookiefunction was_seenfunction get_seen_socketfunction get_nth_socketfunction destroyfunction get_seen_countfunction check_n_were_seen_oncefunction accept_from_onefunction remove_seenfunction remove_seen_establishedfunction remove_unseenfunction remove_unseen_establishedfunction remove_allfunction remove_all_establishedfunction add_somefunction add_some_establishedfunction force_reallocfunction force_realloc_establishedfunction do_resume_testfunction do_resume_testsfunction do_testfunction test_sock_iter_batch
Annotated Snippet
struct iter_out {
int idx;
__u64 cookie;
} __packed;
struct sock_count {
__u64 cookie;
int count;
};
static int insert(__u64 cookie, struct sock_count counts[], int counts_len)
{
int insert = -1;
int i = 0;
for (; i < counts_len; i++) {
if (!counts[i].cookie) {
insert = i;
} else if (counts[i].cookie == cookie) {
insert = i;
break;
}
}
if (insert < 0)
return insert;
counts[insert].cookie = cookie;
counts[insert].count++;
return counts[insert].count;
}
static int read_n(int iter_fd, int n, struct sock_count counts[],
int counts_len)
{
struct iter_out out;
int nread = 1;
int i = 0;
for (; nread > 0 && (n < 0 || i < n); i++) {
nread = read(iter_fd, &out, sizeof(out));
if (!nread || !ASSERT_EQ(nread, sizeof(out), "nread"))
break;
ASSERT_GE(insert(out.cookie, counts, counts_len), 0, "insert");
}
ASSERT_TRUE(n < 0 || i == n, "n < 0 || i == n");
return i;
}
static __u64 socket_cookie(int fd)
{
__u64 cookie;
socklen_t cookie_len = sizeof(cookie);
if (!ASSERT_OK(getsockopt(fd, SOL_SOCKET, SO_COOKIE, &cookie,
&cookie_len), "getsockopt(SO_COOKIE)"))
return 0;
return cookie;
}
static bool was_seen(int fd, struct sock_count counts[], int counts_len)
{
__u64 cookie = socket_cookie(fd);
int i = 0;
for (; cookie && i < counts_len; i++)
if (cookie == counts[i].cookie)
return true;
return false;
}
static int get_seen_socket(int *fds, struct sock_count counts[], int n)
{
int i = 0;
for (; i < n; i++)
if (was_seen(fds[i], counts, n))
return i;
return -1;
}
static int get_nth_socket(int *fds, int fds_len, struct bpf_link *link, int n)
{
int i, nread, iter_fd;
int nth_sock_idx = -1;
struct iter_out out;
Annotation
- Immediate include surface: `poll.h`, `test_progs.h`, `network_helpers.h`, `sock_iter_batch.skel.h`.
- Detected declarations: `struct iter_out`, `struct sock_count`, `struct test_case`, `function insert`, `function read_n`, `function socket_cookie`, `function was_seen`, `function get_seen_socket`, `function get_nth_socket`, `function destroy`.
- 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.