testutilsqemu.c 8.2 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
static int testQemuDefaultConsoleType(const char *ostype ATTRIBUTE_UNUSED,
59
                                      virArch arch)
60
{
61 62
    if (arch == VIR_ARCH_S390 ||
        arch == VIR_ARCH_S390X)
63 64 65
        return VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_VIRTIO;
    else
        return VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_SERIAL;
66 67
}

68 69 70 71 72 73 74 75 76 77
static int testQemuAddPPC64Guest(virCapsPtr caps)
{
    static const char *machine[] = { "pseries" };
    virCapsGuestMachinePtr *machines = NULL;
    virCapsGuestPtr guest;

    machines = virCapabilitiesAllocMachines(machine, 1);
    if (!machines)
        goto error;

78
    guest = virCapabilitiesAddGuest(caps, "hvm", VIR_ARCH_PPC64,
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
                                    "/usr/bin/qemu-system-ppc64", NULL,
                                     1, machines);
    if (!guest)
        goto error;

    if (!virCapabilitiesAddGuestDomain(guest, "qemu", NULL, NULL, 0, NULL))
        goto error;

    return 0;

error:
    /* No way to free a guest? */
    virCapabilitiesFreeMachines(machines, 1);
    return -1;
}

95 96 97 98 99 100 101 102 103 104 105
static int testQemuAddS390Guest(virCapsPtr caps)
{
    static const char *s390_machines[] = { "s390-virtio"};
    virCapsGuestMachinePtr *machines = NULL;
    virCapsGuestPtr guest;

    machines = virCapabilitiesAllocMachines(s390_machines,
                                            ARRAY_CARDINALITY(s390_machines));
    if (!machines)
        goto error;

106
    guest = virCapabilitiesAddGuest(caps, "hvm", VIR_ARCH_S390X,
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
                                    "/usr/bin/qemu-system-s390x", NULL,
                                    ARRAY_CARDINALITY(s390_machines),
                                    machines);
    if (!guest)
        goto error;

    if (!virCapabilitiesAddGuestDomain(guest, "qemu", NULL, NULL, 0, NULL))
        goto error;

    return 0;

error:
    virCapabilitiesFreeMachines(machines, ARRAY_CARDINALITY(s390_machines));
    return -1;
}

123 124 125
virCapsPtr testQemuCapsInit(void) {
    virCapsPtr caps;
    virCapsGuestPtr guest;
126 127
    virCapsGuestMachinePtr *machines = NULL;
    int nmachines = 0;
128 129 130
    static const char *const xen_machines[] = {
        "xenner"
    };
J
Jiri Denemark 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    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 */
148
        0,                      /* mode */
J
Jiri Denemark 已提交
149
        0,                      /* match */
150
        VIR_ARCH_X86_64,        /* arch */
J
Jiri Denemark 已提交
151
        (char *) "core2duo",    /* model */
152
        NULL,                   /* vendor_id */
153
        0,                      /* fallback */
J
Jiri Denemark 已提交
154
        (char *) "Intel",       /* vendor */
J
Jiri Denemark 已提交
155 156 157 158
        1,                      /* sockets */
        2,                      /* cores */
        1,                      /* threads */
        ARRAY_CARDINALITY(host_cpu_features), /* nfeatures */
E
Eric Blake 已提交
159
        ARRAY_CARDINALITY(host_cpu_features), /* nfeatures_max */
E
Eric Blake 已提交
160 161 162 163 164
        host_cpu_features,      /* features */
        0,                      /* ncells */
        0,                      /* ncells_max */
        NULL,                   /* cells */
        0,                      /* cells_cpus */
J
Jiri Denemark 已提交
165
    };
166

167
    if ((caps = virCapabilitiesNew(host_cpu.arch,
168 169 170
                                   0, 0)) == NULL)
        return NULL;

171 172
    caps->defaultConsoleTargetType = testQemuDefaultConsoleType;

J
Jiri Denemark 已提交
173 174
    if ((caps->host.cpu = virCPUDefCopy(&host_cpu)) == NULL ||
        (machines = testQemuAllocMachines(&nmachines)) == NULL)
175 176
        goto cleanup;

177
    qemuDomainSetNamespaceHooks(caps);
178

179
    if ((guest = virCapabilitiesAddGuest(caps, "hvm", VIR_ARCH_I686,
180
                                         "/usr/bin/qemu", NULL,
181 182
                                         nmachines, machines)) == NULL ||
        !virCapabilitiesAddGuestFeature(guest, "cpuselection", 1, 0))
183
        goto cleanup;
184 185
    machines = NULL;

186 187 188 189 190 191 192 193
    if (virCapabilitiesAddGuestDomain(guest,
                                      "qemu",
                                      NULL,
                                      NULL,
                                      0,
                                      NULL) == NULL)
        goto cleanup;

M
Mark McLoughlin 已提交
194
    if ((machines = testQemuAllocNewerMachines(&nmachines)) == NULL)
195 196
        goto cleanup;

197
    if ((guest = virCapabilitiesAddGuest(caps, "hvm", VIR_ARCH_X86_64,
198
                                         "/usr/bin/qemu-system-x86_64", NULL,
199 200
                                         nmachines, machines)) == NULL ||
        !virCapabilitiesAddGuestFeature(guest, "cpuselection", 1, 0))
201
        goto cleanup;
202 203
    machines = NULL;

204 205 206 207 208 209 210
    if (virCapabilitiesAddGuestDomain(guest,
                                      "qemu",
                                      NULL,
                                      NULL,
                                      0,
                                      NULL) == NULL)
        goto cleanup;
211 212 213 214

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

215 216 217 218
    if (virCapabilitiesAddGuestDomain(guest,
                                      "kvm",
                                      "/usr/bin/kvm",
                                      NULL,
219 220
                                      nmachines,
                                      machines) == NULL)
221
        goto cleanup;
222
    machines = NULL;
223

224
    nmachines = ARRAY_CARDINALITY(xen_machines);
225 226 227
    if ((machines = virCapabilitiesAllocMachines(xen_machines, nmachines)) == NULL)
        goto cleanup;

228
    if ((guest = virCapabilitiesAddGuest(caps, "xen", VIR_ARCH_X86_64,
229
                                         "/usr/bin/xenner", NULL,
230
                                         nmachines, machines)) == NULL)
231
        goto cleanup;
232 233
    machines = NULL;

234 235 236 237 238 239 240 241
    if (virCapabilitiesAddGuestDomain(guest,
                                      "kvm",
                                      "/usr/bin/kvm",
                                      NULL,
                                      0,
                                      NULL) == NULL)
        goto cleanup;

242 243 244
    if (testQemuAddPPC64Guest(caps))
        goto cleanup;

245 246 247
    if (testQemuAddS390Guest(caps))
        goto cleanup;

248
    if (virTestGetDebug()) {
249 250 251 252 253 254 255 256 257 258 259
        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);
    }

260 261 262
    return caps;

cleanup:
263
    virCapabilitiesFreeMachines(machines, nmachines);
264 265 266
    virCapabilitiesFree(caps);
    return NULL;
}
A
Atsushi SAKAI 已提交
267
#endif