]> git.ipfire.org Git - thirdparty/openvpn.git/commitdiff
Change hash values in our hash map from uint32_t to uint64_t
authorArne Schwabe <arne@rfc2549.org>
Sun, 28 Jun 2026 21:00:27 +0000 (23:00 +0200)
committerGert Doering <gert@greenie.muc.de>
Mon, 29 Jun 2026 06:52:14 +0000 (08:52 +0200)
32 bit architectures are playing a very small role today, so the difference
between 64 bit and 32 bit hash values is not expected to be large anymore.

This is also in preparation to replace our hash function with a more modern
replacement.

Change-Id: Ib8140107f98164d2a2e1768a6d8ac65016cd7f7c
Signed-off-by: Arne Schwabe <arne@rfc2549.org>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1570
Message-Id: <20260628210033.4583-1-gert@greenie.muc.de>
URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg37349.html
Signed-off-by: Gert Doering <gert@greenie.muc.de>
src/openvpn/list.c
src/openvpn/list.h
src/openvpn/mroute.c
src/openvpn/mroute.h
src/openvpn/mtcp.c
src/openvpn/mudp.c
src/openvpn/multi.c
tests/unit_tests/openvpn/test_misc.c

index f0e9908b5cd1a4f4dfff5ffc5acda8ed3fe97643..c07e764f2b2c1eaee76c940fa1a112f628a45a42 100644 (file)
@@ -35,7 +35,7 @@
 
 struct hash *
 hash_init(const uint32_t n_buckets, const uint32_t iv,
-          uint32_t (*hash_function)(const void *key, uint32_t iv),
+          uint64_t (*hash_function)(const void *key, uint32_t iv),
           bool (*compare_function)(const void *key1, const void *key2))
 {
     struct hash *h;
@@ -76,7 +76,7 @@ hash_free(struct hash *hash)
 }
 
 struct hash_element *
-hash_lookup_fast(struct hash *hash, struct hash_bucket *bucket, const void *key, uint32_t hv)
+hash_lookup_fast(struct hash *hash, struct hash_bucket *bucket, const void *key, uint64_t hv)
 {
     struct hash_element *he;
     struct hash_element *prev = NULL;
@@ -104,7 +104,7 @@ hash_lookup_fast(struct hash *hash, struct hash_bucket *bucket, const void *key,
 }
 
 bool
-hash_remove_fast(struct hash *hash, struct hash_bucket *bucket, const void *key, uint32_t hv)
+hash_remove_fast(struct hash *hash, struct hash_bucket *bucket, const void *key, uint64_t hv)
 {
     struct hash_element *he;
     struct hash_element *prev = NULL;
@@ -136,7 +136,7 @@ hash_remove_fast(struct hash *hash, struct hash_bucket *bucket, const void *key,
 bool
 hash_add(struct hash *hash, const void *key, void *value, bool replace)
 {
-    uint32_t hv;
+    uint64_t hv;
     struct hash_bucket *bucket;
     struct hash_element *he;
     bool ret = false;
@@ -412,7 +412,7 @@ hash_iterator_delete_element(struct hash_iterator *hi)
         c ^= (b >> 15); \
     }
 
-uint32_t
+uint64_t
 hash_func(const uint8_t *k, uint32_t length, uint32_t initval)
 {
     uint32_t a, b, c, len;
index 04cc3abe2f3ae5ded5950368109ffe97d5f3847e..06377c6ce2307fccb24381ca1049fb0347c3988a 100644 (file)
@@ -40,7 +40,7 @@ struct hash_element
 {
     void *value;
     const void *key;
-    uint32_t hash_value;
+    uint64_t hash_value;
     struct hash_element *next;
 };
 
@@ -55,13 +55,13 @@ struct hash
     uint32_t n_elements;
     uint32_t mask;
     uint32_t iv;
-    uint32_t (*hash_function)(const void *key, uint32_t iv);
+    uint64_t (*hash_function)(const void *key, uint32_t iv);
     bool (*compare_function)(const void *key1, const void *key2); /* return true if equal */
     struct hash_bucket *buckets;
 };
 
 struct hash *hash_init(const uint32_t n_buckets, const uint32_t iv,
-                       uint32_t (*hash_function)(const void *key, uint32_t iv),
+                       uint64_t (*hash_function)(const void *key, uint32_t iv),
                        bool (*compare_function)(const void *key1, const void *key2));
 
 void hash_free(struct hash *hash);
@@ -69,9 +69,9 @@ void hash_free(struct hash *hash);
 bool hash_add(struct hash *hash, const void *key, void *value, bool replace);
 
 struct hash_element *hash_lookup_fast(struct hash *hash, struct hash_bucket *bucket,
-                                      const void *key, uint32_t hv);
+                                      const void *key, uint64_t hv);
 
-bool hash_remove_fast(struct hash *hash, struct hash_bucket *bucket, const void *key, uint32_t hv);
+bool hash_remove_fast(struct hash *hash, struct hash_bucket *bucket, const void *key, uint64_t hv);
 
 void hash_remove_by_value(struct hash *hash, void *value);
 
@@ -98,9 +98,9 @@ void hash_iterator_delete_element(struct hash_iterator *hi);
 
 void hash_iterator_free(struct hash_iterator *hi);
 
-uint32_t hash_func(const uint8_t *k, uint32_t length, uint32_t initval);
+uint64_t hash_func(const uint8_t *k, uint32_t length, uint32_t initval);
 
-static inline uint32_t
+static inline uint64_t
 hash_value(const struct hash *hash, const void *key)
 {
     return (*hash->hash_function)(key, hash->iv);
@@ -119,7 +119,7 @@ hash_n_buckets(const struct hash *hash)
 }
 
 static inline struct hash_bucket *
-hash_bucket(struct hash *hash, uint32_t hv)
+hash_bucket(struct hash *hash, uint64_t hv)
 {
     return &hash->buckets[hv & hash->mask];
 }
@@ -129,7 +129,7 @@ hash_lookup(struct hash *hash, const void *key)
 {
     void *ret = NULL;
     struct hash_element *he;
-    uint32_t hv = hash_value(hash, key);
+    uint64_t hv = hash_value(hash, key);
     struct hash_bucket *bucket = &hash->buckets[hv & hash->mask];
 
     he = hash_lookup_fast(hash, bucket, key, hv);
@@ -143,7 +143,7 @@ hash_lookup(struct hash *hash, const void *key)
 
 /* NOTE: assumes that key is not a duplicate */
 static inline void
-hash_add_fast(struct hash *hash, struct hash_bucket *bucket, const void *key, uint32_t hv,
+hash_add_fast(struct hash *hash, struct hash_bucket *bucket, const void *key, uint64_t hv,
               void *value)
 {
     struct hash_element *he;
@@ -160,7 +160,7 @@ hash_add_fast(struct hash *hash, struct hash_bucket *bucket, const void *key, ui
 static inline bool
 hash_remove(struct hash *hash, const void *key)
 {
-    uint32_t hv;
+    uint64_t hv;
     struct hash_bucket *bucket;
     bool ret;
 
index 6fa70a39c3367e6f68ccf44eb4790e50f828c1f7..78c689e3d4f39a6a2d5a22ae4539c9828326e7a7 100644 (file)
@@ -354,7 +354,7 @@ mroute_addr_mask_host_bits(struct mroute_addr *ma)
  * address type, number of bits in the network address,
  * and the actual address.
  */
-uint32_t
+uint64_t
 mroute_addr_hash_function(const void *key, uint32_t iv)
 {
     return hash_func(mroute_addr_hash_ptr((const struct mroute_addr *)key),
index c8daa1551cd8926b82021c6b71aec55c449ac11c..2f5d01931bdf49e9b1ac5154f51fd700ca977db8 100644 (file)
@@ -144,7 +144,7 @@ bool mroute_extract_openvpn_sockaddr(struct mroute_addr *addr,
 
 bool mroute_learnable_address(const struct mroute_addr *addr, struct gc_arena *gc);
 
-uint32_t mroute_addr_hash_function(const void *key, uint32_t iv);
+uint64_t mroute_addr_hash_function(const void *key, uint32_t iv);
 
 bool mroute_addr_compare_function(const void *key1, const void *key2);
 
index 7651b4d14100bde28a180b63f780c4a23fdbd049..f000283d089ad6e12e80231d7636ec942170303f 100644 (file)
@@ -49,7 +49,7 @@ multi_create_instance_tcp(struct multi_context *m, struct link_socket *sock)
     {
         mi->real.proto = sock->info.proto;
         struct hash_element *he;
-        const uint32_t hv = hash_value(hash, &mi->real);
+        const uint64_t hv = hash_value(hash, &mi->real);
         struct hash_bucket *bucket = hash_bucket(hash, hv);
 
         multi_assign_peer_id(m, mi);
index f919b67553ec5d22b60d21704f41c3c1543894e2..9acf2974d0b7c6b8c7ac470797ac630cd66b7604 100644 (file)
@@ -281,7 +281,7 @@ multi_get_create_instance_udp(struct multi_context *m, bool *floated, struct lin
     if (mroute_extract_openvpn_sockaddr(&real, &m->top.c2.from.dest, true) && m->top.c2.buf.len > 0)
     {
         struct hash_element *he;
-        const uint32_t hv = hash_value(hash, &real);
+        const uint64_t hv = hash_value(hash, &real);
         struct hash_bucket *bucket = hash_bucket(hash, hv);
         uint8_t *ptr = BPTR(&m->top.c2.buf);
         uint8_t op = ptr[0] >> P_OPCODE_SHIFT;
index 8b36cf7f5449fe8a0b3cca0803c7cf0de0b99bea..c4293a7f1bc058ebdb5b35164cf57ee3a5513f95 100644 (file)
@@ -228,11 +228,11 @@ reap_buckets_per_pass(uint32_t n_buckets)
 
 #ifdef ENABLE_MANAGEMENT
 
-static uint32_t
+static uint64_t
 cid_hash_function(const void *key, uint32_t iv)
 {
     const unsigned long *k = (const unsigned long *)key;
-    return (uint32_t)*k;
+    return (uint64_t)*k;
 }
 
 static bool
@@ -246,19 +246,19 @@ cid_compare_function(const void *key1, const void *key2)
 #endif
 
 #ifdef ENABLE_ASYNC_PUSH
-static uint32_t
+static uint64_t
 /*
  * inotify watcher descriptors are used as hash value
  */
 int_hash_function(const void *key, uint32_t iv)
 {
-    return (uint32_t)(uintptr_t)key;
+    return (uintptr_t)key;
 }
 
 static bool
 int_compare_function(const void *key1, const void *key2)
 {
-    return (unsigned long)key1 == (unsigned long)key2;
+    return (uintptr_t)key1 == (uintptr_t)key2;
 }
 #endif
 
@@ -1012,7 +1012,7 @@ multi_learn_addr(struct multi_context *m, struct multi_instance *mi, const struc
                  const unsigned int flags)
 {
     struct hash_element *he;
-    const uint32_t hv = hash_value(m->vhash, addr);
+    const uint64_t hv = hash_value(m->vhash, addr);
     struct hash_bucket *bucket = hash_bucket(m->vhash, hv);
     struct multi_route *oldroute = NULL;
     struct multi_instance *owner = NULL;
@@ -3094,7 +3094,7 @@ multi_process_float(struct multi_context *m, struct multi_instance *mi, struct l
         goto done;
     }
 
-    const uint32_t hv = hash_value(hash, &real);
+    const uint64_t hv = hash_value(hash, &real);
     struct hash_bucket *bucket = hash_bucket(hash, hv);
 
     /* make sure that we don't float to an address taken by another client */
@@ -4230,7 +4230,7 @@ static void
 multi_unlearn_addr(struct multi_context *m, struct multi_instance *mi, const struct mroute_addr *addr)
 {
     struct hash_element *he;
-    const uint32_t hv = hash_value(m->vhash, addr);
+    const uint64_t hv = hash_value(m->vhash, addr);
     struct hash_bucket *bucket = hash_bucket(m->vhash, hv);
     struct multi_route *r = NULL;
 
index a9ae33f15a5e775ce9f3d48cb65f6d5547dd5202..ccdfdeed4500b4ed230f79b243cad758b251e9ca 100644 (file)
@@ -124,7 +124,7 @@ struct word
 };
 
 
-static uint32_t
+static uint64_t
 word_hash_function(const void *key, uint32_t iv)
 {
     const char *str = (const char *)key;