[PATCH iproute2-next 1/2] devlink: Pull the value printing logic out of pr_out_param_value()

Daniel Zahka posted 2 patches 2 weeks ago
[PATCH iproute2-next 1/2] devlink: Pull the value printing logic out of pr_out_param_value()
Posted by Daniel Zahka 2 weeks ago
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
Re: [PATCH iproute2-next 1/2] devlink: Pull the value printing logic out of pr_out_param_value()
Posted by Stephen Hemminger 1 week, 6 days ago
On Mon, 17 Nov 2025 16:40:02 -0800
Daniel Zahka <daniel.zahka@gmail.com> wrote:

> -			print_uint(PRINT_ANY, "value", " value %u",
> +			snprintf(format_str, sizeof(format_str), " %s %%u", label);
> +			print_uint(PRINT_ANY, label, format_str,

The problem with generating format strings is that it makes it difficult to
impossible to use any of the compiler format validation flags.