diff options
author | thiagoftsm <thiagoftsm@gmail.com> | 2021-04-28 12:27:15 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-28 12:27:15 +0000 |
commit | 4e5e0a6e71cd92642b71714825611f0cee47f3d4 (patch) | |
tree | 03c70926a52e8a7aa54a4f57e65fd0f0e7e27ed6 /libnetdata | |
parent | 947268b114c1acb9f9bde6e7495affabe97208ae (diff) |
Load names (#11034)
Diffstat (limited to 'libnetdata')
-rw-r--r-- | libnetdata/ebpf/ebpf.c | 67 | ||||
-rw-r--r-- | libnetdata/ebpf/ebpf.h | 10 |
2 files changed, 65 insertions, 12 deletions
diff --git a/libnetdata/ebpf/ebpf.c b/libnetdata/ebpf/ebpf.c index 0e580733aa..985d9a674b 100644 --- a/libnetdata/ebpf/ebpf.c +++ b/libnetdata/ebpf/ebpf.c @@ -324,7 +324,61 @@ void ebpf_update_map_sizes(struct bpf_object *program, ebpf_module_t *em) } } -struct bpf_link **ebpf_load_program(char *plugins_dir, ebpf_module_t *em, char *kernel_string, struct bpf_object **obj, int *map_fd) +size_t ebpf_count_programs(struct bpf_object *obj) +{ + size_t tot = 0; + struct bpf_program *prog; + bpf_object__for_each_program(prog, obj) + { + tot++; + } + + return tot; +} + +static ebpf_specify_name_t *ebpf_find_names(ebpf_specify_name_t *names, const char *prog_name) +{ + size_t i = 0; + while (names[i].program_name) { + if (!strcmp(prog_name, names[i].program_name)) + return &names[i]; + + i++; + } + + return NULL; +} + +static struct bpf_link **ebpf_attach_programs(struct bpf_object *obj, size_t length, ebpf_specify_name_t *names) +{ + struct bpf_link **links = callocz(length , sizeof(struct bpf_link *)); + size_t i = 0; + struct bpf_program *prog; + bpf_object__for_each_program(prog, obj) + { + links[i] = bpf_program__attach(prog); + if (libbpf_get_error(links[i]) && names) { + const char *name = bpf_program__name(prog); + ebpf_specify_name_t *w = ebpf_find_names(names, name); + if (w) { + enum bpf_prog_type type = bpf_program__get_type(prog); + if (type == BPF_PROG_TYPE_KPROBE) + links[i] = bpf_program__attach_kprobe(prog, w->retprobe, w->optional); + } + } + + if (libbpf_get_error(links[i])) { + links[i] = NULL; + } + + i++; + } + + return links; +} + +struct bpf_link **ebpf_load_program(char *plugins_dir, ebpf_module_t *em, char *kernel_string, + struct bpf_object **obj, int *map_fd) { char lpath[4096]; char lname[128]; @@ -357,16 +411,9 @@ struct bpf_link **ebpf_load_program(char *plugins_dir, ebpf_module_t *em, char * i++; } - struct bpf_program *prog; - struct bpf_link **links = callocz(NETDATA_MAX_PROBES , sizeof(struct bpf_link *)); - i = 0; - bpf_object__for_each_program(prog, *obj) - { - links[i] = bpf_program__attach(prog); - i++; - } + size_t count_programs = ebpf_count_programs(*obj); - return links; + return ebpf_attach_programs(*obj, count_programs, em->names); } //---------------------------------------------------------------------------------------------------------------------- diff --git a/libnetdata/ebpf/ebpf.h b/libnetdata/ebpf/ebpf.h index 27d5b2c08f..89f8bf45fb 100644 --- a/libnetdata/ebpf/ebpf.h +++ b/libnetdata/ebpf/ebpf.h @@ -104,6 +104,13 @@ typedef struct ebpf_local_maps { uint32_t user_input; } ebpf_local_maps_t; +typedef struct ebpf_specify_name { + char *program_name; + char *function_to_attach; + char *optional; + bool retprobe; +} ebpf_specify_name_t; + typedef struct ebpf_module { const char *thread_name; const char *config_name; @@ -117,11 +124,10 @@ typedef struct ebpf_module { int optional; void (*apps_routine)(struct ebpf_module *em, void *ptr); ebpf_local_maps_t *maps; + ebpf_specify_name_t *names; uint32_t pid_map_size; } ebpf_module_t; -#define NETDATA_MAX_PROBES 64 - extern int get_kernel_version(char *out, int size); extern int get_redhat_release(); extern int has_condition_to_run(int version); |