xref: /aosp_15_r20/external/crosvm/net_util/src/slirp.rs (revision bb4ee6a4ae7042d18b07a98463b9c8b875e44b39)
1 // Copyright 2022 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 //! Contains the Rust implementation of the libslirp consumer main loop, high
6 //! level interfaces to libslirp that are used to implement that loop, and
7 //! diagnostic tools.
8 
9 #![cfg(windows)]
10 
11 #[path = "../../third_party/libslirp-rs/src/context.rs"]
12 pub mod context;
13 
14 #[cfg(feature = "slirp-ring-capture")]
15 pub mod packet_ring_buffer;
16 
17 pub mod sys;
18 use base::Error as SysError;
19 use remain::sorted;
20 pub use sys::Slirp;
21 use thiserror::Error as ThisError;
22 
23 /// Length includes space for an ethernet frame & the vnet header. See the virtio spec for details:
24 /// <http://docs.oasis-open.org/virtio/virtio/v1.1/csprd01/virtio-v1.1-csprd01.html#x1-2050006>
25 pub const ETHERNET_FRAME_SIZE: usize = 1526;
26 
27 #[cfg(windows)]
28 #[sorted]
29 #[derive(ThisError, Debug)]
30 pub enum SlirpError {
31     #[error("pipe was closed: {0}")]
32     BrokenPipe(std::io::Error),
33     #[error("failed to clone object: {0}")]
34     CloneFailed(std::io::Error),
35     #[error("overlapped operation failed: {0}")]
36     OverlappedError(std::io::Error),
37     /// Error encountered while in a Slirp related poll operation.
38     #[error("slirp poll failed: {0}")]
39     SlirpIOPollError(std::io::Error),
40     /// Error encountered while in a Slirp related poll operation.
41     #[error("slirp poll failed: {0}")]
42     SlirpPollError(SysError),
43     #[error("WSAStartup failed with code: {0}")]
44     WSAStartupError(SysError),
45 }
46 
47 #[cfg(windows)]
48 impl SlirpError {
sys_error(&self) -> SysError49     pub fn sys_error(&self) -> SysError {
50         match self {
51             SlirpError::BrokenPipe(e) => SysError::new(e.raw_os_error().unwrap_or_default()),
52             SlirpError::CloneFailed(e) => SysError::new(e.raw_os_error().unwrap_or_default()),
53             SlirpError::OverlappedError(e) => SysError::new(e.raw_os_error().unwrap_or_default()),
54             SlirpError::SlirpIOPollError(e) => SysError::new(e.raw_os_error().unwrap_or_default()),
55             SlirpError::SlirpPollError(e) => *e,
56             SlirpError::WSAStartupError(e) => *e,
57         }
58     }
59 }
60