Skip to content
Merged
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
72 changes: 58 additions & 14 deletions sbin/prowld/ipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ static pid_t sysrc_pid;
static ipc_client_t *ipc_get_client(int fd);
static void ipc_send_error(int fd, const char *id, const char *message);
static void ipc_dispatch_buffered(ipc_client_t *);
static void ipc_resume_client(ipc_client_t *);

static uintptr_t
ipc_event_tag(uintptr_t type)
Expand Down Expand Up @@ -177,6 +178,17 @@ json_escape_str(const char *src, char *dst, size_t dstsz)
/* Upper bound on queued output per client; slower readers lose messages. */
#define IPC_OUT_MAX (IPC_REPLY_MAX * 2)

/*
* Once this much output is queued for a client, its further requests are
* not handled until the queue drains below it again. They are still read
* into the receive buffer, so a client blocked writing a batch of requests
* can get to its replies; only a full receive buffer stops reading. A
* client that keeps sending requests without reading the replies is slowed
* down instead of making prowld queue replies up to IPC_OUT_MAX and then
* drop them.
*/
#define IPC_OUT_HIWAT (1024 * 1024)

/*
* Drop everything queued for a client and stop watching for writability.
*/
Expand Down Expand Up @@ -295,6 +307,9 @@ ipc_send(int fd, const char *json, size_t len)
/* If EVFILT_WRITE is armed, it will drain the queue in order. */
if (!c->want_write)
ipc_flush_client(c);

if (c->want_write && c->out_len - c->out_off > IPC_OUT_HIWAT)
c->out_held = true;
}

/*
Expand All @@ -309,10 +324,37 @@ ipc_write_ready(int fd, uintptr_t tag)
if (c == NULL || c->event_tag != tag)
return;
ipc_flush_client(c);
if (c->read_eof && !c->want_write)
if (c->out_held && c->out_len - c->out_off <= IPC_OUT_HIWAT) {
c->out_held = false;
ipc_resume_client(c);
if (!c->active)
return;
}
if (c->read_eof && !c->want_write && !c->out_held &&
c->action_tag == 0)
ipc_close_client(fd);
}

/*
* Read and dispatch the client's requests again once it has neither a
* service action running nor too much output waiting to be sent.
*/
static void
ipc_resume_client(ipc_client_t *c)
{
struct timespec now;

if (!c->active || c->action_tag != 0 || c->out_held)
return;
if (ipc_watch_read(c, EV_ADD | EV_ENABLE, false) == -1 ||
clock_gettime(CLOCK_MONOTONIC, &now) == -1) {
ipc_close_client(c->fd);
return;
}
c->frame_started = now.tv_sec;
ipc_dispatch_buffered(c);
}

static void
ipc_send_ok(int fd, const char *id, const char *result_json)
{
Expand Down Expand Up @@ -790,7 +832,6 @@ void
ipc_action_complete(uintptr_t tag, int code, const char *out)
{
ipc_client_t *client = NULL;
struct timespec now;

for (size_t i = 0; i < nitems(g_clients); i++) {
if (g_clients[i].active && g_clients[i].action_tag == tag) {
Expand All @@ -808,13 +849,7 @@ ipc_action_complete(uintptr_t tag, int code, const char *out)
else
ipc_send_error(client->fd, client->action_id,
"could not persist enable setting with sysrc");
if (ipc_watch_read(client, EV_ADD | EV_ENABLE, false) == -1 ||
clock_gettime(CLOCK_MONOTONIC, &now) == -1) {
ipc_close_client(client->fd);
return;
}
client->frame_started = now.tv_sec;
ipc_dispatch_buffered(client);
ipc_resume_client(client);
}

static void
Expand Down Expand Up @@ -1512,6 +1547,7 @@ ipc_expire_clients(void)
return;
for (i = 0; i < IPC_MAX_CLIENTS; i++) {
if (g_clients[i].active && g_clients[i].action_tag == 0 &&
!g_clients[i].out_held &&
(!g_clients[i].completed_request || g_clients[i].buf_used != 0) &&
now.tv_sec - g_clients[i].frame_started >= IPC_FRAME_TIMEOUT)
ipc_close_client(g_clients[i].fd);
Expand All @@ -1536,6 +1572,7 @@ ipc_close_client(int fd)
g_clients[i].out_len = 0;
g_clients[i].out_off = 0;
g_clients[i].want_write = false;
g_clients[i].out_held = false;
g_clients[i].is_subscriber = false;
g_clients[i].active = false;
g_clients[i].fd = -1;
Expand All @@ -1556,11 +1593,17 @@ ipc_read_client(int fd, uintptr_t tag, bool eof)

if (client == NULL || client->event_tag != tag)
return;
if (client->action_tag != 0) {
/* Requests are held, and no more fit in the receive buffer. */
if (client->action_tag != 0 ||
(client->out_held && client->buf_used == IPC_MSG_MAX)) {
struct pollfd pfd = { .fd = fd };

if (!eof)
if (!eof) {
if (client->action_tag == 0 &&
ipc_watch_read(client, EV_ADD, true) == -1)
ipc_close_client(fd);
return;
}
/* A peer that only closed its write side can still read the reply. */
if (poll(&pfd, 1, 0) == -1 && errno == EINTR)
return;
Expand All @@ -1571,8 +1614,9 @@ ipc_read_client(int fd, uintptr_t tag, bool eof)
}

/*
* A partial frame is always shorter than IPC_MSG_MAX, so a full
* buffer can grow; complete frames were dispatched already.
* Unless requests are held, a partial frame is always shorter than
* IPC_MSG_MAX, so a full buffer can grow; complete frames were
* dispatched already.
*/
if (client->buf_used == client->buf_cap) {
size_t ncap = client->buf_cap * 2;
Expand Down Expand Up @@ -1628,7 +1672,7 @@ ipc_dispatch_buffered(ipc_client_t *client)
{
int fd = client->fd;

while (client->action_tag == 0) {
while (client->action_tag == 0 && !client->out_held) {
uint32_t msglen;
size_t total;

Expand Down
1 change: 1 addition & 0 deletions sbin/prowld/prowld.h
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ typedef struct ipc_client {
size_t out_len; /* bytes queued in out */
size_t out_off; /* bytes of out already sent */
bool want_write; /* EVFILT_WRITE registered */
bool out_held; /* reads held: output over HIWAT */
bool read_eof; /* drain queued output before closing */
} ipc_client_t;

Expand Down
Loading