vz_driver.c 43.7 KB
Newer Older
D
Dmitry Guryanov 已提交
1
/*
2
 * vz_driver.c: core driver functions for managing
D
Dmitry Guryanov 已提交
3 4
 * Parallels Cloud Server hosts
 *
5
 * Copyright (C) 2014-2015 Red Hat, Inc.
D
Dmitry Guryanov 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18
 * Copyright (C) 2012 Parallels, Inc.
 *
 * 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
19
 * License along with this library.  If not, see
D
Dmitry Guryanov 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
 * <http://www.gnu.org/licenses/>.
 *
 */

#include <config.h>

#include <sys/types.h>
#include <sys/poll.h>
#include <limits.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <paths.h>
#include <pwd.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/statvfs.h>

#include "datatypes.h"
44
#include "virerror.h"
45
#include "viralloc.h"
46
#include "virlog.h"
47
#include "vircommand.h"
D
Dmitry Guryanov 已提交
48
#include "configmake.h"
49
#include "virfile.h"
50
#include "virstoragefile.h"
D
Dmitry Guryanov 已提交
51
#include "nodeinfo.h"
52
#include "virstring.h"
53
#include "cpu/cpu.h"
54
#include "virtypedparam.h"
D
Dmitry Guryanov 已提交
55

56 57 58
#include "vz_driver.h"
#include "vz_utils.h"
#include "vz_sdk.h"
D
Dmitry Guryanov 已提交
59 60 61

#define VIR_FROM_THIS VIR_FROM_PARALLELS

62 63
VIR_LOG_INIT("parallels.parallels_driver");

D
Dmitry Guryanov 已提交
64
#define PRLCTL                      "prlctl"
65
#define PRLSRVCTL                   "prlsrvctl"
D
Dmitry Guryanov 已提交
66

67
static int vzConnectClose(virConnectPtr conn);
D
Dmitry Guryanov 已提交
68

D
Dmitry Guryanov 已提交
69
void
70
vzDriverLock(vzConnPtr driver)
D
Dmitry Guryanov 已提交
71 72 73 74
{
    virMutexLock(&driver->lock);
}

D
Dmitry Guryanov 已提交
75
void
76
vzDriverUnlock(vzConnPtr driver)
D
Dmitry Guryanov 已提交
77 78 79 80
{
    virMutexUnlock(&driver->lock);
}

D
Dmitry Guryanov 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
static int
vzCapsAddGuestDomain(virCapsPtr caps,
                     virDomainOSType ostype,
                     virArch arch,
                     const char * emulator,
                     virDomainVirtType virt_type)
{
    virCapsGuestPtr guest;

    if ((guest = virCapabilitiesAddGuest(caps, ostype, arch, emulator,
                                         NULL, 0, NULL)) == NULL)
        return -1;


    if (virCapabilitiesAddGuestDomain(guest, virt_type,
                                      NULL, NULL, 0, NULL) == NULL)
        return -1;

    return 0;
}

D
Dmitry Guryanov 已提交
102
static virCapsPtr
103
vzBuildCapabilities(void)
D
Dmitry Guryanov 已提交
104
{
105 106 107 108
    virCapsPtr caps = NULL;
    virCPUDefPtr cpu = NULL;
    virCPUDataPtr data = NULL;
    virNodeInfo nodeinfo;
D
Dmitry Guryanov 已提交
109 110 111 112 113 114 115 116 117 118 119
    virDomainOSType ostypes[] = {
        VIR_DOMAIN_OSTYPE_HVM,
        VIR_DOMAIN_OSTYPE_EXE
    };
    virArch archs[] = { VIR_ARCH_I686, VIR_ARCH_X86_64 };
    const char *const emulators[] = { "parallels", "vz" };
    virDomainVirtType virt_types[] = {
        VIR_DOMAIN_VIRT_PARALLELS,
        VIR_DOMAIN_VIRT_VZ
    };
    size_t i, j, k;
D
Dmitry Guryanov 已提交
120

121
    if ((caps = virCapabilitiesNew(virArchFromHost(),
122
                                   false, false)) == NULL)
123
        return NULL;
D
Dmitry Guryanov 已提交
124

125
    if (nodeCapsInitNUMA(NULL, caps) < 0)
126
        goto error;
D
Dmitry Guryanov 已提交
127

D
Dmitry Guryanov 已提交
128 129 130 131 132 133
    for (i = 0; i < 2; i++)
        for (j = 0; j < 2; j++)
            for (k = 0; k < 2; k++)
                if (vzCapsAddGuestDomain(caps, ostypes[i], archs[j],
                                         emulators[k], virt_types[k]) < 0)
                    goto error;
134

135
    if (nodeGetInfo(NULL, &nodeinfo))
136 137
        goto error;

138
    if (VIR_ALLOC(cpu) < 0)
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
        goto error;

    cpu->arch = caps->host.arch;
    cpu->type = VIR_CPU_TYPE_HOST;
    cpu->sockets = nodeinfo.sockets;
    cpu->cores = nodeinfo.cores;
    cpu->threads = nodeinfo.threads;

    caps->host.cpu = cpu;

    if (!(data = cpuNodeData(cpu->arch))
        || cpuDecode(cpu, data, NULL, 0, NULL) < 0) {
        goto cleanup;
    }

 cleanup:
    cpuDataFree(data);
D
Dmitry Guryanov 已提交
156 157
    return caps;

158
 error:
159
    virObjectUnref(caps);
160
    goto cleanup;
D
Dmitry Guryanov 已提交
161 162 163
}

static char *
164
vzConnectGetCapabilities(virConnectPtr conn)
D
Dmitry Guryanov 已提交
165
{
166
    vzConnPtr privconn = conn->privateData;
D
Dmitry Guryanov 已提交
167 168
    char *xml;

169
    vzDriverLock(privconn);
170
    xml = virCapabilitiesFormatXML(privconn->caps);
171
    vzDriverUnlock(privconn);
D
Dmitry Guryanov 已提交
172 173 174
    return xml;
}

175
static int
176
vzDomainDefPostParse(virDomainDefPtr def,
M
Michal Privoznik 已提交
177
                     virCapsPtr caps ATTRIBUTE_UNUSED,
178
                     unsigned int parseFlags ATTRIBUTE_UNUSED,
M
Michal Privoznik 已提交
179
                     void *opaque ATTRIBUTE_UNUSED)
180
{
181 182 183 184
    /* memory hotplug tunables are not supported by this driver */
    if (virDomainDefCheckUnsupportedMemoryHotplug(def) < 0)
        return -1;

185 186 187 188
    return 0;
}

static int
189
vzDomainDeviceDefPostParse(virDomainDeviceDefPtr dev,
M
Michal Privoznik 已提交
190 191
                           const virDomainDef *def,
                           virCapsPtr caps ATTRIBUTE_UNUSED,
192
                           unsigned int parseFlags ATTRIBUTE_UNUSED,
M
Michal Privoznik 已提交
193
                           void *opaque ATTRIBUTE_UNUSED)
194
{
195 196 197 198 199 200
    int ret = -1;

    if (dev->type == VIR_DOMAIN_DEVICE_NET &&
        (dev->data.net->type == VIR_DOMAIN_NET_TYPE_NETWORK ||
         dev->data.net->type == VIR_DOMAIN_NET_TYPE_BRIDGE) &&
        !dev->data.net->model &&
201
        def->os.type == VIR_DOMAIN_OSTYPE_HVM &&
202 203 204 205 206 207
        VIR_STRDUP(dev->data.net->model, "e1000") < 0)
        goto cleanup;

    ret = 0;
 cleanup:
    return ret;
208 209 210
}


211
virDomainDefParserConfig vzDomainDefParserConfig = {
212
    .macPrefix = {0x42, 0x1C, 0x00},
213 214
    .devicesPostParseCallback = vzDomainDeviceDefPostParse,
    .domainPostParseCallback = vzDomainDefPostParse,
215 216 217
};


D
Dmitry Guryanov 已提交
218
static int
219
vzOpenDefault(virConnectPtr conn)
D
Dmitry Guryanov 已提交
220
{
221
    vzConnPtr privconn;
D
Dmitry Guryanov 已提交
222

223
    if (VIR_ALLOC(privconn) < 0)
D
Dmitry Guryanov 已提交
224 225 226 227
        return VIR_DRV_OPEN_ERROR;
    if (virMutexInit(&privconn->lock) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("cannot initialize mutex"));
228 229 230
        goto err_free;
    }

231 232
    privconn->drivername = conn->driver->name;

233
    if (prlsdkInit()) {
234 235
        VIR_DEBUG("%s", _("Can't initialize Parallels SDK"));
        goto err_free;
D
Dmitry Guryanov 已提交
236 237
    }

238 239 240
    if (prlsdkConnect(privconn) < 0)
        goto err_free;

241
    if (!(privconn->caps = vzBuildCapabilities()))
D
Dmitry Guryanov 已提交
242 243
        goto error;

244
    if (!(privconn->xmlopt = virDomainXMLOptionNew(&vzDomainDefParserConfig,
M
Michal Privoznik 已提交
245
                                                   NULL, NULL)))
246 247
        goto error;

248
    if (!(privconn->domains = virDomainObjListNew()))
D
Dmitry Guryanov 已提交
249 250
        goto error;

251 252 253 254 255 256
    if (!(privconn->domainEventState = virObjectEventStateNew()))
        goto error;

    if (prlsdkSubscribeToPCSEvents(privconn))
        goto error;

257 258 259
    if (!(privconn->closeCallback = virNewConnectCloseCallbackData()))
        goto error;

D
Dmitry Guryanov 已提交
260 261
    conn->privateData = privconn;

262
    if (prlsdkLoadDomains(privconn))
263 264
        goto error;

D
Dmitry Guryanov 已提交
265 266
    return VIR_DRV_OPEN_SUCCESS;

267
 error:
268 269
    virObjectUnref(privconn->closeCallback);
    privconn->closeCallback = NULL;
270
    virObjectUnref(privconn->domains);
271
    virObjectUnref(privconn->caps);
272
    virObjectEventStateFree(privconn->domainEventState);
273 274 275
    prlsdkDisconnect(privconn);
    prlsdkDeinit();
 err_free:
276
    conn->privateData = NULL;
D
Dmitry Guryanov 已提交
277 278 279 280 281
    VIR_FREE(privconn);
    return VIR_DRV_OPEN_ERROR;
}

static virDrvOpenStatus
282
vzConnectOpen(virConnectPtr conn,
M
Michal Privoznik 已提交
283 284
              virConnectAuthPtr auth ATTRIBUTE_UNUSED,
              unsigned int flags)
D
Dmitry Guryanov 已提交
285 286 287 288 289 290 291 292
{
    int ret;

    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

    if (!conn->uri)
        return VIR_DRV_OPEN_DECLINED;

293 294 295 296 297 298 299 300 301 302 303
    if (!conn->uri->scheme)
        return VIR_DRV_OPEN_DECLINED;

    if (STRNEQ(conn->uri->scheme, "vz") &&
        STRNEQ(conn->uri->scheme, "parallels"))
        return VIR_DRV_OPEN_DECLINED;

    if (STREQ(conn->uri->scheme, "vz") && STRNEQ(conn->driver->name, "vz"))
        return VIR_DRV_OPEN_DECLINED;

    if (STREQ(conn->uri->scheme, "parallels") && STRNEQ(conn->driver->name, "Parallels"))
D
Dmitry Guryanov 已提交
304 305 306 307 308 309 310
        return VIR_DRV_OPEN_DECLINED;

    /* Remote driver should handle these. */
    if (conn->uri->server)
        return VIR_DRV_OPEN_DECLINED;

    /* From this point on, the connection is for us. */
311
    if (STRNEQ_NULLABLE(conn->uri->path, "/system")) {
312
        virReportError(VIR_ERR_INTERNAL_ERROR,
313
                       _("Unexpected Virtuozzo URI path '%s', try vz:///system"),
314
                       conn->uri->path);
D
Dmitry Guryanov 已提交
315 316 317
        return VIR_DRV_OPEN_ERROR;
    }

318
    if ((ret = vzOpenDefault(conn)) != VIR_DRV_OPEN_SUCCESS)
D
Dmitry Guryanov 已提交
319 320 321 322 323 324
        return ret;

    return VIR_DRV_OPEN_SUCCESS;
}

static int
325
vzConnectClose(virConnectPtr conn)
D
Dmitry Guryanov 已提交
326
{
327
    vzConnPtr privconn = conn->privateData;
D
Dmitry Guryanov 已提交
328

329 330 331
    if (!privconn)
        return 0;

332
    vzDriverLock(privconn);
333
    prlsdkUnsubscribeFromPCSEvents(privconn);
334
    virObjectUnref(privconn->caps);
335
    virObjectUnref(privconn->xmlopt);
336
    virObjectUnref(privconn->domains);
337 338
    virObjectUnref(privconn->closeCallback);
    privconn->closeCallback = NULL;
339
    virObjectEventStateFree(privconn->domainEventState);
340
    prlsdkDisconnect(privconn);
D
Dmitry Guryanov 已提交
341
    conn->privateData = NULL;
342
    prlsdkDeinit();
D
Dmitry Guryanov 已提交
343

344
    vzDriverUnlock(privconn);
D
Dmitry Guryanov 已提交
345 346 347 348 349 350 351
    virMutexDestroy(&privconn->lock);

    VIR_FREE(privconn);
    return 0;
}

static int
352
vzConnectGetVersion(virConnectPtr conn ATTRIBUTE_UNUSED, unsigned long *hvVer)
D
Dmitry Guryanov 已提交
353
{
354 355 356 357
    char *output, *sVer, *tmp;
    const char *searchStr = "prlsrvctl version ";
    int ret = -1;

358
    output = vzGetOutput(PRLSRVCTL, "--help", NULL);
359 360

    if (!output) {
361
        vzParseError();
362 363 364 365
        goto cleanup;
    }

    if (!(sVer = strstr(output, searchStr))) {
366
        vzParseError();
367 368 369 370 371 372 373 374
        goto cleanup;
    }

    sVer = sVer + strlen(searchStr);

    /* parallels server has versions number like 6.0.17977.782218,
     * so libvirt can handle only first two numbers. */
    if (!(tmp = strchr(sVer, '.'))) {
375
        vzParseError();
376 377 378 379
        goto cleanup;
    }

    if (!(tmp = strchr(tmp + 1, '.'))) {
380
        vzParseError();
381 382 383 384 385
        goto cleanup;
    }

    tmp[0] = '\0';
    if (virParseVersionString(sVer, hvVer, true) < 0) {
386
        vzParseError();
387 388 389 390 391
        goto cleanup;
    }

    ret = 0;

392
 cleanup:
393 394 395 396
    VIR_FREE(output);
    return ret;
}

397

398
static char *vzConnectGetHostname(virConnectPtr conn ATTRIBUTE_UNUSED)
399 400 401 402 403
{
    return virGetHostname();
}


404
static int
405
vzConnectListDomains(virConnectPtr conn, int *ids, int maxids)
406
{
407
    vzConnPtr privconn = conn->privateData;
408 409
    int n;

410
    vzDriverLock(privconn);
411 412
    n = virDomainObjListGetActiveIDs(privconn->domains, ids, maxids,
                                     NULL, NULL);
413
    vzDriverUnlock(privconn);
414 415 416 417 418

    return n;
}

static int
419
vzConnectNumOfDomains(virConnectPtr conn)
420
{
421
    vzConnPtr privconn = conn->privateData;
422 423
    int count;

424
    vzDriverLock(privconn);
425 426
    count = virDomainObjListNumOfDomains(privconn->domains, true,
                                         NULL, NULL);
427
    vzDriverUnlock(privconn);
428 429 430 431 432

    return count;
}

static int
433
vzConnectListDefinedDomains(virConnectPtr conn, char **const names, int maxnames)
434
{
435
    vzConnPtr privconn = conn->privateData;
436 437
    int n;

438
    vzDriverLock(privconn);
439
    memset(names, 0, sizeof(*names) * maxnames);
440
    n = virDomainObjListGetInactiveNames(privconn->domains, names,
441
                                         maxnames, NULL, NULL);
442
    vzDriverUnlock(privconn);
443 444 445 446 447

    return n;
}

static int
448
vzConnectNumOfDefinedDomains(virConnectPtr conn)
449
{
450
    vzConnPtr privconn = conn->privateData;
451 452
    int count;

453
    vzDriverLock(privconn);
454 455
    count = virDomainObjListNumOfDomains(privconn->domains, false,
                                         NULL, NULL);
456
    vzDriverUnlock(privconn);
457 458 459 460 461

    return count;
}

static int
462
vzConnectListAllDomains(virConnectPtr conn,
M
Michal Privoznik 已提交
463 464
                        virDomainPtr **domains,
                        unsigned int flags)
465
{
466
    vzConnPtr privconn = conn->privateData;
467 468
    int ret = -1;

O
Osier Yang 已提交
469
    virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1);
470
    vzDriverLock(privconn);
471 472
    ret = virDomainObjListExport(privconn->domains, conn, domains,
                                 NULL, flags);
473
    vzDriverUnlock(privconn);
474 475 476 477 478

    return ret;
}

static virDomainPtr
479
vzDomainLookupByID(virConnectPtr conn, int id)
480
{
481
    vzConnPtr privconn = conn->privateData;
482 483 484
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;

485
    vzDriverLock(privconn);
486
    dom = virDomainObjListFindByID(privconn->domains, id);
487
    vzDriverUnlock(privconn);
488 489 490 491 492 493 494 495 496 497

    if (dom == NULL) {
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
        goto cleanup;
    }

    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
    if (ret)
        ret->id = dom->def->id;

498
 cleanup:
499
    if (dom)
500
        virObjectUnlock(dom);
501 502 503 504
    return ret;
}

static virDomainPtr
505
vzDomainLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
506
{
507
    vzConnPtr privconn = conn->privateData;
508 509 510
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;

511
    vzDriverLock(privconn);
512
    dom = virDomainObjListFindByUUID(privconn->domains, uuid);
513
    vzDriverUnlock(privconn);
514 515 516 517 518 519 520 521 522 523 524 525 526

    if (dom == NULL) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(uuid, uuidstr);
        virReportError(VIR_ERR_NO_DOMAIN,
                       _("no domain with matching uuid '%s'"), uuidstr);
        goto cleanup;
    }

    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
    if (ret)
        ret->id = dom->def->id;

527
 cleanup:
528
    if (dom)
529
        virObjectUnlock(dom);
530 531 532 533
    return ret;
}

static virDomainPtr
534
vzDomainLookupByName(virConnectPtr conn, const char *name)
535
{
536
    vzConnPtr privconn = conn->privateData;
537 538 539
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;

540
    vzDriverLock(privconn);
541
    dom = virDomainObjListFindByName(privconn->domains, name);
542
    vzDriverUnlock(privconn);
543 544 545 546 547 548 549 550 551 552 553

    if (dom == NULL) {
        virReportError(VIR_ERR_NO_DOMAIN,
                       _("no domain with matching name '%s'"), name);
        goto cleanup;
    }

    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
    if (ret)
        ret->id = dom->def->id;

554
 cleanup:
555
    virDomainObjEndAPI(&dom);
556 557 558 559
    return ret;
}

static int
560
vzDomainGetInfo(virDomainPtr domain, virDomainInfoPtr info)
561 562 563 564
{
    virDomainObjPtr privdom;
    int ret = -1;

565
    if (!(privdom = vzDomObjFromDomainRef(domain)))
566 567 568 569
        goto cleanup;

    info->state = virDomainObjGetState(privdom, NULL);
    info->memory = privdom->def->mem.cur_balloon;
570
    info->maxMem = virDomainDefGetMemoryActual(privdom->def);
571
    info->nrVirtCpu = virDomainDefGetVcpus(privdom->def);
572
    info->cpuTime = 0;
573 574 575 576 577

    if (virDomainObjIsActive(privdom)) {
        unsigned long long vtime;
        size_t i;

578
        for (i = 0; i < virDomainDefGetVcpus(privdom->def); ++i) {
579 580 581 582 583 584 585 586
            if (prlsdkGetVcpuStats(privdom, i, &vtime) < 0) {
                virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                               _("cannot read cputime for domain"));
                goto cleanup;
            }
            info->cpuTime += vtime;
        }
    }
587 588
    ret = 0;

589
 cleanup:
590
    virDomainObjEndAPI(&privdom);
591 592 593 594
    return ret;
}

static char *
595
vzDomainGetOSType(virDomainPtr domain)
596 597 598 599 600
{
    virDomainObjPtr privdom;

    char *ret = NULL;

601
    if (!(privdom = vzDomObjFromDomain(domain)))
602 603
        goto cleanup;

604
    ignore_value(VIR_STRDUP(ret, virDomainOSTypeToString(privdom->def->os.type)));
605

606
 cleanup:
607
    if (privdom)
608
        virObjectUnlock(privdom);
609 610 611 612
    return ret;
}

static int
613
vzDomainIsPersistent(virDomainPtr domain)
614 615 616 617
{
    virDomainObjPtr privdom;
    int ret = -1;

618
    if (!(privdom = vzDomObjFromDomain(domain)))
619 620 621 622
        goto cleanup;

    ret = 1;

623
 cleanup:
624
    if (privdom)
625
        virObjectUnlock(privdom);
626 627 628 629
    return ret;
}

static int
630
vzDomainGetState(virDomainPtr domain,
M
Michal Privoznik 已提交
631
                 int *state, int *reason, unsigned int flags)
632 633 634 635 636
{
    virDomainObjPtr privdom;
    int ret = -1;
    virCheckFlags(0, -1);

637
    if (!(privdom = vzDomObjFromDomain(domain)))
638 639 640 641 642
        goto cleanup;

    *state = virDomainObjGetState(privdom, reason);
    ret = 0;

643
 cleanup:
644
    if (privdom)
645
        virObjectUnlock(privdom);
646 647 648 649
    return ret;
}

static char *
650
vzDomainGetXMLDesc(virDomainPtr domain, unsigned int flags)
651
{
652
    vzConnPtr privconn = domain->conn->privateData;
653 654 655 656 657 658
    virDomainDefPtr def;
    virDomainObjPtr privdom;
    char *ret = NULL;

    /* Flags checked by virDomainDefFormat */

659
    if (!(privdom = vzDomObjFromDomain(domain)))
660 661 662 663 664
        goto cleanup;

    def = (flags & VIR_DOMAIN_XML_INACTIVE) &&
        privdom->newDef ? privdom->newDef : privdom->def;

665
    ret = virDomainDefFormat(def, privconn->caps, flags);
666

667
 cleanup:
668
    if (privdom)
669
        virObjectUnlock(privdom);
670 671 672 673
    return ret;
}

static int
674
vzDomainGetAutostart(virDomainPtr domain, int *autostart)
675 676 677 678
{
    virDomainObjPtr privdom;
    int ret = -1;

679
    if (!(privdom = vzDomObjFromDomain(domain)))
680 681 682 683 684
        goto cleanup;

    *autostart = privdom->autostart;
    ret = 0;

685
 cleanup:
686
    if (privdom)
687
        virObjectUnlock(privdom);
688
    return ret;
D
Dmitry Guryanov 已提交
689 690
}

691
static virDomainPtr
692
vzDomainDefineXMLFlags(virConnectPtr conn, const char *xml, unsigned int flags)
693
{
694
    vzConnPtr privconn = conn->privateData;
695
    virDomainPtr retdom = NULL;
696
    virDomainDefPtr def;
697
    virDomainObjPtr olddom = NULL;
698
    virDomainObjPtr newdom = NULL;
699
    unsigned int parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE;
700

701 702 703 704
    virCheckFlags(VIR_DOMAIN_DEFINE_VALIDATE, NULL);

    if (flags & VIR_DOMAIN_DEFINE_VALIDATE)
        parse_flags |= VIR_DOMAIN_DEF_PARSE_VALIDATE;
705

706
    vzDriverLock(privconn);
707
    if ((def = virDomainDefParseString(xml, privconn->caps, privconn->xmlopt,
708
                                       parse_flags)) == NULL)
709 710
        goto cleanup;

711 712 713
    olddom = virDomainObjListFindByUUID(privconn->domains, def->uuid);
    if (olddom == NULL) {
        virResetLastError();
714 715 716
        newdom = vzNewDomain(privconn, def->name, def->uuid);
        if (!newdom)
            goto cleanup;
717
        if (def->os.type == VIR_DOMAIN_OSTYPE_HVM) {
718
            if (prlsdkCreateVm(conn, def))
719
                goto cleanup;
720
        } else if (def->os.type == VIR_DOMAIN_OSTYPE_EXE) {
721
            if (prlsdkCreateCt(conn, def))
722 723 724
                goto cleanup;
        } else {
            virReportError(VIR_ERR_INVALID_ARG,
725 726
                           _("Unsupported OS type: %s"),
                           virDomainOSTypeToString(def->os.type));
727
            goto cleanup;
728
        }
729

730
        if (prlsdkLoadDomain(privconn, newdom))
731
            goto cleanup;
732
    } else {
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
        int state, reason;

        state = virDomainObjGetState(olddom, &reason);

        if (state == VIR_DOMAIN_SHUTOFF &&
            reason == VIR_DOMAIN_SHUTOFF_SAVED) {

            /* PCS doesn't store domain config in managed save state file.
             * It's forbidden to change config for VMs in this state.
             * It's possible to change config for containers, but after
             * restoring domain will have that new config, not a config,
             * which domain had at the moment of virDomainManagedSave.
             *
             * So forbid this operation, if config is changed. If it's
             * not changed - just do nothing. */

            if (!virDomainDefCheckABIStability(olddom->def, def)) {
                virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                               _("Can't change domain configuration "
                                 "in managed save state"));
                goto cleanup;
            }
        } else {
            if (prlsdkApplyConfig(conn, olddom, def))
                goto cleanup;
758

759 760 761
            if (prlsdkUpdateDomain(privconn, olddom))
                goto cleanup;
        }
762 763
    }

764 765 766
    retdom = virGetDomain(conn, def->name, def->uuid);
    if (retdom)
        retdom->id = def->id;
767

768
 cleanup:
769 770
    if (olddom)
        virObjectUnlock(olddom);
771 772 773 774 775 776
    if (newdom) {
        if (!retdom)
             virDomainObjListRemove(privconn->domains, newdom);
        else
             virObjectUnlock(newdom);
    }
777
    virDomainDefFree(def);
778
    vzDriverUnlock(privconn);
779
    return retdom;
780 781
}

782
static virDomainPtr
783
vzDomainDefineXML(virConnectPtr conn, const char *xml)
784
{
785
    return vzDomainDefineXMLFlags(conn, xml, 0);
786 787 788
}


789
static int
790
vzNodeGetInfo(virConnectPtr conn ATTRIBUTE_UNUSED,
M
Michal Privoznik 已提交
791
              virNodeInfoPtr nodeinfo)
792
{
793
    return nodeGetInfo(NULL, nodeinfo);
794 795
}

796
static int vzConnectIsEncrypted(virConnectPtr conn ATTRIBUTE_UNUSED)
797 798 799 800 801
{
    /* Encryption is not relevant / applicable to way we talk to PCS */
    return 0;
}

802
static int vzConnectIsSecure(virConnectPtr conn ATTRIBUTE_UNUSED)
803 804 805 806 807
{
    /* We run CLI tools directly so this is secure */
    return 1;
}

808
static int vzConnectIsAlive(virConnectPtr conn ATTRIBUTE_UNUSED)
809 810 811 812
{
    return 1;
}

813

814
static char *
815
vzConnectBaselineCPU(virConnectPtr conn ATTRIBUTE_UNUSED,
M
Michal Privoznik 已提交
816 817 818
                     const char **xmlCPUs,
                     unsigned int ncpus,
                     unsigned int flags)
819 820 821 822 823 824 825
{
    virCheckFlags(VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES, NULL);

    return cpuBaselineXML(xmlCPUs, ncpus, NULL, 0, flags);
}


826
static int
827
vzDomainGetVcpus(virDomainPtr domain,
M
Michal Privoznik 已提交
828 829 830 831
                 virVcpuInfoPtr info,
                 int maxinfo,
                 unsigned char *cpumaps,
                 int maplen)
832 833 834 835 836
{
    virDomainObjPtr privdom = NULL;
    size_t i;
    int ret = -1;

N
Nikolay Shirokovskiy 已提交
837
    if (!(privdom = vzDomObjFromDomainRef(domain)))
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
        goto cleanup;

    if (!virDomainObjIsActive(privdom)) {
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s",
                       _("cannot list vcpu pinning for an inactive domain"));
        goto cleanup;
    }

    if (maxinfo >= 1) {
        if (info != NULL) {
            memset(info, 0, sizeof(*info) * maxinfo);
            for (i = 0; i < maxinfo; i++) {
                info[i].number = i;
                info[i].state = VIR_VCPU_RUNNING;
N
Nikolay Shirokovskiy 已提交
853 854
                if (prlsdkGetVcpuStats(privdom, i, &info[i].cpuTime) < 0)
                    goto cleanup;
855 856 857 858
            }
        }
        if (cpumaps != NULL) {
            memset(cpumaps, 0, maplen * maxinfo);
859 860 861 862
            for (i = 0; i < maxinfo; i++)
                virBitmapToDataBuf(privdom->def->cpumask,
                                   VIR_GET_CPUMAP(cpumaps, maplen, i),
                                   maplen);
863 864 865 866 867 868
        }
    }
    ret = maxinfo;

 cleanup:
    if (privdom)
N
Nikolay Shirokovskiy 已提交
869
        virDomainObjEndAPI(&privdom);
870 871 872 873
    return ret;
}


874
static int
875
vzNodeGetCPUMap(virConnectPtr conn ATTRIBUTE_UNUSED,
M
Michal Privoznik 已提交
876 877 878
                unsigned char **cpumap,
                unsigned int *online,
                unsigned int flags)
879
{
880
    return nodeGetCPUMap(NULL, cpumap, online, flags);
881 882
}

883
static int
884
vzConnectDomainEventRegisterAny(virConnectPtr conn,
M
Michal Privoznik 已提交
885 886 887 888 889
                                virDomainPtr domain,
                                int eventID,
                                virConnectDomainEventGenericCallback callback,
                                void *opaque,
                                virFreeCallback freecb)
890 891
{
    int ret = -1;
892
    vzConnPtr privconn = conn->privateData;
893 894 895 896 897 898 899 900 901
    if (virDomainEventStateRegisterID(conn,
                                      privconn->domainEventState,
                                      domain, eventID,
                                      callback, opaque, freecb, &ret) < 0)
        ret = -1;
    return ret;
}

static int
902
vzConnectDomainEventDeregisterAny(virConnectPtr conn,
M
Michal Privoznik 已提交
903
                                  int callbackID)
904
{
905
    vzConnPtr privconn = conn->privateData;
906 907 908 909 910 911 912 913 914 915 916 917
    int ret = -1;

    if (virObjectEventStateDeregisterID(conn,
                                        privconn->domainEventState,
                                        callbackID) < 0)
        goto cleanup;

    ret = 0;

 cleanup:
    return ret;
}
918

919
static int vzDomainSuspend(virDomainPtr domain)
920 921 922 923
{
    return prlsdkDomainChangeState(domain, prlsdkPause);
}

924
static int vzDomainResume(virDomainPtr domain)
925 926 927 928
{
    return prlsdkDomainChangeState(domain, prlsdkResume);
}

929
static int vzDomainCreate(virDomainPtr domain)
930 931 932 933
{
    return prlsdkDomainChangeState(domain, prlsdkStart);
}

934
static int vzDomainDestroy(virDomainPtr domain)
935 936 937 938
{
    return prlsdkDomainChangeState(domain, prlsdkKill);
}

939
static int vzDomainShutdown(virDomainPtr domain)
940 941 942 943
{
    return prlsdkDomainChangeState(domain, prlsdkStop);
}

944 945 946 947 948 949 950
static int vzDomainReboot(virDomainPtr domain,
                          unsigned int flags)
{
    virCheckFlags(0, -1);
    return prlsdkDomainChangeState(domain, prlsdkRestart);
}

951
static int vzDomainIsActive(virDomainPtr domain)
952 953 954 955
{
    virDomainObjPtr dom = NULL;
    int ret = -1;

956
    if (!(dom = vzDomObjFromDomain(domain)))
957 958 959 960 961 962 963 964
        return -1;

    ret = virDomainObjIsActive(dom);
    virObjectUnlock(dom);

    return ret;
}

965
static int
966
vzDomainCreateWithFlags(virDomainPtr domain, unsigned int flags)
967 968 969 970
{
    /* we don't support any create flags */
    virCheckFlags(0, -1);

971
    return vzDomainCreate(domain);
972 973
}

974
static int
975
vzDomainUndefineFlags(virDomainPtr domain,
M
Michal Privoznik 已提交
976
                      unsigned int flags)
977
{
978
    vzConnPtr privconn = domain->conn->privateData;
979
    virDomainObjPtr dom = NULL;
980
    int ret;
981

982 983
    virCheckFlags(VIR_DOMAIN_UNDEFINE_MANAGED_SAVE |
                  VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA, -1);
984

985
    if (!(dom = vzDomObjFromDomain(domain)))
986 987
        return -1;

988
    ret = prlsdkUnregisterDomain(privconn, dom, flags);
989
    if (ret)
M
Michal Privoznik 已提交
990
        virObjectUnlock(dom);
991 992

    return ret;
993 994 995
}

static int
996
vzDomainUndefine(virDomainPtr domain)
997
{
998
    return vzDomainUndefineFlags(domain, 0);
999 1000
}

1001
static int
1002
vzDomainHasManagedSaveImage(virDomainPtr domain, unsigned int flags)
1003 1004
{
    virDomainObjPtr dom = NULL;
1005 1006
    int state, reason;
    int ret = 0;
1007 1008 1009

    virCheckFlags(0, -1);

1010
    if (!(dom = vzDomObjFromDomain(domain)))
1011 1012
        return -1;

1013 1014 1015
    state = virDomainObjGetState(dom, &reason);
    if (state == VIR_DOMAIN_SHUTOFF && reason == VIR_DOMAIN_SHUTOFF_SAVED)
        ret = 1;
1016 1017
    virObjectUnlock(dom);

1018 1019 1020 1021
    return ret;
}

static int
1022
vzDomainManagedSave(virDomainPtr domain, unsigned int flags)
1023
{
1024
    vzConnPtr privconn = domain->conn->privateData;
1025 1026 1027 1028 1029 1030 1031
    virDomainObjPtr dom = NULL;
    int state, reason;
    int ret = -1;

    virCheckFlags(VIR_DOMAIN_SAVE_RUNNING |
                  VIR_DOMAIN_SAVE_PAUSED, -1);

1032
    if (!(dom = vzDomObjFromDomain(domain)))
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
        return -1;

    state = virDomainObjGetState(dom, &reason);

    if (state == VIR_DOMAIN_RUNNING && (flags & VIR_DOMAIN_SAVE_PAUSED)) {
        ret = prlsdkDomainChangeStateLocked(privconn, dom, prlsdkPause);
        if (ret)
            goto cleanup;
    }

    ret = prlsdkDomainChangeStateLocked(privconn, dom, prlsdkSuspend);

 cleanup:
    virObjectUnlock(dom);
    return ret;
}

static int
1051
vzDomainManagedSaveRemove(virDomainPtr domain, unsigned int flags)
1052 1053 1054 1055 1056 1057 1058
{
    virDomainObjPtr dom = NULL;
    int state, reason;
    int ret = -1;

    virCheckFlags(0, -1);

1059
    if (!(dom = vzDomObjFromDomain(domain)))
1060 1061 1062 1063 1064 1065 1066
        return -1;

    state = virDomainObjGetState(dom, &reason);

    if (!(state == VIR_DOMAIN_SHUTOFF && reason == VIR_DOMAIN_SHUTOFF_SAVED))
        goto cleanup;

1067
    ret = prlsdkDomainManagedSaveRemove(dom);
1068 1069 1070 1071

 cleanup:
    virObjectUnlock(dom);
    return ret;
1072 1073
}

1074
static int vzDomainAttachDeviceFlags(virDomainPtr dom, const char *xml,
M
Michal Privoznik 已提交
1075
                                     unsigned int flags)
1076 1077
{
    int ret = -1;
1078
    vzConnPtr privconn = dom->conn->privateData;
1079 1080 1081 1082 1083 1084 1085
    virDomainDeviceDefPtr dev = NULL;
    virDomainObjPtr privdom = NULL;
    bool domactive = false;

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, -1);

1086
    if (!(privdom = vzDomObjFromDomain(dom)))
1087
        return -1;
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115

    if (!(flags & VIR_DOMAIN_AFFECT_CONFIG)) {
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("device attach needs VIR_DOMAIN_AFFECT_CONFIG "
                         "flag to be set"));
        goto cleanup;
    }

    domactive = virDomainObjIsActive(privdom);
    if (!domactive && (flags & VIR_DOMAIN_AFFECT_LIVE)) {
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("cannot do live update a device on "
                         "inactive domain"));
        goto cleanup;
    }
    if (domactive && !(flags & VIR_DOMAIN_AFFECT_LIVE)) {
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("Updates on a running domain need "
                         "VIR_DOMAIN_AFFECT_LIVE flag"));
    }

    dev = virDomainDeviceDefParse(xml, privdom->def, privconn->caps,
                                  privconn->xmlopt, VIR_DOMAIN_XML_INACTIVE);
    if (dev == NULL)
        goto cleanup;

    switch (dev->type) {
    case VIR_DOMAIN_DEVICE_DISK:
1116
        ret = prlsdkAttachVolume(privdom, dev->data.disk);
1117 1118 1119 1120 1121 1122
        if (ret) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("disk attach failed"));
            goto cleanup;
        }
        break;
1123 1124 1125 1126 1127 1128 1129 1130
    case VIR_DOMAIN_DEVICE_NET:
        ret = prlsdkAttachNet(privdom, privconn, dev->data.net);
        if (ret) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("network attach failed"));
            goto cleanup;
        }
        break;
1131 1132
    default:
        virReportError(VIR_ERR_OPERATION_UNSUPPORTED,
1133
                       _("device type '%s' cannot be attached"),
1134 1135 1136 1137 1138 1139
                       virDomainDeviceTypeToString(dev->type));
        break;
    }

    ret = 0;
 cleanup:
1140
    virObjectUnlock(privdom);
1141 1142 1143
    return ret;
}

1144
static int vzDomainAttachDevice(virDomainPtr dom, const char *xml)
1145
{
1146
    return vzDomainAttachDeviceFlags(dom, xml,
M
Michal Privoznik 已提交
1147
                                     VIR_DOMAIN_AFFECT_CONFIG | VIR_DOMAIN_AFFECT_LIVE);
1148 1149
}

1150
static int vzDomainDetachDeviceFlags(virDomainPtr dom, const char *xml,
M
Michal Privoznik 已提交
1151
                                     unsigned int flags)
1152 1153
{
    int ret = -1;
1154
    vzConnPtr privconn = dom->conn->privateData;
1155 1156 1157 1158 1159 1160 1161
    virDomainDeviceDefPtr dev = NULL;
    virDomainObjPtr privdom = NULL;
    bool domactive = false;

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, -1);

1162
    privdom = vzDomObjFromDomain(dom);
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
    if (privdom == NULL)
        return -1;

    if (!(flags & VIR_DOMAIN_AFFECT_CONFIG)) {
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("device detach needs VIR_DOMAIN_AFFECT_CONFIG "
                         "flag to be set"));
        goto cleanup;
    }

    domactive = virDomainObjIsActive(privdom);
    if (!domactive && (flags & VIR_DOMAIN_AFFECT_LIVE)) {
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("cannot do live update a device on "
                         "inactive domain"));
        goto cleanup;
    }
    if (domactive && !(flags & VIR_DOMAIN_AFFECT_LIVE)) {
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("Updates on a running domain need "
                         "VIR_DOMAIN_AFFECT_LIVE flag"));
    }

    dev = virDomainDeviceDefParse(xml, privdom->def, privconn->caps,
                                  privconn->xmlopt, VIR_DOMAIN_XML_INACTIVE);
    if (dev == NULL)
        goto cleanup;

    switch (dev->type) {
    case VIR_DOMAIN_DEVICE_DISK:
1193
        ret = prlsdkDetachVolume(privdom, dev->data.disk);
1194 1195 1196 1197 1198 1199
        if (ret) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("disk detach failed"));
            goto cleanup;
        }
        break;
1200 1201 1202 1203 1204 1205 1206 1207
    case VIR_DOMAIN_DEVICE_NET:
        ret = prlsdkDetachNet(privdom, privconn, dev->data.net);
        if (ret) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("network detach failed"));
            goto cleanup;
        }
        break;
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
    default:
        virReportError(VIR_ERR_OPERATION_UNSUPPORTED,
                       _("device type '%s' cannot be detached"),
                       virDomainDeviceTypeToString(dev->type));
        break;
    }

    ret = 0;
 cleanup:
    virObjectUnlock(privdom);
    return ret;
}

1221
static int vzDomainDetachDevice(virDomainPtr dom, const char *xml)
1222
{
1223
    return vzDomainDetachDeviceFlags(dom, xml,
M
Michal Privoznik 已提交
1224
                                     VIR_DOMAIN_AFFECT_CONFIG | VIR_DOMAIN_AFFECT_LIVE);
1225 1226
}

1227
static unsigned long long
1228
vzDomainGetMaxMemory(virDomainPtr domain)
1229 1230 1231 1232
{
    virDomainObjPtr dom = NULL;
    int ret = -1;

1233
    if (!(dom = vzDomObjFromDomain(domain)))
1234 1235
        return -1;

1236
    ret = virDomainDefGetMemoryActual(dom->def);
1237 1238 1239 1240
    virObjectUnlock(dom);
    return ret;
}

1241
static int
1242
vzDomainBlockStats(virDomainPtr domain, const char *path,
M
Michal Privoznik 已提交
1243
                   virDomainBlockStatsPtr stats)
1244 1245 1246 1247 1248 1249
{
    virDomainObjPtr dom = NULL;
    int ret = -1;
    size_t i;
    int idx;

1250
    if (!(dom = vzDomObjFromDomainRef(domain)))
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
        return -1;

    if (*path) {
        if ((idx = virDomainDiskIndexByName(dom->def, path, false)) < 0) {
            virReportError(VIR_ERR_INVALID_ARG, _("invalid path: %s"), path);
            goto cleanup;
        }
        if (prlsdkGetBlockStats(dom, dom->def->disks[idx], stats) < 0)
            goto cleanup;
    } else {
        virDomainBlockStatsStruct s;

M
Michal Privoznik 已提交
1263
#define PARALLELS_ZERO_STATS(VAR, TYPE, NAME)      \
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
        stats->VAR = 0;

        PARALLELS_BLOCK_STATS_FOREACH(PARALLELS_ZERO_STATS)

#undef PARALLELS_ZERO_STATS

        for (i = 0; i < dom->def->ndisks; i++) {
            if (prlsdkGetBlockStats(dom, dom->def->disks[i], &s) < 0)
                goto cleanup;

1274
#define PARALLELS_SUM_STATS(VAR, TYPE, NAME)        \
M
Michal Privoznik 已提交
1275 1276
    if (s.VAR != -1)                                \
        stats->VAR += s.VAR;
1277

M
Michal Privoznik 已提交
1278
        PARALLELS_BLOCK_STATS_FOREACH(PARALLELS_SUM_STATS)
1279

1280
#undef PARALLELS_SUM_STATS
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
        }
    }
    stats->errs = -1;
    ret = 0;

 cleanup:
    if (dom)
        virDomainObjEndAPI(&dom);

    return ret;
}

static int
1294
vzDomainBlockStatsFlags(virDomainPtr domain,
M
Michal Privoznik 已提交
1295 1296 1297 1298
                        const char *path,
                        virTypedParameterPtr params,
                        int *nparams,
                        unsigned int flags)
1299 1300 1301 1302 1303 1304 1305 1306 1307
{
    virDomainBlockStatsStruct stats;
    int ret = -1;
    size_t i;

    virCheckFlags(VIR_TYPED_PARAM_STRING_OKAY, -1);
    /* We don't return strings, and thus trivially support this flag.  */
    flags &= ~VIR_TYPED_PARAM_STRING_OKAY;

1308
    if (vzDomainBlockStats(domain, path, &stats) < 0)
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
        goto cleanup;

    if (*nparams == 0) {
#define PARALLELS_COUNT_STATS(VAR, TYPE, NAME)       \
        if ((stats.VAR) != -1)                       \
            ++*nparams;

        PARALLELS_BLOCK_STATS_FOREACH(PARALLELS_COUNT_STATS)

#undef PARALLELS_COUNT_STATS
        ret = 0;
        goto cleanup;
    }

    i = 0;
#define PARALLELS_BLOCK_STATS_ASSIGN_PARAM(VAR, TYPE, NAME)                    \
    if (i < *nparams && (stats.VAR) != -1) {                                   \
        if (virTypedParameterAssign(params + i, TYPE,                          \
                                    VIR_TYPED_PARAM_LLONG, (stats.VAR)) < 0)   \
            goto cleanup;                                                      \
        i++;                                                                   \
    }

    PARALLELS_BLOCK_STATS_FOREACH(PARALLELS_BLOCK_STATS_ASSIGN_PARAM)

#undef PARALLELS_BLOCK_STATS_ASSIGN_PARAM

    *nparams = i;
    ret = 0;

 cleanup:
    return ret;
}

1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
static int
vzDomainInterfaceStats(virDomainPtr domain,
                         const char *path,
                         virDomainInterfaceStatsPtr stats)
{
    virDomainObjPtr dom = NULL;
    int ret;

    if (!(dom = vzDomObjFromDomainRef(domain)))
        return -1;

    ret = prlsdkGetNetStats(dom, path, stats);
    virDomainObjEndAPI(&dom);

    return ret;
}
1359

N
Nikolay Shirokovskiy 已提交
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
static int
vzDomainMemoryStats(virDomainPtr domain,
                    virDomainMemoryStatPtr stats,
                    unsigned int nr_stats,
                    unsigned int flags)
{
    virDomainObjPtr dom = NULL;
    int ret = -1;

    virCheckFlags(0, -1);
    if (!(dom = vzDomObjFromDomainRef(domain)))
        return -1;

    ret = prlsdkGetMemoryStats(dom, stats, nr_stats);
    virDomainObjEndAPI(&dom);

    return ret;
}

1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
static int
vzDomainGetVcpusFlags(virDomainPtr dom,
                      unsigned int flags)
{
    virDomainObjPtr privdom = NULL;
    int ret = -1;

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG |
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

    if (!(privdom = vzDomObjFromDomain(dom)))
        goto cleanup;

    if (flags & VIR_DOMAIN_VCPU_MAXIMUM)
1394
        ret = virDomainDefGetVcpusMax(privdom->def);
1395
    else
1396
        ret = virDomainDefGetVcpus(privdom->def);
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428

 cleanup:
    if (privdom)
        virObjectUnlock(privdom);

    return ret;
}

static int vzDomainGetMaxVcpus(virDomainPtr dom)
{
    return vzDomainGetVcpusFlags(dom, (VIR_DOMAIN_AFFECT_LIVE |
                                       VIR_DOMAIN_VCPU_MAXIMUM));
}

static int vzDomainIsUpdated(virDomainPtr dom)
{
    virDomainObjPtr privdom;
    int ret = -1;

    /* As far as VZ domains are always updated (e.g. current==persistent),
     * we just check for domain existence */
    if (!(privdom = vzDomObjFromDomain(dom)))
        goto cleanup;

    ret = 0;

 cleanup:
    if (privdom)
        virObjectUnlock(privdom);
    return ret;
}

1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
static int vzConnectGetMaxVcpus(virConnectPtr conn ATTRIBUTE_UNUSED,
                                const char *type)
{
    /* As far as we have no limitation for containers
     * we report maximum */
    if (type == NULL || STRCASEEQ(type, "vz") || STRCASEEQ(type, "parallels"))
        return 1028;

    virReportError(VIR_ERR_INVALID_ARG,
                   _("unknown type '%s'"), type);
    return -1;
}

1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479
static int
vzNodeGetCPUStats(virConnectPtr conn ATTRIBUTE_UNUSED,
                  int cpuNum,
                  virNodeCPUStatsPtr params,
                  int *nparams,
                  unsigned int flags)
{
    return nodeGetCPUStats(cpuNum, params, nparams, flags);
}

static int
vzNodeGetMemoryStats(virConnectPtr conn ATTRIBUTE_UNUSED,
                     int cellNum,
                     virNodeMemoryStatsPtr params,
                     int *nparams,
                     unsigned int flags)
{
    return nodeGetMemoryStats(NULL, cellNum, params, nparams, flags);
}

static int
vzNodeGetCellsFreeMemory(virConnectPtr conn ATTRIBUTE_UNUSED,
                         unsigned long long *freeMems,
                         int startCell,
                         int maxCells)
{
    return nodeGetCellsFreeMemory(freeMems, startCell, maxCells);
}

static unsigned long long
vzNodeGetFreeMemory(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    unsigned long long freeMem;
    if (nodeGetMemory(NULL, &freeMem) < 0)
        return 0;
    return freeMem;
}

1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
static int
vzConnectRegisterCloseCallback(virConnectPtr conn,
                               virConnectCloseFunc cb,
                               void *opaque,
                               virFreeCallback freecb)
{
    vzConnPtr privconn = conn->privateData;
    int ret = -1;

    vzDriverLock(privconn);

    if (virConnectCloseCallbackDataGetCallback(privconn->closeCallback) != NULL) {
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("A close callback is already registered"));
        goto cleanup;
    }

    virConnectCloseCallbackDataRegister(privconn->closeCallback, conn, cb,
                                        opaque, freecb);
    ret = 0;

 cleanup:
    vzDriverUnlock(privconn);

    return ret;
}

static int
vzConnectUnregisterCloseCallback(virConnectPtr conn, virConnectCloseFunc cb)
{
    vzConnPtr privconn = conn->privateData;
    int ret = -1;

    vzDriverLock(privconn);

    if (virConnectCloseCallbackDataGetCallback(privconn->closeCallback) != cb) {
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("A different callback was requested"));
        goto cleanup;
    }

    virConnectCloseCallbackDataUnregister(privconn->closeCallback, cb);
    ret = 0;

 cleanup:
    vzDriverUnlock(privconn);

    return ret;
}

1530 1531
static virHypervisorDriver vzDriver = {
    .name = "vz",
1532 1533 1534 1535
    .connectOpen = vzConnectOpen,            /* 0.10.0 */
    .connectClose = vzConnectClose,          /* 0.10.0 */
    .connectGetVersion = vzConnectGetVersion,   /* 0.10.0 */
    .connectGetHostname = vzConnectGetHostname,      /* 0.10.0 */
1536
    .connectGetMaxVcpus = vzConnectGetMaxVcpus, /* 1.2.21 */
1537
    .nodeGetInfo = vzNodeGetInfo,      /* 0.10.0 */
1538 1539 1540 1541
    .nodeGetCPUStats = vzNodeGetCPUStats,      /* 1.2.21 */
    .nodeGetMemoryStats = vzNodeGetMemoryStats, /* 1.2.21 */
    .nodeGetCellsFreeMemory = vzNodeGetCellsFreeMemory, /* 1.2.21 */
    .nodeGetFreeMemory = vzNodeGetFreeMemory, /* 1.2.21 */
1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564
    .connectGetCapabilities = vzConnectGetCapabilities,      /* 0.10.0 */
    .connectBaselineCPU = vzConnectBaselineCPU, /* 1.2.6 */
    .connectListDomains = vzConnectListDomains,      /* 0.10.0 */
    .connectNumOfDomains = vzConnectNumOfDomains,    /* 0.10.0 */
    .connectListDefinedDomains = vzConnectListDefinedDomains,        /* 0.10.0 */
    .connectNumOfDefinedDomains = vzConnectNumOfDefinedDomains,      /* 0.10.0 */
    .connectListAllDomains = vzConnectListAllDomains, /* 0.10.0 */
    .domainLookupByID = vzDomainLookupByID,    /* 0.10.0 */
    .domainLookupByUUID = vzDomainLookupByUUID,        /* 0.10.0 */
    .domainLookupByName = vzDomainLookupByName,        /* 0.10.0 */
    .domainGetOSType = vzDomainGetOSType,    /* 0.10.0 */
    .domainGetInfo = vzDomainGetInfo,  /* 0.10.0 */
    .domainGetState = vzDomainGetState,        /* 0.10.0 */
    .domainGetXMLDesc = vzDomainGetXMLDesc,    /* 0.10.0 */
    .domainIsPersistent = vzDomainIsPersistent,        /* 0.10.0 */
    .domainGetAutostart = vzDomainGetAutostart,        /* 0.10.0 */
    .domainGetVcpus = vzDomainGetVcpus, /* 1.2.6 */
    .domainSuspend = vzDomainSuspend,    /* 0.10.0 */
    .domainResume = vzDomainResume,    /* 0.10.0 */
    .domainDestroy = vzDomainDestroy,  /* 0.10.0 */
    .domainShutdown = vzDomainShutdown, /* 0.10.0 */
    .domainCreate = vzDomainCreate,    /* 0.10.0 */
    .domainCreateWithFlags = vzDomainCreateWithFlags, /* 1.2.10 */
P
Pavel Hrdina 已提交
1565
    .domainReboot = vzDomainReboot, /* 1.3.0 */
1566 1567 1568 1569 1570 1571 1572 1573 1574
    .domainDefineXML = vzDomainDefineXML,      /* 0.10.0 */
    .domainDefineXMLFlags = vzDomainDefineXMLFlags, /* 1.2.12 */
    .domainUndefine = vzDomainUndefine, /* 1.2.10 */
    .domainUndefineFlags = vzDomainUndefineFlags, /* 1.2.10 */
    .domainAttachDevice = vzDomainAttachDevice, /* 1.2.15 */
    .domainAttachDeviceFlags = vzDomainAttachDeviceFlags, /* 1.2.15 */
    .domainDetachDevice = vzDomainDetachDevice, /* 1.2.15 */
    .domainDetachDeviceFlags = vzDomainDetachDeviceFlags, /* 1.2.15 */
    .domainIsActive = vzDomainIsActive, /* 1.2.10 */
1575 1576 1577
    .domainIsUpdated = vzDomainIsUpdated,     /* 1.2.21 */
    .domainGetVcpusFlags = vzDomainGetVcpusFlags, /* 1.2.21 */
    .domainGetMaxVcpus = vzDomainGetMaxVcpus, /* 1.2.21 */
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
    .connectDomainEventRegisterAny = vzConnectDomainEventRegisterAny, /* 1.2.10 */
    .connectDomainEventDeregisterAny = vzConnectDomainEventDeregisterAny, /* 1.2.10 */
    .nodeGetCPUMap = vzNodeGetCPUMap, /* 1.2.8 */
    .connectIsEncrypted = vzConnectIsEncrypted, /* 1.2.5 */
    .connectIsSecure = vzConnectIsSecure, /* 1.2.5 */
    .connectIsAlive = vzConnectIsAlive, /* 1.2.5 */
    .domainHasManagedSaveImage = vzDomainHasManagedSaveImage, /* 1.2.13 */
    .domainManagedSave = vzDomainManagedSave, /* 1.2.14 */
    .domainManagedSaveRemove = vzDomainManagedSaveRemove, /* 1.2.14 */
    .domainGetMaxMemory = vzDomainGetMaxMemory, /* 1.2.15 */
1588 1589 1590 1591
    .domainBlockStats = vzDomainBlockStats, /* 1.2.17 */
    .domainBlockStatsFlags = vzDomainBlockStatsFlags, /* 1.2.17 */
    .domainInterfaceStats = vzDomainInterfaceStats, /* 1.2.17 */
    .domainMemoryStats = vzDomainMemoryStats, /* 1.2.17 */
1592 1593
    .connectRegisterCloseCallback = vzConnectRegisterCloseCallback, /* 1.3.2 */
    .connectUnregisterCloseCallback = vzConnectUnregisterCloseCallback, /* 1.3.2 */
D
Dmitry Guryanov 已提交
1594 1595
};

1596 1597
static virConnectDriver vzConnectDriver = {
    .hypervisorDriver = &vzDriver,
1598 1599
};

1600 1601 1602 1603
/* Parallels domain type backward compatibility*/
static virHypervisorDriver parallelsDriver;
static virConnectDriver parallelsConnectDriver;

D
Dmitry Guryanov 已提交
1604
/**
1605
 * vzRegister:
D
Dmitry Guryanov 已提交
1606
 *
1607
 * Registers the vz driver
D
Dmitry Guryanov 已提交
1608 1609
 */
int
1610
vzRegister(void)
D
Dmitry Guryanov 已提交
1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621
{
    char *prlctl_path;

    prlctl_path = virFindFileInPath(PRLCTL);
    if (!prlctl_path) {
        VIR_DEBUG("%s", _("Can't find prlctl command in the PATH env"));
        return 0;
    }

    VIR_FREE(prlctl_path);

1622 1623 1624 1625 1626
    /* Backward compatibility with Parallels domain type */
    parallelsDriver = vzDriver;
    parallelsDriver.name = "Parallels";
    parallelsConnectDriver = vzConnectDriver;
    parallelsConnectDriver.hypervisorDriver = &parallelsDriver;
1627
    if (virRegisterConnectDriver(&parallelsConnectDriver, false) < 0)
D
Dmitry Guryanov 已提交
1628
        return -1;
D
Dmitry Guryanov 已提交
1629

1630 1631 1632
    if (virRegisterConnectDriver(&vzConnectDriver, false) < 0)
        return -1;

D
Dmitry Guryanov 已提交
1633 1634
    return 0;
}