summaryrefslogtreecommitdiff
path: root/lib/net.h
diff options
context:
space:
mode:
authorOndrej Zajicek <santiago@crfreenet.org>2023-05-18 15:55:45 +0200
committerOndrej Zajicek <santiago@crfreenet.org>2023-05-18 16:02:02 +0200
commitb0e97617d98ed02235de37b7e498d81f01330b50 (patch)
tree814b96ac8661ab08feb2888228024c47126dd551 /lib/net.h
parent3cf91fb9eb5e6aa51e63edcd237ee266373aec79 (diff)
Lib: Improve IP/net hashing
Backport some changes from branch oz-parametric-hashes. Replace naive hash function for IPv6 addresses, fix hashing of VPNx (where upper half of RD was ignored), fix hashing of MPLS labels (where identity was used).
Diffstat (limited to 'lib/net.h')
-rw-r--r--lib/net.h34
1 files changed, 21 insertions, 13 deletions
diff --git a/lib/net.h b/lib/net.h
index da7254c2..e9828557 100644
--- a/lib/net.h
+++ b/lib/net.h
@@ -479,39 +479,47 @@ static inline void net_copy_mpls(net_addr_mpls *dst, const net_addr_mpls *src)
{ memcpy(dst, src, sizeof(net_addr_mpls)); }
-/* XXXX */
-static inline u32 u64_hash(u64 a)
-{ return u32_hash(a); }
+static inline u32 px4_hash(ip4_addr prefix, u32 pxlen)
+{ return ip4_hash(prefix) ^ (pxlen << 26); }
+
+static inline u32 px6_hash(ip6_addr prefix, u32 pxlen)
+{ return ip6_hash(prefix) ^ (pxlen << 26); }
static inline u32 net_hash_ip4(const net_addr_ip4 *n)
-{ return ip4_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
+{ return px4_hash(n->prefix, n->pxlen); }
static inline u32 net_hash_ip6(const net_addr_ip6 *n)
-{ return ip6_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
+{ return px6_hash(n->prefix, n->pxlen); }
static inline u32 net_hash_vpn4(const net_addr_vpn4 *n)
-{ return ip4_hash(n->prefix) ^ ((u32) n->pxlen << 26) ^ u64_hash(n->rd); }
+{
+ u64 acc = ip4_hash0(n->prefix, HASH_PARAM, 0) ^ (n->pxlen << 26);
+ return hash_value(u64_hash0(n->rd, HASH_PARAM, acc));
+}
static inline u32 net_hash_vpn6(const net_addr_vpn6 *n)
-{ return ip6_hash(n->prefix) ^ ((u32) n->pxlen << 26) ^ u64_hash(n->rd); }
+{
+ u64 acc = ip6_hash0(n->prefix, HASH_PARAM, 0) ^ (n->pxlen << 26);
+ return hash_value(u64_hash0(n->rd, HASH_PARAM, acc));
+}
static inline u32 net_hash_roa4(const net_addr_roa4 *n)
-{ return ip4_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
+{ return px4_hash(n->prefix, n->pxlen); }
static inline u32 net_hash_roa6(const net_addr_roa6 *n)
-{ return ip6_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
+{ return px6_hash(n->prefix, n->pxlen); }
static inline u32 net_hash_flow4(const net_addr_flow4 *n)
-{ return ip4_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
+{ return px4_hash(n->prefix, n->pxlen); }
static inline u32 net_hash_flow6(const net_addr_flow6 *n)
-{ return ip6_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
+{ return px6_hash(n->prefix, n->pxlen); }
static inline u32 net_hash_ip6_sadr(const net_addr_ip6_sadr *n)
-{ return net_hash_ip6((net_addr_ip6 *) n); }
+{ return px6_hash(n->dst_prefix, n->dst_pxlen); }
static inline u32 net_hash_mpls(const net_addr_mpls *n)
-{ return n->label; }
+{ return u32_hash(n->label); }
u32 net_hash(const net_addr *a);