[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <1622637065-30803-3-git-send-email-dlinkin@nvidia.com>
Date: Wed, 2 Jun 2021 15:31:03 +0300
From: Dmytro Linkin <dlinkin@...dia.com>
To: <dlinkin@...dia.com>
CC: <davem@...emloft.net>, <dsahern@...il.com>, <huyn@...dia.com>,
<jiri@...dia.com>, <kuba@...nel.org>, <netdev@...r.kernel.org>,
<parav@...dia.com>, <stephen@...workplumber.org>,
<vladbu@...dia.com>
Subject: [PATCH RESEND iproute2 net-next 2/4] devlink: Add helper function to validate object handler
Every handler argument validated in two steps, first of which, form
checking, expects identifier is few words separated by slashes.
For device and region handlers just checked if identifier have expected
number of slashes.
Add generic function to do that and make code cleaner & consistent.
Signed-off-by: Dmytro Linkin <dlinkin@...dia.com>
Reviewed-by: Jiri Pirko <jiri@...dia.com>
---
devlink/devlink.c | 38 +++++++++++++++++++++-----------------
1 file changed, 21 insertions(+), 17 deletions(-)
diff --git a/devlink/devlink.c b/devlink/devlink.c
index 16eca4f..f435fc8 100644
--- a/devlink/devlink.c
+++ b/devlink/devlink.c
@@ -915,6 +915,19 @@ static int strtobool(const char *str, bool *p_val)
return 0;
}
+static int ident_str_validate(char *str, unsigned int expected)
+{
+ if (!str)
+ return -EINVAL;
+
+ if (get_str_char_count(str, '/') != expected) {
+ pr_err("Wrong identification string format.\n");
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
static int __dl_argv_handle(char *str, char **p_bus_name, char **p_dev_name)
{
str_split_by_char(str, p_bus_name, p_dev_name, '/');
@@ -924,15 +937,12 @@ static int __dl_argv_handle(char *str, char **p_bus_name, char **p_dev_name)
static int dl_argv_handle(struct dl *dl, char **p_bus_name, char **p_dev_name)
{
char *str = dl_argv_next(dl);
+ int err;
- if (!str) {
+ err = ident_str_validate(str, 1);
+ if (err) {
pr_err("Devlink identification (\"bus_name/dev_name\") expected\n");
- return -EINVAL;
- }
- if (get_str_char_count(str, '/') != 1) {
- pr_err("Wrong devlink identification string format.\n");
- pr_err("Expected \"bus_name/dev_name\".\n");
- return -EINVAL;
+ return err;
}
return __dl_argv_handle(str, p_bus_name, p_dev_name);
}
@@ -1067,18 +1077,12 @@ static int dl_argv_handle_region(struct dl *dl, char **p_bus_name,
char **p_dev_name, char **p_region)
{
char *str = dl_argv_next(dl);
- unsigned int slash_count;
+ int err;
- if (!str) {
- pr_err("Expected \"bus_name/dev_name/region\" identification.\n");
- return -EINVAL;
- }
-
- slash_count = get_str_char_count(str, '/');
- if (slash_count != 2) {
- pr_err("Wrong region identification string format.\n");
+ err = ident_str_validate(str, 2);
+ if (err) {
pr_err("Expected \"bus_name/dev_name/region\" identification.\n"".\n");
- return -EINVAL;
+ return err;
}
return __dl_argv_handle_region(str, p_bus_name, p_dev_name, p_region);
--
1.8.3.1
Powered by blists - more mailing lists