Move capabilities XML generation into driver.c

This commit is contained in:
Daniel P. Berrange 2007-06-26 19:49:50 +00:00
parent 0cb54b45f1
commit 8ebda57b2e
4 changed files with 208 additions and 184 deletions

View File

@ -1,3 +1,9 @@
Tue Jun 26 15:11:00 EST 2007 Daniel P. Berrange <berrange@rdhat.com>
* qemud/dispatch.c, qemud/driver.c, qemud/driver.h: Move
code for generating capabilities XML into driver.c file
alongside other driver APIs
Tue Jun 26 14:52:00 EST 2007 Daniel P. Berrange <berrange@rdhat.com> Tue Jun 26 14:52:00 EST 2007 Daniel P. Berrange <berrange@rdhat.com>
* qemud/event.c, qemud/event.h, qemud/Makefile.am: Generic * qemud/event.c, qemud/event.h, qemud/Makefile.am: Generic

View File

@ -28,14 +28,12 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <sys/utsname.h>
#include <libvirt/virterror.h> #include <libvirt/virterror.h>
#include "internal.h" #include "internal.h"
#include "driver.h" #include "driver.h"
#include "dispatch.h" #include "dispatch.h"
#include "conf.h" #include "conf.h"
#include "buf.h"
static int qemudDispatchFailure(struct qemud_server *server ATTRIBUTE_UNUSED, static int qemudDispatchFailure(struct qemud_server *server ATTRIBUTE_UNUSED,
@ -63,32 +61,21 @@ static int qemudDispatchGetVersion(struct qemud_server *server, struct qemud_cli
static int qemudDispatchGetNodeInfo(struct qemud_server *server, struct qemud_client *client, 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 qemud_packet_client_data *in ATTRIBUTE_UNUSED, struct qemud_packet_server_data *out) {
struct utsname info; if (qemudGetNodeInfo(&out->qemud_packet_server_data_u.getNodeInfoReply.memory,
out->qemud_packet_server_data_u.getNodeInfoReply.model,
if (uname(&info) < 0) { sizeof(out->qemud_packet_server_data_u.getNodeInfoReply.model),
if (qemudDispatchFailure(server, client, out) < 0) &out->qemud_packet_server_data_u.getNodeInfoReply.cpus,
return -1; &out->qemud_packet_server_data_u.getNodeInfoReply.mhz,
return 0; &out->qemud_packet_server_data_u.getNodeInfoReply.nodes,
} &out->qemud_packet_server_data_u.getNodeInfoReply.sockets,
&out->qemud_packet_server_data_u.getNodeInfoReply.cores,
if (qemudGetCPUInfo(&out->qemud_packet_server_data_u.getNodeInfoReply.cpus, &out->qemud_packet_server_data_u.getNodeInfoReply.threads) < 0) {
&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) if (qemudDispatchFailure(server, client, out) < 0)
return -1; return -1;
return 0; return 0;
} }
out->type = QEMUD_SERVER_PKT_GET_NODEINFO; 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'; out->qemud_packet_server_data_u.getNodeInfoReply.model[sizeof(out->qemud_packet_server_data_u.getNodeInfoReply.model)-1] = '\0';
return 0; return 0;
@ -100,166 +87,18 @@ qemudDispatchGetCapabilities (struct qemud_server *server,
struct qemud_packet_client_data *in ATTRIBUTE_UNUSED, struct qemud_packet_client_data *in ATTRIBUTE_UNUSED,
struct qemud_packet_server_data *out) struct qemud_packet_server_data *out)
{ {
struct utsname utsname; char *xml = qemudGetCapabilities(server);
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. */ if (strlen(xml) > QEMUD_MAX_XML_LEN) {
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);
qemudReportError (server, VIR_ERR_XML_ERROR, NULL); qemudReportError (server, VIR_ERR_XML_ERROR, NULL);
qemudDispatchFailure (server, client, out); qemudDispatchFailure (server, client, out);
free(xml);
return 0; return 0;
} }
out->type = QEMUD_SERVER_PKT_GET_CAPABILITIES; out->type = QEMUD_SERVER_PKT_GET_CAPABILITIES;
strcpy (out->qemud_packet_server_data_u.getCapabilitiesReply.xml, xml->content); strcpy (out->qemud_packet_server_data_u.getCapabilitiesReply.xml, xml);
bufferFree (xml); free(xml);
return 0; return 0;
} }

View File

@ -34,9 +34,11 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include <sys/utsname.h>
#include <libvirt/virterror.h> #include <libvirt/virterror.h>
#include "buf.h"
#include "internal.h" #include "internal.h"
#include "driver.h" #include "driver.h"
#include "conf.h" #include "conf.h"
@ -123,7 +125,8 @@ int qemudMonitorCommand(struct qemud_server *server ATTRIBUTE_UNUSED,
return 0; return 0;
} }
int qemudGetMemInfo(unsigned int *memory) {
static int qemudGetMemInfo(unsigned int *memory) {
FILE *meminfo = fopen("/proc/meminfo", "r"); FILE *meminfo = fopen("/proc/meminfo", "r");
char line[1024]; char line[1024];
@ -143,9 +146,9 @@ int qemudGetMemInfo(unsigned int *memory) {
return 0; return 0;
} }
int qemudGetCPUInfo(unsigned int *cpus, unsigned int *mhz, static int qemudGetCPUInfo(unsigned int *cpus, unsigned int *mhz,
unsigned int *nodes, unsigned int *sockets, unsigned int *nodes, unsigned int *sockets,
unsigned int *cores, unsigned int *threads) { unsigned int *cores, unsigned int *threads) {
FILE *cpuinfo = fopen("/proc/cpuinfo", "r"); FILE *cpuinfo = fopen("/proc/cpuinfo", "r");
char line[1024]; char line[1024];
@ -198,6 +201,179 @@ int qemudGetCPUInfo(unsigned int *cpus, unsigned int *mhz,
return 0; 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) { static int qemudGetProcessInfo(unsigned long long *cpuTime, int pid) {
char proc[PATH_MAX]; char proc[PATH_MAX];
FILE *pidinfo; FILE *pidinfo;

View File

@ -31,10 +31,13 @@ void qemudReportError(struct qemud_server *server,
int code, const char *fmt, ...) int code, const char *fmt, ...)
ATTRIBUTE_FORMAT(printf,3,4); ATTRIBUTE_FORMAT(printf,3,4);
int qemudGetCPUInfo(unsigned int *cpus, unsigned int *mhz, int qemudGetNodeInfo(unsigned int *memory,
unsigned int *nodes, unsigned int *sockets, char *cpuModel, int cpuModelLength,
unsigned int *cores, unsigned int *threads); unsigned int *cpus, unsigned int *mhz,
int qemudGetMemInfo(unsigned int *memory); unsigned int *nodes, unsigned int *sockets,
unsigned int *cores, unsigned int *threads);
char *qemudGetCapabilities(struct qemud_server *server);
int qemudMonitorCommand(struct qemud_server *server, int qemudMonitorCommand(struct qemud_server *server,
struct qemud_vm *vm, struct qemud_vm *vm,
const char *cmd, const char *cmd,