From 26fdd42f8fe0ce67f13306f62f301678ac42d6f8 Mon Sep 17 00:00:00 2001 From: whitequark Date: Wed, 22 Nov 2017 08:09:06 +0000 Subject: [PATCH] runtime: update smoltcp. --- artiq/firmware/Cargo.lock | 20 ++--- artiq/firmware/runtime/Cargo.toml | 4 +- artiq/firmware/runtime/ethmac.rs | 121 ++++++++++++++++++------------ artiq/firmware/runtime/lib.rs | 9 +-- 4 files changed, 87 insertions(+), 67 deletions(-) diff --git a/artiq/firmware/Cargo.lock b/artiq/firmware/Cargo.lock index f484c8843..4adc90bc3 100644 --- a/artiq/firmware/Cargo.lock +++ b/artiq/firmware/Cargo.lock @@ -1,7 +1,3 @@ -[root] -name = "std_artiq" -version = "0.0.0" - [[package]] name = "alloc_list" version = "0.0.0" @@ -135,7 +131,7 @@ dependencies = [ [[package]] name = "managed" version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" +source = "git+https://github.com/m-labs/rust-managed.git?rev=629a6786a1cf1692015f464ed16c04eafa5cb8d1#629a6786a1cf1692015f464ed16c04eafa5cb8d1" [[package]] name = "proto" @@ -164,7 +160,7 @@ dependencies = [ "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "logger_artiq 0.0.0", "proto 0.0.0", - "smoltcp 0.4.0 (git+https://github.com/m-labs/smoltcp?rev=1e18c03)", + "smoltcp 0.4.0 (git+https://github.com/m-labs/smoltcp?rev=9914616)", "std_artiq 0.0.0", ] @@ -190,13 +186,17 @@ dependencies = [ [[package]] name = "smoltcp" version = "0.4.0" -source = "git+https://github.com/m-labs/smoltcp?rev=1e18c03#1e18c03d3452daf63040924d01c3ce63865939bd" +source = "git+https://github.com/m-labs/smoltcp?rev=9914616#9914616893c373de3fb52fcd2f06e0fffb4c8904" dependencies = [ "byteorder 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", - "managed 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "managed 0.4.0 (git+https://github.com/m-labs/rust-managed.git?rev=629a6786a1cf1692015f464ed16c04eafa5cb8d1)", ] +[[package]] +name = "std_artiq" +version = "0.0.0" + [[package]] name = "walkdir" version = "1.0.3" @@ -227,9 +227,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" = "a51822fc847e7a8101514d1d44e354ba2ffa7d4c194dcab48870740e327cac70" "checksum log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ab83497bf8bf4ed2a74259c1c802351fcd67a65baa86394b6ba73c36f4838054" "checksum log_buffer 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ec57723b84bbe7bdf76aa93169c9b59e67473317c6de3a83cb2a0f8ccb2aa493" -"checksum managed 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b5d48e8c30a4363e2981fe4db20527f6ab0f32a243bbc75379dea5a64f60dae4" +"checksum managed 0.4.0 (git+https://github.com/m-labs/rust-managed.git?rev=629a6786a1cf1692015f464ed16c04eafa5cb8d1)" = "" "checksum rustc-cfg 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "56a596b5718bf5e059d59a30af12f7f462a152de147aa462b70892849ee18704" -"checksum smoltcp 0.4.0 (git+https://github.com/m-labs/smoltcp?rev=1e18c03)" = "" +"checksum smoltcp 0.4.0 (git+https://github.com/m-labs/smoltcp?rev=9914616)" = "" "checksum walkdir 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "dd7c16466ecc507c7cb5988db03e6eab4aaeab89a5c37a29251fcfd3ac9b7afe" "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" "checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" diff --git a/artiq/firmware/runtime/Cargo.toml b/artiq/firmware/runtime/Cargo.toml index 637fd4025..d376677d7 100644 --- a/artiq/firmware/runtime/Cargo.toml +++ b/artiq/firmware/runtime/Cargo.toml @@ -37,6 +37,6 @@ features = ["alloc"] [dependencies.smoltcp] git = "https://github.com/m-labs/smoltcp" -rev = "1e18c03" +rev = "9914616" default-features = false -features = ["alloc", "log", "proto-tcp"] +features = ["alloc", "log", "socket-tcp"] diff --git a/artiq/firmware/runtime/ethmac.rs b/artiq/firmware/runtime/ethmac.rs index b2a580a0f..3f9be525b 100644 --- a/artiq/firmware/runtime/ethmac.rs +++ b/artiq/firmware/runtime/ethmac.rs @@ -1,6 +1,6 @@ use core::{slice, fmt}; -use smoltcp::Error; -use smoltcp::phy::{DeviceCapabilities, Device}; +use smoltcp::Result; +use smoltcp::phy::{self, DeviceCapabilities, Device}; use board::{csr, mem}; @@ -8,6 +8,26 @@ const RX_SLOTS: usize = csr::ETHMAC_RX_SLOTS as usize; const TX_SLOTS: usize = csr::ETHMAC_TX_SLOTS as usize; const SLOT_SIZE: usize = csr::ETHMAC_SLOT_SIZE as usize; +fn next_rx_slot() -> Option { + unsafe { + if csr::ethmac::sram_writer_ev_pending_read() == 0 { + None + } else { + Some(csr::ethmac::sram_writer_slot_read() as usize) + } + } +} + +fn next_tx_slot() -> Option { + unsafe { + if csr::ethmac::sram_reader_ready_read() == 0 { + None + } else { + Some((csr::ethmac::sram_reader_slot_read() as usize + 1) % TX_SLOTS) + } + } +} + fn rx_buffer(slot: usize) -> *const u8 { debug_assert!(slot < RX_SLOTS); (mem::ETHMAC_BASE + SLOT_SIZE * slot) as _ @@ -18,11 +38,17 @@ fn tx_buffer(slot: usize) -> *mut u8 { (mem::ETHMAC_BASE + SLOT_SIZE * (RX_SLOTS + slot)) as _ } -pub struct EthernetDevice; +pub struct EthernetDevice(()); -impl Device for EthernetDevice { - type RxBuffer = RxBuffer; - type TxBuffer = TxBuffer; +impl EthernetDevice { + pub unsafe fn new() -> EthernetDevice { + EthernetDevice(()) + } +} + +impl<'a> Device<'a> for EthernetDevice { + type RxToken = EthernetRxSlot; + type TxToken = EthernetTxSlot; fn capabilities(&self) -> DeviceCapabilities { let mut caps = DeviceCapabilities::default(); @@ -31,61 +57,56 @@ impl Device for EthernetDevice { caps } - fn receive(&mut self, _timestamp: u64) -> Result { - unsafe { - if csr::ethmac::sram_writer_ev_pending_read() == 0 { - return Err(Error::Exhausted) - } + fn receive(&mut self) -> Option<(Self::RxToken, Self::TxToken)> { + if let (Some(rx_slot), Some(tx_slot)) = (next_rx_slot(), next_tx_slot()) { + Some((EthernetRxSlot(rx_slot), EthernetTxSlot(tx_slot))) + } else { + None + } + } - let slot = csr::ethmac::sram_writer_slot_read() as usize; + fn transmit(&mut self) -> Option { + if let Some(tx_slot) = next_tx_slot() { + Some(EthernetTxSlot(tx_slot)) + } else { + None + } + } +} + +pub struct EthernetRxSlot(usize); + +impl phy::RxToken for EthernetRxSlot { + fn consume(self, _timestamp: u64, f: F) -> Result + where F: FnOnce(&[u8]) -> Result + { + unsafe { let length = csr::ethmac::sram_writer_length_read() as usize; - Ok(RxBuffer(slice::from_raw_parts(rx_buffer(slot), length))) + let result = f(slice::from_raw_parts(rx_buffer(self.0), length)); + csr::ethmac::sram_writer_ev_pending_write(1); + result } } +} + +pub struct EthernetTxSlot(usize); + +impl phy::TxToken for EthernetTxSlot { + fn consume(self, _timestamp: u64, length: usize, f: F) -> Result + where F: FnOnce(&mut [u8]) -> Result + { + debug_assert!(length < SLOT_SIZE); - fn transmit(&mut self, _timestamp: u64, length: usize) -> Result { unsafe { - if csr::ethmac::sram_reader_ready_read() == 0 { - return Err(Error::Exhausted) - } - - let slot = csr::ethmac::sram_reader_slot_read() as usize; - let slot = (slot + 1) % TX_SLOTS; - csr::ethmac::sram_reader_slot_write(slot as u8); + let result = f(slice::from_raw_parts_mut(tx_buffer(self.0), length))?; + csr::ethmac::sram_reader_slot_write(self.0 as u8); csr::ethmac::sram_reader_length_write(length as u16); - Ok(TxBuffer(slice::from_raw_parts_mut(tx_buffer(slot), length))) + csr::ethmac::sram_reader_start_write(1); + Ok(result) } } } -pub struct RxBuffer(&'static [u8]); - -impl AsRef<[u8]> for RxBuffer { - fn as_ref(&self) -> &[u8] { self.0 } -} - -impl Drop for RxBuffer { - fn drop(&mut self) { - unsafe { csr::ethmac::sram_writer_ev_pending_write(1) } - } -} - -pub struct TxBuffer(&'static mut [u8]); - -impl AsRef<[u8]> for TxBuffer { - fn as_ref(&self) -> &[u8] { self.0 } -} - -impl AsMut<[u8]> for TxBuffer { - fn as_mut(&mut self) -> &mut [u8] { self.0 } -} - -impl Drop for TxBuffer { - fn drop(&mut self) { - unsafe { csr::ethmac::sram_reader_start_write(1) } - } -} - #[derive(Debug, Clone, PartialEq, Eq, Default)] pub struct EthernetStatistics { rx_errors: u32, diff --git a/artiq/firmware/runtime/lib.rs b/artiq/firmware/runtime/lib.rs index 9265f0c31..57d3629a4 100644 --- a/artiq/firmware/runtime/lib.rs +++ b/artiq/firmware/runtime/lib.rs @@ -21,7 +21,6 @@ extern crate amp; #[cfg(has_drtio)] extern crate drtioaux; -use std::boxed::Box; use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr}; use proto::{mgmt_proto, analyzer_proto, moninj_proto, rpc_proto, session_proto, kernel_proto}; use amp::{mailbox, rpc_queue}; @@ -127,12 +126,12 @@ fn startup_ethernet() { // print!("\x1b[37m[{:6}.{:06}s]\n{}\x1b[0m", seconds, micros, printer) // } - let net_device = ethmac::EthernetDevice; + let net_device = unsafe { ethmac::EthernetDevice::new() }; // let net_device = smoltcp::phy::EthernetTracer::new(net_device, _net_trace_writer); - let arp_cache = smoltcp::iface::SliceArpCache::new([Default::default(); 8]); + let mut neighbor_cache_storage = [None; 8]; + let neighbor_cache = smoltcp::iface::NeighborCache::new(&mut neighbor_cache_storage[..]); let mut interface = smoltcp::iface::EthernetInterface::new( - Box::new(net_device), Box::new(arp_cache) as Box, - hardware_addr, [IpCidr::new(protocol_addr, 0)], None); + net_device, neighbor_cache, hardware_addr, [IpCidr::new(protocol_addr, 0)], None); let mut scheduler = sched::Scheduler::new(); let io = scheduler.io();