From eaf727473cfd4ad6967ddc3777056ce24b6f0441 Mon Sep 17 00:00:00 2001 From: Katherine Chen Date: Thu, 5 Dec 2024 16:18:13 +1100 Subject: [PATCH 1/8] Add rcv and snd buf to both peer and accept --- vsock-bridge/include/listener.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/vsock-bridge/include/listener.h b/vsock-bridge/include/listener.h index 52383df..c4c39c8 100644 --- a/vsock-bridge/include/listener.h +++ b/vsock-bridge/include/listener.h @@ -174,6 +174,18 @@ namespace vsockio return; } + if (setsockopt(clientFd, SOL_SOCKET, SO_RCVBUF, &_acceptRcvBuf, sizeof(int)) < 0) + { + close(clientFd); + throw std::runtime_error("error setting _acceptRcvBuf to SO_RCVBUF"); + } + + if (setsockopt(clientFd, SOL_SOCKET, SO_SNDBUF, &_acceptSndBuf, sizeof(int)) < 0) + { + close(clientFd); + throw std::runtime_error("error setting _acceptSndBuf to SO_SNDBUF"); + } + auto outPeer = connectToPeer(); if (!outPeer) { @@ -209,6 +221,18 @@ namespace vsockio return nullptr; } + if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &_peerRcvBuf, sizeof(int)) < 0) + { + close(fd); + throw std::runtime_error("error setting _peerRcvBuf to SO_RCVBUF"); + } + + if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &_peerSndBuf, sizeof(int)) < 0) + { + close(fd); + throw std::runtime_error("error setting _peerSndBuf to SO_SNDBUF"); + } + auto addrAndLen = _connectEp->getAddress(); int status = connect(fd, addrAndLen.first, addrAndLen.second); if (status == 0) From 15a4f1001043694f85560fad454171feb8cbf875 Mon Sep 17 00:00:00 2001 From: Katherine Chen Date: Thu, 5 Dec 2024 16:47:22 +1100 Subject: [PATCH 2/8] Pass in buf value --- vsock-bridge/include/config.h | 4 ++++ vsock-bridge/include/listener.h | 6 +++++- vsock-bridge/src/config.cpp | 16 ++++++++++++++++ vsock-bridge/src/vsock-bridge.cpp | 9 ++++++--- 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/vsock-bridge/include/config.h b/vsock-bridge/include/config.h index a8cd0e3..2784435 100644 --- a/vsock-bridge/include/config.h +++ b/vsock-bridge/include/config.h @@ -32,6 +32,10 @@ namespace vsockproxy ServiceType _type = ServiceType::UNKNOWN; EndpointConfig _listenEndpoint; EndpointConfig _connectEndpoint; + int _acceptRcvBuf; + int _acceptSndBuf; + int _peerRcvBuf; + int _peerSndBuf; }; std::vector loadConfig(const std::string& filepath); diff --git a/vsock-bridge/include/listener.h b/vsock-bridge/include/listener.h index c4c39c8..8451351 100644 --- a/vsock-bridge/include/listener.h +++ b/vsock-bridge/include/listener.h @@ -70,8 +70,12 @@ namespace vsockio const int MAX_POLLER_EVENTS = 256; const int SO_BACKLOG = 64; - Listener(std::unique_ptr&& listenEndpoint, std::unique_ptr&& connectEndpoint, Dispatcher& dispatcher) + Listener(std::unique_ptr&& listenEndpoint, std::unique_ptr&& connectEndpoint, Dispatcher& dispatcher, int acceptRcvBuf, int acceptSndBuf, int peerRcvBuf, int peerSndBuf) : _fd(-1) + , _acceptRcvBuf(acceptRcvBuf) + , _acceptSndBuf(acceptSndBuf) + , _peerRcvBuf(peerRcvBuf) + , _peerSndBuf(peerSndBuf) , _listenEp(std::move(listenEndpoint)) , _connectEp(std::move(connectEndpoint)) , _events(new VsbEvent[MAX_POLLER_EVENTS]) diff --git a/vsock-bridge/src/config.cpp b/vsock-bridge/src/config.cpp index eaa4693..791e57d 100644 --- a/vsock-bridge/src/config.cpp +++ b/vsock-bridge/src/config.cpp @@ -251,6 +251,22 @@ namespace vsockproxy } cs._connectEndpoint = *endpoint; } + else if (line._key == "acceptRcvBuf") + { + cs._acceptRcvBuf = std::stoi(line._value); + } + else if (line._key == "acceptSndBuf") + { + cs._acceptSndBuf = std::stoi(line._value); + } + else if (line._key == "peerRcvBuf") + { + cs._peerRcvBuf = std::stoi(line._value); + } + else if (line._key == "peerSndBuf") + { + cs._peerSndBuf = std::stoi(line._value); + } } } } diff --git a/vsock-bridge/src/vsock-bridge.cpp b/vsock-bridge/src/vsock-bridge.cpp index ed7cd6d..eff9229 100644 --- a/vsock-bridge/src/vsock-bridge.cpp +++ b/vsock-bridge/src/vsock-bridge.cpp @@ -27,7 +27,7 @@ static std::unique_ptr createEndpoint(EndpointScheme scheme, const std } } -static std::unique_ptr createListener(Dispatcher& dispatcher, EndpointScheme inScheme, const std::string& inAddress, uint16_t inPort, EndpointScheme outScheme, const std::string& outAddress, uint16_t outPort) +static std::unique_ptr createListener(Dispatcher& dispatcher, EndpointScheme inScheme, const std::string& inAddress, uint16_t inPort, EndpointScheme outScheme, const std::string& outAddress, uint16_t outPort, int acceptRcvBuf, int acceptSndBuf, int peerRcvBuf, int peerSndBuf) { auto listenEp { createEndpoint(inScheme, inAddress, inPort) }; auto connectEp{ createEndpoint(outScheme, outAddress, outPort) }; @@ -44,7 +44,7 @@ static std::unique_ptr createListener(Dispatcher& dispatcher, Endpoint } else { - return std::make_unique(std::move(listenEp), std::move(connectEp), dispatcher); + return std::make_unique(std::move(listenEp), std::move(connectEp), dispatcher, acceptRcvBuf, acceptSndBuf, peerRcvBuf, peerSndBuf); } } @@ -68,7 +68,10 @@ static void startServices(const std::vector& services, int n /*inPort:*/ sd._listenEndpoint._port, /*outScheme:*/ sd._connectEndpoint._scheme, /*outAddress:*/ sd._connectEndpoint._address, - /*outPort:*/ sd._connectEndpoint._port + _acceptRcvBuf, + _acceptSndBuf, + _peerRcvBuf, + _peerSndBuf, ); if (!listener) From 00876039eab2dd9fefa1deacc2a93c35395a511e Mon Sep 17 00:00:00 2001 From: Katherine Chen Date: Thu, 5 Dec 2024 16:50:32 +1100 Subject: [PATCH 3/8] Pass in sd --- vsock-bridge/src/vsock-bridge.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/vsock-bridge/src/vsock-bridge.cpp b/vsock-bridge/src/vsock-bridge.cpp index eff9229..1a910ac 100644 --- a/vsock-bridge/src/vsock-bridge.cpp +++ b/vsock-bridge/src/vsock-bridge.cpp @@ -68,10 +68,10 @@ static void startServices(const std::vector& services, int n /*inPort:*/ sd._listenEndpoint._port, /*outScheme:*/ sd._connectEndpoint._scheme, /*outAddress:*/ sd._connectEndpoint._address, - _acceptRcvBuf, - _acceptSndBuf, - _peerRcvBuf, - _peerSndBuf, + sd._acceptRcvBuf, + sd._acceptSndBuf, + sd._peerRcvBuf, + sd._peerSndBuf, ); if (!listener) From 8caf63f70e665a835bf754bc6477eb26d1132ef2 Mon Sep 17 00:00:00 2001 From: Katherine Chen Date: Thu, 5 Dec 2024 16:52:32 +1100 Subject: [PATCH 4/8] Add variables to listener.h --- vsock-bridge/include/listener.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/vsock-bridge/include/listener.h b/vsock-bridge/include/listener.h index 8451351..9c7c724 100644 --- a/vsock-bridge/include/listener.h +++ b/vsock-bridge/include/listener.h @@ -260,6 +260,10 @@ namespace vsockio inline bool listening() const { return _fd >= 0; } int _fd; + int _acceptRcvBuf; + int _acceptSndBuf; + int _peerRcvBuf; + int _peerSndBuf; std::unique_ptr _listenEp; std::unique_ptr _listenEpClone; std::unique_ptr _connectEp; From dc80d0c955728b25eea55769b95da71318e726ce Mon Sep 17 00:00:00 2001 From: Katherine Chen Date: Thu, 5 Dec 2024 17:28:19 +1100 Subject: [PATCH 5/8] Fix errors --- vsock-bridge/src/vsock-bridge.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/vsock-bridge/src/vsock-bridge.cpp b/vsock-bridge/src/vsock-bridge.cpp index 1a910ac..88fd384 100644 --- a/vsock-bridge/src/vsock-bridge.cpp +++ b/vsock-bridge/src/vsock-bridge.cpp @@ -68,10 +68,11 @@ static void startServices(const std::vector& services, int n /*inPort:*/ sd._listenEndpoint._port, /*outScheme:*/ sd._connectEndpoint._scheme, /*outAddress:*/ sd._connectEndpoint._address, + /*outPort:*/ sd._connectEndpoint._port, sd._acceptRcvBuf, sd._acceptSndBuf, sd._peerRcvBuf, - sd._peerSndBuf, + sd._peerSndBuf ); if (!listener) From 9341e0a9629448362633f3544445ff9683faa137 Mon Sep 17 00:00:00 2001 From: Katherine Chen Date: Mon, 9 Dec 2024 11:39:13 +1100 Subject: [PATCH 6/8] Handle cases where no buf specified --- vsock-bridge/include/config.h | 8 +++---- vsock-bridge/include/listener.h | 40 ++++++++++++++++++++------------- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/vsock-bridge/include/config.h b/vsock-bridge/include/config.h index 2784435..3858735 100644 --- a/vsock-bridge/include/config.h +++ b/vsock-bridge/include/config.h @@ -32,10 +32,10 @@ namespace vsockproxy ServiceType _type = ServiceType::UNKNOWN; EndpointConfig _listenEndpoint; EndpointConfig _connectEndpoint; - int _acceptRcvBuf; - int _acceptSndBuf; - int _peerRcvBuf; - int _peerSndBuf; + int _acceptRcvBuf = -1; + int _acceptSndBuf = -1; + int _peerRcvBuf = -1; + int _peerSndBuf = -1; }; std::vector loadConfig(const std::string& filepath); diff --git a/vsock-bridge/include/listener.h b/vsock-bridge/include/listener.h index 9c7c724..4441670 100644 --- a/vsock-bridge/include/listener.h +++ b/vsock-bridge/include/listener.h @@ -178,16 +178,20 @@ namespace vsockio return; } - if (setsockopt(clientFd, SOL_SOCKET, SO_RCVBUF, &_acceptRcvBuf, sizeof(int)) < 0) - { - close(clientFd); - throw std::runtime_error("error setting _acceptRcvBuf to SO_RCVBUF"); + if (&_acceptRcvBuf != -1) { + if (setsockopt(clientFd, SOL_SOCKET, SO_RCVBUF, &_acceptRcvBuf, sizeof(int)) < 0) + { + close(clientFd); + throw std::runtime_error("error setting _acceptRcvBuf to SO_RCVBUF"); + } } - if (setsockopt(clientFd, SOL_SOCKET, SO_SNDBUF, &_acceptSndBuf, sizeof(int)) < 0) - { - close(clientFd); - throw std::runtime_error("error setting _acceptSndBuf to SO_SNDBUF"); + if (&_acceptSndBuf != -1) { + if (setsockopt(clientFd, SOL_SOCKET, SO_SNDBUF, &_acceptSndBuf, sizeof(int)) < 0) + { + close(clientFd); + throw std::runtime_error("error setting _acceptSndBuf to SO_SNDBUF"); + } } auto outPeer = connectToPeer(); @@ -225,16 +229,20 @@ namespace vsockio return nullptr; } - if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &_peerRcvBuf, sizeof(int)) < 0) - { - close(fd); - throw std::runtime_error("error setting _peerRcvBuf to SO_RCVBUF"); + if (&_peerRcvBuf != -1) { + if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &_peerRcvBuf, sizeof(int)) < 0) + { + close(fd); + throw std::runtime_error("error setting _peerRcvBuf to SO_RCVBUF"); + } } - if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &_peerSndBuf, sizeof(int)) < 0) - { - close(fd); - throw std::runtime_error("error setting _peerSndBuf to SO_SNDBUF"); + if (&_peerSndBuf != -1) { + if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &_peerSndBuf, sizeof(int)) < 0) + { + close(fd); + throw std::runtime_error("error setting _peerSndBuf to SO_SNDBUF"); + } } auto addrAndLen = _connectEp->getAddress(); From 0c2015ca24fa06395540439dcca27565c28d5704 Mon Sep 17 00:00:00 2001 From: Katherine Chen Date: Mon, 9 Dec 2024 11:47:28 +1100 Subject: [PATCH 7/8] Fix pointer error --- vsock-bridge/include/listener.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/vsock-bridge/include/listener.h b/vsock-bridge/include/listener.h index 4441670..977622b 100644 --- a/vsock-bridge/include/listener.h +++ b/vsock-bridge/include/listener.h @@ -178,7 +178,7 @@ namespace vsockio return; } - if (&_acceptRcvBuf != -1) { + if (_acceptRcvBuf != -1) { if (setsockopt(clientFd, SOL_SOCKET, SO_RCVBUF, &_acceptRcvBuf, sizeof(int)) < 0) { close(clientFd); @@ -186,7 +186,7 @@ namespace vsockio } } - if (&_acceptSndBuf != -1) { + if (_acceptSndBuf != -1) { if (setsockopt(clientFd, SOL_SOCKET, SO_SNDBUF, &_acceptSndBuf, sizeof(int)) < 0) { close(clientFd); @@ -229,7 +229,7 @@ namespace vsockio return nullptr; } - if (&_peerRcvBuf != -1) { + if (_peerRcvBuf != -1) { if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &_peerRcvBuf, sizeof(int)) < 0) { close(fd); @@ -237,7 +237,7 @@ namespace vsockio } } - if (&_peerSndBuf != -1) { + if (_peerSndBuf != -1) { if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &_peerSndBuf, sizeof(int)) < 0) { close(fd); From 4ade6bf2b1ee43328b84b21148b463d696d2f83c Mon Sep 17 00:00:00 2001 From: Katherine Chen Date: Mon, 9 Dec 2024 20:22:30 +1100 Subject: [PATCH 8/8] Add debug messages --- vsock-bridge/include/listener.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/vsock-bridge/include/listener.h b/vsock-bridge/include/listener.h index 977622b..f3ee79a 100644 --- a/vsock-bridge/include/listener.h +++ b/vsock-bridge/include/listener.h @@ -194,6 +194,19 @@ namespace vsockio } } + int debugAcceptRcvbuf; + int debugAcceptSndbuf; + socklen_t debugAcceptRcvbufLen = sizeof(debugAcceptRcvbuf); + socklen_t debugAcceptSndbufLen = sizeof(debugAcceptSndbuf); + + if (getsockopt(clientFd, SOL_SOCKET, SO_RCVBUF, &debugAcceptRcvbuf, &debugAcceptRcvbufLen) == 0) { + Logger::instance->Log(Logger::INFO, "Accept client Receive buffer size: ", debugAcceptRcvbuf, "bytes"); + } + + if (getsockopt(clientFd, SOL_SOCKET, SO_SNDBUF, &debugAcceptSndbuf, &debugAcceptSndbufLen) == 0) { + Logger::instance->Log(Logger::INFO, "Accept client Send buffer size: ", debugAcceptSndbuf, "bytes"); + } + auto outPeer = connectToPeer(); if (!outPeer) { @@ -245,6 +258,19 @@ namespace vsockio } } + int debugPeerRcvbuf; + int debugPeerSndbuf; + socklen_t debugPeerRcvbufLen = sizeof(debugPeerRcvbuf); + socklen_t debugPeerSndbufLen = sizeof(debugPeerSndbuf); + + if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &debugPeerRcvbuf, &debugPeerRcvbufLen) == 0) { + Logger::instance->Log(Logger::INFO, "Peer client Receive buffer size: ", debugPeerRcvbuf, "bytes"); + } + + if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &debugPeerSndbuf, &debugPeerSndbufLen) == 0) { + Logger::instance->Log(Logger::INFO, "Peer client Send buffer size: ", debugPeerSndbuf, "bytes"); + } + auto addrAndLen = _connectEp->getAddress(); int status = connect(fd, addrAndLen.first, addrAndLen.second); if (status == 0)