1*3e5580c4SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2702353b5SSoheil Hassas Yeganeh /*
3702353b5SSoheil Hassas Yeganeh * Copyright 2018 Google Inc.
4702353b5SSoheil Hassas Yeganeh * Author: Soheil Hassas Yeganeh (soheil@google.com)
5702353b5SSoheil Hassas Yeganeh *
6702353b5SSoheil Hassas Yeganeh * Simple example on how to use TCP_INQ and TCP_CM_INQ.
7702353b5SSoheil Hassas Yeganeh */
8702353b5SSoheil Hassas Yeganeh #define _GNU_SOURCE
9702353b5SSoheil Hassas Yeganeh
10702353b5SSoheil Hassas Yeganeh #include <error.h>
11702353b5SSoheil Hassas Yeganeh #include <netinet/in.h>
12702353b5SSoheil Hassas Yeganeh #include <netinet/tcp.h>
13702353b5SSoheil Hassas Yeganeh #include <pthread.h>
14702353b5SSoheil Hassas Yeganeh #include <stdio.h>
15702353b5SSoheil Hassas Yeganeh #include <errno.h>
16702353b5SSoheil Hassas Yeganeh #include <stdlib.h>
17702353b5SSoheil Hassas Yeganeh #include <string.h>
18702353b5SSoheil Hassas Yeganeh #include <sys/socket.h>
19702353b5SSoheil Hassas Yeganeh #include <unistd.h>
20702353b5SSoheil Hassas Yeganeh
21702353b5SSoheil Hassas Yeganeh #ifndef TCP_INQ
22702353b5SSoheil Hassas Yeganeh #define TCP_INQ 36
23702353b5SSoheil Hassas Yeganeh #endif
24702353b5SSoheil Hassas Yeganeh
25702353b5SSoheil Hassas Yeganeh #ifndef TCP_CM_INQ
26702353b5SSoheil Hassas Yeganeh #define TCP_CM_INQ TCP_INQ
27702353b5SSoheil Hassas Yeganeh #endif
28702353b5SSoheil Hassas Yeganeh
29702353b5SSoheil Hassas Yeganeh #define BUF_SIZE 8192
30702353b5SSoheil Hassas Yeganeh #define CMSG_SIZE 32
31702353b5SSoheil Hassas Yeganeh
32702353b5SSoheil Hassas Yeganeh static int family = AF_INET6;
33702353b5SSoheil Hassas Yeganeh static socklen_t addr_len = sizeof(struct sockaddr_in6);
34702353b5SSoheil Hassas Yeganeh static int port = 4974;
35702353b5SSoheil Hassas Yeganeh
setup_loopback_addr(int family,struct sockaddr_storage * sockaddr)36702353b5SSoheil Hassas Yeganeh static void setup_loopback_addr(int family, struct sockaddr_storage *sockaddr)
37702353b5SSoheil Hassas Yeganeh {
38702353b5SSoheil Hassas Yeganeh struct sockaddr_in6 *addr6 = (void *) sockaddr;
39702353b5SSoheil Hassas Yeganeh struct sockaddr_in *addr4 = (void *) sockaddr;
40702353b5SSoheil Hassas Yeganeh
41702353b5SSoheil Hassas Yeganeh switch (family) {
42702353b5SSoheil Hassas Yeganeh case PF_INET:
43702353b5SSoheil Hassas Yeganeh memset(addr4, 0, sizeof(*addr4));
44702353b5SSoheil Hassas Yeganeh addr4->sin_family = AF_INET;
45702353b5SSoheil Hassas Yeganeh addr4->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
46702353b5SSoheil Hassas Yeganeh addr4->sin_port = htons(port);
47702353b5SSoheil Hassas Yeganeh break;
48702353b5SSoheil Hassas Yeganeh case PF_INET6:
49702353b5SSoheil Hassas Yeganeh memset(addr6, 0, sizeof(*addr6));
50702353b5SSoheil Hassas Yeganeh addr6->sin6_family = AF_INET6;
51702353b5SSoheil Hassas Yeganeh addr6->sin6_addr = in6addr_loopback;
52702353b5SSoheil Hassas Yeganeh addr6->sin6_port = htons(port);
53702353b5SSoheil Hassas Yeganeh break;
54702353b5SSoheil Hassas Yeganeh default:
55702353b5SSoheil Hassas Yeganeh error(1, 0, "illegal family");
56702353b5SSoheil Hassas Yeganeh }
57702353b5SSoheil Hassas Yeganeh }
58702353b5SSoheil Hassas Yeganeh
start_server(void * arg)59702353b5SSoheil Hassas Yeganeh void *start_server(void *arg)
60702353b5SSoheil Hassas Yeganeh {
61702353b5SSoheil Hassas Yeganeh int server_fd = (int)(unsigned long)arg;
62702353b5SSoheil Hassas Yeganeh struct sockaddr_in addr;
63702353b5SSoheil Hassas Yeganeh socklen_t addrlen = sizeof(addr);
64702353b5SSoheil Hassas Yeganeh char *buf;
65702353b5SSoheil Hassas Yeganeh int fd;
66702353b5SSoheil Hassas Yeganeh int r;
67702353b5SSoheil Hassas Yeganeh
68702353b5SSoheil Hassas Yeganeh buf = malloc(BUF_SIZE);
69702353b5SSoheil Hassas Yeganeh
70702353b5SSoheil Hassas Yeganeh for (;;) {
71702353b5SSoheil Hassas Yeganeh fd = accept(server_fd, (struct sockaddr *)&addr, &addrlen);
72702353b5SSoheil Hassas Yeganeh if (fd == -1) {
73702353b5SSoheil Hassas Yeganeh perror("accept");
74702353b5SSoheil Hassas Yeganeh break;
75702353b5SSoheil Hassas Yeganeh }
76702353b5SSoheil Hassas Yeganeh do {
77702353b5SSoheil Hassas Yeganeh r = send(fd, buf, BUF_SIZE, 0);
78702353b5SSoheil Hassas Yeganeh } while (r < 0 && errno == EINTR);
79702353b5SSoheil Hassas Yeganeh if (r < 0)
80702353b5SSoheil Hassas Yeganeh perror("send");
81702353b5SSoheil Hassas Yeganeh if (r != BUF_SIZE)
82702353b5SSoheil Hassas Yeganeh fprintf(stderr, "can only send %d bytes\n", r);
83702353b5SSoheil Hassas Yeganeh /* TCP_INQ can overestimate in-queue by one byte if we send
84702353b5SSoheil Hassas Yeganeh * the FIN packet. Sleep for 1 second, so that the client
85702353b5SSoheil Hassas Yeganeh * likely invoked recvmsg().
86702353b5SSoheil Hassas Yeganeh */
87702353b5SSoheil Hassas Yeganeh sleep(1);
88702353b5SSoheil Hassas Yeganeh close(fd);
89702353b5SSoheil Hassas Yeganeh }
90702353b5SSoheil Hassas Yeganeh
91702353b5SSoheil Hassas Yeganeh free(buf);
92702353b5SSoheil Hassas Yeganeh close(server_fd);
93702353b5SSoheil Hassas Yeganeh pthread_exit(0);
94702353b5SSoheil Hassas Yeganeh }
95702353b5SSoheil Hassas Yeganeh
main(int argc,char * argv[])96702353b5SSoheil Hassas Yeganeh int main(int argc, char *argv[])
97702353b5SSoheil Hassas Yeganeh {
98702353b5SSoheil Hassas Yeganeh struct sockaddr_storage listen_addr, addr;
99702353b5SSoheil Hassas Yeganeh int c, one = 1, inq = -1;
100702353b5SSoheil Hassas Yeganeh pthread_t server_thread;
101702353b5SSoheil Hassas Yeganeh char cmsgbuf[CMSG_SIZE];
102702353b5SSoheil Hassas Yeganeh struct iovec iov[1];
103702353b5SSoheil Hassas Yeganeh struct cmsghdr *cm;
104702353b5SSoheil Hassas Yeganeh struct msghdr msg;
105702353b5SSoheil Hassas Yeganeh int server_fd, fd;
106702353b5SSoheil Hassas Yeganeh char *buf;
107702353b5SSoheil Hassas Yeganeh
108702353b5SSoheil Hassas Yeganeh while ((c = getopt(argc, argv, "46p:")) != -1) {
109702353b5SSoheil Hassas Yeganeh switch (c) {
110702353b5SSoheil Hassas Yeganeh case '4':
111702353b5SSoheil Hassas Yeganeh family = PF_INET;
112702353b5SSoheil Hassas Yeganeh addr_len = sizeof(struct sockaddr_in);
113702353b5SSoheil Hassas Yeganeh break;
114702353b5SSoheil Hassas Yeganeh case '6':
115702353b5SSoheil Hassas Yeganeh family = PF_INET6;
116702353b5SSoheil Hassas Yeganeh addr_len = sizeof(struct sockaddr_in6);
117702353b5SSoheil Hassas Yeganeh break;
118702353b5SSoheil Hassas Yeganeh case 'p':
119702353b5SSoheil Hassas Yeganeh port = atoi(optarg);
120702353b5SSoheil Hassas Yeganeh break;
121702353b5SSoheil Hassas Yeganeh }
122702353b5SSoheil Hassas Yeganeh }
123702353b5SSoheil Hassas Yeganeh
124702353b5SSoheil Hassas Yeganeh server_fd = socket(family, SOCK_STREAM, 0);
125702353b5SSoheil Hassas Yeganeh if (server_fd < 0)
126702353b5SSoheil Hassas Yeganeh error(1, errno, "server socket");
127702353b5SSoheil Hassas Yeganeh setup_loopback_addr(family, &listen_addr);
128702353b5SSoheil Hassas Yeganeh if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR,
129702353b5SSoheil Hassas Yeganeh &one, sizeof(one)) != 0)
130702353b5SSoheil Hassas Yeganeh error(1, errno, "setsockopt(SO_REUSEADDR)");
131702353b5SSoheil Hassas Yeganeh if (bind(server_fd, (const struct sockaddr *)&listen_addr,
132702353b5SSoheil Hassas Yeganeh addr_len) == -1)
133702353b5SSoheil Hassas Yeganeh error(1, errno, "bind");
134702353b5SSoheil Hassas Yeganeh if (listen(server_fd, 128) == -1)
135702353b5SSoheil Hassas Yeganeh error(1, errno, "listen");
136702353b5SSoheil Hassas Yeganeh if (pthread_create(&server_thread, NULL, start_server,
137702353b5SSoheil Hassas Yeganeh (void *)(unsigned long)server_fd) != 0)
138702353b5SSoheil Hassas Yeganeh error(1, errno, "pthread_create");
139702353b5SSoheil Hassas Yeganeh
140702353b5SSoheil Hassas Yeganeh fd = socket(family, SOCK_STREAM, 0);
141702353b5SSoheil Hassas Yeganeh if (fd < 0)
142702353b5SSoheil Hassas Yeganeh error(1, errno, "client socket");
143702353b5SSoheil Hassas Yeganeh setup_loopback_addr(family, &addr);
144702353b5SSoheil Hassas Yeganeh if (connect(fd, (const struct sockaddr *)&addr, addr_len) == -1)
145702353b5SSoheil Hassas Yeganeh error(1, errno, "connect");
146702353b5SSoheil Hassas Yeganeh if (setsockopt(fd, SOL_TCP, TCP_INQ, &one, sizeof(one)) != 0)
147702353b5SSoheil Hassas Yeganeh error(1, errno, "setsockopt(TCP_INQ)");
148702353b5SSoheil Hassas Yeganeh
149702353b5SSoheil Hassas Yeganeh msg.msg_name = NULL;
150702353b5SSoheil Hassas Yeganeh msg.msg_namelen = 0;
151702353b5SSoheil Hassas Yeganeh msg.msg_iov = iov;
152702353b5SSoheil Hassas Yeganeh msg.msg_iovlen = 1;
153702353b5SSoheil Hassas Yeganeh msg.msg_control = cmsgbuf;
154702353b5SSoheil Hassas Yeganeh msg.msg_controllen = sizeof(cmsgbuf);
155702353b5SSoheil Hassas Yeganeh msg.msg_flags = 0;
156702353b5SSoheil Hassas Yeganeh
157702353b5SSoheil Hassas Yeganeh buf = malloc(BUF_SIZE);
158702353b5SSoheil Hassas Yeganeh iov[0].iov_base = buf;
159702353b5SSoheil Hassas Yeganeh iov[0].iov_len = BUF_SIZE / 2;
160702353b5SSoheil Hassas Yeganeh
161702353b5SSoheil Hassas Yeganeh if (recvmsg(fd, &msg, 0) != iov[0].iov_len)
162702353b5SSoheil Hassas Yeganeh error(1, errno, "recvmsg");
163702353b5SSoheil Hassas Yeganeh if (msg.msg_flags & MSG_CTRUNC)
164702353b5SSoheil Hassas Yeganeh error(1, 0, "control message is truncated");
165702353b5SSoheil Hassas Yeganeh
166702353b5SSoheil Hassas Yeganeh for (cm = CMSG_FIRSTHDR(&msg); cm; cm = CMSG_NXTHDR(&msg, cm))
167702353b5SSoheil Hassas Yeganeh if (cm->cmsg_level == SOL_TCP && cm->cmsg_type == TCP_CM_INQ)
168702353b5SSoheil Hassas Yeganeh inq = *((int *) CMSG_DATA(cm));
169702353b5SSoheil Hassas Yeganeh
170702353b5SSoheil Hassas Yeganeh if (inq != BUF_SIZE - iov[0].iov_len) {
171702353b5SSoheil Hassas Yeganeh fprintf(stderr, "unexpected inq: %d\n", inq);
172702353b5SSoheil Hassas Yeganeh exit(1);
173702353b5SSoheil Hassas Yeganeh }
174702353b5SSoheil Hassas Yeganeh
175702353b5SSoheil Hassas Yeganeh printf("PASSED\n");
176702353b5SSoheil Hassas Yeganeh free(buf);
177702353b5SSoheil Hassas Yeganeh close(fd);
178702353b5SSoheil Hassas Yeganeh return 0;
179702353b5SSoheil Hassas Yeganeh }
180