zenoh_link_vsock/
lib.rs

1//
2// Copyright (c) 2024 ZettaScale Technology
3//
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7// which is available at https://www.apache.org/licenses/LICENSE-2.0.
8//
9// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10//
11// Contributors:
12//   ZettaScale Zenoh Team, <zenoh@zettascale.tech>
13//
14
15//! ⚠️ WARNING ⚠️
16//!
17//! This crate is intended for Zenoh's internal use.
18//!
19//! [Click here for Zenoh's documentation](https://docs.rs/zenoh/latest/zenoh)
20//!
21//! Implements [vsock](https://man7.org/linux/man-pages/man7/vsock.7.html) link support.
22use std::str::FromStr;
23
24use async_trait::async_trait;
25use zenoh_core::zconfigurable;
26use zenoh_link_commons::LocatorInspector;
27use zenoh_protocol::{
28    core::{Locator, Metadata, Reliability},
29    transport::BatchSize,
30};
31use zenoh_result::ZResult;
32
33#[cfg(target_os = "linux")]
34mod unicast;
35#[cfg(target_os = "linux")]
36pub use unicast::*;
37
38pub const VSOCK_LOCATOR_PREFIX: &str = "vsock";
39
40const IS_RELIABLE: bool = true;
41
42#[derive(Default, Clone, Copy)]
43pub struct VsockLocatorInspector;
44#[async_trait]
45impl LocatorInspector for VsockLocatorInspector {
46    fn protocol(&self) -> &str {
47        VSOCK_LOCATOR_PREFIX
48    }
49
50    async fn is_multicast(&self, _locator: &Locator) -> ZResult<bool> {
51        Ok(false)
52    }
53
54    fn is_reliable(&self, locator: &Locator) -> ZResult<bool> {
55        if let Some(reliability) = locator
56            .metadata()
57            .get(Metadata::RELIABILITY)
58            .map(Reliability::from_str)
59            .transpose()?
60        {
61            Ok(reliability == Reliability::Reliable)
62        } else {
63            Ok(IS_RELIABLE)
64        }
65    }
66}
67
68zconfigurable! {
69    // Default MTU in bytes.
70    static ref VSOCK_DEFAULT_MTU: BatchSize = BatchSize::MAX;
71    // Amount of time in microseconds to throttle the accept loop upon an error.
72    // Default set to 100 ms.
73    static ref VSOCK_ACCEPT_THROTTLE_TIME: u64 = 100_000;
74}