ebpf.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_EBPF_H
  3. #define NETDATA_EBPF_H 1
  4. #include <bpf/bpf.h>
  5. #include <bpf/libbpf.h>
  6. #define NETDATA_DEBUGFS "/sys/kernel/debug/tracing/"
  7. #define NETDATA_KALLSYMS "/proc/kallsyms"
  8. // Config files
  9. #define EBPF_GLOBAL_SECTION "global"
  10. #define EBPF_CFG_LOAD_MODE "ebpf load mode"
  11. #define EBPF_CFG_LOAD_MODE_DEFAULT "entry"
  12. #define EBPF_CFG_LOAD_MODE_RETURN "return"
  13. #define EBPF_CFG_UPDATE_EVERY "update every"
  14. #define EBPF_CFG_PID_SIZE "pid table size"
  15. #define EBPF_CFG_APPLICATION "apps"
  16. /**
  17. * The next magic number is got doing the following math:
  18. * 294960 = 4*65536 + 11*256 + 0
  19. *
  20. * For more details, please, read /usr/include/linux/version.h
  21. */
  22. #define NETDATA_MINIMUM_EBPF_KERNEL 264960
  23. /**
  24. * The RedHat magic number was got doing:
  25. *
  26. * 1797 = 7*256 + 5
  27. *
  28. * For more details, please, read /usr/include/linux/version.h
  29. * in any Red Hat installation.
  30. */
  31. #define NETDATA_MINIMUM_RH_VERSION 1797
  32. /**
  33. * 2048 = 8*256 + 0
  34. */
  35. #define NETDATA_RH_8 2048
  36. /**
  37. * Kernel 5.11
  38. *
  39. * 330240 = 5*65536 + 11*256
  40. */
  41. #define NETDATA_EBPF_KERNEL_5_11 330496
  42. /**
  43. * Kernel 5.10
  44. *
  45. * 330240 = 5*65536 + 10*256
  46. */
  47. #define NETDATA_EBPF_KERNEL_5_10 330240
  48. /**
  49. * Kernel 4.17
  50. *
  51. * 266496 = 4*65536 + 17*256
  52. */
  53. #define NETDATA_EBPF_KERNEL_4_17 266496
  54. /**
  55. * Kernel 4.15
  56. *
  57. * 265984 = 4*65536 + 15*256
  58. */
  59. #define NETDATA_EBPF_KERNEL_4_15 265984
  60. /**
  61. * Kernel 4.11
  62. *
  63. * 264960 = 4*65536 + 15*256
  64. */
  65. #define NETDATA_EBPF_KERNEL_4_11 264960
  66. #define VERSION_STRING_LEN 256
  67. #define EBPF_KERNEL_REJECT_LIST_FILE "ebpf_kernel_reject_list.txt"
  68. extern char *ebpf_user_config_dir;
  69. extern char *ebpf_stock_config_dir;
  70. typedef struct ebpf_data {
  71. int *map_fd;
  72. char *kernel_string;
  73. uint32_t running_on_kernel;
  74. int isrh;
  75. } ebpf_data_t;
  76. typedef enum {
  77. MODE_RETURN = 0, // This attaches kprobe when the function returns
  78. MODE_DEVMODE, // This stores log given description about the errors raised
  79. MODE_ENTRY // This attaches kprobe when the function is called
  80. } netdata_run_mode_t;
  81. #define ND_EBPF_DEFAULT_PID_SIZE 32768U
  82. typedef struct ebpf_local_maps {
  83. char *name;
  84. uint32_t internal_input;
  85. uint32_t user_input;
  86. } ebpf_local_maps_t;
  87. typedef struct ebpf_specify_name {
  88. char *program_name;
  89. char *function_to_attach;
  90. char *optional;
  91. bool retprobe;
  92. } ebpf_specify_name_t;
  93. typedef struct ebpf_module {
  94. const char *thread_name;
  95. const char *config_name;
  96. int enabled;
  97. void *(*start_routine)(void *);
  98. int update_time;
  99. int global_charts;
  100. int apps_charts;
  101. netdata_run_mode_t mode;
  102. uint32_t thread_id;
  103. int optional;
  104. void (*apps_routine)(struct ebpf_module *em, void *ptr);
  105. ebpf_local_maps_t *maps;
  106. ebpf_specify_name_t *names;
  107. uint32_t pid_map_size;
  108. } ebpf_module_t;
  109. extern int get_kernel_version(char *out, int size);
  110. extern int get_redhat_release();
  111. extern int has_condition_to_run(int version);
  112. extern char *ebpf_kernel_suffix(int version, int isrh);
  113. extern int ebpf_update_kernel(ebpf_data_t *ef);
  114. extern struct bpf_link **ebpf_load_program(char *plugins_dir,
  115. ebpf_module_t *em,
  116. char *kernel_string,
  117. struct bpf_object **obj,
  118. int *map_fd);
  119. extern void ebpf_mount_config_name(char *filename, size_t length, char *path, char *config);
  120. extern int ebpf_load_config(struct config *config, char *filename);
  121. extern void ebpf_update_module_using_config(ebpf_module_t *modules, struct config *cfg);
  122. extern void ebpf_update_module(ebpf_module_t *em, struct config *cfg, char *cfg_file);
  123. extern void ebpf_update_names(ebpf_specify_name_t *opt, ebpf_module_t *em);
  124. #endif /* NETDATA_EBPF_H */