diff --git a/quest/m1/track-priority-scope.md b/quest/m1/track-priority-scope.md index ea657358e09..9fb29bb5ccc 100644 --- a/quest/m1/track-priority-scope.md +++ b/quest/m1/track-priority-scope.md @@ -79,6 +79,11 @@ Direction to settle in the draft first, then the code: presents send order as unrelated. - A per-session cap on distinct ranks is a scheduling detail; whatever replaces the 255-entry sort must stay O(log n) per group under chat-shaped churn. +- moq-quic already has the fair tier: send domains sharing bytes by deficit + round robin, with groups inside them. They are crate-private (exercised only + by its tests) until this quest gives them a consumer. Exposing them also + gives moq-net's `poll::Session::send_group(priority)` a domain, so land that + in the same trait break and implementers break once. Signed priority, decided with the maintainer: diff --git a/quest/m2/drop-web-transport-trait.md b/quest/m2/drop-web-transport-trait.md index 3c05dd0b474..2ecd247b489 100644 --- a/quest/m2/drop-web-transport-trait.md +++ b/quest/m2/drop-web-transport-trait.md @@ -29,6 +29,25 @@ layer instead of reopening it. - Report the public API impact per crate: deleted generic `Session` adapters, and new impls on concrete session types. +The two native QUIC stacks drifted apart behind the adapters. Align them while +each gets its own impl: + +- moq-tokio's `quic::web::Session` derefs to `quic::driver::Connection`, so it + has two `send_datagram`s with different framing: its own prefixes the + WebTransport session id, the driver's reached through `Deref` does not. + Prefer dropping the `Deref` over documenting the trap. +- The driver stream setters disagree: moq-tokio's `set_priority`/`set_group` + take `&self` and return `Result<(), ClosedStream>`, moq-uring's take + `&mut self` and return nothing. +- The sealed `Grouped::send_group` takes `&self` in moq-tokio and `&mut self` + in moq-uring, and only moq-tokio requires `GroupedSend::Group: Send + Sync`. +- moq-uring's driver opens streams only through `web_transport_trait`, so + `SendOptions` reach a new stream through its setters in both runtimes' + adapters. A native impl can pass them to the open, as moq-tokio's driver + `open_uni`/`open_bi` already accept. +- moq-tokio's `quic::driver` and `quic::web` modules are public but + `#[doc(hidden)]`. Decide whether they are API or should be private. + Notes from related quests: - [qmux on moq-quic](/quest/m2/quic-qmux.md) planned to port qmux's `web-transport-trait` impl. If it lands first, its impl targets moq-net's diff --git a/rs/moq-cli/MIGRATE.md b/rs/moq-cli/MIGRATE.md index 06ae45737dd..ee35d00ce6b 100644 --- a/rs/moq-cli/MIGRATE.md +++ b/rs/moq-cli/MIGRATE.md @@ -4,6 +4,11 @@ How to update your invocation and config for each breaking change, newest first. ## Unreleased +### A fixed `--connect-bind` refuses a WAN cluster (#5674) + +`--connect` and `--cluster-connect` or `--cluster-connect-api` used to share one client; now each binds its own, so a fixed `--connect-bind` port with both stops startup. +Use port 0, or drop `--connect`. + ### `moq export ts --delay` defaults to 1 s and includes the send-ahead (#5553) The output trails a TS source by the delay instead of twice it. diff --git a/rs/moq-ffi/src/transport/adapter.rs b/rs/moq-ffi/src/transport/adapter.rs index 46a2c8ff250..7b6a08e2e10 100644 --- a/rs/moq-ffi/src/transport/adapter.rs +++ b/rs/moq-ffi/src/transport/adapter.rs @@ -67,22 +67,30 @@ impl + Clone + 'st .map_err(Error) .map(|res| res.map(|(send, recv)| (SendStream(send), RecvStream(recv)))) } - fn poll_open_uni(&mut self, cx: &mut Context<'_>) -> Poll> { + fn poll_open_uni( + &mut self, + cx: &mut Context<'_>, + options: poll::SendOptions<'_, poll::Ungrouped>, + ) -> Poll> { backend::Session::poll_open_uni(&mut self.0, cx) .map_err(Error) - .map(|res| res.map(SendStream)) + .map(|res| res.map(|send| SendStream(send).scheduled(options))) } - fn poll_open_bi(&mut self, cx: &mut Context<'_>) -> Poll, Self::Error>> { + fn poll_open_bi( + &mut self, + cx: &mut Context<'_>, + options: poll::SendOptions<'_, poll::Ungrouped>, + ) -> Poll, Self::Error>> { backend::Session::poll_open_bi(&mut self.0, cx) .map_err(Error) - .map(|res| res.map(|(send, recv)| (SendStream(send), RecvStream(recv)))) + .map(|res| res.map(|(send, recv)| (SendStream(send).scheduled(options), RecvStream(recv)))) } fn send_group(&mut self, _: i64) -> Option { None } - /// The backend cannot rank a datagram against a stream, so the group and order are ignored, - /// and a datagram it has no room for is dropped. - fn send_datagram(&mut self, payload: Bytes, _: Option<&poll::Ungrouped>, _: i64) -> Result<(), Self::Error> { + /// The backend cannot rank a datagram against a stream, so `options` are ignored, and a + /// datagram it has no room for is dropped. + fn send_datagram(&mut self, payload: Bytes, _: poll::SendOptions<'_, poll::Ungrouped>) -> Result<(), Self::Error> { let mut cx = Context::from_waker(std::task::Waker::noop()); match backend::Session::poll_send_datagram(&mut self.0, &mut cx, &payload) { Poll::Ready(res) => res.map_err(Error), @@ -113,6 +121,13 @@ impl + Clone + 'st } /// An outgoing WebTransport stream adapted to moq-net. pub struct SendStream(S); +impl SendStream { + /// Schedule a newly opened stream, which has no group to join. + fn scheduled(mut self, options: poll::SendOptions<'_, poll::Ungrouped>) -> Self { + poll::SendStream::set_priority(&mut self, options.priority); + self + } +} impl poll::SendStream for SendStream { type Error = Error; type Group = poll::Ungrouped; @@ -293,7 +308,9 @@ mod tests { poll::Session::stats(&session).rtt(), Some(std::time::Duration::from_millis(7)) ); - let Poll::Ready(Ok((mut send, mut recv))) = poll::Session::poll_open_bi(&mut session, &mut cx) else { + let Poll::Ready(Ok((mut send, mut recv))) = + poll::Session::poll_open_bi(&mut session, &mut cx, Default::default()) + else { panic!("open failed") }; let mut buf = Bytes::from_static(b"abcd"); diff --git a/rs/moq-net/MIGRATE.md b/rs/moq-net/MIGRATE.md index 17a15cdb876..4cafc4bb63e 100644 --- a/rs/moq-net/MIGRATE.md +++ b/rs/moq-net/MIGRATE.md @@ -42,17 +42,24 @@ producer.finish().wait().await; Rename `auth::Token` to `auth::Auth`. `auth::Handle::add` returns an `auth::Auth`, which is `Clone` and compares by identity, so a route can name it with `origin::Route::with_auth`. -### Custom transports send datagrams without polling (#5770) +### Custom transports open and send with `SendOptions` (#5770, #5872) `transport::poll::Session::send_datagram` replaces `poll_send_datagram`. -It takes the payload as `Bytes`, the send group to schedule it in (`None` for ungrouped), and an order, and never blocks. +It takes the payload as `Bytes` and a `SendOptions { group, priority }`, and never blocks. +`poll_open_uni` and `poll_open_bi` take the `SendOptions` the new stream starts with, as do the `open_uni` and `open_bi` helpers. +`SendOptions::default()` is ungrouped at priority 0. ```rust +fn poll_open_uni( + &mut self, + cx: &mut Context<'_>, + options: SendOptions<'_, ::Group>, +) -> Poll> { ... } + fn send_datagram( &mut self, payload: Bytes, - group: Option<&::Group>, - order: i64, + options: SendOptions<'_, ::Group>, ) -> Result<(), Self::Error> { ... } ``` diff --git a/rs/moq-net/src/client.rs b/rs/moq-net/src/client.rs index 5d51a3f8f4b..2596354e2c5 100644 --- a/rs/moq-net/src/client.rs +++ b/rs/moq-net/src/client.rs @@ -619,11 +619,16 @@ mod tests { fn poll_open_bi( &mut self, _cx: &mut Context<'_>, + _: crate::transport::poll::SendOptions<'_, crate::transport::poll::Ungrouped>, ) -> Poll> { Poll::Ready(self.state.control_stream.lock().unwrap().take().ok_or(FakeError)) } - fn poll_open_uni(&mut self, _cx: &mut Context<'_>) -> Poll> { + fn poll_open_uni( + &mut self, + _cx: &mut Context<'_>, + _: crate::transport::poll::SendOptions<'_, crate::transport::poll::Ungrouped>, + ) -> Poll> { Poll::Pending } @@ -634,8 +639,7 @@ mod tests { fn send_datagram( &mut self, _: Bytes, - _: Option<&crate::transport::poll::Ungrouped>, - _: i64, + _: crate::transport::poll::SendOptions<'_, crate::transport::poll::Ungrouped>, ) -> Result<(), Self::Error> { Ok(()) } @@ -1005,8 +1009,9 @@ mod tests { fn poll_open_bi( &mut self, cx: &mut Context<'_>, + options: crate::transport::poll::SendOptions<'_, crate::transport::poll::Ungrouped>, ) -> Poll> { - self.inner.poll_open_bi(cx).map_ok(|(send, recv)| { + self.inner.poll_open_bi(cx, options).map_ok(|(send, recv)| { ( LocalSend { inner: send, @@ -1020,8 +1025,12 @@ mod tests { }) } - fn poll_open_uni(&mut self, cx: &mut Context<'_>) -> Poll> { - self.inner.poll_open_uni(cx).map_ok(|stream| LocalSend { + fn poll_open_uni( + &mut self, + cx: &mut Context<'_>, + options: crate::transport::poll::SendOptions<'_, crate::transport::poll::Ungrouped>, + ) -> Poll> { + self.inner.poll_open_uni(cx, options).map_ok(|stream| LocalSend { inner: stream, _local: self._local.clone(), }) @@ -1034,10 +1043,9 @@ mod tests { fn send_datagram( &mut self, payload: Bytes, - group: Option<&crate::transport::poll::Ungrouped>, - order: i64, + options: crate::transport::poll::SendOptions<'_, crate::transport::poll::Ungrouped>, ) -> Result<(), Self::Error> { - self.inner.send_datagram(payload, group, order) + self.inner.send_datagram(payload, options) } fn poll_recv_datagram(&mut self, cx: &mut Context<'_>) -> Poll> { diff --git a/rs/moq-net/src/coding/stream.rs b/rs/moq-net/src/coding/stream.rs index 36aa621ca31..2eec91c3bb9 100644 --- a/rs/moq-net/src/coding/stream.rs +++ b/rs/moq-net/src/coding/stream.rs @@ -31,7 +31,8 @@ impl Stream { where V: Clone, { - let (send, recv) = ready!(session.poll_open_bi(cx)).map_err(|err| version.transport_error(err))?; + let (send, recv) = + ready!(session.poll_open_bi(cx, Default::default())).map_err(|err| version.transport_error(err))?; Poll::Ready(Ok(Self::build(send, recv, version))) } @@ -40,7 +41,10 @@ impl Stream { where V: Clone, { - let (send, recv) = session.open_bi().await.map_err(|err| version.transport_error(err))?; + let (send, recv) = session + .open_bi(Default::default()) + .await + .map_err(|err| version.transport_error(err))?; Ok(Self::build(send, recv, version)) } diff --git a/rs/moq-net/src/coding/writer.rs b/rs/moq-net/src/coding/writer.rs index 494bd8588ec..31a7f5d9304 100644 --- a/rs/moq-net/src/coding/writer.rs +++ b/rs/moq-net/src/coding/writer.rs @@ -219,11 +219,6 @@ impl Writer { self.stream.as_mut().unwrap().set_priority(priority); } - /// Move the stream into a send group from its session. - pub fn set_group(&mut self, group: &S::Group) { - self.stream.as_mut().unwrap().set_group(group); - } - /// Cast the writer to a different version, used during version negotiation. pub fn with_version(mut self, version: O) -> Writer { Writer { diff --git a/rs/moq-net/src/ietf/adapter.rs b/rs/moq-net/src/ietf/adapter.rs index 960668d14c2..2bf51eac65b 100644 --- a/rs/moq-net/src/ietf/adapter.rs +++ b/rs/moq-net/src/ietf/adapter.rs @@ -753,7 +753,11 @@ impl ControlStreamAdapter { /// Open a real (non-virtual) bidi stream, bypassing control stream multiplexing. /// Used for v16 SubscribeNamespace which moved to its own bidi stream. pub async fn open_native_bi(&mut self) -> Result<(AdapterSend, AdapterRecv), crate::Error> { - let (send, recv) = self.inner.open_bi().await.map_err(|_| crate::Error::Closed)?; + let (send, recv) = self + .inner + .open_bi(Default::default()) + .await + .map_err(|_| crate::Error::Closed)?; Ok((AdapterSend::Real(send), AdapterRecv::Real(recv))) } @@ -1193,14 +1197,23 @@ impl crate::transport::poll::Session for Con fn poll_open_bi( &mut self, _cx: &mut Context<'_>, + // Rides the control stream, which no group schedules. + _: crate::transport::poll::SendOptions<'_, ::Group>, ) -> Poll> { let (send, recv) = self.shared.open_outgoing(self.version); Poll::Ready(Ok((AdapterSend::Virtual(send), AdapterRecv::Virtual(recv)))) } - fn poll_open_uni(&mut self, cx: &mut Context<'_>) -> Poll> { + fn poll_open_uni( + &mut self, + cx: &mut Context<'_>, + options: crate::transport::poll::SendOptions< + '_, + ::Group, + >, + ) -> Poll> { self.inner - .poll_open_uni(cx) + .poll_open_uni(cx, options) .map(|r| r.map(AdapterSend::Real).map_err(|_| crate::Error::Closed)) } @@ -1217,11 +1230,13 @@ impl crate::transport::poll::Session for Con fn send_datagram( &mut self, payload: Bytes, - group: Option<&::Group>, - order: i64, + options: crate::transport::poll::SendOptions< + '_, + ::Group, + >, ) -> Result<(), Self::Error> { self.inner - .send_datagram(payload, group, order) + .send_datagram(payload, options) .map_err(|_| crate::Error::Closed) } @@ -1914,7 +1929,7 @@ mod tests { } script.extend_from_slice(then); let mut session = ScriptedSession::new(script); - let (_, recv) = session.open_bi().await.unwrap(); + let (_, recv) = session.open_bi(Default::default()).await.unwrap(); // We are the server, so the client's request IDs are even. let control = Control::new(None, false).with_window(window, false, VERSION); let adapter = ControlStreamAdapter::new(session, control, VERSION); @@ -1979,7 +1994,7 @@ mod tests { }; let script = encode_raw(ietf::SubscribeUpdate::ID, &Bytes::copy_from_slice(body), version); let mut session = ScriptedSession::new(script.to_vec()); - let (_, recv) = session.open_bi().await.unwrap(); + let (_, recv) = session.open_bi(Default::default()).await.unwrap(); let adapter = ControlStreamAdapter::new(session, Control::new(None, false), version); let log = Log::default(); let writer = Writer::new(SinkSend::new(log.clone()), version); diff --git a/rs/moq-net/src/ietf/publisher.rs b/rs/moq-net/src/ietf/publisher.rs index c05b4f25a02..b0741ae4bd9 100644 --- a/rs/moq-net/src/ietf/publisher.rs +++ b/rs/moq-net/src/ietf/publisher.rs @@ -11,8 +11,8 @@ use std::{ time::Duration, }; -use crate::transport::poll::GroupOf; use crate::transport::poll::SendGroup as _; +use crate::transport::poll::{GroupOf, SendOptions}; use crate::{ AsPath, DecodeError, Error, Timescale, Timestamp, @@ -1785,8 +1785,10 @@ where FillServe::Unsupported => None, }; + let mut rank = rank; + let options = rank.as_mut().map(Rank::options).unwrap_or_default(); let mut session = self.session.clone(); - let stream = match session.open_uni().await { + let stream = match session.open_uni(options).await { Ok(stream) => stream, Err(err) => { tracing::debug!(err = %Error::from_transport(err), fill = %request_id, "fill stream failed to open"); @@ -2502,8 +2504,13 @@ where }) .await?; - let uni = self.session.open_uni().await.map_err(Error::from_transport)?; - let mut writer = Writer::new(Ranked::new(uni, Some(ranking.rank(start))), self.version); + let mut rank = ranking.rank(start); + let uni = self + .session + .open_uni(rank.options()) + .await + .map_err(Error::from_transport)?; + let mut writer = Writer::new(Ranked::new(uni, Some(rank)), self.version); writer.varint(FetchHeader::TYPE).await?; writer .encode(&FetchHeader { @@ -4139,17 +4146,16 @@ impl TrackServe { /// Send the original terminal on a separate stream when its group already sent FIN, /// ranked as the group it ends. async fn write_end_of_track(&mut self, end: track::Position) -> Result<(), Error> { - let stream = std::future::poll_fn(|cx| self.session.poll_open_uni(cx)) + let mut rank = self.ranking.rank(Location { + group: end.group, + object: end.frame, + }); + let options = rank.options(); + let stream = std::future::poll_fn(|cx| self.session.poll_open_uni(cx, options)) .await .map_err(Error::from_transport)?; self.opened.fetch_add(1, Ordering::Relaxed); - let stream = Ranked::new( - stream, - Some(self.ranking.rank(Location { - group: end.group, - object: end.frame, - })), - ); + let stream = Ranked::new(stream, Some(rank)); let mut writer = Writer::new(stream, self.version); writer.buffer(&ietf::GroupHeader { track_alias: self.request_id.0, @@ -4373,8 +4379,11 @@ impl TrackServe { } // Ranked like a stream carrying the group from its start, so it merges into the // subscription's group order and wins a tie with that group's stream. - let order = crate::priority::in_group(sequence, 0, self.ranking.order); - let _ = self.session.send_datagram(body, self.ranking.group.as_ref(), order); + let options = SendOptions { + group: self.ranking.group.as_ref(), + priority: crate::priority::in_group(sequence, 0, self.ranking.order), + }; + let _ = self.session.send_datagram(body, options); } Ok(()) } @@ -4430,10 +4439,8 @@ struct Ranked { } impl Ranked { - fn new(mut stream: T, mut rank: Option>) -> Self { - if let Some(rank) = &mut rank { - rank.place(&mut stream); - } + /// Wrap a stream opened with its rank's [options](Rank::options). + fn new(stream: T, rank: Option>) -> Self { Self { stream, rank, @@ -4517,14 +4524,17 @@ impl Rank { Self::Queue(Queued::solo(priority)) } - /// Put a newly opened stream in its place. - fn place>(&mut self, stream: &mut T) { + /// Where a newly opened stream goes. + fn options(&mut self) -> SendOptions<'_, G> { match self { - Self::Group { group, order } => { - stream.set_group(group); - stream.set_priority(*order); - } - Self::Queue(queued) => stream.set_priority(queued.handle.send_order().into()), + Self::Group { group, order } => SendOptions { + group: Some(&*group), + priority: *order, + }, + Self::Queue(queued) => SendOptions { + group: None, + priority: queued.handle.send_order().into(), + }, } } @@ -4691,7 +4701,8 @@ impl GroupServe { self.state = GroupState::Done; return Poll::Ready(Err(Error::Old)); } - let stream = match ready!(self.session.poll_open_uni(&mut cx)) { + // The current rank, which already includes anything `moved` saw. + let stream = match ready!(self.session.poll_open_uni(&mut cx, self.rank.options())) { Ok(stream) => stream, Err(err) => { self.state = GroupState::Done; @@ -4699,9 +4710,6 @@ impl GroupServe { } }; self.opened.fetch_add(1, Ordering::Relaxed); - let mut stream = stream; - // The current rank, which already includes anything `moved` saw. - self.rank.place(&mut stream); moved = None; let mut writer = Writer::new(stream, self.version); diff --git a/rs/moq-net/src/ietf/session.rs b/rs/moq-net/src/ietf/session.rs index 633e149790b..dab78786fa1 100644 --- a/rs/moq-net/src/ietf/session.rs +++ b/rs/moq-net/src/ietf/session.rs @@ -793,7 +793,10 @@ async fn run_setup( ) -> Result<(), Error> { let outer_version = crate::Version::Ietf(version); - let send = session.open_uni().await.map_err(Error::from_transport)?; + let send = session + .open_uni(Default::default()) + .await + .map_err(Error::from_transport)?; let mut writer: Writer = Writer::new(send, outer_version); let mut parameters = ietf::Parameters::default(); diff --git a/rs/moq-net/src/ietf/subscriber.rs b/rs/moq-net/src/ietf/subscriber.rs index 6f4fc1dcb6e..3ee64ba50c7 100644 --- a/rs/moq-net/src/ietf/subscriber.rs +++ b/rs/moq-net/src/ietf/subscriber.rs @@ -5473,7 +5473,7 @@ mod tests { use crate::lite::test_transport::ScriptedSession; use crate::transport::poll::Session as _; let mut session = ScriptedSession::eof(responses); - let (_, recv) = session.open_bi().await.unwrap(); + let (_, recv) = session.open_bi(Default::default()).await.unwrap(); let mut reader = Reader::new(recv, Version::Draft19); let result = Subscriber::::read_publish_done(&mut reader, Version::Draft19, &AtomicU64::new(0)).await; @@ -5505,7 +5505,7 @@ mod tests { .unwrap(); let mut session = ScriptedSession::new(wire); - let (_, recv) = session.open_bi().await.unwrap(); + let (_, recv) = session.open_bi(Default::default()).await.unwrap(); let mut reader = Reader::new(recv, VERSION); let result = decode_fetch_object(&mut reader, VERSION, true) .now_or_never() @@ -11167,7 +11167,7 @@ mod stitch_tests { session: &ScriptedSession, ) -> Reader<::RecvStream, Version> { let mut session = session.clone(); - let (_, recv) = session.open_bi().await.unwrap(); + let (_, recv) = session.open_bi(Default::default()).await.unwrap(); Reader::new(recv, Version::Draft18) } @@ -11280,7 +11280,7 @@ mod stitch_tests { /// A reader over the next scripted stream, standing in for one the peer opened. async fn stream(&self) -> Reader<::RecvStream, Version> { let mut session = self.session.clone(); - let (_, recv) = session.open_bi().await.unwrap(); + let (_, recv) = session.open_bi(Default::default()).await.unwrap(); Reader::new(recv, VERSION) } } @@ -13545,7 +13545,7 @@ mod stitch_tests { tasks.0.clone(), Default::default(), ); - let (_, recv) = session.open_bi().await.unwrap(); + let (_, recv) = session.open_bi(Default::default()).await.unwrap(); let track = track::Producer::new( std::sync::Arc::new(crate::broadcast::Info::default()), "video", @@ -14094,7 +14094,7 @@ mod joining_fetch_tests { } w.bytes(b"x").unwrap(); let data = ScriptedSession::new(objects); - let (_, recv) = data.clone().open_bi().await.unwrap(); + let (_, recv) = data.clone().open_bi(Default::default()).await.unwrap(); let mut reader = Reader::new(recv, version); let mut receiving = run.subscriber.clone(); let mut fill = Box::pin(receiving.recv_fill(&mut reader)); @@ -14248,7 +14248,7 @@ mod joining_fetch_tests { .unwrap(); objects.push(b'x'); let data = ScriptedSession::new(objects); - let (_, recv) = data.clone().open_bi().await.unwrap(); + let (_, recv) = data.clone().open_bi(Default::default()).await.unwrap(); let mut reader = Reader::new(recv, version); let mut receiving = subscriber.clone(); let mut fill = Box::pin(receiving.recv_group_fetch(&mut reader, slot.clone())); @@ -14596,7 +14596,7 @@ mod joining_fetch_tests { settle().await; // The fetch stream, as the peer would open it. - let (_, recv) = session.clone().open_bi().await.unwrap(); + let (_, recv) = session.clone().open_bi(Default::default()).await.unwrap(); let mut stream = Reader::new(recv, version); subscriber.clone().recv_fill(&mut stream).await.expect("group fetch"); serving.await.expect("run_group_fetch"); diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index abb62a1817f..6a88c03f934 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -17,7 +17,7 @@ use crate::{ lite, priority::{Priority, PriorityHandle, PriorityQueue}, track::Order, - transport::poll::{GroupOf, SendGroup as _}, + transport::poll::{GroupOf, SendGroup as _, SendOptions}, }; use super::Version; @@ -3268,8 +3268,12 @@ impl Subscription { } // The run loop may not have served a group since the last SUBSCRIBE_UPDATE. - let order = crate::priority::in_group(datagram.sequence, 0, self.steer_current().order); - let _ = self.session.send_datagram(body, self.group.as_ref(), order); + let priority = crate::priority::in_group(datagram.sequence, 0, self.steer_current().order); + let options = SendOptions { + group: self.group.as_ref(), + priority, + }; + let _ = self.session.send_datagram(body, options); true } @@ -3870,7 +3874,21 @@ impl GroupServe { return Poll::Ready(Err(Error::Old)); } let mut cx = waiter.context(); - let stream = match ready!(self.ctx.session.poll_open_uni(&mut cx)) { + let options = match &mut self.rank { + Rank::Group(group) => SendOptions { + group: Some(&*group), + priority: crate::priority::in_group( + self.sequence, + self.frame_start, + self.ctx.steer_seen.order, + ), + }, + Rank::Queue(handle) => SendOptions { + group: None, + priority: handle.send_order().into(), + }, + }; + let stream = match ready!(self.ctx.session.poll_open_uni(&mut cx, options)) { Ok(stream) => stream, Err(err) => { self.settle_open(false); @@ -3880,14 +3898,6 @@ impl GroupServe { }; self.settle_open(true); let mut writer = Writer::new(stream, self.ctx.version); - match &mut self.rank { - Rank::Group(group) => { - writer.set_group(group); - let order = self.ctx.steer_seen.order; - writer.set_priority(crate::priority::in_group(self.sequence, self.frame_start, order)); - } - Rank::Queue(handle) => writer.set_priority(handle.send_order().into()), - } let msg = lite::Group { subscribe: self.ctx.id, @@ -6218,7 +6228,7 @@ mod tests { .unwrap(); let mut session = ScriptedSession::new(script); let (send, recv) = futures::future::poll_fn(|cx| { - ::poll_open_bi(&mut session, cx) + ::poll_open_bi(&mut session, cx, Default::default()) }) .await .unwrap(); @@ -6357,7 +6367,11 @@ mod tests { .unwrap(); let mut session = ScriptedSession::new(script); let (_, recv) = futures::future::poll_fn(|cx| { - ::poll_open_bi(&mut session, cx) + ::poll_open_bi( + &mut session, + cx, + Default::default(), + ) }) .await .unwrap(); @@ -6446,7 +6460,7 @@ mod tests { .unwrap(); let mut session = ScriptedSession::new(script); let (send, recv) = futures::future::poll_fn(|cx| { - ::poll_open_bi(&mut session, cx) + ::poll_open_bi(&mut session, cx, Default::default()) }) .await .unwrap(); @@ -6539,7 +6553,7 @@ mod tests { .unwrap(); let mut session = ScriptedSession::new(script); let (send, recv) = futures::future::poll_fn(|cx| { - ::poll_open_bi(&mut session, cx) + ::poll_open_bi(&mut session, cx, Default::default()) }) .await .unwrap(); @@ -6732,7 +6746,7 @@ mod tests { .unwrap(); let mut session = ScriptedSession::new(script); let (send, recv) = futures::future::poll_fn(|cx| { - ::poll_open_bi(&mut session, cx) + ::poll_open_bi(&mut session, cx, Default::default()) }) .await .unwrap(); @@ -6802,7 +6816,7 @@ mod tests { for count in 0..3 { let mut session = ScriptedSession::new(script.clone()); let (send, recv) = futures::future::poll_fn(|cx| { - ::poll_open_bi(&mut session, cx) + ::poll_open_bi(&mut session, cx, Default::default()) }) .await .unwrap(); diff --git a/rs/moq-net/src/lite/session.rs b/rs/moq-net/src/lite/session.rs index c644604053e..25e6e7eb1a0 100644 --- a/rs/moq-net/src/lite/session.rs +++ b/rs/moq-net/src/lite/session.rs @@ -714,7 +714,8 @@ impl SendSetup { loop { match &mut self.state { SendSetupState::Open { session, setup } => { - let stream = ready!(session.poll_open_uni(cx)).map_err(crate::lite::error::from_transport)?; + let stream = ready!(session.poll_open_uni(cx, Default::default())) + .map_err(crate::lite::error::from_transport)?; let mut writer = Writer::new(stream, self.version); writer.buffer(&super::DataType::Setup)?; writer.buffer(&**setup)?; diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 9af84174e0e..08b9c9413fa 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -2245,7 +2245,7 @@ mod tests { }, ); - let (_, recv) = session.open_bi().await.unwrap(); + let (_, recv) = session.open_bi(Default::default()).await.unwrap(); let mut group = GroupRecv::new(subscriber, Reader::new(recv, version)); let res = kio::wait(|waiter| group.poll_serve(waiter)).await; match version { @@ -2315,7 +2315,7 @@ mod tests { }, ); - let (_, recv) = session.open_bi().await.unwrap(); + let (_, recv) = session.open_bi(Default::default()).await.unwrap(); let mut group = GroupRecv::new(subscriber, Reader::new(recv, version)); let res = kio::wait(|waiter| group.poll_serve(waiter)).await; (res, track) @@ -2382,7 +2382,7 @@ mod tests { }, ); - let (_, recv) = session.open_bi().await.unwrap(); + let (_, recv) = session.open_bi(Default::default()).await.unwrap(); let mut group = GroupRecv::new(subscriber, Reader::new(recv, version)); let res = kio::wait(|waiter| group.poll_serve(waiter)).await; assert!(matches!(res, Err(Error::Duplicate)), "{res:?}"); diff --git a/rs/moq-net/src/lite/test_transport.rs b/rs/moq-net/src/lite/test_transport.rs index 967f72e621a..577f25bdc78 100644 --- a/rs/moq-net/src/lite/test_transport.rs +++ b/rs/moq-net/src/lite/test_transport.rs @@ -43,7 +43,7 @@ pub type Rank = (Option, i64); #[derive(Clone, Default)] pub struct Log { pub datagrams: Arc>>, - /// The send group and order each datagram was sent with, beside [`Self::datagrams`]. + /// The send group and priority each datagram was sent with, beside [`Self::datagrams`]. datagram_ranks: Arc>>, pub writes: Arc>>, pub resets: Arc>>, @@ -130,17 +130,17 @@ impl Log { self.orders.lock().unwrap().clone() } - /// The send group and order of every datagram, as (group, order) in call order. + /// The send group and priority of every datagram, as (group, priority) in call order. pub fn datagram_ranks(&self) -> Vec { self.datagram_ranks.lock().unwrap().clone() } - fn record_datagram(&self, payload: bytes::Bytes, group: Option<&SinkGroup>, order: i64) { + fn record_datagram(&self, payload: bytes::Bytes, options: poll::SendOptions<'_, SinkGroup>) { self.datagrams.lock().unwrap().push(payload); self.datagram_ranks .lock() .unwrap() - .push((group.map(|group| group.id), order)); + .push((options.group.map(|group| group.id), options.priority)); } /// The session-level closes, as (code, reason). A list rather than a last-value so @@ -217,6 +217,19 @@ impl poll::SendGroup for SinkGroup { } impl SinkSend { + /// Schedule a stream as it opens, logging only what differs from a fresh stream's + /// ungrouped priority 0, as the setters would. + fn opened(mut self, options: poll::SendOptions<'_, SinkGroup>) -> Self { + use poll::SendStream as _; + if let Some(group) = options.group { + self.set_group(group); + } + if options.group.is_some() || options.priority != 0 { + self.set_priority(options.priority); + } + self + } + pub fn new(log: Log) -> Self { Self { prefix: 0, @@ -445,13 +458,24 @@ impl poll::Session for DeadStreamSession { } } - fn poll_open_bi(&mut self, _cx: &mut Context<'_>) -> Poll, Self::Error>> { + fn poll_open_bi( + &mut self, + _cx: &mut Context<'_>, + options: poll::SendOptions<'_, SinkGroup>, + ) -> Poll, Self::Error>> { self.log.bi_opens.fetch_add(1, Ordering::Relaxed); - Poll::Ready(Ok((SinkSend::new(self.log.clone()), DeadRecv(self.reset)))) + Poll::Ready(Ok(( + SinkSend::new(self.log.clone()).opened(options), + DeadRecv(self.reset), + ))) } - fn poll_open_uni(&mut self, _cx: &mut Context<'_>) -> Poll> { - Poll::Ready(Ok(SinkSend::new(self.log.clone()))) + fn poll_open_uni( + &mut self, + _cx: &mut Context<'_>, + options: poll::SendOptions<'_, SinkGroup>, + ) -> Poll> { + Poll::Ready(Ok(SinkSend::new(self.log.clone()).opened(options))) } fn send_group(&mut self, _: i64) -> Option { @@ -461,10 +485,9 @@ impl poll::Session for DeadStreamSession { fn send_datagram( &mut self, payload: bytes::Bytes, - group: Option<&SinkGroup>, - order: i64, + options: poll::SendOptions<'_, SinkGroup>, ) -> Result<(), Self::Error> { - self.log.record_datagram(payload, group, order); + self.log.record_datagram(payload, options); Ok(()) } @@ -641,18 +664,26 @@ impl poll::Session for SinkSession { Poll::Ready(Ok((send, PendingRecv))) } - fn poll_open_bi(&mut self, _cx: &mut Context<'_>) -> Poll, Self::Error>> { + fn poll_open_bi( + &mut self, + _cx: &mut Context<'_>, + options: poll::SendOptions<'_, SinkGroup>, + ) -> Poll, Self::Error>> { let Some(gate) = self.bi_gate.clone() else { return Poll::Pending; }; self.log.bi_opens.fetch_add(1, Ordering::Relaxed); - let mut send = SinkSend::gated(self.log.clone(), gate); + let mut send = SinkSend::gated(self.log.clone(), gate).opened(options); send.prefix = self.bi_prefix; Poll::Ready(Ok((send, PendingRecv))) } - fn poll_open_uni(&mut self, cx: &mut Context<'_>) -> Poll> { + fn poll_open_uni( + &mut self, + cx: &mut Context<'_>, + options: poll::SendOptions<'_, SinkGroup>, + ) -> Poll> { if let Some(gate) = &self.uni_open_gate { let waiter = self.uni_open_park.hold(cx); match gate.poll(waiter, |open| (**open).then_some(()).map_or(Poll::Pending, Poll::Ready)) { @@ -665,7 +696,7 @@ impl poll::Session for SinkSession { None => SinkSend::new(self.log.clone()), }; send.unacked_fin = self.unacked_fin; - Poll::Ready(Ok(send)) + Poll::Ready(Ok(send.opened(options))) } fn send_group(&mut self, priority: i64) -> Option { @@ -675,10 +706,9 @@ impl poll::Session for SinkSession { fn send_datagram( &mut self, payload: bytes::Bytes, - group: Option<&SinkGroup>, - order: i64, + options: poll::SendOptions<'_, SinkGroup>, ) -> Result<(), Self::Error> { - self.log.record_datagram(payload, group, order); + self.log.record_datagram(payload, options); Ok(()) } @@ -965,7 +995,11 @@ impl poll::Session for ScriptedSession { ))) } - fn poll_open_bi(&mut self, cx: &mut Context<'_>) -> Poll, Self::Error>> { + fn poll_open_bi( + &mut self, + cx: &mut Context<'_>, + options: poll::SendOptions<'_, SinkGroup>, + ) -> Poll, Self::Error>> { if let Some(gate) = self.open_gate.clone() { let waiter = self.park.hold(cx); // A closed gate reads as open: the test dropped the producer, so nothing is @@ -983,7 +1017,7 @@ impl poll::Session for ScriptedSession { Some(queue) => Arc::new(Mutex::new(queue.lock().unwrap().pop_front().unwrap_or_default())), None => self.script.clone(), }; - let mut send = SinkSend::new(self.log.clone()); + let mut send = SinkSend::new(self.log.clone()).opened(options); send.unacked_fin = self.unacked_fin; Poll::Ready(Ok(( send, @@ -995,8 +1029,12 @@ impl poll::Session for ScriptedSession { ))) } - fn poll_open_uni(&mut self, _cx: &mut Context<'_>) -> Poll> { - Poll::Ready(Ok(SinkSend::new(self.log.clone()))) + fn poll_open_uni( + &mut self, + _cx: &mut Context<'_>, + options: poll::SendOptions<'_, SinkGroup>, + ) -> Poll> { + Poll::Ready(Ok(SinkSend::new(self.log.clone()).opened(options))) } fn send_group(&mut self, _: i64) -> Option { @@ -1006,10 +1044,9 @@ impl poll::Session for ScriptedSession { fn send_datagram( &mut self, payload: bytes::Bytes, - group: Option<&SinkGroup>, - order: i64, + options: poll::SendOptions<'_, SinkGroup>, ) -> Result<(), Self::Error> { - self.log.record_datagram(payload, group, order); + self.log.record_datagram(payload, options); Ok(()) } diff --git a/rs/moq-net/src/server.rs b/rs/moq-net/src/server.rs index b23061cfcf4..e6d8675d229 100644 --- a/rs/moq-net/src/server.rs +++ b/rs/moq-net/src/server.rs @@ -1067,12 +1067,14 @@ mod tests { fn poll_open_bi( &mut self, _cx: &mut std::task::Context<'_>, + _: crate::transport::poll::SendOptions<'_, crate::transport::poll::Ungrouped>, ) -> std::task::Poll> { std::task::Poll::Pending } fn poll_open_uni( &mut self, _cx: &mut std::task::Context<'_>, + _: crate::transport::poll::SendOptions<'_, crate::transport::poll::Ungrouped>, ) -> std::task::Poll> { std::task::Poll::Pending } @@ -1083,8 +1085,7 @@ mod tests { fn send_datagram( &mut self, _: Bytes, - _: Option<&crate::transport::poll::Ungrouped>, - _: i64, + _: crate::transport::poll::SendOptions<'_, crate::transport::poll::Ungrouped>, ) -> Result<(), Self::Error> { Ok(()) } diff --git a/rs/moq-net/src/transport.rs b/rs/moq-net/src/transport.rs index 107e1f7fc5d..ddde6f50650 100644 --- a/rs/moq-net/src/transport.rs +++ b/rs/moq-net/src/transport.rs @@ -204,30 +204,39 @@ pub mod poll { /// Poll for a bidirectional stream created by the peer. fn poll_accept_bi(&mut self, cx: &mut Context<'_>) -> Poll, Self::Error>>; - /// Poll to open a unidirectional stream, which blocks while there are too many - /// concurrent streams. - fn poll_open_uni(&mut self, cx: &mut Context<'_>) -> Poll>; + /// Poll to open a unidirectional stream scheduled by `options`, which blocks while + /// there are too many concurrent streams. + fn poll_open_uni( + &mut self, + cx: &mut Context<'_>, + options: SendOptions<'_, ::Group>, + ) -> Poll>; - /// Poll to open a bidirectional stream, which blocks while there are too many - /// concurrent streams. - fn poll_open_bi(&mut self, cx: &mut Context<'_>) -> Poll, Self::Error>>; + /// Poll to open a bidirectional stream, its sending half scheduled by `options`, which + /// blocks while there are too many concurrent streams. + fn poll_open_bi( + &mut self, + cx: &mut Context<'_>, + options: SendOptions<'_, ::Group>, + ) -> Poll, Self::Error>>; /// Open a send group at `priority`, or `None` when the transport orders streams by /// their own priority alone. /// - /// Streams that [join](SendStream::set_group) a group share bytes with the session's + /// Streams that join a group, through [`SendOptions`] or [`SendStream::set_group`], and + /// datagrams sent in it share bytes with the session's /// other groups: a higher priority group preempts every lower one, and groups of equal /// priority get an equal share of bytes however many streams each has queued. Streams /// outside every group go before all of them. The group is released once its last /// handle is dropped and its streams finish. fn send_group(&mut self, priority: i64) -> Option<::Group>; - /// Send a datagram in `group` at `order`, best-effort. + /// Send a datagram scheduled by `options`, best-effort. /// - /// `order` ranks the datagram among the group's streams like a stream's own - /// [priority](SendStream::set_priority), and a datagram goes ahead of a stream of equal - /// order. Without a group it is ranked among the ungrouped streams. A transport that - /// cannot rank datagrams against streams ignores both. + /// The datagram is ranked among its group's streams by its priority, like a stream's + /// own [priority](SendStream::set_priority), and goes ahead of a stream of equal + /// priority. Without a group it is ranked among the ungrouped streams. A transport that + /// cannot rank datagrams against streams ignores `options`. /// /// Never blocks: a transport with no room drops a queued datagram, the lowest priority /// first where it can tell, exactly as the network is allowed to. @@ -240,8 +249,7 @@ pub mod poll { fn send_datagram( &mut self, payload: Bytes, - group: Option<&::Group>, - order: i64, + options: SendOptions<'_, ::Group>, ) -> Result<(), Self::Error>; /// Poll for a datagram from the network. @@ -293,14 +301,22 @@ pub mod poll { AcceptBi(self) } - /// Open a unidirectional stream, waiting for stream credit if necessary. - fn open_uni(&mut self) -> OpenUni<'_, Self> { - OpenUni(self) + /// Open a unidirectional stream scheduled by `options`, waiting for stream credit if + /// necessary. + fn open_uni<'a>( + &'a mut self, + options: SendOptions<'a, ::Group>, + ) -> OpenUni<'a, Self> { + OpenUni { session: self, options } } - /// Open a bidirectional stream, waiting for stream credit if necessary. - fn open_bi(&mut self) -> OpenBi<'_, Self> { - OpenBi(self) + /// Open a bidirectional stream, its sending half scheduled by `options`, waiting for + /// stream credit if necessary. + fn open_bi<'a>( + &'a mut self, + options: SendOptions<'a, ::Group>, + ) -> OpenBi<'a, Self> { + OpenBi { session: self, options } } /// Receive the next datagram from the peer. @@ -338,12 +354,32 @@ pub mod poll { poll_future!( /// A pending [`Session::accept_bi`]. AcceptBi, Session, poll_accept_bi, Result, S::Error>); - poll_future!( + /// One in-flight open, which polls with the same options until it resolves. + macro_rules! open_future { + ($(#[$doc:meta])* $name:ident, $poll:ident, $out:ty) => { + $(#[$doc])* + pub struct $name<'a, S: Session> { + session: &'a mut S, + options: SendOptions<'a, ::Group>, + } + + impl Future for $name<'_, S> { + type Output = $out; + + fn poll(self: std::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + let this = self.get_mut(); + this.session.$poll(cx, this.options) + } + } + }; + } + + open_future!( /// A pending [`Session::open_uni`]. - OpenUni, Session, poll_open_uni, Result); - poll_future!( + OpenUni, poll_open_uni, Result); + open_future!( /// A pending [`Session::open_bi`]. - OpenBi, Session, poll_open_bi, Result, S::Error>); + OpenBi, poll_open_bi, Result, S::Error>); poll_future!( /// A pending [`Session::recv_datagram`]. RecvDatagram, Session, poll_recv_datagram, Result); @@ -384,6 +420,35 @@ pub mod poll { #[cfg(feature = "async")] pub(crate) type GroupOf = <::SendStream as SendStream>::Group; + /// How an outgoing stream or datagram is scheduled, like W3C WebTransport's send options. + /// + /// The default is ungrouped at priority 0. + #[derive(Debug)] + pub struct SendOptions<'a, G> { + /// The send group to join, from [`Session::send_group`], or `None` for the ungrouped + /// streams that go before every group. + pub group: Option<&'a G>, + /// The priority within the group, like [`SendStream::set_priority`]: higher goes first. + pub priority: i64, + } + + impl Default for SendOptions<'_, G> { + fn default() -> Self { + Self { + group: None, + priority: 0, + } + } + } + + impl Clone for SendOptions<'_, G> { + fn clone(&self) -> Self { + *self + } + } + + impl Copy for SendOptions<'_, G> {} + /// A transport's send group, from [`Session::send_group`]. /// /// Clones share the group. diff --git a/rs/moq-net/src/transport/memory.rs b/rs/moq-net/src/transport/memory.rs index e1713331c47..db944fd01fd 100644 --- a/rs/moq-net/src/transport/memory.rs +++ b/rs/moq-net/src/transport/memory.rs @@ -13,7 +13,7 @@ //! with a code and drops what it queued. Closing the session keeps queued messages //! readable, and leaves streams as they were, so the peer reads them, then the close. //! -//! Datagrams ignore their send group and order, and are dropped when oversized or when the +//! Datagrams ignore their send options, and are dropped when oversized or when the //! peer's queue is full, as a network would. use std::{ @@ -282,12 +282,20 @@ impl poll::Session for Session { Self::poll_accept(&self.side.incoming().bi, &self.side.conn.close, waiter) } - fn poll_open_uni(&mut self, _cx: &mut Context<'_>) -> Poll> { + fn poll_open_uni( + &mut self, + _cx: &mut Context<'_>, + _: poll::SendOptions<'_, poll::Ungrouped>, + ) -> Poll> { let (send, recv) = self.conn().stream(); Poll::Ready(self.open(&self.side.outgoing().uni, recv).map(|()| send)) } - fn poll_open_bi(&mut self, _cx: &mut Context<'_>) -> Poll> { + fn poll_open_bi( + &mut self, + _cx: &mut Context<'_>, + _: poll::SendOptions<'_, poll::Ungrouped>, + ) -> Poll> { let (send, peer_recv) = self.conn().stream(); let (peer_send, recv) = self.conn().stream(); let opened = self.open(&self.side.outgoing().bi, (peer_send, peer_recv)); @@ -298,7 +306,7 @@ impl poll::Session for Session { None } - fn send_datagram(&mut self, payload: Bytes, _: Option<&poll::Ungrouped>, _: i64) -> Result<(), Error> { + fn send_datagram(&mut self, payload: Bytes, _: poll::SendOptions<'_, poll::Ungrouped>) -> Result<(), Error> { if let Some(err) = closed(&self.conn().close) { return Err(err); } @@ -638,7 +646,7 @@ mod tests { } fn uni(a: &mut Session, b: &mut Session) -> (SendStream, RecvStream) { - let send = ready(a.poll_open_uni(&mut noop())).unwrap(); + let send = ready(a.poll_open_uni(&mut noop(), Default::default())).unwrap(); let recv = ready(b.poll_accept_uni(&mut noop())).unwrap(); (send, recv) } @@ -814,7 +822,7 @@ mod tests { #[test] fn bidi_streams_pair_up() { let (mut a, mut b) = pair(Config::default()); - let (mut a_send, mut a_recv) = ready(a.poll_open_bi(&mut noop())).unwrap(); + let (mut a_send, mut a_recv) = ready(a.poll_open_bi(&mut noop(), Default::default())).unwrap(); let (mut b_send, mut b_recv) = ready(b.poll_accept_bi(&mut noop())).unwrap(); ready(write(&mut a_send, &Bytes::from_static(b"ping"))).unwrap(); @@ -832,7 +840,7 @@ mod tests { assert_eq!(b.protocol(), Some("moq")); let (mut send, mut recv) = uni(&mut a, &mut b); let (mut unfinished, mut unfinished_recv) = uni(&mut a, &mut b); - let late = ready(a.poll_open_uni(&mut noop())).unwrap(); + let late = ready(a.poll_open_uni(&mut noop(), Default::default())).unwrap(); ready(write(&mut send, &Bytes::from_static(b"last"))).unwrap(); send.finish().unwrap(); @@ -842,7 +850,9 @@ mod tests { a.close(4, "bye"); let closed = |err: Error| matches!(err, Error::Session { code: 4, ref reason } if reason == "bye"); assert!(closed(ready(b.poll_closed(&mut noop())))); - assert!(closed(ready(a.poll_open_uni(&mut noop())).err().unwrap())); + assert!(closed( + ready(a.poll_open_uni(&mut noop(), Default::default())).err().unwrap() + )); assert!(closed( ready(write(&mut unfinished, &Bytes::from_static(b"x"))).unwrap_err() )); @@ -877,12 +887,13 @@ mod tests { #[test] fn datagrams() { let (mut a, mut b) = pair(Config::default()); - a.send_datagram(vec![0; MAX_DATAGRAM_SIZE + 1].into(), None, 0).unwrap(); + a.send_datagram(vec![0; MAX_DATAGRAM_SIZE + 1].into(), Default::default()) + .unwrap(); for _ in 0..DATAGRAM_QUEUE { - a.send_datagram("one".into(), None, 0).unwrap(); + a.send_datagram("one".into(), Default::default()).unwrap(); } // The peer's queue is full, so this one is lost, like the oversized one. - a.send_datagram("two".into(), None, 0).unwrap(); + a.send_datagram("two".into(), Default::default()).unwrap(); for _ in 0..DATAGRAM_QUEUE { assert_eq!(ready(b.poll_recv_datagram(&mut noop())).unwrap(), "one"); } diff --git a/rs/moq-net/tests/integration/goaway.rs b/rs/moq-net/tests/integration/goaway.rs index 55a8de23031..5e32619e302 100644 --- a/rs/moq-net/tests/integration/goaway.rs +++ b/rs/moq-net/tests/integration/goaway.rs @@ -257,7 +257,11 @@ async fn duplicate_goaway_keeps_first_payload_moq_lite_04() { let mut frame = vec![0x05u8, uri.len() as u8 + 1, uri.len() as u8]; frame.extend_from_slice(uri.as_bytes()); - let (mut send, recv) = session.open_bi().await.map_err(|e| e.to_string()).expect("open_bi"); + let (mut send, recv) = session + .open_bi(Default::default()) + .await + .map_err(|e| e.to_string()) + .expect("open_bi"); let n = send.write(&frame).await.map_err(|e| e.to_string()).expect("write"); assert_eq!(n, frame.len()); send.finish().map_err(|e| e.to_string()).expect("finish"); diff --git a/rs/moq-net/tests/integration/subscribe_tracks.rs b/rs/moq-net/tests/integration/subscribe_tracks.rs index 64e0a247fbc..c8b19d3a19f 100644 --- a/rs/moq-net/tests/integration/subscribe_tracks.rs +++ b/rs/moq-net/tests/integration/subscribe_tracks.rs @@ -106,7 +106,12 @@ async fn deliver(setup: &mut Setup, payload: &'static [u8]) { /// Send SUBSCRIBE_TRACKS from the server to the publishing client, returning every byte /// of the reply once the client ends the stream. async fn subscribe_tracks(pair: &MockPair) -> Vec { - let (mut send, mut recv) = pair.server_transport.clone().open_bi().await.expect("open_bi"); + let (mut send, mut recv) = pair + .server_transport + .clone() + .open_bi(Default::default()) + .await + .expect("open_bi"); send.write(SUBSCRIBE_TRACKS).await.expect("write"); let mut reply = Vec::new(); @@ -148,7 +153,13 @@ async fn subscribe_tracks_is_refused_per_request() { #[moq_clock::test] async fn subscribe_tracks_before_draft_18_closes_the_session() { let setup = setup("moq-transport-17".parse().unwrap()).await; - let (mut send, _recv) = setup.pair.server_transport.clone().open_bi().await.expect("open_bi"); + let (mut send, _recv) = setup + .pair + .server_transport + .clone() + .open_bi(Default::default()) + .await + .expect("open_bi"); send.write(SUBSCRIBE_TRACKS).await.expect("write"); moq_clock::timeout(TIMEOUT, setup.pair.client.closed()) diff --git a/rs/moq-net/tests/integration/support/mock.rs b/rs/moq-net/tests/integration/support/mock.rs index 1ba0ec693ba..3ee81a8a738 100644 --- a/rs/moq-net/tests/integration/support/mock.rs +++ b/rs/moq-net/tests/integration/support/mock.rs @@ -555,10 +555,11 @@ impl poll::Session for MockSession { fn poll_open_bi( &mut self, cx: &mut Context<'_>, + options: poll::SendOptions<'_, poll::Ungrouped>, ) -> Poll> { let link = &self.side.out; let mut lines = link.bi.lock().unwrap(); - let (send, recv) = ready!(self.inner.poll_open_bi(cx))?; + let (send, recv) = ready!(self.inner.poll_open_bi(cx, Default::default()))?; let held = link.held_bi.lock().unwrap().is_some(); let forward = Arc::new(Line { held, @@ -572,19 +573,24 @@ impl poll::Session for MockSession { drop(lines); let mut send = self.send(send, forward); + poll::SendStream::set_priority(&mut send, options.priority); send.withhold_fin = Some(self.side.withhold_bidi_fins.clone()); send.first_byte = Some(self.side.bidi_types.clone()); Poll::Ready(Ok((send, self.recv(recv, back)))) } - fn poll_open_uni(&mut self, cx: &mut Context<'_>) -> Poll> { + fn poll_open_uni( + &mut self, + cx: &mut Context<'_>, + options: poll::SendOptions<'_, poll::Ungrouped>, + ) -> Poll> { if self.side.withheld.load(Ordering::Relaxed) { return Poll::Ready(Err(ready!(self.closer.poll_closed(cx)))); } let link = &self.side.out; let mut lines = link.uni.lock().unwrap(); - let send = ready!(self.inner.poll_open_uni(cx))?; + let send = ready!(self.inner.poll_open_uni(cx, Default::default()))?; let line = Arc::new(Line { held: link.held_uni.lock().unwrap().is_some(), split: link.split.lock().unwrap().clone(), @@ -594,6 +600,7 @@ impl poll::Session for MockSession { drop(lines); let mut send = self.send(send, line); + poll::SendStream::set_priority(&mut send, options.priority); send.ack = send.line.held; Poll::Ready(Ok(send)) } @@ -605,13 +612,12 @@ impl poll::Session for MockSession { fn send_datagram( &mut self, payload: Bytes, - group: Option<&poll::Ungrouped>, - order: i64, + options: poll::SendOptions<'_, poll::Ungrouped>, ) -> Result<(), Self::Error> { if self.side.lossy.load(Ordering::Relaxed) { return Ok(()); } - self.inner.send_datagram(payload, group, order) + self.inner.send_datagram(payload, options) } fn poll_recv_datagram(&mut self, cx: &mut Context<'_>) -> Poll> { diff --git a/rs/moq-quic/MIGRATE.md b/rs/moq-quic/MIGRATE.md index 7072146ca40..0b5ffd1aeff 100644 --- a/rs/moq-quic/MIGRATE.md +++ b/rs/moq-quic/MIGRATE.md @@ -4,10 +4,40 @@ How to update your code for each breaking change, newest first. ## Unreleased -### `Datagrams::send` takes a send group and order (#5770) +Each entry is a change from `quinn-proto`. -`Datagrams::send(data, group, order)` replaces `send(data, drop)`; pass `None` for an ungrouped datagram. -A full buffer drops the oldest datagram of the lowest-priority group, so `SendDatagramError::Blocked` and `Event::DatagramsUnblocked` are gone. +### Streams and datagrams take `SendOptions` (#5770, #5872) + +`Streams::open` and `Datagrams::send` take a `SendOptions { group, priority }`, +the send group from `Connection::send_groups` and the priority within it. +`SendOptions::default()` is ungrouped at priority 0, where `open` used to start a stream. +A datagram is scheduled with the streams instead of ahead of them, so `send` loses its +`drop` flag: it never blocks, and a full `datagram_send_buffer_size` drops the oldest +datagram of the lowest-priority group. + +```rust +// Before +let id = conn.streams().open(Dir::Uni).unwrap(); +conn.datagrams().send(data, true)?; + +// After +let id = conn.streams().open(Dir::Uni, SendOptions::default()).unwrap(); +conn.datagrams().send(data, SendOptions { group: Some(&group), priority: 0 })?; +``` + +`SendDatagramError::Blocked` and `Event::DatagramsUnblocked` are gone, since sending +never blocks, and `SendDatagramError` is `#[non_exhaustive]`. + +### `SendStream::set_priority` takes an `i64` + +`set_priority` and `priority` use `i64` instead of `i32`, the range of a send group's +priority. Higher still goes first. + +### `fuzzing` module and `arbitrary` feature removed + +The `cfg(fuzzing)` `fuzzing` module and the `arbitrary` feature are gone. The fuzz +targets run through the hidden `fuzz` module behind the `fuzz` feature; see +[`fuzz/`](fuzz/README.md). ### `congestion::NewReno` removed diff --git a/rs/moq-quic/README.md b/rs/moq-quic/README.md index de35c22ad9c..ed3886470f2 100644 --- a/rs/moq-quic/README.md +++ b/rs/moq-quic/README.md @@ -67,8 +67,9 @@ Changes on top of the upstream commit, besides the renames: - `TransportConfig::handshake_idle_timeout` (default 10s, as msquic's `HandshakeIdleTimeoutMs`) governs the idle timer until the handshake completes, and the negotiated idle timeout takes over once established. A 2s idle timeout otherwise expires 3x the initial PTO in, just before the second Initial probe, leaving a client two flights. A server also drops an Initial left waiting for `accept` past it, and a deadline too far out to represent never fires instead of panicking. Also [moq-dev/noq#34](https://github.com/moq-dev/noq/pull/34), whose third change (the handshake idle timeout picks noq's idle-based PTO interval cap) has nothing to apply to here, since quinn's PTO backoff has no such cap. quinn's `handshake_timeout` test in its async layer expects the handshake to end at the idle timeout, so the driver's copy sets both timeouts. - **Instants on [moq-clock](../moq-clock)**: the crate's `Instant` is `moq_clock::Instant`, so one clock steps every timer, tokio's paused clock and the simulator included. qlog stamps events relative to `QlogConfig::start_time` on that clock rather than the streamer's own `std::time::Instant`. -- **Send groups** (`streams/schedule.rs`, `Connection::send_groups`): streams outside every group go first, in quinn's order; groups in one scheduling domain are served strictly by priority, with equal priorities sharing bytes by deficit round robin; domains share bytes the same way. A stream's priority is an `i64` ordering it only within its group. This replaces quinn's single pending-stream heap, so a cherry-pick touching `PendingStreamsQueue` has to be redone against the schedule. moq-tokio's `quic::driver` and moq-uring's driver wrap a group in a shared `SendGroup` handle that closes on drop. -- **Scheduled datagrams** (`Datagrams::send`): a datagram is queued in a send group (or ungrouped) at an `i64` order and scheduled with the group's streams, winning a tie with a stream of equal order and spending the group's byte credit, instead of quinn's datagram-first FIFO in `populate_packet`. Sending never blocks: `datagram_send_buffer_size` is shared, and an overflow evicts the oldest datagram of the lowest-priority group in the domain holding the most datagram bytes. `SendDatagramError::Blocked` and `Event::DatagramsUnblocked` are gone, and a cherry-pick touching the `// DATAGRAM` loop has to be redone against `StreamsState::write_frames`. +- **Send groups** (`streams/schedule.rs`, `Connection::send_groups`): streams outside every group go first, in quinn's order; groups are served strictly by priority, with equal priorities sharing bytes by deficit round robin. A stream's priority is an `i64` ordering it only within its group, and `Streams::open` takes a `SendOptions { group, priority }` to start it there, like W3C WebTransport's send options. The schedule also has scheduling domains that share bytes the same way, crate-private until a consumer needs them. This replaces quinn's single pending-stream heap, so a cherry-pick touching `PendingStreamsQueue` has to be redone against the schedule. moq-tokio's `quic::driver` and moq-uring's driver wrap a group in a shared `SendGroup` handle that closes on drop. +- **Scheduled datagrams** (`Datagrams::send`): a datagram is queued by its `SendOptions`, in a send group (or ungrouped) at an `i64` priority, and scheduled with the group's streams, winning a tie with a stream of equal priority and spending the group's byte credit, instead of quinn's datagram-first FIFO in `populate_packet`. Sending never blocks: `datagram_send_buffer_size` is shared, and an overflow evicts the oldest datagram of the lowest-priority group in the domain holding the most datagram bytes. `SendDatagramError::Blocked` and `Event::DatagramsUnblocked` are gone, and a cherry-pick touching the `// DATAGRAM` loop has to be redone against `StreamsState::write_frames`. +- **Sharded connection IDs** (`RandomConnectionIdGenerator::set_shard`): every CID names shard `index` of a `count`-endpoint group in its first byte, modulo `count`, and sets a marker in its fourth byte, so a `SO_REUSEPORT` steering filter delivers a packet to the endpoint that issued its CID. `validate` refuses a CID without this shard's prefix and marker, so a misrouted packet is dropped instead of answered with a stateless reset. Not offered upstream; it serves moq-sock's per-core sharding. - **`RESET_STREAM_AT`** ([draft-ietf-quic-reliable-stream-reset](https://datatracker.ietf.org/doc/draft-ietf-quic-reliable-stream-reset/)): the `reset_stream_at` transport parameter is always advertised, `Checkpoint::reset` (from `SendStream::checkpoint`) keeps the data written before the checkpoint reliable, holding a plain `RESET_STREAM` until that data is acknowledged when the peer lacks the extension, and a received reset surfaces only once the reliable prefix has been read. Stopping a stream after it was reset no longer credits its unread data twice. - **Fuzzing** runs through the hidden `fuzz` module behind a `fuzz` feature, with one-line shims in [`fuzz/`](fuzz/README.md): upstream's `packet`, `params`, `streamid`, and `streams` targets, plus `frame` and a connection op sequence. Upstream's `cfg(fuzzing)` `fuzzing` module and `arbitrary` feature are gone, so its fuzz patches port by hand. - **Draining endpoint event**: `EndpointEvent::is_draining` fires once per connection when it stops being active, before the drained event, so the driver's `Endpoint::wait_all_draining` can end a shutdown without sitting out every closing period. diff --git a/rs/moq-quic/benches/receive.rs b/rs/moq-quic/benches/receive.rs index 1cb84fc1a42..10989778c9c 100644 --- a/rs/moq-quic/benches/receive.rs +++ b/rs/moq-quic/benches/receive.rs @@ -39,7 +39,10 @@ fn bench_loss(c: &mut Criterion) { let target = pair.server.received.finished + streams as u64; let conn = pair.client(); for _ in 0..streams { - let id = conn.streams().open(Dir::Uni).expect("stream limit"); + let id = conn + .streams() + .open(Dir::Uni, Default::default()) + .expect("stream limit"); let mut stream = conn.send_stream(id); assert_eq!(stream.write(&data).unwrap(), data.len()); stream.finish().unwrap(); @@ -75,7 +78,10 @@ fn bench_reset(c: &mut Criterion) { let conn = pair.client(); let ids: Vec<_> = (0..streams) .map(|_| { - let id = conn.streams().open(Dir::Uni).expect("stream limit"); + let id = conn + .streams() + .open(Dir::Uni, Default::default()) + .expect("stream limit"); assert_eq!(conn.send_stream(id).write(&data).unwrap(), bytes); id }) @@ -116,7 +122,10 @@ fn bench_stop(c: &mut Criterion) { let target = pair.server.received.stopped + streams as u64; let conn = pair.client(); for _ in 0..streams { - let id = conn.streams().open(Dir::Uni).expect("stream limit"); + let id = conn + .streams() + .open(Dir::Uni, Default::default()) + .expect("stream limit"); assert_eq!(conn.send_stream(id).write(&data).unwrap(), bytes); } pair.drive_until(|pair| { diff --git a/rs/moq-quic/benches/streams.rs b/rs/moq-quic/benches/streams.rs index 35fc49536f8..db88dd21ed4 100644 --- a/rs/moq-quic/benches/streams.rs +++ b/rs/moq-quic/benches/streams.rs @@ -20,7 +20,10 @@ fn send(pair: &mut Pair, streams: usize, priorities: usize, writes: usize, size: let target = pair.server.received.finished + streams as u64; let conn = pair.client(); for index in 0..streams { - let id = conn.streams().open(Dir::Uni).expect("stream limit"); + let id = conn + .streams() + .open(Dir::Uni, Default::default()) + .expect("stream limit"); let mut stream = conn.send_stream(id); // `as _` follows the priority type, which a scheduler rewrite widens. stream.set_priority((index % priorities) as _).unwrap(); diff --git a/rs/moq-quic/benches/wire.rs b/rs/moq-quic/benches/wire.rs index a651516fe9f..da0a5b92ea2 100644 --- a/rs/moq-quic/benches/wire.rs +++ b/rs/moq-quic/benches/wire.rs @@ -50,7 +50,7 @@ fn queue(pair: &mut Pair, packets: usize, frames: usize) -> usize { let mut datagrams = conn.datagrams(); let empty = datagrams.send_buffer_space(); for _ in 0..packets * frames { - datagrams.send(data.clone(), None, 0).unwrap(); + datagrams.send(data.clone(), Default::default()).unwrap(); } empty } diff --git a/rs/moq-quic/src/connection/datagrams.rs b/rs/moq-quic/src/connection/datagrams.rs index 6cd3222f727..374625aa674 100644 --- a/rs/moq-quic/src/connection/datagrams.rs +++ b/rs/moq-quic/src/connection/datagrams.rs @@ -4,7 +4,7 @@ use bytes::Bytes; use thiserror::Error; use tracing::{debug, trace}; -use super::{Connection, SendGroup}; +use super::{Connection, SendOptions}; use crate::{ TransportError, frame::{Datagram, FrameStruct}, @@ -16,28 +16,23 @@ pub struct Datagrams<'a> { } impl Datagrams<'_> { - /// Queue an unreliable, unordered datagram in `group` at `order` + /// Queue an unreliable datagram, scheduled by `options` /// - /// The datagram is scheduled like one of the group's streams at priority `order`: it waits - /// behind higher orders, goes ahead of a stream of equal order, and spends the group's - /// share of bytes. Without a group, it is ordered among the ungrouped streams, which go - /// before every group. + /// The datagram is scheduled like one of its group's streams at its priority: it waits + /// behind higher priorities, goes ahead of a stream of equal priority, and spends the + /// group's share of bytes. Without a group, it is ranked among the ungrouped streams, which + /// go before every group. /// /// Sending never blocks. While queued datagrams overflow /// [`TransportConfig::datagram_send_buffer_size`](crate::TransportConfig::datagram_send_buffer_size), - /// the domain holding the most datagram bytes drops the oldest datagram of its - /// lowest-priority group, and this datagram is dropped instead once it is the one to go. + /// the oldest datagram of the lowest-priority group is dropped, and this datagram is dropped + /// instead once it is the one to go. /// - /// Returns `Err` iff a `len`-byte datagram cannot currently be sent. + /// Returns `Err` iff `data` cannot currently be sent. /// /// # Panics - /// - when `group` was opened on another connection - pub fn send( - &mut self, - data: Bytes, - group: Option<&SendGroup>, - order: i64, - ) -> Result<(), SendDatagramError> { + /// - when `options.group` was opened on another connection + pub fn send(&mut self, data: Bytes, options: SendOptions<'_>) -> Result<(), SendDatagramError> { if self.conn.config.datagram_receive_buffer_size.is_none() { return Err(SendDatagramError::Disabled); } @@ -49,11 +44,7 @@ impl Datagrams<'_> { if datagram.data.len() > max || datagram.size(true) > budget { return Err(SendDatagramError::TooLarge); } - if !self - .conn - .streams - .queue_datagram(datagram, group, order, budget) - { + if !self.conn.streams.queue_datagram(datagram, options, budget) { trace!("dropping outgoing datagram"); } Ok(()) @@ -69,7 +60,7 @@ impl Datagrams<'_> { .drop_oversized_datagrams(max_datagram_size); } - /// Compute the maximum size of datagrams that may passed to `send_datagram` + /// Compute the maximum size of datagrams that may be passed to [`send`](Self::send) /// /// Returns `None` if datagrams are unsupported by the peer or disabled locally. /// @@ -206,6 +197,7 @@ mod tests { /// Errors that can arise when sending a datagram #[derive(Debug, Error, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] +#[non_exhaustive] pub enum SendDatagramError { /// The peer does not support receiving datagrams #[error("datagrams not supported by peer")] diff --git a/rs/moq-quic/src/connection/mod.rs b/rs/moq-quic/src/connection/mod.rs index 8b06a3e3c45..4e55a1179cb 100644 --- a/rs/moq-quic/src/connection/mod.rs +++ b/rs/moq-quic/src/connection/mod.rs @@ -94,7 +94,7 @@ mod streams; pub(crate) use streams::StreamsState; pub use streams::{ Checkpoint, Chunks, ClosedStream, Credit, FinishError, ReadError, ReadableError, RecvStream, - SendDomain, SendGroup, SendGroups, SendStream, ShouldTransmit, StreamEvent, Streams, + SendGroup, SendGroups, SendOptions, SendStream, ShouldTransmit, StreamEvent, Streams, WriteError, Written, }; diff --git a/rs/moq-quic/src/connection/streams/mod.rs b/rs/moq-quic/src/connection/streams/mod.rs index 61412154aef..d97adbaae22 100644 --- a/rs/moq-quic/src/connection/streams/mod.rs +++ b/rs/moq-quic/src/connection/streams/mod.rs @@ -42,10 +42,14 @@ impl<'a> Streams<'a> { Self { state, conn_state } } - /// Open a single stream if possible + /// Open a single stream if possible, its sending half scheduled by `options` /// /// Returns `None` if the streams in the given direction are currently exhausted. - pub fn open(&mut self, dir: Dir) -> Option { + /// + /// # Panics + /// - when `options.group` was opened on another connection + pub fn open(&mut self, dir: Dir, options: SendOptions<'_>) -> Option { + let group = options.group.map(|g| g.index(self.state.tag)); if self.conn_state.is_closed() { return None; } @@ -60,6 +64,21 @@ impl<'a> Streams<'a> { let id = StreamId::new(self.state.side, dir, self.state.next[dir as usize] - 1); self.state.insert_local(id); self.state.send_streams += 1; + + // Send state is allocated on first use, so the default schedule needs none. + if group.is_some() || options.priority != 0 { + let max_send_data = self.state.max_send_data(id); + let stream = self + .state + .send + .get_mut(&id) + .unwrap() + .insert(Send::new(max_send_data)); + stream.priority = options.priority; + self.state + .pending + .join(id, &mut stream.slot, group, stream.priority); + } Some(id) } @@ -518,25 +537,35 @@ impl Checkpoint { /// Scheduling of outgoing streams between send groups /// -/// A send group shares bytes with the other groups of its domain: a higher priority group -/// preempts every lower one, and groups of equal priority get an equal share of bytes no -/// matter how many streams each has queued. Domains share bytes equally whatever their -/// groups' priorities. Streams outside any group go before all of it. A group's datagrams, -/// from [`Datagrams::send`](crate::Datagrams::send), share its bytes in the same order. +/// A higher priority group preempts every lower one, and groups of equal priority get an +/// equal share of bytes no matter how many streams each has queued. Streams outside any group +/// go before all of it. A group's datagrams, from [`Datagrams::send`](crate::Datagrams::send), +/// share its bytes in the same order. pub struct SendGroups<'a> { pub(super) state: &'a mut StreamsState, } impl SendGroups<'_> { - /// Open a send group at `priority` in `domain`, or in the default domain + /// Open a send group at `priority` + pub fn open(&mut self, priority: i64) -> SendGroup { + SendGroup { + index: self.state.pending.open_group(priority, None), + tag: self.state.tag, + } + } + + /// Open a send group at `priority` in `domain` /// /// # Panics /// - when `domain` was opened on another connection - pub fn open(&mut self, priority: i64, domain: Option<&SendDomain>) -> SendGroup { + #[cfg(test)] + pub(crate) fn open_in(&mut self, priority: i64, domain: &SendDomain) -> SendGroup { let tag = self.state.tag; - let domain = domain.map(|d| d.index(tag)); SendGroup { - index: self.state.pending.open_group(priority, domain), + index: self + .state + .pending + .open_group(priority, Some(domain.index(tag))), tag, } } @@ -570,7 +599,8 @@ impl SendGroups<'_> { } /// Open a domain, which gets an equal share of bytes with every other domain - pub fn open_domain(&mut self) -> SendDomain { + #[cfg(test)] + pub(crate) fn open_domain(&mut self) -> SendDomain { SendDomain { index: self.state.pending.open_domain(), tag: self.state.tag, @@ -581,30 +611,30 @@ impl SendGroups<'_> { /// /// # Panics /// - when `domain` was opened on another connection - pub fn close_domain(&mut self, domain: SendDomain) { + #[cfg(test)] + pub(crate) fn close_domain(&mut self, domain: SendDomain) { let index = domain.index(self.state.tag); self.state.pending.close_domain(index); } } impl StreamsState { - /// Queue a datagram in `group` at `order`, within `budget` bytes of queued datagrams. + /// Queue a datagram scheduled by `options`, within `budget` bytes of queued datagrams. /// /// Returns whether it stayed queued rather than being evicted itself. /// /// # Panics - /// - when `group` was opened on another connection + /// - when `options.group` was opened on another connection pub(crate) fn queue_datagram( &mut self, datagram: frame::Datagram, - group: Option<&SendGroup>, - order: i64, + options: SendOptions<'_>, budget: usize, ) -> bool { - let group = group.map(|g| g.index(self.tag)); + let group = options.group.map(|g| g.index(self.tag)); let frame = datagram.size(true); self.pending - .push_datagram(group, order, datagram, frame, budget) + .push_datagram(group, options.priority, datagram, frame, budget) } /// Drop queued datagrams with a payload over `max_payload` bytes, returning whether any was. @@ -628,6 +658,36 @@ impl StreamsState { } } +/// How an outgoing stream or datagram is scheduled, like W3C WebTransport's send options +/// +/// The default is ungrouped at priority 0. Generic so the drivers can carry their own group +/// handle in the same shape. +#[derive(Debug)] +pub struct SendOptions<'a, G = SendGroup> { + /// The send group to join, or `None` for the ungrouped streams that go before every group + pub group: Option<&'a G>, + /// The priority within the group: higher goes first, and a datagram goes ahead of a + /// stream of equal priority + pub priority: i64, +} + +impl Default for SendOptions<'_, G> { + fn default() -> Self { + Self { + group: None, + priority: 0, + } + } +} + +impl Clone for SendOptions<'_, G> { + fn clone(&self) -> Self { + *self + } +} + +impl Copy for SendOptions<'_, G> {} + /// A send group, from [`SendGroups::open`] /// /// Release it with [`SendGroups::close`] on the connection that opened it. @@ -648,13 +708,15 @@ impl SendGroup { /// A domain of send groups, from [`SendGroups::open_domain`] /// /// Release it with [`SendGroups::close_domain`] on the connection that opened it. +#[cfg(test)] #[derive(Debug, PartialEq, Eq)] #[must_use = "a domain lives until it is closed"] -pub struct SendDomain { +pub(crate) struct SendDomain { index: usize, tag: ConnectionTag, } +#[cfg(test)] impl SendDomain { fn index(&self, tag: ConnectionTag) -> usize { assert_eq!(self.tag, tag, "send domain used on another connection"); diff --git a/rs/moq-quic/src/connection/streams/saturated.rs b/rs/moq-quic/src/connection/streams/saturated.rs index 0ae52976672..140d3f916a1 100644 --- a/rs/moq-quic/src/connection/streams/saturated.rs +++ b/rs/moq-quic/src/connection/streams/saturated.rs @@ -7,7 +7,7 @@ use std::collections::HashMap; use bytes::{BufMut, Bytes, BytesMut}; use super::state::StreamsState; -use super::{SendDomain, SendGroup, SendGroups, SendStream, Streams}; +use super::{SendDomain, SendGroup, SendGroups, SendOptions, SendStream, Streams}; use crate::{ Dir, Side, StreamId, VarInt, connection::{State as ConnState, spaces::Retransmits}, @@ -90,7 +90,7 @@ impl Sender { } fn group(&mut self, priority: i64) -> SendGroup { - self.groups().open(priority, None) + self.groups().open(priority) } fn send(&mut self, id: StreamId) -> SendStream<'_> { @@ -108,11 +108,8 @@ impl Sender { state: &mut self.state, conn_state: &self.conn, } - .open(Dir::Uni) + .open(Dir::Uni, SendOptions { group, priority }) .unwrap(); - let mut stream = self.send(id); - stream.set_group(group).unwrap(); - stream.set_priority(priority).unwrap(); self.write(id, bytes); id } @@ -121,8 +118,8 @@ impl Sender { assert_eq!(self.send(id).write(&vec![0; bytes]), Ok(bytes)); } - /// Queue a `bytes` payload datagram in `group` at `order`, returning its tag. - fn datagram(&mut self, group: Option<&SendGroup>, order: i64, bytes: usize) -> u64 { + /// Queue a `bytes` payload datagram in `group` at `priority`, returning its tag. + fn datagram(&mut self, group: Option<&SendGroup>, priority: i64, bytes: usize) -> u64 { let tag = self.tag; self.tag += 1; let mut data = BytesMut::with_capacity(bytes); @@ -133,7 +130,7 @@ impl Sender { }; assert!( self.state - .queue_datagram(datagram, group, order, usize::MAX) + .queue_datagram(datagram, SendOptions { group, priority }, usize::MAX) ); tag } @@ -451,13 +448,13 @@ fn domains_share_bytes_and_confine_priority() { let quiet = sender.groups().open_domain(); let crowded_groups: Vec<_> = (0..10) - .map(|priority| sender.groups().open(priority, Some(&crowded))) + .map(|priority| sender.groups().open_in(priority, &crowded)) .collect(); let crowded_streams: Vec<_> = crowded_groups .iter() .map(|group| sender.open(Some(group), 0, 1_000_000)) .collect(); - let quiet_group = sender.groups().open(-100, Some(&quiet)); + let quiet_group = sender.groups().open_in(-100, &quiet); let quiet_stream = sender.open(Some(&quiet_group), 0, 1_000_000); sender.drain(200); @@ -475,7 +472,7 @@ fn domains_share_bytes_and_confine_priority() { fn the_default_domain_is_one_share() { let mut sender = Sender::new(); let domain = sender.groups().open_domain(); - let inside = sender.groups().open(0, Some(&domain)); + let inside = sender.groups().open_in(0, &domain); let outside: Vec = (0..3).map(|_| sender.group(0)).collect(); let inside = sender.open(Some(&inside), 0, 1_000_000); let outside: Vec<_> = outside @@ -493,7 +490,7 @@ fn the_default_domain_is_one_share() { fn reset_frees_a_released_group() { let mut sender = Sender::new(); let domain: SendDomain = sender.groups().open_domain(); - let group = sender.groups().open(0, Some(&domain)); + let group = sender.groups().open_in(0, &domain); let reset = sender.open(Some(&group), 0, 10_000); let finished = sender.open(Some(&group), 1, 100); sender.send(finished).finish().unwrap(); @@ -676,8 +673,8 @@ fn saturated_datagram_domains_share_bytes() { let mut sender = Sender::new(); let small = sender.groups().open_domain(); let large = sender.groups().open_domain(); - let small = sender.groups().open(0, Some(&small)); - let large = sender.groups().open(9, Some(&large)); + let small = sender.groups().open_in(0, &small); + let large = sender.groups().open_in(9, &large); let small: Vec<_> = (0..2_000) .map(|seq| sender.datagram(Some(&small), seq, 50)) .collect(); diff --git a/rs/moq-quic/src/connection/streams/schedule.rs b/rs/moq-quic/src/connection/streams/schedule.rs index a4c2499e672..1b73fdd08a5 100644 --- a/rs/moq-quic/src/connection/streams/schedule.rs +++ b/rs/moq-quic/src/connection/streams/schedule.rs @@ -291,11 +291,13 @@ impl Schedule { } /// Create a domain, sharing bytes equally with every other domain. + #[cfg(test)] pub(super) fn open_domain(&mut self) -> usize { self.domains.insert(Domain::new()) } /// Release a domain; it is freed once its last group is. + #[cfg(test)] pub(super) fn close_domain(&mut self, domain: usize) { assert_ne!(domain, DEFAULT_DOMAIN, "the default domain is never closed"); self.domains[domain].closed = true; diff --git a/rs/moq-quic/src/connection/streams/state.rs b/rs/moq-quic/src/connection/streams/state.rs index 6c1f57c8d96..e3126bd235c 100644 --- a/rs/moq-quic/src/connection/streams/state.rs +++ b/rs/moq-quic/src/connection/streams/state.rs @@ -1191,7 +1191,7 @@ mod tests { state: &mut server, conn_state: &state, } - .open(Dir::Uni) + .open(Dir::Uni, Default::default()) .unwrap(); SendStream { id, @@ -1258,7 +1258,7 @@ mod tests { state: &mut server, conn_state: &state, } - .open(Dir::Uni) + .open(Dir::Uni, Default::default()) .unwrap(); SendStream { id, @@ -1616,7 +1616,7 @@ mod tests { state: &mut server, conn_state: &state, } - .open(Dir::Uni) + .open(Dir::Uni, Default::default()) .unwrap(); let mut stream = SendStream { @@ -1679,9 +1679,9 @@ mod tests { conn_state: &state, }; - let id_high = streams.open(Dir::Bi).unwrap(); - let id_mid = streams.open(Dir::Bi).unwrap(); - let id_low = streams.open(Dir::Bi).unwrap(); + let id_high = streams.open(Dir::Bi, Default::default()).unwrap(); + let id_mid = streams.open(Dir::Bi, Default::default()).unwrap(); + let id_low = streams.open(Dir::Bi, Default::default()).unwrap(); let mut mid = SendStream { id: id_mid, @@ -1735,8 +1735,8 @@ mod tests { state: &mut server, conn_state: &state, }; - let id_low = streams.open(Dir::Bi).unwrap(); - let id_high = streams.open(Dir::Bi).unwrap(); + let id_low = streams.open(Dir::Bi, Default::default()).unwrap(); + let id_high = streams.open(Dir::Bi, Default::default()).unwrap(); let mut low = SendStream { id: id_low, @@ -1797,8 +1797,8 @@ mod tests { conn_state: &state, }; - let id_high = streams.open(Dir::Bi).unwrap(); - let id_mid = streams.open(Dir::Bi).unwrap(); + let id_high = streams.open(Dir::Bi, Default::default()).unwrap(); + let id_mid = streams.open(Dir::Bi, Default::default()).unwrap(); let mut mid = SendStream { id: id_mid, @@ -1866,9 +1866,9 @@ mod tests { }; // a, b and c all have the same priority - let id_a = streams.open(Dir::Bi).unwrap(); - let id_b = streams.open(Dir::Bi).unwrap(); - let id_c = streams.open(Dir::Bi).unwrap(); + let id_a = streams.open(Dir::Bi, Default::default()).unwrap(); + let id_b = streams.open(Dir::Bi, Default::default()).unwrap(); + let id_c = streams.open(Dir::Bi, Default::default()).unwrap(); let mut stream_a = SendStream { id: id_a, @@ -1948,9 +1948,9 @@ mod tests { }; // a, and b have the same priority, c has higher priority - let id_a = streams.open(Dir::Bi).unwrap(); - let id_b = streams.open(Dir::Bi).unwrap(); - let id_c = streams.open(Dir::Bi).unwrap(); + let id_a = streams.open(Dir::Bi, Default::default()).unwrap(); + let id_b = streams.open(Dir::Bi, Default::default()).unwrap(); + let id_c = streams.open(Dir::Bi, Default::default()).unwrap(); let mut stream_a = SendStream { id: id_a, @@ -2055,7 +2055,7 @@ mod tests { conn_state: &state, }; - let id = streams.open(Dir::Uni).unwrap(); + let id = streams.open(Dir::Uni, Default::default()).unwrap(); let mut stream = SendStream { id, state: &mut server, @@ -2502,7 +2502,7 @@ mod tests { state: &mut server, conn_state: &conn_state, } - .open(Dir::Uni) + .open(Dir::Uni, Default::default()) .expect("should be able to open a stream"); let mut stream = SendStream { @@ -2577,7 +2577,7 @@ mod tests { state: &mut server, conn_state: &conn_state, } - .open(Dir::Uni) + .open(Dir::Uni, Default::default()) .expect("should be able to open a stream"); let mut stream = SendStream { @@ -2907,7 +2907,7 @@ mod tests { state: &mut server, conn_state: &state, } - .open(Dir::Uni) + .open(Dir::Uni, Default::default()) .unwrap(); let mut stream = SendStream { id, @@ -2951,7 +2951,7 @@ mod tests { state: &mut server, conn_state: &state, } - .open(Dir::Uni) + .open(Dir::Uni, Default::default()) .unwrap(); let mut stream = SendStream { id, @@ -2980,7 +2980,7 @@ mod tests { state: &mut server, conn_state: &state, } - .open(Dir::Uni) + .open(Dir::Uni, Default::default()) .unwrap(); let mut stream = SendStream { id, @@ -3023,7 +3023,7 @@ mod tests { state: &mut server, conn_state: &state, } - .open(Dir::Uni) + .open(Dir::Uni, Default::default()) .unwrap(); let mut stream = SendStream { id, @@ -3056,7 +3056,7 @@ mod tests { state: &mut server, conn_state: &state, } - .open(Dir::Uni) + .open(Dir::Uni, Default::default()) .unwrap(); let mut stream = SendStream { id, @@ -3097,7 +3097,7 @@ mod tests { state: &mut server, conn_state: &state, } - .open(Dir::Uni) + .open(Dir::Uni, Default::default()) .unwrap(); let mut stream = SendStream { id, @@ -3272,8 +3272,8 @@ mod tests { fn send_group_from_another_connection() { let mut a = make(Side::Client); let mut b = make(Side::Client); - let group = crate::SendGroups { state: &mut a }.open(0, None); - let _other = crate::SendGroups { state: &mut b }.open(0, None); + let group = crate::SendGroups { state: &mut a }.open(0); + let _other = crate::SendGroups { state: &mut b }.open(0); crate::SendGroups { state: &mut b }.set_priority(&group, 1); } @@ -3284,7 +3284,7 @@ mod tests { let mut b = make(Side::Client); let domain = crate::SendGroups { state: &mut a }.open_domain(); let _other = crate::SendGroups { state: &mut b }.open_domain(); - let _ = crate::SendGroups { state: &mut b }.open(0, Some(&domain)); + let _ = crate::SendGroups { state: &mut b }.open_in(0, &domain); } /// Another connection holding at least as much credit still refuses it. diff --git a/rs/moq-quic/src/fuzz.rs b/rs/moq-quic/src/fuzz.rs index 2f42de6f67e..cb1b060267c 100644 --- a/rs/moq-quic/src/fuzz.rs +++ b/rs/moq-quic/src/fuzz.rs @@ -180,7 +180,7 @@ pub fn streams(data: &[u8]) -> bool { }; match op { StreamOp::Open => { - Streams::new(&mut state, &conn_state).open(dir); + Streams::new(&mut state, &conn_state).open(dir, Default::default()); } StreamOp::Accept { uni } => { let dir = if uni { Dir::Uni } else { Dir::Bi }; diff --git a/rs/moq-quic/src/fuzz/connection.rs b/rs/moq-quic/src/fuzz/connection.rs index 6c419ad0bc8..3c32dc34e0d 100644 --- a/rs/moq-quic/src/fuzz/connection.rs +++ b/rs/moq-quic/src/fuzz/connection.rs @@ -138,7 +138,7 @@ enum Op { Datagram { who: Who, len: u16, - order: i8, + priority: i8, }, Ping { who: Who, @@ -443,7 +443,10 @@ impl Pair { Op::Open { who, uni } => { let peer = self.peer(who); let dir = dir(uni); - let Some(id) = peer.conn().and_then(|conn| conn.streams().open(dir)) else { + let Some(id) = peer + .conn() + .and_then(|conn| conn.streams().open(dir, Default::default())) + else { return; }; peer.send.push(id); @@ -554,7 +557,7 @@ impl Pair { conn.set_send_window(window.into()); } } - Op::Datagram { who, len, order } => { + Op::Datagram { who, len, priority } => { let peer = self.peer(who); let seq = peer.datagrams_sent.len() as u64; let Some(conn) = peer.conn() else { @@ -566,7 +569,17 @@ impl Pair { .chain((0..len).map(|i| datagram_byte(seq, i.into()))) .collect(); let size = data.len(); - if conn.datagrams().send(data, None, order.into()).is_ok() { + if conn + .datagrams() + .send( + data, + crate::SendOptions { + group: None, + priority: priority.into(), + }, + ) + .is_ok() + { peer.datagrams_sent.push(size); } } @@ -820,7 +833,7 @@ pub(super) fn datagrams() -> Vec> { Op::Datagram { who: Who::Server, len: 10, - order: 0, + priority: 0, }, Op::KeyUpdate { who: Who::Client }, Op::Ping { who: Who::Client }, diff --git a/rs/moq-quic/src/lib.rs b/rs/moq-quic/src/lib.rs index 1bbf7520997..b6e978a01bb 100644 --- a/rs/moq-quic/src/lib.rs +++ b/rs/moq-quic/src/lib.rs @@ -55,7 +55,7 @@ mod connection; pub use crate::connection::{ Checkpoint, Chunk, Chunks, ClosedStream, Connection, ConnectionError, ConnectionStats, Credit, CreditStats, Datagrams, Event, FinishError, FrameStats, PathStats, ReadError, ReadableError, - RecvStream, RttEstimator, SendDatagramError, SendDomain, SendGroup, SendGroups, SendStream, + RecvStream, RttEstimator, SendDatagramError, SendGroup, SendGroups, SendOptions, SendStream, ShouldTransmit, StreamEvent, Streams, UdpStats, WriteError, Written, }; #[cfg(feature = "qlog")] diff --git a/rs/moq-quic/src/tests/characterize/limits.rs b/rs/moq-quic/src/tests/characterize/limits.rs index 6fe929ba6ae..14fd1852bfe 100644 --- a/rs/moq-quic/src/tests/characterize/limits.rs +++ b/rs/moq-quic/src/tests/characterize/limits.rs @@ -6,7 +6,7 @@ use super::*; /// How many more uni streams the client can open right now, opening them all fn openable(pair: &mut Pair, ch: ConnectionHandle) -> Vec { - iter::from_fn(|| pair.client_streams(ch).open(Dir::Uni)).collect() + iter::from_fn(|| pair.client_streams(ch).open(Dir::Uni, Default::default())).collect() } /// Finishes each client stream and has the server read it to the end @@ -77,7 +77,10 @@ fn max_concurrent_streams_lowered() { /// How many bytes a fresh client stream can write, up to `limit` fn writable(pair: &mut Pair, ch: ConnectionHandle, limit: usize) -> usize { - let s = pair.client_streams(ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(ch) + .open(Dir::Uni, Default::default()) + .unwrap(); pair.client_send(ch, s).write(&vec![0; limit]).unwrap_or(0) } @@ -200,7 +203,10 @@ fn zero_rtt_window_raise(dir: Dir) { pair.server.set_server_config(Some(Arc::new(server(NEW)))); let client_ch = pair.begin_connect(config); assert!(pair.client_conn_mut(client_ch).has_0rtt()); - let s = pair.client_streams(client_ch).open(dir).unwrap(); + let s = pair + .client_streams(client_ch) + .open(dir, Default::default()) + .unwrap(); let mut written = pair.client_send(client_ch, s).write(&[0; LEN]).unwrap(); assert_eq!(written, OLD as usize); diff --git a/rs/moq-quic/src/tests/characterize/paths.rs b/rs/moq-quic/src/tests/characterize/paths.rs index 2e0bc284ee2..f8f6283ebb7 100644 --- a/rs/moq-quic/src/tests/characterize/paths.rs +++ b/rs/moq-quic/src/tests/characterize/paths.rs @@ -155,7 +155,10 @@ fn duplicate_packet_is_discarded() { let _guard = subscribe(); let (mut pair, client_ch, server_ch) = connected(); pair.drive(); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); pair.client_send(client_ch, s).write(MSG).unwrap(); pair.drive_client(); assert_eq!(pair.server.inbound.len(), 1); @@ -297,7 +300,10 @@ fn zero_rtt_retransmit_after_retry() { let client_ch = pair.begin_connect(config); assert!(pair.client_conn_mut(client_ch).has_0rtt()); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); pair.client_send(client_ch, s).write(MSG).unwrap(); pair.drive(); @@ -344,7 +350,10 @@ fn path_validation_failure_reverts() { assert_matches!(updates.as_slice(), [.., Event::PathUpdated]); // The connection still works on the old path - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); pair.client_send(client_ch, s).write(MSG).unwrap(); pair.drive(); assert_eq!(pair.server_streams(server_ch).accept(Dir::Uni), Some(s)); diff --git a/rs/moq-quic/src/tests/characterize/reset.rs b/rs/moq-quic/src/tests/characterize/reset.rs index 9128f01c495..230b8495d4a 100644 --- a/rs/moq-quic/src/tests/characterize/reset.rs +++ b/rs/moq-quic/src/tests/characterize/reset.rs @@ -27,7 +27,10 @@ fn open( server_ch: ConnectionHandle, data: &[u8], ) -> StreamId { - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); pair.client_send(client_ch, s).write(data).unwrap(); pair.drive(); assert_eq!(pair.server_streams(server_ch).accept(Dir::Uni), Some(s)); @@ -177,11 +180,19 @@ fn reset_returns_stream_credit_once_observed() { let s = open(&mut pair, client_ch, server_ch, b"hello"); pair.client_send(client_ch, s).reset(CODE).unwrap(); pair.drive(); - assert_eq!(pair.client_streams(client_ch).open(Dir::Uni), None); + assert_eq!( + pair.client_streams(client_ch) + .open(Dir::Uni, Default::default()), + None + ); assert_eq!(read_all(&mut pair, server_ch, s).1, ReadError::Reset(CODE)); pair.drive(); - assert!(pair.client_streams(client_ch).open(Dir::Uni).is_some()); + assert!( + pair.client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .is_some() + ); } /// The connection window used by `s`'s unread bytes, which fill it @@ -193,7 +204,10 @@ fn blocked() -> (Pair, ConnectionHandle, ConnectionHandle, StreamId) { connected_with(|t| _ = t.receive_window(VarInt::from_u32(WINDOW)), |_| {}); let s = open(&mut pair, client_ch, server_ch, &[0; WINDOW as usize]); // The window is spent, so nothing more can be written - let other = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let other = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); assert_eq!( pair.client_send(client_ch, other).write(b"x"), Err(WriteError::Blocked) @@ -203,7 +217,10 @@ fn blocked() -> (Pair, ConnectionHandle, ConnectionHandle, StreamId) { /// How many bytes a fresh client stream can write fn writable(pair: &mut Pair, ch: ConnectionHandle) -> usize { - let s = pair.client_streams(ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(ch) + .open(Dir::Uni, Default::default()) + .unwrap(); pair.client_send(ch, s) .write(&[0; 2 * WINDOW as usize]) .unwrap_or(0) diff --git a/rs/moq-quic/src/tests/characterize/schedule.rs b/rs/moq-quic/src/tests/characterize/schedule.rs index 1c13c5029a8..4dcd242601e 100644 --- a/rs/moq-quic/src/tests/characterize/schedule.rs +++ b/rs/moq-quic/src/tests/characterize/schedule.rs @@ -43,7 +43,10 @@ fn assert_golden(pair: &mut Pair, golden: &str) { /// Opens a client uni stream at `priority` and queues `LEN` bytes and a FIN on it fn queue(pair: &mut Pair, ch: ConnectionHandle, priority: i64) -> StreamId { - let s = pair.client_streams(ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(ch) + .open(Dir::Uni, Default::default()) + .unwrap(); pair.client_send(ch, s).set_priority(priority).unwrap(); assert_eq!(pair.client_send(ch, s).write(&[0; LEN]).unwrap(), LEN); pair.client_send(ch, s).finish().unwrap(); @@ -156,12 +159,12 @@ fn datagrams_interleaved() { queue(&mut pair, ch, 0); for _ in 0..3 { pair.client_datagrams(ch) - .send(vec![0; 500].into(), None, 0) + .send(vec![0; 500].into(), Default::default()) .unwrap(); } queue(&mut pair, ch, 0); pair.client_datagrams(ch) - .send(vec![0; 1000].into(), None, 0) + .send(vec![0; 1000].into(), Default::default()) .unwrap(); assert_golden( &mut pair, @@ -183,7 +186,10 @@ fn datagrams_interleaved() { fn retransmit_within_stream() { let _guard = subscribe(); let (mut pair, ch) = idle(true); - let s = pair.client_streams(ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(ch) + .open(Dir::Uni, Default::default()) + .unwrap(); assert_eq!(pair.client_send(ch, s).write(&[0; 16_000]).unwrap(), 16_000); // Lose the second packet of the first flight diff --git a/rs/moq-quic/src/tests/mod.rs b/rs/moq-quic/src/tests/mod.rs index 91b1cec7e16..b0ded60c966 100644 --- a/rs/moq-quic/src/tests/mod.rs +++ b/rs/moq-quic/src/tests/mod.rs @@ -593,7 +593,10 @@ fn finish_stream_simple() { let mut pair = Pair::default(); let (client_ch, server_ch) = pair.connect(); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); const MSG: &[u8] = b"hello"; pair.client_send(client_ch, s).write(MSG).unwrap(); @@ -637,7 +640,10 @@ fn reset_stream() { let mut pair = Pair::default(); let (client_ch, server_ch) = pair.connect(); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); const MSG: &[u8] = b"hello"; pair.client_send(client_ch, s).write(MSG).unwrap(); @@ -666,7 +672,10 @@ fn stop_stream() { let mut pair = Pair::default(); let (client_ch, server_ch) = pair.connect(); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); const MSG: &[u8] = b"hello"; pair.client_send(client_ch, s).write(MSG).unwrap(); pair.drive(); @@ -786,7 +795,10 @@ fn congestion() { const TARGET: u64 = 2048; assert!(pair.client_conn_mut(client_ch).congestion_window() > TARGET); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); // Send data without receiving ACKs until the congestion state falls below target while pair.client_conn_mut(client_ch).congestion_window() > TARGET { let n = pair.client_send(client_ch, s).write(&[42; 1024]).unwrap(); @@ -822,7 +834,10 @@ fn full_initial_window() { let mtu = u64::from(INITIAL_MTU); assert_eq!(window % mtu, 0, "window must be exactly fillable"); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); let data = vec![42; 2 * window as usize]; assert_eq!( pair.client_send(client_ch, s).write(&data), @@ -871,7 +886,10 @@ fn zero_rtt_happypath() { info!("resuming session"); let client_ch = pair.begin_connect(config); assert!(pair.client_conn_mut(client_ch).has_0rtt()); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); const MSG: &[u8] = b"Hello, 0-RTT!"; pair.client_send(client_ch, s).write(MSG).unwrap(); pair.drive(); @@ -970,7 +988,10 @@ fn zero_rtt_rejection() { info!("resuming session"); let client_ch = pair.begin_connect(client_config); assert!(pair.client_conn_mut(client_ch).has_0rtt()); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); const MSG: &[u8] = b"Hello, 0-RTT!"; pair.client_conn_mut(client_ch) .set_send_window(MSG.len() as u64); @@ -991,7 +1012,10 @@ fn zero_rtt_rejection() { Some(Event::Connected) ); assert_matches!(pair.server_conn_mut(server_ch).poll(), None); - let s2 = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s2 = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); assert_eq!(s, s2); // The server discarded its 0-RTT keys, so it never decrypted the STREAM frame and has no @@ -1053,7 +1077,10 @@ fn test_zero_rtt_incoming_limit(configure_server: pair.server.handle_incoming = Box::new(|_| IncomingConnectionBehavior::Wait); let client_ch = pair.begin_connect(config); assert!(pair.client_conn_mut(client_ch).has_0rtt()); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); pair.client_send(client_ch, s) .write(&vec![0; CLIENT_WRITES]) .unwrap(); @@ -1419,7 +1446,11 @@ fn late_peer_params_make_streams_available() { pair.server.drive_incoming(pair.time, pair.client.addr); let incoming = pair.server.pop_waiting_incoming(); let server_ch = pair.server.try_accept(incoming, pair.time).unwrap(); - assert_eq!(pair.server_streams(server_ch).open(Dir::Uni), None); + assert_eq!( + pair.server_streams(server_ch) + .open(Dir::Uni, Default::default()), + None + ); pair.drive(); assert!( @@ -1428,7 +1459,11 @@ fn late_peer_params_make_streams_available() { Event::Stream(StreamEvent::Available { dir: Dir::Uni }) )) ); - assert!(pair.server_streams(server_ch).open(Dir::Uni).is_some()); + assert!( + pair.server_streams(server_ch) + .open(Dir::Uni, Default::default()) + .is_some() + ); } #[test] @@ -1450,10 +1485,11 @@ fn stream_id_limit() { .get_mut(&client_ch) .unwrap() .streams() - .open(Dir::Uni) + .open(Dir::Uni, Default::default()) .expect("couldn't open first stream"); assert_eq!( - pair.client_streams(client_ch).open(Dir::Uni), + pair.client_streams(client_ch) + .open(Dir::Uni, Default::default()), None, "only one stream is permitted at a time" ); @@ -1467,7 +1503,8 @@ fn stream_id_limit() { Some(Event::Stream(StreamEvent::Finished { id })) if id == s ); assert_eq!( - pair.client_streams(client_ch).open(Dir::Uni), + pair.client_streams(client_ch) + .open(Dir::Uni, Default::default()), None, "server does not immediately grant additional credit" ); @@ -1505,7 +1542,7 @@ fn stream_id_limit() { .get_mut(&client_ch) .unwrap() .streams() - .open(Dir::Uni) + .open(Dir::Uni, Default::default()) .expect("didn't get stream id budget"); pair.client_send(client_ch, s).finish().unwrap(); pair.drive(); @@ -1543,9 +1580,13 @@ fn streams_blocked() { // Use up the only stream slot, then try to open another let s = pair .client_streams(client_ch) - .open(Dir::Uni) + .open(Dir::Uni, Default::default()) .expect("first uni stream"); - assert_eq!(pair.client_streams(client_ch).open(Dir::Uni), None); + assert_eq!( + pair.client_streams(client_ch) + .open(Dir::Uni, Default::default()), + None + ); // Send data so the STREAMS_BLOCKED piggybacks on an outgoing packet pair.client_send(client_ch, s).write(b"hi").unwrap(); @@ -1576,7 +1617,7 @@ fn streams_blocked_not_sent_under_limit() { // Default config allows many streams; opening one should not trigger STREAMS_BLOCKED let s = pair .client_streams(client_ch) - .open(Dir::Uni) + .open(Dir::Uni, Default::default()) .expect("open stream"); pair.client_send(client_ch, s).write(b"hi").unwrap(); pair.drive(); @@ -1604,7 +1645,10 @@ fn data_blocked() { let (client_ch, server_ch) = pair.connect(); // Fill the connection-level window, then run into the limit - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); assert_eq!( pair.client_send(client_ch, s) .write(b"0123456789ab") @@ -1682,7 +1726,10 @@ fn stream_data_blocked() { let (client_ch, server_ch) = pair.connect(); // Fill the stream-level window, then run into the limit - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); assert_eq!( pair.client_send(client_ch, s) .write(b"0123456789ab") @@ -1753,7 +1800,10 @@ fn data_blocked_not_sent_under_limit() { let (client_ch, _server_ch) = pair.connect(); // Default windows are far larger than this write, so nothing is blocked - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); pair.client_send(client_ch, s).write(b"hi").unwrap(); pair.drive(); @@ -1770,7 +1820,10 @@ fn data_blocked_not_sent_for_local_send_window() { // Running out of our own send window is not the peer's flow control limit pair.client_conn_mut(client_ch).set_send_window(5); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); assert_eq!( pair.client_send(client_ch, s).write(b"0123456789").unwrap(), 5 @@ -1803,7 +1856,10 @@ fn data_blocked_dropped_when_limit_raised() { let mut pair = Pair::new(Default::default(), server); let (client_ch, server_ch) = pair.connect(); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); assert_eq!( pair.client_send(client_ch, s) .write(b"0123456789ab") @@ -1843,7 +1899,10 @@ fn stream_data_blocked_not_sent_after_reset() { let mut pair = Pair::new(Default::default(), server); let (client_ch, _server_ch) = pair.connect(); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); assert_eq!( pair.client_send(client_ch, s) .write(b"0123456789ab") @@ -1877,7 +1936,10 @@ fn data_blocked_retransmit() { let mut pair = Pair::new(Default::default(), server); let (client_ch, server_ch) = pair.connect(); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); assert_eq!( pair.client_send(client_ch, s) .write(b"0123456789ab") @@ -1921,7 +1983,7 @@ fn key_update_simple() { .get_mut(&client_ch) .unwrap() .streams() - .open(Dir::Bi) + .open(Dir::Bi, Default::default()) .expect("couldn't open first stream"); const MSG1: &[u8] = b"hello1"; @@ -1978,7 +2040,7 @@ fn key_update_reordered() { .get_mut(&client_ch) .unwrap() .streams() - .open(Dir::Bi) + .open(Dir::Bi, Default::default()) .expect("couldn't open first stream"); const MSG1: &[u8] = b"1"; @@ -2223,7 +2285,10 @@ fn unreachable_is_loss_once_established() { assert!(!pair.client_conn_mut(client_ch).is_closed()); assert_matches!(pair.client_conn_mut(client_ch).poll(), None); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); pair.client_send(client_ch, s).write(b"hello").unwrap(); pair.drive(); assert_matches!( @@ -2268,7 +2333,10 @@ fn connection_close_while_congestion_blocked() { // Saturate the congestion window with unacknowledged stream data by transmitting from the // client without driving the server, so no ACKs come back and in-flight bytes stay pinned at // the window - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); pair.client_send(client_ch, s) .write(&[42; 1024 * 1024]) .unwrap(); @@ -2378,7 +2446,10 @@ fn test_flow_control(config: TransportConfig, window_size: usize) { let msg = vec![0xAB; window_size + 10]; // Stream reset before read - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); info!("writing"); assert_eq!(pair.client_send(client_ch, s).write_limit(), window_size); assert_eq!(pair.client_send(client_ch, s).write(&msg), Ok(window_size)); @@ -2403,7 +2474,10 @@ fn test_flow_control(config: TransportConfig, window_size: usize) { // Happy path info!("writing"); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); assert_eq!(pair.client_send(client_ch, s).write_limit(), window_size); assert_eq!(pair.client_send(client_ch, s).write(&msg), Ok(window_size)); assert_eq!(pair.client_send(client_ch, s).write_limit(), 0); @@ -2499,7 +2573,10 @@ fn stop_opens_bidi() { let mut pair = Pair::default(); let (client_ch, server_ch) = pair.connect(); assert_eq!(pair.client_streams(client_ch).send_streams(), 0); - let s = pair.client_streams(client_ch).open(Dir::Bi).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Bi, Default::default()) + .unwrap(); assert_eq!(pair.client_streams(client_ch).send_streams(), 1); const ERROR: VarInt = VarInt(42); pair.client @@ -2543,8 +2620,14 @@ fn implicit_open() { let _guard = subscribe(); let mut pair = Pair::default(); let (client_ch, server_ch) = pair.connect(); - let s1 = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); - let s2 = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s1 = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); + let s2 = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); pair.client_send(client_ch, s2).write(b"hello").unwrap(); pair.drive(); assert_matches!( @@ -2712,7 +2795,10 @@ fn finish_stream_flow_control_reordered() { let mut pair = Pair::default(); let (client_ch, server_ch) = pair.connect(); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); const MSG: &[u8] = b"hello"; pair.client_send(client_ch, s).write(MSG).unwrap(); @@ -2768,7 +2854,10 @@ fn handshake_1rtt_handling() { pair.client.delay_outbound(); // Send some 1-RTT data which will be received first. - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); const MSG: &[u8] = b"hello"; pair.client_send(client_ch, s).write(MSG).unwrap(); pair.client_send(client_ch, s).finish().unwrap(); @@ -2808,7 +2897,10 @@ fn stop_before_finish() { let mut pair = Pair::default(); let (client_ch, server_ch) = pair.connect(); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); const MSG: &[u8] = b"hello"; pair.client_send(client_ch, s).write(MSG).unwrap(); pair.drive(); @@ -2830,7 +2922,10 @@ fn stop_during_finish() { let mut pair = Pair::default(); let (client_ch, server_ch) = pair.connect(); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); const MSG: &[u8] = b"hello"; pair.client_send(client_ch, s).write(MSG).unwrap(); pair.drive(); @@ -2857,7 +2952,10 @@ fn congested_tail_loss() { const TARGET: u64 = 2048; assert!(pair.client_conn_mut(client_ch).congestion_window() > TARGET); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); // Send data without receiving ACKs until the congestion state falls below target while pair.client_conn_mut(client_ch).congestion_window() > TARGET { let n = pair.client_send(client_ch, s).write(&[42; 1024]).unwrap(); @@ -2913,7 +3011,7 @@ fn tail_loss_small_segment_size() { info!("Sending datagram batch"); for _ in 0..DGRAM_NUM { pair.client_datagrams(client_ch) - .send(vec![0; DGRAM_LEN].into(), None, 0) + .send(vec![0; DGRAM_LEN].into(), Default::default()) .unwrap(); } @@ -2966,7 +3064,7 @@ fn tail_loss_probe_keeps_ping_when_datagram_does_not_fit() { let datagram_len = pair.client_datagrams(client_ch).max_size().unwrap(); assert!(datagram_len > INITIAL_MTU as usize); pair.client_datagrams(client_ch) - .send(vec![0; datagram_len].into(), None, 0) + .send(vec![0; datagram_len].into(), Default::default()) .unwrap(); pair.drive(); @@ -3013,7 +3111,7 @@ fn tail_loss_respect_max_datagrams() { info!("Sending datagram batch"); for _ in 0..DGRAM_NUM { pair.client_datagrams(client_ch) - .send(vec![0; DGRAM_LEN].into(), None, 0) + .send(vec![0; DGRAM_LEN].into(), Default::default()) .unwrap(); } @@ -3034,7 +3132,7 @@ fn datagram_send_recv() { const DATA: &[u8] = b"whee"; pair.client_datagrams(client_ch) - .send(DATA.into(), None, 0) + .send(DATA.into(), Default::default()) .unwrap(); pair.drive(); assert_matches!( @@ -3053,15 +3151,25 @@ fn first_flight_datagrams(stream: i64, datagram: i64) -> usize { let mut pair = Pair::default(); let (client_ch, server_ch) = pair.connect(); let conn = pair.server_conn_mut(server_ch); - let streams = conn.send_groups().open(stream, None); - let datagrams = conn.send_groups().open(datagram, None); - let s = conn.streams().open(Dir::Uni).unwrap(); - conn.send_stream(s).set_group(Some(&streams)).unwrap(); + let streams = conn.send_groups().open(stream); + let datagrams = conn.send_groups().open(datagram); + let s = conn + .streams() + .open( + Dir::Uni, + SendOptions { + group: Some(&streams), + priority: 0, + }, + ) + .unwrap(); conn.send_stream(s).write(&[0; 256 * 1024]).unwrap(); for _ in 0..DATAGRAMS { - conn.datagrams() - .send(vec![1; 100].into(), Some(&datagrams), 0) - .unwrap(); + let options = SendOptions { + group: Some(&datagrams), + priority: 0, + }; + conn.datagrams().send(vec![1; 100].into(), options).unwrap(); } // One congestion-limited flight. @@ -3119,13 +3227,13 @@ fn datagram_recv_buffer_overflow() { const DATA2: &[u8] = &[0xBC; (PAYLOAD_WINDOW / 3) + 1]; const DATA3: &[u8] = &[0xCD; (PAYLOAD_WINDOW / 3) + 1]; pair.client_datagrams(client_ch) - .send(DATA1.into(), None, 0) + .send(DATA1.into(), Default::default()) .unwrap(); pair.client_datagrams(client_ch) - .send(DATA2.into(), None, 0) + .send(DATA2.into(), Default::default()) .unwrap(); pair.client_datagrams(client_ch) - .send(DATA3.into(), None, 0) + .send(DATA3.into(), Default::default()) .unwrap(); pair.drive(); assert_matches!( @@ -3137,7 +3245,7 @@ fn datagram_recv_buffer_overflow() { assert_matches!(pair.server_datagrams(server_ch).recv(), None); pair.client_datagrams(client_ch) - .send(DATA1.into(), None, 0) + .send(DATA1.into(), Default::default()) .unwrap(); pair.drive(); assert_eq!(pair.server_datagrams(server_ch).recv().unwrap(), DATA1); @@ -3156,12 +3264,12 @@ fn datagram_larger_than_send_buffer_is_too_large() { assert_matches!( pair.client_datagrams(client_ch) - .send(Bytes::from_static(&[0; 2]), None, 0), + .send(Bytes::from_static(&[0; 2]), Default::default()), Err(SendDatagramError::TooLarge) ); assert_matches!( pair.client_datagrams(client_ch) - .send(Bytes::from_static(&[0; 2]), None, 0), + .send(Bytes::from_static(&[0; 2]), Default::default()), Err(SendDatagramError::TooLarge) ); } @@ -3181,7 +3289,10 @@ fn datagram_unsupported() { assert_matches!(pair.server_conn_mut(server_ch).poll(), None); assert_matches!(pair.client_datagrams(client_ch).max_size(), None); - match pair.client_datagrams(client_ch).send(Bytes::new(), None, 0) { + match pair + .client_datagrams(client_ch) + .send(Bytes::new(), Default::default()) + { Err(SendDatagramError::UnsupportedByPeer) => {} Err(e) => panic!("unexpected error: {e}"), Ok(_) => panic!("unexpected success"), @@ -3231,7 +3342,10 @@ fn finish_acked() { let mut pair = Pair::default(); let (client_ch, server_ch) = pair.connect(); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); const MSG: &[u8] = b"hello"; pair.client_send(client_ch, s).write(MSG).unwrap(); @@ -3290,7 +3404,10 @@ fn finish_retransmit() { let mut pair = Pair::default(); let (client_ch, server_ch) = pair.connect(); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); const MSG: &[u8] = b"hello"; pair.client_send(client_ch, s).write(MSG).unwrap(); @@ -3347,7 +3464,10 @@ fn repeated_request_response() { const REQUEST: &[u8] = b"hello"; const RESPONSE: &[u8] = b"world"; for _ in 0..3 { - let s = pair.client_streams(client_ch).open(Dir::Bi).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Bi, Default::default()) + .unwrap(); pair.client_send(client_ch, s).write(REQUEST).unwrap(); pair.client_send(client_ch, s).finish().unwrap(); @@ -3861,7 +3981,10 @@ fn blackhole_after_mtu_change_repairs_itself() { // The payload will be sent in a single packet, because the detected MTU was 1444, but it will // be dropped because the link no longer supports that packet size! let payload = vec![42; 1300]; - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); pair.client_send(client_ch, s).write(&payload).unwrap(); let out_of_bounds = pair.drive_bounded(); @@ -3909,7 +4032,10 @@ fn packet_splitting_with_default_mtu() { let (client_ch, _) = pair.connect(); pair.drive(); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); pair.client_send(client_ch, s).write(&payload).unwrap(); pair.client.drive(pair.time, pair.server.addr); @@ -3930,7 +4056,10 @@ fn packet_splitting_not_necessary_after_higher_mtu_discovered() { let (client_ch, _) = pair.connect(); pair.drive(); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); pair.client_send(client_ch, s).write(&payload).unwrap(); pair.client.drive(pair.time, pair.server.addr); @@ -4471,7 +4600,7 @@ fn pure_sender_voluntarily_acks() { for _ in 0..100 { const MSG: &[u8] = b"hello"; pair.client_datagrams(client_ch) - .send(Bytes::from_static(MSG), None, 0) + .send(Bytes::from_static(MSG), Default::default()) .unwrap(); pair.drive(); assert_eq!(pair.server_datagrams(server_ch).recv().unwrap(), MSG); @@ -4580,7 +4709,10 @@ fn stream_gso() { config.transport = Arc::new(cubic_transport()); let (client_ch, _) = pair.connect_with(config); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); let initial_ios = pair.client_conn_mut(client_ch).stats().udp_tx.ios; @@ -4605,7 +4737,7 @@ fn datagram_gso() { // Sending ack-eliciting packet from server let client send ACK, which prevents // sending bundled ACK for a while. pair.server_datagrams(server_ch) - .send(Bytes::new(), None, 0) + .send(Bytes::new(), Default::default()) .unwrap(); pair.drive(); @@ -4619,7 +4751,7 @@ fn datagram_gso() { const DATAGRAMS: usize = 10; for _ in 0..DATAGRAMS { pair.client_datagrams(client_ch) - .send(Bytes::from_static(&[0; DATAGRAM_LEN]), None, 0) + .send(Bytes::from_static(&[0; DATAGRAM_LEN]), Default::default()) .unwrap(); } pair.drive(); @@ -4648,7 +4780,7 @@ fn gso_truncation() { const SIZES: [usize; 3] = [1024, 768, 768]; for len in SIZES { pair.client_datagrams(client_ch) - .send(vec![0; len].into(), None, 0) + .send(vec![0; len].into(), Default::default()) .unwrap(); } pair.drive(); @@ -4713,7 +4845,7 @@ fn min_mtu_does_not_pad_application_data() { let message = Bytes::from_static(b"hello"); pair.client_datagrams(client_ch) - .send(message.clone(), None, 0) + .send(message.clone(), Default::default()) .unwrap(); pair.client.drive(pair.time, pair.server.addr); assert_eq!(pair.client.outbound.len(), 1); @@ -4748,7 +4880,10 @@ fn min_mtu_does_not_pad_zero_rtt_batch_tail() { let client_ch = pair.begin_connect(config); assert!(pair.client_conn_mut(client_ch).has_0rtt()); - let stream = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let stream = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); let data = vec![42; 3_000]; assert_eq!( pair.client_send(client_ch, stream).write(&data), @@ -4813,7 +4948,7 @@ fn min_mtu_limits_application_loss_probes() { let now = pair.time; let client = pair.client_conn_mut(client_ch); client.handle_timeout(now); - let stream = client.streams().open(Dir::Uni).unwrap(); + let stream = client.streams().open(Dir::Uni, Default::default()).unwrap(); client.send_stream(stream).write(&[42; 3000]).unwrap(); let mut buf = Vec::new(); let transmit = client.poll_transmit(now, 1, &mut buf).unwrap(); @@ -4898,10 +5033,10 @@ fn pad_to_mtu() { const LEN_1: usize = 800; const LEN_2: usize = 600; pair.client_datagrams(client_ch) - .send(vec![0; LEN_1].into(), None, 0) + .send(vec![0; LEN_1].into(), Default::default()) .unwrap(); pair.client_datagrams(client_ch) - .send(vec![0; LEN_2].into(), None, 0) + .send(vec![0; LEN_2].into(), Default::default()) .unwrap(); pair.client.drive(pair.time, pair.server.addr); @@ -4957,7 +5092,7 @@ fn large_datagram_with_acks() { let max_size = pair.client_datagrams(client_ch).max_size().unwrap(); let msg = Bytes::from(vec![0; max_size]); pair.client_datagrams(client_ch) - .send(msg.clone(), None, 0) + .send(msg.clone(), Default::default()) .unwrap(); let initial_datagrams = pair.client_conn_mut(client_ch).stats().udp_tx.datagrams; pair.drive(); @@ -4982,7 +5117,7 @@ fn voluntary_ack_with_large_datagrams() { for _ in 0..COUNT { let max_size = pair.client_datagrams(client_ch).max_size().unwrap(); pair.client_datagrams(client_ch) - .send(vec![0; max_size].into(), None, 0) + .send(vec![0; max_size].into(), Default::default()) .unwrap(); pair.drive(); } @@ -5004,7 +5139,7 @@ fn ack_bundled_with_datagrams() { // Send packet from client and then send from server. the packet from server should include ACKs pair.client_datagrams(client_ch) - .send(vec![0; 1].into(), None, 0) + .send(vec![0; 1].into(), Default::default()) .unwrap(); pair.drive_client(); pair.drive_server(); @@ -5014,7 +5149,7 @@ fn ack_bundled_with_datagrams() { pair.server_conn_mut(server_ch).stats().udp_tx.datagrams; pair.server_datagrams(server_ch) - .send(vec![0; 1].into(), None, 0) + .send(vec![0; 1].into(), Default::default()) .unwrap(); pair.drive_server(); @@ -5054,7 +5189,7 @@ fn path_changes_unblock_oversized_datagrams() { let data = Bytes::from(vec![42; old_max]); while pair.server_datagrams(server_ch).send_buffer_space() >= old_max { pair.server_datagrams(server_ch) - .send(data.clone(), None, 0) + .send(data.clone(), Default::default()) .unwrap(); } while pair.server_conn_mut(server_ch).poll().is_some() {} @@ -5094,7 +5229,7 @@ fn path_changes_unblock_oversized_datagrams() { let small = Bytes::from_static(b"small"); pair.server_datagrams(server_ch) - .send(small.clone(), None, 0) + .send(small.clone(), Default::default()) .unwrap(); pair.drive(); assert_eq!(pair.client_datagrams(client_ch).recv(), Some(small)); @@ -5124,7 +5259,7 @@ fn oversized_datagrams_are_dropped() { let data = vec![0; max_size]; while pair.client_datagrams(client_ch).send_buffer_space() >= max_size { pair.client_datagrams(client_ch) - .send(data.clone().into(), None, 0) + .send(data.clone().into(), Default::default()) .unwrap(); } // Set the MTU to a smaller value so the queued datagrams cannot be sent. @@ -5137,7 +5272,7 @@ fn oversized_datagrams_are_dropped() { while sent.is_ok() && pair.client_datagrams(client_ch).send_buffer_space() >= max_size { sent = pair .client_datagrams(client_ch) - .send(data.clone().into(), None, 0); + .send(data.clone().into(), Default::default()); } match sent { Ok(()) => {} @@ -5556,7 +5691,10 @@ fn cwnd_limited_is_not_reported_when_only_pacing_blocks() { let mut pair = Pair::default(); let (client_ch, _) = pair.connect_with(client_cfg); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); pair.client_send(client_ch, s) .write(&[42; 64 * 1024]) .unwrap(); @@ -5646,7 +5784,10 @@ fn send_quantum_bounds_the_gso_batch() { let mut pair = Pair::default(); let (client_ch, _) = pair.connect_with(client_cfg); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); pair.client_send(client_ch, s) .write(&[42; 64 * 1024]) .unwrap(); @@ -5891,7 +6032,10 @@ fn congestion_callbacks_identify_packets_across_spaces() { pair.drive(); assert!(!pair.client_conn_mut(client_ch).is_handshaking()); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); pair.client_send(client_ch, s) .write(&[42; 16 * 1024]) .unwrap(); @@ -5982,7 +6126,10 @@ fn ce_events(log: &PacketLog) -> usize { fn old_ce_marks_report_no_new_congestion() { let _guard = subscribe(); let (mut pair, client_ch, log) = recorded_pair(Default::default()); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); pair.client_send(client_ch, s) .write(&[42; 8 * 1024]) .unwrap(); @@ -6013,7 +6160,10 @@ fn invalid_ecn_feedback_reports_no_congestion() { assert!(pair.client_conn_mut(client_ch).using_ecn()); pair.congestion_experienced = true; pair.rewrite_ecn = Some(rewrite); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); pair.client_send(client_ch, s) .write(&[42; 16 * 1024]) .unwrap(); @@ -6056,7 +6206,10 @@ fn check_starvation_reported_before_resumed_send(send: impl Fn(&mut Pair, Connec #[test] fn stream_starvation_reported_before_resumed_send() { check_starvation_reported_before_resumed_send(|pair, ch| { - let s = pair.client_streams(ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(ch) + .open(Dir::Uni, Default::default()) + .unwrap(); pair.client_send(ch, s).write(&[42; 1000]).unwrap(); }); } @@ -6065,7 +6218,7 @@ fn stream_starvation_reported_before_resumed_send() { fn datagram_starvation_reported_before_resumed_send() { check_starvation_reported_before_resumed_send(|pair, ch| { pair.client_datagrams(ch) - .send(Bytes::from_static(&[42; 1000]), None, 0) + .send(Bytes::from_static(&[42; 1000]), Default::default()) .unwrap(); }); } @@ -6081,7 +6234,10 @@ fn check_blocked_backlog_is_not_app_limited(factory: PacketRecorderFactory) { pair.drive(); let before = log.lock().unwrap().len(); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); pair.client_send(client_ch, s) .write(&[42; 64 * 1024]) .unwrap(); @@ -6119,7 +6275,10 @@ fn pacing_blocked_backlog_is_not_app_limited() { fn partial_poll_reports_starvation() { let _guard = subscribe(); let (mut pair, ch, log) = recorded_pair(Default::default()); - let stream = pair.client_streams(ch).open(Dir::Uni).unwrap(); + let stream = pair + .client_streams(ch) + .open(Dir::Uni, Default::default()) + .unwrap(); pair.client_send(ch, stream).write(&[42; 1000]).unwrap(); log.lock().unwrap().clear(); let now = pair.time; @@ -6195,7 +6354,10 @@ fn held_credit_spends_connection_window_only() { ); let chs = pair.connect(); let (client_ch, server_ch) = chs; - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); let msg = vec![0xAB; 3 * WINDOW]; let (mut sent, mut held) = (0, Credit::default()); @@ -6256,7 +6418,10 @@ fn sender_window_is_not_starvation() { let _guard = subscribe(); let (mut pair, ch, log) = recorded_pair(Default::default()); pair.client_conn_mut(ch).set_send_window(1000); - let stream = pair.client_streams(ch).open(Dir::Uni).unwrap(); + let stream = pair + .client_streams(ch) + .open(Dir::Uni, Default::default()) + .unwrap(); assert_eq!(pair.client_send(ch, stream).write(&[42; 2000]), Ok(1000)); log.lock().unwrap().clear(); pair.drive_client(); @@ -6289,7 +6454,10 @@ fn receiver_credit_is_starvation() { client_cfg.transport = Arc::new(client_transport); let (ch, _) = pair.connect_with(client_cfg); let log = factory.logs.lock().unwrap()[0].clone(); - let stream = pair.client_streams(ch).open(Dir::Uni).unwrap(); + let stream = pair + .client_streams(ch) + .open(Dir::Uni, Default::default()) + .unwrap(); assert_eq!(pair.client_send(ch, stream).write(&[42; 2000]), Ok(1000)); log.lock().unwrap().clear(); pair.drive_client(); @@ -6463,7 +6631,10 @@ fn upload( const CHUNK: [u8; 10_000] = [0; 10_000]; let start = pair.time; - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); let mut unsent = total; let mut received = 0; let mut server_stream = None; diff --git a/rs/moq-quic/src/tests/reset_stream_at.rs b/rs/moq-quic/src/tests/reset_stream_at.rs index 4144d3a4974..5db100f3672 100644 --- a/rs/moq-quic/src/tests/reset_stream_at.rs +++ b/rs/moq-quic/src/tests/reset_stream_at.rs @@ -101,7 +101,10 @@ fn stop_sending_abandons_prefix(reset_stream_at: bool) { let (client_ch, server_ch) = pair.connect(); // The server learns of the stream from its first byte - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); pair.client_send(client_ch, s).write(&MSG[..1]).unwrap(); pair.drive(); assert_eq!(accept(&mut pair, server_ch), s); @@ -148,7 +151,10 @@ fn delivers_prefix_before_error() { let mut pair = pair(true); let (client_ch, server_ch) = pair.connect(); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); let checkpoint = write(&mut pair, client_ch, s); reset(&mut pair, client_ch, checkpoint).unwrap(); pair.drive(); @@ -174,7 +180,10 @@ fn delivers_unsent_prefix() { let mut pair = pair(true); let (client_ch, server_ch) = pair.connect(); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); pair.client_send(client_ch, s) .write(&MSG[..RELIABLE]) .unwrap(); @@ -195,7 +204,10 @@ fn retransmits_lost_prefix() { let mut pair = pair(true); let (client_ch, server_ch) = pair.connect(); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); let checkpoint = write(&mut pair, client_ch, s); lose_client_packets(&mut pair); reset(&mut pair, client_ch, checkpoint).unwrap(); @@ -229,7 +241,10 @@ fn retransmits_lost_reset() { let mut pair = pair(true); let (client_ch, server_ch) = pair.connect(); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); let checkpoint = write(&mut pair, client_ch, s); pair.drive(); reset(&mut pair, client_ch, checkpoint).unwrap(); @@ -251,7 +266,10 @@ fn received_reset_after_prefix() { let mut pair = pair(true); let (client_ch, server_ch) = pair.connect(); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); let checkpoint = write(&mut pair, client_ch, s); reset(&mut pair, client_ch, checkpoint).unwrap(); pair.drive(); @@ -282,7 +300,10 @@ fn after_finish() { let (client_ch, server_ch) = pair.connect(); for reliable in [RELIABLE, MSG.len()] { - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); let mut checkpoint = write(&mut pair, client_ch, s); if reliable == MSG.len() { checkpoint = pair.client_send(client_ch, s).checkpoint(); @@ -311,7 +332,10 @@ fn flow_control_blocked() { let mut pair = Pair::new(Default::default(), config); let (client_ch, server_ch) = pair.connect(); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); pair.client_send(client_ch, s) .write(&MSG[..RELIABLE]) .unwrap(); @@ -343,7 +367,10 @@ fn unsupported_holds_reset_until_acked() { let mut pair = pair(false); let (client_ch, server_ch) = pair.connect(); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); let checkpoint = write(&mut pair, client_ch, s); reset(&mut pair, client_ch, checkpoint).unwrap(); @@ -365,7 +392,10 @@ fn unsupported_retransmits_lost_prefix() { let mut pair = pair(false); let (client_ch, server_ch) = pair.connect(); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); let checkpoint = write(&mut pair, client_ch, s); lose_client_packets(&mut pair); reset(&mut pair, client_ch, checkpoint).unwrap(); @@ -386,7 +416,10 @@ fn unsupported_resets_at_once_when_acked() { let mut pair = pair(false); let (client_ch, server_ch) = pair.connect(); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); let checkpoint = write(&mut pair, client_ch, s); pair.drive(); assert_eq!(accept(&mut pair, server_ch), s); @@ -412,7 +445,10 @@ fn unsupported_after_finish() { let mut pair = pair(false); let (client_ch, server_ch) = pair.connect(); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); let checkpoint = write(&mut pair, client_ch, s); pair.client_send(client_ch, s).finish().unwrap(); reset(&mut pair, client_ch, checkpoint).unwrap(); @@ -445,7 +481,10 @@ fn unsupported_repeated_reset_is_closed() { let mut pair = pair(false); let (client_ch, _) = pair.connect(); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); let checkpoint = write(&mut pair, client_ch, s); let again = pair.client_send(client_ch, s).checkpoint(); lose_client_packets(&mut pair); @@ -464,7 +503,10 @@ fn unsupported_close_while_held() { let mut pair = pair(false); let (client_ch, server_ch) = pair.connect(); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); let checkpoint = write(&mut pair, client_ch, s); lose_client_packets(&mut pair); reset(&mut pair, client_ch, checkpoint).unwrap(); @@ -491,10 +533,16 @@ fn unsupported_returns_flow_control() { let mut pair = Pair::new(Default::default(), config); let (client_ch, server_ch) = pair.connect(); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); let checkpoint = write(&mut pair, client_ch, s); reset(&mut pair, client_ch, checkpoint).unwrap(); - let next = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let next = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); assert_eq!( pair.client_send(client_ch, next).write(MSG), Err(WriteError::Blocked) @@ -515,7 +563,10 @@ fn unsupported_allows_zero() { let mut pair = pair(false); let (client_ch, server_ch) = pair.connect(); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); let checkpoint = pair.client_send(client_ch, s).checkpoint(); pair.client_send(client_ch, s).write(MSG).unwrap(); reset(&mut pair, client_ch, checkpoint).unwrap(); @@ -534,7 +585,10 @@ fn repeated_reset_is_closed() { let mut pair = pair(true); let (client_ch, _) = pair.connect(); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); let checkpoint = write(&mut pair, client_ch, s); let again = pair.client_send(client_ch, s).checkpoint(); reset(&mut pair, client_ch, checkpoint).unwrap(); @@ -573,12 +627,18 @@ fn zero_rtt_rejected_checkpoint_is_closed() { inner.alpn_protocols = vec!["bar".into()]; let client_ch = pair.begin_connect(ClientConfig::new(client_crypto)); assert!(pair.client_conn_mut(client_ch).has_0rtt()); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); let checkpoint = write(&mut pair, client_ch, s); pair.drive(); assert!(!pair.client_conn_mut(client_ch).accepted_0rtt()); - let reopened = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let reopened = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); assert_eq!(reopened, s); pair.client_send(client_ch, reopened).write(MSG).unwrap(); assert_eq!( @@ -604,9 +664,15 @@ fn checkpoint_from_another_connection() { let mut second = pair(true); let (second_ch, _) = second.connect(); - let s = first.client_streams(first_ch).open(Dir::Uni).unwrap(); + let s = first + .client_streams(first_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); let checkpoint = write(&mut first, first_ch, s); - let other = second.client_streams(second_ch).open(Dir::Uni).unwrap(); + let other = second + .client_streams(second_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); assert_eq!(other, s); second.client_send(second_ch, other).write(MSG).unwrap(); let _ = reset(&mut second, second_ch, checkpoint); @@ -633,7 +699,10 @@ fn zero_rtt() { pair.client.addr = SocketAddr::new(Ipv6Addr::LOCALHOST.into(), pair.next_port()); let client_ch = pair.begin_connect(config); assert!(pair.client_conn_mut(client_ch).has_0rtt()); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let s = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); let checkpoint = write(&mut pair, client_ch, s); reset(&mut pair, client_ch, checkpoint).unwrap(); pair.drive(); diff --git a/rs/moq-quic/src/tests/sim.rs b/rs/moq-quic/src/tests/sim.rs index a551be8e4e2..97cfbf90afc 100644 --- a/rs/moq-quic/src/tests/sim.rs +++ b/rs/moq-quic/src/tests/sim.rs @@ -253,7 +253,7 @@ impl Peer { for (index, sending) in self.sends.iter_mut().enumerate() { let id = match sending.id { Some(id) => id, - None => match conn.streams().open(Dir::Uni) { + None => match conn.streams().open(Dir::Uni, Default::default()) { Some(id) => { assert_eq!(id.index(), index as u64); conn.send_stream(id) diff --git a/rs/moq-relay/MIGRATE.md b/rs/moq-relay/MIGRATE.md index 8145e268168..46e3d1da268 100644 --- a/rs/moq-relay/MIGRATE.md +++ b/rs/moq-relay/MIGRATE.md @@ -5,6 +5,11 @@ Rolling a cluster across a release also follows the [upgrade guide](https://moq. ## Unreleased +### Cluster dials build from a template (#5674) + +`Relay::client()` and `Cluster::with_client` are removed. +Give the cluster `with_connect(connect, quic)`, plus `with_iroh(endpoint)` for `iroh://` peers, and build your own `moq_tokio::Client` for other outbound dials. + ### `max_streams = 0` is refused (#5871) A `[quic]` `max_streams` of 0 stops startup, as `[peer.quic]` already did, since zero streams wedge the connection. diff --git a/rs/moq-relay/tests/integration/peer_limits.rs b/rs/moq-relay/tests/integration/peer_limits.rs index 2d5eea65826..8cd88b559aa 100644 --- a/rs/moq-relay/tests/integration/peer_limits.rs +++ b/rs/moq-relay/tests/integration/peer_limits.rs @@ -72,7 +72,7 @@ async fn probe(port: u16, peer: bool) -> Seen { .expect("handshake"); // SETUP type, then a one-byte message holding zero parameters. - let mut setup = conn.open_uni().await.expect("open"); + let mut setup = conn.open_uni(Default::default()).await.expect("open"); setup.write_all(&[0x01, 0x01, 0x00]).await.expect("write"); setup.finish().expect("finish"); @@ -85,7 +85,7 @@ async fn probe(port: u16, peer: bool) -> Seen { let mut streams = Vec::new(); while streams.len() < PROBE { - let open = conn.open_uni(); + let open = conn.open_uni(Default::default()); let stream = match peer { true => tokio::time::timeout(TIMEOUT, open) .await @@ -259,7 +259,7 @@ async fn cluster_dials_widen_a_plain_client() { let mut streams = Vec::new(); while streams.len() < PROBE { - match conn.open_uni().now_or_never() { + match conn.open_uni(Default::default()).now_or_never() { Some(stream) => streams.push(stream.expect("open")), None => break, } diff --git a/rs/moq-tokio/MIGRATE.md b/rs/moq-tokio/MIGRATE.md index d408979627a..3c14aaf35b2 100644 --- a/rs/moq-tokio/MIGRATE.md +++ b/rs/moq-tokio/MIGRATE.md @@ -16,10 +16,11 @@ Call `set_limits` on `request.quic()` instead, starting from `quic::Limits::defa The root `Auth` re-export is gone. `Connection::auth` returns a `connection::auth::Handle`, and its `add` returns a `connection::auth::Auth` instead of a `Token`. -### Datagrams take a send group and order (#5770) +### Streams and datagrams take `SendOptions` (#5770, #5872) -`quic::driver::Connection::send_datagram` and `quic::web::Session::send_datagram` take the send group (`None` for ungrouped) and an order, and never block. -`send_datagram_wait` and `SendDatagram` are gone; call `send_datagram(data, None, 0)` instead. +`quic::driver::Connection` and `quic::web::Session` take a `quic::driver::SendOptions { group, priority }` in `open_uni`, `open_bi`, and `send_datagram`, which never blocks. +`send_datagram_wait` and `SendDatagram` are gone; call `send_datagram(data, SendOptions::default())` instead. +`quic::driver::SendDatagramError` carries moq-quic's refusals in `Refused` instead of its own `UnsupportedByPeer`, `Disabled`, and `TooLarge`. ### The `noq` feature is `quic` (#5664) diff --git a/rs/moq-tokio/src/quic/driver/connection.rs b/rs/moq-tokio/src/quic/driver/connection.rs index 9700a1a3477..6913eb8382e 100644 --- a/rs/moq-tokio/src/quic/driver/connection.rs +++ b/rs/moq-tokio/src/quic/driver/connection.rs @@ -24,8 +24,13 @@ use tokio::sync::{ use tracing::{Instrument, Span, debug_span}; use crate::quic::driver::{ - ConnectionEvent, Duration, Instant, VarInt, mutex::Mutex, recv_stream::RecvStream, - runtime::UdpSender, send_group::SendGroup, send_stream::SendStream, udp_transmit, + ConnectionEvent, Duration, Instant, VarInt, + mutex::Mutex, + recv_stream::RecvStream, + runtime::UdpSender, + send_group::{self, SendGroup, SendOptions}, + send_stream::SendStream, + udp_transmit, }; use moq_quic::{ ConnectionError, ConnectionHandle, ConnectionStats, Dir, EndpointEvent, Side, StreamEvent, @@ -321,26 +326,31 @@ impl Future for ConnectionDriver { pub struct Connection(ConnectionRef); impl Connection { - /// Initiate a new outgoing unidirectional stream. + /// Initiate a new outgoing unidirectional stream, scheduled by `options`. /// /// Streams are cheap and instantaneous to open unless blocked by flow control. As a /// consequence, the peer won't be notified that a stream has been opened until the stream is /// actually used. - pub fn open_uni(&self) -> OpenUni<'_> { + /// + /// # Panics + /// - when `options.group` belongs to another connection + pub fn open_uni<'a>(&'a self, options: SendOptions<'a>) -> OpenUni<'a> { OpenUni { conn: &self.0, + options, notify: self.0.shared.stream_budget_available[Dir::Uni as usize].notified(), } } - /// Open a send group at `priority`, for streams to join with [`SendStream::set_group`]. + /// Open a send group at `priority`, for streams and datagrams to join with [`SendOptions`] + /// or [`SendStream::set_group`]. /// /// [`SendStream::set_group`]: crate::quic::driver::SendStream::set_group pub fn send_group(&self, priority: i64) -> SendGroup { SendGroup::new(self.0.0.clone(), priority) } - /// Initiate a new outgoing bidirectional stream. + /// Initiate a new outgoing bidirectional stream, its sending half scheduled by `options`. /// /// Streams are cheap and instantaneous to open unless blocked by flow control. As a /// consequence, the peer won't be notified that a stream has been opened until the stream is @@ -350,9 +360,13 @@ impl Connection { /// [`open_bi()`]: crate::quic::driver::Connection::open_bi /// [`SendStream`]: crate::quic::driver::SendStream /// [`RecvStream`]: crate::quic::driver::RecvStream - pub fn open_bi(&self) -> OpenBi<'_> { + /// + /// # Panics + /// - when `options.group` belongs to another connection + pub fn open_bi<'a>(&'a self, options: SendOptions<'a>) -> OpenBi<'a> { OpenBi { conn: &self.0, + options, notify: self.0.shared.stream_budget_available[Dir::Bi as usize].notified(), } } @@ -508,41 +522,32 @@ impl Connection { } } - /// Transmit `data` as an unreliable, unordered application datagram in `group` at `order` + /// Transmit `data` as an unreliable application datagram, scheduled by `options` /// /// Application datagrams are a low-level primitive. They may be lost or delivered out of order, /// and `data` must both fit inside a single QUIC packet and be smaller than the maximum /// dictated by the peer. /// - /// The datagram is scheduled like one of the group's streams at priority `order`, or among - /// the ungrouped streams without a group. Sending never blocks: a full buffer drops the - /// oldest datagram of the lowest-priority group, see [`moq_quic::Datagrams::send`]. + /// The datagram is scheduled like one of its group's streams at its priority, or among the + /// ungrouped streams without a group. Sending never blocks: a full buffer drops the oldest + /// datagram of the lowest-priority group, see [`moq_quic::Datagrams::send`]. + /// + /// # Panics + /// - when `options.group` belongs to another connection pub fn send_datagram( &self, data: Bytes, - group: Option<&SendGroup>, - order: i64, + options: SendOptions<'_>, ) -> Result<(), SendDatagramError> { let conn = &mut *self.0.state.lock("send_datagram"); if let Some(x) = &conn.error { return Err(SendDatagramError::ConnectionLost(x.clone())); } - use moq_quic::SendDatagramError::*; - match conn - .inner + conn.inner .datagrams() - .send(data, group.map(SendGroup::id), order) - { - Ok(()) => { - conn.wake(); - Ok(()) - } - Err(e) => Err(match e { - UnsupportedByPeer => SendDatagramError::UnsupportedByPeer, - Disabled => SendDatagramError::Disabled, - TooLarge => SendDatagramError::TooLarge, - }), - } + .send(data, send_group::to_quic(options))?; + conn.wake(); + Ok(()) } /// Compute the maximum size of datagrams that may be passed to [`send_datagram()`]. @@ -801,6 +806,7 @@ pin_project! { /// Future produced by [`Connection::open_uni`] pub struct OpenUni<'a> { conn: &'a ConnectionRef, + options: SendOptions<'a>, #[pin] notify: Notified<'a>, } @@ -810,7 +816,13 @@ impl Future for OpenUni<'_> { type Output = Result; fn poll(self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll { let this = self.project(); - let (conn, id, is_0rtt) = ready!(poll_open(ctx, this.conn, this.notify, Dir::Uni))?; + let (conn, id, is_0rtt) = ready!(poll_open( + ctx, + this.conn, + *this.options, + this.notify, + Dir::Uni + ))?; Poll::Ready(Ok(SendStream::new(conn, id, is_0rtt))) } } @@ -819,6 +831,7 @@ pin_project! { /// Future produced by [`Connection::open_bi`] pub struct OpenBi<'a> { conn: &'a ConnectionRef, + options: SendOptions<'a>, #[pin] notify: Notified<'a>, } @@ -828,7 +841,13 @@ impl Future for OpenBi<'_> { type Output = Result<(SendStream, RecvStream), ConnectionError>; fn poll(self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll { let this = self.project(); - let (conn, id, is_0rtt) = ready!(poll_open(ctx, this.conn, this.notify, Dir::Bi))?; + let (conn, id, is_0rtt) = ready!(poll_open( + ctx, + this.conn, + *this.options, + this.notify, + Dir::Bi + ))?; Poll::Ready(Ok(( SendStream::new(conn.clone(), id, is_0rtt), @@ -840,13 +859,18 @@ impl Future for OpenBi<'_> { fn poll_open<'a>( ctx: &mut Context<'_>, conn: &'a ConnectionRef, + options: SendOptions<'_>, mut notify: Pin<&mut Notified<'a>>, dir: Dir, ) -> Poll> { let mut state = conn.state.lock("poll_open"); if let Some(e) = &state.error { return Poll::Ready(Err(e.clone())); - } else if let Some(id) = state.inner.streams().open(dir) { + } else if let Some(id) = state + .inner + .streams() + .open(dir, send_group::to_quic(options)) + { let is_0rtt = state.inner.side().is_client() && state.inner.is_handshaking(); drop(state); // Release the lock so clone can take it return Poll::Ready(Ok((conn.clone(), id, is_0rtt))); @@ -1396,18 +1420,9 @@ fn wake_all_notify(wakers: &mut FxHashMap>) { #[derive(Debug, Error, Clone, Eq, PartialEq)] #[non_exhaustive] pub enum SendDatagramError { - /// The peer does not support receiving datagram frames - #[error("datagrams not supported by peer")] - UnsupportedByPeer, - /// Datagram support is disabled locally - #[error("datagram support disabled")] - Disabled, - /// The datagram is larger than the connection can currently accommodate - /// - /// Indicates that the path MTU minus overhead or the limit advertised by the peer has been - /// exceeded. - #[error("datagram too large")] - TooLarge, + /// The connection refused the datagram. + #[error(transparent)] + Refused(#[from] moq_quic::SendDatagramError), /// The connection was lost #[error("connection lost")] ConnectionLost(#[from] ConnectionError), diff --git a/rs/moq-tokio/src/quic/driver/mod.rs b/rs/moq-tokio/src/quic/driver/mod.rs index 38e5588f6ed..029b1657c9c 100644 --- a/rs/moq-tokio/src/quic/driver/mod.rs +++ b/rs/moq-tokio/src/quic/driver/mod.rs @@ -80,7 +80,7 @@ pub(crate) use crate::quic::driver::runtime::send_failed; #[cfg(test)] pub(crate) use crate::quic::driver::runtime::wrap_udp_socket; pub use crate::quic::driver::runtime::{AsyncUdpSocket, UdpSender}; -pub use crate::quic::driver::send_group::SendGroup; +pub use crate::quic::driver::send_group::{SendGroup, SendOptions}; pub use crate::quic::driver::send_stream::{Checkpoint, SendStream, StoppedError, WriteError}; #[cfg(test)] diff --git a/rs/moq-tokio/src/quic/driver/send_group.rs b/rs/moq-tokio/src/quic/driver/send_group.rs index 9e8adc793ff..5270cdd8de7 100644 --- a/rs/moq-tokio/src/quic/driver/send_group.rs +++ b/rs/moq-tokio/src/quic/driver/send_group.rs @@ -8,14 +8,25 @@ use crate::quic::driver::connection::ConnectionInner; /// the connection's other groups: a higher priority group preempts every lower one, and groups /// of equal priority get an equal share of bytes however many streams each has queued. A /// stream's own priority orders it only against the group's other streams, and a datagram sent -/// in the group is ordered among them by its `order`. Streams and datagrams outside any group go -/// before every group. See [`moq_quic::SendGroups`]. +/// in the group is ordered among them by its priority. Streams and datagrams outside any group +/// go before every group. See [`moq_quic::SendGroups`]. /// /// Clones share one group, released once the last clone is dropped and its streams finish /// and its datagrams are sent. A group does not keep its connection open. #[derive(Debug, Clone)] pub struct SendGroup(Arc); +/// How an outgoing stream or datagram is scheduled: its [`SendGroup`] and priority +pub type SendOptions<'a> = moq_quic::SendOptions<'a, SendGroup>; + +/// The options as the connection knows them. +pub(crate) fn to_quic(options: SendOptions<'_>) -> moq_quic::SendOptions<'_> { + moq_quic::SendOptions { + group: options.group.map(SendGroup::id), + priority: options.priority, + } +} + #[derive(Debug)] struct Inner { conn: Arc, @@ -29,7 +40,7 @@ impl SendGroup { .lock("SendGroup::new") .inner .send_groups() - .open(priority, None); + .open(priority); Self(Arc::new(Inner { conn, group: Some(group), diff --git a/rs/moq-tokio/src/quic/driver/tests.rs b/rs/moq-tokio/src/quic/driver/tests.rs index d7846d30e9f..4b2644bf3e1 100755 --- a/rs/moq-tokio/src/quic/driver/tests.rs +++ b/rs/moq-tokio/src/quic/driver/tests.rs @@ -164,7 +164,7 @@ fn read_after_close() { .expect("endpoint") .await .expect("connection"); - let mut s = new_conn.open_uni().await.unwrap(); + let mut s = new_conn.open_uni(Default::default()).await.unwrap(); s.write_all(MSG).await.unwrap(); s.finish().unwrap(); // Wait for the stream to be closed, one way or another. @@ -709,12 +709,18 @@ async fn zero_rtt() { } }); info!("sending 0.5-RTT"); - let mut s = connection.open_uni().await.expect("open_uni"); + let mut s = connection + .open_uni(Default::default()) + .await + .expect("open_uni"); s.write_all(MSG0).await.expect("write"); s.finish().unwrap(); connection.authenticated().await.expect("connected"); info!("sending 1-RTT"); - let mut s = connection.open_uni().await.expect("open_uni"); + let mut s = connection + .open_uni(Default::default()) + .await + .expect("open_uni"); s.write_all(MSG1).await.expect("write"); // The peer might close the connection before ACKing let _ = s.finish(); @@ -752,7 +758,10 @@ async fn zero_rtt() { .unwrap_or_else(|_| panic!("missing 0-RTT keys")); // Send something before the handshake can make progress, thereby forcing 0-RTT - let mut stream_0rtt = connection.open_uni().await.expect("0-RTT open uni"); + let mut stream_0rtt = connection + .open_uni(Default::default()) + .await + .expect("0-RTT open uni"); info!("sending 0-RTT"); stream_0rtt.write_all(MSG0).await.expect("0-RTT write"); stream_0rtt.finish().unwrap(); @@ -958,7 +967,10 @@ fn run_echo(args: EchoArgs) { for i in 0..args.nr_streams { println!("Opening stream {i}"); - let (mut send, mut recv) = new_conn.open_bi().await.expect("stream open"); + let (mut send, mut recv) = new_conn + .open_bi(Default::default()) + .await + .expect("stream open"); let msg = gen_data(args.stream_size, SEED); let send_task = async { @@ -1130,7 +1142,7 @@ async fn rebind_recv() { info!("got conn"); connected_send.notify_one(); write_recv.notified().await; - let mut stream = connection.open_uni().await.unwrap(); + let mut stream = connection.open_uni(Default::default()).await.unwrap(); stream.write_all(MSG).await.unwrap(); stream.finish().unwrap(); // Wait for the stream to be closed, one way or another. @@ -1494,7 +1506,7 @@ async fn remote_address_monitoring() { .unwrap(); // send data from the new socket to trigger migration on the server - let mut stream = client_conn.open_uni().await.unwrap(); + let mut stream = client_conn.open_uni(Default::default()).await.unwrap(); stream.write_all(b"hello").await.unwrap(); stream.finish().unwrap(); }; @@ -1528,13 +1540,13 @@ async fn stream_id_flow_control() { // If `open_uni` doesn't get unblocked when the previous stream is dropped, this will time out. tokio::join!( async { - client.open_uni().await.unwrap(); + client.open_uni(Default::default()).await.unwrap(); }, async { - client.open_uni().await.unwrap(); + client.open_uni(Default::default()).await.unwrap(); }, async { - client.open_uni().await.unwrap(); + client.open_uni(Default::default()).await.unwrap(); }, async { server.accept_uni().await.unwrap(); @@ -1570,9 +1582,13 @@ async fn two_datagram_readers() { x }, async { - server.send_datagram(b"one"[..].into(), None, 0).unwrap(); + server + .send_datagram(b"one"[..].into(), Default::default()) + .unwrap(); done.notified().await; - server.send_datagram(b"two"[..].into(), None, 0).unwrap(); + server + .send_datagram(b"two"[..].into(), Default::default()) + .unwrap(); } ); assert!(*a == *b"one" || *b == *b"one"); @@ -1651,7 +1667,7 @@ async fn stream_stopped() { .unwrap() .await .unwrap(); - let mut stream = conn.open_uni().await.unwrap(); + let mut stream = conn.open_uni(Default::default()).await.unwrap(); let stopped1 = stream.stopped(); let stopped2 = stream.stopped(); let stopped3 = stream.stopped(); @@ -1696,7 +1712,7 @@ async fn stream_stopped_2() { async { endpoint.accept().await.unwrap().await } ) .unwrap(); - let send_stream = conn.open_uni().await.unwrap(); + let send_stream = conn.open_uni(Default::default()).await.unwrap(); let stopped = timeout(Duration::from_millis(100), send_stream.stopped()) .instrument(error_span!("stopped")); tokio::pin!(stopped); @@ -1762,7 +1778,7 @@ async fn stream_drop_removes_blocked_reader() { .unwrap() .await .unwrap(); - let mut stream = conn.open_uni().await.unwrap(); + let mut stream = conn.open_uni(Default::default()).await.unwrap(); // need to send some data to actually start the stream stream.write_all(b"hello").await.unwrap(); @@ -1806,7 +1822,7 @@ async fn recv_stream_cancel_stop_drop() { .unwrap() .await .unwrap(); - let mut send = conn.open_uni().await.unwrap(); + let mut send = conn.open_uni(Default::default()).await.unwrap(); _ = send.write_all(b"hello").await; // Don't drop (finish) the send stream until the read has been // cancelled by the server, ensuring that read_to_end can't complete @@ -1871,7 +1887,7 @@ async fn held_reads_spend_the_connection_window() { async { endpoint.accept().await.unwrap().await } ) .unwrap(); - let mut send = client.open_uni().await.unwrap(); + let mut send = client.open_uni(Default::default()).await.unwrap(); let writer = tokio::spawn(async move { // The receiver walks away before this finishes. let _ = send.write_all(&[7; 4 * WINDOW]).await; @@ -2045,7 +2061,7 @@ async fn unreachable_sends_are_loss_once_established() { let server_conn = server_conn.unwrap(); outage.active.store(true, Ordering::SeqCst); - let mut send = client_conn.open_uni().await.unwrap(); + let mut send = client_conn.open_uni(Default::default()).await.unwrap(); send.write_all(b"hello").await.unwrap(); send.finish().unwrap(); outage.dropped.notified().await; diff --git a/rs/moq-tokio/src/quic/web/connect.rs b/rs/moq-tokio/src/quic/web/connect.rs index d49228d5f44..04888cd5fef 100644 --- a/rs/moq-tokio/src/quic/web/connect.rs +++ b/rs/moq-tokio/src/quic/web/connect.rs @@ -113,7 +113,7 @@ impl Connected { let request = request.into(); // Create a new stream that will be used to send the CONNECT frame. - let (mut send, mut recv) = conn.open_bi().await?; + let (mut send, mut recv) = conn.open_bi(Default::default()).await?; tracing::debug!(?request, "sending CONNECT request"); request.write(&mut send).await?; diff --git a/rs/moq-tokio/src/quic/web/error.rs b/rs/moq-tokio/src/quic/web/error.rs index 57d9eb1916b..1679d8dbe94 100644 --- a/rs/moq-tokio/src/quic/web/error.rs +++ b/rs/moq-tokio/src/quic/web/error.rs @@ -307,32 +307,6 @@ pub enum ServerError { Rustls(#[from] rustls::Error), } -// #[derive(Clone, Error, Debug)] -// pub enum SendDatagramError { -// #[error("Unsupported peer")] -// UnsupportedPeer, - -// #[error("Datagram support Disabled by peer")] -// DatagramSupportDisabled, - -// #[error("Datagram Too large")] -// TooLarge, - -// #[error("Session errorr: {0}")] -// SessionError(#[from] SessionError), -// } - -// impl From for SendDatagramError { -// fn from(value: crate::quic::driver::SendDatagramError) -> Self { -// match value { -// crate::quic::driver::SendDatagramError::UnsupportedByPeer => SendDatagramError::UnsupportedPeer, -// crate::quic::driver::SendDatagramError::Disabled => SendDatagramError::DatagramSupportDisabled, -// crate::quic::driver::SendDatagramError::TooLarge => SendDatagramError::TooLarge, -// crate::quic::driver::SendDatagramError::ConnectionLost(e) => -// SendDatagramError::SessionError(e.into()), } -// } -// } - impl web_transport_trait::Error for SessionError { fn session_error(&self) -> Option<(u32, String)> { if let SessionError::WebTransportError(WebTransportError::Closed(code, reason)) = self { diff --git a/rs/moq-tokio/src/quic/web/session.rs b/rs/moq-tokio/src/quic/web/session.rs index 3e6f84a0711..d14ed86f191 100644 --- a/rs/moq-tokio/src/quic/web/session.rs +++ b/rs/moq-tokio/src/quic/web/session.rs @@ -13,6 +13,7 @@ use bytes::{Bytes, BytesMut}; use futures::stream::{FuturesUnordered, Stream, StreamExt}; use kio::Waiter; +use crate::quic::driver::SendOptions; use crate::quic::web::fan::Fan; use crate::quic::web::{ ClientError, CloseReason, Connected, RecvStream, SendStream, SessionError, Settings, WebTransportError, @@ -261,40 +262,50 @@ impl Session { } } - /// Open a new unidirectional stream. See [`crate::quic::driver::Connection::open_uni`]. - pub async fn open_uni(&self) -> Result { - let mut send = self.conn.open_uni().await.map_err(|e| self.map_error(e))?; - - // Set the stream priority to max and then write the stream header. + /// Open a new unidirectional stream, its sending half scheduled by `options`. + /// See [`crate::quic::driver::Connection::open_uni`]. + pub async fn open_uni(&self, options: SendOptions<'_>) -> Result { + // Write the stream header ungrouped at the highest priority, before scheduling the stream. // Otherwise the application could write data with lower priority than the header, resulting // in queuing. Also the header is very important for determining the session ID // without reliable reset. - send.set_priority(i64::MAX).ok(); + let header = SendOptions { + group: None, + priority: i64::MAX, + }; + let mut send = self.conn.open_uni(header).await.map_err(|e| self.map_error(e))?; Self::write_full(&mut send, &self.header_uni) .await .map_err(|e| self.map_error(e))?; - // Reset the stream priority back to the default of 0. - send.set_priority(0).ok(); + send.set_priority(options.priority).ok(); + if let Some(group) = options.group { + send.set_group(Some(group)).ok(); + } let header = send.checkpoint(); Ok(SendStream::new(send, self.close.clone(), Some(header))) } - /// Open a new bidirectional stream. See [`crate::quic::driver::Connection::open_bi`]. - pub async fn open_bi(&self) -> Result<(SendStream, RecvStream), SessionError> { - let (mut send, recv) = self.conn.open_bi().await.map_err(|e| self.map_error(e))?; - - // Set the stream priority to max and then write the stream header. + /// Open a new bidirectional stream, its sending half scheduled by `options`. + /// See [`crate::quic::driver::Connection::open_bi`]. + pub async fn open_bi(&self, options: SendOptions<'_>) -> Result<(SendStream, RecvStream), SessionError> { + // Write the stream header ungrouped at the highest priority, before scheduling the stream. // Otherwise the application could write data with lower priority than the header, resulting // in queuing. Also the header is very important for determining the session ID // without reliable reset. - send.set_priority(i64::MAX).ok(); + let header = SendOptions { + group: None, + priority: i64::MAX, + }; + let (mut send, recv) = self.conn.open_bi(header).await.map_err(|e| self.map_error(e))?; Self::write_full(&mut send, &self.header_bi) .await .map_err(|e| self.map_error(e))?; - // Reset the stream priority back to the default of 0. - send.set_priority(0).ok(); + send.set_priority(options.priority).ok(); + if let Some(group) = options.group { + send.set_group(Some(group)).ok(); + } let header = send.checkpoint(); Ok(( SendStream::new(send, self.close.clone(), Some(header)), @@ -326,18 +337,13 @@ impl Session { Ok(datagram) } - /// Sends an application datagram to the remote peer in `group` at `order`. + /// Sends an application datagram to the remote peer, scheduled by `options`. /// /// Datagrams are unreliable and may be dropped or delivered out of order. /// The data must be smaller than [`max_datagram_size`](Self::max_datagram_size). /// See [`Connection::send_datagram`](crate::quic::driver::Connection::send_datagram) for how - /// `group` and `order` schedule it. - pub fn send_datagram( - &self, - data: Bytes, - group: Option<&crate::quic::driver::SendGroup>, - order: i64, - ) -> Result<(), SessionError> { + /// `options` schedule it. + pub fn send_datagram(&self, data: Bytes, options: SendOptions<'_>) -> Result<(), SessionError> { let result = if !self.header_datagram.is_empty() { // Unfortunately, we need to allocate/copy each datagram because of the driver API. // Pls go +1 if you care: https://github.com/quinn-rs/quinn/issues/1724 @@ -347,16 +353,17 @@ impl Session { buf.extend_from_slice(&self.header_datagram); buf.extend_from_slice(&data); - self.conn.send_datagram(buf.into(), group, order) + self.conn.send_datagram(buf.into(), options) } else { - self.conn.send_datagram(data, group, order) + self.conn.send_datagram(data, options) }; result.map_err(|e| self.map_error(e))?; Ok(()) } - /// Open a send group at `priority`, for streams to join with [`SendStream::set_group`]. + /// Open a send group at `priority`, for streams and datagrams to join with [`SendOptions`] + /// or [`SendStream::set_group`]. /// /// See [`crate::quic::driver::SendGroup`]. The HTTP/3 streams stay outside every group, /// so they go first. @@ -991,11 +998,11 @@ impl web_transport_trait::Session for Session { } async fn open_bi(&self) -> Result<(Self::SendStream, Self::RecvStream), Self::Error> { - Self::open_bi(self).await + Self::open_bi(self, SendOptions::default()).await } async fn open_uni(&self) -> Result { - Self::open_uni(self).await + Self::open_uni(self, SendOptions::default()).await } fn close(&self, code: u32, reason: &str) { @@ -1007,7 +1014,7 @@ impl web_transport_trait::Session for Session { } fn send_datagram(&self, data: Bytes) -> Result<(), Self::Error> { - Self::send_datagram(self, data, None, 0) + Self::send_datagram(self, data, SendOptions::default()) } async fn recv_datagram(&self) -> Result { @@ -1233,7 +1240,7 @@ mod tests { let (client, server, _endpoints) = pair().await; // A zero-length reserved frame, so the server sees the stream without a capsule. - let (mut send, _recv) = client.open_bi().await.unwrap(); + let (mut send, _recv) = client.open_bi(Default::default()).await.unwrap(); send.write_all(&[0x21, 0x00]).await.unwrap(); let (_send, recv) = server.accept_bi().await.unwrap(); @@ -1249,7 +1256,7 @@ mod tests { let (client, server, _endpoints) = pair().await; // A DATA frame that promises 5 bytes and carries 1. - let (mut send, _recv) = client.open_bi().await.unwrap(); + let (mut send, _recv) = client.open_bi(Default::default()).await.unwrap(); send.write_all(&[0x00, 0x05, 0x01]).await.unwrap(); send.finish().unwrap(); let (_send, recv) = server.accept_bi().await.unwrap(); @@ -1267,7 +1274,7 @@ mod tests { let (client, server, _endpoints) = pair().await; // The stream type alone, so the reset also cuts off the session ID. - let mut send = client.open_uni().await.unwrap(); + let mut send = client.open_uni(Default::default()).await.unwrap(); send.write_all(&[0x40, 0x54]).await.unwrap(); send.reset(7u32.into()).unwrap(); @@ -1281,7 +1288,7 @@ mod tests { async fn bi_reset_before_header() { let (client, server, _endpoints) = pair().await; - let (mut send, _recv) = client.open_bi().await.unwrap(); + let (mut send, _recv) = client.open_bi(Default::default()).await.unwrap(); send.reset(7u32.into()).unwrap(); let (send, recv) = server.accept_bi().await.unwrap(); @@ -1300,8 +1307,8 @@ mod tests { ); let (client, server) = (client.unwrap(), server.unwrap()); - client.open_uni().await.unwrap().reset(7).unwrap(); - let (mut send, _recv) = client.open_bi().await.unwrap(); + client.open_uni(Default::default()).await.unwrap().reset(7).unwrap(); + let (mut send, _recv) = client.open_bi(Default::default()).await.unwrap(); send.reset(8).unwrap(); let read = async { @@ -1323,7 +1330,7 @@ mod tests { async fn uni_other_session() { let (client, server, _endpoints) = pair().await; - let mut send = client.open_uni().await.unwrap(); + let mut send = client.open_uni(Default::default()).await.unwrap(); send.write_all(&[0x40, 0x54, 0x04]).await.unwrap(); send.finish().unwrap(); diff --git a/rs/moq-tokio/src/quic/web/settings.rs b/rs/moq-tokio/src/quic/web/settings.rs index 034c379ce74..65b69144246 100644 --- a/rs/moq-tokio/src/quic/web/settings.rs +++ b/rs/moq-tokio/src/quic/web/settings.rs @@ -63,7 +63,7 @@ impl Settings { tracing::debug!(?settings, "sending SETTINGS frame"); - let mut send = conn.open_uni().await?; + let mut send = conn.open_uni(Default::default()).await?; settings.write(&mut send).await?; Ok(send) diff --git a/rs/moq-tokio/src/transport.rs b/rs/moq-tokio/src/transport.rs index 9ca2498336b..5f60ad295b9 100644 --- a/rs/moq-tokio/src/transport.rs +++ b/rs/moq-tokio/src/transport.rs @@ -66,8 +66,12 @@ impl std::fmt::Display for Transport { /// plain boxed flavor suffices. type OpBox = futures::future::BoxFuture<'static, T>; -/// Adapts a transport that only implements the async interface -/// ([`web_transport_trait::Session`]) to the poll interface this crate requires. +/// Adapts one of this crate's async backends (qmux, iroh, quic) to the poll interface +/// moq-net requires. +/// +/// Only those backends fit: the bound goes through a sealed trait, so another +/// [`web_transport_trait::Session`] cannot be wrapped. A custom transport implements +/// moq-net's `transport::poll` traits instead. /// /// Each pending session operation is stored as a boxed future built from a clone /// of the underlying session; stream operations temporarily move the stream into @@ -340,6 +344,14 @@ impl SendStream { } } + /// Schedule a newly opened stream by its options. + fn schedule(&mut self, options: moq_net::transport::poll::SendOptions<'_, S::Group>) { + self.prioritize(options.priority); + if let Some(group) = options.group { + self.join(group); + } + } + /// Join `group`, deferred like [`prioritize`](Self::prioritize). fn join(&mut self, group: &S::Group) { match self.state.get_mut().unwrap().as_mut() { @@ -873,10 +885,13 @@ impl Grouped for crate::quic::web::Session { fn send_datagram( &self, payload: Bytes, - group: Option<&crate::quic::driver::SendGroup>, - order: i64, + options: moq_net::transport::poll::SendOptions<'_, crate::quic::driver::SendGroup>, ) -> Result<(), Self::Error> { - Self::send_datagram(self, payload, group, order) + let options = crate::quic::driver::SendOptions { + group: options.group, + priority: options.priority, + }; + Self::send_datagram(self, payload, options) } } diff --git a/rs/moq-tokio/src/transport/grouped.rs b/rs/moq-tokio/src/transport/grouped.rs index 97878edb257..a3b0bfc9f35 100644 --- a/rs/moq-tokio/src/transport/grouped.rs +++ b/rs/moq-tokio/src/transport/grouped.rs @@ -4,6 +4,8 @@ //! implement them: `pub(crate)` is refused (E0446) because the public adapter's //! `type Group` projects through [`GroupedSend`]. +use moq_net::transport::poll::SendOptions; + /// A backend session whose send streams may join send groups, as native QUIC's do. /// /// The async interface has no groups or wire counters, so moq-net's @@ -22,14 +24,13 @@ pub trait Grouped: web_transport_trait::Session { None } - /// Send a datagram in `group` at `order`; a backend without groups ignores both. + /// Send a datagram scheduled by `options`; a backend without groups ignores them. fn send_datagram( &self, payload: bytes::Bytes, - group: Option<&::Group>, - order: i64, + options: SendOptions<'_, ::Group>, ) -> Result<(), Self::Error> { - let _ = (group, order); + let _ = options; web_transport_trait::Session::send_datagram(self, payload) } } diff --git a/rs/moq-tokio/src/transport/owned.rs b/rs/moq-tokio/src/transport/owned.rs index 85361972b41..eec916f342d 100644 --- a/rs/moq-tokio/src/transport/owned.rs +++ b/rs/moq-tokio/src/transport/owned.rs @@ -57,11 +57,29 @@ impl> poll::Session fn poll_accept_bi(&mut self, cx: &mut Context<'_>) -> Poll, Self::Error>> { backend::Session::poll_accept_bi(self, cx).map_err(Error) } - fn poll_open_uni(&mut self, cx: &mut Context<'_>) -> Poll> { - backend::Session::poll_open_uni(self, cx).map_err(Error) - } - fn poll_open_bi(&mut self, cx: &mut Context<'_>) -> Poll, Self::Error>> { - backend::Session::poll_open_bi(self, cx).map_err(Error) + fn poll_open_uni( + &mut self, + cx: &mut Context<'_>, + options: poll::SendOptions<'_, ::Group>, + ) -> Poll> { + backend::Session::poll_open_uni(self, cx) + .map_ok(|mut send| { + send.schedule(options); + send + }) + .map_err(Error) + } + fn poll_open_bi( + &mut self, + cx: &mut Context<'_>, + options: poll::SendOptions<'_, ::Group>, + ) -> Poll, Self::Error>> { + backend::Session::poll_open_bi(self, cx) + .map_ok(|(mut send, recv)| { + send.schedule(options); + (send, recv) + }) + .map_err(Error) } fn send_group(&mut self, priority: i64) -> Option<::Group> { self.session.send_group(priority) @@ -69,10 +87,9 @@ impl> poll::Session fn send_datagram( &mut self, payload: Bytes, - group: Option<&::Group>, - order: i64, + options: poll::SendOptions<'_, ::Group>, ) -> Result<(), Self::Error> { - super::Grouped::send_datagram(&self.session, payload, group, order).map_err(Error) + super::Grouped::send_datagram(&self.session, payload, options).map_err(Error) } fn poll_recv_datagram(&mut self, cx: &mut Context<'_>) -> Poll> { backend::Session::poll_recv_datagram(self, cx).map_err(Error) diff --git a/rs/moq-tokio/tests/integration/stream_code.rs b/rs/moq-tokio/tests/integration/stream_code.rs index 508a56db8d1..97953fe78c6 100644 --- a/rs/moq-tokio/tests/integration/stream_code.rs +++ b/rs/moq-tokio/tests/integration/stream_code.rs @@ -91,12 +91,12 @@ async fn refused_code(server: &Server, alpn: &str) -> Option { .expect("handshake"); // SETUP type, then a one-byte message holding zero parameters. - let mut setup = conn.open_uni().await.expect("open"); + let mut setup = conn.open_uni(Default::default()).await.expect("open"); setup.write_all(&[0x01, 0x01, 0x00]).await.expect("write"); setup.finish().expect("finish"); // GROUP type, then a three-byte header: subscribe 7, sequence 0, frame start 0. - let mut send = conn.open_uni().await.expect("open"); + let mut send = conn.open_uni(Default::default()).await.expect("open"); send.write_all(&[0x00, 0x03, 0x07, 0x00, 0x00]).await.expect("write"); tokio::time::timeout(Duration::from_secs(10), send.stopped()) diff --git a/rs/moq-uring/MIGRATE.md b/rs/moq-uring/MIGRATE.md index 05350df5ed0..35713646fe0 100644 --- a/rs/moq-uring/MIGRATE.md +++ b/rs/moq-uring/MIGRATE.md @@ -4,6 +4,10 @@ How to update your code for each breaking change, newest first. ## Unreleased +### Datagrams take `SendOptions` (#5770, #5872) + +`quic::Connection::send_datagram` and `quic::web::Session::send_datagram` take a `quic::SendOptions { group, priority }` and never block; pass `SendOptions::default()` for an ungrouped datagram. + ### QUIC limits are `moq_quic::Limits` (#5871) `quic::Limits` is `moq_quic::Limits`, where each `None` field keeps that limit, and an invalid one fails `set_limits` with `Error::Limits` instead of `WindowRange` or `StreamsRange`. diff --git a/rs/moq-uring/benches/echo_quic.rs b/rs/moq-uring/benches/echo_quic.rs index 8d99a769317..f499edd2b40 100644 --- a/rs/moq-uring/benches/echo_quic.rs +++ b/rs/moq-uring/benches/echo_quic.rs @@ -82,7 +82,7 @@ mod linux { let session = client.connect(request).await.expect("connect"); let payload = vec![0x5a; PAYLOAD]; while let Ok(done) = start_rx.recv() { - let (mut send, mut recv) = session.open_bi().await.expect("open stream"); + let (mut send, mut recv) = session.open_bi(Default::default()).await.expect("open stream"); send.write_all(&payload).await.expect("write"); send.finish().expect("finish"); assert_eq!(recv.read_to_end(PAYLOAD + 1).await.expect("read").len(), PAYLOAD); diff --git a/rs/moq-uring/src/quic/driver/connection.rs b/rs/moq-uring/src/quic/driver/connection.rs index 8d73725c723..5bc8070b419 100644 --- a/rs/moq-uring/src/quic/driver/connection.rs +++ b/rs/moq-uring/src/quic/driver/connection.rs @@ -303,17 +303,21 @@ impl Connection { } /// Open a send group at `priority`, for streams to join with - /// [`SendStream::set_group`](super::SendStream::set_group). + /// [`SendStream::set_group`](super::SendStream::set_group) and datagrams with + /// [`SendOptions`](super::SendOptions). pub fn send_group(&self, priority: i64) -> super::SendGroup { super::SendGroup::new(self.shared.clone(), priority) } - /// Send a datagram in `group` at `order`, scheduled like one of the group's streams at - /// priority `order`, or among the ungrouped streams without a group. + /// Send a datagram scheduled by `options`, like one of its group's streams at its priority, + /// or among the ungrouped streams without a group. /// /// Never blocks: a full buffer drops the oldest datagram of the lowest-priority group, /// see [`moq_quic::Datagrams::send`]. - pub fn send_datagram(&self, payload: Bytes, group: Option<&super::SendGroup>, order: i64) -> Result<(), Error> { + /// + /// # Panics + /// - when `options.group` belongs to another connection + pub fn send_datagram(&self, payload: Bytes, options: super::SendOptions<'_>) -> Result<(), Error> { if let Some(err) = self.shared.closed() { return Err(err); } @@ -321,7 +325,7 @@ impl Connection { .conn .borrow_mut() .datagrams() - .send(payload, group.map(super::SendGroup::id), order) + .send(payload, super::group::to_quic(options)) .map_err(|err| Error::Quic(err.to_string()))?; self.shared.kick(); Ok(()) @@ -564,7 +568,12 @@ impl web_transport_trait::poll::Session for Connection { } // The peer's MAX_STREAMS credit is what gates us: `open` hands back // nothing while it is spent. - let opened = self.shared.conn.borrow_mut().streams().open(Dir::Uni); + let opened = self + .shared + .conn + .borrow_mut() + .streams() + .open(Dir::Uni, Default::default()); match opened { Some(id) => Poll::Ready(Ok(super::SendStream::new(self.shared.clone(), id))), None => { @@ -583,7 +592,12 @@ impl web_transport_trait::poll::Session for Connection { if let Some(err) = self.shared.closed() { return Poll::Ready(Err(err)); } - let opened = self.shared.conn.borrow_mut().streams().open(Dir::Bi); + let opened = self + .shared + .conn + .borrow_mut() + .streams() + .open(Dir::Bi, Default::default()); match opened { Some(id) => Poll::Ready(Ok(( super::SendStream::new(self.shared.clone(), id), @@ -598,7 +612,11 @@ impl web_transport_trait::poll::Session for Connection { } fn poll_send_datagram(&mut self, _: &mut Context<'_>, payload: &[u8]) -> Poll> { - Poll::Ready(Self::send_datagram(self, Bytes::copy_from_slice(payload), None, 0)) + Poll::Ready(Self::send_datagram( + self, + Bytes::copy_from_slice(payload), + Default::default(), + )) } fn poll_recv_datagram(&mut self, cx: &mut Context<'_>) -> Poll> { diff --git a/rs/moq-uring/src/quic/driver/group.rs b/rs/moq-uring/src/quic/driver/group.rs index bbe81976a84..54861086322 100644 --- a/rs/moq-uring/src/quic/driver/group.rs +++ b/rs/moq-uring/src/quic/driver/group.rs @@ -10,7 +10,7 @@ use super::Shared; /// connection's other groups: a higher priority group preempts every lower one, and groups of /// equal priority get an equal share of bytes however many streams each has queued. A stream's /// own priority orders it only against the group's other streams, and a datagram sent in the -/// group is ordered among them by its `order`. Streams and datagrams outside any group go +/// group is ordered among them by its priority. Streams and datagrams outside any group go /// before every group. See [`moq_quic::SendGroups`]. /// /// Clones share one group, released once the last clone is dropped and its streams finish @@ -18,6 +18,17 @@ use super::Shared; #[derive(Clone)] pub struct SendGroup(Rc); +/// How an outgoing datagram is scheduled: its [`SendGroup`] and priority. +pub type SendOptions<'a> = moq_quic::SendOptions<'a, SendGroup>; + +/// The options as the connection knows them. +pub(crate) fn to_quic(options: SendOptions<'_>) -> moq_quic::SendOptions<'_> { + moq_quic::SendOptions { + group: options.group.map(SendGroup::id), + priority: options.priority, + } +} + struct Inner { shared: Shared, group: Option, @@ -25,7 +36,7 @@ struct Inner { impl SendGroup { pub(crate) fn new(shared: Shared, priority: i64) -> Self { - let group = shared.conn.borrow_mut().send_groups().open(priority, None); + let group = shared.conn.borrow_mut().send_groups().open(priority); Self(Rc::new(Inner { shared, group: Some(group), diff --git a/rs/moq-uring/src/quic/driver/mod.rs b/rs/moq-uring/src/quic/driver/mod.rs index cd6d5a77ae0..87f5bccbe8b 100644 --- a/rs/moq-uring/src/quic/driver/mod.rs +++ b/rs/moq-uring/src/quic/driver/mod.rs @@ -16,7 +16,7 @@ mod stream; pub use connection::Connection; pub use endpoint::Endpoint; -pub use group::SendGroup; +pub use group::{SendGroup, SendOptions}; pub use stream::{Credit, RecvStream, SendStream}; pub(crate) use connection::{End, Shared}; diff --git a/rs/moq-uring/src/quic/mod.rs b/rs/moq-uring/src/quic/mod.rs index 2e30ef674e3..b2f5d5c0053 100644 --- a/rs/moq-uring/src/quic/mod.rs +++ b/rs/moq-uring/src/quic/mod.rs @@ -30,7 +30,7 @@ pub mod web; mod driver; -pub use driver::{Connection, Credit, RecvStream, SendGroup, SendStream}; +pub use driver::{Connection, Credit, RecvStream, SendGroup, SendOptions, SendStream}; pub use endpoint::Endpoint; /// The flow-control limits a live connection can change; see [`Connection::set_limits`]. pub use moq_quic::{Limits, LimitsError}; diff --git a/rs/moq-uring/src/quic/web.rs b/rs/moq-uring/src/quic/web.rs index 6d91056b923..9733fae3bce 100644 --- a/rs/moq-uring/src/quic/web.rs +++ b/rs/moq-uring/src/quic/web.rs @@ -457,19 +457,17 @@ impl Session { self.conn.send_group(priority) } - /// Send a datagram in `group` at `order`, scheduled like one of the group's streams. + /// Send a datagram scheduled by `options`, like one of its group's streams. /// /// See [`Connection::send_datagram`]. - pub fn send_datagram(&self, payload: Bytes, group: Option<&super::SendGroup>, order: i64) -> Result<(), Error> { + pub fn send_datagram(&self, payload: Bytes, options: super::SendOptions<'_>) -> Result<(), Error> { let Some(web) = &self.web else { - return self.conn.send_datagram(payload, group, order); + return self.conn.send_datagram(payload, options); }; let mut framed = BytesMut::with_capacity(web.header_datagram.len() + payload.len()); framed.extend_from_slice(&web.header_datagram); framed.extend_from_slice(&payload); - self.conn - .send_datagram(framed.freeze(), group, order) - .map_err(unmap_err) + self.conn.send_datagram(framed.freeze(), options).map_err(unmap_err) } /// Wrap a raw-QUIC connection in a session, with no @@ -697,7 +695,11 @@ impl web_transport_trait::poll::Session for Session { } fn poll_send_datagram(&mut self, _: &mut Context<'_>, payload: &[u8]) -> Poll> { - Poll::Ready(Self::send_datagram(self, Bytes::copy_from_slice(payload), None, 0)) + Poll::Ready(Self::send_datagram( + self, + Bytes::copy_from_slice(payload), + Default::default(), + )) } fn poll_recv_datagram(&mut self, cx: &mut Context<'_>) -> Poll> { diff --git a/rs/moq-uring/src/transport.rs b/rs/moq-uring/src/transport.rs index 150f429fa48..12efe45010a 100644 --- a/rs/moq-uring/src/transport.rs +++ b/rs/moq-uring/src/transport.rs @@ -22,10 +22,13 @@ impl Grouped for quic::web::Session { fn send_datagram( &mut self, payload: bytes::Bytes, - group: Option<&quic::SendGroup>, - order: i64, + options: moq_net::transport::poll::SendOptions<'_, quic::SendGroup>, ) -> Result<(), Self::Error> { - quic::web::Session::send_datagram(self, payload, group, order) + let options = quic::SendOptions { + group: options.group, + priority: options.priority, + }; + quic::web::Session::send_datagram(self, payload, options) } } diff --git a/rs/moq-uring/src/transport/adapter.rs b/rs/moq-uring/src/transport/adapter.rs index c37beba426f..b2e2fa32c1f 100644 --- a/rs/moq-uring/src/transport/adapter.rs +++ b/rs/moq-uring/src/transport/adapter.rs @@ -68,15 +68,14 @@ pub trait Grouped: backend::Session { None } - /// Send a datagram in `group` at `order`; a backend without groups ignores both and + /// Send a datagram scheduled by `options`; a backend without groups ignores them and /// drops the datagram when it has no room. fn send_datagram( &mut self, payload: Bytes, - group: Option<&::Group>, - order: i64, + options: poll::SendOptions<'_, ::Group>, ) -> Result<(), Self::Error> { - let _ = (group, order); + let _ = options; let mut cx = Context::from_waker(std::task::Waker::noop()); match backend::Session::poll_send_datagram(self, &mut cx, &payload) { Poll::Ready(res) => res, @@ -124,15 +123,23 @@ impl + Clone + 'static> pol .map_err(Error) .map(|res| res.map(|(send, recv)| (SendStream(send), RecvStream(recv)))) } - fn poll_open_uni(&mut self, cx: &mut Context<'_>) -> Poll> { + fn poll_open_uni( + &mut self, + cx: &mut Context<'_>, + options: poll::SendOptions<'_, ::Group>, + ) -> Poll> { backend::Session::poll_open_uni(&mut self.0, cx) .map_err(Error) - .map(|res| res.map(SendStream)) + .map(|res| res.map(|send| SendStream(send).scheduled(options))) } - fn poll_open_bi(&mut self, cx: &mut Context<'_>) -> Poll, Self::Error>> { + fn poll_open_bi( + &mut self, + cx: &mut Context<'_>, + options: poll::SendOptions<'_, ::Group>, + ) -> Poll, Self::Error>> { backend::Session::poll_open_bi(&mut self.0, cx) .map_err(Error) - .map(|res| res.map(|(send, recv)| (SendStream(send), RecvStream(recv)))) + .map(|res| res.map(|(send, recv)| (SendStream(send).scheduled(options), RecvStream(recv)))) } fn send_group(&mut self, priority: i64) -> Option<::Group> { Grouped::send_group(&mut self.0, priority) @@ -140,10 +147,9 @@ impl + Clone + 'static> pol fn send_datagram( &mut self, payload: Bytes, - group: Option<&::Group>, - order: i64, + options: poll::SendOptions<'_, ::Group>, ) -> Result<(), Self::Error> { - Grouped::send_datagram(&mut self.0, payload, group, order).map_err(Error) + Grouped::send_datagram(&mut self.0, payload, options).map_err(Error) } fn poll_recv_datagram(&mut self, cx: &mut Context<'_>) -> Poll> { backend::Session::poll_recv_datagram(&mut self.0, cx).map_err(Error) @@ -172,6 +178,16 @@ impl + Clone + 'static> pol } /// An outgoing WebTransport stream adapted to moq-net. pub struct SendStream(S); +impl SendStream { + /// Schedule a newly opened stream by its options. + fn scheduled(mut self, options: poll::SendOptions<'_, S::Group>) -> Self { + GroupedSend::set_priority(&mut self.0, options.priority); + if let Some(group) = options.group { + GroupedSend::set_group(&mut self.0, group); + } + self + } +} impl poll::SendStream for SendStream { type Error = Error; type Group = S::Group; @@ -370,7 +386,9 @@ mod tests { poll::Session::stats(&session).rtt(), Some(std::time::Duration::from_millis(7)) ); - let Poll::Ready(Ok((mut send, mut recv))) = poll::Session::poll_open_bi(&mut session, &mut cx) else { + let Poll::Ready(Ok((mut send, mut recv))) = + poll::Session::poll_open_bi(&mut session, &mut cx, Default::default()) + else { panic!("open failed") }; let mut buf = Bytes::from_static(b"abcd"); diff --git a/rs/moq-uring/src/transport/local.rs b/rs/moq-uring/src/transport/local.rs index 9161d0100e1..7c42bcb9b7c 100644 --- a/rs/moq-uring/src/transport/local.rs +++ b/rs/moq-uring/src/transport/local.rs @@ -22,6 +22,14 @@ impl Local { } } +/// Unwrap the send group of `options`, checking the thread. +fn unwrap<'a, G>(options: poll::SendOptions<'a, Local>) -> poll::SendOptions<'a, G> { + poll::SendOptions { + group: options.group.map(|group| &*group.0), + priority: options.priority, + } +} + impl poll::Session for Local { type SendStream = Local; type RecvStream = Local; @@ -37,13 +45,21 @@ impl poll::Session for Local { .map_ok(|(send, recv)| (Local::new(send), Local::new(recv))) } - fn poll_open_uni(&mut self, cx: &mut Context<'_>) -> Poll> { - self.0.poll_open_uni(cx).map_ok(Local::new) + fn poll_open_uni( + &mut self, + cx: &mut Context<'_>, + options: poll::SendOptions<'_, Local<::Group>>, + ) -> Poll> { + self.0.poll_open_uni(cx, unwrap(options)).map_ok(Local::new) } - fn poll_open_bi(&mut self, cx: &mut Context<'_>) -> Poll, Self::Error>> { + fn poll_open_bi( + &mut self, + cx: &mut Context<'_>, + options: poll::SendOptions<'_, Local<::Group>>, + ) -> Poll, Self::Error>> { self.0 - .poll_open_bi(cx) + .poll_open_bi(cx, unwrap(options)) .map_ok(|(send, recv)| (Local::new(send), Local::new(recv))) } @@ -54,10 +70,9 @@ impl poll::Session for Local { fn send_datagram( &mut self, payload: Bytes, - group: Option<&Local<::Group>>, - order: i64, + options: poll::SendOptions<'_, Local<::Group>>, ) -> Result<(), Self::Error> { - self.0.send_datagram(payload, group.map(|group| &*group.0), order) + self.0.send_datagram(payload, unwrap(options)) } fn poll_recv_datagram(&mut self, cx: &mut Context<'_>) -> Poll> { diff --git a/rs/moq-uring/tests/integration/echo.rs b/rs/moq-uring/tests/integration/echo.rs index ebf521e3e45..10a99e3464f 100644 --- a/rs/moq-uring/tests/integration/echo.rs +++ b/rs/moq-uring/tests/integration/echo.rs @@ -85,7 +85,7 @@ fn echo_tokio_peer() { url::Url::parse(&format!("https://{addr}/echo")).expect("url"), ); let session = client.connect(request).await.expect("connect"); - let (mut send, mut recv) = session.open_bi().await.expect("open stream"); + let (mut send, mut recv) = session.open_bi(Default::default()).await.expect("open stream"); send.write_all(&payload).await.expect("write"); send.finish().expect("finish"); let echoed = recv.read_to_end(PAYLOAD + 1).await.expect("read"); diff --git a/rs/moq-uring/tests/integration/web.rs b/rs/moq-uring/tests/integration/web.rs index c6aaf4e7783..7d2f59e952f 100644 --- a/rs/moq-uring/tests/integration/web.rs +++ b/rs/moq-uring/tests/integration/web.rs @@ -128,13 +128,13 @@ fn webtransport_echo_end_to_end() { let client = tokio_client(format!("https://{addr}/echo?token=abc"), |session| { Box::pin(async move { // Bidirectional echo. - let (mut send, mut recv) = session.open_bi().await.expect("open_bi"); + let (mut send, mut recv) = session.open_bi(Default::default()).await.expect("open_bi"); send.write_all(PAYLOAD).await.expect("write"); send.finish().expect("finish"); assert_eq!(read_all(&mut recv).await, PAYLOAD, "bidi echo"); // Unidirectional, both directions. - let mut send = session.open_uni().await.expect("open_uni"); + let mut send = session.open_uni(Default::default()).await.expect("open_uni"); send.write_all(PAYLOAD).await.expect("write"); send.finish().expect("finish"); let mut recv = session.accept_uni().await.expect("accept_uni"); @@ -142,7 +142,7 @@ fn webtransport_echo_end_to_end() { // Datagram echo. session - .send_datagram(PAYLOAD.to_vec().into(), None, 0) + .send_datagram(PAYLOAD.to_vec().into(), Default::default()) .expect("send datagram"); let echoed = session.read_datagram().await.expect("read datagram"); assert_eq!(&echoed[..], PAYLOAD, "datagram echo"); @@ -499,7 +499,7 @@ fn a_dropped_stream_carries_a_webtransport_code() { assert_eq!(&buf[..n], PAYLOAD); // Tell it we have the payload, so the reset below cannot race it. - let mut ack = session.open_uni().await.expect("open_uni"); + let mut ack = session.open_uni(Default::default()).await.expect("open_uni"); ack.write_all(b"ack").await.expect("write"); ack.finish().expect("finish"); @@ -508,7 +508,7 @@ fn a_dropped_stream_carries_a_webtransport_code() { // And the other direction: the server drops the read half of this // one, which must arrive as a WebTransport cancellation too. - let mut send = session.open_uni().await.expect("open_uni"); + let mut send = session.open_uni(Default::default()).await.expect("open_uni"); let err = loop { match send.write_all(PAYLOAD).await { Ok(()) => tokio::task::yield_now().await, @@ -605,7 +605,12 @@ fn a_reset_stream_keeps_its_header() { let err = recv.read_chunk(usize::MAX).await.expect_err("the server reset it"); assert!(matches!(err, moq_tokio::quic::web::ReadError::Reset(7)), "got {err:?}"); - session.open_uni().await.expect("open_uni").reset(8).expect("reset"); + session + .open_uni(Default::default()) + .await + .expect("open_uni") + .reset(8) + .expect("reset"); session.closed().await; }) }); diff --git a/rs/moq-wasm/src/transport/adapter.rs b/rs/moq-wasm/src/transport/adapter.rs index 46a2c8ff250..7b6a08e2e10 100644 --- a/rs/moq-wasm/src/transport/adapter.rs +++ b/rs/moq-wasm/src/transport/adapter.rs @@ -67,22 +67,30 @@ impl + Clone + 'st .map_err(Error) .map(|res| res.map(|(send, recv)| (SendStream(send), RecvStream(recv)))) } - fn poll_open_uni(&mut self, cx: &mut Context<'_>) -> Poll> { + fn poll_open_uni( + &mut self, + cx: &mut Context<'_>, + options: poll::SendOptions<'_, poll::Ungrouped>, + ) -> Poll> { backend::Session::poll_open_uni(&mut self.0, cx) .map_err(Error) - .map(|res| res.map(SendStream)) + .map(|res| res.map(|send| SendStream(send).scheduled(options))) } - fn poll_open_bi(&mut self, cx: &mut Context<'_>) -> Poll, Self::Error>> { + fn poll_open_bi( + &mut self, + cx: &mut Context<'_>, + options: poll::SendOptions<'_, poll::Ungrouped>, + ) -> Poll, Self::Error>> { backend::Session::poll_open_bi(&mut self.0, cx) .map_err(Error) - .map(|res| res.map(|(send, recv)| (SendStream(send), RecvStream(recv)))) + .map(|res| res.map(|(send, recv)| (SendStream(send).scheduled(options), RecvStream(recv)))) } fn send_group(&mut self, _: i64) -> Option { None } - /// The backend cannot rank a datagram against a stream, so the group and order are ignored, - /// and a datagram it has no room for is dropped. - fn send_datagram(&mut self, payload: Bytes, _: Option<&poll::Ungrouped>, _: i64) -> Result<(), Self::Error> { + /// The backend cannot rank a datagram against a stream, so `options` are ignored, and a + /// datagram it has no room for is dropped. + fn send_datagram(&mut self, payload: Bytes, _: poll::SendOptions<'_, poll::Ungrouped>) -> Result<(), Self::Error> { let mut cx = Context::from_waker(std::task::Waker::noop()); match backend::Session::poll_send_datagram(&mut self.0, &mut cx, &payload) { Poll::Ready(res) => res.map_err(Error), @@ -113,6 +121,13 @@ impl + Clone + 'st } /// An outgoing WebTransport stream adapted to moq-net. pub struct SendStream(S); +impl SendStream { + /// Schedule a newly opened stream, which has no group to join. + fn scheduled(mut self, options: poll::SendOptions<'_, poll::Ungrouped>) -> Self { + poll::SendStream::set_priority(&mut self, options.priority); + self + } +} impl poll::SendStream for SendStream { type Error = Error; type Group = poll::Ungrouped; @@ -293,7 +308,9 @@ mod tests { poll::Session::stats(&session).rtt(), Some(std::time::Duration::from_millis(7)) ); - let Poll::Ready(Ok((mut send, mut recv))) = poll::Session::poll_open_bi(&mut session, &mut cx) else { + let Poll::Ready(Ok((mut send, mut recv))) = + poll::Session::poll_open_bi(&mut session, &mut cx, Default::default()) + else { panic!("open failed") }; let mut buf = Bytes::from_static(b"abcd");