From: Parav Pandit <parav@nvidia.com>
To: <dsahern@gmail.com>, <stephen@networkplumber.org>,
<netdev@vger.kernel.org>
Cc: Parav Pandit <parav@nvidia.com>, Jiri Pirko <jiri@nvidia.com>
Subject: [PATCH iproute2-next v2 5/6] devlink: Support get port function state
Date: Mon, 1 Feb 2021 23:35:50 +0200 [thread overview]
Message-ID: <20210201213551.8503-6-parav@nvidia.com> (raw)
In-Reply-To: <20210201213551.8503-1-parav@nvidia.com>
Print port function state and operational state whenever reported by
kernel.
Example of a PCI SF port function which supports the state:
$ devlink dev eswitch set pci/0000:06:00.0 mode switchdev
$ devlink port show
pci/0000:06:00.0/65535: type eth netdev ens2f0np0 flavour physical port 0 splittable false
$ devlink port add pci/0000:06:00.0 flavour pcisf pfnum 0 sfnum 88
pci/0000:08:00.0/32768: type eth netdev eth6 flavour pcisf controller 0 pfnum 0 sfnum 88 splittable false
function:
hw_addr 00:00:00:00:00:00 state inactive opstate detached
$ devlink port show pci/0000:06:00.0/32768
pci/0000:06:00.0/32768: type eth netdev ens2f0npf0sf88 flavour pcisf controller 0 pfnum 0 sfnum 88 splittable false
function:
hw_addr 00:00:00:00:00:00 state inactive opstate detached
$ devlink port function set pci/0000:06:00.0/32768 hw_addr 00:00:00:00:88:88
$ devlink port show pci/0000:06:00.0/32768 -jp
{
"port": {
"pci/0000:06:00.0/32768": {
"type": "eth",
"netdev": "ens2f0npf0sf88",
"flavour": "pcisf",
"controller": 0,
"pfnum": 0,
"sfnum": 88,
"splittable": false,
"function": {
"hw_addr": "00:00:00:00:88:88",
"state": "inactive",
"opstate": "detached"
}
}
}
}
Signed-off-by: Parav Pandit <parav@nvidia.com>
Reviewed-by: Jiri Pirko <jiri@nvidia.com>
---
changelog:
v1->v2:
- using helper routine to perform string<->u8 mapping for state and
operational state
---
devlink/devlink.c | 61 ++++++++++++++++++++++++++++++++++++++++-------
include/utils.h | 1 +
lib/utils.c | 13 ++++++++++
3 files changed, 67 insertions(+), 8 deletions(-)
diff --git a/devlink/devlink.c b/devlink/devlink.c
index 76ea7cac..17db8623 100644
--- a/devlink/devlink.c
+++ b/devlink/devlink.c
@@ -1400,6 +1400,18 @@ static struct str_num_map port_flavour_map[] = {
{ .str = NULL, },
};
+static struct str_num_map port_fn_state_map[] = {
+ { .str = "inactive", .num = DEVLINK_PORT_FN_STATE_INACTIVE},
+ { .str = "active", .num = DEVLINK_PORT_FN_STATE_ACTIVE },
+ { .str = NULL, }
+};
+
+static struct str_num_map port_fn_opstate_map[] = {
+ { .str = "attached", .num = DEVLINK_PORT_FN_OPSTATE_ATTACHED},
+ { .str = "detached", .num = DEVLINK_PORT_FN_OPSTATE_DETACHED},
+ { .str = NULL, }
+};
+
static int port_flavour_parse(const char *flavour, uint16_t *value)
{
int num;
@@ -3810,6 +3822,22 @@ static void pr_out_port_pfvfsf_num(struct dl *dl, struct nlattr **tb)
}
}
+static const char *port_fn_state(uint8_t state)
+{
+ const char *str;
+
+ str = str_map_lookup_u8(port_fn_state_map, state);
+ return str ? str : "<unknown state>";
+}
+
+static const char *port_fn_opstate(uint8_t state)
+{
+ const char *str;
+
+ str = str_map_lookup_u8(port_fn_opstate_map, state);
+ return str ? str : "<unknown state>";
+}
+
static void pr_out_port_function(struct dl *dl, struct nlattr **tb_port)
{
struct nlattr *tb[DEVLINK_PORT_FUNCTION_ATTR_MAX + 1] = {};
@@ -3826,16 +3854,33 @@ static void pr_out_port_function(struct dl *dl, struct nlattr **tb_port)
if (err != MNL_CB_OK)
return;
- if (!tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR])
- return;
-
- len = mnl_attr_get_payload_len(tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR]);
- data = mnl_attr_get_payload(tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR]);
-
pr_out_object_start(dl, "function");
check_indent_newline(dl);
- print_string(PRINT_ANY, "hw_addr", "hw_addr %s",
- ll_addr_n2a(data, len, 0, hw_addr, sizeof(hw_addr)));
+
+ if (tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR]) {
+ len = mnl_attr_get_payload_len(tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR]);
+ data = mnl_attr_get_payload(tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR]);
+
+ print_string(PRINT_ANY, "hw_addr", "hw_addr %s",
+ ll_addr_n2a(data, len, 0, hw_addr, sizeof(hw_addr)));
+ }
+ if (tb[DEVLINK_PORT_FN_ATTR_STATE]) {
+ uint8_t state;
+
+ state = mnl_attr_get_u8(tb[DEVLINK_PORT_FN_ATTR_STATE]);
+
+ print_string(PRINT_ANY, "state", " state %s",
+ port_fn_state(state));
+ }
+ if (tb[DEVLINK_PORT_FN_ATTR_OPSTATE]) {
+ uint8_t state;
+
+ state = mnl_attr_get_u8(tb[DEVLINK_PORT_FN_ATTR_OPSTATE]);
+
+ print_string(PRINT_ANY, "opstate", " opstate %s",
+ port_fn_opstate(state));
+ }
+
if (!dl->json_output)
__pr_out_indent_dec();
pr_out_object_end(dl);
diff --git a/include/utils.h b/include/utils.h
index 1d67443e..e66090ae 100644
--- a/include/utils.h
+++ b/include/utils.h
@@ -347,5 +347,6 @@ struct str_num_map {
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);
+const char *str_map_lookup_u8(const struct str_num_map *map, uint8_t val);
#endif /* __UTILS_H__ */
diff --git a/lib/utils.c b/lib/utils.c
index 9fef2d76..af1b553c 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -1965,3 +1965,16 @@ const char *str_map_lookup_u16(const struct str_num_map *map, uint16_t val)
}
return NULL;
}
+
+const char *str_map_lookup_u8(const struct str_num_map *map, uint8_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 ` [PATCH iproute2-next v2 2/6] devlink: Introduce and use string to number mapper Parav Pandit
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 ` Parav Pandit [this message]
2021-02-01 21:35 ` [PATCH iproute2-next v2 6/6] devlink: Support set of port function state 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-6-parav@nvidia.com \
--to=parav@nvidia.com \
--cc=dsahern@gmail.com \
--cc=jiri@nvidia.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).