refactor(net): eliminate NET_MANAGER global write lock from TCP/UDP hot paths#682
Open
ytakano wants to merge 9 commits intotier4:mainfrom
Open
refactor(net): eliminate NET_MANAGER global write lock from TCP/UDP hot paths#682ytakano wants to merge 9 commits intotier4:mainfrom
ytakano wants to merge 9 commits intotier4:mainfrom
Conversation
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>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
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_ALLOCand to useNET_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.
Signed-off-by: Yuuki Takano <ytakanoster@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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)
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?
Notes for reviewers