Skip to content

refactor(net): eliminate NET_MANAGER global write lock from TCP/UDP hot paths#682

Open
ytakano wants to merge 9 commits intotier4:mainfrom
ytakano:improve_nw_stack_1_pr
Open

refactor(net): eliminate NET_MANAGER global write lock from TCP/UDP hot paths#682
ytakano wants to merge 9 commits intotier4:mainfrom
ytakano:improve_nw_stack_1_pr

Conversation

@ytakano
Copy link
Copy Markdown
Collaborator

@ytakano ytakano commented Apr 21, 2026

Description

This PR implements the Pre-Phase bug fix and PortAllocator separation to avoid inefficient locks.

Background

Previously, every TCP/UDP connection establishment and teardown acquired NET_MANAGER.write() — a single global RwLock that serialised all concurrent connect, bind, accept, and drop calls. Port search was also broken: the
ephemeral-port loop called entry(i) (loop index) instead of entry(port), so availability checks were incorrect and the loop always iterated up to 16384 times while holding the write lock.

Changes

bug fixes (net.rs)

  • get_ephemeral_port_tcp_ipv4/v6: entry(i) → entry(port) — port availability is now checked correctly; search terminates at the first free slot.
  • get_ephemeral_port_udp_ipv6: cursor updated udp_port_ipv6_ephemeral instead of udp_port_ipv4_ephemeral (copy-paste bug).

PortAllocator separation

New file: awkernel_lib/src/net/port_alloc.rs

Introduces PortAllocator with four independent Mutex guards (TCP IPv4, TCP IPv6, UDP IPv4, UDP IPv6). A global static PORT_ALLOC replaces the port fields that were embedded in NetManager.

Related links

How was this PR tested?

  • make x86_64 RELEASE=1 — x86_64 no_std release build
  • make aarch64 BSP=aarch64_virt RELEASE=1 — AArch64 no_std release build
  • make std — std build (compile check)
  • make test — all 367 unit tests pass, 0 failures

Notes for reviewers

  • NET_MANAGER.write() is no longer called in any hot path. The remaining NET_MANAGER.write() call is add_interface, which runs once at boot and is not performance-sensitive.
  • The four Mutex guards in PortAllocator are independent; TCP IPv4 allocation cannot block UDP or TCP IPv6 allocation.
  • The advancing-cursor pattern (cursor += 1 before the vacancy check) means the first candidate is always one past the last allocation, not a re-attempt of it. Under low port pressure this resolves in O(1).
  • TcpPort acts as a reference-counted RAII guard: Drop decrements the use count and frees the port when it reaches zero. The increment_ref_tcp_ipv{4,6} path is used by TcpListener::accept to share a listen port across multiple accepted connections.

ytakano and others added 6 commits April 21, 2026 14:47
Signed-off-by: Yuuki Takano <ytakanoster@gmail.com>
Signed-off-by: Yuuki Takano <ytakanoster@gmail.com>
…Phase 1.1)

Move TCP/UDP port tracking out of NetManager into a standalone port_alloc module
with per-protocol Mutex guards, converting NET_MANAGER write locks to read locks
in connect/bind/accept paths.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…r pattern (Phase 1.1 follow-up)

- Replace AtomicU16 ephemeral cursors with u16 fields inside TcpPortsInner/UdpPortsInner structs, eliminating the load-outside/store-inside asymmetry that could produce stale cursor reads on aarch64's weak memory model
- Change search loop from cursor.wrapping_add(i) (i=0) to advancing cursor (cursor+=1 before each check), avoiding the wasted first iteration that always retried the last-allocated port

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…std"))] (Phase 1.1 follow-up)

- port_alloc.rs: add #![cfg(not(feature = "std"))] inner attribute to
  gate the entire module; remove all per-item cfg annotations and both
  #[allow(dead_code)] suppressions
- tcp.rs: gate TcpPort struct and its impl/Drop blocks with
  #[cfg(not(feature = "std"))]; remove redundant inner cfg block in Drop

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Signed-off-by: Yuuki Takano <ytakanoster@gmail.com>
@ytakano ytakano changed the title reractor(net): eliminate NET_MANAGER global write lock from TCP/UDP hot paths refactor(net): eliminate NET_MANAGER global write lock from TCP/UDP hot paths Apr 21, 2026
ytakano and others added 2 commits April 21, 2026 19:26
Signed-off-by: Yuuki Takano <ytakanoster@gmail.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@ytakano ytakano marked this pull request as ready for review April 21, 2026 10:38
@ytakano ytakano requested a review from Copilot April 21, 2026 10:38
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors no-std networking port allocation to remove NET_MANAGER.write() from TCP/UDP hot paths by introducing a dedicated PortAllocator and updating socket code to use finer-grained locks.

Changes:

  • Added PortAllocator (PORT_ALLOC) with independent mutexes for TCP/UDP and IPv4/IPv6 port tracking.
  • Updated TCP stream/listener and UDP socket no-std implementations to allocate/claim/free ports via PORT_ALLOC and to use NET_MANAGER.read() where feasible.
  • Removed embedded port-tracking state and related helpers from NetManager (net.rs).

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
awkernel_lib/src/net/udp_socket/udp_socket_no_std.rs Switch UDP bind/free paths to PORT_ALLOC and reduce NET_MANAGER locking.
awkernel_lib/src/net/tcp_stream/tcp_stream_no_std.rs Use PORT_ALLOC for ephemeral TCP port allocation in connect path.
awkernel_lib/src/net/tcp_listener/tcp_listener_no_std.rs Use PORT_ALLOC for TCP listen/accept port claiming and refcounting.
awkernel_lib/src/net/tcp.rs Move TcpPort to no-std-only and route Drop to PORT_ALLOC refcount decrement.
awkernel_lib/src/net/port_alloc.rs New no-std PortAllocator implementing TCP refcounting and UDP claim/free sets.
awkernel_lib/src/net.rs Remove port bookkeeping from NetManager and add internal port_alloc module.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread awkernel_lib/src/net/udp_socket/udp_socket_no_std.rs Outdated
Signed-off-by: Yuuki Takano <ytakanoster@gmail.com>
@ytakano ytakano requested a review from Koichi98 April 22, 2026 05:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants