Skip to content

Commit

Permalink
[skip ci] Continue implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sashacmc committed Mar 11, 2024
1 parent ac50437 commit a6b9f11
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 200 deletions.
62 changes: 40 additions & 22 deletions io/zenoh-links/zenoh-link-vsock/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
//!
//! [Click here for Zenoh's documentation](../zenoh/index.html)
use async_trait::async_trait;
use std::net::SocketAddr;
use tokio_vsock::{
VsockAddr, VMADDR_CID_ANY, VMADDR_CID_HOST, VMADDR_CID_HYPERVISOR, VMADDR_CID_LOCAL,
};
use zenoh_core::zconfigurable;
use zenoh_link_commons::LocatorInspector;
use zenoh_protocol::core::{endpoint::Address, Locator};
Expand All @@ -27,16 +29,13 @@ use zenoh_result::{zerror, ZResult};
mod unicast;
pub use unicast::*;

// Default MTU (TCP PDU) in bytes.
// NOTE: Since TCP is a byte-stream oriented transport, theoretically it has
// no limit regarding the MTU. However, given the batching strategy
// adopted in Zenoh and the usage of 16 bits in Zenoh to encode the
// payload length in byte-streamed, the TCP MTU is constrained to
// 2^16 - 1 bytes (i.e., 65535).
const TCP_MAX_MTU: u16 = u16::MAX;

pub const VSOCK_LOCATOR_PREFIX: &str = "vsock";

pub const VSOCK_ADDR_VMADDR_CID_ANY: &str = "VMADDR_CID_ANY";
pub const VSOCK_ADDR_VMADDR_CID_HYPERVISOR: &str = "VMADDR_CID_HYPERVISOR";
pub const VSOCK_ADDR_VMADDR_CID_LOCAL: &str = "VMADDR_CID_LOCAL";
pub const VSOCK_ADDR_VMADDR_CID_HOST: &str = "VMADDR_CID_HOST";

#[derive(Default, Clone, Copy)]
pub struct VsockLocatorInspector;
#[async_trait]
Expand All @@ -51,22 +50,41 @@ impl LocatorInspector for VsockLocatorInspector {
}

zconfigurable! {
// Default MTU (TCP PDU) in bytes.
static ref TCP_DEFAULT_MTU: u16 = TCP_MAX_MTU;
// The LINGER option causes the shutdown() call to block until (1) all application data is delivered
// to the remote end or (2) a timeout expires. The timeout is expressed in seconds.
// More info on the LINGER option and its dynamics can be found at:
// https://blog.netherlabs.nl/articles/2009/01/18/the-ultimate-so_linger-page-or-why-is-my-tcp-not-reliable
static ref TCP_LINGER_TIMEOUT: i32 = 10;
// Default MTU in bytes.
static ref VSOCK_DEFAULT_MTU: u16 = u16::MAX;
// Amount of time in microseconds to throttle the accept loop upon an error.
// Default set to 100 ms.
static ref TCP_ACCEPT_THROTTLE_TIME: u64 = 100_000;
}

pub async fn get_vsock_addrs(address: Address<'_>) -> ZResult<impl Iterator<Item = SocketAddr>> {
let iter = tokio::net::lookup_host(address.as_str().to_string())
.await
.map_err(|e| zerror!("{}", e))?
.filter(|x| !x.ip().is_multicast());
Ok(iter)
pub fn get_vsock_addr(address: Address<'_>) -> ZResult<VsockAddr> {
let parts: Vec<&str> = address.as_str().split(':').collect();

if parts.len() != 2 {
zerror!("Incorrect vsock address: {:?}", address);
}

let mut port = 0;
if let Ok(p) = parts[1].parse::<u32>() {
port = p;
} else {
zerror!("Incorrect vsock port: {:?}", parts[1]);
}

let cid = match parts[0] {
VSOCK_ADDR_VMADDR_CID_ANY => VMADDR_CID_ANY,
VSOCK_ADDR_VMADDR_CID_HYPERVISOR => VMADDR_CID_HYPERVISOR,
VSOCK_ADDR_VMADDR_CID_HOST => VMADDR_CID_HOST,
VSOCK_ADDR_VMADDR_CID_LOCAL => VMADDR_CID_LOCAL,
_ => {
if let Ok(cid) = parts[0].parse::<u32>() {
cid
} else {
zerror!("Incorrect vsock cid: {:?}", parts[0]);
0
}
}
};

Ok(VsockAddr::new(cid, port))
}
Loading

0 comments on commit a6b9f11

Please sign in to comment.