Skip to contentSkip to conversation

Optimize lookup of /0 xfrm policies

PATCH In review Yannick Brosseau 2 participants 2 messages Aug 31, 2018 – Sep 3, 2018

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

PATCH v2 1/1 Optimize lookup of /0 xfrm policies

Currently, all the xfrm policies that are not /32 end up in the inexact policies linked list which take a long time to lookup.

We can optimize the case where we have a /0 prefix in the policy, which means we can match any address to that part.
We do this by putting those policies in the direct hash table after zeroing the address part.
At lookup time, we do an additional lookup with the packet address and either the destination or source address zeroed out.
We still call xfrm_policy_match to validate that the packet match the selector.

In our tests, with this optimization we reduce softirq cpu utilisation from about 40% to 7% with 3k policies.

---
 net/xfrm/xfrm_hash.h   | 10 +++++
 net/xfrm/xfrm_policy.c | 87 +++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 95 insertions(+), 2 deletions(-)
net/xfrm/xfrm_hash.h +10 -0
index 61be810389d8..40997fb5336d 100644
@@ -145,6 +145,16 @@ static inline unsigned int __sel_hash(const struct xfrm_selector *sel,
 	const xfrm_address_t *saddr = &sel->saddr;
 	unsigned int h = 0;
 
+	/* A selector with a prefixlen of zero can basically be ignored in
+	 * the matching. To speed up the lookup, let's hash it without those
+	 * component. In the lookup, we'll do an additional check for a zero
+	 * daddr and a zero saddr.
+	 */
+	if (sel->prefixlen_d == 0)
+		dbits = 0;
+	if (sel->prefixlen_s == 0)
+		sbits = 0;
+
 	switch (family) {
 	case AF_INET:
 		if (sel->prefixlen_d < dbits ||
net/xfrm/xfrm_policy.c +85 -2

112 lines not on this page · raw message

Signature · 1 line
2.18.0
maintainer
#1 lore raw
Quoted 2 lines · Yannick Brosseau

Currently, all the xfrm policies that are not /32 end up in the inexact policies linked list which take a long time to lookup.

It is possible to configure which policies are going to the hashtable and the inexact list.

You can do:

ip x p set hthresh4 0 0

This sets the hash threshold to local /0 and remote /0 netmasks. With this configuration, all policies should go to the hashtable.

To view the configuration:

ip -s -s x p count

Can you please do your tests with this too?
I'd really like to avoid to add new code to the policy lookup if we can get similar results with this configuration option.