Skip to main content

net_backend_resources/
lib.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Resource definitions for network backends (endpoints).
5//!
6//! TODO: move the resource definitions to separate crates for each endpoint.
7
8#![forbid(unsafe_code)]
9
10pub mod mac_address;
11
12/// Null backend.
13pub mod null {
14    use mesh::MeshPayload;
15    use vm_resource::ResourceId;
16    use vm_resource::kind::NetEndpointHandleKind;
17
18    /// Handle to a null network endpoint, which drops sent packets and never
19    /// receives packets.
20    #[derive(MeshPayload)]
21    pub struct NullHandle;
22
23    impl ResourceId<NetEndpointHandleKind> for NullHandle {
24        const ID: &'static str = "null";
25    }
26}
27
28/// Consomme backend.
29pub mod consomme {
30    use mesh::MeshPayload;
31    use vm_resource::ResourceId;
32    use vm_resource::kind::NetEndpointHandleKind;
33
34    /// Protocol for host port forwarding.
35    #[derive(Clone, Debug, MeshPayload)]
36    pub enum HostPortProtocol {
37        /// TCP protocol.
38        Tcp,
39        /// UDP protocol.
40        Udp,
41    }
42
43    /// An IP address, suitable for serialization via mesh.
44    #[derive(Clone, Debug, MeshPayload)]
45    pub enum HostIpAddress {
46        /// IPv4 address.
47        Ipv4(std::net::Ipv4Addr),
48        /// IPv6 address.
49        Ipv6(std::net::Ipv6Addr),
50    }
51
52    impl From<std::net::IpAddr> for HostIpAddress {
53        fn from(addr: std::net::IpAddr) -> Self {
54            match addr {
55                std::net::IpAddr::V4(v4) => HostIpAddress::Ipv4(v4),
56                std::net::IpAddr::V6(v6) => HostIpAddress::Ipv6(v6),
57            }
58        }
59    }
60
61    impl From<HostIpAddress> for std::net::IpAddr {
62        fn from(addr: HostIpAddress) -> Self {
63            match addr {
64                HostIpAddress::Ipv4(v4) => std::net::IpAddr::V4(v4),
65                HostIpAddress::Ipv6(v6) => std::net::IpAddr::V6(v6),
66            }
67        }
68    }
69
70    /// The host port to listen on for a port forward.
71    #[derive(Debug, MeshPayload)]
72    pub enum HostPort {
73        /// A fixed host port.
74        Fixed(u16),
75        /// Let the OS assign a port. The assigned port is sent back via the
76        /// oneshot sender.
77        Dynamic(mesh::OneshotSender<u16>),
78    }
79
80    /// Configuration for forwarding a host port into the guest.
81    #[derive(Debug, MeshPayload)]
82    pub struct HostPortConfig {
83        /// The protocol to forward.
84        pub protocol: HostPortProtocol,
85        /// The host IP address to bind to, or `None` to bind to all interfaces.
86        pub host_address: Option<HostIpAddress>,
87        /// The host port to listen on.
88        pub host_port: HostPort,
89        /// The guest port to forward to.
90        pub guest_port: u16,
91    }
92
93    /// A runtime request to bind or unbind a port on a running Consomme endpoint.
94    #[derive(MeshPayload)]
95    pub enum ConsommeRequest {
96        /// Bind a host port to forward traffic to the guest.
97        Bind(mesh::rpc::FailableRpc<HostPortConfig, ()>),
98        /// Unbind a previously forwarded port.
99        Unbind(mesh::rpc::FailableRpc<HostPortConfig, ()>),
100    }
101
102    /// Handle to a Consomme network endpoint.
103    #[derive(MeshPayload)]
104    pub struct ConsommeHandle {
105        /// The CIDR of the network to use.
106        pub cidr: Option<String>,
107        /// Ports to forward from the host into the guest at creation time.
108        pub ports: Vec<HostPortConfig>,
109        /// Optional channel for runtime port bind/unbind after the endpoint starts.
110        pub recv: Option<mesh::Receiver<ConsommeRequest>>,
111    }
112
113    impl ResourceId<NetEndpointHandleKind> for ConsommeHandle {
114        const ID: &'static str = "consomme";
115    }
116}
117
118/// Windows vmswitch DirectIO backend.
119pub mod dio {
120    use guid::Guid;
121    use mesh::MeshPayload;
122    use vm_resource::ResourceId;
123    use vm_resource::kind::NetEndpointHandleKind;
124
125    /// A Hyper-V networking switch port ID.
126    #[derive(Copy, Clone, MeshPayload)]
127    pub struct SwitchPortId {
128        /// The switch ID.
129        pub switch: Guid,
130        /// The allocated port ID.
131        pub port: Guid,
132    }
133
134    /// Handle to a DirectIO network endpoint.
135    #[derive(MeshPayload)]
136    pub struct WindowsDirectIoHandle {
137        /// The allocated switch port ID.
138        pub switch_port_id: SwitchPortId,
139    }
140
141    impl ResourceId<NetEndpointHandleKind> for WindowsDirectIoHandle {
142        const ID: &'static str = "dio";
143    }
144}
145
146/// Linux TAP backend.
147#[cfg(target_os = "linux")]
148pub mod tap {
149    use mesh::MeshPayload;
150    use vm_resource::ResourceId;
151    use vm_resource::kind::NetEndpointHandleKind;
152
153    /// A handle to a TAP device.
154    #[derive(MeshPayload)]
155    pub struct TapHandle {
156        /// A pre-opened TAP file descriptor, configured with
157        /// `IFF_TAP | IFF_NO_PI | IFF_VNET_HDR`.
158        pub fd: std::os::fd::OwnedFd,
159    }
160
161    impl ResourceId<NetEndpointHandleKind> for TapHandle {
162        const ID: &'static str = "tap";
163    }
164}