]> git.proxmox.com Git - mirror_lxc.git/commitdiff
Make lxc-user-nic use mkifname
authorStéphane Graber <stgraber@ubuntu.com>
Thu, 5 Dec 2013 19:29:37 +0000 (14:29 -0500)
committerStéphane Graber <stgraber@ubuntu.com>
Thu, 5 Dec 2013 19:56:25 +0000 (14:56 -0500)
NetworkManager at least expects all veth devices to be called veth*
otherwise it'll consider them as physical interface and try to do DHCP
on them.

This change makes lxc-user-nic use the same function that we use for LXC
itself which will give us standard vethXXXXX kind of interfaces.

Signed-off-by: Stéphane Graber <stgraber@ubuntu.com>
Acked-by: Serge E. Hallyn <serge.hallyn@ubuntu.com>
src/lxc/conf.c
src/lxc/lxc_user_nic.c
src/lxc/network.c
src/lxc/network.h

index daf491f4977d2db6345cb33f2f8938dde6b18d9f..6542ce1257fa03d1fc6e06ead3dff2ea867d417e 100644 (file)
 #include <sys/syscall.h>
 #include <time.h>
 
-#if HAVE_IFADDRS_H
-#include <ifaddrs.h>
-#else
-#include <../include/ifaddrs.h>
-#endif
-
 #if HAVE_PTY_H
 #include <pty.h>
 #else
@@ -280,74 +274,6 @@ static struct caps_opt caps_opt[] = {
 static struct caps_opt caps_opt[] = {};
 #endif
 
-static char padchar[] =
-"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
-
-static char *mkifname(char *template)
-{
-       char *name = NULL;
-       int i = 0;
-       FILE *urandom;
-       unsigned int seed;
-       struct ifaddrs *ifaddr, *ifa;
-       int ifexists = 0;
-
-       /* Get all the network interfaces */
-       getifaddrs(&ifaddr);
-
-       /* Initialize the random number generator */
-       process_lock();
-       urandom = fopen ("/dev/urandom", "r");
-       process_unlock();
-       if (urandom != NULL) {
-               if (fread (&seed, sizeof(seed), 1, urandom) <= 0)
-                       seed = time(0);
-               process_lock();
-               fclose(urandom);
-               process_unlock();
-       }
-       else
-               seed = time(0);
-
-#ifndef HAVE_RAND_R
-       srand(seed);
-#endif
-
-       /* Generate random names until we find one that doesn't exist */
-       while(1) {
-               ifexists = 0;
-               name = strdup(template);
-
-               if (name == NULL)
-                       return NULL;
-
-               for (i = 0; i < strlen(name); i++) {
-                       if (name[i] == 'X') {
-#ifdef HAVE_RAND_R
-                               name[i] = padchar[rand_r(&seed) % (strlen(padchar) - 1)];
-#else
-                               name[i] = padchar[rand() % (strlen(padchar) - 1)];
-#endif
-                       }
-               }
-
-               for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
-                       if (strcmp(ifa->ifa_name, name) == 0) {
-                               ifexists = 1;
-                               break;
-                       }
-               }
-
-               if (ifexists == 0)
-                       break;
-
-               free(name);
-       }
-
-       freeifaddrs(ifaddr);
-       return name;
-}
-
 static int run_buffer(char *buffer)
 {
        FILE *f;
@@ -2668,7 +2594,7 @@ static int instanciate_veth(struct lxc_handler *handler, struct lxc_netdev *netd
                        ERROR("veth1 name too long");
                        return -1;
                }
-               veth1 = mkifname(veth1buf);
+               veth1 = lxc_mkifname(veth1buf);
                if (!veth1) {
                        ERROR("failed to allocate a temporary name");
                        return -1;
@@ -2678,7 +2604,7 @@ static int instanciate_veth(struct lxc_handler *handler, struct lxc_netdev *netd
        }
 
        snprintf(veth2buf, sizeof(veth2buf), "vethXXXXXX");
-       veth2 = mkifname(veth2buf);
+       veth2 = lxc_mkifname(veth2buf);
        if (!veth2) {
                ERROR("failed to allocate a temporary name");
                goto out_delete;
@@ -2787,7 +2713,7 @@ static int instanciate_macvlan(struct lxc_handler *handler, struct lxc_netdev *n
        if (err >= sizeof(peerbuf))
                return -1;
 
-       peer = mkifname(peerbuf);
+       peer = lxc_mkifname(peerbuf);
        if (!peer) {
                ERROR("failed to make a temporary name");
                return -1;
index a4ae90756b8e5a15592c7f0a1bf2a786baf8da2d..caa411a4af0236025de0b51f5ed0daa215a7da5b 100644 (file)
@@ -266,20 +266,16 @@ out_del:
 
 /*
  * Get a new nic.
- * *dest will container the name (lxcuser-%d) which is attached
+ * *dest will container the name (vethXXXXXX) which is attached
  * on the host to the lxc bridge
  */
 static void get_new_nicname(char **dest, char *br, int pid, char **cnic)
 {
-       int i = 0;
-       // TODO - speed this up.  For large installations we won't
-       // want n stats for every nth container startup.
-       while (1) {
-               sprintf(*dest, "lxcuser-%d", i);
-               if (!nic_exists(*dest) && create_nic(*dest, br, pid, cnic))
-                       return;
-               i++;
-       }
+       char template[IFNAMSIZ];
+       snprintf(template, sizeof(template), "vethXXXXXX");
+       *dest = lxc_mkifname(template);
+
+       create_nic(*dest, br, pid, cnic);
 }
 
 static bool get_nic_from_line(char *p, char **nic)
index 941f0ec21bfa56d651f2358ef829177971faaed8..f73e57dbaae22b9a5c8692a994f1d4493ec29db3 100644 (file)
 #include "conf.h"
 #include "lxclock.h"
 
+#if HAVE_IFADDRS_H
+#include <ifaddrs.h>
+#else
+#include <../include/ifaddrs.h>
+#endif
+
 #ifndef IFLA_LINKMODE
 #  define IFLA_LINKMODE 17
 #endif
@@ -1108,6 +1114,74 @@ const char *lxc_net_type_to_str(int type)
        return lxc_network_types[type];
 }
 
+static char padchar[] =
+"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+
+char *lxc_mkifname(char *template)
+{
+       char *name = NULL;
+       int i = 0;
+       FILE *urandom;
+       unsigned int seed;
+       struct ifaddrs *ifaddr, *ifa;
+       int ifexists = 0;
+
+       /* Get all the network interfaces */
+       getifaddrs(&ifaddr);
+
+       /* Initialize the random number generator */
+       process_lock();
+       urandom = fopen ("/dev/urandom", "r");
+       process_unlock();
+       if (urandom != NULL) {
+               if (fread (&seed, sizeof(seed), 1, urandom) <= 0)
+                       seed = time(0);
+               process_lock();
+               fclose(urandom);
+               process_unlock();
+       }
+       else
+               seed = time(0);
+
+#ifndef HAVE_RAND_R
+       srand(seed);
+#endif
+
+       /* Generate random names until we find one that doesn't exist */
+       while(1) {
+               ifexists = 0;
+               name = strdup(template);
+
+               if (name == NULL)
+                       return NULL;
+
+               for (i = 0; i < strlen(name); i++) {
+                       if (name[i] == 'X') {
+#ifdef HAVE_RAND_R
+                               name[i] = padchar[rand_r(&seed) % (strlen(padchar) - 1)];
+#else
+                               name[i] = padchar[rand() % (strlen(padchar) - 1)];
+#endif
+                       }
+               }
+
+               for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
+                       if (strcmp(ifa->ifa_name, name) == 0) {
+                               ifexists = 1;
+                               break;
+                       }
+               }
+
+               if (ifexists == 0)
+                       break;
+
+               free(name);
+       }
+
+       freeifaddrs(ifaddr);
+       return name;
+}
+
 int setup_private_host_hw_addr(char *veth1)
 {
        struct ifreq ifr;
index e3bb7f46752df9153b1c4424c7519f371dd826b5..3ea99ac2cf41cdc9d8e06c87d2060ccb76fbfbd1 100644 (file)
@@ -131,6 +131,11 @@ extern int lxc_neigh_proxy_on(const char *name, int family);
  */
 extern int lxc_neigh_proxy_off(const char *name, int family);
 
+/*
+ * Generate a new unique network interface name
+ */
+extern char *lxc_mkifname(char *template);
+
 extern const char *lxc_net_type_to_str(int type);
 extern int setup_private_host_hw_addr(char *veth1);
 #endif