Skip to content

Commit 4ba235f

Browse files
mtjhrcslp
authored andcommitted
[3/4] Introduce dummy Net device and NetBuilder
The actual virtio-net device will be implemented in the next commit. Signed-off-by: Matej Hrica <[email protected]>
1 parent 32d7633 commit 4ba235f

File tree

7 files changed

+157
-2
lines changed

7 files changed

+157
-2
lines changed

src/devices/src/virtio/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub mod fs;
2121
#[cfg(target_os = "macos")]
2222
pub mod linux_errno;
2323
mod mmio;
24+
pub mod net;
2425
mod queue;
2526
#[cfg(not(feature = "tee"))]
2627
pub mod rng;
@@ -35,6 +36,7 @@ pub use self::device::*;
3536
#[cfg(not(feature = "tee"))]
3637
pub use self::fs::*;
3738
pub use self::mmio::*;
39+
pub use self::net::*;
3840
pub use self::queue::*;
3941
#[cfg(not(feature = "tee"))]
4042
pub use self::rng::*;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
//
4+
// Portions Copyright 2017 The Chromium OS Authors. All rights reserved.
5+
// Use of this source code is governed by a BSD-style license that can be
6+
// found in the THIRD-PARTY file.
7+
use crate::virtio::net::Result;
8+
use std::os::fd::RawFd;
9+
10+
pub struct Net {
11+
id: String,
12+
}
13+
14+
impl Net {
15+
/// Create a new virtio network device using passt
16+
pub fn new(id: String, _passt_fd: RawFd) -> Result<Self> {
17+
Ok(Net { id })
18+
}
19+
20+
/// Provides the ID of this net device.
21+
pub fn id(&self) -> &str {
22+
&self.id
23+
}
24+
}

src/devices/src/virtio/net/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
use std::{io, result};
5+
6+
pub mod device;
7+
8+
pub use self::device::Net;
9+
10+
#[derive(Debug)]
11+
pub enum Error {
12+
/// EventFd error.
13+
EventFd(io::Error),
14+
}
15+
16+
pub type Result<T> = result::Result<T, Error>;

src/libkrun/src/lib.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use vmm::vmm_config::kernel_bundle::KernelBundle;
3333
#[cfg(feature = "tee")]
3434
use vmm::vmm_config::kernel_bundle::{InitrdBundle, QbootBundle};
3535
use vmm::vmm_config::machine_config::VmConfig;
36+
use vmm::vmm_config::net::NetworkInterfaceConfig;
3637
use vmm::vmm_config::vsock::VsockDeviceConfig;
3738

3839
// Minimum krunfw version we require.
@@ -820,7 +821,14 @@ pub extern "C" fn krun_start_enter(ctx_id: u32) -> i32 {
820821
ctx_cfg.vmr.set_vsock_device(vsock_device_config).unwrap();
821822
}
822823
NetworkConfig::Passt(passt_cfg) => {
823-
todo!("Connect to fd {} and implement networking", passt_cfg.fd)
824+
let network_interface_config = NetworkInterfaceConfig {
825+
iface_id: "eth0".to_string(),
826+
passt_fd: passt_cfg.fd,
827+
};
828+
ctx_cfg
829+
.vmr
830+
.add_network_interface(network_interface_config)
831+
.expect("Failed to create network interface");
824832
}
825833
}
826834

src/vmm/src/resources.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use crate::vmm_config::fs::*;
2525
use crate::vmm_config::kernel_bundle::{InitrdBundle, QbootBundle, QbootBundleError};
2626
use crate::vmm_config::kernel_bundle::{KernelBundle, KernelBundleError};
2727
use crate::vmm_config::machine_config::{VmConfig, VmConfigError};
28+
use crate::vmm_config::net::{NetBuilder, NetworkInterfaceConfig, NetworkInterfaceError};
2829
use crate::vmm_config::vsock::*;
2930
use crate::vstate::VcpuConfig;
3031

@@ -101,7 +102,8 @@ pub struct VmResources {
101102
/// The virtio-blk device.
102103
#[cfg(feature = "tee")]
103104
pub block: BlockBuilder,
104-
105+
/// The network devices builder.
106+
pub net_builder: NetBuilder,
105107
/// TEE configuration
106108
#[cfg(feature = "tee")]
107109
pub tee_config: TeeConfig,
@@ -238,6 +240,15 @@ impl VmResources {
238240
self.vsock.insert(config)
239241
}
240242

243+
/// Sets a network device to be attached when the VM starts.
244+
pub fn add_network_interface(
245+
&mut self,
246+
config: NetworkInterfaceConfig,
247+
) -> Result<NetworkInterfaceError> {
248+
self.net_builder.build(config)?;
249+
Ok(())
250+
}
251+
241252
#[cfg(feature = "tee")]
242253
pub fn tee_config(&self) -> &TeeConfig {
243254
&self.tee_config
@@ -288,6 +299,7 @@ mod tests {
288299
kernel_bundle: Default::default(),
289300
fs: Default::default(),
290301
vsock: Default::default(),
302+
net_builder: Default::default(),
291303
}
292304
}
293305

src/vmm/src/vmm_config/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ pub mod machine_config;
2323

2424
/// Wrapper for configuring the vsock devices attached to the microVM.
2525
pub mod vsock;
26+
27+
/// Wrapper for configuring the network devices attached to the microVM.
28+
pub mod net;

src/vmm/src/vmm_config/net.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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

Comments
 (0)