KVM-test: Add a new macaddress pool algorithm v3
PATCH 1/1 KVM-test: Add a new macaddress pool algorithm v3
From: Amos Kong <[email protected]>
Old method uses the addresses in the config files which could lead serious problem when multiple tests running in different hosts.
This patch adds a new macaddress pool algorithm, it generates the mac prefix based on mac address of the host, and fix it to correspond to IEEE802. When user have set the mac_prefix in the config file, we should use it instead of the dynamic generated mac prefix.
Add a parameter like 'preserve_mac', to preserve the original mac address, for things like migration.
MAC addresses are recorded into a dictionary 'address_pool' in following
format: {{'20100310-165222-Wt7l:0' : 'AE:9D:94:6A:9b:f9'},...}
20100310-165222-Wt7l : instance attribute of VM
0 : index of NIC
AE:9D:94:6A:9b:f9 : mac address
Use 'vm instance' + 'nic index' as the key, macaddress is the value.
Changes from v2:
- Instead of basing ourselves in a physical interface address to get an address prefix, just generate one with the prefix 0x9a (convention) randomly and add it to the address pool. If there's already one prefix, keep it there and just return it.
- Made messages more consistent and informative
- Made function names consistent all across the board
- Fixed some single line spacing between functions
Changs from v1:
- Use 'vm instance' + 'nic index' as the key of address_pool, address is value.
- Put 'mac_lock' and 'address_pool' to '/tmp', for sharing them to other autotest instances running on the same host.
- Change function names for less confusion.
- Do not copy 'vm.instance' in vm.clone()
- Split 'adding get_ifname function' to another patch
--- client/tests/kvm/kvm_utils.py | 114 ++++++++++++++++++++++++++++++++ client/tests/kvm/kvm_vm.py | 83 ++++++++++++++++++++++-- client/tests/kvm/tests_base.cfg.sample | 2 +- 3 files changed, 193 insertions(+), 6 deletions(-)
client/tests/kvm/kvm_utils.py +114 -0
@@ -5,6 +5,7 @@ KVM test utility functions. """ import time, string, random, socket, os, signal, re, logging, commands, cPickle +import fcntl, shelve from autotest_lib.client.bin import utils from autotest_lib.client.common_lib import error, logging_config import kvm_subprocess
@@ -82,6 +83,119 @@ def get_sub_dict_names(dict, keyword): # Functions related to MAC/IP addresses +def _generate_mac_address_prefix(): + """ + Generate a MAC address prefix. By convention we will set KVM autotest + MAC addresses to start with 0x9a. + """ + l = [0x9a, random.randint(0x00, 0x7f), random.randint(0x00, 0x7f), + random.randint(0x00, 0xff)] + prefix = ':'.join(map(lambda x: "%02x" % x, l)) + ":" + return prefix + + +def generate_mac_address_prefix(): + """ + Generate a random MAC address prefix and add it to the MAC pool dictionary. + If there's a MAC prefix there already, do not update the MAC pool and just + return what's in there. + """ + lock_file = open("/tmp/mac_lock", 'w') + fcntl.lockf(lock_file.fileno() ,fcntl.LOCK_EX) + mac_pool = shelve.open("/tmp/address_pool", writeback=False) + + if mac_pool.get('prefix'): + prefix = mac_pool.get('prefix') + logging.debug('Retrieved previously generated MAC prefix for this ' + 'host: %s', prefix) + else: + prefix = _generate_mac_address_prefix() + mac_pool['prefix'] = prefix + logging.debug('Generated MAC address prefix for this host: %s', prefix) + + mac_pool.close() + fcntl.lockf(lock_file.fileno(), fcntl.LOCK_UN) + lock_file.close() + + return prefix + + +def generate_mac_address(root_dir, instance_vm, nic_index, prefix=None): + """ + Random generate a MAC address and add it to the MAC pool. + + Try to generate macaddress based on the mac address prefix, add it to a + dictionary 'address_pool'. + key = VM instance + nic index, value = mac address + {['20100310-165222-Wt7l:0'] : 'AE:9D:94:6A:9b:f9'} + + @param root_dir: Root dir for kvm. + @param instance_vm: Here we use instance of vm. + @param nic_index: The index of nic. + @param prefix: Prefix of MAC address. + @return: MAC address string. + """ + if prefix is None: + prefix = generate_mac_address_prefix() + + lock_file = open("/tmp/mac_lock", 'w') + fcntl.lockf(lock_file.fileno() ,fcntl.LOCK_EX) + mac_pool = shelve.open("/tmp/address_pool", writeback=False) + found = False + key = "%s:%s" % (instance_vm, nic_index) + + if mac_pool.get(key): + found = True + mac = mac_pool.get(key) + + while not found: + suffix = "%02x:%02x" % (random.randint(0x00,0xfe), + random.randint(0x00,0xfe)) + mac = prefix + suffix + mac_list = mac.split(":") + # Clear multicast bit + mac_list[0] = int(mac_list[0],16) & 0xfe + # Set local assignment bit (IEEE802) + mac_list[0] = mac_list[0] | 0x02 + mac_list[0] = "%02x" % mac_list[0] + mac = ":".join(mac_list) + if mac in [mac_pool.get(k) for k in mac_pool.keys()]: + continue + mac_pool[key] = mac + found = True + logging.debug("Generated MAC address for NIC %s: %s ", key, mac) + + mac_pool.close() + fcntl.lockf(lock_file.fileno(), fcntl.LOCK_UN) + lock_file.close() + return mac + + +def free_mac_address(root_dir, instance_vm, nic_index): + """ + Free mac address from address pool + + @param root_dir: Root dir for kvm + @param instance_vm: Here we use instance attribute of vm + @param nic_index: The index of nic + """ + lock_file = open("/tmp/mac_lock", 'w') + fcntl.lockf(lock_file.fileno() ,fcntl.LOCK_EX) + mac_pool = shelve.open("/tmp/address_pool", writeback=False) + key = "%s:%s" % (instance_vm, nic_index) + if not mac_pool or (not key in mac_pool.keys()): + logging.debug("NIC not present in the MAC pool, not modifying pool") + logging.debug("NIC: %s" % key) + logging.debug("Contents of MAC pool: %s" % mac_pool) + else: + logging.debug("Freeing MAC addr for NIC %s: %s", key, mac_pool[key]) + mac_pool.pop(key) + + mac_pool.close() + fcntl.lockf(lock_file.fileno(), fcntl.LOCK_UN) + lock_file.close() + + def mac_str_to_int(addr): """ Convert MAC address string to integer.
client/tests/kvm/kvm_vm.py +78 -5
151 lines not on this page · raw message
client/tests/kvm/tests_base.cfg.sample +1 -1
@@ -54,7 +54,7 @@ guest_port_remote_shell = 22 nic_mode = user #nic_mode = tap nic_script = scripts/qemu-ifup -address_index = 0 +#address_index = 0 run_tcpdump = yes # Misc
Signature · 1 line
1.7.2.2