public inbox for netdev@vger.kernel.org 
 help / color / mirror / Atom feed
From: Michael Guralnik <michaelgur@nvidia•com>
To: <netdev@vger•kernel.org>
Cc: <jiri@nvidia•com>, <ariela@nvidia•com>, <maorg@nvidia•com>,
	<saeedm@nvidia•com>, <kuba@kernel•org>, <moshe@nvidia•com>,
	Michael Guralnik <michaelgur@nvidia•com>
Subject: [RFC PATCH net-next 2/2] devlink: Add statistics to port get
Date: Thu, 7 Apr 2022 11:40:50 +0300	[thread overview]
Message-ID: <20220407084050.184989-3-michaelgur@nvidia.com> (raw)
In-Reply-To: <20220407084050.184989-1-michaelgur@nvidia.com>

Add a list of all registered stats and their value to the port get
command.

Signed-off-by: Michael Guralnik <michaelgur@nvidia•com>
---
 include/uapi/linux/devlink.h |  4 ++++
 net/core/devlink.c           | 39 ++++++++++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+)

diff --git a/include/uapi/linux/devlink.h b/include/uapi/linux/devlink.h
index b897b80770f6..8da01e551695 100644
--- a/include/uapi/linux/devlink.h
+++ b/include/uapi/linux/devlink.h
@@ -553,6 +553,10 @@ enum devlink_attr {
 
 	DEVLINK_ATTR_REGION_MAX_SNAPSHOTS,	/* u32 */
 
+	DEVLINK_ATTR_STATS_ENTRY,		/* nested */
+	DEVLINK_ATTR_STATS_NAME,		/* string */
+	DEVLINK_ATTR_STATS_VALUE,		/* u64 */
+
 	/* add new attributes above here, update the policy in devlink.c */
 
 	__DEVLINK_ATTR_MAX,
diff --git a/net/core/devlink.c b/net/core/devlink.c
index 9636d7998630..b735cca727a7 100644
--- a/net/core/devlink.c
+++ b/net/core/devlink.c
@@ -1080,6 +1080,41 @@ devlink_nl_port_function_attrs_put(struct sk_buff *msg, struct devlink_port *por
 	return err;
 }
 
+static int devlink_port_stats_get(struct devlink_port *port)
+{
+	if (!port->stats.get)
+		return -EOPNOTSUPP;
+	return port->stats.get(port);
+}
+
+static int devlink_nl_port_stats_put(struct sk_buff *msg, struct devlink_port *port)
+{
+	struct nlattr *stats_attr, *stats_entry_attr;
+	struct devlink_port_stat_item *stats_item;
+	int err;
+
+	stats_attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
+	if (!stats_attr)
+		return -EMSGSIZE;
+
+	err = devlink_port_stats_get(port);
+	if (err)
+		return err;
+
+	list_for_each_entry(stats_item, &port->stats.stats_list, list) {
+		stats_entry_attr = nla_nest_start(msg, DEVLINK_ATTR_STATS_ENTRY);
+		if (!stats_entry_attr)
+			return -EMSGSIZE;
+
+		nla_put_string(msg, DEVLINK_ATTR_STATS_NAME, stats_item->stat->name);
+		nla_put_u32(msg, DEVLINK_ATTR_STATS_VALUE, stats_item->stat->val);
+
+		nla_nest_end(msg, stats_entry_attr);
+	}
+	nla_nest_end(msg, stats_attr);
+	return 0;
+}
+
 static int devlink_nl_port_fill(struct sk_buff *msg,
 				struct devlink_port *devlink_port,
 				enum devlink_command cmd, u32 portid, u32 seq,
@@ -1132,6 +1167,8 @@ static int devlink_nl_port_fill(struct sk_buff *msg,
 	if (devlink_nl_port_function_attrs_put(msg, devlink_port, extack))
 		goto nla_put_failure;
 
+	if (devlink_nl_port_stats_put(msg, devlink_port))
+		goto nla_put_failure;
 	genlmsg_end(msg, hdr);
 	return 0;
 
@@ -8670,6 +8707,8 @@ static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
 	[DEVLINK_ATTR_RATE_TX_MAX] = { .type = NLA_U64 },
 	[DEVLINK_ATTR_RATE_NODE_NAME] = { .type = NLA_NUL_STRING },
 	[DEVLINK_ATTR_RATE_PARENT_NODE_NAME] = { .type = NLA_NUL_STRING },
+	[DEVLINK_ATTR_STATS_NAME] = {.type = NLA_NUL_STRING },
+	[DEVLINK_ATTR_STATS_VALUE] = {.type = NLA_U64 },
 };
 
 static const struct genl_small_ops devlink_nl_ops[] = {
-- 
2.17.2


  parent reply	other threads:[~2022-04-07  8:41 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-07  8:40 [RFC PATCH net-next 0/2] devlink: Add port stats Michael Guralnik
2022-04-07  8:40 ` [RFC PATCH net-next 1/2] devlink: Introduce stats to the port object Michael Guralnik
2022-04-07  8:40 ` Michael Guralnik [this message]
2022-04-08  3:16 ` [RFC PATCH net-next 0/2] devlink: Add port stats Jakub Kicinski
2022-04-11 10:28   ` Jiri Pirko
2022-04-11 17:49     ` Jakub Kicinski
2022-04-11 10:31   ` Jiri Pirko
2022-04-11 18:01     ` Jakub Kicinski
2022-04-12  8:16       ` Jiri Pirko
2022-04-12 15:34         ` Jakub Kicinski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220407084050.184989-3-michaelgur@nvidia.com \
    --to=michaelgur@nvidia$(echo .)com \
    --cc=ariela@nvidia$(echo .)com \
    --cc=jiri@nvidia$(echo .)com \
    --cc=kuba@kernel$(echo .)org \
    --cc=maorg@nvidia$(echo .)com \
    --cc=moshe@nvidia$(echo .)com \
    --cc=netdev@vger$(echo .)kernel.org \
    --cc=saeedm@nvidia$(echo .)com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox