[master] 94abb7725 Avoid STRING_LIST and simplify sha256 hashing.

Poul-Henning Kamp phk at FreeBSD.org
Thu Jun 20 09:28:08 UTC 2019


commit 94abb772548196f47c7c256fe222124691fcb957
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Thu Jun 20 09:08:00 2019 +0000

    Avoid STRING_LIST and simplify sha256 hashing.

diff --git a/lib/libvmod_directors/shard_cfg.c b/lib/libvmod_directors/shard_cfg.c
index b72bad27f..532b0d9e8 100644
--- a/lib/libvmod_directors/shard_cfg.c
+++ b/lib/libvmod_directors/shard_cfg.c
@@ -236,6 +236,8 @@ shardcfg_hashcircle(struct sharddir *shardd, VCL_INT replicas)
 	const char *ident;
 	const int len = 12; // log10(UINT32_MAX) + 2;
 	char s[len];
+	struct strands ss[1];
+	const char *ssp[2];
 
 	CHECK_OBJ_NOTNULL(shardd, SHARDDIR_MAGIC);
 	AZ(shardd->hashcircle);
@@ -261,8 +263,12 @@ shardcfg_hashcircle(struct sharddir *shardd, VCL_INT replicas)
 
 		for (j = 0; j < replicas; j++) {
 			assert(snprintf(s, len, "%d", j) < len);
+			ss->n = 2;
+			ssp[0] = ident;
+			ssp[1] = s;
+			ss->p = ssp;
 			shardd->hashcircle[i * replicas + j].point =
-				sharddir_sha256(ident, s, vrt_magic_string_end);
+				sharddir_sha256(ss);
 			shardd->hashcircle[i * replicas + j].host = i;
 		}
 		/* not used in current interface */
diff --git a/lib/libvmod_directors/shard_dir.c b/lib/libvmod_directors/shard_dir.c
index e295be1e9..4c707dc5e 100644
--- a/lib/libvmod_directors/shard_dir.c
+++ b/lib/libvmod_directors/shard_dir.c
@@ -89,44 +89,22 @@ sharddir_err(VRT_CTX, enum VSL_tag_e tag,  const char *fmt, ...)
 }
 
 uint32_t
-sharddir_sha256v(const char *s, va_list ap)
+sharddir_sha256(VCL_STRANDS s)
 {
 	struct VSHA256Context sha256;
-	union {
-		unsigned char digest[32];
-		uint32_t uint32_digest[8];
-	} sha256_digest;
-	uint32_t r;
-	const char *p;
+	unsigned char digest[VSHA256_LEN];
+	int i;
 
+	AN(s);
 	VSHA256_Init(&sha256);
-	p = s;
-	while (p != vrt_magic_string_end) {
-		if (p != NULL && *p != '\0')
-			VSHA256_Update(&sha256, p, strlen(p));
-		p = va_arg(ap, const char *);
+	for (i = 0; i < s->n; i++) {
+		if (s->p[i] != NULL)
+			VSHA256_Update(&sha256, s->p[i], strlen(s->p[i]));
 	}
-	VSHA256_Final(sha256_digest.digest, &sha256);
-
-	/*
-	 * use low 32 bits only
-	 * XXX: Are these the best bits to pick?
-	 */
-	vle32enc(&r, sha256_digest.uint32_digest[7]);
-	return (r);
-}
-
-uint32_t
-sharddir_sha256(const char *s, ...)
-{
-	va_list ap;
-	uint32_t r;
-
-	va_start(ap, s);
-	r = sharddir_sha256v(s, ap);
-	va_end(ap);
+	VSHA256_Final(digest, &sha256);
 
-	return (r);
+	/* The low 32 bits are as good as any. */
+	return (vle32dec(&digest[VSHA256_LEN - 4]));
 }
 
 static int
diff --git a/lib/libvmod_directors/shard_dir.h b/lib/libvmod_directors/shard_dir.h
index bf5bb1ed4..021fa1257 100644
--- a/lib/libvmod_directors/shard_dir.h
+++ b/lib/libvmod_directors/shard_dir.h
@@ -117,8 +117,7 @@ sharddir_backend(const struct sharddir *shardd, int id)
 
 void sharddir_debug(struct sharddir *shardd, const uint32_t flags);
 void sharddir_err(VRT_CTX, enum VSL_tag_e tag,  const char *fmt, ...);
-uint32_t sharddir_sha256v(const char *s, va_list ap);
-uint32_t sharddir_sha256(const char *s, ...);
+uint32_t sharddir_sha256(VCL_STRANDS s);
 void sharddir_new(struct sharddir **sharddp, const char *vcl_name,
     const struct vmod_directors_shard_param *param);
 void sharddir_set_param(struct sharddir *shardd,
diff --git a/lib/libvmod_directors/vmod.vcc b/lib/libvmod_directors/vmod.vcc
index 7a81ee422..5bb6837f9 100644
--- a/lib/libvmod_directors/vmod.vcc
+++ b/lib/libvmod_directors/vmod.vcc
@@ -415,7 +415,7 @@ Reconfigure the consistent hashing ring to reflect backend changes.
 This method must be called at least once before the director can be
 used.
 
-$Method INT .key(STRING_LIST)
+$Method INT .key(STRANDS)
 
 Convenience method to generate a sharding key for use with the *key*
 argument to the `vmod_directors.shard.backend`_ method by hashing the
diff --git a/lib/libvmod_directors/vmod_shard.c b/lib/libvmod_directors/vmod_shard.c
index 73611b95c..9de023190 100644
--- a/lib/libvmod_directors/vmod_shard.c
+++ b/lib/libvmod_directors/vmod_shard.c
@@ -281,19 +281,13 @@ vmod_shard__fini(struct vmod_directors_shard **vshardp)
 }
 
 VCL_INT v_matchproto_(td_directors_shard_key)
-vmod_shard_key(VRT_CTX, struct vmod_directors_shard *vshard, const char *s, ...)
+vmod_shard_key(VRT_CTX, struct vmod_directors_shard *vshard, VCL_STRANDS s)
 {
-	va_list ap;
-	uint32_t r;
 
 	(void)ctx;
 	(void)vshard;
 
-	va_start(ap, s);
-	r = sharddir_sha256v(s, ap);
-	va_end(ap);
-
-	return ((VCL_INT)r);
+	return ((VCL_INT)sharddir_sha256(s));
 }
 
 VCL_VOID v_matchproto_(td_directors_set_warmup)
@@ -397,6 +391,8 @@ static inline uint32_t
 shard_get_key(VRT_CTX, const struct vmod_directors_shard_param *p)
 {
 	struct http *http;
+	struct strands s[1];
+	const char *sp[1];
 
 	switch (p->by) {
 	case BY_HASH:
@@ -412,8 +408,10 @@ shard_get_key(VRT_CTX, const struct vmod_directors_shard_param *p)
 			AN(ctx->http_bereq);
 			AN(http = ctx->http_bereq);
 		}
-		return (sharddir_sha256(http->hd[HTTP_HDR_URL].b,
-					vrt_magic_string_end));
+		sp[0] = http->hd[HTTP_HDR_URL].b;
+		s->n = 1;
+		s->p = sp;
+		return (sharddir_sha256(s));
 	case BY_KEY:
 	case BY_BLOB:
 		return (p->key);


More information about the varnish-commit mailing list