summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephen Hemminger <shemminger@vyatta.com>2011-06-23 16:16:33 -0700
committerStephen Hemminger <shemminger@vyatta.com>2011-06-23 16:16:33 -0700
commit0060cc59188e79ada71918b758faa40200ffe531 (patch)
treeb12b245ca2f25a442fc292672f1d51f75ee3c786
parent28c7e5cdba4add122f4a8ae57dc30f20fe28bee4 (diff)
downloadvyatta-cfg-quagga-0060cc59188e79ada71918b758faa40200ffe531.tar.gz
vyatta-cfg-quagga-0060cc59188e79ada71918b758faa40200ffe531.zip
Add better hypervisor detect logic
Bug 7215 1. Display nothing on real hardware 2. Add detection for old Xen
-rw-r--r--src/hypervisor_vendor.c67
1 files changed, 60 insertions, 7 deletions
diff --git a/src/hypervisor_vendor.c b/src/hypervisor_vendor.c
index 03ce1663..bfa78c21 100644
--- a/src/hypervisor_vendor.c
+++ b/src/hypervisor_vendor.c
@@ -6,6 +6,11 @@
#include <stdio.h>
#include <string.h>
+#include <unistd.h>
+
+#define PROC_XEN "/proc/xen"
+#define PROC_XENCAP PROC_XEN "/capabilities"
+#define PROC_PCIDEVS "/proc/bus/pci/devices"
/*
* This CPUID leaf returns the information about the hypervisor.
@@ -61,16 +66,64 @@ static const char *get_hypervisor(void)
else if (!strncmp("VMwareVMware", hyper_vendor_id, 12))
return "VMware";
else
- return "Unknown";
+ return NULL;
}
-int main(int argc, char **argv)
+static int
+has_pci_device(int vendor, int device)
{
- const char *vm = get_hypervisor();
+ FILE *f;
+ int num, fn, ven, dev;
+ int res = 1;
- if (vm) {
- printf("%s\n", vm);
+ f = fopen(PROC_PCIDEVS, "r");
+ if (!f)
return 0;
- } else
- return 1;
+
+ /* for more details about bus/pci/devices format see
+ * drivers/pci/proc.c in linux kernel
+ */
+ while(fscanf(f, "%02x%02x\t%04x%04x\t%*[^\n]",
+ &num, &fn, &ven, &dev) == 4) {
+
+ if (ven == vendor && dev == device)
+ goto found;
+ }
+
+ res = 0;
+found:
+ fclose(f);
+ return res;
+}
+
+int main(int argc, char **argv)
+{
+ const char *hvm = get_hypervisor();
+
+ if (hvm)
+ printf("%s\n", hvm);
+
+ /* Grotty code to look for old Xen */
+ else if (access(PROC_XEN, F_OK) == 0) {
+ FILE *fd = fopen(PROC_XENCAP, "r");
+ int dom0 = 0;
+
+ if (fd) {
+ char buf[256];
+
+ if (fscanf(fd, "%s", buf) == 1 &&
+ !strcmp(buf, "control_d"))
+ dom0 = 1;
+ fclose(fd);
+ }
+ printf("Xen %s\n", dom0 ? "none" : "para");
+ }
+
+ else if (has_pci_device(0x5853, 0x0001)) {
+ /* Xen full-virt on non-x86_64 */
+ printf("Xen full\n");
+ }
+
+ /* print nothing if in real mode */
+ return 0;
}