Skip to content

Commit 57f7d7b

Browse files
hao022davem330
authored andcommitted
net: openvswitch: optimize flow-mask looking up
The full looking up on flow table traverses all mask array. If mask-array is too large, the number of invalid flow-mask increase, performance will be drop. One bad case, for example: M means flow-mask is valid and NULL of flow-mask means deleted. +-------------------------------------------+ | M | NULL | ... | NULL | M| +-------------------------------------------+ In that case, without this patch, openvswitch will traverses all mask array, because there will be one flow-mask in the tail. This patch changes the way of flow-mask inserting and deleting, and the mask array will be keep as below: there is not a NULL hole. In the fast path, we can "break" "for" (not "continue") in flow_lookup when we get a NULL flow-mask. "break" v +-------------------------------------------+ | M | M | NULL |... | NULL | NULL| +-------------------------------------------+ This patch don't optimize slow or control path, still using ma->max to traverse. Slow path: * tbl_mask_array_realloc * ovs_flow_tbl_lookup_exact * flow_mask_find Signed-off-by: Tonghao Zhang <xiangxia.m.yue@gmail.com> Tested-by: Greg Rose <gvrose8192@gmail.com> Acked-by: Pravin B Shelar <pshelar@ovn.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent a7f35e7 commit 57f7d7b

1 file changed

Lines changed: 53 additions & 51 deletions

File tree

‎net/openvswitch/flow_table.c‎

Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -518,8 +518,8 @@ static struct sw_flow *flow_lookup(struct flow_table *tbl,
518518
u32 *n_mask_hit,
519519
u32 *index)
520520
{
521-
struct sw_flow_mask *mask;
522521
struct sw_flow *flow;
522+
struct sw_flow_mask *mask;
523523
int i;
524524

525525
if (*index < ma->max) {
@@ -538,7 +538,7 @@ static struct sw_flow *flow_lookup(struct flow_table *tbl,
538538

539539
mask = rcu_dereference_ovsl(ma->masks[i]);
540540
if (!mask)
541-
continue;
541+
break;
542542

543543
flow = masked_flow_lookup(ti, key, mask, n_mask_hit);
544544
if (flow) { /* Found */
@@ -695,8 +695,7 @@ struct sw_flow *ovs_flow_tbl_lookup_ufid(struct flow_table *tbl,
695695
int ovs_flow_tbl_num_masks(const struct flow_table *table)
696696
{
697697
struct mask_array *ma = rcu_dereference_ovsl(table->mask_array);
698-
699-
return ma->count;
698+
return READ_ONCE(ma->count);
700699
}
701700

702701
static struct table_instance *table_instance_expand(struct table_instance *ti,
@@ -705,21 +704,33 @@ static struct table_instance *table_instance_expand(struct table_instance *ti,
705704
return table_instance_rehash(ti, ti->n_buckets * 2, ufid);
706705
}
707706

708-
static void tbl_mask_array_delete_mask(struct mask_array *ma,
709-
struct sw_flow_mask *mask)
707+
static void tbl_mask_array_del_mask(struct flow_table *tbl,
708+
struct sw_flow_mask *mask)
710709
{
711-
int i;
710+
struct mask_array *ma = ovsl_dereference(tbl->mask_array);
711+
int i, ma_count = READ_ONCE(ma->count);
712712

713713
/* Remove the deleted mask pointers from the array */
714-
for (i = 0; i < ma->max; i++) {
715-
if (mask == ovsl_dereference(ma->masks[i])) {
716-
RCU_INIT_POINTER(ma->masks[i], NULL);
717-
ma->count--;
718-
kfree_rcu(mask, rcu);
719-
return;
720-
}
714+
for (i = 0; i < ma_count; i++) {
715+
if (mask == ovsl_dereference(ma->masks[i]))
716+
goto found;
721717
}
718+
722719
BUG();
720+
return;
721+
722+
found:
723+
WRITE_ONCE(ma->count, ma_count -1);
724+
725+
rcu_assign_pointer(ma->masks[i], ma->masks[ma_count -1]);
726+
RCU_INIT_POINTER(ma->masks[ma_count -1], NULL);
727+
728+
kfree_rcu(mask, rcu);
729+
730+
/* Shrink the mask array if necessary. */
731+
if (ma->max >= (MASK_ARRAY_SIZE_MIN * 2) &&
732+
ma_count <= (ma->max / 3))
733+
tbl_mask_array_realloc(tbl, ma->max / 2);
723734
}
724735

725736
/* Remove 'mask' from the mask list, if it is not needed any more. */
@@ -733,17 +744,8 @@ static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask)
733744
BUG_ON(!mask->ref_count);
734745
mask->ref_count--;
735746

736-
if (!mask->ref_count) {
737-
struct mask_array *ma;
738-
739-
ma = ovsl_dereference(tbl->mask_array);
740-
tbl_mask_array_delete_mask(ma, mask);
741-
742-
/* Shrink the mask array if necessary. */
743-
if (ma->max >= (MASK_ARRAY_SIZE_MIN * 2) &&
744-
ma->count <= (ma->max / 3))
745-
tbl_mask_array_realloc(tbl, ma->max / 2);
746-
}
747+
if (!mask->ref_count)
748+
tbl_mask_array_del_mask(tbl, mask);
747749
}
748750
}
749751

@@ -807,6 +809,29 @@ static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl,
807809
return NULL;
808810
}
809811

812+
static int tbl_mask_array_add_mask(struct flow_table *tbl,
813+
struct sw_flow_mask *new)
814+
{
815+
struct mask_array *ma = ovsl_dereference(tbl->mask_array);
816+
int err, ma_count = READ_ONCE(ma->count);
817+
818+
if (ma_count >= ma->max) {
819+
err = tbl_mask_array_realloc(tbl, ma->max +
820+
MASK_ARRAY_SIZE_MIN);
821+
if (err)
822+
return err;
823+
824+
ma = ovsl_dereference(tbl->mask_array);
825+
}
826+
827+
BUG_ON(ovsl_dereference(ma->masks[ma_count]));
828+
829+
rcu_assign_pointer(ma->masks[ma_count], new);
830+
WRITE_ONCE(ma->count, ma_count +1);
831+
832+
return 0;
833+
}
834+
810835
/* Add 'mask' into the mask list, if it is not already there. */
811836
static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow,
812837
const struct sw_flow_mask *new)
@@ -815,9 +840,6 @@ static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow,
815840

816841
mask = flow_mask_find(tbl, new);
817842
if (!mask) {
818-
struct mask_array *ma;
819-
int i;
820-
821843
/* Allocate a new mask if none exsits. */
822844
mask = mask_alloc();
823845
if (!mask)
@@ -826,29 +848,9 @@ static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow,
826848
mask->range = new->range;
827849

828850
/* Add mask to mask-list. */
829-
ma = ovsl_dereference(tbl->mask_array);
830-
if (ma->count >= ma->max) {
831-
int err;
832-
833-
err = tbl_mask_array_realloc(tbl, ma->max +
834-
MASK_ARRAY_SIZE_MIN);
835-
if (err) {
836-
kfree(mask);
837-
return err;
838-
}
839-
840-
ma = ovsl_dereference(tbl->mask_array);
841-
}
842-
843-
for (i = 0; i < ma->max; i++) {
844-
const struct sw_flow_mask *t;
845-
846-
t = ovsl_dereference(ma->masks[i]);
847-
if (!t) {
848-
rcu_assign_pointer(ma->masks[i], mask);
849-
ma->count++;
850-
break;
851-
}
851+
if (tbl_mask_array_add_mask(tbl, mask)) {
852+
kfree(mask);
853+
return -ENOMEM;
852854
}
853855
} else {
854856
BUG_ON(!mask->ref_count);

0 commit comments

Comments
 (0)