Skip to contentSkip to conversation

net: lan966x: extract IFH fields with word loads

PATCH Applied Daniel Machon 2 participants 2 messages Nov 17 – Nov 19

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: lan966x: extract IFH fields with word loads

Every frame the CPU receives goes through lan966x_ifh_get() for its source port and timestamp. lan966x_ifh_get() builds each field one bit at a time, with a 64 bit shift for each bit.

On a lan9668, this bit loop takes about an eighth of the CPU time spent on receiving a small frame, which limits the RX frame rate.

To improve the RX performance, read the fields with word loads instead. The IFH is a big endian bit field, so a field spans at most two 32 bit words. Load the word, or the two words, the field lives in, then shift and mask. On a lan9668, this saves about 1 us of CPU time per received frame.

---
 .../net/ethernet/microchip/lan966x/lan966x_main.c  | 22 ++++++++++------------
 1 file changed, 10 insertions(+), 12 deletions(-)
drivers/net/ethernet/microchip/lan966x/lan966x_main.c +10 -12
index b8696fc7e9b93..6aa53e5208a02 100644
@@ -11,6 +11,7 @@
 #include <linux/phy/phy.h>
 #include <linux/platform_device.h>
 #include <linux/reset.h>
+#include <linux/unaligned.h>
 #include <net/addrconf.h>
 
 #include "lan966x_main.h"
@@ -633,21 +634,18 @@ static int lan966x_rx_frame_word(struct lan966x *lan966x, u8 grp, u32 *rval)
 
 static u64 lan966x_ifh_get(u8 *ifh, size_t pos, size_t length)
 {
-	u64 val = 0;
-	u8 v;
+	size_t w = pos / 32;
+	u64 val;
 
-	for (int i = 0; i < length ; i++) {
-		int j = pos + i;
-		int k = j % 8;
+	/* Load the 32 bit word that holds bit pos */
+	val = get_unaligned_be32(ifh + IFH_LEN_BYTES - 4 * (w + 1));
 
-		if (i == 0 || k == 0)
-			v = ifh[IFH_LEN_BYTES - (j / 8) - 1];
+	/* If the field continues into the next word, load that word too */
+	if (pos % 32 + length > 32)
+		val |= (u64)get_unaligned_be32(ifh + IFH_LEN_BYTES - 4 * (w + 2)) << 32;
 
-		if (v & (1 << k))
-			val |= (1ULL << i);
-	}
-
-	return val;
+	/* Shift the field down to bit 0 and mask off the bits above it */
+	return (val >> (pos % 32)) & GENMASK_ULL(length - 1, 0);
 }
 
 void lan966x_ifh_get_src_port(void *ifh, u64 *src_port)

---
base-commit: 994089a3fa3882f27f4bed9fc5815ee7df502e88
change-id: 20261116-lan966x-ifh-word-loads-3e9734db3d4c

Best regards,

Signature · 1 line
Daniel Machon <[email protected]>
#1 lore raw
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 8 lines · On Tue, 17 Nov 2026 13:49:34 +0100 you wrote:

Every frame the CPU receives goes through lan966x_ifh_get() for its source port and timestamp. lan966x_ifh_get() builds each field one bit at a time, with a 64 bit shift for each bit.

On a lan9668, this bit loop takes about an eighth of the CPU time spent on receiving a small frame, which limits the RX frame rate.

[...]

Here is the summary with links:
- [net-next] net: lan966x: extract IFH fields with word loads

    https://git.kernel.org/netdev/net-next/c/6d53e01dd9bb

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html