Limit memory consumption

This commit is contained in:
eric committed 2022-11-07 19:03:58 +08:00
1 parent 56b3464a07
commit ad03738fb4
6 files changed
+32 -10

No files matched your search

+2 -2
View File
@@ -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" }
+2 -1
View File
@@ -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<UdpPacket>, TokioReceiver<UdpPacket>) = tokio_channel(100);
let (l_tx, mut l_rx): (TokioSender<UdpPacket>, TokioReceiver<UdpPacket>) =
tokio_channel(*crate::option::UDP_UPLINK_CHANNEL_SIZE);
tokio::spawn(async move {
while let Some(pkt) = l_rx.recv().await {
+2 -2
View File
@@ -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 {
+3 -2
View File
@@ -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()));
+16
View File
@@ -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)
+7 -3
View File
@@ -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<UdpPacket>, TokioReceiver<UdpPacket>) = tokio_channel(32);
let (l_tx, mut l_rx): (TokioSender<UdpPacket>, TokioReceiver<UdpPacket>) =
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<Runner> = 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();
}
}
}));