vz_driver.c 42.3 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
 * <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 <pwd.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/statvfs.h>

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

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

#define VIR_FROM_THIS VIR_FROM_PARALLELS

61 62
VIR_LOG_INIT("parallels.parallels_driver");

D
Dmitry Guryanov 已提交
63 64
#define PRLCTL                      "prlctl"

65
static int vzConnectClose(virConnectPtr conn);
D
Dmitry Guryanov 已提交
66

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

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

D
Dmitry Guryanov 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
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 已提交
100
static virCapsPtr
101
vzBuildCapabilities(void)
D
Dmitry Guryanov 已提交
102
{
103 104 105 106
    virCapsPtr caps = NULL;
    virCPUDefPtr cpu = NULL;
    virCPUDataPtr data = NULL;
    virNodeInfo nodeinfo;
D
Dmitry Guryanov 已提交
107 108 109 110 111 112 113 114 115 116 117
    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 已提交
118

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

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

D
Dmitry Guryanov 已提交
126 127 128 129 130 131
    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;
132

133
    if (nodeGetInfo(NULL, &nodeinfo))
134 135
        goto error;

136
    if (VIR_ALLOC(cpu) < 0)
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
        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 已提交
154 155
    return caps;

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

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

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

173
static int
174
vzDomainDefPostParse(virDomainDefPtr def ATTRIBUTE_UNUSED,
M
Michal Privoznik 已提交
175
                     virCapsPtr caps ATTRIBUTE_UNUSED,
176
                     unsigned int parseFlags ATTRIBUTE_UNUSED,
177
                     void *opaque)
178
{
179 180 181
    if (vzCheckUnsupportedDisks(def, opaque) < 0)
        return -1;

182 183 184
    if (vzCheckUnsupportedControllers(def, opaque) < 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 242 243
    if (vzInitVersion(privconn) < 0)
        goto error;

244
    if (!(privconn->caps = vzBuildCapabilities()))
D
Dmitry Guryanov 已提交
245 246
        goto error;

247
    vzDomainDefParserConfig.priv = &privconn->vzCaps;
248
    if (!(privconn->xmlopt = virDomainXMLOptionNew(&vzDomainDefParserConfig,
M
Michal Privoznik 已提交
249
                                                   NULL, NULL)))
250 251
        goto error;

252
    if (!(privconn->domains = virDomainObjListNew()))
D
Dmitry Guryanov 已提交
253 254
        goto error;

255 256 257 258 259 260
    if (!(privconn->domainEventState = virObjectEventStateNew()))
        goto error;

    if (prlsdkSubscribeToPCSEvents(privconn))
        goto error;

261 262 263
    if (!(privconn->closeCallback = virNewConnectCloseCallbackData()))
        goto error;

D
Dmitry Guryanov 已提交
264 265
    conn->privateData = privconn;

266
    if (prlsdkLoadDomains(privconn))
267 268
        goto error;

D
Dmitry Guryanov 已提交
269 270
    return VIR_DRV_OPEN_SUCCESS;

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

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

    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

    if (!conn->uri)
        return VIR_DRV_OPEN_DECLINED;

297 298 299 300 301 302 303 304 305 306 307
    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 已提交
308 309 310 311 312 313 314
        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. */
315
    if (STRNEQ_NULLABLE(conn->uri->path, "/system")) {
316
        virReportError(VIR_ERR_INTERNAL_ERROR,
317
                       _("Unexpected Virtuozzo URI path '%s', try vz:///system"),
318
                       conn->uri->path);
D
Dmitry Guryanov 已提交
319 320 321
        return VIR_DRV_OPEN_ERROR;
    }

322
    if ((ret = vzOpenDefault(conn)) != VIR_DRV_OPEN_SUCCESS)
D
Dmitry Guryanov 已提交
323 324 325 326 327 328
        return ret;

    return VIR_DRV_OPEN_SUCCESS;
}

static int
329
vzConnectClose(virConnectPtr conn)
D
Dmitry Guryanov 已提交
330
{
331
    vzConnPtr privconn = conn->privateData;
D
Dmitry Guryanov 已提交
332

333 334 335
    if (!privconn)
        return 0;

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

348
    vzDriverUnlock(privconn);
D
Dmitry Guryanov 已提交
349 350 351 352 353 354 355
    virMutexDestroy(&privconn->lock);

    VIR_FREE(privconn);
    return 0;
}

static int
356
vzConnectGetVersion(virConnectPtr conn, unsigned long *hvVer)
D
Dmitry Guryanov 已提交
357
{
358 359 360
    vzConnPtr privconn = conn->privateData;
    *hvVer = privconn->vzVersion;
    return 0;
361 362
}

363

364
static char *vzConnectGetHostname(virConnectPtr conn ATTRIBUTE_UNUSED)
365 366 367 368 369
{
    return virGetHostname();
}


370
static int
371
vzConnectListDomains(virConnectPtr conn, int *ids, int maxids)
372
{
373
    vzConnPtr privconn = conn->privateData;
374 375
    int n;

376
    vzDriverLock(privconn);
377 378
    n = virDomainObjListGetActiveIDs(privconn->domains, ids, maxids,
                                     NULL, NULL);
379
    vzDriverUnlock(privconn);
380 381 382 383 384

    return n;
}

static int
385
vzConnectNumOfDomains(virConnectPtr conn)
386
{
387
    vzConnPtr privconn = conn->privateData;
388 389
    int count;

390
    vzDriverLock(privconn);
391 392
    count = virDomainObjListNumOfDomains(privconn->domains, true,
                                         NULL, NULL);
393
    vzDriverUnlock(privconn);
394 395 396 397 398

    return count;
}

static int
399
vzConnectListDefinedDomains(virConnectPtr conn, char **const names, int maxnames)
400
{
401
    vzConnPtr privconn = conn->privateData;
402 403
    int n;

404
    vzDriverLock(privconn);
405
    memset(names, 0, sizeof(*names) * maxnames);
406
    n = virDomainObjListGetInactiveNames(privconn->domains, names,
407
                                         maxnames, NULL, NULL);
408
    vzDriverUnlock(privconn);
409 410 411 412 413

    return n;
}

static int
414
vzConnectNumOfDefinedDomains(virConnectPtr conn)
415
{
416
    vzConnPtr privconn = conn->privateData;
417 418
    int count;

419
    vzDriverLock(privconn);
420 421
    count = virDomainObjListNumOfDomains(privconn->domains, false,
                                         NULL, NULL);
422
    vzDriverUnlock(privconn);
423 424 425 426 427

    return count;
}

static int
428
vzConnectListAllDomains(virConnectPtr conn,
M
Michal Privoznik 已提交
429 430
                        virDomainPtr **domains,
                        unsigned int flags)
431
{
432
    vzConnPtr privconn = conn->privateData;
433 434
    int ret = -1;

O
Osier Yang 已提交
435
    virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1);
436
    vzDriverLock(privconn);
437 438
    ret = virDomainObjListExport(privconn->domains, conn, domains,
                                 NULL, flags);
439
    vzDriverUnlock(privconn);
440 441 442 443 444

    return ret;
}

static virDomainPtr
445
vzDomainLookupByID(virConnectPtr conn, int id)
446
{
447
    vzConnPtr privconn = conn->privateData;
448 449 450
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;

451
    vzDriverLock(privconn);
452
    dom = virDomainObjListFindByID(privconn->domains, id);
453
    vzDriverUnlock(privconn);
454 455 456 457 458 459 460 461 462 463

    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;

464
 cleanup:
465
    if (dom)
466
        virObjectUnlock(dom);
467 468 469 470
    return ret;
}

static virDomainPtr
471
vzDomainLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
472
{
473
    vzConnPtr privconn = conn->privateData;
474 475 476
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;

477
    vzDriverLock(privconn);
478
    dom = virDomainObjListFindByUUID(privconn->domains, uuid);
479
    vzDriverUnlock(privconn);
480 481 482 483 484 485 486 487 488 489 490 491 492

    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;

493
 cleanup:
494
    if (dom)
495
        virObjectUnlock(dom);
496 497 498 499
    return ret;
}

static virDomainPtr
500
vzDomainLookupByName(virConnectPtr conn, const char *name)
501
{
502
    vzConnPtr privconn = conn->privateData;
503 504 505
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;

506
    vzDriverLock(privconn);
507
    dom = virDomainObjListFindByName(privconn->domains, name);
508
    vzDriverUnlock(privconn);
509 510 511 512 513 514 515 516 517 518 519

    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;

520
 cleanup:
521
    virDomainObjEndAPI(&dom);
522 523 524 525
    return ret;
}

static int
526
vzDomainGetInfo(virDomainPtr domain, virDomainInfoPtr info)
527 528 529 530
{
    virDomainObjPtr privdom;
    int ret = -1;

531
    if (!(privdom = vzDomObjFromDomainRef(domain)))
532 533 534 535
        goto cleanup;

    info->state = virDomainObjGetState(privdom, NULL);
    info->memory = privdom->def->mem.cur_balloon;
536
    info->maxMem = virDomainDefGetMemoryActual(privdom->def);
537
    info->nrVirtCpu = virDomainDefGetVcpus(privdom->def);
538
    info->cpuTime = 0;
539 540 541 542 543

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

544
        for (i = 0; i < virDomainDefGetVcpus(privdom->def); ++i) {
545 546 547 548 549 550 551 552
            if (prlsdkGetVcpuStats(privdom, i, &vtime) < 0) {
                virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                               _("cannot read cputime for domain"));
                goto cleanup;
            }
            info->cpuTime += vtime;
        }
    }
553 554
    ret = 0;

555
 cleanup:
556
    virDomainObjEndAPI(&privdom);
557 558 559 560
    return ret;
}

static char *
561
vzDomainGetOSType(virDomainPtr domain)
562 563 564 565 566
{
    virDomainObjPtr privdom;

    char *ret = NULL;

567
    if (!(privdom = vzDomObjFromDomain(domain)))
568 569
        goto cleanup;

570
    ignore_value(VIR_STRDUP(ret, virDomainOSTypeToString(privdom->def->os.type)));
571

572
 cleanup:
573
    if (privdom)
574
        virObjectUnlock(privdom);
575 576 577 578
    return ret;
}

static int
579
vzDomainIsPersistent(virDomainPtr domain)
580 581 582 583
{
    virDomainObjPtr privdom;
    int ret = -1;

584
    if (!(privdom = vzDomObjFromDomain(domain)))
585 586 587 588
        goto cleanup;

    ret = 1;

589
 cleanup:
590
    if (privdom)
591
        virObjectUnlock(privdom);
592 593 594 595
    return ret;
}

static int
596
vzDomainGetState(virDomainPtr domain,
M
Michal Privoznik 已提交
597
                 int *state, int *reason, unsigned int flags)
598 599 600 601 602
{
    virDomainObjPtr privdom;
    int ret = -1;
    virCheckFlags(0, -1);

603
    if (!(privdom = vzDomObjFromDomain(domain)))
604 605 606 607 608
        goto cleanup;

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

609
 cleanup:
610
    if (privdom)
611
        virObjectUnlock(privdom);
612 613 614 615
    return ret;
}

static char *
616
vzDomainGetXMLDesc(virDomainPtr domain, unsigned int flags)
617
{
618
    vzConnPtr privconn = domain->conn->privateData;
619 620 621 622 623 624
    virDomainDefPtr def;
    virDomainObjPtr privdom;
    char *ret = NULL;

    /* Flags checked by virDomainDefFormat */

625
    if (!(privdom = vzDomObjFromDomain(domain)))
626 627 628 629 630
        goto cleanup;

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

631
    ret = virDomainDefFormat(def, privconn->caps, flags);
632

633
 cleanup:
634
    if (privdom)
635
        virObjectUnlock(privdom);
636 637 638 639
    return ret;
}

static int
640
vzDomainGetAutostart(virDomainPtr domain, int *autostart)
641 642 643 644
{
    virDomainObjPtr privdom;
    int ret = -1;

645
    if (!(privdom = vzDomObjFromDomain(domain)))
646 647 648 649 650
        goto cleanup;

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

651
 cleanup:
652
    if (privdom)
653
        virObjectUnlock(privdom);
654
    return ret;
D
Dmitry Guryanov 已提交
655 656
}

657
static virDomainPtr
658
vzDomainDefineXMLFlags(virConnectPtr conn, const char *xml, unsigned int flags)
659
{
660
    vzConnPtr privconn = conn->privateData;
661
    virDomainPtr retdom = NULL;
662
    virDomainDefPtr def;
663
    virDomainObjPtr olddom = NULL;
664
    virDomainObjPtr newdom = NULL;
665
    unsigned int parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE;
666

667 668 669 670
    virCheckFlags(VIR_DOMAIN_DEFINE_VALIDATE, NULL);

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

672
    vzDriverLock(privconn);
673
    if ((def = virDomainDefParseString(xml, privconn->caps, privconn->xmlopt,
674
                                       parse_flags)) == NULL)
675 676
        goto cleanup;

677 678 679
    olddom = virDomainObjListFindByUUID(privconn->domains, def->uuid);
    if (olddom == NULL) {
        virResetLastError();
680 681 682
        newdom = vzNewDomain(privconn, def->name, def->uuid);
        if (!newdom)
            goto cleanup;
683
        if (def->os.type == VIR_DOMAIN_OSTYPE_HVM) {
684
            if (prlsdkCreateVm(conn, def))
685
                goto cleanup;
686
        } else if (def->os.type == VIR_DOMAIN_OSTYPE_EXE) {
687
            if (prlsdkCreateCt(conn, def))
688 689 690
                goto cleanup;
        } else {
            virReportError(VIR_ERR_INVALID_ARG,
691 692
                           _("Unsupported OS type: %s"),
                           virDomainOSTypeToString(def->os.type));
693
            goto cleanup;
694
        }
695

696
        if (prlsdkLoadDomain(privconn, newdom))
697
            goto cleanup;
698
    } else {
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
        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;
724

725 726 727
            if (prlsdkUpdateDomain(privconn, olddom))
                goto cleanup;
        }
728 729
    }

730 731 732
    retdom = virGetDomain(conn, def->name, def->uuid);
    if (retdom)
        retdom->id = def->id;
733

734
 cleanup:
735 736
    if (olddom)
        virObjectUnlock(olddom);
737 738 739 740 741 742
    if (newdom) {
        if (!retdom)
             virDomainObjListRemove(privconn->domains, newdom);
        else
             virObjectUnlock(newdom);
    }
743
    virDomainDefFree(def);
744
    vzDriverUnlock(privconn);
745
    return retdom;
746 747
}

748
static virDomainPtr
749
vzDomainDefineXML(virConnectPtr conn, const char *xml)
750
{
751
    return vzDomainDefineXMLFlags(conn, xml, 0);
752 753 754
}


755
static int
756
vzNodeGetInfo(virConnectPtr conn ATTRIBUTE_UNUSED,
M
Michal Privoznik 已提交
757
              virNodeInfoPtr nodeinfo)
758
{
759
    return nodeGetInfo(NULL, nodeinfo);
760 761
}

762
static int vzConnectIsEncrypted(virConnectPtr conn ATTRIBUTE_UNUSED)
763 764 765 766 767
{
    /* Encryption is not relevant / applicable to way we talk to PCS */
    return 0;
}

768
static int vzConnectIsSecure(virConnectPtr conn ATTRIBUTE_UNUSED)
769 770 771 772 773
{
    /* We run CLI tools directly so this is secure */
    return 1;
}

774
static int vzConnectIsAlive(virConnectPtr conn ATTRIBUTE_UNUSED)
775 776 777 778
{
    return 1;
}

779

780
static char *
781
vzConnectBaselineCPU(virConnectPtr conn ATTRIBUTE_UNUSED,
M
Michal Privoznik 已提交
782 783 784
                     const char **xmlCPUs,
                     unsigned int ncpus,
                     unsigned int flags)
785 786 787 788 789 790 791
{
    virCheckFlags(VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES, NULL);

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


792
static int
793
vzDomainGetVcpus(virDomainPtr domain,
M
Michal Privoznik 已提交
794 795 796 797
                 virVcpuInfoPtr info,
                 int maxinfo,
                 unsigned char *cpumaps,
                 int maplen)
798 799 800 801 802
{
    virDomainObjPtr privdom = NULL;
    size_t i;
    int ret = -1;

N
Nikolay Shirokovskiy 已提交
803
    if (!(privdom = vzDomObjFromDomainRef(domain)))
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
        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 已提交
819 820
                if (prlsdkGetVcpuStats(privdom, i, &info[i].cpuTime) < 0)
                    goto cleanup;
821 822 823 824
            }
        }
        if (cpumaps != NULL) {
            memset(cpumaps, 0, maplen * maxinfo);
825 826 827 828
            for (i = 0; i < maxinfo; i++)
                virBitmapToDataBuf(privdom->def->cpumask,
                                   VIR_GET_CPUMAP(cpumaps, maplen, i),
                                   maplen);
829 830 831 832 833 834
        }
    }
    ret = maxinfo;

 cleanup:
    if (privdom)
N
Nikolay Shirokovskiy 已提交
835
        virDomainObjEndAPI(&privdom);
836 837 838 839
    return ret;
}


840
static int
841
vzNodeGetCPUMap(virConnectPtr conn ATTRIBUTE_UNUSED,
M
Michal Privoznik 已提交
842 843 844
                unsigned char **cpumap,
                unsigned int *online,
                unsigned int flags)
845
{
846
    return nodeGetCPUMap(NULL, cpumap, online, flags);
847 848
}

849
static int
850
vzConnectDomainEventRegisterAny(virConnectPtr conn,
M
Michal Privoznik 已提交
851 852 853 854 855
                                virDomainPtr domain,
                                int eventID,
                                virConnectDomainEventGenericCallback callback,
                                void *opaque,
                                virFreeCallback freecb)
856 857
{
    int ret = -1;
858
    vzConnPtr privconn = conn->privateData;
859 860 861 862 863 864 865 866 867
    if (virDomainEventStateRegisterID(conn,
                                      privconn->domainEventState,
                                      domain, eventID,
                                      callback, opaque, freecb, &ret) < 0)
        ret = -1;
    return ret;
}

static int
868
vzConnectDomainEventDeregisterAny(virConnectPtr conn,
M
Michal Privoznik 已提交
869
                                  int callbackID)
870
{
871
    vzConnPtr privconn = conn->privateData;
872 873 874 875 876 877 878 879 880 881 882 883
    int ret = -1;

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

    ret = 0;

 cleanup:
    return ret;
}
884

885
static int vzDomainSuspend(virDomainPtr domain)
886 887 888 889
{
    return prlsdkDomainChangeState(domain, prlsdkPause);
}

890
static int vzDomainResume(virDomainPtr domain)
891 892 893 894
{
    return prlsdkDomainChangeState(domain, prlsdkResume);
}

895
static int vzDomainCreate(virDomainPtr domain)
896 897 898 899
{
    return prlsdkDomainChangeState(domain, prlsdkStart);
}

900
static int vzDomainDestroy(virDomainPtr domain)
901 902 903 904
{
    return prlsdkDomainChangeState(domain, prlsdkKill);
}

905
static int vzDomainShutdown(virDomainPtr domain)
906 907 908 909
{
    return prlsdkDomainChangeState(domain, prlsdkStop);
}

910 911 912 913 914 915 916
static int vzDomainReboot(virDomainPtr domain,
                          unsigned int flags)
{
    virCheckFlags(0, -1);
    return prlsdkDomainChangeState(domain, prlsdkRestart);
}

917
static int vzDomainIsActive(virDomainPtr domain)
918 919 920 921
{
    virDomainObjPtr dom = NULL;
    int ret = -1;

922
    if (!(dom = vzDomObjFromDomain(domain)))
923 924 925 926 927 928 929 930
        return -1;

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

    return ret;
}

931
static int
932
vzDomainCreateWithFlags(virDomainPtr domain, unsigned int flags)
933 934 935 936
{
    /* we don't support any create flags */
    virCheckFlags(0, -1);

937
    return vzDomainCreate(domain);
938 939
}

940
static int
941
vzDomainUndefineFlags(virDomainPtr domain,
M
Michal Privoznik 已提交
942
                      unsigned int flags)
943
{
944
    vzConnPtr privconn = domain->conn->privateData;
945
    virDomainObjPtr dom = NULL;
946
    int ret;
947

948 949
    virCheckFlags(VIR_DOMAIN_UNDEFINE_MANAGED_SAVE |
                  VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA, -1);
950

951
    if (!(dom = vzDomObjFromDomain(domain)))
952 953
        return -1;

954
    ret = prlsdkUnregisterDomain(privconn, dom, flags);
955
    if (ret)
M
Michal Privoznik 已提交
956
        virObjectUnlock(dom);
957 958

    return ret;
959 960 961
}

static int
962
vzDomainUndefine(virDomainPtr domain)
963
{
964
    return vzDomainUndefineFlags(domain, 0);
965 966
}

967
static int
968
vzDomainHasManagedSaveImage(virDomainPtr domain, unsigned int flags)
969 970
{
    virDomainObjPtr dom = NULL;
971 972
    int state, reason;
    int ret = 0;
973 974 975

    virCheckFlags(0, -1);

976
    if (!(dom = vzDomObjFromDomain(domain)))
977 978
        return -1;

979 980 981
    state = virDomainObjGetState(dom, &reason);
    if (state == VIR_DOMAIN_SHUTOFF && reason == VIR_DOMAIN_SHUTOFF_SAVED)
        ret = 1;
982 983
    virObjectUnlock(dom);

984 985 986 987
    return ret;
}

static int
988
vzDomainManagedSave(virDomainPtr domain, unsigned int flags)
989
{
990
    vzConnPtr privconn = domain->conn->privateData;
991 992 993 994 995 996 997
    virDomainObjPtr dom = NULL;
    int state, reason;
    int ret = -1;

    virCheckFlags(VIR_DOMAIN_SAVE_RUNNING |
                  VIR_DOMAIN_SAVE_PAUSED, -1);

998
    if (!(dom = vzDomObjFromDomain(domain)))
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
        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
1017
vzDomainManagedSaveRemove(virDomainPtr domain, unsigned int flags)
1018 1019 1020 1021 1022 1023 1024
{
    virDomainObjPtr dom = NULL;
    int state, reason;
    int ret = -1;

    virCheckFlags(0, -1);

1025
    if (!(dom = vzDomObjFromDomain(domain)))
1026 1027 1028 1029 1030 1031 1032
        return -1;

    state = virDomainObjGetState(dom, &reason);

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

1033
    ret = prlsdkDomainManagedSaveRemove(dom);
1034 1035 1036 1037

 cleanup:
    virObjectUnlock(dom);
    return ret;
1038 1039
}

1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
static int vzCheckConfigUpdateFlags(virDomainObjPtr dom, unsigned int *flags)
{
    if (virDomainObjUpdateModificationImpact(dom, flags) < 0)
        return -1;

    if (!(*flags & VIR_DOMAIN_AFFECT_CONFIG)) {
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("domain config update needs VIR_DOMAIN_AFFECT_CONFIG "
                         "flag to be set"));
        return -1;
    }

    if (virDomainObjIsActive(dom) && !(*flags & VIR_DOMAIN_AFFECT_LIVE)) {
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("Updates on a running domain need "
                         "VIR_DOMAIN_AFFECT_LIVE flag"));
        return -1;
    }

    return 0;
}

1062
static int vzDomainAttachDeviceFlags(virDomainPtr dom, const char *xml,
M
Michal Privoznik 已提交
1063
                                     unsigned int flags)
1064 1065
{
    int ret = -1;
1066
    vzConnPtr privconn = dom->conn->privateData;
1067 1068 1069 1070 1071 1072
    virDomainDeviceDefPtr dev = NULL;
    virDomainObjPtr privdom = NULL;

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, -1);

1073
    if (!(privdom = vzDomObjFromDomain(dom)))
1074
        return -1;
1075

1076
    if (vzCheckConfigUpdateFlags(privdom, &flags) < 0)
1077 1078 1079 1080 1081 1082 1083 1084 1085
        goto cleanup;

    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:
M
Mikhail Feoktistov 已提交
1086
        ret = prlsdkAttachVolume(privconn, privdom, dev->data.disk);
1087 1088 1089 1090 1091 1092
        if (ret) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("disk attach failed"));
            goto cleanup;
        }
        break;
1093
    case VIR_DOMAIN_DEVICE_NET:
M
Mikhail Feoktistov 已提交
1094
        ret = prlsdkAttachNet(privconn, privdom, dev->data.net);
1095 1096 1097 1098 1099 1100
        if (ret) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("network attach failed"));
            goto cleanup;
        }
        break;
1101 1102
    default:
        virReportError(VIR_ERR_OPERATION_UNSUPPORTED,
1103
                       _("device type '%s' cannot be attached"),
1104 1105 1106 1107 1108 1109
                       virDomainDeviceTypeToString(dev->type));
        break;
    }

    ret = 0;
 cleanup:
1110
    virObjectUnlock(privdom);
1111 1112 1113
    return ret;
}

1114
static int vzDomainAttachDevice(virDomainPtr dom, const char *xml)
1115
{
1116
    return vzDomainAttachDeviceFlags(dom, xml,
M
Michal Privoznik 已提交
1117
                                     VIR_DOMAIN_AFFECT_CONFIG | VIR_DOMAIN_AFFECT_LIVE);
1118 1119
}

1120
static int vzDomainDetachDeviceFlags(virDomainPtr dom, const char *xml,
M
Michal Privoznik 已提交
1121
                                     unsigned int flags)
1122 1123
{
    int ret = -1;
1124
    vzConnPtr privconn = dom->conn->privateData;
1125 1126 1127 1128 1129 1130
    virDomainDeviceDefPtr dev = NULL;
    virDomainObjPtr privdom = NULL;

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, -1);

1131
    privdom = vzDomObjFromDomain(dom);
1132 1133 1134
    if (privdom == NULL)
        return -1;

1135
    if (vzCheckConfigUpdateFlags(privdom, &flags) < 0)
1136 1137 1138 1139 1140 1141 1142 1143 1144
        goto cleanup;

    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:
1145
        ret = prlsdkDetachVolume(privdom, dev->data.disk);
1146 1147 1148 1149 1150 1151
        if (ret) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("disk detach failed"));
            goto cleanup;
        }
        break;
1152
    case VIR_DOMAIN_DEVICE_NET:
M
Mikhail Feoktistov 已提交
1153
        ret = prlsdkDetachNet(privconn, privdom, dev->data.net);
1154 1155 1156 1157 1158 1159
        if (ret) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("network detach failed"));
            goto cleanup;
        }
        break;
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
    default:
        virReportError(VIR_ERR_OPERATION_UNSUPPORTED,
                       _("device type '%s' cannot be detached"),
                       virDomainDeviceTypeToString(dev->type));
        break;
    }

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

1173
static int vzDomainDetachDevice(virDomainPtr dom, const char *xml)
1174
{
1175
    return vzDomainDetachDeviceFlags(dom, xml,
M
Michal Privoznik 已提交
1176
                                     VIR_DOMAIN_AFFECT_CONFIG | VIR_DOMAIN_AFFECT_LIVE);
1177 1178
}

1179
static unsigned long long
1180
vzDomainGetMaxMemory(virDomainPtr domain)
1181 1182 1183 1184
{
    virDomainObjPtr dom = NULL;
    int ret = -1;

1185
    if (!(dom = vzDomObjFromDomain(domain)))
1186 1187
        return -1;

1188
    ret = virDomainDefGetMemoryActual(dom->def);
1189 1190 1191 1192
    virObjectUnlock(dom);
    return ret;
}

1193
static int
1194
vzDomainBlockStats(virDomainPtr domain, const char *path,
M
Michal Privoznik 已提交
1195
                   virDomainBlockStatsPtr stats)
1196 1197 1198 1199 1200 1201
{
    virDomainObjPtr dom = NULL;
    int ret = -1;
    size_t i;
    int idx;

1202
    if (!(dom = vzDomObjFromDomainRef(domain)))
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
        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 已提交
1215
#define PARALLELS_ZERO_STATS(VAR, TYPE, NAME)      \
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
        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;

1226
#define PARALLELS_SUM_STATS(VAR, TYPE, NAME)        \
M
Michal Privoznik 已提交
1227 1228
    if (s.VAR != -1)                                \
        stats->VAR += s.VAR;
1229

M
Michal Privoznik 已提交
1230
        PARALLELS_BLOCK_STATS_FOREACH(PARALLELS_SUM_STATS)
1231

1232
#undef PARALLELS_SUM_STATS
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
        }
    }
    stats->errs = -1;
    ret = 0;

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

    return ret;
}

static int
1246
vzDomainBlockStatsFlags(virDomainPtr domain,
M
Michal Privoznik 已提交
1247 1248 1249 1250
                        const char *path,
                        virTypedParameterPtr params,
                        int *nparams,
                        unsigned int flags)
1251 1252 1253 1254 1255 1256 1257 1258 1259
{
    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;

1260
    if (vzDomainBlockStats(domain, path, &stats) < 0)
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
        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;
}

1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
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;
}
1311

N
Nikolay Shirokovskiy 已提交
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
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;
}

1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345
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)
1346
        ret = virDomainDefGetVcpusMax(privdom->def);
1347
    else
1348
        ret = virDomainDefGetVcpus(privdom->def);
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380

 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;
}

1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
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;
}

1394 1395 1396 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 1429 1430 1431
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;
}

1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 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 1480 1481
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;
}

1482 1483
static virHypervisorDriver vzDriver = {
    .name = "vz",
1484 1485 1486 1487
    .connectOpen = vzConnectOpen,            /* 0.10.0 */
    .connectClose = vzConnectClose,          /* 0.10.0 */
    .connectGetVersion = vzConnectGetVersion,   /* 0.10.0 */
    .connectGetHostname = vzConnectGetHostname,      /* 0.10.0 */
1488
    .connectGetMaxVcpus = vzConnectGetMaxVcpus, /* 1.2.21 */
1489
    .nodeGetInfo = vzNodeGetInfo,      /* 0.10.0 */
1490 1491 1492 1493
    .nodeGetCPUStats = vzNodeGetCPUStats,      /* 1.2.21 */
    .nodeGetMemoryStats = vzNodeGetMemoryStats, /* 1.2.21 */
    .nodeGetCellsFreeMemory = vzNodeGetCellsFreeMemory, /* 1.2.21 */
    .nodeGetFreeMemory = vzNodeGetFreeMemory, /* 1.2.21 */
1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
    .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 已提交
1517
    .domainReboot = vzDomainReboot, /* 1.3.0 */
1518 1519 1520 1521 1522 1523 1524 1525 1526
    .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 */
1527 1528 1529
    .domainIsUpdated = vzDomainIsUpdated,     /* 1.2.21 */
    .domainGetVcpusFlags = vzDomainGetVcpusFlags, /* 1.2.21 */
    .domainGetMaxVcpus = vzDomainGetMaxVcpus, /* 1.2.21 */
1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
    .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 */
1540 1541 1542 1543
    .domainBlockStats = vzDomainBlockStats, /* 1.2.17 */
    .domainBlockStatsFlags = vzDomainBlockStatsFlags, /* 1.2.17 */
    .domainInterfaceStats = vzDomainInterfaceStats, /* 1.2.17 */
    .domainMemoryStats = vzDomainMemoryStats, /* 1.2.17 */
1544 1545
    .connectRegisterCloseCallback = vzConnectRegisterCloseCallback, /* 1.3.2 */
    .connectUnregisterCloseCallback = vzConnectUnregisterCloseCallback, /* 1.3.2 */
D
Dmitry Guryanov 已提交
1546 1547
};

1548 1549
static virConnectDriver vzConnectDriver = {
    .hypervisorDriver = &vzDriver,
1550 1551
};

1552 1553 1554 1555
/* Parallels domain type backward compatibility*/
static virHypervisorDriver parallelsDriver;
static virConnectDriver parallelsConnectDriver;

D
Dmitry Guryanov 已提交
1556
/**
1557
 * vzRegister:
D
Dmitry Guryanov 已提交
1558
 *
1559
 * Registers the vz driver
D
Dmitry Guryanov 已提交
1560 1561
 */
int
1562
vzRegister(void)
D
Dmitry Guryanov 已提交
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
{
    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);

1574 1575 1576 1577 1578
    /* Backward compatibility with Parallels domain type */
    parallelsDriver = vzDriver;
    parallelsDriver.name = "Parallels";
    parallelsConnectDriver = vzConnectDriver;
    parallelsConnectDriver.hypervisorDriver = &parallelsDriver;
1579
    if (virRegisterConnectDriver(&parallelsConnectDriver, false) < 0)
D
Dmitry Guryanov 已提交
1580
        return -1;
D
Dmitry Guryanov 已提交
1581

1582 1583 1584
    if (virRegisterConnectDriver(&vzConnectDriver, false) < 0)
        return -1;

D
Dmitry Guryanov 已提交
1585 1586
    return 0;
}