Skip to contentSkip to conversation

vl: Simplify vga interface configuration code

PATCH Superseded Eduardo Habkost 2 participants 6 messages Nov 11, 2015 – Nov 12, 2015

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover vl: Simplify vga interface configuration code

Instead of duplicating class lookup and strstart() code for each VGA interface type, use a table to implement select_vgahw().

This series needs the following patch to be applied first:

From: Eduardo Habkost <[email protected]>
Date: Wed, 11 Nov 2015 18:39:18 -0200
Message-Id: <[email protected]>
Subject: [PATCH] vl: Use exit(1) when requested VGA interface is unavailable

Eduardo Habkost (2):
vl: Table-based select_vgahw()
vl: Replace *_vga_available() functions with class_names field

 include/sysemu/sysemu.h |   1 +
 vl.c                    | 156 ++++++++++++++++++++++--------------------------
 2 files changed, 72 insertions(+), 85 deletions(-)
Signature · 1 line
2.1.0
#1 lore raw

PATCH 1/2 vl: Table-based select_vgahw()

Instead of implementing separate check functions for each vga interface type, add a table enumerating the possible VGA interfaces.

---
 include/sysemu/sysemu.h |   1 +
 vl.c                    | 114 ++++++++++++++++++++++++++----------------------
 2 files changed, 63 insertions(+), 52 deletions(-)
include/sysemu/sysemu.h +1 -0
index f992494..6406906 100644
@@ -147,6 +147,7 @@ extern int autostart;
 typedef enum {
     VGA_NONE, VGA_STD, VGA_CIRRUS, VGA_VMWARE, VGA_XENFB, VGA_QXL,
     VGA_TCX, VGA_CG3, VGA_DEVICE, VGA_VIRTIO,
+    VGA_TYPE_MAX,
 } VGAInterfaceType;
 
 extern int vga_interface_type;
vl.c +62 -52

125 lines not on this page · raw message

Signature · 1 line
2.1.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [Qemu-devel] [PATCH 1/2] vl: Table-based select_vgahw()

Quoted 15 lines · Eduardo Habkost

Instead of implementing separate check functions for each vga interface type, add a table enumerating the possible VGA interfaces.

---
 include/sysemu/sysemu.h |   1 +
 vl.c                    | 114 ++++++++++++++++++++++++++----------------------
 2 files changed, 63 insertions(+), 52 deletions(-)
include/sysemu/sysemu.h +0 -0
index f992494..6406906 100644

include/sysemu/sysemu.h extern int autostart; · line 151

Quoted 5 lines
include/sysemu/sysemu.h +1 -0
@@ -147,6 +147,7 @@ extern int autostart;
 typedef enum {
     VGA_NONE, VGA_STD, VGA_CIRRUS, VGA_VMWARE, VGA_XENFB, VGA_QXL,
     VGA_TCX, VGA_CG3, VGA_DEVICE, VGA_VIRTIO,
+    VGA_TYPE_MAX,
 } VGAInterfaceType;

Would it be worth exposing this enum in qapi someday? But doesn't affect the correctness of this patch.

include/sysemu/sysemu.h extern int autostart; · line 152

Quoted 1 line
include/sysemu/sysemu.h +0 -0
 static void select_vgahw (const char *p)

Worth dropping the space before '(' while in the neighborhood?

include/sysemu/sysemu.h

Quoted 5 lines
include/sysemu/sysemu.h +0 -0
 {
     const char *opts;
include/sysemu/sysemu.h +1 -1
+    int t;
 
-    assert(vga_interface_type == VGA_NONE);

Are you intentionally dropping the assert? It protects us from select_vgahw() being called more than once.

Quoted 5 lines
include/sysemu/sysemu.h +0 -5
-    } else if (strstart(p, "cg3", &opts)) {
-        if (cg3_vga_available()) {
-            vga_interface_type = VGA_CG3;
-        } else {
-            error_report("CG3 framebuffer not available");

include/sysemu/sysemu.h

Quoted 10 lines
include/sysemu/sysemu.h +9 -1
-            exit(1);
+    for (t = 0; t < VGA_TYPE_MAX; t++) {
+        VGAInterfaceInfo *ti = &vga_interfaces[t];
+        if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
+            if (ti->available && !ti->available()) {
+                error_report("%s not available", ti->name);
+                exit(1);
+            }
+            vga_interface_type = t;
+            break;

Nice compression in code size.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#3 lore raw

Re: [Qemu-devel] [PATCH 1/2] vl: Table-based select_vgahw()

Quoted 11 lines · Eric Blake
Quoted 10 lines · Eduardo Habkost

Instead of implementing separate check functions for each vga interface type, add a table enumerating the possible VGA interfaces.

---
 include/sysemu/sysemu.h |   1 +
 vl.c                    | 114 ++++++++++++++++++++++++++----------------------
 2 files changed, 63 insertions(+), 52 deletions(-)
Quoted 13 lines

include/sysemu/sysemu.h extern int autostart; · line 151

Quoted 10 lines
include/sysemu/sysemu.h +1 -0
index f992494..6406906 100644
@@ -147,6 +147,7 @@ extern int autostart;
 typedef enum {
     VGA_NONE, VGA_STD, VGA_CIRRUS, VGA_VMWARE, VGA_XENFB, VGA_QXL,
     VGA_TCX, VGA_CG3, VGA_DEVICE, VGA_VIRTIO,
+    VGA_TYPE_MAX,
 } VGAInterfaceType;

Would it be worth exposing this enum in qapi someday? But doesn't affect the correctness of this patch.

I don't know. Maybe not, if we have any plans to configure this using QOM classes somehow (like the CPU model stuff), not using enums.

Quoted 5 lines

include/sysemu/sysemu.h extern int autostart; · line 152

Quoted 2 lines
include/sysemu/sysemu.h +0 -0
 static void select_vgahw (const char *p)

Worth dropping the space before '(' while in the neighborhood?

Quoted 8 lines

include/sysemu/sysemu.h

Quoted 5 lines
include/sysemu/sysemu.h +0 -0
 {
     const char *opts;
include/sysemu/sysemu.h +1 -1
+    int t;
 
-    assert(vga_interface_type == VGA_NONE);

Are you intentionally dropping the assert? It protects us from select_vgahw() being called more than once.

That was not intentional. I will fix it in a new version. Thanks!

Quoted 17 lines
Quoted 6 lines
include/sysemu/sysemu.h +0 -5
-    } else if (strstart(p, "cg3", &opts)) {
-        if (cg3_vga_available()) {
-            vga_interface_type = VGA_CG3;
-        } else {
-            error_report("CG3 framebuffer not available");

include/sysemu/sysemu.h

Quoted 10 lines
include/sysemu/sysemu.h +9 -1
-            exit(1);
+    for (t = 0; t < VGA_TYPE_MAX; t++) {
+        VGAInterfaceInfo *ti = &vga_interfaces[t];
+        if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
+            if (ti->available && !ti->available()) {
+                error_report("%s not available", ti->name);
+                exit(1);
+            }
+            vga_interface_type = t;
+            break;
Quoted 3 lines

Nice compression in code size.

Thanks!

Signature · 1 line
Eduardo
#4 lore raw

PATCH 2/2 vl: Replace *_vga_available() functions with class_names field

In reply to Eduardo Habkost

Instead of requiring a separate function for each VGA interface, just enumerate the corresponding class names on struct VGAInterfaceInfo.

---
 vl.c | 70 +++++++++++++++++++++++---------------------------------------------
 1 file changed, 23 insertions(+), 47 deletions(-)
vl.c +23 -47
index a5dd9a6..c899adf 100644
@@ -1965,46 +1965,12 @@ static const QEMUOption qemu_options[] = {
     { NULL },
 };
 
-static bool vga_available(void)
-{
-    return object_class_by_name("VGA") || object_class_by_name("isa-vga");
-}
-
-static bool cirrus_vga_available(void)
-{
-    return object_class_by_name("cirrus-vga")
-           || object_class_by_name("isa-cirrus-vga");
-}
-
-static bool vmware_vga_available(void)
-{
-    return object_class_by_name("vmware-svga");
-}
-
-static bool qxl_vga_available(void)
-{
-    return object_class_by_name("qxl-vga");
-}
-
-static bool tcx_vga_available(void)
-{
-    return object_class_by_name("SUNW,tcx");
-}
-
-static bool cg3_vga_available(void)
-{
-    return object_class_by_name("cgthree");
-}
-
-static bool virtio_vga_available(void)
-{
-    return object_class_by_name("virtio-vga");
-}
-
 typedef struct VGAInterfaceInfo {
     const char *opt_name;    /* option name */
     const char *name;        /* human-readable name */
-    bool (*available)(void);
+    /* Class names indicating that support is available.
+     * If no class is specified, the interface is always available */
+    const char *class_names[2];
 } VGAInterfaceInfo;
 
 static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
@@ -2014,43 +1980,53 @@ static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
     [VGA_STD] = {
         .opt_name = "std",
         .name = "standard VGA",
-        .available = vga_available,
+        .class_names = { "VGA", "isa-vga" },
     },
     [VGA_CIRRUS] = {
         .opt_name = "cirrus",
         .name = "Cirrus VGA",
-        .available = cirrus_vga_available,
+        .class_names = { "cirrus-vga", "isa-cirrus-vga" },
     },
     [VGA_VMWARE] = {
         .opt_name = "vmware",
         .name = "VMWare SVGA",
-        .available = vmware_vga_available,
+        .class_names = { "vmware-svga" },
     },
     [VGA_VIRTIO] = {
         .opt_name = "virtio",
         .name = "Virtio VGA",
-        .available = virtio_vga_available,
+        .class_names = { "virtio-vga" },
     },
     [VGA_QXL] = {
         .opt_name = "qxl",
         .name = "QXL VGA",
-        .available = qxl_vga_available,
+        .class_names = { "qxl-vga" },
     },
     [VGA_TCX] = {
         .opt_name = "tcx",
         .name = "TCX framebuffer",
-        .available = tcx_vga_available,
+        .class_names = { "SUNW,tcx" },
     },
     [VGA_CG3] = {
         .opt_name = "cg3",
         .name = "CG3 framebuffer",
-        .available = cg3_vga_available,
+        .class_names = { "cgthree" },
     },
     [VGA_XENFB] = {
         .opt_name = "xenfb",
     },
 };
 
+static bool vga_interface_available(VGAInterfaceType t)
+{
+    VGAInterfaceInfo *ti = &vga_interfaces[t];
+
+    assert(t < VGA_TYPE_MAX);
+    return !ti->class_names[0] ||
+           object_class_by_name(ti->class_names[0]) ||
+           object_class_by_name(ti->class_names[1]);
+}
+
 static void select_vgahw (const char *p)
 {
     const char *opts;
@@ -2059,7 +2035,7 @@ static void select_vgahw (const char *p)
     for (t = 0; t < VGA_TYPE_MAX; t++) {
         VGAInterfaceInfo *ti = &vga_interfaces[t];
         if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
-            if (ti->available && !ti->available()) {
+            if (!vga_interface_available(t)) {
                 error_report("%s not available", ti->name);
                 exit(1);
             }
@@ -4503,9 +4479,9 @@ int main(int argc, char **argv, char **envp)
     if (default_vga) {
         if (machine_class->default_display) {
             vga_model = machine_class->default_display;
-        } else if (cirrus_vga_available()) {
+        } else if (vga_interface_available(VGA_CIRRUS)) {
             vga_model = "cirrus";
-        } else if (vga_available()) {
+        } else if (vga_interface_available(VGA_STD)) {
             vga_model = "std";
         }
     }
Signature · 1 line
2.1.0
Reviewed-by on v1 2/2
#5 lore raw

Re: [Qemu-devel] [PATCH 2/2] vl: Replace *_vga_available() functions with class_names field

Quoted 19 lines · Eduardo Habkost

Instead of requiring a separate function for each VGA interface, just enumerate the corresponding class names on struct VGAInterfaceInfo.

---
 vl.c | 70 +++++++++++++++++++++++---------------------------------------------
 1 file changed, 23 insertions(+), 47 deletions(-)
     [VGA_CG3] = {
         .opt_name = "cg3",
         .name = "CG3 framebuffer",
vl.c +1 -1
-        .available = cg3_vga_available,
+        .class_names = { "cgthree" },
     },
     [VGA_XENFB] = {
         .opt_name = "xenfb",
     },

vl.c static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = { · line 2027

Quoted 10 lines
vl.c +8 -0
 };
 
+static bool vga_interface_available(VGAInterfaceType t)
+{
+    VGAInterfaceInfo *ti = &vga_interfaces[t];
+
+    assert(t < VGA_TYPE_MAX);
+    return !ti->class_names[0] ||
+           object_class_by_name(ti->class_names[0]) ||
+           object_class_by_name(ti->class_names[1]);

Does the correct thing when ti->class_names[1] is NULL (but I had to look).

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org