]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
conf: merge network namespace move & rename on shutdown 1269/head
authorWolfgang Bumiller <w.bumiller@proxmox.com>
Wed, 17 Aug 2016 09:36:39 +0000 (11:36 +0200)
committerWolfgang Bumiller <w.bumiller@proxmox.com>
Wed, 2 Nov 2016 13:59:44 +0000 (14:59 +0100)
On shutdown we move physical network interfaces back to the
host namespace and rename them afterwards as well as in the
later lxc_network_delete() step. However, if the device had
a name which already exists in the host namespace then the
moving fails and so do the subsequent rename attempts. When
the namespace ceases to exist the devices finally end up
in the host namespace named 'dev<ID>' by the kernel.

In order to avoid this, we do the moving and renaming in a
single step (lxc_netdev_move_by_*()'s move & rename happen
in a single netlink transaction).

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
src/lxc/conf.c
src/lxc/conf.h
src/lxc/start.c

index f688f9432fea380570bdeca9a45e3b3b46aa369b..b2e0fd90c9ac06bb8ab15e3018d47ef1222ccaa7 100644 (file)
@@ -2397,15 +2397,17 @@ static int setup_network(struct lxc_list *network)
 }
 
 /* try to move physical nics to the init netns */
-void restore_phys_nics_to_netns(int netnsfd, struct lxc_conf *conf)
+void lxc_restore_phys_nics_to_netns(int netnsfd, struct lxc_conf *conf)
 {
        int i, ret, oldfd;
        char path[MAXPATHLEN];
        char ifname[IFNAMSIZ];
 
-       if (netnsfd < 0)
+       if (netnsfd < 0 || conf->num_savednics == 0)
                return;
 
+       INFO("running to reset %d nic names", conf->num_savednics);
+
        ret = snprintf(path, MAXPATHLEN, "/proc/self/ns/net");
        if (ret < 0 || ret >= MAXPATHLEN) {
                WARN("Failed to open monitor netns fd");
@@ -2427,32 +2429,17 @@ void restore_phys_nics_to_netns(int netnsfd, struct lxc_conf *conf)
                        WARN("no interface corresponding to index '%d'", s->ifindex);
                        continue;
                }
-               if (lxc_netdev_move_by_name(ifname, 1, NULL))
+               if (lxc_netdev_move_by_name(ifname, 1, s->orig_name))
                        WARN("Error moving nic name:%s back to host netns", ifname);
+               free(s->orig_name);
        }
+       conf->num_savednics = 0;
+
        if (setns(oldfd, 0) != 0)
                SYSERROR("Failed to re-enter monitor's netns");
        close(oldfd);
 }
 
-void lxc_rename_phys_nics_on_shutdown(int netnsfd, struct lxc_conf *conf)
-{
-       int i;
-
-       if (conf->num_savednics == 0)
-               return;
-
-       INFO("running to reset %d nic names", conf->num_savednics);
-       restore_phys_nics_to_netns(netnsfd, conf);
-       for (i=0; i<conf->num_savednics; i++) {
-               struct saved_nic *s = &conf->saved_nics[i];
-               INFO("resetting nic %d to %s", s->ifindex, s->orig_name);
-               lxc_netdev_rename_by_index(s->ifindex, s->orig_name);
-               free(s->orig_name);
-       }
-       conf->num_savednics = 0;
-}
-
 static char *default_rootfs_mount = LXCROOTFSMOUNT;
 
 struct lxc_conf *lxc_conf_init(void)
index e48466730ab237b0edc0d7df0d0d5518db132eca..842e4dcff71b143837f479820a78ed1d8875c4a6 100644 (file)
@@ -440,7 +440,7 @@ extern int do_rootfs_setup(struct lxc_conf *conf, const char *name,
 struct cgroup_process_info;
 extern int lxc_setup(struct lxc_handler *handler);
 
-extern void lxc_rename_phys_nics_on_shutdown(int netnsfd, struct lxc_conf *conf);
+extern void lxc_restore_phys_nics_to_netns(int netnsfd, struct lxc_conf *conf);
 
 extern int find_unmapped_nsuid(struct lxc_conf *conf, enum idtype idtype);
 extern int mapped_hostid(unsigned id, struct lxc_conf *conf, enum idtype idtype);
index ecc7b08f69d9a8357ce11a49304af6d8d06762ce..0da7f9f2ab3f271d74177c206c3ce599867f6636 100644 (file)
@@ -1420,7 +1420,7 @@ int __lxc_start(const char *name, struct lxc_conf *conf,
        }
 
        DEBUG("Pushing physical nics back to host namespace");
-       lxc_rename_phys_nics_on_shutdown(netnsfd, handler->conf);
+       lxc_restore_phys_nics_to_netns(netnsfd, handler->conf);
 
        DEBUG("Tearing down virtual network devices used by container");
        lxc_delete_network(handler);