]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/cgroup.h
Merge pull request #18007 from fw-strlen/ipv6_masq_and_dnat
[thirdparty/systemd.git] / src / core / cgroup.h
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
c2f1db8f 2#pragma once
8e274523 3
c1ff5570
TA
4#include <stdbool.h>
5
6a48d82f 6#include "cgroup-util.h"
047f5d63 7#include "cpu-set-util.h"
6a48d82f 8#include "ip-address-access.h"
4ad49000 9#include "list.h"
c1ff5570 10#include "time-util.h"
8e274523 11
3a0f06c4
ZJS
12typedef struct TasksMax {
13 /* If scale == 0, just use value; otherwise, value / scale.
14 * See tasks_max_resolve(). */
15 uint64_t value;
16 uint64_t scale;
17} TasksMax;
18
19#define TASKS_MAX_UNSET ((TasksMax) { .value = UINT64_MAX, .scale = 0 })
20
21static inline bool tasks_max_isset(const TasksMax *tasks_max) {
22 return tasks_max->value != UINT64_MAX || tasks_max->scale != 0;
23}
24
25uint64_t tasks_max_resolve(const TasksMax *tasks_max);
26
4ad49000
LP
27typedef struct CGroupContext CGroupContext;
28typedef struct CGroupDeviceAllow CGroupDeviceAllow;
13c31542
TH
29typedef struct CGroupIODeviceWeight CGroupIODeviceWeight;
30typedef struct CGroupIODeviceLimit CGroupIODeviceLimit;
6ae4283c 31typedef struct CGroupIODeviceLatency CGroupIODeviceLatency;
4ad49000
LP
32typedef struct CGroupBlockIODeviceWeight CGroupBlockIODeviceWeight;
33typedef struct CGroupBlockIODeviceBandwidth CGroupBlockIODeviceBandwidth;
8e274523 34
4ad49000 35typedef enum CGroupDevicePolicy {
084870f9
ZJS
36 /* When devices listed, will allow those, plus built-in ones, if none are listed will allow
37 * everything. */
38 CGROUP_DEVICE_POLICY_AUTO,
8e274523 39
4ad49000 40 /* Everything forbidden, except built-in ones and listed ones. */
084870f9 41 CGROUP_DEVICE_POLICY_CLOSED,
9d58f1db 42
5238e957 43 /* Everything forbidden, except for the listed devices */
084870f9 44 CGROUP_DEVICE_POLICY_STRICT,
9d58f1db 45
4ad49000 46 _CGROUP_DEVICE_POLICY_MAX,
2d93c20e 47 _CGROUP_DEVICE_POLICY_INVALID = -EINVAL,
4ad49000 48} CGroupDevicePolicy;
8e274523 49
d9e45bc3
MS
50typedef enum FreezerAction {
51 FREEZER_FREEZE,
52 FREEZER_THAW,
53
54 _FREEZER_ACTION_MAX,
2d93c20e 55 _FREEZER_ACTION_INVALID = -EINVAL,
d9e45bc3
MS
56} FreezerAction;
57
4ad49000
LP
58struct CGroupDeviceAllow {
59 LIST_FIELDS(CGroupDeviceAllow, device_allow);
60 char *path;
61 bool r:1;
62 bool w:1;
63 bool m:1;
64};
8c6db833 65
13c31542
TH
66struct CGroupIODeviceWeight {
67 LIST_FIELDS(CGroupIODeviceWeight, device_weights);
68 char *path;
69 uint64_t weight;
70};
71
72struct CGroupIODeviceLimit {
73 LIST_FIELDS(CGroupIODeviceLimit, device_limits);
74 char *path;
9be57249 75 uint64_t limits[_CGROUP_IO_LIMIT_TYPE_MAX];
13c31542
TH
76};
77
6ae4283c
TH
78struct CGroupIODeviceLatency {
79 LIST_FIELDS(CGroupIODeviceLatency, device_latencies);
80 char *path;
81 usec_t target_usec;
82};
83
4ad49000
LP
84struct CGroupBlockIODeviceWeight {
85 LIST_FIELDS(CGroupBlockIODeviceWeight, device_weights);
86 char *path;
d53d9474 87 uint64_t weight;
8e274523
LP
88};
89
4ad49000
LP
90struct CGroupBlockIODeviceBandwidth {
91 LIST_FIELDS(CGroupBlockIODeviceBandwidth, device_bandwidths);
92 char *path;
979d0311
TH
93 uint64_t rbps;
94 uint64_t wbps;
4ad49000 95};
8e274523 96
4ad49000
LP
97struct CGroupContext {
98 bool cpu_accounting;
13c31542 99 bool io_accounting;
4ad49000
LP
100 bool blockio_accounting;
101 bool memory_accounting;
03a7b521 102 bool tasks_accounting;
6a48d82f 103 bool ip_accounting;
8e274523 104
afcfaa69
LP
105 /* Configures the memory.oom.group attribute (on unified) */
106 bool memory_oom_group;
107
0a6991e0
LP
108 bool delegate;
109 CGroupMask delegate_controllers;
110 CGroupMask disable_controllers;
111
13c31542 112 /* For unified hierarchy */
66ebf6c0
TH
113 uint64_t cpu_weight;
114 uint64_t startup_cpu_weight;
115 usec_t cpu_quota_per_sec_usec;
10f28641 116 usec_t cpu_quota_period_usec;
66ebf6c0 117
047f5d63
PH
118 CPUSet cpuset_cpus;
119 CPUSet cpuset_mems;
120
13c31542
TH
121 uint64_t io_weight;
122 uint64_t startup_io_weight;
123 LIST_HEAD(CGroupIODeviceWeight, io_device_weights);
124 LIST_HEAD(CGroupIODeviceLimit, io_device_limits);
6ae4283c 125 LIST_HEAD(CGroupIODeviceLatency, io_device_latencies);
13c31542 126
7ad5439e 127 uint64_t default_memory_min;
c52db42b 128 uint64_t default_memory_low;
48422635 129 uint64_t memory_min;
da4d897e
TH
130 uint64_t memory_low;
131 uint64_t memory_high;
132 uint64_t memory_max;
96e131ea 133 uint64_t memory_swap_max;
da4d897e 134
7ad5439e 135 bool default_memory_min_set;
c52db42b 136 bool default_memory_low_set;
7ad5439e 137 bool memory_min_set;
c52db42b
CD
138 bool memory_low_set;
139
6a48d82f
DM
140 LIST_HEAD(IPAddressAccessItem, ip_address_allow);
141 LIST_HEAD(IPAddressAccessItem, ip_address_deny);
142
fab34748
KL
143 char **ip_filters_ingress;
144 char **ip_filters_egress;
145
13c31542 146 /* For legacy hierarchies */
d53d9474
LP
147 uint64_t cpu_shares;
148 uint64_t startup_cpu_shares;
8e274523 149
d53d9474
LP
150 uint64_t blockio_weight;
151 uint64_t startup_blockio_weight;
4ad49000
LP
152 LIST_HEAD(CGroupBlockIODeviceWeight, blockio_device_weights);
153 LIST_HEAD(CGroupBlockIODeviceBandwidth, blockio_device_bandwidths);
246aa6dd 154
4ad49000 155 uint64_t memory_limit;
64747e2d 156
4ad49000
LP
157 CGroupDevicePolicy device_policy;
158 LIST_HEAD(CGroupDeviceAllow, device_allow);
a931ad47 159
13c31542 160 /* Common */
3a0f06c4 161 TasksMax tasks_max;
4d824a4e
AZ
162
163 /* Settings for systemd-oomd */
164 ManagedOOMMode moom_swap;
165 ManagedOOMMode moom_mem_pressure;
0a9f9344 166 uint32_t moom_mem_pressure_limit_permyriad;
4e806bfa 167 ManagedOOMPreference moom_preference;
4ad49000 168};
64747e2d 169
906c06f6
DM
170/* Used when querying IP accounting data */
171typedef enum CGroupIPAccountingMetric {
172 CGROUP_IP_INGRESS_BYTES,
173 CGROUP_IP_INGRESS_PACKETS,
174 CGROUP_IP_EGRESS_BYTES,
175 CGROUP_IP_EGRESS_PACKETS,
176 _CGROUP_IP_ACCOUNTING_METRIC_MAX,
2d93c20e 177 _CGROUP_IP_ACCOUNTING_METRIC_INVALID = -EINVAL,
906c06f6
DM
178} CGroupIPAccountingMetric;
179
fbe14fc9
LP
180/* Used when querying IO accounting data */
181typedef enum CGroupIOAccountingMetric {
182 CGROUP_IO_READ_BYTES,
183 CGROUP_IO_WRITE_BYTES,
184 CGROUP_IO_READ_OPERATIONS,
185 CGROUP_IO_WRITE_OPERATIONS,
186 _CGROUP_IO_ACCOUNTING_METRIC_MAX,
2d93c20e 187 _CGROUP_IO_ACCOUNTING_METRIC_INVALID = -EINVAL,
fbe14fc9
LP
188} CGroupIOAccountingMetric;
189
90a8f0b9
FS
190typedef struct Unit Unit;
191typedef struct Manager Manager;
8e274523 192
10f28641
FB
193usec_t cgroup_cpu_adjust_period(usec_t period, usec_t quota, usec_t resolution, usec_t max_period);
194
4ad49000
LP
195void cgroup_context_init(CGroupContext *c);
196void cgroup_context_done(CGroupContext *c);
bc0623df 197void cgroup_context_dump(Unit *u, FILE* f, const char *prefix);
bc432dc7 198
4ad49000 199void cgroup_context_free_device_allow(CGroupContext *c, CGroupDeviceAllow *a);
13c31542
TH
200void cgroup_context_free_io_device_weight(CGroupContext *c, CGroupIODeviceWeight *w);
201void cgroup_context_free_io_device_limit(CGroupContext *c, CGroupIODeviceLimit *l);
6ae4283c 202void cgroup_context_free_io_device_latency(CGroupContext *c, CGroupIODeviceLatency *l);
4ad49000
LP
203void cgroup_context_free_blockio_device_weight(CGroupContext *c, CGroupBlockIODeviceWeight *w);
204void cgroup_context_free_blockio_device_bandwidth(CGroupContext *c, CGroupBlockIODeviceBandwidth *b);
8e274523 205
fd870bac
YW
206int cgroup_add_device_allow(CGroupContext *c, const char *dev, const char *mode);
207
4e806bfa
AZ
208void cgroup_oomd_xattr_apply(Unit *u, const char *cgroup_path);
209
efdb0237 210CGroupMask unit_get_own_mask(Unit *u);
02638280 211CGroupMask unit_get_delegate_mask(Unit *u);
efdb0237 212CGroupMask unit_get_members_mask(Unit *u);
02638280 213CGroupMask unit_get_siblings_mask(Unit *u);
c72703e2
CD
214CGroupMask unit_get_ancestor_disable_mask(Unit *u);
215
efdb0237
LP
216CGroupMask unit_get_target_mask(Unit *u);
217CGroupMask unit_get_enable_mask(Unit *u);
bc432dc7 218
5af88058 219void unit_invalidate_cgroup_members_masks(Unit *u);
efdb0237 220
4c591f39 221void unit_add_family_to_cgroup_realize_queue(Unit *u);
27adcc97 222
6592b975 223const char *unit_get_realized_cgroup_path(Unit *u, CGroupMask mask);
303ee601 224char *unit_default_cgroup_path(const Unit *u);
efdb0237 225int unit_set_cgroup_path(Unit *u, const char *path);
a4634b21 226int unit_pick_cgroup_path(Unit *u);
efdb0237 227
0a1eb06d 228int unit_realize_cgroup(Unit *u);
efdb0237
LP
229void unit_prune_cgroup(Unit *u);
230int unit_watch_cgroup(Unit *u);
afcfaa69 231int unit_watch_cgroup_memory(Unit *u);
efdb0237 232
e08dabfe
AZ
233void unit_release_cgroup(Unit *u);
234/* Releases the cgroup only if it is recursively empty.
235 * Returns true if the cgroup was released, false otherwise. */
236bool unit_maybe_release_cgroup(Unit *u);
237
09e24654 238void unit_add_to_cgroup_empty_queue(Unit *u);
fe8d22fb 239int unit_check_oomd_kill(Unit *u);
2ba6ae6b 240int unit_check_oom(Unit *u);
09e24654 241
6592b975 242int unit_attach_pids_to_cgroup(Unit *u, Set *pids, const char *suffix_path);
8e274523 243
4ad49000
LP
244int manager_setup_cgroup(Manager *m);
245void manager_shutdown_cgroup(Manager *m, bool delete);
6dde1f33 246
91a6073e 247unsigned manager_dispatch_cgroup_realize_queue(Manager *m);
4fbf50b3 248
4ad49000 249Unit *manager_get_unit_by_cgroup(Manager *m, const char *cgroup);
b3ac818b 250Unit *manager_get_unit_by_pid_cgroup(Manager *m, pid_t pid);
4ad49000 251Unit* manager_get_unit_by_pid(Manager *m, pid_t pid);
8e274523 252
7ad5439e 253uint64_t unit_get_ancestor_memory_min(Unit *u);
c52db42b
CD
254uint64_t unit_get_ancestor_memory_low(Unit *u);
255
efdb0237
LP
256int unit_search_main_pid(Unit *u, pid_t *ret);
257int unit_watch_all_pids(Unit *u);
8e274523 258
11aef522
LP
259int unit_synthesize_cgroup_empty_event(Unit *u);
260
5ad096b3 261int unit_get_memory_current(Unit *u, uint64_t *ret);
03a7b521 262int unit_get_tasks_current(Unit *u, uint64_t *ret);
5ad096b3 263int unit_get_cpu_usage(Unit *u, nsec_t *ret);
fbe14fc9 264int unit_get_io_accounting(Unit *u, CGroupIOAccountingMetric metric, bool allow_cache, uint64_t *ret);
906c06f6
DM
265int unit_get_ip_accounting(Unit *u, CGroupIPAccountingMetric metric, uint64_t *ret);
266
267int unit_reset_cpu_accounting(Unit *u);
268int unit_reset_ip_accounting(Unit *u);
fbe14fc9 269int unit_reset_io_accounting(Unit *u);
9b2559a1 270int unit_reset_accounting(Unit *u);
5ad096b3 271
2e4025c0
ZJS
272#define UNIT_CGROUP_BOOL(u, name) \
273 ({ \
274 CGroupContext *cc = unit_get_cgroup_context(u); \
275 cc ? cc->name : false; \
276 })
e9db43d5 277
611c4f8a
LP
278bool manager_owns_host_root_cgroup(Manager *m);
279bool unit_has_host_root_cgroup(Unit *u);
f3725e64 280
efdb0237
LP
281int manager_notify_cgroup_empty(Manager *m, const char *group);
282
e7ab4d1a 283void unit_invalidate_cgroup(Unit *u, CGroupMask m);
906c06f6 284void unit_invalidate_cgroup_bpf(Unit *u);
e7ab4d1a
LP
285
286void manager_invalidate_startup_units(Manager *m);
287
4ad49000
LP
288const char* cgroup_device_policy_to_string(CGroupDevicePolicy i) _const_;
289CGroupDevicePolicy cgroup_device_policy_from_string(const char *s) _pure_;
1d9cc876
LP
290
291bool unit_cgroup_delegate(Unit *u);
da8e1782
MO
292
293int compare_job_priority(const void *a, const void *b);
047f5d63
PH
294
295int unit_get_cpuset(Unit *u, CPUSet *cpus, const char *name);
d9e45bc3
MS
296int unit_cgroup_freezer_action(Unit *u, FreezerAction action);
297
298const char* freezer_action_to_string(FreezerAction a) _const_;
299FreezerAction freezer_action_from_string(const char *s) _pure_;