From 760957154ec5914b1d0e12689a7f381d591867ee Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 21 Feb 2022 00:54:46 +0800 Subject: [PATCH] add a crappy but slightly more accurate approximation of UDP to hosted mode It's crappy because it uses a busy wait loop to poll the udp rx socket in order to accurately emulate the Xous async callback feature (which is _not_ emulated for Tcp, for example). It's more accurate because this emulation happens. But it's a lot more complicated to implement it because zero-cost async UDP callbacks aren't really a host-native feature (but they are a Xous-native feature!), and I didn't want to .. pull in tokio or mio code surface into the Xous hosted mode, seemed a bit like crushing a fly by dropping a house on it. Maybe that's a fine solution in Oz, but Xous doesn't live in Oz. --- services/dns/src/lib.rs | 1 + services/net/src/api.rs | 1 + services/net/src/protocols.rs | 6 + services/net/src/protocols/tcp_hosted.rs | 23 +- services/net/src/protocols/udp.rs | 47 +--- services/net/src/protocols/udp_hosted.rs | 325 +++++++++++++++++++++++ services/shellchat/src/cmds/net_cmd.rs | 28 +- 7 files changed, 362 insertions(+), 69 deletions(-) create mode 100644 services/net/src/protocols/udp_hosted.rs diff --git a/services/dns/src/lib.rs b/services/dns/src/lib.rs index 7a8f2644c..18f491b09 100644 --- a/services/dns/src/lib.rs +++ b/services/dns/src/lib.rs @@ -1,4 +1,5 @@ pub mod api; +#[allow(unused_imports)] use api::*; #[cfg(any(target_os = "none", target_os = "xous"))] diff --git a/services/net/src/api.rs b/services/net/src/api.rs index 8230c7a6f..5c4f9488f 100644 --- a/services/net/src/api.rs +++ b/services/net/src/api.rs @@ -1,4 +1,5 @@ pub(crate) mod udp; +#[allow(unused_imports)] // needed to keep hosted mode quiet, since the Udp implementation is a bodge pub(crate) use udp::*; pub(crate) mod ping; pub(crate) use ping::*; diff --git a/services/net/src/protocols.rs b/services/net/src/protocols.rs index f132aa563..00f5516fc 100644 --- a/services/net/src/protocols.rs +++ b/services/net/src/protocols.rs @@ -1,5 +1,11 @@ +#[cfg(any(target_os = "none", target_os = "xous"))] pub mod udp; +#[cfg(any(target_os = "none", target_os = "xous"))] pub use udp::*; +#[cfg(not(any(target_os = "none", target_os = "xous")))] +pub mod udp_hosted; +#[cfg(not(any(target_os = "none", target_os = "xous")))] +pub use udp_hosted::*; #[cfg(any(target_os = "none", target_os = "xous"))] pub mod dns; diff --git a/services/net/src/protocols/tcp_hosted.rs b/services/net/src/protocols/tcp_hosted.rs index 1eaea86b0..aad9fcb75 100644 --- a/services/net/src/protocols/tcp_hosted.rs +++ b/services/net/src/protocols/tcp_hosted.rs @@ -13,26 +13,11 @@ pub struct TcpStream { } impl TcpStream { - /// This is routine that allows a TcpListener to create a TcpStream object. The following must be observed: - /// 1. The `cb_sid`, `rx_buf` and `rx_notify` are the memory locations mapped into the already-allocated Rx thread - /// 2. These fields are not to be re-used in the Listener object that succeeds the current one - /* - pub(crate) fn build_from_listener( - net: NetConn, - cb_sid: SID, - remote: SocketAddr, - local_port: u16, - rx_buf: Arc::>>, - rx_notify: Arc>, - ) -> TcpStream { - TcpStream { - } - }*/ pub fn connect(maybe_socket: io::Result<&SocketAddr>) -> io::Result { if let Ok(socket) = maybe_socket { match TcpStreamHosted::connect(socket) { Ok(stream) => { - Ok(TcpStream{ + Ok(TcpStream { stream, }) }, @@ -47,7 +32,7 @@ impl TcpStream { let d = std::time::Duration::from_millis(duration.total_millis()); match TcpStreamHosted::connect_timeout(socket, d) { Ok(stream) => { - Ok(TcpStream{ + Ok(TcpStream { stream, }) }, @@ -68,7 +53,7 @@ impl TcpStream { Some(socket_addr) => { match TcpStreamHosted::connect_timeout(&socket_addr, d) { Ok(stream) => { - Ok(TcpStream{ + Ok(TcpStream { stream, }) }, @@ -83,7 +68,7 @@ impl TcpStream { } else { match TcpStreamHosted::connect(addr) { Ok(stream) => { - Ok(TcpStream{ + Ok(TcpStream { stream, }) }, diff --git a/services/net/src/protocols/udp.rs b/services/net/src/protocols/udp.rs index c62210b92..c7d7a5f57 100644 --- a/services/net/src/protocols/udp.rs +++ b/services/net/src/protocols/udp.rs @@ -198,39 +198,17 @@ impl UdpSocket { Ok(self.write_timeout) } - fn recv_inner(&self, pkt: &mut[u8], do_peek: bool) -> io::Result { - let timeout = if let Some(to) = self.read_timeout { - to.total_millis() + self.ticktimer.elapsed_ms() - } else { - u64::MAX - }; - loop { - if self.rx_buf.lock().unwrap().len() > 0 { - let rx_pkt = self.rx_buf.lock().unwrap().remove(0); // safe b/c len > 1, checked above - for (&src, dst) in rx_pkt.data.iter().zip(pkt.iter_mut()) { - *dst = src; - } - let len = rx_pkt.data.len(); - if do_peek { - // re-insert the element after taking it out. We can't mux it above with the if/else because - // a peek is a borrow, but a remove is a move, and that's not easy to coerce in Rust - self.rx_buf.lock().unwrap().insert(0, rx_pkt); - } - return Ok(len); - } else if self.nonblocking { - return Err(Error::new(ErrorKind::WouldBlock, "Nonblocking mode: Rx is empty")); - } - if timeout < self.ticktimer.elapsed_ms() { - return Err(Error::new(ErrorKind::WouldBlock, "UDP Rx timeout reached")); - } - xous::yield_slice(); - } - } pub fn recv(&self, pkt: &mut [u8]) -> io::Result { - self.recv_inner(pkt, false) + self.recv_from_inner(pkt, false).map(|(len, _addr)| len) } pub fn peek(&self, pkt: &mut [u8]) -> io::Result { - self.recv_inner(pkt, true) + self.recv_from_inner(pkt, true).map(|(len, _addr)| len) + } + pub fn recv_from(&self, pkt: &mut [u8]) -> io::Result<(usize, SocketAddr)> { + self.recv_from_inner(pkt, false) + } + pub fn peek_from(&self, pkt: &mut [u8]) -> io::Result<(usize, SocketAddr)> { + self.recv_from_inner(pkt, true) } fn recv_from_inner(&self, pkt: &mut [u8], do_peek: bool) -> io::Result<(usize, SocketAddr)> { @@ -280,20 +258,13 @@ impl UdpSocket { socket_addr )); } - if timeout > self.ticktimer.elapsed_ms() { + if timeout < self.ticktimer.elapsed_ms() { return Err(Error::new(ErrorKind::WouldBlock, "UDP Rx timeout reached")); } xous::yield_slice(); } } - pub fn recv_from(&self, pkt: &mut [u8]) -> io::Result<(usize, SocketAddr)> { - self.recv_from_inner(pkt, false) - } - pub fn peek_from(&self, pkt: &mut [u8]) -> io::Result<(usize, SocketAddr)> { - self.recv_from_inner(pkt, true) - } - pub fn connect(&mut self, maybe_socket_addr: io::Result<&SocketAddr>) -> io::Result<()> { let socket = *maybe_socket_addr?; self.dest_socket = Some(socket); diff --git a/services/net/src/protocols/udp_hosted.rs b/services/net/src/protocols/udp_hosted.rs new file mode 100644 index 000000000..03240e36c --- /dev/null +++ b/services/net/src/protocols/udp_hosted.rs @@ -0,0 +1,325 @@ +use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, ToSocketAddrs}; +use std::unimplemented; +use std::io; +use std::io::{Error, ErrorKind, Result}; +use std::net::UdpSocket as UdpSocketHosted; + +use smoltcp::time::Duration; +use crate::api::XousScalarEndpoint; +use core::sync::atomic::{AtomicBool, Ordering}; + + +use std::sync::{Arc, Mutex}; +use std::thread; +use std::thread::JoinHandle; + +struct UdpRx { + data: Vec, + from: SocketAddr, +} +///////// UdpSocket implementation +pub struct UdpSocket { + handle: Option>, + nonblocking: bool, + socket: Arc>, + rx_buf: Arc>>, + notify: Arc>, + should_drop: Arc, + read_timeout: Option, + ticktimer: ticktimer_server::Ticktimer, +} + +#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] +pub(crate) enum UdpHostedOpcode { + Tx, + Drop, +} + +// next steps: build this stub, and figure out how to clean up the error handling code. +impl UdpSocket { + pub fn bind(maybe_socket: io::Result<&SocketAddr>) -> Result { + if let Ok(socket) = maybe_socket { + UdpSocket::bind_inner(socket) + } else { + Err(Error::new(ErrorKind::InvalidInput, "IP address invalid")) + } + } + pub fn bind_xous(socket: A, _max_payload: Option) -> Result { + match socket.to_socket_addrs() { + Ok(socks) => { + match socks.into_iter().next() { + Some(socket_addr) => { + UdpSocket::bind_inner(&socket_addr) + } + _ => Err(Error::new(ErrorKind::InvalidInput, "IP address invalid")) + } + } + _ => Err(Error::new(ErrorKind::InvalidInput, "IP address invalid")) + } + } + fn bind_inner(socket: &SocketAddr) -> Result { + if let Ok(udpsocket_naked) = UdpSocketHosted::bind(socket) { + let rx_buf = Arc::new(Mutex::new(Vec::new())); + let notify = Arc::new(Mutex::new(XousScalarEndpoint::new())); + let should_drop = Arc::new(AtomicBool::new(false)); + udpsocket_naked.set_nonblocking(true).expect("hosted mode couldn't set socket to nonblocking"); // we emulate blocking behavior outside this thread + let udpsocket = Arc::new(Mutex::new(udpsocket_naked)); + let handle = thread::spawn({ + let udpsocket = udpsocket.clone(); + let should_drop = should_drop.clone(); + let rx_buf = rx_buf.clone(); + let notify = notify.clone(); + move || { + let mut buf = [0u8; 65536]; + let tt = ticktimer_server::Ticktimer::new().unwrap(); + loop { + if should_drop.load(Ordering::Relaxed) { + break; + } else { + if let Ok((len, addr)) = udpsocket.lock().unwrap().recv_from(&mut buf) { + log::info!("received {} bytes from {:?}", len, addr); + rx_buf.lock().unwrap().push(UdpRx { data: buf[..len].to_vec(), from: addr }); + notify.lock().unwrap().notify(); // this will only notify if a destination has been set + } + // give some time for things to not deadlock + tt.sleep_ms(100).unwrap(); + } + } + } + }); + Ok(UdpSocket { + handle: Some(handle), + nonblocking: false, + socket: udpsocket, + rx_buf, + notify, + should_drop, + read_timeout: None, + ticktimer: ticktimer_server::Ticktimer::new().unwrap(), + }) + } else { + Err(Error::new(ErrorKind::InvalidInput, "IP address invalid")) + } + + } + + pub fn get_nonblocking(&self) -> bool { + self.nonblocking + } + + pub fn set_scalar_notification(&mut self, cid: xous::CID, op: usize, args: [Option; 4]) { + self.notify.lock().unwrap().set(cid, op, args); + } + pub fn clear_scalar_notification(&mut self) { + self.notify.lock().unwrap().clear(); + } + + pub fn set_read_timeout(&mut self, timeout: Option) -> io::Result<()> { + self.read_timeout = timeout; + let t = match timeout { + Some(d) => { + Some(std::time::Duration::from_millis(d.total_millis())) + } + None => None, + }; + self.socket.lock().unwrap().set_read_timeout(t) + } + + pub fn set_write_timeout(&mut self, timeout: Option) -> io::Result<()> { + let t = match timeout { + Some(d) => { + Some(std::time::Duration::from_millis(d.total_millis())) + } + None => None, + }; + self.socket.lock().unwrap().set_write_timeout(t) + } + + pub fn read_timeout(&self) -> io::Result> { + match self.socket.lock().unwrap().read_timeout() { + Ok(maybe_t) => { + match maybe_t { + Some(t) => { + Ok(Some(Duration::from_millis(t.as_millis() as u64))) + } + None => Ok(None) + } + }, + Err(e) => Err(e), + } + } + + pub fn write_timeout(&self) -> io::Result> { + match self.socket.lock().unwrap().write_timeout() { + Ok(maybe_t) => { + match maybe_t { + Some(t) => { + Ok(Some(Duration::from_millis(t.as_millis() as u64))) + } + None => Ok(None) + } + }, + Err(e) => Err(e), + } + } + + pub fn recv(&self, pkt: &mut [u8]) -> io::Result { + self.recv_from_inner(pkt, false).map(|(len, _addr)| len) + } + pub fn peek(&self, pkt: &mut [u8]) -> io::Result { + self.recv_from_inner(pkt, true).map(|(len, _addr)| len) + } + + + pub fn recv_from(&self, pkt: &mut [u8]) -> io::Result<(usize, SocketAddr)> { + self.recv_from_inner(pkt, false) + } + pub fn peek_from(&self, pkt: &mut [u8]) -> io::Result<(usize, SocketAddr)> { + self.recv_from_inner(pkt, true) + } + + fn recv_from_inner(&self, pkt: &mut[u8], do_peek: bool) -> io::Result<(usize, SocketAddr)> { + let timeout = if let Some(to) = self.read_timeout { + to.total_millis() + self.ticktimer.elapsed_ms() + } else { + u64::MAX + }; + loop { + if self.rx_buf.lock().unwrap().len() > 0 { + let rx_pkt = self.rx_buf.lock().unwrap().remove(0); // safe b/c len > 1, checked above + for (&src, dst) in rx_pkt.data.iter().zip(pkt.iter_mut()) { + *dst = src; + } + let len = rx_pkt.data.len(); + let socket_addr = rx_pkt.from; + if do_peek { + // re-insert the element after taking it out. We can't mux it above with the if/else because + // a peek is a borrow, but a remove is a move, and that's not easy to coerce in Rust + self.rx_buf.lock().unwrap().insert(0, rx_pkt); + } + return Ok(( + len, + socket_addr + )); + } + if timeout < self.ticktimer.elapsed_ms() { + return Err(Error::new(ErrorKind::WouldBlock, "UDP Rx timeout reached")); + } + xous::yield_slice(); + } + } + + pub fn connect(&mut self, maybe_socket_addr: io::Result<&SocketAddr>) -> io::Result<()> { + let socket = *maybe_socket_addr?; + self.socket.lock().unwrap().connect(socket) + } + pub fn connect_xous(&mut self, addr: A) -> io::Result<()> { + self.socket.lock().unwrap().connect(addr) + } + + pub fn send(&mut self, pkt: &[u8]) -> io::Result { + self.socket.lock().unwrap().send(pkt) + } + + pub fn send_to(&mut self, pkt: &[u8], addr: &SocketAddr) -> io::Result { + self.socket.lock().unwrap().send_to(pkt, addr) + } + pub fn send_to_xous(&mut self, pkt: &[u8], addr: A) -> io::Result { + self.socket.lock().unwrap().send_to(pkt, addr) + } + + pub fn socket_addr(&self) -> io::Result { + self.socket.lock().unwrap().local_addr() + } + + pub fn peer_addr(&self) -> io::Result { + self.socket.lock().unwrap().peer_addr() + } + + pub fn duplicate(&self) -> io::Result { + // do the basic clone connection + let sa = self.socket_addr()?; + let mut cloned_socket = UdpSocket::bind_inner(&sa)?; + // now copy all the properties over as required by spec + cloned_socket.set_read_timeout(self.read_timeout)?; + cloned_socket.set_nonblocking(self.get_nonblocking())?; + // TTL doesn't need to be cloned because it is attached to the port itself in the Net server + Ok(cloned_socket) + } + + pub fn set_ttl(&self, ttl: u32) -> io::Result<()> { + self.socket.lock().unwrap().set_ttl(ttl) + } + + pub fn ttl(&self) -> io::Result { + self.socket.lock().unwrap().ttl() + } + + pub fn take_error(&self) -> io::Result> { + self.socket.lock().unwrap().take_error() + } + + pub fn set_nonblocking(&mut self, nb: bool) -> io::Result<()> { + self.nonblocking = nb; + Ok(()) + } + + //////// the remaining functions are currently unimplemented: we don't have multicast or broadcast support ////////// + pub fn set_broadcast(&self, _: bool) -> io::Result<()> { + unimplemented!("work in progress") + } + + pub fn broadcast(&self) -> io::Result { + unimplemented!("work in progress") + } + + pub fn set_multicast_loop_v4(&self, _: bool) -> io::Result<()> { + unimplemented!("work in progress") + } + + pub fn multicast_loop_v4(&self) -> io::Result { + unimplemented!("work in progress") + } + + pub fn set_multicast_ttl_v4(&self, _: u32) -> io::Result<()> { + unimplemented!("work in progress") + } + + pub fn multicast_ttl_v4(&self) -> io::Result { + unimplemented!("work in progress") + } + + pub fn join_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr) -> io::Result<()> { + unimplemented!("work in progress") + } + + pub fn leave_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr) -> io::Result<()> { + unimplemented!("work in progress") + } + + pub fn set_multicast_loop_v6(&self, _: bool) -> io::Result<()> { + unimplemented!("ipv6 not implemented") + } + + pub fn multicast_loop_v6(&self) -> io::Result { + unimplemented!("ipv6 not implemented") + } + + pub fn join_multicast_v6(&self, _: &Ipv6Addr, _: u32) -> io::Result<()> { + unimplemented!("ipv6 not implemented") + } + + pub fn leave_multicast_v6(&self, _: &Ipv6Addr, _: u32) -> io::Result<()> { + unimplemented!("ipv6 not implemented") + } +} + +impl Drop for UdpSocket { + fn drop(&mut self) { + self.should_drop.store(true, Ordering::Relaxed); + // this will block until the responder thread exits, which it should because it received the Drop message + if let Some(handle) = self.handle.take() { + handle.join().unwrap(); + } + } +} \ No newline at end of file diff --git a/services/shellchat/src/cmds/net_cmd.rs b/services/shellchat/src/cmds/net_cmd.rs index 82daeea3d..698b4b337 100644 --- a/services/shellchat/src/cmds/net_cmd.rs +++ b/services/shellchat/src/cmds/net_cmd.rs @@ -337,20 +337,24 @@ impl<'a> ShellCmdApi<'a> for NetCmd { let mut pkt: [u8; UDP_TEST_SIZE] = [0; UDP_TEST_SIZE]; match udp_socket.recv_from(&mut pkt) { Ok((len, addr)) => { - write!(ret, "UDP rx {} bytes: {:?}: {}\n", len, addr, std::str::from_utf8(&pkt[..len]).unwrap()).unwrap(); + write!(ret, "UDP rx {} bytes: {:?}: {}", len, addr, std::str::from_utf8(&pkt[..len]).unwrap()).unwrap(); log::info!("UDP rx {} bytes: {:?}: {:?}", len, addr, &pkt[..len]); self.udp_count += 1; - let response_addr = SocketAddr::new( - addr.ip(), - udp_socket.socket_addr().unwrap().port() - ); - match udp_socket.send_to( - format!("Received {} packets\n\r", self.udp_count).as_bytes(), - &response_addr - ) { - Ok(len) => write!(ret, "UDP tx {} bytes", len).unwrap(), - Err(_) => write!(ret, "UDP tx err").unwrap(), + if addr.ip() != IpAddr::V4(std::net::Ipv4Addr::new(127, 0, 0, 1)) { + let response_addr = SocketAddr::new( + addr.ip(), + udp_socket.socket_addr().unwrap().port() + ); + match udp_socket.send_to( + format!("Received {} packets\n\r", self.udp_count).as_bytes(), + &response_addr + ) { + Ok(len) => write!(ret, "UDP tx {} bytes", len).unwrap(), + Err(_) => write!(ret, "UDP tx err").unwrap(), + } + } else { + log::info!("localhost UDP origin detected (are you testing in hosted mode?), not reflecting packet as this would create a loop"); } }, Err(e) => { @@ -368,7 +372,7 @@ impl<'a> ShellCmdApi<'a> for NetCmd { let mut pkt: [u8; UDP_TEST_SIZE] = [0; UDP_TEST_SIZE]; match udp_socket.recv_from(&mut pkt) { Ok((len, addr)) => { - write!(ret, "Clone UDP rx {} bytes: {:?}: {}\n", len, addr, std::str::from_utf8(&pkt[..len]).unwrap()).unwrap(); + write!(ret, "Clone UDP rx {} bytes: {:?}: {}", len, addr, std::str::from_utf8(&pkt[..len]).unwrap()).unwrap(); log::info!("Clone UDP rx {} bytes: {:?}: {:?}", len, addr, &pkt[..len]); self.udp_count += 1;