KVM test: simplify MAC address management
PATCH 1/7 KVM test: simplify MAC address management
- Remove the VM.mac_prefix attribute
- Remove the 'root_dir' parameter from generate_mac_address() and free_mac_address()
- Remove the 'prefix' parameter from generate_mac_address()
- Remove the explicit setting and clearing of bits in the most significant byte (it is fixed to 0x9A anyway)
- Remove the 'shareable' parameter from set_mac_address() (no longer required)
- Remove the 'preserve_mac' parameter from VM.clone() and replace it with 'mac_source' in VM.create()
- Remove overly verbose debug messages from free_mac_address()
- Replace VM.free_mac_addresses() with VM.free_mac_address()
- Use _open_mac_pool() and _close_mac_pool() to save code
- Merge _generate_mac_address_prefix() with generate_mac_address_prefix()
- Concentrate all functions that access the MAC address pool in the same module (kvm_utils.py)
- Minor style changes
--- client/tests/kvm/kvm_test_utils.py | 2 +- client/tests/kvm/kvm_utils.py | 185 +++++++++++++++++---------------- client/tests/kvm/kvm_vm.py | 93 ++++++------------ client/tests/kvm/tests/mac_change.py | 11 +-- 4 files changed, 130 insertions(+), 161 deletions(-)
client/tests/kvm/kvm_test_utils.py +1 -1
@@ -187,7 +187,7 @@ def migrate(vm, env=None, mig_timeout=3600, mig_protocol="tcp", # Clone the source VM and ask the clone to wait for incoming migration dest_vm = vm.clone() - if not dest_vm.create(extra_params=mig_extra_params): + if not dest_vm.create(extra_params=mig_extra_params, mac_source=vm): raise error.TestError("Could not create dest VM") try:
client/tests/kvm/kvm_utils.py +95 -90
220 lines not on this page · raw message
client/tests/kvm/kvm_vm.py +30 -63
181 lines not on this page · raw message
client/tests/kvm/tests/mac_change.py +4 -7
@@ -24,14 +24,11 @@ def run_mac_change(test, params, env): raise error.TestFail("Could not log into guest '%s'" % vm.name) old_mac = vm.get_mac_address(0) - new_different = False - while not new_different: - kvm_utils.free_mac_address(vm.root_dir, vm.instance, 0) - new_mac = kvm_utils.generate_mac_address(vm.root_dir, - vm.instance, - 0, vm.mac_prefix) + while True: + vm.free_mac_address(0) + new_mac = kvm_utils.generate_mac_address(vm.instance, 0) if old_mac != new_mac: - new_different = True + break logging.info("The initial MAC address is %s", old_mac) interface = kvm_test_utils.get_linux_ifname(session, old_mac) # Start change MAC address
Signature · 1 line
1.5.5.6
PATCH 2/7 KVM test: VM.get_address(): fix handling of multiple NICs
Use VM.get_mac_address() instead of kvm_utils.get_mac_ip_pair_from_dict().
--- client/tests/kvm/kvm_vm.py | 6 +----- 1 files changed, 1 insertions(+), 5 deletions(-)
client/tests/kvm/kvm_vm.py +1 -5
@@ -930,11 +930,7 @@ class VM: "%s" % mac) return None # Make sure the IP address is assigned to this guest - nic_dicts = [kvm_utils.get_sub_dict(self.params, nic) - for nic in nics] - macs = [kvm_utils.get_mac_ip_pair_from_dict(dict)[0] - for dict in nic_dicts] - macs.append(mac) + macs = [self.get_mac_address(i) for i in range(len(nics))] if not kvm_utils.verify_ip_address_ownership(ip, macs): logging.debug("Could not verify MAC-IP address mapping: " "%s ---> %s" % (mac, ip))
Signature · 1 line
1.5.5.6
RFC PATCH 3/7 KVM test: remove all code related to the old MAC address pool method
This patch removes all code related to the old manual method (address_pools.cfg).
Note that now running in TAP mode requires an external DHCP server that accepts *any* MAC address, because MAC addresses are randomly generated and cannot be manually configured.
--- client/tests/kvm/kvm_utils.py | 159 -------------------- client/tests/kvm/kvm_vm.py | 34 ++--- client/tests/kvm/tests/physical_resources_check.py | 11 +- client/tests/kvm/tests/stress_boot.py | 3 - client/tests/kvm/tests_base.cfg.sample | 2 - 5 files changed, 16 insertions(+), 193 deletions(-)
client/tests/kvm/kvm_utils.py +0 -159
@@ -203,165 +203,6 @@ def get_mac_address(vm_instance, nic_index): return mac -def mac_str_to_int(addr): - """ - Convert MAC address string to integer. - - @param addr: String representing the MAC address. - """ - return sum(int(s, 16) * 256 ** i - for i, s in enumerate(reversed(addr.split(":")))) - - -def mac_int_to_str(addr): - """ - Convert MAC address integer to string. - - @param addr: Integer representing the MAC address. - """ - return ":".join("%02x" % (addr >> 8 * i & 0xFF) - for i in reversed(range(6))) - - -def ip_str_to_int(addr): - """ - Convert IP address string to integer. - - @param addr: String representing the IP address. - """ - return sum(int(s) * 256 ** i - for i, s in enumerate(reversed(addr.split(".")))) - - -def ip_int_to_str(addr): - """ - Convert IP address integer to string. - - @param addr: Integer representing the IP address. - """ - return ".".join(str(addr >> 8 * i & 0xFF) - for i in reversed(range(4))) - - -def offset_mac(base, offset): - """ - Add offset to a given MAC address. - - @param base: String representing a MAC address. - @param offset: Offset to add to base (integer) - @return: A string representing the offset MAC address. - """ - return mac_int_to_str(mac_str_to_int(base) + offset) - - -def offset_ip(base, offset): - """ - Add offset to a given IP address. - - @param base: String representing an IP address. - @param offset: Offset to add to base (integer) - @return: A string representing the offset IP address. - """ - return ip_int_to_str(ip_str_to_int(base) + offset) - - -def get_mac_ip_pair_from_dict(dict): - """ - Fetch a MAC-IP address pair from dict and return it. - - The parameters in dict are expected to conform to a certain syntax. - Typical usage may be: - - address_ranges = r1 r2 r3 - - address_range_base_mac_r1 = 55:44:33:22:11:00 - address_range_base_ip_r1 = 10.0.0.0 - address_range_size_r1 = 16 - - address_range_base_mac_r2 = 55:44:33:22:11:40 - address_range_base_ip_r2 = 10.0.0.60 - address_range_size_r2 = 25 - - address_range_base_mac_r3 = 55:44:33:22:12:10 - address_range_base_ip_r3 = 10.0.1.20 - address_range_size_r3 = 230 - - address_index = 0 - - All parameters except address_index specify a MAC-IP address pool. The - pool consists of several MAC-IP address ranges. - address_index specified the index of the desired MAC-IP pair from the pool. - - @param dict: The dictionary from which to fetch the addresses. - """ - index = int(dict.get("address_index", 0)) - for mac_range_name in get_sub_dict_names(dict, "address_ranges"): - mac_range_params = get_sub_dict(dict, mac_range_name) - mac_base = mac_range_params.get("address_range_base_mac") - ip_base = mac_range_params.get("address_range_base_ip") - size = int(mac_range_params.get("address_range_size", 1)) - if index < size: - return (mac_base and offset_mac(mac_base, index), - ip_base and offset_ip(ip_base, index)) - index -= size - return (None, None) - - -def get_sub_pool(dict, piece, num_pieces): - """ - Split a MAC-IP pool and return a single requested piece. - - For example, get_sub_pool(dict, 0, 3) will split the pool in 3 pieces and - return a dict representing the first piece. - - @param dict: A dict that contains pool parameters. - @param piece: The index of the requested piece. Should range from 0 to - num_pieces - 1. - @param num_pieces: The total number of pieces. - @return: A copy of dict, modified to describe the requested sub-pool. - """ - range_dicts = [get_sub_dict(dict, name) for name in - get_sub_dict_names(dict, "address_ranges")] - if not range_dicts: - return dict - ranges = [[d.get("address_range_base_mac"), - d.get("address_range_base_ip"), - int(d.get("address_range_size", 1))] for d in range_dicts] - total_size = sum(r[2] for r in ranges) - base = total_size * piece / num_pieces - size = total_size * (piece + 1) / num_pieces - base - - # Find base of current sub-pool - for i in range(len(ranges)): - r = ranges[i] - if base < r[2]: - r[0] = r[0] and offset_mac(r[0], base) - r[1] = r[1] and offset_ip(r[1], base) - r[2] -= base - break - base -= r[2] - - # Collect ranges up to end of current sub-pool - new_ranges = [] - for i in range(i, len(ranges)): - r = ranges[i] - new_ranges.append(r) - if size <= r[2]: - r[2] = size - break - size -= r[2] - - # Write new dict - new_dict = dict.copy() - new_dict["address_ranges"] = " ".join("r%d" % i for i in - range(len(new_ranges))) - for i in range(len(new_ranges)): - new_dict["address_range_base_mac_r%d" % i] = new_ranges[i][0] - new_dict["address_range_base_ip_r%d" % i] = new_ranges[i][1] - new_dict["address_range_size_r%d" % i] = new_ranges[i][2] - return new_dict - - def verify_ip_address_ownership(ip, macs, timeout=10.0): """ Use arping and the ARP cache to make sure a given IP address belongs to one
client/tests/kvm/kvm_vm.py +13 -21
@@ -920,21 +920,18 @@ class VM: logging.debug("MAC address unavailable") return None mac = mac.lower() - ip = None - - if not ip or nic_params.get("always_use_tcpdump") == "yes": - # Get the IP address from the cache - ip = self.address_cache.get(mac) - if not ip: - logging.debug("Could not find IP address for MAC address: " - "%s" % mac) - return None - # Make sure the IP address is assigned to this guest - macs = [self.get_mac_address(i) for i in range(len(nics))] - if not kvm_utils.verify_ip_address_ownership(ip, macs): - logging.debug("Could not verify MAC-IP address mapping: " - "%s ---> %s" % (mac, ip)) - return None + # Get the IP address from the cache + ip = self.address_cache.get(mac) + if not ip: + logging.debug("Could not find IP address for MAC address: %s" % + mac) + return None + # Make sure the IP address is assigned to this guest + macs = [self.get_mac_address(i) for i in range(len(nics))] + if not kvm_utils.verify_ip_address_ownership(ip, macs): + logging.debug("Could not verify MAC-IP address mapping: " + "%s ---> %s" % (mac, ip)) + return None return ip else: return "localhost"
@@ -985,12 +982,7 @@ class VM: @param nic_index: Index of the NIC """ - nic_name = kvm_utils.get_sub_dict_names(self.params, "nics")[nic_index] - nic_params = kvm_utils.get_sub_dict(self.params, nic_name) - if nic_params.get("address_index"): - return kvm_utils.get_mac_ip_pair_from_dict(nic_params)[0] - else: - return kvm_utils.get_mac_address(self.instance, nic_index) + return kvm_utils.get_mac_address(self.instance, nic_index) def free_mac_address(self, nic_index=0):
client/tests/kvm/tests/physical_resources_check.py +3 -8
@@ -123,14 +123,9 @@ def run_physical_resources_check(test, params, env): found_mac_addresses = re.findall("macaddr=(\S+)", o) logging.debug("Found MAC adresses: %s" % found_mac_addresses) - nic_index = 0 - for nic_name in kvm_utils.get_sub_dict_names(params, "nics"): - nic_params = kvm_utils.get_sub_dict(params, nic_name) - if "address_index" in nic_params: - mac, ip = kvm_utils.get_mac_ip_pair_from_dict(nic_params) - else: - mac = vm.get_mac_address(nic_index) - nic_index += 1 + num_nics = len(kvm_utils.get_sub_dict_names(params, "nics")) + for nic_index in range(num_nics): + mac = vm.get_mac_address(nic_index) if not string.lower(mac) in found_mac_addresses: n_fail += 1 logging.error("MAC address mismatch:")
client/tests/kvm/tests/stress_boot.py +0 -3
@@ -28,7 +28,6 @@ def run_stress_boot(tests, params, env): num = 2 sessions = [session] - address_index = int(params.get("clone_address_index_base", 10)) # boot the VMs while num <= int(params.get("max_vms")):
@@ -36,7 +35,6 @@ def run_stress_boot(tests, params, env): # clone vm according to the first one vm_name = "vm" + str(num) vm_params = vm.get_params().copy() - vm_params["address_index"] = str(address_index) curr_vm = vm.clone(vm_name, vm_params) kvm_utils.env_register_vm(env, vm_name, curr_vm) logging.info("Booting guest #%d" % num)
@@ -56,7 +54,6 @@ def run_stress_boot(tests, params, env): if se.get_command_status(params.get("alive_test_cmd")) != 0: raise error.TestFail("Session #%d is not responsive" % i) num += 1 - address_index += 1 except (error.TestFail, OSError): for se in sessions:
client/tests/kvm/tests_base.cfg.sample +0 -2
@@ -54,7 +54,6 @@ guest_port_remote_shell = 22 nic_mode = user #nic_mode = tap nic_script = scripts/qemu-ifup -#address_index = 0 run_tcpdump = yes # Misc
@@ -274,7 +273,6 @@ variants: type = stress_boot max_vms = 5 alive_test_cmd = uname -a - clone_address_index_base = 10 login_timeout = 240 kill_vm = yes kill_vm_vm1 = no
Signature · 1 line
1.5.5.6
PATCH 4/7 KVM test: get_ifname(): use self.instance instead of self.vnc_port
The vnc_port attribute is only unique among VMs that use a VNC display. Other VMs don't bother to look for a free VNC port and don't occupy one, so vnc_port can't be considered unique. The last 11 characters of self.instance make up a fairly unique string which can be used instead. (The whole string can't be used because it exceeds 15 characters.)
--- client/tests/kvm/kvm_vm.py | 8 ++------ 1 files changed, 2 insertions(+), 6 deletions(-)
client/tests/kvm/kvm_vm.py +2 -6
@@ -959,10 +959,7 @@ class VM: def get_ifname(self, nic_index=0): """ - Return the ifname of tap device for the guest nic. - - The vnc_port is unique for each VM, nic_index is unique for each nic - of one VM, it can avoid repeated ifname. + Return the ifname of a tap device associated with a NIC. @param nic_index: Index of the NIC """
@@ -972,8 +969,7 @@ class VM: if nic_params.get("nic_ifname"): return nic_params.get("nic_ifname") else: - return "%s_%s_%s" % (nic_params.get("nic_model"), - nic_index, self.vnc_port) + return "t%d-%s" % (nic_index, self.instance[-11:]) def get_mac_address(self, nic_index=0):
Signature · 1 line
1.5.5.6
PATCH 5/7 KVM test: kvm_monitor.py: replace MonitorSendError with MonitorSocketError
- Replace MonitorSendError with MonitorSocketError.
- Embed socket.error messages in MonitorSocketError messages.
- Catch exceptions raised while receiving data (in addition to those raised while sending data).
--- client/tests/kvm/kvm_monitor.py | 35 ++++++++++++++++++++--------------- 1 files changed, 20 insertions(+), 15 deletions(-)
client/tests/kvm/kvm_monitor.py +20 -15
@@ -21,7 +21,7 @@ class MonitorConnectError(MonitorError): pass -class MonitorSendError(MonitorError): +class MonitorSocketError(MonitorError): pass
@@ -111,7 +111,11 @@ class Monitor: def _recvall(self): s = "" while self._data_available(): - data = self._socket.recv(1024) + try: + data = self._socket.recv(1024) + except socket.error, (errno, msg): + raise MonitorSocketError("Could not receive data from monitor " + "(%s)" % msg) if not data: break s += data
@@ -164,7 +168,7 @@ class HumanMonitor(Monitor): s = "" end_time = time.time() + timeout while self._data_available(end_time - time.time()): - data = self._socket.recv(1024) + data = self._recvall() if not data: break s += data
@@ -182,7 +186,7 @@ class HumanMonitor(Monitor): @param cmd: Command to send @raise MonitorLockError: Raised if the lock cannot be acquired - @raise MonitorSendError: Raised if the command cannot be sent + @raise MonitorSocketError: Raised if a socket error occurs """ if not self._acquire_lock(20): raise MonitorLockError("Could not acquire exclusive lock to send "
@@ -191,9 +195,9 @@ class HumanMonitor(Monitor): try: try: self._socket.sendall(cmd + "\n") - except socket.error: - raise MonitorSendError("Could not send monitor command '%s'" % - cmd) + except socket.error, (errno, msg): + raise MonitorSocketError("Could not send monitor command '%s' " + "(%s)" % (cmd, msg)) finally: self._lock.release()
@@ -209,7 +213,7 @@ class HumanMonitor(Monitor): @param timeout: Time duration to wait for the (qemu) prompt to return @return: Output received from the monitor @raise MonitorLockError: Raised if the lock cannot be acquired - @raise MonitorSendError: Raised if the command cannot be sent + @raise MonitorSocketError: Raised if a socket error occurs @raise MonitorProtocolError: Raised if the (qemu) prompt cannot be found after sending the command """
@@ -465,12 +469,13 @@ class QMPMonitor(Monitor): Send raw data without waiting for response. @param data: Data to send - @raise MonitorSendError: Raised if the data cannot be sent + @raise MonitorSocketError: Raised if a socket error occurs """ try: self._socket.sendall(data) - except socket.error: - raise MonitorSendError("Could not send data: %r" % data) + except socket.error, (errno, msg): + raise MonitorSocketError("Could not send data: %r (%s)" % + (data, msg)) def _get_response(self, id=None, timeout=20):
@@ -505,7 +510,7 @@ class QMPMonitor(Monitor): @param timeout: Time duration to wait for response @return: The response received @raise MonitorLockError: Raised if the lock cannot be acquired - @raise MonitorSendError: Raised if the command cannot be sent + @raise MonitorSocketError: Raised if a socket error occurs @raise MonitorProtocolError: Raised if no response is received @raise QMPCmdError: Raised if the response is an error message (the exception's args are (cmd, args, data) where data is the
@@ -547,7 +552,7 @@ class QMPMonitor(Monitor): @param timeout: Time duration to wait for response @return: The response received @raise MonitorLockError: Raised if the lock cannot be acquired - @raise MonitorSendError: Raised if the command cannot be sent + @raise MonitorSocketError: Raised if a socket error occurs @raise MonitorProtocolError: Raised if no response is received """ if not self._acquire_lock(20):
@@ -578,7 +583,7 @@ class QMPMonitor(Monitor): @param timeout: Time duration to wait for response @return: The response received @raise MonitorLockError: Raised if the lock cannot be acquired - @raise MonitorSendError: Raised if the command cannot be sent + @raise MonitorSocketError: Raised if a socket error occurs @raise MonitorProtocolError: Raised if no response is received """ return self.cmd_raw(json.dumps(obj) + "\n")
@@ -597,7 +602,7 @@ class QMPMonitor(Monitor): @param timeout: Time duration to wait for response @return: The response received @raise MonitorLockError: Raised if the lock cannot be acquired - @raise MonitorSendError: Raised if the command cannot be sent + @raise MonitorSocketError: Raised if a socket error occurs @raise MonitorProtocolError: Raised if no response is received """ return self.cmd_obj(self._build_cmd(cmd, args, id), timeout)
Signature · 1 line
1.5.5.6
PATCH 6/7 KVM test: refactor migration code
- Refactor migration code so that the '-incoming ...' strings are hardcoded only in a single location (kvm_test_utils.py).
- Wrap the removal of the gzipped state file in a finally: clause.
- Get rid of the 'for_migration' and 'extra_params' parameters of VM.create() and introduce 'migration_mode' and 'migration_exec_cmd'
- Remove unix socket files in VM.destroy() because they are created by VM.create() (as opposed to gzipped state files which are created in kvm_test_utils.migrate())
--- client/tests/kvm/kvm_preprocessing.py | 10 +-- client/tests/kvm/kvm_test_utils.py | 115 ++++++++++++++++----------------- client/tests/kvm/kvm_vm.py | 44 ++++++------ 3 files changed, 80 insertions(+), 89 deletions(-)
client/tests/kvm/kvm_preprocessing.py +2 -8
@@ -59,14 +59,8 @@ def preprocess_vm(test, params, env, name): kvm_utils.env_register_vm(env, name, vm) start_vm = False - for_migration = False - if params.get("start_vm_for_migration") == "yes": - logging.debug("'start_vm_for_migration' specified; (re)starting VM " - "with -incoming option...") - start_vm = True - for_migration = True - elif params.get("restart_vm") == "yes": + if params.get("restart_vm") == "yes": logging.debug("'restart_vm' specified; (re)starting VM...") start_vm = True elif params.get("start_vm") == "yes":
@@ -81,7 +75,7 @@ def preprocess_vm(test, params, env, name): if start_vm: # Start the VM (or restart it if it's already up) - if not vm.create(name, params, test.bindir, for_migration): + if not vm.create(name, params, test.bindir): raise error.TestError("Could not start VM") else: # Don't start the VM, just update its params
client/tests/kvm/kvm_test_utils.py +55 -58
134 lines not on this page · raw message
client/tests/kvm/kvm_vm.py +22 -22
68 lines not on this page · raw message
Signature · 1 line
1.5.5.6
RFC PATCH 7/7 KVM test: migrate_cancel: allow for alternative spellings of 'cancelled'
Is there any chance someone will decide to switch over to the American spelling?
--- client/tests/kvm/kvm_test_utils.py | 6 ++++-- 1 files changed, 4 insertions(+), 2 deletions(-)
client/tests/kvm/kvm_test_utils.py +4 -2
@@ -157,9 +157,11 @@ def migrate(vm, env=None, mig_timeout=3600, mig_protocol="tcp", def mig_cancelled(): o = vm.monitor.info("migrate") if isinstance(o, str): - return "Migration status: cancelled" in o + return ("Migration status: cancelled" in o or + "Migration status: canceled" in o) else: - return o.get("status") == "cancelled" + return (o.get("status") == "cancelled" or + o.get("status") == "canceled") def wait_for_migration(): if not kvm_utils.wait_for(mig_finished, mig_timeout, 2, 2,
Signature · 1 line
1.5.5.6
Re: [KVM-AUTOTEST PATCH 7/7] [RFC] KVM test: migrate_cancel: allow for alternative spellings of 'cancelled'
Quoted 1 line · Michael Goldish
Is there any chance someone will decide to switch over to the American spelling?
This is mainly harmless. Even if the switch won't happen, this is absolutely fine, thanks!
Quoted 23 lines
Signed-off-by Michael Goldish <[email protected]>--- client/tests/kvm/kvm_test_utils.py | 6 ++++-- 1 files changed, 4 insertions(+), 2 deletions(-)client/tests/kvm/kvm_test_utils.py +4
-2@@ -157,9 +157,11 @@ def migrate(vm, env=None, mig_timeout=3600, mig_protocol="tcp", def mig_cancelled(): o = vm.monitor.info("migrate") if isinstance(o, str): - return "Migration status: cancelled" in o + return ("Migration status: cancelled" in o or + "Migration status: canceled" in o) else: - return o.get("status") == "cancelled" + return (o.get("status") == "cancelled" or + o.get("status") == "canceled") def wait_for_migration(): if not kvm_utils.wait_for(mig_finished, mig_timeout, 2, 2,
Re: [KVM-AUTOTEST PATCH 3/7] [RFC] KVM test: remove all code related to the old MAC address pool method
In reply to Michael GoldishQuoted 6 lines · Michael Goldish
This patch removes all code related to the old manual method (address_pools.cfg).
Note that now running in TAP mode requires an external DHCP server that accepts *any* MAC address, because MAC addresses are randomly generated and cannot be manually configured.
Yes, this patch looks good. I don't think there are much DHCP servers that do not accept any MAC address. It might be good to document this information on the wiki though.
Quoted 301 lines
Signed-off-by Michael Goldish <[email protected]>--- client/tests/kvm/kvm_utils.py | 159 -------------------- client/tests/kvm/kvm_vm.py | 34 ++--- client/tests/kvm/tests/physical_resources_check.py | 11 +- client/tests/kvm/tests/stress_boot.py | 3 - client/tests/kvm/tests_base.cfg.sample | 2 - 5 files changed, 16 insertions(+), 193 deletions(-)client/tests/kvm/kvm_utils.py +0
-159@@ -203,165 +203,6 @@ def get_mac_address(vm_instance, nic_index): return mac -def mac_str_to_int(addr): - """ - Convert MAC address string to integer. - - @param addr: String representing the MAC address. - """ - return sum(int(s, 16) * 256 ** i - for i, s in enumerate(reversed(addr.split(":")))) - - -def mac_int_to_str(addr): - """ - Convert MAC address integer to string. - - @param addr: Integer representing the MAC address. - """ - return ":".join("%02x" % (addr >> 8 * i & 0xFF) - for i in reversed(range(6))) - - -def ip_str_to_int(addr): - """ - Convert IP address string to integer. - - @param addr: String representing the IP address. - """ - return sum(int(s) * 256 ** i - for i, s in enumerate(reversed(addr.split(".")))) - - -def ip_int_to_str(addr): - """ - Convert IP address integer to string. - - @param addr: Integer representing the IP address. - """ - return ".".join(str(addr >> 8 * i & 0xFF) - for i in reversed(range(4))) - - -def offset_mac(base, offset): - """ - Add offset to a given MAC address. - - @param base: String representing a MAC address. - @param offset: Offset to add to base (integer) - @return: A string representing the offset MAC address. - """ - return mac_int_to_str(mac_str_to_int(base) + offset) - - -def offset_ip(base, offset): - """ - Add offset to a given IP address. - - @param base: String representing an IP address. - @param offset: Offset to add to base (integer) - @return: A string representing the offset IP address. - """ - return ip_int_to_str(ip_str_to_int(base) + offset) - - -def get_mac_ip_pair_from_dict(dict): - """ - Fetch a MAC-IP address pair from dict and return it. - - The parameters in dict are expected to conform to a certain syntax. - Typical usage may be: - - address_ranges = r1 r2 r3 - - address_range_base_mac_r1 = 55:44:33:22:11:00 - address_range_base_ip_r1 = 10.0.0.0 - address_range_size_r1 = 16 - - address_range_base_mac_r2 = 55:44:33:22:11:40 - address_range_base_ip_r2 = 10.0.0.60 - address_range_size_r2 = 25 - - address_range_base_mac_r3 = 55:44:33:22:12:10 - address_range_base_ip_r3 = 10.0.1.20 - address_range_size_r3 = 230 - - address_index = 0 - - All parameters except address_index specify a MAC-IP address pool. The - pool consists of several MAC-IP address ranges. - address_index specified the index of the desired MAC-IP pair from the pool. - - @param dict: The dictionary from which to fetch the addresses. - """ - index = int(dict.get("address_index", 0)) - for mac_range_name in get_sub_dict_names(dict, "address_ranges"): - mac_range_params = get_sub_dict(dict, mac_range_name) - mac_base = mac_range_params.get("address_range_base_mac") - ip_base = mac_range_params.get("address_range_base_ip") - size = int(mac_range_params.get("address_range_size", 1)) - if index < size: - return (mac_base and offset_mac(mac_base, index), - ip_base and offset_ip(ip_base, index)) - index -= size - return (None, None) - - -def get_sub_pool(dict, piece, num_pieces): - """ - Split a MAC-IP pool and return a single requested piece. - - For example, get_sub_pool(dict, 0, 3) will split the pool in 3 pieces and - return a dict representing the first piece. - - @param dict: A dict that contains pool parameters. - @param piece: The index of the requested piece. Should range from 0 to - num_pieces - 1. - @param num_pieces: The total number of pieces. - @return: A copy of dict, modified to describe the requested sub-pool. - """ - range_dicts = [get_sub_dict(dict, name) for name in - get_sub_dict_names(dict, "address_ranges")] - if not range_dicts: - return dict - ranges = [[d.get("address_range_base_mac"), - d.get("address_range_base_ip"), - int(d.get("address_range_size", 1))] for d in range_dicts] - total_size = sum(r[2] for r in ranges) - base = total_size * piece / num_pieces - size = total_size * (piece + 1) / num_pieces - base - - # Find base of current sub-pool - for i in range(len(ranges)): - r = ranges[i] - if base < r[2]: - r[0] = r[0] and offset_mac(r[0], base) - r[1] = r[1] and offset_ip(r[1], base) - r[2] -= base - break - base -= r[2] - - # Collect ranges up to end of current sub-pool - new_ranges = [] - for i in range(i, len(ranges)): - r = ranges[i] - new_ranges.append(r) - if size <= r[2]: - r[2] = size - break - size -= r[2] - - # Write new dict - new_dict = dict.copy() - new_dict["address_ranges"] = " ".join("r%d" % i for i in - range(len(new_ranges))) - for i in range(len(new_ranges)): - new_dict["address_range_base_mac_r%d" % i] = new_ranges[i][0] - new_dict["address_range_base_ip_r%d" % i] = new_ranges[i][1] - new_dict["address_range_size_r%d" % i] = new_ranges[i][2] - return new_dict - - def verify_ip_address_ownership(ip, macs, timeout=10.0): """ Use arping and the ARP cache to make sure a given IP address belongs to oneclient/tests/kvm/kvm_vm.py +13
-21@@ -920,21 +920,18 @@ class VM: logging.debug("MAC address unavailable") return None mac = mac.lower() - ip = None - - if not ip or nic_params.get("always_use_tcpdump") == "yes": - # Get the IP address from the cache - ip = self.address_cache.get(mac) - if not ip: - logging.debug("Could not find IP address for MAC address: " - "%s" % mac) - return None - # Make sure the IP address is assigned to this guest - macs = [self.get_mac_address(i) for i in range(len(nics))] - if not kvm_utils.verify_ip_address_ownership(ip, macs): - logging.debug("Could not verify MAC-IP address mapping: " - "%s ---> %s" % (mac, ip)) - return None + # Get the IP address from the cache + ip = self.address_cache.get(mac) + if not ip: + logging.debug("Could not find IP address for MAC address: %s" % + mac) + return None + # Make sure the IP address is assigned to this guest + macs = [self.get_mac_address(i) for i in range(len(nics))] + if not kvm_utils.verify_ip_address_ownership(ip, macs): + logging.debug("Could not verify MAC-IP address mapping: " + "%s ---> %s" % (mac, ip)) + return None return ip else: return "localhost"@@ -985,12 +982,7 @@ class VM: @param nic_index: Index of the NIC """ - nic_name = kvm_utils.get_sub_dict_names(self.params, "nics")[nic_index] - nic_params = kvm_utils.get_sub_dict(self.params, nic_name) - if nic_params.get("address_index"): - return kvm_utils.get_mac_ip_pair_from_dict(nic_params)[0] - else: - return kvm_utils.get_mac_address(self.instance, nic_index) + return kvm_utils.get_mac_address(self.instance, nic_index) def free_mac_address(self, nic_index=0):client/tests/kvm/tests/physical_resources_check.py +3
-8@@ -123,14 +123,9 @@ def run_physical_resources_check(test, params, env): found_mac_addresses = re.findall("macaddr=(\S+)", o) logging.debug("Found MAC adresses: %s" % found_mac_addresses) - nic_index = 0 - for nic_name in kvm_utils.get_sub_dict_names(params, "nics"): - nic_params = kvm_utils.get_sub_dict(params, nic_name) - if "address_index" in nic_params: - mac, ip = kvm_utils.get_mac_ip_pair_from_dict(nic_params) - else: - mac = vm.get_mac_address(nic_index) - nic_index += 1 + num_nics = len(kvm_utils.get_sub_dict_names(params, "nics")) + for nic_index in range(num_nics): + mac = vm.get_mac_address(nic_index) if not string.lower(mac) in found_mac_addresses: n_fail += 1 logging.error("MAC address mismatch:")client/tests/kvm/tests/stress_boot.py +0
-3@@ -28,7 +28,6 @@ def run_stress_boot(tests, params, env): num = 2 sessions = [session] - address_index = int(params.get("clone_address_index_base", 10)) # boot the VMs while num <= int(params.get("max_vms")):@@ -36,7 +35,6 @@ def run_stress_boot(tests, params, env): # clone vm according to the first one vm_name = "vm" + str(num) vm_params = vm.get_params().copy() - vm_params["address_index"] = str(address_index) curr_vm = vm.clone(vm_name, vm_params) kvm_utils.env_register_vm(env, vm_name, curr_vm) logging.info("Booting guest #%d" % num)@@ -56,7 +54,6 @@ def run_stress_boot(tests, params, env): if se.get_command_status(params.get("alive_test_cmd")) != 0: raise error.TestFail("Session #%d is not responsive" % i) num += 1 - address_index += 1 except (error.TestFail, OSError): for se in sessions:client/tests/kvm/tests_base.cfg.sample +0
-2@@ -54,7 +54,6 @@ guest_port_remote_shell = 22 nic_mode = user #nic_mode = tap nic_script = scripts/qemu-ifup -#address_index = 0 run_tcpdump = yes # Misc@@ -274,7 +273,6 @@ variants: type = stress_boot max_vms = 5 alive_test_cmd = uname -a - clone_address_index_base = 10 login_timeout = 240 kill_vm = yes kill_vm_vm1 = no