From patchwork Wed Nov 30 09:15:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: wangchuanlei X-Patchwork-Id: 1710392 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=2605:bc80:3010::136; helo=smtp3.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Received: from smtp3.osuosl.org (smtp3.osuosl.org [IPv6:2605:bc80:3010::136]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4NMYVD6Dbdz23nT for ; Wed, 30 Nov 2022 20:16:20 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by smtp3.osuosl.org (Postfix) with ESMTP id B21AD60D67; Wed, 30 Nov 2022 09:16:18 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp3.osuosl.org B21AD60D67 X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp3.osuosl.org ([127.0.0.1]) by localhost (smtp3.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id OjN2zG8pyB42; Wed, 30 Nov 2022 09:16:17 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [IPv6:2605:bc80:3010:104::8cd3:938]) by smtp3.osuosl.org (Postfix) with ESMTPS id 994C660640; Wed, 30 Nov 2022 09:16:16 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp3.osuosl.org 994C660640 Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 6EAE7C0033; Wed, 30 Nov 2022 09:16:16 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp2.osuosl.org (smtp2.osuosl.org [140.211.166.133]) by lists.linuxfoundation.org (Postfix) with ESMTP id 3FDB5C002D for ; Wed, 30 Nov 2022 09:16:14 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp2.osuosl.org (Postfix) with ESMTP id 1AD5C402D0 for ; Wed, 30 Nov 2022 09:16:14 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp2.osuosl.org 1AD5C402D0 X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp2.osuosl.org ([127.0.0.1]) by localhost (smtp2.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 8RLdK-Pwdxg0 for ; Wed, 30 Nov 2022 09:16:13 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.8.0 DKIM-Filter: OpenDKIM Filter v2.11.0 smtp2.osuosl.org 6E0484013E Received: from ssh248.corpemail.net (ssh248.corpemail.net [210.51.61.248]) by smtp2.osuosl.org (Postfix) with ESMTPS id 6E0484013E for ; Wed, 30 Nov 2022 09:16:12 +0000 (UTC) Received: from ([60.208.111.195]) by ssh248.corpemail.net ((D)) with ASMTP (SSL) id VNR00000; Wed, 30 Nov 2022 17:16:00 +0800 Received: from localhost.localdomain (10.180.206.146) by jtjnmail201609.home.langchao.com (10.100.2.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.12; Wed, 30 Nov 2022 17:16:02 +0800 From: wangchuanlei To: , , , , , , Date: Wed, 30 Nov 2022 04:15:59 -0500 Message-ID: <20221130091559.1120493-1-wangchuanlei@inspur.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 X-Originating-IP: [10.180.206.146] X-ClientProxiedBy: Jtjnmail201613.home.langchao.com (10.100.2.13) To jtjnmail201609.home.langchao.com (10.100.2.9) tUid: 20221130171600ce81488974068f30973d092497f86aa0 X-Abuse-Reports-To: service@corp-email.com Abuse-Reports-To: service@corp-email.com X-Complaints-To: service@corp-email.com X-Report-Abuse-To: service@corp-email.com Cc: dev@openvswitch.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [ovs-dev] [PATCH] [PATCH v6 net-next] net: openvswitch: Add support to count upcall packets X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" Add support to count upall packets, when kmod of openvswitch upcall to userspace , here count the number of packets for upcall succeed and failed, which is a better way to see how many packets upcalled to userspace(ovs-vswitchd) on every interfaces. Here modify format of code used by comments of v6. Changes since v4 & v5: - optimize the function used by comments Changes since v3: - use nested NLA_NESTED attribute in netlink message Changes since v2: - add count of upcall failed packets Changes since v1: - add count of upcall succeed packets Signed-off-by: wangchuanlei --- include/uapi/linux/openvswitch.h | 14 +++++++++ net/openvswitch/datapath.c | 50 ++++++++++++++++++++++++++++++++ net/openvswitch/vport.c | 44 ++++++++++++++++++++++++++++ net/openvswitch/vport.h | 24 +++++++++++++++ 4 files changed, 132 insertions(+) diff --git a/include/uapi/linux/openvswitch.h b/include/uapi/linux/openvswitch.h index 94066f87e9ee..8422ebf6885b 100644 --- a/include/uapi/linux/openvswitch.h +++ b/include/uapi/linux/openvswitch.h @@ -277,11 +277,25 @@ enum ovs_vport_attr { OVS_VPORT_ATTR_PAD, OVS_VPORT_ATTR_IFINDEX, OVS_VPORT_ATTR_NETNSID, + OVS_VPORT_ATTR_UPCALL_STATS, __OVS_VPORT_ATTR_MAX }; #define OVS_VPORT_ATTR_MAX (__OVS_VPORT_ATTR_MAX - 1) +/** + * enum ovs_vport_upcall_attr - attributes for %OVS_VPORT_UPCALL* commands + * @OVS_VPORT_UPCALL_SUCCESS: 64-bit upcall success packets. + * @OVS_VPORT_UPCALL_FAIL: 64-bit upcall fail packets. + */ +enum ovs_vport_upcall_attr { + OVS_VPORT_UPCALL_SUCCESS, + OVS_VPORT_UPCALL_FAIL, + __OVS_VPORT_UPCALL_MAX +}; + +#define OVS_VPORT_UPCALL_MAX (__OVS_VPORT_UPCALL_MAX - 1) + enum { OVS_VXLAN_EXT_UNSPEC, OVS_VXLAN_EXT_GBP, /* Flag or __u32 */ diff --git a/net/openvswitch/datapath.c b/net/openvswitch/datapath.c index c8a9075ddd0a..f9279aee2adb 100644 --- a/net/openvswitch/datapath.c +++ b/net/openvswitch/datapath.c @@ -209,6 +209,26 @@ static struct vport *new_vport(const struct vport_parms *parms) return vport; } +static void ovs_vport_upcalls(struct sk_buff *skb, + const struct dp_upcall_info *upcall_info, + bool upcall_result) +{ + struct vport *p = OVS_CB(skb)->input_vport; + struct vport_upcall_stats_percpu *vport_stats; + + if (upcall_info->cmd != OVS_PACKET_CMD_MISS && + upcall_info->cmd != OVS_PACKET_CMD_ACTION) + return; + + vport_stats = this_cpu_ptr(p->upcall_stats); + u64_stats_update_begin(&vport_stats->syncp); + if (upcall_result) + u64_stats_inc(&vport_stats->n_success); + else + u64_stats_inc(&vport_stats->n_fail); + u64_stats_update_end(&vport_stats->syncp); +} + void ovs_dp_detach_port(struct vport *p) { ASSERT_OVSL(); @@ -216,6 +236,9 @@ void ovs_dp_detach_port(struct vport *p) /* First drop references to device. */ hlist_del_rcu(&p->dp_hash_node); + /* Free percpu memory */ + free_percpu(p->upcall_stats); + /* Then destroy it. */ ovs_vport_del(p); } @@ -305,6 +328,8 @@ int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb, err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen); else err = queue_gso_packets(dp, skb, key, upcall_info, cutlen); + + ovs_vport_upcalls(skb, upcall_info, !err); if (err) goto err; @@ -1825,6 +1850,12 @@ static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info) goto err_destroy_portids; } + vport->upcall_stats = netdev_alloc_pcpu_stats(struct vport_upcall_stats_percpu); + if (!vport->upcall_stats) { + err = -ENOMEM; + goto err_destroy_portids; + } + err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid, info->snd_seq, 0, OVS_DP_CMD_NEW); BUG_ON(err < 0); @@ -2068,6 +2099,8 @@ static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb, { struct ovs_header *ovs_header; struct ovs_vport_stats vport_stats; + struct ovs_vport_upcall_stats stat; + struct nlattr *nla; int err; ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family, @@ -2097,6 +2130,15 @@ static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb, OVS_VPORT_ATTR_PAD)) goto nla_put_failure; + nla = nla_nest_start_noflag(skb, OVS_VPORT_ATTR_UPCALL_STATS); + if (!nla) + goto nla_put_failure; + + ovs_vport_get_upcall_stats(vport, &stat); + if (ovs_vport_put_upcall_stats(skb, &stat)) + goto nla_put_failure; + nla_nest_end(skb, nla); + if (ovs_vport_get_upcall_portids(vport, skb)) goto nla_put_failure; @@ -2278,6 +2320,13 @@ static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info) goto exit_unlock_free; } + vport->upcall_stats = netdev_alloc_pcpu_stats(struct vport_upcall_stats_percpu); + + if (!vport->upcall_stats) { + err = -ENOMEM; + goto exit_unlock_free; + } + err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info), info->snd_portid, info->snd_seq, 0, OVS_VPORT_CMD_NEW, GFP_KERNEL); @@ -2507,6 +2556,7 @@ static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = { [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED }, [OVS_VPORT_ATTR_IFINDEX] = { .type = NLA_U32 }, [OVS_VPORT_ATTR_NETNSID] = { .type = NLA_S32 }, + [OVS_VPORT_ATTR_UPCALL_STATS] = { .type = NLA_NESTED }, }; static const struct genl_small_ops dp_vport_genl_ops[] = { diff --git a/net/openvswitch/vport.c b/net/openvswitch/vport.c index 82a74f998966..fd95536b35ef 100644 --- a/net/openvswitch/vport.c +++ b/net/openvswitch/vport.c @@ -284,6 +284,50 @@ void ovs_vport_get_stats(struct vport *vport, struct ovs_vport_stats *stats) stats->tx_packets = dev_stats->tx_packets; } +/** + * ovs_vport_get_upcall_stats - retrieve upcall stats + * + * @vport: vport from which to retrieve the stats + * @ovs_vport_upcall_stats: location to store stats + * + * Retrieves upcall stats for the given device. + * + * Must be called with ovs_mutex or rcu_read_lock. + */ +void ovs_vport_get_upcall_stats(struct vport *vport, struct ovs_vport_upcall_stats *stats) +{ + int i; + + stats->tx_success = 0; + stats->tx_fail = 0; + + for_each_possible_cpu(i) { + const struct vport_upcall_stats_percpu *upcall_stats; + unsigned int start; + + upcall_stats = per_cpu_ptr(vport->upcall_stats, i); + do { + start = u64_stats_fetch_begin(&upcall_stats->syncp); + stats->tx_success += u64_stats_read(&upcall_stats->n_success); + stats->tx_fail += u64_stats_read(&upcall_stats->n_fail); + } while (u64_stats_fetch_retry(&upcall_stats->syncp, start)); + } +} + +int ovs_vport_put_upcall_stats(struct sk_buff *skb, + struct ovs_vport_upcall_stats *stats) +{ + if (nla_put_u64_64bit(skb, OVS_VPORT_UPCALL_SUCCESS, stats->tx_success, + OVS_VPORT_ATTR_PAD)) + return -EMSGSIZE; + + if (nla_put_u64_64bit(skb, OVS_VPORT_UPCALL_FAIL, stats->tx_fail, + OVS_VPORT_ATTR_PAD)) + return -EMSGSIZE; + + return 0; +} + /** * ovs_vport_get_options - retrieve device options * diff --git a/net/openvswitch/vport.h b/net/openvswitch/vport.h index 7d276f60c000..5ba9f14df55a 100644 --- a/net/openvswitch/vport.h +++ b/net/openvswitch/vport.h @@ -32,6 +32,16 @@ struct vport *ovs_vport_locate(const struct net *net, const char *name); void ovs_vport_get_stats(struct vport *, struct ovs_vport_stats *); +struct ovs_vport_upcall_stats { + __u64 tx_success; /* total packets upcalls succeed */ + __u64 tx_fail; /* total packets upcalls failed */ +}; + +void ovs_vport_get_upcall_stats(struct vport *vport, + struct ovs_vport_upcall_stats *stats); +int ovs_vport_put_upcall_stats(struct sk_buff *skb, + struct ovs_vport_upcall_stats *stats); + int ovs_vport_set_options(struct vport *, struct nlattr *options); int ovs_vport_get_options(const struct vport *, struct sk_buff *); @@ -65,6 +75,7 @@ struct vport_portids { * @hash_node: Element in @dev_table hash table in vport.c. * @dp_hash_node: Element in @datapath->ports hash table in datapath.c. * @ops: Class structure. + * @upcall_stats: Upcall stats of every ports. * @detach_list: list used for detaching vport in net-exit call. * @rcu: RCU callback head for deferred destruction. */ @@ -78,6 +89,7 @@ struct vport { struct hlist_node hash_node; struct hlist_node dp_hash_node; const struct vport_ops *ops; + struct vport_upcall_stats_percpu __percpu *upcall_stats; struct list_head detach_list; struct rcu_head rcu; @@ -137,6 +149,18 @@ struct vport_ops { struct list_head list; }; +/** + * struct vport_upcall_stats_percpu - per-cpu packet upcall statistics for + * a given vport. + * @n_success: Number of packets that upcall to userspace succeed. + * @n_fail: Number of packets that upcall to userspace failed. + */ +struct vport_upcall_stats_percpu { + struct u64_stats_sync syncp; + u64_stats_t n_success; + u64_stats_t n_fail; +}; + struct vport *ovs_vport_alloc(int priv_size, const struct vport_ops *, const struct vport_parms *); void ovs_vport_free(struct vport *);