vz_sdk.c 112.0 KB
Newer Older
1
/*
2
 * vz_sdk.c: core driver functions for managing
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * Parallels Cloud Server hosts
 *
 * Copyright (C) 2014 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
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 */

#include <config.h>
24
#include <stdarg.h>
25 26 27

#include "virerror.h"
#include "viralloc.h"
28 29 30
#include "virstring.h"
#include "nodeinfo.h"
#include "virlog.h"
31
#include "datatypes.h"
32
#include "domain_conf.h"
33
#include "virtime.h"
34

35
#include "vz_sdk.h"
36 37 38 39

#define VIR_FROM_THIS VIR_FROM_PARALLELS
#define JOB_INFINIT_WAIT_TIMEOUT UINT_MAX

40 41
VIR_LOG_INIT("parallels.sdk");

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
/*
 * Log error description
 */
static void
logPrlErrorHelper(PRL_RESULT err, const char *filename,
                  const char *funcname, size_t linenr)
{
    char *msg1 = NULL, *msg2 = NULL;
    PRL_UINT32 len = 0;

    /* Get required buffer length */
    PrlApi_GetResultDescription(err, PRL_TRUE, PRL_FALSE, NULL, &len);

    if (VIR_ALLOC_N(msg1, len) < 0)
        goto cleanup;

    /* get short error description */
    PrlApi_GetResultDescription(err, PRL_TRUE, PRL_FALSE, msg1, &len);

    PrlApi_GetResultDescription(err, PRL_FALSE, PRL_FALSE, NULL, &len);

    if (VIR_ALLOC_N(msg2, len) < 0)
        goto cleanup;

    /* get long error description */
    PrlApi_GetResultDescription(err, PRL_FALSE, PRL_FALSE, msg2, &len);

    virReportErrorHelper(VIR_FROM_THIS, VIR_ERR_INTERNAL_ERROR,
                         filename, funcname, linenr,
                         _("%s %s"), msg1, msg2);

 cleanup:
    VIR_FREE(msg1);
    VIR_FREE(msg2);
}

#define logPrlError(code)                          \
    logPrlErrorHelper(code, __FILE__,              \
M
Michal Privoznik 已提交
80
                      __FUNCTION__, __LINE__)
81

82 83 84 85 86 87 88 89
#define prlsdkCheckRetGoto(ret, label)             \
    do {                                           \
        if (PRL_FAILED(ret)) {                     \
            logPrlError(ret);                      \
            goto label;                            \
        }                                          \
    } while (0)

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
static PRL_RESULT
logPrlEventErrorHelper(PRL_HANDLE event, const char *filename,
                       const char *funcname, size_t linenr)
{
    PRL_RESULT ret, retCode;
    char *msg1 = NULL, *msg2 = NULL;
    PRL_UINT32 len = 0;
    int err = -1;

    if ((ret = PrlEvent_GetErrCode(event, &retCode))) {
        logPrlError(ret);
        return ret;
    }

    PrlEvent_GetErrString(event, PRL_TRUE, PRL_FALSE, NULL, &len);

    if (VIR_ALLOC_N(msg1, len) < 0)
        goto cleanup;

    PrlEvent_GetErrString(event, PRL_TRUE, PRL_FALSE, msg1, &len);

    PrlEvent_GetErrString(event, PRL_FALSE, PRL_FALSE, NULL, &len);

    if (VIR_ALLOC_N(msg2, len) < 0)
        goto cleanup;

    PrlEvent_GetErrString(event, PRL_FALSE, PRL_FALSE, msg2, &len);

    virReportErrorHelper(VIR_FROM_THIS, VIR_ERR_INTERNAL_ERROR,
                         filename, funcname, linenr,
                         _("%s %s"), msg1, msg2);
    err = 0;

 cleanup:
    VIR_FREE(msg1);
    VIR_FREE(msg2);

    return err;
}

#define logPrlEventError(event)                    \
    logPrlEventErrorHelper(event, __FILE__,        \
M
Michal Privoznik 已提交
132
                           __FUNCTION__, __LINE__)
133

134 135
static PRL_RESULT
getJobResultHelper(PRL_HANDLE job, unsigned int timeout, PRL_HANDLE *result,
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
                   const char *filename, const char *funcname,
                   size_t linenr)
{
    PRL_RESULT ret, retCode;

    if ((ret = PrlJob_Wait(job, timeout))) {
        logPrlErrorHelper(ret, filename, funcname, linenr);
        goto cleanup;
    }

    if ((ret = PrlJob_GetRetCode(job, &retCode))) {
        logPrlErrorHelper(ret, filename, funcname, linenr);
        goto cleanup;
    }

    if (retCode) {
        PRL_HANDLE err_handle;

        /* Sometimes it's possible to get additional error info. */
        if ((ret = PrlJob_GetError(job, &err_handle))) {
            logPrlErrorHelper(ret, filename, funcname, linenr);
            goto cleanup;
        }

        if (logPrlEventErrorHelper(err_handle, filename, funcname, linenr))
            logPrlErrorHelper(retCode, filename, funcname, linenr);

        PrlHandle_Free(err_handle);
164
        ret = retCode;
165
    } else {
166
        ret = PrlJob_GetResult(job, result);
167 168
        if (PRL_FAILED(ret)) {
            logPrlErrorHelper(ret, filename, funcname, linenr);
169 170
            PrlHandle_Free(*result);
            *result = NULL;
171 172
            goto cleanup;
        }
173 174

        ret = PRL_ERR_SUCCESS;
175
    }
176 177 178

 cleanup:
    PrlHandle_Free(job);
179
    return ret;
180 181
}

182 183
#define getJobResult(job, result)                       \
    getJobResultHelper(job, JOB_INFINIT_WAIT_TIMEOUT,   \
M
Michal Privoznik 已提交
184
                       result, __FILE__, __FUNCTION__, __LINE__)
185

186
static PRL_RESULT
187 188 189 190
waitJobHelper(PRL_HANDLE job, unsigned int timeout,
              const char *filename, const char *funcname,
              size_t linenr)
{
191 192
    PRL_HANDLE result = PRL_INVALID_HANDLE;
    PRL_RESULT ret;
193

194 195 196 197
    ret = getJobResultHelper(job, timeout, &result,
                             filename, funcname, linenr);
    PrlHandle_Free(result);
    return ret;
198 199
}

200 201
#define waitJob(job)                                        \
    waitJobHelper(job, JOB_INFINIT_WAIT_TIMEOUT, __FILE__,  \
M
Michal Privoznik 已提交
202
                  __FUNCTION__, __LINE__)
203

204

205
int
206
prlsdkInit(void)
207 208 209
{
    PRL_RESULT ret;

210 211 212
    /* Disable console output */
    PrlApi_SwitchConsoleLogging(0);

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
    ret = PrlApi_InitEx(PARALLELS_API_VER, PAM_SERVER, 0, 0);
    if (PRL_FAILED(ret)) {
        logPrlError(ret);
        return -1;
    }

    return 0;
};

void
prlsdkDeinit(void)
{
    PrlApi_Deinit();
};

int
229
prlsdkConnect(vzConnPtr privconn)
230 231 232 233 234 235 236 237 238 239 240 241 242
{
    PRL_RESULT ret;
    PRL_HANDLE job = PRL_INVALID_HANDLE;

    ret = PrlSrv_Create(&privconn->server);
    if (PRL_FAILED(ret)) {
        logPrlError(ret);
        return -1;
    }

    job = PrlSrv_LoginLocalEx(privconn->server, NULL, 0,
                              PSL_HIGH_SECURITY, PACF_NON_INTERACTIVE_MODE);

243
    if (waitJob(job)) {
244 245 246 247 248 249 250 251
        PrlHandle_Free(privconn->server);
        return -1;
    }

    return 0;
}

void
252
prlsdkDisconnect(vzConnPtr privconn)
253 254 255 256
{
    PRL_HANDLE job;

    job = PrlSrv_Logoff(privconn->server);
257
    waitJob(job);
258 259 260

    PrlHandle_Free(privconn->server);
}
261 262

static int
263
prlsdkSdkDomainLookup(vzConnPtr privconn,
264 265 266 267 268 269 270 271 272 273
                      const char *id,
                      unsigned int flags,
                      PRL_HANDLE *sdkdom)
{
    PRL_HANDLE job = PRL_INVALID_HANDLE;
    PRL_HANDLE result = PRL_INVALID_HANDLE;
    PRL_RESULT pret = PRL_ERR_UNINITIALIZED;
    int ret = -1;

    job = PrlSrv_GetVmConfig(privconn->server, id, flags);
274
    if (PRL_FAILED(getJobResult(job, &result)))
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
        goto cleanup;

    pret = PrlResult_GetParamByIndex(result, 0, sdkdom);
    prlsdkCheckRetGoto(pret, cleanup);

    ret = 0;

 cleanup:
    PrlHandle_Free(result);
    return ret;
}

static void
prlsdkUUIDFormat(const unsigned char *uuid, char *uuidstr)
{
    virUUIDFormat(uuid, uuidstr + 1);

    uuidstr[0] = '{';
    uuidstr[VIR_UUID_STRING_BUFLEN] = '}';
    uuidstr[VIR_UUID_STRING_BUFLEN + 1] = '\0';
}

static PRL_HANDLE
298
prlsdkSdkDomainLookupByUUID(vzConnPtr privconn, const unsigned char *uuid)
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
{
    char uuidstr[VIR_UUID_STRING_BUFLEN + 2];
    PRL_HANDLE sdkdom = PRL_INVALID_HANDLE;

    prlsdkUUIDFormat(uuid, uuidstr);

    if (prlsdkSdkDomainLookup(privconn, uuidstr,
                              PGVC_SEARCH_BY_UUID, &sdkdom) < 0) {
        virUUIDFormat(uuid, uuidstr);
        virReportError(VIR_ERR_NO_DOMAIN,
                       _("no domain with matching uuid '%s'"), uuidstr);
        return PRL_INVALID_HANDLE;
    }

    return sdkdom;
}

static int
prlsdkUUIDParse(const char *uuidstr, unsigned char *uuid)
{
    char *tmp = NULL;
    int ret = -1;

    virCheckNonNullArgGoto(uuidstr, error);
    virCheckNonNullArgGoto(uuid, error);

    if (VIR_STRDUP(tmp, uuidstr) < 0)
        goto error;

    tmp[strlen(tmp) - 1] = '\0';

    /* trim curly braces */
    if (virUUIDParse(tmp + 1, uuid) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("UUID in config file malformed"));
        ret = -1;
        goto error;
    }

    ret = 0;
 error:
    VIR_FREE(tmp);
    return ret;
}

static int
prlsdkGetDomainIds(PRL_HANDLE sdkdom,
                   char **name,
                   unsigned char *uuid)
{
    char uuidstr[VIR_UUID_STRING_BUFLEN + 2];
    PRL_UINT32 len;
    PRL_RESULT pret;

    len = 0;
    /* get name length */
    pret = PrlVmCfg_GetName(sdkdom, NULL, &len);
    prlsdkCheckRetGoto(pret, error);

    if (VIR_ALLOC_N(*name, len) < 0)
        goto error;

    PrlVmCfg_GetName(sdkdom, *name, &len);
    prlsdkCheckRetGoto(pret, error);

    len = sizeof(uuidstr);
    PrlVmCfg_GetUuid(sdkdom, uuidstr, &len);
    prlsdkCheckRetGoto(pret, error);

    if (prlsdkUUIDParse(uuidstr, uuid) < 0)
        goto error;

    return 0;

 error:
    VIR_FREE(*name);
    return -1;
}

static int
379
prlsdkGetDomainState(PRL_HANDLE sdkdom, VIRTUAL_MACHINE_STATE_PTR vmState)
380 381 382 383 384 385 386 387 388
{
    PRL_HANDLE job = PRL_INVALID_HANDLE;
    PRL_HANDLE result = PRL_INVALID_HANDLE;
    PRL_HANDLE vmInfo = PRL_INVALID_HANDLE;
    PRL_RESULT pret;
    int ret = -1;

    job = PrlVm_GetState(sdkdom);

389
    if (PRL_FAILED(getJobResult(job, &result)))
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
        goto cleanup;

    pret = PrlResult_GetParamByIndex(result, 0, &vmInfo);
    prlsdkCheckRetGoto(pret, cleanup);

    pret = PrlVmInfo_GetState(vmInfo, vmState);
    prlsdkCheckRetGoto(pret, cleanup);

    ret = 0;

 cleanup:
    PrlHandle_Free(vmInfo);
    PrlHandle_Free(result);
    return ret;
}

static void
prlsdkDomObjFreePrivate(void *p)
{
409
    vzDomObjPtr pdom = p;
410 411 412 413 414

    if (!pdom)
        return;

    PrlHandle_Free(pdom->sdkdom);
415 416
    PrlHandle_Free(pdom->cache.stats);
    virCondDestroy(&pdom->cache.cond);
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
    VIR_FREE(pdom->uuid);
    VIR_FREE(pdom->home);
    VIR_FREE(p);
};

static int
prlsdkAddDomainVideoInfo(PRL_HANDLE sdkdom, virDomainDefPtr def)
{
    virDomainVideoDefPtr video = NULL;
    virDomainVideoAccelDefPtr accel = NULL;
    PRL_RESULT ret;
    PRL_UINT32 videoRam;

    /* video info */
    ret = PrlVmCfg_GetVideoRamSize(sdkdom, &videoRam);
    prlsdkCheckRetGoto(ret, error);

    if (VIR_ALLOC(video) < 0)
        goto error;

    if (VIR_ALLOC(accel) < 0)
        goto error;

    if (VIR_APPEND_ELEMENT_COPY(def->videos, def->nvideos, video) < 0)
        goto error;

    video->type = VIR_DOMAIN_VIDEO_TYPE_VGA;
    video->vram = videoRam << 10; /* from mbibytes to kbibytes */
    video->heads = 1;
    video->accel = accel;

    return 0;

 error:
    VIR_FREE(accel);
    virDomainVideoDefFree(video);
    return -1;
}

static int
prlsdkGetDiskInfo(PRL_HANDLE prldisk,
D
Dmitry Guryanov 已提交
458
                  virDomainDiskDefPtr disk,
459 460
                  bool isCdrom,
                  bool isCt)
461 462 463 464 465 466 467
{
    char *buf = NULL;
    PRL_UINT32 buflen = 0;
    PRL_RESULT pret;
    PRL_UINT32 emulatedType;
    PRL_UINT32 ifType;
    PRL_UINT32 pos;
468
    virDomainDeviceDriveAddressPtr address;
469 470 471 472 473 474
    int ret = -1;

    pret = PrlVmDev_GetEmulatedType(prldisk, &emulatedType);
    prlsdkCheckRetGoto(pret, cleanup);
    if (emulatedType == PDT_USE_IMAGE_FILE) {
        virDomainDiskSetType(disk, VIR_STORAGE_TYPE_FILE);
D
Dmitry Guryanov 已提交
475
        if (isCdrom)
476
            virDomainDiskSetFormat(disk, VIR_STORAGE_FILE_RAW);
D
Dmitry Guryanov 已提交
477 478
        else
            virDomainDiskSetFormat(disk, VIR_STORAGE_FILE_PLOOP);
479 480
    } else {
        virDomainDiskSetType(disk, VIR_STORAGE_TYPE_BLOCK);
481
        virDomainDiskSetFormat(disk, VIR_STORAGE_FILE_RAW);
482 483
    }

484
    if (isCdrom) {
D
Dmitry Guryanov 已提交
485
        disk->device = VIR_DOMAIN_DISK_DEVICE_CDROM;
486 487
        disk->src->readonly = true;
    } else {
D
Dmitry Guryanov 已提交
488
        disk->device = VIR_DOMAIN_DISK_DEVICE_DISK;
489
    }
D
Dmitry Guryanov 已提交
490

491 492 493 494 495 496 497 498 499 500 501 502
    pret = PrlVmDev_GetFriendlyName(prldisk, NULL, &buflen);
    prlsdkCheckRetGoto(pret, cleanup);

    if (VIR_ALLOC_N(buf, buflen) < 0)
        goto cleanup;

    pret = PrlVmDev_GetFriendlyName(prldisk, buf, &buflen);
    prlsdkCheckRetGoto(pret, cleanup);

    if (virDomainDiskSetSource(disk, buf) < 0)
        goto cleanup;

503
    /* Let physical devices added to CT look like SATA disks */
504
    if (isCt) {
505
        ifType = PMS_SATA_DEVICE;
506
    } else {
507 508 509
        pret = PrlVmDev_GetIfaceType(prldisk, &ifType);
        prlsdkCheckRetGoto(pret, cleanup);
    }
510 511 512 513 514

    pret = PrlVmDev_GetStackIndex(prldisk, &pos);
    prlsdkCheckRetGoto(pret, cleanup);

    address = &disk->info.addr.drive;
515 516 517
    switch (ifType) {
    case PMS_IDE_DEVICE:
        disk->bus = VIR_DOMAIN_DISK_BUS_IDE;
518 519 520 521
        disk->dst = virIndexToDiskName(pos, "hd");
        address->bus = pos / 2;
        address->target = 0;
        address->unit = pos % 2;
522 523 524
        break;
    case PMS_SCSI_DEVICE:
        disk->bus = VIR_DOMAIN_DISK_BUS_SCSI;
525 526 527 528
        disk->dst = virIndexToDiskName(pos, "sd");
        address->bus = 0;
        address->target = 0;
        address->unit = pos;
529 530 531
        break;
    case PMS_SATA_DEVICE:
        disk->bus = VIR_DOMAIN_DISK_BUS_SATA;
532 533 534 535
        disk->dst = virIndexToDiskName(pos, "sd");
        address->bus = 0;
        address->target = 0;
        address->unit = pos;
536 537 538 539 540 541 542 543
        break;
    default:
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unknown disk bus: %X"), ifType);
        goto cleanup;
        break;
    }

544 545
    if (!disk->dst)
        goto cleanup;
546 547 548 549 550 551 552 553 554 555

    disk->info.type = VIR_DOMAIN_DEVICE_ADDRESS_TYPE_DRIVE;

    ret = 0;

 cleanup:
    VIR_FREE(buf);
    return ret;
}

556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
static int
prlsdkGetFSInfo(PRL_HANDLE prldisk,
                virDomainFSDefPtr fs)
{
    char *buf = NULL;
    PRL_UINT32 buflen = 0;
    PRL_RESULT pret;
    int ret = -1;

    fs->type = VIR_DOMAIN_FS_TYPE_FILE;
    fs->fsdriver = VIR_DOMAIN_FS_DRIVER_TYPE_PLOOP;
    fs->accessmode = VIR_DOMAIN_FS_ACCESSMODE_PASSTHROUGH;
    fs->wrpolicy = VIR_DOMAIN_FS_WRPOLICY_DEFAULT;
    fs->format = VIR_STORAGE_FILE_PLOOP;

    fs->readonly = false;
    fs->symlinksResolved = false;

    pret = PrlVmDev_GetImagePath(prldisk, NULL, &buflen);
    prlsdkCheckRetGoto(pret, cleanup);

    if (VIR_ALLOC_N(buf, buflen) < 0)
        goto cleanup;

    pret = PrlVmDev_GetImagePath(prldisk, buf, &buflen);
    prlsdkCheckRetGoto(pret, cleanup);

    fs->src = buf;
    buf = NULL;

    pret = PrlVmDevHd_GetMountPoint(prldisk, NULL, &buflen);
    prlsdkCheckRetGoto(pret, cleanup);

    if (VIR_ALLOC_N(buf, buflen) < 0)
        goto cleanup;

    pret = PrlVmDevHd_GetMountPoint(prldisk, buf, &buflen);
    prlsdkCheckRetGoto(pret, cleanup);

    fs->dst = buf;
    buf = NULL;

    ret = 0;

 cleanup:
    VIR_FREE(buf);
    return ret;
}

605 606 607 608 609 610 611 612
static int
prlsdkAddDomainHardDisksInfo(PRL_HANDLE sdkdom, virDomainDefPtr def)
{
    PRL_RESULT pret;
    PRL_UINT32 hddCount;
    PRL_UINT32 i;
    PRL_HANDLE hdd = PRL_INVALID_HANDLE;
    virDomainDiskDefPtr disk = NULL;
613
    virDomainFSDefPtr fs = NULL;
614 615 616 617 618

    pret = PrlVmCfg_GetHardDisksCount(sdkdom, &hddCount);
    prlsdkCheckRetGoto(pret, error);

    for (i = 0; i < hddCount; ++i) {
619 620 621

        PRL_UINT32 emulatedType;

622 623 624
        pret = PrlVmCfg_GetHardDisk(sdkdom, i, &hdd);
        prlsdkCheckRetGoto(pret, error);

625 626 627 628
        pret = PrlVmDev_GetEmulatedType(hdd, &emulatedType);
        prlsdkCheckRetGoto(pret, error);

        if (PDT_USE_REAL_DEVICE != emulatedType && IS_CT(def)) {
629

630 631 632 633 634 635 636 637 638 639
            if (VIR_ALLOC(fs) < 0)
                goto error;

            if (prlsdkGetFSInfo(hdd, fs) < 0)
                goto error;

            if (virDomainFSInsert(def, fs) < 0)
                goto error;

            fs = NULL;
640 641 642
            PrlHandle_Free(hdd);
            hdd = PRL_INVALID_HANDLE;
        } else {
643
            if (!(disk = virDomainDiskDefNew(NULL)))
644 645
                goto error;

646
            if (prlsdkGetDiskInfo(hdd, disk, false, IS_CT(def)) < 0)
647 648 649 650
                goto error;

            if (VIR_APPEND_ELEMENT(def->disks, def->ndisks, disk) < 0)
                goto error;
651 652 653 654

            disk = NULL;
            PrlHandle_Free(hdd);
            hdd = PRL_INVALID_HANDLE;
655 656 657 658 659 660 661 662
        }
    }

    return 0;

 error:
    PrlHandle_Free(hdd);
    virDomainDiskDefFree(disk);
663
    virDomainFSDefFree(fs);
664 665 666
    return -1;
}

D
Dmitry Guryanov 已提交
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
static int
prlsdkAddDomainOpticalDisksInfo(PRL_HANDLE sdkdom, virDomainDefPtr def)
{
    PRL_RESULT pret;
    PRL_UINT32 cdromsCount;
    PRL_UINT32 i;
    PRL_HANDLE cdrom = PRL_INVALID_HANDLE;
    virDomainDiskDefPtr disk = NULL;

    pret = PrlVmCfg_GetOpticalDisksCount(sdkdom, &cdromsCount);
    prlsdkCheckRetGoto(pret, error);

    for (i = 0; i < cdromsCount; ++i) {
        pret = PrlVmCfg_GetOpticalDisk(sdkdom, i, &cdrom);
        prlsdkCheckRetGoto(pret, error);

683
        if (!(disk = virDomainDiskDefNew(NULL)))
D
Dmitry Guryanov 已提交
684 685
            goto error;

686
        if (prlsdkGetDiskInfo(cdrom, disk, true, IS_CT(def)) < 0)
D
Dmitry Guryanov 已提交
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
            goto error;

        PrlHandle_Free(cdrom);
        cdrom = PRL_INVALID_HANDLE;

        if (VIR_APPEND_ELEMENT(def->disks, def->ndisks, disk) < 0)
            goto error;
    }

    return 0;

 error:
    PrlHandle_Free(cdrom);
    virDomainDiskDefFree(disk);
    return -1;
}

704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
static int
prlsdkGetNetInfo(PRL_HANDLE netAdapter, virDomainNetDefPtr net, bool isCt)
{
    char macstr[VIR_MAC_STRING_BUFLEN];
    PRL_UINT32 buflen;
    PRL_UINT32 netAdapterIndex;
    PRL_UINT32 emulatedType;
    PRL_RESULT pret;
    PRL_BOOL isConnected;
    int ret = -1;

    net->type = VIR_DOMAIN_NET_TYPE_NETWORK;


    /* use device name, shown by prlctl as target device
     * for identifying network adapter in virDomainDefineXML */
    pret = PrlVmDevNet_GetHostInterfaceName(netAdapter, NULL, &buflen);
    prlsdkCheckRetGoto(pret, cleanup);

    if (VIR_ALLOC_N(net->ifname, buflen) < 0)
        goto cleanup;

    pret = PrlVmDevNet_GetHostInterfaceName(netAdapter, net->ifname, &buflen);
M
Maxim Nestratov 已提交
727
    prlsdkCheckRetGoto(pret, cleanup);
728

729 730 731
    pret = PrlVmDev_GetIndex(netAdapter, &netAdapterIndex);
    prlsdkCheckRetGoto(pret, cleanup);

732 733 734 735 736
    if (isCt && netAdapterIndex == (PRL_UINT32) -1) {
        /* venet devices don't have mac address and
         * always up */
        net->linkstate = VIR_DOMAIN_NET_INTERFACE_LINK_STATE_UP;
        if (VIR_STRDUP(net->data.network.name,
737
                       PARALLELS_DOMAIN_ROUTED_NETWORK_NAME) < 0)
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
            goto cleanup;
        return 0;
    }

    buflen = ARRAY_CARDINALITY(macstr);
    if (VIR_ALLOC_N(macstr, buflen))
        goto cleanup;
    pret = PrlVmDevNet_GetMacAddressCanonical(netAdapter, macstr, &buflen);
    prlsdkCheckRetGoto(pret, cleanup);

    if (virMacAddrParse(macstr, &net->mac) < 0)
        goto cleanup;

    pret = PrlVmDev_GetEmulatedType(netAdapter, &emulatedType);
    prlsdkCheckRetGoto(pret, cleanup);

    if (emulatedType == PNA_ROUTED) {
        if (VIR_STRDUP(net->data.network.name,
756
                       PARALLELS_DOMAIN_ROUTED_NETWORK_NAME) < 0)
757 758 759 760 761 762 763 764 765 766 767 768
            goto cleanup;
    } else {
        pret = PrlVmDevNet_GetVirtualNetworkId(netAdapter, NULL, &buflen);
        prlsdkCheckRetGoto(pret, cleanup);

        if (VIR_ALLOC_N(net->data.network.name, buflen) < 0)
            goto cleanup;

        pret = PrlVmDevNet_GetVirtualNetworkId(netAdapter,
                                               net->data.network.name,
                                               &buflen);
        prlsdkCheckRetGoto(pret, cleanup);
769 770 771 772 773 774 775 776 777 778

        /*
         * We use VIR_DOMAIN_NET_TYPE_NETWORK for all network adapters
         * except those whose Virtual Network Id differ from Parallels
         * predefined ones such as PARALLELS_DOMAIN_BRIDGED_NETWORK_NAME
         * and PARALLELS_DONAIN_ROUTED_NETWORK_NAME
         */
        if (STRNEQ(net->data.network.name, PARALLELS_DOMAIN_BRIDGED_NETWORK_NAME))
            net->type = VIR_DOMAIN_NET_TYPE_BRIDGE;

779 780
    }

781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
    if (!isCt) {
        PRL_VM_NET_ADAPTER_TYPE type;
        pret = PrlVmDevNet_GetAdapterType(netAdapter, &type);
        prlsdkCheckRetGoto(pret, cleanup);

        switch (type) {
        case PNT_RTL:
            if (VIR_STRDUP(net->model, "rtl8139") < 0)
                goto cleanup;
            break;
        case PNT_E1000:
            if (VIR_STRDUP(net->model, "e1000") < 0)
                goto cleanup;
            break;
        case PNT_VIRTIO:
            if (VIR_STRDUP(net->model, "virtio") < 0)
                goto cleanup;
            break;
        default:
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Unknown adapter type: %X"), type);
            goto cleanup;
        }
    }

806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
    pret = PrlVmDev_IsConnected(netAdapter, &isConnected);
    prlsdkCheckRetGoto(pret, cleanup);

    if (isConnected)
        net->linkstate = VIR_DOMAIN_NET_INTERFACE_LINK_STATE_UP;
    else
        net->linkstate = VIR_DOMAIN_NET_INTERFACE_LINK_STATE_DOWN;

    ret = 0;
 cleanup:
    return ret;
}

static int
prlsdkAddDomainNetInfo(PRL_HANDLE sdkdom, virDomainDefPtr def)
{
    virDomainNetDefPtr net = NULL;
    PRL_RESULT ret;
    PRL_HANDLE netAdapter;
    PRL_UINT32 netAdaptersCount;
    PRL_UINT32 i;

    ret = PrlVmCfg_GetNetAdaptersCount(sdkdom, &netAdaptersCount);
    prlsdkCheckRetGoto(ret, error);
    for (i = 0; i < netAdaptersCount; ++i) {
        ret = PrlVmCfg_GetNetAdapter(sdkdom, i, &netAdapter);
        prlsdkCheckRetGoto(ret, error);

        if (VIR_ALLOC(net) < 0)
            goto error;

        if (prlsdkGetNetInfo(netAdapter, net, IS_CT(def)) < 0)
            goto error;

        PrlHandle_Free(netAdapter);
        netAdapter = PRL_INVALID_HANDLE;

        if (VIR_APPEND_ELEMENT(def->nets, def->nnets, net) < 0)
            goto error;
    }

    return 0;

 error:
    PrlHandle_Free(netAdapter);
    virDomainNetDefFree(net);
    return -1;
}

static int
prlsdkGetSerialInfo(PRL_HANDLE serialPort, virDomainChrDefPtr chr)
{
    PRL_RESULT pret;
    PRL_UINT32 serialPortIndex;
    PRL_UINT32 emulatedType;
    char *friendlyName = NULL;
    PRL_UINT32 buflen;

    chr->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL;
    chr->targetTypeAttr = false;
    pret = PrlVmDev_GetIndex(serialPort, &serialPortIndex);
    prlsdkCheckRetGoto(pret, error);
    chr->target.port = serialPortIndex;

    pret = PrlVmDev_GetEmulatedType(serialPort, &emulatedType);
    prlsdkCheckRetGoto(pret, error);

    pret = PrlVmDev_GetFriendlyName(serialPort, NULL, &buflen);
    prlsdkCheckRetGoto(pret, error);

    if (VIR_ALLOC_N(friendlyName, buflen) < 0)
        goto error;

    pret = PrlVmDev_GetFriendlyName(serialPort, friendlyName, &buflen);
    prlsdkCheckRetGoto(pret, error);

    switch (emulatedType) {
    case PDT_USE_OUTPUT_FILE:
        chr->source.type = VIR_DOMAIN_CHR_TYPE_FILE;
        chr->source.data.file.path = friendlyName;
        break;
    case PDT_USE_SERIAL_PORT_SOCKET_MODE:
        chr->source.type = VIR_DOMAIN_CHR_TYPE_UNIX;
        chr->source.data.nix.path = friendlyName;
        break;
    case PDT_USE_REAL_DEVICE:
        chr->source.type = VIR_DOMAIN_CHR_TYPE_DEV;
        chr->source.data.file.path = friendlyName;
        break;
    default:
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unknown serial type: %X"), emulatedType);
        goto error;
        break;
    }

    return 0;
 error:
    VIR_FREE(friendlyName);
    return -1;
}


static int
prlsdkAddSerialInfo(PRL_HANDLE sdkdom,
                    virDomainChrDefPtr **serials,
                    size_t *nserials)
{
    PRL_RESULT ret;
    PRL_HANDLE serialPort;
    PRL_UINT32 serialPortsCount;
    PRL_UINT32 i;
    virDomainChrDefPtr chr = NULL;

    ret = PrlVmCfg_GetSerialPortsCount(sdkdom, &serialPortsCount);
    prlsdkCheckRetGoto(ret, cleanup);
    for (i = 0; i < serialPortsCount; ++i) {
        ret = PrlVmCfg_GetSerialPort(sdkdom, i, &serialPort);
        prlsdkCheckRetGoto(ret, cleanup);

        if (!(chr = virDomainChrDefNew()))
            goto cleanup;

        if (prlsdkGetSerialInfo(serialPort, chr))
            goto cleanup;

        PrlHandle_Free(serialPort);
        serialPort = PRL_INVALID_HANDLE;

        if (VIR_APPEND_ELEMENT(*serials, *nserials, chr) < 0)
            goto cleanup;
    }

    return 0;

 cleanup:
    PrlHandle_Free(serialPort);
    virDomainChrDefFree(chr);
    return -1;
}


static int
prlsdkAddDomainHardware(PRL_HANDLE sdkdom, virDomainDefPtr def)
{
    if (!IS_CT(def))
        if (prlsdkAddDomainVideoInfo(sdkdom, def) < 0)
            goto error;

    if (prlsdkAddDomainHardDisksInfo(sdkdom, def) < 0)
        goto error;

D
Dmitry Guryanov 已提交
958 959 960
    if (prlsdkAddDomainOpticalDisksInfo(sdkdom, def) < 0)
        goto error;

961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
    if (prlsdkAddDomainNetInfo(sdkdom, def) < 0)
        goto error;

    if (prlsdkAddSerialInfo(sdkdom,
                            &def->serials,
                            &def->nserials) < 0)
        goto error;

    return 0;
 error:
    return -1;
}


static int
prlsdkAddVNCInfo(PRL_HANDLE sdkdom, virDomainDefPtr def)
{
    virDomainGraphicsDefPtr gr = NULL;
    PRL_VM_REMOTE_DISPLAY_MODE vncMode;
    PRL_UINT32 port;
    PRL_UINT32 buflen = 0;
    PRL_RESULT pret;

    pret = PrlVmCfg_GetVNCMode(sdkdom, &vncMode);
    prlsdkCheckRetGoto(pret, error);

    if (vncMode == PRD_DISABLED)
        return 0;

    if (VIR_ALLOC(gr) < 0)
        goto error;

    pret = PrlVmCfg_GetVNCPort(sdkdom, &port);
    prlsdkCheckRetGoto(pret, error);

    gr->data.vnc.autoport = (vncMode == PRD_AUTO);
    gr->type = VIR_DOMAIN_GRAPHICS_TYPE_VNC;
    gr->data.vnc.port = port;
    gr->data.vnc.keymap = NULL;
    gr->data.vnc.socket = NULL;
    gr->data.vnc.auth.passwd = NULL;
    gr->data.vnc.auth.expires = false;
    gr->data.vnc.auth.connected = 0;

    if (VIR_ALLOC(gr->listens) < 0)
        goto error;

    gr->nListens = 1;

    pret = PrlVmCfg_GetVNCHostName(sdkdom, NULL, &buflen);
    prlsdkCheckRetGoto(pret, error);

    if (VIR_ALLOC_N(gr->listens[0].address, buflen) < 0)
        goto error;

    pret = PrlVmCfg_GetVNCHostName(sdkdom, gr->listens[0].address, &buflen);
    prlsdkCheckRetGoto(pret, error);

    gr->listens[0].type = VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_ADDRESS;

    if (VIR_APPEND_ELEMENT(def->graphics, def->ngraphics, gr) < 0)
        goto error;

1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
    if (IS_CT(def)) {
        virDomainVideoDefPtr video;
        if (VIR_ALLOC(video) < 0)
            goto error;
        video->type = virDomainVideoDefaultType(def);
        if (video->type < 0) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("cannot determine default video type"));
            VIR_FREE(video);
            goto error;
        }
        video->vram = virDomainVideoDefaultRAM(def, video->type);
        video->heads = 1;
        if (VIR_ALLOC_N(def->videos, 1) < 0) {
            virDomainVideoDefFree(video);
            goto error;
        }
        def->videos[def->nvideos++] = video;
    }
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 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 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
    return 0;

 error:
    virDomainGraphicsDefFree(gr);
    return -1;
}

static int
prlsdkConvertDomainState(VIRTUAL_MACHINE_STATE domainState,
                         PRL_UINT32 envId,
                         virDomainObjPtr dom)
{
    switch (domainState) {
    case VMS_STOPPED:
    case VMS_MOUNTED:
        virDomainObjSetState(dom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
        dom->def->id = -1;
        break;
    case VMS_STARTING:
    case VMS_COMPACTING:
    case VMS_RESETTING:
    case VMS_PAUSING:
    case VMS_RECONNECTING:
    case VMS_RUNNING:
        virDomainObjSetState(dom, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_BOOTED);
        dom->def->id = envId;
        break;
    case VMS_PAUSED:
        virDomainObjSetState(dom, VIR_DOMAIN_PAUSED,
                             VIR_DOMAIN_PAUSED_USER);
        dom->def->id = envId;
        break;
    case VMS_SUSPENDED:
    case VMS_DELETING_STATE:
    case VMS_SUSPENDING_SYNC:
        virDomainObjSetState(dom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SAVED);
        dom->def->id = -1;
        break;
    case VMS_STOPPING:
        virDomainObjSetState(dom, VIR_DOMAIN_SHUTDOWN,
                             VIR_DOMAIN_SHUTDOWN_USER);
        dom->def->id = envId;
        break;
    case VMS_SNAPSHOTING:
        virDomainObjSetState(dom, VIR_DOMAIN_PAUSED,
                             VIR_DOMAIN_PAUSED_SNAPSHOT);
        dom->def->id = envId;
        break;
    case VMS_MIGRATING:
        virDomainObjSetState(dom, VIR_DOMAIN_PAUSED,
                             VIR_DOMAIN_PAUSED_MIGRATION);
        dom->def->id = envId;
        break;
    case VMS_SUSPENDING:
        virDomainObjSetState(dom, VIR_DOMAIN_PAUSED,
                             VIR_DOMAIN_PAUSED_SAVE);
        dom->def->id = envId;
        break;
    case VMS_RESTORING:
        virDomainObjSetState(dom, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_RESTORED);
        dom->def->id = envId;
        break;
    case VMS_CONTINUING:
        virDomainObjSetState(dom, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_UNPAUSED);
        dom->def->id = envId;
        break;
    case VMS_RESUMING:
        virDomainObjSetState(dom, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_RESTORED);
        dom->def->id = envId;
        break;
    case VMS_UNKNOWN:
        virDomainObjSetState(dom, VIR_DOMAIN_NOSTATE,
                             VIR_DOMAIN_NOSTATE_UNKNOWN);
        break;
    default:
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unknown domain state: %X"), domainState);
        return -1;
        break;
    }

    return 0;
}

static int
prlsdkConvertCpuInfo(PRL_HANDLE sdkdom,
M
Maxim Nestratov 已提交
1135
                     virDomainDefPtr def)
1136 1137 1138 1139 1140 1141 1142 1143
{
    char *buf;
    PRL_UINT32 buflen = 0;
    int hostcpus;
    PRL_UINT32 cpuCount;
    PRL_RESULT pret;
    int ret = -1;

1144
    if ((hostcpus = nodeGetCPUCount(NULL)) < 0)
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
        goto cleanup;

    /* get number of CPUs */
    pret = PrlVmCfg_GetCpuCount(sdkdom, &cpuCount);
    prlsdkCheckRetGoto(pret, cleanup);

    if (cpuCount > hostcpus)
        cpuCount = hostcpus;

    def->vcpus = cpuCount;
    def->maxvcpus = cpuCount;

    pret = PrlVmCfg_GetCpuMask(sdkdom, NULL, &buflen);
    prlsdkCheckRetGoto(pret, cleanup);

    if (VIR_ALLOC_N(buf, buflen) < 0)
        goto cleanup;

    pret = PrlVmCfg_GetCpuMask(sdkdom, buf, &buflen);

    if (strlen(buf) == 0) {
M
Maxim Nestratov 已提交
1166
        if (!(def->cpumask = virBitmapNew(hostcpus)))
1167
            goto cleanup;
M
Maxim Nestratov 已提交
1168
        virBitmapSetAll(def->cpumask);
1169
    } else {
M
Maxim Nestratov 已提交
1170
        if (virBitmapParse(buf, 0, &def->cpumask, hostcpus) < 0)
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
            goto cleanup;
    }

    ret = 0;
 cleanup:
    VIR_FREE(buf);
    return ret;
}

static int
prlsdkConvertDomainType(PRL_HANDLE sdkdom, virDomainDefPtr def)
{
    PRL_VM_TYPE domainType;
    PRL_RESULT pret;

    pret = PrlVmCfg_GetVmType(sdkdom, &domainType);
    prlsdkCheckRetGoto(pret, error);

    switch (domainType) {
    case PVT_VM:
1191
        def->os.type = VIR_DOMAIN_OSTYPE_HVM;
1192 1193
        break;
    case PVT_CT:
1194
        def->os.type = VIR_DOMAIN_OSTYPE_EXE;
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
        if (VIR_STRDUP(def->os.init, "/sbin/init") < 0)
            return -1;
        break;
    default:
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unknown domain type: %X"), domainType);
        return -1;
    }

    return 0;

 error:
    return -1;
}

static int
prlsdkConvertCpuMode(PRL_HANDLE sdkdom, virDomainDefPtr def)
{
    PRL_RESULT pret;
    PRL_CPU_MODE cpuMode;

    pret = PrlVmCfg_GetCpuMode(sdkdom, &cpuMode);
    prlsdkCheckRetGoto(pret, error);

    switch (cpuMode) {
    case PCM_CPU_MODE_32:
        def->os.arch = VIR_ARCH_I686;
        break;
    case PCM_CPU_MODE_64:
        def->os.arch = VIR_ARCH_X86_64;
        break;
    default:
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unknown CPU mode: %X"), cpuMode);
        return -1;
    }

    return 0;
 error:
    return -1;
}

/*
 * This function retrieves information about domain.
 * If the domains is already in the domains list
 * privconn->domains, then locked 'olddom' must be
 * provided. If the domains must be added to the list,
 * olddom must be NULL.
 *
 * The function return a pointer to a locked virDomainObj.
 */
static virDomainObjPtr
1247
prlsdkLoadDomain(vzConnPtr privconn,
1248 1249 1250 1251 1252
                 PRL_HANDLE sdkdom,
                 virDomainObjPtr olddom)
{
    virDomainObjPtr dom = NULL;
    virDomainDefPtr def = NULL;
1253
    vzDomObjPtr pdom = NULL;
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
    VIRTUAL_MACHINE_STATE domainState;

    PRL_UINT32 buflen = 0;
    PRL_RESULT pret;
    PRL_UINT32 ram;
    PRL_UINT32 envId;
    PRL_VM_AUTOSTART_OPTION autostart;

    virCheckNonNullArgGoto(privconn, error);
    virCheckNonNullArgGoto(sdkdom, error);

1265
    if (!(def = virDomainDefNew()))
1266 1267 1268 1269 1270 1271 1272 1273 1274
        goto error;

    if (!olddom) {
        if (VIR_ALLOC(pdom) < 0)
            goto error;
    } else {
        pdom = olddom->privateData;
    }

1275 1276 1277 1278 1279
    if (STREQ(privconn->drivername, "vz"))
        def->virtType = VIR_DOMAIN_VIRT_VZ;
    else
        def->virtType = VIR_DOMAIN_VIRT_PARALLELS;

1280 1281 1282 1283 1284 1285
    def->id = -1;

    /* we will remove this field in the near future, so let's set it
     * to NULL temporarily */
    pdom->uuid = NULL;

1286 1287 1288 1289 1290 1291 1292
    pdom->cache.stats = PRL_INVALID_HANDLE;
    pdom->cache.count = -1;
    if (virCondInit(&pdom->cache.cond) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("cannot initialize condition"));
        goto error;
    }

1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
    if (prlsdkGetDomainIds(sdkdom, &def->name, def->uuid) < 0)
        goto error;

    def->onReboot = VIR_DOMAIN_LIFECYCLE_RESTART;
    def->onPoweroff = VIR_DOMAIN_LIFECYCLE_DESTROY;
    def->onCrash = VIR_DOMAIN_LIFECYCLE_CRASH_DESTROY;

    /* get RAM parameters */
    pret = PrlVmCfg_GetRamSize(sdkdom, &ram);
    prlsdkCheckRetGoto(pret, error);
1303 1304 1305
    virDomainDefSetMemoryInitial(def, ram << 10); /* RAM size obtained in Mbytes,
                                                     convert to Kbytes */
    def->mem.cur_balloon = ram << 10;
1306

M
Maxim Nestratov 已提交
1307
    if (prlsdkConvertCpuInfo(sdkdom, def) < 0)
1308 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
        goto error;

    if (prlsdkConvertCpuMode(sdkdom, def) < 0)
        goto error;

    if (prlsdkConvertDomainType(sdkdom, def) < 0)
        goto error;

    if (prlsdkAddDomainHardware(sdkdom, def) < 0)
        goto error;

    if (prlsdkAddVNCInfo(sdkdom, def) < 0)
        goto error;

    pret = PrlVmCfg_GetEnvId(sdkdom, &envId);
    prlsdkCheckRetGoto(pret, error);
    pdom->id = envId;

    buflen = 0;
    pret = PrlVmCfg_GetHomePath(sdkdom, NULL, &buflen);
    prlsdkCheckRetGoto(pret, error);

    VIR_FREE(pdom->home);
    if (VIR_ALLOC_N(pdom->home, buflen) < 0)
        goto error;

    pret = PrlVmCfg_GetHomePath(sdkdom, pdom->home, &buflen);
    prlsdkCheckRetGoto(pret, error);

1337 1338 1339 1340 1341 1342 1343 1344
    /* For VMs pdom->home is actually /directory/config.pvs */
    if (!IS_CT(def)) {
        /* Get rid of /config.pvs in path string */
        char *s = strrchr(pdom->home, '/');
        if (s)
            *s = '\0';
    }

1345 1346 1347 1348 1349 1350
    pret = PrlVmCfg_GetAutoStart(sdkdom, &autostart);
    prlsdkCheckRetGoto(pret, error);

    if (prlsdkGetDomainState(sdkdom, &domainState) < 0)
        goto error;

1351 1352 1353
    if (virDomainDefAddImplicitControllers(def) < 0)
        goto error;

1354
    if (def->ngraphics > 0) {
M
Michal Privoznik 已提交
1355 1356
        int bus = IS_CT(def) ? VIR_DOMAIN_INPUT_BUS_PARALLELS :
                               VIR_DOMAIN_INPUT_BUS_PS2;
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368

        if (virDomainDefMaybeAddInput(def,
                                      VIR_DOMAIN_INPUT_TYPE_MOUSE,
                                      bus) < 0)
            goto error;

        if (virDomainDefMaybeAddInput(def,
                                      VIR_DOMAIN_INPUT_TYPE_KBD,
                                      bus) < 0)
            goto error;
    }

1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
    if (olddom) {
        /* assign new virDomainDef without any checks */
        /* we can't use virDomainObjAssignDef, because it checks
         * for state and domain name */
        dom = olddom;
        virDomainDefFree(dom->def);
        dom->def = def;
    } else {
        if (!(dom = virDomainObjListAdd(privconn->domains, def,
                                        privconn->xmlopt,
                                        0, NULL)))
M
Michal Privoznik 已提交
1380
            goto error;
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
    }
    /* dom is locked here */

    dom->privateData = pdom;
    dom->privateDataFreeFunc = prlsdkDomObjFreePrivate;
    dom->persistent = 1;

    switch (autostart) {
    case PAO_VM_START_ON_LOAD:
        dom->autostart = 1;
        break;
    case PAO_VM_START_MANUAL:
        dom->autostart = 0;
        break;
    default:
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unknown autostart mode: %X"), autostart);
        goto error;
    }

1401 1402 1403
    if (prlsdkConvertDomainState(domainState, envId, dom) < 0)
        goto error;

1404 1405 1406 1407 1408 1409 1410 1411
    if (!pdom->sdkdom) {
        pret = PrlHandle_AddRef(sdkdom);
        prlsdkCheckRetGoto(pret, error);
        pdom->sdkdom = sdkdom;
    }

    return dom;
 error:
1412 1413 1414 1415 1416 1417 1418
    if (dom && !olddom) {
        /* Domain isn't persistent means that we haven't yet set
         * prlsdkDomObjFreePrivate and should call it manually
         */
        if (!dom->persistent)
            prlsdkDomObjFreePrivate(pdom);

1419
        virDomainObjListRemove(privconn->domains, dom);
1420 1421 1422 1423 1424 1425 1426
    }
    /* Delete newly allocated def only if we haven't assigned it to domain
     * Otherwise we will end up with domain having invalid def within it
     */
    if (!dom)
        virDomainDefFree(def);

1427 1428 1429 1430
    return NULL;
}

int
1431
prlsdkLoadDomains(vzConnPtr privconn)
1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442
{
    PRL_HANDLE job = PRL_INVALID_HANDLE;
    PRL_HANDLE result;
    PRL_HANDLE sdkdom;
    PRL_UINT32 paramsCount;
    PRL_RESULT pret;
    size_t i = 0;
    virDomainObjPtr dom;

    job = PrlSrv_GetVmListEx(privconn->server, PVTF_VM | PVTF_CT);

1443
    if (PRL_FAILED(getJobResult(job, &result)))
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
        return -1;

    pret = PrlResult_GetParamsCount(result, &paramsCount);
    prlsdkCheckRetGoto(pret, error);

    for (i = 0; i < paramsCount; i++) {
        pret = PrlResult_GetParamByIndex(result, i, &sdkdom);
        if (PRL_FAILED(pret)) {
            logPrlError(pret);
            PrlHandle_Free(sdkdom);
            goto error;
        }

        dom = prlsdkLoadDomain(privconn, sdkdom, NULL);
        PrlHandle_Free(sdkdom);

        if (!dom)
            goto error;
        else
            virObjectUnlock(dom);
    }

    PrlHandle_Free(result);
    return 0;

 error:
    PrlHandle_Free(result);
    return -1;
}

virDomainObjPtr
1475
prlsdkAddDomain(vzConnPtr privconn, const unsigned char *uuid)
1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
{
    PRL_HANDLE sdkdom = PRL_INVALID_HANDLE;
    virDomainObjPtr dom;

    dom = virDomainObjListFindByUUID(privconn->domains, uuid);
    if (dom) {
        /* domain is already in the list */
        return dom;
    }

    sdkdom = prlsdkSdkDomainLookupByUUID(privconn, uuid);
    if (sdkdom == PRL_INVALID_HANDLE)
        return NULL;

    dom = prlsdkLoadDomain(privconn, sdkdom, NULL);
    PrlHandle_Free(sdkdom);
    return dom;
}
1494

1495
int
1496
prlsdkUpdateDomain(vzConnPtr privconn, virDomainObjPtr dom)
1497 1498 1499
{
    PRL_HANDLE job;
    virDomainObjPtr retdom = NULL;
1500
    vzDomObjPtr pdom = dom->privateData;
1501 1502

    job = PrlVm_RefreshConfig(pdom->sdkdom);
1503
    if (waitJob(job))
1504 1505 1506 1507 1508 1509
        return -1;

    retdom = prlsdkLoadDomain(privconn, pdom->sdkdom, dom);
    return retdom ? 0 : -1;
}

1510
static int prlsdkSendEvent(vzConnPtr privconn,
1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
                           virDomainObjPtr dom,
                           virDomainEventType lvEventType,
                           int lvEventTypeDetails)
{
    virObjectEventPtr event = NULL;

    event = virDomainEventLifecycleNewFromObj(dom,
                                              lvEventType,
                                              lvEventTypeDetails);
    if (!event)
        return -1;

    virObjectEventStateQueue(privconn->domainEventState, event);
    return 0;
}

static void
prlsdkNewStateToEvent(VIRTUAL_MACHINE_STATE domainState,
                      virDomainEventType *lvEventType,
                      int *lvEventTypeDetails)
{
    /* We skip all intermediate states here, because
     * libvirt doesn't have correspoding event types for
     * them */
    switch (domainState) {
    case VMS_STOPPED:
    case VMS_MOUNTED:
        *lvEventType = VIR_DOMAIN_EVENT_STOPPED;
        *lvEventTypeDetails = VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN;
        break;
    case VMS_RUNNING:
        *lvEventType = VIR_DOMAIN_EVENT_STARTED;
        *lvEventTypeDetails = VIR_DOMAIN_EVENT_STARTED_BOOTED;
        break;
    case VMS_PAUSED:
        *lvEventType = VIR_DOMAIN_EVENT_SUSPENDED;
        *lvEventTypeDetails = VIR_DOMAIN_EVENT_SUSPENDED_PAUSED;
        break;
    case VMS_SUSPENDED:
        *lvEventType = VIR_DOMAIN_EVENT_STOPPED;
        *lvEventTypeDetails = VIR_DOMAIN_EVENT_STOPPED_SAVED;
        break;
    default:
        VIR_DEBUG("Skip sending event about changing state to %X",
                  domainState);
        break;
    }
}

1560
static void
1561
prlsdkHandleVmStateEvent(vzConnPtr privconn,
1562 1563 1564 1565 1566 1567 1568
                         PRL_HANDLE prlEvent,
                         unsigned char *uuid)
{
    PRL_RESULT pret = PRL_ERR_FAILURE;
    PRL_HANDLE eventParam = PRL_INVALID_HANDLE;
    PRL_INT32 domainState;
    virDomainObjPtr dom = NULL;
1569
    vzDomObjPtr pdom;
1570 1571 1572
    virDomainEventType lvEventType = 0;
    int lvEventTypeDetails = 0;

1573 1574 1575 1576
    dom = virDomainObjListFindByUUID(privconn->domains, uuid);
    if (dom == NULL)
        return;

1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590
    pret = PrlEvent_GetParamByName(prlEvent, "vminfo_vm_state", &eventParam);
    prlsdkCheckRetGoto(pret, cleanup);

    pret = PrlEvtPrm_ToInt32(eventParam, &domainState);
    prlsdkCheckRetGoto(pret, cleanup);

    pdom = dom->privateData;
    if (prlsdkConvertDomainState(domainState, pdom->id, dom) < 0)
        goto cleanup;

    prlsdkNewStateToEvent(domainState,
                          &lvEventType,
                          &lvEventTypeDetails);

1591
    prlsdkSendEvent(privconn, dom, lvEventType, lvEventTypeDetails);
1592 1593

 cleanup:
1594 1595
    virObjectUnlock(dom);
    return;
1596 1597
}

1598
static void
1599
prlsdkHandleVmConfigEvent(vzConnPtr privconn,
1600 1601 1602 1603 1604
                          unsigned char *uuid)
{
    virDomainObjPtr dom = NULL;

    dom = virDomainObjListFindByUUID(privconn->domains, uuid);
1605 1606
    if (dom == NULL)
        return;
1607 1608 1609 1610

    if (prlsdkUpdateDomain(privconn, dom) < 0)
        goto cleanup;

1611
    prlsdkSendEvent(privconn, dom, VIR_DOMAIN_EVENT_DEFINED,
M
Michal Privoznik 已提交
1612
                    VIR_DOMAIN_EVENT_DEFINED_UPDATED);
1613 1614

 cleanup:
1615 1616
    virObjectUnlock(dom);
    return;
1617 1618
}

1619
static void
1620
prlsdkHandleVmAddedEvent(vzConnPtr privconn,
M
Michal Privoznik 已提交
1621
                         unsigned char *uuid)
1622 1623 1624 1625
{
    virDomainObjPtr dom = NULL;

    dom = prlsdkAddDomain(privconn, uuid);
1626 1627
    if (dom == NULL)
        return;
1628

1629
    prlsdkSendEvent(privconn, dom, VIR_DOMAIN_EVENT_DEFINED,
M
Michal Privoznik 已提交
1630
                    VIR_DOMAIN_EVENT_DEFINED_ADDED);
1631

1632 1633
    virObjectUnlock(dom);
    return;
1634 1635
}

1636
static void
1637
prlsdkHandleVmRemovedEvent(vzConnPtr privconn,
1638 1639 1640 1641 1642
                           unsigned char *uuid)
{
    virDomainObjPtr dom = NULL;

    dom = virDomainObjListFindByUUID(privconn->domains, uuid);
1643 1644 1645 1646
    /* domain was removed from the list from the libvirt
     * API function in current connection */
    if (dom == NULL)
        return;
1647

1648
    prlsdkSendEvent(privconn, dom, VIR_DOMAIN_EVENT_UNDEFINED,
M
Michal Privoznik 已提交
1649
                    VIR_DOMAIN_EVENT_UNDEFINED_REMOVED);
1650 1651

    virDomainObjListRemove(privconn->domains, dom);
1652
    return;
1653 1654
}

1655 1656 1657
#define PARALLELS_STATISTICS_DROP_COUNT 3

static PRL_RESULT
1658
prlsdkHandlePerfEvent(vzConnPtr privconn,
M
Michal Privoznik 已提交
1659 1660
                      PRL_HANDLE event,
                      unsigned char *uuid)
1661 1662
{
    virDomainObjPtr dom = NULL;
1663
    vzDomObjPtr privdom = NULL;
1664 1665 1666 1667 1668 1669 1670
    PRL_HANDLE job = PRL_INVALID_HANDLE;

    dom = virDomainObjListFindByUUID(privconn->domains, uuid);
    if (dom == NULL)
        goto cleanup;
    privdom = dom->privateData;

M
Michal Privoznik 已提交
1671
    /* delayed event after unsubscribe */
1672 1673 1674 1675 1676 1677 1678 1679 1680 1681
    if (privdom->cache.count == -1)
        goto cleanup;

    PrlHandle_Free(privdom->cache.stats);
    privdom->cache.stats = PRL_INVALID_HANDLE;

    if (privdom->cache.count > PARALLELS_STATISTICS_DROP_COUNT) {
        job = PrlVm_UnsubscribeFromPerfStats(privdom->sdkdom);
        if (PRL_FAILED(waitJob(job)))
            goto cleanup;
M
Michal Privoznik 已提交
1682
        /* change state to unsubscribed */
1683 1684 1685 1686
        privdom->cache.count = -1;
    } else {
        ++privdom->cache.count;
        privdom->cache.stats = event;
M
Michal Privoznik 已提交
1687
        /* thus we get own of event handle */
1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699
        event = PRL_INVALID_HANDLE;
        virCondSignal(&privdom->cache.cond);
    }

 cleanup:
    PrlHandle_Free(event);
    if (dom)
        virObjectUnlock(dom);

    return PRL_ERR_SUCCESS;
}

1700 1701
static PRL_RESULT
prlsdkEventsHandler(PRL_HANDLE prlEvent, PRL_VOID_PTR opaque)
1702
{
1703
    vzConnPtr privconn = opaque;
1704
    PRL_RESULT pret = PRL_ERR_FAILURE;
1705
    PRL_HANDLE_TYPE handleType;
1706 1707 1708 1709 1710
    char uuidstr[VIR_UUID_STRING_BUFLEN + 2];
    unsigned char uuid[VIR_UUID_BUFLEN];
    PRL_UINT32 bufsize = ARRAY_CARDINALITY(uuidstr);
    PRL_EVENT_TYPE prlEventType;

1711
    pret = PrlHandle_GetType(prlEvent, &handleType);
1712
    prlsdkCheckRetGoto(pret, cleanup);
1713

1714 1715 1716 1717 1718 1719 1720
    /* Currently, there is no need to handle anything but events */
    if (handleType != PHT_EVENT)
        goto cleanup;

    if (privconn == NULL)
        goto cleanup;

1721
    pret = PrlEvent_GetIssuerId(prlEvent, uuidstr, &bufsize);
1722
    prlsdkCheckRetGoto(pret, cleanup);
1723

1724 1725 1726
    pret = PrlEvent_GetType(prlEvent, &prlEventType);
    prlsdkCheckRetGoto(pret, cleanup);

1727
    if (prlsdkUUIDParse(uuidstr, uuid) < 0)
1728
        goto cleanup;
1729 1730

    switch (prlEventType) {
M
Michal Privoznik 已提交
1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750
    case PET_DSP_EVT_VM_STATE_CHANGED:
        prlsdkHandleVmStateEvent(privconn, prlEvent, uuid);
        break;
    case PET_DSP_EVT_VM_CONFIG_CHANGED:
        prlsdkHandleVmConfigEvent(privconn, uuid);
        break;
    case PET_DSP_EVT_VM_CREATED:
    case PET_DSP_EVT_VM_ADDED:
        prlsdkHandleVmAddedEvent(privconn, uuid);
        break;
    case PET_DSP_EVT_VM_DELETED:
    case PET_DSP_EVT_VM_UNREGISTERED:
        prlsdkHandleVmRemovedEvent(privconn, uuid);
        break;
    case PET_DSP_EVT_VM_PERFSTATS:
        prlsdkHandlePerfEvent(privconn, prlEvent, uuid);
        /* above function takes own of event */
        prlEvent = PRL_INVALID_HANDLE;
        break;
    default:
1751
        VIR_DEBUG("Skipping event of type %d", prlEventType);
1752 1753 1754 1755
    }

 cleanup:
    PrlHandle_Free(prlEvent);
1756
    return PRL_ERR_SUCCESS;
1757 1758
}

1759
int prlsdkSubscribeToPCSEvents(vzConnPtr privconn)
1760 1761 1762 1763
{
    PRL_RESULT pret = PRL_ERR_UNINITIALIZED;

    pret = PrlSrv_RegEventHandler(privconn->server,
M
Michal Privoznik 已提交
1764 1765
                                  prlsdkEventsHandler,
                                  privconn);
1766 1767 1768 1769 1770 1771 1772
    prlsdkCheckRetGoto(pret, error);
    return 0;

 error:
    return -1;
}

1773
void prlsdkUnsubscribeFromPCSEvents(vzConnPtr privconn)
1774 1775 1776
{
    PRL_RESULT ret = PRL_ERR_UNINITIALIZED;
    ret = PrlSrv_UnregEventHandler(privconn->server,
M
Michal Privoznik 已提交
1777 1778
                                   prlsdkEventsHandler,
                                   privconn);
1779 1780 1781
    if (PRL_FAILED(ret))
        logPrlError(ret);
}
1782

1783
PRL_RESULT prlsdkStart(PRL_HANDLE sdkdom)
1784 1785 1786 1787
{
    PRL_HANDLE job = PRL_INVALID_HANDLE;

    job = PrlVm_StartEx(sdkdom, PSM_VM_START, 0);
1788
    return waitJob(job);
1789 1790
}

1791
static PRL_RESULT prlsdkStopEx(PRL_HANDLE sdkdom, PRL_UINT32 mode)
1792 1793 1794 1795
{
    PRL_HANDLE job = PRL_INVALID_HANDLE;

    job = PrlVm_StopEx(sdkdom, mode, 0);
1796
    return waitJob(job);
1797 1798
}

1799
PRL_RESULT prlsdkKill(PRL_HANDLE sdkdom)
1800
{
1801
    return prlsdkStopEx(sdkdom, PSM_KILL);
1802 1803
}

1804
PRL_RESULT prlsdkStop(PRL_HANDLE sdkdom)
1805
{
1806
    return prlsdkStopEx(sdkdom, PSM_SHUTDOWN);
1807 1808
}

1809
PRL_RESULT prlsdkPause(PRL_HANDLE sdkdom)
1810 1811 1812 1813
{
    PRL_HANDLE job = PRL_INVALID_HANDLE;

    job = PrlVm_Pause(sdkdom, false);
1814
    return waitJob(job);
1815 1816
}

1817
PRL_RESULT prlsdkResume(PRL_HANDLE sdkdom)
1818 1819 1820 1821
{
    PRL_HANDLE job = PRL_INVALID_HANDLE;

    job = PrlVm_Resume(sdkdom);
1822
    return waitJob(job);
1823 1824
}

1825
PRL_RESULT prlsdkSuspend(PRL_HANDLE sdkdom)
1826 1827 1828 1829
{
    PRL_HANDLE job = PRL_INVALID_HANDLE;

    job = PrlVm_Suspend(sdkdom);
1830
    return waitJob(job);
1831 1832
}

1833
int
1834
prlsdkDomainChangeStateLocked(vzConnPtr privconn,
1835 1836
                              virDomainObjPtr dom,
                              prlsdkChangeStateFunc chstate)
1837
{
1838
    vzDomObjPtr pdom;
1839 1840
    PRL_RESULT pret;
    virErrorNumber virerr;
1841 1842

    pdom = dom->privateData;
1843
    pret = chstate(pdom->sdkdom);
1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856
    if (PRL_FAILED(pret)) {
        virResetLastError();

        switch (pret) {
        case PRL_ERR_DISP_VM_IS_NOT_STARTED:
        case PRL_ERR_DISP_VM_IS_NOT_STOPPED:
            virerr = VIR_ERR_OPERATION_INVALID;
            break;
        default:
            virerr = VIR_ERR_OPERATION_FAILED;
        }

        virReportError(virerr, "%s", _("Can't change domain state."));
1857
        return -1;
1858
    }
1859

1860 1861
    return prlsdkUpdateDomain(privconn, dom);
}
1862

1863 1864 1865 1866
int
prlsdkDomainChangeState(virDomainPtr domain,
                        prlsdkChangeStateFunc chstate)
{
1867
    vzConnPtr privconn = domain->conn->privateData;
1868 1869 1870
    virDomainObjPtr dom;
    int ret = -1;

1871
    if (!(dom = vzDomObjFromDomain(domain)))
1872 1873 1874
        return -1;

    ret = prlsdkDomainChangeStateLocked(privconn, dom, chstate);
1875 1876 1877
    virObjectUnlock(dom);
    return ret;
}
1878 1879 1880 1881 1882 1883 1884

static int
prlsdkCheckUnsupportedParams(PRL_HANDLE sdkdom, virDomainDefPtr def)
{
    size_t i;
    PRL_VM_TYPE vmType;
    PRL_RESULT pret;
1885
    virDomainNumatuneMemMode memMode;
1886 1887 1888

    if (def->title) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1889
                       _("titles are not supported by vz driver"));
1890 1891 1892 1893 1894 1895
        return -1;
    }

    if (def->blkio.ndevices > 0) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("blkio parameters are not supported "
1896
                         "by vz driver"));
1897 1898 1899
        return -1;
    }

1900
    if (virDomainDefGetMemoryActual(def) != def->mem.cur_balloon) {
1901
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
M
Michal Privoznik 已提交
1902 1903 1904
                       _("changing balloon parameters is not supported "
                         "by vz driver"));
        return -1;
1905 1906
    }

1907
    if (virDomainDefGetMemoryActual(def) % (1 << 10) != 0) {
1908
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
M
Michal Privoznik 已提交
1909
                       _("Memory size should be multiple of 1Mb."));
1910 1911 1912 1913
        return -1;
    }

    if (def->mem.nhugepages ||
1914 1915
        virMemoryLimitIsSet(def->mem.hard_limit) ||
        virMemoryLimitIsSet(def->mem.soft_limit) ||
1916
        def->mem.min_guarantee ||
1917
        virMemoryLimitIsSet(def->mem.swap_hard_limit)) {
1918 1919 1920

        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Memory parameter is not supported "
1921
                         "by vz driver"));
1922 1923 1924 1925 1926
        return -1;
    }

    if (def->vcpus != def->maxvcpus) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
M
Michal Privoznik 已提交
1927
                       _("current vcpus must be equal to maxvcpus"));
1928 1929 1930 1931 1932 1933
        return -1;
    }

    if (def->placement_mode) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("changing cpu placement mode is not supported "
1934
                         "by vz driver"));
1935 1936 1937 1938 1939 1940
        return -1;
    }

    if (def->cputune.shares ||
        def->cputune.sharesSpecified ||
        def->cputune.period ||
1941
        def->cputune.quota) {
1942 1943

        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1944
                       _("cputune is not supported by vz driver"));
1945 1946 1947
        return -1;
    }

1948
    if (def->cputune.vcpupin) {
M
Michal Privoznik 已提交
1949
        for (i = 0; i < def->vcpus; i++) {
1950 1951 1952 1953 1954 1955 1956 1957 1958
            if (!virBitmapEqual(def->cpumask,
                                def->cputune.vcpupin[i]->cpumask)) {
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                               "%s", _("vcpupin cpumask differs from default cpumask"));
                return -1;
            }
        }
    }

1959 1960 1961 1962 1963 1964

    /*
     * Though we don't support NUMA configuration at the moment
     * virDomainDefPtr always contain non zero NUMA configuration
     * So, just make sure this configuration does't differ from auto generated.
     */
1965 1966
    if ((virDomainNumatuneGetMode(def->numa, -1, &memMode) == 0 &&
         memMode == VIR_DOMAIN_NUMATUNE_MEM_STRICT) ||
M
Michal Privoznik 已提交
1967
        virDomainNumatuneHasPerNodeBinding(def->numa)) {
1968
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
M
Michal Privoznik 已提交
1969 1970
                       _("numa parameters are not supported "
                         "by vz driver"));
1971 1972 1973 1974 1975 1976 1977 1978 1979
        return -1;
    }

    if (def->onReboot != VIR_DOMAIN_LIFECYCLE_RESTART ||
        def->onPoweroff != VIR_DOMAIN_LIFECYCLE_DESTROY ||
        def->onCrash != VIR_DOMAIN_LIFECYCLE_CRASH_DESTROY) {

        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("on_reboot, on_poweroff and on_crash parameters "
1980
                         "are not supported by vz driver"));
1981 1982 1983
        return -1;
    }

1984
    /* we fill only type and arch fields in vzLoadDomain for
1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996
     * hvm type and also init for containers, so we can check that all
     * other paramenters are null and boot devices config is default */

    if (def->os.machine != NULL || def->os.bootmenu != 0 ||
        def->os.kernel != NULL || def->os.initrd != NULL ||
        def->os.cmdline != NULL || def->os.root != NULL ||
        def->os.loader != NULL || def->os.bootloader != NULL ||
        def->os.bootloaderArgs != NULL || def->os.smbios_mode != 0 ||
        def->os.bios.useserial != 0) {

        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("changing OS parameters is not supported "
1997
                         "by vz driver"));
1998 1999 2000 2001 2002 2003 2004 2005 2006
        return -1;
    }

    pret = PrlVmCfg_GetVmType(sdkdom, &vmType);
    if (PRL_FAILED(pret)) {
        logPrlError(pret);
        return -1;
    }

2007 2008
    if (!(vmType == PVT_VM && !IS_CT(def)) &&
        !(vmType == PVT_CT && IS_CT(def))) {
2009 2010 2011

        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("changing OS type is not supported "
2012
                         "by vz driver"));
2013 2014 2015
        return -1;
    }

2016
    if (!IS_CT(def)) {
2017 2018 2019 2020 2021 2022
        if (def->os.nBootDevs != 1 ||
            def->os.bootDevs[0] != VIR_DOMAIN_BOOT_DISK ||
            def->os.init != NULL || def->os.initargv != NULL) {

            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("changing OS parameters is not supported "
2023
                             "by vz driver"));
2024 2025 2026 2027 2028 2029 2030 2031 2032
            return -1;
        }
    } else {
        if (def->os.nBootDevs != 0 ||
            !STREQ_NULLABLE(def->os.init, "/sbin/init") ||
            (def->os.initargv != NULL && def->os.initargv[0] != NULL)) {

            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("changing OS parameters is not supported "
2033
                             "by vz driver"));
2034 2035 2036 2037 2038 2039 2040
            return -1;
        }
    }

    if (def->emulator) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("changing emulator is not supported "
2041
                         "by vz driver"));
2042 2043 2044 2045 2046 2047 2048
        return -1;
    }

    for (i = 0; i < VIR_DOMAIN_FEATURE_LAST; i++) {
        if (def->features[i]) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("changing features is not supported "
2049
                             "by vz driver"));
2050 2051 2052 2053 2054 2055 2056 2057 2058
            return -1;
        }
    }

    if (def->clock.offset != VIR_DOMAIN_CLOCK_OFFSET_UTC ||
        def->clock.ntimers != 0) {

        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("changing clock parameters is not supported "
2059
                         "by vz driver"));
2060 2061 2062
        return -1;
    }

2063 2064 2065
    if (!IS_CT(def) && def->nfss != 0) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Filesystems in VMs are not supported "
2066
                         "by vz driver"));
2067 2068 2069 2070
        return -1;
    }

    if (def->nsounds != 0 || def->nhostdevs != 0 ||
2071 2072 2073 2074 2075 2076
        def->nredirdevs != 0 || def->nsmartcards != 0 ||
        def->nparallels || def->nchannels != 0 ||
        def->nleases != 0 || def->nhubs != 0) {

        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("changing devices parameters is not supported "
2077
                         "by vz driver"));
2078 2079 2080 2081 2082 2083
        return -1;
    }

    /* there may be one auto-input */
    if (def->ninputs != 0 &&
        (def->ninputs != 2 &&
M
Michal Privoznik 已提交
2084 2085 2086 2087
         def->inputs[0]->type != VIR_DOMAIN_INPUT_TYPE_MOUSE &&
         def->inputs[0]->bus != VIR_DOMAIN_INPUT_BUS_PS2 &&
         def->inputs[1]->type != VIR_DOMAIN_INPUT_TYPE_KBD &&
         def->inputs[1]->bus != VIR_DOMAIN_INPUT_BUS_PS2)) {
2088 2089 2090

        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("changing input devices parameters is not supported "
2091
                         "by vz driver"));
2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128
        return -1;
    }

    return 0;
}

static int prlsdkClearDevices(PRL_HANDLE sdkdom)
{
    PRL_RESULT pret;
    PRL_UINT32 n, i;
    PRL_HANDLE devList;
    PRL_HANDLE dev;
    int ret = -1;

    pret = PrlVmCfg_SetVNCMode(sdkdom, PRD_DISABLED);
    prlsdkCheckRetGoto(pret, cleanup);

    pret = PrlVmCfg_GetAllDevices(sdkdom, &devList);
    prlsdkCheckRetGoto(pret, cleanup);

    pret = PrlHndlList_GetItemsCount(devList, &n);
    prlsdkCheckRetGoto(pret, cleanup);

    for (i = 0; i < n; i++) {
        pret = PrlHndlList_GetItem(devList, i, &dev);
        prlsdkCheckRetGoto(pret, cleanup);

        pret = PrlVmDev_Remove(dev);
        PrlHandle_Free(dev);
    }

    ret = 0;
 cleanup:
    PrlHandle_Free(devList);
    return ret;
}

2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191
static int
prlsdkRemoveBootDevices(PRL_HANDLE sdkdom)
{
    PRL_RESULT pret;
    PRL_UINT32 i, devCount;
    PRL_HANDLE dev = PRL_INVALID_HANDLE;
    PRL_DEVICE_TYPE devType;

    pret = PrlVmCfg_GetBootDevCount(sdkdom, &devCount);
    prlsdkCheckRetGoto(pret, error);

    for (i = 0; i < devCount; i++) {

        /* always get device by index 0, because device list resort after delete */
        pret = PrlVmCfg_GetBootDev(sdkdom, 0, &dev);
        prlsdkCheckRetGoto(pret, error);

        pret = PrlBootDev_GetType(dev, &devType);
        prlsdkCheckRetGoto(pret, error);

        pret = PrlBootDev_Remove(dev);
        prlsdkCheckRetGoto(pret, error);
    }

    return 0;

 error:
    return -1;
}

static int
prlsdkAddDeviceToBootList(PRL_HANDLE sdkdom,
                          PRL_UINT32 devIndex,
                          PRL_DEVICE_TYPE devType,
                          PRL_UINT32 bootSequence)
{
    PRL_RESULT pret;
    PRL_HANDLE bootDev = PRL_INVALID_HANDLE;

    pret = PrlVmCfg_CreateBootDev(sdkdom, &bootDev);
    prlsdkCheckRetGoto(pret, error);

    pret = PrlBootDev_SetIndex(bootDev, devIndex);
    prlsdkCheckRetGoto(pret, error);

    pret = PrlBootDev_SetType(bootDev, devType);
    prlsdkCheckRetGoto(pret, error);

    pret = PrlBootDev_SetSequenceIndex(bootDev, bootSequence);
    prlsdkCheckRetGoto(pret, error);

    pret = PrlBootDev_SetInUse(bootDev, PRL_TRUE);
    prlsdkCheckRetGoto(pret, error);

    return 0;

 error:
    if (bootDev != PRL_INVALID_HANDLE)
        PrlBootDev_Remove(bootDev);

    return -1;
}

2192 2193 2194 2195 2196 2197 2198
static int prlsdkCheckGraphicsUnsupportedParams(virDomainDefPtr def)
{
    virDomainGraphicsDefPtr gr;

    if (def->ngraphics == 0)
        return 0;

2199
    if (def->ngraphics > 1) {
2200
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2201
                       _("vz driver supports only "
2202 2203 2204 2205 2206 2207 2208 2209
                         "one VNC per domain."));
        return -1;
    }

    gr = def->graphics[0];

    if (gr->type != VIR_DOMAIN_GRAPHICS_TYPE_VNC) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2210
                       _("vz driver supports only "
2211 2212 2213 2214 2215 2216
                         "VNC graphics."));
        return -1;
    }

    if (gr->data.vnc.websocket != 0) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2217
                       _("vz driver doesn't support "
2218 2219 2220 2221 2222 2223
                         "websockets for VNC graphics."));
        return -1;
    }

    if (gr->data.vnc.keymap != 0) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2224
                       _("vz driver doesn't support "
2225 2226 2227 2228 2229 2230
                         "keymap setting for VNC graphics."));
        return -1;
    }

    if (gr->data.vnc.sharePolicy == VIR_DOMAIN_GRAPHICS_VNC_SHARE_ALLOW_EXCLUSIVE) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2231
                       _("vz driver doesn't support "
2232 2233 2234 2235 2236 2237
                         "exclusive share policy for VNC graphics."));
        return -1;
    }

    if (gr->data.vnc.socket) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2238
                       _("vz driver doesn't support "
2239 2240 2241 2242 2243
                         "VNC graphics over unix sockets."));
        return -1;
    }

    if (gr->data.vnc.auth.connected == VIR_DOMAIN_GRAPHICS_AUTH_CONNECTED_FAIL ||
M
Michal Privoznik 已提交
2244
        gr->data.vnc.auth.connected == VIR_DOMAIN_GRAPHICS_AUTH_CONNECTED_KEEP) {
2245
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2246
                       _("vz driver doesn't support "
2247 2248 2249 2250 2251 2252
                         "given action in case of password change."));
        return -1;
    }

    if (gr->data.vnc.auth.expires) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2253
                       _("vz driver doesn't support "
2254 2255 2256 2257
                         "setting password expire time."));
        return -1;
    }

2258 2259
    if (gr->nListens > 1) {
        virReportError(VIR_ERR_INVALID_ARG, "%s",
2260
                       _("vz driver doesn't support more than "
2261 2262 2263 2264 2265 2266 2267
                         "one listening VNC server per domain"));
        return -1;
    }

    if (gr->nListens == 1 &&
        virDomainGraphicsListenGetType(gr, 0) != VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_ADDRESS) {
        virReportError(VIR_ERR_INVALID_ARG, "%s",
2268
                       _("vz driver supports only address-based VNC listening"));
2269 2270 2271
        return -1;
    }

2272 2273 2274 2275 2276 2277 2278
    return 0;
}

static int prlsdkCheckVideoUnsupportedParams(virDomainDefPtr def)
{
    virDomainVideoDefPtr v;

2279
    if (IS_CT(def)) {
2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290
        if (def->nvideos == 0) {
            return 0;
        } else {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("Video adapters are not supported "
                             "int containers."));
            return -1;
        }
    } else {
        if (def->nvideos != 1) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2291
                           _("vz driver supports "
2292 2293 2294 2295 2296 2297 2298 2299 2300
                             "only one video adapter."));
            return -1;
        }
    }

    v = def->videos[0];

    if (v->type != VIR_DOMAIN_VIDEO_TYPE_VGA) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2301
                       _("vz driver supports "
2302 2303 2304 2305 2306 2307
                         "only VGA video adapters."));
        return -1;
    }

    if (v->heads != 1) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2308
                       _("vz driver doesn't support "
2309 2310 2311 2312
                         "multihead video adapters."));
        return -1;
    }

2313
    if (v->accel != NULL && (v->accel->support2d || v->accel->support3d)) {
2314
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2315
                       _("vz driver doesn't support "
2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327
                         "setting video acceleration parameters."));
        return -1;
    }

    return 0;
}

static int prlsdkCheckSerialUnsupportedParams(virDomainChrDefPtr chr)
{
    if (chr->deviceType != VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Specified character device type is not supported "
2328
                         "by vz driver."));
2329 2330 2331 2332 2333 2334
        return -1;
    }

    if (chr->targetTypeAttr) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Specified character device target type is not "
2335
                         "supported by vz driver."));
2336 2337 2338 2339 2340 2341 2342 2343 2344 2345
        return -1;
    }

    if (chr->source.type != VIR_DOMAIN_CHR_TYPE_DEV &&
        chr->source.type != VIR_DOMAIN_CHR_TYPE_FILE &&
        chr->source.type != VIR_DOMAIN_CHR_TYPE_UNIX) {


        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Specified character device source type is not "
2346
                         "supported by vz driver."));
2347 2348 2349 2350 2351 2352
        return -1;
    }

    if (chr->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Setting device info for character devices is not "
2353
                         "supported by vz driver."));
2354 2355 2356 2357 2358 2359
        return -1;
    }

    if (chr->nseclabels > 0) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Setting security labels is not "
2360
                         "supported by vz driver."));
2361 2362 2363 2364 2365 2366 2367 2368
        return -1;
    }

    return 0;
}

static int prlsdkCheckNetUnsupportedParams(virDomainNetDefPtr net)
{
2369 2370
    if (net->type != VIR_DOMAIN_NET_TYPE_NETWORK &&
        net->type != VIR_DOMAIN_NET_TYPE_BRIDGE) {
2371 2372
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Specified network adapter type is not "
2373
                         "supported by vz driver."));
2374 2375 2376 2377 2378 2379
        return -1;
    }

    if (net->backend.tap || net->backend.vhost) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Interface backend parameters are not "
2380
                         "supported by vz driver."));
2381 2382 2383 2384 2385 2386
        return -1;
    }

    if (net->data.network.portgroup) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Virtual network portgroups are not "
2387
                         "supported by vz driver."));
2388 2389 2390 2391 2392 2393
        return -1;
    }

    if (net->tune.sndbuf_specified) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Setting interface sndbuf is not "
2394
                         "supported by vz driver."));
2395 2396 2397 2398 2399 2400
        return -1;
    }

    if (net->script) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Setting interface script is not "
2401
                         "supported by vz driver."));
2402 2403 2404 2405 2406 2407
        return -1;
    }

    if (net->ifname_guest) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Setting guest interface name is not "
2408
                         "supported by vz driver."));
2409 2410 2411 2412 2413 2414
        return -1;
    }

    if (net->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Setting device info for network devices is not "
2415
                         "supported by vz driver."));
2416 2417 2418 2419 2420 2421
        return -1;
    }

    if (net->filter) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Setting network filter is not "
2422
                         "supported by vz driver."));
2423 2424 2425 2426 2427 2428
        return -1;
    }

    if (net->bandwidth) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Setting network bandwidth is not "
2429
                         "supported by vz driver."));
2430 2431 2432 2433 2434 2435
        return -1;
    }

    if (net->vlan.trunk) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Setting up vlans is not "
2436
                         "supported by vz driver."));
2437 2438 2439 2440 2441 2442 2443 2444
        return -1;
    }

    return 0;
}

static int prlsdkCheckDiskUnsupportedParams(virDomainDiskDefPtr disk)
{
D
Dmitry Guryanov 已提交
2445 2446 2447
    if (disk->device != VIR_DOMAIN_DISK_DEVICE_DISK &&
        disk->device != VIR_DOMAIN_DISK_DEVICE_CDROM) {

2448
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
D
Dmitry Guryanov 已提交
2449
                       _("Only hard disks and cdroms are supported "
2450
                         "by vz driver."));
2451 2452 2453
        return -1;
    }

M
Michal Privoznik 已提交
2454 2455
    if (disk->blockio.logical_block_size ||
        disk->blockio.physical_block_size) {
2456 2457
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Setting disk block sizes is not "
2458
                         "supported by vz driver."));
2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470
        return -1;
    }

    if (disk->blkdeviotune.total_bytes_sec ||
        disk->blkdeviotune.read_bytes_sec ||
        disk->blkdeviotune.write_bytes_sec ||
        disk->blkdeviotune.total_iops_sec ||
        disk->blkdeviotune.read_iops_sec ||
        disk->blkdeviotune.write_iops_sec) {

        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Setting disk io limits is not "
2471
                         "supported by vz driver yet."));
2472 2473 2474 2475
        return -1;
    }

    if (disk->serial) {
2476
        VIR_INFO("%s", _("Setting disk serial number is not "
2477
                         "supported by vz driver."));
2478 2479 2480 2481 2482
    }

    if (disk->wwn) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Setting disk wwn id is not "
2483
                         "supported by vz driver."));
2484 2485 2486 2487 2488 2489
        return -1;
    }

    if (disk->vendor) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Setting disk vendor is not "
2490
                         "supported by vz driver."));
2491 2492 2493 2494 2495 2496
        return -1;
    }

    if (disk->product) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Setting disk product id is not "
2497
                         "supported by vz driver."));
2498 2499 2500 2501 2502 2503
        return -1;
    }

    if (disk->error_policy != VIR_DOMAIN_DISK_ERROR_POLICY_DEFAULT) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Setting disk error policy is not "
2504
                         "supported by vz driver."));
2505 2506 2507 2508 2509 2510
        return -1;
    }

    if (disk->iomode) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Setting disk io mode is not "
2511
                         "supported by vz driver."));
2512 2513 2514 2515 2516 2517
        return -1;
    }

    if (disk->copy_on_read) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Disk copy_on_read is not "
2518
                         "supported by vz driver."));
2519 2520 2521 2522 2523 2524
        return -1;
    }

    if (disk->startupPolicy != VIR_DOMAIN_STARTUP_POLICY_DEFAULT) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Setting up disk startup policy is not "
2525
                         "supported by vz driver."));
2526 2527 2528 2529 2530 2531
        return -1;
    }

    if (disk->transient) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Transient disks are not "
2532
                         "supported by vz driver."));
2533 2534 2535 2536 2537 2538
        return -1;
    }

    if (disk->discard) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Setting up disk discard parameter is not "
2539
                         "supported by vz driver."));
2540 2541 2542 2543 2544 2545
        return -1;
    }

    if (disk->iothread) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Setting up disk io thread # is not "
2546
                         "supported by vz driver."));
2547 2548 2549 2550 2551 2552 2553 2554
        return -1;
    }

    if (disk->src->type != VIR_STORAGE_TYPE_FILE &&
        disk->src->type != VIR_STORAGE_TYPE_BLOCK) {

        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Only disk and block storage types are "
2555
                         "supported by vz driver."));
2556 2557 2558 2559 2560 2561 2562
        return -1;

    }

    return 0;
}

2563 2564 2565 2566 2567
static int prlsdkCheckFSUnsupportedParams(virDomainFSDefPtr fs)
{
    if (fs->type != VIR_DOMAIN_FS_TYPE_FILE) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Only file based filesystems are "
2568
                         "supported by vz driver."));
2569 2570 2571 2572 2573 2574
        return -1;
    }

    if (fs->fsdriver != VIR_DOMAIN_FS_DRIVER_TYPE_PLOOP) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Only ploop fs driver is "
2575
                         "supported by vz driver."));
2576 2577 2578 2579 2580 2581
        return -1;
    }

    if (fs->accessmode != VIR_DOMAIN_FS_ACCESSMODE_PASSTHROUGH) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Changing fs access mode is not "
2582
                         "supported by vz driver."));
2583 2584 2585 2586 2587 2588
        return -1;
    }

    if (fs->wrpolicy != VIR_DOMAIN_FS_WRPOLICY_DEFAULT) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Changing fs write policy is not "
2589
                         "supported by vz driver."));
2590 2591 2592 2593 2594 2595
        return -1;
    }

    if (fs->format != VIR_STORAGE_FILE_PLOOP) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Only ploop disk images are "
2596
                         "supported by vz driver."));
2597 2598 2599 2600 2601 2602
        return -1;
    }

    if (fs->readonly) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Setting readonly for filesystems is "
2603
                         "supported by vz driver."));
2604 2605 2606 2607 2608 2609
        return -1;
    }

    if (fs->space_hard_limit || fs->space_soft_limit) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Setting fs quotas is not "
2610
                         "supported by vz driver."));
2611 2612 2613 2614 2615 2616
        return -1;
    }

    return 0;
}

2617 2618 2619 2620 2621
static int prlsdkApplyGraphicsParams(PRL_HANDLE sdkdom, virDomainDefPtr def)
{
    virDomainGraphicsDefPtr gr;
    PRL_RESULT pret;
    int ret  = -1;
2622
    const char *listenAddr = NULL;
2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642

    if (prlsdkCheckGraphicsUnsupportedParams(def))
        return -1;

    if (def->ngraphics == 0)
        return 0;

    gr = def->graphics[0];

    if (gr->data.vnc.autoport) {
        pret = PrlVmCfg_SetVNCMode(sdkdom, PRD_AUTO);
        prlsdkCheckRetGoto(pret, cleanup);
    } else {
        pret = PrlVmCfg_SetVNCMode(sdkdom, PRD_MANUAL);
        prlsdkCheckRetGoto(pret, cleanup);

        pret = PrlVmCfg_SetVNCPort(sdkdom, gr->data.vnc.port);
        prlsdkCheckRetGoto(pret, cleanup);
    }

2643 2644 2645 2646 2647 2648 2649 2650
    if (gr->nListens == 1) {
        listenAddr = virDomainGraphicsListenGetAddress(gr, 0);
        if (!listenAddr)
            goto cleanup;
        pret = PrlVmCfg_SetVNCHostName(sdkdom, listenAddr);
        prlsdkCheckRetGoto(pret, cleanup);
    }

2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683
    ret = 0;
 cleanup:
    return ret;
}

static int prlsdkApplyVideoParams(PRL_HANDLE sdkdom ATTRIBUTE_UNUSED, virDomainDefPtr def)
{
    PRL_RESULT pret;

    if (def->nvideos == 0)
        return 0;

    if (IS_CT(def)) {
        /* ignore video parameters */
        return 0;
    }

    if (prlsdkCheckVideoUnsupportedParams(def))
        return -1;

    pret = PrlVmCfg_SetVideoRamSize(sdkdom, def->videos[0]->vram >> 10);
    prlsdkCheckRetGoto(pret, error);

    return 0;
 error:
    return -1;
}

static int prlsdkAddSerial(PRL_HANDLE sdkdom, virDomainChrDefPtr chr)
{
    PRL_RESULT pret;
    PRL_HANDLE sdkchr = PRL_INVALID_HANDLE;
    PRL_VM_DEV_EMULATION_TYPE emutype;
M
Michal Privoznik 已提交
2684
    PRL_SERIAL_PORT_SOCKET_OPERATION_MODE socket_mode = PSP_SERIAL_SOCKET_SERVER;
2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712
    char *path;
    int ret = -1;

    if (prlsdkCheckSerialUnsupportedParams(chr) < 0)
        return -1;

    pret = PrlVmCfg_CreateVmDev(sdkdom, PDE_SERIAL_PORT, &sdkchr);
    prlsdkCheckRetGoto(pret, cleanup);

    switch (chr->source.type) {
    case VIR_DOMAIN_CHR_TYPE_DEV:
        emutype = PDT_USE_REAL_DEVICE;
        path = chr->source.data.file.path;
        break;
    case VIR_DOMAIN_CHR_TYPE_FILE:
        emutype = PDT_USE_OUTPUT_FILE;
        path = chr->source.data.file.path;
        break;
    case VIR_DOMAIN_CHR_TYPE_UNIX:
        emutype = PDT_USE_SERIAL_PORT_SOCKET_MODE;
        path = chr->source.data.nix.path;
        if (chr->source.data.nix.listen)
            socket_mode = PSP_SERIAL_SOCKET_SERVER;
        else
            socket_mode = PSP_SERIAL_SOCKET_CLIENT;
        break;
    default:
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2713
                       _("vz driver doesn't support "
2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755
                         "specified serial source type."));
        goto cleanup;
    }

    pret = PrlVmDev_SetEmulatedType(sdkchr, emutype);
    prlsdkCheckRetGoto(pret, cleanup);

    pret = PrlVmDev_SetSysName(sdkchr, path);
    prlsdkCheckRetGoto(pret, cleanup);

    pret = PrlVmDev_SetFriendlyName(sdkchr, path);
    prlsdkCheckRetGoto(pret, cleanup);

    if (chr->source.type == VIR_DOMAIN_CHR_TYPE_UNIX) {
        pret = PrlVmDevSerial_SetSocketMode(sdkchr, socket_mode);
        prlsdkCheckRetGoto(pret, cleanup);
    }

    pret = PrlVmDev_SetEnabled(sdkchr, 1);
    prlsdkCheckRetGoto(pret, cleanup);

    pret = PrlVmDev_SetIndex(sdkchr, chr->target.port);
    prlsdkCheckRetGoto(pret, cleanup);

    ret = 0;
 cleanup:
    PrlHandle_Free(sdkchr);
    return ret;
}

#define PRL_MAC_STRING_BUFNAME  13

static const char * prlsdkFormatMac(virMacAddrPtr mac, char *macstr)
{
    snprintf(macstr, PRL_MAC_STRING_BUFNAME,
             "%02X%02X%02X%02X%02X%02X",
             mac->addr[0], mac->addr[1], mac->addr[2],
             mac->addr[3], mac->addr[4], mac->addr[5]);
    macstr[PRL_MAC_STRING_BUFNAME - 1] = '\0';
    return macstr;
}

2756
static int prlsdkAddNet(PRL_HANDLE sdkdom,
2757
                        vzConnPtr privconn,
2758 2759
                        virDomainNetDefPtr net,
                        bool isCt)
2760 2761 2762
{
    PRL_RESULT pret;
    PRL_HANDLE sdknet = PRL_INVALID_HANDLE;
2763 2764
    PRL_HANDLE vnet = PRL_INVALID_HANDLE;
    PRL_HANDLE job = PRL_INVALID_HANDLE;
2765 2766
    PRL_HANDLE addrlist = PRL_INVALID_HANDLE;
    size_t i;
2767 2768
    int ret = -1;
    char macstr[PRL_MAC_STRING_BUFNAME];
2769 2770 2771
    char *addrstr = NULL;
    bool ipv6present = false;
    bool ipv4present = false;
2772 2773 2774 2775 2776 2777 2778 2779 2780 2781

    if (prlsdkCheckNetUnsupportedParams(net) < 0)
        return -1;

    pret = PrlVmCfg_CreateVmDev(sdkdom, PDE_GENERIC_NETWORK_ADAPTER, &sdknet);
    prlsdkCheckRetGoto(pret, cleanup);

    pret = PrlVmDev_SetEnabled(sdknet, 1);
    prlsdkCheckRetGoto(pret, cleanup);

2782 2783 2784
    pret = PrlVmDev_SetConnected(sdknet, net->linkstate !=
                                 VIR_DOMAIN_NET_INTERFACE_LINK_STATE_DOWN);

2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795
    prlsdkCheckRetGoto(pret, cleanup);

    if (net->ifname) {
        pret = PrlVmDevNet_SetHostInterfaceName(sdknet, net->ifname);
        prlsdkCheckRetGoto(pret, cleanup);
    }

    prlsdkFormatMac(&net->mac, macstr);
    pret = PrlVmDevNet_SetMacAddress(sdknet, macstr);
    prlsdkCheckRetGoto(pret, cleanup);

2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914
    pret = PrlApi_CreateStringsList(&addrlist);
    prlsdkCheckRetGoto(pret, cleanup);

    for (i = 0; i < net->nips; i++) {
        char *tmpstr;

        if (AF_INET == VIR_SOCKET_ADDR_FAMILY(&net->ips[i]->address))
            ipv4present = true;
        else if (AF_INET6 == VIR_SOCKET_ADDR_FAMILY(&net->ips[i]->address))
            ipv6present = true;
        else
            continue;

        if (!(tmpstr = virSocketAddrFormat(&net->ips[i]->address)))
            goto cleanup;

        if (virAsprintf(&addrstr, "%s/%d", tmpstr, net->ips[i]->prefix) < 0) {
            VIR_FREE(tmpstr);
            goto cleanup;
        }

        VIR_FREE(tmpstr);
        pret = PrlStrList_AddItem(addrlist, addrstr);
        prlsdkCheckRetGoto(pret, cleanup);

        VIR_FREE(addrstr);
    }

    if (ipv4present || ipv6present) {
        pret = PrlVmDevNet_SetNetAddresses(sdknet, addrlist);
        prlsdkCheckRetGoto(pret, cleanup);
    }

    pret = PrlVmDevNet_SetConfigureWithDhcp(sdknet, !ipv4present);
    prlsdkCheckRetGoto(pret, cleanup);

    pret = PrlVmDevNet_SetConfigureWithDhcpIPv6(sdknet, !ipv6present);
    prlsdkCheckRetGoto(pret, cleanup);

    pret = PrlVmDevNet_SetAutoApply(sdknet, true);
    prlsdkCheckRetGoto(pret, cleanup);

    if (net->nroutes) {
        bool alreadySetIPv4Gateway = false;
        bool alreadySetIPv6Gateway = false;

        for (i = 0; i < net->nroutes; i++) {
            virSocketAddrPtr addrdst, gateway;
            virSocketAddr zero;

            addrdst = virNetworkRouteDefGetAddress(net->routes[i]);
            gateway = virNetworkRouteDefGetGateway(net->routes[i]);

            ignore_value(virSocketAddrParse(&zero,
                                    (VIR_SOCKET_ADDR_IS_FAMILY(addrdst, AF_INET)
                                     ? VIR_SOCKET_ADDR_IPV4_ALL
                                     : VIR_SOCKET_ADDR_IPV6_ALL),
                                    VIR_SOCKET_ADDR_FAMILY(addrdst)));

            if (!virSocketAddrEqual(addrdst, &zero)) {
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                               _("Support only default gateway"));
                goto cleanup;
            }

            switch (VIR_SOCKET_ADDR_FAMILY(gateway)) {
            case AF_INET:

                if (!ipv4present)
                    continue;

                if (alreadySetIPv4Gateway) {
                    virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                                   _("Support only one IPv4 default gateway"));
                    goto cleanup;
                }

                if (!(addrstr = virSocketAddrFormat(gateway)))
                    goto cleanup;

                pret = PrlVmDevNet_SetDefaultGateway(sdknet, addrstr);
                prlsdkCheckRetGoto(pret, cleanup);

                alreadySetIPv4Gateway = true;
                break;

            case AF_INET6:

                if (!ipv6present)
                    continue;

                if (alreadySetIPv6Gateway) {
                    virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                                   _("Support only one IPv6 default gateway"));
                    goto cleanup;
                }

                if (!(addrstr = virSocketAddrFormat(gateway)))
                    goto cleanup;

                pret = PrlVmDevNet_SetDefaultGatewayIPv6(sdknet, addrstr);
                prlsdkCheckRetGoto(pret, cleanup);

                alreadySetIPv6Gateway = true;
                break;

            default:
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                               _("Unsupported address family %d "
                                 "Only IPv4 or IPv6 default gateway"),
                               VIR_SOCKET_ADDR_FAMILY(gateway));

                goto cleanup;
            }

            VIR_FREE(addrstr);
        }
    }

2915 2916
    if (isCt) {
        if (net->model)
M
Michal Privoznik 已提交
2917 2918
            VIR_WARN("Setting network adapter for containers is not "
                     "supported by vz driver.");
2919
    } else {
2920 2921 2922 2923 2924 2925 2926 2927
        if (STREQ(net->model, "rtl8139")) {
            pret = PrlVmDevNet_SetAdapterType(sdknet, PNT_RTL);
        } else if (STREQ(net->model, "e1000")) {
            pret = PrlVmDevNet_SetAdapterType(sdknet, PNT_E1000);
        } else if (STREQ(net->model, "virtio")) {
            pret = PrlVmDevNet_SetAdapterType(sdknet, PNT_VIRTIO);
        } else {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
M
Michal Privoznik 已提交
2928 2929
                           _("Specified network adapter model is not "
                             "supported by vz driver."));
2930 2931 2932
            goto cleanup;
        }
        prlsdkCheckRetGoto(pret, cleanup);
2933 2934
    }

2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952
    if (net->type == VIR_DOMAIN_NET_TYPE_NETWORK) {
        if (STREQ(net->data.network.name, PARALLELS_DOMAIN_ROUTED_NETWORK_NAME)) {
            pret = PrlVmDev_SetEmulatedType(sdknet, PNA_ROUTED);
            prlsdkCheckRetGoto(pret, cleanup);
        } else if (STREQ(net->data.network.name, PARALLELS_DOMAIN_BRIDGED_NETWORK_NAME)) {
            pret = PrlVmDev_SetEmulatedType(sdknet, PNA_BRIDGED_ETHERNET);
            prlsdkCheckRetGoto(pret, cleanup);

            pret = PrlVmDevNet_SetVirtualNetworkId(sdknet, net->data.network.name);
            prlsdkCheckRetGoto(pret, cleanup);
        }
    } else if (net->type == VIR_DOMAIN_NET_TYPE_BRIDGE) {
        /*
         * For this type of adapter we create a new
         * Virtual Network assuming that bridge with given name exists
         * Failing creating this means domain creation failure
         */
        pret = PrlVirtNet_Create(&vnet);
2953
        prlsdkCheckRetGoto(pret, cleanup);
2954 2955 2956 2957 2958 2959 2960

        pret = PrlVirtNet_SetNetworkId(vnet, net->data.network.name);
        prlsdkCheckRetGoto(pret, cleanup);

        pret = PrlVirtNet_SetNetworkType(vnet, PVN_BRIDGED_ETHERNET);
        prlsdkCheckRetGoto(pret, cleanup);

2961 2962 2963
        job = PrlSrv_AddVirtualNetwork(privconn->server,
                                       vnet,
                                       PRL_USE_VNET_NAME_FOR_BRIDGE_NAME);
2964
        if (PRL_FAILED(pret = waitJob(job)))
2965 2966 2967 2968 2969
            goto cleanup;

        pret = PrlVmDev_SetEmulatedType(sdknet, PNA_BRIDGED_ETHERNET);
        prlsdkCheckRetGoto(pret, cleanup);

2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981
        pret = PrlVmDevNet_SetVirtualNetworkId(sdknet, net->data.network.name);
        prlsdkCheckRetGoto(pret, cleanup);
    }

    if (net->trustGuestRxFilters == VIR_TRISTATE_BOOL_YES)
        pret = PrlVmDevNet_SetPktFilterPreventMacSpoof(sdknet, 0);
    else if (net->trustGuestRxFilters == VIR_TRISTATE_BOOL_NO)
        pret = PrlVmDevNet_SetPktFilterPreventMacSpoof(sdknet, 1);
    prlsdkCheckRetGoto(pret, cleanup);

    ret = 0;
 cleanup:
2982 2983
    VIR_FREE(addrstr);
    PrlHandle_Free(addrlist);
2984
    PrlHandle_Free(vnet);
2985 2986 2987 2988
    PrlHandle_Free(sdknet);
    return ret;
}

2989 2990
static void
prlsdkCleanupBridgedNet(vzConnPtr privconn, virDomainNetDefPtr net)
2991 2992 2993 2994 2995
{
    PRL_RESULT pret;
    PRL_HANDLE vnet = PRL_INVALID_HANDLE;
    PRL_HANDLE job = PRL_INVALID_HANDLE;

2996 2997
    if (net->type != VIR_DOMAIN_NET_TYPE_BRIDGE)
        return;
2998 2999 3000 3001 3002 3003 3004

    pret = PrlVirtNet_Create(&vnet);
    prlsdkCheckRetGoto(pret, cleanup);

    pret = PrlVirtNet_SetNetworkId(vnet, net->data.network.name);
    prlsdkCheckRetGoto(pret, cleanup);

3005
    job = PrlSrv_DeleteVirtualNetwork(privconn->server, vnet, 0);
3006
    if (PRL_FAILED(pret = waitJob(job)))
3007 3008 3009 3010 3011 3012
        goto cleanup;

 cleanup:
    PrlHandle_Free(vnet);
}

3013
int prlsdkAttachNet(virDomainObjPtr dom,
M
Michal Privoznik 已提交
3014
                    vzConnPtr privconn,
3015 3016 3017
                    virDomainNetDefPtr net)
{
    int ret = -1;
M
Michal Privoznik 已提交
3018
    vzDomObjPtr privdom = dom->privateData;
3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033
    PRL_HANDLE job = PRL_INVALID_HANDLE;

    if (!IS_CT(dom->def)) {
        virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
                       _("network device cannot be attached"));
        return ret;
    }

    job = PrlVm_BeginEdit(privdom->sdkdom);
    if (PRL_FAILED(waitJob(job)))
        return ret;

    ret = prlsdkAddNet(privdom->sdkdom, privconn, net, IS_CT(dom->def));
    if (ret == 0) {
        job = PrlVm_CommitEx(privdom->sdkdom, PVCF_DETACH_HDD_BUNDLE);
D
Dmitry Guryanov 已提交
3034
        if (PRL_FAILED(waitJob(job)))
3035 3036 3037 3038 3039 3040
            return -1;
    }

    return ret;
}

3041 3042
static PRL_HANDLE
prlsdkFindNetByMAC(PRL_HANDLE sdkdom, virMacAddrPtr mac)
3043 3044 3045 3046 3047 3048 3049 3050 3051
{
    PRL_RESULT pret;
    PRL_UINT32 adaptersCount;
    PRL_UINT32 i;
    PRL_HANDLE adapter = PRL_INVALID_HANDLE;
    PRL_UINT32 len;
    char adapterMac[PRL_MAC_STRING_BUFNAME];
    char expectedMac[PRL_MAC_STRING_BUFNAME];

3052 3053
    prlsdkFormatMac(mac, expectedMac);

3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065
    pret = PrlVmCfg_GetNetAdaptersCount(sdkdom, &adaptersCount);
    prlsdkCheckRetGoto(pret, cleanup);

    for (i = 0; i < adaptersCount; ++i) {
        pret = PrlVmCfg_GetNetAdapter(sdkdom, i, &adapter);
        prlsdkCheckRetGoto(pret, cleanup);

        len = sizeof(adapterMac);
        memset(adapterMac, 0, sizeof(adapterMac));
        pret = PrlVmDevNet_GetMacAddress(adapter, adapterMac, &len);
        prlsdkCheckRetGoto(pret, cleanup);

3066 3067
        if (memcmp(adapterMac, expectedMac, PRL_MAC_STRING_BUFNAME) == 0)
            return adapter;
3068

3069 3070
        PrlHandle_Free(adapter);
        adapter = PRL_INVALID_HANDLE;
3071 3072 3073 3074
    }

 cleanup:
    PrlHandle_Free(adapter);
3075
    return adapter;
3076 3077 3078
}

int prlsdkDetachNet(virDomainObjPtr dom,
M
Michal Privoznik 已提交
3079
                    vzConnPtr privconn,
3080 3081
                    virDomainNetDefPtr net)
{
3082
    int ret = -1;
M
Michal Privoznik 已提交
3083
    vzDomObjPtr privdom = dom->privateData;
3084
    PRL_HANDLE job = PRL_INVALID_HANDLE;
3085 3086
    PRL_HANDLE sdknet = PRL_INVALID_HANDLE;
    PRL_RESULT pret;
3087 3088 3089 3090

    if (!IS_CT(dom->def)) {
        virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
                       _("network device cannot be detached"));
3091
        goto cleanup;
3092 3093 3094 3095
    }

    job = PrlVm_BeginEdit(privdom->sdkdom);
    if (PRL_FAILED(waitJob(job)))
3096
        goto cleanup;
3097

3098 3099 3100
    sdknet = prlsdkFindNetByMAC(privdom->sdkdom, &net->mac);
    if (sdknet == PRL_INVALID_HANDLE)
        goto cleanup;
3101

3102
    prlsdkCleanupBridgedNet(privconn, net);
3103

3104 3105 3106 3107 3108 3109 3110 3111
    pret = PrlVmDev_Remove(sdknet);
    prlsdkCheckRetGoto(pret, cleanup);

    job = PrlVm_CommitEx(privdom->sdkdom, PVCF_DETACH_HDD_BUNDLE);
    if (PRL_FAILED(waitJob(job)))
        goto cleanup;

    ret = 0;
3112

3113 3114
 cleanup:
    PrlHandle_Free(sdknet);
3115 3116 3117
    return ret;
}

3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136
static int prlsdkDelDisk(PRL_HANDLE sdkdom, int idx)
{
    int ret = -1;
    PRL_RESULT pret;
    PRL_HANDLE sdkdisk = PRL_INVALID_HANDLE;

    pret = PrlVmCfg_GetHardDisk(sdkdom, idx, &sdkdisk);
    prlsdkCheckRetGoto(pret, cleanup);

    pret = PrlVmDev_Remove(sdkdisk);
    prlsdkCheckRetGoto(pret, cleanup);

    ret = 0;

 cleanup:
    PrlHandle_Free(sdkdisk);
    return ret;
}

3137 3138 3139 3140
static int prlsdkAddDisk(PRL_HANDLE sdkdom,
                         virDomainDiskDefPtr disk,
                         bool bootDisk,
                         bool isCt)
3141 3142 3143 3144 3145 3146
{
    PRL_RESULT pret;
    PRL_HANDLE sdkdisk = PRL_INVALID_HANDLE;
    int ret = -1;
    PRL_VM_DEV_EMULATION_TYPE emutype;
    PRL_MASS_STORAGE_INTERFACE_TYPE sdkbus;
3147 3148
    int idx;
    virDomainDeviceDriveAddressPtr drive;
3149 3150
    PRL_UINT32 devIndex;
    PRL_DEVICE_TYPE devType;
3151
    char *dst = NULL;
3152 3153 3154 3155

    if (prlsdkCheckDiskUnsupportedParams(disk) < 0)
        return -1;

D
Dmitry Guryanov 已提交
3156
    if (disk->device == VIR_DOMAIN_DISK_DEVICE_DISK)
3157
        devType = PDE_HARD_DISK;
D
Dmitry Guryanov 已提交
3158
    else
3159 3160 3161
        devType = PDE_OPTICAL_DISK;

    pret = PrlVmCfg_CreateVmDev(sdkdom, devType, &sdkdisk);
3162 3163 3164 3165 3166 3167 3168 3169 3170
    prlsdkCheckRetGoto(pret, cleanup);

    pret = PrlVmDev_SetEnabled(sdkdisk, 1);
    prlsdkCheckRetGoto(pret, cleanup);

    pret = PrlVmDev_SetConnected(sdkdisk, 1);
    prlsdkCheckRetGoto(pret, cleanup);

    if (disk->src->type == VIR_STORAGE_TYPE_FILE) {
D
Dmitry Guryanov 已提交
3171 3172 3173
        if (disk->device == VIR_DOMAIN_DISK_DEVICE_DISK &&
            virDomainDiskGetFormat(disk) != VIR_STORAGE_FILE_PLOOP) {

3174
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("Invalid format of "
M
Michal Privoznik 已提交
3175 3176
                                                         "disk %s, vz driver supports only "
                                                         "images in ploop format."), disk->src->path);
3177 3178 3179 3180 3181
            goto cleanup;
        }

        emutype = PDT_USE_IMAGE_FILE;
    } else {
3182 3183 3184 3185 3186 3187
        if (disk->device == VIR_DOMAIN_DISK_DEVICE_DISK &&
            (virDomainDiskGetFormat(disk) != VIR_STORAGE_FILE_RAW &&
             virDomainDiskGetFormat(disk) != VIR_STORAGE_FILE_NONE &&
             virDomainDiskGetFormat(disk) != VIR_STORAGE_FILE_AUTO)) {

            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("Invalid format "
M
Michal Privoznik 已提交
3188 3189
                                                         "of disk %s, it should be either not set, or set "
                                                         "to raw or auto."), disk->src->path);
3190 3191
            goto cleanup;
        }
3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203
        emutype = PDT_USE_REAL_DEVICE;
    }

    pret = PrlVmDev_SetEmulatedType(sdkdisk, emutype);
    prlsdkCheckRetGoto(pret, cleanup);

    pret = PrlVmDev_SetSysName(sdkdisk, disk->src->path);
    prlsdkCheckRetGoto(pret, cleanup);

    pret = PrlVmDev_SetFriendlyName(sdkdisk, disk->src->path);
    prlsdkCheckRetGoto(pret, cleanup);

3204 3205 3206 3207
    drive = &disk->info.addr.drive;
    if (drive->controller > 0) {
        /* We have only one controller of each type */
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("Invalid drive "
M
Michal Privoznik 已提交
3208 3209
                                                     "address of disk %s, vz driver supports "
                                                     "only one controller."), disk->dst);
3210 3211 3212 3213 3214
        goto cleanup;
    }

    if (drive->target > 0) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("Invalid drive "
M
Michal Privoznik 已提交
3215 3216
                                                     "address of disk %s, vz driver supports "
                                                     "only target 0."), disk->dst);
3217 3218 3219
        goto cleanup;
    }

3220 3221
    switch (disk->bus) {
    case VIR_DOMAIN_DISK_BUS_IDE:
3222 3223
        if (drive->unit > 1) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("Invalid drive "
M
Michal Privoznik 已提交
3224 3225
                                                         "address of disk %s, vz driver supports "
                                                         "only units 0-1 for IDE bus."), disk->dst);
3226 3227
            goto cleanup;
        }
3228
        sdkbus = PMS_IDE_DEVICE;
3229
        idx = 2 * drive->bus + drive->unit;
3230
        dst = virIndexToDiskName(idx, "hd");
3231 3232
        break;
    case VIR_DOMAIN_DISK_BUS_SCSI:
3233 3234
        if (drive->bus > 0) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("Invalid drive "
M
Michal Privoznik 已提交
3235 3236
                                                         "address of disk %s, vz driver supports "
                                                         "only bus 0 for SCSI bus."), disk->dst);
3237 3238
            goto cleanup;
        }
3239
        sdkbus = PMS_SCSI_DEVICE;
3240
        idx = drive->unit;
3241
        dst = virIndexToDiskName(idx, "sd");
3242 3243
        break;
    case VIR_DOMAIN_DISK_BUS_SATA:
3244 3245
        if (drive->bus > 0) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("Invalid drive "
M
Michal Privoznik 已提交
3246 3247
                                                         "address of disk %s, vz driver supports "
                                                         "only bus 0 for SATA bus."), disk->dst);
3248 3249
            goto cleanup;
        }
3250
        sdkbus = PMS_SATA_DEVICE;
3251
        idx = drive->unit;
3252
        dst = virIndexToDiskName(idx, "sd");
3253 3254 3255 3256
        break;
    default:
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Specified disk bus is not "
3257
                         "supported by vz driver."));
3258 3259 3260
        goto cleanup;
    }

3261 3262 3263 3264 3265
    if (!dst)
        goto cleanup;

    if (STRNEQ(dst, disk->dst)) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("Invalid drive "
M
Michal Privoznik 已提交
3266 3267
                                                     "address of disk %s, vz driver supports "
                                                     "only defaults address to logical device name."), disk->dst);
3268 3269 3270
        goto cleanup;
    }

3271 3272 3273
    pret = PrlVmDev_SetIfaceType(sdkdisk, sdkbus);
    prlsdkCheckRetGoto(pret, cleanup);

3274
    pret = PrlVmDev_SetStackIndex(sdkdisk, idx);
3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290
    prlsdkCheckRetGoto(pret, cleanup);

    switch (disk->cachemode) {
    case VIR_DOMAIN_DISK_CACHE_DISABLE:
        pret = PrlVmCfg_SetDiskCacheWriteBack(sdkdom, PRL_FALSE);
        prlsdkCheckRetGoto(pret, cleanup);
        break;
    case VIR_DOMAIN_DISK_CACHE_WRITEBACK:
        pret = PrlVmCfg_SetDiskCacheWriteBack(sdkdom, PRL_TRUE);
        prlsdkCheckRetGoto(pret, cleanup);
        break;
    case VIR_DOMAIN_DISK_CACHE_DEFAULT:
        break;
    default:
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Specified disk cache mode is not "
3291
                         "supported by vz driver."));
3292 3293 3294
        goto cleanup;
    }

3295 3296 3297 3298 3299 3300
    if (bootDisk == true) {
        pret = PrlVmDev_GetIndex(sdkdisk, &devIndex);
        prlsdkCheckRetGoto(pret, cleanup);

        if (prlsdkAddDeviceToBootList(sdkdom, devIndex, devType, 0) < 0)
            goto cleanup;
3301 3302 3303 3304 3305 3306 3307

        /* If we add physical device as a boot disk to container
         * we have to specify mount point for it */
        if (isCt) {
            pret = PrlVmDevHd_SetMountPoint(sdkdisk, "/");
            prlsdkCheckRetGoto(pret, cleanup);
        }
3308 3309
    }

3310 3311 3312
    return 0;
 cleanup:
    PrlHandle_Free(sdkdisk);
3313
    VIR_FREE(dst);
3314 3315 3316
    return ret;
}

3317
int
3318
prlsdkAttachVolume(virDomainObjPtr dom, virDomainDiskDefPtr disk)
3319 3320
{
    int ret = -1;
3321
    vzDomObjPtr privdom = dom->privateData;
3322 3323 3324
    PRL_HANDLE job = PRL_INVALID_HANDLE;

    job = PrlVm_BeginEdit(privdom->sdkdom);
3325
    if (PRL_FAILED(waitJob(job)))
3326 3327
        goto cleanup;

3328
    ret = prlsdkAddDisk(privdom->sdkdom, disk, false, IS_CT(dom->def));
3329 3330
    if (ret == 0) {
        job = PrlVm_CommitEx(privdom->sdkdom, PVCF_DETACH_HDD_BUNDLE);
3331
        if (PRL_FAILED(waitJob(job))) {
3332 3333 3334 3335 3336 3337 3338 3339 3340
            ret = -1;
            goto cleanup;
        }
    }

 cleanup:
    return ret;
}

3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359
static int
prlsdkGetDiskIndex(PRL_HANDLE sdkdom, virDomainDiskDefPtr disk)
{
    int idx = -1;
    char *buf = NULL;
    PRL_UINT32 buflen = 0;
    PRL_RESULT pret;
    PRL_UINT32 hddCount;
    PRL_UINT32 i;
    PRL_HANDLE hdd = PRL_INVALID_HANDLE;

    pret = PrlVmCfg_GetHardDisksCount(sdkdom, &hddCount);
    prlsdkCheckRetGoto(pret, cleanup);

    for (i = 0; i < hddCount; ++i) {

        pret = PrlVmCfg_GetHardDisk(sdkdom, i, &hdd);
        prlsdkCheckRetGoto(pret, cleanup);

3360
        buflen = 0;
3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387
        pret = PrlVmDev_GetFriendlyName(hdd, 0, &buflen);
        prlsdkCheckRetGoto(pret, cleanup);

        if (VIR_ALLOC_N(buf, buflen) < 0)
            goto cleanup;

        pret = PrlVmDev_GetFriendlyName(hdd, buf, &buflen);
        prlsdkCheckRetGoto(pret, cleanup);

        if (STRNEQ(disk->src->path, buf)) {

            PrlHandle_Free(hdd);
            hdd = PRL_INVALID_HANDLE;
            VIR_FREE(buf);
            continue;
        }

        VIR_FREE(buf);
        idx = i;
        break;
    }

 cleanup:
    PrlHandle_Free(hdd);
    return idx;
}

3388 3389
int
prlsdkDetachVolume(virDomainObjPtr dom, virDomainDiskDefPtr disk)
3390 3391
{
    int ret = -1, idx;
3392
    vzDomObjPtr privdom = dom->privateData;
3393 3394 3395 3396 3397 3398 3399
    PRL_HANDLE job = PRL_INVALID_HANDLE;

    idx = prlsdkGetDiskIndex(privdom->sdkdom, disk);
    if (idx < 0)
        goto cleanup;

    job = PrlVm_BeginEdit(privdom->sdkdom);
3400
    if (PRL_FAILED(waitJob(job)))
3401 3402 3403 3404 3405
        goto cleanup;

    ret = prlsdkDelDisk(privdom->sdkdom, idx);
    if (ret == 0) {
        job = PrlVm_CommitEx(privdom->sdkdom, PVCF_DETACH_HDD_BUNDLE);
3406
        if (PRL_FAILED(waitJob(job))) {
3407 3408 3409 3410 3411 3412 3413 3414 3415
            ret = -1;
            goto cleanup;
        }
    }

 cleanup:
    return ret;
}

3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455
static int
prlsdkAddFS(PRL_HANDLE sdkdom, virDomainFSDefPtr fs)
{
    PRL_RESULT pret;
    PRL_HANDLE sdkdisk = PRL_INVALID_HANDLE;
    int ret = -1;

    if (prlsdkCheckFSUnsupportedParams(fs) < 0)
        return -1;

    pret = PrlVmCfg_CreateVmDev(sdkdom, PDE_HARD_DISK, &sdkdisk);
    prlsdkCheckRetGoto(pret, cleanup);

    pret = PrlVmDev_SetEnabled(sdkdisk, 1);
    prlsdkCheckRetGoto(pret, cleanup);

    pret = PrlVmDev_SetConnected(sdkdisk, 1);
    prlsdkCheckRetGoto(pret, cleanup);

    pret = PrlVmDev_SetEmulatedType(sdkdisk, PDT_USE_IMAGE_FILE);
    prlsdkCheckRetGoto(pret, cleanup);

    pret = PrlVmDev_SetSysName(sdkdisk, fs->src);
    prlsdkCheckRetGoto(pret, cleanup);

    pret = PrlVmDev_SetImagePath(sdkdisk, fs->src);
    prlsdkCheckRetGoto(pret, cleanup);

    pret = PrlVmDev_SetFriendlyName(sdkdisk, fs->src);
    prlsdkCheckRetGoto(pret, cleanup);

    pret = PrlVmDevHd_SetMountPoint(sdkdisk, fs->dst);
    prlsdkCheckRetGoto(pret, cleanup);

    ret = 0;

 cleanup:
    PrlHandle_Free(sdkdisk);
    return ret;
}
3456
static int
3457 3458
prlsdkDoApplyConfig(virConnectPtr conn,
                    PRL_HANDLE sdkdom,
3459 3460
                    virDomainDefPtr def,
                    virDomainDefPtr olddef)
3461 3462 3463 3464
{
    PRL_RESULT pret;
    size_t i;
    char uuidstr[VIR_UUID_STRING_BUFLEN + 2];
3465
    bool needBoot = true;
M
Maxim Nestratov 已提交
3466
    char *mask = NULL;
3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487

    if (prlsdkCheckUnsupportedParams(sdkdom, def) < 0)
        return -1;

    if (def->description) {
        pret = PrlVmCfg_SetDescription(sdkdom, def->description);
        prlsdkCheckRetGoto(pret, error);
    }

    if (def->name) {
        pret = PrlVmCfg_SetName(sdkdom, def->name);
        prlsdkCheckRetGoto(pret, error);
    }

    if (def->uuid) {
        prlsdkUUIDFormat(def->uuid, uuidstr);

        pret = PrlVmCfg_SetUuid(sdkdom, uuidstr);
        prlsdkCheckRetGoto(pret, error);
    }

3488
    pret = PrlVmCfg_SetRamSize(sdkdom, virDomainDefGetMemoryActual(def) >> 10);
3489 3490 3491 3492 3493
    prlsdkCheckRetGoto(pret, error);

    pret = PrlVmCfg_SetCpuCount(sdkdom, def->vcpus);
    prlsdkCheckRetGoto(pret, error);

M
Maxim Nestratov 已提交
3494 3495 3496 3497 3498 3499 3500
    if (!(mask = virBitmapFormat(def->cpumask)))
        goto error;

    pret = PrlVmCfg_SetCpuMask(sdkdom, mask);
    prlsdkCheckRetGoto(pret, error);
    VIR_FREE(mask);

3501
    switch (def->os.arch) {
M
Michal Privoznik 已提交
3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512
    case VIR_ARCH_X86_64:
        pret = PrlVmCfg_SetCpuMode(sdkdom, PCM_CPU_MODE_64);
        break;
    case VIR_ARCH_I686:
        pret = PrlVmCfg_SetCpuMode(sdkdom, PCM_CPU_MODE_32);
        break;
    default:
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unknown CPU mode: %s"),
                       virArchToString(def->os.arch));
        goto error;
3513 3514 3515
    }
    prlsdkCheckRetGoto(pret, error);

3516 3517 3518
    if (prlsdkClearDevices(sdkdom) < 0)
        goto error;

3519 3520 3521
    if (prlsdkRemoveBootDevices(sdkdom) < 0)
        goto error;

3522 3523
    if (olddef) {
        for (i = 0; i < olddef->nnets; i++)
3524
            prlsdkCleanupBridgedNet(conn->privateData, olddef->nets[i]);
3525 3526 3527
    }

    for (i = 0; i < def->nnets; i++) {
3528
        if (prlsdkAddNet(sdkdom, conn->privateData, def->nets[i], IS_CT(def)) < 0)
M
Michal Privoznik 已提交
3529
            goto error;
3530 3531
    }

3532 3533 3534 3535 3536 3537 3538
    if (prlsdkApplyGraphicsParams(sdkdom, def) < 0)
        goto error;

    if (prlsdkApplyVideoParams(sdkdom, def) < 0)
        goto error;

    for (i = 0; i < def->nserials; i++) {
M
Mikhail Feoktistov 已提交
3539 3540
        if (prlsdkAddSerial(sdkdom, def->serials[i]) < 0)
            goto error;
3541 3542
    }

3543 3544 3545 3546 3547 3548 3549
    for (i = 0; i < def->nfss; i++) {
        if (STREQ(def->fss[i]->dst, "/"))
            needBoot = false;
        if (prlsdkAddFS(sdkdom, def->fss[i]) < 0)
            goto error;
    }

3550
    for (i = 0; i < def->ndisks; i++) {
3551 3552 3553 3554 3555 3556 3557 3558
        bool bootDisk = false;

        if (needBoot == true &&
            def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_DISK) {

            needBoot = false;
            bootDisk = true;
        }
3559
        if (prlsdkAddDisk(sdkdom, def->disks[i], bootDisk, IS_CT(def)) < 0)
M
Mikhail Feoktistov 已提交
3560
            goto error;
3561 3562 3563 3564 3565
    }

    return 0;

 error:
M
Maxim Nestratov 已提交
3566 3567
    VIR_FREE(mask);

3568
    for (i = 0; i < def->nnets; i++)
3569
        prlsdkCleanupBridgedNet(conn->privateData, def->nets[i]);
3570

M
Michal Privoznik 已提交
3571
    return -1;
3572 3573 3574 3575 3576 3577 3578
}

int
prlsdkApplyConfig(virConnectPtr conn,
                  virDomainObjPtr dom,
                  virDomainDefPtr new)
{
3579
    vzConnPtr privconn = conn->privateData;
3580 3581 3582 3583 3584 3585 3586 3587 3588
    PRL_HANDLE sdkdom = PRL_INVALID_HANDLE;
    PRL_HANDLE job = PRL_INVALID_HANDLE;
    int ret;

    sdkdom = prlsdkSdkDomainLookupByUUID(privconn, dom->def->uuid);
    if (sdkdom == PRL_INVALID_HANDLE)
        return -1;

    job = PrlVm_BeginEdit(sdkdom);
3589
    if (PRL_FAILED(waitJob(job)))
3590 3591
        return -1;

3592
    ret = prlsdkDoApplyConfig(conn, sdkdom, new, dom->def);
3593 3594

    if (ret == 0) {
3595
        job = PrlVm_CommitEx(sdkdom, PVCF_DETACH_HDD_BUNDLE);
3596
        if (PRL_FAILED(waitJob(job)))
3597
            ret = -1;
3598 3599 3600 3601 3602 3603
    }

    PrlHandle_Free(sdkdom);

    return ret;
}
3604 3605 3606 3607

int
prlsdkCreateVm(virConnectPtr conn, virDomainDefPtr def)
{
3608
    vzConnPtr privconn = conn->privateData;
3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619
    PRL_HANDLE sdkdom = PRL_INVALID_HANDLE;
    PRL_HANDLE job = PRL_INVALID_HANDLE;
    PRL_HANDLE result = PRL_INVALID_HANDLE;
    PRL_HANDLE srvconf = PRL_INVALID_HANDLE;
    PRL_RESULT pret;
    int ret = -1;

    pret = PrlSrv_CreateVm(privconn->server, &sdkdom);
    prlsdkCheckRetGoto(pret, cleanup);

    job = PrlSrv_GetSrvConfig(privconn->server);
3620
    if (PRL_FAILED(getJobResult(job, &result)))
3621 3622 3623 3624 3625 3626 3627 3628
        goto cleanup;

    pret = PrlResult_GetParamByIndex(result, 0, &srvconf);
    prlsdkCheckRetGoto(pret, cleanup);

    pret = PrlVmCfg_SetDefaultConfig(sdkdom, srvconf, PVS_GUEST_VER_LIN_REDHAT, 0);
    prlsdkCheckRetGoto(pret, cleanup);

3629 3630 3631
    pret = PrlVmCfg_SetOfflineManagementEnabled(sdkdom, 0);
    prlsdkCheckRetGoto(pret, cleanup);

3632
    ret = prlsdkDoApplyConfig(conn, sdkdom, def, NULL);
3633 3634 3635 3636
    if (ret)
        goto cleanup;

    job = PrlVm_Reg(sdkdom, "", 1);
3637
    if (PRL_FAILED(waitJob(job)))
3638
        ret = -1;
3639 3640 3641 3642 3643 3644 3645 3646 3647

 cleanup:
    PrlHandle_Free(sdkdom);
    return ret;
}

int
prlsdkCreateCt(virConnectPtr conn, virDomainDefPtr def)
{
3648
    vzConnPtr privconn = conn->privateData;
3649 3650 3651 3652 3653 3654
    PRL_HANDLE sdkdom = PRL_INVALID_HANDLE;
    PRL_GET_VM_CONFIG_PARAM_DATA confParam;
    PRL_HANDLE job = PRL_INVALID_HANDLE;
    PRL_HANDLE result = PRL_INVALID_HANDLE;
    PRL_RESULT pret;
    int ret = -1;
3655 3656
    int useTemplate = 0;
    size_t i;
3657

3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674
    if (def->nfss > 1) {
        /* Check all filesystems */
        for (i = 0; i < def->nfss; i++) {
            if (def->fss[i]->type != VIR_DOMAIN_FS_TYPE_FILE) {
                virReportError(VIR_ERR_INVALID_ARG, "%s",
                               _("Unsupported filesystem type."));
                return -1;
            }
        }
    } else if (def->nfss == 1) {
        if (def->fss[0]->type == VIR_DOMAIN_FS_TYPE_TEMPLATE) {
            useTemplate = 1;
        } else if (def->fss[0]->type != VIR_DOMAIN_FS_TYPE_FILE) {
            virReportError(VIR_ERR_INVALID_ARG, "%s",
                           _("Unsupported filesystem type."));
            return -1;
        }
3675 3676 3677 3678 3679 3680 3681
    }

    confParam.nVmType = PVT_CT;
    confParam.sConfigSample = "vswap.1024MB";
    confParam.nOsVersion = 0;

    job = PrlSrv_GetDefaultVmConfig(privconn->server, &confParam, 0);
3682
    if (PRL_FAILED(getJobResult(job, &result)))
3683 3684 3685 3686 3687
        goto cleanup;

    pret = PrlResult_GetParamByIndex(result, 0, &sdkdom);
    prlsdkCheckRetGoto(pret, cleanup);

3688
    if (useTemplate) {
3689 3690
        pret = PrlVmCfg_SetOsTemplate(sdkdom, def->fss[0]->src);
        prlsdkCheckRetGoto(pret, cleanup);
3691

3692 3693
    }

3694
    ret = prlsdkDoApplyConfig(conn, sdkdom, def, NULL);
3695 3696 3697
    if (ret)
        goto cleanup;

3698 3699
    job = PrlVm_RegEx(sdkdom, "",
                      PACF_NON_INTERACTIVE_MODE | PRNVM_PRESERVE_DISK);
3700
    if (PRL_FAILED(waitJob(job)))
3701
        ret = -1;
3702 3703 3704

 cleanup:
    PrlHandle_Free(sdkdom);
3705
    return ret;
3706
}
3707 3708

int
3709
prlsdkUnregisterDomain(vzConnPtr privconn, virDomainObjPtr dom)
3710
{
3711
    vzDomObjPtr privdom = dom->privateData;
3712
    PRL_HANDLE job;
3713 3714 3715
    size_t i;

    for (i = 0; i < dom->def->nnets; i++)
3716
        prlsdkCleanupBridgedNet(privconn, dom->def->nets[i]);
3717 3718

    job = PrlVm_Unreg(privdom->sdkdom);
3719
    if (PRL_FAILED(waitJob(job)))
3720 3721 3722 3723 3724 3725 3726 3727 3728
        return -1;

    if (prlsdkSendEvent(privconn, dom, VIR_DOMAIN_EVENT_UNDEFINED,
                        VIR_DOMAIN_EVENT_UNDEFINED_REMOVED) < 0)
        return -1;

    virDomainObjListRemove(privconn->domains, dom);
    return 0;
}
3729 3730

int
3731
prlsdkDomainManagedSaveRemove(virDomainObjPtr dom)
3732
{
3733
    vzDomObjPtr privdom = dom->privateData;
3734 3735 3736
    PRL_HANDLE job;

    job = PrlVm_DropSuspendedState(privdom->sdkdom);
3737
    if (PRL_FAILED(waitJob(job)))
3738 3739 3740 3741
        return -1;

    return 0;
}
3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772

static int
prlsdkExtractStatsParam(PRL_HANDLE sdkstats, const char *name, long long *val)
{
    PRL_HANDLE param = PRL_INVALID_HANDLE;
    PRL_RESULT pret;
    PRL_INT64 pval = 0;
    int ret = -1;

    pret = PrlEvent_GetParamByName(sdkstats, name, &param);
    if (pret == PRL_ERR_NO_DATA) {
        *val = -1;
        ret = 0;
        goto cleanup;
    } else if (PRL_FAILED(pret)) {
        logPrlError(pret);
        goto cleanup;
    }
    pret = PrlEvtPrm_ToInt64(param, &pval);
    prlsdkCheckRetGoto(pret, cleanup);

    *val = pval;
    ret = 0;

 cleanup:
    PrlHandle_Free(param);
    return ret;
}

#define PARALLELS_STATISTICS_TIMEOUT (60 * 1000)

P
Peter Krempa 已提交
3773
static int
3774 3775
prlsdkGetStatsParam(virDomainObjPtr dom, const char *name, long long *val)
{
3776
    vzDomObjPtr privdom = dom->privateData;
3777 3778 3779 3780
    PRL_HANDLE job = PRL_INVALID_HANDLE;
    unsigned long long now;

    if (privdom->cache.stats != PRL_INVALID_HANDLE) {
M
Michal Privoznik 已提交
3781
        /* reset count to keep subscribtion */
3782 3783 3784 3785 3786 3787 3788 3789 3790 3791
        privdom->cache.count = 0;
        return prlsdkExtractStatsParam(privdom->cache.stats, name, val);
    }

    if (privdom->cache.count == -1) {
        job = PrlVm_SubscribeToPerfStats(privdom->sdkdom, NULL);
        if (PRL_FAILED(waitJob(job)))
            goto error;
    }

M
Michal Privoznik 已提交
3792 3793
    /* change state to subscribed in case of unsubscribed
       or reset count so we stop unsubscribe attempts */
3794 3795 3796 3797 3798 3799 3800 3801 3802
    privdom->cache.count = 0;

    if (virTimeMillisNow(&now) < 0) {
        virReportSystemError(errno, "%s", _("Unable to get current time"));
        goto error;
    }

    while (privdom->cache.stats == PRL_INVALID_HANDLE) {
        if (virCondWaitUntil(&privdom->cache.cond, &dom->parent.lock,
M
Michal Privoznik 已提交
3803
                             now + PARALLELS_STATISTICS_TIMEOUT) < 0) {
3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845
            if (errno == ETIMEDOUT) {
                virReportError(VIR_ERR_OPERATION_TIMEOUT, "%s",
                               _("Timeout on waiting statistics event."));
                goto error;
            } else {
                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                               _("Unable to wait on monitor condition"));
                goto error;
            }
        }
    }

    return prlsdkExtractStatsParam(privdom->cache.stats, name, val);
 error:
    return -1;
}

int
prlsdkGetBlockStats(virDomainObjPtr dom, virDomainDiskDefPtr disk, virDomainBlockStatsPtr stats)
{
    virDomainDeviceDriveAddressPtr address;
    int idx;
    const char *prefix;
    int ret = -1;
    char *name = NULL;

    address = &disk->info.addr.drive;
    switch (disk->bus) {
    case VIR_DOMAIN_DISK_BUS_IDE:
        prefix = "ide";
        idx = address->bus * 2 + address->unit;
        break;
    case VIR_DOMAIN_DISK_BUS_SATA:
        prefix = "sata";
        idx = address->unit;
        break;
    case VIR_DOMAIN_DISK_BUS_SCSI:
        prefix = "scsi";
        idx = address->unit;
        break;
    default:
        virReportError(VIR_ERR_INTERNAL_ERROR,
M
Michal Privoznik 已提交
3846
                       _("Unknown disk bus: %X"), disk->bus);
3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868
        goto cleanup;
    }


#define PRLSDK_GET_STAT_PARAM(VAL, TYPE, NAME)                          \
    if (virAsprintf(&name, "devices.%s%d.%s", prefix, idx, NAME) < 0)   \
        goto cleanup;                                                   \
    if (prlsdkGetStatsParam(dom, name, &stats->VAL) < 0)                \
        goto cleanup;                                                   \
    VIR_FREE(name);

    PARALLELS_BLOCK_STATS_FOREACH(PRLSDK_GET_STAT_PARAM)

#undef PRLSDK_GET_STAT_PARAM

    ret = 0;

 cleanup:

    VIR_FREE(name);
    return ret;
}
3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957

static PRL_HANDLE
prlsdkFindNetByPath(virDomainObjPtr dom, const char *path)
{
    PRL_UINT32 count = 0;
    vzDomObjPtr privdom = dom->privateData;
    PRL_UINT32 buflen = 0;
    PRL_RESULT pret;
    size_t i;
    char *name = NULL;
    PRL_HANDLE net = PRL_INVALID_HANDLE;

    pret = PrlVmCfg_GetNetAdaptersCount(privdom->sdkdom, &count);
    prlsdkCheckRetGoto(pret, error);

    for (i = 0; i < count; ++i) {
        pret = PrlVmCfg_GetNetAdapter(privdom->sdkdom, i, &net);
        prlsdkCheckRetGoto(pret, error);

        pret = PrlVmDevNet_GetHostInterfaceName(net, NULL, &buflen);
        prlsdkCheckRetGoto(pret, error);

        if (VIR_ALLOC_N(name, buflen) < 0)
            goto error;

        pret = PrlVmDevNet_GetHostInterfaceName(net, name, &buflen);
        prlsdkCheckRetGoto(pret, error);

        if (STREQ(name, path))
            break;

        VIR_FREE(name);
        PrlHandle_Free(net);
        net = PRL_INVALID_HANDLE;
    }

    if (net == PRL_INVALID_HANDLE)
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid path, '%s' is not a known interface"), path);
    return net;

 error:
    VIR_FREE(name);
    PrlHandle_Free(net);
    return PRL_INVALID_HANDLE;
}

int
prlsdkGetNetStats(virDomainObjPtr dom, const char *path,
                  virDomainInterfaceStatsPtr stats)
{
    int ret = -1;
    PRL_UINT32 net_index = -1;
    char *name = NULL;
    PRL_RESULT pret;
    PRL_HANDLE net = PRL_INVALID_HANDLE;

    net = prlsdkFindNetByPath(dom, path);
    if (net == PRL_INVALID_HANDLE)
       goto cleanup;

    pret = PrlVmDev_GetIndex(net, &net_index);
    prlsdkCheckRetGoto(pret, cleanup);

#define PRLSDK_GET_NET_COUNTER(VAL, NAME)                           \
    if (virAsprintf(&name, "net.nic%d.%s", net_index, NAME) < 0)    \
        goto cleanup;                                               \
    if (prlsdkGetStatsParam(dom, name, &stats->VAL) < 0)            \
        goto cleanup;                                               \
    VIR_FREE(name);

    PRLSDK_GET_NET_COUNTER(rx_bytes, "bytes_in")
    PRLSDK_GET_NET_COUNTER(rx_packets, "pkts_in")
    PRLSDK_GET_NET_COUNTER(tx_bytes, "bytes_out")
    PRLSDK_GET_NET_COUNTER(tx_packets, "pkts_out")
    stats->rx_errs = -1;
    stats->rx_drop = -1;
    stats->tx_errs = -1;
    stats->tx_drop = -1;

#undef PRLSDK_GET_NET_COUNTER
    ret = 0;

 cleanup:
    VIR_FREE(name);
    PrlHandle_Free(net);

    return ret;
}
N
Nikolay Shirokovskiy 已提交
3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976

int
prlsdkGetVcpuStats(virDomainObjPtr dom, int idx, unsigned long long *vtime)
{
    char *name = NULL;
    long long ptime = 0;
    int ret = -1;

    if (virAsprintf(&name, "guest.vcpu%u.time", (unsigned int)idx) < 0)
        goto cleanup;
    if (prlsdkGetStatsParam(dom, name, &ptime) < 0)
        goto cleanup;
    *vtime = ptime == -1 ? 0 : ptime;
    ret = 0;

 cleanup:
    VIR_FREE(name);
    return ret;
}
N
Nikolay Shirokovskiy 已提交
3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037

int
prlsdkGetMemoryStats(virDomainObjPtr dom,
                     virDomainMemoryStatPtr stats,
                     unsigned int nr_stats)
{
    int ret = -1;
    long long v = 0, t = 0, u = 0;
    size_t i = 0;

#define PRLSDK_GET_COUNTER(NAME, VALUE)                             \
    if (prlsdkGetStatsParam(dom, NAME, &VALUE) < 0)                 \
        goto cleanup;                                               \

#define PRLSDK_MEMORY_STAT_SET(TAG, VALUE)                          \
    if (i < nr_stats) {                                             \
        stats[i].tag = (TAG);                                       \
        stats[i].val = (VALUE);                                     \
        i++;                                                        \
    }

    i = 0;

    // count to kb
    PRLSDK_GET_COUNTER("guest.ram.swap_in", v)
    if (v != -1)
        PRLSDK_MEMORY_STAT_SET(VIR_DOMAIN_MEMORY_STAT_SWAP_IN, v << 12)

    PRLSDK_GET_COUNTER("guest.ram.swap_out", v)
    if (v != -1)
        PRLSDK_MEMORY_STAT_SET(VIR_DOMAIN_MEMORY_STAT_SWAP_OUT, v << 12)

    PRLSDK_GET_COUNTER("guest.ram.minor_fault", v)
    if (v != -1)
        PRLSDK_MEMORY_STAT_SET(VIR_DOMAIN_MEMORY_STAT_MINOR_FAULT, v)

    PRLSDK_GET_COUNTER("guest.ram.major_fault", v)
    if (v != -1)
        PRLSDK_MEMORY_STAT_SET(VIR_DOMAIN_MEMORY_STAT_MAJOR_FAULT, v)

    PRLSDK_GET_COUNTER("guest.ram.total", v)
    if (v != -1)
        PRLSDK_MEMORY_STAT_SET(VIR_DOMAIN_MEMORY_STAT_AVAILABLE, v << 10)

    PRLSDK_GET_COUNTER("guest.ram.balloon_actual", v)
    if (v != -1)
        PRLSDK_MEMORY_STAT_SET(VIR_DOMAIN_MEMORY_STAT_ACTUAL_BALLOON, v << 10)

    PRLSDK_GET_COUNTER("guest.ram.usage", u)
    PRLSDK_GET_COUNTER("guest.ram.total", t)
    if (u != -1 && t != -1)
        PRLSDK_MEMORY_STAT_SET(VIR_DOMAIN_MEMORY_STAT_UNUSED, (t - u) << 10)

#undef PRLSDK_GET_COUNTER
#undef PRLSDK_MEMORY_STAT_SET

    ret = i;
 cleanup:

    return ret;
}