2929#include <linux/icmp.h>
3030#include <linux/icmpv6.h>
3131#include <linux/rculist.h>
32+ #include <linux/sort.h>
3233#include <net/ip.h>
3334#include <net/ipv6.h>
3435#include <net/ndisc.h>
@@ -169,16 +170,70 @@ static struct table_instance *table_instance_alloc(int new_size)
169170 return ti ;
170171}
171172
173+ static void __mask_array_destroy (struct mask_array * ma )
174+ {
175+ free_percpu (ma -> masks_usage_cntr );
176+ kfree (ma );
177+ }
178+
179+ static void mask_array_rcu_cb (struct rcu_head * rcu )
180+ {
181+ struct mask_array * ma = container_of (rcu , struct mask_array , rcu );
182+
183+ __mask_array_destroy (ma );
184+ }
185+
186+ static void tbl_mask_array_reset_counters (struct mask_array * ma )
187+ {
188+ int i , cpu ;
189+
190+ /* As the per CPU counters are not atomic we can not go ahead and
191+ * reset them from another CPU. To be able to still have an approximate
192+ * zero based counter we store the value at reset, and subtract it
193+ * later when processing.
194+ */
195+ for (i = 0 ; i < ma -> max ; i ++ ) {
196+ ma -> masks_usage_zero_cntr [i ] = 0 ;
197+
198+ for_each_possible_cpu (cpu ) {
199+ u64 * usage_counters = per_cpu_ptr (ma -> masks_usage_cntr ,
200+ cpu );
201+ unsigned int start ;
202+ u64 counter ;
203+
204+ do {
205+ start = u64_stats_fetch_begin_irq (& ma -> syncp );
206+ counter = usage_counters [i ];
207+ } while (u64_stats_fetch_retry_irq (& ma -> syncp , start ));
208+
209+ ma -> masks_usage_zero_cntr [i ] += counter ;
210+ }
211+ }
212+ }
213+
172214static struct mask_array * tbl_mask_array_alloc (int size )
173215{
174216 struct mask_array * new ;
175217
176218 size = max (MASK_ARRAY_SIZE_MIN , size );
177219 new = kzalloc (sizeof (struct mask_array ) +
178- sizeof (struct sw_flow_mask * ) * size , GFP_KERNEL );
220+ sizeof (struct sw_flow_mask * ) * size +
221+ sizeof (u64 ) * size , GFP_KERNEL );
179222 if (!new )
180223 return NULL ;
181224
225+ new -> masks_usage_zero_cntr = (u64 * )((u8 * )new +
226+ sizeof (struct mask_array ) +
227+ sizeof (struct sw_flow_mask * ) *
228+ size );
229+
230+ new -> masks_usage_cntr = __alloc_percpu (sizeof (u64 ) * size ,
231+ __alignof__(u64 ));
232+ if (!new -> masks_usage_cntr ) {
233+ kfree (new );
234+ return NULL ;
235+ }
236+
182237 new -> count = 0 ;
183238 new -> max = size ;
184239
@@ -202,10 +257,10 @@ static int tbl_mask_array_realloc(struct flow_table *tbl, int size)
202257 if (ovsl_dereference (old -> masks [i ]))
203258 new -> masks [new -> count ++ ] = old -> masks [i ];
204259 }
260+ call_rcu (& old -> rcu , mask_array_rcu_cb );
205261 }
206262
207263 rcu_assign_pointer (tbl -> mask_array , new );
208- kfree_rcu (old , rcu );
209264
210265 return 0 ;
211266}
@@ -223,6 +278,11 @@ static int tbl_mask_array_add_mask(struct flow_table *tbl,
223278 return err ;
224279
225280 ma = ovsl_dereference (tbl -> mask_array );
281+ } else {
282+ /* On every add or delete we need to reset the counters so
283+ * every new mask gets a fair chance of being prioritized.
284+ */
285+ tbl_mask_array_reset_counters (ma );
226286 }
227287
228288 BUG_ON (ovsl_dereference (ma -> masks [ma_count ]));
@@ -260,6 +320,9 @@ static void tbl_mask_array_del_mask(struct flow_table *tbl,
260320 if (ma -> max >= (MASK_ARRAY_SIZE_MIN * 2 ) &&
261321 ma_count <= (ma -> max / 3 ))
262322 tbl_mask_array_realloc (tbl , ma -> max / 2 );
323+ else
324+ tbl_mask_array_reset_counters (ma );
325+
263326}
264327
265328/* Remove 'mask' from the mask list, if it is not needed any more. */
@@ -312,7 +375,7 @@ int ovs_flow_tbl_init(struct flow_table *table)
312375free_ti :
313376 __table_instance_destroy (ti );
314377free_mask_array :
315- kfree (ma );
378+ __mask_array_destroy (ma );
316379free_mask_cache :
317380 free_percpu (table -> mask_cache );
318381 return - ENOMEM ;
@@ -392,7 +455,7 @@ void ovs_flow_tbl_destroy(struct flow_table *table)
392455 struct table_instance * ufid_ti = rcu_dereference_raw (table -> ufid_ti );
393456
394457 free_percpu (table -> mask_cache );
395- kfree_rcu ( rcu_dereference_raw ( table -> mask_array ), rcu );
458+ call_rcu ( & table -> mask_array -> rcu , mask_array_rcu_cb );
396459 table_instance_destroy (table , ti , ufid_ti , false);
397460}
398461
@@ -606,6 +669,7 @@ static struct sw_flow *flow_lookup(struct flow_table *tbl,
606669 u32 * n_mask_hit ,
607670 u32 * index )
608671{
672+ u64 * usage_counters = this_cpu_ptr (ma -> masks_usage_cntr );
609673 struct sw_flow * flow ;
610674 struct sw_flow_mask * mask ;
611675 int i ;
@@ -614,8 +678,12 @@ static struct sw_flow *flow_lookup(struct flow_table *tbl,
614678 mask = rcu_dereference_ovsl (ma -> masks [* index ]);
615679 if (mask ) {
616680 flow = masked_flow_lookup (ti , key , mask , n_mask_hit );
617- if (flow )
681+ if (flow ) {
682+ u64_stats_update_begin (& ma -> syncp );
683+ usage_counters [* index ]++ ;
684+ u64_stats_update_end (& ma -> syncp );
618685 return flow ;
686+ }
619687 }
620688 }
621689
@@ -631,6 +699,9 @@ static struct sw_flow *flow_lookup(struct flow_table *tbl,
631699 flow = masked_flow_lookup (ti , key , mask , n_mask_hit );
632700 if (flow ) { /* Found */
633701 * index = i ;
702+ u64_stats_update_begin (& ma -> syncp );
703+ usage_counters [* index ]++ ;
704+ u64_stats_update_end (& ma -> syncp );
634705 return flow ;
635706 }
636707 }
@@ -934,6 +1005,98 @@ int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow,
9341005 return 0 ;
9351006}
9361007
1008+ static int compare_mask_and_count (const void * a , const void * b )
1009+ {
1010+ const struct mask_count * mc_a = a ;
1011+ const struct mask_count * mc_b = b ;
1012+
1013+ return (s64 )mc_b -> counter - (s64 )mc_a -> counter ;
1014+ }
1015+
1016+ /* Must be called with OVS mutex held. */
1017+ void ovs_flow_masks_rebalance (struct flow_table * table )
1018+ {
1019+ struct mask_array * ma = rcu_dereference_ovsl (table -> mask_array );
1020+ struct mask_count * masks_and_count ;
1021+ struct mask_array * new ;
1022+ int masks_entries = 0 ;
1023+ int i ;
1024+
1025+ /* Build array of all current entries with use counters. */
1026+ masks_and_count = kmalloc_array (ma -> max , sizeof (* masks_and_count ),
1027+ GFP_KERNEL );
1028+ if (!masks_and_count )
1029+ return ;
1030+
1031+ for (i = 0 ; i < ma -> max ; i ++ ) {
1032+ struct sw_flow_mask * mask ;
1033+ unsigned int start ;
1034+ int cpu ;
1035+
1036+ mask = rcu_dereference_ovsl (ma -> masks [i ]);
1037+ if (unlikely (!mask ))
1038+ break ;
1039+
1040+ masks_and_count [i ].index = i ;
1041+ masks_and_count [i ].counter = 0 ;
1042+
1043+ for_each_possible_cpu (cpu ) {
1044+ u64 * usage_counters = per_cpu_ptr (ma -> masks_usage_cntr ,
1045+ cpu );
1046+ u64 counter ;
1047+
1048+ do {
1049+ start = u64_stats_fetch_begin_irq (& ma -> syncp );
1050+ counter = usage_counters [i ];
1051+ } while (u64_stats_fetch_retry_irq (& ma -> syncp , start ));
1052+
1053+ masks_and_count [i ].counter += counter ;
1054+ }
1055+
1056+ /* Subtract the zero count value. */
1057+ masks_and_count [i ].counter -= ma -> masks_usage_zero_cntr [i ];
1058+
1059+ /* Rather than calling tbl_mask_array_reset_counters()
1060+ * below when no change is needed, do it inline here.
1061+ */
1062+ ma -> masks_usage_zero_cntr [i ] += masks_and_count [i ].counter ;
1063+ }
1064+
1065+ if (i == 0 )
1066+ goto free_mask_entries ;
1067+
1068+ /* Sort the entries */
1069+ masks_entries = i ;
1070+ sort (masks_and_count , masks_entries , sizeof (* masks_and_count ),
1071+ compare_mask_and_count , NULL );
1072+
1073+ /* If the order is the same, nothing to do... */
1074+ for (i = 0 ; i < masks_entries ; i ++ ) {
1075+ if (i != masks_and_count [i ].index )
1076+ break ;
1077+ }
1078+ if (i == masks_entries )
1079+ goto free_mask_entries ;
1080+
1081+ /* Rebuilt the new list in order of usage. */
1082+ new = tbl_mask_array_alloc (ma -> max );
1083+ if (!new )
1084+ goto free_mask_entries ;
1085+
1086+ for (i = 0 ; i < masks_entries ; i ++ ) {
1087+ int index = masks_and_count [i ].index ;
1088+
1089+ new -> masks [new -> count ++ ] =
1090+ rcu_dereference_ovsl (ma -> masks [index ]);
1091+ }
1092+
1093+ rcu_assign_pointer (table -> mask_array , new );
1094+ call_rcu (& ma -> rcu , mask_array_rcu_cb );
1095+
1096+ free_mask_entries :
1097+ kfree (masks_and_count );
1098+ }
1099+
9371100/* Initializes the flow module.
9381101 * Returns zero if successful or a negative error code. */
9391102int ovs_flow_init (void )
0 commit comments