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: Use FxHasher in places where we don't need DDoS resistance #2342

Draft
wants to merge 29 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
eab99c3
feat: Try FxHasher to see if it makes a difference
larseggert Jan 10, 2025
0bda917
Merge branch 'main' into feat-fxhasher
larseggert Jan 10, 2025
6762a02
More FxHasher
larseggert Jan 10, 2025
93c8ce5
Merge branch 'main' into feat-fxhasher
larseggert Jan 14, 2025
2b230a5
Merge branch 'main' into feat-fxhasher
larseggert Jan 24, 2025
0d353a6
Merge branch 'main' into feat-fxhasher
larseggert Feb 4, 2025
5879e28
Suggestion from @martinthomson
larseggert Feb 5, 2025
6b305ea
One more
larseggert Feb 5, 2025
ee86d48
Merge branch 'main' into feat-fxhasher
larseggert Feb 5, 2025
bbea965
Merge branch 'main' into feat-fxhasher
larseggert Feb 5, 2025
bd06169
Suggestions from @mxinden
larseggert Feb 5, 2025
7d6c181
Merge branch 'main' into feat-fxhasher
larseggert Feb 7, 2025
a78c1c6
IndexMap
larseggert Feb 10, 2025
9d9a799
More
larseggert Feb 10, 2025
13cdada
Again
larseggert Feb 10, 2025
d280b66
Again
larseggert Feb 10, 2025
8d0b6fb
Undo
larseggert Feb 10, 2025
b609b9e
Merge branch 'main' into feat-fxhasher
larseggert Feb 10, 2025
6e8d194
`swap_remove` -> `shift_remove`
larseggert Feb 10, 2025
40c8383
`BTreeSet` -> `HashSet`
larseggert Feb 10, 2025
420c6e2
Minimize
larseggert Feb 10, 2025
97773c6
Minimize more
larseggert Feb 10, 2025
f4531aa
Merge branch 'main' into feat-fxhasher
larseggert Feb 11, 2025
fd081e4
Merge branch 'main' into feat-fxhasher
larseggert Feb 12, 2025
9f9e220
Fix
larseggert Feb 12, 2025
548027d
Merge branch 'main' into feat-fxhasher
larseggert Feb 12, 2025
69a6710
Fix
larseggert Feb 12, 2025
bf294ed
Merge branch 'main' into feat-fxhasher
larseggert Feb 13, 2025
bbcb365
Merge branch 'main' into feat-fxhasher
larseggert Feb 18, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ log = { version = "0.4", default-features = false }
qlog = { version = "0.13", default-features = false }
quinn-udp = { version = "0.5.6", default-features = false, features = ["direct-log", "fast-apple-datapath"] }
regex = { version = "1.9", default-features = false, features = ["unicode-perl"] }
rustc-hash = { version = "2.1", default-features = false, features = [ "std" ]}
static_assertions = { version = "1.1", default-features = false }
url = { version = "2.5.3", default-features = false, features = ["std"] }

Expand Down
1 change: 1 addition & 0 deletions neqo-bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ neqo-udp = { path = "./../neqo-udp" }
qlog = { workspace = true }
quinn-udp = { workspace = true }
regex = { workspace = true }
rustc-hash = { workspace = true }
tokio = { version = "1", default-features = false, features = ["net", "time", "macros", "rt", "rt-multi-thread"] }
url = { workspace = true }

Expand Down
7 changes: 4 additions & 3 deletions neqo-bin/src/client/http09.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use std::{
cell::RefCell,
collections::{HashMap, VecDeque},
collections::VecDeque,
fs::File,
io::{BufWriter, Write as _},
net::SocketAddr,
Expand All @@ -25,13 +25,14 @@ use neqo_transport::{
CloseReason, Connection, ConnectionEvent, ConnectionIdGenerator, EmptyConnectionIdGenerator,
Error, Output, RandomConnectionIdGenerator, State, StreamId, StreamType,
};
use rustc_hash::FxHashMap;
use url::Url;

use super::{get_output_file, qlog_new, Args, CloseState, Res};
use crate::STREAM_IO_BUFFER_SIZE;

pub struct Handler<'a> {
streams: HashMap<StreamId, Option<BufWriter<File>>>,
streams: FxHashMap<StreamId, Option<BufWriter<File>>>,
url_queue: VecDeque<Url>,
handled_urls: Vec<Url>,
all_paths: Vec<PathBuf>,
Expand Down Expand Up @@ -226,7 +227,7 @@ impl super::Client for Connection {
impl<'b> Handler<'b> {
pub fn new(url_queue: VecDeque<Url>, args: &'b Args) -> Self {
Self {
streams: HashMap::new(),
streams: FxHashMap::default(),
url_queue,
handled_urls: Vec::new(),
all_paths: Vec::new(),
Expand Down
7 changes: 4 additions & 3 deletions neqo-bin/src/client/http3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use std::{
cell::RefCell,
collections::{HashMap, VecDeque},
collections::VecDeque,
fmt::Display,
fs::File,
io::{BufWriter, Write as _},
Expand All @@ -27,6 +27,7 @@ use neqo_transport::{
AppError, CloseReason, Connection, EmptyConnectionIdGenerator, Error as TransportError, Output,
RandomConnectionIdGenerator, StreamId,
};
use rustc_hash::FxHashMap;
use url::Url;

use super::{get_output_file, qlog_new, Args, CloseState, Res};
Expand All @@ -45,7 +46,7 @@ impl<'a> Handler<'a> {
let url_handler = UrlHandler {
url_queue,
handled_urls: Vec::new(),
stream_handlers: HashMap::new(),
stream_handlers: FxHashMap::default(),
all_paths: Vec::new(),
args,
};
Expand Down Expand Up @@ -367,7 +368,7 @@ impl StreamHandler for UploadStreamHandler {
struct UrlHandler<'a> {
url_queue: VecDeque<Url>,
handled_urls: Vec<Url>,
stream_handlers: HashMap<StreamId, Box<dyn StreamHandler>>,
stream_handlers: FxHashMap<StreamId, Box<dyn StreamHandler>>,
all_paths: Vec<PathBuf>,
args: &'a Args,
}
Expand Down
14 changes: 9 additions & 5 deletions neqo-bin/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#![allow(clippy::unwrap_used)] // This is example code.

use std::{
collections::{HashMap, VecDeque},
collections::VecDeque,
fmt::{self, Display},
fs::{create_dir_all, File, OpenOptions},
io::{self, BufWriter},
Expand All @@ -32,6 +32,7 @@ use neqo_crypto::{
use neqo_http3::Output;
use neqo_transport::{AppError, CloseReason, ConnectionId, Version};
use neqo_udp::RecvBuf;
use rustc_hash::FxHashMap;
use tokio::time::Sleep;
use url::{Host, Origin, Url};

Expand Down Expand Up @@ -529,10 +530,13 @@ const fn local_addr_for(remote_addr: &SocketAddr, local_port: u16) -> SocketAddr

fn urls_by_origin(urls: &[Url]) -> impl Iterator<Item = ((Host, u16), VecDeque<Url>)> {
urls.iter()
.fold(HashMap::<Origin, VecDeque<Url>>::new(), |mut urls, url| {
urls.entry(url.origin()).or_default().push_back(url.clone());
urls
})
.fold(
FxHashMap::<Origin, VecDeque<Url>>::default(),
|mut urls, url| {
urls.entry(url.origin()).or_default().push_back(url.clone());
urls
},
)
.into_iter()
.filter_map(|(origin, urls)| match origin {
Origin::Tuple(_scheme, h, p) => Some(((h, p), urls)),
Expand Down
11 changes: 6 additions & 5 deletions neqo-bin/src/server/http09.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#![allow(clippy::unwrap_used)] // This is example code.

use std::{borrow::Cow, cell::RefCell, collections::HashMap, fmt::Display, rc::Rc, time::Instant};
use std::{borrow::Cow, cell::RefCell, fmt::Display, rc::Rc, time::Instant};

use neqo_common::{event::Provider as _, hex, qdebug, qerror, qinfo, qwarn, Datagram};
use neqo_crypto::{generate_ech_keys, random, AllowZeroRtt, AntiReplay};
Expand All @@ -16,6 +16,7 @@ use neqo_transport::{
ConnectionEvent, ConnectionIdGenerator, Output, State, StreamId,
};
use regex::Regex;
use rustc_hash::FxHashMap;

use super::{qns_read_response, Args};
use crate::{send_data::SendData, STREAM_IO_BUFFER_SIZE};
Expand All @@ -28,8 +29,8 @@ struct HttpStreamState {

pub struct HttpServer {
server: Server,
write_state: HashMap<StreamId, HttpStreamState>,
read_state: HashMap<StreamId, Vec<u8>>,
write_state: FxHashMap<StreamId, HttpStreamState>,
read_state: FxHashMap<StreamId, Vec<u8>>,
is_qns_test: bool,
regex: Regex,
read_buffer: Vec<u8>,
Expand Down Expand Up @@ -68,8 +69,8 @@ impl HttpServer {
let is_qns_test = args.shared.qns_test.is_some();
Ok(Self {
server,
write_state: HashMap::new(),
read_state: HashMap::new(),
write_state: FxHashMap::default(),
read_state: FxHashMap::default(),
is_qns_test,
regex: if is_qns_test {
Regex::new(r"GET +/(\S+)(?:\r)?\n").map_err(|_| Error::Internal)?
Expand Down
10 changes: 5 additions & 5 deletions neqo-bin/src/server/http3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

use std::{
cell::RefCell,
collections::HashMap,
fmt::{self, Display},
rc::Rc,
time::Instant,
Expand All @@ -20,15 +19,16 @@ use neqo_http3::{
Http3OrWebTransportStream, Http3Parameters, Http3Server, Http3ServerEvent, StreamId,
};
use neqo_transport::{server::ValidateAddress, ConnectionIdGenerator};
use rustc_hash::FxHashMap;

use super::{qns_read_response, Args};
use crate::send_data::SendData;

pub struct HttpServer {
server: Http3Server,
/// Progress writing to each stream.
remaining_data: HashMap<StreamId, SendData>,
posts: HashMap<Http3OrWebTransportStream, usize>,
remaining_data: FxHashMap<StreamId, SendData>,
posts: FxHashMap<Http3OrWebTransportStream, usize>,
is_qns_test: bool,
}

Expand Down Expand Up @@ -68,8 +68,8 @@ impl HttpServer {
}
Self {
server,
remaining_data: HashMap::new(),
posts: HashMap::new(),
remaining_data: FxHashMap::default(),
posts: FxHashMap::default(),
is_qns_test: args.shared.qns_test.is_some(),
}
}
Expand Down
1 change: 1 addition & 0 deletions neqo-http3/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ neqo-crypto = { path = "./../neqo-crypto" }
neqo-qpack = { path = "./../neqo-qpack" }
neqo-transport = { path = "./../neqo-transport" }
qlog = { workspace = true }
rustc-hash = { workspace = true }
sfv = { version = "0.9", default-features = false }
url = { workspace = true }

Expand Down
19 changes: 8 additions & 11 deletions neqo-http3/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::{
cell::RefCell,
collections::{BTreeSet, HashMap},
fmt::Debug,
mem,
rc::Rc,
};
#![allow(clippy::module_name_repetitions)]

use std::{cell::RefCell, collections::BTreeSet, fmt::Debug, mem, rc::Rc};

use neqo_common::{qdebug, qerror, qinfo, qtrace, qwarn, Decoder, Header, MessageType, Role};
use neqo_qpack::{decoder::QPackDecoder, encoder::QPackEncoder};
use neqo_transport::{
streams::SendOrder, AppError, CloseReason, Connection, DatagramTracking, State, StreamId,
StreamType, ZeroRttState,
};
use rustc_hash::FxHashMap;

use crate::{
client_events::Http3ClientEvents,
Expand Down Expand Up @@ -304,8 +301,8 @@ pub struct Http3Connection {
pub qpack_decoder: Rc<RefCell<QPackDecoder>>,
settings_state: Http3RemoteSettingsState,
streams_with_pending_data: BTreeSet<StreamId>,
pub send_streams: HashMap<StreamId, Box<dyn SendStream>>,
pub recv_streams: HashMap<StreamId, Box<dyn RecvStream>>,
pub send_streams: FxHashMap<StreamId, Box<dyn SendStream>>,
pub recv_streams: FxHashMap<StreamId, Box<dyn RecvStream>>,
webtransport: ExtendedConnectFeature,
}

Expand Down Expand Up @@ -335,8 +332,8 @@ impl Http3Connection {
local_params: conn_params,
settings_state: Http3RemoteSettingsState::NotReceived,
streams_with_pending_data: BTreeSet::new(),
send_streams: HashMap::new(),
recv_streams: HashMap::new(),
send_streams: FxHashMap::default(),
recv_streams: FxHashMap::default(),
role,
}
}
Expand Down
7 changes: 4 additions & 3 deletions neqo-http3/src/control_stream_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::collections::{HashMap, VecDeque};
use std::collections::VecDeque;

use neqo_common::{qtrace, Encoder};
use neqo_transport::{Connection, StreamId, StreamType};
use rustc_hash::FxHashMap;

use crate::{frames::HFrame, BufferedStream, Error, Http3StreamType, RecvStream, Res};

Expand Down Expand Up @@ -50,7 +51,7 @@ impl ControlStreamLocal {
pub fn send(
&mut self,
conn: &mut Connection,
recv_conn: &mut HashMap<StreamId, Box<dyn RecvStream>>,
recv_conn: &mut FxHashMap<StreamId, Box<dyn RecvStream>>,
) -> Res<()> {
self.stream.send_buffer(conn)?;
self.send_priority_update(conn, recv_conn)
Expand All @@ -59,7 +60,7 @@ impl ControlStreamLocal {
fn send_priority_update(
&mut self,
conn: &mut Connection,
recv_conn: &mut HashMap<StreamId, Box<dyn RecvStream>>,
recv_conn: &mut FxHashMap<StreamId, Box<dyn RecvStream>>,
) -> Res<()> {
// send all necessary priority updates
while let Some(update_id) = self.outstanding_priority_update.pop_front() {
Expand Down
14 changes: 6 additions & 8 deletions neqo-http3/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use std::{
cell::{RefCell, RefMut},
collections::HashMap,
path::PathBuf,
rc::Rc,
time::Instant,
Expand All @@ -18,6 +17,7 @@ use neqo_transport::{
server::{ConnectionRef, Server, ValidateAddress},
ConnectionIdGenerator, Output,
};
use rustc_hash::FxHashMap;

use crate::{
connection::Http3State,
Expand All @@ -38,7 +38,7 @@ const MAX_EVENT_DATA_SIZE: usize = 1024;
pub struct Http3Server {
server: Server,
http3_parameters: Http3Parameters,
http3_handlers: HashMap<ConnectionRef, HandlerRef>,
http3_handlers: FxHashMap<ConnectionRef, HandlerRef>,
events: Http3ServerEvents,
}

Expand Down Expand Up @@ -74,7 +74,7 @@ impl Http3Server {
http3_parameters.get_connection_parameters().clone(),
)?,
http3_parameters,
http3_handlers: HashMap::new(),
http3_handlers: FxHashMap::default(),
events: Http3ServerEvents::default(),
})
}
Expand Down Expand Up @@ -321,17 +321,15 @@ fn prepare_data(

#[cfg(test)]
mod tests {
use std::{
collections::HashMap,
ops::{Deref, DerefMut},
};
use std::ops::{Deref, DerefMut};

use neqo_common::{event::Provider as _, Encoder};
use neqo_crypto::{AuthenticationStatus, ZeroRttCheckResult, ZeroRttChecker};
use neqo_qpack::{encoder::QPackEncoder, QpackSettings};
use neqo_transport::{
CloseReason, Connection, ConnectionEvent, State, StreamId, StreamType, ZeroRttState,
};
use rustc_hash::FxHashMap;
use test_fixture::{
anti_replay, default_client, fixture_init, now, CountingConnectionIdGenerator,
DEFAULT_ALPN, DEFAULT_KEYS,
Expand Down Expand Up @@ -1273,7 +1271,7 @@ mod tests {
let out = peer_conn.process_output(now());
hconn.process(out.dgram(), now());

let mut requests = HashMap::new();
let mut requests = FxHashMap::default();
while let Some(event) = hconn.next_event() {
match event {
Http3ServerEvent::Headers { stream, .. } => {
Expand Down
Loading
Loading