CSIT-514: Kill Qemu when it does not respond
[csit.git] / resources / libraries / python / QemuUtils.py
index ea1d56a..a4e14d2 100644 (file)
@@ -18,7 +18,7 @@ import json
 
 from robot.api import logger
 
-from resources.libraries.python.ssh import SSH
+from resources.libraries.python.ssh import SSH, SSHTimeout
 from resources.libraries.python.constants import Constants
 from resources.libraries.python.topology import NodeType
 
@@ -39,7 +39,7 @@ class QemuUtils(object):
         # Daemonize the QEMU process after initialization. Default one
         # management interface.
         self._qemu_opt['options'] = '-cpu host -daemonize -enable-kvm ' \
-            '-machine pc-1.0,accel=kvm,usb=off,mem-merge=off ' \
+            '-machine pc,accel=kvm,usb=off,mem-merge=off ' \
             '-net nic,macaddr=52:54:00:00:02:01 -balloon none'
         self._qemu_opt['ssh_fwd_port'] = 10022
         # Default serial console port
@@ -60,6 +60,8 @@ class QemuUtils(object):
             'password': 'cisco',
             'interfaces': {},
         }
+        # Virtio queue count
+        self._qemu_opt['queues'] = 1
         self._vhost_id = 0
         self._ssh = None
         self._node = None
@@ -140,8 +142,7 @@ class QemuUtils(object):
             raise ValueError('Host CPU count must match Qemu Thread count')
 
         for qemu_cpu, host_cpu in zip(qemu_cpus, host_cpus):
-            cmd = 'taskset -p {0} {1}'.format(hex(1 << int(host_cpu)),
-                                              qemu_cpu['thread_id'])
+            cmd = 'taskset -pc {0} {1}'.format(host_cpu, qemu_cpu['thread_id'])
             (ret_code, _, stderr) = self._ssh.exec_command_sudo(cmd)
             if int(ret_code) != 0:
                 logger.debug('Set affinity failed {0}'.format(stderr))
@@ -194,15 +195,15 @@ class QemuUtils(object):
             chardev += ',server'
         self._qemu_opt['options'] += chardev
         # Create Vhost-user network backend.
-        netdev = ' -netdev vhost-user,id=vhost{0},chardev=char{0}'.format(
-            self._vhost_id)
+        netdev = ' -netdev vhost-user,id=vhost{0},chardev=char{0},'\
+            'queues={1}'.format(self._vhost_id, self._qemu_opt['queues'])
         self._qemu_opt['options'] += netdev
         # If MAC is not specified use autogenerated 52:54:00:00:04:<vhost_id>
         # e.g. vhost1 MAC is 52:54:00:00:04:01
         if mac is None:
             mac = '52:54:00:00:04:{0:02x}'.format(self._vhost_id)
-        extend_options = 'csum=off,gso=off,guest_tso4=off,guest_tso6=off,'\
-            'guest_ecn=off,mrg_rxbuf=off'
+        extend_options = 'mq=on,csum=off,gso=off,guest_tso4=off,'\
+            'guest_tso6=off,guest_ecn=off,mrg_rxbuf=off'
         # Create Virtio network device.
         device = ' -device virtio-net-pci,netdev=vhost{0},mac={1},{2}'.format(
             self._vhost_id, mac, extend_options)
@@ -278,21 +279,24 @@ class QemuUtils(object):
             return {}
         return json.loads(stdout.split('\n', 1)[0])
 
-    def _wait_until_vm_boot(self, timeout=300):
+    def _wait_until_vm_boot(self, timeout=60):
         """Wait until QEMU VM is booted.
 
         Ping QEMU guest agent each 5s until VM booted or timeout.
 
-        :param timeout: Waiting timeout in seconds (optional, default 300s).
+        :param timeout: Waiting timeout in seconds (optional, default 60s).
         :type timeout: int
         """
         start = time()
-        while 1:
+        while True:
             if time() - start > timeout:
                 raise RuntimeError('timeout, VM {0} not booted on {1}'.format(
                     self._qemu_opt['disk_image'], self._node['host']))
-            self._qemu_qga_flush()
-            out = self._qemu_qga_exec('guest-ping')
+            try:
+                self._qemu_qga_flush()
+                out = self._qemu_qga_exec('guest-ping')
+            except ValueError:
+                logger.trace('QGA guest-ping unexpected output {}'.format(out))
             # Empty output - VM not booted yet
             if not out:
                 sleep(5)
@@ -303,8 +307,10 @@ class QemuUtils(object):
             elif out.get('error') is not None:
                 sleep(5)
             else:
-                raise RuntimeError('QGA guest-ping unexpected output {}'.format(
-                    out))
+                # If there is an unexpected output from QGA guest-info, try
+                # again until timeout.
+                logger.trace('QGA guest-ping unexpected output {}'.format(out))
+
         logger.trace('VM {0} booted on {1}'.format(self._qemu_opt['disk_image'],
                                                    self._node['host']))
 
@@ -511,7 +517,12 @@ class QemuUtils(object):
                 self._node['host']))
         logger.trace('QEMU running')
         # Wait until VM boot
-        self._wait_until_vm_boot()
+        try:
+            self._wait_until_vm_boot()
+        except (RuntimeError, SSHTimeout):
+            self.qemu_kill()
+            self.qemu_clear_socks()
+            raise
         # Update interface names in VM node dict
         self._update_vm_interfaces()
         # Return VM node dict