Message ID | 20230918105416.1107260-3-jiri@resnulli.us (mailing list archive) |
---|---|
State | Superseded |
Delegated to: | David Ahern |
Headers | show |
Series | expose devlink instances relationships | expand |
Context | Check | Description |
---|---|---|
netdev/tree_selection | success | Not a local patch |
> +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
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
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
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? >
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! > > > >>
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,