|
| 1 | +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use std::fmt; |
| 5 | +use std::os::fd::RawFd; |
| 6 | +use std::result; |
| 7 | +use std::sync::{Arc, Mutex}; |
| 8 | + |
| 9 | +use devices::virtio::Net; |
| 10 | + |
| 11 | +#[derive(Debug, PartialEq)] |
| 12 | +//#[serde(deny_unknown_fields)] |
| 13 | +pub struct NetworkInterfaceConfig { |
| 14 | + /// ID of the guest network interface. |
| 15 | + pub iface_id: String, |
| 16 | + /// File descriptor of passt socket to connect this interface to |
| 17 | + pub passt_fd: RawFd, |
| 18 | +} |
| 19 | + |
| 20 | +/// Errors associated with `NetworkInterfaceConfig`. |
| 21 | +#[derive(Debug)] |
| 22 | +pub enum NetworkInterfaceError { |
| 23 | + /// Could not create Network Device. |
| 24 | + CreateNetworkDevice(devices::virtio::net::Error), |
| 25 | + /// Couldn't find the interface to update (patch). |
| 26 | + DeviceIdNotFound, |
| 27 | +} |
| 28 | + |
| 29 | +impl fmt::Display for NetworkInterfaceError { |
| 30 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 31 | + use self::NetworkInterfaceError::*; |
| 32 | + match *self { |
| 33 | + CreateNetworkDevice(ref e) => write!(f, "Could not create Network Device: {:?}", e), |
| 34 | + DeviceIdNotFound => write!(f, "Invalid interface ID - not found."), |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +type Result<T> = result::Result<T, NetworkInterfaceError>; |
| 40 | + |
| 41 | +/// Builder for a list of network devices. |
| 42 | +#[derive(Default)] |
| 43 | +pub struct NetBuilder { |
| 44 | + net_devices: Vec<Arc<Mutex<Net>>>, |
| 45 | +} |
| 46 | + |
| 47 | +impl NetBuilder { |
| 48 | + /// Creates an empty list of Network Devices. |
| 49 | + pub fn new() -> Self { |
| 50 | + NetBuilder { |
| 51 | + /// List of built network devices. |
| 52 | + net_devices: Vec::new(), |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /// Returns a immutable iterator over the network devices. |
| 57 | + pub fn iter(&self) -> ::std::slice::Iter<Arc<Mutex<Net>>> { |
| 58 | + self.net_devices.iter() |
| 59 | + } |
| 60 | + |
| 61 | + /// Returns a mutable iterator over the network devices. |
| 62 | + pub fn iter_mut(&mut self) -> ::std::slice::IterMut<Arc<Mutex<Net>>> { |
| 63 | + self.net_devices.iter_mut() |
| 64 | + } |
| 65 | + |
| 66 | + /// Builds a network device based on a network interface config. Keeps a device reference |
| 67 | + /// in the builder's internal list. |
| 68 | + pub fn build(&mut self, netif_config: NetworkInterfaceConfig) -> Result<Arc<Mutex<Net>>> { |
| 69 | + // If this is an update, just remove the old one. |
| 70 | + if let Some(index) = self |
| 71 | + .net_devices |
| 72 | + .iter() |
| 73 | + .position(|net| net.lock().expect("Poisoned lock").id() == netif_config.iface_id) |
| 74 | + { |
| 75 | + self.net_devices.swap_remove(index); |
| 76 | + } |
| 77 | + |
| 78 | + // Add new device. |
| 79 | + let net = Arc::new(Mutex::new(Self::create_net(netif_config)?)); |
| 80 | + self.net_devices.push(net.clone()); |
| 81 | + |
| 82 | + Ok(net) |
| 83 | + } |
| 84 | + |
| 85 | + /// Creates a Net device from a NetworkInterfaceConfig. |
| 86 | + pub fn create_net(cfg: NetworkInterfaceConfig) -> Result<Net> { |
| 87 | + // Create and return the Net device |
| 88 | + Net::new(cfg.iface_id, cfg.passt_fd).map_err(NetworkInterfaceError::CreateNetworkDevice) |
| 89 | + } |
| 90 | +} |
0 commit comments