From: David Ahern Update netns_get_fd to try the passed in string as a name first. If the netns filesystem entry does not exist, try converting the string to an integer and if successful return an fd to /proc//ns/net. This allows netns_get_fd to uniformly handle the 2 common use cases of specifying network namespace by name and pid. Signed-off-by: David Ahern --- lib/namespace.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/lib/namespace.c b/lib/namespace.c index 74b7e7caf0e5..0bba3a163563 100644 --- a/lib/namespace.c +++ b/lib/namespace.c @@ -99,19 +99,33 @@ int netns_switch(char *name) return 0; } -int netns_get_fd(const char *name) +/* try str as the name of the namespace first, then + * fallback to pid + */ +int netns_get_fd(const char *str) { char pathbuf[PATH_MAX]; - const char *path, *ptr; + const char *path; + int pid; + int fd; - path = name; - ptr = strchr(name, '/'); - if (!ptr) { + path = str; + if (!strchr(str, '/')) { snprintf(pathbuf, sizeof(pathbuf), "%s/%s", - NETNS_RUN_DIR, name ); + NETNS_RUN_DIR, str); path = pathbuf; } - return open(path, O_RDONLY); + + fd = open(path, O_RDONLY); + if (fd >= 0) + return fd; + + /* make sure string is an integer */ + if (get_integer(&pid, str, 0) < 0) + return -1; + + snprintf(pathbuf, sizeof(pathbuf), "/proc/%s/ns/net", str); + return open(pathbuf, O_RDONLY); } int netns_foreach(int (*func)(char *nsname, void *arg), void *arg) -- 2.43.0 From: David Ahern Prior commit handles the fallback to pid, so remove the now duplicate code from iplink_parse. Signed-off-by: David Ahern --- ip/iplink.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/ip/iplink.c b/ip/iplink.c index eae51438d0be..2c052d2970ca 100644 --- a/ip/iplink.c +++ b/ip/iplink.c @@ -650,19 +650,11 @@ int iplink_parse(int argc, char **argv, struct iplink_req *req, char **type) if (offload && name == dev) dev = NULL; } else if (strcmp(*argv, "netns") == 0) { - int pid; - NEXT_ARG(); if (netns != -1) duparg("netns", *argv); + /* try by name then by pid */ netns = netns_get_fd(*argv); - if (netns < 0 && get_integer(&pid, *argv, 0) == 0) { - char path[PATH_MAX]; - - snprintf(path, sizeof(path), "/proc/%d/ns/net", - pid); - netns = open(path, O_RDONLY); - } if (netns < 0) invarg("Invalid \"netns\" value\n", *argv); -- 2.43.0 From: David Ahern Update rdma dev to work like ip link where the netns can be a name or a pid by using netns_get_fd. Update man page and help accordingly. Signed-off-by: David Ahern --- man/man8/rdma-dev.8 | 3 ++- rdma/dev.c | 11 ++++------- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/man/man8/rdma-dev.8 b/man/man8/rdma-dev.8 index abc9f405ede5..34ea1c614ca4 100644 --- a/man/man8/rdma-dev.8 +++ b/man/man8/rdma-dev.8 @@ -32,7 +32,7 @@ rdma-dev \- RDMA device configuration .B rdma dev set .RI "[ " DEV " ]" .BR netns -.BR NSNAME +.BR { NSNAME | PID } .ti -8 .B rdma dev set @@ -104,6 +104,7 @@ Renames the mlx5_3 device to rdma_0. .RE .PP rdma dev set mlx5_3 netns foo +rdma dev set mlx5_3 netns 1234 .RS 4 Changes the network namespace of RDMA device to foo where foo was previously created using the iproute2 ip command. diff --git a/rdma/dev.c b/rdma/dev.c index fd60c1a0e856..c8537912e48e 100644 --- a/rdma/dev.c +++ b/rdma/dev.c @@ -6,6 +6,7 @@ #include #include "rdma.h" +#include "namespace.h" static int dev_help(struct rd *rd) { @@ -13,7 +14,7 @@ static int dev_help(struct rd *rd) pr_out(" %s dev add DEVNAME type TYPE parent PARENT_DEVNAME\n", rd->filename); pr_out(" %s dev delete DEVNAME\n", rd->filename); pr_out(" %s dev set [DEV] name DEVNAME\n", rd->filename); - pr_out(" %s dev set [DEV] netns NSNAME\n", rd->filename); + pr_out(" %s dev set [DEV] netns { NSNAME | PID }\n", rd->filename); pr_out(" %s dev set [DEV] adaptive-moderation [on|off]\n", rd->filename); return 0; } @@ -311,7 +312,7 @@ static int dev_set_name(struct rd *rd) static int dev_set_netns(struct rd *rd) { - char *netns_path; + char *arg = rd_argv(rd); uint32_t seq; int netns; int ret; @@ -321,10 +322,7 @@ static int dev_set_netns(struct rd *rd) return -EINVAL; } - if (asprintf(&netns_path, "%s/%s", NETNS_RUN_DIR, rd_argv(rd)) < 0) - return -ENOMEM; - - netns = open(netns_path, O_RDONLY | O_CLOEXEC); + netns = netns_get_fd(arg); if (netns < 0) { fprintf(stderr, "Cannot open network namespace \"%s\": %s\n", rd_argv(rd), strerror(errno)); @@ -339,7 +337,6 @@ static int dev_set_netns(struct rd *rd) ret = rd_sendrecv_msg(rd, seq); close(netns); done: - free(netns_path); return ret; } -- 2.43.0 From: David Ahern Now that netns_get_fd handles by name and pid, the special handling in devlink to fallback to PID can be removed with both cases handled by the FD attribute. Signed-off-by: David Ahern --- devlink/devlink.c | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/devlink/devlink.c b/devlink/devlink.c index 730515a78950..908a0e32be2b 100644 --- a/devlink/devlink.c +++ b/devlink/devlink.c @@ -428,12 +428,6 @@ static void dl_arg_inc(struct dl *dl) dl->argv++; } -static void dl_arg_dec(struct dl *dl) -{ - dl->argc++; - dl->argv--; -} - static char *dl_argv_next(struct dl *dl) { char *ret; @@ -2153,14 +2147,12 @@ static int dl_argv_parse(struct dl *dl, uint64_t o_required, err = dl_argv_str(dl, &netns_str); if (err) return err; - opts->netns = netns_get_fd(netns_str); - if ((int)opts->netns < 0) { - dl_arg_dec(dl); - err = dl_argv_uint32_t(dl, &opts->netns); - if (err) - return err; - opts->netns_is_pid = true; - } + + err = netns_get_fd(netns_str); + if (err < 0) + return err; + + opts->netns = err; o_found |= DL_OPT_NETNS; } else if (dl_argv_match(dl, "action") && (o_all & DL_OPT_RELOAD_ACTION)) { @@ -2725,10 +2717,7 @@ static void dl_opts_put(struct nlmsghdr *nlh, struct dl *dl) mnl_attr_put_u8(nlh, DEVLINK_ATTR_TRAP_ACTION, opts->trap_action); if (opts->present & DL_OPT_NETNS) - mnl_attr_put_u32(nlh, - opts->netns_is_pid ? DEVLINK_ATTR_NETNS_PID : - DEVLINK_ATTR_NETNS_FD, - opts->netns); + mnl_attr_put_u32(nlh, DEVLINK_ATTR_NETNS_FD, opts->netns); if (opts->present & DL_OPT_RELOAD_ACTION) mnl_attr_put_u8(nlh, DEVLINK_ATTR_RELOAD_ACTION, opts->reload_action); -- 2.43.0