lxc_cgroup.c 12.7 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 43
    if (def->cputune.sharesSpecified) {
        unsigned long long val;
        if (virCgroupSetCpuShares(cgroup, def->cputune.shares) < 0)
44
            return -1;
45 46

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

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

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

59
    return 0;
60 61 62
}


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

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

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

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

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

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

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


102 103 104
static int virLXCCgroupSetupBlkioTune(virDomainDefPtr def,
                                      virCgroupPtr cgroup)
{
105
    return virDomainCgroupSetupBlkio(cgroup, def->blkio);
106 107 108 109 110 111
}


static int virLXCCgroupSetupMemTune(virDomainDefPtr def,
                                    virCgroupPtr cgroup)
{
112
    if (virCgroupSetMemory(cgroup, virDomainDefGetMemoryInitial(def)) < 0)
113
        return -1;
114

115
    return virDomainCgroupSetupMemtune(cgroup, def->mem);
116 117 118
}


119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
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);
140
    meminfo->memusage = (unsigned long long)memUsage;
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

    return ret;
}


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


static int virLXCCgroupGetMemStat(virCgroupPtr cgroup,
                                  virLXCMeminfoPtr meminfo)
{
P
Pavel Hrdina 已提交
156 157 158 159 160 161 162
    return virCgroupGetMemoryStat(cgroup,
                                  &meminfo->cached,
                                  &meminfo->inactive_anon,
                                  &meminfo->active_anon,
                                  &meminfo->inactive_file,
                                  &meminfo->active_file,
                                  &meminfo->unevictable);
163 164 165 166 167
}


int virLXCCgroupGetMeminfo(virLXCMeminfoPtr meminfo)
{
168
    int ret = -1;
169 170
    virCgroupPtr cgroup;

171 172
    if (virCgroupNewSelf(&cgroup) < 0)
        return -1;
173

174
    if (virLXCCgroupGetMemStat(cgroup, meminfo) < 0)
175 176
        goto cleanup;

177
    if (virLXCCgroupGetMemTotal(cgroup, meminfo) < 0)
178 179
        goto cleanup;

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

183 184 185 186 187
    if (virLXCCgroupGetMemSwapTotal(cgroup, meminfo) < 0)
        goto cleanup;

    if (virLXCCgroupGetMemSwapUsage(cgroup, meminfo) < 0)
        goto cleanup;
188 189

    ret = 0;
190
 cleanup:
191
    virCgroupFree(&cgroup);
192 193 194 195 196
    return ret;
}



197 198 199 200 201 202 203 204 205 206
typedef struct _virLXCCgroupDevicePolicy virLXCCgroupDevicePolicy;
typedef virLXCCgroupDevicePolicy *virLXCCgroupDevicePolicyPtr;

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


207
int
J
Ján Tomko 已提交
208
virLXCSetupHostUSBDeviceCgroup(virUSBDevicePtr dev G_GNUC_UNUSED,
209 210 211 212 213 214
                               const char *path,
                               void *opaque)
{
    virCgroupPtr cgroup = opaque;

    VIR_DEBUG("Process path '%s' for USB device", path);
215
    if (virCgroupAllowDevicePath(cgroup, path,
216
                                 VIR_CGROUP_DEVICE_RWM, false) < 0)
217 218 219 220 221 222 223
        return -1;

    return 0;
}


int
J
Ján Tomko 已提交
224
virLXCTeardownHostUSBDeviceCgroup(virUSBDevicePtr dev G_GNUC_UNUSED,
225 226 227 228 229 230
                                  const char *path,
                                  void *opaque)
{
    virCgroupPtr cgroup = opaque;

    VIR_DEBUG("Process path '%s' for USB device", path);
231
    if (virCgroupDenyDevicePath(cgroup, path,
232
                                VIR_CGROUP_DEVICE_RWM, false) < 0)
233 234 235 236 237
        return -1;

    return 0;
}

238 239 240 241

static int virLXCCgroupSetupDeviceACL(virDomainDefPtr def,
                                      virCgroupPtr cgroup)
{
242
    int capMknod = def->caps_features[VIR_DOMAIN_PROCES_CAPS_FEATURE_MKNOD];
243 244 245 246 247 248 249 250 251
    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 已提交
252
        {'c', LXC_DEV_MAJ_FUSE, LXC_DEV_MIN_FUSE},
253 254
        {0,   0, 0}};

255
    if (virCgroupDenyAllDevices(cgroup) < 0)
256
        return -1;
257

258
    /* white list mknod if CAP_MKNOD has to be kept */
J
Ján Tomko 已提交
259
    if (capMknod == VIR_TRISTATE_SWITCH_ON) {
260 261
        if (virCgroupAllowAllDevices(cgroup,
                                    VIR_CGROUP_DEVICE_MKNOD) < 0)
262
            return -1;
263 264
    }

265 266
    for (i = 0; devices[i].type != 0; i++) {
        virLXCCgroupDevicePolicyPtr dev = &devices[i];
267 268 269 270 271
        if (virCgroupAllowDevice(cgroup,
                                 dev->type,
                                 dev->major,
                                 dev->minor,
                                 VIR_CGROUP_DEVICE_RWM) < 0)
272
            return -1;
273 274
    }

275
    VIR_DEBUG("Allowing any disk block devs");
276
    for (i = 0; i < def->ndisks; i++) {
277 278
        if (virStorageSourceIsEmpty(def->disks[i]->src) ||
            !virStorageSourceIsBlockLocal(def->disks[i]->src))
279 280
            continue;

281
        if (virCgroupAllowDevicePath(cgroup,
282
                                     virDomainDiskGetSource(def->disks[i]),
283
                                     (def->disks[i]->src->readonly ?
284 285
                                      VIR_CGROUP_DEVICE_READ :
                                      VIR_CGROUP_DEVICE_RW) |
286
                                     VIR_CGROUP_DEVICE_MKNOD, false) < 0)
287
            return -1;
288 289
    }

290
    VIR_DEBUG("Allowing any filesystem block devs");
291
    for (i = 0; i < def->nfss; i++) {
292 293 294
        if (def->fss[i]->type != VIR_DOMAIN_FS_TYPE_BLOCK)
            continue;

295
        if (virCgroupAllowDevicePath(cgroup,
296
                                     def->fss[i]->src->path,
297 298
                                     def->fss[i]->readonly ?
                                     VIR_CGROUP_DEVICE_READ :
299
                                     VIR_CGROUP_DEVICE_RW, false) < 0)
300
            return -1;
301 302
    }

303
    VIR_DEBUG("Allowing any hostdev block devs");
304 305
    for (i = 0; i < def->nhostdevs; i++) {
        virDomainHostdevDefPtr hostdev = def->hostdevs[i];
306
        virDomainHostdevSubsysUSBPtr usbsrc = &hostdev->source.subsys.u.usb;
307
        virUSBDevicePtr usb;
308

309 310 311 312 313 314 315
        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;

316
            if ((usb = virUSBDeviceNew(usbsrc->bus, usbsrc->device,
317
                                       NULL)) == NULL)
318
                return -1;
319

320
            if (virUSBDeviceFileIterate(usb, virLXCSetupHostUSBDeviceCgroup,
321 322
                                        cgroup) < 0) {
                virUSBDeviceFree(usb);
323
                return -1;
324
            }
325
            virUSBDeviceFree(usb);
326 327 328 329 330 331 332
            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 |
333
                                             VIR_CGROUP_DEVICE_MKNOD, false) < 0)
334
                    return -1;
335
                break;
336 337 338 339
            case VIR_DOMAIN_HOSTDEV_CAPS_TYPE_MISC:
                if (virCgroupAllowDevicePath(cgroup,
                                             hostdev->source.caps.u.misc.chardev,
                                             VIR_CGROUP_DEVICE_RW |
340
                                             VIR_CGROUP_DEVICE_MKNOD, false) < 0)
341
                    return -1;
342
                break;
343 344 345 346 347 348
            default:
                break;
            }
        default:
            break;
        }
349 350
    }

351 352
    if (virCgroupAllowDevice(cgroup, 'c', LXC_DEV_MAJ_PTY, -1,
                             VIR_CGROUP_DEVICE_RWM) < 0)
353
        return -1;
354

355 356
    VIR_DEBUG("Device whitelist complete");

357
    return 0;
358 359 360
}


361
virCgroupPtr virLXCCgroupCreate(virDomainDefPtr def,
362 363 364
                                pid_t initpid,
                                size_t nnicindexes,
                                int *nicindexes)
365
{
366
    virCgroupPtr cgroup = NULL;
367
    char *machineName = virLXCDomainGetMachineName(def, 0);
368 369 370

    if (!machineName)
        goto cleanup;
371

372 373 374 375 376
    if (def->resource->partition[0] != '/') {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Resource partition '%s' must start with '/'"),
                       def->resource->partition);
        goto cleanup;
377
    }
378

379
    if (virCgroupNewMachine(machineName,
380 381 382
                            "lxc",
                            def->uuid,
                            NULL,
383
                            initpid,
384
                            true,
385
                            nnicindexes, nicindexes,
386 387
                            def->resource->partition,
                            -1,
388
                            0,
389
                            &cgroup) < 0)
390
        goto cleanup;
391

392 393 394 395 396 397
    /* 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) {
398
            virCgroupFree(&cgroup);
399 400 401 402 403
            cgroup = NULL;
            goto cleanup;
        }
    }

404
 cleanup:
405 406
    VIR_FREE(machineName);

407 408 409 410 411
    return cgroup;
}


int virLXCCgroupSetup(virDomainDefPtr def,
412 413
                      virCgroupPtr cgroup,
                      virBitmapPtr nodemask)
414
{
415
    if (virLXCCgroupSetupCpuTune(def, cgroup) < 0)
416
        return -1;
417

418
    if (virLXCCgroupSetupCpusetTune(def, cgroup, nodemask) < 0)
419
        return -1;
420

421
    if (virLXCCgroupSetupBlkioTune(def, cgroup) < 0)
422
        return -1;
423 424

    if (virLXCCgroupSetupMemTune(def, cgroup) < 0)
425
        return -1;
426 427

    if (virLXCCgroupSetupDeviceACL(def, cgroup) < 0)
428
        return -1;
429

430
    return 0;
431
}