commit ae6f563: [Project] Preliminary usage of the reuseport

Vsevolod Stakhov vsevolod at highsecure.ru
Tue Jun 9 13:49:10 UTC 2020


Author: Vsevolod Stakhov
Date: 2020-06-09 14:22:44 +0100
URL: https://github.com/rspamd/rspamd/commit/ae6f563623c765e574bd9a211be7524f7495ea28

[Project] Preliminary usage of the reuseport

---
 src/libserver/worker_util.c | 41 +++++++++++++++++++++++++++++++++++++++++
 src/rspamd.c                |  6 ++++--
 2 files changed, 45 insertions(+), 2 deletions(-)

diff --git a/src/libserver/worker_util.c b/src/libserver/worker_util.c
index 639fc9480..d5e603cfa 100644
--- a/src/libserver/worker_util.c
+++ b/src/libserver/worker_util.c
@@ -933,6 +933,36 @@ rspamd_main_heartbeat_start (struct rspamd_worker *wrk, struct ev_loop *event_lo
 	ev_timer_start (event_loop, &wrk->hb.heartbeat_ev);
 }
 
+static bool
+rspamd_maybe_reuseport_socket (struct rspamd_worker_listen_socket *ls)
+{
+	if (ls->fd == -1 || ls->is_systemd ||
+		rspamd_inet_address_get_af (ls->addr) == AF_UNIX) {
+		return false;
+	}
+
+#if defined(SO_REUSEPORT) && defined(SO_REUSEADDR)
+	gint nfd;
+
+	nfd = rspamd_inet_address_listen (ls->addr,
+			(ls->type == RSPAMD_WORKER_SOCKET_UDP ? SOCK_DGRAM : SOCK_STREAM),
+			RSPAMD_INET_ADDRESS_LISTEN_ASYNC|RSPAMD_INET_ADDRESS_LISTEN_REUSEPORT,
+			-1);
+
+	if (nfd == -1) {
+		msg_warn ("cannot create reuseport listen socket for %d: %s",
+				ls->fd, strerror (errno));
+	}
+	else {
+		close (ls->fd);
+		ls->fd = nfd;
+	}
+#else
+
+#endif
+
+	return false;
+}
 
 /**
  * Handles worker after fork returned zero
@@ -1006,6 +1036,17 @@ rspamd_handle_child_fork (struct rspamd_worker *wrk,
 		}
 	}
 
+	/* Reuseport before dropping privs */
+	GList *cur = cf->listen_socks;
+
+	while (cur) {
+		struct rspamd_worker_listen_socket *ls =
+				(struct rspamd_worker_listen_socket *)cur->data;
+
+		rspamd_maybe_reuseport_socket (ls);
+		cur = g_list_next (cur);
+	}
+
 	/* Drop privileges */
 	rspamd_worker_drop_priv (rspamd_main);
 	/* Set limits */
diff --git a/src/rspamd.c b/src/rspamd.c
index 5748fa7ff..2f86739b6 100644
--- a/src/rspamd.c
+++ b/src/rspamd.c
@@ -412,7 +412,8 @@ create_listen_socket (GPtrArray *addrs, guint cnt,
 		 */
 		if (listen_type & RSPAMD_WORKER_SOCKET_TCP) {
 			fd = rspamd_inet_address_listen (g_ptr_array_index (addrs, i),
-					SOCK_STREAM, RSPAMD_INET_ADDRESS_LISTEN_ASYNC, -1);
+					SOCK_STREAM,
+					RSPAMD_INET_ADDRESS_LISTEN_ASYNC|RSPAMD_INET_ADDRESS_LISTEN_REUSEPORT, -1);
 			if (fd != -1) {
 				ls = g_malloc0 (sizeof (*ls));
 				ls->addr = rspamd_inet_address_copy (g_ptr_array_index (addrs, i));
@@ -423,7 +424,8 @@ create_listen_socket (GPtrArray *addrs, guint cnt,
 		}
 		if (listen_type & RSPAMD_WORKER_SOCKET_UDP) {
 			fd = rspamd_inet_address_listen (g_ptr_array_index (addrs, i),
-					SOCK_DGRAM, RSPAMD_INET_ADDRESS_LISTEN_ASYNC, -1);
+					SOCK_DGRAM,
+					RSPAMD_INET_ADDRESS_LISTEN_ASYNC|RSPAMD_INET_ADDRESS_LISTEN_REUSEPORT, -1);
 			if (fd != -1) {
 				ls = g_malloc0 (sizeof (*ls));
 				ls->addr = rspamd_inet_address_copy (g_ptr_array_index (addrs, i));


More information about the Commits mailing list