ebpf_socket.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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_MAX_SOCKET_VECTOR 6
  8. #define NETDATA_UDP_START 3
  9. #define NETDATA_RETRANSMIT_START 5
  10. #define NETDATA_SOCKET_APPS_HASH_TABLE 0
  11. #define NETDATA_SOCKET_IPV4_HASH_TABLE 1
  12. #define NETDATA_SOCKET_IPV6_HASH_TABLE 2
  13. #define NETDATA_SOCKET_GLOBAL_HASH_TABLE 4
  14. #define NETDATA_SOCKET_LISTEN_TABLE 5
  15. #define NETDATA_SOCKET_READ_SLEEP_MS 800000ULL
  16. typedef enum ebpf_socket_idx {
  17. NETDATA_KEY_CALLS_TCP_SENDMSG,
  18. NETDATA_KEY_ERROR_TCP_SENDMSG,
  19. NETDATA_KEY_BYTES_TCP_SENDMSG,
  20. NETDATA_KEY_CALLS_TCP_CLEANUP_RBUF,
  21. NETDATA_KEY_ERROR_TCP_CLEANUP_RBUF,
  22. NETDATA_KEY_BYTES_TCP_CLEANUP_RBUF,
  23. NETDATA_KEY_CALLS_TCP_CLOSE,
  24. NETDATA_KEY_CALLS_UDP_RECVMSG,
  25. NETDATA_KEY_ERROR_UDP_RECVMSG,
  26. NETDATA_KEY_BYTES_UDP_RECVMSG,
  27. NETDATA_KEY_CALLS_UDP_SENDMSG,
  28. NETDATA_KEY_ERROR_UDP_SENDMSG,
  29. NETDATA_KEY_BYTES_UDP_SENDMSG,
  30. NETDATA_KEY_TCP_RETRANSMIT,
  31. NETDATA_SOCKET_COUNTER
  32. } ebpf_socket_index_t;
  33. #define NETDATA_SOCKET_GROUP "Socket"
  34. #define NETDATA_NETWORK_CONNECTIONS_GROUP "Network connections"
  35. // Global chart name
  36. #define NETDATA_TCP_FUNCTION_COUNT "tcp_functions"
  37. #define NETDATA_TCP_FUNCTION_BYTES "tcp_bandwidth"
  38. #define NETDATA_TCP_FUNCTION_ERROR "tcp_error"
  39. #define NETDATA_TCP_RETRANSMIT "tcp_retransmit"
  40. #define NETDATA_UDP_FUNCTION_COUNT "udp_functions"
  41. #define NETDATA_UDP_FUNCTION_BYTES "udp_bandwidth"
  42. #define NETDATA_UDP_FUNCTION_ERROR "udp_error"
  43. // Charts created on Apps submenu
  44. #define NETDATA_NET_APPS_BANDWIDTH_SENT "bandwidth_sent"
  45. #define NETDATA_NET_APPS_BANDWIDTH_RECV "bandwidth_recv"
  46. // Network viewer charts
  47. #define NETDATA_NV_OUTBOUND_BYTES "outbound_bytes"
  48. #define NETDATA_NV_OUTBOUND_PACKETS "outbound_packets"
  49. #define NETDATA_NV_OUTBOUND_RETRANSMIT "outbound_retransmit"
  50. #define NETDATA_NV_INBOUND_BYTES "inbound_bytes"
  51. #define NETDATA_NV_INBOUND_PACKETS "inbound_packets"
  52. // Port range
  53. #define NETDATA_MINIMUM_PORT_VALUE 1
  54. #define NETDATA_MAXIMUM_PORT_VALUE 65535
  55. #define NETDATA_MINIMUM_IPV4_CIDR 0
  56. #define NETDATA_MAXIMUM_IPV4_CIDR 32
  57. typedef struct ebpf_socket_publish_apps {
  58. // Data read
  59. uint64_t sent;
  60. uint64_t received;
  61. // Publish information.
  62. uint64_t publish_sent;
  63. uint64_t publish_recv;
  64. } ebpf_socket_publish_apps_t;
  65. typedef struct ebpf_network_viewer_dimension_names {
  66. char *name;
  67. uint32_t hash;
  68. uint16_t port;
  69. struct ebpf_network_viewer_dimension_names *next;
  70. } ebpf_network_viewer_dim_name_t ;
  71. typedef struct ebpf_network_viewer_port_list {
  72. char *value;
  73. uint32_t hash;
  74. uint16_t first;
  75. uint16_t last;
  76. uint16_t cmp_first;
  77. uint16_t cmp_last;
  78. uint8_t protocol;
  79. struct ebpf_network_viewer_port_list *next;
  80. } ebpf_network_viewer_port_list_t;
  81. /**
  82. * Union used to store ip addresses
  83. */
  84. union netdata_ip_t {
  85. uint8_t addr8[16];
  86. uint16_t addr16[8];
  87. uint32_t addr32[4];
  88. uint64_t addr64[2];
  89. };
  90. typedef struct ebpf_network_viewer_ip_list {
  91. char *value; // IP value
  92. uint32_t hash; // IP hash
  93. uint8_t ver; // IP version
  94. union netdata_ip_t first; // The IP address informed
  95. union netdata_ip_t last; // The IP address informed
  96. struct ebpf_network_viewer_ip_list *next;
  97. } ebpf_network_viewer_ip_list_t;
  98. typedef struct ebpf_network_viewer_hostname_list {
  99. char *value; // IP value
  100. uint32_t hash; // IP hash
  101. SIMPLE_PATTERN *value_pattern;
  102. struct ebpf_network_viewer_hostname_list *next;
  103. } ebpf_network_viewer_hostname_list_t;
  104. #define NETDATA_NV_CAP_VALUE 50L
  105. typedef struct ebpf_network_viewer_options {
  106. uint32_t max_dim; // Store value read from 'maximum dimensions'
  107. uint32_t hostname_resolution_enabled;
  108. uint32_t service_resolution_enabled;
  109. ebpf_network_viewer_port_list_t *excluded_port;
  110. ebpf_network_viewer_port_list_t *included_port;
  111. ebpf_network_viewer_dim_name_t *names;
  112. ebpf_network_viewer_ip_list_t *excluded_ips;
  113. ebpf_network_viewer_ip_list_t *included_ips;
  114. ebpf_network_viewer_hostname_list_t *excluded_hostnames;
  115. ebpf_network_viewer_hostname_list_t *included_hostnames;
  116. ebpf_network_viewer_ip_list_t *ipv4_local_ip;
  117. ebpf_network_viewer_ip_list_t *ipv6_local_ip;
  118. } ebpf_network_viewer_options_t;
  119. extern ebpf_network_viewer_options_t network_viewer_opt;
  120. /**
  121. * Structure to store socket information
  122. */
  123. typedef struct netdata_socket {
  124. uint64_t recv_packets;
  125. uint64_t sent_packets;
  126. uint64_t recv_bytes;
  127. uint64_t sent_bytes;
  128. uint64_t first; // First timestamp
  129. uint64_t ct; // Current timestamp
  130. uint16_t retransmit; // It is never used with UDP
  131. uint8_t protocol;
  132. uint8_t removeme;
  133. uint32_t reserved;
  134. } netdata_socket_t __attribute__((__aligned__(8)));
  135. typedef struct netdata_plot_values {
  136. // Values used in the previous iteration
  137. uint64_t recv_packets;
  138. uint64_t sent_packets;
  139. uint64_t recv_bytes;
  140. uint64_t sent_bytes;
  141. uint16_t retransmit;
  142. uint64_t last_time;
  143. // Values used to plot
  144. uint64_t plot_recv_packets;
  145. uint64_t plot_sent_packets;
  146. uint64_t plot_recv_bytes;
  147. uint64_t plot_sent_bytes;
  148. uint16_t plot_retransmit;
  149. } netdata_plot_values_t;
  150. /**
  151. * Index used together previous structure
  152. */
  153. typedef struct netdata_socket_idx {
  154. union netdata_ip_t saddr;
  155. uint16_t sport;
  156. union netdata_ip_t daddr;
  157. uint16_t dport;
  158. } netdata_socket_idx_t __attribute__((__aligned__(8)));
  159. // Next values were defined according getnameinfo(3)
  160. #define NETDATA_MAX_NETWORK_COMBINED_LENGTH 1018
  161. #define NETDATA_DOTS_PROTOCOL_COMBINED_LENGTH 5 // :TCP:
  162. #define NETDATA_DIM_LENGTH_WITHOUT_SERVICE_PROTOCOL 979
  163. #define NETDATA_INBOUND_DIRECTION (uint32_t)1
  164. #define NETDATA_OUTBOUND_DIRECTION (uint32_t)2
  165. /**
  166. * Allocate the maximum number of structures in the beginning, this can force the collector to use more memory
  167. * in the long term, on the other had it is faster.
  168. */
  169. typedef struct netdata_socket_plot {
  170. // Search
  171. avl avl;
  172. netdata_socket_idx_t index;
  173. // Current data
  174. netdata_socket_t sock;
  175. // Previous values and values used to write on chart.
  176. netdata_plot_values_t plot;
  177. int family; // AF_INET or AF_INET6
  178. char *resolved_name; // Resolve only in the first call
  179. unsigned char resolved;
  180. char *dimension_sent;
  181. char *dimension_recv;
  182. char *dimension_retransmit;
  183. uint32_t flags;
  184. } netdata_socket_plot_t;
  185. #define NETWORK_VIEWER_CHARTS_CREATED (uint32_t)1
  186. typedef struct netdata_vector_plot {
  187. netdata_socket_plot_t *plot; // Vector used to plot charts
  188. avl_tree_lock tree; // AVL tree to speed up search
  189. uint32_t last; // The 'other' dimension, the last chart accepted.
  190. uint32_t next; // The next position to store in the vector.
  191. uint32_t max_plot; // Max number of elements to plot.
  192. uint32_t last_plot; // Last element plot
  193. uint32_t flags; // Flags
  194. } netdata_vector_plot_t;
  195. extern void clean_port_structure(ebpf_network_viewer_port_list_t **clean);
  196. extern ebpf_network_viewer_port_list_t *listen_ports;
  197. extern void update_listen_table(uint16_t value, uint8_t proto);
  198. extern ebpf_socket_publish_apps_t **socket_bandwidth_curr;
  199. extern ebpf_socket_publish_apps_t **socket_bandwidth_prev;
  200. #endif