2424#include <net/flow.h>
2525#include <linux/atomic.h>
2626#include <linux/security.h>
27+ #include <net/net_namespace.h>
2728
2829struct 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-
4942struct 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
10381static 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
11897static 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
158140static void flow_cache_shrink (struct flow_cache * fc ,
@@ -208,7 +190,7 @@ struct flow_cache_object *
208190flow_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
388371done :
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
394377static 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
406391static 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 );
0 commit comments