Commit Graph

585 Commits

Author SHA1 Message Date
Nick Mathewson 20b179bfa9 Replace or remove testing eprintln!()s.
The clippy code for warning about these on nightly CI can't tell the
difference between cfg(test) and no cfg(test).
2021-11-13 11:17:49 -05:00
Nick Mathewson b26fbdb24f Resolve a dead-code warning on nightly.
The `circid` field in `ClientCirc` is now testing-only.
2021-11-13 11:14:11 -05:00
eta c559754116 Get rid of unbounded stream sender, and RawCellStream
Previously, the reactor would use an `UnboundedSender` to send things to
the `RawCellStream`, in order that the reactor wouldn't block if you
failed to read from the latter. This is bad, though, since it means
people can just run us out of memory by sending lots of things.

To fix this, we make the new `StreamReader` type (which does the reading
parts from `RawCellStream`) keep track of the stream's receive window
and issue SENDMEs once *it* has consumed enough data to require it, thus
meaning that we shouldn't get sent enough data to fill the channel
between reactor and `StreamReader` (and, if we do, that's someone trying
to flood us, and we abort the circuit).

As hinted to above, the `RawCellStream` was removed and its reading
functionalities replaced by `StreamReader`; its writing functionalities
are handled by `StreamTarget` anyway, so we just give out one of those
for the write end. This now means we don't need any mutexes!

note: this commit introduces a known issue, arti#230
2021-11-12 15:04:27 +00:00
eta 197816d14c Completely overhaul the tor-proto circuit reactor
Rather like e8e9699c3c ("Get rid of
tor-proto's ChannelImpl, and use the reactor more instead"), this
admittedly rather large commit refactors the way circuits in `tor-proto`
work, centralising all of the logic in one large nonblocking reactor
which other things send messages into and out of, instead of having a
bunch of `-Impl` types that are protected by mutexes.

Congestion control becomes a lot simpler with this refactor, since the
reactor can manage both stream- and circuit-level congestion control
unilaterally without having to share this information with consumers,
meaning we can get rid of some locks.

The way streams work also changes, in order to facilitate better
handling of backpressure / fairness between streams: each stream now has
a set of channels to send and receive messages over, instead of sending
relay cells directly onto the channel (now, the reactor pulls messages
off each stream in each map, and tries to avoid doing so if it won't be
able to forward them yet).

Additionally, a lot of "close this circuit / stream" messages aren't
required any more, since that state is simply indicated by one end of a
channel going away. This should make cleanup a lot less brittle.

Getting all of this to work involved writing a fair deal of intricate
nonblocking code in Reactor::run_once that tries very hard to be mindful
of making backpressure work correctly (and congestion control); the old
code could get away with having tasks .await on things, but the new
reactor can't really do this (as it'd lock the reactor up), so has to do
everything in a nonblocking manner.
2021-11-12 15:04:24 +00:00
Nick Mathewson 5287ac1858 Merge IpVersionPreferences and the optimistic flag into one type.
It seems like a good time to do this, before we add a zillion other
arguments to begin_stream.
2021-11-10 11:04:55 -05:00
Nick Mathewson 428b19c121 Refactor wait_for_connection a bit.
* Make it crate-visible only.
* Make it idempotent
* Have it be an internal error if it's called at the wrong time.
* Simplify the return logic.
2021-11-10 10:33:31 -05:00
Yuan Lyu 7f799c956b Implement optimistic stream 2021-11-09 21:36:12 -05:00
Nick Mathewson 83d9f2299e Replace all println/eprintln calls outside of arti CLI with trace. 2021-11-04 11:04:59 -04:00
eta db6b91164e tor-proto: Use a dedicated sender for channel cells, make full-duplex
@nickm pointed out that refactoring tor_proto::channel's Reactor to do
sending as well meant that it could only send or receive, but not both,
simultaneously, which was bad!

To fix this, rewrite Reactor::run_once to use a handcrafted future (with
futures::future::poll_fn) that can handle the logic required to push
items onto the sink asynchronously (i.e. checking that it can be written
to before trying to do that, and then flushing it).

This also means we don't use select_biased! any more, and just handroll
that logic ourselves; as a small bonus, we can now process all 3 kinds
of message in one run_once() call, instead of having to do only one of
them.
2021-11-03 16:48:54 +00:00
eta e8e9699c3c Get rid of tor-proto's ChannelImpl, and use the reactor more instead
Instead of awkwardly sharing the internals of a `tor-proto` `Channel`
between the reactor task and any other tasks, move most of the internals
into the reactor and have other tasks communicate with the reactor via
message-passing to allocate circuits and send cells.

This makes a lot of things simple, and has convenient properties like
not needing to wrap the `Channel` in an `Arc` (though some places in the
code still do this for now).

A lot of test code required tweaking in order to deal with the refactor;
in fact, fixing the tests probably took longer than writing the mainline
code (!). Importantly, we now use `tokio`'s `tokio::test` annotation
instead of `async_test`, so that we can run things in the background
(which is required to have reactors running for the circuit tests).

This is an instance of #205, and also kind of #217.
2021-11-03 13:19:45 +00:00
Nick Mathewson 656a8e5f48 Merge branch 'timestamp' 2021-11-02 12:17:15 -04:00
Nick Mathewson dddf67a902 Use coarsetime to build an incoming traffic timestamp.
We need this for the circuit timeout estimator (#57).  It needs to
know "how recently have we got some incoming traffic", so that it
can tell whether a circuit has truly timed out, or whether the
entire network is down.

I'm implementing this with coarsetime, since we need to update these
in response to every single incoming cell, and we need the timestamp
operation to be _fast_.

(This reinstates an earlier commit, f30b2280, which I reverted
because we didn't need it at the time.)

Closes #179.
2021-11-02 12:17:11 -04:00
eta 2979bb22a2 Refactor tor_proto::circuit::Reactor to use an UnboundedSender
Basically the same thing as 371437d338
("Refactor tor_proto::channel::Reactor to use an UnboundedSender"), but
for tor_proto::circuit's Reactor instead.

(part of arti#217)
2021-11-02 14:26:25 +00:00
Nick Mathewson 118fddb15f Merge remote-tracking branch 'origin/mr/118' 2021-11-02 10:14:58 -04:00
eta 371437d338 Refactor tor_proto::channel::Reactor to use an UnboundedSender
There wasn't any good reason for tor-proto's channel reactor to use a
shedload of oneshot channels instead of just an mpsc UnboundedSender,
and the whole `CtrlResult` thing made even less sense.

Straighten this code out by replacing all of that machinery with a
simple UnboundedSender, instead.

(part of arti#218)
2021-11-02 14:02:58 +00:00
Nick Mathewson cdae4c033c Remove some dbg!() calls in real code. 2021-11-02 09:50:55 -04:00
Nick Mathewson e6e740646a Bump all crate versions to 0.0.1 2021-10-29 11:05:51 -04:00
eta a12fffc66a Improve docs of more (potentially re-exported) arti-client types
Most of the structs in `arti-client` have example code now, to give a
clearer idea of how they're used.

Annoyingly, a lot of the types exposed in `arti-client` are actually
re-exports, which makes documentation a bit harder: example code that
references other parts of `arti-client` can't actually be run as a
doctest, since the crate it's in is a dependency of `arti-client`.

We might be able to fix this in future by doing the documentation in
`arti-client` itself, but rustdoc seems to have some weird behaviours
there that need to be investigated first (for example, it seems to merge
the re-export and original documentation, and also put the re-export
documentation on the `impl` block for some reason).

For now, though, this commit just writes the docs from the point of view
of an `arti-client` consumer, removing notes specific to the crate in
which they're defined. It's not ideal, but at least the end user
experience is decent.
2021-10-29 14:06:06 +01:00
Nick Mathewson 2057142671 Use correct link for AsyncWriteExt::flush 2021-10-28 20:48:54 -04:00
Nick Mathewson c8f65e532e DataStream: document the importance of flush(). 2021-10-28 20:21:35 -04:00
Nick Mathewson 134c04a67a Update our disclaimers and limitations sections. 2021-10-27 11:13:46 -04:00
Nick Mathewson b477f12d83 s/arti-arti-client/arti-client/ and regenerate readme files 2021-10-25 08:40:00 -04:00
Nick Mathewson 730be38867 Replace references to arti-client in the documentation. 2021-10-21 14:22:21 -04:00
Nick Mathewson e9a29b4523 Remove #![allow(clippy::unnecessary_wraps)] in tor-proto. 2021-10-21 14:03:32 -04:00
Nick Mathewson fb2c7cb85a Also implement tokio Async{Read,Write} on Data{Reader,Writer}.
This will let callers use the tokio traits on these types too, if
they call `split()` on the DataStream.

(Tokio also has a `tokio::io::split()` method, but it requires a
lock whereas `DataStream::split()` doesn't.)
2021-10-19 15:49:49 -04:00
Nick Mathewson a9a9f70eb9 Add a little documentation about when you'll need the tokio trait. 2021-10-19 15:48:55 -04:00
eta ccd1d36e90 tor-proto: implement tokio Async{Read, Write} traits conditionally
futures::io::AsyncRead (and Write) isn't the same thing as tokio::io::AsyncRead,
which is a somewhat annoying misfeature of the Rust async ecosystem (!).

To mitigate this somewhat for people trying to use the `DataStream` struct with
tokio, implement the tokio versions of the above traits using `tokio-util`'s
compat layer, if a crate feature (`tokio`) is enabled.
2021-10-19 19:59:29 +01:00
Nick Mathewson 9df9706010 Use append in place of extend_from_slice in DataReaderImpl::add_data.
Suggested by @cheako.
2021-10-17 12:31:10 -04:00
Nick Mathewson af7c9d5a0b enable checked_conversions lint. 2021-10-09 16:53:13 -04:00
Nick Mathewson 10bedd925c Use subtle and some refactoring to remove branches in ntor
Closes #163
2021-10-01 12:27:24 -04:00
Nick Mathewson 1c97918d67 Typo fixes 2021-09-09 13:06:05 -04:00
Nick Mathewson d9dc6f9d5c Fix clippy warnings in tests with --all-features 2021-09-08 14:28:38 -04:00
Daniel Eades fb3b8b84b5 fix/silence clippy lints in test modules 2021-09-08 17:28:31 +02:00
Jani Monoses 25b4421fa6 Fix typos 2021-09-07 07:52:00 +03:00
Nick Mathewson 557a0ff40b Move all crates into a `crates` subdirectory.
This will cause some pain for now, but now is really the best time
to do this kind of thing.
2021-08-27 09:53:09 -04:00