Recently, I wanted to attach an vhost-user interface but found out that attach-interface command doesn't support it. Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx> --- docs/manpages/virsh.rst | 11 ++++++++++- tools/virsh-completer-domain.c | 19 +++++++++++++++++++ tools/virsh-completer-domain.h | 5 +++++ tools/virsh-domain.c | 32 ++++++++++++++++++++++++++++++-- tools/virsh-domain.h | 8 ++++++++ 5 files changed, 72 insertions(+), 3 deletions(-) diff --git a/docs/manpages/virsh.rst b/docs/manpages/virsh.rst index 20936994ce..8a875d0d2e 100644 --- a/docs/manpages/virsh.rst +++ b/docs/manpages/virsh.rst @@ -4676,6 +4676,7 @@ attach-interface [--target target] [--mac mac] [--script script] [--model model] [--inbound average,peak,burst,floor] [--outbound average,peak,burst] [--alias alias] [--managed] [--print-xml] + [--source-mode mode] Attach a new network interface to the domain. @@ -4689,7 +4690,9 @@ Attach a new network interface to the domain. interfaces or bridges, *hostdev* to indicate connection using a passthrough of PCI device -on the host. +on the host, + +*vhostuser* to indicate connection using a virtio transport protocol. ``source`` indicates the source of the connection. The source depends on the type of the interface: @@ -4703,6 +4706,8 @@ on the type of the interface: *hostdev* the PCI address of the host's interface formatted as domain:bus:slot.function. +*vhostuser* the path to UNIX socket (control plane) + ``--target`` is used to specify the tap/macvtap device to be used to connect the domain to the source. Names starting with 'vnet' are considered as auto-generated and are blanked out/regenerated each @@ -4737,6 +4742,10 @@ Network XML documentation at that the interface should be managed, which means detached and reattached from/to the host by libvirt. +``--source-mode`` is mandatory for *vhostuser* interface and accepts values +*server* and *client* that control whether hypervisor waits for the other +process to connect, or initiates connection, respectively. + If ``--print-xml`` is specified, then the XML of the interface that would be attached is printed instead. diff --git a/tools/virsh-completer-domain.c b/tools/virsh-completer-domain.c index c86d8e8156..3ef6c82388 100644 --- a/tools/virsh-completer-domain.c +++ b/tools/virsh-completer-domain.c @@ -372,6 +372,25 @@ virshDomainInterfaceAddrSourceCompleter(vshControl *ctl G_GNUC_UNUSED, } +char ** +virshDomainInterfaceSourceModeCompleter(vshControl *ctl G_GNUC_UNUSED, + const vshCmd *cmd G_GNUC_UNUSED, + unsigned int flags) +{ + char **ret = NULL; + size_t i; + + virCheckFlags(0, NULL); + + ret = g_new0(char *, VIRSH_DOMAIN_INTERFACE_SOURCE_MODE_LAST); + + for (i = 0; i < VIRSH_DOMAIN_INTERFACE_SOURCE_MODE_LAST; i++) + ret[i] = g_strdup(virshDomainInterfaceSourceModeTypeToString(i)); + + return ret; +} + + char ** virshDomainHostnameSourceCompleter(vshControl *ctl G_GNUC_UNUSED, const vshCmd *cmd G_GNUC_UNUSED, diff --git a/tools/virsh-completer-domain.h b/tools/virsh-completer-domain.h index 45380906f9..39cedf0141 100644 --- a/tools/virsh-completer-domain.h +++ b/tools/virsh-completer-domain.h @@ -59,6 +59,11 @@ virshDomainInterfaceAddrSourceCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); +char ** +virshDomainInterfaceSourceModeCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); + char ** virshDomainHostnameSourceCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index 62912aaf01..e5bd1fdd75 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -831,9 +831,19 @@ static const vshCmdOptDef opts_attach_interface[] = { .type = VSH_OT_BOOL, .help = N_("libvirt will automatically detach/attach the device from/to host") }, + {.name = "source-mode", + .type = VSH_OT_STRING, + .completer = virshDomainInterfaceSourceModeCompleter, + .help = N_("mode attribute of <source/> element") + }, {.name = NULL} }; +VIR_ENUM_IMPL(virshDomainInterfaceSourceMode, + VIRSH_DOMAIN_INTERFACE_SOURCE_MODE_LAST, + "server", + "client"); + /* parse inbound and outbound which are in the format of * 'average,peak,burst,floor', in which peak and burst are optional, * thus 'average,,burst' and 'average,peak' are also legal. */ @@ -881,6 +891,8 @@ cmdAttachInterface(vshControl *ctl, const vshCmd *cmd) const char *mac = NULL, *target = NULL, *script = NULL, *type = NULL, *source = NULL, *model = NULL, *inboundStr = NULL, *outboundStr = NULL, *alias = NULL; + const char *sourceModeStr = NULL; + int sourceMode = -1; virNetDevBandwidthRate inbound, outbound; virDomainNetType typ; int ret; @@ -911,7 +923,8 @@ cmdAttachInterface(vshControl *ctl, const vshCmd *cmd) vshCommandOptStringReq(ctl, cmd, "model", &model) < 0 || vshCommandOptStringReq(ctl, cmd, "alias", &alias) < 0 || vshCommandOptStringReq(ctl, cmd, "inbound", &inboundStr) < 0 || - vshCommandOptStringReq(ctl, cmd, "outbound", &outboundStr) < 0) + vshCommandOptStringReq(ctl, cmd, "outbound", &outboundStr) < 0 || + vshCommandOptStringReq(ctl, cmd, "source-mode", &sourceModeStr) < 0) return false; /* check interface type */ @@ -921,6 +934,12 @@ cmdAttachInterface(vshControl *ctl, const vshCmd *cmd) return false; } + if (sourceModeStr && + (sourceMode = virshDomainInterfaceSourceModeTypeFromString(sourceModeStr)) < 0) { + vshError(ctl, _("Invalid source mode: %s"), sourceModeStr); + return false; + } + if (inboundStr) { memset(&inbound, 0, sizeof(inbound)); if (virshParseRateStr(ctl, inboundStr, &inbound) < 0) @@ -981,9 +1000,18 @@ cmdAttachInterface(vshControl *ctl, const vshCmd *cmd) break; } + case VIR_DOMAIN_NET_TYPE_VHOSTUSER: + if (sourceMode < 0) { + vshError(ctl, _("source-mode is mandatory")); + return false; + } + virBufferAsprintf(&buf, "<source type='unix' path='%s' mode='%s'/>\n", + source, + virshDomainInterfaceSourceModeTypeToString(sourceMode)); + break; + 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/tools/virsh-domain.h b/tools/virsh-domain.h index a1ac1cf1d4..cf5ce28825 100644 --- a/tools/virsh-domain.h +++ b/tools/virsh-domain.h @@ -38,6 +38,14 @@ typedef enum { VIR_ENUM_DECL(virshDomainHostnameSource); +typedef enum { + VIRSH_DOMAIN_INTERFACE_SOURCE_MODE_SERVER, + VIRSH_DOMAIN_INTERFACE_SOURCE_MODE_CLIENT, + VIRSH_DOMAIN_INTERFACE_SOURCE_MODE_LAST +} virshDomainInterfaceSourceMode; + +VIR_ENUM_DECL(virshDomainInterfaceSourceMode); + extern const vshCmdDef domManagementCmds[]; VIR_ENUM_DECL(virshDomainProcessSignal); -- 2.31.1