lxc_cgroup.c 12.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 "domain_cgroup.h"
27
#include "virfile.h"
28
#include "virerror.h"
29
#include "virlog.h"
30
#include "viralloc.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
static int virLXCCgroupSetupCpuTune(virDomainDefPtr def,
                                    virCgroupPtr cgroup)
{
41 42
    if (def->cputune.sharesSpecified) {
        unsigned long long val;
43
        if (virCgroupSetupCpuShares(cgroup, def->cputune.shares, &val) < 0)
44
            return -1;
45 46
        def->cputune.shares = val;
    }
47 48 49

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

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

56
    return 0;
57 58 59
}


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

    if (def->placement_mode != VIR_DOMAIN_CPU_PLACEMENT_MODE_AUTO &&
69 70 71
        def->cpumask &&
        virCgroupSetupCpusetCpus(cgroup, def->cpumask) < 0) {
        return -1;
72 73
    }

74 75
    if (virDomainNumatuneGetMode(def->numa, -1, &mode) < 0 ||
        mode == VIR_DOMAIN_NUMATUNE_MEM_STRICT) {
76
        ret = 0;
77
        goto cleanup;
78
    }
79

80
    if (virDomainNumatuneMaybeFormatNodeset(def->numa, nodemask,
81
                                            &mask, -1) < 0)
82
        goto cleanup;
83

84 85
    if (mask && virCgroupSetCpusetMems(cgroup, mask) < 0)
        goto cleanup;
86

87
    ret = 0;
88
 cleanup:
89
    VIR_FREE(mask);
90
    return ret;
91 92 93
}


94 95 96
static int virLXCCgroupSetupBlkioTune(virDomainDefPtr def,
                                      virCgroupPtr cgroup)
{
97
    return virDomainCgroupSetupBlkio(cgroup, def->blkio);
98 99 100 101 102 103
}


static int virLXCCgroupSetupMemTune(virDomainDefPtr def,
                                    virCgroupPtr cgroup)
{
104
    if (virCgroupSetMemory(cgroup, virDomainDefGetMemoryInitial(def)) < 0)
105
        return -1;
106

107
    return virDomainCgroupSetupMemtune(cgroup, def->mem);
108 109 110
}


111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
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);
132
    meminfo->memusage = (unsigned long long)memUsage;
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147

    return ret;
}


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


static int virLXCCgroupGetMemStat(virCgroupPtr cgroup,
                                  virLXCMeminfoPtr meminfo)
{
P
Pavel Hrdina 已提交
148 149 150 151 152 153 154
    return virCgroupGetMemoryStat(cgroup,
                                  &meminfo->cached,
                                  &meminfo->inactive_anon,
                                  &meminfo->active_anon,
                                  &meminfo->inactive_file,
                                  &meminfo->active_file,
                                  &meminfo->unevictable);
155 156 157 158 159
}


int virLXCCgroupGetMeminfo(virLXCMeminfoPtr meminfo)
{
160
    int ret = -1;
161 162
    virCgroupPtr cgroup;

163 164
    if (virCgroupNewSelf(&cgroup) < 0)
        return -1;
165

166
    if (virLXCCgroupGetMemStat(cgroup, meminfo) < 0)
167 168
        goto cleanup;

169
    if (virLXCCgroupGetMemTotal(cgroup, meminfo) < 0)
170 171
        goto cleanup;

172
    if (virLXCCgroupGetMemUsage(cgroup, meminfo) < 0)
173 174
        goto cleanup;

175 176 177 178 179
    if (virLXCCgroupGetMemSwapTotal(cgroup, meminfo) < 0)
        goto cleanup;

    if (virLXCCgroupGetMemSwapUsage(cgroup, meminfo) < 0)
        goto cleanup;
180 181

    ret = 0;
182
 cleanup:
183
    virCgroupFree(&cgroup);
184 185 186 187 188
    return ret;
}



189 190 191 192 193 194 195 196 197 198
typedef struct _virLXCCgroupDevicePolicy virLXCCgroupDevicePolicy;
typedef virLXCCgroupDevicePolicy *virLXCCgroupDevicePolicyPtr;

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


199
int
J
Ján Tomko 已提交
200
virLXCSetupHostUSBDeviceCgroup(virUSBDevicePtr dev G_GNUC_UNUSED,
201 202 203 204 205 206
                               const char *path,
                               void *opaque)
{
    virCgroupPtr cgroup = opaque;

    VIR_DEBUG("Process path '%s' for USB device", path);
207
    if (virCgroupAllowDevicePath(cgroup, path,
208
                                 VIR_CGROUP_DEVICE_RWM, false) < 0)
209 210 211 212 213 214 215
        return -1;

    return 0;
}


int
J
Ján Tomko 已提交
216
virLXCTeardownHostUSBDeviceCgroup(virUSBDevicePtr dev G_GNUC_UNUSED,
217 218 219 220 221 222
                                  const char *path,
                                  void *opaque)
{
    virCgroupPtr cgroup = opaque;

    VIR_DEBUG("Process path '%s' for USB device", path);
223
    if (virCgroupDenyDevicePath(cgroup, path,
224
                                VIR_CGROUP_DEVICE_RWM, false) < 0)
225 226 227 228 229
        return -1;

    return 0;
}

230 231 232 233

static int virLXCCgroupSetupDeviceACL(virDomainDefPtr def,
                                      virCgroupPtr cgroup)
{
234
    int capMknod = def->caps_features[VIR_DOMAIN_PROCES_CAPS_FEATURE_MKNOD];
235 236 237 238 239 240 241 242 243
    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 已提交
244
        {'c', LXC_DEV_MAJ_FUSE, LXC_DEV_MIN_FUSE},
245 246
        {0,   0, 0}};

247
    if (virCgroupDenyAllDevices(cgroup) < 0)
248
        return -1;
249

250
    /* white list mknod if CAP_MKNOD has to be kept */
J
Ján Tomko 已提交
251
    if (capMknod == VIR_TRISTATE_SWITCH_ON) {
252 253
        if (virCgroupAllowAllDevices(cgroup,
                                    VIR_CGROUP_DEVICE_MKNOD) < 0)
254
            return -1;
255 256
    }

257 258
    for (i = 0; devices[i].type != 0; i++) {
        virLXCCgroupDevicePolicyPtr dev = &devices[i];
259 260 261 262 263
        if (virCgroupAllowDevice(cgroup,
                                 dev->type,
                                 dev->major,
                                 dev->minor,
                                 VIR_CGROUP_DEVICE_RWM) < 0)
264
            return -1;
265 266
    }

267
    VIR_DEBUG("Allowing any disk block devs");
268
    for (i = 0; i < def->ndisks; i++) {
269 270
        if (virStorageSourceIsEmpty(def->disks[i]->src) ||
            !virStorageSourceIsBlockLocal(def->disks[i]->src))
271 272
            continue;

273
        if (virCgroupAllowDevicePath(cgroup,
274
                                     virDomainDiskGetSource(def->disks[i]),
275
                                     (def->disks[i]->src->readonly ?
276 277
                                      VIR_CGROUP_DEVICE_READ :
                                      VIR_CGROUP_DEVICE_RW) |
278
                                     VIR_CGROUP_DEVICE_MKNOD, false) < 0)
279
            return -1;
280 281
    }

282
    VIR_DEBUG("Allowing any filesystem block devs");
283
    for (i = 0; i < def->nfss; i++) {
284 285 286
        if (def->fss[i]->type != VIR_DOMAIN_FS_TYPE_BLOCK)
            continue;

287
        if (virCgroupAllowDevicePath(cgroup,
288
                                     def->fss[i]->src->path,
289 290
                                     def->fss[i]->readonly ?
                                     VIR_CGROUP_DEVICE_READ :
291
                                     VIR_CGROUP_DEVICE_RW, false) < 0)
292
            return -1;
293 294
    }

295
    VIR_DEBUG("Allowing any hostdev block devs");
296 297
    for (i = 0; i < def->nhostdevs; i++) {
        virDomainHostdevDefPtr hostdev = def->hostdevs[i];
298
        virDomainHostdevSubsysUSBPtr usbsrc = &hostdev->source.subsys.u.usb;
299
        virUSBDevicePtr usb;
300

301 302 303 304 305 306 307
        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;

308
            if ((usb = virUSBDeviceNew(usbsrc->bus, usbsrc->device,
309
                                       NULL)) == NULL)
310
                return -1;
311

312
            if (virUSBDeviceFileIterate(usb, virLXCSetupHostUSBDeviceCgroup,
313 314
                                        cgroup) < 0) {
                virUSBDeviceFree(usb);
315
                return -1;
316
            }
317
            virUSBDeviceFree(usb);
318 319 320 321 322 323 324
            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 |
325
                                             VIR_CGROUP_DEVICE_MKNOD, false) < 0)
326
                    return -1;
327
                break;
328 329 330 331
            case VIR_DOMAIN_HOSTDEV_CAPS_TYPE_MISC:
                if (virCgroupAllowDevicePath(cgroup,
                                             hostdev->source.caps.u.misc.chardev,
                                             VIR_CGROUP_DEVICE_RW |
332
                                             VIR_CGROUP_DEVICE_MKNOD, false) < 0)
333
                    return -1;
334
                break;
335 336 337 338 339 340
            default:
                break;
            }
        default:
            break;
        }
341 342
    }

343 344
    if (virCgroupAllowDevice(cgroup, 'c', LXC_DEV_MAJ_PTY, -1,
                             VIR_CGROUP_DEVICE_RWM) < 0)
345
        return -1;
346

347 348
    VIR_DEBUG("Device whitelist complete");

349
    return 0;
350 351 352
}


353
virCgroupPtr virLXCCgroupCreate(virDomainDefPtr def,
354 355 356
                                pid_t initpid,
                                size_t nnicindexes,
                                int *nicindexes)
357
{
358
    virCgroupPtr cgroup = NULL;
359
    char *machineName = virLXCDomainGetMachineName(def, 0);
360 361 362

    if (!machineName)
        goto cleanup;
363

364 365 366 367 368
    if (def->resource->partition[0] != '/') {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Resource partition '%s' must start with '/'"),
                       def->resource->partition);
        goto cleanup;
369
    }
370

371
    if (virCgroupNewMachine(machineName,
372 373 374
                            "lxc",
                            def->uuid,
                            NULL,
375
                            initpid,
376
                            true,
377
                            nnicindexes, nicindexes,
378 379
                            def->resource->partition,
                            -1,
380
                            0,
381
                            &cgroup) < 0)
382
        goto cleanup;
383

384 385 386 387 388 389
    /* 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) {
390
            virCgroupFree(&cgroup);
391 392 393 394 395
            cgroup = NULL;
            goto cleanup;
        }
    }

396
 cleanup:
397 398
    VIR_FREE(machineName);

399 400 401 402 403
    return cgroup;
}


int virLXCCgroupSetup(virDomainDefPtr def,
404 405
                      virCgroupPtr cgroup,
                      virBitmapPtr nodemask)
406
{
407
    if (virLXCCgroupSetupCpuTune(def, cgroup) < 0)
408
        return -1;
409

410
    if (virLXCCgroupSetupCpusetTune(def, cgroup, nodemask) < 0)
411
        return -1;
412

413
    if (virLXCCgroupSetupBlkioTune(def, cgroup) < 0)
414
        return -1;
415 416

    if (virLXCCgroupSetupMemTune(def, cgroup) < 0)
417
        return -1;
418 419

    if (virLXCCgroupSetupDeviceACL(def, cgroup) < 0)
420
        return -1;
421

422
    return 0;
423
}