Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rcv and snd buf to both peer and accept #10

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions vsock-bridge/include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ namespace vsockproxy
ServiceType _type = ServiceType::UNKNOWN;
EndpointConfig _listenEndpoint;
EndpointConfig _connectEndpoint;
int _acceptRcvBuf = -1;
int _acceptSndBuf = -1;
int _peerRcvBuf = -1;
int _peerSndBuf = -1;
};

std::vector<ServiceDescription> loadConfig(const std::string& filepath);
Expand Down
68 changes: 67 additions & 1 deletion vsock-bridge/include/listener.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,12 @@ namespace vsockio
const int MAX_POLLER_EVENTS = 256;
const int SO_BACKLOG = 64;

Listener(std::unique_ptr<Endpoint>&& listenEndpoint, std::unique_ptr<Endpoint>&& connectEndpoint, Dispatcher& dispatcher)
Listener(std::unique_ptr<Endpoint>&& listenEndpoint, std::unique_ptr<Endpoint>&& 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])
Expand Down Expand Up @@ -174,6 +178,35 @@ namespace vsockio
return;
}

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 (_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");
}
}

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)
{
Expand Down Expand Up @@ -209,6 +242,35 @@ namespace vsockio
return nullptr;
}

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 (_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");
}
}

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)
Expand All @@ -232,6 +294,10 @@ namespace vsockio
inline bool listening() const { return _fd >= 0; }

int _fd;
int _acceptRcvBuf;
int _acceptSndBuf;
int _peerRcvBuf;
int _peerSndBuf;
std::unique_ptr<Endpoint> _listenEp;
std::unique_ptr<Endpoint> _listenEpClone;
std::unique_ptr<Endpoint> _connectEp;
Expand Down
16 changes: 16 additions & 0 deletions vsock-bridge/src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions vsock-bridge/src/vsock-bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ static std::unique_ptr<Endpoint> createEndpoint(EndpointScheme scheme, const std
}
}

static std::unique_ptr<Listener> 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<Listener> 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) };
Expand All @@ -44,7 +44,7 @@ static std::unique_ptr<Listener> createListener(Dispatcher& dispatcher, Endpoint
}
else
{
return std::make_unique<Listener>(std::move(listenEp), std::move(connectEp), dispatcher);
return std::make_unique<Listener>(std::move(listenEp), std::move(connectEp), dispatcher, acceptRcvBuf, acceptSndBuf, peerRcvBuf, peerSndBuf);
}
}

Expand All @@ -68,7 +68,11 @@ static void startServices(const std::vector<ServiceDescription>& services, int n
/*inPort:*/ sd._listenEndpoint._port,
/*outScheme:*/ sd._connectEndpoint._scheme,
/*outAddress:*/ sd._connectEndpoint._address,
/*outPort:*/ sd._connectEndpoint._port
/*outPort:*/ sd._connectEndpoint._port,
sd._acceptRcvBuf,
sd._acceptSndBuf,
sd._peerRcvBuf,
sd._peerSndBuf
);

if (!listener)
Expand Down
Loading