lxc_cgroup.c 15.2 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
#include "virsystemd.h"
33 34 35

#define VIR_FROM_THIS VIR_FROM_LXC

36 37
VIR_LOG_INIT("lxc.lxc_cgroup");

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

    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;
    }
52 53 54 55 56 57 58 59 60

    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;

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


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

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

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

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

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

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

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


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

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

115 116
    if (def->blkio.ndevices) {
        for (i = 0; i < def->blkio.ndevices; i++) {
117
            virBlkioDevicePtr dev = &def->blkio.devices[i];
118 119 120

            if (dev->weight &&
                (virCgroupSetBlkioDeviceWeight(cgroup, dev->path,
121 122 123
                                               dev->weight) < 0 ||
                 virCgroupGetBlkioDeviceWeight(cgroup, dev->path,
                                               &dev->weight) < 0))
124 125 126 127
                return -1;

            if (dev->riops &&
                (virCgroupSetBlkioDeviceReadIops(cgroup, dev->path,
128 129 130
                                                 dev->riops) < 0 ||
                 virCgroupGetBlkioDeviceReadIops(cgroup, dev->path,
                                                 &dev->riops) < 0))
131 132 133 134
                return -1;

            if (dev->wiops &&
                (virCgroupSetBlkioDeviceWriteIops(cgroup, dev->path,
135 136 137
                                                  dev->wiops) < 0 ||
                 virCgroupGetBlkioDeviceWriteIops(cgroup, dev->path,
                                                  &dev->wiops) < 0))
138 139 140 141
                return -1;

            if (dev->rbps &&
                (virCgroupSetBlkioDeviceReadBps(cgroup, dev->path,
142 143 144
                                                dev->rbps) < 0 ||
                 virCgroupGetBlkioDeviceReadBps(cgroup, dev->path,
                                                &dev->rbps) < 0))
145 146 147 148
                return -1;

            if (dev->wbps &&
                (virCgroupSetBlkioDeviceWriteBps(cgroup, dev->path,
149 150 151
                                                 dev->wbps) < 0 ||
                 virCgroupGetBlkioDeviceWriteBps(cgroup, dev->path,
                                                 &dev->wbps) < 0))
152 153 154 155 156
                return -1;
        }
    }

    return 0;
157 158 159 160 161 162 163 164
}


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

165
    if (virCgroupSetMemory(cgroup, virDomainDefGetMemoryInitial(def)) < 0)
166 167
        goto cleanup;

168 169 170
    if (virMemoryLimitIsSet(def->mem.hard_limit))
        if (virCgroupSetMemoryHardLimit(cgroup, def->mem.hard_limit) < 0)
            goto cleanup;
171

172 173 174
    if (virMemoryLimitIsSet(def->mem.soft_limit))
        if (virCgroupSetMemorySoftLimit(cgroup, def->mem.soft_limit) < 0)
            goto cleanup;
175

176 177 178
    if (virMemoryLimitIsSet(def->mem.swap_hard_limit))
        if (virCgroupSetMemSwapHardLimit(cgroup, def->mem.swap_hard_limit) < 0)
            goto cleanup;
179 180

    ret = 0;
181
 cleanup:
182 183 184 185
    return ret;
}


186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
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);
207
    meminfo->memusage = (unsigned long long)memUsage;
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222

    return ret;
}


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


static int virLXCCgroupGetMemStat(virCgroupPtr cgroup,
                                  virLXCMeminfoPtr meminfo)
{
P
Pavel Hrdina 已提交
223 224 225 226 227 228 229
    return virCgroupGetMemoryStat(cgroup,
                                  &meminfo->cached,
                                  &meminfo->inactive_anon,
                                  &meminfo->active_anon,
                                  &meminfo->inactive_file,
                                  &meminfo->active_file,
                                  &meminfo->unevictable);
230 231 232 233 234
}


int virLXCCgroupGetMeminfo(virLXCMeminfoPtr meminfo)
{
235
    int ret = -1;
236 237
    virCgroupPtr cgroup;

238 239
    if (virCgroupNewSelf(&cgroup) < 0)
        return -1;
240

241
    if (virLXCCgroupGetMemStat(cgroup, meminfo) < 0)
242 243
        goto cleanup;

244
    if (virLXCCgroupGetMemTotal(cgroup, meminfo) < 0)
245 246
        goto cleanup;

247
    if (virLXCCgroupGetMemUsage(cgroup, meminfo) < 0)
248 249
        goto cleanup;

250 251 252 253 254
    if (virLXCCgroupGetMemSwapTotal(cgroup, meminfo) < 0)
        goto cleanup;

    if (virLXCCgroupGetMemSwapUsage(cgroup, meminfo) < 0)
        goto cleanup;
255 256

    ret = 0;
257
 cleanup:
258
    virCgroupFree(&cgroup);
259 260 261 262 263
    return ret;
}



264 265 266 267 268 269 270 271 272 273
typedef struct _virLXCCgroupDevicePolicy virLXCCgroupDevicePolicy;
typedef virLXCCgroupDevicePolicy *virLXCCgroupDevicePolicyPtr;

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


274
int
275
virLXCSetupHostUSBDeviceCgroup(virUSBDevicePtr dev ATTRIBUTE_UNUSED,
276 277 278 279 280 281
                               const char *path,
                               void *opaque)
{
    virCgroupPtr cgroup = opaque;

    VIR_DEBUG("Process path '%s' for USB device", path);
282
    if (virCgroupAllowDevicePath(cgroup, path,
283
                                 VIR_CGROUP_DEVICE_RWM, false) < 0)
284 285 286 287 288 289 290
        return -1;

    return 0;
}


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

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

    return 0;
}

305 306 307 308

static int virLXCCgroupSetupDeviceACL(virDomainDefPtr def,
                                      virCgroupPtr cgroup)
{
309
    int capMknod = def->caps_features[VIR_DOMAIN_CAPS_FEATURE_MKNOD];
310 311 312 313 314 315 316 317 318 319
    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 已提交
320
        {'c', LXC_DEV_MAJ_FUSE, LXC_DEV_MIN_FUSE},
321 322
        {0,   0, 0}};

323
    if (virCgroupDenyAllDevices(cgroup) < 0)
324 325
        goto cleanup;

326
    /* white list mknod if CAP_MKNOD has to be kept */
J
Ján Tomko 已提交
327
    if (capMknod == VIR_TRISTATE_SWITCH_ON) {
328 329 330 331 332
        if (virCgroupAllowAllDevices(cgroup,
                                    VIR_CGROUP_DEVICE_MKNOD) < 0)
            goto cleanup;
    }

333 334
    for (i = 0; devices[i].type != 0; i++) {
        virLXCCgroupDevicePolicyPtr dev = &devices[i];
335 336 337 338 339
        if (virCgroupAllowDevice(cgroup,
                                 dev->type,
                                 dev->major,
                                 dev->minor,
                                 VIR_CGROUP_DEVICE_RWM) < 0)
340 341 342
            goto cleanup;
    }

343
    VIR_DEBUG("Allowing any disk block devs");
344
    for (i = 0; i < def->ndisks; i++) {
345 346
        if (virStorageSourceIsEmpty(def->disks[i]->src) ||
            !virStorageSourceIsBlockLocal(def->disks[i]->src))
347 348
            continue;

349
        if (virCgroupAllowDevicePath(cgroup,
350
                                     virDomainDiskGetSource(def->disks[i]),
351
                                     (def->disks[i]->src->readonly ?
352 353
                                      VIR_CGROUP_DEVICE_READ :
                                      VIR_CGROUP_DEVICE_RW) |
354
                                     VIR_CGROUP_DEVICE_MKNOD, false) < 0)
355 356 357
            goto cleanup;
    }

358
    VIR_DEBUG("Allowing any filesystem block devs");
359
    for (i = 0; i < def->nfss; i++) {
360 361 362
        if (def->fss[i]->type != VIR_DOMAIN_FS_TYPE_BLOCK)
            continue;

363
        if (virCgroupAllowDevicePath(cgroup,
364
                                     def->fss[i]->src->path,
365 366
                                     def->fss[i]->readonly ?
                                     VIR_CGROUP_DEVICE_READ :
367
                                     VIR_CGROUP_DEVICE_RW, false) < 0)
368 369 370
            goto cleanup;
    }

371
    VIR_DEBUG("Allowing any hostdev block devs");
372 373
    for (i = 0; i < def->nhostdevs; i++) {
        virDomainHostdevDefPtr hostdev = def->hostdevs[i];
374
        virDomainHostdevSubsysUSBPtr usbsrc = &hostdev->source.subsys.u.usb;
375
        virUSBDevicePtr usb;
376

377 378 379 380 381 382 383
        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;

384
            if ((usb = virUSBDeviceNew(usbsrc->bus, usbsrc->device,
385
                                       NULL)) == NULL)
386 387
                goto cleanup;

388
            if (virUSBDeviceFileIterate(usb, virLXCSetupHostUSBDeviceCgroup,
389 390
                                        cgroup) < 0) {
                virUSBDeviceFree(usb);
391
                goto cleanup;
392
            }
393
            virUSBDeviceFree(usb);
394 395 396 397 398 399 400
            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 |
401
                                             VIR_CGROUP_DEVICE_MKNOD, false) < 0)
402 403
                    goto cleanup;
                break;
404 405 406 407
            case VIR_DOMAIN_HOSTDEV_CAPS_TYPE_MISC:
                if (virCgroupAllowDevicePath(cgroup,
                                             hostdev->source.caps.u.misc.chardev,
                                             VIR_CGROUP_DEVICE_RW |
408
                                             VIR_CGROUP_DEVICE_MKNOD, false) < 0)
409 410
                    goto cleanup;
                break;
411 412 413 414 415 416
            default:
                break;
            }
        default:
            break;
        }
417 418
    }

419 420
    if (virCgroupAllowDevice(cgroup, 'c', LXC_DEV_MAJ_PTY, -1,
                             VIR_CGROUP_DEVICE_RWM) < 0)
421 422
        goto cleanup;

423 424
    VIR_DEBUG("Device whitelist complete");

425
    ret = 0;
426
 cleanup:
427 428 429 430
    return ret;
}


431
virCgroupPtr virLXCCgroupCreate(virDomainDefPtr def,
432 433 434
                                pid_t initpid,
                                size_t nnicindexes,
                                int *nicindexes)
435
{
436
    virCgroupPtr cgroup = NULL;
437
    char *machineName = virLXCDomainGetMachineName(def, 0);
438 439 440

    if (!machineName)
        goto cleanup;
441

442 443 444 445 446
    if (def->resource->partition[0] != '/') {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Resource partition '%s' must start with '/'"),
                       def->resource->partition);
        goto cleanup;
447
    }
448

449
    if (virCgroupNewMachine(machineName,
450 451 452
                            "lxc",
                            def->uuid,
                            NULL,
453
                            initpid,
454
                            true,
455
                            nnicindexes, nicindexes,
456 457
                            def->resource->partition,
                            -1,
458
                            0,
459
                            &cgroup) < 0)
460
        goto cleanup;
461

462 463 464 465 466 467
    /* 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) {
468
            virCgroupFree(&cgroup);
469 470 471 472 473
            cgroup = NULL;
            goto cleanup;
        }
    }

474
 cleanup:
475 476
    VIR_FREE(machineName);

477 478 479 480 481
    return cgroup;
}


int virLXCCgroupSetup(virDomainDefPtr def,
482 483
                      virCgroupPtr cgroup,
                      virBitmapPtr nodemask)
484 485 486
{
    int ret = -1;

487 488 489
    if (virLXCCgroupSetupCpuTune(def, cgroup) < 0)
        goto cleanup;

490 491 492
    if (virLXCCgroupSetupCpusetTune(def, cgroup, nodemask) < 0)
        goto cleanup;

493 494 495 496 497 498 499 500 501
    if (virLXCCgroupSetupBlkioTune(def, cgroup) < 0)
        goto cleanup;

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

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

502 503
    ret = 0;

504
 cleanup:
505
    return ret;
506
}