Skip to content

Commit e836427

Browse files
rluboscarlescufi
authored andcommitted
tests: net: socket: reuseaddr_reuseport: Fix userspace testing
net_context_foreach() is not a systemcall, therefore should not be used from userspace threads. Usage of this API made SO_REUSADDR/SO_REUSEPORT userspace tests unfunctional (crashing). Fix this by using CONFIG_NET_TCP_TIME_WAIT_DELAY instead for tests that involve TCP connections. Other tests don't really need to wait as there's no teardown delay on UDP or unconnected TCP contexts. Additionally, remove the platform exclude for qemu_x86 platform, as this was the sole reason the tests were crashing on that platform. Signed-off-by: Robert Lubos <[email protected]>
1 parent e639695 commit e836427

File tree

3 files changed

+9
-61
lines changed

3 files changed

+9
-61
lines changed

tests/net/socket/reuseaddr_reuseport/prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ CONFIG_TEST_RANDOM_GENERATOR=y
3939
# Reduce the retry count, so the close always finishes within a second
4040
CONFIG_NET_TCP_RETRY_COUNT=3
4141
CONFIG_NET_TCP_INIT_RETRANSMISSION_TIMEOUT=120
42+
CONFIG_NET_TCP_TIME_WAIT_DELAY=200
4243

4344
CONFIG_MAIN_STACK_SIZE=2048
4445
CONFIG_ZTEST=y

tests/net/socket/reuseaddr_reuseport/src/main.c

Lines changed: 8 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -237,51 +237,6 @@ static void test_recv_fail(int sock, void *buf, size_t max_len, int flags)
237237
zassert_equal(errno, EAGAIN, "recvfrom() returned unexpected errno (%d)", errno);
238238
}
239239

240-
static void calc_net_context(struct net_context *context, void *user_data)
241-
{
242-
int *count = user_data;
243-
244-
(*count)++;
245-
}
246-
247-
/* Wait until the number of TCP contexts reaches a certain level
248-
* exp_num_contexts : The number of contexts to wait for
249-
* timeout : The time to wait for
250-
*/
251-
int wait_for_n_tcp_contexts(int exp_num_contexts, k_timeout_t timeout)
252-
{
253-
uint32_t start_time = k_uptime_get_32();
254-
uint32_t time_diff;
255-
int context_count = 0;
256-
257-
/* After the client socket closing, the context count should be 1 less */
258-
net_context_foreach(calc_net_context, &context_count);
259-
260-
time_diff = k_uptime_get_32() - start_time;
261-
262-
/* Eventually the client socket should be cleaned up */
263-
while (context_count != exp_num_contexts) {
264-
context_count = 0;
265-
net_context_foreach(calc_net_context, &context_count);
266-
k_sleep(K_MSEC(50));
267-
time_diff = k_uptime_get_32() - start_time;
268-
269-
if (K_MSEC(time_diff).ticks > timeout.ticks) {
270-
return -ETIMEDOUT;
271-
}
272-
}
273-
274-
return 0;
275-
}
276-
277-
static void test_context_cleanup(void)
278-
{
279-
zassert_equal(wait_for_n_tcp_contexts(0, TCP_TEARDOWN_TIMEOUT),
280-
0,
281-
"Not all TCP contexts properly cleaned up");
282-
}
283-
284-
285240
ZTEST_USER(socket_reuseaddr_test_suite, test_enable_disable)
286241
{
287242
int server_sock = -1;
@@ -321,8 +276,6 @@ ZTEST_USER(socket_reuseaddr_test_suite, test_enable_disable)
321276
zassert_equal(value, (int) true, "SO_REUSEADDR not correctly set, returned %d", value);
322277

323278
close(server_sock);
324-
325-
test_context_cleanup();
326279
}
327280

328281

@@ -359,8 +312,6 @@ static void test_reuseaddr_unspecified_specified_common(sa_family_t family,
359312

360313
close(server_sock1);
361314
close(server_sock2);
362-
363-
test_context_cleanup();
364315
}
365316

366317
ZTEST_USER(socket_reuseaddr_test_suite, test_ipv4_first_unspecified)
@@ -440,8 +391,6 @@ static void test_reuseaddr_tcp_listening_common(sa_family_t family,
440391

441392
close(server_sock1);
442393
close(server_sock2);
443-
444-
test_context_cleanup();
445394
}
446395

447396
ZTEST_USER(socket_reuseaddr_test_suite, test_ipv4_tcp_unspecified_listening)
@@ -526,7 +475,10 @@ static void test_reuseaddr_tcp_tcp_time_wait_common(sa_family_t family,
526475
close(client_sock);
527476
close(server_sock);
528477

529-
test_context_cleanup();
478+
/* Connection is in TIME_WAIT state, context will be released
479+
* after K_MSEC(CONFIG_NET_TCP_TIME_WAIT_DELAY), so wait for it.
480+
*/
481+
k_sleep(K_MSEC(CONFIG_NET_TCP_TIME_WAIT_DELAY));
530482
}
531483

532484
ZTEST_USER(socket_reuseaddr_test_suite, test_ipv4_tcp_time_wait_unspecified)
@@ -601,8 +553,6 @@ ZTEST_USER(socket_reuseport_test_suite, test_enable_disable)
601553
zassert_equal(value, (int) true, "SO_REUSEPORT not correctly set, returned %d", value);
602554

603555
close(server_sock);
604-
605-
test_context_cleanup();
606556
}
607557

608558

@@ -644,8 +594,6 @@ static void test_reuseport_unspecified_specified_common(sa_family_t family,
644594

645595
close(server_sock1);
646596
close(server_sock2);
647-
648-
test_context_cleanup();
649597
}
650598

651599
ZTEST_USER(socket_reuseport_test_suite, test_ipv4_both_unspecified_bad)
@@ -879,8 +827,6 @@ static void test_reuseport_udp_server_client_common(sa_family_t family,
879827
close(accept_sock);
880828
close(client_sock);
881829
close(server_sock);
882-
883-
test_context_cleanup();
884830
}
885831

886832
ZTEST_USER(socket_reuseport_test_suite, test_ipv4_udp_bad_both_not_set)
@@ -990,7 +936,10 @@ static void test_reuseport_tcp_identical_clients_common(sa_family_t family,
990936
close(client_sock2);
991937
close(server_sock);
992938

993-
test_context_cleanup();
939+
/* Connection is in TIME_WAIT state, context will be released
940+
* after K_MSEC(CONFIG_NET_TCP_TIME_WAIT_DELAY), so wait for it.
941+
*/
942+
k_sleep(K_MSEC(CONFIG_NET_TCP_TIME_WAIT_DELAY));
994943
}
995944

996945
ZTEST_USER(socket_reuseport_test_suite, test_ipv4_tcp_identical_clients)

tests/net/socket/reuseaddr_reuseport/testcase.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,3 @@ tests:
1616
net.socket.reuseaddr_reuseport.userspace:
1717
extra_configs:
1818
- CONFIG_TEST_USERSPACE=y
19-
# FIXME: This test fails with an unknown error on qemu_x86
20-
platform_exclude: qemu_x86

0 commit comments

Comments
 (0)