Skip to content

Commit 4bc63b1

Browse files
hao022davem330
authored andcommitted
net: openvswitch: convert mask list in mask array
Port the codes to linux upstream and with little changes. Pravin B Shelar, says: | mask caches index of mask in mask_list. On packet recv OVS | need to traverse mask-list to get cached mask. Therefore array | is better for retrieving cached mask. This also allows better | cache replacement algorithm by directly checking mask's existence. Link: openvswitch/ovs@d49fc3f Signed-off-by: Tonghao Zhang <xiangxia.m.yue@gmail.com> Tested-by: Greg Rose <gvrose8192@gmail.com> Acked-by: William Tu <u9012063@gmail.com> Signed-off-by: Pravin B Shelar <pshelar@ovn.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 04b7d13 commit 4bc63b1

3 files changed

Lines changed: 167 additions & 51 deletions

File tree

‎net/openvswitch/flow.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ struct sw_flow_key_range {
166166
struct sw_flow_mask {
167167
int ref_count;
168168
struct rcu_head rcu;
169-
struct list_head list;
170169
struct sw_flow_key_range range;
171170
struct sw_flow_key key;
172171
};

‎net/openvswitch/flow_table.c‎

Lines changed: 160 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <net/ndisc.h>
3535

3636
#define TBL_MIN_BUCKETS 1024
37+
#define MASK_ARRAY_SIZE_MIN 16
3738
#define REHASH_INTERVAL (10 * 60 * HZ)
3839

3940
#define MC_HASH_SHIFT 8
@@ -168,34 +169,82 @@ static struct table_instance *table_instance_alloc(int new_size)
168169
return ti;
169170
}
170171

172+
static struct mask_array *tbl_mask_array_alloc(int size)
173+
{
174+
struct mask_array *new;
175+
176+
size = max(MASK_ARRAY_SIZE_MIN, size);
177+
new = kzalloc(sizeof(struct mask_array) +
178+
sizeof(struct sw_flow_mask *) * size, GFP_KERNEL);
179+
if (!new)
180+
return NULL;
181+
182+
new->count = 0;
183+
new->max = size;
184+
185+
return new;
186+
}
187+
188+
static int tbl_mask_array_realloc(struct flow_table *tbl, int size)
189+
{
190+
struct mask_array *old;
191+
struct mask_array *new;
192+
193+
new = tbl_mask_array_alloc(size);
194+
if (!new)
195+
return -ENOMEM;
196+
197+
old = ovsl_dereference(tbl->mask_array);
198+
if (old) {
199+
int i;
200+
201+
for (i = 0; i < old->max; i++) {
202+
if (ovsl_dereference(old->masks[i]))
203+
new->masks[new->count++] = old->masks[i];
204+
}
205+
}
206+
207+
rcu_assign_pointer(tbl->mask_array, new);
208+
kfree_rcu(old, rcu);
209+
210+
return 0;
211+
}
212+
171213
int ovs_flow_tbl_init(struct flow_table *table)
172214
{
173215
struct table_instance *ti, *ufid_ti;
216+
struct mask_array *ma;
174217

175218
table->mask_cache = __alloc_percpu(sizeof(struct mask_cache_entry) *
176219
MC_HASH_ENTRIES,
177220
__alignof__(struct mask_cache_entry));
178221
if (!table->mask_cache)
179222
return -ENOMEM;
180223

224+
ma = tbl_mask_array_alloc(MASK_ARRAY_SIZE_MIN);
225+
if (!ma)
226+
goto free_mask_cache;
227+
181228
ti = table_instance_alloc(TBL_MIN_BUCKETS);
182229
if (!ti)
183-
goto free_mask_cache;
230+
goto free_mask_array;
184231

185232
ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS);
186233
if (!ufid_ti)
187234
goto free_ti;
188235

189236
rcu_assign_pointer(table->ti, ti);
190237
rcu_assign_pointer(table->ufid_ti, ufid_ti);
191-
INIT_LIST_HEAD(&table->mask_list);
238+
rcu_assign_pointer(table->mask_array, ma);
192239
table->last_rehash = jiffies;
193240
table->count = 0;
194241
table->ufid_count = 0;
195242
return 0;
196243

197244
free_ti:
198245
__table_instance_destroy(ti);
246+
free_mask_array:
247+
kfree(ma);
199248
free_mask_cache:
200249
free_percpu(table->mask_cache);
201250
return -ENOMEM;
@@ -255,6 +304,7 @@ void ovs_flow_tbl_destroy(struct flow_table *table)
255304
struct table_instance *ufid_ti = rcu_dereference_raw(table->ufid_ti);
256305

257306
free_percpu(table->mask_cache);
307+
kfree_rcu(rcu_dereference_raw(table->mask_array), rcu);
258308
table_instance_destroy(ti, ufid_ti, false);
259309
}
260310

@@ -460,17 +510,27 @@ static struct sw_flow *masked_flow_lookup(struct table_instance *ti,
460510

461511
static struct sw_flow *flow_lookup(struct flow_table *tbl,
462512
struct table_instance *ti,
513+
struct mask_array *ma,
463514
const struct sw_flow_key *key,
464-
u32 *n_mask_hit)
515+
u32 *n_mask_hit,
516+
u32 *index)
465517
{
466-
struct sw_flow_mask *mask;
467518
struct sw_flow *flow;
519+
int i;
468520

469-
list_for_each_entry_rcu(mask, &tbl->mask_list, list) {
470-
flow = masked_flow_lookup(ti, key, mask, n_mask_hit);
471-
if (flow) /* Found */
472-
return flow;
521+
for (i = 0; i < ma->max; i++) {
522+
struct sw_flow_mask *mask;
523+
524+
mask = rcu_dereference_ovsl(ma->masks[i]);
525+
if (mask) {
526+
flow = masked_flow_lookup(ti, key, mask, n_mask_hit);
527+
if (flow) { /* Found */
528+
*index = i;
529+
return flow;
530+
}
531+
}
473532
}
533+
474534
return NULL;
475535
}
476536

@@ -486,15 +546,19 @@ struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
486546
u32 skb_hash,
487547
u32 *n_mask_hit)
488548
{
549+
struct mask_array *ma = rcu_dereference_ovsl(tbl->mask_array);
489550
struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
490551
struct mask_cache_entry *entries, *ce, *del;
491552
struct sw_flow *flow;
492553
u32 hash = skb_hash;
493554
int seg;
494555

495556
*n_mask_hit = 0;
496-
if (unlikely(!skb_hash))
497-
return flow_lookup(tbl, ti, key, n_mask_hit);
557+
if (unlikely(!skb_hash)) {
558+
u32 __always_unused mask_index;
559+
560+
return flow_lookup(tbl, ti, ma, key, n_mask_hit, &mask_index);
561+
}
498562

499563
del = NULL;
500564
entries = this_cpu_ptr(tbl->mask_cache);
@@ -507,37 +571,33 @@ struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
507571

508572
if (ce->skb_hash == skb_hash) {
509573
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-
}
574+
struct sw_flow *flow;
575+
576+
mask = rcu_dereference_ovsl(ma->masks[ce->mask_index]);
577+
if (mask) {
578+
flow = masked_flow_lookup(ti, key, mask,
579+
n_mask_hit);
580+
if (flow) /* Found */
581+
return flow;
522582
}
523583

524584
del = ce;
525585
break;
526586
}
527587

528-
if (!del || (del->skb_hash && !ce->skb_hash)) {
588+
if (!del || (del->skb_hash && !ce->skb_hash) ||
589+
(rcu_dereference_ovsl(ma->masks[del->mask_index]) &&
590+
!rcu_dereference_ovsl(ma->masks[ce->mask_index]))) {
529591
del = ce;
530592
}
531593

532594
hash >>= MC_HASH_SHIFT;
533595
}
534596

535-
flow = flow_lookup(tbl, ti, key, n_mask_hit);
597+
flow = flow_lookup(tbl, ti, ma, key, n_mask_hit, &del->mask_index);
536598

537-
if (flow) {
599+
if (flow)
538600
del->skb_hash = skb_hash;
539-
del->mask_index = (*n_mask_hit - 1);
540-
}
541601

542602
return flow;
543603
}
@@ -546,26 +606,38 @@ struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl,
546606
const struct sw_flow_key *key)
547607
{
548608
struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
609+
struct mask_array *ma = rcu_dereference_ovsl(tbl->mask_array);
610+
549611
u32 __always_unused n_mask_hit;
612+
u32 __always_unused index;
550613

551-
return flow_lookup(tbl, ti, key, &n_mask_hit);
614+
return flow_lookup(tbl, ti, ma, key, &n_mask_hit, &index);
552615
}
553616

554617
struct sw_flow *ovs_flow_tbl_lookup_exact(struct flow_table *tbl,
555618
const struct sw_flow_match *match)
556619
{
557-
struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
558-
struct sw_flow_mask *mask;
559-
struct sw_flow *flow;
560-
u32 __always_unused n_mask_hit;
620+
struct mask_array *ma = ovsl_dereference(tbl->mask_array);
621+
int i;
561622

562623
/* Always called under ovs-mutex. */
563-
list_for_each_entry(mask, &tbl->mask_list, list) {
624+
for (i = 0; i < ma->max; i++) {
625+
struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
626+
u32 __always_unused n_mask_hit;
627+
struct sw_flow_mask *mask;
628+
struct sw_flow *flow;
629+
630+
mask = ovsl_dereference(ma->masks[i]);
631+
if (!mask)
632+
continue;
633+
564634
flow = masked_flow_lookup(ti, match->key, mask, &n_mask_hit);
565635
if (flow && ovs_identifier_is_key(&flow->id) &&
566-
ovs_flow_cmp_unmasked_key(flow, match))
636+
ovs_flow_cmp_unmasked_key(flow, match)) {
567637
return flow;
638+
}
568639
}
640+
569641
return NULL;
570642
}
571643

@@ -611,13 +683,9 @@ struct sw_flow *ovs_flow_tbl_lookup_ufid(struct flow_table *tbl,
611683

612684
int ovs_flow_tbl_num_masks(const struct flow_table *table)
613685
{
614-
struct sw_flow_mask *mask;
615-
int num = 0;
686+
struct mask_array *ma = rcu_dereference_ovsl(table->mask_array);
616687

617-
list_for_each_entry(mask, &table->mask_list, list)
618-
num++;
619-
620-
return num;
688+
return ma->count;
621689
}
622690

623691
static struct table_instance *table_instance_expand(struct table_instance *ti,
@@ -638,8 +706,19 @@ static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask)
638706
mask->ref_count--;
639707

640708
if (!mask->ref_count) {
641-
list_del_rcu(&mask->list);
642-
kfree_rcu(mask, rcu);
709+
struct mask_array *ma;
710+
int i;
711+
712+
ma = ovsl_dereference(tbl->mask_array);
713+
for (i = 0; i < ma->max; i++) {
714+
if (mask == ovsl_dereference(ma->masks[i])) {
715+
RCU_INIT_POINTER(ma->masks[i], NULL);
716+
ma->count--;
717+
kfree_rcu(mask, rcu);
718+
return;
719+
}
720+
}
721+
BUG();
643722
}
644723
}
645724
}
@@ -689,13 +768,16 @@ static bool mask_equal(const struct sw_flow_mask *a,
689768
static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl,
690769
const struct sw_flow_mask *mask)
691770
{
692-
struct list_head *ml;
771+
struct mask_array *ma;
772+
int i;
693773

694-
list_for_each(ml, &tbl->mask_list) {
695-
struct sw_flow_mask *m;
696-
m = container_of(ml, struct sw_flow_mask, list);
697-
if (mask_equal(mask, m))
698-
return m;
774+
ma = ovsl_dereference(tbl->mask_array);
775+
for (i = 0; i < ma->max; i++) {
776+
struct sw_flow_mask *t;
777+
t = ovsl_dereference(ma->masks[i]);
778+
779+
if (t && mask_equal(mask, t))
780+
return t;
699781
}
700782

701783
return NULL;
@@ -706,15 +788,44 @@ static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow,
706788
const struct sw_flow_mask *new)
707789
{
708790
struct sw_flow_mask *mask;
791+
709792
mask = flow_mask_find(tbl, new);
710793
if (!mask) {
794+
struct mask_array *ma;
795+
int i;
796+
711797
/* Allocate a new mask if none exsits. */
712798
mask = mask_alloc();
713799
if (!mask)
714800
return -ENOMEM;
715801
mask->key = new->key;
716802
mask->range = new->range;
717-
list_add_tail_rcu(&mask->list, &tbl->mask_list);
803+
804+
/* Add mask to mask-list. */
805+
ma = ovsl_dereference(tbl->mask_array);
806+
if (ma->count >= ma->max) {
807+
int err;
808+
809+
err = tbl_mask_array_realloc(tbl, ma->max +
810+
MASK_ARRAY_SIZE_MIN);
811+
if (err) {
812+
kfree(mask);
813+
return err;
814+
}
815+
816+
ma = ovsl_dereference(tbl->mask_array);
817+
}
818+
819+
for (i = 0; i < ma->max; i++) {
820+
const struct sw_flow_mask *t;
821+
822+
t = ovsl_dereference(ma->masks[i]);
823+
if (!t) {
824+
rcu_assign_pointer(ma->masks[i], mask);
825+
ma->count++;
826+
break;
827+
}
828+
}
718829
} else {
719830
BUG_ON(!mask->ref_count);
720831
mask->ref_count++;

‎net/openvswitch/flow_table.h‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ struct mask_cache_entry {
2727
u32 mask_index;
2828
};
2929

30+
struct mask_array {
31+
struct rcu_head rcu;
32+
int count, max;
33+
struct sw_flow_mask __rcu *masks[];
34+
};
35+
3036
struct table_instance {
3137
struct hlist_head *buckets;
3238
unsigned int n_buckets;
@@ -40,7 +46,7 @@ struct flow_table {
4046
struct table_instance __rcu *ti;
4147
struct table_instance __rcu *ufid_ti;
4248
struct mask_cache_entry __percpu *mask_cache;
43-
struct list_head mask_list;
49+
struct mask_array __rcu *mask_array;
4450
unsigned long last_rehash;
4551
unsigned int count;
4652
unsigned int ufid_count;

0 commit comments

Comments
 (0)