From: Saeed Mahameed <saeed@kernel.org>
To: stephen@networkplumber.org, dsahern@gmail.com,
Jiri Pirko <jiri@nvidia.com>,
jiri@resnulli.us
Cc: netdev@vger.kernel.org, Saeed Mahameed <saeedm@nvidia.com>
Subject: [PATCH iproute2 07/10] devlink: helper function to compare dl_params
Date: Thu, 27 Feb 2025 18:18:34 -0800 [thread overview]
Message-ID: <20250228021837.880041-8-saeed@kernel.org> (raw)
In-Reply-To: <20250228021837.880041-1-saeed@kernel.org>
From: Saeed Mahameed <saeedm@nvidia.com>
In cmd_{dev,port}_param_set, we compare the kernel param values with the
user input, fold this code into a helper function.
Signed-off-by: Saeed Mahameed <saeedm@nvidia.com>
---
devlink/devlink.c | 62 ++++++++++++++++++++++++++++++++---------------
1 file changed, 42 insertions(+), 20 deletions(-)
diff --git a/devlink/devlink.c b/devlink/devlink.c
index edcc5a79..938eb3fb 100644
--- a/devlink/devlink.c
+++ b/devlink/devlink.c
@@ -3711,6 +3711,36 @@ static int dl_param_opts_get(struct dl *dl, enum devlink_dyn_attr_type type,
return err;
}
+/* dl_param_cmp: compare two dl_param structs
+ * @p1: first dl_param struct
+ * @p2: second dl_param struct
+ * Returns: 0 if the two structs are equal, 1 when different,
+ * -errno on validation error
+ */
+static int dl_param_cmp(struct dl_param *p1, struct dl_param *p2)
+{
+ if (p1->type != p2->type)
+ return -EINVAL;
+
+ switch (p1->type) {
+ case DEVLINK_DYN_ATTR_TYPE_U8:
+ return p1->value.vu8 != p2->value.vu8;
+ case DEVLINK_DYN_ATTR_TYPE_U16:
+ return p1->value.vu16 != p2->value.vu16;
+ case DEVLINK_DYN_ATTR_TYPE_U32:
+ return p1->value.vu32 != p2->value.vu32;
+ case DEVLINK_DYN_ATTR_TYPE_FLAG:
+ return p1->value.vbool != p2->value.vbool;
+ case DEVLINK_DYN_ATTR_TYPE_STRING:
+ if (strcmp(p1->value.vstr, p2->value.vstr))
+ return 1;
+ break;
+ default:
+ return -EINVAL;
+ }
+ return 0;
+}
+
static int cmd_param_set_cb(const struct nlmsghdr *nlh, void *data)
{
struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
@@ -3821,26 +3851,24 @@ static int cmd_dev_param_set(struct dl *dl)
if (err)
goto err_param_value_parse;
+ err = dl_param_cmp(&uparam, &kparam);
+ if (err < 0)
+ goto err_param_value_parse;
+ if (!err) /* Value is the same */
+ return 0;
+
mnl_attr_put_u8(nlh, DEVLINK_ATTR_PARAM_TYPE, kparam.type);
switch (kparam.type) {
case DEVLINK_DYN_ATTR_TYPE_U8:
- if (uparam.value.vu8 == kparam.value.vu8)
- return 0;
mnl_attr_put_u8(nlh, DEVLINK_ATTR_PARAM_VALUE_DATA, uparam.value.vu8);
break;
case DEVLINK_DYN_ATTR_TYPE_U16:
- if (uparam.value.vu16 == kparam.value.vu16)
- return 0;
mnl_attr_put_u16(nlh, DEVLINK_ATTR_PARAM_VALUE_DATA, uparam.value.vu16);
break;
case DEVLINK_DYN_ATTR_TYPE_U32:
- if (uparam.value.vu32 == kparam.value.vu32)
- return 0;
mnl_attr_put_u32(nlh, DEVLINK_ATTR_PARAM_VALUE_DATA, uparam.value.vu32);
break;
case DEVLINK_DYN_ATTR_TYPE_FLAG:
- if (uparam.value.vbool == kparam.value.vbool)
- return 0;
if (uparam.value.vbool)
mnl_attr_put(nlh, DEVLINK_ATTR_PARAM_VALUE_DATA,
0, NULL);
@@ -3848,8 +3876,6 @@ static int cmd_dev_param_set(struct dl *dl)
case DEVLINK_DYN_ATTR_TYPE_STRING:
mnl_attr_put_strz(nlh, DEVLINK_ATTR_PARAM_VALUE_DATA,
uparam.value.vstr);
- if (!strcmp(uparam.value.vstr, kparam.value.vstr))
- return 0;
break;
default:
printf("Value type not supported\n");
@@ -5285,26 +5311,24 @@ static int cmd_port_param_set(struct dl *dl)
if (err)
goto err_param_value_parse;
+ err = dl_param_cmp(&uparam, &kparam);
+ if (err < 0)
+ goto err_param_value_parse;
+ if (!err) /* Value is the same */
+ return 0;
+
mnl_attr_put_u8(nlh, DEVLINK_ATTR_PARAM_TYPE, kparam.type);
switch (kparam.type) {
case DEVLINK_DYN_ATTR_TYPE_U8:
- if (uparam.value.vu8 == kparam.value.vu8)
- return 0;
mnl_attr_put_u8(nlh, DEVLINK_ATTR_PARAM_VALUE_DATA, uparam.value.vu8);
break;
case DEVLINK_DYN_ATTR_TYPE_U16:
- if (uparam.value.vu16 == kparam.value.vu16)
- return 0;
mnl_attr_put_u16(nlh, DEVLINK_ATTR_PARAM_VALUE_DATA, uparam.value.vu16);
break;
case DEVLINK_DYN_ATTR_TYPE_U32:
- if (uparam.value.vu32 == kparam.value.vu32)
- return 0;
mnl_attr_put_u32(nlh, DEVLINK_ATTR_PARAM_VALUE_DATA, uparam.value.vu32);
break;
case DEVLINK_DYN_ATTR_TYPE_FLAG:
- if (uparam.value.vbool == kparam.value.vbool)
- return 0;
if (uparam.value.vbool)
mnl_attr_put(nlh, DEVLINK_ATTR_PARAM_VALUE_DATA,
0, NULL);
@@ -5312,8 +5336,6 @@ static int cmd_port_param_set(struct dl *dl)
case DEVLINK_DYN_ATTR_TYPE_STRING:
mnl_attr_put_strz(nlh, DEVLINK_ATTR_PARAM_VALUE_DATA,
uparam.value.vstr);
- if (!strcmp(uparam.value.vstr, kparam.value.vstr))
- return 0;
break;
default:
printf("Value type not supported\n");
--
2.48.1
next prev parent reply other threads:[~2025-02-28 2:19 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-28 2:18 [PATCH iproute2 00/10] devlink params nested multi-attribute values Saeed Mahameed
2025-02-28 2:18 ` [PATCH iproute2 01/10] update kernel headers Saeed Mahameed
2025-02-28 2:18 ` [PATCH iproute2 02/10] devlink: use dynamic attributes enum Saeed Mahameed
2025-02-28 2:18 ` [PATCH iproute2 03/10] devlink: param show: handle multi-attribute values Saeed Mahameed
2025-02-28 2:18 ` [PATCH iproute2 04/10] devlink: param set: reuse cmd_dev_param_set_cb for port params set Saeed Mahameed
2025-02-28 2:18 ` [PATCH iproute2 05/10] devlink: rename param_ctx to dl_param Saeed Mahameed
2025-02-28 2:18 ` [PATCH iproute2 06/10] devlink: helper function to read user param input into dl_param Saeed Mahameed
2025-02-28 2:18 ` Saeed Mahameed [this message]
2025-02-28 2:18 ` [PATCH iproute2 08/10] devlink: helper function to put param value mnl attributes from dl_params Saeed Mahameed
2025-02-28 2:18 ` [PATCH iproute2 09/10] devlink: helper function to parse param vlaue attributes into dl_param Saeed Mahameed
2025-02-28 2:18 ` [PATCH iproute2 10/10] devlink: params set: add support for nested attributes values Saeed Mahameed
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=20250228021837.880041-8-saeed@kernel.org \
--to=saeed@kernel.org \
--cc=dsahern@gmail.com \
--cc=jiri@nvidia.com \
--cc=jiri@resnulli.us \
--cc=netdev@vger.kernel.org \
--cc=saeedm@nvidia.com \
--cc=stephen@networkplumber.org \
/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.