History

net: openvswitch: Add support to count upcall packets

Add support to count upall packets, when kmod of openvswitch upcall to count the number of packets for upcall succeed and failed, which is a better way to see how many packets upcalled on every interfaces.

wangchuanlei committed 3 years ago

Commit 1933ea31 parent e47877c4 files · +121 −0Browse files

Unified · Split

4 files
  1. include/uapi/linux/openvswitch.h +14 −0
  2. net/openvswitch/datapath.c +41 −0
  3. net/openvswitch/vport.c +50 −0
  4. net/openvswitch/vport.h +16 −0
include/uapi/linux/openvswitch.h+14−0
@@ -277,11 +277,25 @@ enum ovs_vport_attr { · 276 unmodified lines
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+41−0
@@ -209,6 +209,26 @@ static struct vport *new_vport(const struct vport_parms *parms) · 208 unmodified lines
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();
@@ -216,6 +236,9 @@ void ovs_dp_detach_port(struct vport *p) · 1 unmodified line
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, · 83 unmodified lines
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
@@ -1826,6 +1851,12 @@ static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info) · 1515 unmodified lines
18261851 goto err_destroy_portids;
18271852 }
18281853
1854+ vport->upcall_stats = netdev_alloc_pcpu_stats(struct vport_upcall_stats_percpu);
1855+ if (!vport->upcall_stats) {
1856+ err = -ENOMEM;
1857+ goto err_destroy_portids;
1858+ }
1859+
18291860 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
18301861 info->snd_seq, 0, OVS_DP_CMD_NEW);
18311862 BUG_ON(err < 0);
@@ -2098,6 +2129,9 @@ static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb, · 266 unmodified lines
20982129 OVS_VPORT_ATTR_PAD))
20992130 goto nla_put_failure;
21002131
2132+ if (ovs_vport_get_upcall_stats(vport, skb))
2133+ goto nla_put_failure;
2134+
21012135 if (ovs_vport_get_upcall_portids(vport, skb))
21022136 goto nla_put_failure;
21032137
@@ -2279,6 +2313,12 @@ restart: · 175 unmodified lines
22792313 goto exit_unlock_free;
22802314 }
22812315
2316+ vport->upcall_stats = netdev_alloc_pcpu_stats(struct vport_upcall_stats_percpu);
2317+ if (!vport->upcall_stats) {
2318+ err = -ENOMEM;
2319+ goto exit_unlock_free;
2320+ }
2321+
22822322 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
22832323 info->snd_portid, info->snd_seq, 0,
22842324 OVS_VPORT_CMD_NEW, GFP_KERNEL);
@@ -2508,6 +2548,7 @@ static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = { · 223 unmodified lines
25082548 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
25092549 [OVS_VPORT_ATTR_IFINDEX] = { .type = NLA_U32 },
25102550 [OVS_VPORT_ATTR_NETNSID] = { .type = NLA_S32 },
2551+ [OVS_VPORT_ATTR_UPCALL_STATS] = { .type = NLA_NESTED },
25112552 };
25122553
25132554 static const struct genl_small_ops dp_vport_genl_ops[] = {
net/openvswitch/vport.c+50−0
@@ -284,6 +284,56 @@ void ovs_vport_get_stats(struct vport *vport, struct ovs_vport_stats *stats) · 283 unmodified lines
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+16−0
@@ -32,6 +32,8 @@ struct vport *ovs_vport_locate(const struct net *net, const char *name); · 31 unmodified lines
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 { · 27 unmodified lines
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 { · 7 unmodified lines
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 { · 53 unmodified lines
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 *);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t