[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20230919115644.1157890-4-jiri@resnulli.us>
Date: Tue, 19 Sep 2023 13:56:42 +0200
From: Jiri Pirko <jiri@...nulli.us>
To: netdev@...r.kernel.org
Cc: stephen@...workplumber.org,
dsahern@...il.com,
daniel.machon@...rochip.com
Subject: [patch iproute2-next v2 3/5] devlink: introduce support for netns id for nested handle
From: Jiri Pirko <jiri@...dia.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@...dia.com>
---
v1->v2:
- use previously introduced netns_netnsid_from_name() instead of code
duplication for the same function.
- s/nesns_name_by_id_func/netns_name_by_id_func/
---
devlink/devlink.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 60 insertions(+)
diff --git a/devlink/devlink.c b/devlink/devlink.c
index d1795f616ca0..cf5d466bfc9d 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,40 @@ static bool should_arr_last_handle_end(struct dl *dl, const char *bus_name,
!cmp_arr_last_handle(dl, bus_name, dev_name);
}
+struct netns_name_by_id_ctx {
+ int32_t id;
+ char *name;
+ struct rtnl_handle *rth;
+};
+
+static int netns_name_by_id_func(char *nsname, void *arg)
+{
+ struct netns_name_by_id_ctx *ctx = arg;
+ int32_t ret;
+
+ ret = netns_netnsid_from_name(ctx->rth, 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 rtnl_handle rth;
+ struct netns_name_by_id_ctx ctx = {
+ .id = id,
+ .rth = &rth,
+ };
+
+ if (rtnl_open(&rth, 0) < 0)
+ return NULL;
+ netns_foreach(netns_name_by_id_func, &ctx);
+ rtnl_close(&rth);
+
+ return ctx.name;
+}
+
static void pr_out_nested_handle(struct nlattr *nla_nested_dl)
{
struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
@@ -2740,6 +2776,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
Powered by blists - more mailing lists