lxc_cgroup.c 14.3 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 "virstring.h"
31
#include "virsystemd.h"
32 33 34

#define VIR_FROM_THIS VIR_FROM_LXC

35 36
VIR_LOG_INIT("lxc.lxc_cgroup");

37 38 39
static int virLXCCgroupSetupCpuTune(virDomainDefPtr def,
                                    virCgroupPtr cgroup)
{
40 41 42
    if (def->cputune.sharesSpecified) {
        unsigned long long val;
        if (virCgroupSetCpuShares(cgroup, def->cputune.shares) < 0)
43
            return -1;
44 45

        if (virCgroupGetCpuShares(cgroup, &val) < 0)
46
            return -1;
47 48
        def->cputune.shares = val;
    }
49 50 51

    if (def->cputune.quota != 0 &&
        virCgroupSetCpuCfsQuota(cgroup, def->cputune.quota) < 0)
52
        return -1;
53 54 55

    if (def->cputune.period != 0 &&
        virCgroupSetCpuCfsPeriod(cgroup, def->cputune.period) < 0)
56
        return -1;
57

58
    return 0;
59 60 61
}


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

    if (def->placement_mode != VIR_DOMAIN_CPU_PLACEMENT_MODE_AUTO &&
        def->cpumask) {
72
        if (!(mask = virBitmapFormat(def->cpumask)))
73 74
            return -1;

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

81 82
    if (virDomainNumatuneGetMode(def->numa, -1, &mode) < 0 ||
        mode == VIR_DOMAIN_NUMATUNE_MEM_STRICT) {
83
        ret = 0;
84
        goto cleanup;
85
    }
86

87
    if (virDomainNumatuneMaybeFormatNodeset(def->numa, nodemask,
88
                                            &mask, -1) < 0)
89
        goto cleanup;
90

91 92
    if (mask && virCgroupSetCpusetMems(cgroup, mask) < 0)
        goto cleanup;
93

94
    ret = 0;
95
 cleanup:
96
    VIR_FREE(mask);
97
    return ret;
98 99 100
}


101 102 103
static int virLXCCgroupSetupBlkioTune(virDomainDefPtr def,
                                      virCgroupPtr cgroup)
{
104
    size_t i;
105 106 107 108

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

110 111
    if (def->blkio.ndevices) {
        for (i = 0; i < def->blkio.ndevices; i++) {
112
            virBlkioDevicePtr dev = &def->blkio.devices[i];
113 114

            if (dev->weight &&
115 116
                virCgroupSetupBlkioDeviceWeight(cgroup, dev->path,
                                                &dev->weight) < 0)
117 118 119
                return -1;

            if (dev->riops &&
120 121
                virCgroupSetupBlkioDeviceReadIops(cgroup, dev->path,
                                                  &dev->riops) < 0)
122 123 124
                return -1;

            if (dev->wiops &&
125 126
                virCgroupSetupBlkioDeviceWriteIops(cgroup, dev->path,
                                                   &dev->wiops) < 0)
127 128 129
                return -1;

            if (dev->rbps &&
130 131
                virCgroupSetupBlkioDeviceReadBps(cgroup, dev->path,
                                                 &dev->rbps) < 0)
132 133 134
                return -1;

            if (dev->wbps &&
135 136
                virCgroupSetupBlkioDeviceWriteBps(cgroup, dev->path,
                                                  &dev->wbps) < 0)
137 138 139 140 141
                return -1;
        }
    }

    return 0;
142 143 144 145 146 147
}


static int virLXCCgroupSetupMemTune(virDomainDefPtr def,
                                    virCgroupPtr cgroup)
{
148
    if (virCgroupSetMemory(cgroup, virDomainDefGetMemoryInitial(def)) < 0)
149
        return -1;
150

151 152
    if (virMemoryLimitIsSet(def->mem.hard_limit))
        if (virCgroupSetMemoryHardLimit(cgroup, def->mem.hard_limit) < 0)
153
            return -1;
154

155 156
    if (virMemoryLimitIsSet(def->mem.soft_limit))
        if (virCgroupSetMemorySoftLimit(cgroup, def->mem.soft_limit) < 0)
157
            return -1;
158

159 160
    if (virMemoryLimitIsSet(def->mem.swap_hard_limit))
        if (virCgroupSetMemSwapHardLimit(cgroup, def->mem.swap_hard_limit) < 0)
161
            return -1;
162

163
    return 0;
164 165 166
}


167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
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);
188
    meminfo->memusage = (unsigned long long)memUsage;
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

    return ret;
}


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


static int virLXCCgroupGetMemStat(virCgroupPtr cgroup,
                                  virLXCMeminfoPtr meminfo)
{
P
Pavel Hrdina 已提交
204 205 206 207 208 209 210
    return virCgroupGetMemoryStat(cgroup,
                                  &meminfo->cached,
                                  &meminfo->inactive_anon,
                                  &meminfo->active_anon,
                                  &meminfo->inactive_file,
                                  &meminfo->active_file,
                                  &meminfo->unevictable);
211 212 213 214 215
}


int virLXCCgroupGetMeminfo(virLXCMeminfoPtr meminfo)
{
216
    int ret = -1;
217 218
    virCgroupPtr cgroup;

219 220
    if (virCgroupNewSelf(&cgroup) < 0)
        return -1;
221

222
    if (virLXCCgroupGetMemStat(cgroup, meminfo) < 0)
223 224
        goto cleanup;

225
    if (virLXCCgroupGetMemTotal(cgroup, meminfo) < 0)
226 227
        goto cleanup;

228
    if (virLXCCgroupGetMemUsage(cgroup, meminfo) < 0)
229 230
        goto cleanup;

231 232 233 234 235
    if (virLXCCgroupGetMemSwapTotal(cgroup, meminfo) < 0)
        goto cleanup;

    if (virLXCCgroupGetMemSwapUsage(cgroup, meminfo) < 0)
        goto cleanup;
236 237

    ret = 0;
238
 cleanup:
239
    virCgroupFree(&cgroup);
240 241 242 243 244
    return ret;
}



245 246 247 248 249 250 251 252 253 254
typedef struct _virLXCCgroupDevicePolicy virLXCCgroupDevicePolicy;
typedef virLXCCgroupDevicePolicy *virLXCCgroupDevicePolicyPtr;

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


255
int
J
Ján Tomko 已提交
256
virLXCSetupHostUSBDeviceCgroup(virUSBDevicePtr dev G_GNUC_UNUSED,
257 258 259 260 261 262
                               const char *path,
                               void *opaque)
{
    virCgroupPtr cgroup = opaque;

    VIR_DEBUG("Process path '%s' for USB device", path);
263
    if (virCgroupAllowDevicePath(cgroup, path,
264
                                 VIR_CGROUP_DEVICE_RWM, false) < 0)
265 266 267 268 269 270 271
        return -1;

    return 0;
}


int
J
Ján Tomko 已提交
272
virLXCTeardownHostUSBDeviceCgroup(virUSBDevicePtr dev G_GNUC_UNUSED,
273 274 275 276 277 278
                                  const char *path,
                                  void *opaque)
{
    virCgroupPtr cgroup = opaque;

    VIR_DEBUG("Process path '%s' for USB device", path);
279
    if (virCgroupDenyDevicePath(cgroup, path,
280
                                VIR_CGROUP_DEVICE_RWM, false) < 0)
281 282 283 284 285
        return -1;

    return 0;
}

286 287 288 289

static int virLXCCgroupSetupDeviceACL(virDomainDefPtr def,
                                      virCgroupPtr cgroup)
{
290
    int capMknod = def->caps_features[VIR_DOMAIN_PROCES_CAPS_FEATURE_MKNOD];
291 292 293 294 295 296 297 298 299
    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 已提交
300
        {'c', LXC_DEV_MAJ_FUSE, LXC_DEV_MIN_FUSE},
301 302
        {0,   0, 0}};

303
    if (virCgroupDenyAllDevices(cgroup) < 0)
304
        return -1;
305

306
    /* white list mknod if CAP_MKNOD has to be kept */
J
Ján Tomko 已提交
307
    if (capMknod == VIR_TRISTATE_SWITCH_ON) {
308 309
        if (virCgroupAllowAllDevices(cgroup,
                                    VIR_CGROUP_DEVICE_MKNOD) < 0)
310
            return -1;
311 312
    }

313 314
    for (i = 0; devices[i].type != 0; i++) {
        virLXCCgroupDevicePolicyPtr dev = &devices[i];
315 316 317 318 319
        if (virCgroupAllowDevice(cgroup,
                                 dev->type,
                                 dev->major,
                                 dev->minor,
                                 VIR_CGROUP_DEVICE_RWM) < 0)
320
            return -1;
321 322
    }

323
    VIR_DEBUG("Allowing any disk block devs");
324
    for (i = 0; i < def->ndisks; i++) {
325 326
        if (virStorageSourceIsEmpty(def->disks[i]->src) ||
            !virStorageSourceIsBlockLocal(def->disks[i]->src))
327 328
            continue;

329
        if (virCgroupAllowDevicePath(cgroup,
330
                                     virDomainDiskGetSource(def->disks[i]),
331
                                     (def->disks[i]->src->readonly ?
332 333
                                      VIR_CGROUP_DEVICE_READ :
                                      VIR_CGROUP_DEVICE_RW) |
334
                                     VIR_CGROUP_DEVICE_MKNOD, false) < 0)
335
            return -1;
336 337
    }

338
    VIR_DEBUG("Allowing any filesystem block devs");
339
    for (i = 0; i < def->nfss; i++) {
340 341 342
        if (def->fss[i]->type != VIR_DOMAIN_FS_TYPE_BLOCK)
            continue;

343
        if (virCgroupAllowDevicePath(cgroup,
344
                                     def->fss[i]->src->path,
345 346
                                     def->fss[i]->readonly ?
                                     VIR_CGROUP_DEVICE_READ :
347
                                     VIR_CGROUP_DEVICE_RW, false) < 0)
348
            return -1;
349 350
    }

351
    VIR_DEBUG("Allowing any hostdev block devs");
352 353
    for (i = 0; i < def->nhostdevs; i++) {
        virDomainHostdevDefPtr hostdev = def->hostdevs[i];
354
        virDomainHostdevSubsysUSBPtr usbsrc = &hostdev->source.subsys.u.usb;
355
        virUSBDevicePtr usb;
356

357 358 359 360 361 362 363
        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;

364
            if ((usb = virUSBDeviceNew(usbsrc->bus, usbsrc->device,
365
                                       NULL)) == NULL)
366
                return -1;
367

368
            if (virUSBDeviceFileIterate(usb, virLXCSetupHostUSBDeviceCgroup,
369 370
                                        cgroup) < 0) {
                virUSBDeviceFree(usb);
371
                return -1;
372
            }
373
            virUSBDeviceFree(usb);
374 375 376 377 378 379 380
            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 |
381
                                             VIR_CGROUP_DEVICE_MKNOD, false) < 0)
382
                    return -1;
383
                break;
384 385 386 387
            case VIR_DOMAIN_HOSTDEV_CAPS_TYPE_MISC:
                if (virCgroupAllowDevicePath(cgroup,
                                             hostdev->source.caps.u.misc.chardev,
                                             VIR_CGROUP_DEVICE_RW |
388
                                             VIR_CGROUP_DEVICE_MKNOD, false) < 0)
389
                    return -1;
390
                break;
391 392 393 394 395 396
            default:
                break;
            }
        default:
            break;
        }
397 398
    }

399 400
    if (virCgroupAllowDevice(cgroup, 'c', LXC_DEV_MAJ_PTY, -1,
                             VIR_CGROUP_DEVICE_RWM) < 0)
401
        return -1;
402

403 404
    VIR_DEBUG("Device whitelist complete");

405
    return 0;
406 407 408
}


409
virCgroupPtr virLXCCgroupCreate(virDomainDefPtr def,
410 411 412
                                pid_t initpid,
                                size_t nnicindexes,
                                int *nicindexes)
413
{
414
    virCgroupPtr cgroup = NULL;
415
    char *machineName = virLXCDomainGetMachineName(def, 0);
416 417 418

    if (!machineName)
        goto cleanup;
419

420 421 422 423 424
    if (def->resource->partition[0] != '/') {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Resource partition '%s' must start with '/'"),
                       def->resource->partition);
        goto cleanup;
425
    }
426

427
    if (virCgroupNewMachine(machineName,
428 429 430
                            "lxc",
                            def->uuid,
                            NULL,
431
                            initpid,
432
                            true,
433
                            nnicindexes, nicindexes,
434 435
                            def->resource->partition,
                            -1,
436
                            0,
437
                            &cgroup) < 0)
438
        goto cleanup;
439

440 441 442 443 444 445
    /* 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) {
446
            virCgroupFree(&cgroup);
447 448 449 450 451
            cgroup = NULL;
            goto cleanup;
        }
    }

452
 cleanup:
453 454
    VIR_FREE(machineName);

455 456 457 458 459
    return cgroup;
}


int virLXCCgroupSetup(virDomainDefPtr def,
460 461
                      virCgroupPtr cgroup,
                      virBitmapPtr nodemask)
462
{
463
    if (virLXCCgroupSetupCpuTune(def, cgroup) < 0)
464
        return -1;
465

466
    if (virLXCCgroupSetupCpusetTune(def, cgroup, nodemask) < 0)
467
        return -1;
468

469
    if (virLXCCgroupSetupBlkioTune(def, cgroup) < 0)
470
        return -1;
471 472

    if (virLXCCgroupSetupMemTune(def, cgroup) < 0)
473
        return -1;
474 475

    if (virLXCCgroupSetupDeviceACL(def, cgroup) < 0)
476
        return -1;
477

478
    return 0;
479
}