提交 aeab0243 编写于 作者: J Jim Fehlig

libxl: refactor capabilities code

Cleanup the libxl capabilities code to be a bit more extensible,
splitting out the creation of host and guest capabilities.  This
should make it easier to implement additional capabilities in the
future, such as NUMA topology reporting.
上级 7a7cb093
...@@ -64,126 +64,62 @@ static const char *xen_cap_re = "(xen|hvm)-[[:digit:]]+\\.[[:digit:]]+-(x86_32|x ...@@ -64,126 +64,62 @@ static const char *xen_cap_re = "(xen|hvm)-[[:digit:]]+\\.[[:digit:]]+-(x86_32|x
static regex_t xen_cap_rec; static regex_t xen_cap_rec;
static virCapsPtr static int
libxlBuildCapabilities(virArch hostarch, libxlCapsInitHost(libxl_ctx *ctx, virCapsPtr caps)
int host_pae,
struct guest_arch *guest_archs,
int nr_guest_archs)
{ {
virCapsPtr caps; int err;
size_t i; libxl_physinfo phy_info;
int host_pae;
if ((caps = virCapabilitiesNew(hostarch, 1, 1)) == NULL)
goto no_memory;
if (host_pae &&
virCapabilitiesAddHostFeature(caps, "pae") < 0)
goto no_memory;
for (i = 0; i < nr_guest_archs; ++i) {
virCapsGuestPtr guest;
char const *const xen_machines[] = {guest_archs[i].hvm ? "xenfv" : "xenpv"};
virCapsGuestMachinePtr *machines;
if ((machines = virCapabilitiesAllocMachines(xen_machines, 1)) == NULL)
goto no_memory;
if ((guest = virCapabilitiesAddGuest(caps, err = regcomp(&xen_cap_rec, xen_cap_re, REG_EXTENDED);
guest_archs[i].hvm ? "hvm" : "xen", if (err != 0) {
guest_archs[i].arch, char error[100];
((hostarch == VIR_ARCH_X86_64) ? regerror(err, &xen_cap_rec, error, sizeof(error));
"/usr/lib64/xen/bin/qemu-dm" : regfree(&xen_cap_rec);
"/usr/lib/xen/bin/qemu-dm"), virReportError(VIR_ERR_INTERNAL_ERROR,
(guest_archs[i].hvm ? _("Failed to compile regex %s"), error);
"/usr/lib/xen/boot/hvmloader" : return -1;
NULL),
1,
machines)) == NULL) {
virCapabilitiesFreeMachines(machines, 1);
goto no_memory;
} }
machines = NULL;
if (virCapabilitiesAddGuestDomain(guest,
"xen",
NULL,
NULL,
0,
NULL) == NULL)
goto no_memory;
if (guest_archs[i].pae &&
virCapabilitiesAddGuestFeature(guest,
"pae",
1,
0) == NULL)
goto no_memory;
if (guest_archs[i].nonpae &&
virCapabilitiesAddGuestFeature(guest,
"nonpae",
1,
0) == NULL)
goto no_memory;
if (guest_archs[i].ia64_be &&
virCapabilitiesAddGuestFeature(guest,
"ia64_be",
1,
0) == NULL)
goto no_memory;
if (guest_archs[i].hvm) { if (libxl_get_physinfo(ctx, &phy_info) != 0) {
if (virCapabilitiesAddGuestFeature(guest, virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
"acpi", _("Failed to get node physical info from libxenlight"));
1, return -1;
1) == NULL)
goto no_memory;
if (virCapabilitiesAddGuestFeature(guest, "apic",
1,
0) == NULL)
goto no_memory;
if (virCapabilitiesAddGuestFeature(guest,
"hap",
0,
1) == NULL)
goto no_memory;
}
} }
return caps; /* hw_caps is an array of 32-bit words whose meaning is listed in
* xen-unstable.hg/xen/include/asm-x86/cpufeature.h. Each feature
* is defined in the form X*32+Y, corresponding to the Y'th bit in
* the X'th 32-bit word of hw_cap.
*/
host_pae = phy_info.hw_cap[0] & LIBXL_X86_FEATURE_PAE_MASK;
if (host_pae &&
virCapabilitiesAddHostFeature(caps, "pae") < 0)
return -1;
no_memory: return 0;
virObjectUnref(caps);
return NULL;
} }
static virCapsPtr static int
libxlMakeCapabilitiesInternal(virArch hostarch, libxlCapsInitGuests(libxl_ctx *ctx, virCapsPtr caps)
libxl_physinfo *phy_info,
char *capabilities)
{ {
const libxl_version_info *ver_info;
char *str, *token; char *str, *token;
regmatch_t subs[4]; regmatch_t subs[4];
char *saveptr = NULL; char *saveptr = NULL;
size_t i; size_t i;
virArch hostarch = caps->host.arch;
int host_pae = 0;
struct guest_arch guest_archs[32]; struct guest_arch guest_archs[32];
int nr_guest_archs = 0; int nr_guest_archs = 0;
virCapsPtr caps = NULL;
memset(guest_archs, 0, sizeof(guest_archs)); memset(guest_archs, 0, sizeof(guest_archs));
/* hw_caps is an array of 32-bit words whose meaning is listed in if ((ver_info = libxl_get_version_info(ctx)) == NULL) {
* xen-unstable.hg/xen/include/asm-x86/cpufeature.h. Each feature virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
* is defined in the form X*32+Y, corresponding to the Y'th bit in _("Failed to get version info from libxenlight"));
* the X'th 32-bit word of hw_cap. return -1;
*/ }
host_pae = phy_info->hw_cap[0] & LIBXL_X86_FEATURE_PAE_MASK;
/* Format of capabilities string is documented in the code in /* Format of capabilities string is documented in the code in
* xen-unstable.hg/xen/arch/.../setup.c. * xen-unstable.hg/xen/arch/.../setup.c.
* *
...@@ -209,7 +145,7 @@ libxlMakeCapabilitiesInternal(virArch hostarch, ...@@ -209,7 +145,7 @@ libxlMakeCapabilitiesInternal(virArch hostarch,
/* Split capabilities string into tokens. strtok_r is OK here because /* Split capabilities string into tokens. strtok_r is OK here because
* we "own" the buffer. Parse out the features from each token. * we "own" the buffer. Parse out the features from each token.
*/ */
for (str = capabilities, nr_guest_archs = 0; for (str = ver_info->capabilities, nr_guest_archs = 0;
nr_guest_archs < sizeof(guest_archs) / sizeof(guest_archs[0]) nr_guest_archs < sizeof(guest_archs) / sizeof(guest_archs[0])
&& (token = strtok_r(str, " ", &saveptr)) != NULL; && (token = strtok_r(str, " ", &saveptr)) != NULL;
str = NULL) { str = NULL) {
...@@ -273,17 +209,80 @@ libxlMakeCapabilitiesInternal(virArch hostarch, ...@@ -273,17 +209,80 @@ libxlMakeCapabilitiesInternal(virArch hostarch,
} }
} }
if ((caps = libxlBuildCapabilities(hostarch, for (i = 0; i < nr_guest_archs; ++i) {
host_pae, virCapsGuestPtr guest;
guest_archs, char const *const xen_machines[] = {guest_archs[i].hvm ? "xenfv" : "xenpv"};
nr_guest_archs)) == NULL) virCapsGuestMachinePtr *machines;
goto error;
return caps; if ((machines = virCapabilitiesAllocMachines(xen_machines, 1)) == NULL)
return -1;
error: if ((guest = virCapabilitiesAddGuest(caps,
virObjectUnref(caps); guest_archs[i].hvm ? "hvm" : "xen",
return NULL; guest_archs[i].arch,
((hostarch == VIR_ARCH_X86_64) ?
"/usr/lib64/xen/bin/qemu-dm" :
"/usr/lib/xen/bin/qemu-dm"),
(guest_archs[i].hvm ?
"/usr/lib/xen/boot/hvmloader" :
NULL),
1,
machines)) == NULL) {
virCapabilitiesFreeMachines(machines, 1);
return -1;
}
machines = NULL;
if (virCapabilitiesAddGuestDomain(guest,
"xen",
NULL,
NULL,
0,
NULL) == NULL)
return -1;
if (guest_archs[i].pae &&
virCapabilitiesAddGuestFeature(guest,
"pae",
1,
0) == NULL)
return -1;
if (guest_archs[i].nonpae &&
virCapabilitiesAddGuestFeature(guest,
"nonpae",
1,
0) == NULL)
return -1;
if (guest_archs[i].ia64_be &&
virCapabilitiesAddGuestFeature(guest,
"ia64_be",
1,
0) == NULL)
return -1;
if (guest_archs[i].hvm) {
if (virCapabilitiesAddGuestFeature(guest,
"acpi",
1,
1) == NULL)
return -1;
if (virCapabilitiesAddGuestFeature(guest, "apic",
1,
0) == NULL)
return -1;
if (virCapabilitiesAddGuestFeature(guest,
"hap",
0,
1) == NULL)
return -1;
}
}
return 0;
} }
static int static int
...@@ -873,35 +872,22 @@ error: ...@@ -873,35 +872,22 @@ error:
virCapsPtr virCapsPtr
libxlMakeCapabilities(libxl_ctx *ctx) libxlMakeCapabilities(libxl_ctx *ctx)
{ {
int err; virCapsPtr caps;
libxl_physinfo phy_info;
const libxl_version_info *ver_info;
err = regcomp(&xen_cap_rec, xen_cap_re, REG_EXTENDED); if ((caps = virCapabilitiesNew(virArchFromHost(), 1, 1)) == NULL)
if (err != 0) {
char error[100];
regerror(err, &xen_cap_rec, error, sizeof(error));
regfree(&xen_cap_rec);
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to compile regex %s"), error);
return NULL; return NULL;
}
if (libxl_get_physinfo(ctx, &phy_info) != 0) { if (libxlCapsInitHost(ctx, caps) < 0)
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", goto error;
_("Failed to get node physical info from libxenlight"));
return NULL;
}
if ((ver_info = libxl_get_version_info(ctx)) == NULL) { if (libxlCapsInitGuests(ctx, caps) < 0)
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", goto error;
_("Failed to get version info from libxenlight"));
return NULL; return caps;
}
return libxlMakeCapabilitiesInternal(virArchFromHost(), error:
&phy_info, virObjectUnref(caps);
ver_info->capabilities); return NULL;
} }
int int
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册