Commit Graph

8062 Commits

Author SHA1 Message Date
Micah Elizabeth Scott 0af908bcf2 hashx: Rearrange destination register validator for performance
This hoists a few decisions out of the innermost portions of
choose_dst_reg, by moving what we can out of dst_register_allowed.

Wallclock time benchmarks:
  generate-interp improves, -6.0%

Cachegrind benchmarks:
  generate_interp_1000x, -5.0% instructions, -11.6% L2 access, -6% RAM

Signed-off-by: Micah Elizabeth Scott <beth@torproject.org>
2023-08-21 15:27:28 -07:00
Micah Elizabeth Scott ceacd5c988 hashx: New approach to avoid memcpy in Program
I was trying to eliminate all the places where we copied a Program
(about 4100 bytes) except for the one final copy into a Box; but that
approach was proving too annoying. Even returning a Program via Result
will cause multiple unnecessary copies that don't optimize out.

This patch switches approaches, and instead allocates a Vec<Instruction>
presized to the correct capacity. This allocation is made as early as
possible and retained for the lifetime of the program if necessary.
This means we'll never avoid a heap allocation, but we can always
avoid extra copies and we don't need a separate Box for interpreted
programs.

Performance effects are subtle. Overall wallclock time doesn't change
much. Cachegrind shows some accesses moving up from RAM to L2 cache.
Using GDB to probe memcpy sizes shows that large (>1024b) memcpy are now
totally gone in the generate-interp test.

Signed-off-by: Micah Elizabeth Scott <beth@torproject.org>
2023-08-21 15:27:28 -07:00
Micah Elizabeth Scott ee6acfa5cd hashx: Rewrite RegisterSet again to reduce CPU frontend stalls
Closer inspection of the CPU counters showed that the branching in
RegisterSet::index() was a big problem, contributing to the overall
CPU frontend stall bottleneck in program generation.

This new version is less general, and closer to the appraoch used by
the original C implementation. We store a sorted ArrayVec of in-set
registers, and most operations construct the RegisterSet only once
using a combined filter predicate.

Choosing a register from a set is now cheaper in branches, instructions,
and L1 cache space. We now very rarely manipulate an entire RegisterSet
in any way other than by selecting a register randomly. (Just for the
register R5 special case.)

Wallclock time benchmarks:
  generate-interp improves, -7.0%
  generate-x86_64 improves, -7.2%

Cachegrind benchmarks:
  generate_interp_1000x, more total instructions run but a large
  decrease in frontend cache misses. +4.6% instructions, +11% L1
  accesses, -99% L2 access, -40% RAM access.

  generate_compiled_100x, +4.0% instructions, +9.4% L1 access.
  cache miss improvements: -57% L2 access, -25% RAM access.

Signed-off-by: Micah Elizabeth Scott <beth@torproject.org>
2023-08-21 15:27:28 -07:00
Micah Elizabeth Scott e142fd9882 hashx: new RegisterWriter format handles more cases transparently
There was a special case in writer_pair_allowed for making add and
subtract equivalent. This patch changes RegisterWriter's encoding, using
per-opcode variants instead of per-format variants. The Add/Sub merge
can now happen earlier, when RegisterWriter is constructed.

Before and after RegisterWriter sizes are the same, at 8 bytes.
This patch removes many uses of Option<RegisterWriter> in favor
of using a new RegisterWriter::None default, and passes by value
rather than by reference.

Wallclock time benchmarks:
  generate-interp improves, -7.5%
  generate-x86_64 improves, -5.3%

Cachegrind benchmarks:
  generate_interp_1000x, negligible change in total instructions,
  improvement in cache footprint: -22.8% L2 accesses

Signed-off-by: Micah Elizabeth Scott <beth@torproject.org>
2023-08-21 15:27:28 -07:00
Nick Mathewson e699607ef8 Upgrade num_enum dependency to 0.7 2023-08-21 13:47:08 -04:00
Ian Jackson c9d8e58f65 Merge branch 'hashx_iai' into 'main'
hashx/bench: Add cachegrind microbenchmarks

See merge request tpo/core/arti!1513
2023-08-21 13:59:12 +00:00
trinity-1686a 5527da5711 upgrade serde to >=1.0.184
to remove the use of a pre-built blob
2023-08-21 15:54:23 +02:00
Nick Mathewson e4373e88e2 proto: new ClientCirc::send_raw_msg function.
Closes #1010.
2023-08-21 09:23:46 -04:00
Micah Elizabeth Scott 780e10e1d5 hashx/bench: Add cachegrind microbenchmarks
This uses the 'iai' crate and valgrind to measure fine grained cache
behavior during program generation and hash computation.

Signed-off-by: Micah Elizabeth Scott <beth@torproject.org>
2023-08-18 20:09:40 -07:00
gabi-250 15f9da4d0e Merge branch 'hss-err' into 'main'
tor-hsservice errors: Introduce more error types

See merge request tpo/core/arti!1515
2023-08-18 13:39:20 +00:00
gabi-250 fbd3a4f597 Merge branch 'ipt-e-drop' into 'main'
Define drop behaviour of IPT establisher, wrt status watch

See merge request tpo/core/arti!1516
2023-08-18 13:15:19 +00:00
gabi-250 b450461dae Merge branch 'hss_circparams' into 'main'
hsservice: Use correct circparameters_from_netparameters fn

See merge request tpo/core/arti!1520
2023-08-18 12:58:30 +00:00
Nick Mathewson af4fcf83ad hsservice: Use correct circparameters_from_netparameters fn
I had incorrectly thought that this function was private.
2023-08-18 08:14:51 -04:00
Nick Mathewson b831bcca9f Merge branch 'allow_stream_req-hop-num' into 'main'
tor-proto: Make ClientCirc::allow_stream_requests take a HopNum.

Closes #1009

See merge request tpo/core/arti!1519
2023-08-18 12:06:29 +00:00
Nick Mathewson 42cd6c2983 Merge branch 'ipt-relay' into 'main'
tor-hsservice: Change IptEstablisher::new to take RelayIds

See merge request tpo/core/arti!1517
2023-08-18 12:01:38 +00:00
Gabriela Moldovan a1913e15f9
tor-proto: Add test where an incoming stream comes from an unexpect hop.
This adds a test that checks if the reactor is shut down if it receives
an incoming stream request from an unexpected hop.

Part of #1009
2023-08-18 12:14:16 +01:00
Gabriela Moldovan 67a77680dd
tor-proto: Make ClientCirc::allow_stream_requests take a HopNum.
For consistency with the other `ClientCirc` APIs,
`ClientCirc::allow_stream_requests` now takes a `HopNum` argument. Upon
receiving an incoming stream request, the reactor now checks if the
request came from the hop specified in `allow_stream_requests` (and if
it came from a different hop, the circuit is closed).

Part of #1009
2023-08-18 12:14:01 +01:00
Ian Jackson 61f58daadd tor-hsservice: Change IptEstablisher::new to take RelayIds
The IptEstablisher needs to continuously maintain the IPT even as the
netdir is updated.  Whereas, the IPT manager just wants to select the
relay from the netdir once and then only think about the relay
identity.

So it makes sense for the establisher to do necessary lookups of the
relay's ids in the netdir.
2023-08-17 15:54:54 +01:00
Ian Jackson 64c8b38c25 tor-hsservice: Define drop behaviour of IPT establisher status 2023-08-17 14:42:26 +01:00
Ian Jackson f4dfc14694 tor-async-utils: Deprecate DropNotifyEofSignallable::is_eof
This was introduced in c82cda85d6
  tor-basic-utils: DropNotifyWatchSender: use DropNotifyEofSignallable

and already, then, the is_eof() method is redundant.
2023-08-17 14:41:56 +01:00
Nick Mathewson f63852f449 Merge branch 'rendezvous_handshake' into 'main'
hsservice: new rend_handshake module

See merge request tpo/core/arti!1512
2023-08-17 13:29:18 +00:00
Ian Jackson 360c90a9d8 tor-hsservice errors: Distinguish operational errors by context
At the very least, I need FatalError to be distinct:
IptEstablisher::new ought not to fail unless everything is terrible.

Add a the Spawn variant to FatalError (that we'll need soon) and the
Bug variant (which it seems likely we might need).

This also gets rid of the crate-level Result alias.
2023-08-17 13:28:44 +01:00
Ian Jackson e1405cdb11 tor-hsservice errors: Return ReconfigureError from reconfigure
This is what we do elsewhere.
2023-08-17 13:28:44 +01:00
Ian Jackson 5e047da9cc tor-hsservice errors: Use Bug for methods that oughtn't to fail
If the service encouters operational errors, surfacing them here is
not helpful.  So these methods ought to work, if they weren't called
erroneously.
2023-08-17 13:19:21 +01:00
Nick Mathewson 2d2bf1c945 hsservice: new rend_handshake module
This code has most of what we need to go from an INTRODUCE2 message
we've just received to the point where we've connected to the
rendezvous point and we're waiting for a stream of BEGIN messages.
Unfinished pieces are marked with TODO HSS.

Most of #980.
2023-08-17 08:18:21 -04:00
gabi-250 7d3741e8dd Merge branch 'always_bug-from' into 'main'
tor-bytes: deprecate always_bug with From trait

See merge request tpo/core/arti!1500
2023-08-17 12:17:32 +00:00
Ian Jackson e3b9371ddf tor-hsservice errors: Make stop() infallible
The semantics of an Err return from this are unclear.  Was it stopped?
And what kind of error might we even return?
2023-08-17 13:16:50 +01:00
Ian Jackson c391ac2a90 tor-hsservice errors: Use `crate::Result` rather than imports
We want to change the error return types of many methods, so we need a
way to name `std::result::Result`.

We could use `StdResult`, but, actually, properly distinguishing the
kinds of errors that can occur in various contexts means we don't
actually want a single Error type for the whole crate, so
`crate::Result` is going to go away.
2023-08-17 13:12:23 +01:00
Emil Engler 73059827f0
tor-bytes: deprecate always_bug with From trait
This commit deprecates the `EncodeError::always_bug` function with a
`From<EncodeError> for Bug` trait, which is a more semantically correct
way to perform this action.
2023-08-17 13:52:29 +02:00
gabi-250 4c2f8d5a9e Merge branch 'handshake-tests' into 'main'
test: add a test for missing NETINFO cells

See merge request tpo/core/arti!1501
2023-08-17 10:17:01 +00:00
Nick Mathewson c8b721aa5d Merge branch 'all_crates_warnings' into 'main'
Resolve all warnings that appear from `maint/every-crate`

See merge request tpo/core/arti!1507
2023-08-16 18:20:21 +00:00
gabi-250 fdb411f287 Merge branch 'netdir-todo' into 'main'
tor-netdir: Only select nodes that haven't been used for lower-numbered replicas.

See merge request tpo/core/arti!1494
2023-08-16 17:52:07 +00:00
Nick Mathewson b0a30da76c arti-client: fix conditional unused_import and dead_code warnings 2023-08-16 13:44:18 -04:00
Nick Mathewson 94eaaa7f61 Merge branch 'ipt-e' into 'main'
tor-hsservice: Have IptEstablisher::new() return the status watch

See merge request tpo/core/arti!1511
2023-08-16 17:41:57 +00:00
Nick Mathewson 9217278ac1 guardmgr: Fix a dead-code warning when built without bridge support. 2023-08-16 13:41:40 -04:00
Nick Mathewson e67152abb9 tor-proto: Fix dead_code when building without experimental-api 2023-08-16 13:41:40 -04:00
Nick Mathewson 6227539446 cell: Add accessors to IntroduceHandshakePayload 2023-08-16 13:23:46 -04:00
Ian Jackson 8e58ab54ba tor-hsservice: Have IptEstablisher::new() return the status watch (fmt) 2023-08-16 18:12:04 +01:00
Ian Jackson c161f41b57 tor-hsservice: Have IptEstablisher::new() return the status watch
The IPT manager is going to want to separate the IptEstablisher
struct (which contains the Drop signal) from the watch receiver.

We could add an accessor to clone the watch, but the copy in the
IptEstablisher would be redundant.

This makes new()'s signature a bit funky but it's an internal method
so I think that's fine.
2023-08-16 18:12:04 +01:00
Ian Jackson 62c50bfe75 Apply some churn from rustfmt (beta) 2023-08-16 18:11:22 +01:00
Gabriela Moldovan 7a030f8661
tor-netdir: Replace somewhat niche scan() usage with flat_map(). 2023-08-16 17:57:33 +01:00
Gabriela Moldovan d395fe356e
tor-netdir: Rework the logic for checking if a node has already been selected.
This just simplifies the expression a little bit. It still has the same
behaviour.
2023-08-16 17:57:29 +01:00
Gabriela Moldovan af99a4a7b9
tor-netdir: Store the hsdir indices in a HashSet instead of a BTreeSet. 2023-08-16 17:57:21 +01:00
Gabriela Moldovan 80f059a171
tor-netdir: Derive Hash for HsDirIndex.
This will become useful later when we build a `HashSet` of `HsDirIndex`.
2023-08-16 17:57:08 +01:00
Gabriela Moldovan b26ecd6553
tor-netdir: Explain what the ring_items_at filter is for. 2023-08-16 17:57:02 +01:00
Gabriela Moldovan ecb3783f5e
tor-netdir: Only select nodes that haven't been used for lower-numbered replicas.
This implements the part of the spec that says a node that has already
been selected for a lowered-numbered replica, shouldn't be considered
when choosing `spread` nodes for any other higher-numbered replicas.

Note: previously, the test added in this commit wouldn't have passed
(because `NetDir::hs_dirs` used to return duplicate relays under some
circumstances).

Part of #960
2023-08-16 17:56:55 +01:00
Gabriela Moldovan 315684af84
tor-netdir: Make ring_items_at() filter the items before returning.
This makes `ring_items_at()` take an extra parameter which specifies
whether an item is acceptable or not. The newly added filtering
capabilities will be used to implement the part of the spec that says
that if a node was selected for a replica, then it shouldn't be
considered when choosing `spread` nodes for any other higher-numbered
replicas.
2023-08-16 17:56:52 +01:00
Gabriela Moldovan 5d30d34b13
tor-netdir: Update ring_items_at() docs.
This updates the docs to reference the `spread` parameter (rather than
`spread_fetch`).
2023-08-16 17:56:49 +01:00
Gabriela Moldovan 619718956d
tor-netdir: Remove extraneous whitespace. 2023-08-16 17:56:45 +01:00
Gabriela Moldovan e6635b139d
tor-netdir: Use hsdir_spread_store if we're uploading.
The spread should be either `hsdir_spread_store` or
`hsdir_spread_fetch`, depending on whether we're uploading or
downloading descriptors.
2023-08-16 17:56:42 +01:00