@cryptotaxi247 / netdata-1 / commits / 4e5e0a6e7

Load names (#11034)

thiagoftsm committed Apr 28, 2021 at 12:27 UTC 4e5e0a6e71cd92642b71714825611f0cee47f3d4
3 files changed +70 -17
collectors/ebpf.plugin/ebpf.c
+5 -5
@@ -78,21 +78,21 @@ ebpf_module_t ebpf_modules[] = {
78 { .thread_name = "process", .config_name = "process", .enabled = 0, .start_routine = ebpf_process_thread,
79 .update_time = 1, .global_charts = 1, .apps_charts = 1, .mode = MODE_ENTRY,
80 .optional = 0, .apps_routine = ebpf_process_create_apps_charts, .maps = NULL,
81 - .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE},
81 + .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL},
82 { .thread_name = "socket", .config_name = "socket", .enabled = 0, .start_routine = ebpf_socket_thread,
83 .update_time = 1, .global_charts = 1, .apps_charts = 1, .mode = MODE_ENTRY,
84 .optional = 0, .apps_routine = ebpf_socket_create_apps_charts, .maps = NULL,
85 - .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE},
85 + .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL},
86 { .thread_name = "cachestat", .config_name = "cachestat", .enabled = 0, .start_routine = ebpf_cachestat_thread,
87 .update_time = 1, .global_charts = 1, .apps_charts = 1, .mode = MODE_ENTRY,
88 .optional = 0, .apps_routine = ebpf_cachestat_create_apps_charts, .maps = NULL,
89 - .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE},
89 + .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL},
90 { .thread_name = "sync", .config_name = "sync", .enabled = 0, .start_routine = ebpf_sync_thread,
91 .update_time = 1, .global_charts = 1, .apps_charts = 1, .mode = MODE_ENTRY,
92 - .optional = 0, .apps_routine = NULL, .maps = NULL, .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE },
92 + .optional = 0, .apps_routine = NULL, .maps = NULL, .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL },
93 { .thread_name = NULL, .enabled = 0, .start_routine = NULL, .update_time = 1,
94 .global_charts = 0, .apps_charts = 1, .mode = MODE_ENTRY,
95 - .optional = 0, .apps_routine = NULL, .maps = NULL, .pid_map_size = 0 },
95 + .optional = 0, .apps_routine = NULL, .maps = NULL, .pid_map_size = 0, .names = NULL },
96 };
97
98 // Link with apps.plugin
libnetdata/ebpf/ebpf.c
+57 -10
@@ -324,7 +324,61 @@ void ebpf_update_map_sizes(struct bpf_object *program, ebpf_module_t *em)
324 }
325 }
326
327 -struct bpf_link **ebpf_load_program(char *plugins_dir, ebpf_module_t *em, char *kernel_string, struct bpf_object **obj, int *map_fd)
327 +size_t ebpf_count_programs(struct bpf_object *obj)
328 +{
329 + size_t tot = 0;
330 + struct bpf_program *prog;
331 + bpf_object__for_each_program(prog, obj)
332 + {
333 + tot++;
334 + }
335 +
336 + return tot;
337 +}
338 +
339 +static ebpf_specify_name_t *ebpf_find_names(ebpf_specify_name_t *names, const char *prog_name)
340 +{
341 + size_t i = 0;
342 + while (names[i].program_name) {
343 + if (!strcmp(prog_name, names[i].program_name))
344 + return &names[i];
345 +
346 + i++;
347 + }
348 +
349 + return NULL;
350 +}
351 +
352 +static struct bpf_link **ebpf_attach_programs(struct bpf_object *obj, size_t length, ebpf_specify_name_t *names)
353 +{
354 + struct bpf_link **links = callocz(length , sizeof(struct bpf_link *));
355 + size_t i = 0;
356 + struct bpf_program *prog;
357 + bpf_object__for_each_program(prog, obj)
358 + {
359 + links[i] = bpf_program__attach(prog);
360 + if (libbpf_get_error(links[i]) && names) {
361 + const char *name = bpf_program__name(prog);
362 + ebpf_specify_name_t *w = ebpf_find_names(names, name);
363 + if (w) {
364 + enum bpf_prog_type type = bpf_program__get_type(prog);
365 + if (type == BPF_PROG_TYPE_KPROBE)
366 + links[i] = bpf_program__attach_kprobe(prog, w->retprobe, w->optional);
367 + }
368 + }
369 +
370 + if (libbpf_get_error(links[i])) {
371 + links[i] = NULL;
372 + }
373 +
374 + i++;
375 + }
376 +
377 + return links;
378 +}
379 +
380 +struct bpf_link **ebpf_load_program(char *plugins_dir, ebpf_module_t *em, char *kernel_string,
381 + struct bpf_object **obj, int *map_fd)
382 {
383 char lpath[4096];
384 char lname[128];
@@ -357,16 +411,9 @@ struct bpf_link **ebpf_load_program(char *plugins_dir, ebpf_module_t *em, char *
411 i++;
412 }
413
360 - struct bpf_program *prog;
361 - struct bpf_link **links = callocz(NETDATA_MAX_PROBES , sizeof(struct bpf_link *));
362 - i = 0;
363 - bpf_object__for_each_program(prog, *obj)
364 - {
365 - links[i] = bpf_program__attach(prog);
366 - i++;
367 - }
414 + size_t count_programs = ebpf_count_programs(*obj);
415
369 - return links;
416 + return ebpf_attach_programs(*obj, count_programs, em->names);
417 }
418
419 //----------------------------------------------------------------------------------------------------------------------
libnetdata/ebpf/ebpf.h
+8 -2
@@ -104,6 +104,13 @@ typedef struct ebpf_local_maps {
104 uint32_t user_input;
105 } ebpf_local_maps_t;
106
107 +typedef struct ebpf_specify_name {
108 + char *program_name;
109 + char *function_to_attach;
110 + char *optional;
111 + bool retprobe;
112 +} ebpf_specify_name_t;
113 +
114 typedef struct ebpf_module {
115 const char *thread_name;
116 const char *config_name;
@@ -117,11 +124,10 @@ typedef struct ebpf_module {
124 int optional;
125 void (*apps_routine)(struct ebpf_module *em, void *ptr);
126 ebpf_local_maps_t *maps;
127 + ebpf_specify_name_t *names;
128 uint32_t pid_map_size;
129 } ebpf_module_t;
130
123 -#define NETDATA_MAX_PROBES 64
124 -
131 extern int get_kernel_version(char *out, int size);
132 extern int get_redhat_release();
133 extern int has_condition_to_run(int version);