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    /// Configuration for forwarding a host port into the guest.
71    #[derive(Clone, Debug, MeshPayload)]
72    pub struct HostPortConfig {
73        /// The protocol to forward.
74        pub protocol: HostPortProtocol,
75        /// The host IP address to bind to, or `None` to bind to all interfaces.
76        pub host_address: Option<HostIpAddress>,
77        /// The host port to listen on.
78        pub host_port: u16,
79        /// The guest port to forward to.
80        pub guest_port: u16,
81    }
82
83    /// Handle to a Consomme network endpoint.
84    #[derive(MeshPayload)]
85    pub struct ConsommeHandle {
86        /// The CIDR of the network to use.
87        pub cidr: Option<String>,
88        /// Ports to forward from the host into the guest.
89        pub ports: Vec<HostPortConfig>,
90    }
91
92    impl ResourceId<NetEndpointHandleKind> for ConsommeHandle {
93        const ID: &'static str = "consomme";
94    }
95}
96
97/// Windows vmswitch DirectIO backend.
98pub mod dio {
99    use guid::Guid;
100    use mesh::MeshPayload;
101    use vm_resource::ResourceId;
102    use vm_resource::kind::NetEndpointHandleKind;
103
104    /// A Hyper-V networking switch port ID.
105    #[derive(Copy, Clone, MeshPayload)]
106    pub struct SwitchPortId {
107        /// The switch ID.
108        pub switch: Guid,
109        /// The allocated port ID.
110        pub port: Guid,
111    }
112
113    /// Handle to a DirectIO network endpoint.
114    #[derive(MeshPayload)]
115    pub struct WindowsDirectIoHandle {
116        /// The allocated switch port ID.
117        pub switch_port_id: SwitchPortId,
118    }
119
120    impl ResourceId<NetEndpointHandleKind> for WindowsDirectIoHandle {
121        const ID: &'static str = "dio";
122    }
123}
124
125/// Linux TAP backend.
126#[cfg(target_os = "linux")]
127pub mod tap {
128    use mesh::MeshPayload;
129    use vm_resource::ResourceId;
130    use vm_resource::kind::NetEndpointHandleKind;
131
132    /// A handle to a TAP device.
133    #[derive(MeshPayload)]
134    pub struct TapHandle {
135        /// A pre-opened TAP file descriptor, configured with
136        /// `IFF_TAP | IFF_NO_PI | IFF_VNET_HDR`.
137        pub fd: std::os::fd::OwnedFd,
138    }
139
140    impl ResourceId<NetEndpointHandleKind> for TapHandle {
141        const ID: &'static str = "tap";
142    }
143}