All of lore.kernel.org
 help / color / mirror / Atom feed
* [patch iproute2-next 0/4] expose devlink instances relationships
@ 2023-09-18 10:54 Jiri Pirko
  2023-09-18 10:54 ` [patch iproute2-next 1/4] devlink: update headers Jiri Pirko
                   ` (3 more replies)
  0 siblings, 4 replies; 10+ messages in thread
From: Jiri Pirko @ 2023-09-18 10:54 UTC (permalink / raw)
  To: netdev; +Cc: stephen, dsahern

From: Jiri Pirko <jiri@nvidia.com>

Print out recently added attributes that expose relationships between
devlink instances. This patchset extends the outputs by
"nested_devlink" and "nested_devlink_netns" attributes.

Examples:
$ devlink dev
pci/0000:08:00.0: nested_devlink auxiliary/mlx5_core.eth.0
pci/0000:08:00.1: nested_devlink auxiliary/mlx5_core.eth.1
auxiliary/mlx5_core.eth.1
auxiliary/mlx5_core.eth.0

$ devlink port add pci/0000:08:00.0 flavour pcisf pfnum 0 sfnum 106
pci/0000:08:00.0/32768: type eth netdev eth4 flavour pcisf controller 0 pfnum 0 sfnum 106 splittable false
  function:
    hw_addr 00:00:00:00:00:00 state inactive opstate detached roce enable
$ devlink port function set pci/0000:08:00.0/32768 state active
$ devlink port show pci/0000:08:00.0/32768
pci/0000:08:00.0/32768: type eth netdev eth4 flavour pcisf controller 0 pfnum 0 sfnum 106 splittable false
  function:
    hw_addr 00:00:00:00:00:00 state active opstate attached roce enable nested_devlink auxiliary/mlx5_core.sf.2

# devlink dev reload auxiliary/mlx5_core.sf.2 netns ns1
$ devlink port show pci/0000:08:00.0/32768
pci/0000:08:00.0/32768: type eth netdev eth4 flavour pcisf controller 0 pfnum 0 sfnum 106 splittable false
  function:
    hw_addr 00:00:00:00:00:00 state active opstate attached roce enable nested_devlink auxiliary/mlx5_core.sf.2 nested_devlink_netns ns1

Jiri Pirko (4):
  devlink: update headers
  devlink: introduce support for netns id for nested handle
  devlink: print nested handle for port function
  devlink: print nested devlink handle for devlink dev

 devlink/devlink.c            | 153 ++++++++++++++++++++++++++++++++++-
 include/uapi/linux/devlink.h |   1 +
 2 files changed, 150 insertions(+), 4 deletions(-)

-- 
2.41.0


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [patch iproute2-next 1/4] devlink: update headers
  2023-09-18 10:54 [patch iproute2-next 0/4] expose devlink instances relationships Jiri Pirko
@ 2023-09-18 10:54 ` Jiri Pirko
  2023-09-18 10:54 ` [patch iproute2-next 2/4] devlink: introduce support for netns id for nested handle Jiri Pirko
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 10+ messages in thread
From: Jiri Pirko @ 2023-09-18 10:54 UTC (permalink / raw)
  To: netdev; +Cc: stephen, dsahern

From: Jiri Pirko <jiri@nvidia.com>

Update the devlink headers to recent net-next.

Signed-off-by: Jiri Pirko <jiri@nvidia.com>
---
 include/uapi/linux/devlink.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/include/uapi/linux/devlink.h b/include/uapi/linux/devlink.h
index 8b9b98e75059..6c4721270910 100644
--- a/include/uapi/linux/devlink.h
+++ b/include/uapi/linux/devlink.h
@@ -680,6 +680,7 @@ enum devlink_port_function_attr {
 	DEVLINK_PORT_FN_ATTR_STATE,	/* u8 */
 	DEVLINK_PORT_FN_ATTR_OPSTATE,	/* u8 */
 	DEVLINK_PORT_FN_ATTR_CAPS,	/* bitfield32 */
+	DEVLINK_PORT_FN_ATTR_DEVLINK,	/* nested */
 
 	__DEVLINK_PORT_FUNCTION_ATTR_MAX,
 	DEVLINK_PORT_FUNCTION_ATTR_MAX = __DEVLINK_PORT_FUNCTION_ATTR_MAX - 1
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [patch iproute2-next 2/4] devlink: introduce support for netns id for nested handle
  2023-09-18 10:54 [patch iproute2-next 0/4] expose devlink instances relationships Jiri Pirko
  2023-09-18 10:54 ` [patch iproute2-next 1/4] devlink: update headers Jiri Pirko
@ 2023-09-18 10:54 ` Jiri Pirko
  2023-09-18 11:34   ` Daniel Machon
  2023-09-19  3:06   ` David Ahern
  2023-09-18 10:54 ` [patch iproute2-next 3/4] devlink: print nested handle for port function Jiri Pirko
  2023-09-18 10:54 ` [patch iproute2-next 4/4] devlink: print nested devlink handle for devlink dev Jiri Pirko
  3 siblings, 2 replies; 10+ messages in thread
From: Jiri Pirko @ 2023-09-18 10:54 UTC (permalink / raw)
  To: netdev; +Cc: stephen, dsahern

From: Jiri Pirko <jiri@nvidia.com>

Nested handle may contain DEVLINK_ATTR_NETNS_ID attribute that indicates
the network namespace where the nested devlink instance resides. Process
this converting to netns name if possible and print to user.

Signed-off-by: Jiri Pirko <jiri@nvidia.com>
---
 devlink/devlink.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 105 insertions(+)

diff --git a/devlink/devlink.c b/devlink/devlink.c
index d1795f616ca0..31dd29452c39 100644
--- a/devlink/devlink.c
+++ b/devlink/devlink.c
@@ -24,6 +24,7 @@
 #include <linux/genetlink.h>
 #include <linux/devlink.h>
 #include <linux/netlink.h>
+#include <linux/net_namespace.h>
 #include <libmnl/libmnl.h>
 #include <netinet/ether.h>
 #include <sys/select.h>
@@ -722,6 +723,7 @@ static const enum mnl_attr_data_type devlink_policy[DEVLINK_ATTR_MAX + 1] = {
 	[DEVLINK_ATTR_LINECARD_SUPPORTED_TYPES] = MNL_TYPE_NESTED,
 	[DEVLINK_ATTR_NESTED_DEVLINK] = MNL_TYPE_NESTED,
 	[DEVLINK_ATTR_SELFTESTS] = MNL_TYPE_NESTED,
+	[DEVLINK_ATTR_NETNS_ID] = MNL_TYPE_U32,
 };
 
 static const enum mnl_attr_data_type
@@ -2723,6 +2725,85 @@ static bool should_arr_last_handle_end(struct dl *dl, const char *bus_name,
 	       !cmp_arr_last_handle(dl, bus_name, dev_name);
 }
 
+static int32_t netns_id_by_name(const char *name)
+{
+	struct {
+		struct nlmsghdr n;
+		struct rtgenmsg g;
+		char            buf[1024];
+	} req = {
+		.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
+		.n.nlmsg_flags = NLM_F_REQUEST,
+		.n.nlmsg_type = RTM_GETNSID,
+		.g.rtgen_family = AF_UNSPEC,
+	};
+	int ret = NETNSA_NSID_NOT_ASSIGNED;
+	struct rtattr *tb[NETNSA_MAX + 1];
+	struct nlmsghdr *n = NULL;
+	struct rtnl_handle rth;
+	struct rtgenmsg *rtg;
+	int len;
+	int fd;
+
+	fd = netns_get_fd(name);
+	if (fd < 0)
+		return ret;
+
+	if (rtnl_open(&rth, 0) < 0)
+		return ret;
+
+	addattr32(&req.n, sizeof(req), NETNSA_FD, fd);
+	if (rtnl_talk(&rth, &req.n, &n) < 0)
+		goto out;
+
+	if (n->nlmsg_type == NLMSG_ERROR)
+		goto out;
+
+	rtg = NLMSG_DATA(n);
+	len = n->nlmsg_len;
+
+	len -= NLMSG_SPACE(sizeof(*rtg));
+	if (len < 0)
+		goto out;
+
+	parse_rtattr(tb, NETNSA_MAX, NETNS_RTA(rtg), len);
+	if (tb[NETNSA_NSID])
+		ret = rta_getattr_s32(tb[NETNSA_NSID]);
+
+out:
+	free(n);
+	rtnl_close(&rth);
+	close(fd);
+	return ret;
+}
+
+struct netns_name_by_id_ctx {
+	int32_t id;
+	char *name;
+};
+
+static int nesns_name_by_id_func(char *nsname, void *arg)
+{
+	struct netns_name_by_id_ctx *ctx = arg;
+	int32_t ret;
+
+	ret = netns_id_by_name(nsname);
+	if (ret < 0 || ret != ctx->id)
+		return 0;
+	ctx->name = strdup(nsname);
+	return 1;
+}
+
+static char *netns_name_by_id(int32_t id)
+{
+	struct netns_name_by_id_ctx ctx = {
+		.id = id,
+	};
+
+	netns_foreach(nesns_name_by_id_func, &ctx);
+	return ctx.name;
+}
+
 static void pr_out_nested_handle(struct nlattr *nla_nested_dl)
 {
 	struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
@@ -2740,6 +2821,30 @@ static void pr_out_nested_handle(struct nlattr *nla_nested_dl)
 	sprintf(buf, "%s/%s", mnl_attr_get_str(tb[DEVLINK_ATTR_BUS_NAME]),
 		mnl_attr_get_str(tb[DEVLINK_ATTR_DEV_NAME]));
 	print_string(PRINT_ANY, "nested_devlink", " nested_devlink %s", buf);
+
+	if (tb[DEVLINK_ATTR_NETNS_ID]) {
+		int32_t id = mnl_attr_get_u32(tb[DEVLINK_ATTR_NETNS_ID]);
+
+		if (id >= 0) {
+			char *name = netns_name_by_id(id);
+
+			if (name) {
+				print_string(PRINT_ANY,
+					     "nested_devlink_netns",
+					     " nested_devlink_netns %s", name);
+				free(name);
+			} else {
+				print_int(PRINT_ANY,
+					  "nested_devlink_netnsid",
+					  " nested_devlink_netnsid %d", id);
+			}
+		} else {
+			print_string(PRINT_FP, NULL,
+				     " nested_devlink_netnsid %s", "unknown");
+			print_int(PRINT_JSON,
+				  "nested_devlink_netnsid", NULL, id);
+		}
+	}
 }
 
 static void __pr_out_handle_start(struct dl *dl, struct nlattr **tb,
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [patch iproute2-next 3/4] devlink: print nested handle for port function
  2023-09-18 10:54 [patch iproute2-next 0/4] expose devlink instances relationships Jiri Pirko
  2023-09-18 10:54 ` [patch iproute2-next 1/4] devlink: update headers Jiri Pirko
  2023-09-18 10:54 ` [patch iproute2-next 2/4] devlink: introduce support for netns id for nested handle Jiri Pirko
@ 2023-09-18 10:54 ` Jiri Pirko
  2023-09-18 10:54 ` [patch iproute2-next 4/4] devlink: print nested devlink handle for devlink dev Jiri Pirko
  3 siblings, 0 replies; 10+ messages in thread
From: Jiri Pirko @ 2023-09-18 10:54 UTC (permalink / raw)
  To: netdev; +Cc: stephen, dsahern

From: Jiri Pirko <jiri@nvidia.com>

If port function contains nested handle attribute, print it.

Signed-off-by: Jiri Pirko <jiri@nvidia.com>
---
 devlink/devlink.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/devlink/devlink.c b/devlink/devlink.c
index 31dd29452c39..8ea7b268c63c 100644
--- a/devlink/devlink.c
+++ b/devlink/devlink.c
@@ -772,6 +772,7 @@ static const enum mnl_attr_data_type
 devlink_function_policy[DEVLINK_PORT_FUNCTION_ATTR_MAX + 1] = {
 	[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR ] = MNL_TYPE_BINARY,
 	[DEVLINK_PORT_FN_ATTR_STATE] = MNL_TYPE_U8,
+	[DEVLINK_PORT_FN_ATTR_DEVLINK] = MNL_TYPE_NESTED,
 };
 
 static int function_attr_cb(const struct nlattr *attr, void *data)
@@ -4875,6 +4876,8 @@ static void pr_out_port_function(struct dl *dl, struct nlattr **tb_port)
 				     port_fn_caps->value & DEVLINK_PORT_FN_CAP_MIGRATABLE ?
 				     "enable" : "disable");
 	}
+	if (tb[DEVLINK_PORT_FN_ATTR_DEVLINK])
+		pr_out_nested_handle(tb[DEVLINK_PORT_FN_ATTR_DEVLINK]);
 
 	if (!dl->json_output)
 		__pr_out_indent_dec();
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [patch iproute2-next 4/4] devlink: print nested devlink handle for devlink dev
  2023-09-18 10:54 [patch iproute2-next 0/4] expose devlink instances relationships Jiri Pirko
                   ` (2 preceding siblings ...)
  2023-09-18 10:54 ` [patch iproute2-next 3/4] devlink: print nested handle for port function Jiri Pirko
@ 2023-09-18 10:54 ` Jiri Pirko
  3 siblings, 0 replies; 10+ messages in thread
From: Jiri Pirko @ 2023-09-18 10:54 UTC (permalink / raw)
  To: netdev; +Cc: stephen, dsahern

From: Jiri Pirko <jiri@nvidia.com>

Devlink dev may contain one or more nested devlink instances. If one is
present, print it out simple. If more are present (there is no
such case in current kernel, but may be in theory in the future),
print them in array.

Signed-off-by: Jiri Pirko <jiri@nvidia.com>
---
 devlink/devlink.c | 45 +++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 41 insertions(+), 4 deletions(-)

diff --git a/devlink/devlink.c b/devlink/devlink.c
index 8ea7b268c63c..a387da0f4995 100644
--- a/devlink/devlink.c
+++ b/devlink/devlink.c
@@ -3889,13 +3889,50 @@ static void pr_out_reload_data(struct dl *dl, struct nlattr **tb)
 	pr_out_object_end(dl);
 }
 
+static void pr_out_dev_nested(struct dl *dl, const struct nlmsghdr *nlh)
+{
+	struct nlattr *attr, *attr2;
+	int count = 0;
+
+	mnl_attr_for_each(attr, nlh, sizeof(struct genlmsghdr)) {
+		if (mnl_attr_get_type(attr) == DEVLINK_ATTR_NESTED_DEVLINK) {
+			count++;
+			attr2 = attr;
+		}
+	}
+	if (!count) {
+		return;
+	} else if (count == 1) {
+		pr_out_nested_handle(attr2);
+		return;
+	}
+
+	pr_out_array_start(dl, "nested_devlinks");
+	mnl_attr_for_each(attr, nlh, sizeof(struct genlmsghdr)) {
+		if (mnl_attr_get_type(attr) == DEVLINK_ATTR_NESTED_DEVLINK) {
+			check_indent_newline(dl);
+			if (dl->json_output)
+				open_json_object(NULL);
+			check_indent_newline(dl);
+			pr_out_nested_handle(attr);
+			if (dl->json_output)
+				close_json_object();
+			else
+				__pr_out_newline();
+		}
+	}
+	pr_out_array_end(dl);
+}
 
-static void pr_out_dev(struct dl *dl, struct nlattr **tb)
+static void pr_out_dev(struct dl *dl, const struct nlmsghdr *nlh,
+		       struct nlattr **tb)
 {
 	if ((tb[DEVLINK_ATTR_RELOAD_FAILED] && mnl_attr_get_u8(tb[DEVLINK_ATTR_RELOAD_FAILED])) ||
-	    (tb[DEVLINK_ATTR_DEV_STATS] && dl->stats)) {
+	    (tb[DEVLINK_ATTR_DEV_STATS] && dl->stats) ||
+	     tb[DEVLINK_ATTR_NESTED_DEVLINK]) {
 		__pr_out_handle_start(dl, tb, true, false);
 		pr_out_reload_data(dl, tb);
+		pr_out_dev_nested(dl, nlh);
 		pr_out_handle_end(dl);
 	} else {
 		pr_out_handle(dl, tb);
@@ -3912,7 +3949,7 @@ static int cmd_dev_show_cb(const struct nlmsghdr *nlh, void *data)
 	if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME])
 		return MNL_CB_ERROR;
 
-	pr_out_dev(dl, tb);
+	pr_out_dev(dl, nlh, tb);
 	return MNL_CB_OK;
 }
 
@@ -6828,7 +6865,7 @@ static int cmd_mon_show_cb(const struct nlmsghdr *nlh, void *data)
 			return MNL_CB_ERROR;
 		pr_out_mon_header(genl->cmd);
 		dl->stats = true;
-		pr_out_dev(dl, tb);
+		pr_out_dev(dl, nlh, tb);
 		pr_out_mon_footer();
 		break;
 	case DEVLINK_CMD_PORT_GET: /* fall through */
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [patch iproute2-next 2/4] devlink: introduce support for netns id for nested handle
  2023-09-18 10:54 ` [patch iproute2-next 2/4] devlink: introduce support for netns id for nested handle Jiri Pirko
@ 2023-09-18 11:34   ` Daniel Machon
  2023-09-18 12:29     ` Jiri Pirko
  2023-09-19  3:06   ` David Ahern
  1 sibling, 1 reply; 10+ messages in thread
From: Daniel Machon @ 2023-09-18 11:34 UTC (permalink / raw)
  To: Jiri Pirko; +Cc: netdev, stephen, dsahern

> +static int nesns_name_by_id_func(char *nsname, void *arg)

Hi Jiri,
nesns -> netns?

> +{
> +       struct netns_name_by_id_ctx *ctx = arg;
> +       int32_t ret;
> +
> +       ret = netns_id_by_name(nsname);
> +       if (ret < 0 || ret != ctx->id)
> +               return 0;
> +       ctx->name = strdup(nsname);
> +       return 1;
> +}
> +
> +static char *netns_name_by_id(int32_t id)
> +{
> +       struct netns_name_by_id_ctx ctx = {
> +               .id = id,
> +       };
> +
> +       netns_foreach(nesns_name_by_id_func, &ctx);

.. and here

> +       return ctx.name;
> +}
> +

/Daniel

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [patch iproute2-next 2/4] devlink: introduce support for netns id for nested handle
  2023-09-18 11:34   ` Daniel Machon
@ 2023-09-18 12:29     ` Jiri Pirko
  0 siblings, 0 replies; 10+ messages in thread
From: Jiri Pirko @ 2023-09-18 12:29 UTC (permalink / raw)
  To: Daniel Machon; +Cc: netdev, stephen, dsahern

Mon, Sep 18, 2023 at 01:34:48PM CEST, daniel.machon@microchip.com wrote:
>> +static int nesns_name_by_id_func(char *nsname, void *arg)
>
>Hi Jiri,
>nesns -> netns?
>
>> +{
>> +       struct netns_name_by_id_ctx *ctx = arg;
>> +       int32_t ret;
>> +
>> +       ret = netns_id_by_name(nsname);
>> +       if (ret < 0 || ret != ctx->id)
>> +               return 0;
>> +       ctx->name = strdup(nsname);
>> +       return 1;
>> +}
>> +
>> +static char *netns_name_by_id(int32_t id)
>> +{
>> +       struct netns_name_by_id_ctx ctx = {
>> +               .id = id,
>> +       };
>> +
>> +       netns_foreach(nesns_name_by_id_func, &ctx);
>
>.. and here

Okay, will fix this typo. Thx.


>
>> +       return ctx.name;
>> +}
>> +
>
>/Daniel

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [patch iproute2-next 2/4] devlink: introduce support for netns id for nested handle
  2023-09-18 10:54 ` [patch iproute2-next 2/4] devlink: introduce support for netns id for nested handle Jiri Pirko
  2023-09-18 11:34   ` Daniel Machon
@ 2023-09-19  3:06   ` David Ahern
  2023-09-19  7:05     ` Jiri Pirko
  1 sibling, 1 reply; 10+ messages in thread
From: David Ahern @ 2023-09-19  3:06 UTC (permalink / raw)
  To: Jiri Pirko, netdev; +Cc: stephen

On 9/18/23 4:54 AM, Jiri Pirko wrote:
>  static const enum mnl_attr_data_type
> @@ -2723,6 +2725,85 @@ static bool should_arr_last_handle_end(struct dl *dl, const char *bus_name,
>  	       !cmp_arr_last_handle(dl, bus_name, dev_name);
>  }
>  
> +static int32_t netns_id_by_name(const char *name)
> +{
> +	struct {
> +		struct nlmsghdr n;
> +		struct rtgenmsg g;
> +		char            buf[1024];
> +	} req = {
> +		.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
> +		.n.nlmsg_flags = NLM_F_REQUEST,
> +		.n.nlmsg_type = RTM_GETNSID,
> +		.g.rtgen_family = AF_UNSPEC,
> +	};
> +	int ret = NETNSA_NSID_NOT_ASSIGNED;
> +	struct rtattr *tb[NETNSA_MAX + 1];
> +	struct nlmsghdr *n = NULL;
> +	struct rtnl_handle rth;
> +	struct rtgenmsg *rtg;
> +	int len;
> +	int fd;
> +
> +	fd = netns_get_fd(name);
> +	if (fd < 0)
> +		return ret;
> +
> +	if (rtnl_open(&rth, 0) < 0)
> +		return ret;
> +
> +	addattr32(&req.n, sizeof(req), NETNSA_FD, fd);
> +	if (rtnl_talk(&rth, &req.n, &n) < 0)
> +		goto out;
> +
> +	if (n->nlmsg_type == NLMSG_ERROR)
> +		goto out;
> +
> +	rtg = NLMSG_DATA(n);
> +	len = n->nlmsg_len;
> +
> +	len -= NLMSG_SPACE(sizeof(*rtg));
> +	if (len < 0)
> +		goto out;
> +
> +	parse_rtattr(tb, NETNSA_MAX, NETNS_RTA(rtg), len);
> +	if (tb[NETNSA_NSID])
> +		ret = rta_getattr_s32(tb[NETNSA_NSID]);
> +
> +out:
> +	free(n);
> +	rtnl_close(&rth);
> +	close(fd);
> +	return ret;
> +}

duplicates get_netnsid_from_name


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [patch iproute2-next 2/4] devlink: introduce support for netns id for nested handle
  2023-09-19  3:06   ` David Ahern
@ 2023-09-19  7:05     ` Jiri Pirko
  2023-09-19 11:57       ` Jiri Pirko
  0 siblings, 1 reply; 10+ messages in thread
From: Jiri Pirko @ 2023-09-19  7:05 UTC (permalink / raw)
  To: David Ahern; +Cc: netdev, stephen

Tue, Sep 19, 2023 at 05:06:12AM CEST, dsahern@gmail.com wrote:
>On 9/18/23 4:54 AM, Jiri Pirko wrote:
>>  static const enum mnl_attr_data_type
>> @@ -2723,6 +2725,85 @@ static bool should_arr_last_handle_end(struct dl *dl, const char *bus_name,
>>  	       !cmp_arr_last_handle(dl, bus_name, dev_name);
>>  }
>>  
>> +static int32_t netns_id_by_name(const char *name)
>> +{
>> +	struct {
>> +		struct nlmsghdr n;
>> +		struct rtgenmsg g;
>> +		char            buf[1024];
>> +	} req = {
>> +		.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
>> +		.n.nlmsg_flags = NLM_F_REQUEST,
>> +		.n.nlmsg_type = RTM_GETNSID,
>> +		.g.rtgen_family = AF_UNSPEC,
>> +	};
>> +	int ret = NETNSA_NSID_NOT_ASSIGNED;
>> +	struct rtattr *tb[NETNSA_MAX + 1];
>> +	struct nlmsghdr *n = NULL;
>> +	struct rtnl_handle rth;
>> +	struct rtgenmsg *rtg;
>> +	int len;
>> +	int fd;
>> +
>> +	fd = netns_get_fd(name);
>> +	if (fd < 0)
>> +		return ret;
>> +
>> +	if (rtnl_open(&rth, 0) < 0)
>> +		return ret;
>> +
>> +	addattr32(&req.n, sizeof(req), NETNSA_FD, fd);
>> +	if (rtnl_talk(&rth, &req.n, &n) < 0)
>> +		goto out;
>> +
>> +	if (n->nlmsg_type == NLMSG_ERROR)
>> +		goto out;
>> +
>> +	rtg = NLMSG_DATA(n);
>> +	len = n->nlmsg_len;
>> +
>> +	len -= NLMSG_SPACE(sizeof(*rtg));
>> +	if (len < 0)
>> +		goto out;
>> +
>> +	parse_rtattr(tb, NETNSA_MAX, NETNS_RTA(rtg), len);
>> +	if (tb[NETNSA_NSID])
>> +		ret = rta_getattr_s32(tb[NETNSA_NSID]);
>> +
>> +out:
>> +	free(n);
>> +	rtnl_close(&rth);
>> +	close(fd);
>> +	return ret;
>> +}
>
>duplicates get_netnsid_from_name

True, but that one is in "ip", that is why I decided to duplicate, for
simplicity sake. How do you suggest to re-use it? Should I move it
to lib? Any existing file?



>

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [patch iproute2-next 2/4] devlink: introduce support for netns id for nested handle
  2023-09-19  7:05     ` Jiri Pirko
@ 2023-09-19 11:57       ` Jiri Pirko
  0 siblings, 0 replies; 10+ messages in thread
From: Jiri Pirko @ 2023-09-19 11:57 UTC (permalink / raw)
  To: David Ahern; +Cc: netdev, stephen

Tue, Sep 19, 2023 at 09:05:57AM CEST, jiri@resnulli.us wrote:
>Tue, Sep 19, 2023 at 05:06:12AM CEST, dsahern@gmail.com wrote:
>>On 9/18/23 4:54 AM, Jiri Pirko wrote:
>>>  static const enum mnl_attr_data_type
>>> @@ -2723,6 +2725,85 @@ static bool should_arr_last_handle_end(struct dl *dl, const char *bus_name,
>>>  	       !cmp_arr_last_handle(dl, bus_name, dev_name);
>>>  }
>>>  
>>> +static int32_t netns_id_by_name(const char *name)
>>> +{
>>> +	struct {
>>> +		struct nlmsghdr n;
>>> +		struct rtgenmsg g;
>>> +		char            buf[1024];
>>> +	} req = {
>>> +		.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
>>> +		.n.nlmsg_flags = NLM_F_REQUEST,
>>> +		.n.nlmsg_type = RTM_GETNSID,
>>> +		.g.rtgen_family = AF_UNSPEC,
>>> +	};
>>> +	int ret = NETNSA_NSID_NOT_ASSIGNED;
>>> +	struct rtattr *tb[NETNSA_MAX + 1];
>>> +	struct nlmsghdr *n = NULL;
>>> +	struct rtnl_handle rth;
>>> +	struct rtgenmsg *rtg;
>>> +	int len;
>>> +	int fd;
>>> +
>>> +	fd = netns_get_fd(name);
>>> +	if (fd < 0)
>>> +		return ret;
>>> +
>>> +	if (rtnl_open(&rth, 0) < 0)
>>> +		return ret;
>>> +
>>> +	addattr32(&req.n, sizeof(req), NETNSA_FD, fd);
>>> +	if (rtnl_talk(&rth, &req.n, &n) < 0)
>>> +		goto out;
>>> +
>>> +	if (n->nlmsg_type == NLMSG_ERROR)
>>> +		goto out;
>>> +
>>> +	rtg = NLMSG_DATA(n);
>>> +	len = n->nlmsg_len;
>>> +
>>> +	len -= NLMSG_SPACE(sizeof(*rtg));
>>> +	if (len < 0)
>>> +		goto out;
>>> +
>>> +	parse_rtattr(tb, NETNSA_MAX, NETNS_RTA(rtg), len);
>>> +	if (tb[NETNSA_NSID])
>>> +		ret = rta_getattr_s32(tb[NETNSA_NSID]);
>>> +
>>> +out:
>>> +	free(n);
>>> +	rtnl_close(&rth);
>>> +	close(fd);
>>> +	return ret;
>>> +}
>>
>>duplicates get_netnsid_from_name
>
>True, but that one is in "ip", that is why I decided to duplicate, for
>simplicity sake. How do you suggest to re-use it? Should I move it
>to lib? Any existing file?

Nevermind, took a stab at it in v2, just sent. Thanks!

>
>
>
>>

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2023-09-19 11:57 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-18 10:54 [patch iproute2-next 0/4] expose devlink instances relationships Jiri Pirko
2023-09-18 10:54 ` [patch iproute2-next 1/4] devlink: update headers Jiri Pirko
2023-09-18 10:54 ` [patch iproute2-next 2/4] devlink: introduce support for netns id for nested handle Jiri Pirko
2023-09-18 11:34   ` Daniel Machon
2023-09-18 12:29     ` Jiri Pirko
2023-09-19  3:06   ` David Ahern
2023-09-19  7:05     ` Jiri Pirko
2023-09-19 11:57       ` Jiri Pirko
2023-09-18 10:54 ` [patch iproute2-next 3/4] devlink: print nested handle for port function Jiri Pirko
2023-09-18 10:54 ` [patch iproute2-next 4/4] devlink: print nested devlink handle for devlink dev Jiri Pirko

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.