196f39
From fbb053fc71c0c072acb3fbf6e5fbbfc3b0667fd2 Mon Sep 17 00:00:00 2001
196f39
From: "Richard W.M. Jones" <rjones@redhat.com>
196f39
Date: Thu, 28 Jan 2021 12:20:49 +0000
196f39
Subject: [PATCH] appliance: Use -cpu max.
196f39
MIME-Version: 1.0
196f39
Content-Type: text/plain; charset=UTF-8
196f39
Content-Transfer-Encoding: 8bit
196f39
196f39
QEMU has a newish feature (from about 2017 / qemu 2.9) called -cpu max
196f39
which is supposed to select the best CPU, ideal for libguestfs.
196f39
196f39
After this change, on x86-64:
196f39
196f39
               KVM                          TCG
196f39
196f39
Direct         -cpu max                     -cpu max
196f39
(non-libvirt)
196f39
196f39
Libvirt   <cpu mode="host-passthrough">     <cpu mode="host-model">
196f39
            <model fallback="allow"/>         <model fallback="allow"/>
196f39
          </cpu>                            </cpu>
196f39
196f39
Thanks: Daniel Berrangé
196f39
(cherry picked from commit 30f74f38bd6e42e783ba80895f4d6826abddd417)
196f39
---
196f39
 lib/appliance-cpu.c  | 16 ++++++++--------
196f39
 lib/launch-libvirt.c |  9 +++++++++
196f39
 2 files changed, 17 insertions(+), 8 deletions(-)
196f39
196f39
diff --git a/lib/appliance-cpu.c b/lib/appliance-cpu.c
196f39
index 5ef9f5c72..54ac6e2e3 100644
196f39
--- a/lib/appliance-cpu.c
196f39
+++ b/lib/appliance-cpu.c
196f39
@@ -38,6 +38,11 @@
196f39
  *
196f39
  * The literal string C<"host"> means use C<-cpu host>.
196f39
  *
196f39
+ * =item C<"max">
196f39
+ *
196f39
+ * The literal string C<"max"> means use C<-cpu max> (the best
196f39
+ * possible).  This requires awkward translation for libvirt.
196f39
+ *
196f39
  * =item some string
196f39
  *
196f39
  * Some string such as C<"cortex-a57"> means use C<-cpu cortex-a57>.
196f39
@@ -80,14 +85,9 @@ guestfs_int_get_cpu_model (int kvm)
196f39
   /* See discussion in https://bugzilla.redhat.com/show_bug.cgi?id=1605071 */
196f39
   return NULL;
196f39
 #else
196f39
-  /* On most architectures, it is faster to pass the CPU host model to
196f39
-   * the appliance, allowing maximum speed for things like checksums
196f39
-   * and encryption.  Only do this with KVM.  It is broken in subtle
196f39
-   * ways on TCG, and fairly pointless when you're emulating anyway.
196f39
+  /* On most architectures we can use "max" to get the best possible CPU.
196f39
+   * For recent qemu this should work even on TCG.
196f39
    */
196f39
-  if (kvm)
196f39
-    return "host";
196f39
-  else
196f39
-    return NULL;
196f39
+  return "max";
196f39
 #endif
196f39
 }
196f39
diff --git a/lib/launch-libvirt.c b/lib/launch-libvirt.c
196f39
index f6bb39d99..e3ff1ffe0 100644
196f39
--- a/lib/launch-libvirt.c
196f39
+++ b/lib/launch-libvirt.c
196f39
@@ -1169,6 +1169,15 @@ construct_libvirt_xml_cpu (guestfs_h *g,
196f39
           attribute ("fallback", "allow");
196f39
         } end_element ();
196f39
       }
196f39
+      else if (STREQ (cpu_model, "max")) {
196f39
+        if (params->data->is_kvm)
196f39
+          attribute ("mode", "host-passthrough");
196f39
+        else
196f39
+          attribute ("mode", "host-model");
196f39
+        start_element ("model") {
196f39
+          attribute ("fallback", "allow");
196f39
+        } end_element ();
196f39
+      }
196f39
       else
196f39
         single_element ("model", cpu_model);
196f39
     } end_element ();
196f39
-- 
196f39
2.31.1
196f39