From: Parav Pandit <parav@nvidia.com>
To: <dsahern@gmail.com>, <stephen@networkplumber.org>,
<netdev@vger.kernel.org>
Cc: Parav Pandit <parav@nvidia.com>
Subject: [PATCH iproute2-next v2 2/6] devlink: Introduce and use string to number mapper
Date: Mon, 1 Feb 2021 23:35:47 +0200 [thread overview]
Message-ID: <20210201213551.8503-3-parav@nvidia.com> (raw)
In-Reply-To: <20210201213551.8503-1-parav@nvidia.com>
Instead of using static mapping in code, introduce a helper routine to
map a value to string.
Signed-off-by: Parav Pandit <parav@nvidia.com>
---
changelog:
v1->v2:
- addressed David's comment to use helper routine to map num->string
---
devlink/devlink.c | 30 ++++++++++++++----------------
include/utils.h | 8 ++++++++
lib/utils.c | 28 ++++++++++++++++++++++++++++
3 files changed, 50 insertions(+), 16 deletions(-)
diff --git a/devlink/devlink.c b/devlink/devlink.c
index a2e06644..d21a7c4d 100644
--- a/devlink/devlink.c
+++ b/devlink/devlink.c
@@ -1383,6 +1383,16 @@ static int reload_limit_get(struct dl *dl, const char *limitstr,
return 0;
}
+static struct str_num_map port_flavour_map[] = {
+ { .str = "physical", .num = DEVLINK_PORT_FLAVOUR_PHYSICAL },
+ { .str = "cpu", .num = DEVLINK_PORT_FLAVOUR_CPU },
+ { .str = "dsa", .num = DEVLINK_PORT_FLAVOUR_DSA },
+ { .str = "pcipf", .num = DEVLINK_PORT_FLAVOUR_PCI_PF },
+ { .str = "pcivf", .num = DEVLINK_PORT_FLAVOUR_PCI_VF },
+ { .str = "virtual", .num = DEVLINK_PORT_FLAVOUR_VIRTUAL},
+ { .str = NULL, },
+};
+
struct dl_args_metadata {
uint64_t o_flag;
char err_msg[DL_ARGS_REQUIRED_MAX_ERR_LEN];
@@ -3717,22 +3727,10 @@ static const char *port_type_name(uint32_t type)
static const char *port_flavour_name(uint16_t flavour)
{
- switch (flavour) {
- case DEVLINK_PORT_FLAVOUR_PHYSICAL:
- return "physical";
- case DEVLINK_PORT_FLAVOUR_CPU:
- return "cpu";
- case DEVLINK_PORT_FLAVOUR_DSA:
- return "dsa";
- case DEVLINK_PORT_FLAVOUR_PCI_PF:
- return "pcipf";
- case DEVLINK_PORT_FLAVOUR_PCI_VF:
- return "pcivf";
- case DEVLINK_PORT_FLAVOUR_VIRTUAL:
- return "virtual";
- default:
- return "<unknown flavour>";
- }
+ const char *str;
+
+ str = str_map_lookup_u16(port_flavour_map, flavour);
+ return str ? str : "<unknown flavour>";
}
static void pr_out_port_pfvf_num(struct dl *dl, struct nlattr **tb)
diff --git a/include/utils.h b/include/utils.h
index f1403f73..1d67443e 100644
--- a/include/utils.h
+++ b/include/utils.h
@@ -340,4 +340,12 @@ int parse_mapping(int *argcp, char ***argvp, bool allow_all,
int (*mapping_cb)(__u32 key, char *value, void *data),
void *mapping_cb_data);
+struct str_num_map {
+ const char *str;
+ int num;
+};
+
+int str_map_lookup_str(const struct str_num_map *map, const char *needle);
+const char *str_map_lookup_u16(const struct str_num_map *map, uint16_t val);
+
#endif /* __UTILS_H__ */
diff --git a/lib/utils.c b/lib/utils.c
index 90e58fa3..9fef2d76 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -1937,3 +1937,31 @@ int parse_mapping(int *argcp, char ***argvp, bool allow_all,
return parse_mapping_gen(argcp, argvp, parse_mapping_num,
mapping_cb, mapping_cb_data);
}
+
+int str_map_lookup_str(const struct str_num_map *map, const char *needle)
+{
+ if (!needle)
+ return -EINVAL;
+
+ /* Process array which is NULL terminated by the string. */
+ while (map && map->str) {
+ if (strcmp(map->str, needle) == 0)
+ return map->num;
+
+ map++;
+ }
+ return -EINVAL;
+}
+
+const char *str_map_lookup_u16(const struct str_num_map *map, uint16_t val)
+{
+ int num = val;
+
+ while (map && map->str) {
+ if (num == map->num)
+ return map->str;
+
+ map++;
+ }
+ return NULL;
+}
--
2.26.2
next prev parent reply other threads:[~2021-02-01 21:37 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-01-29 16:56 [PATCH iproute2-next 0/5] Support devlink port add delete Parav Pandit
2021-01-29 16:56 ` [PATCH iproute2-next 1/5] devlink: Update kernel headers Parav Pandit
2021-01-29 16:56 ` [PATCH iproute2-next 2/5] devlink: Introduce PCI SF port flavour and attribute Parav Pandit
2021-01-29 16:56 ` [PATCH iproute2-next 3/5] devlink: Supporting add and delete of devlink port Parav Pandit
2021-01-31 17:26 ` David Ahern
2021-02-01 20:42 ` Parav Pandit
2021-01-29 16:56 ` [PATCH iproute2-next 4/5] devlink: Support get port function state Parav Pandit
2021-01-29 16:56 ` [PATCH iproute2-next 5/5] devlink: Support set of " Parav Pandit
2021-01-31 17:30 ` David Ahern
2021-02-01 20:50 ` Parav Pandit
2021-02-01 21:35 ` [PATCH iproute2-next v2 0/6] Support devlink port add delete Parav Pandit
2021-02-01 21:35 ` [PATCH iproute2-next v2 1/6] devlink: Update kernel headers Parav Pandit
2021-02-01 21:35 ` Parav Pandit [this message]
2021-02-01 21:35 ` [PATCH iproute2-next v2 3/6] devlink: Introduce PCI SF port flavour and attribute Parav Pandit
2021-02-01 21:35 ` [PATCH iproute2-next v2 4/6] devlink: Supporting add and delete of devlink port Parav Pandit
2021-02-01 21:35 ` [PATCH iproute2-next v2 5/6] devlink: Support get port function state Parav Pandit
2021-02-01 21:35 ` [PATCH iproute2-next v2 6/6] devlink: Support set of " Parav Pandit
2021-02-02 3:20 ` [PATCH iproute2-next v2 0/6] Support devlink port add delete David Ahern
2021-02-02 4:01 ` Parav Pandit
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=20210201213551.8503-3-parav@nvidia.com \
--to=parav@nvidia.com \
--cc=dsahern@gmail.com \
--cc=netdev@vger.kernel.org \
--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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).