diff mbox

[KVM-AUTOTEST,v2,10/11] KVM test: make VMs use a dynamic MAC-IP mapping generated by tcpdump

Message ID 0df7966e6378b54be138c866c39f2d1e58c67333.1250091576.git.mgoldish@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Michael Goldish Aug. 12, 2009, 4 p.m. UTC
In VM.get_address(), return an IP address from the MAC-IP cache if an IP
address base wasn't specified by the user, or if the user requested that IP
addresses be taken from the dynamic cache.

To force the system to obtain IP addresses from the dynamic cache even when
static base addresses are provided by the user, specify
'always_use_tcpdump = yes'.

Signed-off-by: Michael Goldish <mgoldish@redhat.com>
---
 client/tests/kvm/kvm_address_pools.cfg.sample |    8 +++
 client/tests/kvm/kvm_preprocessing.py         |    3 +-
 client/tests/kvm/kvm_utils.py                 |   37 +++++++++++++-
 client/tests/kvm/kvm_vm.py                    |   69 ++++++++++++++++++-------
 4 files changed, 96 insertions(+), 21 deletions(-)
diff mbox

Patch

diff --git a/client/tests/kvm/kvm_address_pools.cfg.sample b/client/tests/kvm/kvm_address_pools.cfg.sample
index 8a27ee1..debfe56 100644
--- a/client/tests/kvm/kvm_address_pools.cfg.sample
+++ b/client/tests/kvm/kvm_address_pools.cfg.sample
@@ -6,6 +6,14 @@ 
 # If you wish to use a static MAC-IP mapping, where each MAC address range is
 # mapped to a known corresponding IP address range, specify the bases of the IP
 # address ranges in this file.
+# If you specify a MAC address range without a corresponding IP address range,
+# the IP addresses for that range will be determined at runtime by listening
+# to DHCP traffic using tcpdump.
+# If you wish to determine IP addresses using tcpdump in any case, regardless
+# of any # IP addresses specified in this file, uncomment the following line:
+#always_use_tcpdump = yes
+# You may also specify this parameter for specific hosts by adding it in the
+# appropriate sections below.
 
 variants:
     # Rename host1 to an actual (short) hostname in the network that will be running the Autotest client
diff --git a/client/tests/kvm/kvm_preprocessing.py b/client/tests/kvm/kvm_preprocessing.py
index 20b8cf6..347108f 100644
--- a/client/tests/kvm/kvm_preprocessing.py
+++ b/client/tests/kvm/kvm_preprocessing.py
@@ -54,7 +54,8 @@  def preprocess_vm(test, params, env, name):
         logging.debug("VM object found in environment")
     else:
         logging.debug("VM object does not exist; creating it")
-        vm = kvm_vm.VM(name, params, qemu_path, image_dir, iso_dir, script_dir)
+        vm = kvm_vm.VM(name, params, qemu_path, image_dir, iso_dir, script_dir,
+                       env.get("address_cache"))
         kvm_utils.env_register_vm(env, name, vm)
 
     start_vm = False
diff --git a/client/tests/kvm/kvm_utils.py b/client/tests/kvm/kvm_utils.py
index ef257bc..c3e5502 100644
--- a/client/tests/kvm/kvm_utils.py
+++ b/client/tests/kvm/kvm_utils.py
@@ -1,5 +1,5 @@ 
 import md5, thread, subprocess, time, string, random, socket, os, signal, pty
-import select, re, logging
+import select, re, logging, commands
 from autotest_lib.client.bin import utils
 from autotest_lib.client.common_lib import error
 import kvm_subprocess
@@ -152,6 +152,41 @@  def get_mac_ip_pair_from_dict(dict):
     return (None, None)
 
 
+def verify_ip_address_ownership(ip, macs, timeout=3.0):
+    """
+    Connect to a given IP address and make sure its MAC address equals one of
+    the given MAC address.
+
+    @param ip: An IP address.
+    @param macs: A list or tuple of MAC addresses.
+    @return: True iff ip is assigned to a MAC address in macs.
+    """
+    def check_arp_cache(regex):
+        o = commands.getoutput("/sbin/arp -n")
+        return bool(re.search(regex, o, re.IGNORECASE))
+
+    mac_regex = "|".join("(%s)" % mac for mac in macs)
+    regex = re.compile(r"\b%s\b.*\b(%s)\b" % (ip, mac_regex))
+
+    if check_arp_cache(regex):
+        return True
+
+    s = socket.socket()
+    s.setblocking(False)
+    try:
+        s.connect((ip, 55555))
+    except socket.error:
+        pass
+    end_time = time.time() + timeout
+    while time.time() < end_time:
+        time.sleep(0.2)
+        if check_arp_cache(regex):
+            s.close()
+            return True
+    s.close()
+    return False
+
+
 # Functions for working with the environment (a dict-like object)
 
 def is_vm(obj):
diff --git a/client/tests/kvm/kvm_vm.py b/client/tests/kvm/kvm_vm.py
index 33f37e5..7bedc4e 100644
--- a/client/tests/kvm/kvm_vm.py
+++ b/client/tests/kvm/kvm_vm.py
@@ -1,5 +1,5 @@ 
 #!/usr/bin/python
-import time, socket, os, logging, fcntl
+import time, socket, os, logging, fcntl, re, commands
 import kvm_utils, kvm_subprocess
 
 """
@@ -101,7 +101,7 @@  class VM:
     """
 
     def __init__(self, name, params, qemu_path, image_dir, iso_dir,
-                 script_dir):
+                 script_dir, address_cache):
         """
         Initialize the object and set a few attributes.
 
@@ -112,6 +112,7 @@  class VM:
         @param image_dir: The directory where images reside
         @param iso_dir: The directory where ISOs reside
         @param script_dir: The directory where -net tap scripts reside
+        @param address_cache: A dict that maps MAC addresses to IP addresses
         """
         self.process = None
         self.redirs = {}
@@ -124,6 +125,7 @@  class VM:
         self.image_dir = image_dir
         self.iso_dir = iso_dir
         self.script_dir = script_dir
+        self.address_cache = address_cache
 
         # Find available monitor filename
         while True:
@@ -137,7 +139,7 @@  class VM:
 
 
     def clone(self, name=None, params=None, qemu_path=None, image_dir=None,
-              iso_dir=None, script_dir=None):
+              iso_dir=None, script_dir=None, address_cache=None):
         """
         Return a clone of the VM object with optionally modified parameters.
         The clone is initially not alive and needs to be started using create().
@@ -150,6 +152,7 @@  class VM:
         @param image_dir: Optional new image dir
         @param iso_dir: Optional new iso directory
         @param script_dir: Optional new -net tap script directory
+        @param address_cache: A dict that maps MAC addresses to IP addresses
         """
         if name == None:
             name = self.name
@@ -163,7 +166,10 @@  class VM:
             iso_dir = self.iso_dir
         if script_dir == None:
             script_dir = self.script_dir
-        return VM(name, params, qemu_path, image_dir, iso_dir, script_dir)
+        if address_cache == None:
+            address_cache = self.address_cache
+        return VM(name, params, qemu_path, image_dir, iso_dir, script_dir,
+                  address_cache)
 
 
     def make_qemu_command(self, name=None, params=None, qemu_path=None,
@@ -621,25 +627,45 @@  class VM:
 
         @param index: Index of the NIC whose address is requested.
         """
-        nic_name = kvm_utils.get_sub_dict_names(self.params, "nics")[index]
+        nics = kvm_utils.get_sub_dict_names(self.params, "nics")
+        nic_name = nics[index]
         nic_params = kvm_utils.get_sub_dict(self.params, nic_name)
         if nic_params.get("nic_mode") == "tap":
             mac, ip = kvm_utils.get_mac_ip_pair_from_dict(nic_params)
+            if not mac:
+                logging.debug("MAC address unavailable")
+                return 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
+                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]
+                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"
 
 
-    def get_port(self, port, index=0):
+    def get_port(self, port, nic_index=0):
         """
         Return the port in host space corresponding to port in guest space.
 
         @param port: Port number in host space.
-        @param index: Index of the NIC.
+        @param nic_index: Index of the NIC.
         @return: If port redirection is used, return the host port redirected
                 to guest port port. Otherwise return port.
         """
-        nic_name = kvm_utils.get_sub_dict_names(self.params, "nics")[index]
+        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("nic_mode") == "tap":
             return port
@@ -666,17 +692,19 @@  class VM:
         """
         address = self.get_address()
         port = self.get_port(int(self.params.get("ssh_port")))
-        if not port:
+        if not address or not port:
+            logging.debug("IP address or port unavailable")
             return False
         return kvm_utils.is_sshd_running(address, port, timeout=timeout)
 
 
-    def ssh_login(self, timeout=10):
+    def ssh_login(self, nic_index=0, timeout=10):
         """
         Log into the guest via SSH/Telnet.
         If timeout expires while waiting for output from the guest (e.g. a
         password prompt or a shell prompt) -- fail.
 
+        @param nic_index: The index of the NIC to connect to.
         @param timeout: Time (seconds) before giving up logging into the
                 guest.
         @return: kvm_spawn object on success and None on failure.
@@ -685,9 +713,10 @@  class VM:
         password = self.params.get("password", "")
         prompt = self.params.get("ssh_prompt", "[\#\$]")
         use_telnet = self.params.get("use_telnet") == "yes"
-        address = self.get_address()
+        address = self.get_address(nic_index)
         port = self.get_port(int(self.params.get("ssh_port")))
-        if not port:
+        if not address or not port:
+            logging.debug("IP address or port unavailable")
             return None
 
         if use_telnet:
@@ -702,7 +731,7 @@  class VM:
         return session
 
 
-    def scp_to_remote(self, local_path, remote_path, timeout=300):
+    def scp_to_remote(self, local_path, remote_path, nic_index=0, timeout=300):
         """
         Transfer files to the guest via SCP.
 
@@ -713,15 +742,16 @@  class VM:
         """
         username = self.params.get("username", "")
         password = self.params.get("password", "")
-        address = self.get_address()
+        address = self.get_address(nic_index)
         port = self.get_port(int(self.params.get("ssh_port")))
-        if not port:
+        if not address or not port:
+            logging.debug("IP address or port unavailable")
             return None
         return kvm_utils.scp_to_remote(address, port, username, password,
                                        local_path, remote_path, timeout)
 
 
-    def scp_from_remote(self, remote_path, local_path, timeout=300):
+    def scp_from_remote(self, remote_path, local_path, nic_index=0, timeout=300):
         """
         Transfer files from the guest via SCP.
 
@@ -732,9 +762,10 @@  class VM:
         """
         username = self.params.get("username", "")
         password = self.params.get("password", "")
-        address = self.get_address()
+        address = self.get_address(nic_index)
         port = self.get_port(int(self.params.get("ssh_port")))
-        if not port:
+        if not address or not port:
+            logging.debug("IP address or port unavailable")
             return None
         return kvm_utils.scp_from_remote(address, port, username, password,
                                          remote_path, local_path, timeout)
@@ -749,7 +780,7 @@  class VM:
         @return: A tuple (status, output). status is 0 on success and 1 on
                 failure.
         """
-        session = self.ssh_login(timeout)
+        session = self.ssh_login(timeout=timeout)
         if not session:
             return (1, "")