From a343a907a9d752aaba2b051dbf76f6e44c123871 Mon Sep 17 00:00:00 2001 From: "sijie.sun" Date: Sat, 13 Jun 2026 01:27:27 +0800 Subject: [PATCH] refactor: add shared nic ctx constructor Add a shared NicCtx constructor that obtains a SharedVirtualNicMember from the registry and wraps it in NicBackend::Shared. Keep NicCtx::new on the dedicated backend path so existing runtime behavior does not change before the shared tunnel implementation is ready. --- easytier/src/instance/virtual_nic.rs | 105 +++++++++++++++++++++++---- 1 file changed, 89 insertions(+), 16 deletions(-) diff --git a/easytier/src/instance/virtual_nic.rs b/easytier/src/instance/virtual_nic.rs index 89c5fc7a..e01b8cff 100644 --- a/easytier/src/instance/virtual_nic.rs +++ b/easytier/src/instance/virtual_nic.rs @@ -44,7 +44,9 @@ use zerocopy::{NativeEndian, NetworkEndian}; #[cfg(target_os = "windows")] use crate::common::ifcfg::RegistryManager; -use super::shared_virtual_nic::SharedVirtualNicMember; +use super::shared_virtual_nic::{ + SharedVirtualNicMember, SharedVirtualNicMemberId, SharedVirtualNicRegistry, +}; pin_project! { pub struct TunStream { @@ -964,14 +966,54 @@ pub struct NicCtx { } impl NicCtx { - fn virtual_nic_config(global_ctx: &ArcGlobalCtx) -> VirtualNicConfig { - let flags = global_ctx.get_flags(); - let mut mtu = flags.mtu; - if flags.enable_encryption { + fn virtual_nic_config_from_parts( + dev_name: String, + mut mtu: u32, + enable_encryption: bool, + net_ns: NetNS, + ) -> VirtualNicConfig { + if enable_encryption { mtu -= 20; } - VirtualNicConfig::new(flags.dev_name, mtu, global_ctx.net_ns.clone()) + VirtualNicConfig::new(dev_name, mtu, net_ns) + } + + fn virtual_nic_config(global_ctx: &ArcGlobalCtx) -> VirtualNicConfig { + let flags = global_ctx.get_flags(); + Self::virtual_nic_config_from_parts( + flags.dev_name, + flags.mtu, + flags.enable_encryption, + global_ctx.net_ns.clone(), + ) + } + + fn dedicated_backend(global_ctx: &ArcGlobalCtx) -> NicBackend { + let nic_config = Self::virtual_nic_config(global_ctx); + NicBackend::dedicated(Arc::new(Mutex::new(VirtualNic::new(nic_config)))) + } + + fn new_with_backend( + global_ctx: ArcGlobalCtx, + peer_manager: &Arc, + peer_packet_receiver: Arc>, + close_notifier: Arc, + backend: NicBackend, + ) -> Self { + NicCtx { + global_ctx: global_ctx.clone(), + peer_mgr: Arc::downgrade(peer_manager), + peer_packet_receiver, + + close_notifier, + + backend, + tasks: JoinSet::new(), + + #[cfg(target_os = "windows")] + windows_udp_broadcast_relay: None, + } } pub fn new( @@ -980,21 +1022,52 @@ impl NicCtx { peer_packet_receiver: Arc>, close_notifier: Arc, ) -> Self { - let nic_config = Self::virtual_nic_config(&global_ctx); + let backend = Self::dedicated_backend(&global_ctx); - NicCtx { - global_ctx: global_ctx.clone(), - peer_mgr: Arc::downgrade(peer_manager), + Self::new_with_backend( + global_ctx, + peer_manager, peer_packet_receiver, - close_notifier, + backend, + ) + } - backend: NicBackend::dedicated(Arc::new(Mutex::new(VirtualNic::new(nic_config)))), - tasks: JoinSet::new(), - - #[cfg(target_os = "windows")] - windows_udp_broadcast_relay: None, + pub async fn new_shared( + global_ctx: ArcGlobalCtx, + peer_manager: &Arc, + peer_packet_receiver: Arc>, + close_notifier: Arc, + registry: Arc>, + member_id: SharedVirtualNicMemberId, + ) -> Result { + let flags = global_ctx.get_flags(); + let dev_name = flags.dev_name.clone(); + if dev_name.is_empty() { + return Err(anyhow::anyhow!("shared virtual nic requires dev_name").into()); } + let nic_config = Self::virtual_nic_config_from_parts( + dev_name.clone(), + flags.mtu, + flags.enable_encryption, + global_ctx.net_ns.clone(), + ); + + let member = registry.lock().await.create_member( + dev_name, + nic_config, + member_id, + close_notifier.clone(), + ); + let backend = NicBackend::shared(member); + + Ok(Self::new_with_backend( + global_ctx, + peer_manager, + peer_packet_receiver, + close_notifier, + backend, + )) } pub async fn ifname(&self) -> Option {