xfrm: optimize ipv4 selector matching
Subsystems (3)
PATCH 1/1 xfrm: optimize ipv4 selector matching
Current addr_match() is errh, under-optimized.
Compiler doesn't know that memcmp() branch doesn't trigger for IPv4. Pass addresses by value.
---
include/net/xfrm.h | 10 ++++++++++ net/xfrm/xfrm_policy.c | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-)
include/net/xfrm.h +10 -0
@@ -827,6 +827,16 @@ static inline bool addr_match(const void *token1, const void *token2,return true; } +static inline int addr4_match (const __be32 a1,const __be32 a2,const u8 prefixlen) +{ +/* C99 6.5.7 (3): u32 << 32 is undefined behaviour */ +if (prefixlen ==0 ) { +/* Matching constants result in smaller assembly. */ +return 0xFFFFFFFFu ; + } +return !((a1 ^ a2) &htonl (0xFFFFFFFFu << (32 - prefixlen))); +} +static __inline____be16 xfrm_flowi_sport (const struct flowi *fl,const union flowi_uli *uli) {
net/xfrm/xfrm_policy.c +2 -2
@@ -61,8 +61,8 @@ __xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl) {const struct flowi4 *fl4 = &fl->u.ip4; -return addr_match (&fl4->daddr, &sel->daddr, sel->prefixlen_d) && -addr_match (&fl4->saddr, &sel->saddr, sel->prefixlen_s) && +return addr4_match (fl4->daddr, sel->daddr.a4, sel->prefixlen_d) && +addr4_match (fl4->saddr, sel->saddr.a4, sel->prefixlen_s) && !((xfrm_flowi_dport (fl, &fl4->uli) ^ sel->dport) & sel->dport_mask) && !((xfrm_flowi_sport (fl, &fl4->uli) ^ sel->sport) & sel->sport_mask) && (fl4->flowi4_proto == sel->proto || !sel->proto) &&
include/net/xfrm.h static inline bool addr_match(const void *token1, const void *token2, · line 839
Quoted 12 lines
+static inline int
addr4_match(const __be32 a1, const __be32 a2, const u8 prefixlen)include/net/xfrm.h +9
-0+{ +/* C99 6.5.7 (3): u32 << 32 is undefined behaviour */ +if (prefixlen ==0 ) { +/* Matching constants result in smaller assembly. */ +return 0xFFFFFFFFu ; + } +return !((a1 ^ a2) &htonl (0xFFFFFFFFu << (32 - prefixlen))); +} +
It would probably be clearer to 'return 1' when prefixlen is zero.
If this is a common path, might be worth caching
htonl(0xFFFFFFFFu << (32 - prefixlen))
in the enclosing structure.
David
Quoted 14 lines · David Laight
include/net/xfrm.h static inline bool addr_match(const void *token1, const void *token2, · line 839
Quoted 12 lines
+static inline int
addr4_match(const __be32 a1, const __be32 a2, const u8 prefixlen)include/net/xfrm.h +9
-0+{ +/* C99 6.5.7 (3): u32 << 32 is undefined behaviour */ +if (prefixlen ==0 ) { +/* Matching constants result in smaller assembly. */ +return 0xFFFFFFFFu ; + } +return !((a1 ^ a2) &htonl (0xFFFFFFFFu << (32 - prefixlen))); +} +It would probably be clearer to 'return 1' when prefixlen is zero.
"return 1" results in bigger code.
This function used only in boolean context, so exact return value doesn't matter.
Quoted 3 lines
If this is a common path, might be worth caching
htonl(0xFFFFFFFFu << (32 - prefixlen))in the enclosing structure.
This means one more branch, which wouldn't be a win compared to current code.
Quoted 16 lines · Alexey Dobriyan
Quoted 14 lines · David Laight
include/net/xfrm.h static inline bool addr_match(const void *token1, const void *token2, · line 839
Quoted 12 lines
+static inline int
addr4_match(const __be32 a1, const __be32 a2, const u8 prefixlen)include/net/xfrm.h +9
-0+{ +/* C99 6.5.7 (3): u32 << 32 is undefined behaviour */ +if (prefixlen ==0 ) { +/* Matching constants result in smaller assembly. */ +return 0xFFFFFFFFu ; + } +return !((a1 ^ a2) &htonl (0xFFFFFFFFu << (32 - prefixlen))); +} +It would probably be clearer to 'return 1' when prefixlen is zero.
Quoted 2 lines
"return 1" results in bigger code.
This function used only in boolean context, so exact return value doesn't matter.
Thats too ugly, sorry.
Also, using "const" attributes on integral types (not pointers) make code less clear. Caller doesnt care if the implementation wants to change a1 or a2 or prefixlen.
Please use :
+static inline bool addr4_match(__be32 a1, __be32 a2, u8 prefixlen) +{
+ /* C99 6.5.7 (3): u32 << 32 is undefined behaviour */
+ if (prefixlen == 0)
+ return true;
+ return !((a1 ^ a2) & htonl(0xFFFFFFFFu << (32 - prefixlen)));
+}
include/net/xfrm.h static inline bool addr_match(const void *token1, const void *token2, · line 838
Quoted 10 lines
Please use :
include/net/xfrm.h +7
-0+static inline bool addr4_match (__be32 a1,__be32 a2,u8 prefixlen) +{ +/* C99 6.5.7 (3): u32 << 32 is undefined behaviour */ +if (prefixlen ==0 ) +return true; +return !((a1 ^ a2) &htonl (0xFFFFFFFFu << (32 - prefixlen))); +}
I'm not sure I'd agree about using 'u8'.
It may well cause an unnecessary mask with 0xff.
David
Quoted 13 lines · David Laight
include/net/xfrm.h static inline bool addr_match(const void *token1, const void *token2, · line 838
Quoted 10 lines
Please use :
include/net/xfrm.h +7
-0+static inline bool addr4_match (__be32 a1,__be32 a2,u8 prefixlen) +{ +/* C99 6.5.7 (3): u32 << 32 is undefined behaviour */ +if (prefixlen ==0 ) +return true; +return !((a1 ^ a2) &htonl (0xFFFFFFFFu << (32 - prefixlen))); +}I'm not sure I'd agree about using 'u8'.
It may well cause an unnecessary mask with0xff.
It's u8 in all other places.
PATCH v2 1/1 xfrm: optimize ipv4 selector matching
In reply to David LaightCurrent addr_match() is errh, under-optimized.
Compiler doesn't know that memcmp() branch doesn't trigger for IPv4. Also, pass addresses by value -- they fit into register.
---
include/net/xfrm.h | 8 ++++++++ net/xfrm/xfrm_policy.c | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-)
include/net/xfrm.h +8 -0
@@ -827,6 +827,14 @@ static inline bool addr_match(const void *token1, const void *token2,return true; } +static inline bool addr4_match (__be32 a1,__be32 a2,u8 prefixlen) +{ +/* C99 6.5.7 (3): u32 << 32 is undefined behaviour */ +if (prefixlen ==0 ) +return true; +return !((a1 ^ a2) &htonl (0xFFFFFFFFu << (32 - prefixlen))); +} +static __inline____be16 xfrm_flowi_sport (const struct flowi *fl,const union flowi_uli *uli) {
net/xfrm/xfrm_policy.c +2 -2
@@ -61,8 +61,8 @@ __xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl) {const struct flowi4 *fl4 = &fl->u.ip4; -return addr_match (&fl4->daddr, &sel->daddr, sel->prefixlen_d) && -addr_match (&fl4->saddr, &sel->saddr, sel->prefixlen_s) && +return addr4_match (fl4->daddr, sel->daddr.a4, sel->prefixlen_d) && +addr4_match (fl4->saddr, sel->saddr.a4, sel->prefixlen_s) && !((xfrm_flowi_dport (fl, &fl4->uli) ^ sel->dport) & sel->dport_mask) && !((xfrm_flowi_sport (fl, &fl4->uli) ^ sel->sport) & sel->sport_mask) && (fl4->flowi4_proto == sel->proto || !sel->proto) &&
From: Alexey Dobriyan <[email protected]>
Date: Tue, 22 Nov 2011 19:46:02 +0300
Quoted 6 lines
Current
addr_match()is errh, under-optimized.Compiler doesn't know that
memcmp()branch doesn't trigger for IPv4. Also, pass addresses by value -- they fit into register.Signed-off-by Alexey Dobriyan <[email protected]>
Applied, thanks.
Quoted 17 lines · Eric Dumazet
Quoted 16 lines · Alexey Dobriyan
Quoted 14 lines · David Laight
include/net/xfrm.h static inline bool addr_match(const void *token1, const void *token2, · line 839
Quoted 12 lines
+static inline int
addr4_match(const __be32 a1, const __be32 a2, const u8 prefixlen)include/net/xfrm.h +9
-0+{ +/* C99 6.5.7 (3): u32 << 32 is undefined behaviour */ +if (prefixlen ==0 ) { +/* Matching constants result in smaller assembly. */ +return 0xFFFFFFFFu ; + } +return !((a1 ^ a2) &htonl (0xFFFFFFFFu << (32 - prefixlen))); +} +It would probably be clearer to 'return 1' when prefixlen is zero.
Quoted 8 lines
Quoted 2 lines
"return 1" results in bigger code.
This function used only in boolean context, so exact return value doesn't matter.Thats too ugly, sorry.
Also, using "const" attributes on integral types (not pointers) make code less clear.
No. it doesn't.
It signals to programmer that functions doesn't change its arguments.
Quoted 2 lines
Caller doesnt care if the implementation wants to
change a1 or a2 or prefixlen.
const is never about caller.
Millions of const qualifiers were injected already, so who cares.
include/net/xfrm.h static inline bool addr_match(const void *token1, const void *token2, · line 838
Quoted 9 lines
Please use :
include/net/xfrm.h +7
-0+static inline bool addr4_match (__be32 a1,__be32 a2,u8 prefixlen) +{ +/* C99 6.5.7 (3): u32 << 32 is undefined behaviour */ +if (prefixlen ==0 ) +return true; +return !((a1 ^ a2) &htonl (0xFFFFFFFFu << (32 - prefixlen))); +}
hmm, bool results in smaller code for -Os, so I'll resend.