Tor  0.4.7.0-alpha-dev
status.c
Go to the documentation of this file.
1 /* Copyright (c) 2010-2021, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
3 
4 /**
5  * \file status.c
6  * \brief Collect status information and log heartbeat messages.
7  *
8  * This module is responsible for implementing the heartbeat log messages,
9  * which periodically inform users and operators about basic facts to
10  * do with their Tor instance. The log_heartbeat() function, invoked from
11  * main.c, is the principle entry point. It collects data from elsewhere
12  * in Tor, and logs it in a human-readable format.
13  **/
14 
15 #define STATUS_PRIVATE
16 
17 #include "core/or/or.h"
18 #include "core/or/circuituse.h"
19 #include "app/config/config.h"
21 #include "core/or/status.h"
23 #include "core/or/relay.h"
24 #include "feature/relay/router.h"
26 #include "core/or/circuitlist.h"
27 #include "core/mainloop/mainloop.h"
28 #include "feature/stats/rephist.h"
30 #include "app/config/statefile.h"
31 #include "feature/hs/hs_stats.h"
32 #include "feature/hs/hs_service.h"
33 #include "core/or/dos.h"
35 
36 #include "app/config/or_state_st.h"
38 #include "lib/tls/tortls.h"
39 
40 static void log_accounting(const time_t now, const or_options_t *options);
41 
42 /** Return the total number of circuits. */
43 STATIC int
45 {
46  return smartlist_len(circuit_get_global_list());
47 }
48 
49 /** Take seconds <b>secs</b> and return a newly allocated human-readable
50  * uptime string. */
51 STATIC char *
52 secs_to_uptime(long secs)
53 {
54  long int days = secs / 86400;
55  int hours = (int)((secs - (days * 86400)) / 3600);
56  int minutes = (int)((secs - (days * 86400) - (hours * 3600)) / 60);
57  char *uptime_string = NULL;
58 
59  switch (days) {
60  case 0:
61  tor_asprintf(&uptime_string, "%d:%02d hours", hours, minutes);
62  break;
63  case 1:
64  tor_asprintf(&uptime_string, "%ld day %d:%02d hours",
65  days, hours, minutes);
66  break;
67  default:
68  tor_asprintf(&uptime_string, "%ld days %d:%02d hours",
69  days, hours, minutes);
70  break;
71  }
72 
73  return uptime_string;
74 }
75 
76 /** Take <b>bytes</b> and returns a newly allocated human-readable usage
77  * string. */
78 STATIC char *
79 bytes_to_usage(uint64_t bytes)
80 {
81  char *bw_string = NULL;
82 
83  if (bytes < (1<<20)) { /* Less than a megabyte. */
84  tor_asprintf(&bw_string, "%"PRIu64" kB", (bytes>>10));
85  } else if (bytes < (1<<30)) { /* Megabytes. Let's add some precision. */
86  double bw = ((double)bytes);
87  tor_asprintf(&bw_string, "%.2f MB", bw/(1<<20));
88  } else { /* Gigabytes. */
89  double bw = ((double)bytes);
90  tor_asprintf(&bw_string, "%.2f GB", bw/(1<<30));
91  }
92 
93  return bw_string;
94 }
95 
96 /** Log some usage info about our onion service(s). */
97 static void
99 {
100  unsigned int num_services = hs_service_get_num_services();
101 
102  /* If there are no active onion services, no need to print logs */
103  if (num_services == 0) {
104  return;
105  }
106 
107  log_notice(LD_HEARTBEAT,
108  "Heartbeat: Our onion service%s received %u v3 INTRODUCE2 cells "
109  "and attempted to launch %d rendezvous circuits.",
110  num_services == 1 ? "" : "s",
113 }
114 
115 /**
116  * @name connection counts for heartbeat
117  *
118  * Tracks incoming and outgoing connections on IPv4/IPv6, for heartbeat
119  * logs.
120  **/
121 /**@{*/
122 static unsigned n_incoming_ipv4;
123 static unsigned n_incoming_ipv6;
124 static unsigned n_outgoing_ipv4;
125 static unsigned n_outgoing_ipv6;
126 /**@}*/
127 
128 /**
129  * Note that a connection has arrived or has been made, for use in the
130  * heartbeat message.
131  **/
132 void
133 note_connection(bool inbound, int family)
134 {
135  if (family == AF_INET) {
136  if (inbound) {
137  ++n_incoming_ipv4;
138  } else {
139  ++n_outgoing_ipv4;
140  }
141  } else if (family == AF_INET6) {
142  if (inbound) {
143  ++n_incoming_ipv6;
144  } else {
145  ++n_outgoing_ipv6;
146  }
147  }
148 }
149 
150 /** Log a "heartbeat" message describing Tor's status and history so that the
151  * user can know that there is indeed a running Tor. Return 0 on success and
152  * -1 on failure. */
153 int
154 log_heartbeat(time_t now)
155 {
156  char *bw_sent = NULL;
157  char *bw_rcvd = NULL;
158  char *uptime = NULL;
159  const routerinfo_t *me;
160  double r = tls_get_write_overhead_ratio();
161  const int hibernating = we_are_hibernating();
162 
163  const or_options_t *options = get_options();
164 
165  if (public_server_mode(options) && !hibernating) {
166  /* Let's check if we are in the current cached consensus. */
167  if (!(me = router_get_my_routerinfo()))
168  return -1; /* Something stinks, we won't even attempt this. */
169  else
170  if (!node_get_by_id(me->cache_info.identity_digest))
171  log_fn(LOG_NOTICE, LD_HEARTBEAT, "Heartbeat: It seems like we are not "
172  "in the cached consensus.");
173  }
174 
175  uptime = secs_to_uptime(get_uptime());
176  bw_rcvd = bytes_to_usage(get_bytes_read());
177  bw_sent = bytes_to_usage(get_bytes_written());
178 
179  log_fn(LOG_NOTICE, LD_HEARTBEAT, "Heartbeat: Tor's uptime is %s, with %d "
180  "circuits open. I've sent %s and received %s. I've received %u "
181  "connections on IPv4 and %u on IPv6. I've made %u connections "
182  "with IPv4 and %u with IPv6.%s",
183  uptime, count_circuits(), bw_sent, bw_rcvd,
184  n_incoming_ipv4, n_incoming_ipv6,
185  n_outgoing_ipv4, n_outgoing_ipv6,
186  hibernating?" We are currently hibernating.":"");
187 
189 
190  if (server_mode(options) && accounting_is_enabled(options) && !hibernating) {
191  log_accounting(now, options);
192  }
193 
194  double fullness_pct = 100;
195  if (stats_n_data_cells_packaged && !hibernating) {
196  fullness_pct =
197  100*(((double)stats_n_data_bytes_packaged) /
199  }
200  const double overhead_pct = ( r - 1.0 ) * 100.0;
201 
202 #define FULLNESS_PCT_THRESHOLD 80
203 #define TLS_OVERHEAD_THRESHOLD 15
204 
205  const int severity = (fullness_pct < FULLNESS_PCT_THRESHOLD ||
206  overhead_pct > TLS_OVERHEAD_THRESHOLD)
207  ? LOG_NOTICE : LOG_INFO;
208 
209  log_fn(severity, LD_HEARTBEAT,
210  "Average packaged cell fullness: %2.3f%%. "
211  "TLS write overhead: %.f%%", fullness_pct, overhead_pct);
212 
213  if (public_server_mode(options)) {
216  dos_log_heartbeat();
217  }
218 
220 
221  if (options->BridgeRelay) {
222  char *msg = NULL;
224  if (msg)
225  log_notice(LD_HEARTBEAT, "%s", msg);
226  tor_free(msg);
227  }
228 
229  if (options->MainloopStats) {
230  const uint64_t main_loop_success_count = get_main_loop_success_count();
231  const uint64_t main_loop_error_count = get_main_loop_error_count();
232  const uint64_t main_loop_idle_count = get_main_loop_idle_count();
233 
234  log_fn(LOG_NOTICE, LD_HEARTBEAT, "Main event loop statistics: "
235  "%"PRIu64 " successful returns, "
236  "%"PRIu64 " erroneous returns, and "
237  "%"PRIu64 " idle returns.",
238  (main_loop_success_count),
239  (main_loop_error_count),
240  (main_loop_idle_count));
241  }
242 
243  /** Now, if we are an HS service, log some stats about our usage */
245 
246  tor_free(uptime);
247  tor_free(bw_sent);
248  tor_free(bw_rcvd);
249 
250  return 0;
251 }
252 
253 static void
254 log_accounting(const time_t now, const or_options_t *options)
255 {
256  or_state_t *state = get_or_state();
257  char *acc_rcvd = bytes_to_usage(state->AccountingBytesReadInInterval);
258  char *acc_sent = bytes_to_usage(state->AccountingBytesWrittenInInterval);
259  char *acc_used = bytes_to_usage(get_accounting_bytes());
260  uint64_t acc_bytes = options->AccountingMax;
261  char *acc_max;
262  time_t interval_end = accounting_get_end_time();
263  char end_buf[ISO_TIME_LEN + 1];
264  char *remaining = NULL;
265  acc_max = bytes_to_usage(acc_bytes);
266  format_local_iso_time(end_buf, interval_end);
267  remaining = secs_to_uptime(interval_end - now);
268 
269  const char *acc_rule;
270  switch (options->AccountingRule) {
271  case ACCT_MAX: acc_rule = "max";
272  break;
273  case ACCT_SUM: acc_rule = "sum";
274  break;
275  case ACCT_OUT: acc_rule = "out";
276  break;
277  case ACCT_IN: acc_rule = "in";
278  break;
279  default: acc_rule = "max";
280  break;
281  }
282 
283  log_notice(LD_HEARTBEAT, "Heartbeat: Accounting enabled. "
284  "Sent: %s, Received: %s, Used: %s / %s, Rule: %s. The "
285  "current accounting interval ends on %s, in %s.",
286  acc_sent, acc_rcvd, acc_used, acc_max, acc_rule, end_buf, remaining);
287 
288  tor_free(acc_rcvd);
289  tor_free(acc_sent);
290  tor_free(acc_used);
291  tor_free(acc_max);
292  tor_free(remaining);
293 }
smartlist_t * circuit_get_global_list(void)
Definition: circuitlist.c:694
Header file for circuitlist.c.
void circuit_log_ancient_one_hop_circuits(int age)
Definition: circuituse.c:869
Header file for circuituse.c.
const or_options_t * get_options(void)
Definition: config.c:919
Header file for config.c.
void dirclient_dump_total_dls(void)
Definition: dirclient.c:1949
Header file for dirclient.c.
Header file for geoip_stats.c.
char * format_client_stats_heartbeat(time_t now)
Definition: geoip_stats.c:1207
time_t accounting_get_end_time(void)
Definition: hibernate.c:322
int accounting_is_enabled(const or_options_t *options)
Definition: hibernate.c:305
int we_are_hibernating(void)
Definition: hibernate.c:937
uint64_t get_accounting_bytes(void)
Definition: hibernate.c:478
Header file for hibernate.c.
unsigned int hs_service_get_num_services(void)
Definition: hs_service.c:3918
Header file containing service data for the HS subsystem.
uint32_t hs_stats_get_n_introduce2_v3_cells(void)
Definition: hs_stats.c:27
uint32_t hs_stats_get_n_rendezvous_launches(void)
Definition: hs_stats.c:41
Header file for hs_stats.c.
#define log_fn(severity, domain, args,...)
Definition: log.h:283
#define LD_HEARTBEAT
Definition: log.h:103
#define LOG_NOTICE
Definition: log.h:50
#define LOG_INFO
Definition: log.h:45
uint64_t get_bytes_read(void)
Definition: mainloop.c:463
uint64_t get_main_loop_error_count(void)
Definition: mainloop.c:548
uint64_t get_main_loop_idle_count(void)
Definition: mainloop.c:562
uint64_t get_main_loop_success_count(void)
Definition: mainloop.c:534
uint64_t get_bytes_written(void)
Definition: mainloop.c:473
long get_uptime(void)
Definition: mainloop.c:2528
Header file for mainloop.c.
#define tor_free(p)
Definition: malloc.h:52
const node_t * node_get_by_id(const char *identity_digest)
Definition: nodelist.c:226
Header file for nodelist.c.
Master header file for Tor-specific functionality.
#define RELAY_PAYLOAD_SIZE
Definition: or.h:486
The or_state_t structure, which represents Tor's state file.
int tor_asprintf(char **strp, const char *fmt,...)
Definition: printf.c:75
uint64_t stats_n_data_cells_packaged
Definition: relay.c:2052
uint64_t stats_n_data_bytes_packaged
Definition: relay.c:2056
Header file for relay.c.
void rep_hist_log_link_protocol_counts(void)
Definition: rephist.c:2586
void rep_hist_log_circuit_handshake_stats(time_t now)
Definition: rephist.c:2065
Header file for rephist.c.
const routerinfo_t * router_get_my_routerinfo(void)
Definition: router.c:1801
Header file for router.c.
Router descriptor structure.
int public_server_mode(const or_options_t *options)
Definition: routermode.c:43
int server_mode(const or_options_t *options)
Definition: routermode.c:34
Header file for routermode.c.
or_state_t * get_or_state(void)
Definition: statefile.c:220
Header for statefile.c.
STATIC char * bytes_to_usage(uint64_t bytes)
Definition: status.c:79
STATIC int count_circuits(void)
Definition: status.c:44
static void log_onion_service_stats(void)
Definition: status.c:98
STATIC char * secs_to_uptime(long secs)
Definition: status.c:52
int log_heartbeat(time_t now)
Definition: status.c:154
void note_connection(bool inbound, int family)
Definition: status.c:133
Header for status.c.
uint64_t AccountingMax
char identity_digest[DIGEST_LEN]
#define STATIC
Definition: testsupport.h:32
void format_local_iso_time(char *buf, time_t t)
Definition: time_fmt.c:285
Headers for tortls.c.
double tls_get_write_overhead_ratio(void)
Definition: tortls_nss.c:712