[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20180605130831.8175-3-nicolas.dichtel@6wind.com>
Date: Tue, 5 Jun 2018 15:08:31 +0200
From: Nicolas Dichtel <nicolas.dichtel@...nd.com>
To: stephen@...workplumber.org
Cc: netdev@...r.kernel.org, Nicolas Dichtel <nicolas.dichtel@...nd.com>
Subject: [PATCH iproute2 v2 2/2] iplink: enable to specify a name for the link-netns
The 'link-netnsid' argument needs a number. Add 'link-netns' when the user
wants to use the iproute2 netns name instead of the nsid.
Example:
ip link add ipip1 link-netns foo type ipip remote 10.16.0.121 local 10.16.0.249
Signed-off-by: Nicolas Dichtel <nicolas.dichtel@...nd.com>
---
ip/ip_common.h | 2 ++
ip/iplink.c | 18 ++++++++++++++++--
ip/ipnetns.c | 8 ++++++--
3 files changed, 24 insertions(+), 4 deletions(-)
diff --git a/ip/ip_common.h b/ip/ip_common.h
index 794478c546cd..4d3227cbc389 100644
--- a/ip/ip_common.h
+++ b/ip/ip_common.h
@@ -61,6 +61,8 @@ void netns_nsid_socket_init(void);
int print_nsid(const struct sockaddr_nl *who,
struct nlmsghdr *n, void *arg);
char *get_name_from_nsid(int nsid);
+int get_netnsid_from_name(const char *name);
+int set_netnsid_from_name(const char *name, int nsid);
int do_ipaddr(int argc, char **argv);
int do_ipaddrlabel(int argc, char **argv);
int do_iproute(int argc, char **argv);
diff --git a/ip/iplink.c b/ip/iplink.c
index 9ff5f692a1d4..e4d4da96aedb 100644
--- a/ip/iplink.c
+++ b/ip/iplink.c
@@ -85,7 +85,7 @@ void iplink_usage(void)
" [ broadcast LLADDR ]\n"
" [ mtu MTU ]\n"
" [ netns { PID | NAME } ]\n"
- " [ link-netnsid ID ]\n"
+ " [ link-netns NAME | link-netnsid ID ]\n"
" [ alias NAME ]\n"
" [ vf NUM [ mac LLADDR ]\n"
" [ vlan VLANID [ qos VLAN-QOS ] [ proto VLAN-PROTO ] ]\n"
@@ -865,10 +865,24 @@ int iplink_parse(int argc, char **argv, struct iplink_req *req, char **type)
IFLA_INET6_ADDR_GEN_MODE, mode);
addattr_nest_end(&req->n, afs6);
addattr_nest_end(&req->n, afs);
+ } else if (matches(*argv, "link-netns") == 0) {
+ NEXT_ARG();
+ if (link_netnsid != -1)
+ duparg("link-netns/link-netnsid", *argv);
+ link_netnsid = get_netnsid_from_name(*argv);
+ /* No nsid? Try to assign one. */
+ if (link_netnsid < 0)
+ set_netnsid_from_name(*argv, -1);
+ link_netnsid = get_netnsid_from_name(*argv);
+ if (link_netnsid < 0)
+ invarg("Invalid \"link-netns\" value\n",
+ *argv);
+ addattr32(&req->n, sizeof(*req), IFLA_LINK_NETNSID,
+ link_netnsid);
} else if (matches(*argv, "link-netnsid") == 0) {
NEXT_ARG();
if (link_netnsid != -1)
- duparg("link-netnsid", *argv);
+ duparg("link-netns/link-netnsid", *argv);
if (get_integer(&link_netnsid, *argv, 0))
invarg("Invalid \"link-netnsid\" value\n",
*argv);
diff --git a/ip/ipnetns.c b/ip/ipnetns.c
index 30af9319f39e..368be0cbc0a4 100644
--- a/ip/ipnetns.c
+++ b/ip/ipnetns.c
@@ -91,7 +91,7 @@ static int ipnetns_have_nsid(void)
return have_rtnl_getnsid;
}
-static int get_netnsid_from_name(const char *name)
+int get_netnsid_from_name(const char *name)
{
struct {
struct nlmsghdr n;
@@ -108,6 +108,8 @@ static int get_netnsid_from_name(const char *name)
struct rtgenmsg *rthdr;
int len, fd;
+ netns_nsid_socket_init();
+
fd = netns_get_fd(name);
if (fd < 0)
return fd;
@@ -705,7 +707,7 @@ out_delete:
return -1;
}
-static int set_netnsid_from_name(const char *name, int nsid)
+int set_netnsid_from_name(const char *name, int nsid)
{
struct {
struct nlmsghdr n;
@@ -719,6 +721,8 @@ static int set_netnsid_from_name(const char *name, int nsid)
};
int fd, err = 0;
+ netns_nsid_socket_init();
+
fd = netns_get_fd(name);
if (fd < 0)
return fd;
--
2.15.1
Powered by blists - more mailing lists