From 2c14dc8976506e4f462789372e213d53ab3ff3b5 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 10 Nov 2026 01:31:26 -0800 Subject: [PATCH 1/9] chore(quest): claim quest/m2/ietf-scope/extensions.md Quest-Claim: e4e5c70f9ba65c1a From 798c75d7726c9d1874c4fe1e5721dfc1c5ee6ad9 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 10 Nov 2026 02:56:56 -0800 Subject: [PATCH 2/9] wip(moq-net): moq-transport stops carrying our extensions Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/benches/auth.rs | 6 - rs/moq-net/src/auth.rs | 88 +- rs/moq-net/src/client.rs | 16 +- rs/moq-net/src/fuzz.rs | 5 +- rs/moq-net/src/fuzz/request.rs | 5 +- rs/moq-net/src/ietf/active_count.rs | 40 - rs/moq-net/src/ietf/auth.rs | 973 -------- rs/moq-net/src/ietf/cluster.rs | 536 ----- rs/moq-net/src/ietf/hidden.rs | 43 - rs/moq-net/src/ietf/message.rs | 1 - rs/moq-net/src/ietf/mod.rs | 39 - rs/moq-net/src/ietf/parameters.rs | 12 - rs/moq-net/src/ietf/peer.rs | 45 +- rs/moq-net/src/ietf/publish_namespace.rs | 224 +- rs/moq-net/src/ietf/publisher.rs | 1096 ++------- rs/moq-net/src/ietf/request.rs | 28 +- rs/moq-net/src/ietf/session.rs | 1192 ++-------- rs/moq-net/src/ietf/solicit.rs | 96 - rs/moq-net/src/ietf/subscribe_namespace.rs | 205 +- rs/moq-net/src/ietf/subscriber.rs | 2018 ++--------------- rs/moq-net/src/ietf/subscriber/namespaces.rs | 30 +- rs/moq-net/src/ietf/subscriber/tracks.rs | 4 - rs/moq-net/src/model/mod.rs | 2 +- rs/moq-net/src/model/origin.rs | 2 +- rs/moq-net/src/server.rs | 44 +- rs/moq-net/src/setup.rs | 26 +- rs/moq-net/tests/integration/auth.rs | 150 +- .../tests/integration/catalog_acquisition.rs | 33 +- rs/moq-net/tests/integration/extensions.rs | 151 -- .../tests/integration/lazy_discovery.rs | 73 +- rs/moq-net/tests/integration/main.rs | 1 - rs/moq-net/tests/integration/replacement.rs | 22 +- rs/moq-net/tests/integration/route_change.rs | 7 +- rs/moq-net/tests/integration/socket.rs | 191 +- rs/moq-net/tests/integration/stream_credit.rs | 5 +- rs/moq-net/tests/integration/synced.rs | 7 +- rs/moq-relay/src/config.rs | 17 - .../tests/integration/auth_lifetime.rs | 5 +- .../tests/integration/runtime_uring.rs | 65 - rs/moq-tokio/src/connect.rs | 19 +- rs/moq-tokio/src/listen.rs | 17 +- rs/moq-tokio/tests/integration/tokens.rs | 4 +- 42 files changed, 803 insertions(+), 6740 deletions(-) delete mode 100644 rs/moq-net/src/ietf/active_count.rs delete mode 100644 rs/moq-net/src/ietf/auth.rs delete mode 100644 rs/moq-net/src/ietf/cluster.rs delete mode 100644 rs/moq-net/src/ietf/hidden.rs delete mode 100644 rs/moq-net/src/ietf/solicit.rs delete mode 100644 rs/moq-net/tests/integration/extensions.rs diff --git a/rs/moq-net/benches/auth.rs b/rs/moq-net/benches/auth.rs index 36502948713..e1f2f8bdb76 100644 --- a/rs/moq-net/benches/auth.rs +++ b/rs/moq-net/benches/auth.rs @@ -122,16 +122,11 @@ async fn publisher(origin: &origin::Producer) -> (Session, Session, auth::Reques mock.protocol = Some(version.alpn().to_string()); let (client, server) = create_mock_session_pair(mock); // Tokens ride requests alone, and the client announces with PUBLISH_NAMESPACE. - let mut client_extensions = moq_net::setup::Extensions::default(); - client_extensions.auth = false; - let mut server_extensions = moq_net::setup::Extensions::default(); - server_extensions.solicit = false; let (subscribe, driver) = origin::Producer::new(origin::Config::new(std::num::NonZeroU64::new(1).unwrap())); harness::spawn(driver); let client = async { let (session, driver) = moq_net::Client::new() .with_versions(version.into()) - .with_extensions(client_extensions) .with_publisher(origin.consume()) .connect(harness::now(), client) .await @@ -142,7 +137,6 @@ async fn publisher(origin: &origin::Producer) -> (Session, Session, auth::Reques let server = async { let mut request = moq_net::Server::new() .with_versions(version.into()) - .with_extensions(server_extensions) .with_subscriber(subscribe.clone()) .accept_request(harness::now(), server) .await diff --git a/rs/moq-net/src/auth.rs b/rs/moq-net/src/auth.rs index 09128722f8f..e8888699f67 100644 --- a/rs/moq-net/src/auth.rs +++ b/rs/moq-net/src/auth.rs @@ -837,10 +837,10 @@ impl Link { Self::bound(supported, false) } - /// A moq-transport session: like [`new`](Self::new), but a request can carry a token - /// itself, so a token needs no AUTH to be presented. - pub(crate) fn ietf(supported: bool) -> Self { - Self::bound(supported, true) + /// A moq-transport session: it speaks no AUTH, but a request can carry a token itself, + /// so a token needs no AUTH to be presented. + pub(crate) fn ietf() -> Self { + Self::bound(false, true) } fn bound(supported: bool, requests: bool) -> Self { @@ -935,12 +935,6 @@ impl Link { self.read(|link, _| link.active()) } - /// Whether this side speaks AUTH: false on a version without it, on a side that does - /// not offer it, and once the peer turns out not to. - pub(crate) fn supported(&self) -> bool { - self.read(|link, _| link.supported) - } - /// Present a token: the session's own credential at setup, presented on this session /// alone, or else one of the handle's, presented on every session. pub(crate) fn present(&self, token: Bytes, setup: bool) -> Result { @@ -1362,42 +1356,6 @@ impl Link { }) } - /// The peer turned out not to negotiate AUTH, leaving the union unknown. On - /// moq-transport every token we presented stays, with an unknown grant, for the - /// requests that name it; elsewhere each fails as unsupported, which ends the handle's - /// tokens for good. The peer's tokens can still ride its requests, so their queue - /// stays open. - pub(crate) fn unsupported(&self) { - self.with_state(|state, link| { - let link = attached(&mut state.links, link); - link.supported = false; - link.opening.clear(); - let requests = link.requests; - let mut answers = Vec::new(); - for (id, slot) in &mut link.slots { - if slot.withdrawn { - continue; - } - let answer = match requests { - true => Ok(()), - false => Err(Error::Unsupported), - }; - slot.answered.get_or_insert(answer.clone()); - if let Err(err) = &answer { - slot.ended.get_or_insert(err.clone()); - } - if !slot.setup { - answers.push((*id, answer)); - } - } - // Nothing will read a withdrawn slot again. - link.slots.retain(|_, slot| !slot.withdrawn); - for (id, answer) in answers { - state.answer(id, answer.map(|()| Answer::Requests)); - } - }); - } - /// Why the session ended, once it has. fn closed(&self) -> Option { self.read(|link, _| link.closed.clone()) @@ -2238,12 +2196,6 @@ impl RequestGrant { self.held().grant.clone() } - /// The request's slot, for re-attaching what it authorized without taking another. - #[cfg(test)] - pub(crate) fn hold(&self) -> Option { - self.held().hold.clone() - } - /// Start verifying a renewal's token, holding the request's slot while it runs. pub(crate) fn verify_renewal(&self, handle: &Link, token: crate::setup::Token) -> RequestVerdict { let hold = self.held().hold.clone(); @@ -3143,7 +3095,7 @@ mod tests { /// could carry it. #[test] fn a_request_carries_an_auth_without_the_auth_stream() { - let ietf = Link::ietf(false); + let ietf = Link::ietf(); let auth = ietf.present(Bytes::from_static(b"jwt"), false).unwrap(); assert!(matches!( auth.poll_answered(&kio::Waiter::noop()), @@ -3164,28 +3116,13 @@ mod tests { )); } - /// A peer that turns out not to speak AUTH leaves a moq-transport auth for its - /// requests rather than failing it. - #[test] - fn an_unsupported_peer_keeps_a_request_auth() { - let handle = Link::ietf(true); - let auth = handle.present(Bytes::from_static(b"jwt"), false).unwrap(); - assert!(auth.poll_answered(&kio::Waiter::noop()).is_pending()); - handle.unsupported(); - assert!(matches!( - auth.poll_answered(&kio::Waiter::noop()), - Poll::Ready(Ok(Answer::Requests)) - )); - assert_eq!(handle.token(&auth).as_deref(), Some(b"jwt".as_slice())); - } - /// Only the handle that issued an auth carries it: a route naming another session's /// auth goes out under this session's scope. #[test] fn another_handle_does_not_carry_an_auth() { - let issuer = Link::ietf(false); + let issuer = Link::ietf(); let auth = issuer.present(Bytes::from_static(b"jwt"), false).unwrap(); - assert_eq!(Link::ietf(false).token(&auth), None); + assert_eq!(Link::ietf().token(&auth), None); } /// Every clone names one credential, which stays presented until the last drops. @@ -3766,21 +3703,18 @@ mod request_token_tests { ); } - /// A request-borne token is verified even without the MoQ Auth extension: it rides the - /// request message, not the AUTH stream, so `requests()` stays live on a session whose - /// `supported` is false, even once the peer turns out not to speak AUTH. This is the shape - /// a standard moq-transport peer presents when it does not negotiate the AUTH extension. + /// A request-borne token is verified without AUTH: it rides the request message, so + /// `requests()` stays live on a session that speaks no AUTH, as on moq-transport. #[kio::test] - async fn a_request_token_is_verified_without_the_auth_extension() { + async fn a_request_token_is_verified_without_the_auth_stream() { let handle = Link::new(false); let mut requests = handle.handle().requests(); - handle.unsupported(); let verdict = handle.verify_request(token(b"jwt", 0), None); let request = requests.next().await.expect("a request reaches the acceptor"); let _issued = request.accept(Grant::all()); assert!( verdict.grant().await.expect("granted").publish.matches("room/alice"), - "admitted without the AUTH extension" + "admitted without AUTH" ); } diff --git a/rs/moq-net/src/client.rs b/rs/moq-net/src/client.rs index c0a14c0e104..c2522a9bbc2 100644 --- a/rs/moq-net/src/client.rs +++ b/rs/moq-net/src/client.rs @@ -197,8 +197,8 @@ impl Client { self } - /// Choose which extensions to offer in SETUP. Defaults to every moq-transport one, and - /// not [`ietf`](setup::Extensions::ietf), which only relays turn on. + /// Choose which extensions to offer in SETUP. Defaults to none: only relays turn on + /// [`ietf`](setup::Extensions::ietf). pub fn with_extensions(mut self, extensions: setup::Extensions) -> Self { self.extensions = extensions; self @@ -272,7 +272,7 @@ impl Client { fn link(&self, supported: bool, requests: bool) -> Result { match &self.auth { Some(handle) => crate::auth::Link::attach(handle, supported, requests), - None if requests => Ok(crate::auth::Link::ietf(supported)), + None if requests => Ok(crate::auth::Link::ietf()), None => Ok(crate::auth::Link::new(supported)), } } @@ -421,11 +421,10 @@ impl Client { // SETUP is exchanged by the connection driver. // We advertise the request path in our SETUP for URL-less transports. - // The peer's SETUP decides whether AUTH is negotiated. A handle that does not - // offer it keeps the option out of our SETUP and presents no credential. - let auth = self.link(self.extensions.auth, true)?; + // moq-transport carries no AUTH: a token rides the requests that name it. + let auth = self.link(false, true)?; let peer_implementation = setup::PeerImplementation::default(); - let (protocol, goaway, credential) = ietf::start(ietf::Config { + let (protocol, goaway) = ietf::start(ietf::Config { runtime: runtime.clone(), stats: self.stats.clone(), limits: self.limits, @@ -443,7 +442,6 @@ impl Client { peer_declared: None, auth: auth.clone(), early_unis: Vec::new(), - extensions: self.extensions, implementation: self.implementation(), peer_implementation: peer_implementation.clone(), })?; @@ -459,7 +457,7 @@ impl Client { stats: self.stats.clone(), goaway, auth, - credential, + credential: None, estimate: None, peer_target: None, bandwidth: self.allocator(), diff --git a/rs/moq-net/src/fuzz.rs b/rs/moq-net/src/fuzz.rs index 4cbcf800809..29bc7ac38f7 100644 --- a/rs/moq-net/src/fuzz.rs +++ b/rs/moq-net/src/fuzz.rs @@ -890,10 +890,7 @@ pub fn encode_namespaces(paths: &[Path]) -> Vec { let mut data = Vec::new(); let w = &mut Encoder::new(&mut data, BENCH_IETF.into()); for path in paths { - let msg = ietf::Namespace { - suffix: path.clone(), - params: None, - }; + let msg = ietf::Namespace { suffix: path.clone() }; msg.encode(w, BENCH_IETF).expect("a namespace that fits"); } data diff --git a/rs/moq-net/src/fuzz/request.rs b/rs/moq-net/src/fuzz/request.rs index b28087945ea..2cb16d0338e 100644 --- a/rs/moq-net/src/fuzz/request.rs +++ b/rs/moq-net/src/fuzz/request.rs @@ -167,10 +167,7 @@ pub(super) fn seeds() -> Vec { for (index, version) in super::IETF_VERSIONS.iter().enumerate() { let messages: Vec<(u64, Vec)> = vec![ encode( - &ietf::RequestOk { - active: None, - largest: None, - }, + &ietf::RequestOk { largest: None }, *version, ), encode( diff --git a/rs/moq-net/src/ietf/active_count.rs b/rs/moq-net/src/ietf/active_count.rs deleted file mode 100644 index 2b37334c255..00000000000 --- a/rs/moq-net/src/ietf/active_count.rs +++ /dev/null @@ -1,40 +0,0 @@ -//! The MoQ Active Count extension (draft-lcurley-moq-active-count-00). -//! -//! A publisher answers SUBSCRIBE_NAMESPACE with a NAMESPACE for every suffix it already -//! advertises, then reports changes on the same stream. Nothing in moq-transport says -//! where the first part ends, so a subscriber can only guess from the stream going -//! quiet. When both endpoints declare the ACTIVE_COUNT option, the publisher puts on its -//! REQUEST_OK how many NAMESPACE messages come before the subscription is synced. - -/// ACTIVE_COUNT Setup Option: the sender counts the NAMESPACE messages before a -/// subscription is synced, and reads the peer's count. Even, so the value is a bare varint. -pub const ACTIVE_COUNT: u64 = 0x40B64; - -/// ACTIVE_COUNT REQUEST_OK parameter: how many NAMESPACE messages immediately follow -/// before the subscription is synced. Only answers SUBSCRIBE_NAMESPACE. -/// Even, so the value is a bare varint. -pub const ACTIVE_COUNT_PARAM: u64 = 0x40B66; - -/// Whether the peer declared the option. We always declare it, so this alone decides -/// whether the extension is negotiated. -pub fn from_setup(params: &super::Parameters) -> bool { - params.get_varint(super::ParameterVarInt::ActiveCount).is_some() -} - -/// Declare that we count and read the peer's count. -pub fn into_setup(params: &mut super::Parameters) { - params.set_varint(super::ParameterVarInt::ActiveCount, 1); -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn declared_option_negotiates() { - let mut params = super::super::Parameters::default(); - assert!(!from_setup(¶ms)); - into_setup(&mut params); - assert!(from_setup(¶ms)); - } -} diff --git a/rs/moq-net/src/ietf/auth.rs b/rs/moq-net/src/ietf/auth.rs deleted file mode 100644 index b6d50929d12..00000000000 --- a/rs/moq-net/src/ietf/auth.rs +++ /dev/null @@ -1,973 +0,0 @@ -//! The MoQ Auth extension (draft-lcurley-moq-auth-01). -//! -//! The moq-transport binding of the lite Auth Stream (see [`crate::auth`]): each -//! token rides a request stream of its own, answered with the patterns it grants, -//! encoded as lite's AUTH_OK encodes them. Negotiated with the AUTH Setup Option. - -#[cfg(feature = "async")] -use crate::driver::Transport; -use std::task::Poll; -use std::time::Duration; - -use bytes::Bytes; - -use crate::auth::{Grant, Link, Reply, Request, Serving}; -use crate::coding::{Decode, DecodeError, Decoder, Encode, EncodeError, Encoder}; -use crate::{Error, Patterns, SessionError}; - -#[cfg(feature = "async")] -use super::Control; -use super::{Message, RequestId, Version, peer}; -#[cfg(feature = "async")] -use crate::coding::Stream; - -/// AUTH Setup Option: the sender speaks this extension. Even, so the value is a bare -/// varint. Draft-01's code point, so a draft-00 peer, which read prefixes, never -/// negotiates it. -pub const AUTH: u64 = 0x40B70; - -/// What the peer declared: `None` for no option, otherwise whether it offered the -/// extension. Only an explicit 1 negotiates it. -pub fn from_setup(params: &super::Parameters) -> Option { - params.get_varint(super::ParameterVarInt::Auth).map(|value| value == 1) -} - -/// Offer the extension. -pub fn into_setup(params: &mut super::Parameters) { - params.set_varint(super::ParameterVarInt::Auth, 1); -} - -/// AUTH: the first message on an Auth request stream, presenting a token. -#[derive(Clone, PartialEq, Eq)] -pub struct Auth { - pub request_id: RequestId, - /// Empty presents the credential the connection already carried. - pub token: Bytes, -} - -impl std::fmt::Debug for Auth { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Auth") - .field("request_id", &self.request_id) - .field("token_len", &self.token.len()) - .finish() - } -} - -impl<'a> Message<'a> for Auth { - const ID: u64 = 0x40B61; - - fn encode_msg(&self, w: &mut Encoder<'_>, version: Version) -> Result<(), EncodeError> { - self.request_id.encode(w, version)?; - w.bytes(&self.token) - } - - fn decode_msg(r: &mut Decoder<'a>, version: Version) -> Result { - Ok(Self { - request_id: RequestId::decode(r, version)?, - token: Bytes::copy_from_slice(r.bytes()?), - }) - } -} - -/// AUTH_OK: the grant a token earns, replacing any earlier one on the stream. -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct AuthOk { - /// What the presenter may publish to the acceptor. - pub publish: Patterns, - /// What the presenter may subscribe to from the acceptor. - pub subscribe: Patterns, - /// How long until the grant lapses, or `None` for never. - pub expires: Option, -} - -/// Largest millisecond count every implementation carries losslessly. -const MAX_EXPIRES_MS: u64 = (1 << 53) - 1; - -impl<'a> Message<'a> for AuthOk { - const ID: u64 = 0x40B62; - - fn encode_msg(&self, w: &mut Encoder<'_>, version: Version) -> Result<(), EncodeError> { - self.publish.encode(w, version)?; - self.subscribe.encode(w, version)?; - // 0 means never, so a grant that has already lapsed rounds up to the smallest - // value that still reads as an expiry. - let expires = match self.expires { - None => 0, - Some(expires) => (expires.as_nanos().div_ceil(1_000_000).min(MAX_EXPIRES_MS as u128) as u64).max(1), - }; - w.varint(expires) - } - - fn decode_msg(r: &mut Decoder<'a>, version: Version) -> Result { - let publish = Patterns::decode(r, version)?; - let subscribe = Patterns::decode(r, version)?; - let expires = match r.varint()? { - 0 => None, - ms if ms <= MAX_EXPIRES_MS => Some(Duration::from_millis(ms)), - _ => return Err(DecodeError::InvalidValue), - }; - Ok(Self { - publish, - subscribe, - expires, - }) - } -} - -/// AUTH_ERROR: the acceptor refusing a token, or revoking it after an AUTH_OK. -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct AuthError { - /// A code from the REQUEST_ERROR registry. - pub code: u64, - pub reason: String, -} - -/// Longest AUTH_ERROR reason, in bytes, matching the lite wire and rejected from the -/// length prefix alone. -const MAX_REASON: usize = 8192; - -impl<'a> Message<'a> for AuthError { - const ID: u64 = 0x40B63; - - fn encode_msg(&self, w: &mut Encoder<'_>, _: Version) -> Result<(), EncodeError> { - if self.reason.len() > MAX_REASON { - return Err(EncodeError::TooLarge); - } - w.varint(self.code)?; - w.string(&self.reason) - } - - fn decode_msg(r: &mut Decoder<'a>, _: Version) -> Result { - let code = r.varint()?; - if code > u32::MAX as u64 { - return Err(DecodeError::InvalidValue); - } - let len = r.varint()?; - if len > MAX_REASON as u64 { - return Err(DecodeError::InvalidValue); - } - let reason = String::from_utf8(r.slice(len as usize)?.to_vec())?; - Ok(Self { code, reason }) - } -} - -/// The REQUEST_ERROR codes AUTH_ERROR carries. -const UNAUTHORIZED: u64 = 0x1; -const NOT_SUPPORTED: u64 = 0x3; -const MALFORMED_AUTH_TOKEN: u64 = 0x4; -const EXPIRED_AUTH_TOKEN: u64 = 0x5; -const GOING_AWAY: u64 = 0x6; - -/// The AUTH_ERROR code for a refusal. The public API speaks session codes, and the -/// actionable distinctions this registry carries are unsupported, malformed, expired, and -/// unauthorized. -fn to_code(code: SessionError) -> u64 { - match code { - SessionError::Version | SessionError::Unsupported => NOT_SUPPORTED, - SessionError::MalformedAuthToken => MALFORMED_AUTH_TOKEN, - SessionError::Expired => EXPIRED_AUTH_TOKEN, - _ => UNAUTHORIZED, - } -} - -/// Read an AUTH_ERROR code, preserving expiration so the presenter can refresh. -fn from_code(code: u64) -> Error { - match code { - NOT_SUPPORTED => Error::Unsupported, - MALFORMED_AUTH_TOKEN => Error::Session(SessionError::MalformedAuthToken), - EXPIRED_AUTH_TOKEN => Error::Expired, - GOING_AWAY => Error::GoingAway, - _ => Error::Session(SessionError::Unauthorized), - } -} - -/// Refuse an AUTH past our GOAWAY cutoff with AUTH_ERROR, the only answer its presenter reads. -#[cfg(feature = "async")] -pub(super) fn going_away(mut stream: Stream) -> super::request_stream::Reject { - let reply = AuthError { - code: GOING_AWAY, - reason: "going away".to_string(), - }; - let buffered = stream.writer.buffer_message(AuthError::ID, &reply); - super::request_stream::Reject::buffered(stream, buffered) -} - -/// What the peer's connection credential earns by default: publishing what our -/// subscribe half accepts, and subscribing to what our publish half serves. -pub(super) fn peer_grant( - publish: Option<&crate::origin::Consumer>, - subscribe: Option<&crate::origin::Producer>, -) -> Grant { - Grant { - publish: subscribe.map(|origin| origin.allowed()).unwrap_or_default(), - subscribe: publish.map(|origin| origin.allowed()).unwrap_or_default(), - expires: None, - } -} - -/// What every one of our Auth requests is sent with. -#[cfg(feature = "async")] -#[derive(Clone)] -struct Presenter { - stats: crate::stats::Session, - runtime: crate::time::Deadlines, - session: Transport, - control: Control, - handle: Link, - version: Version, -} - -/// Present every token this side adds, one AUTH request each as a child task, once the -/// peer's SETUP says it negotiated the extension. Never finishes: a session without it -/// just fails the tokens as unsupported. -#[cfg(feature = "async")] -pub(super) struct Presenting { - presenter: Presenter, - peer_setup: peer::PeerSetup, - /// Whether the peer's SETUP negotiated the extension, once read. - negotiated: Option, - tasks: crate::util::TaskSet, - /// A protocol violation on any token's stream, which ends the session. - violation: kio::Shared>, - /// The session ended, so no token opens another stream. - ended: bool, -} - -#[cfg(feature = "async")] -impl Presenting { - pub(super) fn new( - runtime: crate::time::Deadlines, - session: Transport, - control: Control, - handle: Link, - peer_setup: peer::PeerSetup, - version: Version, - stats: crate::stats::Session, - ) -> Self { - Self { - presenter: Presenter { - stats, - runtime, - session, - control, - handle, - version, - }, - peer_setup, - negotiated: None, - tasks: crate::util::TaskSet::owned(), - violation: Default::default(), - ended: false, - } - } - - /// Ready only with the protocol violation a token's stream hit. - pub(super) fn poll(&mut self, waiter: &kio::Waiter) -> Poll> { - let negotiated = match self.negotiated { - Some(negotiated) => negotiated, - None => { - let negotiated = std::task::ready!(self.peer_setup.poll_get(waiter)).auth; - if !negotiated { - self.presenter.handle.unsupported(); - } - *self.negotiated.insert(negotiated) - } - }; - if !negotiated || self.ended { - return Poll::Pending; - } - - loop { - if let Poll::Ready(mut err) = self.violation.poll(waiter, |err| match err.is_some() { - true => Poll::Ready(()), - false => Poll::Pending, - }) { - return Poll::Ready(Err(err.take().unwrap())); - } - match self.presenter.handle.poll_opening(waiter) { - Poll::Ready(Some((id, token))) => { - let mut present = Present { - presenter: self.presenter.clone(), - violation: self.violation.clone(), - id, - answered: false, - state: PresentState::Start(Ok(token)), - }; - self.tasks.push(move |waiter: &kio::Waiter| present.poll(waiter)); - } - // The children are left where they wait, no longer polled. - Poll::Ready(None) => { - self.ended = true; - return Poll::Pending; - } - Poll::Pending => { - // The children never end the session; `Ready` just means they're drained - // until the next token. - let _ = self.tasks.poll(waiter); - return Poll::Pending; - } - } - } - } -} - -/// One token's Auth requests: presented, tracked until either side ends it, and -/// presented again while [`Link::retry`] asks. -#[cfg(feature = "async")] -struct Present { - presenter: Presenter, - violation: kio::Shared>, - id: u64, - /// The peer replied to the token, on this request or an earlier one, so a request it - /// closes or resets before replying is lost rather than unsupported. - answered: bool, - state: PresentState, -} - -#[cfg(feature = "async")] -enum PresentState { - /// The token to present next, or why it cannot be. - Start(Result), - /// Waiting for a stream to send it on. - Opening { - open: crate::transport::open::Open, - token: Bytes, - }, - /// Flushing the AUTH. - Sending(Stream), - /// Reading the peer's replies. - Reading { - stream: Stream, - messages: super::request_stream::Messages, - budget: kio::coop::Budget, - }, - /// Waiting out a retry's delay. - Reopening(crate::time::Deadline), - Done, -} - -#[cfg(feature = "async")] -impl Present { - fn poll(&mut self, waiter: &kio::Waiter) -> Poll<()> { - loop { - let err = match &mut self.state { - PresentState::Start(token) => match std::mem::replace(token, Err(Error::Cancel)) { - // After a GOAWAY, sent or received, the peer must not see new requests. - Ok(token) if self.presenter.handle.active() => { - self.state = PresentState::Opening { - open: Default::default(), - token, - }; - continue; - } - Ok(_) => Error::GoingAway, - Err(err) => err, - }, - PresentState::Reopening(deadline) => { - let token = match self.presenter.handle.poll_withdrawn(self.id, waiter) { - Poll::Ready(()) => Err(Error::Cancel), - Poll::Pending => { - std::task::ready!(deadline.poll(waiter)); - self.presenter.handle.reopen(self.id) - } - }; - self.state = PresentState::Start(token); - continue; - } - PresentState::Done => return Poll::Ready(()), - PresentState::Opening { .. } | PresentState::Sending(_) | PresentState::Reading { .. } => { - let err = std::task::ready!(self.poll_request(waiter)); - // Drops the stream, whose reset is what tells the peer. - self.state = PresentState::Done; - err - } - }; - - let err = match err { - // The peer negotiated the extension, but a reset before any reply still tells - // us nothing about the token: it earns nothing here, as on moq-lite. - Error::Stream(_) if !self.answered => Error::Unsupported, - err => crate::auth::protocol_error(err), - }; - let handle = &self.presenter.handle; - // AUTH_ERROR carries no code that may pass, so unlike moq-lite only a lost - // request is presented again. - if let Some(delay) = handle.retry(self.id, &err) { - tracing::debug!(%err, ?delay, "auth token presenting again"); - self.state = PresentState::Reopening(crate::time::Deadline::after(&self.presenter.runtime, delay)); - continue; - } - if matches!(err, Error::ProtocolViolation) { - self.violation.lock().get_or_insert(err.clone()); - } - - match &err { - Error::Cancel | Error::Unsupported | Error::Transport(_) | Error::IdleTimeout | Error::Session(_) => { - tracing::debug!(%err, "auth token ended") - } - err => tracing::warn!(%err, "auth token ended"), - } - handle.ended(self.id, err); - return Poll::Ready(()); - } - } - - /// Send the token, then track its grant until either side ends it, ready with why. - fn poll_request(&mut self, waiter: &kio::Waiter) -> Poll { - let presenter = &self.presenter; - loop { - match &mut self.state { - PresentState::Opening { open, token } => { - let stream = - match std::task::ready!(Stream::poll_open(&presenter.session, open, presenter.version, waiter)) - { - Ok(stream) => stream, - Err(err) => return Poll::Ready(err), - }; - let mut stream = stream.with_stats(&presenter.stats); - let auth = Auth { - request_id: presenter.control.next_request_id(), - token: std::mem::take(token), - }; - if let Err(err) = stream.writer.buffer_message(Auth::ID, &auth) { - return Poll::Ready(err); - } - self.state = PresentState::Sending(stream); - } - PresentState::Sending(stream) => { - if let Err(err) = std::task::ready!(stream.writer.poll_flush(waiter)) { - return Poll::Ready(err); - } - let PresentState::Sending(stream) = std::mem::replace(&mut self.state, PresentState::Done) else { - unreachable!() - }; - self.state = PresentState::Reading { - stream, - messages: super::request_stream::Messages::new( - presenter.session.clone(), - presenter.control.client(), - ), - // Each grant recomputes the session's permits, so a peer streaming them - // must not hold the session's other tasks. - budget: kio::coop::Budget::new(32), - }; - } - PresentState::Reading { - stream, - messages, - budget, - } => loop { - std::task::ready!(budget.poll_yield(waiter)); - if presenter.handle.poll_withdrawn(self.id, waiter).is_ready() { - // Resetting our side is what tells the peer. - return Poll::Ready(Error::Cancel); - } - let (id, body) = match std::task::ready!(messages.poll_next(&mut stream.reader, waiter)) { - Ok(Some(super::request_stream::Framed(id, body))) => (id, body), - // The peer ended the grant without revoking it. - Ok(None) if self.answered => return Poll::Ready(Error::Cancel), - // A peer that closes before answering could not tell us anything. - Ok(None) => return Poll::Ready(Error::Unsupported), - Err(err) => return Poll::Ready(err), - }; - - match id { - AuthOk::ID => { - let ok: AuthOk = match decode_reply(&body, presenter.version) { - Ok(ok) => ok, - Err(err) => return Poll::Ready(err), - }; - let now = presenter.runtime.now(); - // The expiry is the peer's number: one past the local clock's range is - // malformed, not a reason to panic. - let expires = match ok.expires.map(|expires| now.checked_add(expires)) { - Some(None) => return Poll::Ready(Error::ProtocolViolation), - expires => expires.flatten(), - }; - self.answered = true; - presenter.handle.granted( - self.id, - Grant { - publish: ok.publish, - subscribe: ok.subscribe, - expires, - }, - ); - } - AuthError::ID => { - let refused: AuthError = match decode_reply(&body, presenter.version) { - Ok(refused) => refused, - Err(err) => return Poll::Ready(err), - }; - let err = from_code(refused.code); - tracing::warn!(%err, code = refused.code, reason = %refused.reason, "auth token refused"); - // A grant the acceptor could not tell, or never judged, is unknown, not - // empty, so only a real refusal settles the union. - if !matches!(err, Error::Unsupported | Error::GoingAway) { - presenter.handle.refused(self.id); - } - self.answered = true; - return Poll::Ready(err); - } - _ => return Poll::Ready(Error::UnexpectedMessage), - } - }, - PresentState::Start(_) | PresentState::Reopening(_) | PresentState::Done => { - unreachable!("not a request") - } - } - } - } -} - -/// Decode a reply that must fill its whole body, as every framed message does. -fn decode_reply<'m, T: Message<'m>>(body: &'m super::Body, version: Version) -> Result { - let mut data = body.decoder(version); - let msg = T::decode_msg(&mut data, version)?; - if !data.is_empty() { - return Err(Error::WrongSize); - } - Ok(msg) -} - -/// Answers the peer's Auth requests: the app's verdict when it took the requests, -/// otherwise the default grant for the connection's own credential. -#[derive(Clone)] -pub(super) struct Serve { - pub runtime: crate::time::Deadlines, - pub handle: Link, - /// What the peer's connection credential earns by default. - pub peer_grant: Grant, -} - -impl Serve { - /// Start answering one Auth request. The stream lives as long as the token. - #[cfg(feature = "async")] - pub(super) fn answer(self, mut stream: Stream, msg: Auth, version: Version) -> Answering { - let goaways = super::request_stream::GoAways::new(msg.request_id.0 % 2 == 1); - let serving = Serving::new(self.handle.clone()); - let mut default = None; - let state = match self.handle.acceptor() { - Some(requests) => { - requests.push(Request::new(msg.token, serving.issue.clone())); - AnsweringState::Serving - } - // Only the connection's own credential has a default answer; a token needs - // someone to verify it. - None if !msg.token.is_empty() => { - let reply = AuthError { - code: NOT_SUPPORTED, - reason: "tokens are not verified in band".to_string(), - }; - match stream.writer.buffer_message(AuthError::ID, &reply) { - Ok(()) => AnsweringState::Unverified, - Err(_) => AnsweringState::Done, - } - } - // Re-sent whenever a new limit changes it. - None => { - default = Some(crate::auth::DefaultGrant::new(self.handle.clone(), self.peer_grant)); - AnsweringState::Serving - } - }; - Answering { - runtime: self.runtime, - serving, - default, - stream: Some(stream), - version, - goaways, - state, - } - } -} - -/// One Auth request being answered: [`Serve::answer`]. -#[cfg(feature = "async")] -pub(super) struct Answering { - runtime: crate::time::Deadlines, - serving: Serving, - /// Feeds the default acceptor's grant into the outbox as it changes. - default: Option, - /// Taken only to close it after refusing an unverified token. - stream: Option>, - version: Version, - goaways: super::request_stream::GoAways, - state: AnsweringState, -} - -#[cfg(feature = "async")] -enum AnsweringState { - /// Flushing the refusal of a token nobody verifies, then closing. - Unverified, - /// Waiting for the refusal's acknowledgement. - UnverifiedClosing { - closing: crate::coding::Closing, - _reader: crate::coding::Reader, - }, - /// Writing the acceptor's replies until either side ends the token. - Serving, - /// Flushing a reply; `last` ends the replies once it is out. - Flushing { - last: bool, - }, - /// Waiting for the acknowledgement of our FIN. - Closing, - Done, -} - -#[cfg(feature = "async")] -impl Answering { - /// Ready once the token's stream ends, failing only on a protocol violation. - pub(super) fn poll(&mut self, waiter: &kio::Waiter) -> Poll> { - loop { - match &mut self.state { - AnsweringState::Unverified => { - let stream = self.stream.as_mut().expect("unverified holds the stream"); - let flushed = std::task::ready!(stream.writer.poll_flush(waiter)); - self.state = AnsweringState::Done; - if flushed.is_ok() { - let Stream { writer, reader } = self.stream.take().expect("unverified holds the stream"); - if let Ok(closing) = writer.closing() { - self.state = AnsweringState::UnverifiedClosing { - closing, - _reader: reader, - }; - } - } - } - AnsweringState::UnverifiedClosing { closing, .. } => { - let _ = std::task::ready!(closing.poll(waiter)); - self.state = AnsweringState::Done; - } - AnsweringState::Done => return Poll::Ready(Ok(())), - AnsweringState::Serving | AnsweringState::Flushing { .. } | AnsweringState::Closing => { - let err = match std::task::ready!(self.poll_issue(waiter)) { - Ok(()) => Error::Cancel, - Err(err) => { - match &err { - Error::Cancel - | Error::Unsupported - | Error::Stream(_) - | Error::Session(_) - | Error::Transport(_) - | Error::IdleTimeout => { - tracing::debug!(%err, "auth request ended") - } - err => tracing::warn!(%err, "auth request error"), - } - err - } - }; - self.state = AnsweringState::Done; - let err = crate::auth::protocol_error(err); - self.serving.end(err.clone()); - if matches!(err, Error::ProtocolViolation) { - return Poll::Ready(Err(err)); - } - return Poll::Ready(Ok(())); - } - } - } - } - - /// Write the acceptor's replies until either side ends the token. - fn poll_issue(&mut self, waiter: &kio::Waiter) -> Poll> { - let issue = &self.serving.issue; - let stream = self.stream.as_mut().expect("serving holds the stream"); - loop { - let reply = match &mut self.state { - AnsweringState::Flushing { last } => { - std::task::ready!(stream.writer.poll_flush(waiter))?; - match *last { - true => { - stream.writer.finish()?; - self.state = AnsweringState::Closing; - } - false => self.state = AnsweringState::Serving, - } - continue; - } - AnsweringState::Closing => return stream.writer.poll_closed(waiter), - AnsweringState::Serving => { - if let Poll::Ready(res) = self - .goaways - .poll_next::(&mut stream.reader, waiter) - .map(|res| res.map(|_| ())) - { - match res { - // The presenter withdrew the token, by FIN or by a cancelling reset. - // That is why it ended, whether or not our own FIN reaches a - // presenter that left. - Ok(()) | Err(Error::Stream(crate::StreamError::Cancel)) => { - issue.lock().peer.get_or_insert(Error::Cancel); - stream.writer.finish()?; - self.state = AnsweringState::Closing; - continue; - } - Err(err) => return Poll::Ready(Err(err)), - } - } - if let Some(default) = self.default.as_mut() - && let Poll::Ready(grant) = default.poll(waiter) - { - issue.lock().queue(Reply::Grant(grant)); - } - let mut issue = std::task::ready!(issue.poll(waiter, |issue| match issue.idle() { - true => Poll::Pending, - false => Poll::Ready(()), - })); - issue.next() - } - AnsweringState::Unverified | AnsweringState::UnverifiedClosing { .. } | AnsweringState::Done => { - unreachable!("not serving") - } - }; - - match reply { - Some(Reply::Grant(grant)) => { - let now = self.runtime.now(); - let ok = AuthOk { - publish: grant.publish, - subscribe: grant.subscribe, - expires: grant.expires.map(|at| at.saturating_duration_since(now)), - }; - // Sized as the full message before anything is written, so a grant over the u16 - // message size never leaves half a message on the wire. Never trim it: refuse, - // which revokes whatever this stream granted before. - if let Err(err) = ok.encode(&mut Encoder::new(&mut Vec::new(), self.version.into()), self.version) { - tracing::debug!(%err, "auth grant cannot be encoded as one AUTH_OK; refusing the token"); - issue.lock().refused(); - let refused = AuthError { - code: NOT_SUPPORTED, - reason: "grant cannot be encoded as one AUTH_OK".to_string(), - }; - stream.writer.buffer_message(AuthError::ID, &refused)?; - self.state = AnsweringState::Flushing { last: true }; - continue; - } - stream.writer.buffer_message(AuthOk::ID, &ok)?; - self.state = AnsweringState::Flushing { last: false }; - } - Some(Reply::Refuse { code, reason }) => { - let refused = AuthError { - code: to_code(code), - reason, - }; - stream.writer.buffer_message(AuthError::ID, &refused)?; - self.state = AnsweringState::Flushing { last: false }; - } - // The app is done with the grant, or refused the token: close our side. - None => { - stream.writer.finish()?; - self.state = AnsweringState::Closing; - } - } - } - } -} - -/// Anything but a GOAWAY after the presenter's Auth: refused once its type is read, -/// rather than waiting on a body that cannot make it legal. -#[cfg(feature = "async")] -#[derive(Debug)] -struct Unexpected; - -#[cfg(feature = "async")] -impl<'a> crate::coding::Decode<'a, Version> for Unexpected { - fn decode(r: &mut crate::coding::Decoder<'_>, _: Version) -> Result { - Err(crate::coding::DecodeError::InvalidMessage(r.varint()?)) - } -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::Pattern; - - #[cfg(feature = "async")] - #[kio::test] - async fn auth_waiting_for_credit_spends_no_request_id() { - for version in [Version::Draft18] { - for client in [false, true] { - let first = if client { 0 } else { 1 }; - let control = Control::new(client); - let mut present = Present { - presenter: Presenter { - runtime: crate::time::sim(), - stats: Default::default(), - session: crate::driver::Transport::new( - crate::lite::test_transport::ScriptedSession::new(Vec::new()).without_credit(), - ), - control: control.clone(), - handle: Link::new(false), - version, - }, - violation: Default::default(), - id: 0, - answered: false, - state: PresentState::Opening { - open: Default::default(), - token: Bytes::new(), - }, - }; - let polled = kio::wait(|waiter| Poll::Ready(present.poll_request(waiter).is_pending())).await; - assert!(polled, "the request waits for credit"); - assert_eq!(control.next_request_id(), RequestId(first)); - } - } - } - - const VERSION: Version = Version::Draft18; - - fn patterns(texts: &[&str]) -> Patterns { - texts.iter().map(|text| Pattern::try_from(*text).unwrap()).collect() - } - - fn encode_msg<'m, T: Message<'m>>(msg: &T, version: Version) -> Result, EncodeError> { - let mut buf = Vec::new(); - msg.encode_msg(&mut Encoder::new(&mut buf, version.into()), version)?; - Ok(buf) - } - - fn round_trip Message<'m> + PartialEq>(msg: &T) -> T { - let buf = encode_msg(msg, VERSION).unwrap(); - let mut r = Decoder::new(&buf, VERSION.into()); - let got = T::decode_msg(&mut r, VERSION).unwrap(); - assert!(r.is_empty(), "trailing bytes after decode"); - got - } - - /// Every draft that negotiates the extension round-trips the option. - #[test] - fn setup_option_round_trips_on_supported_drafts() { - for version in [Version::Draft18, Version::Draft20, Version::Draft22] { - let mut params = super::super::Parameters::default(); - assert_eq!(from_setup(¶ms), None); - into_setup(&mut params); - assert_eq!(from_setup(¶ms), Some(true), "{version:?}"); - } - } - - /// An explicit value other than 1 is an implementation that declined, which is - /// not the same statement as saying nothing. - #[test] - fn only_one_negotiates() { - let mut params = super::super::Parameters::default(); - params.set_varint(super::super::ParameterVarInt::Auth, 0); - assert_eq!(from_setup(¶ms), Some(false)); - } - - #[test] - fn auth_round_trips() { - for token in [Bytes::new(), Bytes::from_static(b"eyJhbGciOi.jwt")] { - let msg = Auth { - request_id: RequestId(4), - token, - }; - assert_eq!(round_trip(&msg), msg); - } - } - - /// `**` grants everything, the empty pattern only the root, and the empty list - /// nothing; literals and wildcards travel exactly, never widened to a prefix. - #[test] - fn auth_ok_round_trips() { - for (publish, subscribe, expires) in [ - (patterns(&["**"]), patterns(&[]), None), - (patterns(&[""]), patterns(&["room/**"]), None), - ( - patterns(&["ch1", "room/*/cam", "**/demo.hang"]), - patterns(&["live/*", "lobby/cam-*.hang"]), - Some(Duration::from_secs(60)), - ), - ] { - let msg = AuthOk { - publish, - subscribe, - expires, - }; - assert_eq!(round_trip(&msg), msg); - } - } - - /// Each pattern is its canonical text, in canonical order, as lite's AUTH_OK spells it. - /// The exact bytes, shared with `js/net/src/ietf/auth.test.ts` so both encoders agree. - #[test] - fn auth_ok_golden() { - let msg = AuthOk { - publish: patterns(&["room/*/cam", "ch1"]), - subscribe: patterns(&[""]), - expires: None, - }; - let buf = encode_msg(&msg, VERSION).unwrap(); - assert_eq!(&buf[..], b"\x02\x03ch1\x0aroom/*/cam\x01\x00\x00"); - } - - #[test] - fn auth_error_round_trips() { - let msg = AuthError { - code: UNAUTHORIZED, - reason: "expired".to_string(), - }; - assert_eq!(round_trip(&msg), msg); - } - - /// An oversized reason is refused from its length prefix, before any of it arrives. - #[test] - fn auth_error_reason_is_capped_by_its_prefix() { - let mut buf = Vec::new(); - let mut w = Encoder::new(&mut buf, VERSION.into()); - w.varint(UNAUTHORIZED).unwrap(); - w.varint(MAX_REASON as u64 + 1).unwrap(); - let err = AuthError::decode_msg(&mut Decoder::new(&buf, VERSION.into()), VERSION).unwrap_err(); - assert!(matches!(err, DecodeError::InvalidValue), "{err:?}"); - } - - /// A reply with bytes past its message is refused, like any other framed message. - #[test] - fn replies_with_trailing_bytes_are_refused() { - let ok = AuthOk { - publish: patterns(&["room"]), - subscribe: Patterns::new(), - expires: None, - }; - let refused = AuthError { - code: UNAUTHORIZED, - reason: "expired".to_string(), - }; - let body = |msg: Vec, extra: &[u8]| super::super::Body(Bytes::from([&msg[..], extra].concat())); - - let exact = encode_msg(&ok, VERSION).unwrap(); - assert_eq!(decode_reply::(&body(exact.clone(), &[]), VERSION).unwrap(), ok); - let err = decode_reply::(&body(exact, &[0]), VERSION).unwrap_err(); - assert!(matches!(err, Error::WrongSize), "{err:?}"); - - let exact = encode_msg(&refused, VERSION).unwrap(); - assert_eq!( - decode_reply::(&body(exact.clone(), &[]), VERSION).unwrap(), - refused - ); - let err = decode_reply::(&body(exact, &[0]), VERSION).unwrap_err(); - assert!(matches!(err, Error::WrongSize), "{err:?}"); - } - - /// NOT_SUPPORTED is the acceptor unable to tell a grant, never a refusal. - #[test] - fn not_supported_is_unsupported() { - assert!(matches!(from_code(NOT_SUPPORTED), Error::Unsupported)); - assert!(matches!( - from_code(UNAUTHORIZED), - Error::Session(SessionError::Unauthorized) - )); - assert_eq!(to_code(SessionError::Unauthorized), UNAUTHORIZED); - assert!(matches!(from_code(EXPIRED_AUTH_TOKEN), Error::Expired)); - assert_eq!(to_code(SessionError::Expired), EXPIRED_AUTH_TOKEN); - assert!(matches!( - from_code(MALFORMED_AUTH_TOKEN), - Error::Session(SessionError::MalformedAuthToken) - )); - assert_eq!(to_code(SessionError::MalformedAuthToken), MALFORMED_AUTH_TOKEN); - } -} diff --git a/rs/moq-net/src/ietf/cluster.rs b/rs/moq-net/src/ietf/cluster.rs deleted file mode 100644 index 96183b57a6d..00000000000 --- a/rs/moq-net/src/ietf/cluster.rs +++ /dev/null @@ -1,536 +0,0 @@ -//! The MoQ Cluster extension (draft-lcurley-moq-cluster-01). -//! -//! moq-transport carries no routing information, so a mesh of relays gossiping -//! namespaces loops forever and has no basis for choosing between two peers -//! advertising the same namespace. This extension adds it: -//! -//! - each endpoint declares its own [`Hop`](crate::Hop) (Hop ID) via the -//! HOP_ID Setup Option, which is also what negotiates the extension; -//! - each endpoint prices what subscribing from it costs via the RELAY_COST Setup -//! Option, so the two directions are priced independently; -//! - every advertisement carries the HOP_PATH it traversed and the accumulated -//! ROUTE_COST of that path, as Key-Value-Pair message parameters. -//! -//! The semantics are the same ones moq-lite carries natively (see -//! [`crate::origin::RouteInfo`]); this module is only the moq-transport binding. - -use crate::coding::{Decode, DecodeError, Decoder, Encode, EncodeError, Encoder}; -use crate::{Hop, Hops}; - -use super::{Param, Version}; - -/// HOP_ID Setup Option: the sender's own Hop ID, and the signal that it speaks this -/// extension. Even, so the value is a bare varint. -pub const HOP_ID: u64 = 0x40B54; - -/// RELAY_COST Setup Option: what subscribing from the sender costs. Even, so the value -/// is a bare varint. Directional, so each endpoint declares its own. -pub const RELAY_COST: u64 = 0x40B56; - -/// HOP_PATH message parameter: the ordered Hop IDs an advertisement traversed. -/// Odd, so the value is a length-prefixed byte string of varints. -pub const HOP_PATH: u64 = 0x40B57; - -/// ROUTE_COST message parameter: the accumulated cost of the advertised path. -/// Even, so the value is a bare varint. -pub const ROUTE_COST: u64 = 0x40B58; - -/// The cost of pulling across a direction nobody priced. -/// -/// One, so an unpriced mesh accumulates a route cost equal to the hop count and -/// ranks routes by shortest path. Zero is meaningful and distinct from absent: it -/// makes that direction free, which is how a deployment describes two relays in the -/// same datacenter. -pub const DEFAULT_COST: u64 = 1; - -/// The ordered list of Hop IDs an advertisement has traversed, from the original -/// publisher to the relay immediately upstream of the receiver. -/// -/// The HOP_PATH parameter value: bare varints filling the parameter length, with no -/// count of their own. That is the only thing separating this from the moq-lite hop -/// chain, which counts its entries; the list itself is the same -/// [`Hops`](crate::Hops). -#[derive(Clone, Debug, Default, PartialEq, Eq)] -pub struct HopPath(Hops); - -impl HopPath { - /// Wrap a hop chain. - /// - /// [`Hops`] already refuses a repeated non-zero entry, so the only wire rule - /// left to check is that the list is non-empty; [`Self::validate`] does that on - /// decode, where an empty parameter can arrive. - pub fn new(hops: Hops) -> Self { - Self(hops) - } - - /// Borrow the hop chain. - pub fn hops(&self) -> &Hops { - &self.0 - } - - /// Reject a path that cannot have come from a conforming sender. - /// - /// Only the empty list is left to catch: the other rule, that a non-zero Hop ID may - /// not appear twice, is enforced by [`Hops`] wherever a chain is built, so it - /// holds on the outbound path too rather than only where one was parsed. - fn validate(&self) -> Result<(), DecodeError> { - match self.0.is_empty() { - true => Err(DecodeError::InvalidValue), - false => Ok(()), - } - } -} - -impl Param for HopPath { - fn param_encode(&self, w: &mut Encoder<'_>, version: Version) -> Result<(), EncodeError> { - // The entries fill the value, so they are written with no count and framed by - // the parameter's own length prefix. - let mut buf = Vec::new(); - let mut inner = Encoder::new(&mut buf, w.form()); - for hop in &self.0 { - hop.encode(&mut inner, version)?; - } - w.bytes(&buf) - } - - fn param_decode(r: &mut Decoder<'_>, version: Version) -> Result { - let mut buf = Decoder::new(r.bytes()?, r.form()); - - let mut hops = Hops::new(); - while !buf.is_empty() { - // A short read here means the entries did not exactly fill the length. - hops.push(Hop::decode(&mut buf, version)?)?; - } - - let path = Self(hops); - path.validate()?; - Ok(path) - } -} - -/// The cluster parameters carried on one advertisement. -/// -/// Present on every PUBLISH_NAMESPACE and NAMESPACE of a session that negotiated the -/// extension, and on none of a session that did not. -#[derive(Clone, Debug, Default, PartialEq, Eq)] -pub struct Advert { - /// HOP_PATH: the path this advertisement traversed, publisher first. - pub hops: HopPath, - - /// ROUTE_COST: the marginal cost of subscribing via this advertisement. Absent on - /// the wire means 0, so an endpoint that prices nothing sends nothing. - pub cost: u64, -} - -impl Advert { - /// Build the advertisement to send for a route, appending our own Hop ID. - /// - /// A relay's own ID is always the last entry, so a receiver reconstructs the full - /// path without the sender restating it. - /// - /// Fails for a chain this cannot be legally appended to: one already at - /// [`MAX_HOPS`](crate::InvalidHop::TooMany), which a real path never reaches and a - /// loop does, or one that already names us. Either is a loop, and the caller's job - /// is to advertise a different route rather than send this one: a receiver must - /// close the session over a repeated Hop ID, so a malformed chain we build costs - /// someone else their session rather than degrading our own routing. - pub fn forward(hops: &Hops, cost: u64, self_hop: Hop) -> Result { - let mut hops = hops.clone(); - hops.push(self_hop)?; - Ok(Self { - hops: HopPath::new(hops), - cost, - }) - } - - /// Whether this advertisement looped back through `self_hop`. - /// - /// A receiver discards such an advertisement: forwarding it would extend the loop, - /// and subscribing through it would route the receiver back to itself. Hop ID 0 - /// identifies nothing, so a receiver that withheld its own identity detects nothing. - pub fn loops(&self, self_hop: Hop) -> bool { - self_hop != Hop::UNKNOWN && self.hops.hops().contains(&self_hop) - } - - /// The route this advertisement describes, after charging the link it arrived on. - /// - /// The addition saturates rather than wraps, so an absurd upstream value ranks last - /// instead of overflowing to best. - /// - /// Add the arriving link price to the peer's static route cost. - pub fn route(&self, link_cost: u64) -> crate::origin::RouteInfo { - let advertised = crate::origin::Cost::new(self.cost); - // The prefix travels separately: it is stamped where the advertisement - // attaches (the namespace). - crate::origin::RouteInfo::new() - .with_hops(self.hops.hops().clone()) - .with_cost(advertised.charged(link_cost)) - } -} - -/// What the peer declared in its SETUP. -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] -pub struct Peer { - /// What the peer put in HOP_ID, or `None` when it did not negotiate the - /// extension. Read [`Self::identity`] for the identity; this field answers whether - /// the extension is on, which is a different question when the peer declared 0. - pub hop: Option, - - /// What the peer said subscribing from it costs, or `None` when it priced nothing - /// (meaning [`DEFAULT_COST`]). Directional: this prices what we pull from the peer, - /// while our own declaration prices the other way, and the two need not match. - pub cost: Option, -} - -impl Peer { - /// Whether the peer negotiated the extension, which is what decides the NAMESPACE - /// encoding. True even for a peer that declared the reserved 0. - pub fn negotiated(&self) -> bool { - self.hop.is_some() - } - - /// The identity the peer declared, or `None` when it declared none. - /// - /// Declaring the reserved 0 turns the extension on while withholding an identity, - /// which reads here exactly like declaring nothing at all. The receiver may assign - /// one as local selection state (`Route.via`); it is never written into HOP_PATH. - pub fn identity(&self) -> Option { - self.hop.filter(|hop| *hop != Hop::UNKNOWN) - } -} - -/// What pulling content across this link costs, added to the route cost of every -/// advertisement received over it. -/// -/// RELAY_COST is directional: each endpoint declares what subscribing from *it* costs, -/// so the peer's declaration is what prices this direction. `local` overrides it, since -/// what we charge our own routing is local policy and a peer should not be able to -/// reprice our mesh unilaterally. Falls back to [`DEFAULT_COST`] when neither priced it. -pub fn link_cost(local: Option, peer: &Peer) -> u64 { - local.or(peer.cost).unwrap_or(DEFAULT_COST) -} - -/// Read the cluster Setup Options out of a decoded SETUP parameter block. -pub fn peer_from_setup(params: &super::Parameters) -> Result { - // 0 is legal here: the peer speaks the extension but withholds its identity. - let hop = params.get_varint(super::ParameterVarInt::HopId).map(Hop::from_wire); - - Ok(Peer { - hop, - cost: params.get_varint(super::ParameterVarInt::RelayCost), - }) -} - -/// Write our cluster Setup Options into a SETUP parameter block. -/// -/// `cost` is the price we put on this link, which only the dialing side declares. -pub fn peer_into_setup(params: &mut super::Parameters, self_hop: Hop, cost: Option) { - params.set_varint(super::ParameterVarInt::HopId, self_hop.id()); - - if let Some(cost) = cost { - params.set_varint(super::ParameterVarInt::RelayCost, cost); - } -} - -#[cfg(test)] -mod tests { - use super::*; - - const VERSION: Version = Version::Draft18; - - fn hop(id: u64) -> Hop { - Hop::new(id).unwrap() - } - - fn hop_path(ids: &[u64]) -> HopPath { - let hops = ids - .iter() - .map(|&id| match id { - 0 => Hop::UNKNOWN, - id => hop(id), - }) - .collect::>(); - HopPath::new(Hops::try_from(hops).unwrap()) - } - - fn round_trip(path: &HopPath) -> Result { - let mut buf = Vec::new(); - path.param_encode(&mut Encoder::new(&mut buf, VERSION.into()), VERSION) - .unwrap(); - let mut bytes = bytes::Bytes::from(buf); - let decoded = crate::coding::decode_buf(&mut bytes, VERSION, HopPath::param_decode)?; - assert!(bytes.is_empty(), "trailing bytes after decode"); - Ok(decoded) - } - - #[test] - fn code_point_parity() { - // The parity is load-bearing: odd values are length-prefixed, even ones bare varints. - assert_eq!(HOP_ID % 2, 0); - assert_eq!(RELAY_COST % 2, 0); - assert_eq!(HOP_PATH % 2, 1); - assert_eq!(ROUTE_COST % 2, 0); - } - - #[test] - fn hop_path_round_trips() { - let path = hop_path(&[7, 9, 11]); - assert_eq!(round_trip(&path).unwrap(), path); - } - - #[test] - fn hop_path_has_no_inner_count() { - // The entries fill the parameter length; a count would make us unreadable to - // every other implementation. One byte of length plus one byte per small varint. - let mut buf = Vec::new(); - hop_path(&[1, 2, 3]) - .param_encode(&mut Encoder::new(&mut buf, VERSION.into()), VERSION) - .unwrap(); - assert_eq!(buf.to_vec(), vec![0x03, 0x01, 0x02, 0x03]); - } - - #[test] - fn hop_path_allows_repeated_zero() { - // 0 identifies nothing, so two unknown hops are not a loop. - let path = hop_path(&[0, 5, 0]); - assert_eq!(round_trip(&path).unwrap(), path); - } - - #[test] - fn hop_path_rejects_repeated_hop() { - // A non-zero id appearing twice is a loop, which the receiver must reject. The - // bytes are forged rather than encoded from a `HopPath`, because `Hops` - // no longer lets one be built: only a non-conforming sender produces this. - let mut value = Vec::new(); - for id in [4u64, 8, 4] { - hop(id) - .encode(&mut Encoder::new(&mut value, VERSION.into()), VERSION) - .unwrap(); - } - - let mut buf = Vec::new(); - Encoder::new(&mut buf, VERSION.into()).bytes(&value).unwrap(); - - let mut bytes = bytes::Bytes::from(buf); - assert!(matches!( - crate::coding::decode_buf(&mut bytes, VERSION, HopPath::param_decode), - Err(DecodeError::InvalidValue) - )); - } - - /// The rule the receiver enforces has to hold on the way out too. A chain that - /// already names us, or one with no room left, cannot be appended to, and the - /// caller's job is to advertise a different route rather than send this one: a - /// receiver must close the session over a repeated Hop ID, so a malformed chain we - /// build costs someone else their session rather than degrading our own routing. - #[test] - fn forward_refuses_a_chain_it_cannot_extend() { - let mut hops = Hops::new(); - hops.push(hop(1)).unwrap(); - hops.push(hop(3)).unwrap(); - - assert_eq!( - Advert::forward(&hops, 5, hop(3)), - Err(crate::InvalidHop::Duplicate), - "appending an id the chain already names must not produce an advertisement" - ); - - // Fill the chain with distinct ids, so the next append fails on length rather - // than on the id already being there. - let mut full = Hops::new(); - let mut next = 1; - while full.push(hop(next)).is_ok() { - next += 1; - } - assert_eq!(Advert::forward(&full, 0, hop(next)), Err(crate::InvalidHop::TooMany)); - } - - #[test] - fn hop_path_rejects_empty() { - // The list always has at least one entry: the original publisher. - let mut buf = Vec::new(); - HopPath::default() - .param_encode(&mut Encoder::new(&mut buf, VERSION.into()), VERSION) - .unwrap(); - let mut bytes = bytes::Bytes::from(buf); - assert!(matches!( - crate::coding::decode_buf(&mut bytes, VERSION, HopPath::param_decode), - Err(DecodeError::InvalidValue) - )); - } - - #[test] - fn hop_path_rejects_partial_entry() { - // Entries must exactly fill Length. Chop the last byte off a multi-byte hop id - // and shrink the length to match, so the value ends mid-varint. - let mut value = Vec::new(); - hop(300) - .encode(&mut Encoder::new(&mut value, VERSION.into()), VERSION) - .unwrap(); - assert!(value.len() > 1, "300 should not fit in one byte"); - value.pop(); - - let mut buf = Vec::new(); - Encoder::new(&mut buf, VERSION.into()).bytes(&value).unwrap(); - - let mut bytes = bytes::Bytes::from(buf); - assert!(crate::coding::decode_buf(&mut bytes, VERSION, HopPath::param_decode).is_err()); - } - - #[test] - fn forward_appends_self() { - let mut hops = Hops::new(); - hops.push(hop(1)).unwrap(); - hops.push(hop(2)).unwrap(); - - let advert = Advert::forward(&hops, 5, hop(3)).unwrap(); - assert_eq!(advert.hops, hop_path(&[1, 2, 3])); - assert_eq!(advert.hops.hops().iter().next().copied(), Some(hop(1))); - assert_eq!(advert.cost, 5); - } - - #[test] - fn loops_ignores_unknown() { - let advert = Advert { - hops: hop_path(&[0, 5]), - cost: 0, - }; - // A receiver whose own id is 0 cannot detect loops through itself. - assert!(!advert.loops(Hop::UNKNOWN)); - assert!(advert.loops(hop(5))); - assert!(!advert.loops(hop(6))); - } - - #[test] - fn route_charges_the_link_and_saturates() { - let advert = Advert { - hops: hop_path(&[1, 2]), - cost: 4, - }; - let route = advert.route(3); - assert_eq!(route.cost.value(), 7); - assert_eq!(&route.hops, hop_path(&[1, 2]).hops()); - - let absurd = Advert { - hops: hop_path(&[1]), - cost: u64::MAX, - }; - assert_eq!(absurd.route(10).cost.value(), crate::origin::Cost::MAX.value()); - } - - /// Negotiating the extension and declaring an identity are separate questions, and a - /// peer that declared the reserved 0 answers them differently: the extension is on, - /// so the NAMESPACE encoding changes, but it named nobody, so it reads exactly like a - /// peer that declared nothing and the receiver assigns an identity instead. - #[test] - fn declared_zero_negotiates_without_an_identity() { - let peer = Peer::default(); - assert!(!peer.negotiated()); - assert_eq!(peer.identity(), None); - - let anonymous = Peer { - hop: Some(Hop::UNKNOWN), - cost: Some(0), - }; - assert!(anonymous.negotiated()); - assert_eq!(anonymous.identity(), None); - - let named = Peer { - hop: Some(hop(9)), - cost: None, - }; - assert!(named.negotiated()); - assert_eq!(named.identity(), Some(hop(9))); - } - - #[test] - fn link_cost_prefers_local_policy_over_the_peer() { - let unpriced = Peer::default(); - let priced = Peer { - hop: Some(hop(9)), - cost: Some(7), - }; - let free = Peer { - hop: Some(hop(9)), - cost: Some(0), - }; - - // The peer prices its own egress, so absent local policy that is what - // pulling from it costs, whichever side dialed. - assert_eq!(link_cost(None, &priced), 7); - // Zero is a price, not "unset": the peer declared this direction free. - assert_eq!(link_cost(None, &free), 0); - // Local policy wins: a peer cannot reprice our routing by declaring a - // cheaper egress than we are willing to believe. - assert_eq!(link_cost(Some(3), &priced), 3); - assert_eq!(link_cost(Some(0), &priced), 0); - // Nobody priced it, so this direction ranks by hop count. - assert_eq!(link_cost(None, &unpriced), DEFAULT_COST); - } - - #[test] - fn setup_options_round_trip() { - for (self_hop, cost) in [ - (hop(42), Some(0)), - (hop(42), Some(9)), - (hop(u64::MAX), None), - (Hop::UNKNOWN, None), - ] { - let mut params = super::super::Parameters::default(); - peer_into_setup(&mut params, self_hop, cost); - - let mut buf = Vec::new(); - params - .encode(&mut Encoder::new(&mut buf, VERSION.into()), VERSION) - .unwrap(); - let mut bytes = bytes::Bytes::from(buf); - let decoded = - crate::coding::decode_buf(&mut bytes, VERSION, |r, v| super::super::Parameters::decode(r, v)).unwrap(); - - let peer = peer_from_setup(&decoded).unwrap(); - assert_eq!(peer.hop, Some(self_hop)); - assert_eq!(peer.cost, cost); - } - } - - #[test] - fn hop_id_is_a_bare_varint() { - // The key sits at delta 0x40B54 from the start of the block and the value follows - // it directly: a length byte here would make us unreadable to every -01 peer. - let mut params = super::super::Parameters::default(); - peer_into_setup(&mut params, hop(42), None); - - let mut buf = Vec::new(); - params - .encode(&mut Encoder::new(&mut buf, VERSION.into()), VERSION) - .unwrap(); - assert_eq!(buf.to_vec(), vec![0xC4, 0x0B, 0x54, 0x2A]); - } - - #[test] - fn setup_without_hop_id_is_not_negotiated() { - let params = super::super::Parameters::default(); - let peer = peer_from_setup(¶ms).unwrap(); - assert!(!peer.negotiated()); - } - - #[test] - fn setup_with_only_relay_hops_is_not_negotiated() { - // A -00 peer declares its Hop ID under the odd key 0x40B55 as a length-prefixed - // varint. That key is unknown now, so the session runs as plain moq-transport - // rather than misreading the value. - let mut params = super::super::Parameters::default(); - params.set_bytes(super::super::ParameterBytes::Unknown(0x40B55), vec![0x2A]); - - let mut buf = Vec::new(); - params - .encode(&mut Encoder::new(&mut buf, VERSION.into()), VERSION) - .unwrap(); - let mut bytes = bytes::Bytes::from(buf); - let decoded = - crate::coding::decode_buf(&mut bytes, VERSION, |r, v| super::super::Parameters::decode(r, v)).unwrap(); - - let peer = peer_from_setup(&decoded).unwrap(); - assert!(!peer.negotiated()); - } -} diff --git a/rs/moq-net/src/ietf/hidden.rs b/rs/moq-net/src/ietf/hidden.rs deleted file mode 100644 index 4336c0ea100..00000000000 --- a/rs/moq-net/src/ietf/hidden.rs +++ /dev/null @@ -1,43 +0,0 @@ -//! The MoQ Hidden extension (draft-lcurley-moq-hidden-00). -//! -//! A namespace with a segment starting with `.` below the prefix a subscription asked -//! for is hidden: it is left out of discovery unless the subscription opts in. The -//! opt-in is the HIDDEN parameter on SUBSCRIBE_NAMESPACE, and since an unknown -//! parameter fails decoding, it is only sent to a peer whose SETUP carried the HIDDEN -//! option. Peers that did not declare the option receive every namespace. -//! The rule itself is the one moq-lite applies (see -//! [`crate::origin::Consumer::with_hidden`]). - -use super::Version; - -/// HIDDEN Setup Option: the sender understands the HIDDEN parameter. Even, so the value -/// is a bare varint. -pub const HIDDEN: u64 = 0x40B5C; - -/// HIDDEN SUBSCRIBE_NAMESPACE parameter: also advertise hidden namespaces. Even, so the -/// value is a bare varint. -pub const HIDDEN_PARAM: u64 = 0x40B5E; - -/// Whether the peer understands the HIDDEN parameter. -pub fn from_setup(params: &super::Parameters, _version: Version) -> bool { - params.get_varint(super::ParameterVarInt::Hidden).is_some() -} - -/// Declare that we understand the HIDDEN parameter. -pub fn into_setup(params: &mut super::Parameters, _version: Version) { - params.set_varint(super::ParameterVarInt::Hidden, 1); -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn every_version_round_trips() { - let version = Version::Draft22; - let mut params = super::super::Parameters::default(); - assert!(!from_setup(¶ms, version)); - into_setup(&mut params, version); - assert!(from_setup(¶ms, version)); - } -} diff --git a/rs/moq-net/src/ietf/message.rs b/rs/moq-net/src/ietf/message.rs index f0e16ec4f3a..9fba2507028 100644 --- a/rs/moq-net/src/ietf/message.rs +++ b/rs/moq-net/src/ietf/message.rs @@ -10,7 +10,6 @@ pub(super) fn control(id: u64) -> crate::stats::Message { TrackStatus::ID | SubscribeTracks::ID | PublishSkipped::ID => Class::TrackInfo, Subscribe::ID | SubscribeOk::ID | Publish::ID | PublishOk::ID | PublishDone::ID => Class::Subscribe, Fetch::ID | FetchOk::ID => Class::Fetch, - auth::Auth::ID | auth::AuthOk::ID | auth::AuthError::ID => Class::Auth, GoAway::ID => Class::Goaway, _ => Class::Other, } diff --git a/rs/moq-net/src/ietf/mod.rs b/rs/moq-net/src/ietf/mod.rs index bfc349d728a..a936aeda57f 100644 --- a/rs/moq-net/src/ietf/mod.rs +++ b/rs/moq-net/src/ietf/mod.rs @@ -15,9 +15,6 @@ #[macro_use] mod parameters; -pub mod active_count; -pub(crate) mod auth; -pub mod cluster; #[cfg(feature = "async")] mod control; mod datagram; @@ -27,7 +24,6 @@ mod filter; pub(crate) mod flate; mod goaway; mod group; -pub mod hidden; mod location; pub(crate) mod max_age; pub mod message; @@ -46,7 +42,6 @@ mod request_stream; mod request_update; #[cfg(feature = "async")] mod session; -pub mod solicit; mod subscribe; mod subscribe_namespace; mod subscribe_tracks; @@ -87,37 +82,3 @@ pub(crate) use { request_stream::{Framed, NamespaceUpdate, Next, Update}, subscriber::GroupIngest, }; - -#[cfg(test)] -mod tests { - use super::*; - - /// Our Setup Options, pinned to the values the extension drafts register. A renumber - /// still round trips against ourselves but breaks every other implementation. - #[test] - fn setup_option_code_points() { - for (name, key, spec) in [ - ("HOP_ID", cluster::HOP_ID, 0x40B54), - ("RELAY_COST", cluster::RELAY_COST, 0x40B56), - ("SOLICIT", solicit::SOLICIT, 0x40B5A), - ("HIDDEN", hidden::HIDDEN, 0x40B5C), - ("AUTH", auth::AUTH, 0x40B70), - ("ACTIVE_COUNT", active_count::ACTIVE_COUNT, 0x40B64), - ] { - assert_eq!(key, spec, "{name}"); - } - } - - /// Our message parameters, pinned to the values the extension drafts register. - #[test] - fn message_parameter_code_points() { - for (name, key, spec) in [ - ("HOP_PATH", cluster::HOP_PATH, 0x40B57), - ("ROUTE_COST", cluster::ROUTE_COST, 0x40B58), - ("HIDDEN", hidden::HIDDEN_PARAM, 0x40B5E), - ("ACTIVE_COUNT", active_count::ACTIVE_COUNT_PARAM, 0x40B66), - ] { - assert_eq!(key, spec, "{name}"); - } - } -} diff --git a/rs/moq-net/src/ietf/parameters.rs b/rs/moq-net/src/ietf/parameters.rs index 38a1e7d3970..e68facec961 100644 --- a/rs/moq-net/src/ietf/parameters.rs +++ b/rs/moq-net/src/ietf/parameters.rs @@ -17,18 +17,6 @@ pub enum ParameterVarInt { MaxAuthTokenCacheSize = 4, /// MAX_REQUEST_UPDATES, added in draft-22. MaxRequestUpdates = super::request_update::OPTION, - /// HOP_ID, from the MoQ Cluster extension. - HopId = super::cluster::HOP_ID, - /// RELAY_COST, from the MoQ Cluster extension. - RelayCost = super::cluster::RELAY_COST, - /// SOLICIT, from the MoQ Solicit extension. - Solicit = super::solicit::SOLICIT, - /// HIDDEN, from the MoQ Hidden extension. - Hidden = super::hidden::HIDDEN, - /// AUTH, from the MoQ Auth extension. - Auth = super::auth::AUTH, - /// ACTIVE_COUNT, from the MoQ Active Count extension. - ActiveCount = super::active_count::ACTIVE_COUNT, /// Subscriber max age, from the MoQ Timestamp extension. MaxAge = super::max_age::OPTION, /// DEFLATE-compressed tracks, from the MoQ Flate extension. diff --git a/rs/moq-net/src/ietf/peer.rs b/rs/moq-net/src/ietf/peer.rs index 011ea9ec76c..cd6350b6194 100644 --- a/rs/moq-net/src/ietf/peer.rs +++ b/rs/moq-net/src/ietf/peer.rs @@ -1,35 +1,14 @@ //! What the peer declared in its SETUP, and the slot the session tasks read it from. -use super::cluster; - /// The Setup Options the peer sent us. /// -/// One value per session: both extensions ride the same SETUP, so they arrive together -/// and are read together. +/// One value per session: the options ride the same SETUP, so they arrive together and +/// are read together. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] pub(crate) struct Peer { - /// The MoQ Cluster options: the peer's Hop ID and what it charges to cross. - pub cluster: cluster::Peer, - - /// MoQ Solicit: whether the peer requires advertisements to be solicited, so an - /// unsolicited PUBLISH_NAMESPACE is unwanted. `None` when it declared nothing, which - /// is the one case where sending us one anyway is not a protocol violation. - pub solicit: Option, - - /// MoQ Hidden: whether the peer understands the HIDDEN parameter on - /// SUBSCRIBE_NAMESPACE, so we may send it. - pub hidden: bool, - /// Whether the peer understands subscriber max age (MoQ Timestamp). pub max_age: bool, - /// MoQ Auth: whether both sides negotiated the Auth request streams. - pub auth: bool, - - /// MoQ Active Count: whether the REQUEST_OK answering SUBSCRIBE_NAMESPACE counts the - /// NAMESPACE messages before the subscription is synced, in both directions. - pub active_count: bool, - /// Whether this peer offered DEFLATE-compressed tracks. pub flate: bool, } @@ -115,35 +94,19 @@ mod tests { use super::*; /// The announce loops read the peer's declaration once and hold it, while - /// subscription serving re-reads it. A second SETUP overwriting the identity would + /// subscription serving re-reads it. A second SETUP overwriting the declaration would /// split those two apart, so the first write is the one that counts. #[kio::test] async fn first_write_wins() { let first = Peer { - cluster: cluster::Peer { - hop: Some(crate::Hop::new(42).unwrap()), - cost: Some(3), - }, - solicit: None, - hidden: false, max_age: false, - auth: false, - active_count: false, flate: false, }; let slot = PeerSetup::default(); slot.set(first); slot.set(Peer { - cluster: cluster::Peer { - hop: Some(crate::Hop::new(99).unwrap()), - cost: Some(0), - }, - solicit: Some(true), - hidden: true, - max_age: false, - auth: true, - active_count: true, + max_age: true, flate: true, }); diff --git a/rs/moq-net/src/ietf/publish_namespace.rs b/rs/moq-net/src/ietf/publish_namespace.rs index 5dffa0ef2c8..c794a3e469e 100644 --- a/rs/moq-net/src/ietf/publish_namespace.rs +++ b/rs/moq-net/src/ietf/publish_namespace.rs @@ -1,6 +1,5 @@ //! IETF moq-transport publish namespace messages -use super::cluster; use crate::{Path, coding::*, ietf::RequestId}; use super::Message; @@ -9,29 +8,6 @@ use super::token::RequestToken; use super::Version; -/// Which advertisement extensions a session negotiated: what its PUBLISH_NAMESPACE and -/// NAMESPACE messages carry. -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] -pub struct Negotiated { - /// MoQ Cluster: every advertisement carries HOP_PATH, and may carry ROUTE_COST. - pub cluster: bool, -} - -impl Negotiated { - /// Whether NAMESPACE takes the extended form, with a Parameters field. - pub fn extended(&self) -> bool { - self.cluster - } -} - -/// The extension parameters one advertisement carries. -#[derive(Clone, Debug, Default, PartialEq, Eq)] -pub struct AdvertParams { - /// The MoQ Cluster parameters (see [`cluster`]). `Some` exactly on a session that - /// negotiated the extension. - pub cluster: Option, -} - /// PublishNamespace message (0x06) /// Sent by the publisher to announce the availability of a namespace. #[derive(Clone, Debug)] @@ -39,196 +15,65 @@ pub struct PublishNamespace { pub request_id: RequestId, pub track_namespace: Path, - /// The extension parameters, which a session that negotiated none leaves empty. - pub params: AdvertParams, - /// The `AUTHORIZATION TOKEN` (0x03) this request presents. At most one is /// accepted: decoding refuses a repeated or second token. pub authorization_token: Option, } -impl PublishNamespace { - /// Decode the message body, expecting the parameters of the extensions the session - /// negotiated. - /// - /// The negotiation is session state rather than anything in the message, so the - /// caller supplies it. A negotiated cluster session that omits HOP_PATH, or a - /// parameter of an extension the session did not negotiate, is a protocol violation, - /// which surfaces here as [`DecodeError::InvalidValue`]. - pub fn decode_body(r: &mut Decoder<'_>, version: Version, negotiated: Negotiated) -> Result { - let request_id = RequestId::decode(r, version)?; - let track_namespace = decode_namespace(r)?; - - decode_params!(r, version, - 0x03 => authorization_token: Vec, - cluster::HOP_PATH => hops: Option, - cluster::ROUTE_COST => cost: Option, - ); - let params = AdvertParams { - cluster: cluster_params(negotiated.cluster, hops, cost)?, - }; - // Last, so a malformed field closes the session before a token refusal. - let authorization_token = super::token::single(authorization_token)?; - - Ok(Self { - request_id, - track_namespace, - params, - authorization_token, - }) - } -} - impl<'a> Message<'a> for PublishNamespace { const ID: u64 = 0x06; fn encode_msg(&self, w: &mut Encoder<'_>, version: Version) -> Result<(), EncodeError> { self.request_id.encode(w, version)?; encode_namespace(w, &self.track_namespace)?; - // ROUTE_COST is optional and absent means 0, so a free path sends nothing. - let hops = self.params.cluster.as_ref().map(|advert| advert.hops.clone()); - let cost = self - .params - .cluster - .as_ref() - .and_then(|advert| (advert.cost != 0).then_some(advert.cost)); - encode_params!(w, version, - 0x03 => self.authorization_token.clone(), - cluster::HOP_PATH => hops, - cluster::ROUTE_COST => cost, - ); + encode_params!(w, version, 0x03 => self.authorization_token.clone()); Ok(()) } fn decode_msg(r: &mut Decoder<'a>, version: Version) -> Result { - Self::decode_body(r, version, Negotiated::default()) - } -} + let request_id = RequestId::decode(r, version)?; + let track_namespace = decode_namespace(r)?; + decode_params!(r, version, 0x03 => authorization_token: Vec); + let authorization_token = super::token::single(authorization_token)?; -/// The MoQ Cluster parameters of an advertisement, given what arrived. -/// -/// A negotiated session requires HOP_PATH, and ROUTE_COST absent means 0. An endpoint -/// must not send either on a session that did not negotiate, so either is a violation. -fn cluster_params( - negotiated: bool, - hops: Option, - cost: Option, -) -> Result, DecodeError> { - match negotiated { - true => Ok(Some(cluster::Advert { - hops: hops.ok_or(DecodeError::InvalidValue)?, - cost: cost.unwrap_or(0), - })), - false if hops.is_some() || cost.is_some() => Err(DecodeError::InvalidValue), - false => Ok(None), + Ok(Self { + request_id, + track_namespace, + authorization_token, + }) } } -/// REQUEST_UPDATE (0x02) on a PUBLISH_NAMESPACE: the cluster parameters that changed -/// (draft-lcurley-moq-cluster, Updating an Advertisement), or a renewed request token. -/// -/// An omitted parameter keeps its value (moq-transport Section 9.5), so a cost that -/// dropped to 0 is sent as an explicit 0, unlike the advertisement itself where absent -/// means 0. +/// REQUEST_UPDATE (0x02) on a PUBLISH_NAMESPACE: a renewed request token. #[derive(Clone, Debug, PartialEq, Eq)] pub struct PublishNamespaceUpdate { /// The update's own Request ID; every REQUEST_UPDATE consumes one. pub request_id: RequestId, - /// HOP_PATH, when the route changed. - pub hops: Option, - /// ROUTE_COST, when the cost changed. - pub cost: Option, /// The `AUTHORIZATION TOKEN` (0x03) renewing the advertisement's credential, if any. At /// most one is accepted: decoding refuses a repeated or second token. pub authorization_token: Option, } -impl PublishNamespaceUpdate { - /// The update that moves a peer holding `held` to `next`: only what changed. - pub fn between(request_id: RequestId, held: &cluster::Advert, next: &cluster::Advert) -> Self { - Self { - request_id, - hops: (held.hops != next.hops).then(|| next.hops.clone()), - cost: (held.cost != next.cost).then_some(next.cost), - authorization_token: None, - } - } - - /// The advertisement after this update is applied to `held`. - pub fn apply(&self, held: &cluster::Advert) -> cluster::Advert { - cluster::Advert { - hops: self.hops.clone().unwrap_or_else(|| held.hops.clone()), - cost: self.cost.unwrap_or(held.cost), - } - } -} - impl<'a> Message<'a> for PublishNamespaceUpdate { const ID: u64 = 0x02; fn encode_msg(&self, w: &mut Encoder<'_>, version: Version) -> Result<(), EncodeError> { self.request_id.encode(w, version)?; - encode_params!(w, version, - 0x03 => self.authorization_token.clone(), - cluster::HOP_PATH => self.hops, - cluster::ROUTE_COST => self.cost, - ); + encode_params!(w, version, 0x03 => self.authorization_token.clone()); Ok(()) } fn decode_msg(r: &mut Decoder<'a>, version: Version) -> Result { let request_id = RequestId::decode(r, version)?; - decode_params!(r, version, - 0x03 => authorization_token: Vec, - cluster::HOP_PATH => hops: Option, - cluster::ROUTE_COST => cost: Option, - ); + decode_params!(r, version, 0x03 => authorization_token: Vec); let authorization_token = super::token::single(authorization_token)?; Ok(Self { request_id, - hops, - cost, authorization_token, }) } } -/// Write the Parameters field of an extended NAMESPACE. -/// -/// ROUTE_COST is optional and absent means 0, so a free path sends nothing. -pub(super) fn encode_advert_params( - w: &mut Encoder<'_>, - version: Version, - params: &AdvertParams, -) -> Result<(), EncodeError> { - let hops = params.cluster.as_ref().map(|advert| advert.hops.clone()); - let cost = params - .cluster - .as_ref() - .and_then(|advert| (advert.cost != 0).then_some(advert.cost)); - encode_params!(w, version, - cluster::HOP_PATH => hops, - cluster::ROUTE_COST => cost, - ); - Ok(()) -} - -/// Read the Parameters field of an extended NAMESPACE. See [`encode_advert_params`]. -pub(super) fn decode_advert_params( - r: &mut Decoder<'_>, - version: Version, - negotiated: Negotiated, -) -> Result { - decode_params!(r, version, - cluster::HOP_PATH => hops: Option, - cluster::ROUTE_COST => cost: Option, - ); - - Ok(AdvertParams { - cluster: cluster_params(negotiated.cluster, hops, cost)?, - }) -} - #[cfg(test)] mod tests { use super::*; @@ -250,7 +95,6 @@ mod tests { let msg = PublishNamespace { request_id: RequestId(1), track_namespace: Path::from_static("test/broadcast"), - params: Default::default(), authorization_token: None, }; @@ -265,7 +109,6 @@ mod tests { let msg = PublishNamespace { request_id: RequestId(5), track_namespace: Path::from_static("v18/broadcast"), - params: Default::default(), authorization_token: None, }; @@ -276,47 +119,6 @@ mod tests { assert_eq!(decoded.track_namespace.as_str(), "v18/broadcast"); } - fn hop_path(ids: &[u64]) -> cluster::HopPath { - let hops: Vec = ids.iter().map(|&id| crate::Hop::new(id).unwrap()).collect(); - cluster::HopPath::new(crate::Hops::try_from(hops).unwrap()) - } - - /// An update carries only what changed, and a cost that dropped to 0 is an explicit 0: - /// REQUEST_UPDATE keeps an omitted parameter, so leaving it out would keep the old price. - #[test] - fn update_carries_only_the_change_and_an_explicit_zero() { - let held = cluster::Advert { - hops: hop_path(&[7, 9]), - cost: 4, - }; - let cheaper = cluster::Advert { - cost: 0, - ..held.clone() - }; - - let msg = PublishNamespaceUpdate::between(RequestId(2), &held, &cheaper); - assert_eq!(msg.hops, None, "an unchanged path is omitted"); - assert_eq!(msg.cost, Some(0), "a cost of 0 is sent explicitly"); - - for version in [Version::Draft18, Version::Draft20, Version::Draft22] { - let encoded = encode_message(&msg, version); - let decoded: PublishNamespaceUpdate = decode_message(&encoded, version).unwrap(); - assert_eq!(decoded, msg, "{version}"); - assert_eq!(decoded.apply(&held), cheaper, "{version}"); - } - - let rerouted = cluster::Advert { - hops: hop_path(&[7, 11]), - cost: 4, - }; - let msg = PublishNamespaceUpdate::between(RequestId(4), &held, &rerouted); - assert_eq!(msg.hops, Some(hop_path(&[7, 11]))); - assert_eq!(msg.cost, None, "an unchanged cost is omitted"); - let decoded: PublishNamespaceUpdate = - decode_message(&encode_message(&msg, Version::Draft18), Version::Draft18).unwrap(); - assert_eq!(decoded.apply(&held), rerouted); - } - #[test] fn test_announce_rejects_parameters() { #[rustfmt::skip] diff --git a/rs/moq-net/src/ietf/publisher.rs b/rs/moq-net/src/ietf/publisher.rs index 234f37f9229..eb11d421d13 100644 --- a/rs/moq-net/src/ietf/publisher.rs +++ b/rs/moq-net/src/ietf/publisher.rs @@ -23,7 +23,7 @@ use crate::{ track::{Order, Subscription}, }; -use super::{Message, Version, cluster, error::request, peer, request_update}; +use super::{Message, Version, error::request, peer, request_update}; /// The REQUEST_ERROR refusing a SUBSCRIBE with `err`. Its Retry Interval is `retry` when an /// upstream refusal asked for one, and derived from `err` otherwise. @@ -604,13 +604,13 @@ impl Watched { } /// What to advertise to this peer for one broadcast. -#[derive(Clone, Debug, Default, PartialEq, Eq)] +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] enum Advert { /// Nothing: every route loops through the peer or us, or none is announced. #[default] None, - /// The namespace, with its hop path and accumulated cost when MoQ Cluster is negotiated. - Some(ietf::AdvertParams), + /// The namespace. + Some, } impl Advert { @@ -618,14 +618,6 @@ impl Advert { fn wanted(&self) -> bool { !matches!(self, Self::None) } - - /// The parameters to put on the wire, as the message structs carry them. - fn params(&self) -> ietf::AdvertParams { - match self { - Self::Some(params) => params.clone(), - Self::None => Default::default(), - } - } } /// How long to wait for a stream to advertise one namespace on. @@ -699,8 +691,6 @@ impl Target { /// One announce loop's state: where its advertisements go, and what the peer holds. struct Namespaces { - /// What the peer declared in its SETUP, which decides what an advertisement carries. - peer: cluster::Peer, target: Target, /// Every announced broadcast under this loop's prefix. watched: HashMap, @@ -730,14 +720,11 @@ struct Namespaces { abandoned: Option>, /// The auth epoch last applied to `permit`. epoch: u64, - /// Whether this discovery cursor includes hidden namespaces. - hidden: bool, } impl Namespaces { - fn new(peer: cluster::Peer, target: Target) -> Self { + fn new(target: Target) -> Self { Self { - peer, target, watched: HashMap::new(), requests: HashMap::new(), @@ -749,16 +736,9 @@ impl Namespaces { verifying: None, abandoned: None, epoch: 0, - hidden: false, } } - /// What the peer negotiated for an advertisement's parameters. - fn negotiated(&self) -> ietf::Negotiated { - ietf::Negotiated { - cluster: self.peer.negotiated(), - } - } /// Whether we may advertise `path`. An unknown grant allows everything. fn permitted(&self, path: &crate::Path) -> bool { @@ -939,9 +919,7 @@ pub(super) struct Publisher { // which is what makes cross-session loop detection work. self_origin: crate::Hop, // The identity assigned to the peer (a fresh per-session id, or one the caller - // pinned with `with_peer_hop`), used when the peer declares - // none itself. A peer that negotiates the MoQ Cluster extension declares its own, - // which wins unless it withheld it as the reserved 0. + // pinned with `with_peer_hop`), since moq-transport carries none of its own. peer_hop: Option, // What the peer declared in its SETUP, filled when that stream is read. peer_setup: peer::PeerSetup, @@ -2779,33 +2757,14 @@ impl Publisher { self } - /// What the peer declared in its SETUP. - /// - /// Resolves only once the peer's SETUP arrives, because the extension changes the - /// NAMESPACE encoding: nothing can be advertised until we know whether the peer speaks it. - #[cfg_attr(feature = "async", kio::wrapper)] - fn poll_peer(&self, waiter: &kio::Waiter) -> Poll { - self.peer_setup.poll_get(waiter).map(|peer| peer.cluster) - } - - /// Whether the peer requires advertisements to be solicited, from the same SETUP. - /// - /// Blocks on it for the same reason [`Self::peer`] does: this decides whether the - /// first advertisement is sent unasked, so it cannot be guessed and corrected later. - fn poll_requires_solicitation(&self, waiter: &kio::Waiter) -> Poll { - self.peer_setup - .poll_get(waiter) - .map(|peer| peer.solicit.unwrap_or(false)) - } - /// The origin to serve this peer's subscriptions from: sources whose hop chain flows /// through the peer are excluded, so a subscription is never handed data that already /// flowed through the subscriber. /// - /// The same exclusion the announce path applies (see [`Self::select`]), which is what - /// keeps advertised paths truthful and prevents subscription cycles of any length. - pub(super) fn poll_serving_origin(&self, waiter: &kio::Waiter) -> Poll { - self.poll_peer(waiter).map(|peer| self.excluding(&peer)) + /// The same exclusion the announce path applies (see [`Self::advertising`]), which is + /// what keeps advertised paths truthful and prevents subscription cycles of any length. + pub(super) fn serving_origin(&self) -> origin::Consumer { + self.origin.clone().excluding(self.exclude()) } /// Resolve a request's broadcast while the peer still wants the answer, a FIN withdrawing it @@ -2821,56 +2780,32 @@ impl Publisher { kio::wait(|waiter| resolve.poll(waiter, self, stream)).await } - /// Our origin handle with [`Self::exclude`] applied, the view both the data plane - /// and the announce loops read this peer's routes through. - fn excluding(&self, peer: &cluster::Peer) -> origin::Consumer { - self.origin.clone().excluding(self.exclude(peer)) + /// `origin` as advertised to this peer: never its own routes back (split horizon), and + /// no hidden namespaces unless the publish origin opted in (the caller's choice for this + /// peer), since moq-transport has no way to ask for them. + fn advertising(&self, origin: origin::Consumer) -> origin::Consumer { + let hidden = self.origin.includes_hidden(); + origin.excluding(self.exclude()).discovery(hidden) } - /// `origin` as advertised to this peer: never its own routes back (split horizon), - /// and under MoQ Cluster only the routes with room for our hop under the origin's - /// cap, so selection falls back to a shorter route instead of withdrawing. - fn advertising(&self, origin: origin::Consumer, peer: &cluster::Peer) -> origin::Consumer { - let origin = origin.excluding(self.exclude(peer)); - match peer.negotiated() { - true => origin.forwardable(), - false => origin, - } - } - - /// The Hop ID whose paths must not be advertised (or served) back to this peer. - /// - /// A peer that declared an identity supplies its own; otherwise fall back to the one - /// we assigned it (a fresh per-session id, or one pinned with `Client::with_peer_hop` - /// or `Request::with_peer_hop`), since moq-transport carries no identity - /// of its own. A peer that declared the reserved 0 declared no identity, so it takes - /// the fallback like any other anonymous peer. - fn exclude(&self, peer: &cluster::Peer) -> crate::Hop { - peer.identity().or(self.peer_hop).unwrap_or(crate::Hop::UNKNOWN) + /// The Hop ID whose paths must not be advertised (or served) back to this peer: the one + /// we assigned it (a fresh per-session id, or one pinned with `Client::with_peer_hop` or + /// `Request::with_peer_hop`), since moq-transport carries no identity of its own. + fn exclude(&self) -> crate::Hop { + self.peer_hop.unwrap_or(crate::Hop::UNKNOWN) } /// Pick what to advertise to this peer for one route. /// /// The origin's announce cursor already filters routes through the peer - /// (control-plane split horizon); this only shapes what the wire can carry. - fn select(&self, route: &crate::origin::RouteInfo, peer: &cluster::Peer) -> Advert { + /// (control-plane split horizon). + fn select(&self, route: &crate::origin::RouteInfo) -> Advert { // A route that already passed through us is a reflection. The origin // filters these on receive, so this is defensive. if self.self_origin != crate::Hop::UNKNOWN && route.hops.contains(&self.self_origin) { return Advert::None; } - - if !peer.negotiated() { - return Advert::Some(ietf::AdvertParams { cluster: None }); - } - - let cost = route.cost.value(); - // Our own Hop ID is always the last entry, so the peer reconstructs the full - // path. A chain with no room left is a loop in all but name. - match cluster::Advert::forward(&route.hops, cost, self.self_origin) { - Ok(advert) => Advert::Some(ietf::AdvertParams { cluster: Some(advert) }), - Err(_) => Advert::None, - } + Advert::Some } /// The task serving an incoming bidi stream dispatched by the session. @@ -3326,7 +3261,7 @@ impl Publisher { writer: &mut Writer, largest: Option, ) -> Result<(), Error> { - writer.buffer_message(ietf::RequestOk::ID, &ietf::RequestOk { largest, active: None }) + writer.buffer_message(ietf::RequestOk::ID, &ietf::RequestOk { largest }) } /// Refuse a SUBSCRIBE, returning the delivery that ends its request stream. @@ -3537,7 +3472,6 @@ impl Publisher { let Some((advert, auth)) = self.offer(ns, suffix, path) else { return Ok(None); }; - let negotiated = ns.negotiated(); let Some(watch) = ns.watched.get(suffix) else { return Ok(None); }; @@ -3626,10 +3560,7 @@ impl Publisher { tracing::debug!(broadcast = %absolute, "namespace"); stream.writer.buffer_message( ietf::Namespace::ID, - &ietf::Namespace { - suffix: suffix.clone(), - params: negotiated.extended().then(|| advert.params()), - }, + &ietf::Namespace { suffix: suffix.clone() }, )?; } (false, true) => { @@ -3662,7 +3593,7 @@ impl Publisher { auth, wanted, } = advertise; - let refused = self.advertise_namespace(ns, &path, suffix.clone(), advert.params(), auth, stream)?; + let refused = self.advertise_namespace(ns, &path, suffix.clone(), auth, stream)?; let sent = Self::requested(ns, &suffix, advert); Self::record(ns, &suffix, wanted, refused, sent); Ok(()) @@ -3701,7 +3632,6 @@ impl Publisher { ns: &mut Namespaces, path: &crate::Path, suffix: crate::Path, - params: ietf::AdvertParams, auth: Option, stream: Option>, ) -> Result { @@ -3715,7 +3645,6 @@ impl Publisher { &ietf::PublishNamespace { request_id, track_namespace: path.clone(), - params, authorization_token: auth.as_ref().and_then(|auth| self.request_token(auth)), }, ) { @@ -3753,7 +3682,7 @@ impl Publisher { ) -> Option<(Advert, Option)> { let watch = ns.watched.get(suffix)?; let advert = match ns.permitted(path) { - true => self.select(&watch.route, &ns.peer), + true => self.select(&watch.route), false => Advert::None, }; let auth = match (&ns.target, advert.wanted()) { @@ -3802,13 +3731,11 @@ impl Publisher { } /// Send the update waiting at the front of the queue with `request_id`: REQUEST_UPDATE - /// on the request that carries the namespace, with only what changed since the peer's - /// copy. Its answer arrives through [`Self::poll_answer`] while the loop carries on. + /// on the request that carries the namespace, renaming its token. Its answer arrives + /// through [`Self::poll_answer`] while the loop carries on. /// /// One update is outstanding per request, which satisfies any MAX_REQUEST_UPDATES the - /// peer set without reading it. A different original publisher is an update like any - /// other: the receiver drains what it already serves from the old one and never splices - /// the two. + /// peer set without reading it. fn send_update(&self, ns: &mut Namespaces, prefix: &crate::Path, request_id: RequestId) -> Result<(), Error> { let suffix = ns.updates.pop_front().expect("allocated for a queued update"); let Some((advert, auth)) = self.offer(ns, &suffix, &prefix.rejoin(&suffix)) else { @@ -3818,26 +3745,17 @@ impl Publisher { return Ok(()); }; request.queued = false; - let mut update = match (advert.params().cluster, watch.sent.params().cluster) { - (Some(next), Some(held)) => ietf::PublishNamespaceUpdate::between(request_id, &held, &next), - _ => ietf::PublishNamespaceUpdate { - request_id, - hops: None, - cost: None, - authorization_token: None, - }, + let update = ietf::PublishNamespaceUpdate { + request_id, + authorization_token: auth.as_ref().and_then(|auth| self.request_token(auth)), }; - if auth != request.auth { - update.authorization_token = auth.as_ref().and_then(|auth| self.request_token(auth)); - } tracing::debug!(broadcast = %crate::path::Rooted(self.origin.root(), &request.path), "publish_namespace update"); let written = request .stream .writer .buffer_message(ietf::PublishNamespaceUpdate::ID, &update); if let Err(err) = written { - // An unencodable update ends only this advertisement; the retry re-offers it - // fresh, carrying the complete parameters. + // An unencodable update ends only this advertisement; the retry re-offers it fresh. tracing::debug!(%err, "publish_namespace update not sent"); self.withdraw_namespace(&mut ns.target, &mut ns.requests, suffix.clone())?; if let Some(watch) = ns.watched.get_mut(&suffix) { @@ -3989,14 +3907,6 @@ impl Publisher { let msg: ietf::RequestOk = body .message(self.version) .map_err(|err| request.messages.failed(err.into()))?; - // ACTIVE_COUNT only answers SUBSCRIBE_NAMESPACE (MoQ Active Count). - if msg.active.is_some() { - self.session.clone().close( - super::error::to_session_code(&crate::SessionError::ProtocolViolation), - "ACTIVE_COUNT on a PUBLISH_NAMESPACE answer", - ); - return Err(Error::ProtocolViolation); - } tracing::debug!(message = ?msg, "publish_namespace ok"); request.accepted = true; // Whatever changed while it was in flight goes out next. @@ -4203,19 +4113,15 @@ impl Publisher { .origin .scope(&*prefix, &scope) .unwrap_or_else(|_| self.origin.empty()); - *announced = self.advertising(origin, &ns.peer).discovery(ns.hidden).routes(); + *announced = self.advertising(origin).routes(); true } None => false, }; let stream = ns.target.stream().expect("update has a stream"); - stream.writer.buffer_message( - ietf::RequestOk::ID, - &ietf::RequestOk { - largest: None, - active: None, - }, - )?; + stream + .writer + .buffer_message(ietf::RequestOk::ID, &ietf::RequestOk { largest: None })?; Ok(moved) } @@ -4345,7 +4251,7 @@ impl Resolve { ) -> Poll, Error>> { let origin = wait!(Origin => match publisher.poll_withdrawn(&mut self.session, stream, self.fin_cancels, waiter) { Poll::Ready(()) => Poll::Ready(None), - Poll::Pending => publisher.poll_serving_origin(waiter).map(Some), + Poll::Pending => Poll::Ready(Some(publisher.serving_origin())), }); let Some(origin) = origin else { return Poll::Ready(Ok(None)); @@ -5645,8 +5551,6 @@ enum NamespaceWork { struct ForwardNamespaces { announced: crate::origin::Routes, prefix: crate::Path, - /// The routes counted before the loop started, applied ahead of any change. - initial: Vec, finished: bool, goaways: super::request_stream::Messages, /// Registered on the first poll, as the loop starts. @@ -5673,10 +5577,6 @@ struct ForwardNamespaces { enum ForwardState { Start, - /// Waiting on the peer's SETUP, and with MoQ Auth on the answer to the credential we - /// presented at setup, so the first advertisement, the initial set included, is already - /// checked against our grant. - Setup, /// Streaming updates (origin route (un)announces), until the peer closes its side. Run, /// The origin is gone and everything is withdrawn: finish the stream and wait for @@ -5694,12 +5594,10 @@ impl ForwardNamespaces { publisher: &Publisher, announced: crate::origin::Routes, prefix: crate::Path, - initial: Vec, ) -> Self { Self { announced, prefix, - initial, finished: false, goaways: super::request_stream::Messages::new(publisher.session.clone(), publisher.control.client()), withdrawing: None, @@ -5724,17 +5622,7 @@ impl ForwardNamespaces { if publisher.withdrawal.poll(&kio::Waiter::noop()).is_ready() { return Poll::Ready(Ok(())); } - self.state = ForwardState::Setup; - } - ForwardState::Setup => { - if ready!(publisher.peer_setup.poll_get(waiter)).auth { - ready!(publisher.auth.poll_setup_answered(waiter)); - } (ns.permit, ns.epoch) = publisher.permit_now(ns.gate.as_ref()); - self.work = std::mem::take(&mut self.initial) - .into_iter() - .map(|update| NamespaceWork::Apply(update, true)) - .collect(); self.state = ForwardState::Run; } ForwardState::Run => { @@ -6097,12 +5985,11 @@ impl NamespaceLoop { announced: crate::origin::Routes, prefix: crate::Path, ns: Namespaces, - initial: Vec, ) -> Self { Self { token: ns.gate.as_ref().and_then(|gate| gate.token_guard()), ns, - forward: Some(ForwardNamespaces::new(publisher, announced, prefix, initial)), + forward: Some(ForwardNamespaces::new(publisher, announced, prefix)), ended: None, } } @@ -6132,47 +6019,26 @@ impl NamespaceLoop { /// /// moq-transport itself says nothing about which of the two discovery messages a peer /// expects, and the peers that never send SUBSCRIBE_NAMESPACE are exactly the ones -/// expecting a publisher to announce itself, so the default has to be to announce. A -/// peer that would rather ask says so with the MoQ Solicit extension -/// ([`solicit`](super::solicit)), -/// and then this loop does nothing. -/// [`Publisher::serve_subscribe_namespace_stream`] also carries every -/// match, so a peer that did not ask hears each namespace both ways. A NAMESPACE is -/// discovery only, not a second route. +/// expecting a publisher to announce itself, so the default has to be to announce. +/// [`Publisher::serve_subscribe_namespace_stream`] also carries every match, so a peer +/// that asked hears each namespace both ways. A NAMESPACE is discovery only, not a second +/// route. Hidden namespaces are left out, since nothing on the wire can ask for them. pub(super) struct PublishNamespaces { publisher: Publisher, - /// Started once the peer's SETUP says to advertise unasked. - run: Option, + run: NamespaceLoop, } impl PublishNamespaces { pub(super) fn new(publisher: Publisher) -> Self { - Self { publisher, run: None } + // Split horizon, as the solicited loop applies it: never advertise a route back to + // the peer it came from. + let origin = publisher.advertising(publisher.origin.clone()); + let run = NamespaceLoop::new(&publisher, origin.routes(), crate::Path::empty(), Namespaces::new(Target::Requests)); + Self { publisher, run } } pub(super) fn poll(&mut self, waiter: &kio::Waiter) -> Poll> { - if self.run.is_none() { - if ready!(self.publisher.poll_requires_solicitation(waiter)) { - return Poll::Ready(Ok(())); - } - - // The cluster extension changes what an advertisement carries, so nothing can be - // sent until the peer's SETUP says whether it speaks it. - let peer = ready!(self.publisher.poll_peer(waiter)); - let hidden = ready!(self.publisher.peer_setup.poll_get(waiter)).hidden; - - // Split horizon, as the solicited loop applies it: never advertise a route back - // to the peer it came from. - let origin = self - .publisher - .advertising(self.publisher.origin.clone(), &peer) - .discovery(!hidden || self.publisher.origin.includes_hidden()); - - let ns = Namespaces::new(peer, Target::Requests); - let run = NamespaceLoop::new(&self.publisher, origin.routes(), crate::Path::empty(), ns, Vec::new()); - self.run = Some(run); - } - self.run.as_mut().expect("started").poll(waiter, &self.publisher) + self.run.poll(waiter, &self.publisher) } } @@ -6186,8 +6052,6 @@ struct NamespaceServe { /// The request stream, until the loop's [`Target`] carries it. stream: Option>, prefix: crate::Path, - /// Whether the requester asked for hidden namespaces too. - wants_hidden: bool, /// The request's gate, until the loop holds it. gate: Option, state: NamespaceServeState, @@ -6196,22 +6060,9 @@ struct NamespaceServe { enum NamespaceServeState { Start(Option), Admit(Admit), - /// The extension changes what an advertisement carries, so nothing can be sent until the - /// peer's SETUP says whether it speaks it. The same SETUP says whether the OK counts what - /// it sends first (MoQ Active Count). - Setup, - /// With MoQ Auth, the count waits for our grant and leaves out what it does not cover. - Count { - peer: cluster::Peer, - hidden: bool, - announced: crate::origin::Routes, - }, /// The acknowledgement, sent only while the token is live, even behind stream credit. Ok { - peer: cluster::Peer, - hidden: bool, announced: crate::origin::Routes, - initial: Vec, /// Taken once buffered. ok: Option, }, @@ -6231,35 +6082,11 @@ impl NamespaceServe { publisher, stream: Some(stream), prefix: msg.namespace, - wants_hidden: msg.hidden, gate: Some(gate), state: NamespaceServeState::Start(msg.authorization_token), } } - /// MoQ Active Count: take what is advertised now, so the OK can say how many NAMESPACE - /// messages carry it. They go out first, ahead of any change. - fn count(&mut self, peer: cluster::Peer, hidden: bool, mut announced: crate::origin::Routes) { - let gate = self.gate.as_ref().expect("held until the loop starts"); - let (permit, _) = self.publisher.permit_now(Some(gate)); - let initial = Publisher::snapshot(&mut announced); - let count = initial - .iter() - .filter(|update| permit.matches(self.prefix.rejoin(&update.prefix).as_str())) - .filter(|update| self.publisher.select(&update.route, &peer).wanted()) - .count(); - self.state = NamespaceServeState::Ok { - peer, - hidden, - announced, - initial, - ok: Some(ietf::RequestOk { - largest: None, - active: Some(count as u64), - }), - }; - } - fn poll(&mut self, waiter: &kio::Waiter) -> Poll> { loop { if let NamespaceServeState::Run(run) = &mut self.state { @@ -6297,11 +6124,6 @@ impl NamespaceServe { } } tracing::debug!(prefix = %crate::path::Rooted(self.publisher.origin.root(), &self.prefix), "subscribe_namespace stream"); - self.state = NamespaceServeState::Setup; - } - NamespaceServeState::Setup => { - let peer = ready!(self.publisher.poll_peer(waiter)); - let declared = ready!(self.publisher.peer_setup.poll_get(waiter)); let publisher = &self.publisher; let prefix = &self.prefix; @@ -6324,52 +6146,14 @@ impl NamespaceServe { // Register the split-horizon peer on the announce cursor too. The origin // model uses this exposure to park a reflected copy before it can replace // the source we are currently advertising to that peer. - let origin = publisher.advertising(origin, &peer); - - // Peers that declared MoQ Hidden filter namespaces unless they opted in. A - // publish origin that already opted in (the caller's choice for this peer) - // keeps them. - let hidden = !declared.hidden || self.wants_hidden || publisher.origin.includes_hidden(); - let origin = origin.discovery(hidden); - + // // A NAMESPACE is discovery, not a route, so a peer that also hears the // unsolicited PUBLISH_NAMESPACE still gets the match here. - let announced = origin.routes(); - - match declared.active_count { - true if declared.auth => { - self.state = NamespaceServeState::Count { - peer, - hidden, - announced, - } - } - true => self.count(peer, hidden, announced), - false => { - self.state = NamespaceServeState::Ok { - peer, - hidden, - announced, - initial: Vec::new(), - ok: Some(ietf::RequestOk { - largest: None, - active: None, - }), - } - } - } - } - NamespaceServeState::Count { .. } => { - ready!(gate.poll_setup_answered(waiter)); - let NamespaceServeState::Count { - peer, - hidden, + let announced = publisher.advertising(origin).routes(); + self.state = NamespaceServeState::Ok { announced, - } = std::mem::replace(&mut self.state, NamespaceServeState::Done) - else { - unreachable!("counting"); + ok: Some(ietf::RequestOk { largest: None }), }; - self.count(peer, hidden, announced); } NamespaceServeState::Ok { ok, .. } => { if let Poll::Ready(refusal) = gate.poll_token_ended(waiter) { @@ -6379,22 +6163,16 @@ impl NamespaceServe { stream.writer.buffer_message(ietf::RequestOk::ID, &ok)?; } ready!(stream.writer.poll_flush(waiter))?; - let NamespaceServeState::Ok { - peer, - hidden, - announced, - initial, - .. - } = std::mem::replace(&mut self.state, NamespaceServeState::Done) + let NamespaceServeState::Ok { announced, .. } = + std::mem::replace(&mut self.state, NamespaceServeState::Done) else { unreachable!("acknowledging"); }; let target = Target::Inline(self.stream.take().expect("acknowledged once")); - let mut ns = Namespaces::new(peer, target); + let mut ns = Namespaces::new(target); ns.gate = self.gate.take(); - ns.hidden = hidden; let prefix = self.prefix.clone(); - let run = NamespaceLoop::new(&self.publisher, announced, prefix, ns, initial); + let run = NamespaceLoop::new(&self.publisher, announced, prefix, ns); self.state = NamespaceServeState::Run(Box::new(run)); } NamespaceServeState::Run(_) => unreachable!("run above"), @@ -10828,7 +10606,6 @@ mod serve_tests { authorization_token: None, request_id: RequestId(0), namespace: crate::Path::empty(), - hidden: false } )); assert!(futures::poll!(running.as_mut()).is_pending()); @@ -10869,7 +10646,6 @@ mod serve_tests { authorization_token: None, request_id: RequestId(0), namespace: crate::Path::from_static("room"), - hidden: false, }; let mut running = std::pin::pin!(h.publisher.clone().run_subscribe_namespace_stream(stream, msg)); assert!(futures::poll!(running.as_mut()).is_pending()); @@ -10914,7 +10690,6 @@ mod serve_tests { authorization_token: None, request_id: RequestId(0), namespace: crate::Path::from_static("old"), - hidden: false, }; let mut running = std::pin::pin!(h.publisher.clone().run_subscribe_namespace_stream(stream, msg)); assert!(futures::poll!(running.as_mut()).is_pending()); @@ -10975,7 +10750,6 @@ mod serve_tests { authorization_token: Some(request_token()), request_id: RequestId(0), namespace: crate::Path::empty(), - hidden: false, }; let mut running: Running = Box::pin(h.publisher.clone().run_subscribe_namespace_stream(stream, msg)); let request = taken_token(&mut running, &mut requests).await; @@ -11166,7 +10940,6 @@ mod serve_tests { authorization_token: None, request_id: RequestId(0), namespace: crate::Path::empty(), - hidden: false, }; let mut running: Running = Box::pin(h.publisher.clone().run_subscribe_namespace_stream(stream, msg)); assert!(futures::poll!(running.as_mut()).is_pending()); @@ -11316,7 +11089,6 @@ mod serve_tests { authorization_token: None, request_id: RequestId(0), namespace: crate::Path::from_static(""), - hidden: false, }; let mut run = std::pin::pin!(h.publisher.clone().run_subscribe_namespace_stream(stream, msg)); assert_eq!( @@ -12028,7 +11800,6 @@ mod serve_tests { authorization_token: None, request_id: RequestId(REQUEST_ID), namespace: crate::Path::from_static(""), - hidden: false, } .encode_msg(&mut w, version) .unwrap(); @@ -16788,7 +16559,6 @@ mod serve_tests { writer .encode(&ietf::RequestOk { largest: None, - active: None, }) .await .unwrap(); @@ -17298,7 +17068,6 @@ mod serve_tests { writer .encode(&ietf::RequestOk { largest: None, - active: None, }) .await .unwrap(); @@ -18237,14 +18006,10 @@ mod tests { out } - /// A SETUP slot already filled with what the peer declared. The announce loops block - /// on it, so a test that leaves it empty is a test that never advertises. - fn declared(solicit: Option) -> peer::PeerSetup { + /// A SETUP slot already filled with a peer that declared nothing. + fn declared() -> peer::PeerSetup { let slot = peer::PeerSetup::default(); - slot.set(peer::Peer { - solicit, - ..Default::default() - }); + slot.set(peer::Peer::default()); slot } @@ -18282,12 +18047,6 @@ mod tests { } } - /// A peer that requires solicitation, which is what hands the advertisements to the - /// SUBSCRIBE_NAMESPACE stream. - fn requires_solicitation() -> peer::PeerSetup { - declared(Some(true)) - } - /// A publisher for a peer assigned `assigned`, over an origin holding two /// broadcasts: `from/peer`, whose only route flows through `assigned`, and /// `from/us`, which does not. The producers are returned so the routes outlive @@ -18337,92 +18096,19 @@ mod tests { let assigned = crate::Hop::new(777).unwrap(); let (publisher, consumer, _routes) = echo_harness(assigned).await; - let peer = cluster::Peer::default(); - // The cursor is what filters: an excluded consumer never sees the echoed route. - let mut announced = consumer.excluding(assigned).routes(); + let mut announced = consumer.excluding(publisher.exclude()).routes(); let local = announced.assert_next_active("from/us"); announced.assert_next_wait(); - assert_eq!(publisher.select(&local, &peer), Advert::Some(Default::default())); + assert_eq!(publisher.select(&local), Advert::Some); } - /// An anonymous chain received from an identified peer keeps the 0 on the wire - /// and is never advertised back to that session: split-horizon matches `via` - /// as well as the chain. + /// A moq-transport peer's routes carry 0 on their chain, since it names no hop; the + /// assigned identity stays on `via` and split-horizon matches it, so the peer is not + /// advertised its own route back. #[kio::test] - async fn anonymous_chain_is_forwarded_with_zero_and_not_echoed() { - let assigned = crate::Hop::new(777).unwrap(); - let r1 = crate::Hop::new(9).unwrap(); - let origin = crate::origin::Config::new(std::num::NonZeroU64::new(1).unwrap()).produce(); - let consumer = origin.consume(); - let publisher = Publisher::new( - crate::time::sim(), - crate::driver::Transport::new(crate::lite::test_transport::SinkSession::new(Default::default())), - origin.consume(), - Control::new(false), - Some(assigned), - peer::PeerSetup::default(), - Version::Draft18, - ); - - let mut hops = crate::Hops::new(); - hops.push(crate::Hop::UNKNOWN).unwrap(); - hops.push(r1).unwrap(); - let _echoed = origin - .announce( - "from/peer", - crate::origin::RouteInfo::new().with_hops(hops.clone()).with_via(r1), - ) - .unwrap(); - - let peer = cluster::Peer { - hop: Some(r1), - cost: None, - }; - let mut announced = consumer.excluding(publisher.exclude(&peer)).routes(); - announced.assert_next_wait(); - - let forwarded = cluster::Advert::forward(&hops, 0, crate::Hop::new(1).unwrap()).unwrap(); - let ids: Vec<_> = forwarded.hops.hops().iter().map(|h| h.id()).collect(); - assert_eq!(ids, vec![0, 9, 1]); - } - - /// Declaring the reserved 0 turns the extension on while naming nobody, so the - /// identity we assigned stands in, exactly as for a peer that never negotiated. - /// Asserted on the resolution itself rather than through an advertisement: a - /// negotiated peer always sends its own HOP_PATH, so a route attributed to the - /// assigned identity is a state this peer class cannot reach; see - /// [`a_declared_zero_chain_is_not_advertised_back`] for what it gets instead. - #[kio::test] - async fn withheld_peer_hop_falls_back_to_assigned() { - let assigned = crate::Hop::new(777).unwrap(); - let declared = crate::Hop::new(9).unwrap(); - let (publisher, _consumer, _routes) = echo_harness(assigned).await; - - let withheld = cluster::Peer { - hop: Some(crate::Hop::UNKNOWN), - cost: None, - }; - assert!(withheld.negotiated(), "the extension is on"); - assert_eq!(publisher.exclude(&withheld), assigned, "0 names nobody, so we do"); - - let absent = cluster::Peer::default(); - assert_eq!(publisher.exclude(&absent), assigned, "so does declaring nothing"); - - let named = cluster::Peer { - hop: Some(declared), - cost: None, - }; - assert_eq!(publisher.exclude(&named), declared, "a declared identity wins"); - } - - /// A peer that negotiated the extension MUST send a HOP_PATH on every advertisement, - /// and one that declared 0 names itself 0 there. An arriving chain is not rewritten, - /// so the route carries 0; the assigned identity stays on `via` and split-horizon - /// matches it, so the peer is not advertised its own route back. - #[kio::test] - async fn a_declared_zero_chain_is_not_advertised_back() { + async fn an_anonymous_chain_is_not_advertised_back() { let assigned = crate::Hop::new(777).unwrap(); let origin = crate::origin::Config::new(std::num::NonZeroU64::new(1).unwrap()).produce(); let consumer = origin.consume(); @@ -18437,8 +18123,8 @@ mod tests { Version::Draft18, ); - // The chain as ingress stores it: the peer named itself 0, and `via` is the - // identity we assigned that session. + // The chain as ingress stores it: 0 for the peer, and `via` is the identity we + // assigned that session. let mut hops = crate::Hops::new(); hops.push(crate::Hop::UNKNOWN).unwrap(); let _echoed = origin @@ -18448,91 +18134,10 @@ mod tests { ) .unwrap(); - let peer = cluster::Peer { - hop: Some(crate::Hop::UNKNOWN), - cost: None, - }; - let mut announced = consumer.excluding(publisher.exclude(&peer)).routes(); + let mut announced = consumer.excluding(publisher.exclude()).routes(); announced.assert_next_wait(); } - /// MoQ Active Count: the OK counts exactly the NAMESPACE messages that follow it, - /// so a route this peer is never told about is not counted either. Counting one would - /// leave the peer waiting on a message that never comes. - #[kio::test] - async fn the_ok_counts_only_what_is_advertised() { - let origin = crate::origin::Config::new(std::num::NonZeroU64::new(1).unwrap()).produce(); - - let session = SinkSession::gated_bi(kio::Producer::new(true).consume()); - let log = session.log.clone(); - let setup = peer::PeerSetup::default(); - setup.set(peer::Peer { - cluster: cluster::Peer { - hop: Some(crate::Hop::new(9).unwrap()), - cost: None, - }, - solicit: Some(true), - active_count: true, - ..Default::default() - }); - let publisher = Publisher::new( - crate::time::sim(), - crate::driver::Transport::new(session.clone()), - origin.consume(), - Control::new(false), - None, - setup, - Version::Draft18, - ); - - let mut clean = crate::Hops::new(); - clean.push(crate::Hop::new(778).unwrap()).unwrap(); - let _clean = origin - .announce("cam-clean", crate::origin::RouteInfo::new().with_hops(clean)) - .unwrap(); - // A chain with no room left for our own hop cannot be forwarded. - let mut full = crate::Hops::new(); - while full.push(crate::Hop::new(100 + full.len() as u64).unwrap()).is_ok() {} - let _full = origin - .announce("cam-full", crate::origin::RouteInfo::new().with_hops(full)) - .unwrap(); - settle().await; - - let stream = Stream::open(&crate::driver::Transport::new(session.clone()), Version::Draft18) - .await - .unwrap(); - let msg = ietf::SubscribeNamespace { - authorization_token: None, - request_id: RequestId(1), - namespace: crate::Path::from_static(""), - hidden: false, - }; - let mut run = std::pin::pin!(publisher.run_subscribe_namespace_stream(stream, msg)); - assert!(futures::poll!(run.as_mut()).is_pending()); - - assert_eq!(occurrences(&log, b"cam-clean"), 1); - assert_eq!(occurrences(&log, b"cam-full"), 0); - - let expected = crate::lite::test_transport::Log::default(); - let mut writer = crate::coding::Writer::new( - crate::transport::boxed::SendStream::new(crate::lite::test_transport::SinkSend::new(expected.clone())), - Version::Draft18, - ); - writer.varint(ietf::RequestOk::ID).await.unwrap(); - writer - .encode(&ietf::RequestOk { - largest: None, - active: Some(1), - }) - .await - .unwrap(); - let expected = expected.writes.lock().unwrap().clone(); - assert!( - log.writes.lock().unwrap().starts_with(&expected), - "the OK counts one NAMESPACE" - ); - } - /// A same-path source can splice into (or detach from) an existing broadcast /// without an origin-level (un)announce, silently flipping `advertisable`. /// Namespace forwarding must follow: advertise when a clean route appears, @@ -18552,7 +18157,7 @@ mod tests { origin.consume(), Control::new(false), Some(assigned), - requires_solicitation(), + declared(), Version::Draft18, ); @@ -18577,7 +18182,6 @@ mod tests { authorization_token: None, request_id: RequestId(1), namespace: crate::Path::from_static(""), - hidden: false, }; let mut run = std::pin::pin!(publisher.run_subscribe_namespace_stream(stream, msg)); @@ -18674,7 +18278,7 @@ mod tests { origin.consume(), Control::new(false), None, - declared(Some(false)), + declared(), VERSION, ); @@ -18713,7 +18317,6 @@ mod tests { writer .encode(&ietf::RequestOk { largest: None, - active: None, }) .await .unwrap(); @@ -18831,78 +18434,20 @@ mod tests { } } - /// ACTIVE_COUNT only answers SUBSCRIBE_NAMESPACE (MoQ Active Count), so one on the OK - /// to a PUBLISH_NAMESPACE is the peer breaking the extension, negotiated or not. - #[kio::test] - async fn a_counted_publish_namespace_ok_is_a_violation() { - const VERSION: Version = Version::Draft18; - - let origin = crate::origin::Config::new(std::num::NonZeroU64::new(1).unwrap()).produce(); - let _local = origin.announce("local-cam", crate::origin::RouteInfo::new()).unwrap(); - settle().await; - - let ok = crate::lite::test_transport::Log::default(); - let mut writer = crate::coding::Writer::new( - crate::transport::boxed::SendStream::new(crate::lite::test_transport::SinkSend::new(ok.clone())), - VERSION, - ); - writer.varint(ietf::RequestOk::ID).await.unwrap(); - writer - .encode(&ietf::RequestOk { - largest: None, - active: Some(0), - }) - .await - .unwrap(); - let ok = ok.writes.lock().unwrap().clone(); - let session = crate::lite::test_transport::ScriptedSession::per_stream(vec![ok]); - let log = session.log.clone(); - - let peer_setup = peer::PeerSetup::default(); - peer_setup.set(peer::Peer { - active_count: true, - ..Default::default() - }); - let publisher = Publisher::new( - crate::time::sim(), - crate::driver::Transport::new(session.clone()), - origin.consume(), - Control::new(false), - None, - peer_setup, - VERSION, - ); - - let res = kio::clock::timeout(Duration::from_secs(5), publisher.run_publish_namespaces()) - .await - .expect("the violation ends the loop"); - assert!(matches!(res, Err(Error::ProtocolViolation)), "{res:?}"); - // Closed at the source, since the solicited path's request task only logs errors. - let code = crate::SessionError::ProtocolViolation.to_code(); - assert!(log.closes().iter().any(|(c, _)| *c == code), "{:?}", log.closes()); - } - - /// Drive both announce loops at once against a peer that declared `solicit`, - /// returning how many times the namespace hit the wire and how many bidi streams - /// were opened. One stream means the entry rode the subscription inline; two means - /// it went out as its own PUBLISH_NAMESPACE request. - async fn advertise_both_ways(solicit: Option) -> (usize, usize) { - let log = advertise_with_hidden(Discovery { - solicit, - ..Discovery::default() - }) - .await; + /// Drive both announce loops at once, returning how many times the namespace hit the + /// wire and how many bidi streams were opened. One stream means the entry rode the + /// subscription inline; two means it also went out as its own PUBLISH_NAMESPACE request. + async fn advertise_both_ways() -> (usize, usize) { + let log = advertise_with_hidden(Discovery::default()).await; (occurrences(&log, b"cam"), log.bi_opens()) } /// [`advertise_both_ways`] with a hidden `.stats/node` beside `cam`, and the peer's - /// SUBSCRIBE_NAMESPACE for `prefix` opting in to hidden namespaces or not. + /// SUBSCRIBE_NAMESPACE for `prefix`. struct Discovery<'a> { version: Version, - declared: bool, - solicit: Option, prefix: &'a str, - hidden: bool, + /// Publish an origin scoped to the hidden subtree alone. scoped: bool, } @@ -18910,24 +18455,14 @@ mod tests { fn default() -> Self { Self { version: Version::Draft18, - declared: true, - solicit: Some(true), prefix: "", - hidden: false, scoped: false, } } } async fn advertise_with_hidden(case: Discovery<'_>) -> crate::lite::test_transport::Log { - let Discovery { - version, - declared: hidden_declared, - solicit, - prefix, - hidden, - scoped, - } = case; + let Discovery { version, prefix, scoped } = case; let origin = crate::origin::Config::new(std::num::NonZeroU64::new(1).unwrap()).produce(); let _cam = origin.announce("cam", crate::origin::RouteInfo::new()).unwrap(); let _stats = origin.announce(".stats/node", crate::origin::RouteInfo::new()).unwrap(); @@ -18941,12 +18476,6 @@ mod tests { ]); let log = session.log.clone(); - let setup = peer::PeerSetup::default(); - setup.set(peer::Peer { - solicit, - hidden: hidden_declared, - ..Default::default() - }); let consume = match scoped { true => origin .consume() @@ -18960,7 +18489,7 @@ mod tests { consume, Control::new(false), None, - setup, + declared(), version, ); @@ -18971,109 +18500,72 @@ mod tests { authorization_token: None, request_id: RequestId(1), namespace: crate::Path::new(prefix).unwrap(), - hidden, }; let mut solicited = std::pin::pin!(publisher.clone().run_subscribe_namespace_stream(stream, msg)); let mut unsolicited = std::pin::pin!(publisher.run_publish_namespaces()); // Poll well past the first advertisement, so a second one from the other loop - // would show up rather than being missed by an early break. The unsolicited loop - // finishes immediately when the peer requires solicitation, and a completed - // future must not be polled again. - let mut quiet = false; + // would show up rather than being missed by an early break. for _ in 0..100 { assert!(futures::poll!(solicited.as_mut()).is_pending()); - if !quiet { - quiet = futures::poll!(unsolicited.as_mut()).is_ready(); - } + assert!(futures::poll!(unsolicited.as_mut()).is_pending()); settle().await; } log } - /// A hidden namespace reaches only a subscription that opted in or named its dot - /// segment. The subscription also repeats every visible - /// namespace the unsolicited loop already sent. A NAMESPACE is discovery, not - /// a second route. + /// moq-transport has no way to ask for hidden namespaces, so a hidden namespace reaches + /// only a subscription whose prefix names its dot segment, and never an unsolicited + /// PUBLISH_NAMESPACE. The subscription also repeats every visible namespace the + /// unsolicited loop already sent: a NAMESPACE is discovery, not a second route. #[kio::test] - async fn hidden_namespaces_need_an_opt_in() { - for (solicit, prefix, hidden, cam, stats) in [ - (Some(false), "", false, 2, 0), - (Some(false), "", true, 2, 1), - (Some(false), ".stats", false, 1, 1), - (Some(true), "", false, 1, 0), - (Some(true), "", true, 1, 1), - (Some(true), ".stats", false, 0, 1), - ] { + async fn hidden_namespaces_need_the_prefix_to_name_them() { + for version in [Version::Draft18, Version::Draft20, Version::Draft22] { + for (prefix, cam, stats) in [("", 2, 0), (".stats", 1, 1)] { + let log = advertise_with_hidden(Discovery { + version, + prefix, + ..Discovery::default() + }) + .await; + let case = format!("{version:?}: prefix {prefix:?}"); + assert_eq!(occurrences(&log, b"cam"), cam, "{case}"); + // An inline entry names its suffix, so count the leaf. + assert_eq!(occurrences(&log, b"node"), stats, "{case}"); + } + } + } + + /// An origin scoped to the hidden subtree alone does not opt this peer in to it: only a + /// prefix naming the dot segment lists it. + #[kio::test] + async fn a_scope_naming_a_hidden_subtree_is_not_an_opt_in() { + for (prefix, stats) in [("", 0), (".stats", 1)] { let log = advertise_with_hidden(Discovery { - solicit, prefix, - hidden, + scoped: true, ..Discovery::default() }) .await; - let case = format!("solicit {solicit:?}, prefix {prefix:?}, hidden {hidden}"); - assert_eq!(occurrences(&log, b"cam"), cam, "{case}"); - // An inline entry names its suffix, so count the leaf. - assert_eq!(occurrences(&log, b"node"), stats, "{case}"); + assert_eq!(occurrences(&log, b"node"), stats, "prefix {prefix:?}"); } } + /// A peer hears each namespace twice: an unsolicited PUBLISH_NAMESPACE and a NAMESPACE + /// on the SUBSCRIBE_NAMESPACE stream. Those are two discoveries of one namespace, not two + /// routes. #[kio::test] - async fn hidden_discovery_obeys_peer_setup_and_requested_prefix() { - for version in [Version::Draft18, Version::Draft20, Version::Draft22] { - for declared in [false, true] { - for solicit in [Some(false), Some(true)] { - for scoped in [false, true] { - for (prefix, hidden) in [("", false), ("", true), (".stats", false)] { - let log = advertise_with_hidden(Discovery { - version, - declared, - solicit, - prefix, - hidden, - scoped, - }) - .await; - // Visible on the subscription: not filtered, opted in, or the prefix - // names the dot segment. The unsolicited loop sends a dot namespace - // only when the peer did not declare MoQ Hidden. - let on_stream = !declared || hidden || prefix == ".stats"; - let unsolicited = solicit != Some(true) && !declared; - let expected = usize::from(on_stream) + usize::from(unsolicited); - assert_eq!( - occurrences(&log, b"node"), - expected, - "{version:?}: declared={declared}, solicit={solicit:?}, scoped={scoped}, prefix={prefix}, hidden={hidden}" - ); - } - } - } - } - } - } - - /// A peer that did not ask to be solicited hears each namespace twice: an - /// unsolicited PUBLISH_NAMESPACE and a NAMESPACE on the SUBSCRIBE_NAMESPACE stream. - /// Those are two discoveries of one namespace, not two routes. A peer that asked to - /// be solicited hears it only on the stream. - #[kio::test] - async fn a_non_solicit_peer_hears_a_namespace_both_ways() { - let (times, streams) = advertise_both_ways(Some(false)).await; + async fn a_peer_hears_a_namespace_both_ways() { + let (times, streams) = advertise_both_ways().await; assert_eq!(times, 2, "PUBLISH_NAMESPACE and NAMESPACE"); assert_eq!(streams, 2, "the subscription plus one PUBLISH_NAMESPACE request"); - - let (once, streams) = advertise_both_ways(Some(true)).await; - assert_eq!(once, 1, "a peer that asked to be told on request is told once"); - assert_eq!(streams, 1, "inline on the SUBSCRIBE_NAMESPACE stream it asked on"); } - /// A peer that did not send SOLICIT still gets NAMESPACE on its SUBSCRIBE_NAMESPACE - /// stream: one for a match that already exists, one announced - /// after, then NAMESPACE_DONE when that announcement ends. + /// A SUBSCRIBE_NAMESPACE stream carries NAMESPACE: one for a match that already + /// exists, one announced after, then NAMESPACE_DONE when that announcement ends. #[kio::test] - async fn a_non_solicit_subscribe_namespace_carries_namespace() { + async fn a_subscribe_namespace_carries_namespace() { for version in [Version::Draft18] { let origin = crate::origin::Config::new(std::num::NonZeroU64::new(1).unwrap()).produce(); let early = origin.announce("early-cam", crate::origin::RouteInfo::new()).unwrap(); @@ -19102,7 +18594,6 @@ mod tests { authorization_token: None, request_id: RequestId(1), namespace: crate::Path::from_static(""), - hidden: false, }; let mut run = std::pin::pin!(publisher.run_subscribe_namespace_stream(stream, msg)); @@ -19182,7 +18673,7 @@ mod tests { origin.consume(), Control::new(false), None, - declared(Some(false)), + declared(), VERSION, ); @@ -19261,7 +18752,7 @@ mod tests { origin.consume(), Control::new(false), Some(assigned), - declared(Some(false)), + declared(), Version::Draft18, ); @@ -19271,7 +18762,7 @@ mod tests { let mut watch = Watched::new(crate::origin::RouteInfo::new().with_hops(hops)); watch.deferred = true; - let mut ns = Namespaces::new(cluster::Peer::default(), Target::Requests); + let mut ns = Namespaces::new(Target::Requests); ns.watched.insert(suffix.clone(), watch); publisher.sync_namespace(&mut ns, &suffix, &suffix).unwrap(); @@ -19312,7 +18803,7 @@ mod tests { origin.consume(), Control::new(false), None, - declared(Some(false)), + declared(), VERSION, ); @@ -19349,25 +18840,6 @@ mod tests { drop(cam); } - /// A SETUP slot for a peer that negotiated the cluster extension, so an - /// advertisement carries a path and a cost worth repricing. - fn clustered(solicit: Option) -> peer::PeerSetup { - let slot = peer::PeerSetup::default(); - slot.set(peer::Peer { - cluster: cluster::Peer { - hop: Some(crate::Hop::new(9).unwrap()), - cost: None, - }, - solicit, - hidden: false, - max_age: false, - auth: false, - active_count: false, - flate: false, - }); - slot - } - /// The bytes of one REQUEST_UPDATE, framed as the publisher writes it. async fn request_update(version: Version, msg: &ietf::PublishNamespaceUpdate) -> Vec { let log = crate::lite::test_transport::Log::default(); @@ -19380,175 +18852,12 @@ mod tests { log.writes.lock().unwrap().clone() } - /// A relay that starts carrying a namespace reprices it with REQUEST_UPDATE on the - /// request that already carries it, sending the changed parameter only. The new cost - /// is 0, which has to be explicit: REQUEST_UPDATE keeps an omitted parameter, so - /// leaving it out would keep the old price. - #[kio::test] - async fn a_repricing_is_a_request_update() { - const VERSION: Version = Version::Draft18; - - // Forward the update at once: the hold is not what this checks. - let origin = crate::origin::Config { - update_hold: Duration::ZERO, - ..crate::origin::Config::new(std::num::NonZeroU64::new(1).unwrap()) - } - .produce(); - // One instance, so the warm route re-prices the instance rather than restart it. - let instance = crate::Instance::random(); - let _cold = origin - .announce( - "cam", - crate::origin::RouteInfo::new() - .with_instance(instance.clone(), 0) - .with_cost(4), - ) - .unwrap(); - settle().await; - - // One stream, answering the PUBLISH_NAMESPACE. The update goes unanswered: an answer - // scripted ahead of it would arrive while the request is idle, which is a violation. - let ok = publish_namespace_ok(VERSION).await; - let session = crate::lite::test_transport::ScriptedSession::per_stream(vec![ok]); - let log = session.log.clone(); - - let publisher = Publisher::new( - crate::time::sim(), - crate::driver::Transport::new(session.clone()), - origin.consume(), - Control::new(false), - None, - clustered(Some(false)), - VERSION, - ); - - let mut run = std::pin::pin!(publisher.run_publish_namespaces()); - for _ in 0..100 { - assert!(futures::poll!(run.as_mut()).is_pending()); - if occurrences(&log, b"cam") >= 1 { - break; - } - settle().await; - } - assert_eq!(occurrences(&log, b"cam"), 1, "the advertisement never went out"); - - // We start carrying it: a free route outranks the cold one. - let _warm = origin - .announce( - "cam", - crate::origin::RouteInfo::new().with_instance(instance, 0).with_cost(0), - ) - .unwrap(); - - // The request consumed id 1, so the update takes the next of our parity. The - // path is unchanged and omitted; the cost is an explicit 0. - let expected = request_update( - VERSION, - &ietf::PublishNamespaceUpdate { - request_id: RequestId(3), - hops: None, - cost: Some(0), - authorization_token: None, - }, - ) - .await; - for _ in 0..100 { - assert!(futures::poll!(run.as_mut()).is_pending()); - if occurrences(&log, &expected) >= 1 { - break; - } - settle().await; - } - - assert_eq!(occurrences(&log, &expected), 1, "REQUEST_UPDATE with an explicit 0"); - assert_eq!(occurrences(&log, b"cam"), 1, "PUBLISH_NAMESPACE was not repeated"); - assert_eq!(log.bi_opens(), 1, "the update rode the request's own stream"); - } - - /// ACTIVE_COUNT only answers SUBSCRIBE_NAMESPACE (MoQ Active Count), so one on the OK - /// to a REQUEST_UPDATE is the peer breaking the extension too. - #[kio::test] - async fn a_counted_update_ok_is_a_violation() { - const VERSION: Version = Version::Draft18; - - let origin = crate::origin::Config::new(std::num::NonZeroU64::new(1).unwrap()).produce(); - let instance = crate::Instance::random(); - let _cold = origin - .announce( - "cam", - crate::origin::RouteInfo::new() - .with_instance(instance.clone(), 0) - .with_cost(4), - ) - .unwrap(); - settle().await; - - // The PUBLISH_NAMESPACE is answered cleanly; its update's OK carries a count. - let counted = crate::lite::test_transport::Log::default(); - let mut writer = crate::coding::Writer::new( - crate::transport::boxed::SendStream::new(crate::lite::test_transport::SinkSend::new(counted.clone())), - VERSION, - ); - writer.varint(ietf::RequestOk::ID).await.unwrap(); - writer - .encode(&ietf::RequestOk { - largest: None, - active: Some(0), - }) - .await - .unwrap(); - let counted = counted.writes.lock().unwrap().clone(); - let ok = publish_namespace_ok(VERSION).await; - let session = crate::lite::test_transport::ScriptedSession::new(ok); - let log = session.log.clone(); - let peer = session.clone(); - - let publisher = Publisher::new( - crate::time::sim(), - crate::driver::Transport::new(session.clone()), - origin.consume(), - Control::new(false), - None, - clustered(Some(false)), - VERSION, - ); - - let mut run = std::pin::pin!(publisher.run_publish_namespaces()); - for _ in 0..100 { - assert!(futures::poll!(run.as_mut()).is_pending()); - if occurrences(&log, b"cam") >= 1 { - break; - } - settle().await; - } - assert_eq!(occurrences(&log, b"cam"), 1, "the advertisement never went out"); - - // Queued behind the OK the idle request already read, so it answers the update: - // nothing wakes the reader until the update goes out. - peer.push(&counted); - // Repricing sends the REQUEST_UPDATE whose OK is counted. - let _warm = origin - .announce( - "cam", - crate::origin::RouteInfo::new().with_instance(instance, 0).with_cost(0), - ) - .unwrap(); - - let res = kio::clock::timeout(Duration::from_secs(5), run) - .await - .expect("the violation ends the loop"); - assert!(matches!(res, Err(Error::ProtocolViolation)), "{res:?}"); - // Closed at the source, since the solicited path's request task only logs errors. - let code = crate::SessionError::ProtocolViolation.to_code(); - assert!(log.closes().iter().any(|(c, _)| *c == code), "{:?}", log.closes()); - } - /// A route from a different original publisher under the same instance (a replica) - /// updates the advertisement in place, like any other change within an instance: - /// withdrawing it would make the namespace briefly vanish downstream just because - /// its replica moved. + /// leaves the advertisement alone, like any other change within an instance: moq-transport + /// carries no path or cost to update, and withdrawing it would make the namespace briefly + /// vanish downstream just because its replica moved. #[kio::test] - async fn a_replica_change_is_a_request_update() { + async fn a_replica_change_sends_nothing() { const VERSION: Version = Version::Draft18; // Forward the update at once: the hold is not what this checks. @@ -19571,8 +18880,7 @@ mod tests { .unwrap(); settle().await; - // One stream, answering the advertisement from A. Its update to B goes unanswered: an - // answer scripted ahead of it would arrive while the request is idle. + // One stream, answering the advertisement from A. let ok = publish_namespace_ok(VERSION).await; let session = crate::lite::test_transport::ScriptedSession::per_stream(vec![ok]); let log = session.log.clone(); @@ -19583,7 +18891,7 @@ mod tests { origin.consume(), Control::new(false), None, - clustered(Some(false)), + declared(), VERSION, ); @@ -19607,28 +18915,13 @@ mod tests { .with_cost(0), ) .unwrap(); - let update = request_update( - VERSION, - &ietf::PublishNamespaceUpdate { - request_id: RequestId(3), - hops: Some(cluster::HopPath::new( - crate::Hops::try_from(vec![crate::Hop::new(8).unwrap(), crate::Hop::new(1).unwrap()]).unwrap(), - )), - cost: Some(0), - authorization_token: None, - }, - ) - .await; + let sent = log.writes.lock().unwrap().len(); for _ in 0..100 { assert!(futures::poll!(run.as_mut()).is_pending()); - if occurrences(&log, &update) >= 1 { - break; - } settle().await; } - assert_eq!(occurrences(&log, &update), 1, "REQUEST_UPDATE carrying B's path"); - assert_eq!(occurrences(&log, b"cam"), 1, "PUBLISH_NAMESPACE was not repeated"); + assert_eq!(log.writes.lock().unwrap().len(), sent, "nothing more on the wire"); assert_eq!(log.bi_opens(), 1, "no withdrawal and no second stream"); } @@ -19656,7 +18949,7 @@ mod tests { origin.consume(), Control::new(false), None, - declared(Some(false)), + declared(), version, ) .with_auth(auth.clone()); @@ -19698,7 +18991,7 @@ mod tests { async fn swaps_a_route_auth(version: Version) { let origin = unheld_origin(); - let auth = crate::auth::Link::ietf(false); + let auth = crate::auth::Link::ietf(); let first = auth.present(bytes::Bytes::from_static(b"first-secret"), false).unwrap(); let second = auth .present(bytes::Bytes::from_static(b"second-secret"), false) @@ -19731,8 +19024,6 @@ mod tests { version, &ietf::PublishNamespaceUpdate { request_id: RequestId(3), - hops: None, - cost: None, authorization_token: Some(auth_token(b"second-secret")), }, ) @@ -19764,7 +19055,7 @@ mod tests { const VERSION: Version = Version::Draft18; let origin = unheld_origin(); - let other = crate::auth::Link::ietf(false); + let other = crate::auth::Link::ietf(); let foreign = other .present(bytes::Bytes::from_static(b"foreign-secret"), false) .unwrap(); @@ -19774,7 +19065,7 @@ mod tests { settle().await; let ok = publish_namespace_ok(VERSION).await; - let (publisher, log) = auth_publisher(&origin, &crate::auth::Link::ietf(false), vec![ok], VERSION); + let (publisher, log) = auth_publisher(&origin, &crate::auth::Link::ietf(), vec![ok], VERSION); let mut run = std::pin::pin!(publisher.run_publish_namespaces()); poll_until(&mut run, &log, b"cam", 1).await; @@ -19793,7 +19084,7 @@ mod tests { const VERSION: Version = Version::Draft18; let origin = unheld_origin(); - let auth = crate::auth::Link::ietf(false); + let auth = crate::auth::Link::ietf(); let token = auth.present(bytes::Bytes::from_static(b"late-secret"), false).unwrap(); let cam = origin.announce("cam", crate::origin::RouteInfo::new()).unwrap(); settle().await; @@ -19819,7 +19110,7 @@ mod tests { const VERSION: Version = Version::Draft18; let origin = unheld_origin(); - let auth = crate::auth::Link::ietf(false); + let auth = crate::auth::Link::ietf(); let present = |token: &'static [u8]| auth.present(bytes::Bytes::from_static(token), false).unwrap(); let a = origin .announce("cam-a", crate::origin::RouteInfo::new().with_auth(present(b"a-first"))) @@ -19878,7 +19169,7 @@ mod tests { origin.consume(), Control::new(false), None, - declared(Some(false)), + declared(), VERSION, ); let run = kio::clock::sim::spawn(publisher.run_publish_namespaces()); @@ -19947,7 +19238,7 @@ mod tests { origin.consume(), Control::new(false), None, - declared(Some(false)), + declared(), version, ); let run = kio::clock::sim::spawn(publisher.run_publish_namespaces()); @@ -20018,7 +19309,7 @@ mod tests { // A token too big for the header's u16 length, so the header fails after its stream // opened and its request ID was spent. A message goes out whole, so this is the one // failure a fresh in-memory stream can be made to show. - let auth = crate::auth::Link::ietf(false); + let auth = crate::auth::Link::ietf(); let token = auth.present(bytes::Bytes::from(vec![0; 70_000]), false).unwrap(); let _cam = origin .announce("cam", crate::origin::RouteInfo::new().with_auth(token)) @@ -20032,7 +19323,7 @@ mod tests { origin.consume(), Control::new(false), None, - declared(Some(false)), + declared(), version, ) .with_auth(auth.clone()); @@ -20065,7 +19356,7 @@ mod tests { const VERSION: Version = Version::Draft18; let origin = unheld_origin(); - let auth = crate::auth::Link::ietf(false); + let auth = crate::auth::Link::ietf(); let present = |token: &'static [u8]| auth.present(bytes::Bytes::from_static(token), false).unwrap(); let cam = origin .announce("cam", crate::origin::RouteInfo::new().with_auth(present(b"first"))) @@ -20082,7 +19373,7 @@ mod tests { origin.consume(), Control::new(false), None, - declared(Some(false)), + declared(), VERSION, ) .with_auth(auth.clone()); @@ -20118,7 +19409,7 @@ mod tests { async fn advertises_again_unanswered(version: Version) { let origin = unheld_origin(); - let auth = crate::auth::Link::ietf(false); + let auth = crate::auth::Link::ietf(); let present = |token: &'static [u8]| auth.present(bytes::Bytes::from_static(token), false).unwrap(); let cam = origin .announce("cam", crate::origin::RouteInfo::new().with_auth(present(b"first"))) @@ -20175,7 +19466,7 @@ mod tests { origin.consume(), Control::new(false), None, - clustered(Some(false)), + declared(), VERSION, ); @@ -20204,86 +19495,6 @@ mod tests { assert_eq!(log.bi_opens(), 2, "on a fresh request stream"); } - /// A peer that refuses an update closes the stream, which withdraws the - /// advertisement. The namespace is then not held at all, so it comes back as a fresh - /// PUBLISH_NAMESPACE once the refusal's wait is out, not as another update on a - /// stream the peer already ended. - #[kio::test] - async fn a_refused_update_is_re_advertised_fresh() { - const VERSION: Version = Version::Draft18; - - let origin = crate::origin::Config::new(std::num::NonZeroU64::new(1).unwrap()).produce(); - let instance = crate::Instance::random(); - let _cold = origin - .announce( - "cam", - crate::origin::RouteInfo::new() - .with_instance(instance.clone(), 0) - .with_cost(4), - ) - .unwrap(); - settle().await; - - // Stream 1 accepts the advertisement and refuses its update, with a wait shorter - // than the retry sweep. The streams share one script, so the refusal is queued once - // the idle request has read its OK. - let ok = publish_namespace_ok(VERSION).await; - let refusal = publish_namespace_error(VERSION, 50).await; - let session = crate::lite::test_transport::ScriptedSession::new(ok); - let log = session.log.clone(); - let peer = session.clone(); - - let publisher = Publisher::new( - crate::time::sim(), - crate::driver::Transport::new(session.clone()), - origin.consume(), - Control::new(false), - None, - clustered(Some(false)), - VERSION, - ); - - let mut run = std::pin::pin!(publisher.run_publish_namespaces()); - for _ in 0..100 { - assert!(futures::poll!(run.as_mut()).is_pending()); - if occurrences(&log, b"cam") >= 1 { - break; - } - settle().await; - } - assert_eq!(occurrences(&log, b"cam"), 1, "the advertisement never went out"); - - peer.push(&refusal); - let _warm = origin - .announce( - "cam", - crate::origin::RouteInfo::new().with_instance(instance, 0).with_cost(0), - ) - .unwrap(); - - for _ in 0..100 { - assert!(futures::poll!(run.as_mut()).is_pending()); - if occurrences(&log, b"cam") >= 2 { - break; - } - tick().await; - } - - assert_eq!(occurrences(&log, b"cam"), 2, "re-advertised after the refusal"); - assert_eq!(log.bi_opens(), 2, "on a fresh request stream"); - let update = request_update( - VERSION, - &ietf::PublishNamespaceUpdate { - request_id: RequestId(3), - hops: None, - cost: Some(0), - authorization_token: None, - }, - ) - .await; - assert_eq!(occurrences(&log, &update), 1, "only the one update was attempted"); - } - /// A peer that never answers one advertisement must not hold the next namespace back. #[kio::test] async fn a_silent_answer_still_lets_the_next_namespace_be_advertised() { @@ -20305,7 +19516,7 @@ mod tests { origin.consume(), Control::new(false), None, - declared(Some(false)), + declared(), VERSION, ); @@ -20351,7 +19562,7 @@ mod tests { } for end in [End::Local, End::Fin, End::Reset, End::Refused] { let origin = unheld_origin(); - let auth = crate::auth::Link::ietf(false); + let auth = crate::auth::Link::ietf(); let first = auth.present(bytes::Bytes::from_static(b"first"), false).unwrap(); let mut first_grant = first.grant(); let cam = origin @@ -20365,7 +19576,6 @@ mod tests { .encode_message(&ietf::PublishNamespace { request_id: RequestId(1), track_namespace: crate::Path::from_static("cam"), - params: Default::default(), authorization_token: Some(auth_token(b"first")), }) .await @@ -20388,7 +19598,7 @@ mod tests { origin.consume(), Control::new(false), None, - declared(Some(false)), + declared(), version, ) .with_auth(auth.clone()); @@ -20460,7 +19670,7 @@ mod tests { origin.consume(), Control::new(false), None, - declared(Some(false)), + declared(), version, ); let mut run = std::pin::pin!(publisher.run_publish_namespaces()); @@ -20500,7 +19710,7 @@ mod tests { origin.consume(), control.clone(), None, - declared(Some(false)), + declared(), version, ); let mut run = std::pin::pin!(publisher.run_publish_namespaces()); @@ -20553,7 +19763,7 @@ mod tests { origin.consume(), Control::new(false), None, - declared(Some(false)), + declared(), VERSION, ); diff --git a/rs/moq-net/src/ietf/request.rs b/rs/moq-net/src/ietf/request.rs index 87768376419..0d50c62afed 100644 --- a/rs/moq-net/src/ietf/request.rs +++ b/rs/moq-net/src/ietf/request.rs @@ -2,7 +2,6 @@ use std::borrow::Cow; use crate::coding::{Decode, DecodeError, Decoder, Encode, EncodeError, Encoder}; -use super::active_count::ACTIVE_COUNT_PARAM; use super::{Location, Message}; use super::Version; @@ -45,9 +44,6 @@ impl<'a> Decode<'a, Version> for RequestId { /// REQUEST_OK (0x07): the generic success response for any request, on its own stream. #[derive(Clone, Debug)] pub struct RequestOk { - /// MoQ Active Count: how many NAMESPACE messages follow before the subscription is - /// synced. Only on the answer to a SUBSCRIBE_NAMESPACE, when negotiated. - pub active: Option, /// LARGEST_OBJECT: on the answer to a REQUEST_UPDATE that resumed forwarding, where the /// subscription now joins (draft-18 section 5.1). Absent before the track has objects. pub largest: Option, @@ -57,10 +53,7 @@ impl<'a> Message<'a> for RequestOk { const ID: u64 = 0x07; fn encode_msg(&self, w: &mut Encoder<'_>, version: Version) -> Result<(), EncodeError> { - encode_params!(w, version, - 0x09 => self.largest, - ACTIVE_COUNT_PARAM => self.active, - ); + encode_params!(w, version, 0x09 => self.largest); Ok(()) } @@ -71,9 +64,8 @@ impl<'a> Message<'a> for RequestOk { decode_params!(r, version, 0x08 => _expires: Option, 0x09 => largest: Option, - ACTIVE_COUNT_PARAM => active: Option, ); - Ok(Self { active, largest }) + Ok(Self { largest }) } } @@ -154,22 +146,6 @@ mod tests { } } - #[test] - fn test_request_ok_active_round_trips() { - for version in [Version::Draft18, Version::Draft20, Version::Draft22] { - for count in [None, Some(0), Some(7)] { - let msg = RequestOk { - largest: None, - active: count, - }; - let wire = encode_message(&msg, version); - - let decoded: RequestOk = decode_message(&wire, version).unwrap(); - assert_eq!(decoded.active, count, "{version:?}"); - } - } - } - #[test] fn test_request_error_round_trip() { let msg = RequestError { diff --git a/rs/moq-net/src/ietf/session.rs b/rs/moq-net/src/ietf/session.rs index 1ca7020ef8a..5e0f146c28f 100644 --- a/rs/moq-net/src/ietf/session.rs +++ b/rs/moq-net/src/ietf/session.rs @@ -11,9 +11,8 @@ use crate::{ use std::task::{Poll, ready}; use super::{ - Control, Message, Namespaces, PublishNamespaces, Publisher, RecvFill, RecvGroup, Subscriber, Version, active_count, - auth, cluster, hidden, max_age, peer, request_stream::Reject, request_update, solicit, - subscriber::is_protocol_violation, + Control, Message, Namespaces, PublishNamespaces, Publisher, RecvFill, RecvGroup, Subscriber, Version, max_age, peer, + request_stream::Reject, request_update, subscriber::is_protocol_violation, }; type RecvStream = crate::transport::boxed::RecvStream; @@ -40,19 +39,13 @@ pub struct Config { pub publish: Option, pub subscribe: Option, - /// The origin (hop) id to assign the peer when it declares none itself. See - /// `Client::with_peer_hop`; a peer that negotiates the MoQ Cluster extension - /// declares its own, which wins. + /// The origin (hop) id to assign the peer, which moq-transport carries no identity + /// for. See `Client::with_peer_hop`. pub peer_hop: Option, - /// What crossing this link costs. Declared in our SETUP (see - /// [`cluster::RELAY_COST`]) for the peer to charge, and charged locally on what the - /// peer sends us. `None` declares nothing and charges whatever the peer declared, - /// falling back to 1; that is what a server accepting a connection passes, since it - /// has no per-peer configuration of its own. - /// - /// A peer that does not negotiate the MoQ Cluster extension carries no cost at all, so - /// nothing is charged and its routes rank on hop count alone. + /// What pulling from the peer costs, charged on every route it advertises. `None` + /// charges 1; that is what a server accepting a connection passes, since it has no + /// per-peer configuration of its own. pub cost: Option, pub version: Version, @@ -78,18 +71,14 @@ pub struct Config { pub peer_declared: Option, /// The session's auth handle, created before [`start`] so a server can take the - /// peer's token requests during its handshake. Supports AUTH exactly when the - /// version can negotiate it and this side offers it; the peer's SETUP decides whether - /// it does. + /// peer's token requests during its handshake. moq-transport carries no AUTH, so it + /// only verifies the tokens requests carry. pub auth: crate::auth::Link, /// Uni streams that arrived before that pre-read SETUP (see [`PeerSetup::early`]), /// classified by the session before it accepts any more. pub early_unis: Vec>, - /// The extensions our SETUP offers. MoQ Auth is offered only while [`Self::auth`] - /// supports it too. - pub extensions: crate::setup::Extensions, /// The Implementation our SETUP advertises, or `None` to omit it. pub implementation: Option, @@ -104,6 +93,9 @@ pub(crate) struct Driver { // Dispatched SUBSCRIBE and FETCH serves still owing the peer data. owed: std::sync::Arc, machine: Machine, + /// Counts this session against the publish origin while it withholds that origin's + /// hidden routes from the peer. + withholding: Option, /// Keeps the test executor's registrations alive between polls of the future form. #[cfg(test)] park: Box, @@ -117,7 +109,9 @@ impl Driver { /// Run the session until it ends, with the error that ended it. pub(crate) fn poll(&mut self, waiter: &kio::Waiter) -> Poll> { - self.machine.poll(waiter) + let res = ready!(self.machine.poll(waiter)); + self.withholding = None; + Poll::Ready(res) } } @@ -125,8 +119,15 @@ impl Driver { impl std::future::Future for Driver { type Output = Result<(), Error>; fn poll(mut self: std::pin::Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll { - let Self { machine, park, .. } = &mut *self; - machine.poll(park.hold(cx)) + let Self { + machine, + park, + withholding, + .. + } = &mut *self; + let res = ready!(machine.poll(park.hold(cx))); + *withholding = None; + Poll::Ready(res) } } @@ -159,7 +160,7 @@ impl Machine { } } -pub fn start(config: Config) -> Result<(Driver, crate::goaway::Handle, Option), Error> { +pub fn start(config: Config) -> Result<(Driver, crate::goaway::Handle), Error> { let Config { runtime, stats, @@ -178,7 +179,6 @@ pub fn start(config: Config) -> Result<(Driver, crate::goaway::Handle, Option Result<(Driver, crate::goaway::Handle, Option auth.present(bytes::Bytes::new(), true).ok(), - false => None, - }; - // Which prefixes to SUBSCRIBE_NAMESPACE, as for moq-lite: a client asks about what // its origin's readers watch, a server or relay link about its whole scope. Decided // now, so the origin's announce consumers never read as synced before a request goes @@ -210,6 +197,15 @@ pub fn start(config: Config) -> Result<(Driver, crate::goaway::Handle, Option