Skip to content

Commit e298e50

Browse files
Pravin B Shelarjessegross
authored andcommitted
openvswitch: Per cpu flow stats.
With mega flow implementation ovs flow can be shared between multiple CPUs which makes stats updates highly contended operation. This patch uses per-CPU stats in cases where a flow is likely to be shared (if there is a wildcard in the 5-tuple and therefore likely to be spread by RSS). In other situations, it uses the current strategy, saving memory and allocation time. Signed-off-by: Pravin B Shelar <pshelar@nicira.com> Signed-off-by: Jesse Gross <jesse@nicira.com>
1 parent 795449d commit e298e50

7 files changed

Lines changed: 210 additions & 55 deletions

File tree

‎net/openvswitch/datapath.c‎

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,9 @@ void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
251251
OVS_CB(skb)->flow = flow;
252252
OVS_CB(skb)->pkt_key = &key;
253253

254-
stats_counter = &stats->n_hit;
255-
ovs_flow_used(OVS_CB(skb)->flow, skb);
254+
ovs_flow_stats_update(OVS_CB(skb)->flow, skb);
256255
ovs_execute_actions(dp, skb);
256+
stats_counter = &stats->n_hit;
257257

258258
out:
259259
/* Update datapath statistics. */
@@ -459,14 +459,6 @@ static int queue_userspace_packet(struct net *net, int dp_ifindex,
459459
return err;
460460
}
461461

462-
static void clear_stats(struct sw_flow *flow)
463-
{
464-
flow->used = 0;
465-
flow->tcp_flags = 0;
466-
flow->packet_count = 0;
467-
flow->byte_count = 0;
468-
}
469-
470462
static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
471463
{
472464
struct ovs_header *ovs_header = info->userhdr;
@@ -505,7 +497,7 @@ static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
505497
packet->protocol = htons(ETH_P_802_2);
506498

507499
/* Build an sw_flow for sending this packet. */
508-
flow = ovs_flow_alloc();
500+
flow = ovs_flow_alloc(false);
509501
err = PTR_ERR(flow);
510502
if (IS_ERR(flow))
511503
goto err_kfree_skb;
@@ -641,10 +633,10 @@ static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
641633
const int skb_orig_len = skb->len;
642634
struct nlattr *start;
643635
struct ovs_flow_stats stats;
636+
__be16 tcp_flags;
637+
unsigned long used;
644638
struct ovs_header *ovs_header;
645639
struct nlattr *nla;
646-
unsigned long used;
647-
u8 tcp_flags;
648640
int err;
649641

650642
ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
@@ -673,24 +665,17 @@ static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
673665

674666
nla_nest_end(skb, nla);
675667

676-
spin_lock_bh(&flow->lock);
677-
used = flow->used;
678-
stats.n_packets = flow->packet_count;
679-
stats.n_bytes = flow->byte_count;
680-
tcp_flags = (u8)ntohs(flow->tcp_flags);
681-
spin_unlock_bh(&flow->lock);
682-
668+
ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
683669
if (used &&
684670
nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
685671
goto nla_put_failure;
686672

687673
if (stats.n_packets &&
688-
nla_put(skb, OVS_FLOW_ATTR_STATS,
689-
sizeof(struct ovs_flow_stats), &stats))
674+
nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
690675
goto nla_put_failure;
691676

692-
if (tcp_flags &&
693-
nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
677+
if ((u8)ntohs(tcp_flags) &&
678+
nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
694679
goto nla_put_failure;
695680

696681
/* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
@@ -770,6 +755,7 @@ static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
770755
struct datapath *dp;
771756
struct sw_flow_actions *acts = NULL;
772757
struct sw_flow_match match;
758+
bool exact_5tuple;
773759
int error;
774760

775761
/* Extract key. */
@@ -778,7 +764,7 @@ static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
778764
goto error;
779765

780766
ovs_match_init(&match, &key, &mask);
781-
error = ovs_nla_get_match(&match,
767+
error = ovs_nla_get_match(&match, &exact_5tuple,
782768
a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
783769
if (error)
784770
goto error;
@@ -817,12 +803,11 @@ static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
817803
goto err_unlock_ovs;
818804

819805
/* Allocate flow. */
820-
flow = ovs_flow_alloc();
806+
flow = ovs_flow_alloc(!exact_5tuple);
821807
if (IS_ERR(flow)) {
822808
error = PTR_ERR(flow);
823809
goto err_unlock_ovs;
824810
}
825-
clear_stats(flow);
826811

827812
flow->key = masked_key;
828813
flow->unmasked_key = key;
@@ -866,11 +851,8 @@ static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
866851
reply = ovs_flow_cmd_build_info(flow, dp, info, OVS_FLOW_CMD_NEW);
867852

868853
/* Clear stats. */
869-
if (a[OVS_FLOW_ATTR_CLEAR]) {
870-
spin_lock_bh(&flow->lock);
871-
clear_stats(flow);
872-
spin_unlock_bh(&flow->lock);
873-
}
854+
if (a[OVS_FLOW_ATTR_CLEAR])
855+
ovs_flow_stats_clear(flow);
874856
}
875857
ovs_unlock();
876858

@@ -908,7 +890,7 @@ static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
908890
}
909891

910892
ovs_match_init(&match, &key, NULL);
911-
err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
893+
err = ovs_nla_get_match(&match, NULL, a[OVS_FLOW_ATTR_KEY], NULL);
912894
if (err)
913895
return err;
914896

@@ -962,7 +944,7 @@ static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
962944
}
963945

964946
ovs_match_init(&match, &key, NULL);
965-
err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
947+
err = ovs_nla_get_match(&match, NULL, a[OVS_FLOW_ATTR_KEY], NULL);
966948
if (err)
967949
goto unlock;
968950

‎net/openvswitch/flow.c‎

Lines changed: 89 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <linux/ip.h>
3636
#include <linux/ipv6.h>
3737
#include <linux/sctp.h>
38+
#include <linux/smp.h>
3839
#include <linux/tcp.h>
3940
#include <linux/udp.h>
4041
#include <linux/icmp.h>
@@ -60,23 +61,104 @@ u64 ovs_flow_used_time(unsigned long flow_jiffies)
6061

6162
#define TCP_FLAGS_BE16(tp) (*(__be16 *)&tcp_flag_word(tp) & htons(0x0FFF))
6263

63-
void ovs_flow_used(struct sw_flow *flow, struct sk_buff *skb)
64+
void ovs_flow_stats_update(struct sw_flow *flow, struct sk_buff *skb)
6465
{
66+
struct flow_stats *stats;
6567
__be16 tcp_flags = 0;
6668

69+
if (!flow->stats.is_percpu)
70+
stats = flow->stats.stat;
71+
else
72+
stats = this_cpu_ptr(flow->stats.cpu_stats);
73+
6774
if ((flow->key.eth.type == htons(ETH_P_IP) ||
6875
flow->key.eth.type == htons(ETH_P_IPV6)) &&
6976
flow->key.ip.proto == IPPROTO_TCP &&
7077
likely(skb->len >= skb_transport_offset(skb) + sizeof(struct tcphdr))) {
7178
tcp_flags = TCP_FLAGS_BE16(tcp_hdr(skb));
7279
}
7380

74-
spin_lock(&flow->lock);
75-
flow->used = jiffies;
76-
flow->packet_count++;
77-
flow->byte_count += skb->len;
78-
flow->tcp_flags |= tcp_flags;
79-
spin_unlock(&flow->lock);
81+
spin_lock(&stats->lock);
82+
stats->used = jiffies;
83+
stats->packet_count++;
84+
stats->byte_count += skb->len;
85+
stats->tcp_flags |= tcp_flags;
86+
spin_unlock(&stats->lock);
87+
}
88+
89+
static void stats_read(struct flow_stats *stats,
90+
struct ovs_flow_stats *ovs_stats,
91+
unsigned long *used, __be16 *tcp_flags)
92+
{
93+
spin_lock(&stats->lock);
94+
if (time_after(stats->used, *used))
95+
*used = stats->used;
96+
*tcp_flags |= stats->tcp_flags;
97+
ovs_stats->n_packets += stats->packet_count;
98+
ovs_stats->n_bytes += stats->byte_count;
99+
spin_unlock(&stats->lock);
100+
}
101+
102+
void ovs_flow_stats_get(struct sw_flow *flow, struct ovs_flow_stats *ovs_stats,
103+
unsigned long *used, __be16 *tcp_flags)
104+
{
105+
int cpu, cur_cpu;
106+
107+
*used = 0;
108+
*tcp_flags = 0;
109+
memset(ovs_stats, 0, sizeof(*ovs_stats));
110+
111+
if (!flow->stats.is_percpu) {
112+
stats_read(flow->stats.stat, ovs_stats, used, tcp_flags);
113+
} else {
114+
cur_cpu = get_cpu();
115+
for_each_possible_cpu(cpu) {
116+
struct flow_stats *stats;
117+
118+
if (cpu == cur_cpu)
119+
local_bh_disable();
120+
121+
stats = per_cpu_ptr(flow->stats.cpu_stats, cpu);
122+
stats_read(stats, ovs_stats, used, tcp_flags);
123+
124+
if (cpu == cur_cpu)
125+
local_bh_enable();
126+
}
127+
put_cpu();
128+
}
129+
}
130+
131+
static void stats_reset(struct flow_stats *stats)
132+
{
133+
spin_lock(&stats->lock);
134+
stats->used = 0;
135+
stats->packet_count = 0;
136+
stats->byte_count = 0;
137+
stats->tcp_flags = 0;
138+
spin_unlock(&stats->lock);
139+
}
140+
141+
void ovs_flow_stats_clear(struct sw_flow *flow)
142+
{
143+
int cpu, cur_cpu;
144+
145+
if (!flow->stats.is_percpu) {
146+
stats_reset(flow->stats.stat);
147+
} else {
148+
cur_cpu = get_cpu();
149+
150+
for_each_possible_cpu(cpu) {
151+
152+
if (cpu == cur_cpu)
153+
local_bh_disable();
154+
155+
stats_reset(per_cpu_ptr(flow->stats.cpu_stats, cpu));
156+
157+
if (cpu == cur_cpu)
158+
local_bh_enable();
159+
}
160+
put_cpu();
161+
}
80162
}
81163

82164
static int check_header(struct sk_buff *skb, int len)

‎net/openvswitch/flow.h‎

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#ifndef FLOW_H
2020
#define FLOW_H 1
2121

22+
#include <linux/cache.h>
2223
#include <linux/kernel.h>
2324
#include <linux/netlink.h>
2425
#include <linux/openvswitch.h>
@@ -146,6 +147,22 @@ struct sw_flow_actions {
146147
struct nlattr actions[];
147148
};
148149

150+
struct flow_stats {
151+
u64 packet_count; /* Number of packets matched. */
152+
u64 byte_count; /* Number of bytes matched. */
153+
unsigned long used; /* Last used time (in jiffies). */
154+
spinlock_t lock; /* Lock for atomic stats update. */
155+
__be16 tcp_flags; /* Union of seen TCP flags. */
156+
};
157+
158+
struct sw_flow_stats {
159+
bool is_percpu;
160+
union {
161+
struct flow_stats *stat;
162+
struct flow_stats __percpu *cpu_stats;
163+
};
164+
};
165+
149166
struct sw_flow {
150167
struct rcu_head rcu;
151168
struct hlist_node hash_node[2];
@@ -155,12 +172,7 @@ struct sw_flow {
155172
struct sw_flow_key unmasked_key;
156173
struct sw_flow_mask *mask;
157174
struct sw_flow_actions __rcu *sf_acts;
158-
159-
spinlock_t lock; /* Lock for values below. */
160-
unsigned long used; /* Last used time (in jiffies). */
161-
u64 packet_count; /* Number of packets matched. */
162-
u64 byte_count; /* Number of bytes matched. */
163-
__be16 tcp_flags; /* Union of seen TCP flags. */
175+
struct sw_flow_stats stats;
164176
};
165177

166178
struct arp_eth_header {
@@ -177,7 +189,10 @@ struct arp_eth_header {
177189
unsigned char ar_tip[4]; /* target IP address */
178190
} __packed;
179191

180-
void ovs_flow_used(struct sw_flow *, struct sk_buff *);
192+
void ovs_flow_stats_update(struct sw_flow *flow, struct sk_buff *skb);
193+
void ovs_flow_stats_get(struct sw_flow *flow, struct ovs_flow_stats *stats,
194+
unsigned long *used, __be16 *tcp_flags);
195+
void ovs_flow_stats_clear(struct sw_flow *flow);
181196
u64 ovs_flow_used_time(unsigned long flow_jiffies);
182197

183198
int ovs_flow_extract(struct sk_buff *, u16 in_port, struct sw_flow_key *);

0 commit comments

Comments
 (0)