Skip to main content

ns3_config/
config_lib_rust_config_lib_source.rs

1use fidl::unpersist;
2use fidl_cf_sc_internal_ns3config::Config as FidlConfig;
3use fuchsia_component_config::{Config as ComponentConfig, Error};
4use fuchsia_inspect::Node;
5use std::convert::TryInto;
6const EXPECTED_CHECKSUM: &[u8] = &[
7    0x85, 0x6a, 0xe5, 0x70, 0x54, 0x7c, 0xfa, 0x89, 0xd4, 0x72, 0x23, 0x1d, 0x1d, 0xcb, 0xbb, 0x65,
8    0x96, 0x04, 0xc4, 0x5a, 0x46, 0x5d, 0x63, 0x40, 0xd4, 0x60, 0x08, 0xc1, 0xd5, 0x6d, 0xe4, 0xe3,
9];
10const EXPECTED_CHECKSUM_LENGTH: [u8; 2] = (EXPECTED_CHECKSUM.len() as u16).to_le_bytes();
11#[derive(Debug)]
12pub struct Config {
13    pub debug_logs: bool,
14    pub max_rolling_capture_buffer_size: u32,
15    pub multi_vmo: bool,
16    pub num_threads: u8,
17    pub opaque_iids: bool,
18    pub sampled_stats_enabled: bool,
19    pub suspend_enabled: bool,
20    pub tcp_gro_enabled: bool,
21}
22impl Config {
23    #[doc = r" Take the config startup handle and parse its contents."]
24    #[doc = r""]
25    #[doc = r" # Panics"]
26    #[doc = r""]
27    #[doc = r" If the config startup handle was already taken or if it is not valid."]
28    pub fn take_from_startup_handle() -> Self {
29        <Self as ComponentConfig>::take_from_startup_handle()
30    }
31    #[doc = r" Parse `Self` from `vmo`."]
32    pub fn from_vmo(vmo: &zx::Vmo) -> Result<Self, Error> {
33        <Self as ComponentConfig>::from_vmo(vmo)
34    }
35    #[doc = r" Parse `Self` from `bytes`."]
36    pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
37        <Self as ComponentConfig>::from_bytes(bytes)
38    }
39    pub fn record_inspect(&self, inspector_node: &Node) {
40        <Self as ComponentConfig>::record_inspect(self, inspector_node)
41    }
42}
43impl ComponentConfig for Config {
44    #[doc = r" Parse `Self` from `bytes`."]
45    fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
46        let (checksum_len_bytes, bytes) = bytes.split_at_checked(2).ok_or(Error::TooFewBytes)?;
47        let checksum_len_bytes: [u8; 2] =
48            checksum_len_bytes.try_into().expect("previous call guaranteed 2 element slice");
49        let checksum_length = u16::from_le_bytes(checksum_len_bytes) as usize;
50        let (observed_checksum, bytes) =
51            bytes.split_at_checked(checksum_length).ok_or(Error::TooFewBytes)?;
52        if observed_checksum != EXPECTED_CHECKSUM {
53            return Err(Error::ChecksumMismatch {
54                expected_checksum: EXPECTED_CHECKSUM.to_vec(),
55                observed_checksum: observed_checksum.to_vec(),
56            });
57        }
58        let fidl_config: FidlConfig = unpersist(bytes).map_err(Error::Unpersist)?;
59        Ok(Self {
60            debug_logs: fidl_config.debug_logs,
61            max_rolling_capture_buffer_size: fidl_config.max_rolling_capture_buffer_size,
62            multi_vmo: fidl_config.multi_vmo,
63            num_threads: fidl_config.num_threads,
64            opaque_iids: fidl_config.opaque_iids,
65            sampled_stats_enabled: fidl_config.sampled_stats_enabled,
66            suspend_enabled: fidl_config.suspend_enabled,
67            tcp_gro_enabled: fidl_config.tcp_gro_enabled,
68        })
69    }
70    fn to_bytes(&self) -> Result<Vec<u8>, Error> {
71        let fidl_config = FidlConfig {
72            debug_logs: self.debug_logs.clone(),
73            max_rolling_capture_buffer_size: self.max_rolling_capture_buffer_size.clone(),
74            multi_vmo: self.multi_vmo.clone(),
75            num_threads: self.num_threads.clone(),
76            opaque_iids: self.opaque_iids.clone(),
77            sampled_stats_enabled: self.sampled_stats_enabled.clone(),
78            suspend_enabled: self.suspend_enabled.clone(),
79            tcp_gro_enabled: self.tcp_gro_enabled.clone(),
80        };
81        let mut fidl_bytes = fidl::persist(&fidl_config).map_err(Error::Persist)?;
82        let mut bytes = Vec::with_capacity(
83            EXPECTED_CHECKSUM_LENGTH.len() + EXPECTED_CHECKSUM.len() + fidl_bytes.len(),
84        );
85        bytes.extend_from_slice(&EXPECTED_CHECKSUM_LENGTH);
86        bytes.extend_from_slice(EXPECTED_CHECKSUM);
87        bytes.append(&mut fidl_bytes);
88        Ok(bytes)
89    }
90    fn record_inspect(&self, inspector_node: &Node) {
91        inspector_node.record_bool("debug_logs", self.debug_logs);
92        inspector_node.record_uint(
93            "max_rolling_capture_buffer_size",
94            self.max_rolling_capture_buffer_size as u64,
95        );
96        inspector_node.record_bool("multi_vmo", self.multi_vmo);
97        inspector_node.record_uint("num_threads", self.num_threads as u64);
98        inspector_node.record_bool("opaque_iids", self.opaque_iids);
99        inspector_node.record_bool("sampled_stats_enabled", self.sampled_stats_enabled);
100        inspector_node.record_bool("suspend_enabled", self.suspend_enabled);
101        inspector_node.record_bool("tcp_gro_enabled", self.tcp_gro_enabled);
102    }
103}