Skip to contentSkip to conversation

Make flow cache name space aware

PATCH New Fan Du 1 participant 3 messages Jan 18, 2014

Subsystems (4)
lore
Share & tools
lore

Fan Du

author
#0 lore raw

v4 cover Make flow cache name space aware

Hi,

This patch set aims to make flow cache operating in a per net style when inserting flow cache entry or flush flow cache. The reason to do so is not much compelling but reasonable, which is flushing flow cache in original implementation has global effective, the collateral damage is netns with only a few flow cache entries has gone.

So this patch make flow cache running in a per net scope. Operation from different netns won't interfere with each other. And the flushing operation is worthwhile for the netns which supposed to be.

v2:
- Pick up newly created file include/net/flowcache.h missed in v1.
v3:
- I made a mistake by spreading one unified patch into different patches, which break bisect operation. So forge into one piece patches advised by Cong and Sabrina.
v4:
- Fix selinux xfrm compiling issue.

Fan Du (2):
flowcache: Make flow cache name space aware
flowcache: Bring net/core/flow.c under IPsec maintain scope

 MAINTAINERS                     |    1 +
 include/net/flow.h              |    5 +-
 include/net/flowcache.h         |   25 ++++++++
 include/net/netns/xfrm.h        |   11 ++++
 net/core/flow.c                 |  127 ++++++++++++++++++---------------------
 net/xfrm/xfrm_policy.c          |    7 ++-
 security/selinux/include/xfrm.h |    5 +-
 7 files changed, 104 insertions(+), 77 deletions(-)
 create mode 100644 include/net/flowcache.h
Signature · 1 line
1.7.9.5
author
#1 lore raw

PATCH v4 1/2 flowcache: Make flow cache name space aware

Inserting a entry into flowcache, or flushing flowcache should be based on per net scope. The reason to do so is flushing operation from fat netns crammed with flow entries will also making the slim netns with only a few flow cache entries go away in original implementation.

Since flowcache is tightly coupled with IPsec, so it would be easier to put flow cache global parameters into xfrm namespace part. And one last thing needs to do is bumping flow cache genid, and flush flow cache should also be made in per net style.

---
 include/net/flow.h              |    5 +-
 include/net/flowcache.h         |   25 ++++++++
 include/net/netns/xfrm.h        |   11 ++++
 net/core/flow.c                 |  127 ++++++++++++++++++---------------------
 net/xfrm/xfrm_policy.c          |    7 ++-
 security/selinux/include/xfrm.h |    5 +-
 6 files changed, 103 insertions(+), 77 deletions(-)
 create mode 100644 include/net/flowcache.h
include/net/flow.h +3 -2
index d23e7fa..bee3741 100644
@@ -218,9 +218,10 @@ struct flow_cache_object *flow_cache_lookup(struct net *net,
 					    const struct flowi *key, u16 family,
 					    u8 dir, flow_resolve_t resolver,
 					    void *ctx);
+int flow_cache_init(struct net *net);
 
-void flow_cache_flush(void);
-void flow_cache_flush_deferred(void);
+void flow_cache_flush(struct net *net);
+void flow_cache_flush_deferred(struct net *net);
 extern atomic_t flow_cache_genid;
 
 #endif
include/net/flowcache.h new file +25 -0
new file mode 100644
index 0000000..c8f665e
@@ -0,0 +1,25 @@
+#ifndef _NET_FLOWCACHE_H
+#define _NET_FLOWCACHE_H
+
+#include <linux/interrupt.h>
+#include <linux/types.h>
+#include <linux/timer.h>
+#include <linux/notifier.h>
+
+struct flow_cache_percpu {
+	struct hlist_head		*hash_table;
+	int				hash_count;
+	u32				hash_rnd;
+	int				hash_rnd_recalc;
+	struct tasklet_struct		flush_tasklet;
+};
+
+struct flow_cache {
+	u32				hash_shift;
+	struct flow_cache_percpu __percpu *percpu;
+	struct notifier_block		hotcpu_notifier;
+	int				low_watermark;
+	int				high_watermark;
+	struct timer_list		rnd_timer;
+};
+#endif	/* _NET_FLOWCACHE_H */
include/net/netns/xfrm.h +11 -0
index 1006a26..52d0086 100644
@@ -6,6 +6,7 @@
 #include <linux/workqueue.h>
 #include <linux/xfrm.h>
 #include <net/dst_ops.h>
+#include <net/flowcache.h>
 
 struct ctl_table_header;
 
@@ -61,6 +62,16 @@ struct netns_xfrm {
 	spinlock_t xfrm_policy_sk_bundle_lock;
 	rwlock_t xfrm_policy_lock;
 	struct mutex xfrm_cfg_mutex;
+
+	/* flow cache part */
+	struct flow_cache	flow_cache_global;
+	struct kmem_cache	*flow_cachep;
+	atomic_t		flow_cache_genid;
+	struct list_head	flow_cache_gc_list;
+	spinlock_t		flow_cache_gc_lock;
+	struct work_struct	flow_cache_gc_work;
+	struct work_struct	flow_cache_flush_work;
+	struct mutex		flow_flush_sem;
 };
 
 #endif
net/core/flow.c +57 -70

282 lines not on this page · raw message

net/xfrm/xfrm_policy.c +4 -3
index 0177082..9d60590 100644
@@ -661,7 +661,7 @@ int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
 		hlist_add_head(&policy->bydst, chain);
 	xfrm_pol_hold(policy);
 	net->xfrm.policy_count[dir]++;
-	atomic_inc(&flow_cache_genid);
+	atomic_inc(&net->xfrm.flow_cache_genid);
 
 	/* After previous checking, family can either be AF_INET or AF_INET6 */
 	if (policy->family == AF_INET)
@@ -2567,14 +2567,14 @@ static void __xfrm_garbage_collect(struct net *net)
 
 void xfrm_garbage_collect(struct net *net)
 {
-	flow_cache_flush();
+	flow_cache_flush(net);
 	__xfrm_garbage_collect(net);
 }
 EXPORT_SYMBOL(xfrm_garbage_collect);
 
 static void xfrm_garbage_collect_deferred(struct net *net)
 {
-	flow_cache_flush_deferred();
+	flow_cache_flush_deferred(net);
 	__xfrm_garbage_collect(net);
 }
 
@@ -2947,6 +2947,7 @@ static int __net_init xfrm_net_init(struct net *net)
 	spin_lock_init(&net->xfrm.xfrm_policy_sk_bundle_lock);
 	mutex_init(&net->xfrm.xfrm_cfg_mutex);
 
+	flow_cache_init(net);
 	return 0;
 
 out_sysctl:
security/selinux/include/xfrm.h +3 -2
index 48c3cc9..dfe3fda 100644
@@ -45,10 +45,11 @@ static inline void selinux_xfrm_notify_policyload(void)
 {
 	struct net *net;
 
-	atomic_inc(&flow_cache_genid);
 	rtnl_lock();
-	for_each_net(net)
+	for_each_net(net) {
+		atomic_inc(&net->xfrm.flow_cache_genid);
 		rt_genid_bump_all(net);
+	}
 	rtnl_unlock();
 }
 #else
Signature · 1 line
1.7.9.5
author
#2 lore raw

PATCH v4 2/2 flowcache: Bring net/core/flow.c under IPsec maintain scope

In reply to Fan Du

As flow cache is mainly manipulated from IPsec.

---
 MAINTAINERS |    1 +
 1 file changed, 1 insertion(+)
MAINTAINERS +1 -0
index b358a3f..492e6c9 100644
@@ -5921,6 +5921,7 @@ L:	[email protected]
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/klassert/ipsec.git
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/klassert/ipsec-next.git
 S:	Maintained
+F:	net/core/flow.c
 F:	net/xfrm/
 F:	net/key/
 F:	net/ipv4/xfrm*
Signature · 1 line
1.7.9.5