Skip to contentSkip to conversation

pppoe: optimize hash with word access

PATCH Superseded Qingfang Deng 2 participants 3 messages Apr 13 – Apr 14

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pppoe: optimize hash with word access

Currently, hash_item() processes the 6-byte Ethernet address and the 2-byte session ID byte-wise to compute a hash.

Optimize this by using 16-bit word operations: XOR three 16-bit words from the Ethernet address and the 16-bit session ID, then fold the result. This reduces the total number of loads and XORs. The Ethernet addresses in a skb and struct pppoe_addr are both 2-byte aligned, so the u16 pointer cast is safe.

---
 drivers/net/ppp/pppoe.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/net/ppp/pppoe.c +6 -6
index d546a7af0d54..e2e70628958b 100644
@@ -136,15 +136,15 @@ static inline int cmp_addr(struct pppoe_addr *a, __be16 sid, char *addr)
 #error 8 must be a multiple of PPPOE_HASH_BITS
 #endif
 
-static int hash_item(__be16 sid, unsigned char *addr)
+static u8 hash_item(__be16 sid, const u8 addr[ETH_ALEN])
 {
-	unsigned char hash = 0;
+	const u16 *addr16 = (const u16 *)addr;
 	unsigned int i;
+	u16 hash16;
+	u8 hash;
 
-	for (i = 0; i < ETH_ALEN; i++)
-		hash ^= addr[i];
-	for (i = 0; i < sizeof(sid_t) * 8; i += 8)
-		hash ^= (__force __u32)sid >> i;
+	hash16 = addr16[0] ^ addr16[1] ^ addr16[2] ^ (__force u16)sid;
+	hash = (hash16 >> 8) ^ hash16;
 	for (i = 8; (i >>= 1) >= PPPOE_HASH_BITS;)
 		hash ^= hash >> i;
 
Signature · 1 line
2.43.0
#1 lore raw
Quoted 11 lines · Qingfang Deng

Currently, hash_item() processes the 6-byte Ethernet address and the 2-byte session ID byte-wise to compute a hash.

Optimize this by using 16-bit word operations: XOR three 16-bit words from the Ethernet address and the 16-bit session ID, then fold the result. This reduces the total number of loads and XORs. The Ethernet addresses in a skb and struct pppoe_addr are both 2-byte aligned, so the u16 pointer cast is safe.

net-next is closed.

https://lore.kernel.org/netdev/[email protected]/

Also I would suggest using hash32(hash, PPPOE_HASH_BITS)