diff --git a/shared/cfg/base.cfg b/shared/cfg/base.cfg index 87578f40b5..79bb80117a 100644 --- a/shared/cfg/base.cfg +++ b/shared/cfg/base.cfg @@ -694,3 +694,6 @@ inputs = "" # systems that require this extra setting. Host_Ubuntu.m18.u10: firewalld_dhcp_workaround = yes + +# Add vsock device +# vsocks = vhost_vsock0 diff --git a/virttest/arch.py b/virttest/arch.py index 844e821969..6cfb92dcc6 100644 --- a/virttest/arch.py +++ b/virttest/arch.py @@ -38,6 +38,8 @@ NLMSG_ERROR = 2 # From linux/socket.h AF_PACKET = 17 + # From linux/vhost.h + VHOST_VSOCK_SET_GUEST_CID = 0x8008af60 else: # From include/linux/sockios.h SIOCSIFHWADDR = 0x8924 @@ -72,6 +74,8 @@ NLMSG_ERROR = 2 # From linux/socket.h AF_PACKET = 17 + # From linux/vhost.h + VHOST_VSOCK_SET_GUEST_CID = 0x4008af60 def get_kvm_module_list(): diff --git a/virttest/qemu_devices/qdevices.py b/virttest/qemu_devices/qdevices.py index 070a424d51..7c4669c415 100644 --- a/virttest/qemu_devices/qdevices.py +++ b/virttest/qemu_devices/qdevices.py @@ -1839,7 +1839,7 @@ def is_direct_plug(self, device): :param device: the QBaseDevice object :return: bool value for directly plug or not. """ - pcie_devices = ["virtio-net-pci", "virtio-blk-pci", + pcie_devices = ["virtio-net-pci", "virtio-blk-pci", "vhost-vsock-pci" "virtio-scsi-pci", "virtio-balloon-pci", "virtio-serial-pci", "virtio-rng-pci", "e1000e", "virtio-gpu-device", "qemu-xhci"] diff --git a/virttest/qemu_vm.py b/virttest/qemu_vm.py index 612e949fa2..cccbcbde7a 100644 --- a/virttest/qemu_vm.py +++ b/virttest/qemu_vm.py @@ -9,8 +9,10 @@ import os import logging import fcntl +import struct import re import random +import errno from functools import partial @@ -18,6 +20,7 @@ from avocado.core import exceptions from avocado.utils import process from avocado.utils import crypto +from avocado.utils import linux_modules import six from six.moves import xrange @@ -1897,6 +1900,46 @@ def sort_key(dev): pci_bus=pci_bus) iov += 1 + def get_cid(cid): + """ Get an unused guest cid from system """ + while cid: + cid_c = struct.pack('L', cid) + try: + fcntl.ioctl( + vsock_fd, arch.VHOST_VSOCK_SET_GUEST_CID, cid_c) + except IOError as e: + if e.errno == errno.EADDRINUSE: + cid += 1 + continue + else: + raise e + else: + return cid + + # Add vsock device, cid 0-2 are reserved by system + vsocks = params.objects('vsocks') + if vsocks: + vsock_path = "/dev/vhost-vsock" + if not os.path.exists(vsock_path): + logging.info("vsock module was not loaded, loading it...") + if not linux_modules.load_module('vhost_vsock'): + raise exceptions.TestError( + "Failed on loading module vhost_vsock.") + vsock_fd = os.open(vsock_path, os.O_RDWR) + min_cid = 3 + for vsock in vsocks: + guest_cid = get_cid(min_cid) + vsock_params = {"id": vsock, "guest-cid": guest_cid} + if '-mmio:' in params.get('machine_type'): + dev_vsock = QDevice('vhost-vsock-device', vsock_params) + elif params.get('machine_type').startswith("s390"): + dev_vsock = QDevice("vhost-vsock-ccw", vsock_params) + else: + dev_vsock = QDevice('vhost-vsock-pci', vsock_params) + devices.insert(dev_vsock) + min_cid = guest_cid + 1 + os.close(vsock_fd) + # Add Memory devices add_memorys(devices, params) smp = int(params.get("smp", 0))