Skip to content

Commit ca925cf

Browse files
Fan Duklassert
authored andcommitted
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. Signed-off-by: Fan Du <fan.du@windriver.com> Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com>
1 parent 0171410 commit ca925cf

6 files changed

Lines changed: 103 additions & 77 deletions

File tree

‎include/net/flow.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,10 @@ struct flow_cache_object *flow_cache_lookup(struct net *net,
218218
const struct flowi *key, u16 family,
219219
u8 dir, flow_resolve_t resolver,
220220
void *ctx);
221+
int flow_cache_init(struct net *net);
221222

222-
void flow_cache_flush(void);
223-
void flow_cache_flush_deferred(void);
223+
void flow_cache_flush(struct net *net);
224+
void flow_cache_flush_deferred(struct net *net);
224225
extern atomic_t flow_cache_genid;
225226

226227
#endif

‎include/net/flowcache.h‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef _NET_FLOWCACHE_H
2+
#define _NET_FLOWCACHE_H
3+
4+
#include <linux/interrupt.h>
5+
#include <linux/types.h>
6+
#include <linux/timer.h>
7+
#include <linux/notifier.h>
8+
9+
struct flow_cache_percpu {
10+
struct hlist_head *hash_table;
11+
int hash_count;
12+
u32 hash_rnd;
13+
int hash_rnd_recalc;
14+
struct tasklet_struct flush_tasklet;
15+
};
16+
17+
struct flow_cache {
18+
u32 hash_shift;
19+
struct flow_cache_percpu __percpu *percpu;
20+
struct notifier_block hotcpu_notifier;
21+
int low_watermark;
22+
int high_watermark;
23+
struct timer_list rnd_timer;
24+
};
25+
#endif /* _NET_FLOWCACHE_H */

‎include/net/netns/xfrm.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <linux/workqueue.h>
77
#include <linux/xfrm.h>
88
#include <net/dst_ops.h>
9+
#include <net/flowcache.h>
910

1011
struct ctl_table_header;
1112

@@ -61,6 +62,16 @@ struct netns_xfrm {
6162
spinlock_t xfrm_policy_sk_bundle_lock;
6263
rwlock_t xfrm_policy_lock;
6364
struct mutex xfrm_cfg_mutex;
65+
66+
/* flow cache part */
67+
struct flow_cache flow_cache_global;
68+
struct kmem_cache *flow_cachep;
69+
atomic_t flow_cache_genid;
70+
struct list_head flow_cache_gc_list;
71+
spinlock_t flow_cache_gc_lock;
72+
struct work_struct flow_cache_gc_work;
73+
struct work_struct flow_cache_flush_work;
74+
struct mutex flow_flush_sem;
6475
};
6576

6677
#endif

‎net/core/flow.c‎

Lines changed: 57 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <net/flow.h>
2525
#include <linux/atomic.h>
2626
#include <linux/security.h>
27+
#include <net/net_namespace.h>
2728

2829
struct flow_cache_entry {
2930
union {
@@ -38,37 +39,12 @@ struct flow_cache_entry {
3839
struct flow_cache_object *object;
3940
};
4041

41-
struct flow_cache_percpu {
42-
struct hlist_head *hash_table;
43-
int hash_count;
44-
u32 hash_rnd;
45-
int hash_rnd_recalc;
46-
struct tasklet_struct flush_tasklet;
47-
};
48-
4942
struct flow_flush_info {
5043
struct flow_cache *cache;
5144
atomic_t cpuleft;
5245
struct completion completion;
5346
};
5447

55-
struct flow_cache {
56-
u32 hash_shift;
57-
struct flow_cache_percpu __percpu *percpu;
58-
struct notifier_block hotcpu_notifier;
59-
int low_watermark;
60-
int high_watermark;
61-
struct timer_list rnd_timer;
62-
};
63-
64-
atomic_t flow_cache_genid = ATOMIC_INIT(0);
65-
EXPORT_SYMBOL(flow_cache_genid);
66-
static struct flow_cache flow_cache_global;
67-
static struct kmem_cache *flow_cachep __read_mostly;
68-
69-
static DEFINE_SPINLOCK(flow_cache_gc_lock);
70-
static LIST_HEAD(flow_cache_gc_list);
71-
7248
#define flow_cache_hash_size(cache) (1 << (cache)->hash_shift)
7349
#define FLOW_HASH_RND_PERIOD (10 * 60 * HZ)
7450

@@ -84,46 +60,50 @@ static void flow_cache_new_hashrnd(unsigned long arg)
8460
add_timer(&fc->rnd_timer);
8561
}
8662

87-
static int flow_entry_valid(struct flow_cache_entry *fle)
63+
static int flow_entry_valid(struct flow_cache_entry *fle,
64+
struct netns_xfrm *xfrm)
8865
{
89-
if (atomic_read(&flow_cache_genid) != fle->genid)
66+
if (atomic_read(&xfrm->flow_cache_genid) != fle->genid)
9067
return 0;
9168
if (fle->object && !fle->object->ops->check(fle->object))
9269
return 0;
9370
return 1;
9471
}
9572

96-
static void flow_entry_kill(struct flow_cache_entry *fle)
73+
static void flow_entry_kill(struct flow_cache_entry *fle,
74+
struct netns_xfrm *xfrm)
9775
{
9876
if (fle->object)
9977
fle->object->ops->delete(fle->object);
100-
kmem_cache_free(flow_cachep, fle);
78+
kmem_cache_free(xfrm->flow_cachep, fle);
10179
}
10280

10381
static void flow_cache_gc_task(struct work_struct *work)
10482
{
10583
struct list_head gc_list;
10684
struct flow_cache_entry *fce, *n;
85+
struct netns_xfrm *xfrm = container_of(work, struct netns_xfrm,
86+
flow_cache_gc_work);
10787

10888
INIT_LIST_HEAD(&gc_list);
109-
spin_lock_bh(&flow_cache_gc_lock);
110-
list_splice_tail_init(&flow_cache_gc_list, &gc_list);
111-
spin_unlock_bh(&flow_cache_gc_lock);
89+
spin_lock_bh(&xfrm->flow_cache_gc_lock);
90+
list_splice_tail_init(&xfrm->flow_cache_gc_list, &gc_list);
91+
spin_unlock_bh(&xfrm->flow_cache_gc_lock);
11292

11393
list_for_each_entry_safe(fce, n, &gc_list, u.gc_list)
114-
flow_entry_kill(fce);
94+
flow_entry_kill(fce, xfrm);
11595
}
116-
static DECLARE_WORK(flow_cache_gc_work, flow_cache_gc_task);
11796

11897
static void flow_cache_queue_garbage(struct flow_cache_percpu *fcp,
119-
int deleted, struct list_head *gc_list)
98+
int deleted, struct list_head *gc_list,
99+
struct netns_xfrm *xfrm)
120100
{
121101
if (deleted) {
122102
fcp->hash_count -= deleted;
123-
spin_lock_bh(&flow_cache_gc_lock);
124-
list_splice_tail(gc_list, &flow_cache_gc_list);
125-
spin_unlock_bh(&flow_cache_gc_lock);
126-
schedule_work(&flow_cache_gc_work);
103+
spin_lock_bh(&xfrm->flow_cache_gc_lock);
104+
list_splice_tail(gc_list, &xfrm->flow_cache_gc_list);
105+
spin_unlock_bh(&xfrm->flow_cache_gc_lock);
106+
schedule_work(&xfrm->flow_cache_gc_work);
127107
}
128108
}
129109

@@ -135,14 +115,16 @@ static void __flow_cache_shrink(struct flow_cache *fc,
135115
struct hlist_node *tmp;
136116
LIST_HEAD(gc_list);
137117
int i, deleted = 0;
118+
struct netns_xfrm *xfrm = container_of(fc, struct netns_xfrm,
119+
flow_cache_global);
138120

139121
for (i = 0; i < flow_cache_hash_size(fc); i++) {
140122
int saved = 0;
141123

142124
hlist_for_each_entry_safe(fle, tmp,
143125
&fcp->hash_table[i], u.hlist) {
144126
if (saved < shrink_to &&
145-
flow_entry_valid(fle)) {
127+
flow_entry_valid(fle, xfrm)) {
146128
saved++;
147129
} else {
148130
deleted++;
@@ -152,7 +134,7 @@ static void __flow_cache_shrink(struct flow_cache *fc,
152134
}
153135
}
154136

155-
flow_cache_queue_garbage(fcp, deleted, &gc_list);
137+
flow_cache_queue_garbage(fcp, deleted, &gc_list, xfrm);
156138
}
157139

158140
static void flow_cache_shrink(struct flow_cache *fc,
@@ -208,7 +190,7 @@ struct flow_cache_object *
208190
flow_cache_lookup(struct net *net, const struct flowi *key, u16 family, u8 dir,
209191
flow_resolve_t resolver, void *ctx)
210192
{
211-
struct flow_cache *fc = &flow_cache_global;
193+
struct flow_cache *fc = &net->xfrm.flow_cache_global;
212194
struct flow_cache_percpu *fcp;
213195
struct flow_cache_entry *fle, *tfle;
214196
struct flow_cache_object *flo;
@@ -248,7 +230,7 @@ flow_cache_lookup(struct net *net, const struct flowi *key, u16 family, u8 dir,
248230
if (fcp->hash_count > fc->high_watermark)
249231
flow_cache_shrink(fc, fcp);
250232

251-
fle = kmem_cache_alloc(flow_cachep, GFP_ATOMIC);
233+
fle = kmem_cache_alloc(net->xfrm.flow_cachep, GFP_ATOMIC);
252234
if (fle) {
253235
fle->net = net;
254236
fle->family = family;
@@ -258,7 +240,7 @@ flow_cache_lookup(struct net *net, const struct flowi *key, u16 family, u8 dir,
258240
hlist_add_head(&fle->u.hlist, &fcp->hash_table[hash]);
259241
fcp->hash_count++;
260242
}
261-
} else if (likely(fle->genid == atomic_read(&flow_cache_genid))) {
243+
} else if (likely(fle->genid == atomic_read(&net->xfrm.flow_cache_genid))) {
262244
flo = fle->object;
263245
if (!flo)
264246
goto ret_object;
@@ -279,7 +261,7 @@ flow_cache_lookup(struct net *net, const struct flowi *key, u16 family, u8 dir,
279261
}
280262
flo = resolver(net, key, family, dir, flo, ctx);
281263
if (fle) {
282-
fle->genid = atomic_read(&flow_cache_genid);
264+
fle->genid = atomic_read(&net->xfrm.flow_cache_genid);
283265
if (!IS_ERR(flo))
284266
fle->object = flo;
285267
else
@@ -303,12 +285,14 @@ static void flow_cache_flush_tasklet(unsigned long data)
303285
struct hlist_node *tmp;
304286
LIST_HEAD(gc_list);
305287
int i, deleted = 0;
288+
struct netns_xfrm *xfrm = container_of(fc, struct netns_xfrm,
289+
flow_cache_global);
306290

307291
fcp = this_cpu_ptr(fc->percpu);
308292
for (i = 0; i < flow_cache_hash_size(fc); i++) {
309293
hlist_for_each_entry_safe(fle, tmp,
310294
&fcp->hash_table[i], u.hlist) {
311-
if (flow_entry_valid(fle))
295+
if (flow_entry_valid(fle, xfrm))
312296
continue;
313297

314298
deleted++;
@@ -317,7 +301,7 @@ static void flow_cache_flush_tasklet(unsigned long data)
317301
}
318302
}
319303

320-
flow_cache_queue_garbage(fcp, deleted, &gc_list);
304+
flow_cache_queue_garbage(fcp, deleted, &gc_list, xfrm);
321305

322306
if (atomic_dec_and_test(&info->cpuleft))
323307
complete(&info->completion);
@@ -351,10 +335,9 @@ static void flow_cache_flush_per_cpu(void *data)
351335
tasklet_schedule(tasklet);
352336
}
353337

354-
void flow_cache_flush(void)
338+
void flow_cache_flush(struct net *net)
355339
{
356340
struct flow_flush_info info;
357-
static DEFINE_MUTEX(flow_flush_sem);
358341
cpumask_var_t mask;
359342
int i, self;
360343

@@ -365,8 +348,8 @@ void flow_cache_flush(void)
365348

366349
/* Don't want cpus going down or up during this. */
367350
get_online_cpus();
368-
mutex_lock(&flow_flush_sem);
369-
info.cache = &flow_cache_global;
351+
mutex_lock(&net->xfrm.flow_flush_sem);
352+
info.cache = &net->xfrm.flow_cache_global;
370353
for_each_online_cpu(i)
371354
if (!flow_cache_percpu_empty(info.cache, i))
372355
cpumask_set_cpu(i, mask);
@@ -386,21 +369,23 @@ void flow_cache_flush(void)
386369
wait_for_completion(&info.completion);
387370

388371
done:
389-
mutex_unlock(&flow_flush_sem);
372+
mutex_unlock(&net->xfrm.flow_flush_sem);
390373
put_online_cpus();
391374
free_cpumask_var(mask);
392375
}
393376

394377
static void flow_cache_flush_task(struct work_struct *work)
395378
{
396-
flow_cache_flush();
397-
}
379+
struct netns_xfrm *xfrm = container_of(work, struct netns_xfrm,
380+
flow_cache_gc_work);
381+
struct net *net = container_of(xfrm, struct net, xfrm);
398382

399-
static DECLARE_WORK(flow_cache_flush_work, flow_cache_flush_task);
383+
flow_cache_flush(net);
384+
}
400385

401-
void flow_cache_flush_deferred(void)
386+
void flow_cache_flush_deferred(struct net *net)
402387
{
403-
schedule_work(&flow_cache_flush_work);
388+
schedule_work(&net->xfrm.flow_cache_flush_work);
404389
}
405390

406391
static int flow_cache_cpu_prepare(struct flow_cache *fc, int cpu)
@@ -425,7 +410,8 @@ static int flow_cache_cpu(struct notifier_block *nfb,
425410
unsigned long action,
426411
void *hcpu)
427412
{
428-
struct flow_cache *fc = container_of(nfb, struct flow_cache, hotcpu_notifier);
413+
struct flow_cache *fc = container_of(nfb, struct flow_cache,
414+
hotcpu_notifier);
429415
int res, cpu = (unsigned long) hcpu;
430416
struct flow_cache_percpu *fcp = per_cpu_ptr(fc->percpu, cpu);
431417

@@ -444,9 +430,20 @@ static int flow_cache_cpu(struct notifier_block *nfb,
444430
return NOTIFY_OK;
445431
}
446432

447-
static int __init flow_cache_init(struct flow_cache *fc)
433+
int flow_cache_init(struct net *net)
448434
{
449435
int i;
436+
struct flow_cache *fc = &net->xfrm.flow_cache_global;
437+
438+
/* Initialize per-net flow cache global variables here */
439+
net->xfrm.flow_cachep = kmem_cache_create("flow_cache",
440+
sizeof(struct flow_cache_entry),
441+
0, SLAB_PANIC, NULL);
442+
spin_lock_init(&net->xfrm.flow_cache_gc_lock);
443+
INIT_LIST_HEAD(&net->xfrm.flow_cache_gc_list);
444+
INIT_WORK(&net->xfrm.flow_cache_gc_work, flow_cache_gc_task);
445+
INIT_WORK(&net->xfrm.flow_cache_flush_work, flow_cache_flush_task);
446+
mutex_init(&net->xfrm.flow_flush_sem);
450447

451448
fc->hash_shift = 10;
452449
fc->low_watermark = 2 * flow_cache_hash_size(fc);
@@ -484,14 +481,4 @@ static int __init flow_cache_init(struct flow_cache *fc)
484481

485482
return -ENOMEM;
486483
}
487-
488-
static int __init flow_cache_init_global(void)
489-
{
490-
flow_cachep = kmem_cache_create("flow_cache",
491-
sizeof(struct flow_cache_entry),
492-
0, SLAB_PANIC, NULL);
493-
494-
return flow_cache_init(&flow_cache_global);
495-
}
496-
497-
module_init(flow_cache_init_global);
484+
EXPORT_SYMBOL(flow_cache_init);

‎net/xfrm/xfrm_policy.c‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
661661
hlist_add_head(&policy->bydst, chain);
662662
xfrm_pol_hold(policy);
663663
net->xfrm.policy_count[dir]++;
664-
atomic_inc(&flow_cache_genid);
664+
atomic_inc(&net->xfrm.flow_cache_genid);
665665

666666
/* After previous checking, family can either be AF_INET or AF_INET6 */
667667
if (policy->family == AF_INET)
@@ -2567,14 +2567,14 @@ static void __xfrm_garbage_collect(struct net *net)
25672567

25682568
void xfrm_garbage_collect(struct net *net)
25692569
{
2570-
flow_cache_flush();
2570+
flow_cache_flush(net);
25712571
__xfrm_garbage_collect(net);
25722572
}
25732573
EXPORT_SYMBOL(xfrm_garbage_collect);
25742574

25752575
static void xfrm_garbage_collect_deferred(struct net *net)
25762576
{
2577-
flow_cache_flush_deferred();
2577+
flow_cache_flush_deferred(net);
25782578
__xfrm_garbage_collect(net);
25792579
}
25802580

@@ -2947,6 +2947,7 @@ static int __net_init xfrm_net_init(struct net *net)
29472947
spin_lock_init(&net->xfrm.xfrm_policy_sk_bundle_lock);
29482948
mutex_init(&net->xfrm.xfrm_cfg_mutex);
29492949

2950+
flow_cache_init(net);
29502951
return 0;
29512952

29522953
out_sysctl:

0 commit comments

Comments
 (0)