From ad03738fb41ca7bfbc36f7f892ee765207113824 Mon Sep 17 00:00:00 2001 From: eric Date: Tue, 25 Oct 2022 09:44:40 +0800 Subject: [PATCH] Limit memory consumption --- leaf/Cargo.toml | 4 ++-- leaf/src/app/inbound/network_listener.rs | 3 ++- leaf/src/app/logger.rs | 4 ++-- leaf/src/app/nat_manager.rs | 5 +++-- leaf/src/option/mod.rs | 16 ++++++++++++++++ leaf/src/proxy/tun/inbound.rs | 10 +++++++--- 6 files changed, 32 insertions(+), 10 deletions(-) diff --git a/leaf/Cargo.toml b/leaf/Cargo.toml index 8830f57..893f579 100644 --- a/leaf/Cargo.toml +++ b/leaf/Cargo.toml @@ -225,8 +225,8 @@ notify = { version = "5.0.0-pre.13", optional = true } # TUN [target.'cfg(any(target_os = "ios", target_os = "android", target_os = "macos", target_os = "linux"))'.dependencies] -tun = { git = "https://github.com/eycorsican/rust-tun.git", branch = "upgrade", features = ["async"], optional = true } -netstack-lwip = { git = "https://github.com/eycorsican/netstack-lwip.git", tag = "v0.3.3", optional = true } +tun = { git = "https://github.com/eycorsican/rust-tun.git", branch = "fork", features = ["async"], optional = true } +netstack-lwip = { git = "https://github.com/eycorsican/netstack-lwip.git", optional = true } [target.'cfg(any(target_os = "macos", target_os = "linux"))'.dependencies] pnet_datalink = { version = "0.28", package = "pnet_datalink" } diff --git a/leaf/src/app/inbound/network_listener.rs b/leaf/src/app/inbound/network_listener.rs index 8669a1e..9cddf18 100644 --- a/leaf/src/app/inbound/network_listener.rs +++ b/leaf/src/app/inbound/network_listener.rs @@ -34,7 +34,8 @@ async fn handle_inbound_datagram( // to the socket by the NAT manager. When the NAT manager reads some packets // from the right-hand side socket, they would be sent back here through a // channel, then we can send them to left-hand side socket. - let (l_tx, mut l_rx): (TokioSender, TokioReceiver) = tokio_channel(100); + let (l_tx, mut l_rx): (TokioSender, TokioReceiver) = + tokio_channel(*crate::option::UDP_UPLINK_CHANNEL_SIZE); tokio::spawn(async move { while let Some(pkt) = l_rx.recv().await { diff --git a/leaf/src/app/logger.rs b/leaf/src/app/logger.rs index 080c44c..b8d8bba 100644 --- a/leaf/src/app/logger.rs +++ b/leaf/src/app/logger.rs @@ -45,8 +45,8 @@ pub fn setup_logger(config: &config::Log) -> Result<()> { } }) .level(log::LevelFilter::Warn) - .level_for("rust-tun", loglevel) - .level_for("netstack-lwip", loglevel) + .level_for("rust_tun", loglevel) + .level_for("netstack_lwip", loglevel) .level_for("leaf", loglevel); match config.output { diff --git a/leaf/src/app/nat_manager.rs b/leaf/src/app/nat_manager.rs index 05caf8e..429485e 100644 --- a/leaf/src/app/nat_manager.rs +++ b/leaf/src/app/nat_manager.rs @@ -111,7 +111,7 @@ impl NatManager { ) { if let Some(sess) = guard.get_mut(key) { if let Err(err) = sess.0.try_send(pkt) { - debug!("send uplink packet failed {}", err); + trace!("send uplink packet failed {}", err); } sess.2 = Instant::now(); // activity update } else { @@ -169,7 +169,8 @@ impl NatManager { tokio::spawn(task); } - let (target_ch_tx, mut target_ch_rx) = mpsc::channel(64); + let (target_ch_tx, mut target_ch_rx) = + mpsc::channel(*crate::option::UDP_UPLINK_CHANNEL_SIZE); let (downlink_abort_tx, downlink_abort_rx) = oneshot::channel(); guard.insert(raddr, (target_ch_tx, downlink_abort_tx, Instant::now())); diff --git a/leaf/src/option/mod.rs b/leaf/src/option/mod.rs index f766394..b6839a4 100644 --- a/leaf/src/option/mod.rs +++ b/leaf/src/option/mod.rs @@ -112,6 +112,22 @@ lazy_static! { get_env_var_or("LINK_BUFFER_SIZE", 2) }; + pub static ref NETSTACK_OUTPUT_CHANNEL_SIZE: usize = { + get_env_var_or("NETSTACK_OUTPUT_CHANNEL_SIZE", 512) + }; + + pub static ref NETSTACK_UDP_UPLINK_CHANNEL_SIZE: usize = { + get_env_var_or("NETSTACK_UDP_UPLINK_CHANNEL_SIZE", 256) + }; + + pub static ref UDP_UPLINK_CHANNEL_SIZE: usize = { + get_env_var_or("UDP_UPLINK_CHANNEL_SIZE", 256) + }; + + pub static ref UDP_DOWNLINK_CHANNEL_SIZE: usize = { + get_env_var_or("UDP_DOWNLINK_CHANNEL_SIZE", 256) + }; + /// Buffer size for UDP datagrams receiving/sending, in KB. pub static ref DATAGRAM_BUFFER_SIZE: usize = { get_env_var_or("DATAGRAM_BUFFER_SIZE", 2) diff --git a/leaf/src/proxy/tun/inbound.rs b/leaf/src/proxy/tun/inbound.rs index 7cd4053..7d283a8 100644 --- a/leaf/src/proxy/tun/inbound.rs +++ b/leaf/src/proxy/tun/inbound.rs @@ -71,7 +71,8 @@ async fn handle_inbound_datagram( let ls = Arc::new(ls); // The channel for sending back datagrams from NAT manager to netstack. - let (l_tx, mut l_rx): (TokioSender, TokioReceiver) = tokio_channel(32); + let (l_tx, mut l_rx): (TokioSender, TokioReceiver) = + tokio_channel(*crate::option::UDP_DOWNLINK_CHANNEL_SIZE); // Receive datagrams from NAT manager and send back to netstack. let fakedns_cloned = fakedns.clone(); @@ -231,7 +232,10 @@ pub fn new( let inbound_tag = inbound.tag.clone(); let framed = tun.into_framed(); let (mut tun_sink, mut tun_stream) = framed.split(); - let (stack, mut tcp_listener, udp_socket) = netstack::NetStack::new(); + let (stack, mut tcp_listener, udp_socket) = netstack::NetStack::with_buffer_size( + *crate::option::NETSTACK_OUTPUT_CHANNEL_SIZE, + *crate::option::NETSTACK_UDP_UPLINK_CHANNEL_SIZE, + ); let (mut stack_sink, mut stack_stream) = stack.split(); let mut futs: Vec = Vec::new(); @@ -249,7 +253,7 @@ pub fn new( futs.push(Box::pin(async move { while let Some(pkt) = tun_stream.next().await { if let Ok(pkt) = pkt { - stack_sink.send(pkt.get_bytes().to_vec()).await.unwrap(); + stack_sink.send(pkt.into_bytes().into()).await.unwrap(); } } }));