Skip to content

testing/nettest: Add TCP backlog and /proc/net/tcp cases. - #4106

Open
casaroli wants to merge 2 commits into
apache:masterfrom
casaroli:tcp-lock-fixes-test-up
Open

casaroli wants to merge 2 commits into
apache:masterfrom
casaroli:tcp-lock-fixes-test-up

Conversation

@casaroli

@casaroli casaroli commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

New cases of cmocka_net_tcp for the fixes of apache/nuttx#22145, on the IPv4 loopback address:

  • test_tcp_backlog_relisten_rst: client A connects to a listener of backlog 4 and nobody accepts it. listen() again gives a backlog of 1, then A resets its connection (SO_LINGER {1, 0}). The connect() of client C must end within 2 s. 10 rounds. Needs CONFIG_NET_SOLINGER.
  • test_tcp_backlog_relisten_race: 50 rounds of a listener of backlog 8, 6 clients that connect at once, listen() again with a backlog of 1 while they connect, and the close of the listener. Two more clients keep the loopback input busy.
  • test_tcp_backlog_close_race: the same with 4 clients and the close of the listener while their handshakes run.
  • test_tcp_procfs_many: 300 connections that the client closes first (they stay in TIME_WAIT), then a read of /proc/net/tcp to its end in 1 KiB parts. The read must end within 8192 lines and list each connection of the case's listener once (300 to 600 lines with its port). Connections that the earlier cases leave in TIME_WAIT are not counted.
  • test_tcp_procfs_close: 8 open pairs to a first listener, then 300 connections in TIME_WAIT to a second one. The file is read until the lines of the pairs are there, then the pairs reset their connections and the read goes on to its end. It must list the client side of each connection to the second listener once. Needs CONFIG_NET_SOLINGER.

The backlog cases need CONFIG_NET_TCPBACKLOG. The procfs cases need /proc/net/tcp (CONFIG_NET_STATISTICS), CONFIG_NET_TCP_ALLOC_CONNS > 0, CONFIG_NET_ALLOC_DEVIF_CALLBACKS > 0, CONFIG_NET_TCP_MAX_CONNS 0 or 1024 or more, and CONFIG_NET_TCP_WAIT_TIMEOUT 60 s or more. Every socket has a 10 s send and receive timeout. A kernel with the lock-order bug stops the network in the race cases.

Impact

Only testing/nettest/tcp: two new source files (test_tcp_backlog.c, test_tcp_procfs.c, built with CONFIG_NET_IPv4) and their lines in Makefile and CMakeLists.txt. No new Kconfig option.

Testing

qemu-armv8a:knsh (kernel build) and qemu-armv8a:nsh (flat build) under HVF, 4 CPUs, TCP over the loopback device, NET_TCPBACKLOG, NET_SOLINGER, dynamic connections and devif callbacks. NuttX master 623b1a9ad18 and apache/nuttx#22145 both with apache/nuttx#22168 applied (without it, master stops in cmocka_net_tcp before these cases):

Case NuttX master with apache/nuttx#22145
test_tcp_backlog_relisten_rst FAIL: connect() of C times out (110) PASS
test_tcp_backlog_relisten_race the network stops (the close of the listener waits for the device lock with the list lock held; master c51b1af1ee2, not run again) PASS
test_tcp_backlog_close_race (not reached) PASS
test_tcp_procfs_many FAIL: "8193 lines, 8004 of the listener, 7750 more than once" (the read starts again from the head) PASS
test_tcp_procfs_close FAIL: "8193 lines, 0 of the pairs" PASS

All 36 cases of cmocka_net_tcp in one run with apache/nuttx#22145: 34 pass on knsh, 35 on flat nsh. test_tcp_send_limit_wakeup and test_tcp_close_timewait (knsh) and test_tcp_reset_send (flat) fail on master in the same way.

tools/checkpatch.sh -c -u -m -g passes.

@cederom

cederom commented Nov 22, 2026

Copy link
Copy Markdown
Contributor
  1. test_tcp_procfs_many will fail now and then. It requires all 300 of its connections to appear (own >= TCP_PROCFS_CONNS). The race cases that run just before it leave many connections in TIME_WAIT, and those expire while the roughly 50 reads of 1 KiB are under way. Each expiry ahead of the read position hides one of the test's connections (nuttx Wapi simplify #1). The fix is either to allow a few missing, or to wait until earlier connections have drained. The no-duplicates and the read-ends checks are solid as they are.
  2. The procfs case's config guard is incomplete. TEST_TCP_PROCFS should also require CONFIG_NET_TCP_MAX_CONNS == 0 or a value well above 300 plus what earlier cases leave, and a CONFIG_NET_TCP_WAIT_TIMEOUT long enough for the 300 connections to stay in TIME_WAIT.
  3. The backlog cases need a CONFIG_NET_TCPBACKLOG guard. Without a backlog, client A's connect in relisten_rst gets a reset and the case fails for the wrong reason.
  4. A stall hangs the whole run instead of failing. The PR says the race case stalls the network on master. pthread_join() on the race clients has no timeout, so that hangs the entire cmocka run and CI. A timed join or watchdog would turn it into a failed case.
  5. Possible use-after-free in the test after a failed assert. If an assert fails inside tcp_backlog_race() after the clients start, the teardown frees st while those threads still use st->race, because their thread IDs (tids) are local. Keeping tids in the state and joining them in teardown fixes it.
  6. Nits:
    • tcp_backlog_now() computes tv_sec * 1000 in a long, which overflows on 32-bit targets after about 24.8 days of uptime. The < TCP_BACKLOG_CONNECT check after it adds nothing, since poll() already has that timeout.
    • The busy clients create a lot of TIME_WAIT connections; with a fixed pool of connections, later cases may run out.

The PR description also says the test server needs nuttx-apps #4103; that should be listed as a dependency.

@casaroli

Copy link
Copy Markdown
Contributor Author

@cederom 1, 2, 3, 5: Done.

@cederom

cederom commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Please resolve the tesitng/nettest conflicts :-)

Fixed:

Still open:

  1. A stall still hangs the run (was Application.mk remove _main suffix from REGLIST #4). The race clients are joined with a plain pthread_join(), now in teardown as well. If the network stalls the way the description reports on master, the whole cmocka run hangs instead of failing that case.

  2. The branch includes three commits that are not in apps master:

    • 5ff9666 (peer port checks)
    • 95c5d82 (I/O buffer send timeout)
    • bce81d5 (peer that closes early)

    This PR's Makefile and CMake changes depend on them (test_tcp_iov.c). If they are separate PRs, the description should list them as dependencies. If they belong to this PR, its scope has grown. I couldn't confirm which from git alone.

  3. Nit: in both procfs cases, an assert inside tcp_procfs_listen() or tcp_procfs_timewait() leaks seen, because the explicit free() only covers the open() failure and the final check. The setsockopt(SO_LINGER) return value in test_tcp_procfs_close is not checked.

@xiaoxiang781216

Copy link
Copy Markdown
Contributor

@casaroli please fix the conflict

@casaroli

Copy link
Copy Markdown
Contributor Author

4: On master the case does not stop in pthread_join(): its main thread stops before, in close() of the listener, in the kernel (tcp_free() waits for the device lock with the list lock held). No time limit in the test can end that.

6: Done for the time check (tcp_backlog_now() is removed). A fixed pool does not run out: when it is full, tcp_alloc_conn() frees the oldest connection in TIME_WAIT (tcp_reclaim()).

#4103 is merged.

Three cases in cmocka_net_tcp (IPv4 on the loopback address) for the
backlog of a listener.  They need CONFIG_NET_TCPBACKLOG.

- test_tcp_backlog_relisten_rst: client A connects to a listener of
  backlog 4 and nobody accepts it.  listen() again gives a backlog of 1
  (A moves to the new backlog), then A resets its connection (SO_LINGER
  0).  The connect() of client C must end within 2 s: a backlog that
  still holds A drops the SYN of C.  accept() then gives C (the reset
  connection of A may come first).  10 rounds.  It needs
  CONFIG_NET_SOLINGER.
- test_tcp_backlog_relisten_race: 50 rounds of a listener of backlog 8,
  6 clients that connect at once, listen() again with a backlog of 1
  while they connect, and the close of the listener.  Two more clients
  keep the loopback input busy with connections to another listener.
  Each round must end.
- test_tcp_backlog_close_race: the same with 4 clients to a listener of
  backlog 4 that closes while their handshakes run.

The clients of the race cases use a non-blocking connect() and wait at
most 500 ms, so a dropped SYN does not make a round long.  Their thread
IDs are in the state of the case, so that the teardown waits for them
when an assert ends the case.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
Two cases in cmocka_net_tcp (IPv4 on the loopback address) for a read of
/proc/net/tcp with more than 255 connections.

- test_tcp_procfs_many: 300 connections to a listener on 127.0.0.1, each
  closed by the client first, so that each stays in TIME_WAIT on the
  client side.  Then /proc/net/tcp is read to its end in reads of
  1 KiB.  The read must end within 8192 lines (more lines mean that the
  read starts again from the head of the table), and must list each
  connection of the listener once: 300 to 600 lines with its port.
  Connections that earlier cases leave in TIME_WAIT are not counted.
- test_tcp_procfs_close: 8 open pairs to a first listener, then 300
  connections in TIME_WAIT to a second one.  The file is read until the
  lines of the pairs are there, then the pairs reset their connections
  (SO_LINGER 0) and the read goes on to the end.  It must list the client
  side of each connection to the second listener once: connections that
  go away during a read must not hide others.  It needs
  CONFIG_NET_SOLINGER.

They need /proc/net/tcp (CONFIG_NET_STATISTICS) and dynamic connections
and callbacks (CONFIG_NET_TCP_ALLOC_CONNS and
CONFIG_NET_ALLOC_DEVIF_CALLBACKS greater than 0), with no limit or a
limit of 1024 or more (CONFIG_NET_TCP_MAX_CONNS), and a
CONFIG_NET_TCP_WAIT_TIMEOUT of 60 s or more: the connections in
TIME_WAIT must stay while the file is read.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
@casaroli
casaroli force-pushed the tcp-lock-fixes-test-up branch from 16e7aed to db61af7 Compare November 24, 2026 17:15
@casaroli
casaroli marked this pull request as ready for review November 24, 2026 17:16
@casaroli

Copy link
Copy Markdown
Contributor Author

3: Done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants