vircgrouptest.c 32.9 KB
Newer Older
1
/*
2
 * Copyright (C) 2013-2015 Red Hat, Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 * 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
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

#include <config.h>

#include "testutils.h"

#ifdef __linux__


# define __VIR_CGROUP_ALLOW_INCLUDE_PRIV_H__
# include "vircgrouppriv.h"
30
# include "virstring.h"
31 32 33
# include "virerror.h"
# include "virlog.h"
# include "virfile.h"
34
# include "virbuffer.h"
35
# include "testutilslxc.h"
36
# include "virhostcpu.h"
37 38 39

# define VIR_FROM_THIS VIR_FROM_NONE

40 41
VIR_LOG_INIT("tests.cgrouptest");

42 43 44
static int validateCgroup(virCgroupPtr cgroup,
                          const char *expectPath,
                          const char **expectMountPoint,
45
                          const char **expectLinkPoint,
46 47
                          const char **expectPlacement)
{
48
    size_t i;
49 50 51 52 53 54 55

    if (STRNEQ(cgroup->path, expectPath)) {
        fprintf(stderr, "Wrong path '%s', expected '%s'\n",
                cgroup->path, expectPath);
        return -1;
    }

56
    for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
57
        if (STRNEQ_NULLABLE(expectMountPoint[i],
58
                            cgroup->legacy[i].mountPoint)) {
59
            fprintf(stderr, "Wrong mount '%s', expected '%s' for '%s'\n",
60
                    cgroup->legacy[i].mountPoint,
61 62 63 64
                    expectMountPoint[i],
                    virCgroupControllerTypeToString(i));
            return -1;
        }
65
        if (STRNEQ_NULLABLE(expectLinkPoint[i],
66
                            cgroup->legacy[i].linkPoint)) {
67
            fprintf(stderr, "Wrong link '%s', expected '%s' for '%s'\n",
68
                    cgroup->legacy[i].linkPoint,
69 70 71 72
                    expectLinkPoint[i],
                    virCgroupControllerTypeToString(i));
            return -1;
        }
73
        if (STRNEQ_NULLABLE(expectPlacement[i],
74
                            cgroup->legacy[i].placement)) {
75
            fprintf(stderr, "Wrong placement '%s', expected '%s' for '%s'\n",
76
                    cgroup->legacy[i].placement,
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
                    expectPlacement[i],
                    virCgroupControllerTypeToString(i));
            return -1;
        }
    }

    return 0;
}

const char *mountsSmall[VIR_CGROUP_CONTROLLER_LAST] = {
    [VIR_CGROUP_CONTROLLER_CPU] = "/not/really/sys/fs/cgroup/cpu,cpuacct",
    [VIR_CGROUP_CONTROLLER_CPUACCT] = "/not/really/sys/fs/cgroup/cpu,cpuacct",
    [VIR_CGROUP_CONTROLLER_CPUSET] = NULL,
    [VIR_CGROUP_CONTROLLER_MEMORY] = "/not/really/sys/fs/cgroup/memory",
    [VIR_CGROUP_CONTROLLER_DEVICES] = NULL,
    [VIR_CGROUP_CONTROLLER_FREEZER] = NULL,
    [VIR_CGROUP_CONTROLLER_BLKIO] = NULL,
94
    [VIR_CGROUP_CONTROLLER_SYSTEMD] = NULL,
95 96 97 98 99 100 101 102 103
};
const char *mountsFull[VIR_CGROUP_CONTROLLER_LAST] = {
    [VIR_CGROUP_CONTROLLER_CPU] = "/not/really/sys/fs/cgroup/cpu,cpuacct",
    [VIR_CGROUP_CONTROLLER_CPUACCT] = "/not/really/sys/fs/cgroup/cpu,cpuacct",
    [VIR_CGROUP_CONTROLLER_CPUSET] = "/not/really/sys/fs/cgroup/cpuset",
    [VIR_CGROUP_CONTROLLER_MEMORY] = "/not/really/sys/fs/cgroup/memory",
    [VIR_CGROUP_CONTROLLER_DEVICES] = NULL,
    [VIR_CGROUP_CONTROLLER_FREEZER] = "/not/really/sys/fs/cgroup/freezer",
    [VIR_CGROUP_CONTROLLER_BLKIO] = "/not/really/sys/fs/cgroup/blkio",
104
    [VIR_CGROUP_CONTROLLER_SYSTEMD] = "/not/really/sys/fs/cgroup/systemd",
105
};
106 107 108 109 110 111 112 113 114 115
const char *mountsAllInOne[VIR_CGROUP_CONTROLLER_LAST] = {
    [VIR_CGROUP_CONTROLLER_CPU] = "/not/really/sys/fs/cgroup",
    [VIR_CGROUP_CONTROLLER_CPUACCT] = "/not/really/sys/fs/cgroup",
    [VIR_CGROUP_CONTROLLER_CPUSET] = "/not/really/sys/fs/cgroup",
    [VIR_CGROUP_CONTROLLER_MEMORY] = "/not/really/sys/fs/cgroup",
    [VIR_CGROUP_CONTROLLER_DEVICES] = "/not/really/sys/fs/cgroup",
    [VIR_CGROUP_CONTROLLER_FREEZER] = NULL,
    [VIR_CGROUP_CONTROLLER_BLKIO] = "/not/really/sys/fs/cgroup",
    [VIR_CGROUP_CONTROLLER_SYSTEMD] = NULL,
};
116

117 118 119 120 121 122 123 124
const char *links[VIR_CGROUP_CONTROLLER_LAST] = {
    [VIR_CGROUP_CONTROLLER_CPU] = "/not/really/sys/fs/cgroup/cpu",
    [VIR_CGROUP_CONTROLLER_CPUACCT] = "/not/really/sys/fs/cgroup/cpuacct",
    [VIR_CGROUP_CONTROLLER_CPUSET] = NULL,
    [VIR_CGROUP_CONTROLLER_MEMORY] = NULL,
    [VIR_CGROUP_CONTROLLER_DEVICES] = NULL,
    [VIR_CGROUP_CONTROLLER_FREEZER] = NULL,
    [VIR_CGROUP_CONTROLLER_BLKIO] = NULL,
125 126 127 128 129 130 131 132 133 134 135 136
    [VIR_CGROUP_CONTROLLER_SYSTEMD] = NULL,
};

const char *linksAllInOne[VIR_CGROUP_CONTROLLER_LAST] = {
    [VIR_CGROUP_CONTROLLER_CPU] = NULL,
    [VIR_CGROUP_CONTROLLER_CPUACCT] = NULL,
    [VIR_CGROUP_CONTROLLER_CPUSET] = NULL,
    [VIR_CGROUP_CONTROLLER_MEMORY] = NULL,
    [VIR_CGROUP_CONTROLLER_DEVICES] = NULL,
    [VIR_CGROUP_CONTROLLER_FREEZER] = NULL,
    [VIR_CGROUP_CONTROLLER_BLKIO] = NULL,
    [VIR_CGROUP_CONTROLLER_SYSTEMD] = NULL,
137 138 139
};


140 141 142 143 144 145
struct _detectMountsData {
    const char *file;
    bool fail;
};


146 147 148 149
static int
testCgroupDetectMounts(const void *args)
{
    int result = -1;
150
    const struct _detectMountsData *data = args;
151 152 153 154 155 156
    char *parsed = NULL;
    const char *actual;
    virCgroupPtr group = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    size_t i;

157
    setenv("VIR_CGROUP_MOCK_FILENAME", data->file, 1);
158

159 160
    if (virAsprintf(&parsed, "%s/vircgroupdata/%s.parsed",
                    abs_srcdir, data->file) < 0) {
161
        goto cleanup;
162
    }
163

164 165 166
    if (virCgroupNewSelf(&group) < 0) {
        if (data->fail)
            result = 0;
167
        goto cleanup;
168
    }
169

170
    if (data->fail)
171 172 173 174 175
        goto cleanup;

    for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
        virBufferAsprintf(&buf, "%-12s %s\n",
                          virCgroupControllerTypeToString(i),
176
                          NULLSTR(group->legacy[i].mountPoint));
177
    }
178 179
    virBufferAsprintf(&buf, "%-12s %s\n",
                      "unified", NULLSTR(group->unified.mountPoint));
180 181 182 183
    if (virBufferCheckError(&buf) < 0)
        goto cleanup;

    actual = virBufferCurrentContent(&buf);
184
    if (virTestCompareToFile(actual, parsed) < 0)
185 186 187 188 189
        goto cleanup;

    result = 0;

 cleanup:
190
    unsetenv("VIR_CGROUP_MOCK_FILENAME");
191
    VIR_FREE(parsed);
192
    virCgroupFree(&group);
193 194 195 196 197
    virBufferFreeAndReset(&buf);
    return result;
}


198 199 200 201 202 203 204
static int testCgroupNewForSelf(const void *args ATTRIBUTE_UNUSED)
{
    virCgroupPtr cgroup = NULL;
    int ret = -1;
    const char *placement[VIR_CGROUP_CONTROLLER_LAST] = {
        [VIR_CGROUP_CONTROLLER_CPU] = "/system",
        [VIR_CGROUP_CONTROLLER_CPUACCT] = "/system",
205 206
        [VIR_CGROUP_CONTROLLER_CPUSET] = "/",
        [VIR_CGROUP_CONTROLLER_MEMORY] = "/",
207
        [VIR_CGROUP_CONTROLLER_DEVICES] = NULL,
208 209
        [VIR_CGROUP_CONTROLLER_FREEZER] = "/",
        [VIR_CGROUP_CONTROLLER_BLKIO] = "/",
210
        [VIR_CGROUP_CONTROLLER_SYSTEMD] = "/user/berrange/123",
211 212 213 214 215 216 217
    };

    if (virCgroupNewSelf(&cgroup) < 0) {
        fprintf(stderr, "Cannot create cgroup for self\n");
        goto cleanup;
    }

218
    ret = validateCgroup(cgroup, "", mountsFull, links, placement);
219

220
 cleanup:
221
    virCgroupFree(&cgroup);
222 223 224 225
    return ret;
}


226 227 228 229 230 231 232
# define ENSURE_ERRNO(en) \
    do { \
    if (!virLastErrorIsSystemErrno(en)) { \
        virErrorPtr err = virGetLastError(); \
        fprintf(stderr, "Did not get " #en " error code: %d:%d\n", \
                err ? err->code : 0, err ? err->int1 : 0); \
        goto cleanup; \
233 234 235 236 237
    } } while (0)

    /* Asking for impossible combination since CPU is co-mounted */


238 239 240 241 242 243
static int testCgroupNewForPartition(const void *args ATTRIBUTE_UNUSED)
{
    virCgroupPtr cgroup = NULL;
    int ret = -1;
    int rv;
    const char *placementSmall[VIR_CGROUP_CONTROLLER_LAST] = {
244 245
        [VIR_CGROUP_CONTROLLER_CPU] = "/virtualmachines.partition",
        [VIR_CGROUP_CONTROLLER_CPUACCT] = "/virtualmachines.partition",
246
        [VIR_CGROUP_CONTROLLER_CPUSET] = NULL,
247
        [VIR_CGROUP_CONTROLLER_MEMORY] = "/virtualmachines.partition",
248 249 250
        [VIR_CGROUP_CONTROLLER_DEVICES] = NULL,
        [VIR_CGROUP_CONTROLLER_FREEZER] = NULL,
        [VIR_CGROUP_CONTROLLER_BLKIO] = NULL,
251
        [VIR_CGROUP_CONTROLLER_SYSTEMD] = NULL,
252 253
    };
    const char *placementFull[VIR_CGROUP_CONTROLLER_LAST] = {
254 255 256 257
        [VIR_CGROUP_CONTROLLER_CPU] = "/virtualmachines.partition",
        [VIR_CGROUP_CONTROLLER_CPUACCT] = "/virtualmachines.partition",
        [VIR_CGROUP_CONTROLLER_CPUSET] = "/virtualmachines.partition",
        [VIR_CGROUP_CONTROLLER_MEMORY] = "/virtualmachines.partition",
258
        [VIR_CGROUP_CONTROLLER_DEVICES] = NULL,
259 260
        [VIR_CGROUP_CONTROLLER_FREEZER] = "/virtualmachines.partition",
        [VIR_CGROUP_CONTROLLER_BLKIO] = "/virtualmachines.partition",
261
        [VIR_CGROUP_CONTROLLER_SYSTEMD] = "/user/berrange/123",
262 263
    };

264
    if ((rv = virCgroupNewPartition("/virtualmachines", false, -1, &cgroup)) != -1) {
265 266 267
        fprintf(stderr, "Unexpected found /virtualmachines cgroup: %d\n", -rv);
        goto cleanup;
    }
268
    ENSURE_ERRNO(ENOENT);
269 270 271 272

    /* Asking for impossible combination since CPU is co-mounted */
    if ((rv = virCgroupNewPartition("/virtualmachines", true,
                                    (1 << VIR_CGROUP_CONTROLLER_CPU),
273
                                    &cgroup)) != -1) {
274 275 276
        fprintf(stderr, "Should not have created /virtualmachines cgroup: %d\n", -rv);
        goto cleanup;
    }
277
    ENSURE_ERRNO(EINVAL);
278 279 280 281

    /* Asking for impossible combination since devices is not mounted */
    if ((rv = virCgroupNewPartition("/virtualmachines", true,
                                    (1 << VIR_CGROUP_CONTROLLER_DEVICES),
282
                                    &cgroup)) != -1) {
283 284 285
        fprintf(stderr, "Should not have created /virtualmachines cgroup: %d\n", -rv);
        goto cleanup;
    }
286
    ENSURE_ERRNO(ENXIO);
287 288 289 290 291 292 293 294 295 296

    /* Asking for small combination since devices is not mounted */
    if ((rv = virCgroupNewPartition("/virtualmachines", true,
                                    (1 << VIR_CGROUP_CONTROLLER_CPU) |
                                    (1 << VIR_CGROUP_CONTROLLER_CPUACCT) |
                                    (1 << VIR_CGROUP_CONTROLLER_MEMORY),
                                    &cgroup)) != 0) {
        fprintf(stderr, "Cannot create /virtualmachines cgroup: %d\n", -rv);
        goto cleanup;
    }
297
    ret = validateCgroup(cgroup, "/virtualmachines.partition", mountsSmall, links, placementSmall);
298
    virCgroupFree(&cgroup);
299 300 301 302 303

    if ((rv = virCgroupNewPartition("/virtualmachines", true, -1, &cgroup)) != 0) {
        fprintf(stderr, "Cannot create /virtualmachines cgroup: %d\n", -rv);
        goto cleanup;
    }
304
    ret = validateCgroup(cgroup, "/virtualmachines.partition", mountsFull, links, placementFull);
305

306
 cleanup:
307
    virCgroupFree(&cgroup);
308 309 310
    return ret;
}

311

312 313 314 315 316 317
static int testCgroupNewForPartitionNested(const void *args ATTRIBUTE_UNUSED)
{
    virCgroupPtr cgroup = NULL;
    int ret = -1;
    int rv;
    const char *placementFull[VIR_CGROUP_CONTROLLER_LAST] = {
318 319 320 321
        [VIR_CGROUP_CONTROLLER_CPU] = "/deployment.partition/production.partition",
        [VIR_CGROUP_CONTROLLER_CPUACCT] = "/deployment.partition/production.partition",
        [VIR_CGROUP_CONTROLLER_CPUSET] = "/deployment.partition/production.partition",
        [VIR_CGROUP_CONTROLLER_MEMORY] = "/deployment.partition/production.partition",
322
        [VIR_CGROUP_CONTROLLER_DEVICES] = NULL,
323 324
        [VIR_CGROUP_CONTROLLER_FREEZER] = "/deployment.partition/production.partition",
        [VIR_CGROUP_CONTROLLER_BLKIO] = "/deployment.partition/production.partition",
325
        [VIR_CGROUP_CONTROLLER_SYSTEMD] = "/user/berrange/123",
326
    };
327

328
    if ((rv = virCgroupNewPartition("/deployment/production", false, -1, &cgroup)) != -1) {
329
        fprintf(stderr, "Unexpected found /deployment/production cgroup: %d\n", -rv);
330 331
        goto cleanup;
    }
332
    ENSURE_ERRNO(ENOENT);
333

334
    /* Should not work, since we require /deployment to be pre-created */
335
    if ((rv = virCgroupNewPartition("/deployment/production", true, -1, &cgroup)) != -1) {
336
        fprintf(stderr, "Unexpected created /deployment/production cgroup: %d\n", -rv);
337 338
        goto cleanup;
    }
339
    ENSURE_ERRNO(ENOENT);
340

341 342
    if ((rv = virCgroupNewPartition("/deployment", true, -1, &cgroup)) != 0) {
        fprintf(stderr, "Failed to create /deployment cgroup: %d\n", -rv);
343 344 345 346
        goto cleanup;
    }

    /* Should now work */
347
    virCgroupFree(&cgroup);
348 349
    if ((rv = virCgroupNewPartition("/deployment/production", true, -1, &cgroup)) != 0) {
        fprintf(stderr, "Failed to create /deployment/production cgroup: %d\n", -rv);
350 351 352
        goto cleanup;
    }

353 354 355
    ret = validateCgroup(cgroup, "/deployment.partition/production.partition",
                         mountsFull, links, placementFull);

356
 cleanup:
357
    virCgroupFree(&cgroup);
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
    return ret;
}


static int testCgroupNewForPartitionNestedDeep(const void *args ATTRIBUTE_UNUSED)
{
    virCgroupPtr cgroup = NULL;
    int ret = -1;
    int rv;
    const char *placementFull[VIR_CGROUP_CONTROLLER_LAST] = {
        [VIR_CGROUP_CONTROLLER_CPU] = "/user/berrange.user/production.partition",
        [VIR_CGROUP_CONTROLLER_CPUACCT] = "/user/berrange.user/production.partition",
        [VIR_CGROUP_CONTROLLER_CPUSET] = "/user/berrange.user/production.partition",
        [VIR_CGROUP_CONTROLLER_MEMORY] = "/user/berrange.user/production.partition",
        [VIR_CGROUP_CONTROLLER_DEVICES] = NULL,
        [VIR_CGROUP_CONTROLLER_FREEZER] = "/user/berrange.user/production.partition",
        [VIR_CGROUP_CONTROLLER_BLKIO] = "/user/berrange.user/production.partition",
375
        [VIR_CGROUP_CONTROLLER_SYSTEMD] = "/user/berrange/123",
376 377
    };

378
    if ((rv = virCgroupNewPartition("/user/berrange.user/production", false, -1, &cgroup)) != -1) {
379 380 381
        fprintf(stderr, "Unexpected found /user/berrange.user/production cgroup: %d\n", -rv);
        goto cleanup;
    }
382
    ENSURE_ERRNO(ENOENT);
383 384

    /* Should not work, since we require /user/berrange.user to be pre-created */
385
    if ((rv = virCgroupNewPartition("/user/berrange.user/production", true, -1, &cgroup)) != -1) {
386 387 388
        fprintf(stderr, "Unexpected created /user/berrange.user/production cgroup: %d\n", -rv);
        goto cleanup;
    }
389
    ENSURE_ERRNO(ENOENT);
390 391 392 393 394 395

    if ((rv = virCgroupNewPartition("/user", true, -1, &cgroup)) != 0) {
        fprintf(stderr, "Failed to create /user/berrange.user cgroup: %d\n", -rv);
        goto cleanup;
    }

396
    virCgroupFree(&cgroup);
397 398 399 400 401 402
    if ((rv = virCgroupNewPartition("/user/berrange.user", true, -1, &cgroup)) != 0) {
        fprintf(stderr, "Failed to create /user/berrange.user cgroup: %d\n", -rv);
        goto cleanup;
    }

    /* Should now work */
403
    virCgroupFree(&cgroup);
404 405 406 407 408 409 410
    if ((rv = virCgroupNewPartition("/user/berrange.user/production", true, -1, &cgroup)) != 0) {
        fprintf(stderr, "Failed to create /user/berrange.user/production cgroup: %d\n", -rv);
        goto cleanup;
    }

    ret = validateCgroup(cgroup, "/user/berrange.user/production.partition",
                         mountsFull, links, placementFull);
411

412
 cleanup:
413
    virCgroupFree(&cgroup);
414 415 416 417 418 419 420 421 422 423 424 425
    return ret;
}



static int testCgroupNewForPartitionDomain(const void *args ATTRIBUTE_UNUSED)
{
    virCgroupPtr partitioncgroup = NULL;
    virCgroupPtr domaincgroup = NULL;
    int ret = -1;
    int rv;
    const char *placement[VIR_CGROUP_CONTROLLER_LAST] = {
426 427 428 429
        [VIR_CGROUP_CONTROLLER_CPU] = "/production.partition/foo.libvirt-lxc",
        [VIR_CGROUP_CONTROLLER_CPUACCT] = "/production.partition/foo.libvirt-lxc",
        [VIR_CGROUP_CONTROLLER_CPUSET] = "/production.partition/foo.libvirt-lxc",
        [VIR_CGROUP_CONTROLLER_MEMORY] = "/production.partition/foo.libvirt-lxc",
430
        [VIR_CGROUP_CONTROLLER_DEVICES] = NULL,
431 432
        [VIR_CGROUP_CONTROLLER_FREEZER] = "/production.partition/foo.libvirt-lxc",
        [VIR_CGROUP_CONTROLLER_BLKIO] = "/production.partition/foo.libvirt-lxc",
433
        [VIR_CGROUP_CONTROLLER_SYSTEMD] = "/user/berrange/123",
434 435 436 437 438 439 440 441 442 443 444 445
    };

    if ((rv = virCgroupNewPartition("/production", true, -1, &partitioncgroup)) != 0) {
        fprintf(stderr, "Failed to create /production cgroup: %d\n", -rv);
        goto cleanup;
    }

    if ((rv = virCgroupNewDomainPartition(partitioncgroup, "lxc", "foo", true, &domaincgroup)) != 0) {
        fprintf(stderr, "Cannot create LXC cgroup: %d\n", -rv);
        goto cleanup;
    }

446
    ret = validateCgroup(domaincgroup, "/production.partition/foo.libvirt-lxc", mountsFull, links, placement);
447

448
 cleanup:
449 450
    virCgroupFree(&partitioncgroup);
    virCgroupFree(&domaincgroup);
451 452 453
    return ret;
}

454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
static int testCgroupNewForPartitionDomainEscaped(const void *args ATTRIBUTE_UNUSED)
{
    virCgroupPtr partitioncgroup1 = NULL;
    virCgroupPtr partitioncgroup2 = NULL;
    virCgroupPtr partitioncgroup3 = NULL;
    virCgroupPtr domaincgroup = NULL;
    int ret = -1;
    int rv;
    const char *placement[VIR_CGROUP_CONTROLLER_LAST] = {
        [VIR_CGROUP_CONTROLLER_CPU] = "/_cgroup.evil/net_cls.evil/__evil.evil/_cpu.foo.libvirt-lxc",
        [VIR_CGROUP_CONTROLLER_CPUACCT] = "/_cgroup.evil/net_cls.evil/__evil.evil/_cpu.foo.libvirt-lxc",
        [VIR_CGROUP_CONTROLLER_CPUSET] = "/_cgroup.evil/net_cls.evil/__evil.evil/_cpu.foo.libvirt-lxc",
        [VIR_CGROUP_CONTROLLER_MEMORY] = "/_cgroup.evil/net_cls.evil/__evil.evil/_cpu.foo.libvirt-lxc",
        [VIR_CGROUP_CONTROLLER_DEVICES] = NULL,
        [VIR_CGROUP_CONTROLLER_FREEZER] = "/_cgroup.evil/net_cls.evil/__evil.evil/_cpu.foo.libvirt-lxc",
        [VIR_CGROUP_CONTROLLER_BLKIO] = "/_cgroup.evil/net_cls.evil/__evil.evil/_cpu.foo.libvirt-lxc",
470
        [VIR_CGROUP_CONTROLLER_SYSTEMD] = "/user/berrange/123",
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
    };

    if ((rv = virCgroupNewPartition("/cgroup.evil", true, -1, &partitioncgroup1)) != 0) {
        fprintf(stderr, "Failed to create /cgroup.evil cgroup: %d\n", -rv);
        goto cleanup;
    }

    if ((rv = virCgroupNewPartition("/cgroup.evil/net_cls.evil", true, -1, &partitioncgroup2)) != 0) {
        fprintf(stderr, "Failed to create /cgroup.evil/cpu.evil cgroup: %d\n", -rv);
        goto cleanup;
    }

    if ((rv = virCgroupNewPartition("/cgroup.evil/net_cls.evil/_evil.evil", true, -1, &partitioncgroup3)) != 0) {
        fprintf(stderr, "Failed to create /cgroup.evil cgroup: %d\n", -rv);
        goto cleanup;
    }

    if ((rv = virCgroupNewDomainPartition(partitioncgroup3, "lxc", "cpu.foo", true, &domaincgroup)) != 0) {
        fprintf(stderr, "Cannot create LXC cgroup: %d\n", -rv);
        goto cleanup;
    }

    /* NB we're not expecting 'net_cls.evil' to be escaped,
     * since our fake /proc/cgroups pretends this controller
     * isn't compiled into the kernel
     */
    ret = validateCgroup(domaincgroup, "/_cgroup.evil/net_cls.evil/__evil.evil/_cpu.foo.libvirt-lxc", mountsFull, links, placement);

499
 cleanup:
500 501 502 503
    virCgroupFree(&partitioncgroup3);
    virCgroupFree(&partitioncgroup2);
    virCgroupFree(&partitioncgroup1);
    virCgroupFree(&domaincgroup);
504 505 506
    return ret;
}

507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
static int testCgroupNewForSelfAllInOne(const void *args ATTRIBUTE_UNUSED)
{
    virCgroupPtr cgroup = NULL;
    int ret = -1;
    const char *placement[VIR_CGROUP_CONTROLLER_LAST] = {
        [VIR_CGROUP_CONTROLLER_CPU] = "/",
        [VIR_CGROUP_CONTROLLER_CPUACCT] = "/",
        [VIR_CGROUP_CONTROLLER_CPUSET] = "/",
        [VIR_CGROUP_CONTROLLER_MEMORY] = "/",
        [VIR_CGROUP_CONTROLLER_DEVICES] = "/",
        [VIR_CGROUP_CONTROLLER_FREEZER] = NULL,
        [VIR_CGROUP_CONTROLLER_BLKIO] = "/",
    };

    if (virCgroupNewSelf(&cgroup) < 0) {
        fprintf(stderr, "Cannot create cgroup for self\n");
        goto cleanup;
    }

    ret = validateCgroup(cgroup, "", mountsAllInOne, linksAllInOne, placement);

528
 cleanup:
529
    virCgroupFree(&cgroup);
530 531 532 533
    return ret;
}


534 535 536 537 538
static int testCgroupNewForSelfLogind(const void *args ATTRIBUTE_UNUSED)
{
    virCgroupPtr cgroup = NULL;
    int ret = -1;

539 540
    if (virCgroupNewSelf(&cgroup) == 0) {
        fprintf(stderr, "Expected cgroup creation to fail.\n");
541 542 543
        goto cleanup;
    }

544
    ret = 0;
545
 cleanup:
546
    virCgroupFree(&cgroup);
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
    return ret;
}


static int testCgroupAvailable(const void *args)
{
    bool got = virCgroupAvailable();
    bool want = args == (void*)0x1;

    if (got != want) {
        fprintf(stderr, "Expected cgroup %savailable, but state was wrong\n",
                want ? "" : "not ");
        return -1;
    }

    return 0;
}

565 566 567 568
static int testCgroupControllerAvailable(const void *args ATTRIBUTE_UNUSED)
{
    int ret = 0;

569 570 571 572 573 574 575
# define CHECK_CONTROLLER(c, present) \
    if ((present && !virCgroupControllerAvailable(c)) || \
        (!present && virCgroupControllerAvailable(c))) { \
        fprintf(stderr, present ? \
                "Expected controller %s not available\n" : \
                "Unexpected controller %s available\n", #c); \
        ret = -1; \
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
    }

    CHECK_CONTROLLER(VIR_CGROUP_CONTROLLER_CPU, true)
    CHECK_CONTROLLER(VIR_CGROUP_CONTROLLER_CPUACCT, true)
    CHECK_CONTROLLER(VIR_CGROUP_CONTROLLER_CPUSET, true)
    CHECK_CONTROLLER(VIR_CGROUP_CONTROLLER_MEMORY, true)
    CHECK_CONTROLLER(VIR_CGROUP_CONTROLLER_DEVICES, false)
    CHECK_CONTROLLER(VIR_CGROUP_CONTROLLER_FREEZER, true)
    CHECK_CONTROLLER(VIR_CGROUP_CONTROLLER_BLKIO, true)
    CHECK_CONTROLLER(VIR_CGROUP_CONTROLLER_NET_CLS, false)
    CHECK_CONTROLLER(VIR_CGROUP_CONTROLLER_PERF_EVENT, false)
    CHECK_CONTROLLER(VIR_CGROUP_CONTROLLER_SYSTEMD, true)

# undef CHECK_CONTROLLER
    return ret;
}

593 594 595 596 597
static int testCgroupGetPercpuStats(const void *args ATTRIBUTE_UNUSED)
{
    virCgroupPtr cgroup = NULL;
    size_t i;
    int rv, ret = -1;
598 599
    virTypedParameterPtr params = NULL;
# define EXPECTED_NCPUS 160
600

J
Ján Tomko 已提交
601
    unsigned long long expected[EXPECTED_NCPUS] = {
602
        0, 0, 0, 0, 0, 0, 0, 0,
603 604
        7059492996ULL, 0, 0, 0, 0, 0, 0, 0,
        4180532496ULL, 0, 0, 0, 0, 0, 0, 0,
605 606 607
        0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0,
608 609 610 611 612 613 614 615 616 617 618 619 620 621
        1957541268ULL, 0, 0, 0, 0, 0, 0, 0,
        2065932204ULL, 0, 0, 0, 0, 0, 0, 0,
        18228689414ULL, 0, 0, 0, 0, 0, 0, 0,
        4245525148ULL, 0, 0, 0, 0, 0, 0, 0,
        2911161568ULL, 0, 0, 0, 0, 0, 0, 0,
        1407758136ULL, 0, 0, 0, 0, 0, 0, 0,
        1836807700ULL, 0, 0, 0, 0, 0, 0, 0,
        1065296618ULL, 0, 0, 0, 0, 0, 0, 0,
        2046213266ULL, 0, 0, 0, 0, 0, 0, 0,
        747889778ULL, 0, 0, 0, 0, 0, 0, 0,
        709566900ULL, 0, 0, 0, 0, 0, 0, 0,
        444777342ULL, 0, 0, 0, 0, 0, 0, 0,
        5683512916ULL, 0, 0, 0, 0, 0, 0, 0,
        635751356ULL, 0, 0, 0, 0, 0, 0, 0,
622
    };
623 624 625

    if (VIR_ALLOC_N(params, EXPECTED_NCPUS) < 0)
        goto cleanup;
626 627 628 629 630 631 632 633 634

    if ((rv = virCgroupNewPartition("/virtualmachines", true,
                                    (1 << VIR_CGROUP_CONTROLLER_CPU) |
                                    (1 << VIR_CGROUP_CONTROLLER_CPUACCT),
                                    &cgroup)) < 0) {
        fprintf(stderr, "Could not create /virtualmachines cgroup: %d\n", -rv);
        goto cleanup;
    }

635 636
    if (virHostCPUGetCount() != EXPECTED_NCPUS) {
        fprintf(stderr, "Unexpected: virHostCPUGetCount() yields: %d\n", virHostCPUGetCount());
637 638 639 640 641
        goto cleanup;
    }

    if ((rv = virCgroupGetPercpuStats(cgroup,
                                      params,
642
                                      1, 0, EXPECTED_NCPUS, NULL)) < 0) {
643 644 645 646
        fprintf(stderr, "Failed call to virCgroupGetPercpuStats for /virtualmachines cgroup: %d\n", -rv);
        goto cleanup;
    }

647
    for (i = 0; i < EXPECTED_NCPUS; i++) {
648
        if (STRNEQ(params[i].field, VIR_DOMAIN_CPU_STATS_CPUTIME)) {
649
            fprintf(stderr,
650 651
                    "Wrong parameter name value from virCgroupGetPercpuStats at %zu (is: %s)\n",
                    i, params[i].field);
652 653 654 655 656
            goto cleanup;
        }

        if (params[i].type != VIR_TYPED_PARAM_ULLONG) {
            fprintf(stderr,
657 658
                    "Wrong parameter value type from virCgroupGetPercpuStats at %zu (is: %d)\n",
                    i, params[i].type);
659 660 661 662 663
            goto cleanup;
        }

        if (params[i].value.ul != expected[i]) {
            fprintf(stderr,
664 665
                    "Wrong value from virCgroupGetMemoryUsage at %zu (expected %llu)\n",
                    i, params[i].value.ul);
666 667 668 669 670 671
            goto cleanup;
        }
    }

    ret = 0;

672
 cleanup:
673
    virCgroupFree(&cgroup);
674
    VIR_FREE(params);
675 676 677
    return ret;
}

678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
static int testCgroupGetMemoryUsage(const void *args ATTRIBUTE_UNUSED)
{
    virCgroupPtr cgroup = NULL;
    int rv, ret = -1;
    unsigned long kb;

    if ((rv = virCgroupNewPartition("/virtualmachines", true,
                                    (1 << VIR_CGROUP_CONTROLLER_MEMORY),
                                    &cgroup)) < 0) {
        fprintf(stderr, "Could not create /virtualmachines cgroup: %d\n", -rv);
        goto cleanup;
    }

    if ((rv = virCgroupGetMemoryUsage(cgroup, &kb)) < 0) {
        fprintf(stderr, "Could not retrieve GetMemoryUsage for /virtualmachines cgroup: %d\n", -rv);
        goto cleanup;
    }

    if (kb != 1421212UL) {
        fprintf(stderr,
                "Wrong value from virCgroupGetMemoryUsage (expected %ld)\n",
                1421212UL);
        goto cleanup;
    }

    ret = 0;

705
 cleanup:
706
    virCgroupFree(&cgroup);
707 708 709
    return ret;
}

710 711 712 713 714 715 716
static int testCgroupGetBlkioIoServiced(const void *args ATTRIBUTE_UNUSED)
{
    virCgroupPtr cgroup = NULL;
    size_t i;
    int rv, ret = -1;

    const long long expected_values[] = {
E
Eric Blake 已提交
717 718
        119084214273ULL,
        822880960513ULL,
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
        9665167,
        73283807
    };
    const char* names[] = {
        "bytes read",
        "bytes written",
        "requests read",
        "requests written"
    };
    long long values[ARRAY_CARDINALITY(expected_values)];

    if ((rv = virCgroupNewPartition("/virtualmachines", true,
                                    (1 << VIR_CGROUP_CONTROLLER_BLKIO),
                                    &cgroup)) < 0) {
        fprintf(stderr, "Could not create /virtualmachines cgroup: %d\n", -rv);
        goto cleanup;
    }

    if ((rv = virCgroupGetBlkioIoServiced(cgroup,
                                          values, &values[1],
                                          &values[2], &values[3])) < 0) {
        fprintf(stderr, "Could not retrieve BlkioIoServiced for /virtualmachines cgroup: %d\n", -rv);
        goto cleanup;
    }

    for (i = 0; i < ARRAY_CARDINALITY(expected_values); i++) {
        if (expected_values[i] != values[i]) {
            fprintf(stderr,
                    "Wrong value for %s from virCgroupBlkioIoServiced (expected %lld)\n",
                    names[i], expected_values[i]);
            goto cleanup;
        }
    }

    ret = 0;

755
 cleanup:
756
    virCgroupFree(&cgroup);
757 758 759 760 761 762 763 764 765
    return ret;
}

static int testCgroupGetBlkioIoDeviceServiced(const void *args ATTRIBUTE_UNUSED)
{
    virCgroupPtr cgroup = NULL;
    size_t i;
    int rv, ret = -1;
    const long long expected_values0[] = {
E
Eric Blake 已提交
766 767
        59542107136ULL,
        411440480256ULL,
768 769 770 771
        4832583,
        36641903
    };
    const long long expected_values1[] = {
E
Eric Blake 已提交
772 773
        59542107137ULL,
        411440480257ULL,
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
        4832584,
        36641904
    };
    const char* names[] = {
        "bytes read",
        "bytes written",
        "requests read",
        "requests written"
    };
    long long values[ARRAY_CARDINALITY(expected_values0)];

    if ((rv = virCgroupNewPartition("/virtualmachines", true,
                                    (1 << VIR_CGROUP_CONTROLLER_BLKIO),
                                    &cgroup)) < 0) {
        fprintf(stderr, "Could not create /virtualmachines cgroup: %d\n", -rv);
        goto cleanup;
    }

    if ((rv = virCgroupGetBlkioIoDeviceServiced(cgroup,
                                                FAKEDEVDIR0,
                                                values, &values[1],
                                                &values[2], &values[3])) < 0) {
        fprintf(stderr, "Could not retrieve BlkioIoDeviceServiced for /virtualmachines cgroup: %d\n", -rv);
        goto cleanup;
    }

    for (i = 0; i < ARRAY_CARDINALITY(expected_values0); i++) {
        if (expected_values0[i] != values[i]) {
            fprintf(stderr,
                    "Wrong value for %s from virCgroupGetBlkioIoDeviceServiced (expected %lld)\n",
                    names[i], expected_values0[i]);
            goto cleanup;
        }
    }

    if ((rv = virCgroupGetBlkioIoDeviceServiced(cgroup,
                                                FAKEDEVDIR1,
                                                values, &values[1],
                                                &values[2], &values[3])) < 0) {
        fprintf(stderr, "Could not retrieve BlkioIoDeviceServiced for /virtualmachines cgroup: %d\n", -rv);
        goto cleanup;
    }

    for (i = 0; i < ARRAY_CARDINALITY(expected_values1); i++) {
        if (expected_values1[i] != values[i]) {
            fprintf(stderr,
                    "Wrong value for %s from virCgroupGetBlkioIoDeviceServiced (expected %lld)\n",
                    names[i], expected_values1[i]);
            goto cleanup;
        }
    }

    ret = 0;

828
 cleanup:
829
    virCgroupFree(&cgroup);
830 831
    return ret;
}
832

833
# define FAKEROOTDIRTEMPLATE abs_builddir "/fakerootdir-XXXXXX"
834

835 836 837
static char *
initFakeFS(const char *mode,
           const char *filename)
838
{
839
    char *fakerootdir;
840

841
    if (VIR_STRDUP_QUIET(fakerootdir, FAKEROOTDIRTEMPLATE) < 0) {
842 843 844 845
        fprintf(stderr, "Out of memory\n");
        abort();
    }

846 847
    if (!mkdtemp(fakerootdir)) {
        fprintf(stderr, "Cannot create fakerootdir");
848 849 850
        abort();
    }

851
    setenv("LIBVIRT_FAKE_ROOT_DIR", fakerootdir, 1);
852

853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
    if (mode)
        setenv("VIR_CGROUP_MOCK_MODE", mode, 1);

    if (filename)
        setenv("VIR_CGROUP_MOCK_FILENAME", filename, 1);

    return fakerootdir;
}

static void
cleanupFakeFS(char *fakerootdir)
{
    if (getenv("LIBVIRT_SKIP_CLEANUP") == NULL)
        virFileDeleteTree(fakerootdir);

    VIR_FREE(fakerootdir);
    unsetenv("LIBVIRT_FAKE_ROOT_DIR");
    unsetenv("VIR_CGROUP_MOCK_MODE");
    unsetenv("VIR_CGROUP_MOCK_FILENAME");
}

static int
mymain(void)
{
    int ret = 0;
    char *fakerootdir;

880
# define DETECT_MOUNTS_FULL(file, fail) \
881
    do { \
882
        struct _detectMountsData data = { file, fail }; \
883 884
        if (virTestRun("Detect cgroup mounts for " file, \
                       testCgroupDetectMounts, \
885
                       &data) < 0) \
886
            ret = -1; \
887
    } while (0)
888 889
# define DETECT_MOUNTS(file) DETECT_MOUNTS_FULL(file, false);
# define DETECT_MOUNTS_FAIL(file) DETECT_MOUNTS_FULL(file, true);
890 891 892 893 894 895 896 897 898 899

    DETECT_MOUNTS("ovirt-node-6.6");
    DETECT_MOUNTS("ovirt-node-7.1");
    DETECT_MOUNTS("fedora-18");
    DETECT_MOUNTS("fedora-21");
    DETECT_MOUNTS("rhel-7.1");
    DETECT_MOUNTS("cgroups1");
    DETECT_MOUNTS("cgroups2");
    DETECT_MOUNTS("cgroups3");
    DETECT_MOUNTS("all-in-one");
900
    DETECT_MOUNTS_FAIL("no-cgroups");
901
    DETECT_MOUNTS("kubevirt");
902 903 904
    fakerootdir = initFakeFS("unified", NULL);
    DETECT_MOUNTS("unified");
    cleanupFakeFS(fakerootdir);
905

906
    fakerootdir = initFakeFS(NULL, "systemd");
907
    if (virTestRun("New cgroup for self", testCgroupNewForSelf, NULL) < 0)
908 909
        ret = -1;

910
    if (virTestRun("New cgroup for partition", testCgroupNewForPartition, NULL) < 0)
911 912
        ret = -1;

913
    if (virTestRun("New cgroup for partition nested", testCgroupNewForPartitionNested, NULL) < 0)
914 915
        ret = -1;

916
    if (virTestRun("New cgroup for partition nested deeply", testCgroupNewForPartitionNestedDeep, NULL) < 0)
917 918
        ret = -1;

919
    if (virTestRun("New cgroup for domain partition", testCgroupNewForPartitionDomain, NULL) < 0)
920 921
        ret = -1;

922
    if (virTestRun("New cgroup for domain partition escaped", testCgroupNewForPartitionDomainEscaped, NULL) < 0)
923
        ret = -1;
924

925
    if (virTestRun("Cgroup available", testCgroupAvailable, (void*)0x1) < 0)
926 927
        ret = -1;

928
    if (virTestRun("Cgroup controller available", testCgroupControllerAvailable, NULL) < 0)
929 930
        ret = -1;

931
    if (virTestRun("virCgroupGetBlkioIoServiced works", testCgroupGetBlkioIoServiced, NULL) < 0)
932 933
        ret = -1;

934
    if (virTestRun("virCgroupGetBlkioIoDeviceServiced works", testCgroupGetBlkioIoDeviceServiced, NULL) < 0)
935 936
        ret = -1;

937
    if (virTestRun("virCgroupGetMemoryUsage works", testCgroupGetMemoryUsage, NULL) < 0)
938 939
        ret = -1;

940
    if (virTestRun("virCgroupGetPercpuStats works", testCgroupGetPercpuStats, NULL) < 0)
941
        ret = -1;
942
    cleanupFakeFS(fakerootdir);
943

944
    fakerootdir = initFakeFS(NULL, "all-in-one");
945
    if (virTestRun("New cgroup for self (allinone)", testCgroupNewForSelfAllInOne, NULL) < 0)
946
        ret = -1;
947
    if (virTestRun("Cgroup available", testCgroupAvailable, (void*)0x1) < 0)
948
        ret = -1;
949
    cleanupFakeFS(fakerootdir);
950

951
    fakerootdir = initFakeFS(NULL, "logind");
952
    if (virTestRun("New cgroup for self (logind)", testCgroupNewForSelfLogind, NULL) < 0)
953
        ret = -1;
954
    if (virTestRun("Cgroup available", testCgroupAvailable, (void*)0x0) < 0)
955
        ret = -1;
956
    cleanupFakeFS(fakerootdir);
957

958 959


960
    return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
961 962
}

963
VIR_TEST_MAIN_PRELOAD(mymain, abs_builddir "/.libs/vircgroupmock.so")
964 965 966 967 968 969 970 971

#else
int
main(void)
{
    return EXIT_AM_SKIP;
}
#endif