From: Peter Krempa Date: Mon, 9 May 2022 14:19:49 +0000 (+0200) Subject: qemuBuildHostNetProps: Refactor construction of tapfd/vhostfd arguments X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=29067596f205902504cedc6958b2b02316bc2b07;p=libvirt.git qemuBuildHostNetProps: Refactor construction of tapfd/vhostfd arguments Pre-construct the array the same way for the case when there's only one FD and when there are multiple. We just change the argument name depending on the count. Signed-off-by: Peter Krempa Reviewed-by: Jonathon Jongsma Reviewed-by: Ján Tomko --- diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index f384f245e1..1f940779d0 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -4236,54 +4236,50 @@ qemuBuildHostNetProps(virDomainNetDef *net, case VIR_DOMAIN_NET_TYPE_BRIDGE: case VIR_DOMAIN_NET_TYPE_NETWORK: case VIR_DOMAIN_NET_TYPE_DIRECT: - case VIR_DOMAIN_NET_TYPE_ETHERNET: - if (virJSONValueObjectAdd(&netprops, "s:type", "tap", NULL) < 0) - return NULL; + case VIR_DOMAIN_NET_TYPE_ETHERNET: { + g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER; + /* for one tapfd/vhostfd 'fd=' shall be used, for more use 'fds=' */ + const char *tapfd_field = "s:fd"; + g_autofree char *tapfd_arg = NULL; + const char *vhostfd_field = "S:vhostfd"; + g_autofree char *vhostfd_arg = NULL; + bool vhost = false; - /* for one tapfd 'fd=' shall be used, - * for more than one 'fds=' is the right choice */ - if (tapfdSize == 1) { - if (virJSONValueObjectAdd(&netprops, "s:fd", tapfd[0], NULL) < 0) - return NULL; - } else { - g_auto(virBuffer) fdsbuf = VIR_BUFFER_INITIALIZER; + for (i = 0; i < tapfdSize; i++) + virBufferAsprintf(&buf, "%s:", tapfd[i]); - for (i = 0; i < tapfdSize; i++) - virBufferAsprintf(&fdsbuf, "%s:", tapfd[i]); + if (tapfdSize > 1) + tapfd_field = "s:fds"; - virBufferTrim(&fdsbuf, ":"); + virBufferTrim(&buf, ":"); + tapfd_arg = virBufferContentAndReset(&buf); - if (virJSONValueObjectAdd(&netprops, - "s:fds", virBufferCurrentContent(&fdsbuf), - NULL) < 0) - return NULL; - } + if (vhostfdSize > 0) { + vhost = true; - if (vhostfdSize) { - if (virJSONValueObjectAppendBoolean(netprops, "vhost", true) < 0) - return NULL; + for (i = 0; i < vhostfdSize; i++) + virBufferAsprintf(&buf, "%s:", vhostfd[i]); - if (vhostfdSize == 1) { - if (virJSONValueObjectAdd(&netprops, "s:vhostfd", vhostfd[0], NULL) < 0) - return NULL; - } else { - g_auto(virBuffer) fdsbuf = VIR_BUFFER_INITIALIZER; + if (vhostfdSize > 1) + vhostfd_field = "s:vhostfds"; + } - for (i = 0; i < vhostfdSize; i++) - virBufferAsprintf(&fdsbuf, "%s:", vhostfd[i]); + virBufferTrim(&buf, ":"); + vhostfd_arg = virBufferContentAndReset(&buf); - virBufferTrim(&fdsbuf, ":"); + if (virJSONValueObjectAdd(&netprops, + "s:type", "tap", + tapfd_field, tapfd_arg, + "B:vhost", vhost, + vhostfd_field, vhostfd_arg, + NULL) < 0) + return NULL; - if (virJSONValueObjectAdd(&netprops, - "s:vhostfds", virBufferCurrentContent(&fdsbuf), - NULL) < 0) - return NULL; - } - } if (net->tune.sndbuf_specified && virJSONValueObjectAppendNumberUlong(netprops, "sndbuf", net->tune.sndbuf) < 0) return NULL; + } break; case VIR_DOMAIN_NET_TYPE_CLIENT: