aboutgitcodebugslistschat
path: root/inany.h
diff options
context:
space:
mode:
authorDavid Gibson <david@gibson.dropbear.id.au>2023-09-28 11:21:02 +1000
committerStefano Brivio <sbrivio@redhat.com>2023-09-30 12:40:53 +0200
commitfc8f0f8c48ef12edbf60f74f37024917f5812385 (patch)
tree470cc57b733611a8447b725635fa5d9e18b7315d /inany.h
parent04b10a8d907d8ceb09e3adb4885c02f546841bbc (diff)
downloadpasst-fc8f0f8c48ef12edbf60f74f37024917f5812385.tar
passt-fc8f0f8c48ef12edbf60f74f37024917f5812385.tar.gz
passt-fc8f0f8c48ef12edbf60f74f37024917f5812385.tar.bz2
passt-fc8f0f8c48ef12edbf60f74f37024917f5812385.tar.lz
passt-fc8f0f8c48ef12edbf60f74f37024917f5812385.tar.xz
passt-fc8f0f8c48ef12edbf60f74f37024917f5812385.tar.zst
passt-fc8f0f8c48ef12edbf60f74f37024917f5812385.zip
siphash: Use incremental rather than all-at-once siphash functions
We have a bunch of variants of the siphash functions for different data sizes. The callers, in tcp.c, need to pack the various values they want to hash into a temporary structure, then call the appropriate version. We can avoid the copy into the temporary by directly using the incremental siphash functions. The length specific hash functions also have an undocumented constraint that the data pointer they take must, in fact, be aligned to avoid unaligned accesses, which may cause crashes on some architectures. So, prefer the incremental approach and remove the length-specific functions. Signed-off-by: David Gibson <david@gibson.dropbear.id.au> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Diffstat (limited to 'inany.h')
-rw-r--r--inany.h17
1 files changed, 16 insertions, 1 deletions
diff --git a/inany.h b/inany.h
index aadb20b..85a18e3 100644
--- a/inany.h
+++ b/inany.h
@@ -14,8 +14,9 @@
* @v4mapped.zero: All zero-bits for an IPv4 address
* @v4mapped.one: All one-bits for an IPv4 address
* @v4mapped.a4: If @a6 is an IPv4 mapped address, the IPv4 address
+ * @u64: As an array of u64s (solely for hashing)
*
- * @v4mapped shouldn't be accessed except via helpers.
+ * @v4mapped and @u64 shouldn't be accessed except via helpers.
*/
union inany_addr {
struct in6_addr a6;
@@ -24,7 +25,10 @@ union inany_addr {
uint8_t one[2];
struct in_addr a4;
} v4mapped;
+ uint32_t u32[4];
};
+static_assert(sizeof(union inany_addr) == sizeof(struct in6_addr));
+static_assert(_Alignof(union inany_addr) == _Alignof(uint32_t));
/** inany_v4 - Extract IPv4 address, if present, from IPv[46] address
* @addr: IPv4 or IPv6 address
@@ -94,4 +98,15 @@ static inline void inany_from_sockaddr(union inany_addr *aa, in_port_t *port,
}
}
+/** inany_siphash_feed- Fold IPv[46] address into an in-progress siphash
+ * @state: siphash state
+ * @aa: inany to hash
+ */
+static inline void inany_siphash_feed(struct siphash_state *state,
+ const union inany_addr *aa)
+{
+ siphash_feed(state, (uint64_t)aa->u32[0] << 32 | aa->u32[1]);
+ siphash_feed(state, (uint64_t)aa->u32[2] << 32 | aa->u32[3]);
+}
+
#endif /* INANY_H */