linma / rpms / iproute

Forked from rpms/iproute 4 years ago
Clone

Blame SOURCES/0056-devlink-Add-support-for-devlink-resource-abstraction.patch

99be8f
From 5190aa430d198420679e53163604f7b6860bcd0f Mon Sep 17 00:00:00 2001
99be8f
From: Andrea Claudi <aclaudi@redhat.com>
99be8f
Date: Mon, 25 Mar 2019 11:40:10 +0100
99be8f
Subject: [PATCH] devlink: Add support for devlink resource abstraction
99be8f
99be8f
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1644731
99be8f
Upstream Status: iproute2.git commit 8cd644095842a
99be8f
Conflicts: adjusted help printout due to missing commit
99be8f
           3e897912cbff9 ("devlink: add batch command support")
99be8f
99be8f
commit 8cd644095842af3107320e86eeb01be6af6c77bb
99be8f
Author: Arkadi Sharshevsky <arkadis@mellanox.com>
99be8f
Date:   Wed Feb 14 10:55:18 2018 +0200
99be8f
99be8f
    devlink: Add support for devlink resource abstraction
99be8f
99be8f
    Add support for devlink resource abstraction. The resources are
99be8f
    represented by a tree based structure and are identified by a name and
99be8f
    a size. Some resources can present their real time occupancy.
99be8f
99be8f
    First the resources exposed by the driver can be observed, for example:
99be8f
99be8f
    $devlink resource show pci/0000:03:00.0
99be8f
    pci/0000:03:00.0:
99be8f
      name kvd size 245760 unit entry
99be8f
        resources:
99be8f
          name linear size 98304 occ 0 unit entry size_min 0 size_max 147456 size_gran 128
99be8f
          name hash_double size 60416 unit entry size_min 32768 size_max 180224 size_gran 128
99be8f
          name hash_single size 87040 unit entry size_min 65536 size_max 212992 size_gran 128
99be8f
99be8f
    Some resource's size can be changed. Examples:
99be8f
99be8f
    $devlink resource set pci/0000:03:00.0 path /kvd/hash_single size 73088
99be8f
    $devlink resource set pci/0000:03:00.0 path /kvd/hash_double size 74368
99be8f
99be8f
    The changes do not apply immediately, this can be validate by the 'size_new'
99be8f
    attribute, which represents the pending changed size. For example
99be8f
99be8f
    $devlink resource show pci/0000:03:00.0
99be8f
    pci/0000:03:00.0:
99be8f
      name kvd size 245760 unit entry size_valid false
99be8f
      resources:
99be8f
        name linear size 98304 size_new 147456 occ 0 unit entry size_min 0 size_max 147456 size_gran 128
99be8f
        name hash_double size 60416 unit entry size_min 32768 size_max 180224 size_gran 128
99be8f
        name hash_single size 87040 unit entry size_min 65536 size_max 212992 size_gran 128
99be8f
99be8f
    In case of a pending change the nested resources present an indication
99be8f
    for a valid configuration of its children (sum of its children sizes
99be8f
    doesn't exceed the parent's size).
99be8f
99be8f
    In order for the changes to take place hot reload is needed. The hot
99be8f
    reload through devlink will be introduced in the following patch.
99be8f
99be8f
    Signed-off-by: Arkadi Sharshevsky <arkadis@mellanox.com>
99be8f
    Acked-by: Jiri Pirko <jiri@mellanox.com>
99be8f
    Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
99be8f
---
99be8f
 devlink/devlink.c | 490 +++++++++++++++++++++++++++++++++++++++++++++-
99be8f
 include/list.h    |   5 +
99be8f
 2 files changed, 494 insertions(+), 1 deletion(-)
99be8f
99be8f
diff --git a/devlink/devlink.c b/devlink/devlink.c
99be8f
index f9bc16c350c40..7f47b79450094 100644
99be8f
--- a/devlink/devlink.c
99be8f
+++ b/devlink/devlink.c
99be8f
@@ -177,6 +177,8 @@ static void ifname_map_free(struct ifname_map *ifname_map)
99be8f
 #define DL_OPT_DPIPE_TABLE_NAME	BIT(13)
99be8f
 #define DL_OPT_DPIPE_TABLE_COUNTERS	BIT(14)
99be8f
 #define DL_OPT_ESWITCH_ENCAP_MODE	BIT(15)
99be8f
+#define DL_OPT_RESOURCE_PATH	BIT(16)
99be8f
+#define DL_OPT_RESOURCE_SIZE	BIT(17)
99be8f
 
99be8f
 struct dl_opts {
99be8f
 	uint32_t present; /* flags of present items */
99be8f
@@ -197,6 +199,10 @@ struct dl_opts {
99be8f
 	const char *dpipe_table_name;
99be8f
 	bool dpipe_counters_enable;
99be8f
 	bool eswitch_encap_mode;
99be8f
+	const char *resource_path;
99be8f
+	uint32_t resource_size;
99be8f
+	uint32_t resource_id;
99be8f
+	bool resource_id_valid;
99be8f
 };
99be8f
 
99be8f
 struct dl {
99be8f
@@ -937,6 +943,20 @@ static int dl_argv_parse(struct dl *dl, uint32_t o_required,
99be8f
 			if (err)
99be8f
 				return err;
99be8f
 			o_found |= DL_OPT_ESWITCH_ENCAP_MODE;
99be8f
+		} else if (dl_argv_match(dl, "path") &&
99be8f
+			   (o_all & DL_OPT_RESOURCE_PATH)) {
99be8f
+			dl_arg_inc(dl);
99be8f
+			err = dl_argv_str(dl, &opts->resource_path);
99be8f
+			if (err)
99be8f
+				return err;
99be8f
+			o_found |= DL_OPT_RESOURCE_PATH;
99be8f
+		} else if (dl_argv_match(dl, "size") &&
99be8f
+			   (o_all & DL_OPT_RESOURCE_SIZE)) {
99be8f
+			dl_arg_inc(dl);
99be8f
+			err = dl_argv_uint32_t(dl, &opts->resource_size);
99be8f
+			if (err)
99be8f
+				return err;
99be8f
+			o_found |= DL_OPT_RESOURCE_SIZE;
99be8f
 		} else {
99be8f
 			pr_err("Unknown option \"%s\"\n", dl_argv(dl));
99be8f
 			return -EINVAL;
99be8f
@@ -1079,6 +1099,12 @@ static void dl_opts_put(struct nlmsghdr *nlh, struct dl *dl)
99be8f
 	if (opts->present & DL_OPT_ESWITCH_ENCAP_MODE)
99be8f
 		mnl_attr_put_u8(nlh, DEVLINK_ATTR_ESWITCH_ENCAP_MODE,
99be8f
 				opts->eswitch_encap_mode);
99be8f
+	if ((opts->present & DL_OPT_RESOURCE_PATH) && opts->resource_id_valid)
99be8f
+		mnl_attr_put_u64(nlh, DEVLINK_ATTR_RESOURCE_ID,
99be8f
+				 opts->resource_id);
99be8f
+	if (opts->present & DL_OPT_RESOURCE_SIZE)
99be8f
+		mnl_attr_put_u64(nlh, DEVLINK_ATTR_RESOURCE_SIZE,
99be8f
+				 opts->resource_size);
99be8f
 }
99be8f
 
99be8f
 static int dl_argv_parse_put(struct nlmsghdr *nlh, struct dl *dl,
99be8f
@@ -2666,6 +2692,91 @@ struct dpipe_header {
99be8f
 	unsigned int fields_count;
99be8f
 };
99be8f
 
99be8f
+struct resource {
99be8f
+	char *name;
99be8f
+	uint64_t size;
99be8f
+	uint64_t size_new;
99be8f
+	uint64_t size_min;
99be8f
+	uint64_t size_max;
99be8f
+	uint64_t size_gran;
99be8f
+	enum devlink_resource_unit unit;
99be8f
+	bool size_valid;
99be8f
+	uint64_t size_occ;
99be8f
+	bool occ_valid;
99be8f
+	uint64_t id;
99be8f
+	struct list_head list;
99be8f
+	struct list_head resource_list;
99be8f
+	struct resource *parent;
99be8f
+};
99be8f
+
99be8f
+struct resources {
99be8f
+	struct list_head resource_list;
99be8f
+};
99be8f
+
99be8f
+struct resource_ctx {
99be8f
+	struct dl *dl;
99be8f
+	int err;
99be8f
+	struct resources *resources;
99be8f
+	bool print_resources;
99be8f
+	bool pending_change;
99be8f
+};
99be8f
+
99be8f
+static struct resource *resource_alloc(void)
99be8f
+{
99be8f
+	struct resource *resource;
99be8f
+
99be8f
+	resource = calloc(1, sizeof(struct resource));
99be8f
+	if (!resource)
99be8f
+		return NULL;
99be8f
+	INIT_LIST_HEAD(&resource->resource_list);
99be8f
+	return resource;
99be8f
+}
99be8f
+
99be8f
+static void resource_free(struct resource *resource)
99be8f
+{
99be8f
+	struct resource *child_resource, *tmp;
99be8f
+
99be8f
+	list_for_each_entry_safe(child_resource, tmp, &resource->resource_list,
99be8f
+				 list) {
99be8f
+		free(child_resource->name);
99be8f
+		resource_free(child_resource);
99be8f
+	}
99be8f
+	free(resource);
99be8f
+}
99be8f
+
99be8f
+static struct resources *resources_alloc(void)
99be8f
+{
99be8f
+	struct resources *resources;
99be8f
+
99be8f
+	resources = calloc(1, sizeof(struct resources));
99be8f
+	if (!resources)
99be8f
+		return NULL;
99be8f
+	INIT_LIST_HEAD(&resources->resource_list);
99be8f
+	return resources;
99be8f
+}
99be8f
+
99be8f
+static void resources_free(struct resources *resources)
99be8f
+{
99be8f
+	struct resource *resource, *tmp;
99be8f
+
99be8f
+	list_for_each_entry_safe(resource, tmp, &resources->resource_list, list)
99be8f
+		resource_free(resource);
99be8f
+}
99be8f
+
99be8f
+static int resource_ctx_init(struct resource_ctx *ctx, struct dl *dl)
99be8f
+{
99be8f
+	ctx->resources = resources_alloc();
99be8f
+	if (!ctx->resources)
99be8f
+		return -ENOMEM;
99be8f
+	ctx->dl = dl;
99be8f
+	return 0;
99be8f
+}
99be8f
+
99be8f
+static void resource_ctx_fini(struct resource_ctx *ctx)
99be8f
+{
99be8f
+	resources_free(ctx->resources);
99be8f
+}
99be8f
+
99be8f
 struct dpipe_ctx {
99be8f
 	struct dl *dl;
99be8f
 	int err;
99be8f
@@ -3203,6 +3314,66 @@ err_match_show:
99be8f
 	return -EINVAL;
99be8f
 }
99be8f
 
99be8f
+static struct resource *
99be8f
+resource_find(struct resources *resources, struct resource *resource,
99be8f
+	      uint64_t resource_id)
99be8f
+{
99be8f
+	struct list_head *list_head;
99be8f
+
99be8f
+	if (!resource)
99be8f
+		list_head = &resources->resource_list;
99be8f
+	else
99be8f
+		list_head = &resource->resource_list;
99be8f
+
99be8f
+	list_for_each_entry(resource, list_head, list) {
99be8f
+		struct resource *child_resource;
99be8f
+
99be8f
+		if (resource->id == resource_id)
99be8f
+			return resource;
99be8f
+
99be8f
+		child_resource = resource_find(resources, resource,
99be8f
+					       resource_id);
99be8f
+		if (child_resource)
99be8f
+			return child_resource;
99be8f
+	}
99be8f
+	return NULL;
99be8f
+}
99be8f
+
99be8f
+static void
99be8f
+resource_path_print(struct dl *dl, struct resources *resources,
99be8f
+		    uint64_t resource_id)
99be8f
+{
99be8f
+	struct resource *resource, *parent_resource;
99be8f
+	const char del[] = "/";
99be8f
+	int path_len = 0;
99be8f
+	char *path;
99be8f
+
99be8f
+	resource = resource_find(resources, NULL, resource_id);
99be8f
+	if (!resource)
99be8f
+		return;
99be8f
+
99be8f
+	for (parent_resource = resource; parent_resource;
99be8f
+	     parent_resource = parent_resource->parent)
99be8f
+		path_len += strlen(parent_resource->name) + 1;
99be8f
+
99be8f
+	path_len++;
99be8f
+	path = calloc(1, path_len);
99be8f
+	if (!path)
99be8f
+		return;
99be8f
+
99be8f
+	path += path_len - 1;
99be8f
+	for (parent_resource = resource; parent_resource;
99be8f
+		parent_resource = parent_resource->parent) {
99be8f
+		path -= strlen(parent_resource->name);
99be8f
+		memcpy(path, parent_resource->name,
99be8f
+		       strlen(parent_resource->name));
99be8f
+		path -= strlen(del);
99be8f
+		memcpy(path, del, strlen(del));
99be8f
+	}
99be8f
+	pr_out_str(dl, "resource_path", path);
99be8f
+	free(path);
99be8f
+}
99be8f
+
99be8f
 static int dpipe_table_show(struct dpipe_ctx *ctx, struct nlattr *nl)
99be8f
 {
99be8f
 	struct nlattr *nla_table[DEVLINK_ATTR_MAX + 1] = {};
99be8f
@@ -3617,10 +3788,324 @@ static int cmd_dpipe(struct dl *dl)
99be8f
 	return -ENOENT;
99be8f
 }
99be8f
 
99be8f
+static int
99be8f
+resource_parse(struct resource_ctx *ctx, struct resource *resource,
99be8f
+	       struct nlattr **nla_resource)
99be8f
+{
99be8f
+	if (!nla_resource[DEVLINK_ATTR_RESOURCE_NAME] ||
99be8f
+	    !nla_resource[DEVLINK_ATTR_RESOURCE_SIZE] ||
99be8f
+	    !nla_resource[DEVLINK_ATTR_RESOURCE_ID] ||
99be8f
+	    !nla_resource[DEVLINK_ATTR_RESOURCE_UNIT] ||
99be8f
+	    !nla_resource[DEVLINK_ATTR_RESOURCE_SIZE_MIN] ||
99be8f
+	    !nla_resource[DEVLINK_ATTR_RESOURCE_SIZE_MAX] ||
99be8f
+	    !nla_resource[DEVLINK_ATTR_RESOURCE_SIZE_GRAN]) {
99be8f
+		return -EINVAL;
99be8f
+	}
99be8f
+
99be8f
+	resource->name = strdup(mnl_attr_get_str(nla_resource[DEVLINK_ATTR_RESOURCE_NAME]));
99be8f
+	resource->size = mnl_attr_get_u64(nla_resource[DEVLINK_ATTR_RESOURCE_SIZE]);
99be8f
+	resource->id = mnl_attr_get_u64(nla_resource[DEVLINK_ATTR_RESOURCE_ID]);
99be8f
+	resource->unit = mnl_attr_get_u8(nla_resource[DEVLINK_ATTR_RESOURCE_UNIT]);
99be8f
+	resource->size_min = mnl_attr_get_u64(nla_resource[DEVLINK_ATTR_RESOURCE_SIZE_MIN]);
99be8f
+	resource->size_max = mnl_attr_get_u64(nla_resource[DEVLINK_ATTR_RESOURCE_SIZE_MAX]);
99be8f
+	resource->size_gran = mnl_attr_get_u64(nla_resource[DEVLINK_ATTR_RESOURCE_SIZE_GRAN]);
99be8f
+
99be8f
+	if (nla_resource[DEVLINK_ATTR_RESOURCE_SIZE_NEW])
99be8f
+		resource->size_new = mnl_attr_get_u64(nla_resource[DEVLINK_ATTR_RESOURCE_SIZE_NEW]);
99be8f
+	else
99be8f
+		resource->size_new = resource->size;
99be8f
+
99be8f
+	if (nla_resource[DEVLINK_ATTR_RESOURCE_OCC]) {
99be8f
+		resource->size_occ = mnl_attr_get_u64(nla_resource[DEVLINK_ATTR_RESOURCE_OCC]);
99be8f
+		resource->occ_valid = true;
99be8f
+	}
99be8f
+
99be8f
+	if (resource->size_new != resource->size)
99be8f
+		ctx->pending_change = true;
99be8f
+
99be8f
+	return 0;
99be8f
+}
99be8f
+
99be8f
+static int
99be8f
+resource_get(struct resource_ctx *ctx, struct resource *resource,
99be8f
+	     struct resource *parent_resource, struct nlattr *nl)
99be8f
+{
99be8f
+	struct nlattr *nla_resource[DEVLINK_ATTR_MAX + 1] = {};
99be8f
+	struct nlattr *nla_child_resource;
99be8f
+	struct nlattr *nla_resources;
99be8f
+	bool top = false;
99be8f
+	int err;
99be8f
+
99be8f
+	if (!resource) {
99be8f
+		nla_resources = nl;
99be8f
+		top = true;
99be8f
+		goto out;
99be8f
+	}
99be8f
+
99be8f
+	err = mnl_attr_parse_nested(nl, attr_cb, nla_resource);
99be8f
+	if (err != MNL_CB_OK)
99be8f
+		return -EINVAL;
99be8f
+
99be8f
+	err = resource_parse(ctx, resource, nla_resource);
99be8f
+	if (err)
99be8f
+		return err;
99be8f
+
99be8f
+	resource->parent = parent_resource;
99be8f
+	if (!nla_resource[DEVLINK_ATTR_RESOURCE_LIST])
99be8f
+		return 0;
99be8f
+
99be8f
+	resource->size_valid = !!mnl_attr_get_u8(nla_resource[DEVLINK_ATTR_RESOURCE_SIZE_VALID]);
99be8f
+	nla_resources = nla_resource[DEVLINK_ATTR_RESOURCE_LIST];
99be8f
+out:
99be8f
+	mnl_attr_for_each_nested(nla_child_resource, nla_resources) {
99be8f
+		struct resource *child_resource;
99be8f
+		struct list_head *list;
99be8f
+
99be8f
+		child_resource = resource_alloc();
99be8f
+		if (!child_resource)
99be8f
+			return -ENOMEM;
99be8f
+
99be8f
+		if (top)
99be8f
+			list = &ctx->resources->resource_list;
99be8f
+		else
99be8f
+			list = &resource->resource_list;
99be8f
+
99be8f
+		list_add_tail(&child_resource->list, list);
99be8f
+		err = resource_get(ctx, child_resource, resource,
99be8f
+				   nla_child_resource);
99be8f
+		if (err)
99be8f
+			return err;
99be8f
+	}
99be8f
+
99be8f
+	return 0;
99be8f
+}
99be8f
+
99be8f
+static const char *resource_unit_str_get(enum devlink_resource_unit unit)
99be8f
+{
99be8f
+	switch (unit) {
99be8f
+	case DEVLINK_RESOURCE_UNIT_ENTRY: return "entry";
99be8f
+	default: return "<unknown unit>";
99be8f
+	}
99be8f
+}
99be8f
+
99be8f
+static void resource_show(struct resource *resource,
99be8f
+			  struct resource_ctx *ctx)
99be8f
+{
99be8f
+	struct resource *child_resource;
99be8f
+	struct dl *dl = ctx->dl;
99be8f
+
99be8f
+	pr_out_str(dl, "name", resource->name);
99be8f
+	if (dl->verbose)
99be8f
+		resource_path_print(dl, ctx->resources, resource->id);
99be8f
+	pr_out_uint(dl, "size", resource->size);
99be8f
+	if (resource->size != resource->size_new)
99be8f
+		pr_out_uint(dl, "size_new", resource->size_new);
99be8f
+	if (resource->occ_valid)
99be8f
+		pr_out_uint(dl, "occ", resource->size_occ);
99be8f
+	pr_out_str(dl, "unit", resource_unit_str_get(resource->unit));
99be8f
+
99be8f
+	if (resource->size_min != resource->size_max) {
99be8f
+		pr_out_uint(dl, "size_min", resource->size_min);
99be8f
+		pr_out_uint(dl, "size_max", resource->size_max);
99be8f
+		pr_out_uint(dl, "size_gran", resource->size_gran);
99be8f
+	}
99be8f
+
99be8f
+	if (list_empty(&resource->resource_list))
99be8f
+		return;
99be8f
+
99be8f
+	if (ctx->pending_change)
99be8f
+		pr_out_str(dl, "size_valid", resource->size_valid ?
99be8f
+			   "true" : "false");
99be8f
+	pr_out_array_start(dl, "resources");
99be8f
+	list_for_each_entry(child_resource, &resource->resource_list, list) {
99be8f
+		pr_out_entry_start(dl);
99be8f
+		resource_show(child_resource, ctx);
99be8f
+		pr_out_entry_end(dl);
99be8f
+	}
99be8f
+	pr_out_array_end(dl);
99be8f
+}
99be8f
+
99be8f
+static void
99be8f
+resources_show(struct resource_ctx *ctx, struct nlattr **tb)
99be8f
+{
99be8f
+	struct resources *resources = ctx->resources;
99be8f
+	struct resource *resource;
99be8f
+
99be8f
+	list_for_each_entry(resource, &resources->resource_list, list) {
99be8f
+		pr_out_handle_start_arr(ctx->dl, tb);
99be8f
+		resource_show(resource, ctx);
99be8f
+		pr_out_handle_end(ctx->dl);
99be8f
+	}
99be8f
+}
99be8f
+
99be8f
+static int resources_get(struct resource_ctx *ctx, struct nlattr **tb)
99be8f
+{
99be8f
+	return resource_get(ctx, NULL, NULL, tb[DEVLINK_ATTR_RESOURCE_LIST]);
99be8f
+}
99be8f
+
99be8f
+static int cmd_resource_dump_cb(const struct nlmsghdr *nlh, void *data)
99be8f
+{
99be8f
+	struct resource_ctx *ctx = data;
99be8f
+	struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
99be8f
+	struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
99be8f
+	int err;
99be8f
+
99be8f
+	mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
99be8f
+	if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
99be8f
+	    !tb[DEVLINK_ATTR_RESOURCE_LIST])
99be8f
+		return MNL_CB_ERROR;
99be8f
+
99be8f
+	err = resources_get(ctx, tb);
99be8f
+	if (err) {
99be8f
+		ctx->err = err;
99be8f
+		return MNL_CB_ERROR;
99be8f
+	}
99be8f
+
99be8f
+	if (ctx->print_resources)
99be8f
+		resources_show(ctx, tb);
99be8f
+
99be8f
+	return MNL_CB_OK;
99be8f
+}
99be8f
+
99be8f
+static int cmd_resource_show(struct dl *dl)
99be8f
+{
99be8f
+	struct nlmsghdr *nlh;
99be8f
+	struct resource_ctx ctx = {};
99be8f
+	int err;
99be8f
+
99be8f
+	nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_RESOURCE_DUMP,
99be8f
+			       NLM_F_REQUEST | NLM_F_ACK);
99be8f
+
99be8f
+	err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE, 0);
99be8f
+	if (err)
99be8f
+		return err;
99be8f
+
99be8f
+	err = resource_ctx_init(&ctx, dl);
99be8f
+	if (err)
99be8f
+		return err;
99be8f
+
99be8f
+	ctx.print_resources = true;
99be8f
+	pr_out_section_start(dl, "resources");
99be8f
+	err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_resource_dump_cb, &ctx;;
99be8f
+	pr_out_section_end(dl);
99be8f
+	resource_ctx_fini(&ctx;;
99be8f
+	return err;
99be8f
+}
99be8f
+
99be8f
+static void cmd_resource_help(void)
99be8f
+{
99be8f
+	pr_err("Usage: devlink resource show DEV\n"
99be8f
+	       "       devlink resource set DEV path PATH size SIZE\n");
99be8f
+}
99be8f
+
99be8f
+static struct resource *
99be8f
+resource_find_by_name(struct list_head *list, char *name)
99be8f
+{
99be8f
+	struct resource *resource;
99be8f
+
99be8f
+	list_for_each_entry(resource, list, list) {
99be8f
+		if (!strcmp(resource->name, name))
99be8f
+			return resource;
99be8f
+	}
99be8f
+	return NULL;
99be8f
+}
99be8f
+
99be8f
+static int
99be8f
+resource_path_parse(struct resource_ctx *ctx, const char *resource_path,
99be8f
+		    uint32_t *p_resource_id, bool *p_resource_valid)
99be8f
+{
99be8f
+	struct resource *resource;
99be8f
+	uint32_t resource_id = 0;
99be8f
+	char *resource_path_dup;
99be8f
+	struct list_head *list;
99be8f
+	const char del[] = "/";
99be8f
+	char *resource_name;
99be8f
+
99be8f
+	resource_path_dup = strdup(resource_path);
99be8f
+	list = &ctx->resources->resource_list;
99be8f
+	resource_name = strtok(resource_path_dup, del);
99be8f
+	while (resource_name != NULL) {
99be8f
+		resource = resource_find_by_name(list, resource_name);
99be8f
+		if (!resource)
99be8f
+			goto err_resource_lookup;
99be8f
+
99be8f
+		list = &resource->resource_list;
99be8f
+		resource_name = strtok(NULL, del);
99be8f
+		resource_id = resource->id;
99be8f
+	}
99be8f
+	free(resource_path_dup);
99be8f
+	*p_resource_valid = true;
99be8f
+	*p_resource_id = resource_id;
99be8f
+	return 0;
99be8f
+
99be8f
+err_resource_lookup:
99be8f
+	free(resource_path_dup);
99be8f
+	return -EINVAL;
99be8f
+}
99be8f
+
99be8f
+static int cmd_resource_set(struct dl *dl)
99be8f
+{
99be8f
+	struct nlmsghdr *nlh;
99be8f
+	struct resource_ctx ctx = {};
99be8f
+	int err;
99be8f
+
99be8f
+	err = resource_ctx_init(&ctx, dl);
99be8f
+	if (err)
99be8f
+		return err;
99be8f
+
99be8f
+	ctx.print_resources = false;
99be8f
+	err = dl_argv_parse(dl, DL_OPT_HANDLE | DL_OPT_RESOURCE_PATH |
99be8f
+			    DL_OPT_RESOURCE_SIZE, 0);
99be8f
+	if (err)
99be8f
+		goto out;
99be8f
+
99be8f
+	nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_RESOURCE_DUMP,
99be8f
+			       NLM_F_REQUEST);
99be8f
+	dl_opts_put(nlh, dl);
99be8f
+	err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_resource_dump_cb, &ctx;;
99be8f
+	if (err) {
99be8f
+		pr_err("error getting resources %s\n", strerror(ctx.err));
99be8f
+		goto out;
99be8f
+	}
99be8f
+
99be8f
+	err = resource_path_parse(&ctx, dl->opts.resource_path,
99be8f
+				  &dl->opts.resource_id,
99be8f
+				  &dl->opts.resource_id_valid);
99be8f
+	if (err) {
99be8f
+		pr_err("error parsing resource path %s\n", strerror(err));
99be8f
+		goto out;
99be8f
+	}
99be8f
+
99be8f
+	nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_RESOURCE_SET,
99be8f
+			       NLM_F_REQUEST | NLM_F_ACK);
99be8f
+
99be8f
+	dl_opts_put(nlh, dl);
99be8f
+	err = _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
99be8f
+out:
99be8f
+	resource_ctx_fini(&ctx;;
99be8f
+	return err;
99be8f
+}
99be8f
+
99be8f
+static int cmd_resource(struct dl *dl)
99be8f
+{
99be8f
+	if (dl_argv_match(dl, "help") || dl_no_arg(dl)) {
99be8f
+		cmd_resource_help();
99be8f
+		return 0;
99be8f
+	} else if (dl_argv_match(dl, "show")) {
99be8f
+		dl_arg_inc(dl);
99be8f
+		return cmd_resource_show(dl);
99be8f
+	} else if (dl_argv_match(dl, "set")) {
99be8f
+		dl_arg_inc(dl);
99be8f
+		return cmd_resource_set(dl);
99be8f
+	}
99be8f
+	pr_err("Command \"%s\" not found\n", dl_argv(dl));
99be8f
+	return -ENOENT;
99be8f
+}
99be8f
+
99be8f
 static void help(void)
99be8f
 {
99be8f
 	pr_err("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n"
99be8f
-	       "where  OBJECT := { dev | port | sb | monitor | dpipe }\n"
99be8f
+	       "where  OBJECT := { dev | port | sb | monitor | dpipe | resource }\n"
99be8f
 	       "       OPTIONS := { -V[ersion] | -n[no-nice-names] | -j[json] | -p[pretty] | -v[verbose] }\n");
99be8f
 }
99be8f
 
99be8f
@@ -3644,6 +4129,9 @@ static int dl_cmd(struct dl *dl)
99be8f
 	} else if (dl_argv_match(dl, "dpipe")) {
99be8f
 		dl_arg_inc(dl);
99be8f
 		return cmd_dpipe(dl);
99be8f
+	} else if (dl_argv_match(dl, "resource")) {
99be8f
+		dl_arg_inc(dl);
99be8f
+		return cmd_resource(dl);
99be8f
 	}
99be8f
 	pr_err("Object \"%s\" not found\n", dl_argv(dl));
99be8f
 	return -ENOENT;
99be8f
diff --git a/include/list.h b/include/list.h
99be8f
index 5b529dc6e5211..b2adf55578449 100644
99be8f
--- a/include/list.h
99be8f
+++ b/include/list.h
99be8f
@@ -107,6 +107,11 @@ static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
99be8f
 	n->pprev = &h->first;
99be8f
 }
99be8f
 
99be8f
+static inline int list_empty(const struct list_head *head)
99be8f
+{
99be8f
+	return head->next == head;
99be8f
+}
99be8f
+
99be8f
 #define hlist_for_each(pos, head) \
99be8f
 	for (pos = (head)->first; pos ; pos = pos->next)
99be8f
 
99be8f
-- 
d30c09
2.21.0
99be8f