Skip to content

Commit 04b7d13

Browse files
hao022davem330
authored andcommitted
net: openvswitch: add flow-mask cache for performance
The idea of this optimization comes from a patch which is committed in 2014, openvswitch community. The author is Pravin B Shelar. In order to get high performance, I implement it again. Later patches will use it. Pravin B Shelar, says: | On every packet OVS needs to lookup flow-table with every | mask until it finds a match. The packet flow-key is first | masked with mask in the list and then the masked key is | looked up in flow-table. Therefore number of masks can | affect packet processing performance. Link: openvswitch/ovs@5604935 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 ae8a76f commit 04b7d13

3 files changed

Lines changed: 107 additions & 16 deletions

File tree

‎net/openvswitch/datapath.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,8 @@ void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
227227
stats = this_cpu_ptr(dp->stats_percpu);
228228

229229
/* Look up flow. */
230-
flow = ovs_flow_tbl_lookup_stats(&dp->table, key, &n_mask_hit);
230+
flow = ovs_flow_tbl_lookup_stats(&dp->table, key, skb_get_hash(skb),
231+
&n_mask_hit);
231232
if (unlikely(!flow)) {
232233
struct dp_upcall_info upcall;
233234

‎net/openvswitch/flow_table.c‎

Lines changed: 96 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
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+
3943
static struct kmem_cache *flow_cache;
4044
struct 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

188197
free_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

426438
static 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+
464545
struct 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

472554
struct 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++;

‎net/openvswitch/flow_table.h‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222

2323
#include "flow.h"
2424

25+
struct mask_cache_entry {
26+
u32 skb_hash;
27+
u32 mask_index;
28+
};
29+
2530
struct table_instance {
2631
struct hlist_head *buckets;
2732
unsigned int n_buckets;
@@ -34,6 +39,7 @@ struct table_instance {
3439
struct flow_table {
3540
struct table_instance __rcu *ti;
3641
struct table_instance __rcu *ufid_ti;
42+
struct mask_cache_entry __percpu *mask_cache;
3743
struct list_head mask_list;
3844
unsigned long last_rehash;
3945
unsigned int count;
@@ -60,8 +66,9 @@ int ovs_flow_tbl_num_masks(const struct flow_table *table);
6066
struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *table,
6167
u32 *bucket, u32 *idx);
6268
struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *,
63-
const struct sw_flow_key *,
64-
u32 *n_mask_hit);
69+
const struct sw_flow_key *,
70+
u32 skb_hash,
71+
u32 *n_mask_hit);
6572
struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *,
6673
const struct sw_flow_key *);
6774
struct sw_flow *ovs_flow_tbl_lookup_exact(struct flow_table *tbl,

0 commit comments

Comments
 (0)