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

adding the ability to pass buffer sizes into the config #9

Open
wants to merge 5 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
2 changes: 2 additions & 0 deletions vsock-bridge/include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ namespace vsockproxy
ServiceType _type = ServiceType::UNKNOWN;
EndpointConfig _listenEndpoint;
EndpointConfig _connectEndpoint;
int _sndbuf;
int _rcvbuf;
};

std::vector<ServiceDescription> loadConfig(const std::string& filepath);
Expand Down
31 changes: 30 additions & 1 deletion vsock-bridge/include/listener.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,11 @@ 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 SNDBUF, int RCVBUF)
: _fd(-1)
, _sndbuf(SNDBUF)
, _rcvbuf(RCVBUF)
, _listenEp(std::move(listenEndpoint))
, _connectEp(std::move(connectEndpoint))
, _events(new VsbEvent[MAX_POLLER_EVENTS])
Expand Down Expand Up @@ -174,6 +177,18 @@ namespace vsockio
return;
}

if (setsockopt(clientFd, SOL_SOCKET, SO_RCVBUF, &_rcvbuf, sizeof(int)) < 0)
{
close(clientFd);
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)
{
Expand Down Expand Up @@ -209,6 +224,18 @@ 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);
throw std::runtime_error("error setting SO_SNDBUF");
}

auto addrAndLen = _connectEp->getAddress();
int status = connect(fd, addrAndLen.first, addrAndLen.second);
if (status == 0)
Expand All @@ -232,6 +259,8 @@ namespace vsockio
inline bool listening() const { return _fd >= 0; }

int _fd;
int _sndbuf;
int _rcvbuf;
std::unique_ptr<Endpoint> _listenEp;
std::unique_ptr<Endpoint> _listenEpClone;
std::unique_ptr<Endpoint> _connectEp;
Expand Down
9 changes: 6 additions & 3 deletions vsock-bridge/src/vsock-bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ 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 SNDBUF, int RCVBUF)
{
auto listenEp { createEndpoint(inScheme, inAddress, inPort) };
auto connectEp{ createEndpoint(outScheme, outAddress, outPort) };
Expand All @@ -44,7 +45,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, SNDBUF, RCVBUF);
}
}

Expand All @@ -68,7 +69,9 @@ 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._sndbuf,
sd._rcvbuf
);

if (!listener)
Expand Down
Loading