Ebpf support new collectors (#10680)
Extend original support from kprobe for all available eBPF programs and allow `eBPF.plugin` to use some Netdata features.
thiagoftsm committed
Feb 25, 2021 at 16:17 UTC
2bddc7a0f4ad406bf45f526a853b3d025839bc3b
6 files changed
+67
-35
collectors/ebpf.plugin/ebpf.c
+29
-24
@@ -79,13 +79,13 @@ pthread_cond_t collect_data_cond_var;
79
ebpf_module_t ebpf_modules[] = {
80
{ .thread_name = "process", .config_name = "process", .enabled = 0, .start_routine = ebpf_process_thread,
81
.update_time = 1, .global_charts = 1, .apps_charts = 1, .mode = MODE_ENTRY,
82
- .optional = 0 },
82
+ .optional = 0, .apps_routine = ebpf_process_create_apps_charts },
83
{ .thread_name = "socket", .config_name = "socket", .enabled = 0, .start_routine = ebpf_socket_thread,
84
.update_time = 1, .global_charts = 1, .apps_charts = 1, .mode = MODE_ENTRY,
85
- .optional = 0 },
85
+ .optional = 0, .apps_routine = ebpf_socket_create_apps_charts },
86
{ .thread_name = NULL, .enabled = 0, .start_routine = NULL, .update_time = 1,
87
.global_charts = 0, .apps_charts = 1, .mode = MODE_ENTRY,
88
- .optional = 0 },
88
+ .optional = 0, .apps_routine = NULL },
89
};
90
91
// Link with apps.plugin
@@ -343,23 +343,26 @@ void write_io_chart(char *chart, char *family, char *dwrite, long long vwrite, c
343
/**
344
* Write chart cmd on standard output
345
*
346
- * @param type the chart type
347
- * @param id the chart id
348
- * @param title the chart title
349
- * @param units the units label
350
- * @param family the group name used to attach the chart on dashaboard
351
- * @param charttype the chart type
352
- * @param order the chart order
346
+ * @param type chart type
347
+ * @param id chart id
348
+ * @param title chart title
349
+ * @param units units label
350
+ * @param family group name used to attach the chart on dashaboard
351
+ * @param charttype chart type
352
+ * @param context chart context
353
+ * @param order chart order
354
*/
354
-void ebpf_write_chart_cmd(char *type, char *id, char *title, char *units, char *family, char *charttype, int order)
355
+void ebpf_write_chart_cmd(char *type, char *id, char *title, char *units, char *family,
356
+ char *charttype, char *context, int order)
357
{
356
- printf("CHART %s.%s '' '%s' '%s' '%s' '' %s %d %d\n",
358
+ printf("CHART %s.%s '' '%s' '%s' '%s' '%s' '%s' %d %d\n",
359
type,
360
id,
361
title,
362
units,
361
- family,
362
- charttype,
363
+ (family)?family:"",
364
+ (context)?context:"",
365
+ (charttype)?charttype:"",
366
order,
367
update_every);
368
}
@@ -398,26 +401,28 @@ void ebpf_create_global_dimension(void *ptr, int end)
401
/**
402
* Call write_chart_cmd to create the charts
403
*
401
- * @param type the chart type
402
- * @param id the chart id
403
- * @param units the axis label
404
- * @param family the group name used to attach the chart on dashaboard
405
- * @param order the order number of the specified chart
406
- * @param ncd a pointer to a function called to create dimensions
407
- * @param move a pointer for a structure that has the dimensions
408
- * @param end number of dimensions for the chart created
404
+ * @param type chart type
405
+ * @param id chart id
406
+ * @param units axis label
407
+ * @param family group name used to attach the chart on dashaboard
408
+ * @param order order number of the specified chart
409
+ * @param context chart context
410
+ * @param ncd a pointer to a function called to create dimensions
411
+ * @param move a pointer for a structure that has the dimensions
412
+ * @param end number of dimensions for the chart created
413
*/
414
void ebpf_create_chart(char *type,
415
char *id,
416
char *title,
417
char *units,
418
char *family,
419
+ char *context,
420
int order,
421
void (*ncd)(void *, int),
422
void *move,
423
int end)
424
{
420
- ebpf_write_chart_cmd(type, id, title, units, family, "line", order);
425
+ ebpf_write_chart_cmd(type, id, title, units, family, "line", context, order);
426
427
ncd(move, end);
428
}
@@ -437,7 +442,7 @@ void ebpf_create_charts_on_apps(char *id, char *title, char *units, char *family
442
char *algorithm, struct target *root)
443
{
444
struct target *w;
440
- ebpf_write_chart_cmd(NETDATA_APPS_FAMILY, id, title, units, family, "stacked", order);
445
+ ebpf_write_chart_cmd(NETDATA_APPS_FAMILY, id, title, units, family, "stacked", NULL, order);
446
447
for (w = root; w; w = w->next) {
448
if (unlikely(w->exposed))
collectors/ebpf.plugin/ebpf.h
+4
-1
@@ -133,6 +133,7 @@ extern void ebpf_write_chart_cmd(char *type,
133
char *units,
134
char *family,
135
char *charttype,
136
+ char *context,
137
int order);
138
139
extern void ebpf_write_global_dimension(char *name, char *id, char *algorithm);
@@ -144,6 +145,7 @@ extern void ebpf_create_chart(char *type,
145
char *title,
146
char *units,
147
char *family,
148
+ char *context,
149
int order,
150
void (*ncd)(void *, int),
151
void *move,
@@ -194,7 +196,8 @@ extern char *ebpf_algorithms[];
196
197
// Socket functions and variables
198
// Common functions
197
-extern void ebpf_socket_create_apps_charts(ebpf_module_t *em, struct target *root);
199
+extern void ebpf_process_create_apps_charts(struct ebpf_module *em, void *ptr);
200
+extern void ebpf_socket_create_apps_charts(struct ebpf_module *em, void *ptr);
201
extern collected_number get_value_from_structure(char *basis, size_t offset);
202
extern struct pid_stat *root_of_pids;
203
extern ebpf_process_stat_t *global_process_stat;
collectors/ebpf.plugin/ebpf_process.c
+17
-7
@@ -520,6 +520,7 @@ static void ebpf_create_global_charts(ebpf_module_t *em)
520
"Open and close calls",
521
EBPF_COMMON_DIMENSION_CALL,
522
NETDATA_FILE_GROUP,
523
+ NULL,
524
21000,
525
ebpf_create_global_dimension,
526
process_publish_aggregated,
@@ -531,6 +532,7 @@ static void ebpf_create_global_charts(ebpf_module_t *em)
532
"Open fails",
533
EBPF_COMMON_DIMENSION_CALL,
534
NETDATA_FILE_GROUP,
535
+ NULL,
536
21001,
537
ebpf_create_global_dimension,
538
process_publish_aggregated,
@@ -542,6 +544,7 @@ static void ebpf_create_global_charts(ebpf_module_t *em)
544
"Remove files",
545
EBPF_COMMON_DIMENSION_CALL,
546
NETDATA_VFS_GROUP,
547
+ NULL,
548
21002,
549
ebpf_create_global_dimension,
550
&process_publish_aggregated[NETDATA_DEL_START],
@@ -552,6 +555,7 @@ static void ebpf_create_global_charts(ebpf_module_t *em)
555
"Calls to IO",
556
EBPF_COMMON_DIMENSION_CALL,
557
NETDATA_VFS_GROUP,
558
+ NULL,
559
21003,
560
ebpf_create_global_dimension,
561
&process_publish_aggregated[NETDATA_IN_START_BYTE],
@@ -569,6 +573,7 @@ static void ebpf_create_global_charts(ebpf_module_t *em)
573
"Fails to write or read",
574
EBPF_COMMON_DIMENSION_CALL,
575
NETDATA_VFS_GROUP,
576
+ NULL,
577
21005,
578
ebpf_create_global_dimension,
579
&process_publish_aggregated[2],
@@ -580,6 +585,7 @@ static void ebpf_create_global_charts(ebpf_module_t *em)
585
"Start process",
586
EBPF_COMMON_DIMENSION_CALL,
587
NETDATA_PROCESS_GROUP,
588
+ NULL,
589
21006,
590
ebpf_create_global_dimension,
591
&process_publish_aggregated[NETDATA_PROCESS_START],
@@ -590,6 +596,7 @@ static void ebpf_create_global_charts(ebpf_module_t *em)
596
"Exit process",
597
EBPF_COMMON_DIMENSION_CALL,
598
NETDATA_PROCESS_GROUP,
599
+ NULL,
600
21007,
601
ebpf_create_global_dimension,
602
&process_publish_aggregated[NETDATA_EXIT_START],
@@ -608,6 +615,7 @@ static void ebpf_create_global_charts(ebpf_module_t *em)
615
"Fails to create process",
616
EBPF_COMMON_DIMENSION_CALL,
617
NETDATA_PROCESS_GROUP,
618
+ NULL,
619
21009,
620
ebpf_create_global_dimension,
621
&process_publish_aggregated[NETDATA_PROCESS_START],
@@ -621,10 +629,11 @@ static void ebpf_create_global_charts(ebpf_module_t *em)
629
* Call ebpf_create_chart to create the charts on apps submenu.
630
*
631
* @param em a pointer to the structure with the default values.
624
- * @param root a pointer for the targets.
632
+ * @param ptr a pointer for the targets.
633
*/
626
-static void ebpf_process_create_apps_charts(ebpf_module_t *em, struct target *root)
634
+void ebpf_process_create_apps_charts(struct ebpf_module *em, void *ptr)
635
{
636
+ struct target *root = ptr;
637
ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_FILE_OPEN,
638
"Number of open files",
639
EBPF_COMMON_DIMENSION_CALL,
@@ -786,11 +795,12 @@ static void ebpf_create_apps_charts(ebpf_module_t *em, struct target *root)
795
if (!newly_added)
796
return;
797
789
- if (ebpf_modules[EBPF_MODULE_PROCESS_IDX].apps_charts)
790
- ebpf_process_create_apps_charts(em, root);
791
-
792
- if (ebpf_modules[EBPF_MODULE_SOCKET_IDX].apps_charts)
793
- ebpf_socket_create_apps_charts(NULL, root);
798
+ int counter;
799
+ for (counter = 0; ebpf_modules[counter].thread_name; counter++) {
800
+ ebpf_module_t *current = &ebpf_modules[counter];
801
+ if (current->apps_charts && current->apps_routine)
802
+ current->apps_routine(em, root);
803
+ }
804
}
805
806
/*****************************************************************
collectors/ebpf.plugin/ebpf_socket.c
+13
-2
@@ -427,6 +427,7 @@ static void ebpf_create_global_charts(ebpf_module_t *em)
427
"Calls to internal functions",
428
EBPF_COMMON_DIMENSION_CALL,
429
NETDATA_SOCKET_GROUP,
430
+ NULL,
431
21070,
432
ebpf_create_global_dimension,
433
socket_publish_aggregated,
@@ -435,6 +436,7 @@ static void ebpf_create_global_charts(ebpf_module_t *em)
436
ebpf_create_chart(NETDATA_EBPF_FAMILY, NETDATA_TCP_FUNCTION_BITS,
437
"TCP bandwidth", EBPF_COMMON_DIMENSION_BITS,
438
NETDATA_SOCKET_GROUP,
439
+ NULL,
440
21071,
441
ebpf_create_global_dimension,
442
socket_publish_aggregated,
@@ -446,6 +448,7 @@ static void ebpf_create_global_charts(ebpf_module_t *em)
448
"TCP errors",
449
EBPF_COMMON_DIMENSION_CALL,
450
NETDATA_SOCKET_GROUP,
451
+ NULL,
452
21072,
453
ebpf_create_global_dimension,
454
socket_publish_aggregated,
@@ -457,6 +460,7 @@ static void ebpf_create_global_charts(ebpf_module_t *em)
460
"Packages retransmitted",
461
EBPF_COMMON_DIMENSION_CALL,
462
NETDATA_SOCKET_GROUP,
463
+ NULL,
464
21073,
465
ebpf_create_global_dimension,
466
&socket_publish_aggregated[NETDATA_RETRANSMIT_START],
@@ -467,6 +471,7 @@ static void ebpf_create_global_charts(ebpf_module_t *em)
471
"UDP calls",
472
EBPF_COMMON_DIMENSION_CALL,
473
NETDATA_SOCKET_GROUP,
474
+ NULL,
475
21074,
476
ebpf_create_global_dimension,
477
&socket_publish_aggregated[NETDATA_UDP_START],
@@ -475,6 +480,7 @@ static void ebpf_create_global_charts(ebpf_module_t *em)
480
ebpf_create_chart(NETDATA_EBPF_FAMILY, NETDATA_UDP_FUNCTION_BITS,
481
"UDP bandwidth", EBPF_COMMON_DIMENSION_BITS,
482
NETDATA_SOCKET_GROUP,
483
+ NULL,
484
21075,
485
ebpf_create_global_dimension,
486
&socket_publish_aggregated[NETDATA_UDP_START],
@@ -486,6 +492,7 @@ static void ebpf_create_global_charts(ebpf_module_t *em)
492
"UDP errors",
493
EBPF_COMMON_DIMENSION_CALL,
494
NETDATA_SOCKET_GROUP,
495
+ NULL,
496
21076,
497
ebpf_create_global_dimension,
498
&socket_publish_aggregated[NETDATA_UDP_START],
@@ -498,11 +505,13 @@ static void ebpf_create_global_charts(ebpf_module_t *em)
505
*
506
* Call ebpf_create_chart to create the charts on apps submenu.
507
*
501
- * @param em a pointer to the structure with the default values.
508
+ * @param em a pointer to the structure with the default values.
509
+ * @param ptr a pointer for targets
510
*/
503
-void ebpf_socket_create_apps_charts(ebpf_module_t *em, struct target *root)
511
+void ebpf_socket_create_apps_charts(struct ebpf_module *em, void *ptr)
512
{
513
UNUSED(em);
514
+ struct target *root = ptr;;
515
ebpf_create_charts_on_apps(NETDATA_NET_APPS_BANDWIDTH_SENT,
516
"Bytes sent", EBPF_COMMON_DIMENSION_BITS,
517
NETDATA_APPS_NET_GROUP,
@@ -581,6 +590,7 @@ static void ebpf_socket_create_nv_chart(char *id, char *title, char *units,
590
units,
591
family,
592
"stacked",
593
+ NULL,
594
order);
595
596
uint32_t i;
@@ -617,6 +627,7 @@ static void ebpf_socket_create_nv_retransmit(char *id, char *title, char *units,
627
units,
628
family,
629
"stacked",
630
+ NULL,
631
order);
632
633
uint32_t i;
libnetdata/ebpf/ebpf.c
+3
-1
@@ -295,7 +295,9 @@ struct bpf_link **ebpf_load_program(char *plugins_dir, ebpf_module_t *em, char *
295
return NULL;
296
297
snprintf(lpath, 4096, "%s/%s", plugins_dir, lname);
298
- if (bpf_prog_load(lpath, BPF_PROG_TYPE_KPROBE, obj, &prog_fd)) {
298
+ // We are using BPF_PROG_TYPE_UNSPEC instead a specific type for bpf_prog_load to define the type
299
+ // according the eBPF program loaded
300
+ if (bpf_prog_load(lpath, BPF_PROG_TYPE_UNSPEC, obj, &prog_fd)) {
301
em->enabled = CONFIG_BOOLEAN_NO;
302
info("Cannot load program: %s", lpath);
303
return NULL;
libnetdata/ebpf/ebpf.h
+1
@@ -87,6 +87,7 @@ typedef struct ebpf_module {
87
netdata_run_mode_t mode;
88
uint32_t thread_id;
89
int optional;
90
+ void (*apps_routine)(struct ebpf_module *em, void *ptr);
91
} ebpf_module_t;
92
93
#define NETDATA_MAX_PROBES 64