Commit 8d357196 by Dongsheng Yang Committed by Serge Hallyn

network: allow lxc_network_move_by_index() rename netdev in moving.

In netlink, we can set the dest_name of netdev when move netdev between namespaces in one netlink request. And moving a netdev of a src_name to a netdev with a dest_name is a common usecase. So this patch add a parametaer to lxc_network_move_by_index() to indicate the dest_name for the movement. NULL means same with the src_name. Signed-off-by: 's avatarDongsheng Yang <yangds.fnst@cn.fujitsu.com> Acked-by: 's avatarSerge E. Hallyn <serge.hallyn@ubuntu.com>
parent 312f9c5d
...@@ -2703,7 +2703,7 @@ void restore_phys_nics_to_netns(int netnsfd, struct lxc_conf *conf) ...@@ -2703,7 +2703,7 @@ void restore_phys_nics_to_netns(int netnsfd, struct lxc_conf *conf)
} }
for (i=0; i<conf->num_savednics; i++) { for (i=0; i<conf->num_savednics; i++) {
struct saved_nic *s = &conf->saved_nics[i]; struct saved_nic *s = &conf->saved_nics[i];
if (lxc_netdev_move_by_index(s->ifindex, 1)) if (lxc_netdev_move_by_index(s->ifindex, 1, NULL))
WARN("Error moving nic index:%d back to host netns", WARN("Error moving nic index:%d back to host netns",
s->ifindex); s->ifindex);
} }
...@@ -3285,7 +3285,7 @@ int lxc_assign_network(struct lxc_list *network, pid_t pid) ...@@ -3285,7 +3285,7 @@ int lxc_assign_network(struct lxc_list *network, pid_t pid)
if (!netdev->ifindex) if (!netdev->ifindex)
continue; continue;
err = lxc_netdev_move_by_index(netdev->ifindex, pid); err = lxc_netdev_move_by_index(netdev->ifindex, pid, NULL);
if (err) { if (err) {
ERROR("failed to move '%s' to the container : %s", ERROR("failed to move '%s' to the container : %s",
netdev->link, strerror(-err)); netdev->link, strerror(-err));
......
...@@ -240,7 +240,7 @@ int main(int argc, char *argv[]) ...@@ -240,7 +240,7 @@ int main(int argc, char *argv[])
if (my_iflist) { if (my_iflist) {
for (tmpif = my_iflist; tmpif; tmpif = tmpif->mi_next) { for (tmpif = my_iflist; tmpif; tmpif = tmpif->mi_next) {
if (lxc_netdev_move_by_name(tmpif->mi_ifname, pid) < 0) if (lxc_netdev_move_by_name(tmpif->mi_ifname, pid, NULL) < 0)
fprintf(stderr,"Could not move interface %s into container %d: %s\n", tmpif->mi_ifname, pid, strerror(errno)); fprintf(stderr,"Could not move interface %s into container %d: %s\n", tmpif->mi_ifname, pid, strerror(errno));
} }
} }
......
...@@ -268,7 +268,7 @@ static bool create_nic(char *nic, char *br, int pid, char **cnic) ...@@ -268,7 +268,7 @@ static bool create_nic(char *nic, char *br, int pid, char **cnic)
} }
/* pass veth2 to target netns */ /* pass veth2 to target netns */
ret = lxc_netdev_move_by_name(veth2buf, pid); ret = lxc_netdev_move_by_name(veth2buf, pid, NULL);
if (ret < 0) { if (ret < 0) {
fprintf(stderr, "Error moving %s to netns %d\n", veth2buf, pid); fprintf(stderr, "Error moving %s to netns %d\n", veth2buf, pid);
goto out_del; goto out_del;
......
...@@ -103,7 +103,7 @@ struct rt_req { ...@@ -103,7 +103,7 @@ struct rt_req {
struct rtmsg rt; struct rtmsg rt;
}; };
int lxc_netdev_move_by_index(int ifindex, pid_t pid) int lxc_netdev_move_by_index(int ifindex, pid_t pid, const char* ifname)
{ {
struct nl_handler nlh; struct nl_handler nlh;
struct nlmsg *nlmsg = NULL; struct nlmsg *nlmsg = NULL;
...@@ -129,6 +129,11 @@ int lxc_netdev_move_by_index(int ifindex, pid_t pid) ...@@ -129,6 +129,11 @@ int lxc_netdev_move_by_index(int ifindex, pid_t pid)
if (nla_put_u32(nlmsg, IFLA_NET_NS_PID, pid)) if (nla_put_u32(nlmsg, IFLA_NET_NS_PID, pid))
goto out; goto out;
if (ifname != NULL) {
if (nla_put_string(nlmsg, IFLA_IFNAME, ifname))
goto out;
}
err = netlink_transaction(&nlh, nlmsg, nlmsg); err = netlink_transaction(&nlh, nlmsg, nlmsg);
out: out:
netlink_close(&nlh); netlink_close(&nlh);
...@@ -136,7 +141,7 @@ out: ...@@ -136,7 +141,7 @@ out:
return err; return err;
} }
int lxc_netdev_move_by_name(const char *ifname, pid_t pid) int lxc_netdev_move_by_name(const char *ifname, pid_t pid, const char* newname)
{ {
int index; int index;
...@@ -147,7 +152,7 @@ int lxc_netdev_move_by_name(const char *ifname, pid_t pid) ...@@ -147,7 +152,7 @@ int lxc_netdev_move_by_name(const char *ifname, pid_t pid)
if (!index) if (!index)
return -EINVAL; return -EINVAL;
return lxc_netdev_move_by_index(index, pid); return lxc_netdev_move_by_index(index, pid, newname);
} }
int lxc_netdev_delete_by_index(int ifindex) int lxc_netdev_delete_by_index(int ifindex)
......
...@@ -31,8 +31,8 @@ extern int lxc_convert_mac(char *macaddr, struct sockaddr *sockaddr); ...@@ -31,8 +31,8 @@ extern int lxc_convert_mac(char *macaddr, struct sockaddr *sockaddr);
/* /*
* Move a device between namespaces * Move a device between namespaces
*/ */
extern int lxc_netdev_move_by_index(int ifindex, pid_t pid); extern int lxc_netdev_move_by_index(int ifindex, pid_t pid, const char* ifname);
extern int lxc_netdev_move_by_name(const char *ifname, pid_t pid); extern int lxc_netdev_move_by_name(const char *ifname, pid_t pid, const char* newname);
/* /*
* Delete a network device * Delete a network device
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment