cputest.c 29.7 KB
Newer Older
1 2 3
/*
 * cputest.c: Test the libvirtd internal CPU APIs
 *
4
 * Copyright (C) 2010-2014 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library.  If not, see
O
Osier Yang 已提交
18
 * <http://www.gnu.org/licenses/>.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 *
 * Author: Jiri Denemark <jdenemar@redhat.com>
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include <sys/types.h>
#include <fcntl.h>

#include "internal.h"
34
#include "virxml.h"
35
#include "viralloc.h"
36
#include "virbuffer.h"
37 38 39 40
#include "testutils.h"
#include "cpu_conf.h"
#include "cpu/cpu.h"
#include "cpu/cpu_map.h"
41
#include "virstring.h"
42

J
Jiri Denemark 已提交
43 44 45 46 47 48
#if WITH_QEMU && WITH_YAJL
# include "testutilsqemu.h"
# include "qemumonitortestutils.h"
# include "qemu/qemu_monitor_json.h"
#endif

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#define VIR_FROM_THIS VIR_FROM_CPU

enum cpuTestBoolWithError {
    FAIL    = -1,
    NO      = 0,
    YES     = 1
};


struct data {
    const char *arch;
    const char *host;
    const char *name;
    const char **models;
    const char *modelsName;
    unsigned int nmodels;
    const char *preferred;
66
    unsigned int flags;
67 68 69
    int result;
};

J
Jiri Denemark 已提交
70 71 72 73
#if WITH_QEMU && WITH_YAJL
static virQEMUDriver driver;
#endif

74 75 76 77

static virCPUDefPtr
cpuTestLoadXML(const char *arch, const char *name)
{
78
    char *xml = NULL;
79 80 81 82
    xmlDocPtr doc = NULL;
    xmlXPathContextPtr ctxt = NULL;
    virCPUDefPtr cpu = NULL;

83 84
    if (virAsprintf(&xml, "%s/cputestdata/%s-%s.xml", abs_srcdir, arch, name) < 0)
        goto cleanup;
85

86
    if (!(doc = virXMLParseFileCtxt(xml, &ctxt)))
87 88 89 90
        goto cleanup;

    cpu = virCPUDefParseXML(ctxt->node, ctxt, VIR_CPU_TYPE_AUTO);

91
 cleanup:
92 93
    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(doc);
94
    VIR_FREE(xml);
95 96 97 98 99 100 101 102 103
    return cpu;
}


static virCPUDefPtr *
cpuTestLoadMultiXML(const char *arch,
                    const char *name,
                    unsigned int *count)
{
104
    char *xml = NULL;
105 106 107 108 109
    xmlDocPtr doc = NULL;
    xmlXPathContextPtr ctxt = NULL;
    xmlNodePtr *nodes = NULL;
    virCPUDefPtr *cpus = NULL;
    int n;
110
    size_t i;
111

112 113
    if (virAsprintf(&xml, "%s/cputestdata/%s-%s.xml", abs_srcdir, arch, name) < 0)
        goto cleanup;
114

115
    if (!(doc = virXMLParseFileCtxt(xml, &ctxt)))
J
Jiri Denemark 已提交
116
        goto cleanup;
117 118

    n = virXPathNodeSet("/cpuTest/cpu", ctxt, &nodes);
119 120
    if (n <= 0 || (VIR_ALLOC_N(cpus, n) < 0)) {
        fprintf(stderr, "\nNo /cpuTest/cpu elements found in %s\n", xml);
J
Jiri Denemark 已提交
121
        goto cleanup;
122
    }
123 124 125 126 127

    for (i = 0; i < n; i++) {
        ctxt->node = nodes[i];
        cpus[i] = virCPUDefParseXML(nodes[i], ctxt, VIR_CPU_TYPE_HOST);
        if (!cpus[i])
J
Jiri Denemark 已提交
128
            goto cleanup_cpus;
129 130 131 132
    }

    *count = n;

133
 cleanup:
134 135
    VIR_FREE(xml);
    VIR_FREE(nodes);
136 137 138 139
    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(doc);
    return cpus;

140
 cleanup_cpus:
J
Jiri Denemark 已提交
141 142 143
    for (i = 0; i < n; i++)
        virCPUDefFree(cpus[i]);
    VIR_FREE(cpus);
144 145 146 147 148 149
    goto cleanup;
}


static int
cpuTestCompareXML(const char *arch,
E
Eric Blake 已提交
150
                  virCPUDef *cpu,
151
                  const char *name,
152
                  bool updateCPU)
153
{
154
    char *xml = NULL;
155 156 157
    char *actual = NULL;
    int ret = -1;

158 159 160
    if (virAsprintf(&xml, "%s/cputestdata/%s-%s.xml",
                    abs_srcdir, arch, name) < 0)
        goto cleanup;
161

162
    if (!(actual = virCPUDefFormat(cpu, NULL, updateCPU)))
163 164
        goto cleanup;

165
    if (virTestCompareToFile(actual, xml) < 0)
166 167 168 169
        goto cleanup;

    ret = 0;

170
 cleanup:
171 172
    VIR_FREE(xml);
    VIR_FREE(actual);
173 174 175 176 177 178 179 180 181 182 183 184
    return ret;
}


static const char *
cpuTestCompResStr(virCPUCompareResult result)
{
    switch (result) {
    case VIR_CPU_COMPARE_ERROR:         return "ERROR";
    case VIR_CPU_COMPARE_INCOMPATIBLE:  return "INCOMPATIBLE";
    case VIR_CPU_COMPARE_IDENTICAL:     return "IDENTICAL";
    case VIR_CPU_COMPARE_SUPERSET:      return "SUPERSET";
185
    case VIR_CPU_COMPARE_LAST:          break;
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    }

    return "unknown";
}


static const char *
cpuTestBoolWithErrorStr(enum cpuTestBoolWithError result)
{
    switch (result) {
    case FAIL:  return "FAIL";
    case NO:    return "NO";
    case YES:   return "YES";
    }

    return "unknown";
}


static int
cpuTestCompare(const void *arg)
{
    const struct data *data = arg;
    int ret = -1;
    virCPUDefPtr host = NULL;
    virCPUDefPtr cpu = NULL;
    virCPUCompareResult result;

    if (!(host = cpuTestLoadXML(data->arch, data->host)) ||
        !(cpu = cpuTestLoadXML(data->arch, data->name)))
        goto cleanup;

218
    result = cpuCompare(host, cpu, false);
219 220 221 222
    if (data->result == VIR_CPU_COMPARE_ERROR)
        virResetLastError();

    if (data->result != result) {
223
        VIR_TEST_VERBOSE("\nExpected result %s, got %s\n",
224 225
                    cpuTestCompResStr(data->result),
                    cpuTestCompResStr(result));
226 227
        /* Pad to line up with test name ... in virTestRun */
        VIR_TEST_VERBOSE("%74s", "... ");
228 229 230 231 232
        goto cleanup;
    }

    ret = 0;

233
 cleanup:
234 235 236 237 238 239 240 241 242 243
    virCPUDefFree(host);
    virCPUDefFree(cpu);
    return ret;
}


static int
cpuTestGuestData(const void *arg)
{
    const struct data *data = arg;
244
    int ret = -2;
245 246 247
    virCPUDefPtr host = NULL;
    virCPUDefPtr cpu = NULL;
    virCPUDefPtr guest = NULL;
248
    virCPUDataPtr guestData = NULL;
249 250 251 252 253 254 255 256
    virCPUCompareResult cmpResult;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    char *result = NULL;

    if (!(host = cpuTestLoadXML(data->arch, data->host)) ||
        !(cpu = cpuTestLoadXML(data->arch, data->name)))
        goto cleanup;

257
    cmpResult = cpuGuestData(host, cpu, &guestData, NULL);
258
    if (cmpResult == VIR_CPU_COMPARE_ERROR ||
259 260
        cmpResult == VIR_CPU_COMPARE_INCOMPATIBLE) {
        ret = -1;
261
        goto cleanup;
262
    }
263

264
    if (VIR_ALLOC(guest) < 0)
265 266
        goto cleanup;

267
    guest->arch = host->arch;
268 269
    guest->type = VIR_CPU_TYPE_GUEST;
    guest->match = VIR_CPU_MATCH_EXACT;
270
    guest->fallback = cpu->fallback;
271 272
    if (cpuDecode(guest, guestData, data->models,
                  data->nmodels, data->preferred) < 0) {
273
        ret = -1;
274 275 276
        goto cleanup;
    }

277
    virBufferAsprintf(&buf, "%s+%s", data->host, data->name);
278
    if (data->nmodels)
279
        virBufferAsprintf(&buf, ",%s", data->modelsName);
280
    if (data->preferred)
281
        virBufferAsprintf(&buf, ",%s", data->preferred);
282 283 284 285 286 287 288 289
    virBufferAddLit(&buf, "-result");

    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        goto cleanup;
    }
    result = virBufferContentAndReset(&buf);

290 291 292 293
    if (cpuTestCompareXML(data->arch, guest, result, false) < 0)
        goto cleanup;

    ret = 0;
294

295
 cleanup:
296
    VIR_FREE(result);
J
Jiri Denemark 已提交
297
    cpuDataFree(guestData);
298 299 300
    virCPUDefFree(host);
    virCPUDefFree(cpu);
    virCPUDefFree(guest);
301 302 303 304 305 306 307 308 309 310 311 312 313 314

    if (ret == data->result) {
        /* We got the result we expected, whether it was
         * a success or a failure */
        virResetLastError();
        ret = 0;
    } else {
        VIR_TEST_VERBOSE("\nExpected result %d, got %d\n",
                         data->result, ret);
        /* Pad to line up with test name ... in virTestRun */
        VIR_TEST_VERBOSE("%74s", "... ");
        ret = -1;
    }

315 316 317 318 319 320 321 322 323 324 325 326
    return ret;
}


static int
cpuTestBaseline(const void *arg)
{
    const struct data *data = arg;
    int ret = -1;
    virCPUDefPtr *cpus = NULL;
    virCPUDefPtr baseline = NULL;
    unsigned int ncpus = 0;
327
    char *result = NULL;
328
    const char *suffix;
329
    size_t i;
330 331 332 333

    if (!(cpus = cpuTestLoadMultiXML(data->arch, data->name, &ncpus)))
        goto cleanup;

334
    baseline = cpuBaseline(cpus, ncpus, NULL, 0, data->flags);
335 336
    if (data->result < 0) {
        virResetLastError();
337
        if (!baseline) {
338
            ret = 0;
339 340
        } else {
            VIR_TEST_VERBOSE("\n%-70s... ",
341 342 343 344 345 346 347
                    "cpuBaseline was expected to fail but it succeeded");
        }
        goto cleanup;
    }
    if (!baseline)
        goto cleanup;

348 349
    if (data->flags & VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES)
        suffix = "expanded";
350 351
    else if (data->flags & VIR_CONNECT_BASELINE_CPU_MIGRATABLE)
        suffix = "migratable";
352 353 354
    else
        suffix = "result";
    if (virAsprintf(&result, "%s-%s", data->name, suffix) < 0)
355 356
        goto cleanup;

357
    if (cpuTestCompareXML(data->arch, baseline, result, false) < 0)
358 359 360 361 362
        goto cleanup;

    for (i = 0; i < ncpus; i++) {
        virCPUCompareResult cmp;

363
        cmp = cpuCompare(cpus[i], baseline, false);
364 365
        if (cmp != VIR_CPU_COMPARE_SUPERSET &&
            cmp != VIR_CPU_COMPARE_IDENTICAL) {
366 367 368
            VIR_TEST_VERBOSE("\nbaseline CPU is incompatible with CPU %zu\n",
                             i);
            VIR_TEST_VERBOSE("%74s", "... ");
369 370 371 372 373 374 375
            ret = -1;
            goto cleanup;
        }
    }

    ret = 0;

376
 cleanup:
377 378 379
    if (cpus) {
        for (i = 0; i < ncpus; i++)
            virCPUDefFree(cpus[i]);
380
        VIR_FREE(cpus);
381 382
    }
    virCPUDefFree(baseline);
383
    VIR_FREE(result);
384 385 386 387 388 389 390 391 392 393 394
    return ret;
}


static int
cpuTestUpdate(const void *arg)
{
    const struct data *data = arg;
    int ret = -1;
    virCPUDefPtr host = NULL;
    virCPUDefPtr cpu = NULL;
395
    char *result = NULL;
396 397 398 399 400

    if (!(host = cpuTestLoadXML(data->arch, data->host)) ||
        !(cpu = cpuTestLoadXML(data->arch, data->name)))
        goto cleanup;

J
Jiri Denemark 已提交
401
    if (virCPUUpdate(host->arch, cpu, host) < 0)
402 403
        goto cleanup;

404 405 406
    if (virAsprintf(&result, "%s+%s", data->host, data->name) < 0)
        goto cleanup;

407
    ret = cpuTestCompareXML(data->arch, cpu, result, true);
408

409
 cleanup:
410 411
    virCPUDefFree(host);
    virCPUDefFree(cpu);
412
    VIR_FREE(result);
413 414 415 416 417 418 419 420 421 422
    return ret;
}


static int
cpuTestHasFeature(const void *arg)
{
    const struct data *data = arg;
    int ret = -1;
    virCPUDefPtr host = NULL;
423
    virCPUDataPtr hostData = NULL;
424 425 426 427 428 429 430 431 432
    int result;

    if (!(host = cpuTestLoadXML(data->arch, data->host)))
        goto cleanup;

    if (cpuEncode(host->arch, host, NULL, &hostData,
                  NULL, NULL, NULL, NULL) < 0)
        goto cleanup;

433
    result = virCPUDataCheckFeature(hostData, data->name);
434 435 436 437
    if (data->result == -1)
        virResetLastError();

    if (data->result != result) {
438 439 440 441 442
        VIR_TEST_VERBOSE("\nExpected result %s, got %s\n",
            cpuTestBoolWithErrorStr(data->result),
            cpuTestBoolWithErrorStr(result));
        /* Pad to line up with test name ... in virTestRun */
        VIR_TEST_VERBOSE("%74s", "... ");
443 444 445 446 447
        goto cleanup;
    }

    ret = 0;

448
 cleanup:
J
Jiri Denemark 已提交
449
    cpuDataFree(hostData);
450 451 452 453 454
    virCPUDefFree(host);
    return ret;
}


J
Jiri Denemark 已提交
455
static int
456
cpuTestCPUID(bool guest, const void *arg)
J
Jiri Denemark 已提交
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
{
    const struct data *data = arg;
    int ret = -1;
    virCPUDataPtr hostData = NULL;
    char *hostFile = NULL;
    char *host = NULL;
    virCPUDefPtr cpu = NULL;
    char *result = NULL;

    if (virAsprintf(&hostFile, "%s/cputestdata/%s-cpuid-%s.xml",
                    abs_srcdir, data->arch, data->host) < 0)
        goto cleanup;

    if (virTestLoadFile(hostFile, &host) < 0 ||
        !(hostData = cpuDataParse(host)))
        goto cleanup;

    if (VIR_ALLOC(cpu) < 0)
        goto cleanup;

    cpu->arch = hostData->arch;
478
    if (guest) {
J
Jiri Denemark 已提交
479 480 481 482 483 484 485 486 487 488 489 490
        cpu->type = VIR_CPU_TYPE_GUEST;
        cpu->match = VIR_CPU_MATCH_EXACT;
        cpu->fallback = VIR_CPU_FALLBACK_FORBID;
    } else {
        cpu->type = VIR_CPU_TYPE_HOST;
    }

    if (cpuDecode(cpu, hostData, NULL, 0, NULL) < 0)
        goto cleanup;

    if (virAsprintf(&result, "cpuid-%s-%s",
                    data->host,
491
                    guest ? "guest" : "host") < 0)
J
Jiri Denemark 已提交
492 493 494 495 496 497 498 499 500 501 502 503 504 505
        goto cleanup;

    ret = cpuTestCompareXML(data->arch, cpu, result, false);

 cleanup:
    VIR_FREE(hostFile);
    VIR_FREE(host);
    cpuDataFree(hostData);
    virCPUDefFree(cpu);
    VIR_FREE(result);
    return ret;
}


506 507 508 509 510 511 512 513 514 515 516 517 518 519
static int
cpuTestHostCPUID(const void *arg)
{
    return cpuTestCPUID(false, arg);
}


static int
cpuTestGuestCPUID(const void *arg)
{
    return cpuTestCPUID(true, arg);
}


J
Jiri Denemark 已提交
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
#if WITH_QEMU && WITH_YAJL
static int
cpuTestJSONCPUID(const void *arg)
{
    const struct data *data = arg;
    virCPUDataPtr cpuData = NULL;
    virCPUDefPtr cpu = NULL;
    qemuMonitorTestPtr testMon = NULL;
    char *json = NULL;
    char *result = NULL;
    int ret = -1;

    if (virAsprintf(&json, "%s/cputestdata/%s-cpuid-%s.json",
                    abs_srcdir, data->arch, data->host) < 0 ||
        virAsprintf(&result, "cpuid-%s-json", data->host) < 0)
        goto cleanup;

    if (!(testMon = qemuMonitorTestNewFromFile(json, driver.xmlopt, true)))
        goto cleanup;

    if (qemuMonitorJSONGetCPUx86Data(qemuMonitorTestGetMonitor(testMon),
                                     "feature-words", &cpuData) < 0)
        goto cleanup;

    if (VIR_ALLOC(cpu) < 0)
        goto cleanup;

    cpu->arch = cpuData->arch;
    cpu->type = VIR_CPU_TYPE_GUEST;
    cpu->match = VIR_CPU_MATCH_EXACT;
    cpu->fallback = VIR_CPU_FALLBACK_FORBID;

    if (cpuDecode(cpu, cpuData, NULL, 0, NULL) < 0)
        goto cleanup;

    ret = cpuTestCompareXML(data->arch, cpu, result, false);

 cleanup:
    qemuMonitorTestFree(testMon);
    cpuDataFree(cpuData);
    virCPUDefFree(cpu);
    VIR_FREE(result);
    VIR_FREE(json);
    return ret;
}
#endif


568 569 570
static const char *model486[]   = { "486" };
static const char *nomodel[]    = { "nomodel" };
static const char *models[]     = { "qemu64", "core2duo", "Nehalem" };
571
static const char *haswell[]    = { "SandyBridge", "Haswell" };
572
static const char *ppc_models[] = { "POWER6", "POWER7", "POWER8" };
573 574

static int
E
Eric Blake 已提交
575
mymain(void)
576 577 578
{
    int ret = 0;

J
Jiri Denemark 已提交
579 580 581 582 583 584 585
#if WITH_QEMU && WITH_YAJL
    if (qemuTestDriverInit(&driver) < 0)
        return EXIT_FAILURE;

    virEventRegisterDefaultImpl();
#endif

586
#define DO_TEST(arch, api, name, host, cpu,                             \
587
                models, nmodels, preferred, flags, result)              \
588
    do {                                                                \
589 590
        struct data data = {                                            \
            arch, host, cpu, models,                                    \
591
            models == NULL ? NULL : #models,                            \
592
            nmodels, preferred, flags, result                           \
593
        };                                                              \
594 595 596 597 598 599 600 601
        char *testLabel;                                                \
        char *tmp;                                                      \
                                                                        \
        tmp = virTestLogContentAndReset();                              \
        VIR_FREE(tmp);                                                  \
                                                                        \
        if (virAsprintf(&testLabel, "%s(%s): %s",                       \
                        #api, arch, name) < 0) {                        \
602
            ret = -1;                                                   \
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
            break;                                                      \
        }                                                               \
                                                                        \
        if (virTestRun(testLabel, api, &data) < 0) {                    \
            if (virTestGetDebug()) {                                    \
                char *log;                                              \
                if ((log = virTestLogContentAndReset()) &&              \
                     strlen(log) > 0)                                   \
                    VIR_TEST_DEBUG("\n%s\n", log);                      \
                VIR_FREE(log);                                          \
            }                                                           \
            ret = -1;                                                   \
        }                                                               \
                                                                        \
        VIR_FREE(testLabel);                                            \
618 619 620
    } while (0)

#define DO_TEST_COMPARE(arch, host, cpu, result)                        \
621
    DO_TEST(arch, cpuTestCompare,                                       \
622
            host "/" cpu " (" #result ")",                              \
623
            host, cpu, NULL, 0, NULL, 0, result)
624

J
Jiri Denemark 已提交
625 626 627 628 629
#define DO_TEST_UPDATE_ONLY(arch, host, cpu)                            \
    DO_TEST(arch, cpuTestUpdate,                                        \
            cpu " on " host,                                            \
            host, cpu, NULL, 0, NULL, 0, 0)                             \

630 631
#define DO_TEST_UPDATE(arch, host, cpu, result)                         \
    do {                                                                \
J
Jiri Denemark 已提交
632
        DO_TEST_UPDATE_ONLY(arch, host, cpu);                           \
633 634 635
        DO_TEST_COMPARE(arch, host, host "+" cpu, result);              \
    } while (0)

636
#define DO_TEST_BASELINE(arch, name, flags, result)                     \
637 638 639 640 641
    do {                                                                \
        const char *suffix = "";                                        \
        char *label;                                                    \
        if ((flags) & VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES)         \
            suffix = " (expanded)";                                     \
642 643
        if ((flags) & VIR_CONNECT_BASELINE_CPU_MIGRATABLE)              \
            suffix = " (migratable)";                                   \
644 645 646
        if (virAsprintf(&label, "%s%s", name, suffix) < 0) {            \
            ret = -1;                                                   \
        } else {                                                        \
647 648
            DO_TEST(arch, cpuTestBaseline, label, NULL,                 \
                    "baseline-" name, NULL, 0, NULL, flags, result);    \
649 650 651
        }                                                               \
        VIR_FREE(label);                                                \
    } while (0)
652 653

#define DO_TEST_HASFEATURE(arch, host, feature, result)                 \
654
    DO_TEST(arch, cpuTestHasFeature,                                    \
655
            host "/" feature " (" #result ")",                          \
656
            host, feature, NULL, 0, NULL, 0, result)
657 658

#define DO_TEST_GUESTDATA(arch, host, cpu, models, preferred, result)   \
659
    DO_TEST(arch, cpuTestGuestData,                                     \
660 661 662
            host "/" cpu " (" #models ", pref=" #preferred ")",         \
            host, cpu, models,                                          \
            models == NULL ? 0 : sizeof(models) / sizeof(char *),       \
663
            preferred, 0, result)
664

J
Jiri Denemark 已提交
665 666 667 668
#if WITH_QEMU && WITH_YAJL
# define DO_TEST_CPUID_JSON(arch, host, json)                           \
    do {                                                                \
        if (json) {                                                     \
669
            DO_TEST(arch, cpuTestJSONCPUID, host, host,                 \
J
Jiri Denemark 已提交
670 671 672 673
                    NULL, NULL, 0, NULL, 0, 0);                         \
        }                                                               \
    } while (0)
#else
674
# define DO_TEST_CPUID_JSON(arch, host, json)
J
Jiri Denemark 已提交
675 676 677 678
#endif

#define DO_TEST_CPUID(arch, host, json)                                 \
    do {                                                                \
679
        DO_TEST(arch, cpuTestHostCPUID, host, host,                     \
J
Jiri Denemark 已提交
680
                NULL, NULL, 0, NULL, 0, 0);                             \
681
        DO_TEST(arch, cpuTestGuestCPUID, host, host,                    \
J
Jiri Denemark 已提交
682 683 684 685
                NULL, NULL, 0, NULL, 0, 0);                             \
        DO_TEST_CPUID_JSON(arch, host, json);                           \
    } while (0)

686
    /* host to host comparison */
687 688 689 690 691 692 693
    DO_TEST_COMPARE("x86", "host", "host", VIR_CPU_COMPARE_IDENTICAL);
    DO_TEST_COMPARE("x86", "host", "host-better", VIR_CPU_COMPARE_INCOMPATIBLE);
    DO_TEST_COMPARE("x86", "host", "host-worse", VIR_CPU_COMPARE_SUPERSET);
    DO_TEST_COMPARE("x86", "host", "host-amd-fake", VIR_CPU_COMPARE_INCOMPATIBLE);
    DO_TEST_COMPARE("x86", "host", "host-incomp-arch", VIR_CPU_COMPARE_INCOMPATIBLE);
    DO_TEST_COMPARE("x86", "host", "host-no-vendor", VIR_CPU_COMPARE_IDENTICAL);
    DO_TEST_COMPARE("x86", "host-no-vendor", "host", VIR_CPU_COMPARE_INCOMPATIBLE);
694

695 696 697 698 699 700 701
    DO_TEST_COMPARE("ppc64", "host", "host", VIR_CPU_COMPARE_IDENTICAL);
    DO_TEST_COMPARE("ppc64", "host", "host-better", VIR_CPU_COMPARE_INCOMPATIBLE);
    DO_TEST_COMPARE("ppc64", "host", "host-worse", VIR_CPU_COMPARE_INCOMPATIBLE);
    DO_TEST_COMPARE("ppc64", "host", "host-incomp-arch", VIR_CPU_COMPARE_INCOMPATIBLE);
    DO_TEST_COMPARE("ppc64", "host", "host-no-vendor", VIR_CPU_COMPARE_IDENTICAL);
    DO_TEST_COMPARE("ppc64", "host-no-vendor", "host", VIR_CPU_COMPARE_INCOMPATIBLE);

702
    /* guest to host comparison */
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
    DO_TEST_COMPARE("x86", "host", "bogus-model", VIR_CPU_COMPARE_ERROR);
    DO_TEST_COMPARE("x86", "host", "bogus-feature", VIR_CPU_COMPARE_ERROR);
    DO_TEST_COMPARE("x86", "host", "min", VIR_CPU_COMPARE_SUPERSET);
    DO_TEST_COMPARE("x86", "host", "pentium3", VIR_CPU_COMPARE_SUPERSET);
    DO_TEST_COMPARE("x86", "host", "exact", VIR_CPU_COMPARE_SUPERSET);
    DO_TEST_COMPARE("x86", "host", "exact-forbid", VIR_CPU_COMPARE_INCOMPATIBLE);
    DO_TEST_COMPARE("x86", "host", "exact-forbid-extra", VIR_CPU_COMPARE_SUPERSET);
    DO_TEST_COMPARE("x86", "host", "exact-disable", VIR_CPU_COMPARE_SUPERSET);
    DO_TEST_COMPARE("x86", "host", "exact-disable2", VIR_CPU_COMPARE_SUPERSET);
    DO_TEST_COMPARE("x86", "host", "exact-disable-extra", VIR_CPU_COMPARE_SUPERSET);
    DO_TEST_COMPARE("x86", "host", "exact-require", VIR_CPU_COMPARE_SUPERSET);
    DO_TEST_COMPARE("x86", "host", "exact-require-extra", VIR_CPU_COMPARE_INCOMPATIBLE);
    DO_TEST_COMPARE("x86", "host", "exact-force", VIR_CPU_COMPARE_SUPERSET);
    DO_TEST_COMPARE("x86", "host", "strict", VIR_CPU_COMPARE_INCOMPATIBLE);
    DO_TEST_COMPARE("x86", "host", "strict-full", VIR_CPU_COMPARE_IDENTICAL);
    DO_TEST_COMPARE("x86", "host", "strict-disable", VIR_CPU_COMPARE_IDENTICAL);
    DO_TEST_COMPARE("x86", "host", "strict-force-extra", VIR_CPU_COMPARE_IDENTICAL);
    DO_TEST_COMPARE("x86", "host", "guest", VIR_CPU_COMPARE_SUPERSET);
    DO_TEST_COMPARE("x86", "host", "pentium3-amd", VIR_CPU_COMPARE_INCOMPATIBLE);
    DO_TEST_COMPARE("x86", "host-amd", "pentium3-amd", VIR_CPU_COMPARE_SUPERSET);
723
    DO_TEST_COMPARE("x86", "host-worse", "penryn-force", VIR_CPU_COMPARE_IDENTICAL);
724
    DO_TEST_COMPARE("x86", "host-SandyBridge", "exact-force-Haswell", VIR_CPU_COMPARE_IDENTICAL);
725

726 727
    DO_TEST_COMPARE("ppc64", "host", "guest-strict", VIR_CPU_COMPARE_IDENTICAL);
    DO_TEST_COMPARE("ppc64", "host", "guest-exact", VIR_CPU_COMPARE_INCOMPATIBLE);
728 729 730
    DO_TEST_COMPARE("ppc64", "host", "guest-legacy", VIR_CPU_COMPARE_IDENTICAL);
    DO_TEST_COMPARE("ppc64", "host", "guest-legacy-incompatible", VIR_CPU_COMPARE_INCOMPATIBLE);
    DO_TEST_COMPARE("ppc64", "host", "guest-legacy-invalid", VIR_CPU_COMPARE_ERROR);
731 732 733 734
    DO_TEST_COMPARE("ppc64", "host", "guest-compat-none", VIR_CPU_COMPARE_IDENTICAL);
    DO_TEST_COMPARE("ppc64", "host", "guest-compat-valid", VIR_CPU_COMPARE_IDENTICAL);
    DO_TEST_COMPARE("ppc64", "host", "guest-compat-invalid", VIR_CPU_COMPARE_ERROR);
    DO_TEST_COMPARE("ppc64", "host", "guest-compat-incompatible", VIR_CPU_COMPARE_INCOMPATIBLE);
735

736 737
    /* guest updates for migration
     * automatically compares host CPU with the result */
738 739 740 741 742
    DO_TEST_UPDATE("x86", "host", "min", VIR_CPU_COMPARE_IDENTICAL);
    DO_TEST_UPDATE("x86", "host", "pentium3", VIR_CPU_COMPARE_IDENTICAL);
    DO_TEST_UPDATE("x86", "host", "guest", VIR_CPU_COMPARE_SUPERSET);
    DO_TEST_UPDATE("x86", "host", "host-model", VIR_CPU_COMPARE_IDENTICAL);
    DO_TEST_UPDATE("x86", "host", "host-model-nofallback", VIR_CPU_COMPARE_IDENTICAL);
743
    DO_TEST_UPDATE("x86", "host-invtsc", "host-model", VIR_CPU_COMPARE_SUPERSET);
J
Jiri Denemark 已提交
744 745
    DO_TEST_UPDATE_ONLY("x86", "host", "host-passthrough");
    DO_TEST_UPDATE_ONLY("x86", "host", "host-passthrough-features");
746

747 748 749 750 751 752 753 754 755 756
    DO_TEST_UPDATE("ppc64", "host", "guest", VIR_CPU_COMPARE_IDENTICAL);
    DO_TEST_UPDATE("ppc64", "host", "guest-nofallback", VIR_CPU_COMPARE_INCOMPATIBLE);
    DO_TEST_UPDATE("ppc64", "host", "guest-legacy", VIR_CPU_COMPARE_IDENTICAL);
    DO_TEST_UPDATE("ppc64", "host", "guest-legacy-incompatible", VIR_CPU_COMPARE_INCOMPATIBLE);
    DO_TEST_UPDATE("ppc64", "host", "guest-legacy-invalid", VIR_CPU_COMPARE_ERROR);
    DO_TEST_UPDATE("ppc64", "host", "guest-compat-none", VIR_CPU_COMPARE_IDENTICAL);
    DO_TEST_UPDATE("ppc64", "host", "guest-compat-valid", VIR_CPU_COMPARE_IDENTICAL);
    DO_TEST_UPDATE("ppc64", "host", "guest-compat-invalid", VIR_CPU_COMPARE_ERROR);
    DO_TEST_UPDATE("ppc64", "host", "guest-compat-incompatible", VIR_CPU_COMPARE_INCOMPATIBLE);

757
    /* computing baseline CPUs */
758 759 760 761 762
    DO_TEST_BASELINE("x86", "incompatible-vendors", 0, -1);
    DO_TEST_BASELINE("x86", "no-vendor", 0, 0);
    DO_TEST_BASELINE("x86", "some-vendors", 0, 0);
    DO_TEST_BASELINE("x86", "1", 0, 0);
    DO_TEST_BASELINE("x86", "2", 0, 0);
763
    DO_TEST_BASELINE("x86", "3", 0, 0);
764
    DO_TEST_BASELINE("x86", "3", VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES, 0);
765 766 767 768
    DO_TEST_BASELINE("x86", "4", 0, 0);
    DO_TEST_BASELINE("x86", "4", VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES, 0);
    DO_TEST_BASELINE("x86", "5", 0, 0);
    DO_TEST_BASELINE("x86", "5", VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES, 0);
769 770
    DO_TEST_BASELINE("x86", "6", 0, 0);
    DO_TEST_BASELINE("x86", "6", VIR_CONNECT_BASELINE_CPU_MIGRATABLE, 0);
771 772
    DO_TEST_BASELINE("x86", "7", 0, 0);
    DO_TEST_BASELINE("x86", "8", 0, 0);
773

774 775
    DO_TEST_BASELINE("ppc64", "incompatible-vendors", 0, -1);
    DO_TEST_BASELINE("ppc64", "no-vendor", 0, 0);
776 777 778
    DO_TEST_BASELINE("ppc64", "incompatible-models", 0, -1);
    DO_TEST_BASELINE("ppc64", "same-model", 0, 0);
    DO_TEST_BASELINE("ppc64", "legacy", 0, -1);
779

780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
    /* CPU features */
    DO_TEST_HASFEATURE("x86", "host", "vmx", YES);
    DO_TEST_HASFEATURE("x86", "host", "lm", YES);
    DO_TEST_HASFEATURE("x86", "host", "sse4.1", YES);
    DO_TEST_HASFEATURE("x86", "host", "3dnowext", NO);
    DO_TEST_HASFEATURE("x86", "host", "skinit", NO);
    DO_TEST_HASFEATURE("x86", "host", "foo", FAIL);

    /* computing guest data and decoding the data into a guest CPU XML */
    DO_TEST_GUESTDATA("x86", "host", "guest", NULL, NULL, 0);
    DO_TEST_GUESTDATA("x86", "host-better", "pentium3", NULL, NULL, 0);
    DO_TEST_GUESTDATA("x86", "host-better", "pentium3", NULL, "pentium3", 0);
    DO_TEST_GUESTDATA("x86", "host-better", "pentium3", NULL, "core2duo", 0);
    DO_TEST_GUESTDATA("x86", "host-worse", "guest", NULL, NULL, 0);
    DO_TEST_GUESTDATA("x86", "host", "strict-force-extra", NULL, NULL, 0);
795
    DO_TEST_GUESTDATA("x86", "host", "penryn-force", NULL, NULL, 0);
796 797 798 799 800
    DO_TEST_GUESTDATA("x86", "host", "guest", model486, NULL, 0);
    DO_TEST_GUESTDATA("x86", "host", "guest", models, NULL, 0);
    DO_TEST_GUESTDATA("x86", "host", "guest", models, "Penryn", 0);
    DO_TEST_GUESTDATA("x86", "host", "guest", models, "qemu64", 0);
    DO_TEST_GUESTDATA("x86", "host", "guest", nomodel, NULL, -1);
801
    DO_TEST_GUESTDATA("x86", "host", "guest-nofallback", models, "Penryn", -1);
802 803 804
    DO_TEST_GUESTDATA("x86", "host", "host+host-model", models, "Penryn", 0);
    DO_TEST_GUESTDATA("x86", "host", "host+host-model-nofallback",
                      models, "Penryn", -1);
805 806 807 808 809 810 811 812
    DO_TEST_GUESTDATA("x86", "host-Haswell-noTSX", "Haswell",
                      haswell, "Haswell", 0);
    DO_TEST_GUESTDATA("x86", "host-Haswell-noTSX", "Haswell-noTSX",
                      haswell, "Haswell-noTSX", 0);
    DO_TEST_GUESTDATA("x86", "host-Haswell-noTSX", "Haswell-noTSX-nofallback",
                      haswell, "Haswell-noTSX", -1);
    DO_TEST_GUESTDATA("x86", "host-Haswell-noTSX", "Haswell-noTSX",
                      NULL, "Haswell-noTSX", 0);
813

814
    DO_TEST_GUESTDATA("ppc64", "host", "guest", ppc_models, NULL, 0);
815
    DO_TEST_GUESTDATA("ppc64", "host", "guest-nofallback", ppc_models, "POWER8", -1);
816 817 818
    DO_TEST_GUESTDATA("ppc64", "host", "guest-legacy", ppc_models, NULL, 0);
    DO_TEST_GUESTDATA("ppc64", "host", "guest-legacy-incompatible", ppc_models, NULL, -1);
    DO_TEST_GUESTDATA("ppc64", "host", "guest-legacy-invalid", ppc_models, NULL, -1);
819

820
    DO_TEST_CPUID("x86", "A10-5800K", true);
821
    DO_TEST_CPUID("x86", "Atom-D510", false);
822
    DO_TEST_CPUID("x86", "Atom-N450", false);
823
    DO_TEST_CPUID("x86", "Core-i5-2500", true);
824
    DO_TEST_CPUID("x86", "Core-i5-2540M", true);
825
    DO_TEST_CPUID("x86", "Core-i5-4670T", true);
826
    DO_TEST_CPUID("x86", "Core-i5-6600", true);
827
    DO_TEST_CPUID("x86", "Core-i7-2600", true);
828
    DO_TEST_CPUID("x86", "Core-i7-3520M", false);
829
    DO_TEST_CPUID("x86", "Core-i7-3740QM", true);
830
    DO_TEST_CPUID("x86", "Core-i7-3770", true);
831
    DO_TEST_CPUID("x86", "Core-i7-4600U", true);
832
    DO_TEST_CPUID("x86", "Core-i7-5600U", true);
833
    DO_TEST_CPUID("x86", "Core2-E6850", true);
834
    DO_TEST_CPUID("x86", "Core2-Q9500", false);
835
    DO_TEST_CPUID("x86", "FX-8150", false);
836
    DO_TEST_CPUID("x86", "Opteron-1352", false);
837
    DO_TEST_CPUID("x86", "Opteron-2350", true);
838
    DO_TEST_CPUID("x86", "Opteron-6234", true);
839
    DO_TEST_CPUID("x86", "Opteron-6282", false);
840
    DO_TEST_CPUID("x86", "Pentium-P6100", false);
841
    DO_TEST_CPUID("x86", "Phenom-B95", true);
842
    DO_TEST_CPUID("x86", "Xeon-5110", false);
843
    DO_TEST_CPUID("x86", "Xeon-E3-1245", true);
844
    DO_TEST_CPUID("x86", "Xeon-E5-2630", true);
845
    DO_TEST_CPUID("x86", "Xeon-E5-2650", true);
846
    DO_TEST_CPUID("x86", "Xeon-E7-4820", true);
847
    DO_TEST_CPUID("x86", "Xeon-W3520", true);
848
    DO_TEST_CPUID("x86", "Xeon-X5460", false);
849

J
Jiri Denemark 已提交
850 851 852 853
#if WITH_QEMU && WITH_YAJL
    qemuTestDriverFree(&driver);
#endif

854
    return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
855 856 857
}

VIRT_TEST_MAIN(mymain)