1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
//! Module exposing the relay circuit reactor subsystem.
//!
//! The entry point of the reactor is [`RelayReactor::run`], which launches the
//! reactor background tasks, and begins listening for inbound cells on the provided
//! inbound Tor channel.
//!
//! ### Architecture
//!
//! Internally, the circuit reactor consists of two reactors, that run in separate tasks:
//!
//! * [`ForwardReactor`]: handles exit-bound cells, by moving cells in the
//! forward direction (from the client to the exit)
//! * [`BackwardReactor`]: handles client-bound cells, by moving cells in the
//! backward direction (from the exit to the client), and by packaging
//! and sending application stream data towards the client
//!
//! The read and write ends of the inbound and outbound Tor channels are "split",
//! such that each reactor holds an `input` stream (for reading)
//! and a `chan_sender` sink (for writing):
//!
//! * `ForwardReactor` holds the reading end of the inbound (coming from the client) Tor channel,
//! and the writing end of the outbound (towards the exit) Tor channel, if there is one
//! * `BackwardReactor` holds the reading end of the outbound channel, if there is one,
//! and the writing end of the inbound channel, if there is one
//!
//! Upon receiving an unrecognized cell, the `ForwardReactor` forwards it towards the exit.
//! However, upon receiving a *recognized* cell, the `ForwardReactor` might need to
//! send that cell to the `BackwardReactor` for handling (for example, a cell
//! containing stream data needs to be delivered to the appropriate stream
//! in the `StreamMap`). For this, it uses the `cell_tx` MPSC channel.
//! This is needed because the read and write sides of `StreamMap` are not "splittable",
//! so we are stuck having to reroute all stream data to the reactor that owns the `StreamMap`
//! (i.e. to `BackwardReactor`). In the future, we'd like to redesign the `StreamMap`
//! to split the read ends of the streams from the write ones, which will enable us
//! to pass the read side to the `ForwardReactor` and the write side to the `BackwardReactor`.
//
// TODO(relay): the above is underspecified, because it's not implemented yet,
// but the plan is to iron out these details soon
//
//! This dual reactor architecture should, in theory, have better performance than
//! a single reactor system, because it enables us to parallelize some of the work:
//! the forward and backward directions share little state,
//! because they read from, and write to, different sinks/streams,
//! so they can be run in parallel (as separate tasks).
//! With a single reactor architecture, the reactor would need to drive
//! both the forward and the backward direction, and on each iteration
//! would need to decide which to prioritize, which might prove tricky
//! (though prioritizing one of them at random would've probably been good enough).
//!
//! The monolithic single reactor alternative would also have been significantly
//! more convoluted, and so more difficult to maintain in the long run.
//!
//
// Note: if we address the TODO below, the dual reactor architecture might even
// have some performance benefits:
//
// TODO: the part about sharing little state is not entirely accurate.
// Right now, they share the congestion control state, which is behind a mutex,
// and, indirectly, the `StreamMap` (via the `cell_tx` construction).
// In the future, we'd like to switch to a lock-less architecture,
// but that will involve redesign `CongestionControl`
// (to be mutable without &mut, for example by using atomics under the hood).
mod backward;
mod forward;
use std::result::Result as StdResult;
use std::sync::{Arc, Mutex};
use futures::channel::mpsc;
use futures::{FutureExt as _, StreamExt as _, select_biased};
use postage::broadcast;
use tracing::{debug, trace};
use tor_cell::chancell::CircId;
use tor_cell::relaycell::RelayCellDecoder;
use tor_error::internal;
use tor_linkspec::HasRelayIds;
use tor_memquota::mq_queue::{self, MpscSpec};
use crate::channel::Channel;
use crate::circuit::UniqId;
use crate::circuit::celltypes::RelayCircChanMsg;
use crate::circuit::circhop::{CircHopInbound, CircHopOutbound, HopSettings};
use crate::congestion::CongestionControl;
use crate::crypto::cell::{InboundRelayLayer, OutboundRelayLayer};
use crate::memquota::CircuitAccount;
use crate::relay::RelayCirc;
use crate::relay::channel_provider::ChannelProvider;
use crate::util::err::ReactorError;
use backward::BackwardReactor;
use forward::ForwardReactor;
/// A message telling the reactor to do something.
///
/// For each `RelayCtrlMsg`, the reactor will send a cell on the underlying Tor channel.
///
/// The difference between this and [`RelayCtrlCmd`] is that `RelayCtrlMsg`s
/// cause the reactor to send cells on the reactor's `chan_sender`,
/// whereas `RelayCtrlCmd` do not.
///
// TODO(relay): we may not need this
#[allow(unused)] // TODO(relay)
#[derive(Debug)]
pub(crate) enum RelayCtrlMsg {}
/// A message telling the reactor to do something.
///
/// The difference between this and [`RelayCtrlMsg`] is that `RelayCtrlCmd`s
/// never cause cells to sent on the Tor channel,
/// while `RelayCtrlMsg`s potentially do.
//
// TODO(relay): we may not need this
#[derive(educe::Educe)]
#[educe(Debug)]
#[allow(unused)] // TODO(relay)
pub(crate) enum RelayCtrlCmd {
/// Shut down the reactor.
Shutdown,
}
/// The entry point of the circuit reactor subsystem.
#[allow(unused)] // TODO(relay)
#[must_use = "If you don't call run() on a reactor, the circuit won't work."]
pub(crate) struct RelayReactor<T: HasRelayIds> {
/// The process-unique identifier of this circuit.
///
/// Used for logging;
unique_id: UniqId,
/// The reactor for handling the forward direction (client to exit).
///
/// Optional so we can move it out of self in run().
forward: Option<ForwardReactor<T>>,
/// The reactor for handling the backward direction (exit to client).
///
/// Optional so we can move it out of self in run().
backward: Option<BackwardReactor>,
/// Receiver for control messages for this reactor, sent by reactor handle objects.
control: mpsc::UnboundedReceiver<RelayCtrlMsg>,
/// Receiver for command messages for this reactor, sent by reactor handle objects.
///
/// This MPSC channel is polled in [`run`](Self::run).
///
/// NOTE: this is a separate channel from `control`, because some messages
/// have higher priority and need to be handled even if the `chan_sender` is not
/// ready (whereas `control` messages are not read until the `chan_sender` sink
/// is ready to accept cells).
command: mpsc::UnboundedReceiver<RelayCtrlCmd>,
/// A sender that is used to alert the [`ForwardReactor`] and [`BackwardReactor`]
/// when this reactor is finally dropped.
///
/// It is a sender for Void because we never actually want to send anything here;
/// we only want to generate canceled events.
#[allow(dead_code)] // the only purpose of this field is to be dropped.
reactor_closed_tx: broadcast::Sender<void::Void>,
}
/// MPSC queue for inbound data on its way from channel to circuit, sender
#[allow(unused)] // TODO(relay)
pub(crate) type CircuitRxSender = mq_queue::Sender<RelayCircChanMsg, MpscSpec>;
/// MPSC queue for inbound data on its way from channel to circuit, receiver
pub(crate) type CircuitRxReceiver = mq_queue::Receiver<RelayCircChanMsg, MpscSpec>;
#[allow(unused)] // TODO(relay)
impl<T: HasRelayIds> RelayReactor<T> {
/// Create a new circuit reactor.
///
/// The reactor will send outbound messages on `channel`, receive incoming
/// messages on `input`, and identify this circuit by the channel-local
/// [`CircId`] provided.
///
/// The internal unique identifier for this circuit will be `unique_id`.
#[allow(clippy::needless_pass_by_value)] // TODO(relay)
#[allow(clippy::too_many_arguments)] // TODO
pub(super) fn new(
channel: Arc<Channel>,
circ_id: CircId,
unique_id: UniqId,
input: CircuitRxReceiver,
crypto_in: Box<dyn InboundRelayLayer + Send>,
crypto_out: Box<dyn OutboundRelayLayer + Send>,
settings: &HopSettings,
chan_provider: Box<dyn ChannelProvider<BuildSpec = T> + Send>,
memquota: CircuitAccount,
) -> (Self, RelayCirc) {
let (outgoing_chan_tx, outgoing_chan_rx) = mpsc::unbounded();
let (reactor_closed_tx, reactor_closed_rx) = broadcast::channel(0);
let (cell_tx, cell_rx) = mpsc::unbounded();
let (control_tx, control_rx) = mpsc::unbounded();
let (command_tx, command_rx) = mpsc::unbounded();
let relay_format = settings.relay_crypt_protocol().relay_cell_format();
let ccontrol = Arc::new(Mutex::new(CongestionControl::new(&settings.ccontrol)));
let inbound = CircHopInbound::new(
Arc::clone(&ccontrol),
RelayCellDecoder::new(relay_format),
settings,
);
let forward = ForwardReactor::new(
inbound,
unique_id,
input,
outgoing_chan_rx,
crypto_out,
chan_provider,
cell_tx,
reactor_closed_rx.clone(),
);
let outbound = CircHopOutbound::new(
ccontrol,
relay_format,
Arc::new(settings.flow_ctrl_params.clone()),
settings,
);
let backward = BackwardReactor::new(
channel,
outbound,
circ_id,
unique_id,
crypto_in,
settings,
cell_rx,
outgoing_chan_tx,
reactor_closed_rx.clone(),
);
let handle = RelayCirc {
control: control_tx,
command: command_tx,
};
let reactor = RelayReactor {
unique_id,
forward: Some(forward),
backward: Some(backward),
control: control_rx,
command: command_rx,
reactor_closed_tx,
};
(reactor, handle)
}
/// Launch the reactor, and run until the circuit closes or we
/// encounter an error.
///
/// Once this method returns, the circuit is dead and cannot be
/// used again.
pub(crate) async fn run(mut self) -> StdResult<(), ReactorError> {
let unique_id = self.unique_id;
debug!(
circ_id = %unique_id,
"Running relay circuit reactor",
);
let res = self.run_inner().await;
debug!(
circ_id = %unique_id,
"Relay circuit reactor shutting down",
);
res
}
/// Helper for [`run`](Self::run).
pub(crate) async fn run_inner(mut self) -> StdResult<(), ReactorError> {
let (forward, backward) = (|| Some((self.forward.take()?, self.backward.take()?)))()
.expect("relay reactor spawned twice?!");
let mut forward = Box::pin(forward.run()).fuse();
let mut backward = Box::pin(backward.run()).fuse();
loop {
// If either of these completes, this function returns,
// dropping reactor_closed_tx, which will, in turn,
// cause the remaining reactor, if there is one, to shut down too
select_biased! {
res = self.command.next() => {
let Some(cmd) = res else {
trace!(
circ_id = %self.unique_id,
reason = "command channel drop",
"reactor shutdown",
);
return Err(ReactorError::Shutdown);
};
self.handle_command(&cmd)?;
},
res = self.control.next() => {
let Some(msg) = res else {
trace!(
circ_id = %self.unique_id,
reason = "control channel drop",
"reactor shutdown",
);
return Err(ReactorError::Shutdown);
};
self.handle_control(&msg)?;
},
// No need to log the error here, because it was already logged
// by the reactor that shut down
res = forward => return Ok(res?),
res = backward => return Ok(res?),
}
}
}
/// Handle a [`RelayCtrlCmd`].
fn handle_command(&self, cmd: &RelayCtrlCmd) -> StdResult<(), ReactorError> {
match cmd {
RelayCtrlCmd::Shutdown => self.handle_shutdown(),
}
}
/// Handle a [`RelayCtrlMsg`].
#[allow(clippy::unnecessary_wraps)]
fn handle_control(&self, cmd: &RelayCtrlMsg) -> StdResult<(), ReactorError> {
Err(internal!("not implemented: {cmd:?}").into())
}
/// Handle a shutdown request.
fn handle_shutdown(&self) -> StdResult<(), ReactorError> {
trace!(
tunnel_id = %self.unique_id,
"reactor shutdown due to explicit request",
);
Err(ReactorError::Shutdown)
}
}
|