From 89f9bec49abb0e7016e0c2c9f46d7d758a0bd91c Mon Sep 17 00:00:00 2001 From: Vort Date: Thu, 29 Feb 2024 16:02:43 +0200 Subject: [PATCH 1/2] derive SSU2 socket buffer size from bandwidth limit --- libi2pd/SSU2.cpp | 33 +++++++++++++++++++-------------- libi2pd/SSU2.h | 4 ++-- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/libi2pd/SSU2.cpp b/libi2pd/SSU2.cpp index b2452fda..d7a82ee8 100644 --- a/libi2pd/SSU2.cpp +++ b/libi2pd/SSU2.cpp @@ -256,27 +256,32 @@ namespace transport socket.open (localEndpoint.protocol ()); if (localEndpoint.address ().is_v6 ()) socket.set_option (boost::asio::ip::v6_only (true)); - boost::asio::socket_base::receive_buffer_size receive_buffer_size_set (SSU2_SOCKET_RECEIVE_BUFFER_SIZE); - boost::asio::socket_base::send_buffer_size send_buffer_size_set (SSU2_SOCKET_SEND_BUFFER_SIZE); - socket.set_option (receive_buffer_size_set); - socket.set_option (send_buffer_size_set); - boost::asio::socket_base::receive_buffer_size receive_buffer_size_get; - boost::asio::socket_base::send_buffer_size send_buffer_size_get; - socket.get_option (receive_buffer_size_get); - socket.get_option (send_buffer_size_get); - if (receive_buffer_size_get.value () != receive_buffer_size_set.value () || - send_buffer_size_get.value () != send_buffer_size_set.value ()) + + uint64_t bufferSize = i2p::context.GetBandwidthLimit() * 1024 / 5; // max lag = 200ms + bufferSize = std::max(SSU2_SOCKET_MIN_BUFFER_SIZE, std::min(bufferSize, SSU2_SOCKET_MAX_BUFFER_SIZE)); + + boost::asio::socket_base::receive_buffer_size receiveBufferSizeSet (bufferSize); + boost::asio::socket_base::send_buffer_size sendBufferSizeSet (bufferSize); + socket.set_option (receiveBufferSizeSet); + socket.set_option (sendBufferSizeSet); + boost::asio::socket_base::receive_buffer_size receiveBufferSizeGet; + boost::asio::socket_base::send_buffer_size sendBufferSizeGet; + socket.get_option (receiveBufferSizeGet); + socket.get_option (sendBufferSizeGet); + if (receiveBufferSizeGet.value () != receiveBufferSizeSet.value () || + sendBufferSizeGet.value () != sendBufferSizeSet.value ()) { LogPrint (eLogWarning, "SSU2: Socket receive buffer size: requested = ", - receive_buffer_size_set.value (), ", got = ", receive_buffer_size_get.value ()); + receiveBufferSizeSet.value (), ", got = ", receiveBufferSizeGet.value ()); LogPrint (eLogWarning, "SSU2: Socket send buffer size: requested = ", - send_buffer_size_set.value (), ", got = ", send_buffer_size_get.value ()); + sendBufferSizeSet.value (), ", got = ", sendBufferSizeGet.value ()); } else { - LogPrint (eLogInfo, "SSU2: Socket receive buffer size: ", receive_buffer_size_get.value ()); - LogPrint (eLogInfo, "SSU2: Socket send buffer size: ", send_buffer_size_get.value ()); + LogPrint (eLogInfo, "SSU2: Socket receive buffer size: ", receiveBufferSizeGet.value ()); + LogPrint (eLogInfo, "SSU2: Socket send buffer size: ", sendBufferSizeGet.value ()); } + socket.non_blocking (true); } catch (std::exception& ex ) diff --git a/libi2pd/SSU2.h b/libi2pd/SSU2.h index da5ca317..b1aa073e 100644 --- a/libi2pd/SSU2.h +++ b/libi2pd/SSU2.h @@ -25,8 +25,8 @@ namespace transport const int SSU2_RESEND_CHECK_TIMEOUT_VARIANCE = 100; // in milliseconds const int SSU2_RESEND_CHECK_MORE_TIMEOUT = 10; // in milliseconds const size_t SSU2_MAX_RESEND_PACKETS = 128; // packets to resend at the time - const size_t SSU2_SOCKET_RECEIVE_BUFFER_SIZE = 0x1FFFF; // 128K - const size_t SSU2_SOCKET_SEND_BUFFER_SIZE = 0x1FFFF; // 128K + const uint64_t SSU2_SOCKET_MIN_BUFFER_SIZE = 128 * 1024; + const uint64_t SSU2_SOCKET_MAX_BUFFER_SIZE = 4 * 1024 * 1024; const size_t SSU2_MAX_NUM_INTRODUCERS = 3; const size_t SSU2_MIN_RECEIVED_PACKET_SIZE = 40; // 16 byte short header + 8 byte minimum payload + 16 byte MAC const int SSU2_TO_INTRODUCER_SESSION_DURATION = 3600; // 1 hour From e5f75eb61ca87453e5d090eba28a6112ccb1eae3 Mon Sep 17 00:00:00 2001 From: Vort Date: Thu, 29 Feb 2024 16:12:51 +0200 Subject: [PATCH 2/2] log would_block error at info level --- libi2pd/SSU2.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/libi2pd/SSU2.cpp b/libi2pd/SSU2.cpp index d7a82ee8..28ffb8a0 100644 --- a/libi2pd/SSU2.cpp +++ b/libi2pd/SSU2.cpp @@ -662,7 +662,10 @@ namespace transport if (!ec) i2p::transport::transports.UpdateSentBytes (headerLen + payloadLen); else - LogPrint (eLogError, "SSU2: Send exception: ", ec.message (), " to ", to); + { + LogPrint (ec == boost::asio::error::would_block ? eLogInfo : eLogError, + "SSU2: Send exception: ", ec.message (), " to ", to); + } } void SSU2Server::Send (const uint8_t * header, size_t headerLen, const uint8_t * headerX, size_t headerXLen, @@ -696,7 +699,10 @@ namespace transport if (!ec) i2p::transport::transports.UpdateSentBytes (headerLen + headerXLen + payloadLen); else - LogPrint (eLogError, "SSU2: Send exception: ", ec.message (), " to ", to); + { + LogPrint (ec == boost::asio::error::would_block ? eLogInfo : eLogError, + "SSU2: Send exception: ", ec.message (), " to ", to); + } } bool SSU2Server::CreateSession (std::shared_ptr router,