net: inline eth_type_trans() fast path
Subsystems (5)
Eric Dumazet
maintainerPATCH 1/1 net: inline eth_type_trans() fast path
eth_type_trans() is called once per received packet by most Ethernet drivers, and by core helpers (napi_gro_frags(), xdp_build_skb_from_*(), veth, tun, tunnels, loopback...).
With CONFIG_MITIGATION_RETHUNK / SRSO, the call and return are not free anymore.
Add eth_type_trans_inline(), handling the common case inline:
unicast frame sent to dev->dev_addr, with a real ethertype, on a device which is not a DSA conduit. All other frames (multicast, broadcast, otherhost, 802.2, runts, DSA) are handled by the out-of-line eth_type_trans().
eth_type_trans() is now a macro calling eth_type_trans_inline(), so that all existing callers get the fast path. The out-of-line version remains exported, and can be called with (eth_type_trans)(skb, dev). bpf_prog_test_run_skb() uses this to keep tools/testing/selftests/bpf fentry/fexit tests working.
The inline part is about 25 instructions on x86_64, and the whole function would be almost twice as big when fully inlined.
--- include/linux/etherdevice.h | 43 +++++++++++++++++++++++++++++++++++++ net/bpf/test_run.c | 3 ++- net/ethernet/eth.c | 8 +++++++ 3 files changed, 53 insertions(+), 1 deletion(-)
include/linux/etherdevice.h +43 -0
@@ -645,6 +645,49 @@ static inline struct ethhdr *eth_skb_pull_mac(struct sk_buff *skb)return eth; } +static inline bool eth_dev_maybe_dsa (const struct net_device *dev) +{ +#if IS_ENABLED (CONFIG_NET_DSA ) +return !!dev->dsa_ptr; +#else +return false; +#endif +} + +/** +* eth_type_trans_inline - determine the packet's protocol ID. +* @skb: received socket data +* @dev: receiving network device +* +* Inline fast path of eth_type_trans(), handling unicast frames +* sent to @dev address, with a real ethertype. +* Other frames are handled by the out-of-line eth_type_trans(). +* +* Return: the packet protocol ID, in network byte order. +*/ +static __always_inline __be16 eth_type_trans_inline(struct sk_buff *skb, +struct net_device *dev) +{ +const struct ethhdr *eth = (const struct ethhdr *)skb->data; + +if (likely (skb->len >=ETH_HLEN && +ether_addr_equal_64bits (eth->h_dest, dev->dev_addr) && +eth_proto_is_802_3 (eth->h_proto) && + !eth_dev_maybe_dsa (dev))) { + skb->dev = dev; +skb_reset_mac_header (skb); +__skb_pull (skb,ETH_HLEN ); +/* Like eth_skb_pkt_type(), leave skb->pkt_type unchanged. */ +return eth->h_proto; + } +return (eth_type_trans)(skb, dev); +} + +/* eth_type_trans() is called for every received packet by most drivers. +* Use (eth_type_trans)(skb, dev) to force a call to the out-of-line version. +*/ +#define eth_type_trans (skb, dev) eth_type_trans_inline(skb, dev) +/** * eth_skb_pad - Pad buffer to minimum number of octets for Ethernet frame * @skb: Buffer to pad
net/bpf/test_run.c +2 -1
@@ -1165,7 +1165,8 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr, goto out; } } - skb->protocol =eth_type_trans (skb, dev); +/* Keep calling the out-of-line version, for fentry/fexit selftests. */ + skb->protocol = (eth_type_trans)(skb, dev);skb_reset_network_header (skb);switch (skb->protocol) {
net/ethernet/eth.c +8 -0
@@ -143,6 +143,9 @@ u32 eth_get_headlen(const struct net_device *dev, const void *data, u32 len) }EXPORT_SYMBOL (eth_get_headlen); +/* Define the out-of-line version, not the eth_type_trans_inline() wrapper. */ +#undef eth_type_trans +/** * eth_type_trans - determine the packet's protocol ID. * @skb: received socket data
@@ -151,6 +154,11 @@ EXPORT_SYMBOL(eth_get_headlen);* The rule here is that we * assume 802.3 if the type field is short enough to be a length. * This is normal practice and works for any 'now in use' protocol. +* +* Most callers use the eth_type_trans_inline() fast path, +* which calls this function for less common frames. +* +* Return: the packet protocol ID, in network byte order. */ __be16 eth_type_trans (struct sk_buff *skb,struct net_device *dev) {
Signature · 1 line
2.53.0
From: [email protected] <[email protected]>
Date: Wed, 30 Sep 2026 19:29:57 +0200
Quoted 13 lines
eth_type_trans()is called once per received packet by most Ethernet drivers, and by core helpers (napi_gro_frags(), xdp_build_skb_from_*(), veth, tun, tunnels, loopback...).With
CONFIG_MITIGATION_RETHUNK/ SRSO, the call and return are not free anymore.Add
eth_type_trans_inline(), handling the common case inline:
unicast frame sent todev->dev_addr, with a real ethertype, on a device which is not a DSA conduit. All other frames (multicast, broadcast, otherhost, 802.2, runts, DSA) are handled by the out-of-lineeth_type_trans().
Quoted 4 lines
eth_type_trans()is now a macro callingeth_type_trans_inline(), so that all existing callers get the fast path. The out-of-line version remains exported, and can be called with (eth_type_trans)(skb, dev).bpf_prog_test_run_skb()uses this
Shouldn't it get renamed to e.g. eth_type_trans_slow() to avoid this confusion?
Quoted 3 lines
to keep
tools/testing/selftests/bpffentry/fexit tests working.The inline part is about 25 instructions on
x86_64, and the whole
Nice!
Quoted 8 lines
function would be almost twice as big when fully inlined.
Signed-off-by Eric Dumazet <[email protected]>--- include/linux/etherdevice.h | 43 +++++++++++++++++++++++++++++++++++++ net/bpf/test_run.c | 3 ++- net/ethernet/eth.c | 8 +++++++ 3 files changed, 53 insertions(+), 1 deletion(-)
Thanks,
Olek
Eric Dumazet
maintainerQuoted 17 lines · Alexander Lobakin
From: [email protected] <[email protected]>
Date: Wed, 30 Sep 2026 19:29:57 +0200Quoted 13 lines
eth_type_trans()is called once per received packet by most Ethernet drivers, and by core helpers (napi_gro_frags(), xdp_build_skb_from_*(), veth, tun, tunnels, loopback...).With
CONFIG_MITIGATION_RETHUNK/ SRSO, the call and return are not free anymore.Add
eth_type_trans_inline(), handling the common case inline:
unicast frame sent todev->dev_addr, with a real ethertype, on a device which is not a DSA conduit. All other frames (multicast, broadcast, otherhost, 802.2, runts, DSA) are handled by the out-of-lineeth_type_trans().
Quoted 7 lines
Quoted 4 lines
eth_type_trans()is now a macro callingeth_type_trans_inline(), so that all existing callers get the fast path. The out-of-line version remains exported, and can be called with (eth_type_trans)(skb, dev).bpf_prog_test_run_skb()uses thisShouldn't it get renamed to e.g.
eth_type_trans_slow()to avoid this confusion?
This was my initial idea, but this would make the patch a bit more invasive and touch bpf selftests, something like:
Let me know if I should send a V2 and CC bpf maintainers, thanks!
net/bpf/test_run.c +0 -0
100644
net/bpf/test_run.c +0 -0
@@ -1165,7 +1165,8 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog,
const union bpf_attr *kattr,
goto out;
}
}
- skb->protocol = eth_type_trans(skb, dev);
+ /* Always call the out-of-line version, for fentry/fexit selftests. */
+ skb->protocol = eth_type_trans_slow(skb, dev);
skb_reset_network_header(skb);
switch (skb->protocol) {(quoted diff) +0 -0
b/tools/testing/selftests/bpf/progs/core_kern.c
index 004f2acef2eb0b0d80184423a3e8ba399feee1f3..fca26af98a7b92898a4adfde19f63b342f29db74 100644
tools/testing/selftests/bpf/progs/core_kern.c +0 -0
@@ -47,14 +47,14 @@ int BPF_PROG(tp_xdp_devmap_xmit_multi, const
struct net_device
return randmap(from_dev->ifindex, from_dev);
}
-SEC("fentry/eth_type_trans") +SEC("fentry/eth_type_trans_slow")
int BPF_PROG(fentry_eth_type_trans, struct sk_buff *skb,
struct net_device *dev, unsigned short protocol)
{
return randmap(dev->ifindex + skb->len, dev);}
-SEC("fexit/eth_type_trans") +SEC("fexit/eth_type_trans_slow")
int BPF_PROG(fexit_eth_type_trans, struct sk_buff *skb,
struct net_device *dev, unsigned short protocol)
{
(quoted diff) +0 -0
b/tools/testing/selftests/bpf/progs/kfree_skb.c
index 7236da72ce8055f9a0165d3b4334a04d17d110b8..678c15c151944b44e276cf1280c77cb436ff7b10 100644
tools/testing/selftests/bpf/progs/kfree_skb.c +1 -1
@@ -114,7 +114,7 @@ struct {bool fexit_test_ok; } result = {}; -SEC("fentry/eth_type_trans") +SEC("fentry/eth_type_trans_slow") int BPF_PROG(fentry_eth_type_trans, struct sk_buff *skb, struct
net_device *dev,
unsigned short protocol)
{
@@ -132,7 +132,7 @@ int BPF_PROG(fentry_eth_type_trans, struct sk_buff *skb, struct net_device *dev,
return 0;
}
-SEC("fexit/eth_type_trans")
+SEC("fexit/eth_type_trans_slow")
int BPF_PROG(fexit_eth_type_trans, struct sk_buff *skb, struct net_device *dev,
unsigned short protocol)
{
Quoted 24 lines · Eric Dumazet
Quoted 22 lines · Alexander Lobakin
From: [email protected] <[email protected]>
Date: Wed, 30 Sep 2026 19:29:57 +0200Quoted 17 lines
eth_type_trans()is called once per received packet by most Ethernet drivers, and by core helpers (napi_gro_frags(), xdp_build_skb_from_*(), veth, tun, tunnels, loopback...).With
CONFIG_MITIGATION_RETHUNK/ SRSO, the call and return are not free anymore.Add
eth_type_trans_inline(), handling the common case inline:
unicast frame sent todev->dev_addr, with a real ethertype, on a device which is not a DSA conduit. All other frames (multicast, broadcast, otherhost, 802.2, runts, DSA) are handled by the out-of-lineeth_type_trans().
eth_type_trans()is now a macro callingeth_type_trans_inline(), so that all existing callers get the fast path. The out-of-line version remains exported, and can be called with (eth_type_trans)(skb, dev).bpf_prog_test_run_skb()uses this
Quoted 7 lines
Quoted 2 lines
Shouldn't it get renamed to e.g.
eth_type_trans_slow()to avoid this confusion?This was my initial idea, but this would make the patch a bit more invasive and touch bpf selftests, something like:
Let me know if I should send a V2 and CC bpf maintainers, thanks!
I prefer this _slow version as well, less magic.
From: [email protected] <[email protected]>
Date: Thu, 1 Oct 2026 19:10:46 +0200
Quoted 97 lines
Quoted 17 lines · Alexander Lobakin
From: [email protected] <[email protected]>
Date: Wed, 30 Sep 2026 19:29:57 +0200Quoted 13 lines
eth_type_trans()is called once per received packet by most Ethernet drivers, and by core helpers (napi_gro_frags(), xdp_build_skb_from_*(), veth, tun, tunnels, loopback...).With
CONFIG_MITIGATION_RETHUNK/ SRSO, the call and return are not free anymore.Add
eth_type_trans_inline(), handling the common case inline:
unicast frame sent todev->dev_addr, with a real ethertype, on a device which is not a DSA conduit. All other frames (multicast, broadcast, otherhost, 802.2, runts, DSA) are handled by the out-of-lineeth_type_trans().Quoted 7 lines
Quoted 4 lines
eth_type_trans()is now a macro callingeth_type_trans_inline(), so that all existing callers get the fast path. The out-of-line version remains exported, and can be called with (eth_type_trans)(skb, dev).bpf_prog_test_run_skb()uses thisShouldn't it get renamed to e.g.
eth_type_trans_slow()to avoid this confusion?This was my initial idea, but this would make the patch a bit more invasive and touch bpf selftests, something like:
Let me know if I should send a V2 and CC bpf maintainers, thanks!
net/bpf/test_run.c +0
-0100644
net/bpf/test_run.c +2
-1@@ -1165,7 +1165,8 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog,const union bpf_attr *kattr, goto out; } } - skb->protocol =eth_type_trans (skb, dev); +/* Always call the out-of-line version, for fentry/fexit selftests. */ + skb->protocol =eth_type_trans_slow (skb, dev);skb_reset_network_header (skb);switch (skb->protocol) {tools/testing/selftests/bpf/progs/core_kern.c +0
-0100644
tools/testing/selftests/bpf/progs/core_kern.c +0
-0@@ -47,14 +47,14 @@ int BPF_PROG(tp_xdp_devmap_xmit_multi, conststruct
net_devicereturn randmap(from_dev->ifindex, from_dev);}
tools/testing/selftests/bpf/progs/core_kern.c +2
-2-SEC("fentry/eth_type_trans") +SEC("fentry/eth_type_trans_slow")int BPF_PROG (fentry_eth_type_trans ,struct sk_buff *skb,struct net_device *dev,unsigned short protocol) {return randmap (dev->ifindex + skb->len, dev); } -SEC("fexit/eth_type_trans") +SEC("fexit/eth_type_trans_slow")int BPF_PROG (fexit_eth_type_trans ,struct sk_buff *skb,struct net_device *dev,unsigned short protocol) {tools/testing/selftests/bpf/progs/kfree_skb.c +0
-0100644
tools/testing/selftests/bpf/progs/kfree_skb.c +1
-1@@ -114,7 +114,7 @@ struct {bool fexit_test_ok; } result = {}; -SEC("fentry/eth_type_trans") +SEC("fentry/eth_type_trans_slow")int BPF_PROG (fentry_eth_type_trans ,struct sk_buff *skb,struct net_device *dev,unsigned short protocol){
tools/testing/selftests/bpf/progs/kfree_skb.c +1
-1@@ -132,7 +132,7 @@ int BPF_PROG(fentry_eth_type_trans, struct sk_buff *skb, struct net_device *dev, return 0; } -SEC("fexit/eth_type_trans") +SEC("fexit/eth_type_trans_slow")int BPF_PROG (fexit_eth_type_trans ,struct sk_buff *skb,struct net_device *dev,unsigned short protocol)
Quoted 1 line
{
This shouldn't break anything, but CCing bpf just in case.
Thanks,
Olek
Eric Dumazet
maintainerQuoted 14 lines · Eric Dumazet
eth_type_trans()is called once per received packet by most Ethernet drivers, and by core helpers (napi_gro_frags(), xdp_build_skb_from_*(), veth, tun, tunnels, loopback...).With
CONFIG_MITIGATION_RETHUNK/ SRSO, the call and return are not free anymore.Add
eth_type_trans_inline(), handling the common case inline:
unicast frame sent todev->dev_addr, with a real ethertype, on a device which is not a DSA conduit. All other frames (multicast, broadcast, otherhost, 802.2, runts, DSA) are handled by the out-of-lineeth_type_trans().
Quoted 8 lines
eth_type_trans()is now a macro callingeth_type_trans_inline(), so that all existing callers get the fast path. The out-of-line version remains exported, and can be called with (eth_type_trans)(skb, dev).bpf_prog_test_run_skb()uses this to keeptools/testing/selftests/bpffentry/fexit tests working.The inline part is about 25 instructions on
x86_64, and the whole function would be almost twice as big when fully inlined.
I think we can fully inline it.
I will remove PX raw 802.3 detection from eth_type_trans() first.
pw-bot: cr