Skip to content

Commit

Permalink
feat: remove unnecessary VL field from messages
Browse files Browse the repository at this point in the history
  • Loading branch information
dadada committed Apr 8, 2024
1 parent 3e7fd25 commit 5dfe7f3
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 56 deletions.
19 changes: 5 additions & 14 deletions a653rs-router-linux/src/network.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use a653rs_linux::partition::ApexLinuxPartition;
use a653rs_router::prelude::*;
use std::{mem::size_of, net::UdpSocket};
use std::net::UdpSocket;

#[derive(Debug)]
pub struct UdpNetworkInterface<const MTU: usize>;
Expand All @@ -15,14 +15,8 @@ impl<const MTU: usize> PlatformNetworkInterface for UdpNetworkInterface<MTU> {
let sock = get_interface(id)?;
match sock.sock.recv(buffer) {
Ok(read) => {
let vl_id_len = size_of::<VirtualLinkId>();
let vl_id = &buffer[0..vl_id_len];
let mut vl_id_buf = [0u8; size_of::<VirtualLinkId>()];
vl_id_buf.copy_from_slice(vl_id);
let vl_id = u32::from_be_bytes(vl_id_buf);
let _vl_id = VirtualLinkId::from_u32(vl_id);
let msg = &buffer[vl_id_len..read];
router_trace!("Received message from UDP socket for VL {vl_id}: {:?}", msg);
let msg = &buffer[..read];
router_trace!("Received message from UDP socket");
Ok(msg)
}
Err(_) => Err(InterfaceError::NoData),
Expand All @@ -31,18 +25,15 @@ impl<const MTU: usize> PlatformNetworkInterface for UdpNetworkInterface<MTU> {

fn platform_interface_send_unchecked(
id: NetworkInterfaceId,
vl: VirtualLinkId,
buffer: &[u8],
) -> Result<usize, InterfaceError> {
// This is safe, because the interfaces are only created before the list of
// interfaces is used
let sock = get_interface(id)?;
let vlid = vl.into_inner().to_be_bytes();
let udp_buf = [vlid.as_slice(), buffer].concat();
let res = sock.sock.send(&udp_buf);
let res = sock.sock.send(buffer);
match res {
Ok(trans) => {
router_trace!("Send {} bytes to UDP socket", udp_buf.len());
router_trace!("Send {} bytes to UDP socket", buffer.len());
Ok(trans)
}
Err(e) => {
Expand Down
1 change: 0 additions & 1 deletion a653rs-router-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ pub struct DummyNetIntf;
impl PlatformNetworkInterface for DummyNetIntf {
fn platform_interface_send_unchecked(
_id: NetworkInterfaceId,
_vl: a653rs_router::prelude::VirtualLinkId,
_buffer: &[u8],
) -> Result<usize, a653rs_router::prelude::InterfaceError> {
Ok(1)
Expand Down
29 changes: 9 additions & 20 deletions a653rs-router-zynq7000/src/network.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use a653rs_router::prelude::{
CreateNetworkInterfaceId, InterfaceConfig, InterfaceError, NetworkInterfaceId,
PlatformNetworkInterface, VirtualLinkId,
PlatformNetworkInterface,
};
use cobs::{decode_in_place, encode};
use core::mem::size_of;
Expand All @@ -21,7 +21,6 @@ mod config {
}

struct UartFrame<'p, const MTU: usize> {
vl: VirtualLinkId,
pl: &'p [u8],
}

Expand Down Expand Up @@ -49,25 +48,21 @@ impl<'p, const MTU: usize> UartFrame<'p, MTU> {
return Err(());
}

// VL ID
let vl_id: [u8; 2] = (self.vl.into_inner() as u16).to_be_bytes();
buf[0..2].copy_from_slice(&vl_id);

// Payload
buf[2..self.pl.len() + 2].copy_from_slice(self.pl);
buf[..self.pl.len()].copy_from_slice(self.pl);

// CRC
let crc = crc16::State::<crc16::USB>::calculate(&buf[..self.pl.len() + 2]);
let crc = crc16::State::<crc16::USB>::calculate(&buf[..self.pl.len()]);
let crc: [u8; 2] = crc.to_be_bytes();
buf[self.pl.len() + 2..self.pl.len() + 4].copy_from_slice(&crc);
buf[self.pl.len()..self.pl.len() + 2].copy_from_slice(&crc);

// COBS encode
let enclen = encode(&buf[0..self.pl.len() + 4], encoded);
let enclen = encode(&buf[0..self.pl.len() + 2], encoded);

Ok(&encoded[..enclen])
}

fn decode(buf: &mut [u8]) -> Result<(VirtualLinkId, &[u8]), ()> {
fn decode(buf: &mut [u8]) -> Result<&[u8], ()> {
// COBS decode
let declen = decode_in_place(buf).or(Err(()))?;

Expand All @@ -85,12 +80,7 @@ impl<'p, const MTU: usize> UartFrame<'p, MTU> {
return Err(());
}

// VL ID
let (vl, pl) = msg.split_at(2);
let vl: [u8; 2] = vl.try_into().or(Err(()))?;
let vl = u16::from_be_bytes(vl);

Ok((VirtualLinkId::from_u32(vl as u32), pl))
Ok(msg)
}
}

Expand Down Expand Up @@ -198,7 +188,7 @@ where
}
}
match UartFrame::<MTU>::decode(&mut buf) {
Ok((_vl, pl)) => {
Ok(pl) => {
let rpl = &mut buffer[0..pl.len()];
rpl.copy_from_slice(pl);
trace!(end_network_receive, id.0 as u16);
Expand All @@ -213,11 +203,10 @@ where

fn platform_interface_send_unchecked(
id: NetworkInterfaceId,
vl: VirtualLinkId,
buffer: &[u8],
) -> Result<usize, InterfaceError> {
let mut buf = [0u8; UartFrame::<MTU>::max_encoded_len()];
let frame = UartFrame::<MTU> { vl, pl: buffer };
let frame = UartFrame::<MTU> { pl: buffer };

let encoded =
UartFrame::<MTU>::encode(&frame, &mut buf).or(Err(InterfaceError::InvalidData))?;
Expand Down
19 changes: 8 additions & 11 deletions a653rs-router/src/network.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
ports::PortError,
router::{RouterInput, RouterOutput},
types::{DataRate, VirtualLinkId},
types::DataRate,
};

use core::{
Expand Down Expand Up @@ -45,13 +45,13 @@ pub struct NetworkInterface<P: PlatformNetworkInterface> {

impl<H: PlatformNetworkInterface> NetworkInterface<H> {
/// Sends data to the interface.
pub fn send(&self, vl: &VirtualLinkId, buf: &[u8]) -> Result<usize, InterfaceError> {
pub fn send(&self, buf: &[u8]) -> Result<usize, InterfaceError> {
if buf.len() > self.mtu {
return Err(InterfaceError::InsufficientBuffer);
}

router_trace!("Sending to interface");
H::platform_interface_send_unchecked(self.id, *vl, buf)
H::platform_interface_send_unchecked(self.id, buf)
}

/// Receives data from the interface.
Expand All @@ -69,7 +69,6 @@ pub trait PlatformNetworkInterface {
/// Send something to the network and report how long it took.
fn platform_interface_send_unchecked(
id: NetworkInterfaceId,
vl: VirtualLinkId,
buffer: &[u8],
) -> Result<usize, InterfaceError>;

Expand Down Expand Up @@ -158,13 +157,11 @@ impl<H: PlatformNetworkInterface> RouterInput for NetworkInterface<H> {
}

impl<H: PlatformNetworkInterface> RouterOutput for NetworkInterface<H> {
fn send(&self, vl: &VirtualLinkId, buf: &[u8]) -> Result<(), PortError> {
NetworkInterface::send(self, vl, buf)
.map(|_| ())
.map_err(|e| {
router_debug!("Failed to send to network interface: {:?}", e);
PortError::Send
})
fn send(&self, buf: &[u8]) -> Result<(), PortError> {
NetworkInterface::send(self, buf).map(|_| ()).map_err(|e| {
router_debug!("Failed to send to network interface: {:?}", e);
PortError::Send
})
}

fn mtu(&self) -> PayloadSize {
Expand Down
16 changes: 8 additions & 8 deletions a653rs-router/src/ports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl<H: ApexQueuingPortP4> RouterInput for QueuingIn<H> {
}

impl<H: ApexQueuingPortP4> RouterOutput for QueuingOut<H> {
fn send(&self, _vl: &VirtualLinkId, buf: &[u8]) -> Result<(), PortError> {
fn send(&self, buf: &[u8]) -> Result<(), PortError> {
let buf = buf.validate_write(self.inner.msg_size)?;
let timeout = Duration::from_micros(10).as_nanos() as ApexSystemTime;
<H as ApexQueuingPortP4>::send_queuing_message(self.inner.id, buf, timeout)
Expand All @@ -95,7 +95,7 @@ impl<H: ApexQueuingPortP4> RouterOutput for QueuingOut<H> {
}

impl<H: ApexSamplingPortP4> RouterOutput for SamplingOut<H> {
fn send(&self, _vl: &VirtualLinkId, buf: &[u8]) -> Result<(), PortError> {
fn send(&self, buf: &[u8]) -> Result<(), PortError> {
let buf = buf.validate_write(self.inner.msg_size)?;
<H as ApexSamplingPortP4>::write_sampling_message(self.inner.id, buf)
.map_err(|_e| PortError::Send)
Expand Down Expand Up @@ -210,10 +210,10 @@ impl<const M: MessageSize, S: ApexSamplingPortP4> RouterInput for SamplingPortDe
}

impl<const M: MessageSize, S: ApexSamplingPortP4> RouterOutput for SamplingPortSource<M, S> {
fn send(&self, vl: &VirtualLinkId, buf: &[u8]) -> Result<(), PortError> {
router_bench!(begin_apex_send, vl.0 as u16);
fn send(&self, buf: &[u8]) -> Result<(), PortError> {
router_bench!(begin_apex_send, self.id() as u16);
let res = self.send(buf);
router_bench!(end_apex_send, vl.0 as u16);
router_bench!(end_apex_send, self.id() as u16);
res.map_err(|_e| PortError::Send)?;
Ok(())
}
Expand Down Expand Up @@ -243,11 +243,11 @@ impl<const M: MessageSize, const R: MessageRange, Q: ApexQueuingPortP4> RouterIn
impl<const M: MessageSize, const R: MessageRange, Q: ApexQueuingPortP4> RouterOutput
for QueuingPortSender<M, R, Q>
{
fn send(&self, vl: &VirtualLinkId, buf: &[u8]) -> Result<(), PortError> {
fn send(&self, buf: &[u8]) -> Result<(), PortError> {
let timeout = SystemTime::Normal(Duration::from_micros(10));
router_bench!(begin_apex_send, vl.0 as u16);
router_bench!(begin_apex_send, self.id() as u16);
let res = self.send(buf, timeout);
router_bench!(end_apex_send, vl.0 as u16);
router_bench!(end_apex_send, self.id() as u16);
res.map_err(|_e| PortError::Send)?;
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions a653rs-router/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ pub trait RouterOutput {
/// Sends `buf` to a virtual link on this `Output`.
///
/// Returns a slice to the portion of `buf` that has *not* been transmitted.
fn send(&self, vl: &VirtualLinkId, buf: &[u8]) -> Result<(), PortError>;
fn send(&self, buf: &[u8]) -> Result<(), PortError>;

/// Maximum transfer unit
fn mtu(&self) -> PayloadSize;
Expand All @@ -193,7 +193,7 @@ impl<'a, const I: usize, const O: usize> RouteTable<'a, I, O> {
router_debug!("Received from {vl:?}: {buf:?}");
let outs = self.outputs.get(vl).ok_or(RouteError::InvalidVl)?;
for out in outs.into_iter() {
out.send(vl, buf).map_err(|e| {
out.send(buf).map_err(|e| {
router_debug!("Failed to route {:?}", vl);
e
})?;
Expand Down

0 comments on commit 5dfe7f3

Please sign in to comment.