tools/testing/selftests/net/tcp_inq.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/net/tcp_inq.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/net/tcp_inq.c- Extension
.c- Size
- 4204 bytes
- Lines
- 180
- 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.hnetinet/in.hnetinet/tcp.hpthread.hstdio.herrno.hstdlib.hstring.hsys/socket.hunistd.h
Detected Declarations
function setup_loopback_addrfunction main
Annotated Snippet
if (fd == -1) {
perror("accept");
break;
}
do {
r = send(fd, buf, BUF_SIZE, 0);
} while (r < 0 && errno == EINTR);
if (r < 0)
perror("send");
if (r != BUF_SIZE)
fprintf(stderr, "can only send %d bytes\n", r);
/* TCP_INQ can overestimate in-queue by one byte if we send
* the FIN packet. Sleep for 1 second, so that the client
* likely invoked recvmsg().
*/
sleep(1);
close(fd);
}
free(buf);
close(server_fd);
pthread_exit(0);
}
int main(int argc, char *argv[])
{
struct sockaddr_storage listen_addr, addr;
int c, one = 1, inq = -1;
pthread_t server_thread;
char cmsgbuf[CMSG_SIZE];
struct iovec iov[1];
struct cmsghdr *cm;
struct msghdr msg;
int server_fd, fd;
char *buf;
while ((c = getopt(argc, argv, "46p:")) != -1) {
switch (c) {
case '4':
family = PF_INET;
addr_len = sizeof(struct sockaddr_in);
break;
case '6':
family = PF_INET6;
addr_len = sizeof(struct sockaddr_in6);
break;
case 'p':
port = atoi(optarg);
break;
}
}
server_fd = socket(family, SOCK_STREAM, 0);
if (server_fd < 0)
error(1, errno, "server socket");
setup_loopback_addr(family, &listen_addr);
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR,
&one, sizeof(one)) != 0)
error(1, errno, "setsockopt(SO_REUSEADDR)");
if (bind(server_fd, (const struct sockaddr *)&listen_addr,
addr_len) == -1)
error(1, errno, "bind");
if (listen(server_fd, 128) == -1)
error(1, errno, "listen");
if (pthread_create(&server_thread, NULL, start_server,
(void *)(unsigned long)server_fd) != 0)
error(1, errno, "pthread_create");
fd = socket(family, SOCK_STREAM, 0);
if (fd < 0)
error(1, errno, "client socket");
setup_loopback_addr(family, &addr);
if (connect(fd, (const struct sockaddr *)&addr, addr_len) == -1)
error(1, errno, "connect");
if (setsockopt(fd, SOL_TCP, TCP_INQ, &one, sizeof(one)) != 0)
error(1, errno, "setsockopt(TCP_INQ)");
msg.msg_name = NULL;
msg.msg_namelen = 0;
msg.msg_iov = iov;
msg.msg_iovlen = 1;
msg.msg_control = cmsgbuf;
msg.msg_controllen = sizeof(cmsgbuf);
msg.msg_flags = 0;
buf = malloc(BUF_SIZE);
iov[0].iov_base = buf;
iov[0].iov_len = BUF_SIZE / 2;
if (recvmsg(fd, &msg, 0) != iov[0].iov_len)
Annotation
- Immediate include surface: `error.h`, `netinet/in.h`, `netinet/tcp.h`, `pthread.h`, `stdio.h`, `errno.h`, `stdlib.h`, `string.h`.
- Detected declarations: `function setup_loopback_addr`, `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.