[Lxc-users] How are pseudorandom MACs selected?

Brian K. White brian at aljex.com
Wed Feb 2 12:54:29 UTC 2011


On 2/2/2011 6:20 AM, Daniel Lezcano wrote:
> On 02/02/2011 10:26 AM, Trent W. Buck wrote:
>> For each lxc.network.type = veth, if you DON'T specify an
>> lxc.network.hwaddr, you get one assigned at random (example below).
>>
>> Are these assignments made from a reserved range (a la 169.254/16 in
>> IPv4), or are they randomized across the entire address space?  AFAICT,
>> it MUST be the latter.
>>
>> Further, when manually allocating a static hwaddr (so I can map it to an
>> IP within the DHCP server),
>
> The dhcp relies on an identifier to map the IP, the default is to use
> the mac address but you can use another identifier like the hostname for
> example. AFAIR, there is an option in the system network configuration
> scripts to send the hostname for the dhcp requests.
>
>>    is there any particular range I should avoid
>> or stick to?
>
> This is how the kernel allocates the mac address.
>
> /**
>    * random_ether_addr - Generate software assigned random Ethernet address
>    * @addr: Pointer to a six-byte array containing the Ethernet address
>    *
>    * Generate a random Ethernet address (MAC) that is not multicast
>    * and has the local assigned bit set.
>    */
> static inline void random_ether_addr(u8 *addr)
> {
>           get_random_bytes (addr, ETH_ALEN);
>           addr [0]&= 0xfe;       /* clear multicast bit */
>           addr [0] |= 0x02;       /* set local assignment bit (IEEE802) */
> }
>
>
> Maybe you can use the mac address range used for the virtual nic of vmware.

I just use 02:00:<ip address> which ends up being automatically unique 
enough to not collide with anything else on your subnet assuming you 
already know the ip's you want to use

IP=192.168.0.50   # container nic IP
HA=`printf "02:00:%x:%x:%x:%x" ${IP//./ }` # generate a MAC from the IP

The assumption is that you are already prepared to manage IP's somehow 
and wish the MAC's would be automatic yet at least relatively stable to 
keep from breaking things too often that track macs. So this way as you 
provision vm's, the macs will never collide without you having to 
actually track them manually, yet they aren't generated randomly and 
they stay the same as long as a given vm's ip stays the same.

-- 
bkw




More information about the lxc-users mailing list