From 76d8feae8d043693059f3db08dc748f961e55ffa Mon Sep 17 00:00:00 2001 From: Dan Robertson Date: Sat, 10 Feb 2018 20:43:58 +0000 Subject: [PATCH] Use smoltcp::time types instead of u64 timestamps smoltcp is moving towards using time::Duration and time::Instant instead of a u64 timestamp. --- Cargo.lock | 86 +++++++++++++++++++++++++++++--------- src/smolnetd/device.rs | 5 ++- src/smolnetd/scheme/mod.rs | 45 ++++++++++---------- 3 files changed, 90 insertions(+), 46 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ba97aa7ca1..dc2d5eae74 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,6 +3,14 @@ name = "arg_parser" version = "0.1.0" source = "git+https://github.com/redox-os/arg-parser.git#7503531821b0c111f9fac77a5d2536ea221105fe" +[[package]] +name = "arrayvec" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "nodrop 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "base64" version = "0.6.0" @@ -33,14 +41,36 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] -name = "coco" -version = "0.1.1" +name = "crossbeam-deque" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "either 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "crossbeam-epoch 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "crossbeam-utils 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "arrayvec 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "crossbeam-utils 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "memoffset 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "nodrop 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "crossbeam-utils" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "dns-parser" version = "0.7.1" @@ -50,11 +80,6 @@ dependencies = [ "quick-error 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "either" -version = "1.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "extra" version = "0.1.0" @@ -141,6 +166,11 @@ name = "lazy_static" version = "0.2.11" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "lazy_static" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "libc" version = "0.2.36" @@ -172,6 +202,11 @@ name = "matches" version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "memoffset" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "mime" version = "0.2.6" @@ -183,7 +218,7 @@ dependencies = [ [[package]] name = "netutils" version = "0.1.0" -source = "git+https://github.com/redox-os/netutils.git#a719fa50b065840ffacc81d989e47f258b3e5170" +source = "git+https://github.com/redox-os/netutils.git#a85c2af76fdfd0522d0fb31bebbe04f4b4e7e4b6" dependencies = [ "arg_parser 0.1.0 (git+https://github.com/redox-os/arg-parser.git)", "extra 0.1.0 (git+https://github.com/redox-os/libextra.git)", @@ -197,6 +232,11 @@ dependencies = [ "termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "nodrop" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "ntpclient" version = "0.0.1" @@ -238,11 +278,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "rand" -version = "0.3.20" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -250,19 +291,19 @@ name = "rayon" version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "rayon-core 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rayon-core 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "rayon-core" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "coco 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "crossbeam-deque 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.3.20 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -342,7 +383,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "smoltcp" version = "0.4.0" -source = "git+https://github.com/m-labs/smoltcp.git#c2d18ec071a2cd68aee2724ce13eefa6ccf1f0c1" +source = "git+https://github.com/m-labs/smoltcp.git#c418b60b5db93753999ae51d33ec4dc1d1631c69" dependencies = [ "byteorder 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "managed 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -469,14 +510,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] "checksum arg_parser 0.1.0 (git+https://github.com/redox-os/arg-parser.git)" = "" +"checksum arrayvec 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)" = "a1e964f9e24d588183fcb43503abda40d288c8657dfc27311516ce2f05675aef" "checksum base64 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "96434f987501f0ed4eb336a411e0631ecd1afa11574fe148587adc4ff96143c9" "checksum bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b3c30d3802dfb7281680d6285f2ccdaa8c2d8fee41f93805dba5c4cf50dc23cf" "checksum byteorder 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0fc10e8cc6b2580fda3f36eb6dc5316657f812a3df879a44a66fc9f0fdbc4855" "checksum byteorder 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "652805b7e73fada9d85e9a6682a4abd490cb52d96aeecc12e33a0de34dfd0d23" "checksum cfg-if 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d4c819a1287eb618df47cc647173c5c4c66ba19d888a6e50d605672aed3140de" -"checksum coco 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c06169f5beb7e31c7c67ebf5540b8b472d23e3eade3b2ec7d1f5b504a85f91bd" +"checksum crossbeam-deque 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f739f8c5363aca78cfb059edf753d8f0d36908c348f3d8d1503f03d8b75d9cf3" +"checksum crossbeam-epoch 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "59796cc6cbbdc6bb319161349db0c3250ec73ec7fcb763a51065ec4e2e158552" +"checksum crossbeam-utils 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2760899e32a1d58d5abb31129f8fae5de75220bc2176e77ff7c627ae45c918d9" "checksum dns-parser 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f7020f6760aea312d43d23cb83bf6c0c49f162541db8b02bf95209ac51dc253d" -"checksum either 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "740178ddf48b1a9e878e6d6509a1442a2d42fd2928aae8e7a6f8a36fb01981b3" "checksum extra 0.1.0 (git+https://github.com/redox-os/libextra.git)" = "" "checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" "checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" @@ -488,21 +531,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" "checksum language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a91d884b6667cd606bb5a69aa0c99ba811a115fc68915e7056ec08a46e93199a" "checksum lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "76f033c7ad61445c5b347c7382dd1237847eb1bce590fe50365dcb33d546be73" +"checksum lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c8f31047daa365f19be14b47c29df4f7c3b581832407daabe6ae77397619237d" "checksum libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)" = "1e5d97d6708edaa407429faa671b942dc0f2727222fb6b6539bf1db936e4b121" "checksum log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e19e8d5c34a3e0e2223db8e060f9e8264aeeb5c5fc64a4ee9965c062211c024b" "checksum log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "89f010e843f2b1a31dbd316b3b8d443758bc634bed37aabade59c686d644e0a2" "checksum managed 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "43e2737ecabe4ae36a68061398bf27d2bfd0763f4c3c837a398478459494c4b7" "checksum matches 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "100aabe6b8ff4e4a7e32c1c13523379802df0772b82466207ac25b013f193376" +"checksum memoffset 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0f9dc261e2b62d7a622bf416ea3c5245cdd5d9a7fcc428c0d06804dfce1775b3" "checksum mime 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ba626b8a6de5da682e1caa06bdb42a335aee5a84db8e5046a3e8ab17ba0a3ae0" "checksum netutils 0.1.0 (git+https://github.com/redox-os/netutils.git)" = "" +"checksum nodrop 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "9a2228dca57108069a5262f2ed8bd2e82496d2e074a06d1ccc7ce1687b6ae0a2" "checksum ntpclient 0.0.1 (git+https://github.com/willem66745/ntpclient-rust)" = "" "checksum num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c51a3322e4bca9d212ad9a158a02abc6934d005490c054a2778df73a70aa0a30" "checksum pbr 1.0.0 (git+https://github.com/a8m/pb)" = "" "checksum percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "31010dd2e1ac33d5b46a5b413495239882813e0369f8ed8a5e266f173602f831" "checksum quick-error 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "eda5fe9b71976e62bc81b781206aaa076401769b2143379d3eb2118388babac4" -"checksum rand 0.3.20 (registry+https://github.com/rust-lang/crates.io-index)" = "512870020642bb8c221bf68baa1b2573da814f6ccfe5c9699b1c303047abe9b1" +"checksum rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "eba5f8cb59cc50ed56be8880a5c7b496bfd9bd26394e176bc67884094145c2c5" "checksum rayon 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a77c51c07654ddd93f6cb543c7a849863b03abc7e82591afda6dc8ad4ac3ac4a" -"checksum rayon-core 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e64b609139d83da75902f88fd6c01820046840a18471e4dfcd5ac7c0f46bea53" +"checksum rayon-core 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9d24ad214285a7729b174ed6d3bcfcb80177807f959d95fafd5bfc5c4f201ac8" "checksum redox_event 0.1.0 (git+https://github.com/redox-os/event.git)" = "" "checksum redox_syscall 0.1.37 (git+https://github.com/redox-os/syscall.git)" = "" "checksum redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)" = "0d92eecebad22b767915e4d529f89f28ee96dbbf5a4810d2b844373f136417fd" diff --git a/src/smolnetd/device.rs b/src/smolnetd/device.rs index 950f35ff9d..dbb6eefe4d 100644 --- a/src/smolnetd/device.rs +++ b/src/smolnetd/device.rs @@ -5,6 +5,7 @@ use std::fs::File; use std::io::Write; use std::rc::Rc; +use smoltcp::time::Instant; use smoltcp::wire::EthernetAddress; use buffer_pool::{Buffer, BufferPool}; @@ -44,7 +45,7 @@ pub struct RxToken { } impl smoltcp::phy::RxToken for RxToken { - fn consume(self, _timestamp: u64, f: F) -> smoltcp::Result + fn consume(self, _timestamp: Instant, f: F) -> smoltcp::Result where F: FnOnce(&[u8]) -> smoltcp::Result, { @@ -57,7 +58,7 @@ pub struct TxToken { } impl smoltcp::phy::TxToken for TxToken { - fn consume(self, _timestamp: u64, len: usize, f: F) -> smoltcp::Result + fn consume(self, _timestamp: Instant, len: usize, f: F) -> smoltcp::Result where F: FnOnce(&mut [u8]) -> smoltcp::Result, { diff --git a/src/smolnetd/scheme/mod.rs b/src/smolnetd/scheme/mod.rs index 22ad873c48..1afb129605 100644 --- a/src/smolnetd/scheme/mod.rs +++ b/src/smolnetd/scheme/mod.rs @@ -2,6 +2,7 @@ use netutils::getcfg; use smoltcp; use smoltcp::iface::{EthernetInterface, EthernetInterfaceBuilder, NeighborCache}; use smoltcp::socket::SocketSet as SmoltcpSocketSet; +use smoltcp::time::{Duration, Instant}; use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, IpEndpoint, Ipv4Address}; use std::cell::RefCell; use std::collections::{BTreeMap, VecDeque}; @@ -10,7 +11,6 @@ use std::io::{Read, Write}; use std::mem::size_of; use std::rc::Rc; use std::str::FromStr; -use std::time::Instant; use syscall::data::TimeSpec; use syscall; @@ -33,14 +33,16 @@ mod netcfg; type SocketSet = SmoltcpSocketSet<'static, 'static, 'static>; type Interface = Rc>>; +const MAX_DURATION: Duration = Duration { millis: ::std::u64::MAX }; +const MIN_DURATION: Duration = Duration { millis: 0 }; + pub struct Smolnetd { network_file: Rc>, time_file: File, iface: Interface, socket_set: Rc>, - - startup_time: Instant, + timer: ::std::time::Instant, ip_scheme: IpScheme, udp_scheme: UdpScheme, @@ -55,8 +57,8 @@ pub struct Smolnetd { impl Smolnetd { const MAX_PACKET_SIZE: usize = 2048; const SOCKET_BUFFER_SIZE: usize = 128; //packets - const MIN_CHECK_TIMEOUT_MS: i64 = 10; - const MAX_CHECK_TIMEOUT_MS: i64 = 500; + const MIN_CHECK_TIMEOUT: Duration = Duration { millis: 10 }; + const MAX_CHECK_TIMEOUT: Duration = Duration { millis: 500 }; pub fn new( network_file: File, @@ -98,7 +100,7 @@ impl Smolnetd { Smolnetd { iface: Rc::clone(&iface), socket_set: Rc::clone(&socket_set), - startup_time: Instant::now(), + timer: ::std::time::Instant::now(), time_file, ip_scheme: IpScheme::new(Rc::clone(&socket_set), ip_file), udp_scheme: UdpScheme::new(Rc::clone(&socket_set), udp_file), @@ -153,7 +155,7 @@ impl Smolnetd { Ok(None) } - fn schedule_time_event(&mut self, timeout: i64) -> Result<()> { + fn schedule_time_event(&mut self, timeout: Duration) -> Result<()> { let mut time = TimeSpec::default(); if self.time_file.read(&mut time)? < size_of::() { return Err(Error::from_syscall_error( @@ -162,7 +164,7 @@ impl Smolnetd { )); } let mut time_ms = time.tv_sec * 1000i64 + i64::from(time.tv_nsec) / 1_000_000i64; - time_ms += timeout; + time_ms += timeout.total_millis() as i64; time.tv_sec = time_ms / 1000; time.tv_nsec = ((time_ms % 1000) * 1_000_000) as i32; self.time_file @@ -171,37 +173,37 @@ impl Smolnetd { Ok(()) } - fn poll(&mut self) -> Result { + fn poll(&mut self) -> Result { let timeout = { let mut iter_limit = 10usize; let mut iface = self.iface.borrow_mut(); let mut socket_set = self.socket_set.borrow_mut(); - let timestamp = self.get_timestamp(); + let timestamp = Instant::from(self.timer); loop { if iter_limit == 0 { - break 0; + break MIN_DURATION; } iter_limit -= 1; match iface.poll(&mut socket_set, timestamp) { Ok(_) | Err(smoltcp::Error::Unrecognized) => (), Err(e) => { error!("poll error: {}", e); - break 0; + break MIN_DURATION; } } - match iface.poll_at(&socket_set, timestamp) { - Some(n) if n > timestamp => { - break ::std::cmp::min(::std::i64::MAX as u64, n - timestamp) as i64 + match iface.poll_delay(&socket_set, timestamp) { + Some(Duration { millis: 0 }) => { } + Some(delay) => { + break ::std::cmp::min(MAX_DURATION, delay) } - Some(_) => {} - None => break ::std::i64::MAX, + None => break MAX_DURATION } } }; self.notify_sockets()?; Ok(::std::cmp::min( - ::std::cmp::max(Smolnetd::MIN_CHECK_TIMEOUT_MS, timeout), - Smolnetd::MAX_CHECK_TIMEOUT_MS, + ::std::cmp::max(Smolnetd::MIN_CHECK_TIMEOUT, timeout), + Smolnetd::MAX_CHECK_TIMEOUT, )) } @@ -223,11 +225,6 @@ impl Smolnetd { Ok(total_frames) } - fn get_timestamp(&self) -> u64 { - let duration = Instant::now().duration_since(self.startup_time); - (duration.as_secs() * 1000) + u64::from(duration.subsec_nanos() / 1_000_000) - } - fn notify_sockets(&mut self) -> Result<()> { self.ip_scheme.notify_sockets()?; self.udp_scheme.notify_sockets()?;