Skip to content

Commit

Permalink
fix: check that message sizes of ports match
Browse files Browse the repository at this point in the history
Fixes #4
  • Loading branch information
dadada committed Apr 8, 2024
1 parent 9b2a7e9 commit 1e8b725
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 8 deletions.
8 changes: 8 additions & 0 deletions a653rs-router/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ impl<H: PlatformNetworkInterface> RouterInput for NetworkInterface<H> {
PortError::Receive
})
}

fn mtu(&self) -> PayloadSize {
self.mtu
}
}

impl<H: PlatformNetworkInterface> RouterOutput for NetworkInterface<H> {
Expand All @@ -169,6 +173,10 @@ impl<H: PlatformNetworkInterface> RouterOutput for NetworkInterface<H> {
PortError::Send
})
}

fn mtu(&self) -> PayloadSize {
self.mtu
}
}

/// Inteface error type.
Expand Down
33 changes: 33 additions & 0 deletions a653rs-router/src/ports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use core::{fmt::Display, marker::PhantomData, str::FromStr, time::Duration};

use crate::{
config::{QueuingInCfg, QueuingOutCfg, SamplingInCfg, SamplingOutCfg},
network::PayloadSize,
prelude::*,
router::{RouterInput, RouterOutput},
};
Expand Down Expand Up @@ -62,6 +63,10 @@ impl<H: ApexSamplingPortP4> RouterInput for SamplingIn<H> {
}?;
Ok((*vl, &buf[..(len as usize)]))
}

fn mtu(&self) -> PayloadSize {
self.inner.msg_size as PayloadSize
}
}

impl<H: ApexQueuingPortP4> RouterInput for QueuingIn<H> {
Expand All @@ -78,6 +83,10 @@ impl<H: ApexQueuingPortP4> RouterInput for QueuingIn<H> {
}?;
Ok((*vl, &buf[..val as usize]))
}

fn mtu(&self) -> PayloadSize {
self.inner.msg_size as PayloadSize
}
}

impl<H: ApexQueuingPortP4> RouterOutput for QueuingOut<H> {
Expand All @@ -87,6 +96,10 @@ impl<H: ApexQueuingPortP4> RouterOutput for QueuingOut<H> {
<H as ApexQueuingPortP4>::send_queuing_message(self.inner.id, buf, timeout)
.map_err(|_e| PortError::Send)
}

fn mtu(&self) -> PayloadSize {
self.inner.msg_size as PayloadSize
}
}

impl<H: ApexSamplingPortP4> RouterOutput for SamplingOut<H> {
Expand All @@ -95,6 +108,10 @@ impl<H: ApexSamplingPortP4> RouterOutput for SamplingOut<H> {
<H as ApexSamplingPortP4>::write_sampling_message(self.inner.id, buf)
.map_err(|_e| PortError::Send)
}

fn mtu(&self) -> PayloadSize {
self.inner.msg_size as PayloadSize
}
}

impl<H: ApexSamplingPortP4> SamplingIn<H> {
Expand Down Expand Up @@ -198,6 +215,10 @@ impl<const M: MessageSize, S: ApexSamplingPortP4> RouterInput for SamplingPortDe
let (_val, data) = res.map_err(|_e| PortError::Receive)?;
Ok((*vl, data))
}

fn mtu(&self) -> PayloadSize {
M as PayloadSize
}
}

impl<const M: MessageSize, S: ApexSamplingPortP4> RouterOutput for SamplingPortSource<M, S> {
Expand All @@ -208,6 +229,10 @@ impl<const M: MessageSize, S: ApexSamplingPortP4> RouterOutput for SamplingPortS
res.map_err(|_e| PortError::Send)?;
Ok(())
}

fn mtu(&self) -> PayloadSize {
M as PayloadSize
}
}

impl<const M: MessageSize, const R: MessageRange, Q: ApexQueuingPortP4> RouterInput
Expand All @@ -225,6 +250,10 @@ impl<const M: MessageSize, const R: MessageRange, Q: ApexQueuingPortP4> RouterIn
let (buf, _overflow) = res.map_err(|_e| PortError::Receive)?;
Ok((*vl, buf))
}

fn mtu(&self) -> PayloadSize {
M as PayloadSize
}
}

impl<const M: MessageSize, const R: MessageRange, Q: ApexQueuingPortP4> RouterOutput
Expand All @@ -238,6 +267,10 @@ impl<const M: MessageSize, const R: MessageRange, Q: ApexQueuingPortP4> RouterOu
res.map_err(|_e| PortError::Send)?;
Ok(())
}

fn mtu(&self) -> PayloadSize {
M as PayloadSize
}
}

trait BufferExt {
Expand Down
26 changes: 21 additions & 5 deletions a653rs-router/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
InterfacesConfig, PortConfig, PortName, PortsConfig, RouterConfigError, VirtualLinksConfig,
},
error::Error,
network::{CreateNetworkInterface, NetworkInterface, PlatformNetworkInterface},
network::{CreateNetworkInterface, NetworkInterface, PayloadSize, PlatformNetworkInterface},
ports::{Port, PortError, QueuingIn, QueuingOut, SamplingIn, SamplingOut},
prelude::InterfaceName,
scheduler::{DeadlineRrScheduler, ScheduleError, Scheduler, TimeSource},
Expand Down Expand Up @@ -165,6 +165,9 @@ pub trait RouterInput {
vl: &VirtualLinkId,
buf: &'a mut [u8],
) -> Result<(VirtualLinkId, &'a [u8]), PortError>;

/// Maximum transfer unit
fn mtu(&self) -> PayloadSize;
}

/// An output from a virtual link.
Expand All @@ -173,6 +176,9 @@ pub trait RouterOutput {
///
/// Returns a slice to the portion of `buf` that has *not* been transmitted.
fn send(&self, vl: &VirtualLinkId, buf: &[u8]) -> Result<(), PortError>;

/// Maximum transfer unit
fn mtu(&self) -> PayloadSize;
}

type FwdTable<'a, const I: usize, const O: usize> =
Expand Down Expand Up @@ -259,14 +265,24 @@ impl<'a, const I: usize, const O: usize> RouteTable<'a, I, O> {
router_debug!("Unknown input: {}", cfg.src.deref());
RouterConfigError::Destination
})?;
let input_msg_size = inp.mtu();
let outs: Result<Vec<_, O>, RouterConfigError> = cfg
.dsts
.iter()
.map(|d| {
outputs.get(d).ok_or_else(|| {
router_debug!("Unknown output {}", d.deref());
RouterConfigError::Source
})
outputs
.get(d)
.ok_or_else(|| {
router_debug!("Unknown output {}", d.deref());
RouterConfigError::Source
})
.and_then(|outp| {
if outp.mtu() == input_msg_size {
Ok(outp)
} else {
Err(RouterConfigError::Destination)
}
})
})
.map(|d| d.copied())
.collect();
Expand Down
2 changes: 1 addition & 1 deletion examples/config/echo-remote/client-alt/router.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ ports:
interfaces:
NodeB:
destination: "192.168.1.2:8082"
mtu: 1500
mtu: 1000
rate: 10000000
source: "0.0.0.0:8081"
2 changes: 1 addition & 1 deletion examples/config/echo-remote/client/router.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ ports:
interfaces:
NodeB:
destination: "192.168.1.2:8082"
mtu: 1500
mtu: 1000
rate: 10000000
source: "0.0.0.0:8081"
2 changes: 1 addition & 1 deletion examples/config/echo-remote/server/router.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ ports:
interfaces:
NodeA:
destination: "192.168.1.1:8081"
mtu: 1500
mtu: 1000
rate: 10000000
source: "0.0.0.0:8082"

0 comments on commit 1e8b725

Please sign in to comment.