[master] d34a356cd Add VRT_Endpoint_Clone() and give backends a copy of the endpoint information to make them easier to clone.
Poul-Henning Kamp
phk at FreeBSD.org
Tue Jan 26 13:48:06 UTC 2021
commit d34a356cdda34ed671de90f77c9b84ff18f98bd2
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date: Tue Jan 26 13:46:30 2021 +0000
Add VRT_Endpoint_Clone() and give backends a copy of the endpoint
information to make them easier to clone.
diff --git a/bin/varnishd/cache/cache_backend.c b/bin/varnishd/cache/cache_backend.c
index b93c1e9e4..da7be381f 100644
--- a/bin/varnishd/cache/cache_backend.c
+++ b/bin/varnishd/cache/cache_backend.c
@@ -438,6 +438,7 @@ vbe_free(struct backend *be)
VRT_BACKEND_HANDLE();
#undef DA
#undef DN
+ free(be->endpoint);
Lck_Delete(&be->mtx);
FREE_OBJ(be);
@@ -588,6 +589,8 @@ VRT_new_backend_clustered(VRT_CTX, struct vsmw_cluster *vc,
return (NULL);
Lck_New(&be->mtx, lck_backend);
+ be->endpoint = VRT_Endpoint_Clone(vrt->endpoint);
+ vep = be->endpoint;
#define DA(x) do { if (vrt->x != NULL) REPLACE((be->x), (vrt->x)); } while (0)
#define DN(x) do { be->x = vrt->x; } while (0)
VRT_BACKEND_HANDLE();
diff --git a/bin/varnishd/cache/cache_backend.h b/bin/varnishd/cache/cache_backend.h
index 393b5df09..b2c211d9e 100644
--- a/bin/varnishd/cache/cache_backend.h
+++ b/bin/varnishd/cache/cache_backend.h
@@ -57,6 +57,8 @@ struct backend {
VTAILQ_ENTRY(backend) list;
struct lock mtx;
+ struct vrt_endpoint *endpoint;
+
VRT_BACKEND_FIELDS()
unsigned sick;
diff --git a/bin/varnishd/cache/cache_vrt.c b/bin/varnishd/cache/cache_vrt.c
index f8b9223a3..6d1cb6b77 100644
--- a/bin/varnishd/cache/cache_vrt.c
+++ b/bin/varnishd/cache/cache_vrt.c
@@ -33,6 +33,8 @@
#include "config.h"
+#include <stdlib.h>
+
#include "cache_varnishd.h"
#include "cache_objhead.h"
@@ -1061,3 +1063,47 @@ VRT_Format_Proxy(struct vsb *vsb, VCL_INT version, VCL_IP sac, VCL_IP sas,
{
VPX_Format_Proxy(vsb, (int)version, sac, sas, auth);
}
+
+/*
+ * Clone a struct vrt_endpoint in a single malloc() allocation
+ */
+
+struct vrt_endpoint *
+VRT_Endpoint_Clone(const struct vrt_endpoint *vep)
+{
+ size_t sz;
+ struct vrt_endpoint *nvep;
+ struct suckaddr *sa;
+ char *p;
+
+ CHECK_OBJ_NOTNULL(vep, VRT_ENDPOINT_MAGIC);
+ sz = sizeof *nvep;
+ if (vep->ipv4)
+ sz += vsa_suckaddr_len;
+ if (vep->ipv6)
+ sz += vsa_suckaddr_len;
+ if (vep->uds_path != NULL)
+ sz += strlen(vep->uds_path) + 1;
+ p = malloc(sz);
+ AN(p);
+ nvep = (void*)p;
+ p += sizeof *nvep;
+ INIT_OBJ(nvep, VRT_ENDPOINT_MAGIC);
+ if (vep->ipv4) {
+ sa = (void*)p; //lint !e826
+ memcpy(sa, vep->ipv4, vsa_suckaddr_len); //lint !e826
+ nvep->ipv4 = sa;
+ p += vsa_suckaddr_len;
+ }
+ if (vep->ipv6) {
+ sa = (void*)p; //lint !e826
+ memcpy(sa, vep->ipv6, vsa_suckaddr_len); //lint !e826
+ nvep->ipv6 = sa;
+ p += vsa_suckaddr_len;
+ }
+ if (vep->uds_path != NULL) {
+ strcpy(p, vep->uds_path);
+ nvep->uds_path = p;
+ }
+ return (nvep);
+}
diff --git a/include/vrt.h b/include/vrt.h
index 20782fe45..abd4fb2fb 100644
--- a/include/vrt.h
+++ b/include/vrt.h
@@ -53,8 +53,9 @@
* binary/load-time compatible, increment MAJOR version
*
* 13.0 (2021-03-15)
+ * VRT_Endpoint_Clone() added.
* Calling convention for VDP implementation changed
- * Added VRT_ValidHdr()
+ * VRT_ValidHdr() added.
* struct vmod_priv_methods added
* struct vmod_priv free member replaced with methods
* VRT_CTX_Assert() added
@@ -521,6 +522,7 @@ VCL_BACKEND VRT_new_backend_clustered(VRT_CTX,
struct vsmw_cluster *, const struct vrt_backend *);
size_t VRT_backend_vsm_need(VRT_CTX);
void VRT_delete_backend(VRT_CTX, VCL_BACKEND *);
+struct vrt_endpoint *VRT_Endpoint_Clone(const struct vrt_endpoint *vep);
/* VSM related */
struct vsmw_cluster *VRT_VSM_Cluster_New(VRT_CTX, size_t);
More information about the varnish-commit
mailing list