cmux: Make EWMA policy mandatory
[tor.git] / src / or / channel.h
blob6cf8cd7f7238be453a153f4391731f171c2c1a54
1 /* * Copyright (c) 2012-2017, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 /**
5 * \file channel.h
6 * \brief Header file for channel.c
7 **/
9 #ifndef TOR_CHANNEL_H
10 #define TOR_CHANNEL_H
12 #include "or.h"
13 #include "circuitmux.h"
14 #include "timers.h"
15 #include "handles.h"
17 /* Channel handler function pointer typedefs */
18 typedef void (*channel_listener_fn_ptr)(channel_listener_t *, channel_t *);
19 typedef void (*channel_cell_handler_fn_ptr)(channel_t *, cell_t *);
20 typedef void (*channel_var_cell_handler_fn_ptr)(channel_t *, var_cell_t *);
22 /**
23 * This enum is used by channelpadding to decide when to pad channels.
24 * Don't add values to it without updating the checks in
25 * channelpadding_decide_to_pad_channel().
27 typedef enum {
28 CHANNEL_USED_NOT_USED_FOR_FULL_CIRCS = 0,
29 CHANNEL_USED_FOR_FULL_CIRCS,
30 CHANNEL_USED_FOR_USER_TRAFFIC,
31 } channel_usage_info_t;
33 /**
34 * Channel struct; see the channel_t typedef in or.h. A channel is an
35 * abstract interface for the OR-to-OR connection, similar to connection_or_t,
36 * but without the strong coupling to the underlying TLS implementation. They
37 * are constructed by calling a protocol-specific function to open a channel
38 * to a particular node, and once constructed support the abstract operations
39 * defined below.
41 struct channel_s {
42 /** Magic number for type-checking cast macros */
43 uint32_t magic;
45 /** List entry for hashtable for global-identifier lookup. */
46 HT_ENTRY(channel_s) gidmap_node;
48 /** Handle entry for handle-based lookup */
49 HANDLE_ENTRY(channel, channel_s);
51 /** Current channel state */
52 channel_state_t state;
54 /** Globally unique ID number for a channel over the lifetime of a Tor
55 * process. This may not be 0.
57 uint64_t global_identifier;
59 /** Should we expect to see this channel in the channel lists? */
60 unsigned char registered:1;
62 /** has this channel ever been open? */
63 unsigned int has_been_open:1;
65 /**
66 * This field indicates if the other side has enabled or disabled
67 * padding via either the link protocol version or
68 * channelpadding_negotiate cells.
70 * Clients can override this with ConnectionPadding in torrc to
71 * disable or force padding to relays, but relays cannot override the
72 * client's request.
74 unsigned int padding_enabled:1;
76 /** Cached value of our decision to pad (to avoid expensive
77 * checks during critical path statistics counting). */
78 unsigned int currently_padding:1;
80 /** Is there a pending netflow padding callback? */
81 unsigned int pending_padding_callback:1;
83 /** Is our peer likely to consider this channel canonical? */
84 unsigned int is_canonical_to_peer:1;
86 /** Has this channel ever been used for non-directory traffic?
87 * Used to decide what channels to pad, and when. */
88 channel_usage_info_t channel_usage;
90 /** When should we send a cell for netflow padding? 0 means no padding is
91 * scheduled. */
92 monotime_coarse_t next_padding_time;
94 /** The callback pointer for the padding callbacks */
95 tor_timer_t *padding_timer;
96 /** The handle to this channel (to free on canceled timers) */
97 struct channel_handle_t *timer_handle;
99 /**
100 * These two fields specify the minimum and maximum negotiated timeout
101 * values for inactivity (send or receive) before we decide to pad a
102 * channel. These fields can be set either via a PADDING_NEGOTIATE cell,
103 * or the torrc option ReducedConnectionPadding. The consensus parameters
104 * nf_ito_low and nf_ito_high are used to ensure that padding can only be
105 * negotiated to be less frequent than what is specified in the consensus.
106 * (This is done to prevent wingnut clients from requesting excessive
107 * padding).
109 * The actual timeout value is randomly chosen between these two values
110 * as per the table in channelpadding_get_netflow_inactive_timeout_ms(),
111 * after ensuring that these values do not specify lower timeouts than
112 * the consensus parameters.
114 * If these are 0, we have not negotiated or specified custom padding
115 * times, and instead use consensus defaults. */
116 uint16_t padding_timeout_low_ms;
117 uint16_t padding_timeout_high_ms;
119 /** Why did we close?
121 enum {
122 CHANNEL_NOT_CLOSING = 0,
123 CHANNEL_CLOSE_REQUESTED,
124 CHANNEL_CLOSE_FROM_BELOW,
125 CHANNEL_CLOSE_FOR_ERROR
126 } reason_for_closing;
128 /** State variable for use by the scheduler */
129 enum {
131 * The channel is not open, or it has a full output buffer but no queued
132 * cells.
134 SCHED_CHAN_IDLE = 0,
136 * The channel has space on its output buffer to write, but no queued
137 * cells.
139 SCHED_CHAN_WAITING_FOR_CELLS,
141 * The scheduler has queued cells but no output buffer space to write.
143 SCHED_CHAN_WAITING_TO_WRITE,
145 * The scheduler has both queued cells and output buffer space, and is
146 * eligible for the scheduler loop.
148 SCHED_CHAN_PENDING
149 } scheduler_state;
151 /** Heap index for use by the scheduler */
152 int sched_heap_idx;
154 /** Timestamps for both cell channels and listeners */
155 time_t timestamp_created; /* Channel created */
156 time_t timestamp_active; /* Any activity */
159 * This is a monotonic timestamp that marks when we
160 * believe the channel has actually sent or received data to/from
161 * the wire. Right now, it is used to determine when we should send
162 * a padding cell for channelpadding.
164 * XXX: Are we setting timestamp_xfer_ms in the right places to
165 * accurately reflect actual network data transfer? Or might this be
166 * very wrong wrt when bytes actually go on the wire?
168 monotime_coarse_t timestamp_xfer;
170 /* Methods implemented by the lower layer */
172 /** Free a channel */
173 void (*free_fn)(channel_t *);
174 /** Close an open channel */
175 void (*close)(channel_t *);
176 /** Describe the transport subclass for this channel */
177 const char * (*describe_transport)(channel_t *);
178 /** Optional method to dump transport-specific statistics on the channel */
179 void (*dumpstats)(channel_t *, int);
181 /** Registered handlers for incoming cells */
182 channel_cell_handler_fn_ptr cell_handler;
183 channel_var_cell_handler_fn_ptr var_cell_handler;
185 /* Methods implemented by the lower layer */
188 * Ask the lower layer for an estimate of the average overhead for
189 * transmissions on this channel.
191 double (*get_overhead_estimate)(channel_t *);
193 * Ask the underlying transport what the remote endpoint address is, in
194 * a tor_addr_t. This is optional and subclasses may leave this NULL.
195 * If they implement it, they should write the address out to the
196 * provided tor_addr_t *, and return 1 if successful or 0 if no address
197 * available.
199 int (*get_remote_addr)(channel_t *, tor_addr_t *);
200 int (*get_transport_name)(channel_t *chan, char **transport_out);
202 #define GRD_FLAG_ORIGINAL 1
203 #define GRD_FLAG_ADDR_ONLY 2
205 * Get a text description of the remote endpoint; canonicalized if the flag
206 * GRD_FLAG_ORIGINAL is not set, or the one we originally connected
207 * to/received from if it is. If GRD_FLAG_ADDR_ONLY is set, we return only
208 * the original address.
210 const char * (*get_remote_descr)(channel_t *, int);
211 /** Check if the lower layer has queued writes */
212 int (*has_queued_writes)(channel_t *);
214 * If the second param is zero, ask the lower layer if this is
215 * 'canonical', for a transport-specific definition of canonical; if
216 * it is 1, ask if the answer to the preceding query is safe to rely
217 * on.
219 int (*is_canonical)(channel_t *, int);
220 /** Check if this channel matches a specified extend_info_t */
221 int (*matches_extend_info)(channel_t *, extend_info_t *);
222 /** Check if this channel matches a target address when extending */
223 int (*matches_target)(channel_t *, const tor_addr_t *);
224 /* Ask the lower layer how many bytes it has queued but not yet sent */
225 size_t (*num_bytes_queued)(channel_t *);
226 /* Ask the lower layer how many cells can be written */
227 int (*num_cells_writeable)(channel_t *);
228 /* Write a cell to an open channel */
229 int (*write_cell)(channel_t *, cell_t *);
230 /** Write a packed cell to an open channel */
231 int (*write_packed_cell)(channel_t *, packed_cell_t *);
232 /** Write a variable-length cell to an open channel */
233 int (*write_var_cell)(channel_t *, var_cell_t *);
236 * Hash of the public RSA key for the other side's RSA identity key -- or
237 * zeroes if we don't have an RSA identity in mind for the other side, and
238 * it hasn't shown us one.
240 * Note that this is the RSA identity that we hope the other side has -- not
241 * necessarily its true identity. Don't believe this identity unless
242 * authentication has happened.
244 char identity_digest[DIGEST_LEN];
246 * Ed25519 key for the other side of this channel -- or zeroes if we don't
247 * have an Ed25519 identity in mind for the other side, and it hasn't shown
248 * us one.
250 * Note that this is the identity that we hope the other side has -- not
251 * necessarily its true identity. Don't believe this identity unless
252 * authentication has happened.
254 ed25519_public_key_t ed25519_identity;
257 * Linked list of channels with the same RSA identity digest, for use with
258 * the digest->channel map
260 TOR_LIST_ENTRY(channel_s) next_with_same_id;
262 /** Circuit mux for circuits sending on this channel */
263 circuitmux_t *cmux;
265 /** Circuit ID generation stuff for use by circuitbuild.c */
268 * When we send CREATE cells along this connection, which half of the
269 * space should we use?
271 circ_id_type_bitfield_t circ_id_type:2;
272 /* DOCDOC */
273 unsigned wide_circ_ids:1;
275 /** For how many circuits are we n_chan? What about p_chan? */
276 unsigned int num_n_circuits, num_p_circuits;
279 * True iff this channel shouldn't get any new circs attached to it,
280 * because the connection is too old, or because there's a better one.
281 * More generally, this flag is used to note an unhealthy connection;
282 * for example, if a bad connection fails we shouldn't assume that the
283 * router itself has a problem.
285 unsigned int is_bad_for_new_circs:1;
287 /** True iff we have decided that the other end of this connection
288 * is a client or bridge relay. Connections with this flag set should never
289 * be used to satisfy an EXTEND request. */
290 unsigned int is_client:1;
292 /** Set if the channel was initiated remotely (came from a listener) */
293 unsigned int is_incoming:1;
295 /** Set by lower layer if this is local; i.e., everything it communicates
296 * with for this channel returns true for is_local_addr(). This is used
297 * to decide whether to declare reachability when we receive something on
298 * this channel in circuitbuild.c
300 unsigned int is_local:1;
302 /** Have we logged a warning about circID exhaustion on this channel?
303 * If so, when? */
304 ratelim_t last_warned_circ_ids_exhausted;
306 /** Channel timestamps for cell channels */
307 time_t timestamp_client; /* Client used this, according to relay.c */
308 time_t timestamp_recv; /* Cell received from lower layer */
309 time_t timestamp_xmit; /* Cell sent to lower layer */
311 /** Timestamp for run_connection_housekeeping(). We update this once a
312 * second when we run housekeeping and find a circuit on this channel, and
313 * whenever we add a circuit to the channel. */
314 time_t timestamp_last_had_circuits;
316 /** Unique ID for measuring direct network status requests;vtunneled ones
317 * come over a circuit_t, which has a dirreq_id field as well, but is a
318 * distinct namespace. */
319 uint64_t dirreq_id;
321 /** Channel counters for cell channels */
322 uint64_t n_cells_recved, n_bytes_recved;
323 uint64_t n_cells_xmitted, n_bytes_xmitted;
326 struct channel_listener_s {
327 /* Current channel listener state */
328 channel_listener_state_t state;
330 /* Globally unique ID number for a channel over the lifetime of a Tor
331 * process.
333 uint64_t global_identifier;
335 /** Should we expect to see this channel in the channel lists? */
336 unsigned char registered:1;
338 /** Why did we close?
340 enum {
341 CHANNEL_LISTENER_NOT_CLOSING = 0,
342 CHANNEL_LISTENER_CLOSE_REQUESTED,
343 CHANNEL_LISTENER_CLOSE_FROM_BELOW,
344 CHANNEL_LISTENER_CLOSE_FOR_ERROR
345 } reason_for_closing;
347 /** Timestamps for both cell channels and listeners */
348 time_t timestamp_created; /* Channel created */
349 time_t timestamp_active; /* Any activity */
351 /* Methods implemented by the lower layer */
353 /** Free a channel */
354 void (*free_fn)(channel_listener_t *);
355 /** Close an open channel */
356 void (*close)(channel_listener_t *);
357 /** Describe the transport subclass for this channel */
358 const char * (*describe_transport)(channel_listener_t *);
359 /** Optional method to dump transport-specific statistics on the channel */
360 void (*dumpstats)(channel_listener_t *, int);
362 /** Registered listen handler to call on incoming connection */
363 channel_listener_fn_ptr listener;
365 /** List of pending incoming connections */
366 smartlist_t *incoming_list;
368 /** Timestamps for listeners */
369 time_t timestamp_accepted;
371 /** Counters for listeners */
372 uint64_t n_accepted;
375 /* Channel state manipulations */
377 int channel_state_is_valid(channel_state_t state);
378 int channel_listener_state_is_valid(channel_listener_state_t state);
380 int channel_state_can_transition(channel_state_t from, channel_state_t to);
381 int channel_listener_state_can_transition(channel_listener_state_t from,
382 channel_listener_state_t to);
384 const char * channel_state_to_string(channel_state_t state);
385 const char *
386 channel_listener_state_to_string(channel_listener_state_t state);
388 /* Abstract channel operations */
390 void channel_mark_for_close(channel_t *chan);
391 int channel_write_packed_cell(channel_t *chan, packed_cell_t *cell);
393 void channel_listener_mark_for_close(channel_listener_t *chan_l);
395 /* Channel callback registrations */
397 /* Listener callback */
398 void channel_listener_set_listener_fn(channel_listener_t *chan,
399 channel_listener_fn_ptr listener);
401 /* Incoming cell callbacks */
402 channel_cell_handler_fn_ptr channel_get_cell_handler(channel_t *chan);
404 channel_var_cell_handler_fn_ptr
405 channel_get_var_cell_handler(channel_t *chan);
407 void channel_set_cell_handlers(channel_t *chan,
408 channel_cell_handler_fn_ptr cell_handler,
409 channel_var_cell_handler_fn_ptr
410 var_cell_handler);
412 /* Clean up closed channels and channel listeners periodically; these are
413 * called from run_scheduled_events() in main.c.
415 void channel_run_cleanup(void);
416 void channel_listener_run_cleanup(void);
418 /* Close all channels and deallocate everything */
419 void channel_free_all(void);
421 /* Dump some statistics in the log */
422 void channel_dumpstats(int severity);
423 void channel_listener_dumpstats(int severity);
425 #ifdef TOR_CHANNEL_INTERNAL_
427 #ifdef CHANNEL_PRIVATE_
429 STATIC void channel_add_to_digest_map(channel_t *chan);
431 #endif /* defined(CHANNEL_PRIVATE_) */
433 /* Channel operations for subclasses and internal use only */
435 /* Initialize a newly allocated channel - do this first in subclass
436 * constructors.
439 void channel_init(channel_t *chan);
440 void channel_init_listener(channel_listener_t *chan);
442 /* Channel registration/unregistration */
443 void channel_register(channel_t *chan);
444 void channel_unregister(channel_t *chan);
446 /* Channel listener registration/unregistration */
447 void channel_listener_register(channel_listener_t *chan_l);
448 void channel_listener_unregister(channel_listener_t *chan_l);
450 /* Close from below */
451 void channel_close_from_lower_layer(channel_t *chan);
452 void channel_close_for_error(channel_t *chan);
453 void channel_closed(channel_t *chan);
455 /* Free a channel */
456 void channel_free_(channel_t *chan);
457 #define channel_free(chan) FREE_AND_NULL(channel_t, channel_free_, (chan))
458 void channel_listener_free_(channel_listener_t *chan_l);
459 #define channel_listener_free(chan_l) \
460 FREE_AND_NULL(channel_listener_t, channel_listener_free_, (chan_l))
462 /* State/metadata setters */
464 void channel_change_state(channel_t *chan, channel_state_t to_state);
465 void channel_change_state_open(channel_t *chan);
466 void channel_clear_identity_digest(channel_t *chan);
467 void channel_clear_remote_end(channel_t *chan);
468 void channel_mark_local(channel_t *chan);
469 void channel_mark_incoming(channel_t *chan);
470 void channel_mark_outgoing(channel_t *chan);
471 void channel_mark_remote(channel_t *chan);
472 void channel_set_identity_digest(channel_t *chan,
473 const char *identity_digest,
474 const ed25519_public_key_t *ed_identity);
476 void channel_listener_change_state(channel_listener_t *chan_l,
477 channel_listener_state_t to_state);
479 /* Timestamp updates */
480 void channel_timestamp_created(channel_t *chan);
481 void channel_timestamp_active(channel_t *chan);
482 void channel_timestamp_recv(channel_t *chan);
483 void channel_timestamp_xmit(channel_t *chan);
485 void channel_listener_timestamp_created(channel_listener_t *chan_l);
486 void channel_listener_timestamp_active(channel_listener_t *chan_l);
487 void channel_listener_timestamp_accepted(channel_listener_t *chan_l);
489 /* Incoming channel handling */
490 void channel_listener_process_incoming(channel_listener_t *listener);
491 void channel_listener_queue_incoming(channel_listener_t *listener,
492 channel_t *incoming);
494 /* Incoming cell handling */
495 void channel_process_cell(channel_t *chan, cell_t *cell);
497 /* Request from lower layer for more cells if available */
498 MOCK_DECL(ssize_t, channel_flush_some_cells,
499 (channel_t *chan, ssize_t num_cells));
501 /* Query if data available on this channel */
502 MOCK_DECL(int, channel_more_to_flush, (channel_t *chan));
504 /* Notify flushed outgoing for dirreq handling */
505 void channel_notify_flushed(channel_t *chan);
507 /* Handle stuff we need to do on open like notifying circuits */
508 void channel_do_open_actions(channel_t *chan);
510 #endif /* defined(TOR_CHANNEL_INTERNAL_) */
512 /* Helper functions to perform operations on channels */
514 int channel_send_destroy(circid_t circ_id, channel_t *chan,
515 int reason);
518 * Outside abstract interfaces that should eventually get turned into
519 * something transport/address format independent.
522 channel_t * channel_connect(const tor_addr_t *addr, uint16_t port,
523 const char *rsa_id_digest,
524 const ed25519_public_key_t *ed_id);
526 channel_t * channel_get_for_extend(const char *rsa_id_digest,
527 const ed25519_public_key_t *ed_id,
528 const tor_addr_t *target_addr,
529 const char **msg_out,
530 int *launch_out);
532 /* Ask which of two channels is better for circuit-extension purposes */
533 int channel_is_better(channel_t *a, channel_t *b);
535 /** Channel lookups
538 channel_t * channel_find_by_global_id(uint64_t global_identifier);
539 channel_t * channel_find_by_remote_identity(const char *rsa_id_digest,
540 const ed25519_public_key_t *ed_id);
542 /** For things returned by channel_find_by_remote_digest(), walk the list.
543 * The RSA key will match for all returned elements; the Ed25519 key might not.
545 channel_t * channel_next_with_rsa_identity(channel_t *chan);
548 * Helper macros to lookup state of given channel.
551 #define CHANNEL_IS_CLOSED(chan) (channel_is_in_state((chan), \
552 CHANNEL_STATE_CLOSED))
553 #define CHANNEL_IS_OPENING(chan) (channel_is_in_state((chan), \
554 CHANNEL_STATE_OPENING))
555 #define CHANNEL_IS_OPEN(chan) (channel_is_in_state((chan), \
556 CHANNEL_STATE_OPEN))
557 #define CHANNEL_IS_MAINT(chan) (channel_is_in_state((chan), \
558 CHANNEL_STATE_MAINT))
559 #define CHANNEL_IS_CLOSING(chan) (channel_is_in_state((chan), \
560 CHANNEL_STATE_CLOSING))
561 #define CHANNEL_IS_ERROR(chan) (channel_is_in_state((chan), \
562 CHANNEL_STATE_ERROR))
564 #define CHANNEL_FINISHED(chan) (CHANNEL_IS_CLOSED(chan) || \
565 CHANNEL_IS_ERROR(chan))
567 #define CHANNEL_CONDEMNED(chan) (CHANNEL_IS_CLOSING(chan) || \
568 CHANNEL_FINISHED(chan))
570 #define CHANNEL_CAN_HANDLE_CELLS(chan) (CHANNEL_IS_OPENING(chan) || \
571 CHANNEL_IS_OPEN(chan) || \
572 CHANNEL_IS_MAINT(chan))
574 static inline int
575 channel_is_in_state(channel_t *chan, channel_state_t state)
577 return chan->state == state;
581 * Metadata queries/updates
584 const char * channel_describe_transport(channel_t *chan);
585 MOCK_DECL(void, channel_dump_statistics, (channel_t *chan, int severity));
586 void channel_dump_transport_statistics(channel_t *chan, int severity);
587 const char * channel_get_actual_remote_descr(channel_t *chan);
588 const char * channel_get_actual_remote_address(channel_t *chan);
589 MOCK_DECL(int, channel_get_addr_if_possible, (channel_t *chan,
590 tor_addr_t *addr_out));
591 const char * channel_get_canonical_remote_descr(channel_t *chan);
592 int channel_has_queued_writes(channel_t *chan);
593 int channel_is_bad_for_new_circs(channel_t *chan);
594 void channel_mark_bad_for_new_circs(channel_t *chan);
595 int channel_is_canonical(channel_t *chan);
596 int channel_is_canonical_is_reliable(channel_t *chan);
597 int channel_is_client(const channel_t *chan);
598 int channel_is_local(channel_t *chan);
599 int channel_is_incoming(channel_t *chan);
600 int channel_is_outgoing(channel_t *chan);
601 void channel_mark_client(channel_t *chan);
602 void channel_clear_client(channel_t *chan);
603 int channel_matches_extend_info(channel_t *chan, extend_info_t *extend_info);
604 int channel_matches_target_addr_for_extend(channel_t *chan,
605 const tor_addr_t *target);
606 unsigned int channel_num_circuits(channel_t *chan);
607 MOCK_DECL(void,channel_set_circid_type,(channel_t *chan,
608 crypto_pk_t *identity_rcvd,
609 int consider_identity));
610 void channel_timestamp_client(channel_t *chan);
612 const char * channel_listener_describe_transport(channel_listener_t *chan_l);
613 void channel_listener_dump_statistics(channel_listener_t *chan_l,
614 int severity);
615 void channel_listener_dump_transport_statistics(channel_listener_t *chan_l,
616 int severity);
617 void channel_check_for_duplicates(void);
619 void channel_update_bad_for_new_circs(const char *digest, int force);
621 /* Flow control queries */
622 int channel_num_cells_writeable(channel_t *chan);
624 /* Timestamp queries */
625 time_t channel_when_created(channel_t *chan);
626 time_t channel_when_last_client(channel_t *chan);
627 time_t channel_when_last_xmit(channel_t *chan);
629 /* Counter queries */
630 int packed_cell_is_destroy(channel_t *chan,
631 const packed_cell_t *packed_cell,
632 circid_t *circid_out);
634 /* Declare the handle helpers */
635 HANDLE_DECL(channel, channel_s,)
636 #define channel_handle_free(h) \
637 FREE_AND_NULL(channel_handle_t, channel_handle_free_, (h))
639 #endif /* !defined(TOR_CHANNEL_H) */