]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/cgroup.h
tree-wide: drop 'This file is part of systemd' blurb
[thirdparty/systemd.git] / src / core / cgroup.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5 Copyright 2013 Lennart Poettering
6 ***/
7
8 #include <stdbool.h>
9
10 #include "cgroup-util.h"
11 #include "ip-address-access.h"
12 #include "list.h"
13 #include "time-util.h"
14
15 typedef struct CGroupContext CGroupContext;
16 typedef struct CGroupDeviceAllow CGroupDeviceAllow;
17 typedef struct CGroupIODeviceWeight CGroupIODeviceWeight;
18 typedef struct CGroupIODeviceLimit CGroupIODeviceLimit;
19 typedef struct CGroupBlockIODeviceWeight CGroupBlockIODeviceWeight;
20 typedef struct CGroupBlockIODeviceBandwidth CGroupBlockIODeviceBandwidth;
21
22 typedef enum CGroupDevicePolicy {
23
24 /* When devices listed, will allow those, plus built-in ones,
25 if none are listed will allow everything. */
26 CGROUP_AUTO,
27
28 /* Everything forbidden, except built-in ones and listed ones. */
29 CGROUP_CLOSED,
30
31 /* Everythings forbidden, except for the listed devices */
32 CGROUP_STRICT,
33
34 _CGROUP_DEVICE_POLICY_MAX,
35 _CGROUP_DEVICE_POLICY_INVALID = -1
36 } CGroupDevicePolicy;
37
38 struct CGroupDeviceAllow {
39 LIST_FIELDS(CGroupDeviceAllow, device_allow);
40 char *path;
41 bool r:1;
42 bool w:1;
43 bool m:1;
44 };
45
46 struct CGroupIODeviceWeight {
47 LIST_FIELDS(CGroupIODeviceWeight, device_weights);
48 char *path;
49 uint64_t weight;
50 };
51
52 struct CGroupIODeviceLimit {
53 LIST_FIELDS(CGroupIODeviceLimit, device_limits);
54 char *path;
55 uint64_t limits[_CGROUP_IO_LIMIT_TYPE_MAX];
56 };
57
58 struct CGroupBlockIODeviceWeight {
59 LIST_FIELDS(CGroupBlockIODeviceWeight, device_weights);
60 char *path;
61 uint64_t weight;
62 };
63
64 struct CGroupBlockIODeviceBandwidth {
65 LIST_FIELDS(CGroupBlockIODeviceBandwidth, device_bandwidths);
66 char *path;
67 uint64_t rbps;
68 uint64_t wbps;
69 };
70
71 struct CGroupContext {
72 bool cpu_accounting;
73 bool io_accounting;
74 bool blockio_accounting;
75 bool memory_accounting;
76 bool tasks_accounting;
77 bool ip_accounting;
78
79 /* For unified hierarchy */
80 uint64_t cpu_weight;
81 uint64_t startup_cpu_weight;
82 usec_t cpu_quota_per_sec_usec;
83
84 uint64_t io_weight;
85 uint64_t startup_io_weight;
86 LIST_HEAD(CGroupIODeviceWeight, io_device_weights);
87 LIST_HEAD(CGroupIODeviceLimit, io_device_limits);
88
89 uint64_t memory_low;
90 uint64_t memory_high;
91 uint64_t memory_max;
92 uint64_t memory_swap_max;
93
94 LIST_HEAD(IPAddressAccessItem, ip_address_allow);
95 LIST_HEAD(IPAddressAccessItem, ip_address_deny);
96
97 /* For legacy hierarchies */
98 uint64_t cpu_shares;
99 uint64_t startup_cpu_shares;
100
101 uint64_t blockio_weight;
102 uint64_t startup_blockio_weight;
103 LIST_HEAD(CGroupBlockIODeviceWeight, blockio_device_weights);
104 LIST_HEAD(CGroupBlockIODeviceBandwidth, blockio_device_bandwidths);
105
106 uint64_t memory_limit;
107
108 CGroupDevicePolicy device_policy;
109 LIST_HEAD(CGroupDeviceAllow, device_allow);
110
111 /* Common */
112 uint64_t tasks_max;
113
114 bool delegate;
115 CGroupMask delegate_controllers;
116 };
117
118 /* Used when querying IP accounting data */
119 typedef enum CGroupIPAccountingMetric {
120 CGROUP_IP_INGRESS_BYTES,
121 CGROUP_IP_INGRESS_PACKETS,
122 CGROUP_IP_EGRESS_BYTES,
123 CGROUP_IP_EGRESS_PACKETS,
124 _CGROUP_IP_ACCOUNTING_METRIC_MAX,
125 _CGROUP_IP_ACCOUNTING_METRIC_INVALID = -1,
126 } CGroupIPAccountingMetric;
127
128 typedef struct Unit Unit;
129 typedef struct Manager Manager;
130
131 void cgroup_context_init(CGroupContext *c);
132 void cgroup_context_done(CGroupContext *c);
133 void cgroup_context_dump(CGroupContext *c, FILE* f, const char *prefix);
134
135 CGroupMask cgroup_context_get_mask(CGroupContext *c);
136
137 void cgroup_context_free_device_allow(CGroupContext *c, CGroupDeviceAllow *a);
138 void cgroup_context_free_io_device_weight(CGroupContext *c, CGroupIODeviceWeight *w);
139 void cgroup_context_free_io_device_limit(CGroupContext *c, CGroupIODeviceLimit *l);
140 void cgroup_context_free_blockio_device_weight(CGroupContext *c, CGroupBlockIODeviceWeight *w);
141 void cgroup_context_free_blockio_device_bandwidth(CGroupContext *c, CGroupBlockIODeviceBandwidth *b);
142
143 CGroupMask unit_get_own_mask(Unit *u);
144 CGroupMask unit_get_delegate_mask(Unit *u);
145 CGroupMask unit_get_members_mask(Unit *u);
146 CGroupMask unit_get_siblings_mask(Unit *u);
147 CGroupMask unit_get_subtree_mask(Unit *u);
148
149 CGroupMask unit_get_target_mask(Unit *u);
150 CGroupMask unit_get_enable_mask(Unit *u);
151
152 bool unit_get_needs_bpf(Unit *u);
153
154 void unit_update_cgroup_members_masks(Unit *u);
155
156 const char *unit_get_realized_cgroup_path(Unit *u, CGroupMask mask);
157 char *unit_default_cgroup_path(Unit *u);
158 int unit_set_cgroup_path(Unit *u, const char *path);
159 int unit_pick_cgroup_path(Unit *u);
160
161 int unit_realize_cgroup(Unit *u);
162 void unit_release_cgroup(Unit *u);
163 void unit_prune_cgroup(Unit *u);
164 int unit_watch_cgroup(Unit *u);
165
166 void unit_add_to_cgroup_empty_queue(Unit *u);
167
168 int unit_attach_pids_to_cgroup(Unit *u, Set *pids, const char *suffix_path);
169
170 int manager_setup_cgroup(Manager *m);
171 void manager_shutdown_cgroup(Manager *m, bool delete);
172
173 unsigned manager_dispatch_cgroup_realize_queue(Manager *m);
174
175 Unit *manager_get_unit_by_cgroup(Manager *m, const char *cgroup);
176 Unit *manager_get_unit_by_pid_cgroup(Manager *m, pid_t pid);
177 Unit* manager_get_unit_by_pid(Manager *m, pid_t pid);
178
179 int unit_search_main_pid(Unit *u, pid_t *ret);
180 int unit_watch_all_pids(Unit *u);
181
182 int unit_synthesize_cgroup_empty_event(Unit *u);
183
184 int unit_get_memory_current(Unit *u, uint64_t *ret);
185 int unit_get_tasks_current(Unit *u, uint64_t *ret);
186 int unit_get_cpu_usage(Unit *u, nsec_t *ret);
187 int unit_get_ip_accounting(Unit *u, CGroupIPAccountingMetric metric, uint64_t *ret);
188
189 int unit_reset_cpu_accounting(Unit *u);
190 int unit_reset_ip_accounting(Unit *u);
191
192 #define UNIT_CGROUP_BOOL(u, name) \
193 ({ \
194 CGroupContext *cc = unit_get_cgroup_context(u); \
195 cc ? cc->name : false; \
196 })
197
198 bool manager_owns_root_cgroup(Manager *m);
199 bool unit_has_root_cgroup(Unit *u);
200
201 int manager_notify_cgroup_empty(Manager *m, const char *group);
202
203 void unit_invalidate_cgroup(Unit *u, CGroupMask m);
204 void unit_invalidate_cgroup_bpf(Unit *u);
205
206 void manager_invalidate_startup_units(Manager *m);
207
208 const char* cgroup_device_policy_to_string(CGroupDevicePolicy i) _const_;
209 CGroupDevicePolicy cgroup_device_policy_from_string(const char *s) _pure_;
210
211 bool unit_cgroup_delegate(Unit *u);