1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2024-12-22 17:34:18 +03:00

virsh: Support vhostuser in attach-interface

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@redhat.com>
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
This commit is contained in:
Michal Privoznik 2021-08-17 13:03:04 +02:00
parent 8efd949c8b
commit 868bacd380
5 changed files with 72 additions and 3 deletions

View File

@ -4659,6 +4659,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.
@ -4672,7 +4673,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:
@ -4686,6 +4689,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
@ -4720,6 +4725,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.

View File

@ -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,

View File

@ -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);

View File

@ -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:

View File

@ -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);