Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: In-place crypto #2385

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open

Conversation

larseggert
Copy link
Collaborator

@larseggert larseggert commented Jan 23, 2025

Fixes #2246 (eventually)

Only in-place encryption so far, and only for the main data path.

Fixes mozilla#2246 (eventually)
Copy link

codecov bot commented Jan 23, 2025

Codecov Report

Attention: Patch coverage is 99.59184% with 1 line in your changes missing coverage. Please review.

Project coverage is 95.33%. Comparing base (1090de3) to head (f2c3558).

Files with missing lines Patch % Lines
neqo-crypto/src/aead.rs 98.03% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2385      +/-   ##
==========================================
+ Coverage   95.31%   95.33%   +0.01%     
==========================================
  Files         114      114              
  Lines       36869    36983     +114     
  Branches    36869    36983     +114     
==========================================
+ Hits        35142    35257     +115     
+ Misses       1721     1720       -1     
  Partials        6        6              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

github-actions bot commented Jan 23, 2025

Failed Interop Tests

QUIC Interop Runner, client vs. server, differences relative to 108fb8d.

neqo-latest as client

neqo-latest as server

All results

Succeeded Interop Tests

QUIC Interop Runner, client vs. server

neqo-latest as client

neqo-latest as server

Unsupported Interop Tests

QUIC Interop Runner, client vs. server

neqo-latest as client

neqo-latest as server

Copy link

github-actions bot commented Jan 23, 2025

Benchmark results

Performance differences relative to 1090de3.

decode 4096 bytes, mask ff: 💚 Performance has improved.
       time:   [11.192 µs 11.236 µs 11.285 µs]
       change: [-6.4112% -5.8161% -5.3446%] (p = 0.00 < 0.05)

Found 14 outliers among 100 measurements (14.00%)
3 (3.00%) low mild
1 (1.00%) high mild
10 (10.00%) high severe

decode 1048576 bytes, mask ff: 💔 Performance has regressed.
       time:   [3.0966 ms 3.1058 ms 3.1167 ms]
       change: [+6.8020% +7.3059% +7.7777%] (p = 0.00 < 0.05)

Found 8 outliers among 100 measurements (8.00%)
8 (8.00%) high severe

decode 4096 bytes, mask 7f: Change within noise threshold.
       time:   [19.552 µs 19.599 µs 19.654 µs]
       change: [-1.6966% -1.2868% -0.9060%] (p = 0.00 < 0.05)

Found 19 outliers among 100 measurements (19.00%)
3 (3.00%) low severe
1 (1.00%) low mild
1 (1.00%) high mild
14 (14.00%) high severe

decode 1048576 bytes, mask 7f: 💔 Performance has regressed.
       time:   [5.1618 ms 5.1729 ms 5.1856 ms]
       change: [+1.2926% +1.6611% +2.0116%] (p = 0.00 < 0.05)

Found 13 outliers among 100 measurements (13.00%)
13 (13.00%) high severe

decode 4096 bytes, mask 3f: 💚 Performance has improved.
       time:   [5.5324 µs 5.6030 µs 5.7263 µs]
       change: [-20.936% -19.851% -18.476%] (p = 0.00 < 0.05)

Found 14 outliers among 100 measurements (14.00%)
6 (6.00%) low mild
1 (1.00%) high mild
7 (7.00%) high severe

decode 1048576 bytes, mask 3f: 💔 Performance has regressed.
       time:   [1.7584 ms 1.7613 ms 1.7655 ms]
       change: [+23.482% +24.084% +24.625%] (p = 0.00 < 0.05)

Found 6 outliers among 100 measurements (6.00%)
2 (2.00%) high mild
4 (4.00%) high severe

coalesce_acked_from_zero 1+1 entries: No change in performance detected.
       time:   [99.163 ns 99.488 ns 99.826 ns]
       change: [+0.0081% +0.5022% +1.0990%] (p = 0.06 > 0.05)

Found 15 outliers among 100 measurements (15.00%)
8 (8.00%) high mild
7 (7.00%) high severe

coalesce_acked_from_zero 3+1 entries: No change in performance detected.
       time:   [116.81 ns 117.21 ns 117.64 ns]
       change: [-0.4451% +0.0312% +0.5722%] (p = 0.90 > 0.05)

Found 21 outliers among 100 measurements (21.00%)
1 (1.00%) low severe
2 (2.00%) low mild
1 (1.00%) high mild
17 (17.00%) high severe

coalesce_acked_from_zero 10+1 entries: No change in performance detected.
       time:   [116.43 ns 116.82 ns 117.33 ns]
       change: [-0.1348% +0.2142% +0.5851%] (p = 0.24 > 0.05)

Found 10 outliers among 100 measurements (10.00%)
4 (4.00%) low mild
1 (1.00%) high mild
5 (5.00%) high severe

coalesce_acked_from_zero 1000+1 entries: No change in performance detected.
       time:   [97.244 ns 97.384 ns 97.537 ns]
       change: [-0.8469% -0.0665% +0.6640%] (p = 0.87 > 0.05)

Found 11 outliers among 100 measurements (11.00%)
7 (7.00%) high mild
4 (4.00%) high severe

RxStreamOrderer::inbound_frame(): Change within noise threshold.
       time:   [111.39 ms 111.44 ms 111.50 ms]
       change: [+0.0987% +0.1647% +0.2283%] (p = 0.00 < 0.05)

Found 20 outliers among 100 measurements (20.00%)
11 (11.00%) low mild
9 (9.00%) high mild

SentPackets::take_ranges: No change in performance detected.
       time:   [5.4731 µs 5.6223 µs 5.7810 µs]
       change: [-2.0929% +0.4847% +3.1445%] (p = 0.72 > 0.05)

Found 5 outliers among 100 measurements (5.00%)
4 (4.00%) high mild
1 (1.00%) high severe

transfer/pacing-false/varying-seeds: 💚 Performance has improved.
       time:   [38.890 ms 38.978 ms 39.074 ms]
       change: [-5.6621% -5.3655% -5.0577%] (p = 0.00 < 0.05)

Found 4 outliers among 100 measurements (4.00%)
1 (1.00%) low mild
2 (2.00%) high mild
1 (1.00%) high severe

transfer/pacing-true/varying-seeds: 💚 Performance has improved.
       time:   [38.850 ms 38.932 ms 39.015 ms]
       change: [-6.6408% -6.3655% -6.0914%] (p = 0.00 < 0.05)

Found 1 outliers among 100 measurements (1.00%)
1 (1.00%) high mild

transfer/pacing-false/same-seed: 💚 Performance has improved.
       time:   [38.804 ms 38.879 ms 38.960 ms]
       change: [-6.5180% -6.2381% -5.9577%] (p = 0.00 < 0.05)

Found 1 outliers among 100 measurements (1.00%)
1 (1.00%) high severe

transfer/pacing-true/same-seed: 💚 Performance has improved.
       time:   [39.185 ms 39.261 ms 39.347 ms]
       change: [-6.5829% -6.3280% -6.0593%] (p = 0.00 < 0.05)

Found 1 outliers among 100 measurements (1.00%)
1 (1.00%) high severe

1-conn/1-100mb-resp/mtu-1504 (aka. Download)/client: 💚 Performance has improved.
       time:   [841.97 ms 852.80 ms 863.79 ms]
       thrpt:  [115.77 MiB/s 117.26 MiB/s 118.77 MiB/s]
change:
       time:   [-6.4469% -4.8621% -3.2645%] (p = 0.00 < 0.05)
       thrpt:  [+3.3747% +5.1106% +6.8912%]
1-conn/10_000-parallel-1b-resp/mtu-1504 (aka. RPS)/client: No change in performance detected.
       time:   [316.49 ms 318.85 ms 321.25 ms]
       thrpt:  [31.128 Kelem/s 31.363 Kelem/s 31.597 Kelem/s]
change:
       time:   [-1.5344% -0.5007% +0.5358%] (p = 0.35 > 0.05)
       thrpt:  [-0.5330% +0.5032% +1.5583%]

Found 1 outliers among 100 measurements (1.00%)
1 (1.00%) high mild

1-conn/1-1b-resp/mtu-1504 (aka. HPS)/client: No change in performance detected.
       time:   [34.243 ms 34.425 ms 34.620 ms]
       thrpt:  [28.885  elem/s 29.048  elem/s 29.203  elem/s]
change:
       time:   [-0.5670% +0.2295% +0.9740%] (p = 0.57 > 0.05)
       thrpt:  [-0.9646% -0.2290% +0.5702%]

Found 13 outliers among 100 measurements (13.00%)
6 (6.00%) low mild
7 (7.00%) high severe

1-conn/1-100mb-resp/mtu-1504 (aka. Upload)/client: Change within noise threshold.
       time:   [1.5788 s 1.5948 s 1.6111 s]
       thrpt:  [62.070 MiB/s 62.703 MiB/s 63.341 MiB/s]
change:
       time:   [-3.2723% -1.8347% -0.2694%] (p = 0.02 < 0.05)
       thrpt:  [+0.2701% +1.8690% +3.3830%]

Client/server transfer results

Transfer of 33554432 bytes over loopback.

Client Server CC Pacing MTU Mean [ms] Min [ms] Max [ms]
gquiche gquiche 1504 555.4 ± 68.3 507.4 684.5
neqo gquiche reno on 1504 739.0 ± 72.4 689.9 938.3
neqo gquiche reno 1504 773.5 ± 42.6 742.6 885.8
neqo gquiche cubic on 1504 806.1 ± 96.8 702.7 954.7
neqo gquiche cubic 1504 767.0 ± 87.3 703.3 986.6
msquic msquic 1504 168.1 ± 84.7 102.2 374.5
neqo msquic reno on 1504 240.7 ± 65.8 203.6 452.6
neqo msquic reno 1504 241.4 ± 62.3 205.7 386.1
neqo msquic cubic on 1504 266.4 ± 75.9 207.9 435.4
neqo msquic cubic 1504 269.2 ± 87.7 207.1 461.0
gquiche neqo reno on 1504 680.5 ± 91.9 590.5 865.1
gquiche neqo reno 1504 672.7 ± 94.9 544.8 810.0
gquiche neqo cubic on 1504 657.1 ± 85.2 514.5 785.5
gquiche neqo cubic 1504 706.4 ± 124.3 557.7 970.7
msquic neqo reno on 1504 508.8 ± 135.3 429.9 865.4
msquic neqo reno 1504 446.5 ± 10.2 434.6 464.5
msquic neqo cubic on 1504 482.3 ± 64.7 438.5 604.2
msquic neqo cubic 1504 472.7 ± 44.6 434.2 576.4
neqo neqo reno on 1504 469.6 ± 63.7 422.0 610.8
neqo neqo reno 1504 460.5 ± 56.5 410.0 596.8
neqo neqo cubic on 1504 481.8 ± 57.9 426.7 598.1
neqo neqo cubic 1504 441.1 ± 9.8 428.4 459.8

⬇️ Download logs

@larseggert
Copy link
Collaborator Author

@mxinden when you have a moment, would you take a look at the borrow-checker issue in PublicPacket::decode? It's the last one I couldn't figure out how to address.

@mxinden
Copy link
Collaborator

mxinden commented Jan 24, 2025

Took a quick look.

            let dcid = Self::opt(dcid_decoder.decode_cid(&mut decoder))?;
            if decoder.remaining() < SAMPLE_OFFSET + SAMPLE_SIZE {
                return Err(Error::InvalidPacket);
            }
            let header_len = decoder.offset();
            return Ok((
                Self {
                    packet_type: PacketType::Short,
                    dcid,
                    scid: None,
                    token: &[],
                    header_len,
                    version: None,
                    data,
                },
                &[],
                &mut [],
            ));
  • decoder has an immutable reference to data.
  • Through decoder dcid has an immutable reference to data.
  • The function returns both dcid (immutable reference to data) AND data. In other words, it returns both an immutable and a mutable reference to data, which is disallowed.

I can take a deeper look and try to fix it.

@larseggert
Copy link
Collaborator Author

Thanks for the analysis! Wonder if we can make dcid a Range into data...

@mxinden
Copy link
Collaborator

mxinden commented Jan 24, 2025

Ah, never seen this before. That would be error prone as the bytes within the range in data could change at any point in time, right? I will give this more thought.

@mxinden
Copy link
Collaborator

mxinden commented Jan 25, 2025

The above described issue, namely that of dcid and data being a conflicting (im-) mutable borrow, can be fixed by "allocating" dcid on the stack via an owned ConnectionId backed by a SmallVec:

diff --git a/neqo-transport/src/packet/mod.rs b/neqo-transport/src/packet/mod.rs
index 73b47bcc..779ca72b 100644
--- a/neqo-transport/src/packet/mod.rs
+++ b/neqo-transport/src/packet/mod.rs
@@ -563,7 +563,7 @@ pub struct PublicPacket<'a> {
     /// The packet type.
     packet_type: PacketType,
     /// The recovered destination connection ID.
-    dcid: ConnectionIdRef<'a>,
+    dcid: ConnectionId,
     /// The source connection ID, if this is a long header packet.
     scid: Option<ConnectionIdRef<'a>>,
     /// Any token that is included in the packet (Retry always has a token; Initial sometimes

That leaves us with another issue, namely rustc not being able to infer that early returns of data don't interfere with the final return of remainder. I have an idea which I will explore tomorrow.

@mxinden
Copy link
Collaborator

mxinden commented Jan 26, 2025

Okay, I got it.

Let's take a look at PacketBuilder on main:

/// `PublicPacket` holds information from packets that is public only.  This allows for
/// processing of packets prior to decryption.
pub struct PublicPacket<'a> {
    /// The packet type.
    packet_type: PacketType,
    /// The recovered destination connection ID.
    dcid: ConnectionIdRef<'a>,
    /// The source connection ID, if this is a long header packet.
    scid: Option<ConnectionIdRef<'a>>,
    /// Any token that is included in the packet (Retry always has a token; Initial sometimes
    /// does). This is empty when there is no token.
    token: &'a [u8],
    /// The size of the header, not including the packet number.
    header_len: usize,
    /// Protocol version, if present in header.
    version: Option<WireVersion>,
    /// A reference to the entire packet, including the header.
    data: &'a [u8],
}

dcid, scid, token and data are all immutable references into the same underlying memory allocation, here our long lived receive buffer.

This pull request introduces the following change:

@@ -564,7 +574,7 @@ pub struct PublicPacket<'a> {
     /// Protocol version, if present in header.
     version: Option<WireVersion>,
     /// A reference to the entire packet, including the header.
-    data: &'a [u8],
+    data: &'a mut [u8],
 }

While dcid, scid and token are untouched, data is now a mutable reference. Having both immutable and mutable references to the same memory allocation is illegal, thus the compiler error.

An easy fix would be to make dcid, scid and token owned types. Given their small footprint, this is likely fine. There might be some additional optimizations, but I doubt they are worth it.

diff --git a/neqo-transport/src/packet/mod.rs b/neqo-transport/src/packet/mod.rs
index 73b47bcc..dc85bbd0 100644
--- a/neqo-transport/src/packet/mod.rs
+++ b/neqo-transport/src/packet/mod.rs
@@ -563,12 +563,12 @@ pub struct PublicPacket<'a> {
     /// The packet type.
     packet_type: PacketType,
     /// The recovered destination connection ID.
-    dcid: ConnectionIdRef<'a>,
+    dcid: ConnectionId,
     /// The source connection ID, if this is a long header packet.
-    scid: Option<ConnectionIdRef<'a>>,
+    scid: Option<ConnectionId>,
     /// Any token that is included in the packet (Retry always has a token; Initial sometimes
     /// does). This is empty when there is no token.
-    token: &'a [u8],
+    token: Vec<u8>,
     /// The size of the header, not including the packet number.
     header_len: usize,
     /// Protocol version, if present in header.

The above, plus a couple of smaller lifetime changes resolve the borrow checker issues.

I will propose a commit with my local changes.

@mxinden
Copy link
Collaborator

mxinden commented Jan 26, 2025

@larseggert let me know what you think of larseggert#34.

Note that it only addresses the borrow-checker issues in neqo-transport/src/packet/mod.rs.

Happy to look at the neqo-http3 failures as well.

neqo-crypto/src/aead.rs Outdated Show resolved Hide resolved
neqo-crypto/src/aead.rs Show resolved Hide resolved
neqo-crypto/src/aead.rs Outdated Show resolved Hide resolved
neqo-crypto/src/aead.rs Outdated Show resolved Hide resolved
neqo-crypto/src/aead.rs Outdated Show resolved Hide resolved
neqo-transport/src/packet/mod.rs Outdated Show resolved Hide resolved
neqo-transport/src/packet/mod.rs Show resolved Hide resolved
@martinthomson
Copy link
Member

FWIW, I looked at the aead crate interface here. I don't think that is going to fit our needs very well.

@larseggert larseggert marked this pull request as ready for review January 29, 2025 17:52
@larseggert
Copy link
Collaborator Author

FWIW, I looked at the aead crate interface here. I don't think that is going to fit our needs very well.

I actually think they might? Our API after the last round of changes is very similar. (Or I'm missing something.)

@martinthomson
Copy link
Member

The challenge I see is twofold:

  1. Their encryption API returns the tag as a return parameter, where we'd probably just prefer that it be stuck at the end (I really don't like that APIs treat tags as separate, I just want a seal()/open() API), meaning that would move costs to the caller
  2. Their decryption API relies on being passed one of their Buffer traits (using dyn no less!). That trait is not implemented by slices because it includes the option to resize, which the encryption API uses to ensure that the modified buffer is 16 byte shorter.

The latter is worse for us because we're passing in a mutable slice, which can't be resized in that way (we might do something with &mut &mut [u8], but ugh, gross).

Copy link
Member

@martinthomson martinthomson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a few gotchas here, but I like how this is shaping up.

The real question I have is: does this really make it go faster? The benchmarks show some improvements. Are those consistent enough for you to be happy? I see that the improvements aren't 100% consistent.

neqo-crypto/src/aead.rs Outdated Show resolved Hide resolved
neqo-crypto/src/aead.rs Show resolved Hide resolved
neqo-crypto/src/aead.rs Show resolved Hide resolved
neqo-crypto/src/aead.rs Outdated Show resolved Hide resolved
neqo-crypto/src/aead_null.rs Outdated Show resolved Hide resolved
let mut decoder = Decoder::new(data);
let first = Self::opt(decoder.decode_uint::<u8>())?;
let first = PublicPacket::opt(decoder.decode_uint::<u8>())?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I to guess that this needs to be PublicPacket and not Self because the lifetimes are different? Subtle shit like that is where rust loses points in my book.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was able to revert all but one of those changes. Reverting the one that is left makes the borrow checker freak out.

larseggert and others added 6 commits January 31, 2025 11:48
Co-authored-by: Martin Thomson <[email protected]>
Signed-off-by: Lars Eggert <[email protected]>
Co-authored-by: Martin Thomson <[email protected]>
Signed-off-by: Lars Eggert <[email protected]>
Co-authored-by: Martin Thomson <[email protected]>
Signed-off-by: Lars Eggert <[email protected]>
Co-authored-by: Martin Thomson <[email protected]>
Signed-off-by: Lars Eggert <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

perf: consider in-place en- and decryption
3 participants