Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: smoltcp-rs/smoltcp
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 2354e0b29d6f
Choose a base ref
...
head repository: smoltcp-rs/smoltcp
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 39464a53fc00
Choose a head ref
  • 2 commits
  • 18 files changed
  • 1 contributor

Commits on Aug 29, 2017

  1. Reset the timer transitioning from TCP FIN-WAIT-1 to FIN-WAIT-2.

    We don't transmit anything in FIN-WAIT-2, so we don't need the timer
    running, or we'll get spurious log messages about retransmissions.
    This also makes logic cleaner, although with no functional change.
    whitequark committed Aug 29, 2017

    Unverified

    This user has not yet uploaded their public signing key.
    Copy the full SHA
    4663060 View commit details
  2. Compute soft deadline in poll() and use nonblocking sockets.

    Before this commit, anything that touched RawSocket or TapInterface
    worked partly by accident and partly because of a horrible crutch
    that resulted in massive latencies as well as inevitable packet loss
    every time an ARP request had to be issued. Also, there was no way
    to use poll() other than by continuously calling it in a busy loop.
    
    After this commit, poll() indicates when the earliest timer expires,
    and so the caller can sleep until that moment (or until packets
    arrive).
    
    Note that there is a subtle problem remaining: every time poll()
    is called, every socket with a pending outbound packet whose
    IP address doesn't correspond to a MAC address will send a new
    ARP request, resulting in potentially a whole lot of such requests.
    ARP rate limiting is a separate topic though.
    whitequark committed Aug 29, 2017
    1
    Copy the full SHA
    39464a5 View commit details
14 changes: 6 additions & 8 deletions examples/client.rs
Original file line number Diff line number Diff line change
@@ -8,7 +8,8 @@ mod utils;

use std::str::{self, FromStr};
use std::time::Instant;
use smoltcp::Error;
use std::os::unix::io::AsRawFd;
use smoltcp::phy::wait as phy_wait;
use smoltcp::wire::{EthernetAddress, IpAddress};
use smoltcp::iface::{ArpCache, SliceArpCache, EthernetInterface};
use smoltcp::socket::{AsSocket, SocketSet};
@@ -25,6 +26,7 @@ fn main() {

let mut matches = utils::parse_options(&opts, free);
let device = utils::parse_tap_options(&mut matches);
let fd = device.as_raw_fd();
let device = utils::parse_middleware_options(&mut matches, device, /*loopback=*/false);
let address = IpAddress::from_str(&matches.free[0]).expect("invalid address format");
let port = u16::from_str(&matches.free[1]).expect("invalid port format");
@@ -86,12 +88,8 @@ fn main() {
}
}

let timestamp = Instant::now().duration_since(startup_time);
let timestamp_ms = (timestamp.as_secs() * 1000) +
(timestamp.subsec_nanos() / 1000000) as u64;
match iface.poll(&mut sockets, timestamp_ms) {
Ok(()) | Err(Error::Exhausted) => (),
Err(e) => debug!("poll error: {}", e)
}
let timestamp = utils::millis_since(startup_time);
let poll_at = iface.poll(&mut sockets, timestamp).expect("poll error");
phy_wait(fd, poll_at).expect("wait error");
}
}
10 changes: 6 additions & 4 deletions examples/loopback.rs
Original file line number Diff line number Diff line change
@@ -16,7 +16,6 @@ extern crate getopts;
mod utils;

use core::str;
use smoltcp::Error;
use smoltcp::phy::Loopback;
use smoltcp::wire::{EthernetAddress, IpAddress};
use smoltcp::iface::{ArpCache, SliceArpCache, EthernetInterface};
@@ -161,11 +160,14 @@ fn main() {
}

match iface.poll(&mut socket_set, clock.elapsed()) {
Ok(()) | Err(Error::Exhausted) => (),
Ok(Some(poll_at)) => {
let delay = poll_at - clock.elapsed();
debug!("sleeping for {} ms", delay);
clock.advance(delay)
}
Ok(None) => clock.advance(1),
Err(e) => debug!("poll error: {}", e)
}

clock.advance(1);
}

if done {
39 changes: 23 additions & 16 deletions examples/ping.rs
Original file line number Diff line number Diff line change
@@ -8,8 +8,10 @@ extern crate byteorder;
mod utils;

use std::str::{self, FromStr};
use std::time::{Duration, Instant};
use smoltcp::Error;
use std::cmp;
use std::time::Instant;
use std::os::unix::io::AsRawFd;
use smoltcp::phy::wait as phy_wait;
use smoltcp::wire::{EthernetAddress, IpVersion, IpProtocol, IpAddress,
Ipv4Address, Ipv4Packet, Ipv4Repr,
Icmpv4Repr, Icmpv4Packet};
@@ -35,6 +37,7 @@ fn main() {

let mut matches = utils::parse_options(&opts, free);
let device = utils::parse_tap_options(&mut matches);
let fd = device.as_raw_fd();
let device = utils::parse_middleware_options(&mut matches, device, /*loopback=*/false);
let address = Ipv4Address::from_str(&matches.free[0]).expect("invalid address format");
let count = matches.opt_str("count").map(|s| usize::from_str(&s).unwrap()).unwrap_or(4);
@@ -61,7 +64,7 @@ fn main() {
let mut sockets = SocketSet::new(vec![]);
let raw_handle = sockets.add(raw_socket);

let mut send_next = Duration::default();
let mut send_at = 0;
let mut seq_no = 0;
let mut received = 0;
let mut echo_payload = [0xffu8; 40];
@@ -75,11 +78,8 @@ fn main() {
let timestamp_us = (timestamp.as_secs() * 1000000) +
(timestamp.subsec_nanos() / 1000) as u64;

if seq_no == count as u16 && waiting_queue.is_empty() {
break;
}

if socket.can_send() && seq_no < count as u16 && send_next <= timestamp {
if socket.can_send() && seq_no < count as u16 &&
send_at <= utils::millis_since(startup_time) {
NetworkEndian::write_u64(&mut echo_payload, timestamp_us);
let icmp_repr = Icmpv4Repr::EchoRequest {
ident: 1,
@@ -105,7 +105,7 @@ fn main() {

waiting_queue.insert(seq_no, timestamp);
seq_no += 1;
send_next += Duration::new(interval, 0);
send_at += interval * 1000;
}

if socket.can_recv() {
@@ -137,16 +137,23 @@ fn main() {
println!("From {} icmp_seq={} timeout", remote_addr, seq);
false
}
})
});

if seq_no == count as u16 && waiting_queue.is_empty() {
break
}
}

let timestamp = Instant::now().duration_since(startup_time);
let timestamp_ms = (timestamp.as_secs() * 1000) +
(timestamp.subsec_nanos() / 1000000) as u64;
match iface.poll(&mut sockets, timestamp_ms) {
Ok(()) | Err(Error::Exhausted) => (),
Err(e) => debug!("poll error: {}", e),
let timestamp = utils::millis_since(startup_time);

let poll_at = iface.poll(&mut sockets, timestamp).expect("poll error");
let mut resume_at = Some(send_at);
if let Some(poll_at) = poll_at {
resume_at = resume_at.map(|at| cmp::min(at, poll_at))
}

debug!("waiting until {:?} ms", resume_at);
phy_wait(fd, resume_at.map(|at| at.saturating_sub(timestamp))).expect("wait error");
}

println!("--- {} ping statistics ---", remote_addr);
14 changes: 6 additions & 8 deletions examples/server.rs
Original file line number Diff line number Diff line change
@@ -9,7 +9,8 @@ mod utils;
use std::str;
use std::fmt::Write;
use std::time::Instant;
use smoltcp::Error;
use std::os::unix::io::AsRawFd;
use smoltcp::phy::wait as phy_wait;
use smoltcp::wire::{EthernetAddress, IpAddress};
use smoltcp::iface::{ArpCache, SliceArpCache, EthernetInterface};
use smoltcp::socket::{AsSocket, SocketSet};
@@ -25,6 +26,7 @@ fn main() {

let mut matches = utils::parse_options(&opts, free);
let device = utils::parse_tap_options(&mut matches);
let fd = device.as_raw_fd();
let device = utils::parse_middleware_options(&mut matches, device, /*loopback=*/false);

let startup_time = Instant::now();
@@ -154,12 +156,8 @@ fn main() {
}
}

let timestamp = Instant::now().duration_since(startup_time);
let timestamp_ms = (timestamp.as_secs() * 1000) +
(timestamp.subsec_nanos() / 1000000) as u64;
match iface.poll(&mut sockets, timestamp_ms) {
Ok(()) | Err(Error::Exhausted) => (),
Err(e) => debug!("poll error: {}", e)
}
let timestamp = utils::millis_since(startup_time);
let poll_at = iface.poll(&mut sockets, timestamp).expect("poll error");
phy_wait(fd, poll_at).expect("wait error");
}
}
7 changes: 7 additions & 0 deletions examples/utils.rs
Original file line number Diff line number Diff line change
@@ -129,3 +129,10 @@ pub fn parse_middleware_options<D: Device>(matches: &mut Matches, device: D, loo
device.set_bucket_interval(shaping_interval);
device
}

pub fn millis_since(startup_time: Instant) -> u64 {
let duration = Instant::now().duration_since(startup_time);
let duration_ms = (duration.as_secs() * 1000) +
(duration.subsec_nanos() / 1000000) as u64;
duration_ms
}
Loading