Skip to content

Commit c4b2bf6

Browse files
hao022davem330
authored andcommitted
openvswitch: Optimize operations for OvS flow_stats.
When calling the flow_free() to free the flow, we call many times (cpu_possible_mask, eg. 128 as default) cpumask_next(). That will take up our CPU usage if we call the flow_free() frequently. When we put all packets to userspace via upcall, and OvS will send them back via netlink to ovs_packet_cmd_execute(will call flow_free). The test topo is shown as below. VM01 sends TCP packets to VM02, and OvS forward packtets. When testing, we use perf to report the system performance. VM01 --- OvS-VM --- VM02 Without this patch, perf-top show as below: The flow_free() is 3.02% CPU usage. 4.23% [kernel] [k] _raw_spin_unlock_irqrestore 3.62% [kernel] [k] __do_softirq 3.16% [kernel] [k] __memcpy 3.02% [kernel] [k] flow_free 2.42% libc-2.17.so [.] __memcpy_ssse3_back 2.18% [kernel] [k] copy_user_generic_unrolled 2.17% [kernel] [k] find_next_bit When applied this patch, perf-top show as below: Not shown on the list anymore. 4.11% [kernel] [k] _raw_spin_unlock_irqrestore 3.79% [kernel] [k] __do_softirq 3.46% [kernel] [k] __memcpy 2.73% libc-2.17.so [.] __memcpy_ssse3_back 2.25% [kernel] [k] copy_user_generic_unrolled 1.89% libc-2.17.so [.] _int_malloc 1.53% ovs-vswitchd [.] xlate_actions With this patch, the TCP throughput(we dont use Megaflow Cache + Microflow Cache) between VMs is 1.18Gbs/sec up to 1.30Gbs/sec (maybe ~10% performance imporve). This patch adds cpumask struct, the cpu_used_mask stores the cpu_id that the flow used. And we only check the flow_stats on the cpu we used, and it is unncessary to check all possible cpu when getting, cleaning, and updating the flow_stats. Adding the cpu_used_mask to sw_flow struct does’t increase the cacheline number. Signed-off-by: Tonghao Zhang <[email protected]> Acked-by: Pravin B Shelar <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent c57c054 commit c4b2bf6

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

net/openvswitch/flow.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void ovs_flow_stats_update(struct sw_flow *flow, __be16 tcp_flags,
7272
const struct sk_buff *skb)
7373
{
7474
struct flow_stats *stats;
75-
int cpu = smp_processor_id();
75+
unsigned int cpu = smp_processor_id();
7676
int len = skb->len + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
7777

7878
stats = rcu_dereference(flow->stats[cpu]);
@@ -117,6 +117,7 @@ void ovs_flow_stats_update(struct sw_flow *flow, __be16 tcp_flags,
117117

118118
rcu_assign_pointer(flow->stats[cpu],
119119
new_stats);
120+
cpumask_set_cpu(cpu, &flow->cpu_used_mask);
120121
goto unlock;
121122
}
122123
}
@@ -144,7 +145,7 @@ void ovs_flow_stats_get(const struct sw_flow *flow,
144145
memset(ovs_stats, 0, sizeof(*ovs_stats));
145146

146147
/* We open code this to make sure cpu 0 is always considered */
147-
for (cpu = 0; cpu < nr_cpu_ids; cpu = cpumask_next(cpu, cpu_possible_mask)) {
148+
for (cpu = 0; cpu < nr_cpu_ids; cpu = cpumask_next(cpu, &flow->cpu_used_mask)) {
148149
struct flow_stats *stats = rcu_dereference_ovsl(flow->stats[cpu]);
149150

150151
if (stats) {
@@ -168,7 +169,7 @@ void ovs_flow_stats_clear(struct sw_flow *flow)
168169
int cpu;
169170

170171
/* We open code this to make sure cpu 0 is always considered */
171-
for (cpu = 0; cpu < nr_cpu_ids; cpu = cpumask_next(cpu, cpu_possible_mask)) {
172+
for (cpu = 0; cpu < nr_cpu_ids; cpu = cpumask_next(cpu, &flow->cpu_used_mask)) {
172173
struct flow_stats *stats = ovsl_dereference(flow->stats[cpu]);
173174

174175
if (stats) {

net/openvswitch/flow.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <linux/jiffies.h>
3232
#include <linux/time.h>
3333
#include <linux/flex_array.h>
34+
#include <linux/cpumask.h>
3435
#include <net/inet_ecn.h>
3536
#include <net/ip_tunnels.h>
3637
#include <net/dst_metadata.h>
@@ -219,6 +220,7 @@ struct sw_flow {
219220
*/
220221
struct sw_flow_key key;
221222
struct sw_flow_id id;
223+
struct cpumask cpu_used_mask;
222224
struct sw_flow_mask *mask;
223225
struct sw_flow_actions __rcu *sf_acts;
224226
struct flow_stats __rcu *stats[]; /* One for each CPU. First one

net/openvswitch/flow_table.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ struct sw_flow *ovs_flow_alloc(void)
9898

9999
RCU_INIT_POINTER(flow->stats[0], stats);
100100

101+
cpumask_set_cpu(0, &flow->cpu_used_mask);
102+
101103
return flow;
102104
err:
103105
kmem_cache_free(flow_cache, flow);
@@ -141,7 +143,7 @@ static void flow_free(struct sw_flow *flow)
141143
if (flow->sf_acts)
142144
ovs_nla_free_flow_actions((struct sw_flow_actions __force *)flow->sf_acts);
143145
/* We open code this to make sure cpu 0 is always considered */
144-
for (cpu = 0; cpu < nr_cpu_ids; cpu = cpumask_next(cpu, cpu_possible_mask))
146+
for (cpu = 0; cpu < nr_cpu_ids; cpu = cpumask_next(cpu, &flow->cpu_used_mask))
145147
if (flow->stats[cpu])
146148
kmem_cache_free(flow_stats_cache,
147149
(struct flow_stats __force *)flow->stats[cpu]);

0 commit comments

Comments
 (0)