Tor  0.4.5.0-alpha-dev
status.c
Go to the documentation of this file.
1 /* Copyright (c) 2010-2020, 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  "Our onion service%s received %u v2 and %u v3 INTRODUCE2 cells "
109  "and attempted to launch %d rendezvous circuits.",
110  num_services == 1 ? "" : "s",
114 }
115 
116 /**
117  * @name connection counts for heartbeat
118  *
119  * Tracks incoming and outgoing connections on IPv4/IPv6, for heartbeat
120  * logs.
121  **/
122 /**@{*/
123 static unsigned n_incoming_ipv4;
124 static unsigned n_incoming_ipv6;
125 static unsigned n_outgoing_ipv4;
126 static unsigned n_outgoing_ipv6;
127 /**@}*/
128 
129 /**
130  * Note that a connection has arrived or has been made, for use in the
131  * heartbeat message.
132  **/
133 void
134 note_connection(bool inbound, int family)
135 {
136  if (family == AF_INET) {
137  if (inbound) {
138  ++n_incoming_ipv4;
139  } else {
140  ++n_outgoing_ipv4;
141  }
142  } else if (family == AF_INET6) {
143  if (inbound) {
144  ++n_incoming_ipv6;
145  } else {
146  ++n_outgoing_ipv6;
147  }
148  }
149 }
150 
151 /** Log a "heartbeat" message describing Tor's status and history so that the
152  * user can know that there is indeed a running Tor. Return 0 on success and
153  * -1 on failure. */
154 int
155 log_heartbeat(time_t now)
156 {
157  char *bw_sent = NULL;
158  char *bw_rcvd = NULL;
159  char *uptime = NULL;
160  const routerinfo_t *me;
161  double r = tls_get_write_overhead_ratio();
162  const int hibernating = we_are_hibernating();
163 
164  const or_options_t *options = get_options();
165 
166  if (public_server_mode(options) && !hibernating) {
167  /* Let's check if we are in the current cached consensus. */
168  if (!(me = router_get_my_routerinfo()))
169  return -1; /* Something stinks, we won't even attempt this. */
170  else
171  if (!node_get_by_id(me->cache_info.identity_digest))
172  log_fn(LOG_NOTICE, LD_HEARTBEAT, "Heartbeat: It seems like we are not "
173  "in the cached consensus.");
174  }
175 
176  uptime = secs_to_uptime(get_uptime());
177  bw_rcvd = bytes_to_usage(get_bytes_read());
178  bw_sent = bytes_to_usage(get_bytes_written());
179 
180  log_fn(LOG_NOTICE, LD_HEARTBEAT, "Heartbeat: Tor's uptime is %s, with %d "
181  "circuits open. I've sent %s and received %s. I've received %u "
182  "connections on IPv4 and %u on IPv6. I've made %u connections "
183  "with IPv4 and %u with IPv6.%s",
184  uptime, count_circuits(), bw_sent, bw_rcvd,
185  n_incoming_ipv4, n_incoming_ipv6,
186  n_outgoing_ipv4, n_outgoing_ipv6,
187  hibernating?" We are currently hibernating.":"");
188 
190 
191  if (server_mode(options) && accounting_is_enabled(options) && !hibernating) {
192  log_accounting(now, options);
193  }
194 
195  double fullness_pct = 100;
196  if (stats_n_data_cells_packaged && !hibernating) {
197  fullness_pct =
198  100*(((double)stats_n_data_bytes_packaged) /
200  }
201  const double overhead_pct = ( r - 1.0 ) * 100.0;
202 
203 #define FULLNESS_PCT_THRESHOLD 80
204 #define TLS_OVERHEAD_THRESHOLD 15
205 
206  const int severity = (fullness_pct < FULLNESS_PCT_THRESHOLD ||
207  overhead_pct > TLS_OVERHEAD_THRESHOLD)
208  ? LOG_NOTICE : LOG_INFO;
209 
210  log_fn(severity, LD_HEARTBEAT,
211  "Average packaged cell fullness: %2.3f%%. "
212  "TLS write overhead: %.f%%", fullness_pct, overhead_pct);
213 
214  if (public_server_mode(options)) {
217  dos_log_heartbeat();
218  }
219 
221 
222  if (options->BridgeRelay) {
223  char *msg = NULL;
225  if (msg)
226  log_notice(LD_HEARTBEAT, "%s", msg);
227  tor_free(msg);
228  }
229 
230  if (options->MainloopStats) {
231  const uint64_t main_loop_success_count = get_main_loop_success_count();
232  const uint64_t main_loop_error_count = get_main_loop_error_count();
233  const uint64_t main_loop_idle_count = get_main_loop_idle_count();
234 
235  log_fn(LOG_NOTICE, LD_HEARTBEAT, "Main event loop statistics: "
236  "%"PRIu64 " successful returns, "
237  "%"PRIu64 " erroneous returns, and "
238  "%"PRIu64 " idle returns.",
239  (main_loop_success_count),
240  (main_loop_error_count),
241  (main_loop_idle_count));
242  }
243 
244  /** Now, if we are an HS service, log some stats about our usage */
246 
247  tor_free(uptime);
248  tor_free(bw_sent);
249  tor_free(bw_rcvd);
250 
251  return 0;
252 }
253 
254 static void
255 log_accounting(const time_t now, const or_options_t *options)
256 {
257  or_state_t *state = get_or_state();
258  char *acc_rcvd = bytes_to_usage(state->AccountingBytesReadInInterval);
259  char *acc_sent = bytes_to_usage(state->AccountingBytesWrittenInInterval);
260  char *acc_used = bytes_to_usage(get_accounting_bytes());
261  uint64_t acc_bytes = options->AccountingMax;
262  char *acc_max;
263  time_t interval_end = accounting_get_end_time();
264  char end_buf[ISO_TIME_LEN + 1];
265  char *remaining = NULL;
266  acc_max = bytes_to_usage(acc_bytes);
267  format_local_iso_time(end_buf, interval_end);
268  remaining = secs_to_uptime(interval_end - now);
269 
270  const char *acc_rule;
271  switch (options->AccountingRule) {
272  case ACCT_MAX: acc_rule = "max";
273  break;
274  case ACCT_SUM: acc_rule = "sum";
275  break;
276  case ACCT_OUT: acc_rule = "out";
277  break;
278  case ACCT_IN: acc_rule = "in";
279  break;
280  default: acc_rule = "max";
281  break;
282  }
283 
284  log_notice(LD_HEARTBEAT, "Heartbeat: Accounting enabled. "
285  "Sent: %s, Received: %s, Used: %s / %s, Rule: %s. The "
286  "current accounting interval ends on %s, in %s.",
287  acc_sent, acc_rcvd, acc_used, acc_max, acc_rule, end_buf, remaining);
288 
289  tor_free(acc_rcvd);
290  tor_free(acc_sent);
291  tor_free(acc_used);
292  tor_free(acc_max);
293  tor_free(remaining);
294 }
log_fn
#define log_fn(severity, domain, args,...)
Definition: log.h:287
routermode.h
Header file for routermode.c.
tor_free
#define tor_free(p)
Definition: malloc.h:52
routerinfo_t
Definition: routerinfo_st.h:20
hs_service.h
Header file containing service data for the HS subsytem.
or_state_t
Definition: or_state_st.h:21
get_main_loop_success_count
uint64_t get_main_loop_success_count(void)
Definition: mainloop.c:536
LOG_NOTICE
#define LOG_NOTICE
Definition: log.h:50
or_options_t::BridgeRelay
int BridgeRelay
Definition: or_options_st.h:248
circuit_get_global_list
smartlist_t * circuit_get_global_list(void)
Definition: circuitlist.c:696
get_bytes_read
uint64_t get_bytes_read(void)
Definition: mainloop.c:465
bytes_to_usage
STATIC char * bytes_to_usage(uint64_t bytes)
Definition: status.c:79
router.h
Header file for router.c.
circuituse.h
Header file for circuituse.c.
RELAY_PAYLOAD_SIZE
#define RELAY_PAYLOAD_SIZE
Definition: or.h:606
router_get_my_routerinfo
const routerinfo_t * router_get_my_routerinfo(void)
Definition: router.c:1760
hs_stats_get_n_introduce2_v2_cells
uint32_t hs_stats_get_n_introduce2_v2_cells(void)
Definition: hs_stats.c:40
count_circuits
STATIC int count_circuits(void)
Definition: status.c:44
node_get_by_id
const node_t * node_get_by_id(const char *identity_digest)
Definition: nodelist.c:223
we_are_hibernating
int we_are_hibernating(void)
Definition: hibernate.c:937
statefile.h
Header for statefile.c.
get_or_state
or_state_t * get_or_state(void)
Definition: statefile.c:208
geoip_stats.h
Header file for geoip_stats.c.
signed_descriptor_t::identity_digest
char identity_digest[DIGEST_LEN]
Definition: signed_descriptor_st.h:31
note_connection
void note_connection(bool inbound, int family)
Definition: status.c:134
accounting_get_end_time
time_t accounting_get_end_time(void)
Definition: hibernate.c:322
get_bytes_written
uint64_t get_bytes_written(void)
Definition: mainloop.c:475
hibernate.h
Header file for hibernate.c.
log_onion_service_stats
static void log_onion_service_stats(void)
Definition: status.c:98
circuitlist.h
Header file for circuitlist.c.
mainloop.h
Header file for mainloop.c.
accounting_is_enabled
int accounting_is_enabled(const or_options_t *options)
Definition: hibernate.c:305
tortls.h
Headers for tortls.c.
secs_to_uptime
STATIC char * secs_to_uptime(long secs)
Definition: status.c:52
get_accounting_bytes
uint64_t get_accounting_bytes(void)
Definition: hibernate.c:478
rep_hist_log_link_protocol_counts
void rep_hist_log_link_protocol_counts(void)
Definition: rephist.c:2111
hs_stats.h
Header file for hs_stats.c.
nodelist.h
Header file for nodelist.c.
LD_HEARTBEAT
#define LD_HEARTBEAT
Definition: log.h:103
dirclient_dump_total_dls
void dirclient_dump_total_dls(void)
Definition: dirclient.c:1986
routerinfo_st.h
Router descriptor structure.
or_options_t::MainloopStats
int MainloopStats
Definition: or_options_st.h:417
LOG_INFO
#define LOG_INFO
Definition: log.h:45
get_options
const or_options_t * get_options(void)
Definition: config.c:928
or_state_st.h
The or_state_t structure, which represents Tor's state file.
tls_get_write_overhead_ratio
double tls_get_write_overhead_ratio(void)
Definition: tortls_nss.c:712
relay.h
Header file for relay.c.
server_mode
int server_mode(const or_options_t *options)
Definition: routermode.c:34
dirclient.h
Header file for dirclient.c.
tor_asprintf
int tor_asprintf(char **strp, const char *fmt,...)
Definition: printf.c:75
get_main_loop_error_count
uint64_t get_main_loop_error_count(void)
Definition: mainloop.c:550
format_client_stats_heartbeat
char * format_client_stats_heartbeat(time_t now)
Definition: geoip_stats.c:1205
public_server_mode
int public_server_mode(const or_options_t *options)
Definition: routermode.c:43
hs_stats_get_n_rendezvous_launches
uint32_t hs_stats_get_n_rendezvous_launches(void)
Definition: hs_stats.c:54
log_heartbeat
int log_heartbeat(time_t now)
Definition: status.c:155
stats_n_data_bytes_packaged
uint64_t stats_n_data_bytes_packaged
Definition: relay.c:2048
or_options_t::AccountingMax
uint64_t AccountingMax
Definition: or_options_st.h:493
format_local_iso_time
void format_local_iso_time(char *buf, time_t t)
Definition: time_fmt.c:285
config.h
Header file for config.c.
or_options_t
Definition: or_options_st.h:45
get_uptime
long get_uptime(void)
Definition: mainloop.c:2522
circuit_log_ancient_one_hop_circuits
void circuit_log_ancient_one_hop_circuits(int age)
Definition: circuituse.c:883
STATIC
#define STATIC
Definition: testsupport.h:32
get_main_loop_idle_count
uint64_t get_main_loop_idle_count(void)
Definition: mainloop.c:564
hs_service_get_num_services
unsigned int hs_service_get_num_services(void)
Definition: hs_service.c:3799
status.h
Header for status.c.
rephist.h
Header file for rephist.c.
stats_n_data_cells_packaged
uint64_t stats_n_data_cells_packaged
Definition: relay.c:2044
rep_hist_log_circuit_handshake_stats
void rep_hist_log_circuit_handshake_stats(time_t now)
Definition: rephist.c:1696
or.h
Master header file for Tor-specific functionality.
hs_stats_get_n_introduce2_v3_cells
uint32_t hs_stats_get_n_introduce2_v3_cells(void)
Definition: hs_stats.c:33