eBPF - Network Viewer (Move code) (#17018)
thiagoftsm committed
Feb 16, 2024 at 13:51 UTC
23e9e990bad6e988801898514b8c0f295dedb885
6 files changed
+81
-72
src/collectors/ebpf.plugin/ebpf.c
+3
-45
@@ -3050,48 +3050,6 @@ static inline void ebpf_load_thread_config()
3050
}
3051
}
3052
3053
-/**
3054
- * Check Conditions
3055
- *
3056
- * This function checks kernel that plugin is running and permissions.
3057
- *
3058
- * @return It returns 0 on success and -1 otherwise
3059
- */
3060
-int ebpf_check_conditions()
3061
-{
3062
- if (!has_condition_to_run(running_on_kernel)) {
3063
- netdata_log_error("The current collector cannot run on this kernel.");
3064
- return -1;
3065
- }
3066
-
3067
- if (!am_i_running_as_root()) {
3068
- netdata_log_error(
3069
- "ebpf.plugin should either run as root (now running with uid %u, euid %u) or have special capabilities..",
3070
- (unsigned int)getuid(), (unsigned int)geteuid());
3071
- return -1;
3072
- }
3073
-
3074
- return 0;
3075
-}
3076
-
3077
-/**
3078
- * Adjust memory
3079
- *
3080
- * Adjust memory values to load eBPF programs.
3081
- *
3082
- * @return It returns 0 on success and -1 otherwise
3083
- */
3084
-int ebpf_adjust_memory_limit()
3085
-{
3086
- struct rlimit r = { RLIM_INFINITY, RLIM_INFINITY };
3087
- if (setrlimit(RLIMIT_MEMLOCK, &r)) {
3088
- netdata_log_error("Setrlimit(RLIMIT_MEMLOCK)");
3089
- return -1;
3090
- }
3091
-
3092
- return 0;
3093
-}
3094
-
3053
/**
3054
* Parse arguments given from user.
3055
*
@@ -3324,7 +3282,7 @@ static void ebpf_parse_args(int argc, char **argv)
3282
case EBPF_OPTION_UNITTEST: {
3283
// if we cannot run until the end, we will cancel the unittest
3284
int exit_code = ECANCELED;
3327
- if (ebpf_check_conditions())
3285
+ if (ebpf_can_plugin_load_code(running_on_kernel, NETDATA_EBPF_PLUGIN_NAME))
3286
goto unittest;
3287
3288
if (ebpf_adjust_memory_limit())
@@ -4009,7 +3967,7 @@ static void ebpf_manage_pid(pid_t pid)
3967
int main(int argc, char **argv)
3968
{
3969
clocks_init();
4012
- nd_log_initialize_for_external_plugins("ebpf.plugin");
3970
+ nd_log_initialize_for_external_plugins(NETDATA_EBPF_PLUGIN_NAME);
3971
3972
main_thread_id = gettid();
3973
@@ -4017,7 +3975,7 @@ int main(int argc, char **argv)
3975
ebpf_parse_args(argc, argv);
3976
ebpf_manage_pid(getpid());
3977
4020
- if (ebpf_check_conditions())
3978
+ if (ebpf_can_plugin_load_code(running_on_kernel, NETDATA_EBPF_PLUGIN_NAME))
3979
return 2;
3980
3981
if (ebpf_adjust_memory_limit())
src/collectors/ebpf.plugin/ebpf_apps.c
-18
@@ -371,24 +371,6 @@ int ebpf_read_hash_table(void *ep, int fd, uint32_t pid)
371
*
372
*****************************************************************/
373
374
-/**
375
- * Am I running as Root
376
- *
377
- * Verify the user that is running the collector.
378
- *
379
- * @return It returns 1 for root and 0 otherwise.
380
- */
381
-int am_i_running_as_root()
382
-{
383
- uid_t uid = getuid(), euid = geteuid();
384
-
385
- if (uid == 0 || euid == 0) {
386
- return 1;
387
- }
388
-
389
- return 0;
390
-}
391
-
374
/**
375
* Reset the target values
376
*
src/collectors/ebpf.plugin/ebpf_apps.h
-2
@@ -195,8 +195,6 @@ void clean_apps_groups_target(struct ebpf_target *apps_groups_root_target);
195
196
size_t zero_all_targets(struct ebpf_target *root);
197
198
-int am_i_running_as_root();
199
-
198
void cleanup_exited_pids();
199
200
int ebpf_read_hash_table(void *ep, int fd, uint32_t pid);
src/collectors/ebpf.plugin/ebpf_functions.c
+4
-1
@@ -116,7 +116,10 @@ static void ebpf_fill_function_buffer(BUFFER *wb, netdata_socket_plus_t *values,
116
buffer_json_add_array_item_uint64(wb, (uint64_t)values->pid);
117
118
// NAME
119
- buffer_json_add_array_item_string(wb, (name) ? name : "unknown");
119
+ if (!values->data.name[0])
120
+ buffer_json_add_array_item_string(wb, (name) ? name : "unknown");
121
+ else
122
+ buffer_json_add_array_item_string(wb, values->data.name);
123
124
// Origin
125
buffer_json_add_array_item_string(wb, (values->data.external_origin) ? "in" : "out");
src/libnetdata/ebpf/ebpf.c
+69
-5
@@ -244,7 +244,16 @@ static int kernel_is_rejected()
244
return 0;
245
}
246
247
-static int has_ebpf_kernel_version(int version)
247
+/**
248
+ * Check Kernel Version
249
+ *
250
+ * Test kernel version
251
+ *
252
+ * @param version current kernel version
253
+ *
254
+ * @return It returns 1 when kernel is supported and 0 otherwise
255
+ */
256
+int ebpf_check_kernel_version(int version)
257
{
258
if (kernel_is_rejected())
259
return 0;
@@ -253,12 +262,67 @@ static int has_ebpf_kernel_version(int version)
262
return (version >= NETDATA_MINIMUM_EBPF_KERNEL || get_redhat_release() >= NETDATA_MINIMUM_RH_VERSION);
263
}
264
256
-int has_condition_to_run(int version)
265
+/**
266
+ * Am I running as Root
267
+ *
268
+ * Verify the user that is running the collector.
269
+ *
270
+ * @return It returns 1 for root and 0 otherwise.
271
+ */
272
+int is_ebpf_plugin_running_as_root()
273
{
258
- if (!has_ebpf_kernel_version(version))
259
- return 0;
274
+ uid_t uid = getuid(), euid = geteuid();
275
+
276
+ if (uid == 0 || euid == 0) {
277
+ return 1;
278
+ }
279
+
280
+ return 0;
281
+}
282
+
283
+/**
284
+ * Can the plugin run eBPF code
285
+ *
286
+ * This function checks kernel version and permissions.
287
+ *
288
+ * @param kver the kernel version
289
+ * @param name the plugin name.
290
+ *
291
+ * @return It returns 0 on success and -1 otherwise
292
+ */
293
+int ebpf_can_plugin_load_code(int kver, char *plugin_name)
294
+{
295
+ if (!ebpf_check_kernel_version(kver)) {
296
+ netdata_log_error("The current collector cannot run on this kernel.");
297
+ return -1;
298
+ }
299
+
300
+ if (!is_ebpf_plugin_running_as_root()) {
301
+ netdata_log_error(
302
+ "%s should either run as root (now running with uid %u, euid %u) or have special capabilities.",
303
+ plugin_name, (unsigned int)getuid(), (unsigned int)geteuid());
304
+ return -1;
305
+ }
306
+
307
+ return 0;
308
+}
309
261
- return 1;
310
+/**
311
+ * Adjust memory
312
+ *
313
+ * Adjust memory values to load eBPF programs.
314
+ *
315
+ * @return It returns 0 on success and -1 otherwise
316
+ */
317
+int ebpf_adjust_memory_limit()
318
+{
319
+ struct rlimit r = { RLIM_INFINITY, RLIM_INFINITY };
320
+ if (setrlimit(RLIMIT_MEMLOCK, &r)) {
321
+ netdata_log_error("Setrlimit(RLIMIT_MEMLOCK)");
322
+ return -1;
323
+ }
324
+
325
+ return 0;
326
}
327
328
//----------------------------------------------------------------------------------------------------------------------
src/libnetdata/ebpf/ebpf.h
+5
-1
@@ -3,6 +3,8 @@
3
#ifndef NETDATA_EBPF_H
4
#define NETDATA_EBPF_H 1
5
6
+#define NETDATA_EBPF_PLUGIN_NAME "ebpf.plugin"
7
+
8
#include <bpf/bpf.h>
9
#include <bpf/libbpf.h>
10
#ifdef LIBBPF_DEPRECATED
@@ -361,7 +363,6 @@ typedef struct ebpf_module {
363
364
int ebpf_get_kernel_version();
365
int get_redhat_release();
364
-int has_condition_to_run(int version);
366
char *ebpf_kernel_suffix(int version, int isrh);
367
struct bpf_link **ebpf_load_program(char *plugins_dir, ebpf_module_t *em, int kver, int is_rhf,
368
struct bpf_object **obj);
@@ -484,4 +485,7 @@ int ebpf_statistic_create_aral_chart(char *name, ebpf_module_t *em);
485
void ebpf_statistic_obsolete_aral_chart(ebpf_module_t *em, int prio);
486
void ebpf_send_data_aral_chart(ARAL *memory, ebpf_module_t *em);
487
488
+int ebpf_can_plugin_load_code(int kver, char *plugin_name);
489
+int ebpf_adjust_memory_limit();
490
+
491
#endif /* NETDATA_EBPF_H */