Skip to content

Commit

Permalink
daemon udp connect: use connected udp communication
Browse files Browse the repository at this point in the history
  • Loading branch information
Frantisek Tobias committed Sep 30, 2024
1 parent 7ef1a7c commit f0dee04
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion daemon/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void udp_recv(uv_udp_t *handle, ssize_t nread, const uv_buf_t *buf,
if (s->closing || nread <= 0 || comm_addr->sa_family == AF_UNSPEC)
return;

if (s->outgoing) {
if (!the_network->enable_connect_udp && s->outgoing) {
const struct sockaddr *peer = session2_get_peer(s);
if (kr_fails_assert(peer->sa_family != AF_UNSPEC))
return;
Expand Down
1 change: 1 addition & 0 deletions daemon/lua/kres-gen-33.lua
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,7 @@ struct network {
int snd;
int rcv;
} listen_tcp_buflens;
_Bool enable_connect_udp;
};
struct args *the_args;
struct endpoint {
Expand Down
1 change: 1 addition & 0 deletions daemon/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ void network_init(uv_loop_t *loop, int tcp_backlog)
the_network->tcp.tls_handshake_timeout = TLS_MAX_HANDSHAKE_TIME;
the_network->tcp.user_timeout = 1000; // 1s should be more than enough
the_network->tcp_backlog = tcp_backlog;
the_network->enable_connect_udp = true;

// On Linux, unset means some auto-tuning mechanism also depending on RAM,
// which might be OK default (together with the user_timeout above)
Expand Down
7 changes: 7 additions & 0 deletions daemon/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ struct network {
struct {
int snd, rcv;
} listen_udp_buflens, listen_tcp_buflens;

/** Use uv_udp_connect as the transport method for UDP.
* Enabling this increases the total number of syscalls, with a variable
* impact on the time spent processing them, sometimes resulting in
* a slight improvement in syscall processing efficiency.
* Note: This does not necessarily lead to overall performance gains. */
bool enable_connect_udp;
};

/** Pointer to the singleton network state. NULL if not initialized. */
Expand Down
4 changes: 2 additions & 2 deletions daemon/session2.c
Original file line number Diff line number Diff line change
Expand Up @@ -1447,8 +1447,8 @@ static int session2_transport_pushv(struct session2 *s,
ctx);
return kr_ok();
} else {
int ret = uv_udp_try_send((uv_udp_t*)handle,
(uv_buf_t *)iov, iovcnt, comm->comm_addr);
int ret = uv_udp_try_send((uv_udp_t*)handle, (uv_buf_t *)iov, iovcnt,
the_network->enable_connect_udp ? NULL : comm->comm_addr);
if (ret > 0) // equals buffer size, only confuses us
ret = 0;
if (ret == UV_EAGAIN) {
Expand Down
11 changes: 11 additions & 0 deletions daemon/worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,17 @@ static int transmit(struct qr_task *task)
struct comm_info out_comm = {
.comm_addr = (struct sockaddr *)choice
};

if (the_network->enable_connect_udp && session->outgoing && !session->stream) {
uv_udp_t *udp = (uv_udp_t *)session2_get_handle(session);
int connect_tries = 3;

do {
ret = uv_udp_connect(udp, out_comm.comm_addr);
} while (ret == UV_EADDRINUSE && --connect_tries > 0);
if (ret < 0)
kr_log_error(IO, "Failed to establish udp connection: %s\n", uv_strerror(ret));
}
ret = qr_task_send(task, session, &out_comm, task->pktbuf);
if (ret) {
session2_close(session);
Expand Down

0 comments on commit f0dee04

Please sign in to comment.