3636#define TBL_MIN_BUCKETS 1024
3737#define REHASH_INTERVAL (10 * 60 * HZ)
3838
39+ #define MC_HASH_SHIFT 8
40+ #define MC_HASH_ENTRIES (1u << MC_HASH_SHIFT)
41+ #define MC_HASH_SEGS ((sizeof(uint32_t) * 8) / MC_HASH_SHIFT)
42+
3943static struct kmem_cache * flow_cache ;
4044struct kmem_cache * flow_stats_cache __read_mostly ;
4145
@@ -168,10 +172,15 @@ int ovs_flow_tbl_init(struct flow_table *table)
168172{
169173 struct table_instance * ti , * ufid_ti ;
170174
171- ti = table_instance_alloc (TBL_MIN_BUCKETS );
175+ table -> mask_cache = __alloc_percpu (sizeof (struct mask_cache_entry ) *
176+ MC_HASH_ENTRIES ,
177+ __alignof__(struct mask_cache_entry ));
178+ if (!table -> mask_cache )
179+ return - ENOMEM ;
172180
181+ ti = table_instance_alloc (TBL_MIN_BUCKETS );
173182 if (!ti )
174- return - ENOMEM ;
183+ goto free_mask_cache ;
175184
176185 ufid_ti = table_instance_alloc (TBL_MIN_BUCKETS );
177186 if (!ufid_ti )
@@ -187,6 +196,8 @@ int ovs_flow_tbl_init(struct flow_table *table)
187196
188197free_ti :
189198 __table_instance_destroy (ti );
199+ free_mask_cache :
200+ free_percpu (table -> mask_cache );
190201 return - ENOMEM ;
191202}
192203
@@ -243,6 +254,7 @@ void ovs_flow_tbl_destroy(struct flow_table *table)
243254 struct table_instance * ti = rcu_dereference_raw (table -> ti );
244255 struct table_instance * ufid_ti = rcu_dereference_raw (table -> ufid_ti );
245256
257+ free_percpu (table -> mask_cache );
246258 table_instance_destroy (ti , ufid_ti , false);
247259}
248260
@@ -425,7 +437,8 @@ static bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
425437
426438static struct sw_flow * masked_flow_lookup (struct table_instance * ti ,
427439 const struct sw_flow_key * unmasked ,
428- const struct sw_flow_mask * mask )
440+ const struct sw_flow_mask * mask ,
441+ u32 * n_mask_hit )
429442{
430443 struct sw_flow * flow ;
431444 struct hlist_head * head ;
@@ -435,6 +448,8 @@ static struct sw_flow *masked_flow_lookup(struct table_instance *ti,
435448 ovs_flow_mask_key (& masked_key , unmasked , false, mask );
436449 hash = flow_hash (& masked_key , & mask -> range );
437450 head = find_bucket (ti , hash );
451+ (* n_mask_hit )++ ;
452+
438453 hlist_for_each_entry_rcu (flow , head , flow_table .node [ti -> node_ver ]) {
439454 if (flow -> mask == mask && flow -> flow_table .hash == hash &&
440455 flow_cmp_masked_key (flow , & masked_key , & mask -> range ))
@@ -443,30 +458,97 @@ static struct sw_flow *masked_flow_lookup(struct table_instance *ti,
443458 return NULL ;
444459}
445460
446- struct sw_flow * ovs_flow_tbl_lookup_stats (struct flow_table * tbl ,
447- const struct sw_flow_key * key ,
448- u32 * n_mask_hit )
461+ static struct sw_flow * flow_lookup (struct flow_table * tbl ,
462+ struct table_instance * ti ,
463+ const struct sw_flow_key * key ,
464+ u32 * n_mask_hit )
449465{
450- struct table_instance * ti = rcu_dereference_ovsl (tbl -> ti );
451466 struct sw_flow_mask * mask ;
452467 struct sw_flow * flow ;
453468
454- * n_mask_hit = 0 ;
455469 list_for_each_entry_rcu (mask , & tbl -> mask_list , list ) {
456- (* n_mask_hit )++ ;
457- flow = masked_flow_lookup (ti , key , mask );
470+ flow = masked_flow_lookup (ti , key , mask , n_mask_hit );
458471 if (flow ) /* Found */
459472 return flow ;
460473 }
461474 return NULL ;
462475}
463476
477+ /*
478+ * mask_cache maps flow to probable mask. This cache is not tightly
479+ * coupled cache, It means updates to mask list can result in inconsistent
480+ * cache entry in mask cache.
481+ * This is per cpu cache and is divided in MC_HASH_SEGS segments.
482+ * In case of a hash collision the entry is hashed in next segment.
483+ * */
484+ struct sw_flow * ovs_flow_tbl_lookup_stats (struct flow_table * tbl ,
485+ const struct sw_flow_key * key ,
486+ u32 skb_hash ,
487+ u32 * n_mask_hit )
488+ {
489+ struct table_instance * ti = rcu_dereference_ovsl (tbl -> ti );
490+ struct mask_cache_entry * entries , * ce , * del ;
491+ struct sw_flow * flow ;
492+ u32 hash = skb_hash ;
493+ int seg ;
494+
495+ * n_mask_hit = 0 ;
496+ if (unlikely (!skb_hash ))
497+ return flow_lookup (tbl , ti , key , n_mask_hit );
498+
499+ del = NULL ;
500+ entries = this_cpu_ptr (tbl -> mask_cache );
501+
502+ for (seg = 0 ; seg < MC_HASH_SEGS ; seg ++ ) {
503+ int index ;
504+
505+ index = hash & (MC_HASH_ENTRIES - 1 );
506+ ce = & entries [index ];
507+
508+ if (ce -> skb_hash == skb_hash ) {
509+ struct sw_flow_mask * mask ;
510+ int i ;
511+
512+ i = 0 ;
513+ list_for_each_entry_rcu (mask , & tbl -> mask_list , list ) {
514+ if (ce -> mask_index == i ++ ) {
515+ flow = masked_flow_lookup (ti , key , mask ,
516+ n_mask_hit );
517+ if (flow ) /* Found */
518+ return flow ;
519+
520+ break ;
521+ }
522+ }
523+
524+ del = ce ;
525+ break ;
526+ }
527+
528+ if (!del || (del -> skb_hash && !ce -> skb_hash )) {
529+ del = ce ;
530+ }
531+
532+ hash >>= MC_HASH_SHIFT ;
533+ }
534+
535+ flow = flow_lookup (tbl , ti , key , n_mask_hit );
536+
537+ if (flow ) {
538+ del -> skb_hash = skb_hash ;
539+ del -> mask_index = (* n_mask_hit - 1 );
540+ }
541+
542+ return flow ;
543+ }
544+
464545struct sw_flow * ovs_flow_tbl_lookup (struct flow_table * tbl ,
465546 const struct sw_flow_key * key )
466547{
548+ struct table_instance * ti = rcu_dereference_ovsl (tbl -> ti );
467549 u32 __always_unused n_mask_hit ;
468550
469- return ovs_flow_tbl_lookup_stats (tbl , key , & n_mask_hit );
551+ return flow_lookup (tbl , ti , key , & n_mask_hit );
470552}
471553
472554struct sw_flow * ovs_flow_tbl_lookup_exact (struct flow_table * tbl ,
@@ -475,10 +557,11 @@ struct sw_flow *ovs_flow_tbl_lookup_exact(struct flow_table *tbl,
475557 struct table_instance * ti = rcu_dereference_ovsl (tbl -> ti );
476558 struct sw_flow_mask * mask ;
477559 struct sw_flow * flow ;
560+ u32 __always_unused n_mask_hit ;
478561
479562 /* Always called under ovs-mutex. */
480563 list_for_each_entry (mask , & tbl -> mask_list , list ) {
481- flow = masked_flow_lookup (ti , match -> key , mask );
564+ flow = masked_flow_lookup (ti , match -> key , mask , & n_mask_hit );
482565 if (flow && ovs_identifier_is_key (& flow -> id ) &&
483566 ovs_flow_cmp_unmasked_key (flow , match ))
484567 return flow ;
@@ -631,7 +714,7 @@ static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow,
631714 return - ENOMEM ;
632715 mask -> key = new -> key ;
633716 mask -> range = new -> range ;
634- list_add_rcu (& mask -> list , & tbl -> mask_list );
717+ list_add_tail_rcu (& mask -> list , & tbl -> mask_list );
635718 } else {
636719 BUG_ON (!mask -> ref_count );
637720 mask -> ref_count ++ ;
0 commit comments