1
2
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
// SPDX-License-Identifier: GPL-3.0-or-later
#include <sys/resource.h>
#include "ebpf.h"
#include "ebpf_cgroup.h"
ebpf_cgroup_target_t *ebpf_cgroup_pids = NULL;
static void *ebpf_mapped_memory = NULL;
int send_cgroup_chart = 0;
// --------------------------------------------------------------------------------------------------------------------
// Map shared memory
/**
* Map Shared Memory locally
*
* Map the shared memory for current process
*
* @param fd file descriptor returned after shm_open was called.
* @param length length of the shared memory
*
* @return It returns a pointer to the region mapped on success and MAP_FAILED otherwise.
*/
static inline void *ebpf_cgroup_map_shm_locally(int fd, size_t length)
{
void *value;
value = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (!value) {
netdata_log_error("Cannot map shared memory used between eBPF and cgroup, integration between processes won't happen");
close(shm_fd_ebpf_cgroup);
shm_fd_ebpf_cgroup = -1;
shm_unlink(NETDATA_SHARED_MEMORY_EBPF_CGROUP_NAME);
}
return value;
}
/**
* Unmap Shared Memory
*
* Unmap shared memory used to integrate eBPF and cgroup plugin
*/
void ebpf_unmap_cgroup_shared_memory()
{
munmap(ebpf_mapped_memory, shm_ebpf_cgroup.header->body_length);
}
/**
* Map cgroup shared memory
*
* Map cgroup shared memory from cgroup to plugin
*/
void ebpf_map_cgroup_shared_memory()
{
static int limit_try = 0;
static time_t next_try = 0;
if (shm_ebpf_cgroup.header || limit_try > NETDATA_EBPF_CGROUP_MAX_TRIES)
return;
time_t curr_time = time(NULL);
if (curr_time < next_try)
return;
limit_try++;
next_try = curr_time + NETDATA_EBPF_CGROUP_NEXT_TRY_SEC;
if (shm_fd_ebpf_cgroup < 0) {
shm_fd_ebpf_cgroup = shm_open(NETDATA_SHARED_MEMORY_EBPF_CGROUP_NAME, O_RDWR, 0660);
if (shm_fd_ebpf_cgroup < 0) {
if (limit_try == NETDATA_EBPF_CGROUP_MAX_TRIES)
netdata_log_error("Shared memory was not initialized, integration between processes won't happen.");
return;
}
}
// Map only header
void *mapped = (netdata_ebpf_cgroup_shm_header_t *) ebpf_cgroup_map_shm_locally(shm_fd_ebpf_cgroup,
sizeof(netdata_ebpf_cgroup_shm_header_t));
if (unlikely(mapped == SEM_FAILED)) {
return;
}
netdata_ebpf_cgroup_shm_header_t *header = mapped;
size_t length = header->body_length;
munmap(header, sizeof(netdata_ebpf_cgroup_shm_header_t));
if (length <= ((sizeof(netdata_ebpf_cgroup_shm_header_t) + sizeof(netdata_ebpf_cgroup_shm_body_t)))) {
return;
}
ebpf_mapped_memory = (void *)ebpf_cgroup_map_shm_locally(shm_fd_ebpf_cgroup, length);
if (unlikely(ebpf_mapped_memory == MAP_FAILED)) {
return;
}
shm_ebpf_cgroup.header = ebpf_mapped_memory;
shm_ebpf_cgroup.body = ebpf_mapped_memory + sizeof(netdata_ebpf_cgroup_shm_header_t);
shm_sem_ebpf_cgroup = sem_open(NETDATA_NAMED_SEMAPHORE_EBPF_CGROUP_NAME, O_CREAT, 0660, 1);
if (shm_sem_ebpf_cgroup == SEM_FAILED) {
netdata_log_error("Cannot create semaphore, integration between eBPF and cgroup won't happen");
limit_try = NETDATA_EBPF_CGROUP_MAX_TRIES + 1;
munmap(ebpf_mapped_memory, length);
shm_ebpf_cgroup.header = NULL;
shm_ebpf_cgroup.body = NULL;
close(shm_fd_ebpf_cgroup);
shm_fd_ebpf_cgroup = -1;
shm_unlink(NETDATA_SHARED_MEMORY_EBPF_CGROUP_NAME);
}
}
// --------------------------------------------------------------------------------------------------------------------
// Close and Cleanup
/**
* Clean Specific cgroup pid
*
* Clean all PIDs associated with cgroup.
*
* @param pt structure pid on target that will have your PRs removed
*/
static inline void ebpf_clean_specific_cgroup_pids(struct pid_on_target2 *pt)
{
while (pt) {
struct pid_on_target2 *next_pid = pt->next;
freez(pt);
pt = next_pid;
}
}
/**
* Remove Cgroup Update Target Update List
*
* Remove from cgroup target and update the link list
*/
static void ebpf_remove_cgroup_target_update_list()
{
ebpf_cgroup_target_t *next, *ect = ebpf_cgroup_pids;
ebpf_cgroup_target_t *prev = ebpf_cgroup_pids;
while (ect) {
next = ect->next;
if (!ect->updated) {
if (ect == ebpf_cgroup_pids) {
ebpf_cgroup_pids = next;
prev = next;
} else {
prev->next = next;
}
ebpf_clean_specific_cgroup_pids(ect->pids);
freez(ect);
} else {
prev = ect;
}
ect = next;
}
}
// --------------------------------------------------------------------------------------------------------------------
// Fill variables
/**
* Set Target Data
*
* Set local variable values according shared memory information.
*
* @param out local output variable.
* @param ptr input from shared memory.
*/
static inline void ebpf_cgroup_set_target_data(ebpf_cgroup_target_t *out, netdata_ebpf_cgroup_shm_body_t *ptr)
{
out->hash = ptr->hash;
snprintfz(out->name, 255, "%s", ptr->name);
out->systemd = ptr->options & CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE;
out->updated = 1;
}
/**
* Find or create
*
* Find the structure inside the link list or allocate and link when it is not present.
*
* @param ptr Input from shared memory.
*
* @return It returns a pointer for the structure associated with the input.
*/
static ebpf_cgroup_target_t * ebpf_cgroup_find_or_create(netdata_ebpf_cgroup_shm_body_t *ptr)
{
ebpf_cgroup_target_t *ect, *prev;
for (ect = ebpf_cgroup_pids, prev = ebpf_cgroup_pids; ect; prev = ect, ect = ect->next) {
if (ect->hash == ptr->hash && !strcmp(ect->name, ptr->name)) {
ect->updated = 1;
return ect;
}
}
ebpf_cgroup_target_t *new_ect = callocz(1, sizeof(ebpf_cgroup_target_t));
ebpf_cgroup_set_target_data(new_ect, ptr);
if (!ebpf_cgroup_pids) {
ebpf_cgroup_pids = new_ect;
} else {
prev->next = new_ect;
}
return new_ect;
}
/**
* Update pid link list
*
* Update PIDs list associated with specific cgroup.
*
* @param ect cgroup structure where pids will be stored
* @param path file with PIDs associated to cgroup.
*/
static void ebpf_update_pid_link_list(ebpf_cgroup_target_t *ect, char *path)
{
procfile *ff = procfile_open_no_log(path, " \t:", PROCFILE_FLAG_DEFAULT);
if (!ff)
return;
ff = procfile_readall(ff);
if (!ff)
return;
size_t lines = procfile_lines(ff), l;
for (l = 0; l < lines ;l++) {
int pid = (int)str2l(procfile_lineword(ff, l, 0));
if (pid) {
struct pid_on_target2 *pt, *prev;
for (pt = ect->pids, prev = ect->pids; pt; prev = pt, pt = pt->next) {
if (pt->pid == pid)
break;
}
if (!pt) {
struct pid_on_target2 *w = callocz(1, sizeof(struct pid_on_target2));
w->pid = pid;
if (!ect->pids)
ect->pids = w;
else
prev->next = w;
}
}
}
procfile_close(ff);
}
/**
* Set remove var
*
* Set variable remove. If this variable is not reset, the structure will be removed from link list.
*/
void ebpf_reset_updated_var()
{
ebpf_cgroup_target_t *ect;
for (ect = ebpf_cgroup_pids; ect; ect = ect->next) {
ect->updated = 0;
}
}
/**
* Parse cgroup shared memory
*
* This function is responsible to copy necessary data from shared memory to local memory.
*/
void ebpf_parse_cgroup_shm_data()
{
|