Skip to content

Commit 98f0fb1

Browse files
author
Herton R. Krzesinski
committed
Merge: net: openvswitch: Add support to count upcall packets
MR: https://gitlab.com/redhat/centos-stream/src/kernel/centos-stream-9/-/merge_requests/1936 Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2163678 Signed-off-by: Antoine Tenart <atenart@redhat.com> Approved-by: Eelco Chaudron <echaudro@redhat.com> Approved-by: Aaron Conole <aconole@redhat.com> Approved-by: Xin Long <lxin@redhat.com> Approved-by: Andrea Claudi <aclaudi@redhat.com> Signed-off-by: Herton R. Krzesinski <herton@redhat.com>
2 parents a4cfe99 + 988b7c8 commit 98f0fb1

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed

include/uapi/linux/openvswitch.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,11 +277,25 @@ enum ovs_vport_attr {
277277
OVS_VPORT_ATTR_PAD,
278278
OVS_VPORT_ATTR_IFINDEX,
279279
OVS_VPORT_ATTR_NETNSID,
280+
OVS_VPORT_ATTR_UPCALL_STATS,
280281
__OVS_VPORT_ATTR_MAX
281282
};
282283

283284
#define OVS_VPORT_ATTR_MAX (__OVS_VPORT_ATTR_MAX - 1)
284285

286+
/**
287+
* enum ovs_vport_upcall_attr - attributes for %OVS_VPORT_UPCALL* commands
288+
* @OVS_VPORT_UPCALL_SUCCESS: 64-bit upcall success packets.
289+
* @OVS_VPORT_UPCALL_FAIL: 64-bit upcall fail packets.
290+
*/
291+
enum ovs_vport_upcall_attr {
292+
OVS_VPORT_UPCALL_ATTR_SUCCESS,
293+
OVS_VPORT_UPCALL_ATTR_FAIL,
294+
__OVS_VPORT_UPCALL_ATTR_MAX
295+
};
296+
297+
#define OVS_VPORT_UPCALL_ATTR_MAX (__OVS_VPORT_UPCALL_ATTR_MAX - 1)
298+
285299
enum {
286300
OVS_VXLAN_EXT_UNSPEC,
287301
OVS_VXLAN_EXT_GBP, /* Flag or __u32 */

net/openvswitch/datapath.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,36 @@ static struct vport *new_vport(const struct vport_parms *parms)
209209
return vport;
210210
}
211211

212+
static void ovs_vport_update_upcall_stats(struct sk_buff *skb,
213+
const struct dp_upcall_info *upcall_info,
214+
bool upcall_result)
215+
{
216+
struct vport *p = OVS_CB(skb)->input_vport;
217+
struct vport_upcall_stats_percpu *stats;
218+
219+
if (upcall_info->cmd != OVS_PACKET_CMD_MISS &&
220+
upcall_info->cmd != OVS_PACKET_CMD_ACTION)
221+
return;
222+
223+
stats = this_cpu_ptr(p->upcall_stats);
224+
u64_stats_update_begin(&stats->syncp);
225+
if (upcall_result)
226+
u64_stats_inc(&stats->n_success);
227+
else
228+
u64_stats_inc(&stats->n_fail);
229+
u64_stats_update_end(&stats->syncp);
230+
}
231+
212232
void ovs_dp_detach_port(struct vport *p)
213233
{
214234
ASSERT_OVSL();
215235

216236
/* First drop references to device. */
217237
hlist_del_rcu(&p->dp_hash_node);
218238

239+
/* Free percpu memory */
240+
free_percpu(p->upcall_stats);
241+
219242
/* Then destroy it. */
220243
ovs_vport_del(p);
221244
}
@@ -305,6 +328,8 @@ int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
305328
err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
306329
else
307330
err = queue_gso_packets(dp, skb, key, upcall_info, cutlen);
331+
332+
ovs_vport_update_upcall_stats(skb, upcall_info, !err);
308333
if (err)
309334
goto err;
310335

@@ -1831,6 +1856,12 @@ static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
18311856
goto err_destroy_portids;
18321857
}
18331858

1859+
vport->upcall_stats = netdev_alloc_pcpu_stats(struct vport_upcall_stats_percpu);
1860+
if (!vport->upcall_stats) {
1861+
err = -ENOMEM;
1862+
goto err_destroy_vport;
1863+
}
1864+
18341865
err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
18351866
info->snd_seq, 0, OVS_DP_CMD_NEW);
18361867
BUG_ON(err < 0);
@@ -1843,6 +1874,8 @@ static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
18431874
ovs_notify(&dp_datapath_genl_family, reply, info);
18441875
return 0;
18451876

1877+
err_destroy_vport:
1878+
ovs_dp_detach_port(vport);
18461879
err_destroy_portids:
18471880
kfree(rcu_dereference_raw(dp->upcall_portids));
18481881
err_unlock_and_destroy_meters:
@@ -2102,6 +2135,9 @@ static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
21022135
OVS_VPORT_ATTR_PAD))
21032136
goto nla_put_failure;
21042137

2138+
if (ovs_vport_get_upcall_stats(vport, skb))
2139+
goto nla_put_failure;
2140+
21052141
if (ovs_vport_get_upcall_portids(vport, skb))
21062142
goto nla_put_failure;
21072143

@@ -2283,6 +2319,12 @@ static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
22832319
goto exit_unlock_free;
22842320
}
22852321

2322+
vport->upcall_stats = netdev_alloc_pcpu_stats(struct vport_upcall_stats_percpu);
2323+
if (!vport->upcall_stats) {
2324+
err = -ENOMEM;
2325+
goto exit_unlock_free_vport;
2326+
}
2327+
22862328
err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
22872329
info->snd_portid, info->snd_seq, 0,
22882330
OVS_VPORT_CMD_NEW, GFP_KERNEL);
@@ -2300,6 +2342,8 @@ static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
23002342
ovs_notify(&dp_vport_genl_family, reply, info);
23012343
return 0;
23022344

2345+
exit_unlock_free_vport:
2346+
ovs_dp_detach_port(vport);
23032347
exit_unlock_free:
23042348
ovs_unlock();
23052349
kfree_skb(reply);
@@ -2512,6 +2556,7 @@ static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
25122556
[OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
25132557
[OVS_VPORT_ATTR_IFINDEX] = { .type = NLA_U32 },
25142558
[OVS_VPORT_ATTR_NETNSID] = { .type = NLA_S32 },
2559+
[OVS_VPORT_ATTR_UPCALL_STATS] = { .type = NLA_NESTED },
25152560
};
25162561

25172562
static const struct genl_small_ops dp_vport_genl_ops[] = {

net/openvswitch/vport.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,56 @@ void ovs_vport_get_stats(struct vport *vport, struct ovs_vport_stats *stats)
284284
stats->tx_packets = dev_stats->tx_packets;
285285
}
286286

287+
/**
288+
* ovs_vport_get_upcall_stats - retrieve upcall stats
289+
*
290+
* @vport: vport from which to retrieve the stats.
291+
* @skb: sk_buff where upcall stats should be appended.
292+
*
293+
* Retrieves upcall stats for the given device.
294+
*
295+
* Must be called with ovs_mutex or rcu_read_lock.
296+
*/
297+
int ovs_vport_get_upcall_stats(struct vport *vport, struct sk_buff *skb)
298+
{
299+
struct nlattr *nla;
300+
int i;
301+
302+
__u64 tx_success = 0;
303+
__u64 tx_fail = 0;
304+
305+
for_each_possible_cpu(i) {
306+
const struct vport_upcall_stats_percpu *stats;
307+
unsigned int start;
308+
309+
stats = per_cpu_ptr(vport->upcall_stats, i);
310+
do {
311+
start = u64_stats_fetch_begin(&stats->syncp);
312+
tx_success += u64_stats_read(&stats->n_success);
313+
tx_fail += u64_stats_read(&stats->n_fail);
314+
} while (u64_stats_fetch_retry(&stats->syncp, start));
315+
}
316+
317+
nla = nla_nest_start_noflag(skb, OVS_VPORT_ATTR_UPCALL_STATS);
318+
if (!nla)
319+
return -EMSGSIZE;
320+
321+
if (nla_put_u64_64bit(skb, OVS_VPORT_UPCALL_ATTR_SUCCESS, tx_success,
322+
OVS_VPORT_ATTR_PAD)) {
323+
nla_nest_cancel(skb, nla);
324+
return -EMSGSIZE;
325+
}
326+
327+
if (nla_put_u64_64bit(skb, OVS_VPORT_UPCALL_ATTR_FAIL, tx_fail,
328+
OVS_VPORT_ATTR_PAD)) {
329+
nla_nest_cancel(skb, nla);
330+
return -EMSGSIZE;
331+
}
332+
nla_nest_end(skb, nla);
333+
334+
return 0;
335+
}
336+
287337
/**
288338
* ovs_vport_get_options - retrieve device options
289339
*

net/openvswitch/vport.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ struct vport *ovs_vport_locate(const struct net *net, const char *name);
3232

3333
void ovs_vport_get_stats(struct vport *, struct ovs_vport_stats *);
3434

35+
int ovs_vport_get_upcall_stats(struct vport *vport, struct sk_buff *skb);
36+
3537
int ovs_vport_set_options(struct vport *, struct nlattr *options);
3638
int ovs_vport_get_options(const struct vport *, struct sk_buff *);
3739

@@ -65,6 +67,7 @@ struct vport_portids {
6567
* @hash_node: Element in @dev_table hash table in vport.c.
6668
* @dp_hash_node: Element in @datapath->ports hash table in datapath.c.
6769
* @ops: Class structure.
70+
* @upcall_stats: Upcall stats of every ports.
6871
* @detach_list: list used for detaching vport in net-exit call.
6972
* @rcu: RCU callback head for deferred destruction.
7073
*/
@@ -78,6 +81,7 @@ struct vport {
7881
struct hlist_node hash_node;
7982
struct hlist_node dp_hash_node;
8083
const struct vport_ops *ops;
84+
struct vport_upcall_stats_percpu __percpu *upcall_stats;
8185

8286
struct list_head detach_list;
8387
struct rcu_head rcu;
@@ -137,6 +141,18 @@ struct vport_ops {
137141
struct list_head list;
138142
};
139143

144+
/**
145+
* struct vport_upcall_stats_percpu - per-cpu packet upcall statistics for
146+
* a given vport.
147+
* @n_success: Number of packets that upcall to userspace succeed.
148+
* @n_fail: Number of packets that upcall to userspace failed.
149+
*/
150+
struct vport_upcall_stats_percpu {
151+
struct u64_stats_sync syncp;
152+
u64_stats_t n_success;
153+
u64_stats_t n_fail;
154+
};
155+
140156
struct vport *ovs_vport_alloc(int priv_size, const struct vport_ops *,
141157
const struct vport_parms *);
142158
void ovs_vport_free(struct vport *);

0 commit comments

Comments
 (0)