Re: PATCH 2/20: Move driver capabilities code

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Attached this time..

On Fri, Jun 22, 2007 at 02:38:50AM +0100, Daniel P. Berrange wrote:
> This patch moves the QEMU driver capabilities XML generation code out of
> the dispatch.c file and into the driver.c where the rest of the QEMU driver
> impls live. This is to enable the dispatch.c file to be removed later.
> 
>  dispatch.c |  193 +++++--------------------------------------------------------
>  driver.c   |  184 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
>  driver.h   |   11 ++-
>  3 files changed, 203 insertions(+), 185 deletions(-)

Dan.
-- 
|=- Red Hat, Engineering, Emerging Technologies, Boston.  +1 978 392 2496 -=|
|=-           Perl modules: http://search.cpan.org/~danberr/              -=|
|=-               Projects: http://freshmeat.net/~danielpb/               -=|
|=-  GnuPG: 7D3B9505   F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505  -=| 
diff -r 13f57e3de09e qemud/dispatch.c
--- a/qemud/dispatch.c	Sun Jun 17 16:30:02 2007 -0400
+++ b/qemud/dispatch.c	Sun Jun 17 16:46:32 2007 -0400
@@ -28,14 +28,12 @@
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
-#include <sys/utsname.h>
 #include <libvirt/virterror.h>
 
 #include "internal.h"
 #include "driver.h"
 #include "dispatch.h"
 #include "conf.h"
-#include "buf.h"
 
 
 static int qemudDispatchFailure(struct qemud_server *server ATTRIBUTE_UNUSED,
@@ -63,32 +61,21 @@ static int qemudDispatchGetVersion(struc
 
 static int qemudDispatchGetNodeInfo(struct qemud_server *server, struct qemud_client *client,
                                     struct qemud_packet_client_data *in ATTRIBUTE_UNUSED, struct qemud_packet_server_data *out) {
-    struct utsname info;
-
-    if (uname(&info) < 0) {
+    if (qemudGetNodeInfo(&out->qemud_packet_server_data_u.getNodeInfoReply.memory,
+                         out->qemud_packet_server_data_u.getNodeInfoReply.model,
+                         sizeof(out->qemud_packet_server_data_u.getNodeInfoReply.model),
+                         &out->qemud_packet_server_data_u.getNodeInfoReply.cpus,
+                         &out->qemud_packet_server_data_u.getNodeInfoReply.mhz,
+                         &out->qemud_packet_server_data_u.getNodeInfoReply.nodes,
+                         &out->qemud_packet_server_data_u.getNodeInfoReply.sockets,
+                         &out->qemud_packet_server_data_u.getNodeInfoReply.cores,
+                         &out->qemud_packet_server_data_u.getNodeInfoReply.threads) < 0) {
         if (qemudDispatchFailure(server, client, out) < 0)
             return -1;
         return 0;
     }
 
-    if (qemudGetCPUInfo(&out->qemud_packet_server_data_u.getNodeInfoReply.cpus,
-                        &out->qemud_packet_server_data_u.getNodeInfoReply.mhz,
-                        &out->qemud_packet_server_data_u.getNodeInfoReply.nodes,
-                        &out->qemud_packet_server_data_u.getNodeInfoReply.sockets,
-                        &out->qemud_packet_server_data_u.getNodeInfoReply.cores,
-                        &out->qemud_packet_server_data_u.getNodeInfoReply.threads) < 0) {
-        if (qemudDispatchFailure(server, client, out) < 0)
-            return -1;
-        return 0;
-    }
-    if (qemudGetMemInfo(&out->qemud_packet_server_data_u.getNodeInfoReply.memory) < 0) {
-        if (qemudDispatchFailure(server, client, out) < 0)
-            return -1;
-        return 0;
-    }
-
     out->type = QEMUD_SERVER_PKT_GET_NODEINFO;
-    strncpy(out->qemud_packet_server_data_u.getNodeInfoReply.model, info.machine, sizeof(out->qemud_packet_server_data_u.getNodeInfoReply.model));
     out->qemud_packet_server_data_u.getNodeInfoReply.model[sizeof(out->qemud_packet_server_data_u.getNodeInfoReply.model)-1] = '\0';
 
     return 0;
@@ -100,166 +87,18 @@ qemudDispatchGetCapabilities (struct qem
                               struct qemud_packet_client_data *in ATTRIBUTE_UNUSED,
                               struct qemud_packet_server_data *out)
 {
-    struct utsname utsname;
-    int i, j, r;
-    int have_kqemu = 0;
-    int have_kvm = 0;
-    bufferPtr xml;
-    int len;
-
-    /* Really, this never fails - look at the man-page. */
-    uname (&utsname);
-
-    have_kqemu = access ("/dev/kqemu", F_OK) == 0;
-    have_kvm = access ("/dev/kvm", F_OK) == 0;
-
-    /* Construct the XML. */
-    xml = bufferNew (1024);
-    if (!xml) {
-        qemudReportError (server, VIR_ERR_NO_MEMORY, NULL);
-        qemudDispatchFailure (server, client, out);
-        return 0;
-    }
-
-    r = bufferVSprintf (xml,
-                        "\
-<capabilities>\n\
-  <host>\n\
-    <cpu>\n\
-      <arch>%s</arch>\n\
-    </cpu>\n\
-  </host>\n",
-                        utsname.machine);
-    if (r == -1) {
-    vir_buffer_failed:
-        bufferFree (xml);
-        qemudReportError (server, VIR_ERR_NO_MEMORY, NULL);
-        qemudDispatchFailure (server, client, out);
-        return 0;
-    }
-
-    i = -1;
-    if (strcmp (utsname.machine, "i686") == 0) i = 0;
-    else if (strcmp (utsname.machine, "x86_64") == 0) i = 1;
-    if (i >= 0) {
-        /* For the default (PC-like) guest, qemudArchs[0] or [1]. */
-        r = bufferVSprintf (xml,
-                            "\
-\n\
-  <guest>\n\
-    <os_type>hvm</os_type>\n\
-    <arch name=\"%s\">\n\
-      <wordsize>%d</wordsize>\n\
-      <emulator>/usr/bin/%s</emulator>\n\
-      <domain type=\"qemu\"/>\n",
-                            qemudArchs[i].arch,
-                            qemudArchs[i].wordsize,
-                            qemudArchs[i].binary);
-        if (r == -1) goto vir_buffer_failed;
-
-        for (j = 0; qemudArchs[i].machines[j]; ++j) {
-            r = bufferVSprintf (xml,
-                                "\
-      <machine>%s</machine>\n",
-                                qemudArchs[i].machines[j]);
-            if (r == -1) goto vir_buffer_failed;
-        }
-
-        if (have_kqemu) {
-            r = bufferAdd (xml,
-                           "\
-      <domain type=\"kqemu\"/>\n", -1);
-            if (r == -1) goto vir_buffer_failed;
-        }
-        if (have_kvm) {
-            r = bufferAdd (xml,
-                           "\
-      <domain type=\"kvm\">\n\
-        <emulator>/usr/bin/qemu-kvm</emulator>\n\
-      </domain>\n", -1);
-            if (r == -1) goto vir_buffer_failed;
-        }
-        r = bufferAdd (xml,
-                       "\
-    </arch>\n\
-  </guest>\n", -1);
-        if (r == -1) goto vir_buffer_failed;
-
-        /* The "other" PC architecture needs emulation. */
-        i = i ^ 1;
-        r = bufferVSprintf (xml,
-                            "\
-\n\
-  <guest>\n\
-    <os_type>hvm</os_type>\n\
-    <arch name=\"%s\">\n\
-      <wordsize>%d</wordsize>\n\
-      <emulator>/usr/bin/%s</emulator>\n\
-      <domain type=\"qemu\"/>\n",
-                            qemudArchs[i].arch,
-                            qemudArchs[i].wordsize,
-                            qemudArchs[i].binary);
-        if (r == -1) goto vir_buffer_failed;
-        for (j = 0; qemudArchs[i].machines[j]; ++j) {
-            r = bufferVSprintf (xml,
-                                "\
-      <machine>%s</machine>\n",
-                                qemudArchs[i].machines[j]);
-            if (r == -1) goto vir_buffer_failed;
-        }
-        r = bufferAdd (xml,
-                       "\
-    </arch>\n\
-  </guest>\n", -1);
-        if (r == -1) goto vir_buffer_failed;
-    }
-
-    /* The non-PC architectures, qemudArchs[>=2]. */
-    for (i = 2; qemudArchs[i].arch; ++i) {
-        r = bufferVSprintf (xml,
-                            "\
-\n\
-  <guest>\n\
-    <os_type>hvm</os_type>\n\
-    <arch name=\"%s\">\n\
-      <wordsize>%d</wordsize>\n\
-      <emulator>/usr/bin/%s</emulator>\n\
-      <domain type=\"qemu\"/>\n",
-                            qemudArchs[i].arch,
-                            qemudArchs[i].wordsize,
-                            qemudArchs[i].binary);
-        if (r == -1) goto vir_buffer_failed;
-        for (j = 0; qemudArchs[i].machines[j]; ++j) {
-            r = bufferVSprintf (xml,
-                                "\
-      <machine>%s</machine>\n",
-                                qemudArchs[i].machines[j]);
-            if (r == -1) goto vir_buffer_failed;
-        }
-        r = bufferAdd (xml,
-                       "\
-    </arch>\n\
-  </guest>\n", -1);
-        if (r == -1) goto vir_buffer_failed;
-    }
-
-    /* Finish off. */
-    r = bufferAdd (xml,
-                      "\
-</capabilities>\n", -1);
-    if (r == -1) goto vir_buffer_failed;
-
-    /* Copy the XML into the outgoing packet, assuming it's not too large. */
-    len = strlen (xml->content);
-    if (len > QEMUD_MAX_XML_LEN) {
-        bufferFree (xml);
+    char *xml = qemudGetCapabilities(server);
+
+    if (strlen(xml) > QEMUD_MAX_XML_LEN) {
         qemudReportError (server, VIR_ERR_XML_ERROR, NULL);
         qemudDispatchFailure (server, client, out);
+        free(xml);
         return 0;
     }
+
     out->type = QEMUD_SERVER_PKT_GET_CAPABILITIES;
-    strcpy (out->qemud_packet_server_data_u.getCapabilitiesReply.xml, xml->content);
-    bufferFree (xml);
+    strcpy (out->qemud_packet_server_data_u.getCapabilitiesReply.xml, xml);
+    free(xml);
     return 0;
 }
 
diff -r 13f57e3de09e qemud/driver.c
--- a/qemud/driver.c	Sun Jun 17 16:30:02 2007 -0400
+++ b/qemud/driver.c	Sun Jun 17 16:45:46 2007 -0400
@@ -34,9 +34,11 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <errno.h>
+#include <sys/utsname.h>
 
 #include <libvirt/virterror.h>
 
+#include "buf.h"
 #include "internal.h"
 #include "driver.h"
 #include "conf.h"
@@ -119,7 +121,8 @@ int qemudMonitorCommand(struct qemud_ser
     return 0;
 }
 
-int qemudGetMemInfo(unsigned int *memory) {
+
+static int qemudGetMemInfo(unsigned int *memory) {
     FILE *meminfo = fopen("/proc/meminfo", "r");
     char line[1024];
 
@@ -139,9 +142,9 @@ int qemudGetMemInfo(unsigned int *memory
     return 0;
 }
 
-int qemudGetCPUInfo(unsigned int *cpus, unsigned int *mhz,
-                    unsigned int *nodes, unsigned int *sockets,
-                    unsigned int *cores, unsigned int *threads) {
+static int qemudGetCPUInfo(unsigned int *cpus, unsigned int *mhz,
+                           unsigned int *nodes, unsigned int *sockets,
+                           unsigned int *cores, unsigned int *threads) {
     FILE *cpuinfo = fopen("/proc/cpuinfo", "r");
     char line[1024];
 
@@ -194,6 +197,179 @@ int qemudGetCPUInfo(unsigned int *cpus, 
     return 0;
 }
 
+int qemudGetNodeInfo(unsigned int *memory,
+                     char *cpuModel, int cpuModelLength,
+                     unsigned int *cpus, unsigned int *mhz,
+                     unsigned int *nodes, unsigned int *sockets,
+                     unsigned int *cores, unsigned int *threads) {
+    struct utsname info;
+
+    if (uname(&info) < 0)
+        return -1;
+
+    strncpy(cpuModel, info.machine, cpuModelLength-1);
+    cpuModel[cpuModelLength-1] = '\0';
+
+    if (qemudGetMemInfo(memory) < 0)
+        return -1;
+
+    if (qemudGetCPUInfo(cpus, mhz, nodes, sockets, cores, threads) < 0)
+        return -1;
+    return 0;
+}
+
+char *qemudGetCapabilities(struct qemud_server *server) {
+    struct utsname utsname;
+    int i, j, r;
+    int have_kqemu = 0;
+    int have_kvm = 0;
+    bufferPtr xml;
+
+    /* Really, this never fails - look at the man-page. */
+    uname (&utsname);
+
+    have_kqemu = access ("/dev/kqemu", F_OK) == 0;
+    have_kvm = access ("/dev/kvm", F_OK) == 0;
+
+    /* Construct the XML. */
+    xml = bufferNew (1024);
+    if (!xml) {
+        qemudReportError (server, VIR_ERR_NO_MEMORY, NULL);
+        return NULL;
+    }
+
+    r = bufferVSprintf (xml,
+                        "\
+<capabilities>\n\
+  <host>\n\
+    <cpu>\n\
+      <arch>%s</arch>\n\
+    </cpu>\n\
+  </host>\n",
+                        utsname.machine);
+    if (r == -1) {
+    vir_buffer_failed:
+        bufferFree (xml);
+        qemudReportError (server, VIR_ERR_NO_MEMORY, NULL);
+        return NULL;
+    }
+
+    i = -1;
+    if (strcmp (utsname.machine, "i686") == 0) i = 0;
+    else if (strcmp (utsname.machine, "x86_64") == 0) i = 1;
+    if (i >= 0) {
+        /* For the default (PC-like) guest, qemudArchs[0] or [1]. */
+        r = bufferVSprintf (xml,
+                            "\
+\n\
+  <guest>\n\
+    <os_type>hvm</os_type>\n\
+    <arch name=\"%s\">\n\
+      <wordsize>%d</wordsize>\n\
+      <emulator>/usr/bin/%s</emulator>\n\
+      <domain type=\"qemu\"/>\n",
+                            qemudArchs[i].arch,
+                            qemudArchs[i].wordsize,
+                            qemudArchs[i].binary);
+        if (r == -1) goto vir_buffer_failed;
+
+        for (j = 0; qemudArchs[i].machines[j]; ++j) {
+            r = bufferVSprintf (xml,
+                                "\
+      <machine>%s</machine>\n",
+                                qemudArchs[i].machines[j]);
+            if (r == -1) goto vir_buffer_failed;
+        }
+
+        if (have_kqemu) {
+            r = bufferAdd (xml,
+                           "\
+      <domain type=\"kqemu\"/>\n", -1);
+            if (r == -1) goto vir_buffer_failed;
+        }
+        if (have_kvm) {
+            r = bufferAdd (xml,
+                           "\
+      <domain type=\"kvm\">\n\
+        <emulator>/usr/bin/qemu-kvm</emulator>\n\
+      </domain>\n", -1);
+            if (r == -1) goto vir_buffer_failed;
+        }
+        r = bufferAdd (xml,
+                       "\
+    </arch>\n\
+  </guest>\n", -1);
+        if (r == -1) goto vir_buffer_failed;
+
+        /* The "other" PC architecture needs emulation. */
+        i = i ^ 1;
+        r = bufferVSprintf (xml,
+                            "\
+\n\
+  <guest>\n\
+    <os_type>hvm</os_type>\n\
+    <arch name=\"%s\">\n\
+      <wordsize>%d</wordsize>\n\
+      <emulator>/usr/bin/%s</emulator>\n\
+      <domain type=\"qemu\"/>\n",
+                            qemudArchs[i].arch,
+                            qemudArchs[i].wordsize,
+                            qemudArchs[i].binary);
+        if (r == -1) goto vir_buffer_failed;
+        for (j = 0; qemudArchs[i].machines[j]; ++j) {
+            r = bufferVSprintf (xml,
+                                "\
+      <machine>%s</machine>\n",
+                                qemudArchs[i].machines[j]);
+            if (r == -1) goto vir_buffer_failed;
+        }
+        r = bufferAdd (xml,
+                       "\
+    </arch>\n\
+  </guest>\n", -1);
+        if (r == -1) goto vir_buffer_failed;
+    }
+
+    /* The non-PC architectures, qemudArchs[>=2]. */
+    for (i = 2; qemudArchs[i].arch; ++i) {
+        r = bufferVSprintf (xml,
+                            "\
+\n\
+  <guest>\n\
+    <os_type>hvm</os_type>\n\
+    <arch name=\"%s\">\n\
+      <wordsize>%d</wordsize>\n\
+      <emulator>/usr/bin/%s</emulator>\n\
+      <domain type=\"qemu\"/>\n",
+                            qemudArchs[i].arch,
+                            qemudArchs[i].wordsize,
+                            qemudArchs[i].binary);
+        if (r == -1) goto vir_buffer_failed;
+        for (j = 0; qemudArchs[i].machines[j]; ++j) {
+            r = bufferVSprintf (xml,
+                                "\
+      <machine>%s</machine>\n",
+                                qemudArchs[i].machines[j]);
+            if (r == -1) goto vir_buffer_failed;
+        }
+        r = bufferAdd (xml,
+                       "\
+    </arch>\n\
+  </guest>\n", -1);
+        if (r == -1) goto vir_buffer_failed;
+    }
+
+    /* Finish off. */
+    r = bufferAdd (xml,
+                      "\
+</capabilities>\n", -1);
+    if (r == -1) goto vir_buffer_failed;
+
+    return bufferContentAndFree(xml);
+}
+
+
+
 static int qemudGetProcessInfo(unsigned long long *cpuTime, int pid) {
     char proc[PATH_MAX];
     FILE *pidinfo;
diff -r 13f57e3de09e qemud/driver.h
--- a/qemud/driver.h	Sun Jun 17 16:30:02 2007 -0400
+++ b/qemud/driver.h	Sun Jun 17 16:44:34 2007 -0400
@@ -31,10 +31,13 @@ void qemudReportError(struct qemud_serve
                       int code, const char *fmt, ...)
     ATTRIBUTE_FORMAT(printf,3,4);
 
-int qemudGetCPUInfo(unsigned int *cpus, unsigned int *mhz,
-                    unsigned int *nodes, unsigned int *sockets,
-                    unsigned int *cores, unsigned int *threads);
-int qemudGetMemInfo(unsigned int *memory);
+int qemudGetNodeInfo(unsigned int *memory,
+                     char *cpuModel, int cpuModelLength,
+                     unsigned int *cpus, unsigned int *mhz,
+                     unsigned int *nodes, unsigned int *sockets,
+                     unsigned int *cores, unsigned int *threads);
+
+char *qemudGetCapabilities(struct qemud_server *server);
 int qemudMonitorCommand(struct qemud_server *server,
                         struct qemud_vm *vm,
                         const char *cmd,

[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]