lxc_cgroup.c 15.5 KB
Newer Older
1
/*
2
 * Copyright (C) 2010-2014 Red Hat, Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 * 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

#define VIR_FROM_THIS VIR_FROM_LXC

35 36
VIR_LOG_INIT("lxc.lxc_cgroup");

37 38 39 40
static int virLXCCgroupSetupCpuTune(virDomainDefPtr def,
                                    virCgroupPtr cgroup)
{
    int ret = -1;
41 42 43 44 45 46 47 48 49 50

    if (def->cputune.sharesSpecified) {
        unsigned long long val;
        if (virCgroupSetCpuShares(cgroup, def->cputune.shares) < 0)
            goto cleanup;

        if (virCgroupGetCpuShares(cgroup, &val) < 0)
            goto cleanup;
        def->cputune.shares = val;
    }
51 52 53 54 55 56 57 58 59

    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;

60
    ret = 0;
61
 cleanup:
62 63 64 65
    return ret;
}


66 67 68 69
static int virLXCCgroupSetupCpusetTune(virDomainDefPtr def,
                                       virCgroupPtr cgroup,
                                       virBitmapPtr nodemask)
{
70
    int ret = -1;
71
    char *mask = NULL;
72
    virDomainNumatuneMemMode mode;
73 74 75

    if (def->placement_mode != VIR_DOMAIN_CPU_PLACEMENT_MODE_AUTO &&
        def->cpumask) {
76
        if (!(mask = virBitmapFormat(def->cpumask)))
77 78
            return -1;

79
        if (virCgroupSetCpusetCpus(cgroup, mask) < 0)
80
            goto cleanup;
81 82
        /* free mask to make sure we won't use it in a wrong way later */
        VIR_FREE(mask);
83 84
    }

85 86
    if (virDomainNumatuneGetMode(def->numa, -1, &mode) < 0 ||
        mode == VIR_DOMAIN_NUMATUNE_MEM_STRICT) {
87
        ret = 0;
88
        goto cleanup;
89
    }
90

91
    if (virDomainNumatuneMaybeFormatNodeset(def->numa, nodemask,
92
                                            &mask, -1) < 0)
93
        goto cleanup;
94

95 96
    if (mask && virCgroupSetCpusetMems(cgroup, mask) < 0)
        goto cleanup;
97

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


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

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

114 115
    if (def->blkio.ndevices) {
        for (i = 0; i < def->blkio.ndevices; i++) {
116
            virBlkioDevicePtr dev = &def->blkio.devices[i];
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140

            if (dev->weight &&
                (virCgroupSetBlkioDeviceWeight(cgroup, dev->path,
                                               dev->weight) < 0))
                return -1;

            if (dev->riops &&
                (virCgroupSetBlkioDeviceReadIops(cgroup, dev->path,
                                                 dev->riops) < 0))
                return -1;

            if (dev->wiops &&
                (virCgroupSetBlkioDeviceWriteIops(cgroup, dev->path,
                                                  dev->wiops) < 0))
                return -1;

            if (dev->rbps &&
                (virCgroupSetBlkioDeviceReadBps(cgroup, dev->path,
                                                dev->rbps) < 0))
                return -1;

            if (dev->wbps &&
                (virCgroupSetBlkioDeviceWriteBps(cgroup, dev->path,
                                                 dev->wbps) < 0))
141 142 143 144 145
                return -1;
        }
    }

    return 0;
146 147 148 149 150 151 152 153
}


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

154
    if (virCgroupSetMemory(cgroup, virDomainDefGetMemoryInitial(def)) < 0)
155 156
        goto cleanup;

157 158 159
    if (virMemoryLimitIsSet(def->mem.hard_limit))
        if (virCgroupSetMemoryHardLimit(cgroup, def->mem.hard_limit) < 0)
            goto cleanup;
160

161 162 163
    if (virMemoryLimitIsSet(def->mem.soft_limit))
        if (virCgroupSetMemorySoftLimit(cgroup, def->mem.soft_limit) < 0)
            goto cleanup;
164

165 166 167
    if (virMemoryLimitIsSet(def->mem.swap_hard_limit))
        if (virCgroupSetMemSwapHardLimit(cgroup, def->mem.swap_hard_limit) < 0)
            goto cleanup;
168 169

    ret = 0;
170
 cleanup:
171 172 173 174
    return ret;
}


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 253 254 255 256 257 258 259 260 261 262 263 264
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;

265
 cleanup:
266 267 268 269 270 271 272 273 274
    VIR_FREE(line);
    VIR_FREE(statFile);
    VIR_FORCE_FCLOSE(statfd);
    return ret;
}


int virLXCCgroupGetMeminfo(virLXCMeminfoPtr meminfo)
{
275
    int ret = -1;
276 277
    virCgroupPtr cgroup;

278 279
    if (virCgroupNewSelf(&cgroup) < 0)
        return -1;
280

281
    if (virLXCCgroupGetMemStat(cgroup, meminfo) < 0)
282 283
        goto cleanup;

284
    if (virLXCCgroupGetMemTotal(cgroup, meminfo) < 0)
285 286
        goto cleanup;

287
    if (virLXCCgroupGetMemUsage(cgroup, meminfo) < 0)
288 289
        goto cleanup;

290 291 292 293 294
    if (virLXCCgroupGetMemSwapTotal(cgroup, meminfo) < 0)
        goto cleanup;

    if (virLXCCgroupGetMemSwapUsage(cgroup, meminfo) < 0)
        goto cleanup;
295 296

    ret = 0;
297
 cleanup:
298 299 300 301 302 303
    virCgroupFree(&cgroup);
    return ret;
}



304 305 306 307 308 309 310 311 312 313
typedef struct _virLXCCgroupDevicePolicy virLXCCgroupDevicePolicy;
typedef virLXCCgroupDevicePolicy *virLXCCgroupDevicePolicyPtr;

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


314
int
315
virLXCSetupHostUSBDeviceCgroup(virUSBDevicePtr dev ATTRIBUTE_UNUSED,
316 317 318 319 320 321
                               const char *path,
                               void *opaque)
{
    virCgroupPtr cgroup = opaque;

    VIR_DEBUG("Process path '%s' for USB device", path);
322
    if (virCgroupAllowDevicePath(cgroup, path,
323
                                 VIR_CGROUP_DEVICE_RWM) < 0)
324 325 326 327 328 329 330
        return -1;

    return 0;
}


int
331
virLXCTeardownHostUSBDeviceCgroup(virUSBDevicePtr dev ATTRIBUTE_UNUSED,
332 333 334 335 336 337
                                  const char *path,
                                  void *opaque)
{
    virCgroupPtr cgroup = opaque;

    VIR_DEBUG("Process path '%s' for USB device", path);
338
    if (virCgroupDenyDevicePath(cgroup, path,
339
                                VIR_CGROUP_DEVICE_RWM) < 0)
340 341 342 343 344
        return -1;

    return 0;
}

345 346 347 348

static int virLXCCgroupSetupDeviceACL(virDomainDefPtr def,
                                      virCgroupPtr cgroup)
{
349
    int capMknod = def->caps_features[VIR_DOMAIN_CAPS_FEATURE_MKNOD];
350 351 352 353 354 355 356 357 358 359
    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 已提交
360
        {'c', LXC_DEV_MAJ_FUSE, LXC_DEV_MIN_FUSE},
361 362
        {0,   0, 0}};

363
    if (virCgroupDenyAllDevices(cgroup) < 0)
364 365
        goto cleanup;

366
    /* white list mknod if CAP_MKNOD has to be kept */
J
Ján Tomko 已提交
367
    if (capMknod == VIR_TRISTATE_SWITCH_ON) {
368 369 370 371 372
        if (virCgroupAllowAllDevices(cgroup,
                                    VIR_CGROUP_DEVICE_MKNOD) < 0)
            goto cleanup;
    }

373 374
    for (i = 0; devices[i].type != 0; i++) {
        virLXCCgroupDevicePolicyPtr dev = &devices[i];
375 376 377 378 379
        if (virCgroupAllowDevice(cgroup,
                                 dev->type,
                                 dev->major,
                                 dev->minor,
                                 VIR_CGROUP_DEVICE_RWM) < 0)
380 381 382
            goto cleanup;
    }

383
    VIR_DEBUG("Allowing any disk block devs");
384
    for (i = 0; i < def->ndisks; i++) {
385
        if (!virDomainDiskSourceIsBlockType(def->disks[i]->src))
386 387
            continue;

388
        if (virCgroupAllowDevicePath(cgroup,
389
                                     virDomainDiskGetSource(def->disks[i]),
390
                                     (def->disks[i]->src->readonly ?
391 392 393
                                      VIR_CGROUP_DEVICE_READ :
                                      VIR_CGROUP_DEVICE_RW) |
                                     VIR_CGROUP_DEVICE_MKNOD) < 0)
394 395 396
            goto cleanup;
    }

397
    VIR_DEBUG("Allowing any filesystem block devs");
398
    for (i = 0; i < def->nfss; i++) {
399 400 401
        if (def->fss[i]->type != VIR_DOMAIN_FS_TYPE_BLOCK)
            continue;

402 403 404 405 406
        if (virCgroupAllowDevicePath(cgroup,
                                     def->fss[i]->src,
                                     def->fss[i]->readonly ?
                                     VIR_CGROUP_DEVICE_READ :
                                     VIR_CGROUP_DEVICE_RW) < 0)
407 408 409
            goto cleanup;
    }

410
    VIR_DEBUG("Allowing any hostdev block devs");
411 412
    for (i = 0; i < def->nhostdevs; i++) {
        virDomainHostdevDefPtr hostdev = def->hostdevs[i];
413
        virDomainHostdevSubsysUSBPtr usbsrc = &hostdev->source.subsys.u.usb;
414
        virUSBDevicePtr usb;
415

416 417 418 419 420 421 422
        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;

423
            if ((usb = virUSBDeviceNew(usbsrc->bus, usbsrc->device,
424
                                       NULL)) == NULL)
425 426
                goto cleanup;

427
            if (virUSBDeviceFileIterate(usb, virLXCSetupHostUSBDeviceCgroup,
428 429
                                        cgroup) < 0) {
                virUSBDeviceFree(usb);
430
                goto cleanup;
431
            }
432
            virUSBDeviceFree(usb);
433 434 435 436 437 438 439 440 441 442
            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;
443 444 445 446 447 448 449
            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;
450 451 452 453 454 455
            default:
                break;
            }
        default:
            break;
        }
456 457
    }

458 459
    if (virCgroupAllowDeviceMajor(cgroup, 'c', LXC_DEV_MAJ_PTY,
                                  VIR_CGROUP_DEVICE_RWM) < 0)
460 461
        goto cleanup;

462 463
    VIR_DEBUG("Device whitelist complete");

464
    ret = 0;
465
 cleanup:
466 467 468 469
    return ret;
}


470
virCgroupPtr virLXCCgroupCreate(virDomainDefPtr def,
471 472 473
                                pid_t initpid,
                                size_t nnicindexes,
                                int *nicindexes)
474
{
475
    virCgroupPtr cgroup = NULL;
476

477 478 479 480 481
    if (def->resource->partition[0] != '/') {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Resource partition '%s' must start with '/'"),
                       def->resource->partition);
        goto cleanup;
482
    }
483

484 485 486 487 488
    if (virCgroupNewMachine(def->name,
                            "lxc",
                            true,
                            def->uuid,
                            NULL,
489
                            initpid,
490
                            true,
491
                            nnicindexes, nicindexes,
492 493 494
                            def->resource->partition,
                            -1,
                            &cgroup) < 0)
495
        goto cleanup;
496

497 498 499 500 501 502 503 504 505 506 507 508
    /* setup control group permissions for user namespace */
    if (def->idmap.uidmap) {
        if (virCgroupSetOwner(cgroup,
                              def->idmap.uidmap[0].target,
                              def->idmap.gidmap[0].target,
                              (1 << VIR_CGROUP_CONTROLLER_SYSTEMD)) < 0) {
            virCgroupFree(&cgroup);
            cgroup = NULL;
            goto cleanup;
        }
    }

509
 cleanup:
510 511 512 513 514
    return cgroup;
}


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

520 521 522
    if (virLXCCgroupSetupCpuTune(def, cgroup) < 0)
        goto cleanup;

523 524 525
    if (virLXCCgroupSetupCpusetTune(def, cgroup, nodemask) < 0)
        goto cleanup;

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

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

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

535 536
    ret = 0;

537
 cleanup:
538
    return ret;
539
}