diff --git a/services/net/src/api.rs b/services/net/src/api.rs index 80fcbf503..5c1f0419f 100644 --- a/services/net/src/api.rs +++ b/services/net/src/api.rs @@ -2,6 +2,7 @@ pub(crate) mod udp; pub(crate) use udp::*; pub(crate) mod ping; pub(crate) use ping::*; +pub use ping::NetPingCallback; use rkyv::{Archive, Deserialize, Serialize}; use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr}; @@ -11,6 +12,9 @@ use std::fmt; use std::fmt::Debug; use std::io::Write; +// republish this so we can decode the icmpv4 error codes +pub use smoltcp::wire::Icmpv4DstUnreachable; + pub(crate) const SERVER_NAME_NET: &str = "_Middleware Network Server_"; /// Dispatch opcodes to the Net crate main loop. @@ -33,14 +37,11 @@ pub(crate) enum Opcode { DnsUnhookAll, /// Ping stack - PingTx, - PingRegisterRx, - PingUnregisterRx, + Ping, PingSetTtl, PingGetTtl, - - /// subscription for network responses - //NetCallbackSubscribe, + PingSetTimeout, + PingGetTimeout, /// [Internal] com llio interrupt callback ComInterrupt, @@ -52,6 +53,15 @@ pub(crate) enum Opcode { Quit } +#[derive(Debug, Archive, Serialize, Deserialize, Copy, Clone)] +pub enum XousServerId { + /// A SID that is shared directly with the Net crate; a private, single-use SID for best security + PrivateSid([u32; 4]), + /// A name that needs to be looked up with XousNames. Easier to implement, but less secure as it requires + /// an open connection slot that could be abused by other processes. + ServerName(xous_ipc::String::<64>), +} + /// These opcodes are reserved for private SIDs shared from a DNS server to /// reconfigure DNS on IP change/update. #[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] diff --git a/services/net/src/api/ping.rs b/services/net/src/api/ping.rs index 4fae65150..9d9154abd 100644 --- a/services/net/src/api/ping.rs +++ b/services/net/src/api/ping.rs @@ -1,28 +1,32 @@ use rkyv::{Archive, Deserialize, Serialize}; use crate::api::*; -// anything bigger than this just gets truncated. -// do we really care to respond to naughty hosts? -pub(crate) const PING_MAX_PKT_LEN: usize = 256; +/// Scalar responses to pings have the following format: +/// arg1: bottom byte = NetPingCallback as below; top byte = DstUnreachable code as u8 +/// arg2: remote IP address hint (IPv4 is full address; IPv6 is just bottom 4 bytes) +/// arg3: sequence number (if echo response) or top 4 bytes of an IPv6 address (if reporting timeout or drop) +/// arg4: elapsed time +#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] +pub enum NetPingCallback { + /// echo response + NoErr, + /// timeout on a sequence number + Timeout, + /// dest unreachable + Unreachable, + /// drop the responding server, if it was spawned specifically for this use + Drop, +} //////// Intra-crate Ping structures #[derive(Debug, Archive, Serialize, Deserialize, Copy, Clone)] pub(crate) struct NetPingPacket { - pub len: u32, - pub data: [u8; PING_MAX_PKT_LEN], + /// the address we are pinging pub endpoint: NetIpAddr, -} - -#[derive(Debug, Archive, Serialize, Deserialize, Copy, Clone)] -pub(crate) struct NetPingResponsePacket { - pub len: u32, - pub data: [u8; PING_MAX_PKT_LEN], -} - -#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] -pub(crate) enum NetPingCallback { - RxData, - SrcAddr, - CheckTimeout, - Drop, + /// the server for our callback when the pong arrives + pub server: XousServerId, + /// the opcode ID for the callback + pub return_opcode: usize, + /// a response from the Net crate informing if the send was successful + pub sent_ok: Option, } diff --git a/services/net/src/backend/ping.rs b/services/net/src/backend/ping.rs index fe010b5fb..b76f6978e 100644 --- a/services/net/src/backend/ping.rs +++ b/services/net/src/backend/ping.rs @@ -1,302 +1,251 @@ -use std::convert::TryInto; use std::net::IpAddr; -use std::unimplemented; -use std::sync::{Arc, Mutex}; +use std::sync::Arc; use std::thread; use std::thread::JoinHandle; -use std::collections::HashMap; -use core::sync::atomic::{AtomicBool, Ordering}; +use core::sync::atomic::{AtomicBool, AtomicU32, Ordering}; -use byteorder::{ByteOrder, NetworkEndian}; -use smoltcp::phy::ChecksumCapabilities; -use smoltcp::wire::{ - Icmpv4Packet, Icmpv4Repr, IpAddress, - Icmpv6Packet, Icmpv6Repr, Ipv6Address -}; - -use xous::{CID, Message, SID, msg_blocking_scalar_unpack, send_message}; +use xous::{Message, msg_scalar_unpack, send_message}; use xous_ipc::Buffer; use crate::NetConn; use crate::api::*; use num_traits::*; ///////// Ping implementation -/// Ping will accept an IP Address (IPv4 or IPv6) and attempt to ping -/// the remote server. It will manage its own sequence number. The -/// starting number is picked at random for each instance, which means -/// if you have multiple pings running at once there is a chance of -/// sequence collisions, but this is considered an edge case. +/// smoltcp's ICMP stack is a little weird. It's designed with an icmp packet +/// formatter, but it's built into the socket handle itself. The reasoning behind +/// this was to keep memory allocations small and static. /// -/// Ping responses are meant to trigger a callback message which contains -/// the ping time and remote host; however, if None is specified for the -/// ping responder, the process simply blocks until a response happens or -/// a timeout is met. +/// In a single-threaded implemnetation, one would normally have no problem simply +/// pulling up the socket handle and jamming or unpacking data using that handle. +/// Unfortunately, in Xous, we want to keep smoltcp inside the Net crate, and not +/// have it bleed into other parts of the OS. The first take at implementing this +/// thus ended up having to duplicate the storage for ICMP into the "lib" side code, +/// and using the smoltcp convenience functions to create and format packets. /// -/// The default timeout is 10 seconds. -const PING_DEFAULT_TIMEOUT_MS: u64 = 10_000; -const PING_POLL_INTERVAL_MS: usize = 1_000; +/// This ugly division of labor leads to an unweildy handler prone to locking +/// and object lifetime issues. There's two options on how to work our way out of this. +/// 1. One is to re-implement ICMP packet packing and unpacking inside Xous, so that +/// smoltcp is just a raw ICMP packet pusher. +/// 2. Make the Xous Net interface to ICMP specific to "ping", pushing sequence numbers +/// and origin timestamps into the Net crate, and getting back sequence numbers with +/// the corresponding timestamps. +/// +/// The downside of (1) is duplication of effort. ICMP is fairly straightforward for +/// IPv4, but unfortunately for IPv6 it requires knowledge of the source address +/// which puts a burden on coordinating DHCP updates into the implementation. +/// The downside of (2) is it makes things a little awkward later on if someone +/// wants to implement fancy ICMP protocol hacks in the Net stack. +/// +/// However, it looks like there is a clean place to hook a traceroute implementation +/// into the smoltcp stack, as it has a parser for TimeExceeded packts built in, +/// and we could provide a hook for that, and perhaps a Traceroute utility that +/// exists in parallel to the Ping implementation on the "lib" side of the Net crate. +/// +/// Thus for this refactor, we are going to push more function of ICMP parsing and +/// formatting into the "main" side of the Net crate and thus into the ICMP socket +/// implementation, and leave the client side a bit more naive & high-level, functioning +/// closer to a full utility. This is unlike the UDP and TCP implementations, which +/// are ignorant of what's being done with them. + pub struct Ping { net: NetConn, - cb_sid: SID, - self_cid: CID, - handle: Option>, - timer: ticktimer_server::Ticktimer, - timeout: Arc>, - notify: Arc>, - waiting_queue: Arc>>, - seq_no: u16, - buf: [u8; PING_MAX_PKT_LEN], - checksum_caps: Arc>, - src_addr: Arc>>, - remote_addr: Arc>>, - one_shot_active: Arc, + callback_server: Option, + dispatch_opcode: Option, } impl Ping { - /// Ping is fully asynchronous. Responses need to be hooked with a callback. - pub fn new() -> Ping { + /// The caller must guarantee that the `callback_server` -- whatever it is -- outlives the duration + /// of any Ping return callbacks. These callbacks can even outlive the scope of this Ping + /// object. + /// + /// The server can know all the callbacks are done when it + /// receives the `NetPingCallback::Drop` sub-opcode from the Net server. + /// `dispatch_opcode` is a number that is inserted in the Scalar message ID field to help + /// the server route the message to the correct bit of code. This is typically an Opcode + /// from the master dispatch enum for your server, or if it is a single-use server, the + /// field could be ignored. + pub fn non_blocking_handle(callback_server: XousServerId, dispatch_opcode: usize) -> Ping { let xns = xous_names::XousNames::new().unwrap(); let net = NetConn::new(&xns).unwrap(); - let cb_sid = xous::create_server().unwrap(); - let trng = trng::Trng::new(&xns).unwrap(); - - let waiting_queue = Arc::new(Mutex::new(HashMap::new())); - let notify = Arc::new(Mutex::new(XousScalarEndpoint::new())); - let src_addr = Arc::new(Mutex::new(None)); - let remote_addr = Arc::new(Mutex::new(None)); - let checksum_caps = Arc::new(Mutex::new(ChecksumCapabilities::default())); // defaults to "Both" - let timeout = Arc::new(Mutex::new(PING_DEFAULT_TIMEOUT_MS)); - let one_shot_active = Arc::new(AtomicBool::new(false)); - let self_cid = xous::connect(cb_sid).unwrap(); - let handle = thread::spawn({ - let cb_sid_clone = cb_sid.clone(); - let waiting_queue = Arc::clone(&waiting_queue); - let notify = Arc::clone(¬ify); - let src_addr = Arc::clone(&src_addr); - let checksum_caps = Arc::clone(&checksum_caps); - let remote_addr = Arc::clone(&remote_addr); - let timeout = Arc::clone(&timeout); - let one_shot_active = Arc::clone(&one_shot_active); - let self_cid = self_cid.clone(); - move || { - loop { - let tt = ticktimer_server::Ticktimer::new().unwrap(); - let msg = xous::receive_message(cb_sid_clone).unwrap(); - match FromPrimitive::from_usize(msg.body.id()) { - Some(NetPingCallback::RxData) => { - let buffer = unsafe {Buffer::from_memory_message(msg.body.memory_message().unwrap())}; - let incoming = buffer.to_original::().unwrap(); - let payload = &incoming.data[..incoming.len as usize]; - log::trace!("pinger got {:?}", payload); - let remote_addr_clone = match *remote_addr.lock().unwrap() { - Some(ra) => ra, - None => { - log::warn!("remote address was not set, yet somehow a remote has responded!"); - continue - }, - }; - match remote_addr_clone { - IpAddress::Ipv4(_) => { - log::trace!("decode ipv4 ping"); - let icmp_packet = Icmpv4Packet::new_checked(&payload).unwrap(); - log::trace!("icmp_packet: {:?}", icmp_packet); - let icmp_repr = - Icmpv4Repr::parse(&icmp_packet, &checksum_caps.lock().unwrap()).unwrap(); - log::trace!("icmp_repr: {:?}", icmp_repr); - if let Icmpv4Repr::EchoReply{ - seq_no, - data, - .. - } = icmp_repr { - log::trace!("ping back of seq {}", seq_no); - let seq_valid = waiting_queue.lock().unwrap().contains_key(&seq_no); - // bind to seq_valid so lock is droppped, alowing lock to be got again inside the if below - if seq_valid { - let packet_timestamp_ms = NetworkEndian::read_u64(data); - waiting_queue.lock().unwrap().remove(&seq_no); - log::trace!("sending notification of seq {}", seq_no); - notify.lock().unwrap().notify_custom_args([ - None, // replaced with discriminant setup in the callback - Some((tt.elapsed_ms() - packet_timestamp_ms) as u32), - Some(u32::from_be_bytes(remote_addr_clone.as_bytes().try_into().unwrap())), - None, - ]); - } else { - log::warn!("pong seq {} not found!", seq_no); - } - } else { - log::warn!("packet response did not match icmp template"); - } - } - IpAddress::Ipv6(_) => { - if let Some(src_addr) = *src_addr.lock().unwrap() { - let src_ipv6 = match src_addr { - // not sure if this a valid way to convert v4->v6, but "meh" - IpAddr::V4(ipv4) => { - let octets = ipv4.octets(); - IpAddress::Ipv6(Ipv6Address::new( - 0, 0, 0, 0, 0, - 0xffff, - u16::from_be_bytes([octets[0], octets[1]]), - u16::from_be_bytes([octets[2], octets[3]]) - )) - } - IpAddr::V6(ipv6) => IpAddress::from(ipv6) - }; - let icmp_packet = Icmpv6Packet::new_checked(&payload).unwrap(); - let icmp_repr = Icmpv6Repr::parse( - &remote_addr.lock().unwrap().unwrap(), - &src_ipv6, - &icmp_packet, - &checksum_caps.lock().unwrap(), - ).unwrap(); - - if let Icmpv6Repr::EchoReply{ - seq_no, - data, - .. - } = icmp_repr { - let seq_valid = waiting_queue.lock().unwrap().contains_key(&seq_no); - // bind to seq_valid so lock is droppped, alowing lock to be got again inside the if below - if seq_valid { - let packet_timestamp_ms = NetworkEndian::read_u64(data); - waiting_queue.lock().unwrap().remove(&seq_no); - let octet_slice = remote_addr_clone.as_bytes(); - let mut octets: [u8; 16] = [0; 16]; - for (&src, dst) in octet_slice.iter().zip(octets.iter_mut()) { - *dst = src; - } - notify.lock().unwrap().notify_custom_args([ - None, // replaced with discriminant - Some((tt.elapsed_ms() - packet_timestamp_ms) as u32), // drop the first 4 octets and replace with response time... - Some(u32::from_be_bytes(octets[8..12].try_into().unwrap())), - Some(u32::from_be_bytes(octets[12..16].try_into().unwrap())), - ]); - } - } - } else { - log::error!("Got IPV6 response, but our source address wasn't set.") - } - } - _ => { - log::warn!("Internal error on formatting of remote address. Memory corruption?"); - } - } - log::info!("managing wait queue"); - let timestamp = tt.elapsed_ms(); - waiting_queue.lock().unwrap().retain(|seq, from| { - if timestamp - *from < *timeout.lock().unwrap() { - log::trace!("sequence {} still pending", seq); - true - } else { - log::trace!("expiring sequence {}", seq); - // TODO: fix IPV6 case -- need to pack out partial args of the address or something... - notify.lock().unwrap().notify_custom_args([ - None, // replaced with discriminant setup in the callback - None, // indicates failure -> resolves to 0 time - Some(u32::from_be_bytes(remote_addr.lock().unwrap().unwrap().as_bytes().try_into().unwrap())), - None, - ]); - false - } - }); - }, - Some(NetPingCallback::CheckTimeout) => { - let timestamp = tt.elapsed_ms(); - waiting_queue.lock().unwrap().retain(|_seq, from| { - if timestamp - *from < *timeout.lock().unwrap() { - true - } else { - // TODO: fix IPV6 case -- need to pack out partial args of the address or something... - notify.lock().unwrap().notify_custom_args([ - None, // replaced with discriminant setup in the callback - None, // indicates failure -> resolves to 0 time - Some(u32::from_be_bytes(remote_addr.lock().unwrap().unwrap().as_bytes().try_into().unwrap())), - None, - ]); - false - } - }); - if waiting_queue.lock().unwrap().len() > 0 { - if !one_shot_active.swap(true, Ordering::SeqCst) { - log::trace!("spawing one-shot"); - thread::spawn({ - let self_cid = self_cid.clone(); - let one_shot_active = one_shot_active.clone(); - move || { - let tt = ticktimer_server::Ticktimer::new().unwrap(); - tt.sleep_ms(PING_POLL_INTERVAL_MS).unwrap(); - one_shot_active.store(false, Ordering::SeqCst); - match xous::send_message(self_cid, - Message::new_scalar(NetPingCallback::CheckTimeout.to_usize().unwrap(), 0, 0, 0, 0) - ) { - Ok(_) => {}, - Err(xous::Error::ServerNotFound) => { - log::warn!("Responder went out of scope for timeout poll. Maybe the lifetime of your Ping object was too short?"); - } - _ => panic!("unhandled error in sending ping poll") - } - } - }); - log::trace!("one-shot spawned"); - } - } - } - Some(NetPingCallback::SrcAddr) => { - // source address is required for ipv6 icmp packet generation - let buffer = unsafe {Buffer::from_memory_message(msg.body.memory_message().unwrap())}; - let incoming = buffer.to_original::().unwrap(); - *src_addr.lock().unwrap() = Some(IpAddr::from(incoming)); - } - Some(NetPingCallback::Drop) => msg_blocking_scalar_unpack!(msg, _, _, _, _, { - log::debug!("Drop received, exiting ping handler"); - xous::return_scalar(msg.sender, 1).unwrap(); // actual return value doesn't matter -- it's that there is a return value - break; - }), - None => { - log::error!("got unknown message type on Ping callback: {:?}", msg); - } - } - } - } - }); - - let sid = cb_sid.to_array(); - send_message( - net.conn(), - Message::new_scalar(Opcode::PingRegisterRx.to_usize().unwrap(), - sid[0] as _, sid[1] as _, sid[2] as _,sid[3] as _, - ) - ).expect("couldn't register Ping listener"); - Ping { net, - cb_sid, - self_cid, - handle: Some(handle), - timer: ticktimer_server::Ticktimer::new().unwrap(), - timeout, - notify, - waiting_queue, - seq_no: trng.get_u32().unwrap() as u16, - buf: [0; PING_MAX_PKT_LEN], - checksum_caps, - src_addr, - remote_addr, - one_shot_active, + callback_server: Some(callback_server), + dispatch_opcode: Some(dispatch_opcode), + } + } + /// This is a handle just for manipulating settings on the ping socket. You would create + /// one of these if you wanted to adjust the ping settings before issuing a `blocking` ping, + /// which is incapable of manipulating the Net crate state. + pub fn settings_handle() -> Ping { + let xns = xous_names::XousNames::new().unwrap(); + let net = NetConn::new(&xns).unwrap(); + Ping { net, callback_server: None, dispatch_opcode: None } + } + + /// Send a single ping immediately, with the result going to an asynchronous callback + pub fn ping(&self, remote: IpAddr) -> bool { + if let Some(cbs) = self.callback_server { + if let Some(dispatch_op) = self.dispatch_opcode { + let ping = NetPingPacket { + endpoint: NetIpAddr::from(remote), + server: cbs, + return_opcode: dispatch_op, + sent_ok: None, + }; + let mut buf = Buffer::into_buf(ping).expect("couldn't allocate memory to send Ping"); + buf.lend_mut(self.net.conn(), Opcode::Ping.to_u32().unwrap()).expect("couldn't send Ping command"); + let ret = buf.to_original::().unwrap(); + if let Some(sent_ok) = ret.sent_ok { + sent_ok + } else { + panic!("Internal error: malformed data returned from Net while processing Ping operation"); + } + } else { + log::warn!("Ping object was not initialized for async operation."); + false + } + } else { + log::warn!("Ping object was not initialized for async operation."); + false } } - pub fn set_timeout(&mut self, timeout_ms: u64) { - *self.timeout.lock().unwrap() = timeout_ms; + + /// Send multiple pings by spawning a helper thread. Results go to the asynchronous callback + /// specified during the creation of the Ping object. + pub fn ping_spawn_thread(&self, remote: IpAddr, count: usize, delay_ms: usize) -> Option> { + if let Some(cbs) = self.callback_server { + if let Some(dispatch_op) = self.dispatch_opcode { + let handle = thread::spawn({ + let cbs = cbs.clone(); + let dispatch_op = dispatch_op.clone(); + let net_conn = self.net.conn().clone(); + move || { + let tt = ticktimer_server::Ticktimer::new().unwrap(); + let mut cur_count = 0; + while cur_count < count { + let ping = NetPingPacket { + endpoint: NetIpAddr::from(remote), + server: cbs, + return_opcode: dispatch_op, + sent_ok: None, + }; + let mut buf = Buffer::into_buf(ping).expect("couldn't allocate memory to send Ping"); + buf.lend_mut(net_conn, Opcode::Ping.to_u32().unwrap()).expect("couldn't send Ping command"); + let ret = buf.to_original::().unwrap(); + if let Some(sent_ok) = ret.sent_ok { + if !sent_ok { + log::warn!("Problem sending a Ping inside ping_spawn; ignoring error and moving on."); + } + } else { + panic!("Internal error: malformed data returned from Net while processing Ping operation"); + } + tt.sleep_ms(delay_ms).unwrap(); + cur_count += 1; + } + } + }); + Some(handle) + } else { + log::warn!("Ping object was not initialized for async operation."); + None + } + } else { + log::warn!("Ping object was not initialized for async operation."); + None + } } - pub fn get_timeout(&self) -> u64 { - *self.timeout.lock().unwrap() + + /// This sends a ping, and blocks execution until it either times out, or a ping is received. + /// It immediately destroys all of its temporary allocated connections to the Net crate upon exit, + /// so it does not return a Ping object. + pub fn blocking(remote: IpAddr) -> (bool, u32) { + let reachable = Arc::new(AtomicBool::new(false)); + let ping_time = Arc::new(AtomicU32::new(0)); + let handle = thread::spawn({ + let reachable = Arc::clone(&reachable); + let ping_time = Arc::clone(&ping_time); + move || { + let xns = xous_names::XousNames::new().unwrap(); + let net = NetConn::new(&xns).unwrap(); + let sid = xous::create_server().unwrap(); + let ping = NetPingPacket { + endpoint: NetIpAddr::from(remote), + server: XousServerId::PrivateSid(sid.to_array()), + return_opcode: 0, + sent_ok: None, + }; + let mut buf = Buffer::into_buf(ping).expect("couldn't allocate memory to send Ping"); + buf.lend_mut(net.conn(), Opcode::Ping.to_u32().unwrap()).expect("couldn't send Ping command"); + let ret = buf.to_original::().unwrap(); + if let Some(sent_ok) = ret.sent_ok { + if sent_ok { + loop { + let msg = xous::receive_message(sid).unwrap(); + // only one message type is expected back, so we don't match on ID -- just unpack the message already! + msg_scalar_unpack!(msg, op, _addr, seq_or_addr, timestamp, { + match FromPrimitive::from_usize(op & 0xFF) { + Some(NetPingCallback::Drop) => { + break; + } + Some(NetPingCallback::NoErr) => { + match remote { + IpAddr::V4(_) => { + reachable.store(true, Ordering::SeqCst); + ping_time.store(timestamp as u32, Ordering::SeqCst); + log::info!("Pong from {:?} seq {} received: {} ms", remote, seq_or_addr, timestamp); + }, + IpAddr::V6(_) => { + reachable.store(true, Ordering::SeqCst); + ping_time.store(timestamp as u32, Ordering::SeqCst); + log::info!("Pong from {:?} received: {} ms", remote, timestamp); + }, + } + } + Some(NetPingCallback::Timeout) => { + reachable.store(false, Ordering::SeqCst); + ping_time.store(timestamp as u32, Ordering::SeqCst); + log::info!("Ping to {:?} timed out", remote); + } + Some(NetPingCallback::Unreachable) => { + let code = smoltcp::wire::Icmpv4DstUnreachable::from((op >> 24) as u8); + reachable.store(false, Ordering::SeqCst); + log::info!("Ping to {:?} unreachable: {:?}", remote, code); + } + None => { + log::error!("Unknown opcode received in one-time server: {:?}", op); + } + } + }); + } + } else { + log::error!("Ping to {:?} was requested, but an internal error prevented it from being sent (maybe ICMP socket is busy?).", remote); + } + } else { + panic!("Internal error -- Ping was requested, but the returned memory was malformed"); + } + xous::destroy_server(sid).expect("couldn't destroy one-time use server"); + } + }); + handle.join().expect("couldn't join single-use server handle"); + (reachable.load(Ordering::SeqCst), ping_time.load(Ordering::SeqCst)) } - pub fn set_scalar_notification(&mut self, cid: CID, op: usize, discriminant: u32) { - self.notify.lock().unwrap().set(cid, op, [Some(discriminant as usize), None, None, None]); + + pub fn set_timeout(&mut self, timeout_ms: u32) { + send_message( + self.net.conn(), + Message::new_scalar(Opcode::PingSetTimeout.to_usize().unwrap(), timeout_ms as usize, 0, 0, 0) + ).expect("couldn't send set timeout for ping"); } - pub fn clear_scalar_notification(&mut self) { - self.notify.lock().unwrap().clear(); + pub fn get_timeout(&self) -> u32 { + let response = send_message( + self.net.conn(), + Message::new_blocking_scalar(Opcode::PingGetTimeout.to_usize().unwrap(), 0, 0, 0, 0) + ).expect("Couldn't get ping timeout"); + if let xous::Result::Scalar1(result) = response { + result as u32 + } else { + panic!("Could execute get_timeout call"); + } } pub fn get_ttl(&self) -> u8 { let response = send_message( @@ -315,145 +264,4 @@ impl Ping { Message::new_scalar(Opcode::PingSetTtl.to_usize().unwrap(), ttl as usize, 0, 0, 0) ).expect("couldn't send set TTL message for ping"); } - /// blocks until all the pings have either pong'd or timed out -- not sure if this is actually a good idea to have instead of - /// relying on the async callback in all cases? - /* - pub fn wait_pong(&self) { - loop { - let no_pongs_pending = self.waiting_queue.lock().unwrap().is_empty(); - if no_pongs_pending { - break; - } else { - self.timer.sleep_ms(PING_POLL_INTERVAL_MS / 4).unwrap(); - } - } - }*/ - pub fn ping(&mut self, remote: IpAddr) { - let ident = 0x22b; - let timestamp = self.timer.elapsed_ms(); - let mut echo_payload = [0xffu8; 40]; - NetworkEndian::write_u64(&mut echo_payload, timestamp); - let remote_addr = IpAddress::from(remote); - match remote_addr { - IpAddress::Ipv4(_) => { - log::debug!("ping sending to {:?} seq_no {}", remote_addr, self.seq_no); - let icmp_repr = Icmpv4Repr::EchoRequest { - ident, - seq_no: self.seq_no, - data: &echo_payload - }; - let mut icmp_packet = Icmpv4Packet::new_unchecked(&mut self.buf); - icmp_repr.emit(&mut icmp_packet, &self.checksum_caps.lock().unwrap()); - let icmp_inner = icmp_packet.into_inner(); - - let mut pkt = NetPingPacket { - len: icmp_inner.len() as u32, - data: [0; PING_MAX_PKT_LEN], - endpoint: NetIpAddr::from(remote), - }; - for (&src, dst) in icmp_inner.iter().zip(pkt.data.iter_mut()) { - *dst = src; - } - let buf = Buffer::into_buf(pkt).expect("couldn't allocate Buffer"); - buf.send(self.net.conn(), Opcode::PingTx.to_u32().unwrap()).expect("couldn't send Ping packet"); - } - IpAddress::Ipv6(_) => { - // this code probably does not work, as it has not been tested - if let Some(src_addr) = *self.src_addr.lock().unwrap() { - let src_ipv6 = match src_addr { - // not sure if this a valid way to convert v4->v6, but "meh" - IpAddr::V4(ipv4) => { - let octets = ipv4.octets(); - IpAddress::Ipv6(Ipv6Address::new( - 0, 0, 0, 0, 0, - 0xffff, - u16::from_be_bytes([octets[0], octets[1]]), - u16::from_be_bytes([octets[2], octets[3]]) - )) - } - IpAddr::V6(ipv6) => IpAddress::from(ipv6) - }; - let icmp_repr = Icmpv6Repr::EchoRequest { - ident, - seq_no: self.seq_no, - data: &echo_payload - }; - let mut icmp_packet = Icmpv6Packet::new_unchecked(&mut self.buf); - icmp_repr.emit( - &src_ipv6, - &remote_addr, - &mut icmp_packet, - &self.checksum_caps.lock().unwrap() - ); - let icmp_inner = icmp_packet.into_inner(); - let mut pkt = NetPingPacket { - len: icmp_inner.len() as u32, - data: [0; PING_MAX_PKT_LEN], - endpoint: NetIpAddr::from(remote), - }; - for (&src, dst) in icmp_inner.iter().zip(pkt.data.iter_mut()) { - *dst = src; - } - let buf = Buffer::into_buf(pkt).expect("couldn't allocate Buffer"); - buf.send(self.net.conn(), Opcode::PingTx.to_u32().unwrap()).expect("couldn't send Ping packet"); - } else { - log::error!("Attempt to send ipv6 ping but no source address is available"); - } - } - _ => unimplemented!(), - } - self.waiting_queue.lock().unwrap().insert(self.seq_no, timestamp); - self.seq_no = self.seq_no.wrapping_add(1); - - // record the remote_addr so we know if we're parsing an ipv4 or ipv6 packet - *self.remote_addr.lock().unwrap() = Some(remote_addr); - log::debug!("spawning first one-shot"); - self.one_shot_active.store(true, Ordering::SeqCst); - thread::spawn({ - let self_cid = self.self_cid.clone(); - let one_shot_active = self.one_shot_active.clone(); - move || { - let tt = ticktimer_server::Ticktimer::new().unwrap(); - tt.sleep_ms(PING_POLL_INTERVAL_MS).unwrap(); - one_shot_active.store(false, Ordering::SeqCst); - match xous::try_send_message(self_cid, - Message::new_scalar(NetPingCallback::CheckTimeout.to_usize().unwrap(), 0, 0, 0, 0) - ) { - Ok(_) => {}, - Err(xous::Error::ServerNotFound) => { - log::warn!("Responder went out of scope for timeout poll. Maybe the lifetime of your Ping object was too short?"); - } - _ => panic!("unhandled error in sending ping poll") - } - } - }); - log::debug!("spawning first one-shot spawned"); - } } - -impl Drop for Ping { - fn drop(&mut self) { - - let sid = self.cb_sid.to_array(); - send_message( - self.net.conn(), - Message::new_blocking_scalar(Opcode::PingUnregisterRx.to_usize().unwrap(), - sid[0] as _, sid[1] as _, sid[2] as _,sid[3] as _, - ) - ).expect("couldn't unregister Ping listener"); - - let drop_cid = xous::connect(self.cb_sid).unwrap(); - xous::send_message( - drop_cid, - Message::new_blocking_scalar(NetPingCallback::Drop.to_usize().unwrap(), 0, 0, 0, 0) - ).expect("couldn't send Drop to our repsonding server"); - unsafe{xous::disconnect(drop_cid).unwrap()}; // should be safe because we're the only connection and the previous was a blocking scalar - - // 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(); - } - // now we can detroy the server id of the responder thread - xous::destroy_server(self.cb_sid).unwrap(); - } -} \ No newline at end of file diff --git a/services/net/src/main.rs b/services/net/src/main.rs index 18bbffc0a..88d4d1393 100644 --- a/services/net/src/main.rs +++ b/services/net/src/main.rs @@ -12,6 +12,7 @@ use xous::{send_message, Message, CID, SID, msg_scalar_unpack, msg_blocking_scal use xous_ipc::Buffer; use std::collections::BTreeMap; use std::collections::HashMap; +use std::convert::TryInto; use smoltcp::phy::{Medium, Device}; use smoltcp::iface::{InterfaceBuilder, NeighborCache, Routes, Interface}; @@ -19,12 +20,19 @@ use smoltcp::socket::{IcmpEndpoint, IcmpPacketMetadata, IcmpSocket, IcmpSocketBu use smoltcp::wire::{ EthernetAddress, IpAddress, IpCidr, Ipv4Address, Ipv4Cidr, IpEndpoint }; +use smoltcp::wire::{ + Icmpv4Packet, Icmpv4Repr, Icmpv6Packet, Icmpv6Repr, +}; +use byteorder::{ByteOrder, NetworkEndian}; + use smoltcp::socket::{UdpPacketMetadata, UdpSocket, UdpSocketBuffer, SocketHandle}; use smoltcp::time::Instant; use std::thread; use std::sync::Arc; use core::sync::atomic::{AtomicU32, Ordering}; +const PING_DEFAULT_TIMEOUT_MS: u32 = 10_000; + fn set_ipv4_addr(iface: &mut Interface<'_, DeviceT>, cidr: Ipv4Cidr) where DeviceT: for<'d> Device<'d>, @@ -35,20 +43,6 @@ where }); } -struct SmoltcpTimer { - ticktimer: ticktimer_server::Ticktimer, -} -impl SmoltcpTimer { - pub fn new() -> Self { - SmoltcpTimer { - ticktimer: ticktimer_server::Ticktimer::new().unwrap(), - } - } - pub fn now(&self) -> Instant { - Instant::from_millis(self.ticktimer.elapsed_ms() as i64) - } -} - #[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] enum WaitOp { WaitMs, @@ -56,12 +50,34 @@ enum WaitOp { Quit, } +/// UdpState will return a full custom datastructure, and is designed to work with +/// a one-time use dedicated server created as part of the Net library code. pub struct UdpState { handle: SocketHandle, cid: CID, sid: SID, } +/// PingConnection can return a Scalar: because of the simplicity of the return data +/// we give implementors the option to unpack the Scalar themselves within the main loop +/// of their event handler, *or* they can create a dedicated server that handles the return +/// code. +/// +/// Unpacking the Scalar type is more efficient, but essentially requires a connection +/// to their private main loop server connection for the message to arrive, brokered via +/// xous-names. This can create a potential security concern, as the "unclaimed" connection +/// could be abused by a malicious process, which would have access to all of the dispatchable +/// opcodes of the main loop through that connection. +/// +/// Thus, for security-sensitive processes, it is recommended that those create a single-purpose +/// server ID and broker the connection through that mechanism. +#[derive(Hash, PartialEq, Eq)] +pub struct PingConnection { + remote: IpAddress, + cid: CID, + retop: usize, +} + #[xous::xous_main] fn xmain() -> ! { log_server::init_wait().unwrap(); @@ -138,13 +154,31 @@ fn xmain() -> ! { let mut sockets = SocketSet::new(vec![]); // ping storage - let mut ping_responders = HashMap::<[u32;4], CID>::new(); - let icmp_rx_buffer = IcmpSocketBuffer::new(vec![IcmpPacketMetadata::EMPTY], vec![0; 256]); - let icmp_tx_buffer = IcmpSocketBuffer::new(vec![IcmpPacketMetadata::EMPTY], vec![0; 256]); + // up to four concurrent pings in the queue + let icmp_rx_buffer = IcmpSocketBuffer::new( + vec![ + IcmpPacketMetadata::EMPTY, + IcmpPacketMetadata::EMPTY, + IcmpPacketMetadata::EMPTY, + IcmpPacketMetadata::EMPTY + ], + vec![0; 1024]); + let icmp_tx_buffer = IcmpSocketBuffer::new( + vec![ + IcmpPacketMetadata::EMPTY, + IcmpPacketMetadata::EMPTY, + IcmpPacketMetadata::EMPTY, + IcmpPacketMetadata::EMPTY + ], + vec![0; 1024]); let mut icmp_socket = IcmpSocket::new(icmp_rx_buffer, icmp_tx_buffer); let ident = 0x22b; icmp_socket.bind(IcmpEndpoint::Ident(ident)).expect("couldn't bind to icmp socket"); let icmp_handle = sockets.add(icmp_socket); + let mut seq: u16 = 0; + // this record stores the origin time + IP address of the outgoing ping sequence number + let mut ping_destinations = HashMap::>::new(); + let mut ping_timeout_ms = PING_DEFAULT_TIMEOUT_MS; // udp storage let mut udp_handles = HashMap::::new(); @@ -154,12 +188,14 @@ fn xmain() -> ! { let mut udp_clones = HashMap::>::new(); // additional clones for UDP responders // other link storage - let timer = SmoltcpTimer::new(); + let timer = ticktimer_server::Ticktimer::new().unwrap(); let neighbor_cache = NeighborCache::new(BTreeMap::new()); let ip_addrs = [IpCidr::new(Ipv4Address::UNSPECIFIED.into(), 0)]; let routes = Routes::new(BTreeMap::new()); let device = device::NetPhy::new(&xns); + // needed by ICMP to determine if we should compute checksums + let device_caps = device.capabilities(); let medium = device.capabilities().medium; let mut builder = InterfaceBuilder::new(device) .ip_addrs(ip_addrs) @@ -202,31 +238,87 @@ fn xmain() -> ! { } } match FromPrimitive::from_usize(msg.body.id()) { - Some(Opcode::PingTx) => { - let buf = unsafe{Buffer::from_memory_message(msg.body.memory_message().unwrap())}; - let pkt = buf.to_original::().unwrap(); + Some(Opcode::Ping) => { + let mut buf = unsafe{Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap())}; + let mut pkt = buf.to_original::().unwrap(); let mut socket = sockets.get::(icmp_handle); if socket.can_send() { - socket.send_slice( - &pkt.data[..pkt.len as usize], - IpAddress::from(pkt.endpoint) - ).expect("couldn't send ICMP"); + let remote = IpAddress::from(pkt.endpoint); + // we take advantage of the fact that the same CID is always returned for repeated connect requests to the same SID. + let cid = match pkt.server { + XousServerId::PrivateSid(sid) => match xous::connect(SID::from_array(sid)) { + Ok(cid) => cid, + Err(e) => { + log::error!("Ping request with single-use callback SID is invalid. Aborting request. {:?}",e); + continue; + } + } + XousServerId::ServerName(name) => match xns.request_connection(name.to_str()) { + Ok(cid) => cid, + Err(e) => { + log::error!("Ping request received, but callback name '{}' is invalid. Aborting request. {:?}", name, e); + continue; + } + } + }; + // this structure can be a HashMap key because it "should" be invariant across well-formed ping requests + let conn = PingConnection { + remote, + cid, + retop: pkt.return_opcode, + }; + // this code will guarantee the sequence number goes up, but if multiple concurrent + // pings are in progress, they may not be directly in sequence. This is OK. + let now = timer.elapsed_ms(); + if let Some(queue) = ping_destinations.get_mut(&conn) { + queue.insert(seq, now); + } else { + let mut new_queue = HashMap::::new(); + new_queue.insert(seq, now); + ping_destinations.insert(conn, new_queue); + }; + seq += 1; + + // now emit the actual packet + let mut echo_payload = [0xffu8; 40]; + let ident = 0x22b; + NetworkEndian::write_i64(&mut echo_payload, now as i64); + match remote { + IpAddress::Ipv4(_) => { + let icmp_repr = Icmpv4Repr::EchoRequest { + ident, + seq_no: seq, + data: &echo_payload, + }; + let icmp_payload = socket.send(icmp_repr.buffer_len(), remote).unwrap(); + let mut icmp_packet = Icmpv4Packet::new_unchecked(icmp_payload); + icmp_repr.emit(&mut icmp_packet, &device_caps.checksum); + } + IpAddress::Ipv6(_) => { + // not sure if this is a valid thing to do, to just assign the source some number like this?? + let src_ipv6 = IpAddress::v6(0xfdaa, 0, 0, 0, 0, 0, 0, 1); + let icmp_repr = Icmpv6Repr::EchoRequest { + ident, + seq_no: seq, + data: &echo_payload, + }; + let icmp_payload = socket.send(icmp_repr.buffer_len(), remote).unwrap(); + let mut icmp_packet = Icmpv6Packet::new_unchecked(icmp_payload); + icmp_repr.emit( + &src_ipv6, + &remote, + &mut icmp_packet, + &device_caps.checksum, + ); + } + _ => unimplemented!(), + } + pkt.sent_ok = Some(true); } else { - log::warn!("Ping packet dropped, local ICMP socket not available for sending"); + pkt.sent_ok = Some(false); } + buf.replace(pkt).expect("Xous couldn't issue response to Ping request"); } - Some(Opcode::PingRegisterRx) => msg_scalar_unpack!(msg, s0, s1, s2, s3, { - let sid = [s0 as u32, s1 as u32, s2 as u32, s3 as u32]; - let cid = xous::connect(SID::from_array(sid)).expect("couldn't connect ping callback"); - ping_responders.insert(sid, cid); - }), - Some(Opcode::PingUnregisterRx) => msg_blocking_scalar_unpack!(msg, s0, s1, s2, s3, { - let sid = [s0 as u32, s1 as u32, s2 as u32, s3 as u32]; - if ping_responders.remove(&sid).is_none() { - log::warn!("got an unregistration request for a server that wasn't registered: {:?}", sid); - }; - xous::return_scalar(msg.sender, 1).unwrap(); - }), Some(Opcode::PingSetTtl) => msg_scalar_unpack!(msg, ttl, _, _, _, { let checked_ttl = if ttl > 255 { 255 as u8 @@ -245,6 +337,12 @@ fn xmain() -> ! { }; xous::return_scalar(msg.sender, checked_ttl as usize).unwrap(); }), + Some(Opcode::PingSetTimeout) => msg_scalar_unpack!(msg, to, _, _, _, { + ping_timeout_ms = to as u32; + }), + Some(Opcode::PingGetTimeout) => msg_blocking_scalar_unpack!(msg, _, _, _, _, { + xous::return_scalar(msg.sender, ping_timeout_ms as usize).unwrap(); + }), Some(Opcode::DnsHookAddIpv4) => { let mut buf = unsafe{Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap())}; let hook = buf.to_original::().unwrap(); @@ -566,7 +664,7 @@ fn xmain() -> ! { com.ints_ack(&com_int_list); } Some(Opcode::NetPump) => { - let timestamp = timer.now(); + let timestamp = Instant::from_millis(timer.elapsed_ms() as i64); match iface.poll(&mut sockets, timestamp) { Ok(_) => { } Err(e) => { @@ -574,6 +672,7 @@ fn xmain() -> ! { } } + // this block handles UDP { for (port, udpstate) in udp_handles.iter() { let handle = udpstate.handle; @@ -613,7 +712,7 @@ fn xmain() -> ! { } } - // this enclosure contains the ICMP handler. Tx is initiated by an incoming message to the Net crate. + // this block contains the ICMP Rx handler. Tx is initiated by an incoming message to the Net crate. { let mut socket = sockets.get::(icmp_handle); if !socket.is_open() { @@ -623,28 +722,189 @@ fn xmain() -> ! { if socket.can_recv() { let (payload, _) = socket.recv().expect("couldn't receive on socket despite asserting availability"); log::trace!("icmp payload: {:x?}", payload); - let mut response = NetPingResponsePacket { - len: payload.len() as u32, - data: [0; PING_MAX_PKT_LEN], - }; - for (&src, dst) in payload.iter().zip(response.data.iter_mut()) { - *dst = src; - } - for (_, responder_cid) in ping_responders.iter() { - let buf = Buffer::into_buf(response).expect("couldn't alloc send buffer"); - match buf.send(*responder_cid, NetPingCallback::RxData.to_u32().unwrap()) { - Ok(_) => {}, - Err(xous::Error::ServerNotFound) => { - log::warn!("Ping responder went out of scope. Maybe the lifetime of the originator was not long enough?"); + let now = timer.elapsed_ms(); + + for (connection, waiting_queue) in ping_destinations.iter_mut() { + let remote_addr = connection.remote; + match remote_addr { + IpAddress::Ipv4(_) => { + let icmp_packet = Icmpv4Packet::new_checked(&payload).unwrap(); + let icmp_repr = + Icmpv4Repr::parse(&icmp_packet, &device_caps.checksum).unwrap(); + if let Icmpv4Repr::EchoReply { seq_no, data, .. } = icmp_repr { + if let Some(_) = waiting_queue.get(&seq_no) { + let packet_timestamp_ms = NetworkEndian::read_i64(data); + waiting_queue.remove(&seq_no); + // use try_send_message because we don't want to block if the recipient's queue is full; + // instead, the message is just dropped + match xous::try_send_message(connection.cid, + Message::new_scalar( + connection.retop, + NetPingCallback::NoErr.to_usize().unwrap(), + u32::from_be_bytes(remote_addr.as_bytes().try_into().unwrap()) as usize, + seq_no as usize, + (now as i64 - packet_timestamp_ms) as usize, + ) + ) { + Ok(_) => {}, + Err(xous::Error::ServerQueueFull) => { + log::warn!("Got seq {} response, but upstream server queue is full; dropping.", &seq_no); + }, + Err(e) => { + log::error!("Unhandled error: {:?}; ignoring", e); + } + } + } + } else if let Icmpv4Repr::DstUnreachable { reason, header, .. } = icmp_repr { + log::warn!("Got dst unreachable {:?}: {:?}", header.dst_addr, reason); + let reason_code: u8 = From::from(reason); + match xous::try_send_message(connection.cid, + Message::new_scalar( + connection.retop, + NetPingCallback::Unreachable.to_usize().unwrap() | (reason_code as usize) << 24, + u32::from_be_bytes(remote_addr.as_bytes().try_into().unwrap()) as usize, + 0, + 0, + ) + ) { + Ok(_) => {}, + Err(xous::Error::ServerQueueFull) => { + log::warn!("Got dst {:?} unreachable, but upstream server queue is full; dropping.", remote_addr); + }, + Err(e) => { + log::error!("Unhandled error: {:?}; ignoring", e); + } + } + } else { + log::error!("got unhandled ICMP type, ignoring!"); + } } - _ => panic!("unhandled error in sending ping response") + + IpAddress::Ipv6(_) => { + // NOTE: actually not sure what src_ipv6 should be. This is just from an example. + let src_ipv6 = IpAddress::v6(0xfdaa, 0, 0, 0, 0, 0, 0, 1); + let icmp_packet = Icmpv6Packet::new_checked(&payload).unwrap(); + let icmp_repr = Icmpv6Repr::parse( + &remote_addr, + &src_ipv6, + &icmp_packet, + &device_caps.checksum, + ) + .unwrap(); + let ra = remote_addr.as_bytes(); + if let Icmpv6Repr::EchoReply { seq_no, data, .. } = icmp_repr { + if let Some(_) = waiting_queue.get(&seq_no) { + let packet_timestamp_ms = NetworkEndian::read_i64(data); + waiting_queue.remove(&seq_no); + match xous::try_send_message(connection.cid, + Message::new_scalar( + connection.retop, + NetPingCallback::NoErr.to_usize().unwrap(), + u32::from_be_bytes(ra[..4].try_into().unwrap()) as usize, + u32::from_be_bytes(ra[12..].try_into().unwrap()) as usize, + (now as i64 - packet_timestamp_ms) as usize, + ) + ) { + Ok(_) => {}, + Err(xous::Error::ServerQueueFull) => { + log::warn!("Got seq {} response, but upstream server queue is full; dropping.", &seq_no); + }, + Err(e) => { + log::error!("Unhandled error: {:?}; ignoring", e); + } + } + } + } else if let Icmpv6Repr::DstUnreachable { reason, header, .. } = icmp_repr { + let reason_code: u8 = From::from(reason); + log::warn!("Got dst unreachable {:?}: {:?}", header.dst_addr, reason); + match xous::try_send_message(connection.cid, + Message::new_scalar( + connection.retop, + NetPingCallback::Unreachable.to_usize().unwrap() | (reason_code as usize) << 24, + u32::from_be_bytes(ra[..4].try_into().unwrap()) as usize, + u32::from_be_bytes(ra[8..12].try_into().unwrap()) as usize, + u32::from_be_bytes(ra[12..].try_into().unwrap()) as usize, + ) + ){ + Ok(_) => {}, + Err(xous::Error::ServerQueueFull) => { + log::warn!("Got dst {:?} unreachable, but upstream server queue is full; dropping.", remote_addr); + }, + Err(e) => { + log::error!("Unhandled error: {:?}; ignoring", e); + } + } + } else { + log::error!("got unhandled ICMP type, ignoring!"); + } + } + _ => unimplemented!(), } } + + // notify the callback to drop its connection, because the queue is now empty + // do this before we clear the queue, because we want the Drop message to come on the iteration + // *after* the queue is empty. + ping_destinations.retain(|conn, v| + if v.len() == 0 { + let ra = conn.remote.as_bytes(); + xous::send_message(conn.cid, + Message::new_scalar( // we should wait if the queue is full, as the "Drop" message is important + conn.retop, + NetPingCallback::Drop.to_usize().unwrap(), + u32::from_be_bytes(ra[..4].try_into().unwrap()) as usize, + if ra.len() == 16 {u32::from_be_bytes(ra[12..16].try_into().unwrap()) as usize} else {0}, + 0, + ) + ).expect("couldn't send Drop on empty queue from Ping server"); + match unsafe{xous::disconnect(conn.cid)} { + Ok(_) => {}, + Err(xous::Error::ServerNotFound) => { + log::debug!("Disconnected from a server that has already disappeared. Moving on."); + } + Err(e) => { + panic!("Unhandled error disconnecting from ping server: {:?}", e); + } + } + false + } else { + true + } + ); + + // now: sequence through the waiting_queue and remove entries that have hit our timeout + for (conn, waiting_queue) in ping_destinations.iter_mut() { + let ra = conn.remote.as_bytes(); + waiting_queue.retain(|&seq, &mut start_time| + if now - start_time > ping_timeout_ms as u64 { + match xous::try_send_message(conn.cid, + Message::new_scalar( // we should wait if the queue is full, as the "Drop" message is important + conn.retop, + NetPingCallback::Timeout.to_usize().unwrap(), + u32::from_be_bytes(ra[..4].try_into().unwrap()) as usize, + seq as usize, + (now - start_time) as usize, + ) + ) { + Ok(_) => {}, + Err(xous::Error::ServerQueueFull) => { + log::warn!("Got dst {:?} timeout, but upstream server queue is full; dropping.", conn.remote); + }, + Err(e) => { + log::error!("Unhandled error: {:?}; ignoring", e); + } + } + false + } else { + true + } + ); + } } } // establish our next check-up interval - let timestamp = timer.now(); + let timestamp = Instant::from_millis(timer.elapsed_ms() as i64); match iface.poll_at(&sockets, timestamp) { // set a future time to check the interface Some(poll_at) if timestamp < poll_at => { diff --git a/services/shellchat/src/cmds/net_cmd.rs b/services/shellchat/src/cmds/net_cmd.rs index 9a4feca29..de60d54b4 100644 --- a/services/shellchat/src/cmds/net_cmd.rs +++ b/services/shellchat/src/cmds/net_cmd.rs @@ -1,10 +1,9 @@ use crate::{ShellCmdApi, CommonEnv}; use xous_ipc::String; -use net::Duration; +use net::{Duration, XousServerId, NetPingCallback}; use xous::MessageEnvelope; use num_traits::*; use std::net::{SocketAddr, IpAddr}; -use std::thread; pub struct NetCmd { udp: Option, @@ -13,6 +12,7 @@ pub struct NetCmd { callback_conn: u32, udp_count: u32, dns: dns::Dns, + ping: Option, } impl NetCmd { pub fn new(xns: &xous_names::XousNames) -> Self { @@ -23,15 +23,15 @@ impl NetCmd { callback_conn: xns.request_connection_blocking(crate::SERVER_NAME_SHELLCHAT).unwrap(), udp_count: 0, dns: dns::Dns::new(&xns).unwrap(), + ping: None, } } } #[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] pub(crate) enum NetCmdDispatch { - UdpTest1, - UdpTest2, - Ping, + UdpTest1 = 0x1_0000, // we're muxing our own dispatch + ping dispatch, so we need a custom discriminant + UdpTest2 = 0x1_0001, } pub const UDP_TEST_SIZE: usize = 64; @@ -119,32 +119,36 @@ impl<'a> ShellCmdApi<'a> for NetCmd { match self.dns.lookup(name) { Ok(ipaddr) => { log::debug!("sending ping to {:?}", ipaddr); - let count = if let Some(count_str) = tokens.next() { - count_str.parse::().unwrap() - } else { - 1 - }; - thread::spawn({ - let count = count.clone(); - let cb_conn = self.callback_conn.clone(); - let cb_id = self.callback_id.unwrap() as usize; - move || { - let tt = ticktimer_server::Ticktimer::new().unwrap(); - let mut pinger = net::Ping::new(); - pinger.set_scalar_notification( - cb_conn, - cb_id, - NetCmdDispatch::Ping.to_u32().unwrap() + if self.ping.is_none() { + self.ping = Some(net::Ping::non_blocking_handle( + XousServerId::ServerName(xous_ipc::String::from_str(crate::SERVER_NAME_SHELLCHAT)), + self.callback_id.unwrap() as usize, + )); + } + if let Some(count_str) = tokens.next() { + let count = count_str.parse::().unwrap(); + if let Some(pinger) = &self.ping { + pinger.ping_spawn_thread( + IpAddr::from(ipaddr), + count as usize, + 1000 ); - for i in 0..count { - log::info!("ping {} of {}", i + 1, count); - pinger.ping(IpAddr::from(ipaddr)); - tt.sleep_ms(1000).unwrap(); - } - // pinger.wait_pong(); + write!(ret, "Sending {} pings to {} ({:?})", count, name, ipaddr).unwrap(); + } else { + // this just shouldn't happen based on the structure of the code above. + write!(ret, "Can't ping, internal error.").unwrap(); } - }); - write!(ret, "Sending {} pings to {} ({:?})", count, name, ipaddr).unwrap(); + } else { + if let Some(pinger) = &self.ping { + if pinger.ping(IpAddr::from(ipaddr)) { + write!(ret, "Sending a ping to {} ({:?})", name, ipaddr).unwrap(); + } else { + write!(ret, "Couldn't send a ping to {}, maybe socket is busy?", name).unwrap(); + } + } else { + write!(ret, "Can't ping, internal error.").unwrap(); + } + }; } Err(e) => { write!(ret, "Can't ping, DNS lookup error: {:?}", e).unwrap(); @@ -171,7 +175,8 @@ impl<'a> ShellCmdApi<'a> for NetCmd { log::debug!("net callback"); let mut ret = String::<1024>::new(); - xous::msg_scalar_unpack!(msg, dispatch, response_time, remote, _, { + xous::msg_scalar_unpack!(msg, arg1, arg2, arg3, arg4, { + let dispatch = arg1; match FromPrimitive::from_usize(dispatch) { Some(NetCmdDispatch::UdpTest1) => { if let Some(udp_socket) = &mut self.udp { @@ -235,22 +240,42 @@ impl<'a> ShellCmdApi<'a> for NetCmd { write!(ret, "Got NetCmd callback from uninitialized socket").unwrap(); } }, - Some(NetCmdDispatch::Ping) => { - log::info!("pong {:?} t={}ms", IpAddr::from((remote as u32).to_be_bytes()), response_time); - // this only works for ipv4, but ping is ipv6 compatible and could send you an ipv6 response. oh well. - if response_time > 0 { - write!(ret, "Pong from {:?} time={}ms", - IpAddr::from((remote as u32).to_be_bytes()), - response_time - ).unwrap(); - } else { - write!(ret, "Ping timeout {:?}", IpAddr::from((remote as u32).to_be_bytes())).unwrap(); - } - } None => { - log::error!("NetCmd callback with unrecognized dispatch ID"); - write!(ret, "NetCmd callback with unrecognized dispatch ID").unwrap(); - } + // rebind the scalar args to the Ping convention + let op = arg1; + let addr = IpAddr::from((arg2 as u32).to_be_bytes()); + let seq_or_addr = arg3; + let timestamp = arg4; + match FromPrimitive::from_usize(op & 0xFF) { + Some(NetPingCallback::Drop) => { + write!(ret, "Info: All pending pings done").unwrap(); + } + Some(NetPingCallback::NoErr) => { + match addr { + IpAddr::V4(_) => { + write!(ret, "Pong from {:?} seq {} received: {} ms", + addr, + seq_or_addr, + timestamp).unwrap(); + }, + IpAddr::V6(_) => { + write!(ret, "Ipv6 pong received: {} ms", timestamp).unwrap(); + }, + } + } + Some(NetPingCallback::Timeout) => { + write!(ret, "Ping to {:?} timed out", addr).unwrap(); + } + Some(NetPingCallback::Unreachable) => { + let code = net::Icmpv4DstUnreachable::from((op >> 24) as u8); + write!(ret, "Ping to {:?} unreachable: {:?}", addr, code).unwrap(); + } + None => { + log::error!("Unknown opcode received in NetCmd callback: {:?}", op); + write!(ret, "Unknown opcode received in NetCmd callback: {:?}", op).unwrap(); + } + } + }, } }); Ok(Some(ret))