From nobody Wed May 14 06:55:48 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 152691709194151.63359411914246; Mon, 21 May 2018 08:38:11 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C0C68C0C1128; Mon, 21 May 2018 15:38:09 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8EA8128DCC; Mon, 21 May 2018 15:38:09 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 493D41801256; Mon, 21 May 2018 15:38:09 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w4LFbcYZ024137 for ; Mon, 21 May 2018 11:37:38 -0400 Received: by smtp.corp.redhat.com (Postfix) id A228E10EE6D6; Mon, 21 May 2018 15:37:38 +0000 (UTC) Received: from icr.brq.redhat.com (unknown [10.43.2.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 296C010EE6D5; Mon, 21 May 2018 15:37:38 +0000 (UTC) From: =?UTF-8?q?J=C3=A1n=20Tomko?= To: libvir-list@redhat.com Date: Mon, 21 May 2018 17:37:19 +0200 Message-Id: <2fb01f09c7cb526ae35b2da033ebe1110896eb52.1526916651.git.jtomko@redhat.com> In-Reply-To: References: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-loop: libvir-list@redhat.com Cc: stefanha@redhat.com Subject: [libvirt] [PATCH 9/9] qemu: implement vhost-vsock-pci support X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Mon, 21 May 2018 15:38:10 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Create a new vsock endpoint by opening /dev/vhost-vsock, set the requested CID via ioctl, pass the file descriptor to QEMU and build the command line. Signed-off-by: J=C3=A1n Tomko --- src/qemu/qemu_command.c | 37 ++++++++++++++++++= ---- src/qemu/qemu_domain.c | 10 +++++- src/qemu/qemu_domain.h | 6 +++- src/qemu/qemu_interface.c | 31 ++++++++++++++++++ src/qemu/qemu_interface.h | 4 +++ src/qemu/qemu_process.c | 5 +++ .../vhost-vsock.x86_64-latest.args | 32 +++++++++++++++++++ tests/qemuxml2argvtest.c | 15 +++++++++ 8 files changed, 132 insertions(+), 8 deletions(-) create mode 100644 tests/qemuxml2argvdata/vhost-vsock.x86_64-latest.args diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index a9c67bbb3b..2aa47f0980 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -3375,6 +3375,9 @@ qemuBuildNicDevStr(virDomainDefPtr def, bool usingVirtio =3D false; char macaddr[VIR_MAC_STRING_BUFLEN]; =20 + if (net->type =3D=3D VIR_DOMAIN_NET_TYPE_VSOCK) + nic =3D "vhost-vsock-pci"; + if (STREQ_NULLABLE(net->model, "virtio")) { if (net->info.type =3D=3D VIR_DOMAIN_DEVICE_ADDRESS_TYPE_CCW) nic =3D "virtio-net-ccw"; @@ -3514,13 +3517,23 @@ qemuBuildNicDevStr(virDomainDefPtr def, virBufferAsprintf(&buf, ",host_mtu=3D%u", net->mtu); } =20 - if (vlan =3D=3D -1) - virBufferAsprintf(&buf, ",netdev=3Dhost%s", net->info.alias); - else - virBufferAsprintf(&buf, ",vlan=3D%d", vlan); + if (net->type !=3D VIR_DOMAIN_NET_TYPE_VSOCK) { + if (vlan =3D=3D -1) + virBufferAsprintf(&buf, ",netdev=3Dhost%s", net->info.alias); + else + virBufferAsprintf(&buf, ",vlan=3D%d", vlan); + } virBufferAsprintf(&buf, ",id=3D%s", net->info.alias); - virBufferAsprintf(&buf, ",mac=3D%s", - virMacAddrFormat(&net->mac, macaddr)); + if (net->type !=3D VIR_DOMAIN_NET_TYPE_VSOCK) { + virBufferAsprintf(&buf, ",mac=3D%s", + virMacAddrFormat(&net->mac, macaddr)); + } + + if (net->type =3D=3D VIR_DOMAIN_NET_TYPE_VSOCK) { + virBufferAsprintf(&buf, ",guest-cid=3D%u", net->data.vsock.guest_c= id); + virBufferAsprintf(&buf, ",vhostfd=3D%u", + QEMU_DOMAIN_NET_PRIVATE(net)->vhostfds[0]); + } =20 if (qemuBuildDeviceAddressStr(&buf, def, &net->info, qemuCaps) < 0) goto error; @@ -8375,6 +8388,18 @@ qemuBuildInterfaceCommandLine(virQEMUDriverPtr drive= r, goto cleanup; } =20 + if (actualType =3D=3D VIR_DOMAIN_NET_TYPE_VSOCK) { + if (!(nic =3D qemuBuildNicDevStr(def, net, -1, bootindex, 0, qemuC= aps))) + goto cleanup; + + virCommandPassFD(cmd, QEMU_DOMAIN_NET_PRIVATE(net)->vhostfds[0], + VIR_COMMAND_PASS_FD_CLOSE_PARENT); + QEMU_DOMAIN_NET_PRIVATE(net)->vhostfds[0] =3D -1; + virCommandAddArgList(cmd, "-device", nic, NULL); + ret =3D 0; + goto cleanup; + } + /* Possible combinations: * * 1. Old way: -net nic,model=3De1000,vlan=3D1 -net tap,vlan=3D1 diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index eb88de83db..33e3bb340f 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -1164,8 +1164,16 @@ qemuDomainNetPrivateNew(void) =20 =20 static void -qemuDomainNetPrivateDispose(void *obj ATTRIBUTE_UNUSED) +qemuDomainNetPrivateDispose(void *obj) { + qemuDomainNetPrivatePtr priv =3D obj; + size_t i; + + if (priv->vhostfds) { + for (i =3D 0; i < priv->nvhostfds; i++) + VIR_FORCE_CLOSE(priv->vhostfds[i]); + } + VIR_FREE(priv->vhostfds); } =20 =20 diff --git a/src/qemu/qemu_domain.h b/src/qemu/qemu_domain.h index a04ded16ed..f1034a57bf 100644 --- a/src/qemu/qemu_domain.h +++ b/src/qemu/qemu_domain.h @@ -353,6 +353,9 @@ struct _qemuDomainObjPrivate { # define QEMU_DOMAIN_DISK_PRIVATE(disk) \ ((qemuDomainDiskPrivatePtr) (disk)->privateData) =20 +# define QEMU_DOMAIN_NET_PRIVATE(net) \ + ((qemuDomainNetPrivatePtr) (net)->privateData) + typedef struct _qemuDomainDiskPrivate qemuDomainDiskPrivate; typedef qemuDomainDiskPrivate *qemuDomainDiskPrivatePtr; struct _qemuDomainDiskPrivate { @@ -446,7 +449,8 @@ typedef qemuDomainNetPrivate *qemuDomainNetPrivatePtr; struct _qemuDomainNetPrivate { virObject parent; =20 - virTristateBool maybe; + int *vhostfds; + size_t nvhostfds; }; =20 =20 diff --git a/src/qemu/qemu_interface.c b/src/qemu/qemu_interface.c index 60bb6b22ef..fb38ee0d67 100644 --- a/src/qemu/qemu_interface.c +++ b/src/qemu/qemu_interface.c @@ -698,3 +698,34 @@ qemuInterfaceOpenVhostNet(virDomainDefPtr def, =20 return -1; } + + +int +qemuInterfaceOpenVhostVsock(virDomainDefPtr def, + virDomainNetDefPtr net) +{ + qemuDomainNetPrivatePtr netPriv =3D QEMU_DOMAIN_NET_PRIVATE(net); + const char *vsock_path =3D "/dev/vhost-vsock"; + int fd; + + if (VIR_ALLOC_N(netPriv->vhostfds, 1) < 0) + return -1; + + if ((fd =3D open(vsock_path, O_RDWR)) < 0) { + virDomainAuditNetDevice(def, net, vsock_path, false); + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + "%s", _("unable to open vhost-vsock device")); + return -1; + } + if (virNetDevVsockSetGuestCid(fd, net->data.vsock.guest_cid) < 0) + goto error; + + virDomainAuditNetDevice(def, net, vsock_path, true); + netPriv->vhostfds[0] =3D fd; + netPriv->nvhostfds =3D 1; + return 0; + + error: + VIR_FORCE_CLOSE(fd); + return -1; +} diff --git a/src/qemu/qemu_interface.h b/src/qemu/qemu_interface.h index 45fdbc4b8e..b327a35f99 100644 --- a/src/qemu/qemu_interface.h +++ b/src/qemu/qemu_interface.h @@ -58,4 +58,8 @@ int qemuInterfaceOpenVhostNet(virDomainDefPtr def, virDomainNetDefPtr net, int *vhostfd, size_t *vhostfdSize); + +int +qemuInterfaceOpenVhostVsock(virDomainDefPtr def, + virDomainNetDefPtr net); #endif /* __QEMU_INTERFACE_H__ */ diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index d73f3764f5..0cac864b60 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -5348,6 +5348,11 @@ qemuProcessNetworkPrepareDevices(virDomainDefPtr def) if (virDomainHostdevInsert(def, hostdev) < 0) goto cleanup; } + + if (net->type =3D=3D VIR_DOMAIN_NET_TYPE_VSOCK) { + if (qemuInterfaceOpenVhostVsock(def, net) < 0) + goto cleanup; + } } ret =3D 0; cleanup: diff --git a/tests/qemuxml2argvdata/vhost-vsock.x86_64-latest.args b/tests/= qemuxml2argvdata/vhost-vsock.x86_64-latest.args new file mode 100644 index 0000000000..266144f6f0 --- /dev/null +++ b/tests/qemuxml2argvdata/vhost-vsock.x86_64-latest.args @@ -0,0 +1,32 @@ +LC_ALL=3DC \ +PATH=3D/bin \ +HOME=3D/home/test \ +USER=3Dtest \ +LOGNAME=3Dtest \ +QEMU_AUDIO_DRV=3Dnone \ +/usr/bin/qemu-system-x86_64 \ +-name guest=3Dtest,debug-threads=3Don \ +-S \ +-object secret,id=3DmasterKey0,format=3Draw,\ +file=3D/tmp/lib/domain--1-test/master-key.aes \ +-machine pc-0.13,accel=3Dtcg,usb=3Doff,dump-guest-core=3Doff \ +-m 1024 \ +-realtime mlock=3Doff \ +-smp 1,sockets=3D1,cores=3D1,threads=3D1 \ +-uuid bba65c0e-c049-934f-b6aa-4e2c0582acdf \ +-display none \ +-no-user-config \ +-nodefaults \ +-chardev socket,id=3Dcharmonitor,path=3D/tmp/lib/domain--1-test/monitor.so= ck,\ +server,nowait \ +-mon chardev=3Dcharmonitor,id=3Dmonitor,mode=3Dcontrol \ +-rtc base=3Dutc \ +-no-shutdown \ +-no-acpi \ +-boot menu=3Don,strict=3Don \ +-device piix3-usb-uhci,id=3Dusb,bus=3Dpci.0,addr=3D0x1.0x2 \ +-device virtio-serial-pci,id=3Dvirtio-serial0,bus=3Dpci.0,addr=3D0x6 \ +-device vhost-vsock-pci,id=3Dnet0,guest-cid=3D4,vhostfd=3D6789,bus=3Dpci.0= ,addr=3D0x3 \ +-sandbox on,obsolete=3Ddeny,elevateprivileges=3Ddeny,spawn=3Ddeny,\ +resourcecontrol=3Ddeny \ +-msg timestamp=3Don diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c index 78454acb1a..761a2628c8 100644 --- a/tests/qemuxml2argvtest.c +++ b/tests/qemuxml2argvtest.c @@ -532,6 +532,19 @@ testCompareXMLToArgv(const void *data) } } =20 + for (i =3D 0; i < vm->def->nnets; i++) { + virDomainNetDefPtr net =3D vm->def->nets[i]; + + if (net->type =3D=3D VIR_DOMAIN_NET_TYPE_VSOCK) { + qemuDomainNetPrivatePtr netPriv =3D QEMU_DOMAIN_NET_PRIVATE(ne= t); + if (VIR_ALLOC_N(netPriv->vhostfds, 1) < 0) + goto cleanup; + + netPriv->vhostfds[0] =3D 6789; + netPriv->nvhostfds =3D 1; + } + } + if (!(cmd =3D qemuProcessCreatePretendCmd(&driver, vm, migrateURI, (flags & FLAG_FIPS), false, VIR_QEMU_PROCESS_START_COLD)))= { @@ -2848,6 +2861,8 @@ mymain(void) QEMU_CAPS_DEVICE_VIRTIO_MOUSE_CCW, QEMU_CAPS_DEVICE_VIRTIO_TABLET_CCW); =20 + DO_TEST_CAPS_LATEST("vhost-vsock"); + if (getenv("LIBVIRT_SKIP_CLEANUP") =3D=3D NULL) virFileDeleteTree(fakerootdir); =20 --=20 2.16.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list