testutilsqemu.c 5.9 KB
Newer Older
1
#include <config.h>
A
Atsushi SAKAI 已提交
2
#ifdef WITH_QEMU
3
# include <stdlib.h>
4

5 6 7
# include "testutilsqemu.h"
# include "testutils.h"
# include "memory.h"
J
Jiri Denemark 已提交
8
# include "cpu_conf.h"
9
# include "qemu/qemu_driver.h"
10
# include "qemu/qemu_domain.h"
11

12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
static virCapsGuestMachinePtr *testQemuAllocMachines(int *nmachines)
{
    virCapsGuestMachinePtr *machines;
    static const char *const x86_machines[] = {
        "pc", "isapc"
    };

    machines = virCapabilitiesAllocMachines(x86_machines,
                                            ARRAY_CARDINALITY(x86_machines));
    if (machines == NULL)
        return NULL;

    *nmachines = ARRAY_CARDINALITY(x86_machines);

    return machines;
}

M
Mark McLoughlin 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
/* Newer versions of qemu have versioned machine types to allow
 * compatibility with older releases.
 * The 'pc' machine type is an alias of the newest machine type.
 */
static virCapsGuestMachinePtr *testQemuAllocNewerMachines(int *nmachines)
{
    virCapsGuestMachinePtr *machines;
    char *canonical;
    static const char *const x86_machines[] = {
        "pc-0.11", "pc", "pc-0.10", "isapc"
    };

    if ((canonical = strdup(x86_machines[0])) == NULL)
        return NULL;

    machines = virCapabilitiesAllocMachines(x86_machines,
                                            ARRAY_CARDINALITY(x86_machines));
    if (machines == NULL) {
        VIR_FREE(canonical);
        return NULL;
    }

    machines[1]->canonical = canonical;

    *nmachines = ARRAY_CARDINALITY(x86_machines);

    return machines;
}

58 59 60
virCapsPtr testQemuCapsInit(void) {
    virCapsPtr caps;
    virCapsGuestPtr guest;
61 62
    virCapsGuestMachinePtr *machines = NULL;
    int nmachines = 0;
63 64 65
    static const char *const xen_machines[] = {
        "xenner"
    };
J
Jiri Denemark 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    static virCPUFeatureDef host_cpu_features[] = {
        { (char *) "lahf_lm",   -1 },
        { (char *) "xtpr",      -1 },
        { (char *) "cx16",      -1 },
        { (char *) "tm2",       -1 },
        { (char *) "est",       -1 },
        { (char *) "vmx",       -1 },
        { (char *) "ds_cpl",    -1 },
        { (char *) "pbe",       -1 },
        { (char *) "tm",        -1 },
        { (char *) "ht",        -1 },
        { (char *) "ss",        -1 },
        { (char *) "acpi",      -1 },
        { (char *) "ds",        -1 }
    };
    static virCPUDef host_cpu = {
        VIR_CPU_TYPE_HOST,      /* type */
        0,                      /* match */
        (char *) "x86_64",      /* arch */
        (char *) "core2duo",    /* model */
J
Jiri Denemark 已提交
86
        (char *) "Intel",       /* vendor */
J
Jiri Denemark 已提交
87 88 89 90
        1,                      /* sockets */
        2,                      /* cores */
        1,                      /* threads */
        ARRAY_CARDINALITY(host_cpu_features), /* nfeatures */
E
Eric Blake 已提交
91
        ARRAY_CARDINALITY(host_cpu_features), /* nfeatures_max */
J
Jiri Denemark 已提交
92 93
        host_cpu_features       /* features */
    };
94

95
    if ((caps = virCapabilitiesNew(host_cpu.arch,
96 97 98
                                   0, 0)) == NULL)
        return NULL;

J
Jiri Denemark 已提交
99 100
    if ((caps->host.cpu = virCPUDefCopy(&host_cpu)) == NULL ||
        (machines = testQemuAllocMachines(&nmachines)) == NULL)
101 102
        goto cleanup;

103
    qemuDomainSetNamespaceHooks(caps);
104

105 106
    if ((guest = virCapabilitiesAddGuest(caps, "hvm", "i686", 32,
                                         "/usr/bin/qemu", NULL,
107 108
                                         nmachines, machines)) == NULL ||
        !virCapabilitiesAddGuestFeature(guest, "cpuselection", 1, 0))
109
        goto cleanup;
110 111
    machines = NULL;

112 113 114 115 116 117 118 119
    if (virCapabilitiesAddGuestDomain(guest,
                                      "qemu",
                                      NULL,
                                      NULL,
                                      0,
                                      NULL) == NULL)
        goto cleanup;

M
Mark McLoughlin 已提交
120
    if ((machines = testQemuAllocNewerMachines(&nmachines)) == NULL)
121 122
        goto cleanup;

123 124
    if ((guest = virCapabilitiesAddGuest(caps, "hvm", "x86_64", 64,
                                         "/usr/bin/qemu-system-x86_64", NULL,
125 126
                                         nmachines, machines)) == NULL ||
        !virCapabilitiesAddGuestFeature(guest, "cpuselection", 1, 0))
127
        goto cleanup;
128 129
    machines = NULL;

130 131 132 133 134 135 136
    if (virCapabilitiesAddGuestDomain(guest,
                                      "qemu",
                                      NULL,
                                      NULL,
                                      0,
                                      NULL) == NULL)
        goto cleanup;
137 138 139 140

    if ((machines = testQemuAllocMachines(&nmachines)) == NULL)
        goto cleanup;

141 142 143 144
    if (virCapabilitiesAddGuestDomain(guest,
                                      "kvm",
                                      "/usr/bin/kvm",
                                      NULL,
145 146
                                      nmachines,
                                      machines) == NULL)
147
        goto cleanup;
148
    machines = NULL;
149

150
    nmachines = ARRAY_CARDINALITY(xen_machines);
151 152 153
    if ((machines = virCapabilitiesAllocMachines(xen_machines, nmachines)) == NULL)
        goto cleanup;

154 155
    if ((guest = virCapabilitiesAddGuest(caps, "xen", "x86_64", 64,
                                         "/usr/bin/xenner", NULL,
156
                                         nmachines, machines)) == NULL)
157
        goto cleanup;
158 159
    machines = NULL;

160 161 162 163 164 165 166 167
    if (virCapabilitiesAddGuestDomain(guest,
                                      "kvm",
                                      "/usr/bin/kvm",
                                      NULL,
                                      0,
                                      NULL) == NULL)
        goto cleanup;

168
    if (virTestGetDebug()) {
169 170 171 172 173 174 175 176 177 178 179
        char *caps_str;

        caps_str = virCapabilitiesFormatXML(caps);
        if (!caps_str)
            goto cleanup;

        fprintf(stderr, "QEMU driver capabilities:\n%s", caps_str);

        VIR_FREE(caps_str);
    }

180 181 182
    return caps;

cleanup:
183
    virCapabilitiesFreeMachines(machines, nmachines);
184 185 186
    virCapabilitiesFree(caps);
    return NULL;
}
A
Atsushi SAKAI 已提交
187
#endif