Skip to content

Commit a7f35e7

Browse files
hao022davem330
authored andcommitted
net: openvswitch: optimize flow mask cache hash collision
Port the codes to linux upstream and with little changes. Pravin B Shelar, says: | In case hash collision on mask cache, OVS does extra flow | lookup. Following patch avoid it. Link: openvswitch/ovs@0e6efbe Signed-off-by: Tonghao Zhang <xiangxia.m.yue@gmail.com> Tested-by: Greg Rose <gvrose8192@gmail.com> Signed-off-by: Pravin B Shelar <pshelar@ovn.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 1689754 commit a7f35e7

1 file changed

Lines changed: 53 additions & 42 deletions

File tree

‎net/openvswitch/flow_table.c‎

Lines changed: 53 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -508,26 +508,42 @@ static struct sw_flow *masked_flow_lookup(struct table_instance *ti,
508508
return NULL;
509509
}
510510

511+
/* Flow lookup does full lookup on flow table. It starts with
512+
* mask from index passed in *index.
513+
*/
511514
static struct sw_flow *flow_lookup(struct flow_table *tbl,
512515
struct table_instance *ti,
513516
struct mask_array *ma,
514517
const struct sw_flow_key *key,
515518
u32 *n_mask_hit,
516519
u32 *index)
517520
{
521+
struct sw_flow_mask *mask;
518522
struct sw_flow *flow;
519523
int i;
520524

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 (*index < ma->max) {
526+
mask = rcu_dereference_ovsl(ma->masks[*index]);
525527
if (mask) {
526528
flow = masked_flow_lookup(ti, key, mask, n_mask_hit);
527-
if (flow) { /* Found */
528-
*index = i;
529+
if (flow)
529530
return flow;
530-
}
531+
}
532+
}
533+
534+
for (i = 0; i < ma->max; i++) {
535+
536+
if (i == *index)
537+
continue;
538+
539+
mask = rcu_dereference_ovsl(ma->masks[i]);
540+
if (!mask)
541+
continue;
542+
543+
flow = masked_flow_lookup(ti, key, mask, n_mask_hit);
544+
if (flow) { /* Found */
545+
*index = i;
546+
return flow;
531547
}
532548
}
533549

@@ -546,58 +562,54 @@ struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
546562
u32 skb_hash,
547563
u32 *n_mask_hit)
548564
{
549-
struct mask_array *ma = rcu_dereference_ovsl(tbl->mask_array);
550-
struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
551-
struct mask_cache_entry *entries, *ce, *del;
565+
struct mask_array *ma = rcu_dereference(tbl->mask_array);
566+
struct table_instance *ti = rcu_dereference(tbl->ti);
567+
struct mask_cache_entry *entries, *ce;
552568
struct sw_flow *flow;
553-
u32 hash = skb_hash;
569+
u32 hash;
554570
int seg;
555571

556572
*n_mask_hit = 0;
557573
if (unlikely(!skb_hash)) {
558-
u32 __always_unused mask_index;
574+
u32 mask_index = 0;
559575

560576
return flow_lookup(tbl, ti, ma, key, n_mask_hit, &mask_index);
561577
}
562578

563-
del = NULL;
579+
/* Pre and post recirulation flows usually have the same skb_hash
580+
* value. To avoid hash collisions, rehash the 'skb_hash' with
581+
* 'recirc_id'. */
582+
if (key->recirc_id)
583+
skb_hash = jhash_1word(skb_hash, key->recirc_id);
584+
585+
ce = NULL;
586+
hash = skb_hash;
564587
entries = this_cpu_ptr(tbl->mask_cache);
565588

589+
/* Find the cache entry 'ce' to operate on. */
566590
for (seg = 0; seg < MC_HASH_SEGS; seg++) {
567-
int index;
568-
569-
index = hash & (MC_HASH_ENTRIES - 1);
570-
ce = &entries[index];
571-
572-
if (ce->skb_hash == skb_hash) {
573-
struct sw_flow_mask *mask;
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;
582-
}
583-
584-
del = ce;
585-
break;
591+
int index = hash & (MC_HASH_ENTRIES - 1);
592+
struct mask_cache_entry *e;
593+
594+
e = &entries[index];
595+
if (e->skb_hash == skb_hash) {
596+
flow = flow_lookup(tbl, ti, ma, key, n_mask_hit,
597+
&e->mask_index);
598+
if (!flow)
599+
e->skb_hash = 0;
600+
return flow;
586601
}
587602

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]))) {
591-
del = ce;
592-
}
603+
if (!ce || e->skb_hash < ce->skb_hash)
604+
ce = e; /* A better replacement cache candidate. */
593605

594606
hash >>= MC_HASH_SHIFT;
595607
}
596608

597-
flow = flow_lookup(tbl, ti, ma, key, n_mask_hit, &del->mask_index);
598-
609+
/* Cache miss, do full lookup. */
610+
flow = flow_lookup(tbl, ti, ma, key, n_mask_hit, &ce->mask_index);
599611
if (flow)
600-
del->skb_hash = skb_hash;
612+
ce->skb_hash = skb_hash;
601613

602614
return flow;
603615
}
@@ -607,9 +619,8 @@ struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl,
607619
{
608620
struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
609621
struct mask_array *ma = rcu_dereference_ovsl(tbl->mask_array);
610-
611622
u32 __always_unused n_mask_hit;
612-
u32 __always_unused index;
623+
u32 index = 0;
613624

614625
return flow_lookup(tbl, ti, ma, key, &n_mask_hit, &index);
615626
}

0 commit comments

Comments
 (0)