Blame SOURCES/0006-launch-libvirt-add-virtio-net-via-the-standard-interface-element.patch

d03fcc
From a18bc12081bcebf2d78883d1c6981c454149bb39 Mon Sep 17 00:00:00 2001
d03fcc
From: Laszlo Ersek <lersek@redhat.com>
d03fcc
Date: Thu, 23 Dec 2021 11:37:01 +0100
d03fcc
Subject: [PATCH] launch-libvirt: add virtio-net via the standard <interface>
d03fcc
 element
d03fcc
d03fcc
Starting with version 3.8.0, libvirt allows us to specify the network
d03fcc
address and network mask (as prefix) for SLIRP directly via the
d03fcc
<interface> element in the domain XML:
d03fcc
<https://libvirt.org/formatdomain.html#userspace-slirp-stack>. This means
d03fcc
we don't need the <qemu:commandline> hack for virtio-net on such versions.
d03fcc
d03fcc
Restrict the hack in construct_libvirt_xml_qemu_cmdline() to
d03fcc
libvirt<3.8.0, and generate the proper <interface> element in
d03fcc
construct_libvirt_xml_devices() on libvirt>=3.8.0.
d03fcc
d03fcc
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2034160
d03fcc
Suggested-by: Richard W.M. Jones <rjones@redhat.com>
d03fcc
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
d03fcc
Message-Id: <20211223103701.12702-4-lersek@redhat.com>
d03fcc
Reviewed-by: Richard W.M. Jones <rjones@redhat.com>
d03fcc
Tested-by: Richard W.M. Jones <rjones@redhat.com>
d03fcc
(cherry picked from commit 5858c2cf6c24b3776e3867eafd9d86a1f4912d9c)
d03fcc
---
d03fcc
 lib/guestfs-internal.h |  3 ++-
d03fcc
 lib/launch-libvirt.c   | 27 +++++++++++++++++++++++++--
d03fcc
 2 files changed, 27 insertions(+), 3 deletions(-)
d03fcc
d03fcc
diff --git a/lib/guestfs-internal.h b/lib/guestfs-internal.h
d03fcc
index e24d570f5..4a19e5c6b 100644
d03fcc
--- a/lib/guestfs-internal.h
d03fcc
+++ b/lib/guestfs-internal.h
d03fcc
@@ -173,7 +173,8 @@ cleanup_mutex_unlock (pthread_mutex_t **ptr)
d03fcc
 #endif
d03fcc
 
d03fcc
 /* Place the virtio-net controller in slot 0x1e on the root bus, on normal
d03fcc
- * hardware with PCI. Refer to RHBZ#2034160.
d03fcc
+ * hardware with PCI. Necessary only before libvirt 3.8.0. Refer to
d03fcc
+ * RHBZ#2034160.
d03fcc
  */
d03fcc
 #ifdef HAVE_LIBVIRT_BACKEND
d03fcc
 #if defined(__arm__) || defined(__s390x__)
d03fcc
diff --git a/lib/launch-libvirt.c b/lib/launch-libvirt.c
d03fcc
index 266d88824..cc714c02e 100644
d03fcc
--- a/lib/launch-libvirt.c
d03fcc
+++ b/lib/launch-libvirt.c
d03fcc
@@ -1413,6 +1413,28 @@ construct_libvirt_xml_devices (guestfs_h *g,
d03fcc
       } end_element ();
d03fcc
     } end_element ();
d03fcc
 
d03fcc
+    /* Virtio-net NIC with SLIRP (= userspace) back-end, if networking is
d03fcc
+     * enabled. Starting with libvirt 3.8.0, we can specify the network address
d03fcc
+     * and prefix for SLIRP in the domain XML. Therefore, we can add the NIC
d03fcc
+     * via the standard <interface> element rather than <qemu:commandline>, and
d03fcc
+     * so libvirt can manage the PCI address of the virtio-net NIC like the PCI
d03fcc
+     * addresses of all other devices. Refer to RHBZ#2034160.
d03fcc
+     */
d03fcc
+    if (g->enable_network &&
d03fcc
+        guestfs_int_version_ge (&params->data->libvirt_version, 3, 8, 0)) {
d03fcc
+      start_element ("interface") {
d03fcc
+        attribute ("type", "user");
d03fcc
+        start_element ("model") {
d03fcc
+          attribute ("type", "virtio");
d03fcc
+        } end_element ();
d03fcc
+        start_element ("ip") {
d03fcc
+          attribute ("family", "ipv4");
d03fcc
+          attribute ("address", NETWORK_ADDRESS);
d03fcc
+          attribute ("prefix", NETWORK_PREFIX);
d03fcc
+        } end_element ();
d03fcc
+      } end_element ();
d03fcc
+    }
d03fcc
+
d03fcc
     /* Libvirt adds some devices by default.  Indicate to libvirt
d03fcc
      * that we don't want them.
d03fcc
      */
d03fcc
@@ -1835,9 +1857,10 @@ construct_libvirt_xml_qemu_cmdline (guestfs_h *g,
d03fcc
     } end_element ();
d03fcc
 
d03fcc
     /* Workaround because libvirt user networking cannot specify "net="
d03fcc
-     * parameter.
d03fcc
+     * parameter. Necessary only before libvirt 3.8.0; refer to RHBZ#2034160.
d03fcc
      */
d03fcc
-    if (g->enable_network) {
d03fcc
+    if (g->enable_network &&
d03fcc
+        !guestfs_int_version_ge (&params->data->libvirt_version, 3, 8, 0)) {
d03fcc
       start_element ("qemu:arg") {
d03fcc
         attribute ("value", "-netdev");
d03fcc
       } end_element ();
d03fcc
-- 
d03fcc
2.19.1.3.g30247aa5d201
d03fcc