Skip to content

Commit

Permalink
qemu_vm: add vsock device
Browse files Browse the repository at this point in the history
1. The cmd line: -device vhost-vsock-pci,id=vhost-vsock0,guest-cid=3
2. guest-cid is between '3' and '0xffffffff - 1'(unsigned 32-bit
 integer. 0-2 and 0xffffffff are reserved)
3. It is a virtio device so should be a pcie device

Signed-off-by: Qianqian Zhu <[email protected]>
  • Loading branch information
vivianQizhu committed Feb 26, 2019
1 parent 75fef6b commit 5cf1d84
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
3 changes: 3 additions & 0 deletions shared/cfg/base.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions virttest/arch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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():
Expand Down
2 changes: 1 addition & 1 deletion virttest/qemu_devices/qdevices.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
43 changes: 43 additions & 0 deletions virttest/qemu_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@
import os
import logging
import fcntl
import struct
import re
import random
import errno

from functools import partial

import aexpect
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
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 5cf1d84

Please sign in to comment.