vmm/vmm_config/
mmds.rs

1// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3use std::net::Ipv4Addr;
4
5use serde::{Deserialize, Serialize};
6
7use crate::mmds::data_store;
8use crate::mmds::data_store::MmdsVersion;
9
10/// Keeps the MMDS configuration.
11#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
12#[serde(deny_unknown_fields)]
13pub struct MmdsConfig {
14    /// MMDS version.
15    #[serde(default)]
16    pub version: MmdsVersion,
17    /// Network interfaces that allow forwarding packets to MMDS.
18    pub network_interfaces: Vec<String>,
19    /// MMDS IPv4 configured address.
20    pub ipv4_address: Option<Ipv4Addr>,
21    /// Compatibility with EC2 IMDS.
22    #[serde(default)]
23    pub imds_compat: bool,
24}
25
26impl MmdsConfig {
27    /// Returns the MMDS version configured.
28    pub fn version(&self) -> MmdsVersion {
29        self.version
30    }
31
32    /// Returns the network interfaces that accept MMDS requests.
33    pub fn network_interfaces(&self) -> Vec<String> {
34        self.network_interfaces.clone()
35    }
36
37    /// Returns the MMDS IPv4 address if one was configured.
38    /// Otherwise returns None.
39    pub fn ipv4_addr(&self) -> Option<Ipv4Addr> {
40        self.ipv4_address
41    }
42}
43
44/// MMDS configuration related errors.
45#[rustfmt::skip]
46#[derive(Debug, thiserror::Error, displaydoc::Display)]
47pub enum MmdsConfigError {
48    /// The list of network interface IDs that allow forwarding MMDS requests is empty.
49    EmptyNetworkIfaceList,
50    /// The MMDS IPv4 address is not link local.
51    InvalidIpv4Addr,
52    /// The list of network interface IDs provided contains at least one ID that does not correspond to any existing network interface.
53    InvalidNetworkInterfaceId,
54    /// Failed to initialize MMDS data store: {0}
55    InitMmdsDatastore(#[from] data_store::MmdsDatastoreError),
56}