All of lore.kernel.org
 help / color / mirror / Atom feed
From: Daniel Zahka <daniel.zahka@gmail.com>
To: Jiri Pirko <jiri@resnulli.us>,
	"David S. Miller" <davem@davemloft.net>,
	 Jakub Kicinski <kuba@kernel.org>,
	Paolo Abeni <pabeni@redhat.com>,  Simon Horman <horms@kernel.org>,
	Donald Hunter <donald.hunter@gmail.com>,
	 David Ahern <dsahern@kernel.org>
Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	 Daniel Zahka <daniel.zahka@gmail.com>
Subject: [PATCH iproute2-next 1/2] devlink: Pull the value printing logic out of pr_out_param_value()
Date: Mon, 17 Nov 2025 16:40:02 -0800	[thread overview]
Message-ID: <20251117-param-defaults-v1-1-c99604175d09@gmail.com> (raw)
In-Reply-To: <20251117-param-defaults-v1-0-c99604175d09@gmail.com>

Split the type demux and value print out of pr_out_param_value() into
a new function pr_out_param_value_print(). This new function can be
re-used for printing additional kinds of values e.g., a default value
reported by the kernel.

Signed-off-by: Daniel Zahka <daniel.zahka@gmail.com>
---
 devlink/devlink.c | 88 ++++++++++++++++++++++++++++++++++---------------------
 1 file changed, 54 insertions(+), 34 deletions(-)

diff --git a/devlink/devlink.c b/devlink/devlink.c
index fd9fac21..e1612b77 100644
--- a/devlink/devlink.c
+++ b/devlink/devlink.c
@@ -3518,33 +3518,14 @@ static const struct param_val_conv param_val_conv[] = {
 
 #define PARAM_VAL_CONV_LEN ARRAY_SIZE(param_val_conv)
 
-static void pr_out_param_value(struct dl *dl, const char *nla_name,
-			       int nla_type, struct nlattr *nl)
+static int pr_out_param_value_print(const char *nla_name, int nla_type,
+				     struct nlattr *val_attr, bool conv_exists,
+				     const char *label)
 {
-	struct nlattr *nla_value[DEVLINK_ATTR_MAX + 1] = {};
-	struct nlattr *val_attr;
+	char format_str[32];
 	const char *vstr;
-	bool conv_exists;
 	int err;
 
-	err = mnl_attr_parse_nested(nl, attr_cb, nla_value);
-	if (err != MNL_CB_OK)
-		return;
-
-	if (!nla_value[DEVLINK_ATTR_PARAM_VALUE_CMODE] ||
-	    (nla_type != MNL_TYPE_FLAG &&
-	     !nla_value[DEVLINK_ATTR_PARAM_VALUE_DATA]))
-		return;
-
-	check_indent_newline(dl);
-	print_string(PRINT_ANY, "cmode", "cmode %s",
-		     param_cmode_name(mnl_attr_get_u8(nla_value[DEVLINK_ATTR_PARAM_VALUE_CMODE])));
-
-	val_attr = nla_value[DEVLINK_ATTR_PARAM_VALUE_DATA];
-
-	conv_exists = param_val_conv_exists(param_val_conv, PARAM_VAL_CONV_LEN,
-					    nla_name);
-
 	switch (nla_type) {
 	case MNL_TYPE_U8:
 		if (conv_exists) {
@@ -3554,10 +3535,12 @@ static void pr_out_param_value(struct dl *dl, const char *nla_name,
 						     mnl_attr_get_u8(val_attr),
 						     &vstr);
 			if (err)
-				return;
-			print_string(PRINT_ANY, "value", " value %s", vstr);
+				return err;
+			snprintf(format_str, sizeof(format_str), " %s %%s", label);
+			print_string(PRINT_ANY, label, format_str, vstr);
 		} else {
-			print_uint(PRINT_ANY, "value", " value %u",
+			snprintf(format_str, sizeof(format_str), " %s %%u", label);
+			print_uint(PRINT_ANY, label, format_str,
 				   mnl_attr_get_u8(val_attr));
 		}
 		break;
@@ -3569,10 +3552,12 @@ static void pr_out_param_value(struct dl *dl, const char *nla_name,
 						     mnl_attr_get_u16(val_attr),
 						     &vstr);
 			if (err)
-				return;
-			print_string(PRINT_ANY, "value", " value %s", vstr);
+				return err;
+			snprintf(format_str, sizeof(format_str), " %s %%s", label);
+			print_string(PRINT_ANY, label, format_str, vstr);
 		} else {
-			print_uint(PRINT_ANY, "value", " value %u",
+			snprintf(format_str, sizeof(format_str), " %s %%u", label);
+			print_uint(PRINT_ANY, label, format_str,
 				   mnl_attr_get_u16(val_attr));
 		}
 		break;
@@ -3584,21 +3569,56 @@ static void pr_out_param_value(struct dl *dl, const char *nla_name,
 						     mnl_attr_get_u32(val_attr),
 						     &vstr);
 			if (err)
-				return;
-			print_string(PRINT_ANY, "value", " value %s", vstr);
+				return err;
+			snprintf(format_str, sizeof(format_str), " %s %%s", label);
+			print_string(PRINT_ANY, label, format_str, vstr);
 		} else {
-			print_uint(PRINT_ANY, "value", " value %u",
+			snprintf(format_str, sizeof(format_str), " %s %%u", label);
+			print_uint(PRINT_ANY, label, format_str,
 				   mnl_attr_get_u32(val_attr));
 		}
 		break;
 	case MNL_TYPE_STRING:
-		print_string(PRINT_ANY, "value", " value %s",
+		snprintf(format_str, sizeof(format_str), " %s %%s", label);
+		print_string(PRINT_ANY, label, format_str,
 			     mnl_attr_get_str(val_attr));
 		break;
 	case MNL_TYPE_FLAG:
-		print_bool(PRINT_ANY, "value", " value %s", val_attr);
+		snprintf(format_str, sizeof(format_str), " %s %%s", label);
+		print_bool(PRINT_ANY, label, format_str, val_attr);
 		break;
 	}
+
+	return 0;
+}
+
+static void pr_out_param_value(struct dl *dl, const char *nla_name,
+			       int nla_type, struct nlattr *nl)
+{
+	struct nlattr *nla_value[DEVLINK_ATTR_MAX + 1] = {};
+	struct nlattr *val_attr;
+	bool conv_exists;
+	int err;
+
+	err = mnl_attr_parse_nested(nl, attr_cb, nla_value);
+	if (err != MNL_CB_OK)
+		return;
+
+	if (!nla_value[DEVLINK_ATTR_PARAM_VALUE_CMODE] ||
+	    (nla_type != MNL_TYPE_FLAG &&
+	     !nla_value[DEVLINK_ATTR_PARAM_VALUE_DATA]))
+		return;
+
+	check_indent_newline(dl);
+	print_string(PRINT_ANY, "cmode", "cmode %s",
+		     param_cmode_name(mnl_attr_get_u8(nla_value[DEVLINK_ATTR_PARAM_VALUE_CMODE])));
+
+	val_attr = nla_value[DEVLINK_ATTR_PARAM_VALUE_DATA];
+
+	conv_exists = param_val_conv_exists(param_val_conv, PARAM_VAL_CONV_LEN,
+					    nla_name);
+
+	pr_out_param_value_print(nla_name, nla_type, val_attr, conv_exists, "value");
 }
 
 static void pr_out_param(struct dl *dl, struct nlattr **tb, bool array,

-- 
2.47.3


  reply	other threads:[~2025-11-18  0:40 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-18  0:40 [PATCH iproute2-next 0/2] devlink: support default flag attr for param-get and param-set commands Daniel Zahka
2025-11-18  0:40 ` Daniel Zahka [this message]
2025-11-18 16:02   ` [PATCH iproute2-next 1/2] devlink: Pull the value printing logic out of pr_out_param_value() Stephen Hemminger
2025-11-18  0:40 ` [PATCH iproute2-next 2/2] devlink: support displaying and resetting to default params Daniel Zahka
2025-11-18  0:46   ` David Ahern

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=20251117-param-defaults-v1-1-c99604175d09@gmail.com \
    --to=daniel.zahka@gmail.com \
    --cc=davem@davemloft.net \
    --cc=donald.hunter@gmail.com \
    --cc=dsahern@kernel.org \
    --cc=horms@kernel.org \
    --cc=jiri@resnulli.us \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.