[lxc-devel] [PATCH] Make lxc-user-nic use mkifname

Serge Hallyn serge.hallyn at ubuntu.com
Thu Dec 5 19:53:33 UTC 2013


Quoting Stéphane Graber (stgraber at ubuntu.com):
> 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 at ubuntu.com>

thanks.

Acked-by: Serge E. Hallyn <serge.hallyn at ubuntu.com>

> ---
>  src/lxc/conf.c         | 80 ++------------------------------------------------
>  src/lxc/lxc_user_nic.c | 16 ++++------
>  src/lxc/network.c      | 74 ++++++++++++++++++++++++++++++++++++++++++++++
>  src/lxc/network.h      |  5 ++++
>  4 files changed, 88 insertions(+), 87 deletions(-)
> 
> diff --git a/src/lxc/conf.c b/src/lxc/conf.c
> index daf491f..6542ce1 100644
> --- a/src/lxc/conf.c
> +++ b/src/lxc/conf.c
> @@ -34,12 +34,6 @@
>  #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;
> diff --git a/src/lxc/lxc_user_nic.c b/src/lxc/lxc_user_nic.c
> index a4ae907..caa411a 100644
> --- a/src/lxc/lxc_user_nic.c
> +++ b/src/lxc/lxc_user_nic.c
> @@ -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)
> diff --git a/src/lxc/network.c b/src/lxc/network.c
> index 941f0ec..f73e57d 100644
> --- a/src/lxc/network.c
> +++ b/src/lxc/network.c
> @@ -50,6 +50,12 @@
>  #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;
> diff --git a/src/lxc/network.h b/src/lxc/network.h
> index e3bb7f4..3ea99ac 100644
> --- a/src/lxc/network.h
> +++ b/src/lxc/network.h
> @@ -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
> -- 
> 1.8.5.1
> 
> 
> ------------------------------------------------------------------------------
> Sponsored by Intel(R) XDK 
> Develop, test and display web and hybrid apps with a single code base.
> Download it for free now!
> http://pubads.g.doubleclick.net/gampad/clk?id=111408631&iu=/4140/ostg.clktrk
> _______________________________________________
> lxc-devel mailing list
> lxc-devel at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/lxc-devel




More information about the lxc-devel mailing list