[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20251114120555.2430520-3-poros@redhat.com>
Date: Fri, 14 Nov 2025 13:05:54 +0100
From: Petr Oros <poros@...hat.com>
To: netdev@...r.kernel.org
Cc: dsahern@...nel.org,
stephen@...workplumber.org,
ivecera@...hat.com,
jiri@...nulli.us,
Petr Oros <poros@...hat.com>
Subject: [PATCH iproute2-next v3 2/3] lib: Add str_to_bool helper function
Add str_to_bool() helper function to lib/utils.c that uses
parse_one_of() to parse boolean values. Update devlink to
use this common implementation.
Signed-off-by: Petr Oros <poros@...hat.com>
---
devlink/devlink.c | 22 +++-------------------
include/utils.h | 1 +
lib/utils.c | 17 +++++++++++++++++
3 files changed, 21 insertions(+), 19 deletions(-)
diff --git a/devlink/devlink.c b/devlink/devlink.c
index b162cf4050f918..3dc55118424d20 100644
--- a/devlink/devlink.c
+++ b/devlink/devlink.c
@@ -1038,22 +1038,6 @@ static int ifname_map_rev_lookup(struct dl *dl, const char *bus_name,
return -ENOENT;
}
-static int strtobool(const char *str, bool *p_val)
-{
- bool val;
-
- if (!strcmp(str, "true") || !strcmp(str, "1") ||
- !strcmp(str, "enable"))
- val = true;
- else if (!strcmp(str, "false") || !strcmp(str, "0") ||
- !strcmp(str, "disable"))
- val = false;
- else
- return -EINVAL;
- *p_val = val;
- return 0;
-}
-
static int ident_str_validate(char *str, unsigned int expected)
{
if (!str)
@@ -1356,7 +1340,7 @@ static int dl_argv_bool(struct dl *dl, bool *p_val)
return -EINVAL;
}
- err = strtobool(str, p_val);
+ err = str_to_bool(str, p_val);
if (err) {
pr_err("\"%s\" is not a valid boolean value\n", str);
return err;
@@ -3821,7 +3805,7 @@ static int cmd_dev_param_set(struct dl *dl)
mnl_attr_put_u32(nlh, DEVLINK_ATTR_PARAM_VALUE_DATA, val_u32);
break;
case MNL_TYPE_FLAG:
- err = strtobool(dl->opts.param_value, &val_bool);
+ err = str_to_bool(dl->opts.param_value, &val_bool);
if (err)
goto err_param_value_parse;
if (val_bool == ctx.value.vbool)
@@ -5395,7 +5379,7 @@ static int cmd_port_param_set(struct dl *dl)
mnl_attr_put_u32(nlh, DEVLINK_ATTR_PARAM_VALUE_DATA, val_u32);
break;
case MNL_TYPE_FLAG:
- err = strtobool(dl->opts.param_value, &val_bool);
+ err = str_to_bool(dl->opts.param_value, &val_bool);
if (err)
goto err_param_value_parse;
if (val_bool == ctx.value.vbool)
diff --git a/include/utils.h b/include/utils.h
index 7d9b3cfb35a665..e0a9c780cb9eb1 100644
--- a/include/utils.h
+++ b/include/utils.h
@@ -351,6 +351,7 @@ int parse_one_of_deprecated(const char *msg, const char *realval,
const char * const *list,
size_t len, int *p_err);
bool parse_on_off(const char *msg, const char *realval, int *p_err);
+int str_to_bool(const char *str, bool *p_val);
int parse_mapping_num_all(__u32 *keyp, const char *key);
int parse_mapping_gen(int *argcp, char ***argvp,
diff --git a/lib/utils.c b/lib/utils.c
index dd242d4d672e47..0719281a059d14 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -1820,6 +1820,23 @@ bool parse_on_off(const char *msg, const char *realval, int *p_err)
ARRAY_SIZE(values_on_off), p_err, strcmp);
}
+int str_to_bool(const char *str, bool *p_val)
+{
+ static const char * const values[] = {
+ "false", "true",
+ "0", "1",
+ "disable", "enable"
+ };
+ int err, index;
+
+ index = parse_one_of(NULL, str, values, ARRAY_SIZE(values), &err);
+ if (err)
+ return err;
+
+ *p_val = index & 1;
+ return 0;
+}
+
int parse_mapping_gen(int *argcp, char ***argvp,
int (*key_cb)(__u32 *keyp, const char *key),
int (*mapping_cb)(__u32 key, char *value, void *data),
--
2.51.0
Powered by blists - more mailing lists