[Kimchi-devel] [RFC]: help, how to get the mac after create a VM interface.

Sheldon shaohef at linux.vnet.ibm.com
Mon Jan 20 15:22:18 UTC 2014


On 01/20/2014 09:34 PM, Sheldon wrote:
> when I create a interface, There is no mac attribute in the xml.
> After create successfully, libvirt will allocate a mac.
> The problem is that, How can I get the mac.
>
> Should the CREATE method always needs to return an interface info?
>
> Here is some of create code.
>
> def vmifaces_create(self, vm, params):
> dom = self._get_vm(vm)
> xml = """
> <interface type='network'>
> <source network='default'/>
> </interface>
> """
>
> dom.attachDeviceFlags(xml,
> libvirt.VIR_DOMAIN_AFFECT_CURRENT)
>
> # here I need to return the mac
> return ????
>
>
>
> The libvirt will create a xml like the follow? The problem is that the 
> a vm may have several interfaces.
> xml = """
> <interface type='network'>
> <mac address='52:54:00:2a:53:8f'/>
> <source network='default'/>
> <model type='rtl8139'/>
> <address type='pci' domain='0x0000' bus='0x00' slot='0x03' 
> function='0x0'/>
> </interface>
> """
>
>
check the libvirt code:
libvirt will generate a mac address,  and the mac starts with "52:52:00"
and the others bit are generated by virRandomBits


virDomainNetGenerateMAC(xmlopt, &def->mac);

void
virDomainNetGenerateMAC(virDomainXMLOptionPtr xmlopt,
                         virMacAddrPtr mac)
{
     virMacAddrGenerate(xmlopt->config.macPrefix, mac);
}



void virMacAddrGenerate(const unsigned char prefix[VIR_MAC_PREFIX_BUFLEN],
                         virMacAddrPtr addr)
{
     addr->addr[0] = prefix[0];
     addr->addr[1] = prefix[1];
     addr->addr[2] = prefix[2];
     addr->addr[3] = virRandomBits(8);
     addr->addr[4] = virRandomBits(8);
     addr->addr[5] = virRandomBits(8);
}


/**
  * virRandomBits:
  * @nbits: Number of bits of randommess required
  *
  * Generate an evenly distributed random number between [0,2^nbits), where
  * @nbits must be in the range (0,64].
  *
  * Return: a random number with @nbits entropy
  */
uint64_t virRandomBits(int nbits)
{
     uint64_t ret = 0;
     int32_t bits;

     if (virRandomInitialize() < 0) {
         /* You're already hosed, so this particular non-random value
          * isn't any worse.  */
         VIR_WARN("random number generation is broken");
         return 0;
     }

     virMutexLock(&randomLock);

     while (nbits > RANDOM_BITS_PER_ITER) {
         random_r(&randomData, &bits);
         ret = (ret << RANDOM_BITS_PER_ITER) | (bits & RANDOM_BITS_MASK);
         nbits -= RANDOM_BITS_PER_ITER;
     }

     random_r(&randomData, &bits);
     ret = (ret << nbits) | (bits & ((1 << nbits) - 1));

     virMutexUnlock(&randomLock);
     return ret;
}


/**
  * virDomainXMLOptionNew:
  *
  * Allocate a new domain XML configuration
  */
virDomainXMLOptionPtr
virDomainXMLOptionNew(virDomainDefParserConfigPtr config,
                       virDomainXMLPrivateDataCallbacksPtr priv,
                       virDomainXMLNamespacePtr xmlns)
{
     virDomainXMLOptionPtr xmlopt;

...

     /* Technically this forbids to use one of Xerox's MAC address 
prefixes in
      * our hypervisor drivers. This shouldn't ever be a problem.
      *
      * Use the KVM prefix as default as it's in the privately administered
      * range */
     if (xmlopt->config.macPrefix[0] == 0 &&
         xmlopt->config.macPrefix[1] == 0 &&
         xmlopt->config.macPrefix[2] == 0) {
         xmlopt->config.macPrefix[0] = 0x52;
         xmlopt->config.macPrefix[1] = 0x54;
     }

     return xmlopt;
}


-- 
Thanks and best regards!

Sheldon Feng(冯少合)<shaohef at linux.vnet.ibm.com>
IBM Linux Technology Center




More information about the Kimchi-devel mailing list