ebpf_socket.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_EBPF_SOCKET_H
  3. #define NETDATA_EBPF_SOCKET_H 1
  4. #include <stdint.h>
  5. #include "libnetdata/avl/avl.h"
  6. // Vector indexes
  7. #define NETDATA_UDP_START 3
  8. #define NETDATA_RETRANSMIT_START 5
  9. #define NETDATA_SOCKET_APPS_HASH_TABLE 0
  10. #define NETDATA_SOCKET_IPV4_HASH_TABLE 1
  11. #define NETDATA_SOCKET_IPV6_HASH_TABLE 2
  12. #define NETDATA_SOCKET_GLOBAL_HASH_TABLE 4
  13. #define NETDATA_SOCKET_LISTEN_TABLE 5
  14. #define NETDATA_SOCKET_READ_SLEEP_MS 800000ULL
  15. // config file
  16. #define NETDATA_NETWORK_CONFIG_FILE "network.conf"
  17. #define EBPF_NETWORK_VIEWER_SECTION "network connections"
  18. #define EBPF_SERVICE_NAME_SECTION "service name"
  19. #define EBPF_CONFIG_RESOLVE_HOSTNAME "resolve hostnames"
  20. #define EBPF_CONFIG_RESOLVE_SERVICE "resolve service names"
  21. #define EBPF_CONFIG_PORTS "ports"
  22. #define EBPF_CONFIG_HOSTNAMES "hostnames"
  23. #define EBPF_CONFIG_BANDWIDTH_SIZE "bandwidth table size"
  24. #define EBPF_CONFIG_IPV4_SIZE "ipv4 connection table size"
  25. #define EBPF_CONFIG_IPV6_SIZE "ipv6 connection table size"
  26. #define EBPF_CONFIG_UDP_SIZE "udp connection table size"
  27. #define EBPF_MAXIMUM_DIMENSIONS "maximum dimensions"
  28. enum ebpf_socket_table_list {
  29. NETDATA_SOCKET_TABLE_BANDWIDTH,
  30. NETDATA_SOCKET_TABLE_IPV4,
  31. NETDATA_SOCKET_TABLE_IPV6,
  32. NETDATA_SOCKET_TABLE_UDP
  33. };
  34. enum ebpf_socket_publish_index {
  35. NETDATA_IDX_TCP_SENDMSG,
  36. NETDATA_IDX_TCP_CLEANUP_RBUF,
  37. NETDATA_IDX_TCP_CLOSE,
  38. NETDATA_IDX_UDP_RECVBUF,
  39. NETDATA_IDX_UDP_SENDMSG,
  40. NETDATA_IDX_TCP_RETRANSMIT,
  41. // Keep this as last and don't skip numbers as it is used as element counter
  42. NETDATA_MAX_SOCKET_VECTOR
  43. };
  44. typedef enum ebpf_socket_idx {
  45. NETDATA_KEY_CALLS_TCP_SENDMSG,
  46. NETDATA_KEY_ERROR_TCP_SENDMSG,
  47. NETDATA_KEY_BYTES_TCP_SENDMSG,
  48. NETDATA_KEY_CALLS_TCP_CLEANUP_RBUF,
  49. NETDATA_KEY_ERROR_TCP_CLEANUP_RBUF,
  50. NETDATA_KEY_BYTES_TCP_CLEANUP_RBUF,
  51. NETDATA_KEY_CALLS_TCP_CLOSE,
  52. NETDATA_KEY_CALLS_UDP_RECVMSG,
  53. NETDATA_KEY_ERROR_UDP_RECVMSG,
  54. NETDATA_KEY_BYTES_UDP_RECVMSG,
  55. NETDATA_KEY_CALLS_UDP_SENDMSG,
  56. NETDATA_KEY_ERROR_UDP_SENDMSG,
  57. NETDATA_KEY_BYTES_UDP_SENDMSG,
  58. NETDATA_KEY_TCP_RETRANSMIT,
  59. // Keep this as last and don't skip numbers as it is used as element counter
  60. NETDATA_SOCKET_COUNTER
  61. } ebpf_socket_index_t;
  62. #define NETDATA_SOCKET_GROUP "Socket"
  63. #define NETDATA_NETWORK_CONNECTIONS_GROUP "Network connections"
  64. // Global chart name
  65. #define NETDATA_TCP_FUNCTION_COUNT "tcp_functions"
  66. #define NETDATA_TCP_FUNCTION_BITS "total_tcp_bandwidth"
  67. #define NETDATA_TCP_FUNCTION_ERROR "tcp_error"
  68. #define NETDATA_TCP_RETRANSMIT "tcp_retransmit"
  69. #define NETDATA_UDP_FUNCTION_COUNT "udp_functions"
  70. #define NETDATA_UDP_FUNCTION_BITS "total_udp_bandwidth"
  71. #define NETDATA_UDP_FUNCTION_ERROR "udp_error"
  72. // Charts created on Apps submenu
  73. #define NETDATA_NET_APPS_BANDWIDTH_SENT "total_bandwidth_sent"
  74. #define NETDATA_NET_APPS_BANDWIDTH_RECV "total_bandwidth_recv"
  75. #define NETDATA_NET_APPS_BANDWIDTH_TCP_SEND_CALLS "bandwidth_tcp_send"
  76. #define NETDATA_NET_APPS_BANDWIDTH_TCP_RECV_CALLS "bandwidth_tcp_recv"
  77. #define NETDATA_NET_APPS_BANDWIDTH_TCP_RETRANSMIT "bandwidth_tcp_retransmit"
  78. #define NETDATA_NET_APPS_BANDWIDTH_UDP_SEND_CALLS "bandwidth_udp_send"
  79. #define NETDATA_NET_APPS_BANDWIDTH_UDP_RECV_CALLS "bandwidth_udp_recv"
  80. // Network viewer charts
  81. #define NETDATA_NV_OUTBOUND_BYTES "outbound_bytes"
  82. #define NETDATA_NV_OUTBOUND_PACKETS "outbound_packets"
  83. #define NETDATA_NV_OUTBOUND_RETRANSMIT "outbound_retransmit"
  84. #define NETDATA_NV_INBOUND_BYTES "inbound_bytes"
  85. #define NETDATA_NV_INBOUND_PACKETS "inbound_packets"
  86. // Port range
  87. #define NETDATA_MINIMUM_PORT_VALUE 1
  88. #define NETDATA_MAXIMUM_PORT_VALUE 65535
  89. #define NETDATA_COMPILED_CONNECTIONS_ALLOWED 65535U
  90. #define NETDATA_MAXIMUM_CONNECTIONS_ALLOWED 16384U
  91. #define NETDATA_COMPILED_UDP_CONNECTIONS_ALLOWED 8192U
  92. #define NETDATA_MAXIMUM_UDP_CONNECTIONS_ALLOWED 4096U
  93. #define NETDATA_MINIMUM_IPV4_CIDR 0
  94. #define NETDATA_MAXIMUM_IPV4_CIDR 32
  95. typedef struct ebpf_socket_publish_apps {
  96. // Data read
  97. uint64_t bytes_sent; // Bytes sent
  98. uint64_t bytes_received; // Bytes received
  99. uint64_t call_tcp_sent; // Number of times tcp_sendmsg was called
  100. uint64_t call_tcp_received; // Number of times tcp_cleanup_rbuf was called
  101. uint64_t retransmit; // Number of times tcp_retransmit was called
  102. uint64_t call_udp_sent; // Number of times udp_sendmsg was called
  103. uint64_t call_udp_received; // Number of times udp_recvmsg was called
  104. // Publish information.
  105. uint64_t publish_sent_bytes;
  106. uint64_t publish_received_bytes;
  107. uint64_t publish_tcp_sent;
  108. uint64_t publish_tcp_received;
  109. uint64_t publish_retransmit;
  110. uint64_t publish_udp_sent;
  111. uint64_t publish_udp_received;
  112. } ebpf_socket_publish_apps_t;
  113. typedef struct ebpf_network_viewer_dimension_names {
  114. char *name;
  115. uint32_t hash;
  116. uint16_t port;
  117. struct ebpf_network_viewer_dimension_names *next;
  118. } ebpf_network_viewer_dim_name_t ;
  119. typedef struct ebpf_network_viewer_port_list {
  120. char *value;
  121. uint32_t hash;
  122. uint16_t first;
  123. uint16_t last;
  124. uint16_t cmp_first;
  125. uint16_t cmp_last;
  126. uint8_t protocol;
  127. struct ebpf_network_viewer_port_list *next;
  128. } ebpf_network_viewer_port_list_t;
  129. /**
  130. * Union used to store ip addresses
  131. */
  132. union netdata_ip_t {
  133. uint8_t addr8[16];
  134. uint16_t addr16[8];
  135. uint32_t addr32[4];
  136. uint64_t addr64[2];
  137. };
  138. typedef struct ebpf_network_viewer_ip_list {
  139. char *value; // IP value
  140. uint32_t hash; // IP hash
  141. uint8_t ver; // IP version
  142. union netdata_ip_t first; // The IP address informed
  143. union netdata_ip_t last; // The IP address informed
  144. struct ebpf_network_viewer_ip_list *next;
  145. } ebpf_network_viewer_ip_list_t;
  146. typedef struct ebpf_network_viewer_hostname_list {
  147. char *value; // IP value
  148. uint32_t hash; // IP hash
  149. SIMPLE_PATTERN *value_pattern;
  150. struct ebpf_network_viewer_hostname_list *next;
  151. } ebpf_network_viewer_hostname_list_t;
  152. #define NETDATA_NV_CAP_VALUE 50L
  153. typedef struct ebpf_network_viewer_options {
  154. uint32_t max_dim; // Store value read from 'maximum dimensions'
  155. uint32_t hostname_resolution_enabled;
  156. uint32_t service_resolution_enabled;
  157. ebpf_network_viewer_port_list_t *excluded_port;
  158. ebpf_network_viewer_port_list_t *included_port;
  159. ebpf_network_viewer_dim_name_t *names;
  160. ebpf_network_viewer_ip_list_t *excluded_ips;
  161. ebpf_network_viewer_ip_list_t *included_ips;
  162. ebpf_network_viewer_hostname_list_t *excluded_hostnames;
  163. ebpf_network_viewer_hostname_list_t *included_hostnames;
  164. ebpf_network_viewer_ip_list_t *ipv4_local_ip;
  165. ebpf_network_viewer_ip_list_t *ipv6_local_ip;
  166. } ebpf_network_viewer_options_t;
  167. extern ebpf_network_viewer_options_t network_viewer_opt;
  168. /**
  169. * Structure to store socket information
  170. */
  171. typedef struct netdata_socket {
  172. uint64_t recv_packets;
  173. uint64_t sent_packets;
  174. uint64_t recv_bytes;
  175. uint64_t sent_bytes;
  176. uint64_t first; // First timestamp
  177. uint64_t ct; // Current timestamp
  178. uint16_t retransmit; // It is never used with UDP
  179. uint8_t protocol;
  180. uint8_t removeme;
  181. uint32_t reserved;
  182. } netdata_socket_t __attribute__((__aligned__(8)));
  183. typedef struct netdata_plot_values {
  184. // Values used in the previous iteration
  185. uint64_t recv_packets;
  186. uint64_t sent_packets;
  187. uint64_t recv_bytes;
  188. uint64_t sent_bytes;
  189. uint16_t retransmit;
  190. uint64_t last_time;
  191. // Values used to plot
  192. uint64_t plot_recv_packets;
  193. uint64_t plot_sent_packets;
  194. uint64_t plot_recv_bytes;
  195. uint64_t plot_sent_bytes;
  196. uint16_t plot_retransmit;
  197. } netdata_plot_values_t;
  198. /**
  199. * Index used together previous structure
  200. */
  201. typedef struct netdata_socket_idx {
  202. union netdata_ip_t saddr;
  203. uint16_t sport;
  204. union netdata_ip_t daddr;
  205. uint16_t dport;
  206. } netdata_socket_idx_t __attribute__((__aligned__(8)));
  207. // Next values were defined according getnameinfo(3)
  208. #define NETDATA_MAX_NETWORK_COMBINED_LENGTH 1018
  209. #define NETDATA_DOTS_PROTOCOL_COMBINED_LENGTH 5 // :TCP:
  210. #define NETDATA_DIM_LENGTH_WITHOUT_SERVICE_PROTOCOL 979
  211. #define NETDATA_INBOUND_DIRECTION (uint32_t)1
  212. #define NETDATA_OUTBOUND_DIRECTION (uint32_t)2
  213. /**
  214. * Allocate the maximum number of structures in the beginning, this can force the collector to use more memory
  215. * in the long term, on the other had it is faster.
  216. */
  217. typedef struct netdata_socket_plot {
  218. // Search
  219. avl_t avl;
  220. netdata_socket_idx_t index;
  221. // Current data
  222. netdata_socket_t sock;
  223. // Previous values and values used to write on chart.
  224. netdata_plot_values_t plot;
  225. int family; // AF_INET or AF_INET6
  226. char *resolved_name; // Resolve only in the first call
  227. unsigned char resolved;
  228. char *dimension_sent;
  229. char *dimension_recv;
  230. char *dimension_retransmit;
  231. uint32_t flags;
  232. } netdata_socket_plot_t;
  233. #define NETWORK_VIEWER_CHARTS_CREATED (uint32_t)1
  234. typedef struct netdata_vector_plot {
  235. netdata_socket_plot_t *plot; // Vector used to plot charts
  236. avl_tree_lock tree; // AVL tree to speed up search
  237. uint32_t last; // The 'other' dimension, the last chart accepted.
  238. uint32_t next; // The next position to store in the vector.
  239. uint32_t max_plot; // Max number of elements to plot.
  240. uint32_t last_plot; // Last element plot
  241. uint32_t flags; // Flags
  242. } netdata_vector_plot_t;
  243. extern void clean_port_structure(ebpf_network_viewer_port_list_t **clean);
  244. extern ebpf_network_viewer_port_list_t *listen_ports;
  245. extern void update_listen_table(uint16_t value, uint8_t proto);
  246. extern void parse_network_viewer_section(struct config *cfg);
  247. extern void fill_ip_list(ebpf_network_viewer_ip_list_t **out, ebpf_network_viewer_ip_list_t *in, char *table);
  248. extern void parse_service_name_section(struct config *cfg);
  249. extern void clean_socket_apps_structures();
  250. extern ebpf_socket_publish_apps_t **socket_bandwidth_curr;
  251. extern struct config socket_config;
  252. #endif