qemu: command: Format netdev as JSON when QEMU_CAPS_NETDEV_JSON is present

Base the JSON output on a regular capability flag rather than purely
internal flag. This will prepare for the time when QEMU will accept JSON
argumets for -netdev.

For now the capability is not set (thus we for now don't have QMP
schema validation) but that will be addressed later.

To achieve this 'qemuBuildNetdevCommandlineFromJSON' is introduced
and all callers of 'virQEMUBuildNetdevCommandlineFromJSON' are
refactored to use the new helper.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2021-09-24 18:47:59 +02:00
parent 36253a48b4
commit 63a833038f
4 changed files with 42 additions and 20 deletions

View File

@ -3118,6 +3118,7 @@ virQEMUBuildBufferEscapeComma;
virQEMUBuildCommandLineJSON; virQEMUBuildCommandLineJSON;
virQEMUBuildCommandLineJSONArrayBitmap; virQEMUBuildCommandLineJSONArrayBitmap;
virQEMUBuildCommandLineJSONArrayNumbered; virQEMUBuildCommandLineJSONArrayNumbered;
virQEMUBuildCommandLineJSONArrayObjectsStr;
virQEMUBuildDriveCommandlineFromJSON; virQEMUBuildDriveCommandlineFromJSON;
virQEMUBuildNetdevCommandlineFromJSON; virQEMUBuildNetdevCommandlineFromJSON;

View File

@ -183,6 +183,34 @@ qemuBuildObjectCommandlineFromJSON(virCommand *cmd,
} }
static int
qemuBuildNetdevCommandlineFromJSON(virCommand *cmd,
virJSONValue *props,
virQEMUCaps *qemuCaps)
{
g_autofree char *arg = NULL;
if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_NETDEV_JSON)) {
if (!(arg = virJSONValueToString(props, false)))
return -1;
} else {
const char *type = virJSONValueObjectGetString(props, "type");
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
virBufferAsprintf(&buf, "%s,", type);
if (virQEMUBuildCommandLineJSON(props, &buf, "type",
virQEMUBuildCommandLineJSONArrayObjectsStr) < 0)
return -1;
arg = virBufferContentAndReset(&buf);
}
virCommandAddArgList(cmd, "-netdev", arg, NULL);
return 0;
}
/** /**
* qemuBuildMasterKeyCommandLine: * qemuBuildMasterKeyCommandLine:
* @cmd: the command to modify * @cmd: the command to modify
@ -8402,13 +8430,11 @@ qemuBuildInterfaceCommandLine(virQEMUDriver *driver,
virNetDevVPortProfileOp vmop, virNetDevVPortProfileOp vmop,
bool standalone, bool standalone,
size_t *nnicindexes, size_t *nnicindexes,
int **nicindexes, int **nicindexes)
unsigned int flags)
{ {
virDomainDef *def = vm->def; virDomainDef *def = vm->def;
int ret = -1; int ret = -1;
g_autofree char *nic = NULL; g_autofree char *nic = NULL;
g_autofree char *host = NULL;
g_autofree char *chardev = NULL; g_autofree char *chardev = NULL;
int *tapfd = NULL; int *tapfd = NULL;
size_t tapfdSize = 0; size_t tapfdSize = 0;
@ -8662,12 +8688,9 @@ qemuBuildInterfaceCommandLine(virQEMUDriver *driver,
slirpfdName, vdpafdName))) slirpfdName, vdpafdName)))
goto cleanup; goto cleanup;
if (!(host = virQEMUBuildNetdevCommandlineFromJSON(hostnetprops, if (qemuBuildNetdevCommandlineFromJSON(cmd, hostnetprops, qemuCaps) < 0)
(flags & QEMU_BUILD_COMMANDLINE_VALIDATE_KEEP_JSON))))
goto cleanup; goto cleanup;
virCommandAddArgList(cmd, "-netdev", host, NULL);
/* Possible combinations: /* Possible combinations:
* *
* Old way: -netdev type=tap,id=netdev1 \ * Old way: -netdev type=tap,id=netdev1 \
@ -8740,8 +8763,7 @@ qemuBuildNetCommandLine(virQEMUDriver *driver,
bool standalone, bool standalone,
size_t *nnicindexes, size_t *nnicindexes,
int **nicindexes, int **nicindexes,
unsigned int *bootHostdevNet, unsigned int *bootHostdevNet)
unsigned int flags)
{ {
size_t i; size_t i;
int last_good_net = -1; int last_good_net = -1;
@ -8765,7 +8787,7 @@ qemuBuildNetCommandLine(virQEMUDriver *driver,
if (qemuBuildInterfaceCommandLine(driver, vm, logManager, secManager, cmd, net, if (qemuBuildInterfaceCommandLine(driver, vm, logManager, secManager, cmd, net,
qemuCaps, bootNet, vmop, qemuCaps, bootNet, vmop,
standalone, nnicindexes, standalone, nnicindexes,
nicindexes, flags) < 0) nicindexes) < 0)
goto error; goto error;
last_good_net = i; last_good_net = i;
@ -9289,8 +9311,7 @@ qemuBuildChannelsCommandLine(virLogManager *logManager,
virQEMUDriverConfig *cfg, virQEMUDriverConfig *cfg,
const virDomainDef *def, const virDomainDef *def,
virQEMUCaps *qemuCaps, virQEMUCaps *qemuCaps,
bool chardevStdioLogd, bool chardevStdioLogd)
unsigned int flags)
{ {
size_t i; size_t i;
unsigned int cdevflags = QEMU_BUILD_CHARDEV_TCP_NOWAIT | unsigned int cdevflags = QEMU_BUILD_CHARDEV_TCP_NOWAIT |
@ -9302,7 +9323,6 @@ qemuBuildChannelsCommandLine(virLogManager *logManager,
virDomainChrDef *channel = def->channels[i]; virDomainChrDef *channel = def->channels[i];
g_autofree char *chardevstr = NULL; g_autofree char *chardevstr = NULL;
g_autoptr(virJSONValue) netdevprops = NULL; g_autoptr(virJSONValue) netdevprops = NULL;
g_autofree char *netdevstr = NULL;
if (!(chardevstr = qemuBuildChrChardevStr(logManager, secManager, if (!(chardevstr = qemuBuildChrChardevStr(logManager, secManager,
cmd, cfg, def, cmd, cfg, def,
@ -9319,11 +9339,8 @@ qemuBuildChannelsCommandLine(virLogManager *logManager,
if (!(netdevprops = qemuBuildChannelGuestfwdNetdevProps(channel))) if (!(netdevprops = qemuBuildChannelGuestfwdNetdevProps(channel)))
return -1; return -1;
if (!(netdevstr = virQEMUBuildNetdevCommandlineFromJSON(netdevprops, if (qemuBuildNetdevCommandlineFromJSON(cmd, netdevprops, qemuCaps) < 0)
(flags & QEMU_BUILD_COMMANDLINE_VALIDATE_KEEP_JSON))))
return -1; return -1;
virCommandAddArgList(cmd, "-netdev", netdevstr, NULL);
break; break;
case VIR_DOMAIN_CHR_CHANNEL_TARGET_TYPE_VIRTIO: case VIR_DOMAIN_CHR_CHANNEL_TARGET_TYPE_VIRTIO:
@ -10471,7 +10488,7 @@ qemuBuildCommandLine(virQEMUDriver *driver,
if (qemuBuildNetCommandLine(driver, vm, logManager, secManager, cmd, if (qemuBuildNetCommandLine(driver, vm, logManager, secManager, cmd,
qemuCaps, vmop, standalone, qemuCaps, vmop, standalone,
nnicindexes, nicindexes, &bootHostdevNet, flags) < 0) nnicindexes, nicindexes, &bootHostdevNet) < 0)
return NULL; return NULL;
if (qemuBuildSmartcardCommandLine(logManager, secManager, cmd, cfg, def, qemuCaps, if (qemuBuildSmartcardCommandLine(logManager, secManager, cmd, cfg, def, qemuCaps,
@ -10487,7 +10504,7 @@ qemuBuildCommandLine(virQEMUDriver *driver,
return NULL; return NULL;
if (qemuBuildChannelsCommandLine(logManager, secManager, cmd, cfg, def, qemuCaps, if (qemuBuildChannelsCommandLine(logManager, secManager, cmd, cfg, def, qemuCaps,
chardevStdioLogd, flags) < 0) chardevStdioLogd) < 0)
return NULL; return NULL;
if (qemuBuildConsoleCommandLine(logManager, secManager, cmd, cfg, def, qemuCaps, if (qemuBuildConsoleCommandLine(logManager, secManager, cmd, cfg, def, qemuCaps,

View File

@ -127,7 +127,7 @@ virQEMUBuildCommandLineJSONArrayNumbered(const char *key,
* *
* guestfwd=tcp:10.0.2.1:4600-chardev:charchannel0,guestfwd=... * guestfwd=tcp:10.0.2.1:4600-chardev:charchannel0,guestfwd=...
*/ */
static int int
virQEMUBuildCommandLineJSONArrayObjectsStr(const char *key, virQEMUBuildCommandLineJSONArrayObjectsStr(const char *key,
virJSONValue *array, virJSONValue *array,
virBuffer *buf, virBuffer *buf,

View File

@ -30,6 +30,10 @@ typedef int (*virQEMUBuildCommandLineJSONArrayFormatFunc)(const char *key,
virJSONValue *array, virJSONValue *array,
virBuffer *buf, virBuffer *buf,
const char *skipKey); const char *skipKey);
int virQEMUBuildCommandLineJSONArrayObjectsStr(const char *key,
virJSONValue *array,
virBuffer *buf,
const char *skipKey);
int virQEMUBuildCommandLineJSONArrayBitmap(const char *key, int virQEMUBuildCommandLineJSONArrayBitmap(const char *key,
virJSONValue *array, virJSONValue *array,
virBuffer *buf, virBuffer *buf,