]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
network: allow lxc_network_move_by_index() rename netdev in moving.
authorDongsheng Yang <yangds.fnst@cn.fujitsu.com>
Tue, 16 Sep 2014 04:58:55 +0000 (12:58 +0800)
committerStéphane Graber <stgraber@ubuntu.com>
Tue, 25 Nov 2014 21:52:19 +0000 (16:52 -0500)
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: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>
Acked-by: Serge E. Hallyn <serge.hallyn@ubuntu.com>
src/lxc/conf.c
src/lxc/lxc_unshare.c
src/lxc/lxc_user_nic.c
src/lxc/network.c
src/lxc/network.h

index 71e49baa5f02b718e7de6e1f11cffc9cd9cf5466..a7d090429c50d343ace4fd9d960f6a6676c195f3 100644 (file)
@@ -2830,7 +2830,7 @@ void restore_phys_nics_to_netns(int netnsfd, struct lxc_conf *conf)
        }
        for (i=0; i<conf->num_savednics; 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",
                                        s->ifindex);
        }
@@ -3408,7 +3408,7 @@ int lxc_assign_network(struct lxc_list *network, pid_t pid)
                if (!netdev->ifindex)
                        continue;
 
-               err = lxc_netdev_move_by_index(netdev->ifindex, pid);
+               err = lxc_netdev_move_by_index(netdev->ifindex, pid, NULL);
                if (err) {
                        ERROR("failed to move '%s' to the container : %s",
                              netdev->link, strerror(-err));
index fc6f9db13c6f0def7d23002cdd9f62d5ce629458..59a231e578201a625573fbbafa35fad0174adc74 100644 (file)
@@ -240,7 +240,7 @@ int main(int argc, char *argv[])
 
        if (my_iflist) {
                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));
                }
        }
index 64e9d1a47c692985e0c20cbb271ce1a4a20b6813..23c29d1688356b21413a16623ad5c4972d9edd0c 100644 (file)
@@ -268,7 +268,7 @@ static bool create_nic(char *nic, char *br, int pid, char **cnic)
        }
 
        /* 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) {
                fprintf(stderr, "Error moving %s to netns %d\n", veth2buf, pid);
                goto out_del;
index 334d55fc93cba06f5b4ba4271b257884f8ae5f32..5dd973c796624d5c1fdc5a01179bbaa721b671a6 100644 (file)
@@ -102,7 +102,7 @@ struct rt_req {
        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 nlmsg *nlmsg = NULL;
@@ -128,6 +128,11 @@ int lxc_netdev_move_by_index(int ifindex, pid_t pid)
        if (nla_put_u32(nlmsg, IFLA_NET_NS_PID, pid))
                goto out;
 
+       if (ifname != NULL) {
+               if (nla_put_string(nlmsg, IFLA_IFNAME, ifname))
+                       goto out;
+       }
+
        err = netlink_transaction(&nlh, nlmsg, nlmsg);
 out:
        netlink_close(&nlh);
@@ -135,7 +140,7 @@ out:
        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;
 
@@ -146,7 +151,7 @@ int lxc_netdev_move_by_name(const char *ifname, pid_t pid)
        if (!index)
                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)
index a8e0612c115932479b6d013b8747eb4ca4a6595f..768489df0d0f257fa0c8d97d6fda84b864e2af7b 100644 (file)
@@ -31,8 +31,8 @@ extern int lxc_convert_mac(char *macaddr, struct sockaddr *sockaddr);
 /*
  * Move a device between namespaces
  */
-extern int lxc_netdev_move_by_index(int ifindex, pid_t pid);
-extern int lxc_netdev_move_by_name(const char *ifname, 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, const char* newname);
 
 /*
  * Delete a network device