Skip to contentSkip to conversation

TAP patchset corrections

PATCH In review Michael Goldish 2 participants 13 messages Aug 12, 2009 – Aug 13, 2009 KVM

lore
Share & tools
lore
#0 lore raw

v2 cover TAP patchset corrections

Differences compared to the previous patchset:
- In kvm_utils.verify_ip_address_ownership() and kvm_vm.get_address():
Require that '/sbin/arp -n' list any of the the VM's MAC addresses, not necessarily the MAC address being verified.
After having a very hard time deciding which solution was better, I chose this one because I think it is slightly safer, and it is more similar to the code in the original patch set, which I feel rather confident in since it has had some testing.
The new code has been tested and works with RHEL 3.9 which is one of the problematic guests (with 2 NICs).
However, it can and should be replaced if it causes any trouble (i.e. if we find out that some of our guests make it impossible to find their MAC addresses this way).

- User networking mode is the default. We can make TAP mode the default when it matures a little.

- qemu-ifup is in test.bindir/scripts, instead of test.bindir.

Some of the patches in this set have already been applied. Please ignore those.

#1 lore raw

PATCH v2 1/11 KVM test: add some MAC/IP address utilities to kvm_utils

Add function get_mac_ip_pair_from_dict() which gets a dict (specified in the config file by the user) and fetches a MAC-IP address pair according to a certain syntax. The syntax allows the user to specify a group of MAC-IP address ranges. For example:

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

The lines above may be specified globally, so that they apply to all VMs and all NICs.

However, a line similar to the following must be specified per NIC (each VM may have several NICs):

address_index = 0

Currently, we usually use a single VM and a single NIC, so specifying address_index once should suffice. If a test requires an additional NIC the user should add something like:

address_index_nic2 = 1

The index is simply the index in the MAC-IP table that consists of all the specified ranges. In the above example, if the user specifies an index of 18, the MAC-IP pair will be taken from the second range, because the first range has only 16 entries.

When running migration, both the source and destination VMs should have the same address_index, because they should have the same MAC and IP addresses.

Note that different copies of the KVM test, running simultaneously in the same network environment, _must_ specify different MAC-IP pools. This can be done in several ways:
- By specifying the ranges (as in the example above) in an external file such as /etc/kvm-autotest/client_mac_ip_pool.cfg, and setting up that file for each host manually
- By keeping several .cfg files with different names that match the hostname of each host, e.g. mac_ip_pool_hostname1.cfg, mac_ip_pool_hostname2.cfg (hostname1 and hostname2 should be replaced by actual hostnames), and parsing the right file in the control file at runtime
- By defining all the different pools in a variants block in a single file, and specifying 'only <hostname>' at runtime in the control file (using config.parse_string())
When we start running in server mode, assigning MAC and IP addresses to hosts can be done automatically by the server, but the user will still be required to specify a single global pool for the server (which the server will divide among the hosts). The address_index parameter will be specified inside the regular config file, and does not need to be different for each host.

This patch also adds some small utility functions used by get_mac_ip_pair_from_dict().

---
 client/tests/kvm/kvm_utils.py |  106 +++++++++++++++++++++++++++++++++++++++++
 1 files changed, 106 insertions(+), 0 deletions(-)
client/tests/kvm/kvm_utils.py +106 -0
index e897e78..4891592 100644
@@ -46,6 +46,112 @@ def get_sub_dict_names(dict, keyword):
         return []
 
 
+# Functions related to MAC/IP addresses
+
+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)
+
+
 # Functions for working with the environment (a dict-like object)
 
 def is_vm(obj):
Signature · 1 line
1.5.4.1
#2 lore raw

PATCH v2 2/11 KVM test: add basic TAP support with static MAC-IP mapping

The network mode is specified by a NIC parameter named 'nic_mode'. Currently two modes are supported: 'tap' and 'user'.

Other NIC parameters are now handled: nic_script, nic_downscript and nic_ifname. nic_ifname should usually not be specified (qemu chooses one automatically). nic_script and nic_downscript may specify a relative path, e.g. 'qemu-ifup', which refers to a file in the KVM test local dir, or an absolute path, e.g. '/etc/qemu-ifup'.

This patch mainly modifies VM.get_address(), VM.get_port() and VM.make_qemu_command(), and adds a 'script_dir' parameter to several VM routines. 'script_dir' should normally be the directory 'scripts' under the main KVM test directory.

---
 client/tests/kvm/kvm_preprocessing.py |    8 ++-
 client/tests/kvm/kvm_tests.cfg.sample |    6 ++
 client/tests/kvm/kvm_vm.py            |   95 +++++++++++++++++++++++---------
 3 files changed, 79 insertions(+), 30 deletions(-)
client/tests/kvm/kvm_preprocessing.py +5 -3
index d118826..22c2b8b 100644
@@ -46,6 +46,7 @@ def preprocess_vm(test, params, env, name):
     qemu_path = os.path.join(test.bindir, "qemu")
     image_dir = os.path.join(test.bindir, "images")
     iso_dir = os.path.join(test.bindir, "isos")
+    script_dir = os.path.join(test.bindir, "scripts")
 
     logging.debug("Preprocessing VM '%s'..." % name)
     vm = kvm_utils.env_get_vm(env, name)
@@ -53,7 +54,7 @@ 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)
+        vm = kvm_vm.VM(name, params, qemu_path, image_dir, iso_dir, script_dir)
         kvm_utils.env_register_vm(env, name, vm)
 
     start_vm = False
@@ -74,14 +75,15 @@ def preprocess_vm(test, params, env, name):
         elif vm.make_qemu_command() != vm.make_qemu_command(name, params,
                                                             qemu_path,
                                                             image_dir,
-                                                            iso_dir):
+                                                            iso_dir,
+                                                            script_dir):
             logging.debug("VM's qemu command differs from requested one; "
                           "restarting it...")
             start_vm = True
 
     if start_vm:
         if not vm.create(name, params, qemu_path, image_dir, iso_dir,
-                         for_migration):
+                         script_dir, for_migration):
             message = "Could not start VM"
             logging.error(message)
             raise error.TestError(message)
client/tests/kvm/kvm_tests.cfg.sample +6 -0
index 7cd12cb..9ad115e 100644
@@ -24,6 +24,12 @@ display = vnc
 redirs = ssh
 guest_port_ssh = 22
 
+# NIC parameters
+nic_mode = user
+#nic_mode = tap
+nic_script = qemu-ifup
+address_index = 0
+
 
 # Tests
 variants:
client/tests/kvm/kvm_vm.py +68 -27

217 lines not on this page · raw message

Signature · 1 line
1.5.4.1
#3 lore raw

PATCH v2 3/11 KVM test: add sample 'qemu-ifup' script

The script adds a requested interface to an existing bridge. It is meant to be used by qemu when running in TAP mode.

Note: the user is responsible for setting up the bridge before running any tests. This can be done with brctl or in any manner that is appropriate for the host OS. It can be done inside 'qemu-ifup' as well, but this sample script doesn't do it.

---
 client/tests/kvm/scripts/qemu-ifup |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)
 create mode 100755 client/tests/kvm/scripts/qemu-ifup
client/tests/kvm/scripts/qemu-ifup new file +8 -0
new file mode 100755
index 0000000..bcd9a7a
@@ -0,0 +1,8 @@
+#!/bin/sh
+
+# The following expression selects the first bridge listed by 'brctl show'.
+# Modify it to suit your needs.
+switch=$(/usr/sbin/brctl show | awk 'NR==2 { print $1 }')
+
+/sbin/ifconfig $1 0.0.0.0 up
+/usr/sbin/brctl addif ${switch} $1
Signature · 1 line
1.5.4.1
#4 lore raw

PATCH v2 4/11 KVM test: add host-specific MAC-IP pool configuration

- Add kvm_address_pools.cfg which specifies a set of MAC-IP ranges for each host.
- Read kvm_address_pools.cfg in the control file and filter it by hostname, so that each host uses only the MAC addresses assigned to it. If the hostname doesn't match any hostname in kvm_address_pools.cfg, use 'default_host'.
- While we're at it, add a comment informing the user where to modify the test configration, if that is desirable
- (Also add a missing space somewhere in the control file, and remove a few extra ones)

---
 client/tests/kvm/control                      |   25 +++++++++--
 client/tests/kvm/kvm_address_pools.cfg.sample |   56 +++++++++++++++++++++++++
 2 files changed, 76 insertions(+), 5 deletions(-)
 create mode 100644 client/tests/kvm/kvm_address_pools.cfg.sample
client/tests/kvm/control +20 -5
index 5c5d424..2ad8d81 100644
@@ -133,12 +133,27 @@ if not r:
 import kvm_config
 
 filename = os.path.join(pwd, "kvm_tests.cfg")
-list = kvm_config.config(filename).get_list()
+cfg = kvm_config.config(filename)
+
+# If desirable, make changes to the test configuration here.  For example:
+# cfg.parse_string("only fc8_quick")
+# cfg.parse_string("display = sdl")
+
+filename = os.path.join(pwd, "kvm_address_pools.cfg")
+if os.path.exists(filename):
+    cfg.parse_file(filename)
+    hostname = os.uname()[1].split(".")[0]
+    if cfg.filter("^" + hostname):
+        cfg.parse_string("only ^%s" % hostname)
+    else:
+        cfg.parse_string("only ^default_host")
+
+list = cfg.get_list()
 
 
-#  -------------
-#  Run the tests
-#  -------------
+# -------------
+# Run the tests
+# -------------
 status_dict = {}
 
 for dict in list:
@@ -153,7 +168,7 @@ for dict in list:
                 dependencies_satisfied = False
                 break
     if dependencies_satisfied:
-        test_iterations=int(dict.get("iterations", 1))
+        test_iterations = int(dict.get("iterations", 1))
         current_status = job.run_test("kvm", params=dict,
                                       tag=dict.get("shortname"),
                                       iterations=test_iterations)
client/tests/kvm/kvm_address_pools.cfg.sample new file +56 -0

56 lines not on this page · raw message

Signature · 1 line
1.5.4.1
#5 lore raw

PATCH v2 5/11 KVM test: specify installation cdkeys in a dedicated file

- Add file kvm_cdkeys.cfg.sample which specifies cdkeys for guests that require them. Users should rename the file to kvm_cdkeys.cfg and enter the real cdkeys in the file.

- Remove all cdkey lines from kvm_tests.cfg.sample and add an include statement instead ('include kvm_cdkeys.cfg').

---
 client/tests/kvm/kvm_cdkeys.cfg.sample |   16 ++++++++++++++++
 client/tests/kvm/kvm_tests.cfg.sample  |   10 +++-------
 2 files changed, 19 insertions(+), 7 deletions(-)
 create mode 100644 client/tests/kvm/kvm_cdkeys.cfg.sample
client/tests/kvm/kvm_cdkeys.cfg.sample new file +16 -0
new file mode 100644
index 0000000..60bfe59
@@ -0,0 +1,16 @@
+# Rename this file to kvm_cdkeys.cfg.
+# Replace the 'CDKEY' strings with real cdkeys where necessary.
+# Guests that do not require cdkeys by default are commented out.
+# You may uncomment them if necessary.
+
+RHEL.5.3.i386: cdkey = CDKEY
+RHEL.5.3.x86_64: cdkey = CDKEY
+Win2000: cdkey = CDKEY
+WinXP.32: cdkey = CDKEY
+WinXP.64: cdkey = CDKEY
+Win2003.32: cdkey = CDKEY
+Win2003.64: cdkey = CDKEY
+#WinVista.32: cdkey = CDKEY
+#WinVista.64: cdkey = CDKEY
+#Win2008.32: cdkey = CDKEY
+#Win2008.64: cdkey = CDKEY
client/tests/kvm/kvm_tests.cfg.sample +3 -7
index 9ad115e..255716a 100644
@@ -272,7 +272,6 @@ variants:
                         image_name = rhel5-32
                         install:
                             steps=RHEL-5.3-i386.steps
-                            cdkey=RHEL_INSTALL_NUMBER
                             cdrom=linux/RHEL-5.3-i386-DVD.iso
                             md5sum=371c62851611fd32ead440df6f24a296
                             md5sum_1m=242318dd44152210f6ff6cdda1bfbf51
@@ -282,7 +281,6 @@ variants:
                         image_name = rhel5-64
                         install:
                             steps=RHEL-5.3-x86_64.steps
-                            cdkey=RHEL_INSTALL_NUMBER
                             cdrom=linux/RHEL-5.3-x86_64-DVD.iso
                             md5sum=c5ed6b284410f4d8212cafc78fd7a8c5
                             md5sum_1m=b999f437583098ea5bbd56fb1de1d011
@@ -363,7 +361,6 @@ variants:
                     md5sum = dda6039f3a9173f0f6bfae40f5efdfea
                     md5sum_1m = dd28fba196d366d56fe774bd93df5527
                     user = user
-                    cdkey = WIN_2000_32_CDKEY
                 setup:
                     steps = Win2000-32-setupssh.steps
                     cdrom = windows/setupssh.iso
@@ -376,7 +373,6 @@ variants:
                     md5sum = 743450644b1d9fe97b3cf379e22dceb0
                     md5sum_1m = b473bf75af2d1269fec8958cf0202bfd
                     user = user
-                    cdkey = WIN_XP_32_CDKEY
                 setup:
                     steps = WinXP-32-setupssh.steps
                     cdrom = windows/setupssh.iso
@@ -390,7 +386,6 @@ variants:
                     md5sum = 8d3f007ec9c2060cec8a50ee7d7dc512
                     md5sum_1m = e812363ff427effc512b7801ee70e513
                     user = user
-                    cdkey = WIN_XP_64_CDKEY
 
             - Win2003:
                 image_size = 20G
@@ -406,7 +401,6 @@ variants:
                             md5sum = 03e921e9b4214773c21a39f5c3f42ef7
                             md5sum_1m = 37c2fdec15ac4ec16aa10fdfdb338aa3
                             user = user
-                            cdkey = WIN_2003_32_CDKEY
                         setup:
                             steps = Win2003-32-setupssh.steps
                             cdrom = windows/setupssh.iso
@@ -420,7 +414,6 @@ variants:
                             md5sum = 5703f87c9fd77d28c05ffadd3354dbbd
                             md5sum_1m = 439393c384116aa09e08a0ad047dcea8
                             user = user
-                            cdkey = WIN_2003_64_CDKEY
 
             - WinVista:
                 only install
@@ -624,5 +617,8 @@ variants:
         only rtl8139
 
 
+include kvm_cdkeys.cfg
+
+
 # Choose your test list
 only fc8_quick
Signature · 1 line
1.5.4.1
#6 lore raw

PATCH v2 6/11 KVM test: kvm_config.py: do not fail when including a nonexistent file

Instead of failing, just print a warning. Included files may not always be crucial for tests to run (kvm_cdkeys.cfg for example).

---
 client/tests/kvm/kvm_config.py |   21 +++++++++++----------
 1 files changed, 11 insertions(+), 10 deletions(-)
client/tests/kvm/kvm_config.py +11 -10
index e478a55..7ff7a07 100755
@@ -406,17 +406,18 @@ class config:
                 if self.filename:
                     filename = os.path.join(os.path.dirname(self.filename),
                                             words[1])
-                    if not os.path.exists(filename):
-                        e_msg = "Cannot include %s -- file not found" % filename
-                        raise error.AutotestError(e_msg)
-                    new_file = open(filename, "r")
-                    list = self.parse(new_file, list, restricted)
-                    new_file.close()
-                    if self.debug and not restricted:
-                        self.__debug_print("", "Leaving file %s" % words[1])
+                    if os.path.exists(filename):
+                        new_file = open(filename, "r")
+                        list = self.parse(new_file, list, restricted)
+                        new_file.close()
+                        if self.debug and not restricted:
+                            self.__debug_print("", "Leaving file %s" % words[1])
+                    else:
+                        print ("WARNING: Cannot include %s -- "
+                               "file not found" % filename)
                 else:
-                    e_msg = "Cannot include anything because no file is open"
-                    raise error.AutotestError(e_msg)
+                    print ("WARNING: Cannot include %s because no file is "
+                           "currently open" % words[1])
 
             # Parse multi-line exceptions
             # (the block is parsed for each dict separately)
Signature · 1 line
1.5.4.1
#7 lore raw

PATCH v2 7/11 KVM test: kvm_subprocess: send user specified parameters to kvm_tail callbacks

(A mistake was made in the first version of this patch -- this is a corrected version.)

Allow the user to specify parameters to send to output_func and termination_func in addition to the regular output string and exit status. The user specified parameters will be pre-pended to the other parameters. This is mainly meant to bypass Python's refusal to pickle instance methods.

---
 client/tests/kvm/kvm_subprocess.py |   60 +++++++++++++++++++++++++++++++----
 1 files changed, 53 insertions(+), 7 deletions(-)
client/tests/kvm/kvm_subprocess.py +53 -7
index 6362856..41b9d80 100644
@@ -470,7 +470,9 @@ class kvm_tail(kvm_spawn):
     """
 
     def __init__(self, command=None, id=None, echo=False, linesep="\n",
-                 termination_func=None, output_func=None, output_prefix=""):
+                 termination_func=None, termination_params=(),
+                 output_func=None, output_params=(),
+                 output_prefix=""):
         """
         Initialize the class and run command as a child process.
 
@@ -485,10 +487,14 @@ class kvm_tail(kvm_spawn):
                 child process by sendline().
         @param termination_func: Function to call when the process exits.  The
                 function must accept a single exit status parameter.
+        @param termination_params: Parameters to send to termination_func
+                before the exit status.
         @param output_func: Function to call whenever a line of output is
                 available from the STDOUT or STDERR streams of the process.
                 The function must accept a single string parameter.  The string
                 does not include the final newline.
+        @param output_params: Parameters to send to output_func before the
+                output line.
         @param output_prefix: String to prepend to lines sent to output_func.
         """
         # Add a reader and a close hook
@@ -500,7 +506,9 @@ class kvm_tail(kvm_spawn):
 
         # Remember some attributes
         self.termination_func = termination_func
+        self.termination_params = termination_params
         self.output_func = output_func
+        self.output_params = output_params
         self.output_prefix = output_prefix
 
         # Start the thread in the background
@@ -510,7 +518,9 @@ class kvm_tail(kvm_spawn):
 
     def __getinitargs__(self):
         return kvm_spawn.__getinitargs__(self) + (self.termination_func,
+                                                  self.termination_params,
                                                   self.output_func,
+                                                  self.output_params,
                                                   self.output_prefix)
 
 
@@ -524,6 +534,16 @@ class kvm_tail(kvm_spawn):
         self.termination_func = termination_func
 
 
+    def set_termination_params(self, termination_params):
+        """
+        Set the termination_params attribute. See __init__() for details.
+
+        @param termination_params: Parameters to send to termination_func
+                before the exit status.
+        """
+        self.termination_params = termination_params
+
+
     def set_output_func(self, output_func):
         """
         Set the output_func attribute. See __init__() for details.
@@ -534,6 +554,16 @@ class kvm_tail(kvm_spawn):
         self.output_func = output_func
 
 
+    def set_output_params(self, output_params):
+        """
+        Set the output_params attribute. See __init__() for details.
+
+        @param output_params: Parameters to send to output_func before the
+                output line.
+        """
+        self.output_params = output_params
+
+
     def set_output_prefix(self, output_prefix):
         """
         Set the output_prefix attribute. See __init__() for details.
@@ -552,7 +582,8 @@ class kvm_tail(kvm_spawn):
             text = text.decode("utf-8", "replace")
             # Pass it to output_func
             try:
-                self.output_func(text)
+                params = self.output_params + (text,)
+                self.output_func(*params)
             except TypeError:
                 pass
 
@@ -593,7 +624,8 @@ class kvm_tail(kvm_spawn):
             return
         print_line("(Process terminated with status %s)" % status)
         try:
-            self.termination_func(status)
+            params = self.termination_params + (status,)
+            self.termination_func(*params)
         except TypeError:
             pass
 
@@ -613,7 +645,9 @@ class kvm_expect(kvm_tail):
     """
 
     def __init__(self, command=None, id=None, echo=False, linesep="\n",
-                 termination_func=None, output_func=None, output_prefix=""):
+                 termination_func=None, termination_params=(),
+                 output_func=None, output_params=(),
+                 output_prefix=""):
         """
         Initialize the class and run command as a child process.
 
@@ -628,10 +662,14 @@ class kvm_expect(kvm_tail):
                 child process by sendline().
         @param termination_func: Function to call when the process exits.  The
                 function must accept a single exit status parameter.
+        @param termination_params: Parameters to send to termination_func
+                before the exit status.
         @param output_func: Function to call whenever a line of output is
                 available from the STDOUT or STDERR streams of the process.
                 The function must accept a single string parameter.  The string
                 does not include the final newline.
+        @param output_params: Parameters to send to output_func before the
+                output line.
         @param output_prefix: String to prepend to lines sent to output_func.
         """
         # Add a reader
@@ -639,7 +677,8 @@ class kvm_expect(kvm_tail):
 
         # Init the superclass
         kvm_tail.__init__(self, command, id, echo, linesep,
-                          termination_func, output_func, output_prefix)
+                          termination_func, termination_params,
+                          output_func, output_params, output_prefix)
 
 
     def __getinitargs__(self):
@@ -813,7 +852,9 @@ class kvm_shell_session(kvm_expect):
     """
 
     def __init__(self, command=None, id=None, echo=False, linesep="\n",
-                 termination_func=None, output_func=None, output_prefix="",
+                 termination_func=None, termination_params=(),
+                 output_func=None, output_params=(),
+                 output_prefix="",
                  prompt=r"[\#\$]\s*$", status_test_command="echo $?"):
         """
         Initialize the class and run command as a child process.
@@ -829,10 +870,14 @@ class kvm_shell_session(kvm_expect):
                 child process by sendline().
         @param termination_func: Function to call when the process exits.  The
                 function must accept a single exit status parameter.
+        @param termination_params: Parameters to send to termination_func
+                before the exit status.
         @param output_func: Function to call whenever a line of output is
                 available from the STDOUT or STDERR streams of the process.
                 The function must accept a single string parameter.  The string
                 does not include the final newline.
+        @param output_params: Parameters to send to output_func before the
+                output line.
         @param output_prefix: String to prepend to lines sent to output_func.
         @param prompt: Regular expression describing the shell's prompt line.
         @param status_test_command: Command to be used for getting the last
@@ -841,7 +886,8 @@ class kvm_shell_session(kvm_expect):
         """
         # Init the superclass
         kvm_expect.__init__(self, command, id, echo, linesep,
-                            termination_func, output_func, output_prefix)
+                            termination_func, termination_params,
+                            output_func, output_params, output_prefix)
 
         # Remember some attributes
         self.prompt = prompt
Signature · 1 line
1.5.4.1
#8 lore raw

PATCH v2 8/11 KVM test: dynamically detect and cache MAC-IP pairs using tcpdump

Run tcpdump in the background at all times and listen for DHCP communication. Whenever a DHCP server assigns an IP address to a MAC address, cache it for future reference.

---
 client/tests/kvm/kvm_preprocessing.py |   41 +++++++++++++++++++++++++++++++++
 1 files changed, 41 insertions(+), 0 deletions(-)
client/tests/kvm/kvm_preprocessing.py +41 -0
index 22c2b8b..20b8cf6 100644
@@ -196,6 +196,26 @@ def preprocess(test, params, env):
     @param params: A dict containing all VM and image parameters.
     @param env: The environment (a dict-like object).
     """
+    # Start tcpdump if it isn't already running
+    if not env.has_key("address_cache"):
+        env["address_cache"] = {}
+    if env.has_key("tcpdump") and not env["tcpdump"].is_alive():
+        env["tcpdump"].close()
+        del env["tcpdump"]
+    if not env.has_key("tcpdump"):
+        command = "/usr/sbin/tcpdump -npvi any 'dst port 68'"
+        logging.debug("Starting tcpdump (%s)...", command)
+        env["tcpdump"] = kvm_subprocess.kvm_tail(
+            command=command,
+            output_func=_update_address_cache,
+            output_params=(env["address_cache"],))
+        if kvm_utils.wait_for(lambda: not env["tcpdump"].is_alive(),
+                              0.1, 0.1, 1.0):
+            logging.warn("Could not start tcpdump")
+            logging.warn("Status: %s" % env["tcpdump"].get_status())
+            logging.warn("Output:" + kvm_utils.format_str_for_message(
+                env["tcpdump"].get_output()))
+
     # Destroy and remove VMs that are no longer needed in the environment
     requested_vms = kvm_utils.get_sub_dict_names(params, "vms")
     for key in env.keys():
@@ -280,6 +300,12 @@ def postprocess(test, params, env):
                         int(params.get("post_command_timeout", "600")),
                         params.get("post_command_noncritical") == "yes")
 
+    # Terminate tcpdump if no VMs are alive
+    living_vms = [vm for vm in kvm_utils.env_get_all_vms(env) if vm.is_alive()]
+    if not living_vms and env.has_key("tcpdump"):
+        env["tcpdump"].close()
+        del env["tcpdump"]
+
 
 def postprocess_on_error(test, params, env):
     """
@@ -290,3 +316,18 @@ def postprocess_on_error(test, params, env):
     @param env: The environment (a dict-like object).
     """
     params.update(kvm_utils.get_sub_dict(params, "on_error"))
+
+
+def _update_address_cache(address_cache, line):
+    if re.search("Your.IP", line, re.IGNORECASE):
+        matches = re.findall(r"\d*\.\d*\.\d*\.\d*", line)
+        if matches:
+            address_cache["last_seen"] = matches[0]
+    if re.search("Client.Ethernet.Address", line, re.IGNORECASE):
+        matches = re.findall(r"\w*:\w*:\w*:\w*:\w*:\w*", line)
+        if matches and address_cache.get("last_seen"):
+            mac_address = matches[0].lower()
+            logging.debug("(address cache) Adding cache entry: %s ---> %s",
+                          mac_address, address_cache.get("last_seen"))
+            address_cache[mac_address] = address_cache.get("last_seen")
+            del address_cache["last_seen"]
Signature · 1 line
1.5.4.1
#9 lore raw

PATCH v2 9/11 KVM test: kvm_utils.py: small fix for format_str_for_message()

format_str_for_message() sometimes adds an extra newline. Fix that.

---
 client/tests/kvm/kvm_utils.py |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
client/tests/kvm/kvm_utils.py +3 -1
index 4891592..ef257bc 100644
@@ -654,7 +654,9 @@ def format_str_for_message(str):
 
     @param str: string that will be formatted.
     """
-    num_lines = len(str.splitlines())
+    lines = str.splitlines()
+    num_lines = len(lines)
+    str = "\n".join(lines)
     if num_lines == 0:
         return ""
     elif num_lines == 1:
Signature · 1 line
1.5.4.1
#10 lore raw

PATCH v2 10/11 KVM test: make VMs use a dynamic MAC-IP mapping generated by tcpdump

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'.

---
 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(-)
client/tests/kvm/kvm_address_pools.cfg.sample +8 -0
index 8a27ee1..debfe56 100644
@@ -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
client/tests/kvm/kvm_preprocessing.py +2 -1
index 20b8cf6..347108f 100644
@@ -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
client/tests/kvm/kvm_utils.py +36 -1
index ef257bc..c3e5502 100644
@@ -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):
client/tests/kvm/kvm_vm.py +50 -19

183 lines not on this page · raw message

Signature · 1 line
1.5.4.1
#11 lore raw

PATCH v2 11/11 KVM test: make stress_boot work properly with TAP networking

Take an additional parameter 'clone_address_index_base' which indicates the initial value for 'address_index' for the cloned VMs. This value is incremented after each clone is created. I assume the original VM has a single NIC; otherwise NICs will end up sharing MAC addresses, which is bad.

Also, make a few small corrections:

- Take the params for the clones from the original VM's params, not from the test's params, because the test's params contain information about several VMs, only one of which is the original VM. The original VM's params, on the other hand, describe just a single VM (which we want to clone).

- Change the way kill_vm.* parameters are sent to the postprocessor. (The postprocessor doesn't read params from the VM objects but rather from the test's params dict.)

- Replace 'if get_command_status(...)' with 'if get_command_status(...) != 0'.

- Replace the test command 'ps aux' with 'uname -a'. The silly reason for this is that DSL-4.2.5 doesn't like 'ps aux'. Since 'uname -a' is just as good (AFAIK), use it instead.

---
 client/tests/kvm/kvm_tests.cfg.sample |    7 ++++++-
 client/tests/kvm/kvm_tests.py         |   14 ++++++--------
 2 files changed, 12 insertions(+), 9 deletions(-)
client/tests/kvm/kvm_tests.cfg.sample +6 -1
index 255716a..a8ba6b3 100644
@@ -117,7 +117,12 @@ variants:
     - stress_boot:  install setup
         type = stress_boot
         max_vms = 5    
-        alive_test_cmd = ps aux
+        alive_test_cmd = uname -a
+        clone_address_index_base = 10
+        kill_vm = yes
+        kill_vm_vm1 = no
+        kill_vm_gracefully = no
+        extra_params += " -snapshot"
 
     - shutdown:     install setup
         type = shutdown
client/tests/kvm/kvm_tests.py +6 -8
index 4bc8718..d146929 100644
@@ -541,8 +541,8 @@ def run_stress_boot(tests, params, env):
         raise error.TestFail("Could not log into first guest")
 
     num = 2
-    vms = []
     sessions = [session]
+    address_index = int(params.get("clone_address_index_base", 10))
 
     # boot the VMs
     while num <= int(params.get("max_vms")):
@@ -550,15 +550,12 @@ def run_stress_boot(tests, params, env):
             vm_name = "vm" + str(num)
 
             # clone vm according to the first one
-            vm_params = params.copy()
-            vm_params['image_snapshot'] = "yes"
-            vm_params['kill_vm'] = "yes"
-            vm_params['kill_vm_gracefully'] = "no"
+            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)
             params['vms'] += " " + vm_name
 
-            #vms.append(curr_vm)
             logging.info("Booting guest #%d" % num)
             if not curr_vm.create():
                 raise error.TestFail("Cannot create VM #%d" % num)
@@ -571,10 +568,11 @@ def run_stress_boot(tests, params, env):
             sessions.append(curr_vm_session)
 
             # check whether all previous ssh sessions are responsive
-            for i, vm_session in enumerate(sessions):
-                if vm_session.get_command_status(params.get("alive_test_cmd")):
+            for i, se in enumerate(sessions):
+                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:
Signature · 1 line
1.5.4.1
#12 lore raw
In reply to Michael Goldish
Quoted 15 lines · Michael Goldish

Differences compared to the previous patchset:
- In kvm_utils.verify_ip_address_ownership() and kvm_vm.get_address():
 Require that '/sbin/arp -n' list any of the the VM's MAC addresses, not necessarily the MAC address being verified.
 After having a very hard time deciding which solution was better, I chose this one because I think it is slightly safer, and it is more similar to the code in the original patch set, which I feel rather confident in since it has had some testing.
 The new code has been tested and works with RHEL 3.9 which is one of the problematic guests (with 2 NICs).
 However, it can and should be replaced if it causes any trouble (i.e. if we find out that some of our guests make it impossible to find their MAC addresses this way).

Quoted 4 lines

- User networking mode is the default.  We can make TAP mode the default when it matures a little.

- qemu-ifup is in test.bindir/scripts, instead of test.bindir.

Ok, I have checked them and tried it on my test box. Works OK, the patchset looks good. Applied!

Thanks Michael!