Skip to content

Commit

Permalink
Set TCP_NODELAY on the socket used for intercept IPC to reduce latency.
Browse files Browse the repository at this point in the history
On some systems, Nagle's algorithm was delaying receipt of the data,
causing commands with intercept or log_subcmds to run slowly.
Related to Bug #1034.
  • Loading branch information
millert committed Jun 20, 2022
1 parent b10201b commit 332a6af
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/exec_intercept.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>

#if defined(HAVE_STDINT_H)
# include <stdint.h>
Expand Down Expand Up @@ -946,7 +947,7 @@ intercept_accept_cb(int fd, int what, void *v)
struct sudo_event_base *evbase = sudo_ev_get_base(&closure->ev);
struct sockaddr_in sin;
socklen_t sin_len = sizeof(sin);
int client_sock, flags;
int client_sock, flags, on = 1;
debug_decl(intercept_accept_cb, SUDO_DEBUG_EXEC);

if (closure->state != RECV_CONNECTION) {
Expand All @@ -967,6 +968,9 @@ intercept_accept_cb(int fd, int what, void *v)
if (flags != -1)
(void)fcntl(client_sock, F_SETFL, flags | O_NONBLOCK);

/* Send data immediately, we need low latency IPC. */
(void)setsockopt(client_sock, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));

/*
* Create a new intercept closure and register an event for client_sock.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/sudo_intercept_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>

#if defined(HAVE_STDINT_H)
# include <stdint.h>
Expand Down Expand Up @@ -355,6 +356,7 @@ static int
intercept_connect(void)
{
int sock = -1;
int on = 1;
struct sockaddr_in sin;
debug_decl(command_allowed, SUDO_DEBUG_EXEC);

Expand All @@ -374,6 +376,9 @@ intercept_connect(void)
goto done;
}

/* Send data immediately, we need low latency IPC. */
(void)setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));

if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
sudo_warn("connect");
close(sock);
Expand Down

0 comments on commit 332a6af

Please sign in to comment.