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

Add dumpit handler for resource-dump command to iterate over all devlink
devices and show their resources.

  $ devlink resource show
  pci/0000:08:00.0:
    name local_max_SFs size 508 unit entry
    name external_max_SFs size 508 unit entry
  pci/0000:08:00.1:
    name local_max_SFs size 508 unit entry
    name external_max_SFs size 508 unit entry

Signed-off-by: Or Har-Toov <[email protected]>
Reviewed-by: Shay Drori <[email protected]>
Reviewed-by: Moshe Shemesh <[email protected]>
Reviewed-by: Jiri Pirko <[email protected]>
Signed-off-by: Tariq Toukan <[email protected]>
---
 Documentation/netlink/specs/devlink.yaml |  6 +-
 net/devlink/netlink_gen.c                | 20 +++++-
 net/devlink/netlink_gen.h                |  4 +-
 net/devlink/resource.c                   | 77 ++++++++++++++++++++++++
 4 files changed, 102 insertions(+), 5 deletions(-)

diff --git a/Documentation/netlink/specs/devlink.yaml 
b/Documentation/netlink/specs/devlink.yaml
index b495d56b9137..c423e049c7bd 100644
--- a/Documentation/netlink/specs/devlink.yaml
+++ b/Documentation/netlink/specs/devlink.yaml
@@ -1764,13 +1764,17 @@ operations:
             - bus-name
             - dev-name
             - index
-        reply:
+        reply: &resource-dump-reply
           value: 36
           attributes:
             - bus-name
             - dev-name
             - index
             - resource-list
+      dump:
+        request:
+          attributes: *dev-id-attrs
+        reply: *resource-dump-reply
 
     -
       name: reload
diff --git a/net/devlink/netlink_gen.c b/net/devlink/netlink_gen.c
index eb35e80e01d1..a5a47a4c6de8 100644
--- a/net/devlink/netlink_gen.c
+++ b/net/devlink/netlink_gen.c
@@ -305,7 +305,14 @@ static const struct nla_policy 
devlink_resource_set_nl_policy[DEVLINK_ATTR_INDEX
 };
 
 /* DEVLINK_CMD_RESOURCE_DUMP - do */
-static const struct nla_policy 
devlink_resource_dump_nl_policy[DEVLINK_ATTR_INDEX + 1] = {
+static const struct nla_policy 
devlink_resource_dump_do_nl_policy[DEVLINK_ATTR_INDEX + 1] = {
+       [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING, },
+       [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING, },
+       [DEVLINK_ATTR_INDEX] = NLA_POLICY_FULL_RANGE(NLA_UINT, 
&devlink_attr_index_range),
+};
+
+/* DEVLINK_CMD_RESOURCE_DUMP - dump */
+static const struct nla_policy 
devlink_resource_dump_dump_nl_policy[DEVLINK_ATTR_INDEX + 1] = {
        [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING, },
        [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING, },
        [DEVLINK_ATTR_INDEX] = NLA_POLICY_FULL_RANGE(NLA_UINT, 
&devlink_attr_index_range),
@@ -680,7 +687,7 @@ static const struct nla_policy 
devlink_notify_filter_set_nl_policy[DEVLINK_ATTR_
 };
 
 /* Ops table for devlink */
-const struct genl_split_ops devlink_nl_ops[74] = {
+const struct genl_split_ops devlink_nl_ops[75] = {
        {
                .cmd            = DEVLINK_CMD_GET,
                .validate       = GENL_DONT_VALIDATE_STRICT,
@@ -958,10 +965,17 @@ const struct genl_split_ops devlink_nl_ops[74] = {
                .pre_doit       = devlink_nl_pre_doit,
                .doit           = devlink_nl_resource_dump_doit,
                .post_doit      = devlink_nl_post_doit,
-               .policy         = devlink_resource_dump_nl_policy,
+               .policy         = devlink_resource_dump_do_nl_policy,
                .maxattr        = DEVLINK_ATTR_INDEX,
                .flags          = GENL_CMD_CAP_DO,
        },
+       {
+               .cmd            = DEVLINK_CMD_RESOURCE_DUMP,
+               .dumpit         = devlink_nl_resource_dump_dumpit,
+               .policy         = devlink_resource_dump_dump_nl_policy,
+               .maxattr        = DEVLINK_ATTR_INDEX,
+               .flags          = GENL_CMD_CAP_DUMP,
+       },
        {
                .cmd            = DEVLINK_CMD_RELOAD,
                .validate       = GENL_DONT_VALIDATE_STRICT,
diff --git a/net/devlink/netlink_gen.h b/net/devlink/netlink_gen.h
index 2817d53a0eba..d79f6a0888f6 100644
--- a/net/devlink/netlink_gen.h
+++ b/net/devlink/netlink_gen.h
@@ -18,7 +18,7 @@ extern const struct nla_policy 
devlink_dl_rate_tc_bws_nl_policy[DEVLINK_RATE_TC_
 extern const struct nla_policy 
devlink_dl_selftest_id_nl_policy[DEVLINK_ATTR_SELFTEST_ID_FLASH + 1];
 
 /* Ops table for devlink */
-extern const struct genl_split_ops devlink_nl_ops[74];
+extern const struct genl_split_ops devlink_nl_ops[75];
 
 int devlink_nl_pre_doit(const struct genl_split_ops *ops, struct sk_buff *skb,
                        struct genl_info *info);
@@ -80,6 +80,8 @@ int devlink_nl_dpipe_table_counters_set_doit(struct sk_buff 
*skb,
                                             struct genl_info *info);
 int devlink_nl_resource_set_doit(struct sk_buff *skb, struct genl_info *info);
 int devlink_nl_resource_dump_doit(struct sk_buff *skb, struct genl_info *info);
+int devlink_nl_resource_dump_dumpit(struct sk_buff *skb,
+                                   struct netlink_callback *cb);
 int devlink_nl_reload_doit(struct sk_buff *skb, struct genl_info *info);
 int devlink_nl_param_get_doit(struct sk_buff *skb, struct genl_info *info);
 int devlink_nl_param_get_dumpit(struct sk_buff *skb,
diff --git a/net/devlink/resource.c b/net/devlink/resource.c
index f3014ec425c4..02fb36e25c52 100644
--- a/net/devlink/resource.c
+++ b/net/devlink/resource.c
@@ -223,6 +223,31 @@ static int devlink_resource_put(struct devlink *devlink, 
struct sk_buff *skb,
        return -EMSGSIZE;
 }
 
+static int devlink_resource_list_fill(struct sk_buff *skb,
+                                     struct devlink *devlink,
+                                     struct list_head *resource_list_head,
+                                     int *idx)
+{
+       struct devlink_resource *resource;
+       int i = 0;
+       int err;
+
+       list_for_each_entry(resource, resource_list_head, list) {
+               if (i < *idx) {
+                       i++;
+                       continue;
+               }
+               err = devlink_resource_put(devlink, skb, resource);
+               if (err) {
+                       *idx = i;
+                       return err;
+               }
+               i++;
+       }
+       *idx = 0;
+       return 0;
+}
+
 static int devlink_resource_fill(struct genl_info *info,
                                 enum devlink_command cmd, int flags)
 {
@@ -302,6 +327,58 @@ int devlink_nl_resource_dump_doit(struct sk_buff *skb, 
struct genl_info *info)
        return devlink_resource_fill(info, DEVLINK_CMD_RESOURCE_DUMP, 0);
 }
 
+static int
+devlink_nl_resource_dump_one(struct sk_buff *skb, struct devlink *devlink,
+                            struct netlink_callback *cb, int flags)
+{
+       struct devlink_nl_dump_state *state = devlink_dump_state(cb);
+       struct nlattr *resources_attr;
+       int start_idx = state->idx;
+       void *hdr;
+       int err;
+
+       if (list_empty(&devlink->resource_list))
+               return 0;
+
+       err = -EMSGSIZE;
+       hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
+                         &devlink_nl_family, flags, DEVLINK_CMD_RESOURCE_DUMP);
+       if (!hdr)
+               return err;
+
+       if (devlink_nl_put_handle(skb, devlink))
+               goto nla_put_failure;
+
+       resources_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_RESOURCE_LIST);
+       if (!resources_attr)
+               goto nla_put_failure;
+
+       err = devlink_resource_list_fill(skb, devlink,
+                                        &devlink->resource_list, &state->idx);
+       if (err) {
+               if (state->idx == start_idx)
+                       goto resource_list_cancel;
+               nla_nest_end(skb, resources_attr);
+               genlmsg_end(skb, hdr);
+               return err;
+       }
+       nla_nest_end(skb, resources_attr);
+       genlmsg_end(skb, hdr);
+       return 0;
+
+resource_list_cancel:
+       nla_nest_cancel(skb, resources_attr);
+nla_put_failure:
+       genlmsg_cancel(skb, hdr);
+       return err;
+}
+
+int devlink_nl_resource_dump_dumpit(struct sk_buff *skb,
+                                   struct netlink_callback *cb)
+{
+       return devlink_nl_dumpit(skb, cb, devlink_nl_resource_dump_one);
+}
+
 int devlink_resources_validate(struct devlink *devlink,
                               struct devlink_resource *resource,
                               struct genl_info *info)
-- 
2.44.0


Reply via email to