[master] 08cea68 Move the backend.* CLI functions to VDI level

Poul-Henning Kamp phk at FreeBSD.org
Tue Nov 14 12:53:06 UTC 2017


commit 08cea68f4c8fe26b6cc740bc38409b593c7cb1a1
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Tue Nov 14 12:51:51 2017 +0000

    Move the backend.* CLI functions to VDI level

diff --git a/bin/varnishd/cache/cache_backend_cfg.c b/bin/varnishd/cache/cache_backend_cfg.c
index d795b5b..d5e48bf 100644
--- a/bin/varnishd/cache/cache_backend_cfg.c
+++ b/bin/varnishd/cache/cache_backend_cfg.c
@@ -32,13 +32,11 @@
 
 #include "config.h"
 
-#include <fnmatch.h>
 #include <stdio.h>
 #include <stdlib.h>
 
 #include "cache_varnishd.h"
 
-#include "vcli_serve.h"
 #include "vtim.h"
 #include "waiter/waiter.h"
 
@@ -193,146 +191,6 @@ VBE_Delete(const struct director *d)
 	FREE_OBJ(be);
 }
 
-/*--------------------------------------------------------------------
- * Test if backend is healthy and report when it last changed
- */
-
-unsigned
-VDI_Healthy(const struct director *d, double *changed)
-{
-	CHECK_OBJ_NOTNULL(d, DIRECTOR_MAGIC);
-
-	if (changed != NULL)
-		*changed = d->health_changed;
-
-	if (d->admin_health == VDI_AH_PROBE)
-		return (d->health);
-
-	if (d->admin_health == VDI_AH_SICK)
-		return (0);
-
-	if (d->admin_health == VDI_AH_DELETED)
-		return (0);
-
-	if (d->admin_health == VDI_AH_HEALTHY)
-		return (1);
-
-	WRONG("Wrong admin health");
-}
-
-/*---------------------------------------------------------------------*/
-
-static int __match_proto__(vcl_be_func)
-do_list(struct cli *cli, struct director *d, void *priv)
-{
-	int *probes;
-	char time_str[VTIM_FORMAT_SIZE];
-	struct backend *be;
-
-	AN(priv);
-	probes = priv;
-	CHECK_OBJ_NOTNULL(d, DIRECTOR_MAGIC);
-	CAST_OBJ_NOTNULL(be, d->priv, BACKEND_MAGIC);
-	if (d->admin_health == VDI_AH_DELETED)
-		return (0);
-
-	VCLI_Out(cli, "\n%-30s", d->display_name);
-
-	VCLI_Out(cli, " %-10s", VDI_Ahealth(d));
-
-	if (be->probe == NULL)
-		VCLI_Out(cli, " %-20s", "Healthy (no probe)");
-	else {
-		if (d->health)
-			VCLI_Out(cli, " %-20s", "Healthy ");
-		else
-			VCLI_Out(cli, " %-20s", "Sick ");
-		VBP_Status(cli, be, *probes);
-	}
-
-	VTIM_format(d->health_changed, time_str);
-	VCLI_Out(cli, " %s", time_str);
-
-	return (0);
-}
-
-static void __match_proto__(cli_func_t)
-cli_backend_list(struct cli *cli, const char * const *av, void *priv)
-{
-	int probes = 0;
-
-	(void)priv;
-	ASSERT_CLI();
-	if (av[2] != NULL && !strcmp(av[2], "-p")) {
-		av++;
-		probes = 1;
-	} else if (av[2] != NULL && av[2][0] == '-') {
-		VCLI_Out(cli, "Invalid flags %s", av[2]);
-		VCLI_SetResult(cli, CLIS_PARAM);
-		return;
-	} else if (av[3] != NULL) {
-		VCLI_Out(cli, "Too many arguments");
-		VCLI_SetResult(cli, CLIS_PARAM);
-		return;
-	}
-	VCLI_Out(cli, "%-30s %-10s %-20s %s", "Backend name", "Admin",
-	    "Probe", "Last updated");
-	(void)VCL_IterDirector(cli, av[2], do_list, &probes);
-}
-
-/*---------------------------------------------------------------------*/
-
-static int __match_proto__(vcl_be_func)
-do_set_health(struct cli *cli, struct director *d, void *priv)
-{
-	unsigned prev;
-
-	(void)cli;
-	AN(priv);
-	CHECK_OBJ_NOTNULL(d, DIRECTOR_MAGIC);
-	if (d->admin_health == VDI_AH_DELETED)
-		return (0);
-	prev = VDI_Healthy(d, NULL);
-	d->admin_health = *(const struct vdi_ahealth **)priv;
-	(void)VDI_Ahealth(d);			// Acts like type-check
-	if (prev != VDI_Healthy(d, NULL))
-		d->health_changed = VTIM_real();
-
-	return (0);
-}
-
-static void __match_proto__()
-cli_backend_set_health(struct cli *cli, const char * const *av, void *priv)
-{
-	const struct vdi_ahealth *ah;
-	int n;
-
-	(void)av;
-	(void)priv;
-	ASSERT_CLI();
-	AN(av[2]);
-	AN(av[3]);
-	ah = VDI_Str2Ahealth(av[3]);
-	if (ah == NULL || ah == VDI_AH_DELETED) {
-		VCLI_Out(cli, "Invalid state %s", av[3]);
-		VCLI_SetResult(cli, CLIS_PARAM);
-		return;
-	}
-	n = VCL_IterDirector(cli, av[2], do_set_health, &ah);
-	if (n == 0) {
-		VCLI_Out(cli, "No Backends matches");
-		VCLI_SetResult(cli, CLIS_PARAM);
-	}
-}
-
-/*---------------------------------------------------------------------*/
-
-static struct cli_proto backend_cmds[] = {
-	{ CLICMD_BACKEND_LIST,		"", cli_backend_list },
-	{ CLICMD_BACKEND_SET_HEALTH,	"", cli_backend_set_health },
-	{ NULL }
-};
-
 /*---------------------------------------------------------------------*/
 
 void
@@ -362,6 +220,5 @@ void
 VBE_InitCfg(void)
 {
 
-	CLI_AddFuncs(backend_cmds);
 	Lck_New(&backends_mtx, lck_vbe);
 }
diff --git a/bin/varnishd/cache/cache_director.c b/bin/varnishd/cache/cache_director.c
index cbb3e8f..f47ac1a 100644
--- a/bin/varnishd/cache/cache_director.c
+++ b/bin/varnishd/cache/cache_director.c
@@ -38,6 +38,10 @@
 #include "cache_varnishd.h"
 
 #include "cache_director.h"
+#include "cache_backend.h"
+
+#include "vcli_serve.h"
+#include "vtim.h"
 
 /* -------------------------------------------------------------------*/
 
@@ -51,8 +55,8 @@ struct vdi_ahealth {
 VBE_AHEALTH_LIST
 #undef VBE_AHEALTH
 
-const struct vdi_ahealth *
-VDI_Str2Ahealth(const char *t)
+static const struct vdi_ahealth *
+vdi_str2ahealth(const char *t)
 {
 #define VBE_AHEALTH(l,u) if (!strcasecmp(t, #l)) return (VDI_AH_##u);
 VBE_AHEALTH_LIST
@@ -249,3 +253,152 @@ VDI_Panic(const struct director *d, struct vsb *vsb, const char *nm)
 	VSB_indent(vsb, -2);
 	VSB_printf(vsb, "},\n");
 }
+
+/*--------------------------------------------------------------------
+ * Test if backend is healthy and report when it last changed
+ */
+
+unsigned
+VDI_Healthy(const struct director *d, double *changed)
+{
+	CHECK_OBJ_NOTNULL(d, DIRECTOR_MAGIC);
+
+	if (changed != NULL)
+		*changed = d->health_changed;
+
+	if (d->admin_health == VDI_AH_PROBE)
+		return (d->health);
+
+	if (d->admin_health == VDI_AH_SICK)
+		return (0);
+
+	if (d->admin_health == VDI_AH_DELETED)
+		return (0);
+
+	if (d->admin_health == VDI_AH_HEALTHY)
+		return (1);
+
+	WRONG("Wrong admin health");
+}
+
+/*---------------------------------------------------------------------*/
+
+static int __match_proto__(vcl_be_func)
+do_list(struct cli *cli, struct director *d, void *priv)
+{
+	int *probes;
+	char time_str[VTIM_FORMAT_SIZE];
+	struct backend *be;
+
+	AN(priv);
+	probes = priv;
+	CHECK_OBJ_NOTNULL(d, DIRECTOR_MAGIC);
+	CAST_OBJ_NOTNULL(be, d->priv, BACKEND_MAGIC);
+	if (d->admin_health == VDI_AH_DELETED)
+		return (0);
+
+	VCLI_Out(cli, "\n%-30s", d->display_name);
+
+	VCLI_Out(cli, " %-10s", VDI_Ahealth(d));
+
+	if (be->probe == NULL)
+		VCLI_Out(cli, " %-20s", "Healthy (no probe)");
+	else {
+		if (d->health)
+			VCLI_Out(cli, " %-20s", "Healthy ");
+		else
+			VCLI_Out(cli, " %-20s", "Sick ");
+		VBP_Status(cli, be, *probes);
+	}
+
+	VTIM_format(d->health_changed, time_str);
+	VCLI_Out(cli, " %s", time_str);
+
+	return (0);
+}
+
+static void __match_proto__(cli_func_t)
+cli_backend_list(struct cli *cli, const char * const *av, void *priv)
+{
+	int probes = 0;
+
+	(void)priv;
+	ASSERT_CLI();
+	if (av[2] != NULL && !strcmp(av[2], "-p")) {
+		av++;
+		probes = 1;
+	} else if (av[2] != NULL && av[2][0] == '-') {
+		VCLI_Out(cli, "Invalid flags %s", av[2]);
+		VCLI_SetResult(cli, CLIS_PARAM);
+		return;
+	} else if (av[3] != NULL) {
+		VCLI_Out(cli, "Too many arguments");
+		VCLI_SetResult(cli, CLIS_PARAM);
+		return;
+	}
+	VCLI_Out(cli, "%-30s %-10s %-20s %s", "Backend name", "Admin",
+	    "Probe", "Last updated");
+	(void)VCL_IterDirector(cli, av[2], do_list, &probes);
+}
+
+/*---------------------------------------------------------------------*/
+
+static int __match_proto__(vcl_be_func)
+do_set_health(struct cli *cli, struct director *d, void *priv)
+{
+	unsigned prev;
+
+	(void)cli;
+	AN(priv);
+	CHECK_OBJ_NOTNULL(d, DIRECTOR_MAGIC);
+	if (d->admin_health == VDI_AH_DELETED)
+		return (0);
+	prev = VDI_Healthy(d, NULL);
+	d->admin_health = *(const struct vdi_ahealth **)priv;
+	(void)VDI_Ahealth(d);			// Acts like type-check
+	if (prev != VDI_Healthy(d, NULL))
+		d->health_changed = VTIM_real();
+
+	return (0);
+}
+
+static void __match_proto__()
+cli_backend_set_health(struct cli *cli, const char * const *av, void *priv)
+{
+	const struct vdi_ahealth *ah;
+	int n;
+
+	(void)av;
+	(void)priv;
+	ASSERT_CLI();
+	AN(av[2]);
+	AN(av[3]);
+	ah = vdi_str2ahealth(av[3]);
+	if (ah == NULL || ah == VDI_AH_DELETED) {
+		VCLI_Out(cli, "Invalid state %s", av[3]);
+		VCLI_SetResult(cli, CLIS_PARAM);
+		return;
+	}
+	n = VCL_IterDirector(cli, av[2], do_set_health, &ah);
+	if (n == 0) {
+		VCLI_Out(cli, "No Backends matches");
+		VCLI_SetResult(cli, CLIS_PARAM);
+	}
+}
+
+/*---------------------------------------------------------------------*/
+
+static struct cli_proto backend_cmds[] = {
+	{ CLICMD_BACKEND_LIST,		"", cli_backend_list },
+	{ CLICMD_BACKEND_SET_HEALTH,	"", cli_backend_set_health },
+	{ NULL }
+};
+
+/*---------------------------------------------------------------------*/
+
+void
+VDI_Init(void)
+{
+
+	CLI_AddFuncs(backend_cmds);
+}
diff --git a/bin/varnishd/cache/cache_director.h b/bin/varnishd/cache/cache_director.h
index c074cf1..8d1d380 100644
--- a/bin/varnishd/cache/cache_director.h
+++ b/bin/varnishd/cache/cache_director.h
@@ -106,5 +106,4 @@ void VCL_DelDirector(struct director *);
 VBE_AHEALTH_LIST
 #undef VBE_AHEALTH
 
-const struct vdi_ahealth *VDI_Str2Ahealth(const char *t);
 const char *VDI_Ahealth(const struct director *d);
diff --git a/bin/varnishd/cache/cache_main.c b/bin/varnishd/cache/cache_main.c
index 239c095..799cda3 100644
--- a/bin/varnishd/cache/cache_main.c
+++ b/bin/varnishd/cache/cache_main.c
@@ -339,6 +339,7 @@ child_main(int sigmagic, size_t altstksz)
 	VBO_Init();
 	VTP_Init();
 	VBP_Init();
+	VDI_Init();
 	VBE_InitCfg();
 	Pool_Init();
 	V1P_Init();
diff --git a/bin/varnishd/cache/cache_varnishd.h b/bin/varnishd/cache/cache_varnishd.h
index b7fb23c..21b1e63 100644
--- a/bin/varnishd/cache/cache_varnishd.h
+++ b/bin/varnishd/cache/cache_varnishd.h
@@ -71,6 +71,7 @@ void VDI_Finish(struct worker *wrk, struct busyobj *bo);
 enum sess_close VDI_Http1Pipe(struct req *, struct busyobj *);
 void VDI_Panic(const struct director *, struct vsb *, const char *nm);
 void VDI_Event(const struct director *d, enum vcl_event_e ev);
+void VDI_Init(void);
 
 /* cache_exp.c */
 double EXP_Ttl(const struct req *, const struct objcore *);
diff --git a/bin/varnishd/flint.lnt b/bin/varnishd/flint.lnt
index 7619b33..e31a0d5 100644
--- a/bin/varnishd/flint.lnt
+++ b/bin/varnishd/flint.lnt
@@ -6,6 +6,9 @@
 -printf(2, http_PrintfHeader)
 -printf(2, VSB_printf)
 
+-esym(759, VDI_AH_*)	// could be moved from header to module
+-esym(765, VDI_AH_*)	// could be made static
+
 -esym(755, vct_*)
 -esym(759, vpf_*)
 -esym(765, vpf_*)


More information about the varnish-commit mailing list