xfrm: optimize ipv4 selector matching
authorAlexey Dobriyan <adobriyan@gmail.com>
Tue, 22 Nov 2011 06:46:02 +0000 (06:46 +0000)
committerDavid S. Miller <davem@davemloft.net>
Tue, 22 Nov 2011 20:27:18 +0000 (15:27 -0500)
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 <adobriyan@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
include/net/xfrm.h
net/xfrm/xfrm_policy.c

index b203e14d26b7f59a5f884b87183d3ecb74447d2a..4de7ed9016d9f6bf231c838a2da5b4aa5b41c458 100644 (file)
@@ -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)
 {
index 552df27dcf53d3388fb2eb9ab3ba7a31c7ffcd64..593c8a1f14403163b26bf2cd8fda5f91b1074b91 100644 (file)
@@ -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) &&