From e5d2cfeb14bdc8a58d3c6556d425165c00ae28bc Mon Sep 17 00:00:00 2001 From: zephyr Date: Thu, 25 Nov 2021 16:03:31 +0900 Subject: [PATCH] adjust buffer size --- src/relay/tcp.rs | 10 +++++----- src/relay/udp.rs | 7 ++++--- src/utils/consts.rs | 9 +++++++++ src/utils/mod.rs | 4 +++- 4 files changed, 21 insertions(+), 9 deletions(-) create mode 100644 src/utils/consts.rs diff --git a/src/relay/tcp.rs b/src/relay/tcp.rs index 4e9dbdb..e62530a 100644 --- a/src/relay/tcp.rs +++ b/src/relay/tcp.rs @@ -14,9 +14,9 @@ cfg_if! { cfg_if! { if #[cfg(all(target_os = "linux", feature = "zero-copy"))] { - const BUFFER_SIZE: usize = 0x10000; + const BUF_SIZE: usize = crate::utils::DEFAULT_PIPE_CAP; } else { - const BUFFER_SIZE: usize = 0x4000; + const BUF_SIZE: usize = crate::utils::DEFAULT_BUF_SIZE; } } @@ -143,7 +143,7 @@ mod normal_copy { where { use tokio::io::{AsyncReadExt, AsyncWriteExt}; - let mut buf = vec![0u8; BUFFER_SIZE]; + let mut buf = vec![0u8; BUF_SIZE]; let mut n: usize; loop { n = timeoutfut(timeout, r.read(&mut buf)).await??; @@ -253,8 +253,8 @@ mod zero_copy { // read until the socket buffer is empty // or the pipe is filled timeoutfut(timeout, rx.readable()).await??; - while n < BUFFER_SIZE { - match splice_n(rfd, wpipe, BUFFER_SIZE - n) { + while n < BUF_SIZE { + match splice_n(rfd, wpipe, BUF_SIZE - n) { x if x > 0 => n += x as usize, x if x == 0 => { done = true; diff --git a/src/relay/udp.rs b/src/relay/udp.rs index abb925c..b8b4bee 100644 --- a/src/relay/udp.rs +++ b/src/relay/udp.rs @@ -9,12 +9,13 @@ use log::{debug, info, error}; use tokio::net::UdpSocket; use tokio::time::timeout as timeoutfut; +use crate::utils::DEFAULT_BUF_SIZE; use crate::utils::{RemoteAddr, ConnectOpts}; use crate::utils::{new_sockaddr_v4, new_sockaddr_v6}; // client <--> allocated socket type SockMap = Arc>>>; -const BUFFERSIZE: usize = 0x1000; +const BUF_SIZE: usize = DEFAULT_BUF_SIZE; pub async fn proxy( local: SocketAddr, @@ -29,7 +30,7 @@ pub async fn proxy( let sock_map: SockMap = Arc::new(RwLock::new(HashMap::new())); let local_sock = Arc::new(UdpSocket::bind(&local).await?); let timeout = Duration::from_secs(timeout as u64); - let mut buf = vec![0u8; BUFFERSIZE]; + let mut buf = vec![0u8; BUF_SIZE]; loop { let (n, client_addr) = match local_sock.recv_from(&mut buf).await { @@ -82,7 +83,7 @@ async fn send_back( alloc_sock: Arc, timeout: Duration, ) { - let mut buf = vec![0u8; BUFFERSIZE]; + let mut buf = vec![0u8; BUF_SIZE]; loop { let res = diff --git a/src/utils/consts.rs b/src/utils/consts.rs new file mode 100644 index 0000000..9abfb22 --- /dev/null +++ b/src/utils/consts.rs @@ -0,0 +1,9 @@ +// https://github.com/rust-lang/rust/blob/master/library/std/src/sys_common/io.rs#L1 +pub const DEFAULT_BUF_SIZE: usize = if cfg!(target_os = "espidf") { + 512 +} else { + 8 * 1024 +}; + +// Since Linux 2.6.11, the pipe capacity is 16 pages +pub const DEFAULT_PIPE_CAP: usize = 16 * 4096; diff --git a/src/utils/mod.rs b/src/utils/mod.rs index b66a16f..cedd490 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,7 +1,9 @@ mod types; - pub use types::*; +mod consts; +pub use consts::*; + use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr}; #[allow(unused)]