diff --git a/services/com/src/lib.rs b/services/com/src/lib.rs index 1f98ef955..951639c95 100644 --- a/services/com/src/lib.rs +++ b/services/com/src/lib.rs @@ -540,19 +540,6 @@ impl Com { if pkt.len() > NET_MTU { return Err(xous::Error::OutOfMemory) } - { - // this is a hack to make loopbacks work on smoltcp. Work-around taken from Redox, but tracking this issue as well: - // https://github.com/smoltcp-rs/smoltcp/issues/50 and https://github.com/smoltcp-rs/smoltcp/issues/55 - // Inject the loopback packets. loopback packets always take priority, for now. - if let Some(loop_packet) = self.loopback_buf.borrow_mut().pop_front() { - // pkt.len() is almost always not loop_packet.len(), just copy the first bit that can fit and the rest is garbarge... - for (&s, d) in loop_packet.iter().zip(pkt.iter_mut()) { - *d = s; - } - // skips polling the COM -- let's hope there wasn't a race condition on the interrupt - return Ok(()) - } - } let mut prealloc: [u8; NET_MTU] = [0; NET_MTU]; let len_bytes = (pkt.len() as u16).to_be_bytes(); prealloc[0] = len_bytes[0]; @@ -566,6 +553,19 @@ impl Com { Ok(()) } + pub fn wlan_fetch_loopback_packet(&self, pkt: &mut [u8]) -> Result<(), xous::Error> { + // this is a hack to make loopbacks work on smoltcp. Work-around taken from Redox, but tracking this issue as well: + // https://github.com/smoltcp-rs/smoltcp/issues/50 and https://github.com/smoltcp-rs/smoltcp/issues/55 + // Inject the loopback packets. loopback packets always take priority, for now. + if let Some(loop_packet) = self.loopback_buf.borrow_mut().pop_front() { + // pkt.len() is almost always not loop_packet.len(), just copy the first bit that can fit and the rest is garbarge... + for (&s, d) in loop_packet.iter().zip(pkt.iter_mut()) { + *d = s; + } + } + Ok(()) + } + // this is a hack to make loopbacks work on smoltcp. Work-around taken from Redox, but tracking this issue as well: // https://github.com/smoltcp-rs/smoltcp/issues/50 and https://github.com/smoltcp-rs/smoltcp/issues/55 // this function handles enqueuing packets for local injection diff --git a/services/net/src/device.rs b/services/net/src/device.rs index 277172a7a..705031dc6 100644 --- a/services/net/src/device.rs +++ b/services/net/src/device.rs @@ -13,6 +13,8 @@ use smoltcp::{ use crate::{MAC_ADDRESS_LSB, MAC_ADDRESS_MSB}; use core::sync::atomic::Ordering; +use std::collections::VecDeque; +use std::sync::{Arc, Mutex}; pub struct NetPhy { rx_buffer: [u8; NET_MTU], @@ -20,6 +22,8 @@ pub struct NetPhy { com: Com, rx_avail: Option, loopback_conn: xous::CID, + // tracks the length (and count) of the loopback packets pending + loopback_pending: Arc::>>, } impl<'a> NetPhy { @@ -30,6 +34,7 @@ impl<'a> NetPhy { com: Com::new(&xns).unwrap(), rx_avail: None, loopback_conn, + loopback_pending: Arc::new(Mutex::new(VecDeque::new())), } } // returns None if there was a slot to put the availability into @@ -49,18 +54,26 @@ impl<'a> phy::Device<'a> for NetPhy { type TxToken = NetPhyTxToken<'a>; fn receive(&'a mut self) -> Option<(Self::RxToken, Self::TxToken)> { - if let Some(rx_len) = self.rx_avail.take() { - self.com.wlan_fetch_packet(&mut self.rx_buffer[..rx_len as usize]).expect("Couldn't call wlan_fetch_packet in device adapter"); + if let Some(rx_len) = self.loopback_pending.lock().unwrap().pop_front() { + // loopback takes precedence + self.com.wlan_fetch_loopback_packet(&mut self.rx_buffer[..rx_len as usize]).expect("Couldn't call wlan_fetch_packet in device adapter"); Some((NetPhyRxToken{buf: &mut self.rx_buffer[..rx_len as usize]}, - NetPhyTxToken{buf: &mut self.tx_buffer[..], com: & self.com, loopback_conn: self.loopback_conn})) + NetPhyTxToken{buf: &mut self.tx_buffer[..], com: & self.com, loopback_conn: self.loopback_conn, loopback_count: self.loopback_pending.clone()})) } else { - None + if let Some(rx_len) = self.rx_avail.take() { + self.com.wlan_fetch_packet(&mut self.rx_buffer[..rx_len as usize]).expect("Couldn't call wlan_fetch_packet in device adapter"); + + Some((NetPhyRxToken{buf: &mut self.rx_buffer[..rx_len as usize]}, + NetPhyTxToken{buf: &mut self.tx_buffer[..], com: & self.com, loopback_conn: self.loopback_conn, loopback_count: self.loopback_pending.clone()})) + } else { + None + } } } fn transmit(&'a mut self) -> Option { - Some(NetPhyTxToken{buf: &mut self.tx_buffer[..], com: &self.com, loopback_conn: self.loopback_conn}) + Some(NetPhyTxToken{buf: &mut self.tx_buffer[..], com: &self.com, loopback_conn: self.loopback_conn, loopback_count: self.loopback_pending.clone()}) } fn capabilities(&self) -> DeviceCapabilities { @@ -90,10 +103,12 @@ pub struct NetPhyTxToken<'a> { buf: &'a mut [u8], com: &'a Com, loopback_conn: xous::CID, + loopback_count: Arc::>>, } impl <'a> NetPhyTxToken<'a> { /// Initiates the Rx side of things to read out the loopback packet that was queued fn loopback_rx(&self, rxlen: usize) { + self.loopback_count.lock().unwrap().push_back(rxlen as u16); xous::try_send_message(self.loopback_conn, xous::Message::new_scalar(crate::Opcode::LoopbackRx.to_usize().unwrap(), rxlen, 0, 0, 0) ).ok(); diff --git a/services/net/src/main.rs b/services/net/src/main.rs index 5179d4f90..768220ade 100644 --- a/services/net/src/main.rs +++ b/services/net/src/main.rs @@ -1263,11 +1263,15 @@ fn main() -> ! { } } } - Some(Opcode::LoopbackRx) => msg_scalar_unpack!(msg, rxlen, _, _, _, { + Some(Opcode::LoopbackRx) => msg_scalar_unpack!(msg, _rxlen, _, _, _, { + /* + // the rx buf for loopback is different from the wlan interface + // loopback uses an "infinite" internal buffer with its own length tracking, so we don't need + // to track rxlen match iface.device_mut().push_rx_avail(rxlen as u16) { None => {} //log::info!("pushed {} bytes avail to iface", rxlen), - Some(_) => log::warn!("Got more packets, but smoltcp didn't drain them in time"), - } + Some(_) => log::warn!("Got more loopback packets, but smoltcp didn't drain them in time"), + } */ match xous::try_send_message( net_conn, Message::new_scalar(