ebpf_socket.h 9.3 KB

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