From f5ca254d24a272a931c65bacc0a9989cd282b1bd Mon Sep 17 00:00:00 2001 From: Cody Constine Date: Wed, 4 Dec 2024 13:57:47 -0700 Subject: [PATCH 1/5] adding the ability to pass buffer sizes into the config --- vsock-bridge/include/config.h | 2 ++ vsock-bridge/include/listener.h | 15 ++++++++++++++- vsock-bridge/src/vsock-bridge.cpp | 9 ++++++--- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/vsock-bridge/include/config.h b/vsock-bridge/include/config.h index a8cd0e3..a26cc1c 100644 --- a/vsock-bridge/include/config.h +++ b/vsock-bridge/include/config.h @@ -32,6 +32,8 @@ namespace vsockproxy ServiceType _type = ServiceType::UNKNOWN; EndpointConfig _listenEndpoint; EndpointConfig _connectEndpoint; + int _sndbuf; + int _rcvbuf; }; std::vector loadConfig(const std::string& filepath); diff --git a/vsock-bridge/include/listener.h b/vsock-bridge/include/listener.h index 52383df..a62356e 100644 --- a/vsock-bridge/include/listener.h +++ b/vsock-bridge/include/listener.h @@ -70,7 +70,8 @@ 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 SNDBUF, int RCVBUF) : _fd(-1) , _listenEp(std::move(listenEndpoint)) , _connectEp(std::move(connectEndpoint)) @@ -91,6 +92,18 @@ namespace vsockio throw std::runtime_error("error setting SO_REUSEADDR"); } + if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &SNDBUF, sizeof(int)) < 0) + { + close(fd); + throw std::runtime_error("error setting SO_SNDBUF"); + } + + if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &RCVBUF, sizeof(int)) < 0) + { + close(fd); + throw std::runtime_error("error setting SO_RCVBUF"); + } + std::pair addressAndLen = _listenEp->getAddress(); if (bind(fd, addressAndLen.first, addressAndLen.second) < 0) diff --git a/vsock-bridge/src/vsock-bridge.cpp b/vsock-bridge/src/vsock-bridge.cpp index ed7cd6d..f5c2a4c 100644 --- a/vsock-bridge/src/vsock-bridge.cpp +++ b/vsock-bridge/src/vsock-bridge.cpp @@ -27,7 +27,8 @@ 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 SNDBUF, int RCVBUF) { auto listenEp { createEndpoint(inScheme, inAddress, inPort) }; auto connectEp{ createEndpoint(outScheme, outAddress, outPort) }; @@ -44,7 +45,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, SNDBUF, RCVBUF); } } @@ -68,7 +69,9 @@ 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 + /*outPort:*/ sd._connectEndpoint._port, + sd._sndbuf, + sd._rcvbuf ); if (!listener) From b250bbd36de2ccb55ac47c97ce8b7a1411cadb71 Mon Sep 17 00:00:00 2001 From: Cody Constine Date: Wed, 4 Dec 2024 14:16:03 -0700 Subject: [PATCH 2/5] Moved to more important sockets --- vsock-bridge/include/listener.h | 40 +++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/vsock-bridge/include/listener.h b/vsock-bridge/include/listener.h index a62356e..2987059 100644 --- a/vsock-bridge/include/listener.h +++ b/vsock-bridge/include/listener.h @@ -73,6 +73,8 @@ namespace vsockio Listener(std::unique_ptr&& listenEndpoint, std::unique_ptr&& connectEndpoint, Dispatcher& dispatcher, int SNDBUF, int RCVBUF) : _fd(-1) + , _sndbuf(SNDBUF) + , _rcvbuf(RCVBUF) , _listenEp(std::move(listenEndpoint)) , _connectEp(std::move(connectEndpoint)) , _events(new VsbEvent[MAX_POLLER_EVENTS]) @@ -92,18 +94,6 @@ namespace vsockio throw std::runtime_error("error setting SO_REUSEADDR"); } - if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &SNDBUF, sizeof(int)) < 0) - { - close(fd); - throw std::runtime_error("error setting SO_SNDBUF"); - } - - if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &RCVBUF, sizeof(int)) < 0) - { - close(fd); - throw std::runtime_error("error setting SO_RCVBUF"); - } - std::pair addressAndLen = _listenEp->getAddress(); if (bind(fd, addressAndLen.first, addressAndLen.second) < 0) @@ -187,6 +177,18 @@ namespace vsockio return; } + if (setsockopt(clientFd, SOL_SOCKET, SO_SNDBUF, &_sndbuf, sizeof(int)) < 0) + { + close(clientFd); + throw std::runtime_error("error setting SO_SNDBUF"); + } + + if (setsockopt(clientFd, SOL_SOCKET, SO_RCVBUF, &_rcvbuf, sizeof(int)) < 0) + { + close(clientFd); + throw std::runtime_error("error setting SO_RCVBUF"); + } + auto outPeer = connectToPeer(); if (!outPeer) { @@ -222,6 +224,18 @@ namespace vsockio return nullptr; } + if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &_sndbuf, sizeof(int)) < 0) + { + close(fd); + throw std::runtime_error("error setting SO_SNDBUF"); + } + + if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &_rcvbuf, sizeof(int)) < 0) + { + close(fd); + throw std::runtime_error("error setting SO_RCVBUF"); + } + auto addrAndLen = _connectEp->getAddress(); int status = connect(fd, addrAndLen.first, addrAndLen.second); if (status == 0) @@ -245,6 +259,8 @@ namespace vsockio inline bool listening() const { return _fd >= 0; } int _fd; + int _sndbuf; + int _rcvbuf; std::unique_ptr _listenEp; std::unique_ptr _listenEpClone; std::unique_ptr _connectEp; From 4a17e28e3ffc806728733895c2de50a05f59137d Mon Sep 17 00:00:00 2001 From: Katherine Chen Date: Thu, 5 Dec 2024 11:26:20 +1100 Subject: [PATCH 3/5] Separate REC and SEND buffer --- vsock-bridge/include/listener.h | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/vsock-bridge/include/listener.h b/vsock-bridge/include/listener.h index 2987059..d8f0bff 100644 --- a/vsock-bridge/include/listener.h +++ b/vsock-bridge/include/listener.h @@ -177,12 +177,6 @@ namespace vsockio return; } - if (setsockopt(clientFd, SOL_SOCKET, SO_SNDBUF, &_sndbuf, sizeof(int)) < 0) - { - close(clientFd); - throw std::runtime_error("error setting SO_SNDBUF"); - } - if (setsockopt(clientFd, SOL_SOCKET, SO_RCVBUF, &_rcvbuf, sizeof(int)) < 0) { close(clientFd); @@ -230,12 +224,6 @@ namespace vsockio throw std::runtime_error("error setting SO_SNDBUF"); } - if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &_rcvbuf, sizeof(int)) < 0) - { - close(fd); - throw std::runtime_error("error setting SO_RCVBUF"); - } - auto addrAndLen = _connectEp->getAddress(); int status = connect(fd, addrAndLen.first, addrAndLen.second); if (status == 0) From c326fdd6c8f580f9409b2a4bd6111d0d44060242 Mon Sep 17 00:00:00 2001 From: Katherine Chen Date: Thu, 5 Dec 2024 11:31:22 +1100 Subject: [PATCH 4/5] Fix indentation --- vsock-bridge/include/config.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vsock-bridge/include/config.h b/vsock-bridge/include/config.h index a26cc1c..f060d43 100644 --- a/vsock-bridge/include/config.h +++ b/vsock-bridge/include/config.h @@ -32,8 +32,8 @@ namespace vsockproxy ServiceType _type = ServiceType::UNKNOWN; EndpointConfig _listenEndpoint; EndpointConfig _connectEndpoint; - int _sndbuf; - int _rcvbuf; + int _sndbuf; + int _rcvbuf; }; std::vector loadConfig(const std::string& filepath); From f7dced66943d6d60a7fa85e28877569c5b2434d2 Mon Sep 17 00:00:00 2001 From: Katherine Chen Date: Thu, 5 Dec 2024 15:57:30 +1100 Subject: [PATCH 5/5] Set both rcvbuf and sndbuf for accepted and peer clients --- vsock-bridge/include/listener.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/vsock-bridge/include/listener.h b/vsock-bridge/include/listener.h index d8f0bff..3ab953a 100644 --- a/vsock-bridge/include/listener.h +++ b/vsock-bridge/include/listener.h @@ -183,6 +183,12 @@ namespace vsockio throw std::runtime_error("error setting SO_RCVBUF"); } + if (setsockopt(clientFd, SOL_SOCKET, SO_SNDBUF, &_sndbuf, sizeof(int)) < 0) + { + close(clientFd); + throw std::runtime_error("error setting SO_SNDBUF"); + } + auto outPeer = connectToPeer(); if (!outPeer) { @@ -218,6 +224,12 @@ namespace vsockio return nullptr; } + if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &_rcvbuf, sizeof(int)) < 0) + { + close(fd); + throw std::runtime_error("error setting SO_RCVBUF"); + } + if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &_sndbuf, sizeof(int)) < 0) { close(fd);