From: Or Har-Toov <[email protected]>

Add optional 'scope { dev | port }' argument to 'devlink resource show'
without a device handle to filter the full dump to device-level or
port-level resources only.

Example - dump only device-level resources:

  $ devlink resource show scope dev
  pci/0000:03:00.0:
    name max_local_SFs size 128 unit entry dpipe_tables none
    name max_external_SFs size 128 unit entry dpipe_tables none
  pci/0000:03:00.1:
    name max_local_SFs size 128 unit entry dpipe_tables none
    name max_external_SFs size 128 unit entry dpipe_tables none

Example - dump only port-level resources:

  $ devlink resource show scope port
  pci/0000:03:00.0/196608:
    name max_SFs size 128 unit entry dpipe_tables none
  pci/0000:03:00.0/196609:
    name max_SFs size 128 unit entry dpipe_tables none
  pci/0000:03:00.1/196708:
    name max_SFs size 128 unit entry dpipe_tables none
  pci/0000:03:00.1/196709:
    name max_SFs size 128 unit entry dpipe_tables none

Signed-off-by: Or Har-Toov <[email protected]>
Signed-off-by: Tariq Toukan <[email protected]>
---
 bash-completion/devlink     |  7 ++++++
 devlink/devlink.c           | 47 ++++++++++++++++++++++++++++++++-----
 man/man8/devlink-resource.8 | 11 ++++++++-
 3 files changed, 58 insertions(+), 7 deletions(-)

diff --git a/bash-completion/devlink b/bash-completion/devlink
index 3d8452a8869e..bfc0083f647b 100644
--- a/bash-completion/devlink
+++ b/bash-completion/devlink
@@ -702,9 +702,16 @@ _devlink_resource()
 {
     case $command in
         show)
+            case $prev in
+                scope)
+                    COMPREPLY=( $( compgen -W "dev port" -- "$cur" ) )
+                    return
+                    ;;
+            esac
             if [[ $cword -eq 3 ]]; then
                 _devlink_direct_complete "dev"
                 _devlink_direct_complete "port"
+                COMPREPLY+=( $( compgen -W "scope" -- "$cur" ) )
             fi
             return
             ;;
diff --git a/devlink/devlink.c b/devlink/devlink.c
index 4224b7fa6792..1a94b6413048 100644
--- a/devlink/devlink.c
+++ b/devlink/devlink.c
@@ -314,6 +314,7 @@ static int ifname_map_update(struct ifname_map *ifname_map, 
const char *ifname)
 #define DL_OPT_PORT_FN_RATE_TC_BWS     BIT(59)
 #define DL_OPT_HEALTH_REPORTER_BURST_PERIOD    BIT(60)
 #define DL_OPT_PARAM_SET_DEFAULT       BIT(61)
+#define DL_OPT_RESOURCE_SCOPE          BIT(62)
 
 struct dl_opts {
        uint64_t present; /* flags of present items */
@@ -382,6 +383,7 @@ struct dl_opts {
        bool selftests_opt[DEVLINK_ATTR_SELFTEST_ID_MAX + 1];
        struct nla_bitfield32 port_fn_caps;
        uint32_t port_fn_max_io_eqs;
+       uint32_t resource_scope_mask;
 };
 
 struct dl {
@@ -1467,6 +1469,19 @@ static int flash_overwrite_section_get(const char 
*sectionstr, uint32_t *mask)
        return 0;
 }
 
+static int resource_scope_get(const char *scopestr, uint32_t *scope)
+{
+       if (strcmp(scopestr, "dev") == 0) {
+               *scope = DEVLINK_RESOURCE_SCOPE_DEV;
+       } else if (strcmp(scopestr, "port") == 0) {
+               *scope = DEVLINK_RESOURCE_SCOPE_PORT;
+       } else {
+               pr_err("Unknown resource scope \"%s\"\n", scopestr);
+               return -EINVAL;
+       }
+       return 0;
+}
+
 static int param_cmode_get(const char *cmodestr,
                           enum devlink_param_cmode *cmode)
 {
@@ -1647,6 +1662,7 @@ static const struct dl_args_metadata dl_args_required[] = 
{
        {DL_OPT_ESWITCH_ENCAP_MODE,   "E-Switch encapsulation option 
expected."},
        {DL_OPT_RESOURCE_PATH,        "Resource path expected."},
        {DL_OPT_RESOURCE_SIZE,        "Resource size expected."},
+       {DL_OPT_RESOURCE_SCOPE,       "Resource scope expected."},
        {DL_OPT_PARAM_NAME,           "Parameter name expected."},
        {DL_OPT_PARAM_VALUE,          "Value to set expected."},
        {DL_OPT_PARAM_CMODE,          "Configuration mode expected."},
@@ -2662,6 +2678,9 @@ static void dl_opts_put(struct nlmsghdr *nlh, struct dl 
*dl)
        if (opts->present & DL_OPT_RESOURCE_SIZE)
                mnl_attr_put_u64(nlh, DEVLINK_ATTR_RESOURCE_SIZE,
                                 opts->resource_size);
+       if (opts->present & DL_OPT_RESOURCE_SCOPE)
+               mnl_attr_put_u32(nlh, DEVLINK_ATTR_RESOURCE_SCOPE_MASK,
+                                opts->resource_scope_mask);
        if (opts->present & DL_OPT_PARAM_NAME)
                mnl_attr_put_strz(nlh, DEVLINK_ATTR_PARAM_NAME,
                                  opts->param_name);
@@ -9010,13 +9029,29 @@ static int cmd_resource_show(struct dl *dl)
        uint16_t flags = NLM_F_REQUEST | NLM_F_ACK;
        struct nlmsghdr *nlh;
        struct resource_ctx resource_ctx = {};
+       struct dl_opts *opts = &dl->opts;
        int err;
 
-       err = dl_argv_parse_with_selector(dl, &flags, DEVLINK_CMD_RESOURCE_DUMP,
-                                         DL_OPT_HANDLE | DL_OPT_HANDLEP,
-                                         0, 0, 0);
-       if (err)
-               return err;
+       if (dl_argv_match(dl, "scope")) {
+               const char *scopestr;
+
+               dl_arg_inc(dl);
+               err = dl_argv_str(dl, &scopestr);
+               if (err)
+                       return err;
+               err = resource_scope_get(scopestr, &opts->resource_scope_mask);
+               if (err)
+                       return err;
+               opts->present |= DL_OPT_RESOURCE_SCOPE;
+               flags |= NLM_F_DUMP;
+       } else {
+               err = dl_argv_parse_with_selector(dl, &flags,
+                                                 DEVLINK_CMD_RESOURCE_DUMP,
+                                                 DL_OPT_HANDLE | 
DL_OPT_HANDLEP,
+                                                 0, 0, 0);
+               if (err)
+                       return err;
+       }
 
        err = resource_ctx_init(&resource_ctx, dl);
        if (err)
@@ -9036,7 +9071,7 @@ static int cmd_resource_show(struct dl *dl)
 
 static void cmd_resource_help(void)
 {
-       pr_err("Usage: devlink resource show [ DEV[/PORT_INDEX] ]\n"
+       pr_err("Usage: devlink resource show [ DEV[/PORT_INDEX] | scope { dev | 
port } ]\n"
               "       devlink resource set DEV path PATH size SIZE\n");
 }
 
diff --git a/man/man8/devlink-resource.8 b/man/man8/devlink-resource.8
index 1e7d96126ce5..04cde2bf8958 100644
--- a/man/man8/devlink-resource.8
+++ b/man/man8/devlink-resource.8
@@ -19,7 +19,7 @@ devlink-resource \- devlink device resource configuration
 
 .ti -8
 .B devlink resource show
-.RI "[ " DEV "[/" PORT_INDEX "] ]"
+.RI "[ " DEV "[/" PORT_INDEX "] | " scope " { " dev " | " port " } ]"
 
 .ti -8
 .B devlink resource help
@@ -53,6 +53,15 @@ Format is:
 .in +2
 BUS_NAME/BUS_ADDRESS/PORT_INDEX
 
+.TP
+.BI scope " { dev | port }"
+Filter resources by scope.
+.B dev
+shows only device-level resources.
+.B port
+shows only port-level resources.
+When omitted, resources of both scopes are shown.
+
 .SS devlink resource set - sets resource size of specific resource
 
 .PP
-- 
2.44.0


Reply via email to