lxc_cgroup.c 15.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (C) 2010-2012 Red Hat, Inc.
 * Copyright IBM Corp. 2008
 *
 * lxc_cgroup.c: LXC cgroup helpers
 *
 * 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
 */

#include <config.h>

#include "lxc_cgroup.h"
#include "lxc_container.h"
26
#include "virfile.h"
27
#include "virerror.h"
28
#include "virlog.h"
29
#include "viralloc.h"
30
#include "vircgroup.h"
31
#include "virstring.h"
32 33 34 35 36 37 38

#define VIR_FROM_THIS VIR_FROM_LXC

static int virLXCCgroupSetupCpuTune(virDomainDefPtr def,
                                    virCgroupPtr cgroup)
{
    int ret = -1;
39 40 41 42 43 44 45 46 47 48 49 50
    if (def->cputune.shares != 0 &&
        virCgroupSetCpuShares(cgroup, def->cputune.shares) < 0)
        goto cleanup;

    if (def->cputune.quota != 0 &&
        virCgroupSetCpuCfsQuota(cgroup, def->cputune.quota) < 0)
        goto cleanup;

    if (def->cputune.period != 0 &&
        virCgroupSetCpuCfsPeriod(cgroup, def->cputune.period) < 0)
        goto cleanup;

51 52 53 54 55 56
    ret = 0;
cleanup:
    return ret;
}


57 58 59 60
static int virLXCCgroupSetupCpusetTune(virDomainDefPtr def,
                                       virCgroupPtr cgroup,
                                       virBitmapPtr nodemask)
{
61
    int ret = -1;
62 63 64 65 66 67 68 69 70 71 72
    char *mask = NULL;

    if (def->placement_mode != VIR_DOMAIN_CPU_PLACEMENT_MODE_AUTO &&
        def->cpumask) {
        mask = virBitmapFormat(def->cpumask);
        if (!mask) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("failed to convert cpumask"));
            return -1;
        }

73
        if (virCgroupSetCpusetCpus(cgroup, mask) < 0)
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
            goto cleanup;
    }

    if ((def->numatune.memory.nodemask ||
         (def->numatune.memory.placement_mode ==
          VIR_NUMA_TUNE_MEM_PLACEMENT_MODE_AUTO)) &&
          def->numatune.memory.mode == VIR_DOMAIN_NUMATUNE_MEM_STRICT) {
        if (def->numatune.memory.placement_mode ==
            VIR_NUMA_TUNE_MEM_PLACEMENT_MODE_AUTO)
            mask = virBitmapFormat(nodemask);
        else
            mask = virBitmapFormat(def->numatune.memory.nodemask);

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

93 94
        if (virCgroupSetCpusetMems(cgroup, mask) < 0)
            goto cleanup;
95 96
    }

97
    ret = 0;
98 99
cleanup:
    VIR_FREE(mask);
100
    return ret;
101 102 103
}


104 105 106
static int virLXCCgroupSetupBlkioTune(virDomainDefPtr def,
                                      virCgroupPtr cgroup)
{
107
    size_t i;
108 109 110 111

    if (def->blkio.weight &&
        virCgroupSetBlkioWeight(cgroup, def->blkio.weight) < 0)
        return -1;
112

113 114 115 116 117
    if (def->blkio.ndevices) {
        for (i = 0; i < def->blkio.ndevices; i++) {
            virBlkioDeviceWeightPtr dw = &def->blkio.devices[i];
            if (!dw->weight)
                continue;
118
            if (virCgroupSetBlkioDeviceWeight(cgroup, dw->path, dw->weight) < 0)
119 120 121 122 123
                return -1;
        }
    }

    return 0;
124 125 126 127 128 129 130 131
}


static int virLXCCgroupSetupMemTune(virDomainDefPtr def,
                                    virCgroupPtr cgroup)
{
    int ret = -1;

132
    if (virCgroupSetMemory(cgroup, def->mem.max_balloon) < 0)
133 134
        goto cleanup;

135 136 137
    if (def->mem.hard_limit &&
        virCgroupSetMemoryHardLimit(cgroup, def->mem.hard_limit) < 0)
        goto cleanup;
138

139 140 141
    if (def->mem.soft_limit &&
        virCgroupSetMemorySoftLimit(cgroup, def->mem.soft_limit) < 0)
        goto cleanup;
142

143 144 145
    if (def->mem.swap_hard_limit &&
        virCgroupSetMemSwapHardLimit(cgroup, def->mem.swap_hard_limit) < 0)
        goto cleanup;
146 147 148 149 150 151 152

    ret = 0;
cleanup:
    return ret;
}


153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 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 245 246 247 248 249 250 251 252
static int virLXCCgroupGetMemSwapUsage(virCgroupPtr cgroup,
                                       virLXCMeminfoPtr meminfo)
{
    return virCgroupGetMemSwapUsage(cgroup, &meminfo->swapusage);
}


static int virLXCCgroupGetMemSwapTotal(virCgroupPtr cgroup,
                                       virLXCMeminfoPtr meminfo)
{
    return virCgroupGetMemSwapHardLimit(cgroup, &meminfo->swaptotal);
}


static int virLXCCgroupGetMemUsage(virCgroupPtr cgroup,
                                   virLXCMeminfoPtr meminfo)
{
    int ret;
    unsigned long memUsage;

    ret = virCgroupGetMemoryUsage(cgroup, &memUsage);
    meminfo->memusage = (unsigned long long) memUsage;

    return ret;
}


static int virLXCCgroupGetMemTotal(virCgroupPtr cgroup,
                                   virLXCMeminfoPtr meminfo)
{
    return virCgroupGetMemoryHardLimit(cgroup, &meminfo->memtotal);
}


static int virLXCCgroupGetMemStat(virCgroupPtr cgroup,
                                  virLXCMeminfoPtr meminfo)
{
    int ret = 0;
    FILE *statfd = NULL;
    char *statFile = NULL;
    char *line = NULL;
    size_t n;

    ret = virCgroupPathOfController(cgroup, VIR_CGROUP_CONTROLLER_MEMORY,
                                    "memory.stat", &statFile);
    if (ret != 0) {
        virReportSystemError(-ret, "%s",
                             _("cannot get the path of MEMORY cgroup controller"));
        return ret;
    }

    statfd = fopen(statFile, "r");
    if (statfd == NULL) {
        ret = -errno;
        goto cleanup;
    }

    while (getline(&line, &n, statfd) > 0) {

        char *value = strchr(line, ' ');
        char *nl = value ? strchr(line, '\n') : NULL;
        unsigned long long stat_value;

        if (!value)
            continue;

        if (nl)
            *nl = '\0';

        *value = '\0';

        if (virStrToLong_ull(value + 1, NULL, 10, &stat_value) < 0) {
            ret = -EINVAL;
            goto cleanup;
        }
        if (STREQ(line, "cache"))
            meminfo->cached = stat_value >> 10;
        else if (STREQ(line, "inactive_anon"))
            meminfo->inactive_anon = stat_value >> 10;
        else if (STREQ(line, "active_anon"))
            meminfo->active_anon = stat_value >> 10;
        else if (STREQ(line, "inactive_file"))
            meminfo->inactive_file = stat_value >> 10;
        else if (STREQ(line, "active_file"))
            meminfo->active_file = stat_value >> 10;
        else if (STREQ(line, "unevictable"))
            meminfo->unevictable = stat_value >> 10;
    }
    ret = 0;

cleanup:
    VIR_FREE(line);
    VIR_FREE(statFile);
    VIR_FORCE_FCLOSE(statfd);
    return ret;
}


int virLXCCgroupGetMeminfo(virLXCMeminfoPtr meminfo)
{
253
    int ret = -1;
254 255
    virCgroupPtr cgroup;

256 257
    if (virCgroupNewSelf(&cgroup) < 0)
        return -1;
258

259
    if (virLXCCgroupGetMemStat(cgroup, meminfo) < 0)
260 261
        goto cleanup;

262
    if (virLXCCgroupGetMemTotal(cgroup, meminfo) < 0)
263 264
        goto cleanup;

265
    if (virLXCCgroupGetMemUsage(cgroup, meminfo) < 0)
266 267 268 269 270 271 272 273 274 275 276 277 278
        goto cleanup;

    virLXCCgroupGetMemSwapTotal(cgroup, meminfo);
    virLXCCgroupGetMemSwapUsage(cgroup, meminfo);

    ret = 0;
cleanup:
    virCgroupFree(&cgroup);
    return ret;
}



279 280 281 282 283 284 285 286 287 288
typedef struct _virLXCCgroupDevicePolicy virLXCCgroupDevicePolicy;
typedef virLXCCgroupDevicePolicy *virLXCCgroupDevicePolicyPtr;

struct _virLXCCgroupDevicePolicy {
    char type;
    int major;
    int minor;
};


289
int
290
virLXCSetupHostUsbDeviceCgroup(virUSBDevicePtr dev ATTRIBUTE_UNUSED,
291 292 293 294 295 296
                               const char *path,
                               void *opaque)
{
    virCgroupPtr cgroup = opaque;

    VIR_DEBUG("Process path '%s' for USB device", path);
297 298
    if (virCgroupAllowDevicePath(cgroup, path,
                                 VIR_CGROUP_DEVICE_RW) < 0)
299 300 301 302 303 304 305
        return -1;

    return 0;
}


int
306
virLXCTeardownHostUsbDeviceCgroup(virUSBDevicePtr dev ATTRIBUTE_UNUSED,
307 308 309 310 311 312
                                  const char *path,
                                  void *opaque)
{
    virCgroupPtr cgroup = opaque;

    VIR_DEBUG("Process path '%s' for USB device", path);
313 314
    if (virCgroupDenyDevicePath(cgroup, path,
                                VIR_CGROUP_DEVICE_RW) < 0)
315 316 317 318 319
        return -1;

    return 0;
}

320 321 322 323 324 325 326 327 328 329 330 331 332 333

static int virLXCCgroupSetupDeviceACL(virDomainDefPtr def,
                                      virCgroupPtr cgroup)
{
    int ret = -1;
    size_t i;
    static virLXCCgroupDevicePolicy devices[] = {
        {'c', LXC_DEV_MAJ_MEMORY, LXC_DEV_MIN_NULL},
        {'c', LXC_DEV_MAJ_MEMORY, LXC_DEV_MIN_ZERO},
        {'c', LXC_DEV_MAJ_MEMORY, LXC_DEV_MIN_FULL},
        {'c', LXC_DEV_MAJ_MEMORY, LXC_DEV_MIN_RANDOM},
        {'c', LXC_DEV_MAJ_MEMORY, LXC_DEV_MIN_URANDOM},
        {'c', LXC_DEV_MAJ_TTY, LXC_DEV_MIN_TTY},
        {'c', LXC_DEV_MAJ_TTY, LXC_DEV_MIN_PTMX},
G
Gao feng 已提交
334
        {'c', LXC_DEV_MAJ_FUSE, LXC_DEV_MIN_FUSE},
335 336
        {0,   0, 0}};

337
    if (virCgroupDenyAllDevices(cgroup) < 0)
338 339 340 341
        goto cleanup;

    for (i = 0; devices[i].type != 0; i++) {
        virLXCCgroupDevicePolicyPtr dev = &devices[i];
342 343 344 345 346
        if (virCgroupAllowDevice(cgroup,
                                 dev->type,
                                 dev->major,
                                 dev->minor,
                                 VIR_CGROUP_DEVICE_RWM) < 0)
347 348 349
            goto cleanup;
    }

350
    for (i = 0; i < def->ndisks; i++) {
351 352 353
        if (def->disks[i]->type != VIR_DOMAIN_DISK_TYPE_BLOCK)
            continue;

354 355 356 357 358 359
        if (virCgroupAllowDevicePath(cgroup,
                                     def->disks[i]->src,
                                     (def->disks[i]->readonly ?
                                      VIR_CGROUP_DEVICE_READ :
                                      VIR_CGROUP_DEVICE_RW) |
                                     VIR_CGROUP_DEVICE_MKNOD) < 0)
360 361 362
            goto cleanup;
    }

363
    for (i = 0; i < def->nfss; i++) {
364 365 366
        if (def->fss[i]->type != VIR_DOMAIN_FS_TYPE_BLOCK)
            continue;

367 368 369 370 371
        if (virCgroupAllowDevicePath(cgroup,
                                     def->fss[i]->src,
                                     def->fss[i]->readonly ?
                                     VIR_CGROUP_DEVICE_READ :
                                     VIR_CGROUP_DEVICE_RW) < 0)
372 373 374
            goto cleanup;
    }

375 376
    for (i = 0; i < def->nhostdevs; i++) {
        virDomainHostdevDefPtr hostdev = def->hostdevs[i];
377
        virUSBDevicePtr usb;
378

379 380 381 382 383 384 385
        switch (hostdev->mode) {
        case VIR_DOMAIN_HOSTDEV_MODE_SUBSYS:
            if (hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB)
                continue;
            if (hostdev->missing)
                continue;

386 387 388
            if ((usb = virUSBDeviceNew(hostdev->source.subsys.u.usb.bus,
                                       hostdev->source.subsys.u.usb.device,
                                       NULL)) == NULL)
389 390
                goto cleanup;

391 392 393
            if (virUSBDeviceFileIterate(usb, virLXCSetupHostUsbDeviceCgroup,
                                        cgroup) < 0) {
                virUSBDeviceFree(usb);
394
                goto cleanup;
395
            }
396
            virUSBDeviceFree(usb);
397 398 399 400 401 402 403 404 405 406
            break;
        case VIR_DOMAIN_HOSTDEV_MODE_CAPABILITIES:
            switch (hostdev->source.caps.type) {
            case VIR_DOMAIN_HOSTDEV_CAPS_TYPE_STORAGE:
                if (virCgroupAllowDevicePath(cgroup,
                                             hostdev->source.caps.u.storage.block,
                                             VIR_CGROUP_DEVICE_RW |
                                             VIR_CGROUP_DEVICE_MKNOD) < 0)
                    goto cleanup;
                break;
407 408 409 410 411 412 413
            case VIR_DOMAIN_HOSTDEV_CAPS_TYPE_MISC:
                if (virCgroupAllowDevicePath(cgroup,
                                             hostdev->source.caps.u.misc.chardev,
                                             VIR_CGROUP_DEVICE_RW |
                                             VIR_CGROUP_DEVICE_MKNOD) < 0)
                    goto cleanup;
                break;
414 415 416 417 418 419
            default:
                break;
            }
        default:
            break;
        }
420 421
    }

422 423
    if (virCgroupAllowDeviceMajor(cgroup, 'c', LXC_DEV_MAJ_PTY,
                                  VIR_CGROUP_DEVICE_RWM) < 0)
424 425 426 427 428 429 430 431
        goto cleanup;

    ret = 0;
cleanup:
    return ret;
}


432
virCgroupPtr virLXCCgroupCreate(virDomainDefPtr def, bool startup)
433
{
434 435
    virCgroupPtr parent = NULL;
    virCgroupPtr cgroup = NULL;
436

437 438 439
    if (!def->resource && startup) {
        virDomainResourceDefPtr res;

440
        if (VIR_ALLOC(res) < 0)
441 442
            goto cleanup;

443
        if (VIR_STRDUP(res->partition, "/machine") < 0) {
444 445 446 447 448
            VIR_FREE(res);
            goto cleanup;
        }

        def->resource = res;
449 450
    }

451 452 453 454 455 456 457 458 459 460
    if (def->resource &&
        def->resource->partition) {
        if (def->resource->partition[0] != '/') {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Resource partition '%s' must start with '/'"),
                           def->resource->partition);
            goto cleanup;
        }
        /* We only auto-create the default partition. In other
         * cases we expec the sysadmin/app to have done so */
461 462 463 464
        if (virCgroupNewPartition(def->resource->partition,
                                  STREQ(def->resource->partition, "/machine"),
                                  -1,
                                  &parent) < 0)
465 466
            goto cleanup;

467 468 469 470 471
        if (virCgroupNewDomainPartition(parent,
                                        "lxc",
                                        def->name,
                                        true,
                                        &cgroup) < 0)
472 473
            goto cleanup;
    } else {
474 475 476 477
        if (virCgroupNewDriver("lxc",
                               true,
                               -1,
                               &parent) < 0)
478 479
            goto cleanup;

480 481 482 483
        if (virCgroupNewDomainDriver(parent,
                                     def->name,
                                     true,
                                     &cgroup) < 0)
484
            goto cleanup;
485 486
    }

487
cleanup:
488
    virCgroupFree(&parent);
489 490 491 492 493 494 495 496 497
    return cgroup;
}


virCgroupPtr virLXCCgroupJoin(virDomainDefPtr def)
{
    virCgroupPtr cgroup = NULL;
    int ret = -1;

498
    if (!(cgroup = virLXCCgroupCreate(def, true)))
499 500
        return NULL;

501
    if (virCgroupAddTask(cgroup, getpid()) < 0)
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
        goto cleanup;

    ret = 0;

cleanup:
    if (ret < 0) {
        virCgroupFree(&cgroup);
        return NULL;
    }

    return cgroup;
}


int virLXCCgroupSetup(virDomainDefPtr def,
517 518
                      virCgroupPtr cgroup,
                      virBitmapPtr nodemask)
519 520 521
{
    int ret = -1;

522 523 524
    if (virLXCCgroupSetupCpuTune(def, cgroup) < 0)
        goto cleanup;

525 526 527
    if (virLXCCgroupSetupCpusetTune(def, cgroup, nodemask) < 0)
        goto cleanup;

528 529 530 531 532 533 534 535 536
    if (virLXCCgroupSetupBlkioTune(def, cgroup) < 0)
        goto cleanup;

    if (virLXCCgroupSetupMemTune(def, cgroup) < 0)
        goto cleanup;

    if (virLXCCgroupSetupDeviceACL(def, cgroup) < 0)
        goto cleanup;

537 538
    ret = 0;

539
cleanup:
540
    return ret;
541
}