[master] 72aef9995 acceptor: plugable acceptor

Nils Goroll nils.goroll at uplex.de
Mon Sep 30 15:53:07 UTC 2024


commit 72aef9995479ec3a568739c2724f1781664b6f50
Author: Asad Sajjad Ahmed <asadsa at varnish-software.com>
Date:   Wed Sep 6 10:22:09 2023 +0200

    acceptor: plugable acceptor
    
    Make the acceptor code plugable and start off with two acceptor modules TCP
    and UDS. Adding a new QUIC acceptor module in the future should be easy now.
    
    Signed-off-by: Asad Sajjad Ahmed <asadsa at varnish-software.com>

diff --git a/bin/varnishd/acceptor/acceptor_priv.h b/bin/varnishd/acceptor/acceptor_priv.h
index 5876c00a3..727e07d1b 100644
--- a/bin/varnishd/acceptor/acceptor_priv.h
+++ b/bin/varnishd/acceptor/acceptor_priv.h
@@ -33,10 +33,6 @@
 struct sockaddr_storage;
 struct pool_task;
 
-enum vca_event {
-	VCA_EVENT_LADDR,
-};
-
 struct wrk_accept {
 	unsigned		magic;
 #define WRK_ACCEPT_MAGIC	0x8c4b4d59
diff --git a/bin/varnishd/acceptor/acceptor_tcp.h b/bin/varnishd/acceptor/acceptor_tcp.h
index 402f549bd..350d5f72a 100644
--- a/bin/varnishd/acceptor/acceptor_tcp.h
+++ b/bin/varnishd/acceptor/acceptor_tcp.h
@@ -37,10 +37,3 @@ struct listen_arg;
 int vca_tcp_config(void);
 int vca_tcp_open(char **, struct listen_arg *, const char **);
 int vca_tcp_reopen(void);
-
-void vca_tcp_init(void);
-void vca_tcp_start(struct cli *cli);
-void vca_tcp_event(struct cli *cli, enum vca_event event);
-void vca_tcp_accept(struct pool *pp);
-void vca_tcp_update(pthread_mutex_t *shut_mtx);
-void vca_tcp_shutdown(void);
diff --git a/bin/varnishd/acceptor/acceptor_uds.h b/bin/varnishd/acceptor/acceptor_uds.h
index 9abcc0f46..6a04546c3 100644
--- a/bin/varnishd/acceptor/acceptor_uds.h
+++ b/bin/varnishd/acceptor/acceptor_uds.h
@@ -44,10 +44,3 @@ struct uds_perms {
 int vca_uds_config(void);
 int vca_uds_open(char **, struct listen_arg *, const char **);
 int vca_uds_reopen(void);
-
-void vca_uds_init(void);
-void vca_uds_start(struct cli *cli);
-void vca_uds_event(struct cli *cli, enum vca_event event);
-void vca_uds_accept(struct pool *pp);
-void vca_uds_update(pthread_mutex_t *shut_mtx);
-void vca_uds_shutdown(void);
diff --git a/bin/varnishd/acceptor/cache_acceptor.c b/bin/varnishd/acceptor/cache_acceptor.c
index 88c4c22e6..375edb074 100644
--- a/bin/varnishd/acceptor/cache_acceptor.c
+++ b/bin/varnishd/acceptor/cache_acceptor.c
@@ -42,8 +42,6 @@
 #include "cache/cache_varnishd.h"
 #include "acceptor/cache_acceptor.h"
 #include "acceptor/acceptor_priv.h"
-#include "acceptor/acceptor_tcp.h"
-#include "acceptor/acceptor_uds.h"
 
 #include "cache/cache_transport.h"
 #include "cache/cache_pool.h"
@@ -126,9 +124,12 @@ vca_pace_good(void)
 void
 VCA_NewPool(struct pool *pp)
 {
+	struct acceptor *vca;
 
-	vca_tcp_accept(pp);
-	vca_uds_accept(pp);
+	VCA_Foreach(vca) {
+		CHECK_OBJ_NOTNULL(vca, ACCEPTOR_MAGIC);
+		vca->accept(pp);
+	}
 }
 
 void
@@ -147,6 +148,7 @@ VCA_DestroyPool(struct pool *pp)
 static void * v_matchproto_()
 vca_acct(void *arg)
 {
+	struct acceptor *vca;
 	vtim_real t0;
 
 	// XXX Actually a misnomer now because the accept happens in a pool
@@ -162,8 +164,12 @@ vca_acct(void *arg)
 
 	while (1) {
 		(void)sleep(1);
-		vca_tcp_update(&shut_mtx);
-		vca_uds_update(&shut_mtx);
+
+		VCA_Foreach(vca) {
+			CHECK_OBJ_NOTNULL(vca, ACCEPTOR_MAGIC);
+			vca->update(&shut_mtx);
+		}
+
 		vca_periodic(t0);
 	}
 
@@ -175,11 +181,14 @@ vca_acct(void *arg)
 void
 VCA_Start(struct cli *cli)
 {
+	struct acceptor *vca;
 
 	ASSERT_CLI();
 
-	vca_tcp_start(cli);
-	vca_uds_start(cli);
+	VCA_Foreach(vca) {
+		CHECK_OBJ_NOTNULL(vca, ACCEPTOR_MAGIC);
+		vca->start(cli);
+	}
 
 	PTOK(pthread_create(&VCA_thread, NULL, vca_acct, NULL));
 }
@@ -190,7 +199,6 @@ static void v_matchproto_(cli_func_t)
 ccf_listen_address(struct cli *cli, const char * const *av, void *priv)
 {
 	struct listen_sock *ls;
-	char h[VTCP_ADDRBUFSIZE], p[VTCP_PORTBUFSIZE];
 
 	(void)av;
 	(void)priv;
@@ -205,12 +213,17 @@ ccf_listen_address(struct cli *cli, const char * const *av, void *priv)
 		VTIM_sleep(.1);
 
 	PTOK(pthread_mutex_lock(&shut_mtx));
-		if (!ls->uds) {
-			VTCP_myname(ls->sock, h, sizeof h, p, sizeof p);
-			VCLI_Out(cli, "%s %s %s\n", ls->name, h, p);
-		}
-		else
-			VCLI_Out(cli, "%s %s -\n", ls->name, ls->endpoint);
+
+	/*
+	 * Varnishtest expects the list of listen sockets to come out in the
+	 * same order as it is specified on the command line.
+	 */
+	VTAILQ_FOREACH(ls, &heritage.socks, list) {
+		CHECK_OBJ_NOTNULL(ls, LISTEN_SOCK_MAGIC);
+		CHECK_OBJ_NOTNULL(ls->vca, ACCEPTOR_MAGIC);
+		ls->vca->event(cli, ls, VCA_EVENT_LADDR);
+	}
+
 	PTOK(pthread_mutex_unlock(&shut_mtx));
 }
 
@@ -224,20 +237,29 @@ static struct cli_proto vca_cmds[] = {
 void
 VCA_Init(void)
 {
+	struct acceptor *vca;
 
 	CLI_AddFuncs(vca_cmds);
 	Lck_New(&pace_mtx, lck_vcapace);
-	vca_tcp_init();
-	vca_uds_init();
+
+	VCA_Foreach(vca) {
+		CHECK_OBJ_NOTNULL(vca, ACCEPTOR_MAGIC);
+		vca->init();
+	}
 }
 
 void
 VCA_Shutdown(void)
 {
+	struct acceptor *vca;
 
 	PTOK(pthread_mutex_lock(&shut_mtx));
-	vca_tcp_shutdown();
-	vca_uds_shutdown();
+
+	VCA_Foreach(vca) {
+		CHECK_OBJ_NOTNULL(vca, ACCEPTOR_MAGIC);
+		vca->shutdown();
+	}
+
 	PTOK(pthread_mutex_unlock(&shut_mtx));
 }
 
diff --git a/bin/varnishd/acceptor/cache_acceptor.h b/bin/varnishd/acceptor/cache_acceptor.h
index 5e9fbe282..287c2e8ec 100644
--- a/bin/varnishd/acceptor/cache_acceptor.h
+++ b/bin/varnishd/acceptor/cache_acceptor.h
@@ -31,7 +31,49 @@
  */
 
 /* cache_acceptor.c */
+struct listen_sock;
+struct listen_arg;
+struct pool;
 
 void VCA_Init(void);
 void VCA_Start(struct cli *cli);
 void VCA_Shutdown(void);
+
+enum vca_event {
+	VCA_EVENT_LADDR,
+};
+
+typedef int acceptor_config_f(void);
+typedef void acceptor_init_f(void);
+typedef int acceptor_open_f(char **, struct listen_arg *,
+    const char **);
+typedef int acceptor_reopen_f(void);
+typedef void acceptor_start_f(struct cli *);
+typedef void acceptor_event_f(struct cli *, struct listen_sock *,
+    enum vca_event);
+typedef void acceptor_accept_f(struct pool *);
+typedef void acceptor_update_f(pthread_mutex_t *);
+typedef void acceptor_shutdown_f(void);
+
+struct acceptor {
+	unsigned			magic;
+#define ACCEPTOR_MAGIC			0x0611847c
+	VTAILQ_ENTRY(acceptor)		list;
+	const char			*name;
+
+	acceptor_config_f		*config;
+	acceptor_init_f			*init;
+	acceptor_open_f			*open;
+	acceptor_reopen_f		*reopen;
+	acceptor_start_f		*start;
+	acceptor_event_f		*event;
+	acceptor_accept_f		*accept;
+	acceptor_update_f		*update;
+	acceptor_shutdown_f		*shutdown;
+};
+
+#define VCA_Foreach(arg) for (arg = NULL; VCA__iter(&arg);)
+int VCA__iter(struct acceptor ** const pp);
+
+extern struct acceptor TCP_acceptor;
+extern struct acceptor UDS_acceptor;
diff --git a/bin/varnishd/acceptor/cache_acceptor_tcp.c b/bin/varnishd/acceptor/cache_acceptor_tcp.c
index ad6d91dc4..ad4737275 100644
--- a/bin/varnishd/acceptor/cache_acceptor_tcp.c
+++ b/bin/varnishd/acceptor/cache_acceptor_tcp.c
@@ -268,7 +268,7 @@ vca_tcp_sockopt_set(const struct listen_sock *ls, const struct sess *sp)
 	}
 }
 
-void
+static void
 vca_tcp_init(void)
 {
 
@@ -310,7 +310,7 @@ vca_tcp_listen(struct cli *cli, struct listen_sock *ls)
 	return (0);
 }
 
-void
+static void
 vca_tcp_start(struct cli *cli)
 {
 	struct listen_sock *ls;
@@ -330,14 +330,11 @@ vca_tcp_start(struct cli *cli)
 	}
 }
 
-void
+static void
 vca_tcp_event(struct cli *cli, struct listen_sock *ls, enum vca_event event)
 {
 	char h[VTCP_ADDRBUFSIZE], p[VTCP_PORTBUFSIZE];
 
-	if (ls->uds)
-		return;
-
 	switch (event) {
 	case VCA_EVENT_LADDR:
 		VTCP_myname(ls->sock, h, sizeof h, p, sizeof p);
@@ -554,7 +551,7 @@ vca_tcp_accept_task(struct worker *wrk, void *arg)
 	FREE_OBJ(ps);
 }
 
-void
+static void
 vca_tcp_accept(struct pool *pp)
 {
 	struct listen_sock *ls;
@@ -577,7 +574,7 @@ vca_tcp_accept(struct pool *pp)
 	}
 }
 
-void
+static void
 vca_tcp_update(pthread_mutex_t *shut_mtx)
 {
 	struct listen_sock *ls;
@@ -610,7 +607,7 @@ vca_tcp_update(pthread_mutex_t *shut_mtx)
 	PTOK(pthread_mutex_unlock(shut_mtx));
 }
 
-void
+static void
 vca_tcp_shutdown(void)
 {
 	struct listen_sock *ls;
@@ -627,3 +624,17 @@ vca_tcp_shutdown(void)
 		(void)close(i);
 	}
 }
+
+struct acceptor TCP_acceptor = {
+	.magic		= ACCEPTOR_MAGIC,
+	.name		= "tcp",
+	.config		= vca_tcp_config,
+	.init		= vca_tcp_init,
+	.open		= vca_tcp_open,
+	.reopen		= vca_tcp_reopen,
+	.start		= vca_tcp_start,
+	.event		= vca_tcp_event,
+	.accept		= vca_tcp_accept,
+	.update		= vca_tcp_update,
+	.shutdown	= vca_tcp_shutdown,
+};
diff --git a/bin/varnishd/acceptor/cache_acceptor_uds.c b/bin/varnishd/acceptor/cache_acceptor_uds.c
index bea713abe..c18406474 100644
--- a/bin/varnishd/acceptor/cache_acceptor_uds.c
+++ b/bin/varnishd/acceptor/cache_acceptor_uds.c
@@ -242,7 +242,7 @@ vca_uds_sockopt_set(const struct listen_sock *ls, const struct sess *sp)
 	}
 }
 
-void
+static void
 vca_uds_init(void)
 {
 
@@ -278,7 +278,7 @@ vca_uds_listen(struct cli *cli, struct listen_sock *ls)
 	return (0);
 }
 
-void
+static void
 vca_uds_start(struct cli *cli)
 {
 	struct listen_sock *ls;
@@ -298,13 +298,10 @@ vca_uds_start(struct cli *cli)
 	}
 }
 
-void
+static void
 vca_uds_event(struct cli *cli, struct listen_sock *ls, enum vca_event event)
 {
 
-	if (!ls->uds)
-		return;
-
 	switch (event) {
 	case VCA_EVENT_LADDR:
 		CHECK_OBJ_NOTNULL(ls, LISTEN_SOCK_MAGIC);
@@ -508,7 +505,7 @@ vca_uds_accept_task(struct worker *wrk, void *arg)
 	FREE_OBJ(ps);
 }
 
-void
+static void
 vca_uds_accept(struct pool *pp)
 {
 	struct listen_sock *ls;
@@ -531,7 +528,7 @@ vca_uds_accept(struct pool *pp)
 	}
 }
 
-void
+static void
 vca_uds_update(pthread_mutex_t *shut_mtx)
 {
 	struct listen_sock *ls;
@@ -564,7 +561,7 @@ vca_uds_update(pthread_mutex_t *shut_mtx)
 	PTOK(pthread_mutex_unlock(shut_mtx));
 }
 
-void
+static void
 vca_uds_shutdown(void)
 {
 	struct listen_sock *ls;
@@ -581,3 +578,17 @@ vca_uds_shutdown(void)
 		(void)close(i);
 	}
 }
+
+struct acceptor UDS_acceptor = {
+	.magic		= ACCEPTOR_MAGIC,
+	.name		= "uds",
+	.config		= vca_uds_config,
+	.init		= vca_uds_init,
+	.open		= vca_uds_open,
+	.reopen		= vca_uds_reopen,
+	.start		= vca_uds_start,
+	.event		= vca_uds_event,
+	.accept		= vca_uds_accept,
+	.update		= vca_uds_update,
+	.shutdown	= vca_uds_shutdown,
+};
diff --git a/bin/varnishd/acceptor/mgt_acceptor.c b/bin/varnishd/acceptor/mgt_acceptor.c
index 1e25a9078..d172b4418 100644
--- a/bin/varnishd/acceptor/mgt_acceptor.c
+++ b/bin/varnishd/acceptor/mgt_acceptor.c
@@ -57,6 +57,36 @@
 static VTAILQ_HEAD(,listen_arg) listen_args =
     VTAILQ_HEAD_INITIALIZER(listen_args);
 
+static VTAILQ_HEAD(,acceptor) acceptors = VTAILQ_HEAD_INITIALIZER(acceptors);
+
+int
+VCA__iter(struct acceptor ** const pvca)
+{
+
+	AN(pvca);
+	CHECK_OBJ_ORNULL(*pvca, ACCEPTOR_MAGIC);
+	if (*pvca != NULL)
+		*pvca = VTAILQ_NEXT(*pvca, list);
+	else
+		*pvca = VTAILQ_FIRST(&acceptors);
+	return (*pvca != NULL);
+}
+
+static struct acceptor *
+VCA_Find(const char *name)
+{
+	struct acceptor *vca;
+
+	VCA_Foreach(vca) {
+		CHECK_OBJ_NOTNULL(vca, ACCEPTOR_MAGIC);
+
+		if (!strcmp(vca->name, name))
+			return (vca);
+	}
+
+	return (NULL);
+}
+
 /*=====================================================================
  * Reopen the accept sockets to get rid of listen status.
  * returns the highest errno encountered, 0 for success
@@ -65,12 +95,19 @@ static VTAILQ_HEAD(,listen_arg) listen_args =
 int
 VCA_reopen_sockets(void)
 {
-	int fail, fail2;
+	struct acceptor *vca;
+	int fail;
+	int err;
 
-	fail = vca_tcp_open();
-	fail2 = vca_uds_open();
+	fail = 0;
 
-	return (vmax(fail, fail2));
+	VCA_Foreach(vca) {
+		CHECK_OBJ_NOTNULL(vca, ACCEPTOR_MAGIC);
+		err = vca->reopen();
+		fail = vmax(fail, err);
+	}
+
+	return (fail);
 }
 
 /*--------------------------------------------------------------------*/
@@ -78,6 +115,7 @@ VCA_reopen_sockets(void)
 void
 VCA_Arg(const char *spec)
 {
+	struct acceptor *vca;
 	char **av;
 	struct listen_arg *la;
 	const char *err;
@@ -104,9 +142,12 @@ VCA_Arg(const char *spec)
 	la->name = name;
 
 	if (VUS_is(la->endpoint))
-		error = vca_uds_open(av[1], la, &err);
+		vca = VCA_Find("uds");
 	else
-		error = vca_tcp_open(av[1], la, &err);
+		vca = VCA_Find("tcp");
+
+	AN(vca);
+	error = vca->open(av, la, &err);
 
 	if (error)
 		ARGV_ERR("Got no socket(s) for %s (%s)\n", av[1], err);
@@ -114,3 +155,36 @@ VCA_Arg(const char *spec)
 		ARGV_ERR("Got no socket(s) for %s\n", av[1]);
 	VAV_Free(av);
 }
+
+void
+VCA_Add(struct acceptor *vca)
+{
+
+	CHECK_OBJ_NOTNULL(vca, ACCEPTOR_MAGIC);
+	AN(vca->name);
+	AN(vca->config);
+	AN(vca->init);
+	AN(vca->open);
+	AN(vca->reopen);
+	AN(vca->start);
+	AN(vca->event);
+	AN(vca->accept);
+	AN(vca->update);
+	AN(vca->shutdown);
+
+	if (VCA_Find(vca->name) != NULL)
+		ARGV_ERR("Acceptor '%s' already exist\n", vca->name);
+
+	VTAILQ_INSERT_TAIL(&acceptors, vca, list);
+
+	if (vca->config())
+		ARGV_ERR("Acceptor '%s' failed to initialize\n", vca->name);
+}
+
+void
+VCA_Config(void)
+{
+
+	VCA_Add(&TCP_acceptor);
+	VCA_Add(&UDS_acceptor);
+}
diff --git a/bin/varnishd/acceptor/mgt_acceptor.h b/bin/varnishd/acceptor/mgt_acceptor.h
index 5558383c5..ed9d4354d 100644
--- a/bin/varnishd/acceptor/mgt_acceptor.h
+++ b/bin/varnishd/acceptor/mgt_acceptor.h
@@ -43,5 +43,8 @@ struct listen_arg {
 	const struct uds_perms		*perms;
 };
 
+void VCA_Add(struct acceptor *);
+void VCA_Config(void);
+
 void VCA_Arg(const char *);
 int VCA_reopen_sockets(void);
diff --git a/bin/varnishd/acceptor/mgt_acceptor_tcp.c b/bin/varnishd/acceptor/mgt_acceptor_tcp.c
index 279ea7479..43ba9c2f2 100644
--- a/bin/varnishd/acceptor/mgt_acceptor_tcp.c
+++ b/bin/varnishd/acceptor/mgt_acceptor_tcp.c
@@ -111,6 +111,7 @@ vca_tcp_open_cb(void *priv, const struct suckaddr *sa)
 	AN(ls);
 
 	ls->sock = -1;
+	ls->vca = &TCP_acceptor;
 
 	ls->addr = VSA_Clone(sa);
 	AN(ls->addr);
diff --git a/bin/varnishd/acceptor/mgt_acceptor_uds.c b/bin/varnishd/acceptor/mgt_acceptor_uds.c
index 93641e39d..e1b0d3242 100644
--- a/bin/varnishd/acceptor/mgt_acceptor_uds.c
+++ b/bin/varnishd/acceptor/mgt_acceptor_uds.c
@@ -129,6 +129,7 @@ vca_uds_open_cb(void *priv, const struct sockaddr_un *uds)
 	AN(ls);
 
 	ls->sock = -1;
+	ls->vca = &UDS_acceptor;
 
 	ls->addr = VSA_Clone(bogo_ip);
 	AN(ls->addr);
diff --git a/bin/varnishd/common/heritage.h b/bin/varnishd/common/heritage.h
index 3112c1214..499c91423 100644
--- a/bin/varnishd/common/heritage.h
+++ b/bin/varnishd/common/heritage.h
@@ -44,6 +44,7 @@ struct listen_sock {
 #define LISTEN_SOCK_MAGIC		0x999e4b57
 	VTAILQ_ENTRY(listen_sock)	list;
 	VTAILQ_ENTRY(listen_sock)	arglist;
+	VTAILQ_ENTRY(listen_sock)	vcalist;
 	int				sock;
 	int				uds;
 	char				*endpoint;
@@ -53,6 +54,7 @@ struct listen_sock {
 	const struct uds_perms		*perms;
 	unsigned			test_heritage;
 	struct conn_heritage		*conn_heritage;
+	struct acceptor			*vca;
 };
 
 VTAILQ_HEAD(listen_sock_head, listen_sock);


More information about the varnish-commit mailing list