vircgroupbackend.h 15.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * vircgroupbackend.h: methods for cgroups backend
 *
 * Copyright (C) 2018 Red Hat, Inc.
 *
 * 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/>.
 */

21
#pragma once
22

23
#include "internal.h"
24

25 26
#include "vircgroup.h"
#include "virhash.h"
27

28
#define CGROUP_MAX_VAL 512
29

30 31 32 33 34 35
typedef enum {
    VIR_CGROUP_NONE = 0, /* create subdir under each cgroup if possible. */
    VIR_CGROUP_MEM_HIERACHY = 1 << 0, /* call virCgroupSetMemoryUseHierarchy
                                       * before creating subcgroups and
                                       * attaching tasks
                                       */
36
    VIR_CGROUP_THREAD = 1 << 1, /* cgroup v2 handles threads differently */
37 38 39
    VIR_CGROUP_SYSTEMD = 1 << 2, /* with systemd and cgroups v2 we cannot
                                  * manually enable controllers that systemd
                                  * doesn't know how to delegate */
40 41
} virCgroupBackendFlags;

42 43 44 45 46 47 48 49 50 51 52 53 54 55
typedef enum {
    /* Adds a whole process with all threads to specific cgroup except
     * to systemd named controller. */
    VIR_CGROUP_TASK_PROCESS = 1 << 0,

    /* Same as VIR_CGROUP_TASK_PROCESS but it also adds the task to systemd
     * named controller. */
    VIR_CGROUP_TASK_SYSTEMD = 1 << 1,

    /* Moves only specific thread into cgroup except to systemd
     * named controller. */
    VIR_CGROUP_TASK_THREAD = 1 << 2,
} virCgroupBackendTaskFlags;

56
typedef enum {
P
Pavel Hrdina 已提交
57 58
    VIR_CGROUP_BACKEND_TYPE_V2 = 0,
    VIR_CGROUP_BACKEND_TYPE_V1,
59 60 61
    VIR_CGROUP_BACKEND_TYPE_LAST,
} virCgroupBackendType;

62 63 64
typedef bool
(*virCgroupAvailableCB)(void);

65 66 67 68 69 70
typedef bool
(*virCgroupValidateMachineGroupCB)(virCgroupPtr group,
                                   const char *name,
                                   const char *drivername,
                                   const char *machinename);

71 72 73 74
typedef int
(*virCgroupCopyMountsCB)(virCgroupPtr group,
                         virCgroupPtr parent);

75 76 77 78 79
typedef int
(*virCgroupCopyPlacementCB)(virCgroupPtr group,
                            const char *path,
                            virCgroupPtr parent);

80 81 82 83 84 85 86 87 88 89 90 91
typedef int
(*virCgroupDetectMountsCB)(virCgroupPtr group,
                           const char *mntType,
                           const char *mntOpts,
                           const char *mntDir);

typedef int
(*virCgroupDetectPlacementCB)(virCgroupPtr group,
                              const char *path,
                              const char *controllers,
                              const char *selfpath);

92 93 94 95
typedef int
(*virCgroupValidatePlacementCB)(virCgroupPtr group,
                                pid_t pid);

96 97 98
typedef char *
(*virCgroupStealPlacementCB)(virCgroupPtr group);

99 100
typedef int
(*virCgroupDetectControllersCB)(virCgroupPtr group,
101
                                int controllers);
102

103 104 105 106
typedef bool
(*virCgroupHasControllerCB)(virCgroupPtr cgroup,
                            int controller);

107 108 109
typedef int
(*virCgroupGetAnyControllerCB)(virCgroupPtr group);

110 111 112 113 114 115
typedef int
(*virCgroupPathOfControllerCB)(virCgroupPtr group,
                               int controller,
                               const char *key,
                               char **path);

116 117 118 119 120 121
typedef int
(*virCgroupMakeGroupCB)(virCgroupPtr parent,
                        virCgroupPtr group,
                        bool create,
                        unsigned int flags);

122 123 124
typedef int
(*virCgroupRemoveCB)(virCgroupPtr group);

125 126 127 128 129
typedef int
(*virCgroupAddTaskCB)(virCgroupPtr group,
                      pid_t pid,
                      unsigned int flags);

130 131 132 133
typedef int
(*virCgroupHasEmptyTasksCB)(virCgroupPtr cgroup,
                            int controller);

134 135 136 137 138
typedef int
(*virCgroupKillRecursiveCB)(virCgroupPtr group,
                            int signum,
                            virHashTablePtr pids);

139 140 141 142 143
typedef int
(*virCgroupBindMountCB)(virCgroupPtr group,
                        const char *oldroot,
                        const char *mountopts);

144 145 146 147 148 149
typedef int
(*virCgroupSetOwnerCB)(virCgroupPtr cgroup,
                       uid_t uid,
                       gid_t gid,
                       int controllers);

150 151 152 153 154 155 156 157
typedef int
(*virCgroupSetBlkioWeightCB)(virCgroupPtr group,
                             unsigned int weight);

typedef int
(*virCgroupGetBlkioWeightCB)(virCgroupPtr group,
                             unsigned int *weight);

158 159 160 161 162 163 164
typedef int
(*virCgroupGetBlkioIoServicedCB)(virCgroupPtr group,
                                 long long *bytes_read,
                                 long long *bytes_write,
                                 long long *requests_read,
                                 long long *requests_write);

165 166 167 168 169 170 171 172
typedef int
(*virCgroupGetBlkioIoDeviceServicedCB)(virCgroupPtr group,
                                       const char *path,
                                       long long *bytes_read,
                                       long long *bytes_write,
                                       long long *requests_read,
                                       long long *requests_write);

173 174 175 176 177 178 179 180 181 182
typedef int
(*virCgroupSetBlkioDeviceWeightCB)(virCgroupPtr group,
                                   const char *path,
                                   unsigned int weight);

typedef int
(*virCgroupGetBlkioDeviceWeightCB)(virCgroupPtr group,
                                   const char *path,
                                   unsigned int *weight);

183 184 185 186 187 188 189 190 191 192
typedef int
(*virCgroupSetBlkioDeviceReadIopsCB)(virCgroupPtr group,
                                     const char *path,
                                     unsigned int riops);

typedef int
(*virCgroupGetBlkioDeviceReadIopsCB)(virCgroupPtr group,
                                     const char *path,
                                     unsigned int *riops);

193 194 195 196 197 198 199 200 201 202
typedef int
(*virCgroupSetBlkioDeviceWriteIopsCB)(virCgroupPtr group,
                                      const char *path,
                                      unsigned int wiops);

typedef int
(*virCgroupGetBlkioDeviceWriteIopsCB)(virCgroupPtr group,
                                      const char *path,
                                      unsigned int *wiops);

203 204 205 206 207 208 209 210 211 212
typedef int
(*virCgroupSetBlkioDeviceReadBpsCB)(virCgroupPtr group,
                                    const char *path,
                                    unsigned long long rbps);

typedef int
(*virCgroupGetBlkioDeviceReadBpsCB)(virCgroupPtr group,
                                    const char *path,
                                    unsigned long long *rbps);

213 214 215 216 217 218 219 220 221 222
typedef int
(*virCgroupSetBlkioDeviceWriteBpsCB)(virCgroupPtr group,
                                     const char *path,
                                     unsigned long long wbps);

typedef int
(*virCgroupGetBlkioDeviceWriteBpsCB)(virCgroupPtr group,
                                     const char *path,
                                     unsigned long long *wbps);

223 224 225 226
typedef int
(*virCgroupSetMemoryCB)(virCgroupPtr group,
                        unsigned long long kb);

227 228 229 230 231 232 233 234 235
typedef int
(*virCgroupGetMemoryStatCB)(virCgroupPtr group,
                            unsigned long long *cache,
                            unsigned long long *activeAnon,
                            unsigned long long *inactiveAnon,
                            unsigned long long *activeFile,
                            unsigned long long *inactiveFile,
                            unsigned long long *unevictable);

236 237 238 239
typedef int
(*virCgroupGetMemoryUsageCB)(virCgroupPtr group,
                             unsigned long *kb);

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
typedef int
(*virCgroupSetMemoryHardLimitCB)(virCgroupPtr group,
                                 unsigned long long kb);

typedef int
(*virCgroupGetMemoryHardLimitCB)(virCgroupPtr group,
                                 unsigned long long *kb);

typedef int
(*virCgroupSetMemorySoftLimitCB)(virCgroupPtr group,
                                 unsigned long long kb);

typedef int
(*virCgroupGetMemorySoftLimitCB)(virCgroupPtr group,
                                 unsigned long long *kb);

typedef int
(*virCgroupSetMemSwapHardLimitCB)(virCgroupPtr group,
                                  unsigned long long kb);

typedef int
(*virCgroupGetMemSwapHardLimitCB)(virCgroupPtr group,
                                  unsigned long long *kb);

264 265 266 267
typedef int
(*virCgroupGetMemSwapUsageCB)(virCgroupPtr group,
                              unsigned long long *kb);

268 269 270 271 272 273 274 275 276 277 278 279 280 281
typedef int
(*virCgroupAllowDeviceCB)(virCgroupPtr group,
                          char type,
                          int major,
                          int minor,
                          int perms);

typedef int
(*virCgroupDenyDeviceCB)(virCgroupPtr group,
                         char type,
                         int major,
                         int minor,
                         int perms);

282 283 284 285 286 287 288
typedef int
(*virCgroupAllowAllDevicesCB)(virCgroupPtr group,
                              int perms);

typedef int
(*virCgroupDenyAllDevicesCB)(virCgroupPtr group);

289 290 291 292 293 294 295 296
typedef int
(*virCgroupSetCpuSharesCB)(virCgroupPtr group,
                           unsigned long long shares);

typedef int
(*virCgroupGetCpuSharesCB)(virCgroupPtr group,
                           unsigned long long *shares);

297 298 299 300 301 302 303 304
typedef int
(*virCgroupSetCpuCfsPeriodCB)(virCgroupPtr group,
                              unsigned long long cfs_period);

typedef int
(*virCgroupGetCpuCfsPeriodCB)(virCgroupPtr group,
                              unsigned long long *cfs_period);

305 306 307 308 309 310 311 312
typedef int
(*virCgroupSetCpuCfsQuotaCB)(virCgroupPtr group,
                             long long cfs_quota);

typedef int
(*virCgroupGetCpuCfsQuotaCB)(virCgroupPtr group,
                             long long *cfs_quota);

313 314 315
typedef bool
(*virCgroupSupportsCpuBWCB)(virCgroupPtr cgroup);

316 317 318 319 320 321 322 323
typedef int
(*virCgroupGetCpuacctUsageCB)(virCgroupPtr group,
                              unsigned long long *usage);

typedef int
(*virCgroupGetCpuacctPercpuUsageCB)(virCgroupPtr group,
                                    char **usage);

324 325 326 327 328
typedef int
(*virCgroupGetCpuacctStatCB)(virCgroupPtr group,
                             unsigned long long *user,
                             unsigned long long *sys);

329 330 331 332 333 334 335 336
typedef int
(*virCgroupSetFreezerStateCB)(virCgroupPtr group,
                              const char *state);

typedef int
(*virCgroupGetFreezerStateCB)(virCgroupPtr group,
                              char **state);

337 338 339 340 341 342 343 344
typedef int
(*virCgroupSetCpusetMemsCB)(virCgroupPtr group,
                            const char *mems);

typedef int
(*virCgroupGetCpusetMemsCB)(virCgroupPtr group,
                            char **mems);

345 346 347 348 349 350 351 352
typedef int
(*virCgroupSetCpusetMemoryMigrateCB)(virCgroupPtr group,
                                     bool migrate);

typedef int
(*virCgroupGetCpusetMemoryMigrateCB)(virCgroupPtr group,
                                     bool *migrate);

353 354 355 356 357 358 359 360
typedef int
(*virCgroupSetCpusetCpusCB)(virCgroupPtr group,
                            const char *cpus);

typedef int
(*virCgroupGetCpusetCpusCB)(virCgroupPtr group,
                            char **cpus);

361 362
struct _virCgroupBackend {
    virCgroupBackendType type;
363 364 365

    /* Mandatory callbacks that need to be implemented for every backend. */
    virCgroupAvailableCB available;
366
    virCgroupValidateMachineGroupCB validateMachineGroup;
367
    virCgroupCopyMountsCB copyMounts;
368
    virCgroupCopyPlacementCB copyPlacement;
369 370
    virCgroupDetectMountsCB detectMounts;
    virCgroupDetectPlacementCB detectPlacement;
371
    virCgroupValidatePlacementCB validatePlacement;
372
    virCgroupStealPlacementCB stealPlacement;
373
    virCgroupDetectControllersCB detectControllers;
374
    virCgroupHasControllerCB hasController;
375
    virCgroupGetAnyControllerCB getAnyController;
376
    virCgroupPathOfControllerCB pathOfController;
377
    virCgroupMakeGroupCB makeGroup;
378
    virCgroupRemoveCB remove;
379
    virCgroupAddTaskCB addTask;
380
    virCgroupHasEmptyTasksCB hasEmptyTasks;
381
    virCgroupKillRecursiveCB killRecursive;
382
    virCgroupBindMountCB bindMount;
383
    virCgroupSetOwnerCB setOwner;
384 385 386 387

    /* Optional cgroup controller specific callbacks. */
    virCgroupSetBlkioWeightCB setBlkioWeight;
    virCgroupGetBlkioWeightCB getBlkioWeight;
388
    virCgroupGetBlkioIoServicedCB getBlkioIoServiced;
389
    virCgroupGetBlkioIoDeviceServicedCB getBlkioIoDeviceServiced;
390 391
    virCgroupSetBlkioDeviceWeightCB setBlkioDeviceWeight;
    virCgroupGetBlkioDeviceWeightCB getBlkioDeviceWeight;
392 393
    virCgroupSetBlkioDeviceReadIopsCB setBlkioDeviceReadIops;
    virCgroupGetBlkioDeviceReadIopsCB getBlkioDeviceReadIops;
394 395
    virCgroupSetBlkioDeviceWriteIopsCB setBlkioDeviceWriteIops;
    virCgroupGetBlkioDeviceWriteIopsCB getBlkioDeviceWriteIops;
396 397
    virCgroupSetBlkioDeviceReadBpsCB setBlkioDeviceReadBps;
    virCgroupGetBlkioDeviceReadBpsCB getBlkioDeviceReadBps;
398 399
    virCgroupSetBlkioDeviceWriteBpsCB setBlkioDeviceWriteBps;
    virCgroupGetBlkioDeviceWriteBpsCB getBlkioDeviceWriteBps;
400 401

    virCgroupSetMemoryCB setMemory;
402
    virCgroupGetMemoryStatCB getMemoryStat;
403
    virCgroupGetMemoryUsageCB getMemoryUsage;
404 405 406 407 408 409
    virCgroupSetMemoryHardLimitCB setMemoryHardLimit;
    virCgroupGetMemoryHardLimitCB getMemoryHardLimit;
    virCgroupSetMemorySoftLimitCB setMemorySoftLimit;
    virCgroupGetMemorySoftLimitCB getMemorySoftLimit;
    virCgroupSetMemSwapHardLimitCB setMemSwapHardLimit;
    virCgroupGetMemSwapHardLimitCB getMemSwapHardLimit;
410
    virCgroupGetMemSwapUsageCB getMemSwapUsage;
411 412 413

    virCgroupAllowDeviceCB allowDevice;
    virCgroupDenyDeviceCB denyDevice;
414 415
    virCgroupAllowAllDevicesCB allowAllDevices;
    virCgroupDenyAllDevicesCB denyAllDevices;
416 417 418

    virCgroupSetCpuSharesCB setCpuShares;
    virCgroupGetCpuSharesCB getCpuShares;
419 420
    virCgroupSetCpuCfsPeriodCB setCpuCfsPeriod;
    virCgroupGetCpuCfsPeriodCB getCpuCfsPeriod;
421 422
    virCgroupSetCpuCfsQuotaCB setCpuCfsQuota;
    virCgroupGetCpuCfsQuotaCB getCpuCfsQuota;
423
    virCgroupSupportsCpuBWCB supportsCpuBW;
424 425 426

    virCgroupGetCpuacctUsageCB getCpuacctUsage;
    virCgroupGetCpuacctPercpuUsageCB getCpuacctPercpuUsage;
427
    virCgroupGetCpuacctStatCB getCpuacctStat;
428 429 430

    virCgroupSetFreezerStateCB setFreezerState;
    virCgroupGetFreezerStateCB getFreezerState;
431 432 433

    virCgroupSetCpusetMemsCB setCpusetMems;
    virCgroupGetCpusetMemsCB getCpusetMems;
434 435
    virCgroupSetCpusetMemoryMigrateCB setCpusetMemoryMigrate;
    virCgroupGetCpusetMemoryMigrateCB getCpusetMemoryMigrate;
436 437
    virCgroupSetCpusetCpusCB setCpusetCpus;
    virCgroupGetCpusetCpusCB getCpusetCpus;
438 439 440 441 442 443 444 445 446 447
};
typedef struct _virCgroupBackend virCgroupBackend;
typedef virCgroupBackend *virCgroupBackendPtr;

void
virCgroupBackendRegister(virCgroupBackendPtr backend);

virCgroupBackendPtr *
virCgroupBackendGetAll(void);

448 449 450 451
virCgroupBackendPtr
virCgroupBackendForController(virCgroupPtr group,
                              unsigned int controller);

452
#define VIR_CGROUP_BACKEND_CALL(group, controller, func, ret, ...) \
453 454 455 456 457 458 459
    virCgroupBackendPtr backend = virCgroupBackendForController(group, controller); \
    if (!backend) { \
        virReportError(VIR_ERR_INTERNAL_ERROR, \
                       _("failed to get cgroup backend for '%s'"), #func); \
        return ret; \
    } \
    if (!backend->func) { \
460 461 462 463
        virReportError(VIR_ERR_OPERATION_UNSUPPORTED, \
                       _("operation '%s' not supported"), #func); \
        return ret; \
    } \
464
    return backend->func(group, ##__VA_ARGS__);