#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>
[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
return;
}
- __pr_out_handle_start(dl, tb, false, false);
+ __pr_out_handle_start(dl, tb, tb[DEVLINK_ATTR_NETNS_ID], false);
+ 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_from_id(id);
+
+ if (name) {
+ print_string(PRINT_ANY, "netns",
+ " netns %s", name);
+ free(name);
+ } else {
+ print_int(PRINT_ANY, "netnsid",
+ " netnsid %d", id);
+ }
+ } else {
+ print_string(PRINT_FP, NULL, " netnsid %s", "unknown");
+ print_int(PRINT_JSON, "netnsid", NULL, id);
+ }
+ }
pr_out_handle_end(dl);
}
free(answer);
return ret;
}
+
+struct netns_name_from_id_ctx {
+ int32_t id;
+ char *name;
+ struct rtnl_handle *rth;
+};
+
+static int netns_name_from_id_func(char *nsname, void *arg)
+{
+ struct netns_name_from_id_ctx *ctx = arg;
+ int32_t ret;
+
+ ret = netns_id_from_name(ctx->rth, nsname);
+ if (ret < 0 || ret != ctx->id)
+ return 0;
+ ctx->name = strdup(nsname);
+ return 1;
+}
+
+char *netns_name_from_id(int32_t id)
+{
+ struct rtnl_handle rth;
+ struct netns_name_from_id_ctx ctx = {
+ .id = id,
+ .rth = &rth,
+ };
+
+ if (rtnl_open(&rth, 0) < 0)
+ return NULL;
+ netns_foreach(netns_name_from_id_func, &ctx);
+ rtnl_close(&rth);
+
+ return ctx.name;
+}