Skip to content

Commit

Permalink
feat(rust): bump smoltcp version in smoltcp transport to 0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
conectado committed Dec 14, 2021
1 parent e754914 commit 123f0a9
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 55 deletions.
15 changes: 5 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ ockam = { path = "../../../ockam", default_features = false, features = ["softwa
ockam_node = { path = "../../../ockam_node", default_features = false }
ockam_executor = { path = "../../../ockam_executor", default_features = false, optional = true }
ockam_transport_smoltcp = { path = "../../../ockam_transport_smoltcp", default_features = false }
smoltcp = "0.7.5"
smoltcp = "0.8"

# TODO: this dependency here is required because rustc doesn't yet
# support re-exporting attributes from crates. Tracking issue:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async fn main(mut ctx: Context) -> Result<()> {
&default_gateway,
// Note: This is quite uglyt but the `Clock` trait implementation for `smoltcp::time::Instant` uses `Instant::now` which is stateless
// So passing any instant struct is okay. (Maybe we should create a wrapper around `Instant` to hide this ugliness)
smoltcp::time::Instant { millis: 0 }
smoltcp::time::Instant::from_millis(0)
)
.await?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async fn main(ctx: Context) -> Result<()> {
bind_port,
// Note: This is quite uglyt but the `Clock` trait implementation for `smoltcp::time::Instant` uses `Instant::now` which is stateless
// So passing any instant struct is okay. (Maybe we should create a wrapper around `Instant` to hide this ugliness)
smoltcp::time::Instant { millis: 0 }
smoltcp::time::Instant::from_millis(0)
).await?;

// Create the responder worker
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion implementations/rust/ockam/ockam_transport_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ alloc = [
[dependencies]
ockam_core = { path = "../ockam_core", version = "^0.40.0", default_features = false }
tracing = { version = "0.1", default-features = false }
smoltcp = { version = "0.7", default-features = false, features = ["proto-ipv4"] }
smoltcp = { version = "0.8", default-features = false, features = ["proto-ipv4"] }
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ no_std = ["ockam_core/no_std", "ockam_node/no_std"]
alloc = ["smoltcp/alloc", "ockam_core/alloc", "ockam_node/alloc"]

[dependencies]
smoltcp = { version = "0.7", default_features = false, features = ["proto-ipv4", "proto-ipv6", "ethernet", "socket-tcp", "async"] }
smoltcp = { version = "0.8", default_features = false, features = ["proto-ipv4", "proto-ipv6", "medium-ethernet", "socket-tcp", "async"] }
ockam_core = { path = "../ockam_core", version = "0.40.0", default_features = false }
ockam_node = { path = "../ockam_node", version = "0.39.0", default_features = false }
tracing = { version = "0.1", default-features = false }
Expand Down
36 changes: 19 additions & 17 deletions implementations/rust/ockam/ockam_transport_smoltcp/src/net/stack.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use core::future::Future;
use core::task::Context;
use futures::pin_mut;
use smoltcp::iface::{EthernetInterface, EthernetInterfaceBuilder, NeighborCache, Routes};
use smoltcp::socket::{SocketHandle, SocketSet, TcpSocket, TcpSocketBuffer};
use smoltcp::iface::{
Context as SmolContext, Interface, InterfaceBuilder, NeighborCache, Routes, SocketHandle,
};
use smoltcp::socket::{TcpSocket, TcpSocketBuffer};
use smoltcp::time::Instant;
use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, IpEndpoint, Ipv4Address};
use smoltcp::Result;
Expand Down Expand Up @@ -37,15 +39,15 @@ fn create_iface<'a>(
ip: IpAddress,
routes: Routes<'a>,
device: Arc<Mutex<(dyn Device + Send + Sync)>>,
) -> EthernetInterface<'a, DeviceAdapter> {
) -> Interface<'a, DeviceAdapter> {
let ip_addrs = [IpCidr::new(ip, 24)]; // TODO: support multiple ips and different netmask
let neighbor_cache = NeighborCache::new(BTreeMap::new());
let eth_addr = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]);

let device = DeviceAdapter::new(device);

let iface = EthernetInterfaceBuilder::new(device)
.ethernet_addr(eth_addr)
let iface = InterfaceBuilder::new(device, vec![])
.hardware_addr(eth_addr.into())
.neighbor_cache(neighbor_cache)
.ip_addrs(ip_addrs)
.routes(routes)
Expand All @@ -67,9 +69,8 @@ fn get_routing_for_mode<'a>(mode: &TcpMode) -> Result<Routes<'a>> {
}

pub(crate) struct Stack {
socket_set: Mutex<SocketSet<'static>>,
ip: IpAddress,
iface: Mutex<EthernetInterface<'static, DeviceAdapter>>,
iface: Mutex<Interface<'static, DeviceAdapter>>,
waker: Mutex<Option<Waker>>,
}

Expand All @@ -79,22 +80,24 @@ impl Stack {
mode: TcpMode,
device: Arc<Mutex<dyn Device + Send + Sync>>,
) -> Arc<Self> {
let socket_set = Mutex::new(SocketSet::new(vec![]));
//let device = Arc::new(Mutex::new(TunTapDevice::new("tap0").unwrap()));
let routes = get_routing_for_mode(&mode).unwrap(); // TODO: don't unwrap
let iface = Mutex::new(create_iface(ip, routes, device));
Arc::new(Self {
socket_set,
ip,
iface,
waker: Mutex::new(None),
})
}

pub(crate) fn with<R>(&self, handle: SocketHandle, f: impl FnOnce(&mut TcpSocket) -> R) -> R {
let mut socket_set = self.socket_set.lock().unwrap();
let mut socket = &mut *socket_set.get(handle);
let res = f(&mut socket);
pub(crate) fn with<R>(
&self,
handle: SocketHandle,
f: impl FnOnce(&mut TcpSocket, &mut SmolContext) -> R,
) -> R {
let mut iface = self.iface.lock().unwrap();
let (mut socket, mut cx) = iface.get_socket_and_context(handle);
let res = f(&mut socket, &mut cx);
if let Some(waker) = &*self.waker.lock().unwrap() {
waker.wake_by_ref();
}
Expand All @@ -107,7 +110,7 @@ impl Stack {
let socket_tx_buff = TcpSocketBuffer::new(vec![0; 65535]);

let socket = TcpSocket::new(socket_rx_buff, socket_tx_buff);
self.socket_set.lock().unwrap().add(socket)
self.iface.lock().unwrap().add_socket(socket) // TODO: This can panic if out of buffer!
}

pub(crate) fn get_async_tcp_socket(self: &Arc<Self>) -> AsyncTcpSocket {
Expand All @@ -129,18 +132,17 @@ impl Stack {

let delay;
{
let mut socket_set = self.socket_set.lock().unwrap();
let mut iface = self.iface.lock().unwrap();
let timestamp = timestamp.into();
match iface.poll(&mut socket_set, timestamp) {
match iface.poll(timestamp) {
Ok(_) => {}
Err(e) => {
error!("poll error: {}", e);
cx.waker().wake_by_ref();
}
}

delay = iface.poll_delay(&socket_set, timestamp);
delay = iface.poll_delay(timestamp);
}

if let Some(delay) = delay {
Expand Down
33 changes: 18 additions & 15 deletions implementations/rust/ockam/ockam_transport_smoltcp/src/net/tcp.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::pin;
use core::task::Context;
use core::task::Poll;
use smoltcp::socket::SocketHandle;
use smoltcp::iface::SocketHandle;
use smoltcp::socket::TcpState;
use smoltcp::wire::IpEndpoint;
use tokio::io::ReadBuf;
Expand Down Expand Up @@ -31,26 +31,28 @@ impl AsyncTcpSocket {
}

pub(crate) fn remote_endpoint(&self) -> IpEndpoint {
self.stack.with(self.socket_handle, |s| s.remote_endpoint())
self.stack
.with(self.socket_handle, |s, _| s.remote_endpoint())
}

fn get_state(&self) -> TcpState {
self.stack.with(self.socket_handle, |s| s.state())
self.stack.with(self.socket_handle, |s, _| s.state())
}

fn register_send_waker(&self, cx: &mut Context) {
self.stack
.with(self.socket_handle, |s| s.register_recv_waker(cx.waker()));
.with(self.socket_handle, |s, _| s.register_recv_waker(cx.waker()));
}

fn register_recv_waker(&self, cx: &mut Context) {
self.stack
.with(self.socket_handle, |s| s.register_recv_waker(cx.waker()));
.with(self.socket_handle, |s, _| s.register_recv_waker(cx.waker()));
}

pub(crate) fn get_connection_status(&mut self) -> (bool, IpEndpoint) {
self.stack
.with(self.socket_handle, |s| (s.is_active(), s.remote_endpoint()))
self.stack.with(self.socket_handle, |s, _| {
(s.is_active(), s.remote_endpoint())
})
}

pub(crate) async fn connect<T, U>(
Expand All @@ -62,8 +64,8 @@ impl AsyncTcpSocket {
T: Into<IpEndpoint>,
U: Into<IpEndpoint>,
{
self.stack.with(self.socket_handle, |s| {
s.connect(remote_endpoint, local_endpoint)
self.stack.with(self.socket_handle, |s, cx| {
s.connect(cx, remote_endpoint, local_endpoint)
})?;

futures::future::poll_fn(|cx| match self.get_state() {
Expand All @@ -84,8 +86,9 @@ impl AsyncTcpSocket {
where
T: Into<IpEndpoint>,
{
self.stack
.with(self.socket_handle, |socket| socket.listen(local_endpoint))?;
self.stack.with(self.socket_handle, |socket, _| {
socket.listen(local_endpoint)
})?;

futures::future::poll_fn(|cx| match self.get_state() {
TcpState::Closed | TcpState::TimeWait => {
Expand Down Expand Up @@ -132,7 +135,7 @@ impl AsyncRead for AsyncTcpStream {
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
let recv_slice = self.inner.stack.with(self.inner.socket_handle, |s| {
let recv_slice = self.inner.stack.with(self.inner.socket_handle, |s, _| {
// TODO: We're initializing parts of the buffer that might not be used so this might be
// optimizable but will not focus on that optimization right now.
s.recv_slice(buf.initialize_unfilled())
Expand Down Expand Up @@ -164,7 +167,7 @@ impl AsyncWrite for AsyncTcpStream {
let send_slice = self
.inner
.stack
.with(self.inner.socket_handle, |s| s.send_slice(buf));
.with(self.inner.socket_handle, |s, _| s.send_slice(buf));
match send_slice {
Ok(0) => {
self.inner.register_send_waker(cx);
Expand All @@ -182,7 +185,7 @@ impl AsyncWrite for AsyncTcpStream {
let send_queue = self
.inner
.stack
.with(self.inner.socket_handle, |s| s.send_queue());
.with(self.inner.socket_handle, |s, _| s.send_queue());
if send_queue > 0 {
self.inner.register_send_waker(cx);
Poll::Pending
Expand All @@ -199,7 +202,7 @@ impl AsyncWrite for AsyncTcpStream {
// be emptied
self.inner
.stack
.with(self.inner.socket_handle, |s| s.close());
.with(self.inner.socket_handle, |s, _| s.close());
Poll::Ready(Ok(()))
}
}

0 comments on commit 123f0a9

Please sign in to comment.