Skip to content

Commit

Permalink
refactor: use path buffer provided by caller in recv*_with_path
Browse files Browse the repository at this point in the history
This commit modifies the path types to be generic over the underlying storage.
This enables us to store paths in buffers provided by a caller as opposed to
needing to allocate storage for paths.
  • Loading branch information
jpcsmith committed Dec 17, 2023
1 parent 7980af0 commit 6b637ca
Show file tree
Hide file tree
Showing 5 changed files with 310 additions and 120 deletions.
11 changes: 7 additions & 4 deletions crates/scion-proto/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ pub use epic::EpicAuths;

/// A SCION end-to-end path with optional metadata.
#[derive(Debug, Clone, PartialEq)]
pub struct Path {
pub struct Path<T = Bytes> {
/// The raw bytes to be added as the path header to SCION dataplane packets.
pub dataplane_path: DataplanePath,
pub dataplane_path: DataplanePath<T>,
/// The underlay address (IP + port) of the next hop; i.e., the local border router.
pub underlay_next_hop: Option<SocketAddr>,
/// The ISD-ASN where the path starts and ends.
Expand All @@ -40,9 +40,9 @@ pub struct Path {
}

#[allow(missing_docs)]
impl Path {
impl<T> Path<T> {
pub fn new(
dataplane_path: DataplanePath,
dataplane_path: DataplanePath<T>,
isd_asn: ByEndpoint<IsdAsn>,
underlay_next_hop: Option<SocketAddr>,
) -> Self {
Expand Down Expand Up @@ -76,7 +76,10 @@ impl Path {
pub fn is_empty(&self) -> bool {
self.dataplane_path.is_empty()
}
}

#[allow(missing_docs)]
impl Path<Bytes> {
#[tracing::instrument]
pub fn try_from_grpc_with_endpoints(
mut value: daemon_grpc::Path,
Expand Down
100 changes: 79 additions & 21 deletions crates/scion-proto/src/path/dataplane.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Types and functions for SCION dataplane paths.

use std::ops::Deref;

use bytes::{Buf, BufMut, Bytes};

use crate::{
Expand Down Expand Up @@ -60,32 +62,26 @@ pub struct UnsupportedPathType(pub u8);

/// Dataplane path found in a SCION packet.
#[derive(Debug, Clone, PartialEq)]
pub enum DataplanePath {
pub enum DataplanePath<T = Bytes> {
/// The empty path type, used for intra-AS hops.
EmptyPath,
/// The standard SCION path header.
Standard(StandardPath),
Standard(StandardPath<T>),
/// The raw bytes of an unsupported path header type.
Unsupported {
/// The path's type.
path_type: PathType,
/// The raw encoded path.
bytes: Bytes,
bytes: T,
},
}

impl DataplanePath {
/// Returns a deep copy of the object.
pub fn deep_copy(&self) -> Self {
match self {
Self::EmptyPath => Self::EmptyPath,
Self::Standard(path) => Self::Standard(path.deep_copy()),
Self::Unsupported { path_type, bytes } => Self::Unsupported {
path_type: *path_type,
bytes: Bytes::copy_from_slice(bytes),
},
}
}
impl<T> DataplanePath<T> {
/// The maximum length of a SCION dataplane path.
///
/// Computed from the max header length (1020) minus the common header length (12)
/// and the minimum SCION address header length (24).
pub const MAX_LEN: usize = 984;

/// Returns the path's type.
pub fn path_type(&self) -> PathType {
Expand All @@ -96,14 +92,68 @@ impl DataplanePath {
}
}

/// Returns true iff the path is a [`DataplanePath::EmptyPath`].
pub fn is_empty(&self) -> bool {
matches!(self, Self::EmptyPath)
}
}

impl<T> DataplanePath<T>
where
T: Deref<Target = [u8]>,
{
/// Returns the raw binary of the path.
pub fn raw(&self) -> &[u8] {
match self {
DataplanePath::EmptyPath => &[],
DataplanePath::Standard(path) => path.raw(),
DataplanePath::Unsupported { bytes, .. } => bytes.deref(),
}
}

/// Creates a new DataplanePath by copying this one into the provided backing buffer.
///
/// # Panics
///
/// Panics if the provided buffer does not have the same length as self.raw().
pub fn copy_to_slice<'b>(&self, buffer: &'b mut [u8]) -> DataplanePath<&'b mut [u8]> {
match self {
DataplanePath::EmptyPath => DataplanePath::EmptyPath,
DataplanePath::Standard(path) => DataplanePath::Standard(path.copy_to_slice(buffer)),
DataplanePath::Unsupported { path_type, bytes } => {
buffer.copy_from_slice(bytes);
DataplanePath::Unsupported {
path_type: *path_type,
bytes: buffer,
}
}
}
}

/// Reverses the path.
pub fn to_reversed(&self) -> Result<Self, UnsupportedPathType> {
pub fn to_reversed(&self) -> Result<DataplanePath, UnsupportedPathType> {
match self {
Self::EmptyPath => Ok(Self::EmptyPath),
Self::Standard(standard_path) => Ok(Self::Standard(standard_path.to_reversed())),
Self::EmptyPath => Ok(DataplanePath::EmptyPath),
Self::Standard(standard_path) => {
Ok(DataplanePath::Standard(standard_path.to_reversed()))
}
Self::Unsupported { path_type, .. } => Err(UnsupportedPathType(u8::from(*path_type))),
}
}
}

impl DataplanePath<Bytes> {
/// Returns a deep copy of the object.
pub fn deep_copy(&self) -> Self {
match self {
Self::EmptyPath => Self::EmptyPath,
Self::Standard(path) => Self::Standard(path.deep_copy()),
Self::Unsupported { path_type, bytes } => Self::Unsupported {
path_type: *path_type,
bytes: Bytes::copy_from_slice(bytes),
},
}
}

/// Reverses the path in place.
pub fn reverse(&mut self) -> Result<&mut Self, UnsupportedPathType> {
Expand All @@ -116,10 +166,18 @@ impl DataplanePath {
}
Ok(self)
}
}

/// Returns true iff the path is a [`DataplanePath::EmptyPath`].
pub fn is_empty(&self) -> bool {
self == &Self::EmptyPath
impl From<DataplanePath<&mut [u8]>> for DataplanePath<Bytes> {
fn from(value: DataplanePath<&mut [u8]>) -> Self {
match value {
DataplanePath::EmptyPath => DataplanePath::EmptyPath,
DataplanePath::Standard(path) => DataplanePath::Standard(path.into()),
DataplanePath::Unsupported { path_type, bytes } => DataplanePath::Unsupported {
path_type,
bytes: Bytes::copy_from_slice(bytes),
},
}
}
}

Expand Down
Loading

0 comments on commit 6b637ca

Please sign in to comment.