qemu_cgroup.c 34.0 KB
Newer Older
1 2 3
/*
 * qemu_cgroup.c: QEMU cgroup management
 *
E
Eric Blake 已提交
4
 * Copyright (C) 2006-2013 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17
 * Copyright (C) 2006 Daniel P. Berrange
 *
 * 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
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
20 21 22 23 24 25 26
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

#include <config.h>

#include "qemu_cgroup.h"
27
#include "qemu_domain.h"
28
#include "qemu_process.h"
29
#include "vircgroup.h"
30
#include "virlog.h"
31
#include "viralloc.h"
32
#include "virerror.h"
33
#include "domain_audit.h"
34
#include "virscsi.h"
35
#include "virstring.h"
36 37 38 39 40 41 42

#define VIR_FROM_THIS VIR_FROM_QEMU

static const char *const defaultDeviceACL[] = {
    "/dev/null", "/dev/full", "/dev/zero",
    "/dev/random", "/dev/urandom",
    "/dev/ptmx", "/dev/kvm", "/dev/kqemu",
43
    "/dev/rtc", "/dev/hpet", "/dev/vfio/vfio",
44 45 46 47 48
    NULL,
};
#define DEVICE_PTY_MAJOR 136
#define DEVICE_SND_MAJOR 116

49
static int
50
qemuSetupDiskPathAllow(virDomainDiskDefPtr disk,
51 52 53
                       const char *path,
                       size_t depth ATTRIBUTE_UNUSED,
                       void *opaque)
54
{
55 56
    virDomainObjPtr vm = opaque;
    qemuDomainObjPrivatePtr priv = vm->privateData;
57 58 59
    int rc;

    VIR_DEBUG("Process path %s for disk", path);
60
    rc = virCgroupAllowDevicePath(priv->cgroup, path,
61 62
                                  (disk->readonly ? VIR_CGROUP_DEVICE_READ
                                   : VIR_CGROUP_DEVICE_RW));
63
    virDomainAuditCgroupPath(vm, priv->cgroup, "allow", path,
64
                             disk->readonly ? "r" : "rw", rc);
65 66
    if (rc < 0) {
        if (rc == -EACCES) { /* Get this for root squash NFS */
67 68 69 70 71 72 73 74 75 76 77 78
            VIR_DEBUG("Ignoring EACCES for %s", path);
        } else {
            virReportSystemError(-rc,
                                 _("Unable to allow access for disk path %s"),
                                 path);
            return -1;
        }
    }
    return 0;
}


79
int qemuSetupDiskCgroup(virDomainObjPtr vm,
80 81
                        virDomainDiskDefPtr disk)
{
82 83 84 85 86 87
    qemuDomainObjPrivatePtr priv = vm->privateData;

    if (!virCgroupHasController(priv->cgroup,
                                VIR_CGROUP_CONTROLLER_DEVICES))
        return 0;

88 89 90
    return virDomainDiskDefForeachPath(disk,
                                       true,
                                       qemuSetupDiskPathAllow,
91
                                       vm);
92 93 94
}


95 96 97 98 99
static int
qemuTeardownDiskPathDeny(virDomainDiskDefPtr disk ATTRIBUTE_UNUSED,
                         const char *path,
                         size_t depth ATTRIBUTE_UNUSED,
                         void *opaque)
100
{
101 102
    virDomainObjPtr vm = opaque;
    qemuDomainObjPrivatePtr priv = vm->privateData;
103 104 105
    int rc;

    VIR_DEBUG("Process path %s for disk", path);
106
    rc = virCgroupDenyDevicePath(priv->cgroup, path,
107
                                 VIR_CGROUP_DEVICE_RWM);
108
    virDomainAuditCgroupPath(vm, priv->cgroup, "deny", path, "rwm", rc);
109 110
    if (rc < 0) {
        if (rc == -EACCES) { /* Get this for root squash NFS */
111 112 113 114 115 116 117 118 119 120 121 122
            VIR_DEBUG("Ignoring EACCES for %s", path);
        } else {
            virReportSystemError(-rc,
                                 _("Unable to deny access for disk path %s"),
                                 path);
            return -1;
        }
    }
    return 0;
}


123
int qemuTeardownDiskCgroup(virDomainObjPtr vm,
124 125
                           virDomainDiskDefPtr disk)
{
126 127 128 129 130 131
    qemuDomainObjPrivatePtr priv = vm->privateData;

    if (!virCgroupHasController(priv->cgroup,
                                VIR_CGROUP_CONTROLLER_DEVICES))
        return 0;

132 133 134
    return virDomainDiskDefForeachPath(disk,
                                       true,
                                       qemuTeardownDiskPathDeny,
135
                                       vm);
136 137
}

138
static int
139 140
qemuSetupChrSourceCgroup(virDomainDefPtr def,
                         virDomainChrSourceDefPtr dev,
141
                         void *opaque)
142
{
143 144
    virDomainObjPtr vm = opaque;
    qemuDomainObjPrivatePtr priv = vm->privateData;
145 146
    int rc;

147
    if (dev->type != VIR_DOMAIN_CHR_TYPE_DEV)
148 149
        return 0;

150
    VIR_DEBUG("Process path '%s' for device", dev->data.file.path);
151

152
    rc = virCgroupAllowDevicePath(priv->cgroup, dev->data.file.path,
153
                                  VIR_CGROUP_DEVICE_RW);
154
    virDomainAuditCgroupPath(vm, priv->cgroup, "allow",
155
                             dev->data.file.path, "rw", rc);
156
    if (rc < 0) {
157 158
        virReportSystemError(-rc,
                             _("Unable to allow device %s for %s"),
159
                             dev->data.file.path, def->name);
160 161 162 163 164 165
        return -1;
    }

    return 0;
}

166 167 168 169 170
static int
qemuSetupChardevCgroup(virDomainDefPtr def,
                       virDomainChrDefPtr dev,
                       void *opaque)
{
171
    return qemuSetupChrSourceCgroup(def, &dev->source, opaque);
172 173 174 175 176 177
}


static int
qemuSetupTPMCgroup(virDomainDefPtr def,
                   virDomainTPMDefPtr dev,
178
                   void *opaque)
179 180 181 182 183 184
{
    int rc = 0;

    switch (dev->type) {
    case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH:
        rc = qemuSetupChrSourceCgroup(def, &dev->data.passthrough.source,
185
                                      opaque);
186 187 188 189 190 191 192 193
        break;
    case VIR_DOMAIN_TPM_TYPE_LAST:
        break;
    }

    return rc;
}

194

195 196 197 198
static int
qemuSetupHostUsbDeviceCgroup(virUSBDevicePtr dev ATTRIBUTE_UNUSED,
                             const char *path,
                             void *opaque)
199
{
200 201
    virDomainObjPtr vm = opaque;
    qemuDomainObjPrivatePtr priv = vm->privateData;
202 203 204
    int rc;

    VIR_DEBUG("Process path '%s' for USB device", path);
205
    rc = virCgroupAllowDevicePath(priv->cgroup, path,
206
                                  VIR_CGROUP_DEVICE_RW);
207
    virDomainAuditCgroupPath(vm, priv->cgroup, "allow", path, "rw", rc);
208
    if (rc < 0) {
209 210 211 212 213 214 215 216 217
        virReportSystemError(-rc,
                             _("Unable to allow device %s"),
                             path);
        return -1;
    }

    return 0;
}

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
static int
qemuSetupHostScsiDeviceCgroup(virSCSIDevicePtr dev ATTRIBUTE_UNUSED,
                              const char *path,
                              void *opaque)
{
    virDomainObjPtr vm = opaque;
    qemuDomainObjPrivatePtr priv = vm->privateData;
    int rc;

    VIR_DEBUG("Process path '%s' for SCSI device", path);

    rc = virCgroupAllowDevicePath(priv->cgroup, path,
                                  virSCSIDeviceGetReadonly(dev) ?
                                  VIR_CGROUP_DEVICE_READ :
                                  VIR_CGROUP_DEVICE_RW);

    virDomainAuditCgroupPath(vm, priv->cgroup, "allow", path,
                             virSCSIDeviceGetReadonly(dev) ? "r" : "rw", rc);
    if (rc < 0) {
        virReportSystemError(-rc,
                             _("Unable to allow device %s"),
                             path);
        return -1;
    }

    return 0;
}
245

246 247 248 249 250 251 252
int
qemuSetupHostdevCGroup(virDomainObjPtr vm,
                       virDomainHostdevDefPtr dev)
{
    int ret = -1;
    qemuDomainObjPrivatePtr priv = vm->privateData;
    virPCIDevicePtr pci = NULL;
253
    virUSBDevicePtr usb = NULL;
254
    virSCSIDevicePtr scsi = NULL;
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
    char *path = NULL;

    /* currently this only does something for PCI devices using vfio
     * for device assignment, but it is called for *all* hostdev
     * devices.
     */

    if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_DEVICES))
        return 0;

    if (dev->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS) {

        switch (dev->source.subsys.type) {
        case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI:
            if (dev->source.subsys.u.pci.backend
270
                == VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) {
271 272 273 274 275 276 277 278 279
                int rc;

                pci = virPCIDeviceNew(dev->source.subsys.u.pci.addr.domain,
                                      dev->source.subsys.u.pci.addr.bus,
                                      dev->source.subsys.u.pci.addr.slot,
                                      dev->source.subsys.u.pci.addr.function);
                if (!pci)
                    goto cleanup;

280
                if (!(path = virPCIDeviceGetIOMMUGroupDev(pci)))
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
                    goto cleanup;

                VIR_DEBUG("Cgroup allow %s for PCI device assignment", path);
                rc = virCgroupAllowDevicePath(priv->cgroup, path,
                                              VIR_CGROUP_DEVICE_RW);
                virDomainAuditCgroupPath(vm, priv->cgroup,
                                         "allow", path, "rw", rc);
                if (rc < 0) {
                    virReportSystemError(-rc,
                                         _("Unable to allow access "
                                           "for device path %s"),
                                         path);
                    goto cleanup;
                }
            }
            break;
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318

        case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB:
            /* NB: hostdev->missing wasn't previously checked in the
             * case of hotplug, only when starting a domain. Now it is
             * always checked, and the cgroup setup skipped if true.
             */
            if (dev->missing)
                break;
            if ((usb = virUSBDeviceNew(dev->source.subsys.u.usb.bus,
                                       dev->source.subsys.u.usb.device,
                                       NULL)) == NULL) {
                goto cleanup;
            }

            /* oddly, qemuSetupHostUsbDeviceCgroup doesn't ever
             * reference the usb object we just created
             */
            if (virUSBDeviceFileIterate(usb, qemuSetupHostUsbDeviceCgroup,
                                        vm) < 0) {
                goto cleanup;
            }
            break;
319 320 321 322 323 324 325 326 327 328 329 330 331 332

        case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI:
            if ((scsi = virSCSIDeviceNew(dev->source.subsys.u.scsi.adapter,
                                         dev->source.subsys.u.scsi.bus,
                                         dev->source.subsys.u.scsi.target,
                                         dev->source.subsys.u.scsi.unit,
                                         dev->readonly)) == NULL)
                goto cleanup;

            if (virSCSIDeviceFileIterate(scsi,
                                         qemuSetupHostScsiDeviceCgroup,
                                         vm) < 0)
                goto cleanup;

333 334 335 336 337 338 339 340
        default:
            break;
        }
    }

    ret = 0;
cleanup:
    virPCIDeviceFree(pci);
341
    virUSBDeviceFree(usb);
342
    virSCSIDeviceFree(scsi);
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
    VIR_FREE(path);
    return ret;
}

int
qemuTeardownHostdevCgroup(virDomainObjPtr vm,
                       virDomainHostdevDefPtr dev)
{
    int ret = -1;
    qemuDomainObjPrivatePtr priv = vm->privateData;
    virPCIDevicePtr pci = NULL;
    char *path = NULL;

    /* currently this only does something for PCI devices using vfio
     * for device assignment, but it is called for *all* hostdev
     * devices.
     */

    if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_DEVICES))
        return 0;

    if (dev->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS) {

        switch (dev->source.subsys.type) {
        case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI:
            if (dev->source.subsys.u.pci.backend
369
                == VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) {
370 371 372 373 374 375 376 377 378
                int rc;

                pci = virPCIDeviceNew(dev->source.subsys.u.pci.addr.domain,
                                      dev->source.subsys.u.pci.addr.bus,
                                      dev->source.subsys.u.pci.addr.slot,
                                      dev->source.subsys.u.pci.addr.function);
                if (!pci)
                    goto cleanup;

379
                if (!(path = virPCIDeviceGetIOMMUGroupDev(pci)))
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
                    goto cleanup;

                VIR_DEBUG("Cgroup deny %s for PCI device assignment", path);
                rc = virCgroupDenyDevicePath(priv->cgroup, path,
                                             VIR_CGROUP_DEVICE_RWM);
                virDomainAuditCgroupPath(vm, priv->cgroup,
                                         "deny", path, "rwm", rc);
                if (rc < 0) {
                    virReportSystemError(-rc,
                                         _("Unable to deny access "
                                           "for device path %s"),
                                         path);
                    goto cleanup;
                }
            }
            break;
396 397 398
        case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB:
            /* nothing to tear down for USB */
            break;
399 400 401 402 403 404 405 406 407 408 409 410
        default:
            break;
        }
    }

    ret = 0;
cleanup:
    virPCIDeviceFree(pci);
    VIR_FREE(path);
    return ret;
}

411 412 413 414 415
static int
qemuSetupBlkioCgroup(virDomainObjPtr vm)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    int rc = -1;
416
    size_t i;
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 458

    if (!virCgroupHasController(priv->cgroup,
                                VIR_CGROUP_CONTROLLER_BLKIO)) {
        if (vm->def->blkio.weight || vm->def->blkio.ndevices) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("Block I/O tuning is not available on this host"));
            return -1;
        } else {
            return 0;
        }
    }

    if (vm->def->blkio.weight != 0) {
        rc = virCgroupSetBlkioWeight(priv->cgroup, vm->def->blkio.weight);
        if (rc != 0) {
            virReportSystemError(-rc,
                                 _("Unable to set io weight for domain %s"),
                                 vm->def->name);
            return -1;
        }
    }

    if (vm->def->blkio.ndevices) {
        for (i = 0; i < vm->def->blkio.ndevices; i++) {
            virBlkioDeviceWeightPtr dw = &vm->def->blkio.devices[i];
            if (!dw->weight)
                continue;
            rc = virCgroupSetBlkioDeviceWeight(priv->cgroup, dw->path,
                                               dw->weight);
            if (rc != 0) {
                virReportSystemError(-rc,
                                     _("Unable to set io device weight "
                                       "for domain %s"),
                                     vm->def->name);
                return -1;
            }
        }
    }

    return 0;
}

459

460 461 462 463 464 465 466 467 468 469 470 471 472
static int
qemuSetupMemoryCgroup(virDomainObjPtr vm)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    int rc;

    if (!virCgroupHasController(priv->cgroup,VIR_CGROUP_CONTROLLER_MEMORY)) {
        if (vm->def->mem.hard_limit != 0 ||
            vm->def->mem.soft_limit != 0 ||
            vm->def->mem.swap_hard_limit != 0) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("Memory cgroup is not available on this host"));
            return -1;
O
Osier Yang 已提交
473 474
        } else {
            return 0;
475 476 477
        }
    }

478 479
    rc = virCgroupSetMemoryHardLimit(priv->cgroup,
                                     qemuDomainMemoryLimit(vm->def));
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
    if (rc != 0) {
        virReportSystemError(-rc,
                             _("Unable to set memory hard limit for domain %s"),
                             vm->def->name);
        return -1;
    }
    if (vm->def->mem.soft_limit != 0) {
        rc = virCgroupSetMemorySoftLimit(priv->cgroup, vm->def->mem.soft_limit);
        if (rc != 0) {
            virReportSystemError(-rc,
                                 _("Unable to set memory soft limit for domain %s"),
                                 vm->def->name);
            return -1;
        }
    }

    if (vm->def->mem.swap_hard_limit != 0) {
        rc = virCgroupSetMemSwapHardLimit(priv->cgroup, vm->def->mem.swap_hard_limit);
        if (rc != 0) {
            virReportSystemError(-rc,
                                 _("Unable to set swap hard limit for domain %s"),
                                 vm->def->name);
            return -1;
        }
    }

    return 0;
}


510 511 512 513 514 515 516 517 518
static int
qemuSetupDevicesCgroup(virQEMUDriverPtr driver,
                       virDomainObjPtr vm)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    virQEMUDriverConfigPtr cfg = NULL;
    const char *const *deviceACL = NULL;
    int rc = -1;
    int ret = -1;
519
    size_t i;
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536

    if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_DEVICES))
        return 0;

    rc = virCgroupDenyAllDevices(priv->cgroup);
    virDomainAuditCgroup(vm, priv->cgroup, "deny", "all", rc == 0);
    if (rc != 0) {
        if (rc == -EPERM) {
            VIR_WARN("Group devices ACL is not accessible, disabling whitelisting");
            return 0;
        }

        virReportSystemError(-rc,
                             _("Unable to deny all devices for %s"), vm->def->name);
        goto cleanup;
    }

537
    for (i = 0; i < vm->def->ndisks; i++) {
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
        if (qemuSetupDiskCgroup(vm, vm->def->disks[i]) < 0)
            goto cleanup;
    }

    rc = virCgroupAllowDeviceMajor(priv->cgroup, 'c', DEVICE_PTY_MAJOR,
                                   VIR_CGROUP_DEVICE_RW);
    virDomainAuditCgroupMajor(vm, priv->cgroup, "allow", DEVICE_PTY_MAJOR,
                              "pty", "rw", rc == 0);
    if (rc != 0) {
        virReportSystemError(-rc, "%s",
                             _("unable to allow /dev/pts/ devices"));
        goto cleanup;
    }

    cfg = virQEMUDriverGetConfig(driver);
    deviceACL = cfg->cgroupDeviceACL ?
                (const char *const *)cfg->cgroupDeviceACL :
                defaultDeviceACL;

    if (vm->def->nsounds &&
        (!vm->def->ngraphics ||
         ((vm->def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_VNC &&
           cfg->vncAllowHostAudio) ||
           (vm->def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_SDL)))) {
        rc = virCgroupAllowDeviceMajor(priv->cgroup, 'c', DEVICE_SND_MAJOR,
                                       VIR_CGROUP_DEVICE_RW);
        virDomainAuditCgroupMajor(vm, priv->cgroup, "allow", DEVICE_SND_MAJOR,
                                  "sound", "rw", rc == 0);
        if (rc != 0) {
            virReportSystemError(-rc, "%s",
                                     _("unable to allow /dev/snd/ devices"));
            goto cleanup;
        }
    }

573
    for (i = 0; deviceACL[i] != NULL; i++) {
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 605 606 607 608 609 610 611 612 613 614 615
        if (access(deviceACL[i], F_OK) < 0) {
            VIR_DEBUG("Ignoring non-existant device %s",
                      deviceACL[i]);
            continue;
        }

        rc = virCgroupAllowDevicePath(priv->cgroup, deviceACL[i],
                                      VIR_CGROUP_DEVICE_RW);
        virDomainAuditCgroupPath(vm, priv->cgroup, "allow", deviceACL[i], "rw", rc);
        if (rc < 0 &&
            rc != -ENOENT) {
            virReportSystemError(-rc,
                                 _("unable to allow device %s"),
                                 deviceACL[i]);
            goto cleanup;
        }
    }

    if (virDomainChrDefForeach(vm->def,
                               true,
                               qemuSetupChardevCgroup,
                               vm) < 0)
        goto cleanup;

    if (vm->def->tpm &&
        (qemuSetupTPMCgroup(vm->def,
                            vm->def->tpm,
                            vm) < 0))
        goto cleanup;

    for (i = 0; i < vm->def->nhostdevs; i++) {
        if (qemuSetupHostdevCGroup(vm, vm->def->hostdevs[i]) < 0)
            goto cleanup;
    }

    ret = 0;
cleanup:
    virObjectUnref(cfg);
    return ret;
}


616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
static int
qemuSetupCpusetCgroup(virDomainObjPtr vm,
                      virBitmapPtr nodemask)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    char *mask = NULL;
    int rc;
    int ret = -1;

    if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_CPUSET))
        return 0;

    if ((vm->def->numatune.memory.nodemask ||
         (vm->def->numatune.memory.placement_mode ==
          VIR_NUMA_TUNE_MEM_PLACEMENT_MODE_AUTO)) &&
        vm->def->numatune.memory.mode == VIR_DOMAIN_NUMATUNE_MEM_STRICT) {

        if (vm->def->numatune.memory.placement_mode ==
            VIR_NUMA_TUNE_MEM_PLACEMENT_MODE_AUTO)
            mask = virBitmapFormat(nodemask);
        else
            mask = virBitmapFormat(vm->def->numatune.memory.nodemask);

        if (!mask) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("failed to convert memory nodemask"));
            goto cleanup;
        }

        rc = virCgroupSetCpusetMems(priv->cgroup, mask);

        if (rc != 0) {
            virReportSystemError(-rc,
                                 _("Unable to set cpuset.mems for domain %s"),
                                 vm->def->name);
            goto cleanup;
        }
    }

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


662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
static int
qemuSetupCpuCgroup(virDomainObjPtr vm)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    int rc = -1;

    if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_CPU)) {
       if (vm->def->cputune.shares) {
           virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                          _("CPU tuning is not available on this host"));
           return -1;
       } else {
           return 0;
       }
    }

    if (vm->def->cputune.shares) {
        rc = virCgroupSetCpuShares(priv->cgroup, vm->def->cputune.shares);
        if (rc != 0) {
            virReportSystemError(-rc,
                                 _("Unable to set io cpu shares for domain %s"),
                                 vm->def->name);
            return -1;
        }
    }

    return 0;
}


692
int qemuInitCgroup(virQEMUDriverPtr driver,
693 694
                   virDomainObjPtr vm,
                   bool startup)
695
{
696
    int rc = -1;
697
    qemuDomainObjPrivatePtr priv = vm->privateData;
698
    virCgroupPtr parent = NULL;
699 700
    virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);

701 702 703
    if (!cfg->privileged)
        goto done;

704 705
    virCgroupFree(&priv->cgroup);

706 707 708
    if (!vm->def->resource && startup) {
        virDomainResourceDefPtr res;

709
        if (VIR_ALLOC(res) < 0)
710
            goto cleanup;
711

712
        if (VIR_STRDUP(res->partition, "/machine") < 0) {
713 714 715 716 717
            VIR_FREE(res);
            goto cleanup;
        }

        vm->def->resource = res;
718 719
    }

720 721 722 723 724 725 726 727 728 729 730
    if (vm->def->resource &&
        vm->def->resource->partition) {
        if (vm->def->resource->partition[0] != '/') {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Resource partition '%s' must start with '/'"),
                           vm->def->resource->partition);
            goto cleanup;
        }
        /* We only auto-create the default partition. In other
         * cases we expec the sysadmin/app to have done so */
        rc = virCgroupNewPartition(vm->def->resource->partition,
731
                                   STREQ(vm->def->resource->partition, "/machine"),
732 733 734 735 736
                                   cfg->cgroupControllers,
                                   &parent);
        if (rc != 0) {
            if (rc == -ENXIO ||
                rc == -EPERM ||
737
                rc == -EACCES) { /* No cgroups mounts == success */
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
                VIR_DEBUG("No cgroups present/configured/accessible, ignoring error");
                goto done;
            }

            virReportSystemError(-rc,
                                 _("Unable to initialize %s cgroup"),
                                 vm->def->resource->partition);
            goto cleanup;
        }

        rc = virCgroupNewDomainPartition(parent,
                                         "qemu",
                                         vm->def->name,
                                         true,
                                         &priv->cgroup);
        if (rc != 0) {
            virReportSystemError(-rc,
                                 _("Unable to create cgroup for %s"),
                                 vm->def->name);
            goto cleanup;
        }
    } else {
        rc = virCgroupNewDriver("qemu",
                                true,
                                cfg->cgroupControllers,
                                &parent);
        if (rc != 0) {
            if (rc == -ENXIO ||
                rc == -EPERM ||
                rc == -EACCES) { /* No cgroups mounts == success */
                VIR_DEBUG("No cgroups present/configured/accessible, ignoring error");
                goto done;
            }

            virReportSystemError(-rc,
                                 _("Unable to create cgroup for %s"),
                                 vm->def->name);
            goto cleanup;
        }

        rc = virCgroupNewDomainDriver(parent,
                                      vm->def->name,
                                      true,
                                      &priv->cgroup);
        if (rc != 0) {
            virReportSystemError(-rc,
                                 _("Unable to create cgroup for %s"),
                                 vm->def->name);
            goto cleanup;
        }
788 789 790 791 792
    }

done:
    rc = 0;
cleanup:
793
    virCgroupFree(&parent);
794 795 796 797 798
    virObjectUnref(cfg);
    return rc;
}


799
int qemuSetupCgroup(virQEMUDriverPtr driver,
800
                    virDomainObjPtr vm,
801
                    virBitmapPtr nodemask)
802
{
803
    qemuDomainObjPrivatePtr priv = vm->privateData;
804
    int ret = -1;
805

806
    if (qemuInitCgroup(driver, vm, true) < 0)
807
        return -1;
808

809
    if (!priv->cgroup)
810
        return 0;
811

812 813
    if (qemuSetupDevicesCgroup(driver, vm) < 0)
        goto cleanup;
814

815 816
    if (qemuSetupBlkioCgroup(vm) < 0)
        goto cleanup;
817

818 819
    if (qemuSetupMemoryCgroup(vm) < 0)
        goto cleanup;
820

821 822
    if (qemuSetupCpuCgroup(vm) < 0)
        goto cleanup;
823

824 825
    if (qemuSetupCpusetCgroup(vm, nodemask) < 0)
        goto cleanup;
826

827
    ret = 0;
828
cleanup:
829
    return ret;
830 831
}

832 833 834 835 836 837 838 839 840 841 842 843 844 845
int qemuSetupCgroupVcpuBW(virCgroupPtr cgroup, unsigned long long period,
                          long long quota)
{
    int rc;
    unsigned long long old_period;

    if (period == 0 && quota == 0)
        return 0;

    if (period) {
        /* get old period, and we can rollback if set quota failed */
        rc = virCgroupGetCpuCfsPeriod(cgroup, &old_period);
        if (rc < 0) {
            virReportSystemError(-rc,
W
Wen Congyang 已提交
846
                                 "%s", _("Unable to get cpu bandwidth period"));
847 848 849 850 851 852
            return -1;
        }

        rc = virCgroupSetCpuCfsPeriod(cgroup, period);
        if (rc < 0) {
            virReportSystemError(-rc,
W
Wen Congyang 已提交
853
                                 "%s", _("Unable to set cpu bandwidth period"));
854 855 856 857 858 859 860 861
            return -1;
        }
    }

    if (quota) {
        rc = virCgroupSetCpuCfsQuota(cgroup, quota);
        if (rc < 0) {
            virReportSystemError(-rc,
W
Wen Congyang 已提交
862
                                 "%s", _("Unable to set cpu bandwidth quota"));
863 864 865 866 867 868 869 870 871 872
            goto cleanup;
        }
    }

    return 0;

cleanup:
    if (period) {
        rc = virCgroupSetCpuCfsPeriod(cgroup, old_period);
        if (rc < 0)
873
            virReportSystemError(-rc, "%s",
874
                                 _("Unable to rollback cpu bandwidth period"));
875 876 877 878 879
    }

    return -1;
}

880 881 882 883 884
int qemuSetupCgroupVcpuPin(virCgroupPtr cgroup,
                           virDomainVcpuPinDefPtr *vcpupin,
                           int nvcpupin,
                           int vcpuid)
{
885
    size_t i;
886 887 888

    for (i = 0; i < nvcpupin; i++) {
        if (vcpuid == vcpupin[i]->vcpuid) {
889
            return qemuSetupCgroupEmulatorPin(cgroup, vcpupin[i]->cpumask);
890 891 892
        }
    }

893 894 895 896
    return -1;
}

int qemuSetupCgroupEmulatorPin(virCgroupPtr cgroup,
897
                               virBitmapPtr cpumask)
898 899 900 901
{
    int rc = 0;
    char *new_cpus = NULL;

902
    new_cpus = virBitmapFormat(cpumask);
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
    if (!new_cpus) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("failed to convert cpu mask"));
        rc = -1;
        goto cleanup;
    }

    rc = virCgroupSetCpusetCpus(cgroup, new_cpus);
    if (rc < 0) {
        virReportSystemError(-rc,
                             "%s",
                             _("Unable to set cpuset.cpus"));
        goto cleanup;
    }

918 919 920 921 922
cleanup:
    VIR_FREE(new_cpus);
    return rc;
}

923
int qemuSetupCgroupForVcpu(virDomainObjPtr vm)
924 925 926
{
    virCgroupPtr cgroup_vcpu = NULL;
    qemuDomainObjPrivatePtr priv = vm->privateData;
927
    virDomainDefPtr def = vm->def;
928
    int rc;
929
    size_t i, j;
930 931 932
    unsigned long long period = vm->def->cputune.period;
    long long quota = vm->def->cputune.quota;

933
    if ((period || quota) &&
934
        !virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_CPU)) {
935 936
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("cgroup cpu is required for scheduler tuning"));
H
Hu Tao 已提交
937 938 939
        return -1;
    }

940 941 942 943
    /* We are trying to setup cgroups for CPU pinning, which can also be done
     * with virProcessInfoSetAffinity, thus the lack of cgroups is not fatal
     * here.
     */
944
    if (priv->cgroup == NULL)
945 946
        return 0;

947
    if (priv->nvcpupids == 0 || priv->vcpupids[0] == vm->pid) {
948
        /* If we don't know VCPU<->PID mapping or all vcpu runs in the same
W
Wen Congyang 已提交
949
         * thread, we cannot control each vcpu.
950
         */
951 952
        VIR_WARN("Unable to get vcpus' pids.");
        return 0;
953 954 955
    }

    for (i = 0; i < priv->nvcpupids; i++) {
956
        rc = virCgroupNewVcpu(priv->cgroup, i, true, &cgroup_vcpu);
957 958 959
        if (rc < 0) {
            virReportSystemError(-rc,
                                 _("Unable to create vcpu cgroup for %s(vcpu:"
960
                                   " %zu)"),
961 962 963 964 965 966 967 968
                                 vm->def->name, i);
            goto cleanup;
        }

        /* move the thread for vcpu to sub dir */
        rc = virCgroupAddTask(cgroup_vcpu, priv->vcpupids[i]);
        if (rc < 0) {
            virReportSystemError(-rc,
969
                                 _("unable to add vcpu %zu task %d to cgroup"),
970 971 972 973 974
                                 i, priv->vcpupids[i]);
            goto cleanup;
        }

        if (period || quota) {
H
Hu Tao 已提交
975 976
            if (qemuSetupCgroupVcpuBW(cgroup_vcpu, period, quota) < 0)
                goto cleanup;
977 978
        }

979
        /* Set vcpupin in cgroup if vcpupin xml is provided */
980
        if (virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_CPUSET)) {
M
Martin Kletzander 已提交
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
            /* find the right CPU to pin, otherwise
             * qemuSetupCgroupVcpuPin will fail. */
            for (j = 0; j < def->cputune.nvcpupin; j++) {
                if (def->cputune.vcpupin[j]->vcpuid != i)
                    continue;

                if (qemuSetupCgroupVcpuPin(cgroup_vcpu,
                                           def->cputune.vcpupin,
                                           def->cputune.nvcpupin,
                                           i) < 0)
                    goto cleanup;

                break;
            }
        }
996

997 998 999 1000 1001 1002
        virCgroupFree(&cgroup_vcpu);
    }

    return 0;

cleanup:
1003 1004 1005 1006 1007
    if (cgroup_vcpu) {
        virCgroupRemove(cgroup_vcpu);
        virCgroupFree(&cgroup_vcpu);
    }

1008 1009 1010
    return -1;
}

1011
int qemuSetupCgroupForEmulator(virQEMUDriverPtr driver,
1012 1013
                               virDomainObjPtr vm,
                               virBitmapPtr nodemask)
1014
{
1015
    virBitmapPtr cpumask = NULL;
1016
    virBitmapPtr cpumap = NULL;
1017
    virCgroupPtr cgroup_emulator = NULL;
1018
    virDomainDefPtr def = vm->def;
1019
    qemuDomainObjPrivatePtr priv = vm->privateData;
1020 1021
    unsigned long long period = vm->def->cputune.emulator_period;
    long long quota = vm->def->cputune.emulator_quota;
1022
    int rc;
1023

1024
    if ((period || quota) &&
1025
        !virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_CPU)) {
1026 1027 1028 1029 1030
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("cgroup cpu is required for scheduler tuning"));
        return -1;
    }

1031
    if (priv->cgroup == NULL)
1032 1033
        return 0; /* Not supported, so claim success */

1034
    rc = virCgroupNewEmulator(priv->cgroup, true, &cgroup_emulator);
1035 1036 1037 1038 1039 1040 1041
    if (rc < 0) {
        virReportSystemError(-rc,
                             _("Unable to create emulator cgroup for %s"),
                             vm->def->name);
        goto cleanup;
    }

1042
    rc = virCgroupMoveTask(priv->cgroup, cgroup_emulator);
1043 1044 1045 1046 1047 1048
    if (rc < 0) {
        virReportSystemError(-rc,
                             _("Unable to move tasks from domain cgroup to "
                               "emulator cgroup for %s"),
                             vm->def->name);
        goto cleanup;
1049 1050
    }

1051 1052 1053 1054 1055
    if (def->placement_mode == VIR_DOMAIN_CPU_PLACEMENT_MODE_AUTO) {
        if (!(cpumap = qemuPrepareCpumap(driver, nodemask)))
            goto cleanup;
        cpumask = cpumap;
    } else if (def->cputune.emulatorpin) {
1056
        cpumask = def->cputune.emulatorpin->cpumask;
1057
    } else if (def->cpumask) {
1058
        cpumask = def->cpumask;
1059
    }
1060 1061

    if (cpumask) {
1062
        if (virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_CPUSET)) {
1063 1064 1065 1066 1067
            rc = qemuSetupCgroupEmulatorPin(cgroup_emulator, cpumask);
            if (rc < 0)
                goto cleanup;
        }
        cpumask = NULL; /* sanity */
H
Hu Tao 已提交
1068
    }
1069

1070
    if (period || quota) {
1071
        if (virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_CPU)) {
H
Hu Tao 已提交
1072 1073
            if ((rc = qemuSetupCgroupVcpuBW(cgroup_emulator, period,
                                            quota)) < 0)
1074 1075 1076 1077
                goto cleanup;
        }
    }

1078
    virCgroupFree(&cgroup_emulator);
1079
    virBitmapFree(cpumap);
1080 1081 1082
    return 0;

cleanup:
1083 1084
    virBitmapFree(cpumap);

1085 1086 1087 1088 1089 1090 1091
    if (cgroup_emulator) {
        virCgroupRemove(cgroup_emulator);
        virCgroupFree(&cgroup_emulator);
    }

    return rc;
}
1092

1093
int qemuRemoveCgroup(virDomainObjPtr vm)
1094
{
1095
    qemuDomainObjPrivatePtr priv = vm->privateData;
1096

1097
    if (priv->cgroup == NULL)
1098 1099
        return 0; /* Not supported, so claim success */

1100
    return virCgroupRemove(priv->cgroup);
1101 1102
}

1103
int qemuAddToCgroup(virDomainObjPtr vm)
1104
{
1105
    qemuDomainObjPrivatePtr priv = vm->privateData;
1106 1107
    int rc;

1108
    if (priv->cgroup == NULL)
1109 1110
        return 0; /* Not supported, so claim success */

1111
    rc = virCgroupAddTask(priv->cgroup, getpid());
1112 1113 1114
    if (rc != 0) {
        virReportSystemError(-rc,
                             _("unable to add domain %s task %d to cgroup"),
1115 1116
                             vm->def->name, getpid());
        return -1;
1117 1118
    }

1119
    return 0;
1120
}