tools/testing/selftests/net/bind_bhash.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/net/bind_bhash.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/net/bind_bhash.c- Extension
.c- Size
- 2962 bytes
- Lines
- 146
- 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
unistd.hstdio.hnetdb.hpthread.hstring.hstdbool.h
Detected Declarations
function bind_socketfunction main
Annotated Snippet
if (err) {
perror("setsockopt failed");
goto err_free_info;
}
}
err = bind(sock_fd, res->ai_addr, res->ai_addrlen);
if (err) {
perror("failed to bind to port");
goto err_free_info;
}
freeaddrinfo(res);
return sock_fd;
err_free_info:
freeaddrinfo(res);
cleanup:
close(sock_fd);
return err;
}
static void *setup(void *arg)
{
int sock_fd, i;
int *array = (int *)arg;
for (i = 0; i < MAX_CONNECTIONS; i++) {
sock_fd = bind_socket(SO_REUSEPORT, setup_addr);
if (sock_fd < 0) {
ret = sock_fd;
pthread_exit(&ret);
}
array[i] = sock_fd;
}
return NULL;
}
int main(int argc, const char *argv[])
{
int listener_fd, sock_fd, i, j;
pthread_t tid[MAX_THREADS];
clock_t begin, end;
if (argc != 4) {
printf("Usage: listener <port> <ipv6 | ipv4> <bind-addr>\n");
return -1;
}
port = argv[1];
use_v6 = strcmp(argv[2], "ipv6") == 0;
bind_addr = argv[3];
setup_addr = use_v6 ? setup_addr_v6 : setup_addr_v4;
listener_fd = bind_socket(SO_REUSEPORT, setup_addr);
if (listen(listener_fd, 100) < 0) {
perror("listen failed");
return -1;
}
/* Set up threads to populate the bhash table entry for the port */
for (i = 0; i < MAX_THREADS; i++)
pthread_create(&tid[i], NULL, setup, fd_array[i]);
for (i = 0; i < MAX_THREADS; i++)
pthread_join(tid[i], NULL);
if (ret)
goto done;
begin = clock();
/* Bind to the same port on a different address */
sock_fd = bind_socket(0, bind_addr);
if (sock_fd < 0)
goto done;
end = clock();
printf("time spent = %f\n", (double)(end - begin) / CLOCKS_PER_SEC);
/* clean up */
close(sock_fd);
done:
close(listener_fd);
for (i = 0; i < MAX_THREADS; i++) {
for (j = 0; i < MAX_THREADS; i++)
close(fd_array[i][j]);
}
Annotation
- Immediate include surface: `unistd.h`, `stdio.h`, `netdb.h`, `pthread.h`, `string.h`, `stdbool.h`.
- Detected declarations: `function bind_socket`, `function main`.
- 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.