]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: sample: add the xxh3 converter
authorDragan Dosen <ddosen@haproxy.com>
Tue, 22 Dec 2020 20:44:33 +0000 (21:44 +0100)
committerWilly Tarreau <w@1wt.eu>
Wed, 23 Dec 2020 05:39:21 +0000 (06:39 +0100)
This patch adds support for the XXH3 variant of hash function that
generates a 64-bit hash.

doc/configuration.txt
src/sample.c

index 95b020c03f94afb2231c58fb04082f48b1f080f4..ca4c581db43f89cf951227292d3d4bca0ac96ac4 100644 (file)
@@ -16356,6 +16356,15 @@ xor(<value>)
   This prefix is followed by a name. The separator is a '.'. The name may only
   contain characters 'a-z', 'A-Z', '0-9', '.' and '_'.
 
+xxh3([<seed>])
+  Hashes a binary input sample into a signed 64-bit quantity using the XXH3
+  64-bit variant of the XXhash hash function. This hash supports a seed which
+  defaults to zero but a different value maybe passed as the <seed> argument.
+  This hash is known to be very good and very fast so it can be used to hash
+  URLs and/or URL parameters for use as stick-table keys to collect statistics
+  with a low collision rate, though care must be taken as the algorithm is not
+  considered as cryptographically secure.
+
 xxh32([<seed>])
   Hashes a binary input sample into an unsigned 32-bit quantity using the 32-bit
   variant of the XXHash hash function. This hash supports a seed which defaults
index d1941ce839c2c4a0a7d668c3e79041d1f249d477..f70797dae2e9cb02a0867678f34dd42b4f4fb9d8 100644 (file)
@@ -2099,6 +2099,20 @@ static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *
        return 1;
 }
 
+static int sample_conv_xxh3(const struct arg *arg_p, struct sample *smp, void *private)
+{
+       unsigned long long int seed;
+
+       if (arg_p && arg_p->data.sint)
+               seed = (unsigned long long int)arg_p->data.sint;
+       else
+               seed = 0;
+       smp->data.u.sint = (long long int)XXH3(smp->data.u.str.area,
+                                              smp->data.u.str.data, seed);
+       smp->data.type = SMP_T_SINT;
+       return 1;
+}
+
 /* hashes the binary input into a 32-bit unsigned int */
 static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
 {
@@ -3959,6 +3973,7 @@ static struct sample_conv_kw_list sample_conv_kws = {ILH, {
        { "djb2",   sample_conv_djb2,      ARG1(0,SINT), NULL, SMP_T_BIN,  SMP_T_SINT  },
        { "sdbm",   sample_conv_sdbm,      ARG1(0,SINT), NULL, SMP_T_BIN,  SMP_T_SINT  },
        { "wt6",    sample_conv_wt6,       ARG1(0,SINT), NULL, SMP_T_BIN,  SMP_T_SINT  },
+       { "xxh3",   sample_conv_xxh3,      ARG1(0,SINT), NULL, SMP_T_BIN,  SMP_T_SINT  },
        { "xxh32",  sample_conv_xxh32,     ARG1(0,SINT), NULL, SMP_T_BIN,  SMP_T_SINT  },
        { "xxh64",  sample_conv_xxh64,     ARG1(0,SINT), NULL, SMP_T_BIN,  SMP_T_SINT  },
        { "json",   sample_conv_json,      ARG1(1,STR),  sample_conv_json_check, SMP_T_STR,  SMP_T_STR },