Skip to content

Commit 3e67803

Browse files
committed
testing/nettest: Add a TCP case for sends that time out on I/O buffers.
test_tcp_sndtimeo_iob: a loopback connection whose reader does not read, and a sender with SO_SNDTIMEO. The sends fill the write buffers, then 10 sends time out in the wait for an I/O buffer. After both ends close, the free I/O buffers in /proc/iobinfo must come back to the number before the test. The case is skipped without /proc/iobinfo, or when a send fails with ENOMEM (no free devif callback: connections of earlier cases in TIME_WAIT can keep theirs). Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
1 parent 1f00614 commit 3e67803

5 files changed

Lines changed: 319 additions & 0 deletions

File tree

‎testing/nettest/CMakeLists.txt‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ if(CONFIG_TESTING_NET_TEST)
3434
list(APPEND SRCS ${CMAKE_CURRENT_LIST_DIR}/tcp/test_tcp_connect_ipv6.c)
3535
endif()
3636

37+
if(CONFIG_NET_IPv4
38+
AND CONFIG_NET_SOCKOPTS
39+
AND CONFIG_NET_TCP_WRITE_BUFFERS)
40+
list(APPEND SRCS ${CMAKE_CURRENT_LIST_DIR}/tcp/test_tcp_sndtimeo.c)
41+
endif()
42+
3743
nuttx_add_application(
3844
NAME
3945
cmocka_net_tcp

‎testing/nettest/Makefile‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ ifeq ($(CONFIG_NET_IPv6), y)
4343
CSRCS += tcp/test_tcp_connect_ipv6.c
4444
endif
4545

46+
ifeq ($(CONFIG_NET_IPv4)$(CONFIG_NET_SOCKOPTS)$(CONFIG_NET_TCP_WRITE_BUFFERS),yyy)
47+
CSRCS += tcp/test_tcp_sndtimeo.c
48+
endif
49+
4650
endif
4751

4852
ifeq ($(CONFIG_TESTING_NET_UDP),y)

‎testing/nettest/tcp/test_tcp.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ int main(int argc, FAR char *argv[])
5555
cmocka_unit_test_setup_teardown(test_tcp_accept_inherit,
5656
test_tcp_bind_listen_setup,
5757
test_tcp_bind_listen_teardown),
58+
# ifdef CONFIG_NET_TCP_WRITE_BUFFERS
59+
cmocka_unit_test_setup_teardown(test_tcp_sndtimeo_iob,
60+
test_tcp_sndtimeo_setup,
61+
test_tcp_sndtimeo_teardown),
62+
# endif
5863
# endif
5964
cmocka_unit_test_setup_teardown(test_tcp_listen_again,
6065
test_tcp_bind_listen_setup,

‎testing/nettest/tcp/test_tcp.h‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,26 @@ void test_tcp_bind_reuseaddr_listener(FAR void **state);
108108
****************************************************************************/
109109

110110
void test_tcp_accept_inherit(FAR void **state);
111+
112+
# ifdef CONFIG_NET_TCP_WRITE_BUFFERS
113+
/****************************************************************************
114+
* Name: test_tcp_sndtimeo_setup
115+
****************************************************************************/
116+
117+
int test_tcp_sndtimeo_setup(FAR void **state);
118+
119+
/****************************************************************************
120+
* Name: test_tcp_sndtimeo_teardown
121+
****************************************************************************/
122+
123+
int test_tcp_sndtimeo_teardown(FAR void **state);
124+
125+
/****************************************************************************
126+
* Name: test_tcp_sndtimeo_iob
127+
****************************************************************************/
128+
129+
void test_tcp_sndtimeo_iob(FAR void **state);
130+
# endif
111131
# endif
112132

113133
/****************************************************************************
Lines changed: 284 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,284 @@
1+
/****************************************************************************
2+
* apps/testing/nettest/tcp/test_tcp_sndtimeo.c
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed to the Apache Software Foundation (ASF) under one or more
7+
* contributor license agreements. See the NOTICE file distributed with
8+
* this work for additional information regarding copyright ownership. The
9+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
* "License"); you may not use this file except in compliance with the
11+
* License. You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
* License for the specific language governing permissions and limitations
19+
* under the License.
20+
*
21+
****************************************************************************/
22+
23+
/* send() with SO_SNDTIMEO on a loopback connection whose reader does not
24+
* read: the sends that time out in the wait for an I/O buffer must not
25+
* keep I/O buffers after the sockets close.
26+
*/
27+
28+
/****************************************************************************
29+
* Included Files
30+
****************************************************************************/
31+
32+
#include <errno.h>
33+
#include <fcntl.h>
34+
#include <limits.h>
35+
#include <netinet/in.h>
36+
#include <poll.h>
37+
#include <setjmp.h>
38+
#include <stdarg.h>
39+
#include <stddef.h>
40+
#include <stdint.h>
41+
#include <stdio.h>
42+
#include <stdlib.h>
43+
#include <string.h>
44+
#include <unistd.h>
45+
#include <sys/socket.h>
46+
#include <sys/time.h>
47+
#include <cmocka.h>
48+
49+
#include "test_tcp.h"
50+
#include "utils.h"
51+
52+
/****************************************************************************
53+
* Pre-processor Definitions
54+
****************************************************************************/
55+
56+
#define TEST_IOBINFO "/proc/iobinfo"
57+
#define TEST_SNDTIMEO 100 /* ms */
58+
#define TEST_TIMEOUTS 10 /* sends that time out */
59+
#define TEST_MAXSENDS 2000
60+
#define TEST_SENDSIZE 4096
61+
#define TEST_POLL 1000 /* ms, loopback answers at once */
62+
#define TEST_RECOVER 30 /* tries of 100 ms */
63+
64+
/****************************************************************************
65+
* Private Types
66+
****************************************************************************/
67+
68+
struct tcp_sndtimeo_state_s
69+
{
70+
int fds[3];
71+
};
72+
73+
/****************************************************************************
74+
* Private Functions
75+
****************************************************************************/
76+
77+
/****************************************************************************
78+
* Name: tcp_sndtimeo_nfree
79+
*
80+
* Description:
81+
* Return the free I/O buffers (nfree of /proc/iobinfo), or -1.
82+
*
83+
****************************************************************************/
84+
85+
static int tcp_sndtimeo_nfree(void)
86+
{
87+
char buf[128];
88+
FAR char *line;
89+
ssize_t n;
90+
int nfree;
91+
int fd;
92+
93+
fd = open(TEST_IOBINFO, O_RDONLY | O_CLOEXEC);
94+
if (fd < 0)
95+
{
96+
return -1;
97+
}
98+
99+
n = read(fd, buf, sizeof(buf) - 1);
100+
close(fd);
101+
if (n <= 0)
102+
{
103+
return -1;
104+
}
105+
106+
buf[n] = '\0';
107+
line = strchr(buf, '\n');
108+
if (line == NULL || sscanf(line + 1, "%*d %d", &nfree) != 1)
109+
{
110+
return -1;
111+
}
112+
113+
return nfree;
114+
}
115+
116+
/****************************************************************************
117+
* Public Functions
118+
****************************************************************************/
119+
120+
/****************************************************************************
121+
* Name: test_tcp_sndtimeo_setup
122+
****************************************************************************/
123+
124+
int test_tcp_sndtimeo_setup(FAR void **state)
125+
{
126+
FAR struct tcp_sndtimeo_state_s *st = malloc(sizeof(*st));
127+
128+
assert_non_null(st);
129+
st->fds[0] = -1;
130+
st->fds[1] = -1;
131+
st->fds[2] = -1;
132+
*state = st;
133+
return 0;
134+
}
135+
136+
/****************************************************************************
137+
* Name: test_tcp_sndtimeo_teardown
138+
****************************************************************************/
139+
140+
int test_tcp_sndtimeo_teardown(FAR void **state)
141+
{
142+
FAR struct tcp_sndtimeo_state_s *st = *state;
143+
int i;
144+
145+
for (i = 0; i < 3; i++)
146+
{
147+
if (st->fds[i] >= 0)
148+
{
149+
close(st->fds[i]);
150+
}
151+
}
152+
153+
free(st);
154+
return 0;
155+
}
156+
157+
/****************************************************************************
158+
* Name: test_tcp_sndtimeo_iob
159+
*
160+
* Description:
161+
* The sender fills the I/O buffers that a send can take, then
162+
* TEST_TIMEOUTS sends time out. After both ends close, the free I/O
163+
* buffers come back to the number before the test. The case is
164+
* skipped without /proc/iobinfo, or when a send fails with ENOMEM.
165+
*
166+
****************************************************************************/
167+
168+
void test_tcp_sndtimeo_iob(FAR void **state)
169+
{
170+
FAR struct tcp_sndtimeo_state_s *st = *state;
171+
struct sockaddr_in addr;
172+
socklen_t addrlen = sizeof(addr);
173+
struct timeval tv;
174+
struct pollfd pfd;
175+
FAR char *buf;
176+
int timeouts = 0;
177+
int before;
178+
int after;
179+
int i;
180+
181+
before = tcp_sndtimeo_nfree();
182+
if (before < 0)
183+
{
184+
skip();
185+
}
186+
187+
buf = malloc(TEST_SENDSIZE);
188+
assert_non_null(buf);
189+
memset(buf, 'w', TEST_SENDSIZE);
190+
191+
st->fds[0] = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
192+
assert_return_code(st->fds[0], errno);
193+
nettest_lo_addr((FAR struct sockaddr *)&addr, AF_INET);
194+
addr.sin_port = 0;
195+
assert_return_code(bind(st->fds[0], (FAR struct sockaddr *)&addr,
196+
sizeof(addr)), errno);
197+
assert_return_code(listen(st->fds[0], 1), errno);
198+
assert_return_code(getsockname(st->fds[0], (FAR struct sockaddr *)&addr,
199+
&addrlen), errno);
200+
201+
st->fds[1] = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
202+
assert_return_code(st->fds[1], errno);
203+
assert_return_code(connect(st->fds[1], (FAR struct sockaddr *)&addr,
204+
addrlen), errno);
205+
pfd.fd = st->fds[0];
206+
pfd.events = POLLIN;
207+
pfd.revents = 0;
208+
assert_int_equal(poll(&pfd, 1, TEST_POLL), 1);
209+
st->fds[2] = accept4(st->fds[0], NULL, NULL, SOCK_CLOEXEC);
210+
assert_return_code(st->fds[2], errno);
211+
212+
tv.tv_sec = 0;
213+
tv.tv_usec = TEST_SNDTIMEO * 1000;
214+
assert_return_code(setsockopt(st->fds[1], SOL_SOCKET, SO_SNDTIMEO, &tv,
215+
sizeof(tv)), errno);
216+
217+
/* The sends must wait for an I/O buffer, not for the send buffer. A
218+
* stack without SO_SNDBUF has no send limit.
219+
*/
220+
221+
i = TEST_MAXSENDS * TEST_SENDSIZE;
222+
if (setsockopt(st->fds[1], SOL_SOCKET, SO_SNDBUF, &i, sizeof(i)) < 0)
223+
{
224+
assert_int_equal(errno, ENOPROTOOPT);
225+
}
226+
227+
/* The reader does not read. The sends fill the write buffers, then
228+
* each send waits for an I/O buffer and times out.
229+
*/
230+
231+
for (i = 0; i < TEST_MAXSENDS && timeouts < TEST_TIMEOUTS; i++)
232+
{
233+
ssize_t ret = send(st->fds[1], buf, TEST_SENDSIZE, 0);
234+
235+
if (ret < 0 && errno == ENOMEM)
236+
{
237+
/* No free callback or write buffer (connections of earlier
238+
* cases in TIME_WAIT can keep the callbacks): not this case.
239+
*/
240+
241+
free(buf);
242+
skip();
243+
}
244+
245+
if (ret < 0)
246+
{
247+
assert_int_equal(errno, EAGAIN);
248+
timeouts++;
249+
}
250+
}
251+
252+
free(buf);
253+
254+
/* The sends did not take the I/O buffers down to the throttle (a send
255+
* limit or a large pool): they did not wait for an I/O buffer.
256+
*/
257+
258+
if (timeouts < TEST_TIMEOUTS ||
259+
tcp_sndtimeo_nfree() > CONFIG_IOB_THROTTLE)
260+
{
261+
skip();
262+
}
263+
264+
for (i = 0; i < 3; i++)
265+
{
266+
close(st->fds[i]);
267+
st->fds[i] = -1;
268+
}
269+
270+
/* The connections free their buffers when they end */
271+
272+
for (i = 0; i < TEST_RECOVER; i++)
273+
{
274+
after = tcp_sndtimeo_nfree();
275+
if (after >= before)
276+
{
277+
break;
278+
}
279+
280+
usleep(100 * 1000);
281+
}
282+
283+
assert_in_range(after, before, INT_MAX);
284+
}

0 commit comments

Comments
 (0)