From b93109cad26f9ed3a249af19d132f29b05024a06 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 11 Oct 2021 05:21:01 +0800 Subject: [PATCH] cleaning up net event loop, still figuring out APIs and integration currently there is some issue with ARPs not sure where it's breaking, suspect issue on EC side... --- services/com/src/main.rs | 4 +- services/llio/Cargo.toml | 3 +- services/llio/src/main.rs | 5 + services/net/src/api.rs | 4 +- services/net/src/device.rs | 32 ++--- services/net/src/main.rs | 273 ++++++++++++++++++++++++------------- 6 files changed, 200 insertions(+), 121 deletions(-) diff --git a/services/com/src/main.rs b/services/com/src/main.rs index bddcebf23..24e2ec4d7 100644 --- a/services/com/src/main.rs +++ b/services/com/src/main.rs @@ -773,6 +773,7 @@ fn xmain() -> ! { retbuf[word_index * 2] = be_bytes[0]; retbuf[word_index * 2 + 1] = be_bytes[1]; } + log::info!("rx: {:?}", &retbuf[..len_bytes as usize]); buffer.replace(retbuf).expect("couldn't return packet"); } Some(Opcode::WlanSendPacket) => { @@ -789,6 +790,7 @@ fn xmain() -> ! { log::error!("invalid packet send length: {}, aborting without send", len_bytes); continue; } + log::info!("tx: {:?}", &txbuf[2..len_bytes as usize + 2]); com.txrx(ComState::NET_FRAME_SEND_0.verb | len_bytes); for word_index in 0..len_words as usize { let be_bytes: [u8; 2] = [txbuf[2 + word_index * 2], txbuf[2 + word_index * 2 + 1]]; @@ -818,7 +820,7 @@ fn xmain() -> ! { let maybe_vector = com.try_wait_txrx(ComState::LINK_READ.verb, 500); // longer timeout because interrupts tend to be busy times let rxlen = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT)); if let Some(vector) = maybe_vector { - log::info!("vector: 0x{:x}, len: {}", vector, rxlen); + log::debug!("vector: 0x{:x}, len: {}", vector, rxlen); xous::return_scalar2(msg.sender, vector as _, rxlen as _) .expect("couldn't return IntFetchVector"); } else { diff --git a/services/llio/Cargo.toml b/services/llio/Cargo.toml index 7818d6c80..5e422485f 100644 --- a/services/llio/Cargo.toml +++ b/services/llio/Cargo.toml @@ -25,5 +25,6 @@ utralib = {path = "../../utralib"} [features] debugprint = [] -default = [] # "debugprint" +wfi_off = [] # useful for serial port debugging, forces power on so the UART characters can finish printing +default = ["wfi_off"] # "debugprint" #default = ["debugprint"] # , "fccagent" diff --git a/services/llio/src/main.rs b/services/llio/src/main.rs index f90eb4efd..968a01907 100644 --- a/services/llio/src/main.rs +++ b/services/llio/src/main.rs @@ -712,6 +712,11 @@ fn xmain() -> ! { let mut llio = Llio::new(handler_conn, gpio_base); llio.ec_power_on(); // ensure this is set correctly; if we're on, we always want the EC on. + if cfg!(feature = "wfi_off") { + log::warn!("WFI is overridden at boot -- automatic power savings is OFF!"); + llio.wfi_override(true); + } + // register a suspend/resume listener let sr_cid = xous::connect(llio_sid).expect("couldn't create suspend callback connection"); let mut susres = susres::Susres::new(&xns, Opcode::SuspendResume as u32, sr_cid).expect("couldn't create suspend/resume object"); diff --git a/services/net/src/api.rs b/services/net/src/api.rs index cbef775c0..34ca08ef4 100644 --- a/services/net/src/api.rs +++ b/services/net/src/api.rs @@ -1,11 +1,13 @@ pub(crate) const SERVER_NAME_NET: &str = "_Middleware Network Server_"; -#[allow(dead_code)] #[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] pub(crate) enum Opcode { /// com llio interrupt callback ComInterrupt, + /// run the network stack code + NetPump, + /// Suspend/resume callback SuspendResume, } diff --git a/services/net/src/device.rs b/services/net/src/device.rs index 3a0f68e5d..9fe00d56c 100644 --- a/services/net/src/device.rs +++ b/services/net/src/device.rs @@ -41,8 +41,14 @@ impl<'a> phy::Device<'a> for NetPhy { type TxToken = NetPhyTxToken<'a>; fn receive(&'a mut self) -> Option<(Self::RxToken, Self::TxToken)> { - Some((NetPhyRxToken{buf: &mut self.rx_buffer[..], com: & self.com, rx_avail: self.rx_avail.take()}, - NetPhyTxToken{buf: &mut self.tx_buffer[..], com: & self.com})) + 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})) + } else { + None + } } fn transmit(&'a mut self) -> Option { @@ -60,30 +66,14 @@ impl<'a> phy::Device<'a> for NetPhy { pub struct NetPhyRxToken<'a> { buf: &'a mut [u8], - com: &'a Com, - rx_avail: Option, } impl<'a, 'c> phy::RxToken for NetPhyRxToken<'a> { fn consume(mut self, _timestamp: Instant, f: F) -> Result where F: FnOnce(&mut [u8]) -> Result { - if let Some(rx_len) = self.rx_avail.take() { - self.com.wlan_fetch_packet(&mut self.buf[..rx_len as usize]).expect("Couldn't call wlan_fetch_packet in device adapter"); - self.buf = &mut self.buf[..rx_len as usize]; - log::info!("rxbuf: {:x?}", self.buf); - } - // else, the buf is not updated -- and it would be empty, yielding no availability - let result = f(&mut self.buf); - match result { - Err(e) => { - log::info!("rx err: {:?}", e); - } - _ => { - log::info!("rx result: {:x?}", self.buf); - } - } + //log::info!("rx: {:x?}", self.buf); result } } @@ -98,10 +88,10 @@ impl<'a> phy::TxToken for NetPhyTxToken<'a> { where F: FnOnce(&mut [u8]) -> Result { let result = f(&mut self.buf[..len]); - log::info!("tx called {}", len); + log::info!("txlen: {}", len); if result.is_ok() { - self.com.wlan_send_packet(self.buf).map_err(|_| smoltcp::Error::Dropped)?; + self.com.wlan_send_packet(&self.buf[..len]).map_err(|_| smoltcp::Error::Dropped)?; } result } diff --git a/services/net/src/main.rs b/services/net/src/main.rs index 211f4b95d..d2305456b 100644 --- a/services/net/src/main.rs +++ b/services/net/src/main.rs @@ -4,17 +4,19 @@ mod api; use api::*; use num_traits::*; -use com::api::{Ipv4Conf, NET_MTU, ComIntSources}; +use com::api::{Ipv4Conf, ComIntSources}; mod device; use byteorder::{ByteOrder, NetworkEndian}; +use xous::msg_scalar_unpack; +use xous::{send_message, Message}; use std::cmp; use std::collections::BTreeMap; use std::collections::HashMap; use std::str::FromStr; -use smoltcp::phy::{Loopback, Medium, Device}; +use smoltcp::phy::{Medium, Device}; use smoltcp::iface::{InterfaceBuilder, NeighborCache, Routes, Interface}; use smoltcp::socket::{IcmpEndpoint, IcmpPacketMetadata, IcmpSocket, IcmpSocketBuffer, SocketSet}; use smoltcp::wire::{ @@ -23,6 +25,9 @@ use smoltcp::wire::{ use smoltcp::{ time::{Duration, Instant}, }; +use std::thread; +use std::sync::Arc; +use core::sync::atomic::{AtomicU32, Ordering}; macro_rules! send_icmp_ping { ( $repr_type:ident, $packet_type:ident, $ident:expr, $seq_no:expr, @@ -84,6 +89,13 @@ impl SmoltcpTimer { } } +#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] +enum WaitOp { + WaitMs, + PollAt, + Quit, +} + #[xous::xous_main] fn xmain() -> ! { log_server::init_wait().unwrap(); @@ -92,6 +104,7 @@ fn xmain() -> ! { let xns = xous_names::XousNames::new().unwrap(); let net_sid = xns.register_name(api::SERVER_NAME_NET, None).expect("can't register server"); + let net_conn = xous::connect(net_sid).unwrap(); log::trace!("registered with NS -- {:?}", net_sid); // hook the COM interrupt listener @@ -113,12 +126,50 @@ fn xmain() -> ! { com.ints_get_active(&mut com_int_list); log::info!("COM pending interrupts after enabling: {:?}", com_int_list); + // build the waiting thread + let wait_conn = Arc::new(AtomicU32::new(0)); + thread::spawn({ + let parent_conn = net_conn.clone(); + let wait_conn_clone = wait_conn.clone(); + move || { + let wait_sid = xous::create_server().unwrap(); + wait_conn_clone.store(xous::connect(wait_sid).unwrap(), Ordering::SeqCst); + let tt = ticktimer_server::Ticktimer::new().unwrap(); + loop { + let msg = xous::receive_message(wait_sid).unwrap(); + match FromPrimitive::from_usize(msg.body.id()) { + Some(WaitOp::WaitMs) => msg_scalar_unpack!(msg, duration_lsb, duration_msb, _, _, { + if duration_msb != 0 { + log::error!("wait duration exceeds API bounds"); + } + tt.sleep_ms(duration_lsb).unwrap(); + send_message(parent_conn, Message::new_scalar(Opcode::NetPump.to_usize().unwrap(), 0, 0, 0, 0)).expect("couldn't pump the net loop"); + }), + Some(WaitOp::PollAt) => msg_scalar_unpack!(msg, deadline_lsb, deadline_msb, _, _, { + let deadline: u64 = (deadline_lsb as u64) | ((deadline_msb as u64) << 32); + let now = tt.elapsed_ms(); + if deadline > now { + log::info!("sleeping for {}", deadline - now); + tt.sleep_ms((deadline - now) as usize).unwrap(); + send_message(parent_conn, Message::new_scalar(Opcode::NetPump.to_usize().unwrap(), 0, 0, 0, 0)).expect("couldn't pump the net loop"); + } + }), + Some(WaitOp::Quit) => break, + None => log::error!("got unknown message: {:?}", msg) + } + } + xous::destroy_server(wait_sid).unwrap(); + } + }); + // wait until the waiting thread starts and has populated a reverse connection ID + while wait_conn.load(Ordering::SeqCst) == 0 { + xous::yield_slice(); + } + let mut net_config: Option = None; - let mut incoming_pkt_buf: [u8; NET_MTU] = [0; NET_MTU]; - let mut incoming_pkt: &mut [u8]; // ping-specific storage - let ping_remote_addr = IpAddress::from_str("10.0.245.10").expect("invalid address format"); + let ping_remote_addr = IpAddress::from_str("10.0.245.243").expect("invalid address format"); let icmp_rx_buffer = IcmpSocketBuffer::new(vec![IcmpPacketMetadata::EMPTY], vec![0; 256]); let icmp_tx_buffer = IcmpSocketBuffer::new(vec![IcmpPacketMetadata::EMPTY], vec![0; 256]); @@ -202,7 +253,7 @@ fn xmain() -> ! { config.gtwy[3], ); - iface.routes_mut().remove_default_ipv4_route(); + // iface.routes_mut().remove_default_ipv4_route(); match iface.routes_mut().add_default_ipv4_route(default_v4_gw) { Ok(route) => log::info!("routing table updated successfully: {:?}", route), Err(e) => log::info!("routing table update error: {}", e), @@ -212,98 +263,13 @@ fn xmain() -> ! { if let Some(_config) = net_config { if let Some(rxlen) = maybe_rxlen { match iface.device_mut().push_rx_avail(rxlen) { - None => log::info!("pushed {} bytes avail to iface", rxlen), - Some(_) => log::warn!("Got more packets, but smoltcp didn't receive them"), + None => {} //log::info!("pushed {} bytes avail to iface", rxlen), + Some(_) => log::warn!("Got more packets, but smoltcp didn't drain them in time"), } - - // now fire off the smoltcp receive machine.... - { - let timestamp = timer.now(); - log::info!("entering poll"); - match iface.poll(&mut sockets, timestamp) { - Ok(_) => { log::info!("poll OK") } - Err(e) => { - log::debug!("poll error: {}", e); - } - } - log::info!("leaving poll"); - { - let timestamp = timer.now(); - let mut socket = sockets.get::(icmp_handle); - if !socket.is_open() { - log::info!("bind to icmp"); - socket.bind(IcmpEndpoint::Ident(ident)).expect("couldn't bind to icmp socket"); - send_at = timestamp; - } - - if socket.can_send() && seq_no < count as u16 && send_at <= timestamp { - log::info!("send icmp {}", count); - NetworkEndian::write_i64(&mut echo_payload, timestamp.total_millis()); - - match ping_remote_addr { - IpAddress::Ipv4(_) => { - let (icmp_repr, mut icmp_packet) = send_icmp_ping!( - Icmpv4Repr, - Icmpv4Packet, - ident, - seq_no, - echo_payload, - socket, - ping_remote_addr - ); - icmp_repr.emit(&mut icmp_packet, &device_caps.checksum); - } - _ => unimplemented!(), - } - - waiting_queue.insert(seq_no, timestamp); - seq_no += 1; - send_at += interval; - } - - if socket.can_recv() { - log::info!("socket can_recv()"); - let (payload, _) = socket.recv().expect("couldn't receive on socket despite asserting availability"); - log::info!("payload: {:x?}", payload); - - match ping_remote_addr { - IpAddress::Ipv4(_) => { - let icmp_packet = Icmpv4Packet::new_checked(&payload).expect("couldn't make icmp payload"); - let icmp_repr = - Icmpv4Repr::parse(&icmp_packet, &device_caps.checksum).expect("error parsing icmp4 repr"); - get_icmp_pong!( - Icmpv4Repr, - icmp_repr, - payload, - waiting_queue, - ping_remote_addr, - timestamp, - received - ); - } - _ => unimplemented!(), - } - } - - waiting_queue.retain(|seq, from| { - if timestamp - *from < timeout { - true - } else { - log::info!("From {} icmp_seq={} timeout", ping_remote_addr, seq); - false - } - }); - - if seq_no == count as u16 && waiting_queue.is_empty() { - break; - } - } - } - - /* - incoming_pkt = &mut incoming_pkt_buf[0..rxlen as usize]; - com.wlan_fetch_packet(incoming_pkt).unwrap(); - log::info!("Rx: {:x?}", incoming_pkt);*/ + send_message( + net_conn, + Message::new_scalar(Opcode::NetPump.to_usize().unwrap(), 0, 0, 0, 0) + ).expect("WlanRxReady couldn't pump the loop"); } else { log::error!("Got RxReady interrupt but no packet length specified!"); } @@ -319,6 +285,119 @@ fn xmain() -> ! { } com.ints_ack(&com_int_list); } + Some(Opcode::NetPump) => { + let timestamp = timer.now(); + match iface.poll(&mut sockets, timestamp) { + Ok(_) => { } + Err(e) => { + log::debug!("poll error: {}", e); + } + } + { + let timestamp = timer.now(); + let mut socket = sockets.get::(icmp_handle); + if !socket.is_open() { + log::info!("bind to icmp"); + socket.bind(IcmpEndpoint::Ident(ident)).expect("couldn't bind to icmp socket"); + send_at = timestamp; + } + + if socket.can_send() && seq_no < count as u16 && send_at <= timestamp { + log::info!("send icmp {}", count); + NetworkEndian::write_i64(&mut echo_payload, timestamp.total_millis()); + + let (icmp_repr, mut icmp_packet) = send_icmp_ping!( + Icmpv4Repr, + Icmpv4Packet, + ident, + seq_no, + echo_payload, + socket, + ping_remote_addr + ); + icmp_repr.emit(&mut icmp_packet, &device_caps.checksum); + log::info!("icmp pkt: {:?}", icmp_packet); + + waiting_queue.insert(seq_no, timestamp); + seq_no += 1; + send_at += interval; + } + + if socket.can_recv() { + log::info!("socket can_recv()"); + let (payload, _) = socket.recv().expect("couldn't receive on socket despite asserting availability"); + log::info!("icmp payload: {:x?}", payload); + + let icmp_packet = Icmpv4Packet::new_checked(&payload).expect("couldn't make icmp payload"); + let icmp_repr = + Icmpv4Repr::parse(&icmp_packet, &device_caps.checksum).expect("error parsing icmp4 repr"); + get_icmp_pong!( + Icmpv4Repr, + icmp_repr, + payload, + waiting_queue, + ping_remote_addr, + timestamp, + received + ); + } + + waiting_queue.retain(|seq, from| { + if timestamp - *from < timeout { + true + } else { + log::info!("From {} icmp_seq={} timeout", ping_remote_addr, seq); + false + } + }); + + if seq_no == count as u16 && waiting_queue.is_empty() { + log::info!("{} packets transmitted, {} received, {:.0}% packet loss", + seq_no, + received, + 100.0 * (seq_no - received) as f64 / seq_no as f64 + ); + seq_no += 1; // extinguish the message after it has printed once + } + } + + // establish our next check-up interval + // it's unclear why the "ping" example has such a crazy complicated poll_at mechanism... + let timestamp = timer.now(); + match iface.poll_at(&sockets, timestamp) { + Some(poll_at) if timestamp < poll_at => { + //log::info!("poll_at: {}", poll_at); + xous::try_send_message(wait_conn.load(Ordering::SeqCst), + Message::new_scalar( + WaitOp::PollAt.to_usize().unwrap(), + (poll_at.total_millis() as u64 & 0xFFFF_FFFF) as usize, + ((poll_at.total_millis() as u64 >> 32) & 0xFFF_FFFF) as usize, + 0, 0) + ).ok(); + } + Some(_) => { + //log::info!("didn't get a specific wait_at, polling immediately..."); + xous::try_send_message(net_conn, + Message::new_scalar( + Opcode::NetPump.to_usize().unwrap(), + 0, 0, 0, 0) + ).ok(); + }, + None => { + /* + let wait_time = (send_at - timestamp).millis(); + log::info!("default wait: {}", wait_time); + send_message(wait_conn.load(Ordering::SeqCst), + Message::new_scalar( + WaitOp::WaitMs.to_usize().unwrap(), + (wait_time & 0xFFFF_FFFF) as usize, + ((wait_time >> 32) & 0xFFF_FFFF) as usize, + 0, 0) + ).expect("couldn't issue wait message"); + */ + } + } + } Some(Opcode::SuspendResume) => xous::msg_scalar_unpack!(msg, token, _, _, _, { // handle an suspend/resume state stuff here. right now, it's a NOP susres.suspend_until_resume(token).expect("couldn't execute suspend/resume");