smolvm_network/lib.rs
1//! Host-side virtio-net runtime.
2//!
3//! Context
4//! =======
5//!
6//! This module is the host-side half of the new networking path:
7//!
8//! ```text
9//! guest app
10//! -> guest kernel TCP/IP stack
11//! -> virtio-net device
12//! -> libkrun unix-stream bridge
13//! -> smolvm FrameStreamBridge
14//! -> shared frame queues
15//! -> smoltcp gateway/runtime
16//! -> host sockets / DNS forwarding / TCP relay
17//! -> external network
18//! ```
19//!
20//! Main runtime components:
21//!
22//! ```text
23//! VirtioNetworkRuntime
24//! ├─ FrameStreamBridge
25//! │ ├─ reader thread
26//! │ └─ writer thread
27//! ├─ TcpPortListeners
28//! │ └─ one non-blocking accept loop per `-p HOST:GUEST`
29//! ├─ Arc<NetworkFrameQueues>
30//! │ ├─ guest_to_host
31//! │ ├─ host_to_guest
32//! │ ├─ guest_wake
33//! │ ├─ host_wake
34//! │ └─ relay_wake
35//! └─ smolvm-net-poll thread
36//! ├─ VirtioNetworkDevice
37//! ├─ smoltcp Interface
38//! ├─ SocketSet
39//! └─ TcpRelayTable
40//! ```
41//!
42//! Component roles:
43//! - `FrameStreamBridge`: translates libkrun's Unix-stream frame protocol into
44//! queue operations
45//! - `TcpPortListeners`: accepts host TCP connections for published ports
46//! and hands them to the poll loop
47//! - `NetworkFrameQueues`: handoff boundary between threads
48//! - `VirtioNetworkDevice`: adapts those queues to smoltcp's `phy::Device`
49//! - poll thread: acts as the guest-visible gateway and protocol dispatcher
50//! - `TcpRelayTable`: maps guest TCP flows onto host-side relay threads
51//!
52//! This runtime is responsible for:
53//! - exchanging raw Ethernet frames with libkrun
54//! - presenting a gateway endpoint to the guest
55//! - handling DNS through a gateway UDP socket and host UDP forwarding
56//! - relaying guest TCP connections to host `TcpStream`s
57//! - accepting published host TCP ports and forwarding them into guest TCP
58//! connections
59
60pub mod device;
61pub mod frame_stream;
62pub mod queues;
63pub mod stack;
64pub mod tcp_listeners;
65pub mod tcp_relay;
66
67use std::fmt;
68use std::io;
69use std::net::{IpAddr, Ipv4Addr};
70use std::os::fd::RawFd;
71use std::thread::JoinHandle;
72use std::time::SystemTime;
73
74use frame_stream::{start_frame_stream_bridge, FrameStreamBridge};
75use queues::{NetworkFrameQueues, DEFAULT_FRAME_QUEUE_CAPACITY};
76use stack::{start_network_stack, VirtioPollConfig};
77use tcp_listeners::{create_tcp_channel, TcpPortListeners};
78
79/// Default upstream DNS resolver used by the gateway runtime.
80pub const DEFAULT_DNS_ADDR: IpAddr = IpAddr::V4(Ipv4Addr::new(1, 1, 1, 1));
81
82/// Host->guest published TCP port mapping serviced by the virtio gateway.
83///
84/// This stays crate-local so the launchers can translate CLI/data-layer port
85/// mappings into the gateway runtime without pulling the gateway logic back
86/// into the main `smolvm` crate.
87#[derive(Debug, Clone, Copy, PartialEq, Eq)]
88pub struct PortMapping {
89 /// Port bound on the host loopback interface.
90 pub host: u16,
91 /// Port exposed inside the guest.
92 pub guest: u16,
93}
94
95impl PortMapping {
96 /// Create a new published port mapping.
97 pub const fn new(host: u16, guest: u16) -> Self {
98 Self { host, guest }
99 }
100}
101
102/// Static guest network configuration for the virtio-net MVP.
103///
104/// This struct describes the two endpoints of the single virtual Ethernet link:
105/// - the guest NIC (`guest_*`)
106/// - the host-side gateway implemented by smolvm (`gateway_*`)
107#[derive(Debug, Clone, Copy, PartialEq, Eq)]
108pub struct GuestNetworkConfig {
109 /// Guest IPv4 address.
110 pub guest_ip: Ipv4Addr,
111 /// Gateway IPv4 address.
112 pub gateway_ip: Ipv4Addr,
113 /// Prefix length.
114 pub prefix_len: u8,
115 /// Guest MAC address.
116 pub guest_mac: [u8; 6],
117 /// Gateway MAC address.
118 pub gateway_mac: [u8; 6],
119 /// DNS server address presented to the guest.
120 pub dns_server: Ipv4Addr,
121}
122
123impl GuestNetworkConfig {
124 /// Default Phase 1 guest network configuration.
125 pub const fn default() -> Self {
126 Self {
127 guest_ip: Ipv4Addr::new(100, 96, 0, 2),
128 gateway_ip: Ipv4Addr::new(100, 96, 0, 1),
129 prefix_len: 30,
130 guest_mac: [0x02, 0x53, 0x4d, 0x00, 0x00, 0x02],
131 gateway_mac: [0x02, 0x53, 0x4d, 0x00, 0x00, 0x01],
132 dns_server: Ipv4Addr::new(100, 96, 0, 1),
133 }
134 }
135}
136
137fn format_network_log_line(timestamp: SystemTime, message: &str) -> String {
138 format!(
139 "[{}]: {}",
140 humantime::format_rfc3339_seconds(timestamp),
141 message
142 )
143}
144
145pub(crate) fn emit_network_log_line(message: fmt::Arguments<'_>) {
146 eprintln!(
147 "{}",
148 format_network_log_line(SystemTime::now(), &message.to_string())
149 );
150}
151
152macro_rules! virtio_net_log {
153 ($($arg:tt)*) => {
154 $crate::emit_network_log_line(format_args!($($arg)*))
155 };
156}
157
158pub(crate) use virtio_net_log;
159
160/// Running host-side virtio-net runtime for one guest NIC.
161///
162/// Ownership model:
163/// - one runtime instance corresponds to one guest virtio NIC
164/// - it owns the queue set shared by the worker threads
165/// - it owns the libkrun Unix-stream bridge threads
166/// - it owns the published-port listener threads
167/// - it owns the smoltcp poll thread
168///
169/// Dropping the runtime is the shutdown signal. `Drop` marks the shared queues
170/// as shutting down, wakes blocked workers, and joins the poll thread.
171pub struct VirtioNetworkRuntime {
172 queues: std::sync::Arc<NetworkFrameQueues>,
173 _frame_bridge: FrameStreamBridge,
174 published_ports: Option<TcpPortListeners>,
175 poll_handle: Option<JoinHandle<()>>,
176}
177
178/// Start the host-side virtio-net runtime for one guest NIC.
179///
180/// Inputs:
181/// - `host_fd`: the host-side Unix stream fd that libkrun will use for this
182/// guest NIC. The launcher eventually gets this from the libkrun
183/// `krun_add_net_unixstream()` setup path.
184/// - `guest_network`: the static guest/gateway addressing and MAC plan for this
185/// NIC.
186/// - `published_ports`: host->guest TCP port mappings that should be serviced
187/// directly by the virtio runtime instead of TSI.
188///
189/// High-level flow:
190///
191/// ```text
192/// start_virtio_network()
193/// -> create shared frame queues + wake pipes
194/// -> start frame reader/writer threads on the Unix stream
195/// -> start host TcpListeners for published ports
196/// -> start the smoltcp poll thread
197/// -> return a handle that owns the whole runtime
198/// ```
199///
200/// Expanded startup picture:
201///
202/// ```text
203/// host_fd from libkrun
204/// -> FrameStreamBridge(host_fd)
205/// -> reader thread
206/// -> writer thread
207/// -> TcpPortListeners
208/// -> accept host TcpStreams
209/// -> send them to the poll loop over a bounded channel
210/// -> NetworkFrameQueues
211/// -> start_network_stack(...)
212/// -> poll thread owns smoltcp Interface + sockets
213/// -> VirtioNetworkRuntime returned to launcher
214/// ```
215///
216/// Outcome:
217/// - guest->host Ethernet frames start flowing into the queues
218/// - host->guest Ethernet frames emitted by smoltcp are written back to libkrun
219/// - published host TCP connections can be forwarded toward guest listeners
220/// - the poll loop starts acting as the guest-visible gateway
221pub fn start_virtio_network(
222 host_fd: RawFd,
223 guest_network: GuestNetworkConfig,
224 published_ports: &[PortMapping],
225) -> io::Result<VirtioNetworkRuntime> {
226 virtio_net_log!(
227 "virtio-net: starting runtime host_fd={} guest_ip={} gateway_ip={} dns_server={}",
228 host_fd,
229 guest_network.guest_ip,
230 guest_network.gateway_ip,
231 guest_network.dns_server
232 );
233 let queues = NetworkFrameQueues::shared(DEFAULT_FRAME_QUEUE_CAPACITY);
234 let frame_bridge = start_frame_stream_bridge(host_fd, queues.clone())?;
235 // tcp_sender sends the accepted TCP connections to the channel
236 // tcp_receiver receives the accepted TCP connections via the channel, and let it be consumed in poll thread.
237 let (tcp_sender, tcp_receiver) = create_tcp_channel();
238 let tcp_listeners = if published_ports.is_empty() {
239 None
240 } else {
241 Some(TcpPortListeners::start(
242 published_ports,
243 tcp_sender,
244 queues.relay_wake.clone(),
245 )?)
246 };
247 let poll_handle = start_network_stack(
248 queues.clone(),
249 VirtioPollConfig {
250 gateway_mac: guest_network.gateway_mac,
251 guest_mac: guest_network.guest_mac,
252 gateway_ipv4: guest_network.gateway_ip,
253 guest_ipv4: guest_network.guest_ip,
254 mtu: 1500,
255 },
256 tcp_listeners.as_ref().map(|_| tcp_receiver),
257 )?;
258
259 Ok(VirtioNetworkRuntime {
260 queues,
261 _frame_bridge: frame_bridge,
262 published_ports: tcp_listeners,
263 poll_handle: Some(poll_handle),
264 })
265}
266
267impl Drop for VirtioNetworkRuntime {
268 /// Shut down the worker threads in a bounded, cooperative way.
269 ///
270 /// The queue shutdown flag wakes the frame bridge and smoltcp poll loop so
271 /// they can exit on their own. We only explicitly join the poll thread
272 /// here because the frame bridge joins its own threads in its own `Drop`.
273 fn drop(&mut self) {
274 self.queues.begin_shutdown();
275 self.published_ports = None;
276 if let Some(handle) = self.poll_handle.take() {
277 let _ = handle.join();
278 }
279 }
280}
281
282#[cfg(test)]
283mod tests {
284 use super::format_network_log_line;
285 use std::time::UNIX_EPOCH;
286
287 #[test]
288 fn formats_timestamped_network_log_prefix() {
289 let line = format_network_log_line(UNIX_EPOCH, "virtio-net: smoke test");
290 assert_eq!(line, "[1970-01-01T00:00:00Z]: virtio-net: smoke test");
291 }
292}