Skip to content

Commit

Permalink
feat(rust): use static ref for device in smoltcp device adapter
Browse files Browse the repository at this point in the history
Instead of using `Arc` this uses an static ref as a change towards not
depending on alloc
  • Loading branch information
conectado committed Dec 17, 2021
1 parent e2caee1 commit a7afe28
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 234 deletions.
8 changes: 5 additions & 3 deletions implementations/rust/ockam/ockam_core/src/compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,22 +190,24 @@ pub mod sync {
pub use spin::RwLock;

/// spin::Mutex.lock() does not return Option<T>
pub struct Mutex<T>(spin::Mutex<T>);
pub struct Mutex<T: ?Sized>(spin::Mutex<T>);
impl<T> Mutex<T> {
pub fn new(value: T) -> Self {
Mutex(spin::Mutex::new(value))
}
}
impl<T: ?Sized> Mutex<T> {
pub fn lock(&self) -> Option<spin::MutexGuard<'_, T>> {
Some(self.0.lock())
}
}
impl<T> core::ops::Deref for Mutex<T> {
impl<T: ?Sized> core::ops::Deref for Mutex<T> {
type Target = spin::Mutex<T>;
fn deref(&self) -> &spin::Mutex<T> {
&self.0
}
}
impl<T> core::ops::DerefMut for Mutex<T> {
impl<T: ?Sized> core::ops::DerefMut for Mutex<T> {
fn deref_mut(&mut self) -> &mut spin::Mutex<T> {
&mut self.0
}
Expand Down

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 @@ -35,9 +35,9 @@ alloc = [
[dependencies]
ockam = { path = "../../../ockam", default_features = false, features = ["software_vault"] }
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.8"
lazy_static = "1.4"

# 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 @@ -15,6 +15,10 @@ fn get_peer_addr() -> (String, String) {
)
}

lazy_static! {
static ref DEVICE: Mutex<TunTapDevice> = Mutex::new(TunTapDevice::new("tap0").unwrap());
}

#[ockam::node]
async fn main(mut ctx: Context) -> Result<()> {
let (peer_addr, bind_ip_addr) = get_peer_addr();
Expand All @@ -34,9 +38,8 @@ async fn main(mut ctx: Context) -> Result<()> {

// Initialize the TCP stack by opening a connection to a the remote
let tcp = SmolTcpTransport::create(&ctx).await?;
let device = Arc::new(Mutex::new(TunTapDevice::new("tap0").unwrap()));
tcp.connect(
device,
&*DEVICE,
&bind_ip_addr,
bind_port,
&peer_ip_addr,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ extern crate tracing;

use ockam::{Context, Result, Routed, Worker};
use ockam_transport_smoltcp::{SmolTcpTransport, TunTapDevice};
use ockam::compat::sync::{Arc, Mutex};
use ockam::compat::sync::Mutex;
use lazy_static::lazy_static;

struct Responder;

Expand All @@ -32,6 +33,10 @@ fn get_bind_addr() -> String {
.unwrap_or(format!("192.168.69.1:10222"))
}

lazy_static! {
static ref DEVICE: Mutex<TunTapDevice> = Mutex::new(TunTapDevice::new("tap0").unwrap());
}

#[ockam::node]
async fn main(ctx: Context) -> Result<()> {
// Get either the default socket address, or a user-input
Expand All @@ -45,9 +50,8 @@ async fn main(ctx: Context) -> Result<()> {
};

let tcp = SmolTcpTransport::create(&ctx).await?;
let device = Arc::new(Mutex::new(TunTapDevice::new("tap0").unwrap()));
tcp.listen(
device,
&*DEVICE,
bind_ip_addr,
bind_port,
// Note: This is quite uglyt but the `Clock` trait implementation for `smoltcp::time::Instant` uses `Instant::now` which is stateless
Expand Down
Loading

0 comments on commit a7afe28

Please sign in to comment.