add a parameter to rtnl_link_veth_add()

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@gmail.com>
Signed-off-by: Thomas Graf <tgraf@suug.ch>
This commit is contained in:
Cong Wang 2013-12-03 20:16:08 -08:00 committed by Thomas Graf
parent 8b87ae5516
commit aa1c5628fd
3 changed files with 8 additions and 4 deletions

View file

@ -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
}

View file

@ -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);

View file

@ -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) {