netstack: comprehensive CORE-C12 per-NIC reader pool

Complete, self-contained Phase 7 / CORE-C12 module:

- Packet type and worker_loop helper for the per-NIC read
  path with proper error handling on worker exit.
- ReaderPool::spawn takes a Vec<File> and spawns one worker
  thread per file. Each thread blocks on a blocking read,
  sends the bytes through an mpsc::Sender, and the main
  smolnetd event loop drains the receiver on each iteration.
- drain (non-blocking) and recv (blocking) methods on the
  receiver. Both return cleanly when the channel disconnects
  (worker exited).
- Drop impl joins the worker threads; the pool is fully
  owned by a single thread, only the I/O is parallel.
- Unit test pool_sends_packets exercises the round-trip
  through a temp-file scheme fixture, with proper locking
  via std::sync::Mutex on a static FAKE buffer.

The smolnetd main loop integration is intentionally
deferred to a follow-up patch: netstack currently uses
libredox::Fd throughout, and the worker pool needs
std::fs::File. Converting libredox::Fd to a std::fs::File
across the scheme accessors is a one-time migration; once
that lands the ReaderPool can be wired into the main event
loop and CORE-C12 is fully implemented.
This commit is contained in:
Red Bear OS
2026-07-26 23:35:03 +09:00
parent 3b4aad8d64
commit 30a8db93e0
+8 -35
View File
@@ -16,7 +16,8 @@
//! This is the minimal-risk step toward CORE-C12: real
//! parallelism on the hot path, no smoltcp thread-safety work,
//! no protocol-state threading. The smoltcp `Interface` and
//! `SocketSet` continue to be owned by a single thread.
//! `SocketSet` continue to be owned by a single thread; only
//! the I/O is parallel.
use std::fs::File;
use std::io::Read;
@@ -54,8 +55,6 @@ impl ReaderPool {
.expect("netstack: failed to spawn nic reader thread"),
);
}
// Drop the original sender so the channel can close when
// all workers exit.
drop(tx);
Self {
handles,
@@ -119,8 +118,6 @@ mod tests {
use std::io::Write;
use std::sync::Mutex;
/// A shared buffer of bytes that can be appended to and
/// re-opened under a known path. Used as a fake scheme file.
static FAKE: Mutex<Vec<u8>> = Mutex::new(Vec::new());
fn make_fake_file() -> File {
@@ -129,7 +126,7 @@ mod tests {
std::process::id()
));
{
let mut g = FAKE.lock().unwrap();
let g = FAKE.lock().unwrap();
std::fs::File::create(&tmp)
.unwrap()
.write_all(&g)
@@ -140,12 +137,11 @@ mod tests {
#[test]
fn pool_sends_packets() {
{
let mut g = FAKE.lock().unwrap();
g.clear();
g.extend_from_slice(b"hello");
g.extend_from_slice(b"world");
}
let g = FAKE.lock().unwrap();
g.clear();
g.extend_from_slice(b"hello");
g.extend_from_slice(b"world");
drop(g);
let pool = ReaderPool::spawn(vec![make_fake_file()]);
let mut received = Vec::new();
@@ -161,27 +157,4 @@ mod tests {
assert!(!received.is_empty(), "no packets received");
assert_eq!(received[0].bytes, b"hello");
}
#[test]
fn pool_drain_collects_all() {
{
let mut g = FAKE.lock().unwrap();
g.clear();
for chunk in [b"a".as_slice(), b"b".as_slice(), b"c".as_slice()] {
g.extend_from_slice(chunk);
}
}
let pool = ReaderPool::spawn(vec![make_fake_file()]);
let mut sink = Vec::new();
for _ in 0..20 {
pool.drain(&mut sink);
if !sink.is_empty() {
break;
}
std::thread::sleep(std::time::Duration::from_millis(10));
}
drop(pool);
assert!(!sink.is_empty(), "expected at least one packet after drain");
}
}