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: b0fc1d9542ef
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: 100b57aa669a
Choose a head ref
  • 2 commits
  • 7 files changed
  • 1 contributor

Commits on Oct 4, 2017

  1. Fix a typo.

    whitequark committed Oct 4, 2017
    Copy the full SHA
    067fb2d View commit details

Commits on Oct 5, 2017

  1. Replace "socket debug identifiers" with just socket handles.

    This is basically a rename that now calls an apple an apple,
    except user code can no longer change it. It's not obvious if
    user code getting the socket handle from the socket is very useful,
    but it's not harmful either, so why not.
    whitequark committed Oct 5, 2017
    Copy the full SHA
    100b57a View commit details
Showing with 161 additions and 160 deletions.
  1. +1 −1 src/iface/ethernet.rs
  2. +22 −18 src/macros.rs
  3. +5 −9 src/socket/mod.rs
  4. +24 −26 src/socket/raw.rs
  5. +20 −11 src/socket/set.rs
  6. +71 −74 src/socket/tcp.rs
  7. +18 −21 src/socket/udp.rs
2 changes: 1 addition & 1 deletion src/iface/ethernet.rs
Original file line number Diff line number Diff line change
@@ -145,7 +145,7 @@ impl<'a, 'b, 'c, DeviceT: Device + 'a> Interface<'a, 'b, 'c, DeviceT> {
/// activity as well as certain boundary conditions such as buffer exhaustion.
/// These errors are provided as an aid for troubleshooting, and are meant
/// to be logged and ignored.
///
/// As a special case, `Err(Error::Unrecognized)` is returned in response to
/// packets containing any unsupported protocol, option, or form, which is
/// a very common occurrence and on a production system it should not even
40 changes: 22 additions & 18 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
#[cfg(feature = "log")]
macro_rules! net_log_enabled {
(trace) => (log_enabled!($crate::log::LogLevel::Trace));
(debug) => (log_enabled!($crate::log::LogLevel::Debug));
#[macro_use]
mod log {
macro_rules! net_log_enabled {
(trace) => (log_enabled!($crate::log::LogLevel::Trace));
(debug) => (log_enabled!($crate::log::LogLevel::Debug));
}

macro_rules! net_log {
(trace, $($arg:expr),*) => { trace!($($arg),*); };
(debug, $($arg:expr),*) => { debug!($($arg),*); };
}
}

#[cfg(not(feature = "log"))]
macro_rules! net_log_enabled {
(trace) => (false);
(debug) => (false);
#[macro_use]
mod log {
macro_rules! net_log_enabled {
($level:ident) => (false);
}

macro_rules! net_log {
($level:ident, $($arg:expr),*) => { $( let _ = $arg );* }
}
}

macro_rules! net_trace {
($($arg:expr),*) => {{
#[cfg(feature = "log")]
trace!($($arg),*);
#[cfg(not(feature = "log"))]
$( let _ = $arg );*; // suppress unused variable warnings
}}
($($arg:expr),*) => (net_log!(trace, $($arg),*));
}

macro_rules! net_debug {
($($arg:expr),*) => {{
#[cfg(feature = "log")]
debug!($($arg),*);
#[cfg(not(feature = "log"))]
$( let _ = $arg );*; // suppress unused variable warnings
}}
($($arg:expr),*) => (net_log!(debug, $($arg),*));
}

macro_rules! enum_with_unknown {
14 changes: 5 additions & 9 deletions src/socket/mod.rs
Original file line number Diff line number Diff line change
@@ -76,17 +76,13 @@ macro_rules! dispatch_socket {
}

impl<'a, 'b> Socket<'a, 'b> {
/// Return the debug identifier.
pub fn debug_id(&self) -> usize {
dispatch_socket!(self, |socket []| socket.debug_id())
/// Return the socket handle.
pub fn handle(&self) -> SocketHandle {
dispatch_socket!(self, |socket []| socket.handle())
}

/// Set the debug identifier.
///
/// The debug identifier is a number printed in socket trace messages.
/// It could as well be used by the user code.
pub fn set_debug_id(&mut self, id: usize) {
dispatch_socket!(self, |socket [mut]| socket.set_debug_id(id))
pub(crate) fn set_handle(&mut self, handle: SocketHandle) {
dispatch_socket!(self, |socket [mut]| socket.set_handle(handle))
}

pub(crate) fn poll_at(&self) -> Option<u64> {
50 changes: 24 additions & 26 deletions src/socket/raw.rs
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ use managed::Managed;
use {Error, Result};
use phy::ChecksumCapabilities;
use wire::{IpVersion, IpProtocol, Ipv4Repr, Ipv4Packet};
use socket::{IpRepr, Socket};
use socket::{IpRepr, Socket, SocketHandle};
use storage::{Resettable, RingBuffer};

/// A buffered raw IP packet.
@@ -57,7 +57,7 @@ pub type SocketBuffer<'a, 'b: 'a> = RingBuffer<'a, PacketBuffer<'b>>;
/// transmit and receive packet buffers.
#[derive(Debug)]
pub struct RawSocket<'a, 'b: 'a> {
debug_id: usize,
handle: SocketHandle,
ip_version: IpVersion,
ip_protocol: IpProtocol,
rx_buffer: SocketBuffer<'a, 'b>,
@@ -71,26 +71,23 @@ impl<'a, 'b> RawSocket<'a, 'b> {
rx_buffer: SocketBuffer<'a, 'b>,
tx_buffer: SocketBuffer<'a, 'b>) -> Socket<'a, 'b> {
Socket::Raw(RawSocket {
debug_id: 0,
handle: SocketHandle::EMPTY,
ip_version,
ip_protocol,
rx_buffer,
tx_buffer,
})
}

/// Return the debug identifier.
/// Return the socket handle.
#[inline]
pub fn debug_id(&self) -> usize {
self.debug_id
pub fn handle(&self) -> SocketHandle {
self.handle
}

/// Set the debug identifier.
///
/// The debug identifier is a number printed in socket trace messages.
/// It could as well be used by the user code.
pub fn set_debug_id(&mut self, id: usize) {
self.debug_id = id;
/// Set the socket handle.
pub(in super) fn set_handle(&mut self, handle: SocketHandle) {
self.handle = handle;
}

/// Return the IP version the socket is bound to.
@@ -129,8 +126,8 @@ impl<'a, 'b> RawSocket<'a, 'b> {
/// the header actually transmitted bit for bit.
pub fn send(&mut self, size: usize) -> Result<&mut [u8]> {
let packet_buf = self.tx_buffer.enqueue_one_with(|buf| buf.resize(size))?;
net_trace!("[{}]:{}:{}: buffer to send {} octets",
self.debug_id, self.ip_version, self.ip_protocol,
net_trace!("{}:{}:{}: buffer to send {} octets",
self.handle, self.ip_version, self.ip_protocol,
packet_buf.size);
Ok(packet_buf.as_mut())
}
@@ -151,8 +148,8 @@ impl<'a, 'b> RawSocket<'a, 'b> {
/// the header actually received bit for bit.
pub fn recv(&mut self) -> Result<&[u8]> {
let packet_buf = self.rx_buffer.dequeue_one()?;
net_trace!("[{}]:{}:{}: receive {} buffered octets",
self.debug_id, self.ip_version, self.ip_protocol,
net_trace!("{}:{}:{}: receive {} buffered octets",
self.handle, self.ip_version, self.ip_protocol,
packet_buf.size);
Ok(&packet_buf.as_ref())
}
@@ -174,7 +171,7 @@ impl<'a, 'b> RawSocket<'a, 'b> {
true
}

pub(crate) fn process(&mut self, ip_repr: &IpRepr, payload: &[u8],
pub(crate) fn process(&mut self, ip_repr: &IpRepr, payload: &[u8],
checksum_caps: &ChecksumCapabilities) -> Result<()> {
debug_assert!(self.accepts(ip_repr));

@@ -183,15 +180,16 @@ impl<'a, 'b> RawSocket<'a, 'b> {
let packet_buf = self.rx_buffer.enqueue_one_with(|buf| buf.resize(total_len))?;
ip_repr.emit(&mut packet_buf.as_mut()[..header_len], &checksum_caps);
packet_buf.as_mut()[header_len..].copy_from_slice(payload);
net_trace!("[{}]:{}:{}: receiving {} octets",
self.debug_id, self.ip_version, self.ip_protocol,
net_trace!("{}:{}:{}: receiving {} octets",
self.handle, self.ip_version, self.ip_protocol,
packet_buf.size);
Ok(())
}

pub(crate) fn dispatch<F>(&mut self, emit: F, checksum_caps: &ChecksumCapabilities) -> Result<()>
pub(crate) fn dispatch<F>(&mut self, emit: F, checksum_caps: &ChecksumCapabilities) ->
Result<()>
where F: FnOnce((IpRepr, &[u8])) -> Result<()> {
fn prepare<'a>(protocol: IpProtocol, buffer: &'a mut [u8],
fn prepare<'a>(protocol: IpProtocol, buffer: &'a mut [u8],
checksum_caps: &ChecksumCapabilities) -> Result<(IpRepr, &'a [u8])> {
match IpVersion::of_packet(buffer.as_ref())? {
IpVersion::Ipv4 => {
@@ -213,20 +211,20 @@ impl<'a, 'b> RawSocket<'a, 'b> {
}
}

let debug_id = self.debug_id;
let handle = self.handle;
let ip_protocol = self.ip_protocol;
let ip_version = self.ip_version;
self.tx_buffer.dequeue_one_with(|packet_buf| {
match prepare(ip_protocol, packet_buf.as_mut(), &checksum_caps) {
Ok((ip_repr, raw_packet)) => {
net_trace!("[{}]:{}:{}: sending {} octets",
debug_id, ip_version, ip_protocol,
net_trace!("{}:{}:{}: sending {} octets",
handle, ip_version, ip_protocol,
ip_repr.buffer_len() + raw_packet.len());
emit((ip_repr, raw_packet))
}
Err(error) => {
net_debug!("[{}]:{}:{}: dropping outgoing packet ({})",
debug_id, ip_version, ip_protocol,
net_debug!("{}:{}:{}: dropping outgoing packet ({})",
handle, ip_version, ip_protocol,
error);
// Return Ok(()) so the packet is dequeued.
Ok(())
31 changes: 20 additions & 11 deletions src/socket/set.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core::{fmt, slice};
use managed::ManagedSlice;
use core::slice;

use super::Socket;
#[cfg(feature = "socket-tcp")] use super::TcpState;
@@ -16,8 +16,16 @@ pub struct Item<'a, 'b: 'a> {

/// A handle, identifying a socket in a set.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
pub struct Handle {
index: usize
pub struct Handle(usize);

impl Handle {
pub(crate) const EMPTY: Handle = Handle(0);
}

impl fmt::Display for Handle {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "#{}", self.0)
}
}

/// An extensible set of sockets, with stable numeric identifiers.
@@ -46,9 +54,10 @@ impl<'a, 'b: 'a, 'c: 'a + 'b> Set<'a, 'b, 'c> {
fn put<'b, 'c>(index: usize, slot: &mut Option<Item<'b, 'c>>,
mut socket: Socket<'b, 'c>) -> Handle {
net_trace!("[{}]: adding", index);
socket.set_debug_id(index);
let handle = Handle(index);
socket.set_handle(handle);
*slot = Some(Item { socket: socket, refs: 1 });
return Handle { index: index }
handle
}

for (index, slot) in self.sockets.iter_mut().enumerate() {
@@ -75,7 +84,7 @@ impl<'a, 'b: 'a, 'c: 'a + 'b> Set<'a, 'b, 'c> {
/// # Panics
/// This function may panic if the handle does not belong to this socket set.
pub fn get(&self, handle: Handle) -> &Socket<'b, 'c> {
&self.sockets[handle.index]
&self.sockets[handle.0]
.as_ref()
.expect("handle does not refer to a valid socket")
.socket
@@ -86,7 +95,7 @@ impl<'a, 'b: 'a, 'c: 'a + 'b> Set<'a, 'b, 'c> {
/// # Panics
/// This function may panic if the handle does not belong to this socket set.
pub fn get_mut(&mut self, handle: Handle) -> &mut Socket<'b, 'c> {
&mut self.sockets[handle.index]
&mut self.sockets[handle.0]
.as_mut()
.expect("handle does not refer to a valid socket")
.socket
@@ -97,8 +106,8 @@ impl<'a, 'b: 'a, 'c: 'a + 'b> Set<'a, 'b, 'c> {
/// # Panics
/// This function may panic if the handle does not belong to this socket set.
pub fn remove(&mut self, handle: Handle) -> Socket<'b, 'c> {
net_trace!("[{}]: removing", handle.index);
match self.sockets[handle.index].take() {
net_trace!("[{}]: removing", handle.0);
match self.sockets[handle.0].take() {
Some(item) => item.socket,
None => panic!("handle does not refer to a valid socket")
}
@@ -109,7 +118,7 @@ impl<'a, 'b: 'a, 'c: 'a + 'b> Set<'a, 'b, 'c> {
/// # Panics
/// This function may panic if the handle does not belong to this socket set.
pub fn retain(&mut self, handle: Handle) {
self.sockets[handle.index]
self.sockets[handle.0]
.as_mut()
.expect("handle does not refer to a valid socket")
.refs += 1
@@ -121,7 +130,7 @@ impl<'a, 'b: 'a, 'c: 'a + 'b> Set<'a, 'b, 'c> {
/// This function may panic if the handle does not belong to this socket set,
/// or if the reference count is already zero.
pub fn release(&mut self, handle: Handle) {
let refs = &mut self.sockets[handle.index]
let refs = &mut self.sockets[handle.0]
.as_mut()
.expect("handle does not refer to a valid socket")
.refs;
Loading