From a14abd463a97b1d5790793fafaedec096372d5e4 Mon Sep 17 00:00:00 2001 From: Michele Paolino Date: Fri, 11 Jul 2014 19:47:31 +0200 Subject: [PATCH] support for QEMU vhost-user This patch adds support for the QEMU vhost-user feature to libvirt. vhost-user enables the communication between a QEMU virtual machine and other userspace process using the Virtio transport protocol. It uses a char dev (e.g. Unix socket) for the control plane, while the data plane based on shared memory. The XML looks like: Signed-off-by: Michele Paolino Signed-off-by: Michal Privoznik --- docs/formatdomain.html.in | 31 +++++++ docs/schemas/domaincommon.rng | 25 ++++++ src/conf/domain_conf.c | 87 +++++++++++++++++++ src/conf/domain_conf.h | 10 ++- src/libxl/libxl_conf.c | 1 + src/lxc/lxc_process.c | 1 + src/qemu/qemu_command.c | 73 ++++++++++++++++ src/uml/uml_conf.c | 5 ++ src/xenxs/xen_sxpr.c | 1 + .../qemuxml2argv-net-vhostuser.args | 7 ++ .../qemuxml2argv-net-vhostuser.xml | 33 +++++++ tests/qemuxml2argvtest.c | 1 + tests/qemuxml2xmltest.c | 1 + 13 files changed, 274 insertions(+), 2 deletions(-) create mode 100644 tests/qemuxml2argvdata/qemuxml2argv-net-vhostuser.args create mode 100644 tests/qemuxml2argvdata/qemuxml2argv-net-vhostuser.xml diff --git a/docs/formatdomain.html.in b/docs/formatdomain.html.in index b69da4c657..27465bb618 100644 --- a/docs/formatdomain.html.in +++ b/docs/formatdomain.html.in @@ -3944,6 +3944,37 @@ qemu-kvm -net nic,model=? /dev/null Since 0.9.5

+
vhost-user interface
+ +

+ Since 1.2.7 the vhost-user enables the + communication between a QEMU virtual machine and other userspace process + using the Virtio transport protocol. A char dev (e.g. Unix socket) is used + for the control plane, while the data plane is based on shared memory. +

+ +
+  ...
+  <devices>
+    <interface type='vhostuser'>
+      <mac address='52:54:00:3b:83:1a'/>
+      <source type='unix' path='/tmp/vhost.sock' mode='server'/>
+      <model type='virtio'/>
+    </interface>
+  </devices>
+  ...
+ +

+ The <source> element has to be specified + along with the type of char device. + Currently, only type='unix' is supported, where the path (the + directory path of the socket) and mode attributes are required. + Both mode='server' and mode='client' + are supported. + vhost-user requires the virtio model type, thus the + <model> element is mandatory. +

+

Input devices

diff --git a/docs/schemas/domaincommon.rng b/docs/schemas/domaincommon.rng index 7be028d160..cfd8629cd2 100644 --- a/docs/schemas/domaincommon.rng +++ b/docs/schemas/domaincommon.rng @@ -1976,6 +1976,31 @@ + + + vhostuser + + + + + + unix + + + + + + + + server + client + + + + + + + network diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 1d83f13853..ad4549c70b 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -348,6 +348,7 @@ VIR_ENUM_IMPL(virDomainFSWrpolicy, VIR_DOMAIN_FS_WRPOLICY_LAST, VIR_ENUM_IMPL(virDomainNet, VIR_DOMAIN_NET_TYPE_LAST, "user", "ethernet", + "vhostuser", "server", "client", "mcast", @@ -1346,6 +1347,10 @@ void virDomainNetDefFree(virDomainNetDefPtr def) VIR_FREE(def->data.ethernet.ipaddr); break; + case VIR_DOMAIN_NET_TYPE_VHOSTUSER: + virDomainChrSourceDefFree(def->data.vhostuser); + break; + case VIR_DOMAIN_NET_TYPE_SERVER: case VIR_DOMAIN_NET_TYPE_CLIENT: case VIR_DOMAIN_NET_TYPE_MCAST: @@ -6618,6 +6623,9 @@ virDomainNetDefParseXML(virDomainXMLOptionPtr xmlopt, char *mode = NULL; char *linkstate = NULL; char *addrtype = NULL; + char *vhostuser_mode = NULL; + char *vhostuser_path = NULL; + char *vhostuser_type = NULL; virNWFilterHashTablePtr filterparams = NULL; virDomainActualNetDefPtr actual = NULL; xmlNodePtr oldnode = ctxt->node; @@ -6663,6 +6671,12 @@ virDomainNetDefParseXML(virDomainXMLOptionPtr xmlopt, xmlStrEqual(cur->name, BAD_CAST "source")) { dev = virXMLPropString(cur, "dev"); mode = virXMLPropString(cur, "mode"); + } else if (!vhostuser_path && !vhostuser_mode && !vhostuser_type + && def->type == VIR_DOMAIN_NET_TYPE_VHOSTUSER && + xmlStrEqual(cur->name, BAD_CAST "source")) { + vhostuser_type = virXMLPropString(cur, "type"); + vhostuser_path = virXMLPropString(cur, "path"); + vhostuser_mode = virXMLPropString(cur, "mode"); } else if (!def->virtPortProfile && xmlStrEqual(cur->name, BAD_CAST "virtualport")) { if (def->type == VIR_DOMAIN_NET_TYPE_NETWORK) { @@ -6820,6 +6834,65 @@ virDomainNetDefParseXML(virDomainXMLOptionPtr xmlopt, actual = NULL; break; + case VIR_DOMAIN_NET_TYPE_VHOSTUSER: + if (STRNEQ_NULLABLE(model, "virtio")) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Wrong or no 'type' attribute " + "specified with . " + "vhostuser requires the virtio-net* frontend")); + goto error; + } + + if (STRNEQ_NULLABLE(vhostuser_type, "unix")) { + if (vhostuser_type) + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("Type='%s' unsupported for" + " "), + vhostuser_type); + else + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("No 'type' attribute " + "specified for ")); + goto error; + } + + if (vhostuser_path == NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("No 'path' attribute " + "specified with ")); + goto error; + } + + if (vhostuser_mode == NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("No 'mode' attribute " + "specified with ")); + goto error; + } + + if (VIR_ALLOC(def->data.vhostuser) < 0) + goto error; + + def->data.vhostuser->type = VIR_DOMAIN_CHR_TYPE_UNIX; + def->data.vhostuser->data.nix.path = vhostuser_path; + vhostuser_path = NULL; + + if (STREQ(vhostuser_mode, "server")) + def->data.vhostuser->data.nix.listen = true; + else if (STREQ(vhostuser_mode, "client")) + def->data.vhostuser->data.nix.listen = false; + else { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("Wrong 'mode' attribute " + "specified with ")); + goto error; + } + break; + case VIR_DOMAIN_NET_TYPE_ETHERNET: if (dev != NULL) { def->data.ethernet.dev = dev; @@ -7065,6 +7138,9 @@ virDomainNetDefParseXML(virDomainXMLOptionPtr xmlopt, VIR_FREE(portgroup); VIR_FREE(address); VIR_FREE(port); + VIR_FREE(vhostuser_type); + VIR_FREE(vhostuser_path); + VIR_FREE(vhostuser_mode); VIR_FREE(ifname); VIR_FREE(dev); virDomainActualNetDefFree(actual); @@ -15816,6 +15892,17 @@ virDomainNetDefFormat(virBufferPtr buf, def->data.ethernet.ipaddr); break; + case VIR_DOMAIN_NET_TYPE_VHOSTUSER: + if (def->data.vhostuser->type == VIR_DOMAIN_CHR_TYPE_UNIX) { + virBufferAddLit(buf, "data.vhostuser->data.nix.path); + if (def->data.vhostuser->data.nix.listen) + virBufferAddLit(buf, " mode='server'"); + virBufferAddLit(buf, "/>\n"); + } + break; + case VIR_DOMAIN_NET_TYPE_BRIDGE: virBufferEscapeString(buf, "\n", def->data.bridge.brname); diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index 32674e06c0..037db424b6 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -130,6 +130,12 @@ typedef virDomainIdMapDef *virDomainIdMapDefPtr; typedef struct _virDomainPanicDef virDomainPanicDef; typedef virDomainPanicDef *virDomainPanicDefPtr; +/* forward declarations virDomainChrSourceDef, required by + * virDomainNetDef + */ +typedef struct _virDomainChrSourceDef virDomainChrSourceDef; +typedef virDomainChrSourceDef *virDomainChrSourceDefPtr; + /* Flags for the 'type' field in virDomainDeviceDef */ typedef enum { VIR_DOMAIN_DEVICE_NONE = 0, @@ -790,6 +796,7 @@ struct _virDomainFSDef { typedef enum { VIR_DOMAIN_NET_TYPE_USER, VIR_DOMAIN_NET_TYPE_ETHERNET, + VIR_DOMAIN_NET_TYPE_VHOSTUSER, VIR_DOMAIN_NET_TYPE_SERVER, VIR_DOMAIN_NET_TYPE_CLIENT, VIR_DOMAIN_NET_TYPE_MCAST, @@ -875,6 +882,7 @@ struct _virDomainNetDef { char *dev; char *ipaddr; } ethernet; + virDomainChrSourceDefPtr vhostuser; struct { char *address; int port; @@ -1001,8 +1009,6 @@ typedef enum { } virDomainChrSpicevmcName; /* The host side information for a character device. */ -typedef struct _virDomainChrSourceDef virDomainChrSourceDef; -typedef virDomainChrSourceDef *virDomainChrSourceDefPtr; struct _virDomainChrSourceDef { int type; /* virDomainChrType */ union { diff --git a/src/libxl/libxl_conf.c b/src/libxl/libxl_conf.c index a39dc4f3d9..488ce2aa9b 100644 --- a/src/libxl/libxl_conf.c +++ b/src/libxl/libxl_conf.c @@ -986,6 +986,7 @@ libxlMakeNic(virDomainDefPtr def, return -1; break; } + case VIR_DOMAIN_NET_TYPE_VHOSTUSER: case VIR_DOMAIN_NET_TYPE_USER: case VIR_DOMAIN_NET_TYPE_SERVER: case VIR_DOMAIN_NET_TYPE_CLIENT: diff --git a/src/lxc/lxc_process.c b/src/lxc/lxc_process.c index 0aef13a0bd..854f65d89c 100644 --- a/src/lxc/lxc_process.c +++ b/src/lxc/lxc_process.c @@ -437,6 +437,7 @@ static int virLXCProcessSetupInterfaces(virConnectPtr conn, case VIR_DOMAIN_NET_TYPE_USER: case VIR_DOMAIN_NET_TYPE_ETHERNET: + case VIR_DOMAIN_NET_TYPE_VHOSTUSER: case VIR_DOMAIN_NET_TYPE_SERVER: case VIR_DOMAIN_NET_TYPE_CLIENT: case VIR_DOMAIN_NET_TYPE_MCAST: diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index 2185ef485c..c0a5a13a5e 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -6848,6 +6848,76 @@ qemuBuildGraphicsCommandLine(virQEMUDriverConfigPtr cfg, return 0; } +static int +qemuBuildVhostuserCommandLine(virCommandPtr cmd, + virDomainDefPtr def, + virDomainNetDefPtr net, + virQEMUCapsPtr qemuCaps) +{ + virBuffer chardev_buf = VIR_BUFFER_INITIALIZER; + virBuffer netdev_buf = VIR_BUFFER_INITIALIZER; + char *nic = NULL; + + if (!qemuDomainSupportsNetdev(def, qemuCaps, net)) { + virReportError(VIR_ERR_INTERNAL_ERROR, + "%s", _("Netdev support unavailable")); + goto error; + } + + switch ((virDomainChrType) net->data.vhostuser->type) { + case VIR_DOMAIN_CHR_TYPE_UNIX: + virBufferAsprintf(&chardev_buf, "socket,id=char%s,path=%s%s", + net->info.alias, net->data.vhostuser->data.nix.path, + net->data.vhostuser->data.nix.listen ? ",server" : ""); + break; + + case VIR_DOMAIN_CHR_TYPE_NULL: + case VIR_DOMAIN_CHR_TYPE_VC: + case VIR_DOMAIN_CHR_TYPE_PTY: + case VIR_DOMAIN_CHR_TYPE_DEV: + case VIR_DOMAIN_CHR_TYPE_FILE: + case VIR_DOMAIN_CHR_TYPE_PIPE: + case VIR_DOMAIN_CHR_TYPE_STDIO: + case VIR_DOMAIN_CHR_TYPE_UDP: + case VIR_DOMAIN_CHR_TYPE_TCP: + case VIR_DOMAIN_CHR_TYPE_SPICEVMC: + case VIR_DOMAIN_CHR_TYPE_SPICEPORT: + case VIR_DOMAIN_CHR_TYPE_NMDM: + case VIR_DOMAIN_CHR_TYPE_LAST: + virReportError(VIR_ERR_INTERNAL_ERROR, + _("vhost-user type '%s' not supported"), + virDomainChrTypeToString(net->data.vhostuser->type)); + goto error; + } + + virBufferAsprintf(&netdev_buf, "type=vhost-user,id=host%s,chardev=char%s", + net->info.alias, net->info.alias); + + virCommandAddArg(cmd, "-chardev"); + virCommandAddArgBuffer(cmd, &chardev_buf); + + virCommandAddArg(cmd, "-netdev"); + virCommandAddArgBuffer(cmd, &netdev_buf); + + if (!(nic = qemuBuildNicDevStr(def, net, -1, 0, 0, qemuCaps))) { + virReportError(VIR_ERR_INTERNAL_ERROR, + "%s", _("Error generating NIC -device string")); + goto error; + } + + virCommandAddArgList(cmd, "-device", nic, NULL); + VIR_FREE(nic); + + return 0; + + error: + virBufferFreeAndReset(&chardev_buf); + virBufferFreeAndReset(&netdev_buf); + VIR_FREE(nic); + + return -1; +} + static int qemuBuildInterfaceCommandLine(virCommandPtr cmd, virQEMUDriverPtr driver, @@ -6871,6 +6941,9 @@ qemuBuildInterfaceCommandLine(virCommandPtr cmd, int actualType = virDomainNetGetActualType(net); size_t i; + if (actualType == VIR_DOMAIN_NET_TYPE_VHOSTUSER) + return qemuBuildVhostuserCommandLine(cmd, def, net, qemuCaps); + if (actualType == VIR_DOMAIN_NET_TYPE_HOSTDEV) { /* NET_TYPE_HOSTDEV devices are really hostdev devices, so * their commandlines are constructed with other hostdevs. diff --git a/src/uml/uml_conf.c b/src/uml/uml_conf.c index e25bf8ccf1..41ce03c593 100644 --- a/src/uml/uml_conf.c +++ b/src/uml/uml_conf.c @@ -182,6 +182,11 @@ umlBuildCommandLineNet(virConnectPtr conn, } break; + case VIR_DOMAIN_NET_TYPE_VHOSTUSER: + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("vhostuser networking type not supported")); + goto error; + case VIR_DOMAIN_NET_TYPE_SERVER: virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("TCP server networking type not supported")); diff --git a/src/xenxs/xen_sxpr.c b/src/xenxs/xen_sxpr.c index 9e598045d6..62ca72994f 100644 --- a/src/xenxs/xen_sxpr.c +++ b/src/xenxs/xen_sxpr.c @@ -1931,6 +1931,7 @@ xenFormatSxprNet(virConnectPtr conn, virBufferEscapeSexpr(buf, "(ip '%s')", def->data.ethernet.ipaddr); break; + case VIR_DOMAIN_NET_TYPE_VHOSTUSER: case VIR_DOMAIN_NET_TYPE_USER: case VIR_DOMAIN_NET_TYPE_SERVER: case VIR_DOMAIN_NET_TYPE_CLIENT: diff --git a/tests/qemuxml2argvdata/qemuxml2argv-net-vhostuser.args b/tests/qemuxml2argvdata/qemuxml2argv-net-vhostuser.args new file mode 100644 index 0000000000..cc66ec3ff9 --- /dev/null +++ b/tests/qemuxml2argvdata/qemuxml2argv-net-vhostuser.args @@ -0,0 +1,7 @@ +LC_ALL=C PATH=/bin HOME=/home/test USER=test LOGNAME=test QEMU_AUDIO_DRV=none \ +/usr/bin/qemu -S -M \ +pc -m 214 -smp 1 -nographic -nodefaults -monitor unix:/tmp/test-monitor,server,nowait \ +-no-acpi -boot c -usb -hda /dev/HostVG/QEMUGuest1 \ +-chardev socket,id=charnet0,path=/tmp/vhost.sock,server \ +-netdev type=vhost-user,id=hostnet0,chardev=charnet0 \ +-device virtio-net-pci,netdev=hostnet0,id=net0,mac=52:54:00:ee:96:6b,bus=pci.0,addr=0x3 diff --git a/tests/qemuxml2argvdata/qemuxml2argv-net-vhostuser.xml b/tests/qemuxml2argvdata/qemuxml2argv-net-vhostuser.xml new file mode 100644 index 0000000000..b49d48ef93 --- /dev/null +++ b/tests/qemuxml2argvdata/qemuxml2argv-net-vhostuser.xml @@ -0,0 +1,33 @@ + + QEMUGuest1 + c7a5fdbd-edaf-9455-926a-d65c16db1809 + 219136 + 219136 + 1 + + hvm + + + + destroy + restart + destroy + + /usr/bin/qemu + + + + +

+ + + + + + + + + + + + diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c index a841adb3af..557c87693d 100644 --- a/tests/qemuxml2argvtest.c +++ b/tests/qemuxml2argvtest.c @@ -933,6 +933,7 @@ mymain(void) DO_TEST_FAILURE("misc-enable-s4", NONE); DO_TEST("misc-no-reboot", NONE); DO_TEST("misc-uuid", QEMU_CAPS_NAME, QEMU_CAPS_UUID); + DO_TEST("net-vhostuser", QEMU_CAPS_DEVICE, QEMU_CAPS_NETDEV); DO_TEST("net-user", NONE); DO_TEST("net-virtio", NONE); DO_TEST("net-virtio-device", diff --git a/tests/qemuxml2xmltest.c b/tests/qemuxml2xmltest.c index 9f919de53b..8884b22121 100644 --- a/tests/qemuxml2xmltest.c +++ b/tests/qemuxml2xmltest.c @@ -247,6 +247,7 @@ mymain(void) DO_TEST("misc-disable-suspends"); DO_TEST("misc-enable-s4"); DO_TEST("misc-no-reboot"); + DO_TEST("net-vhostuser"); DO_TEST("net-user"); DO_TEST("net-virtio"); DO_TEST("net-virtio-device");