From d69cefc4b2290fe69ef2505612ba3d70e692fb17 Mon Sep 17 00:00:00 2001 From: Andrei Tarassov Date: Tue, 29 Oct 2024 18:03:13 +1100 Subject: [PATCH] Turn off Nagle --- vsock-bridge/include/listener.h | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/vsock-bridge/include/listener.h b/vsock-bridge/include/listener.h index 635ab19..676f17a 100644 --- a/vsock-bridge/include/listener.h +++ b/vsock-bridge/include/listener.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -51,6 +52,17 @@ namespace vsockio } return true; } + + static bool setTcpNoDelay(int fd) { + int enable = 1; + if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &enable, sizeof(enable)) < 0) + { + const int err = errno; + Logger::instance->Log(Logger::ERROR, "setsockopt error: ", strerror(err)); + return false; + } + return true; + } }; struct Listener @@ -156,7 +168,13 @@ namespace vsockio return; } - auto outPeer = connectToPeer(); + if (_listenEp->getAddress().first->sa_family == AF_INET && !IOControl::setTcpNoDelay(clientFd)) + { + Logger::instance->Log(Logger::ERROR, "failed to turn off Nagle algorithm (fd=", clientFd, ")"); + return; + } + + auto outPeer = connectToPeer(); if (!outPeer) { return; @@ -183,6 +201,12 @@ namespace vsockio return nullptr; } + if (_connectEp->getAddress().first->sa_family == AF_INET && !IOControl::setTcpNoDelay(fd)) + { + Logger::instance->Log(Logger::ERROR, "failed to turn off Nagle algorithm (fd=", fd, ")"); + return nullptr; + } + auto addrAndLen = _connectEp->getAddress(); int status = connect(fd, addrAndLen.first, addrAndLen.second); if (status == 0 || (status = errno) == EINPROGRESS)