Skip to content

Commit eac87c4

Browse files
chaudrondavem330
authored andcommitted
net: openvswitch: reorder masks array based on usage
This patch reorders the masks array every 4 seconds based on their usage count. This greatly reduces the masks per packet hit, and hence the overall performance. Especially in the OVS/OVN case for OpenShift. Here are some results from the OVS/OVN OpenShift test, which use 8 pods, each pod having 512 uperf connections, each connection sends a 64-byte request and gets a 1024-byte response (TCP). All uperf clients are on 1 worker node while all uperf servers are on the other worker node. Kernel without this patch : 7.71 Gbps Kernel with this patch applied: 14.52 Gbps We also run some tests to verify the rebalance activity does not lower the flow insertion rate, which does not. Signed-off-by: Eelco Chaudron <echaudro@redhat.com> Tested-by: Andrew Theurer <atheurer@redhat.com> Reviewed-by: Paolo Abeni <pabeni@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent b18432c commit eac87c4

4 files changed

Lines changed: 207 additions & 7 deletions

File tree

‎net/openvswitch/datapath.c‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
130130
const struct dp_upcall_info *,
131131
uint32_t cutlen);
132132

133+
static void ovs_dp_masks_rebalance(struct work_struct *work);
134+
133135
/* Must be called with rcu_read_lock or ovs_mutex. */
134136
const char *ovs_dp_name(const struct datapath *dp)
135137
{
@@ -1653,6 +1655,7 @@ static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
16531655
goto err_destroy_reply;
16541656

16551657
ovs_dp_set_net(dp, sock_net(skb->sk));
1658+
INIT_DELAYED_WORK(&dp->masks_rebalance, ovs_dp_masks_rebalance);
16561659

16571660
/* Allocate table. */
16581661
err = ovs_flow_tbl_init(&dp->table);
@@ -1712,6 +1715,9 @@ static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
17121715
ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
17131716
list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
17141717

1718+
schedule_delayed_work(&dp->masks_rebalance,
1719+
msecs_to_jiffies(DP_MASKS_REBALANCE_INTERVAL));
1720+
17151721
ovs_unlock();
17161722

17171723
ovs_notify(&dp_datapath_genl_family, reply, info);
@@ -1756,6 +1762,9 @@ static void __dp_destroy(struct datapath *dp)
17561762

17571763
/* RCU destroy the flow table */
17581764
call_rcu(&dp->rcu, destroy_dp_rcu);
1765+
1766+
/* Cancel remaining work. */
1767+
cancel_delayed_work_sync(&dp->masks_rebalance);
17591768
}
17601769

17611770
static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
@@ -2338,6 +2347,19 @@ static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
23382347
return skb->len;
23392348
}
23402349

2350+
static void ovs_dp_masks_rebalance(struct work_struct *work)
2351+
{
2352+
struct datapath *dp = container_of(work, struct datapath,
2353+
masks_rebalance.work);
2354+
2355+
ovs_lock();
2356+
ovs_flow_masks_rebalance(&dp->table);
2357+
ovs_unlock();
2358+
2359+
schedule_delayed_work(&dp->masks_rebalance,
2360+
msecs_to_jiffies(DP_MASKS_REBALANCE_INTERVAL));
2361+
}
2362+
23412363
static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
23422364
[OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
23432365
[OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },

‎net/openvswitch/datapath.h‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
#include "meter.h"
2121
#include "vport-internal_dev.h"
2222

23-
#define DP_MAX_PORTS USHRT_MAX
24-
#define DP_VPORT_HASH_BUCKETS 1024
23+
#define DP_MAX_PORTS USHRT_MAX
24+
#define DP_VPORT_HASH_BUCKETS 1024
25+
#define DP_MASKS_REBALANCE_INTERVAL 4000
2526

2627
/**
2728
* struct dp_stats_percpu - per-cpu packet processing statistics for a given
@@ -83,6 +84,9 @@ struct datapath {
8384

8485
/* Switch meters. */
8586
struct dp_meter_table meter_tbl;
87+
88+
/* re-balance flow masks timer */
89+
struct delayed_work masks_rebalance;
8690
};
8791

8892
/**

‎net/openvswitch/flow_table.c‎

Lines changed: 168 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
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+
172214
static 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)
312375
free_ti:
313376
__table_instance_destroy(ti);
314377
free_mask_array:
315-
kfree(ma);
378+
__mask_array_destroy(ma);
316379
free_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. */
9391102
int ovs_flow_init(void)

‎net/openvswitch/flow_table.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,17 @@ struct mask_cache_entry {
2727
u32 mask_index;
2828
};
2929

30+
struct mask_count {
31+
int index;
32+
u64 counter;
33+
};
34+
3035
struct mask_array {
3136
struct rcu_head rcu;
3237
int count, max;
38+
u64 __percpu *masks_usage_cntr;
39+
u64 *masks_usage_zero_cntr;
40+
struct u64_stats_sync syncp;
3341
struct sw_flow_mask __rcu *masks[];
3442
};
3543

@@ -86,4 +94,7 @@ bool ovs_flow_cmp(const struct sw_flow *, const struct sw_flow_match *);
8694

8795
void ovs_flow_mask_key(struct sw_flow_key *dst, const struct sw_flow_key *src,
8896
bool full, const struct sw_flow_mask *mask);
97+
98+
void ovs_flow_masks_rebalance(struct flow_table *table);
99+
89100
#endif /* flow_table.h */

0 commit comments

Comments
 (0)