netfilter: fib: avoid lookup if socket is available
Subsystems (2)
Florian Westphal
maintainerPATCH 1/1 netfilter: fib: avoid lookup if socket is available
In case the fib match is used from the input hook we can avoid the fib lookup if early demux assigned a socket for us: check that the input interface matches sk-cached one.
Rework the existing 'lo bypass' logic to first check sk, then for loopback interface type to elide the fib lookup.
This speeds up fib matching a little, before:
93.08 GBit/s (no rules at all)
75.1 GBit/s ("fib saddr . iif oif missing drop" in prerouting) 75.62 GBit/s ("fib saddr . iif oif missing drop" in input)
After:
92.48 GBit/s (no rules at all)
75.62 GBit/s (fib rule in prerouting)
90.37 GBit/s (fib rule in input).
Numbers for the 'no rules' and 'prerouting' are expected to closely match in-between runs, the 3rd/input test case exercises the the 'avoid lookup if cached ifindex in sk matches' case.
Test used iperf3 via veth interface, lo can't be used due to existing loopback test.
--- include/net/netfilter/nft_fib.h | 21 +++++++++++++++++++++ net/ipv4/netfilter/nft_fib_ipv4.c | 11 +++++------ net/ipv6/netfilter/nft_fib_ipv6.c | 19 ++++++++++--------- 3 files changed, 36 insertions(+), 15 deletions(-)
include/net/netfilter/nft_fib.h +21 -0
@@ -18,6 +18,27 @@ nft_fib_is_loopback(const struct sk_buff *skb, const struct net_device *in)return skb->pkt_type ==PACKET_LOOPBACK || in->flags &IFF_LOOPBACK ; } +static inline bool nft_fib_can_skip (const struct nft_pktinfo *pkt) +{ +const struct net_device *indev =nft_in (pkt); +const struct sock *sk; + +switch (nft_hook (pkt)) { +case NF_INET_PRE_ROUTING : +case NF_INET_INGRESS : +case NF_INET_LOCAL_IN : +break ; +default : +return false; + } + + sk = pkt->skb->sk; +if (sk &&sk_fullsock (sk)) +return sk->sk_rx_dst_ifindex == indev->ifindex; + +return nft_fib_is_loopback (pkt->skb, indev); +} +int nft_fib_dump (struct sk_buff *skb,const struct nft_expr *expr,bool reset);int nft_fib_init (const struct nft_ctx *ctx,const struct nft_expr *expr,const struct nlattr *const tb[]);
net/ipv4/netfilter/nft_fib_ipv4.c +5 -6
@@ -71,6 +71,11 @@ void nft_fib4_eval(const struct nft_expr *expr, struct nft_regs *regs,const struct net_device *oif;const struct net_device *found; +if (nft_fib_can_skip (pkt)) { +nft_fib_store_result (dest, priv,nft_in (pkt)); +return ; + } +/* * Do not set flowi4_oif, it restricts results (for example, asking * for oif 3 will get RTN_UNICAST result even if the daddr exits
@@ -85,12 +90,6 @@ void nft_fib4_eval(const struct nft_expr *expr, struct nft_regs *regs,else oif =NULL ; -if (nft_hook (pkt) ==NF_INET_PRE_ROUTING && -nft_fib_is_loopback (pkt->skb,nft_in (pkt))) { -nft_fib_store_result (dest, priv,nft_in (pkt)); -return ; - } - iph =skb_header_pointer (pkt->skb, noff,sizeof (_iph), &_iph);if (!iph) { regs->verdict.code =NFT_BREAK ;
net/ipv6/netfilter/nft_fib_ipv6.c +10 -9
@@ -170,6 +170,11 @@ void nft_fib6_eval(const struct nft_expr *expr, struct nft_regs *regs,struct rt6_info *rt;int lookup_flags; +if (nft_fib_can_skip (pkt)) { +nft_fib_store_result (dest, priv,nft_in (pkt)); +return ; + } +if (priv->flags &NFTA_FIB_F_IIF ) oif =nft_in (pkt);else if (priv->flags &NFTA_FIB_F_OIF )
@@ -181,17 +186,13 @@ void nft_fib6_eval(const struct nft_expr *expr, struct nft_regs *regs,return ; } - lookup_flags =nft_fib6_flowi_init (&fl6, priv, pkt, oif, iph); - -if (nft_hook (pkt) ==NF_INET_PRE_ROUTING || -nft_hook (pkt) ==NF_INET_INGRESS ) { -if (nft_fib_is_loopback (pkt->skb,nft_in (pkt)) || -nft_fib_v6_skip_icmpv6 (pkt->skb, pkt->tprot, iph)) { -nft_fib_store_result (dest, priv,nft_in (pkt)); -return ; - } +if (nft_fib_v6_skip_icmpv6 (pkt->skb, pkt->tprot, iph)) { +nft_fib_store_result (dest, priv,nft_in (pkt)); +return ; } + lookup_flags =nft_fib6_flowi_init (&fl6, priv, pkt, oif, iph); + *dest =0 ; rt = (void *)ip6_route_lookup (nft_net (pkt), &fl6, pkt->skb, lookup_flags);
Signature · 1 line
2.45.3
Pablo Neira Ayuso
maintainerQuoted 17 lines · Florian Westphal
In case the fib match is used from the input hook we can avoid the fib lookup if early demux assigned a socket for us: check that the input interface matches sk-cached one.
Rework the existing 'lo bypass' logic to first check sk, then for loopback interface type to elide the fib lookup.
This speeds up fib matching a little, before:
93.08 GBit/s (no rules at all)
75.1 GBit/s ("fib saddr . iif oif missing drop" in prerouting) 75.62 GBit/s ("fib saddr . iif oif missing drop" in input)After:
92.48 GBit/s (no rules at all)
75.62 GBit/s (fib rule in prerouting)
90.37 GBit/s (fib rule in input).
Quoted 6 lines
Numbers for the 'no rules' and 'prerouting' are expected to closely match in-between runs, the 3rd/input test case exercises the the 'avoid lookup if cached ifindex in sk matches' case.
Test used iperf3 via veth interface, lo can't be used due to existing loopback test.
A few questions below.
Quoted 13 lines
Signed-off-by Florian Westphal <[email protected]>--- include/net/netfilter/nft_fib.h | 21 +++++++++++++++++++++ net/ipv4/netfilter/nft_fib_ipv4.c | 11 +++++------ net/ipv6/netfilter/nft_fib_ipv6.c | 19 ++++++++++--------- 3 files changed, 36 insertions(+), 15 deletions(-)include/net/netfilter/nft_fib.h +0
-0@@ -18,6 +18,27 @@ nft_fib_is_loopback(const struct sk_buff *skb, const struct net_device *in)return skb->pkt_type ==PACKET_LOOPBACK || in->flags &IFF_LOOPBACK ;
include/net/netfilter/nft_fib.h nft_fib_is_loopback(const struct sk_buff *skb, const struct net_device *in) · line 28
Quoted 10 lines
include/net/netfilter/nft_fib.h +8
-0} +static inline bool nft_fib_can_skip (const struct nft_pktinfo *pkt) +{ +const struct net_device *indev =nft_in (pkt); +const struct sock *sk; + +switch (nft_hook (pkt)) { +case NF_INET_PRE_ROUTING : +case NF_INET_INGRESS :
Not an issue in your patch itself, it seems nft_fib_validate() was never updated to support NF_INET_INGRESS.
Quoted 21 lines
include/net/netfilter/nft_fib.h +13
-0+case NF_INET_LOCAL_IN : +break ; +default : +return false; + } + + sk = pkt->skb->sk; +if (sk &&sk_fullsock (sk)) +return sk->sk_rx_dst_ifindex == indev->ifindex; + +return nft_fib_is_loopback (pkt->skb, indev); +} +int nft_fib_dump (struct sk_buff *skb,const struct nft_expr *expr,bool reset);int nft_fib_init (const struct nft_ctx *ctx,const struct nft_expr *expr,const struct nlattr *const tb[]);net/ipv4/netfilter/nft_fib_ipv4.c +0
-0
net/ipv4/netfilter/nft_fib_ipv4.c void nft_fib4_eval(const struct nft_expr *expr, struct nft_regs *regs, · line 77
Quoted 7 lines
net/ipv4/netfilter/nft_fib_ipv4.c +4
-0@@ -71,6 +71,11 @@ void nft_fib4_eval(const struct nft_expr *expr, struct nft_regs *regs,const struct net_device *oif;const struct net_device *found; +if (nft_fib_can_skip (pkt)) { +nft_fib_store_result (dest, priv,nft_in (pkt)); +return ; + }
Silly question: Does this optimization work for all cases? NFTA_FIB_F_MARK and NFTA_FIB_F_DADDR.
Quoted 59 lines
net/ipv4/netfilter/nft_fib_ipv4.c +0
-6/* * Do not set flowi4_oif, it restricts results (for example, asking * for oif 3 will get RTN_UNICAST result even if the daddr exits @@ -85,12 +90,6 @@ void nft_fib4_eval(const struct nft_expr *expr, struct nft_regs *regs,else oif =NULL ; -if (nft_hook (pkt) ==NF_INET_PRE_ROUTING && -nft_fib_is_loopback (pkt->skb,nft_in (pkt))) { -nft_fib_store_result (dest, priv,nft_in (pkt)); -return ; - } - iph =skb_header_pointer (pkt->skb, noff,sizeof (_iph), &_iph);if (!iph) { regs->verdict.code =NFT_BREAK ;net/ipv6/netfilter/nft_fib_ipv6.c +10
-9@@ -170,6 +170,11 @@ void nft_fib6_eval(const struct nft_expr *expr, struct nft_regs *regs,struct rt6_info *rt;int lookup_flags; +if (nft_fib_can_skip (pkt)) { +nft_fib_store_result (dest, priv,nft_in (pkt)); +return ; + } +if (priv->flags &NFTA_FIB_F_IIF ) oif =nft_in (pkt);else if (priv->flags &NFTA_FIB_F_OIF )@@ -181,17 +186,13 @@ void nft_fib6_eval(const struct nft_expr *expr, struct nft_regs *regs,return ; } - lookup_flags =nft_fib6_flowi_init (&fl6, priv, pkt, oif, iph); - -if (nft_hook (pkt) ==NF_INET_PRE_ROUTING || -nft_hook (pkt) ==NF_INET_INGRESS ) { -if (nft_fib_is_loopback (pkt->skb,nft_in (pkt)) || -nft_fib_v6_skip_icmpv6 (pkt->skb, pkt->tprot, iph)) { -nft_fib_store_result (dest, priv,nft_in (pkt)); -return ; - } +if (nft_fib_v6_skip_icmpv6 (pkt->skb, pkt->tprot, iph)) { +nft_fib_store_result (dest, priv,nft_in (pkt)); +return ; } + lookup_flags =nft_fib6_flowi_init (&fl6, priv, pkt, oif, iph); + *dest =0 ; rt = (void *)ip6_route_lookup (nft_net (pkt), &fl6, pkt->skb, lookup_flags);Signature · 1 line
2.45.3
Florian Westphal
maintainerQuoted 6 lines · Pablo Neira Ayuso
include/net/netfilter/nft_fib.h nft_fib_is_loopback(const struct sk_buff *skb, const struct net_device *in) · line 28
Quoted 3 lines
include/net/netfilter/nft_fib.h +3
-0+switch (nft_hook (pkt)) { +case NF_INET_PRE_ROUTING : +case NF_INET_INGRESS :Not an issue in your patch itself, it seems
nft_fib_validate()was never updated to supportNF_INET_INGRESS.
Yes, probably better to do that in a different patch.
Quoted 7 lines
net/ipv4/netfilter/nft_fib_ipv4.c void nft_fib4_eval(const struct nft_expr *expr, struct nft_regs *regs, · line 77
Quoted 4 lines
net/ipv4/netfilter/nft_fib_ipv4.c +4
-0+if (nft_fib_can_skip (pkt)) { +nft_fib_store_result (dest, priv,nft_in (pkt)); +return ; + }Silly question: Does this optimization work for all cases?
NFTA_FIB_F_MARKandNFTA_FIB_F_DADDR.
Its the socket that the skb will be delivered to, so I don't see an issue. Theoretically you could set a different mark in input, but what is it good for? Its too late to change routing result.
As this sits in input hook, route lookup done by stack (not by fib expr) already picked nft_in as the 'right' interface for this daddr.
Pablo Neira Ayuso
maintainerQuoted 18 lines · Florian Westphal
Quoted 6 lines · Pablo Neira Ayuso
include/net/netfilter/nft_fib.h nft_fib_is_loopback(const struct sk_buff *skb, const struct net_device *in) · line 28
Quoted 3 lines
include/net/netfilter/nft_fib.h +3
-0+switch (nft_hook (pkt)) { +case NF_INET_PRE_ROUTING : +case NF_INET_INGRESS :Not an issue in your patch itself, it seems
nft_fib_validate()was never updated to supportNF_INET_INGRESS.Yes, probably better to do that in a different patch.
Quoted 7 lines
net/ipv4/netfilter/nft_fib_ipv4.c void nft_fib4_eval(const struct nft_expr *expr, struct nft_regs *regs, · line 77
Quoted 4 lines
net/ipv4/netfilter/nft_fib_ipv4.c +4
-0+if (nft_fib_can_skip (pkt)) { +nft_fib_store_result (dest, priv,nft_in (pkt)); +return ; + }Silly question: Does this optimization work for all cases?
NFTA_FIB_F_MARKandNFTA_FIB_F_DADDR.
Quoted 3 lines
Its the socket that the skb will be delivered to, so I don't see an issue. Theoretically you could set a different mark in input, but what is it good for? Its too late to change routing result.
I see, makes no sense to trigger another lookup with the different mark after the stack already provides a route (no use-case for this).
Quoted 2 lines
As this sits in input hook, route lookup done by stack (not by fib expr) already picked
nft_inas the 'right' interface for this daddr.
thanks for explaining.