[PATCH 1/4] add a parameter to rtnl_link_veth_add()

Cong Wang xiyou.wangcong at gmail.com
Tue Dec 3 23:16:08 EST 2013


Usually the peer of a veth device is moved to a new netns
after creation, so add a pid parameter to rtnl_link_veth_add().

Signed-off-by: Cong Wang <xiyou.wangcong at gmail.com>
---
 include/netlink/route/link/veth.h | 3 ++-
 lib/route/link/veth.c             | 7 +++++--
 tests/test-create-veth.c          | 2 +-
 3 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/include/netlink/route/link/veth.h b/include/netlink/route/link/veth.h
index 7ca7d32..35c2345 100644
--- a/include/netlink/route/link/veth.h
+++ b/include/netlink/route/link/veth.h
@@ -14,6 +14,7 @@
 
 #include <netlink/netlink.h>
 #include <netlink/route/link.h>
+#include <sys/types.h>
 
 #ifdef __cplusplus
 extern "C" {
@@ -26,7 +27,7 @@ extern int rtnl_link_is_veth(struct rtnl_link *);
 
 extern struct rtnl_link *rtnl_link_veth_get_peer(struct rtnl_link *);
 extern int rtnl_link_veth_add(struct nl_sock *sock, const char *name,
-			      const char *peer);
+			      const char *peer, pid_t pid);
 
 #ifdef __cplusplus
 }
diff --git a/lib/route/link/veth.c b/lib/route/link/veth.c
index dba1294..d5c1926 100644
--- a/lib/route/link/veth.c
+++ b/lib/route/link/veth.c
@@ -236,15 +236,17 @@ int rtnl_link_is_veth(struct rtnl_link *link)
  * @arg sock		netlink socket
  * @arg name		name of the veth device or NULL
  * @arg peer_name	name of its peer or NULL
+ * @arg pid		pid of the process in the new netns
  *
- * Creates a new veth device pair in the kernel. If no name is
+ * Creates a new veth device pair in the kernel and move the peer
+ * to the network namespace where the process is. If no name is
  * provided, the kernel will automatically pick a name of the
  * form "veth%d" (e.g. veth0, veth1, etc.)
  *
  * @return 0 on success or a negative error code
  */
 int rtnl_link_veth_add(struct nl_sock *sock, const char *name,
-                       const char *peer_name)
+                       const char *peer_name, pid_t pid)
 {
 	struct rtnl_link *link, *peer;
 	int err = -NLE_NOMEM;
@@ -258,6 +260,7 @@ int rtnl_link_veth_add(struct nl_sock *sock, const char *name,
 		rtnl_link_set_name(peer, peer_name);
 	}
 
+	rtnl_link_set_ns_pid(peer, pid);
 	err = rtnl_link_add(sock, link, NLM_F_CREATE);
 
 	rtnl_link_put(peer);
diff --git a/tests/test-create-veth.c b/tests/test-create-veth.c
index 038353f..c0d9145 100644
--- a/tests/test-create-veth.c
+++ b/tests/test-create-veth.c
@@ -16,7 +16,7 @@ int main(int argc, char *argv[])
 	}
 
 #if 0
-	rtnl_link_veth_add(sk, "veth2", "veth3");
+	rtnl_link_veth_add(sk, "veth2", "veth3", getpid());
 #else
 	link = rtnl_link_veth_alloc();
 	if (!link) {
-- 
1.8.1.4




More information about the libnl mailing list