vmm: Leverage virtio-net to provide connectivity

This patch expand the device registration to add a new virtio-net
device in case the user provide the appropriate flag --net from the
command line.

If the flag is provided, the code will parse the TAP interface name
and the expected MAC address from the command line. The VM will be
connected to the provided TAP interface, and it will communicate the
MAC address to the virtio-net driver.

If the flag is not provided, the VM will not register any virtio-net
device, therefore it will not have any connectivity with the host.

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf 2019-05-09 08:01:42 -07:00 committed by Samuel Ortiz
parent 53f5295454
commit c0be6642ad
4 changed files with 91 additions and 5 deletions

1
Cargo.lock generated
View File

@ -460,6 +460,7 @@ dependencies = [
"kvm-ioctls 0.1.0 (git+https://github.com/rust-vmm/kvm-ioctls)", "kvm-ioctls 0.1.0 (git+https://github.com/rust-vmm/kvm-ioctls)",
"libc 0.2.54 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.54 (registry+https://github.com/rust-lang/crates.io-index)",
"linux-loader 0.1.0 (git+https://github.com/sameo/linux-loader)", "linux-loader 0.1.0 (git+https://github.com/sameo/linux-loader)",
"net_util 0.1.0",
"pci 0.1.0", "pci 0.1.0",
"qcow 0.1.0", "qcow 0.1.0",
"vm-allocator 0.1.0", "vm-allocator 0.1.0",

View File

@ -37,6 +37,12 @@ fn main() {
.help("Path to VM disk image") .help("Path to VM disk image")
.takes_value(true), .takes_value(true),
) )
.arg(
Arg::with_name("net")
.long("net")
.help("Network parameters \"tap=<if_name>,mac=<mac_addr>\"")
.takes_value(true),
)
.arg( .arg(
Arg::with_name("cpus") Arg::with_name("cpus")
.long("cpus") .long("cpus")
@ -68,6 +74,11 @@ fn main() {
.expect("Missing argument: disk"); .expect("Missing argument: disk");
let disk_path = disk_arg.as_path(); let disk_path = disk_arg.as_path();
let mut net_params = None;
if let Some(net) = cmd_arguments.value_of("net") {
net_params = Some(net.to_string());
}
let mut vcpus = DEFAULT_VCPUS; let mut vcpus = DEFAULT_VCPUS;
if let Some(cpus) = cmd_arguments.value_of("cpus") { if let Some(cpus) = cmd_arguments.value_of("cpus") {
vcpus = cpus.parse::<u8>().unwrap(); vcpus = cpus.parse::<u8>().unwrap();
@ -81,7 +92,8 @@ fn main() {
println!("VM [{} vCPUS {} MB of memory]", vcpus, memory); println!("VM [{} vCPUS {} MB of memory]", vcpus, memory);
println!("Booting {:?}...", kernel_path); println!("Booting {:?}...", kernel_path);
let vm_config = VmConfig::new(kernel_path, disk_path, cmdline, vcpus, memory).unwrap(); let vm_config =
VmConfig::new(kernel_path, disk_path, cmdline, net_params, vcpus, memory).unwrap();
vmm::boot_kernel(vm_config).unwrap(); vmm::boot_kernel(vm_config).unwrap();
} }

View File

@ -11,6 +11,7 @@ epoll = "=4.0.1"
kvm-bindings = "0.1" kvm-bindings = "0.1"
kvm-ioctls = { git = "https://github.com/rust-vmm/kvm-ioctls" } kvm-ioctls = { git = "https://github.com/rust-vmm/kvm-ioctls" }
libc = ">=0.2.39" libc = ">=0.2.39"
net_util = { path = "../net_util" }
pci = {path = "../pci"} pci = {path = "../pci"}
qcow = { path = "../qcow" } qcow = { path = "../qcow" }
linux-loader = { git = "https://github.com/sameo/linux-loader" } linux-loader = { git = "https://github.com/sameo/linux-loader" }

View File

@ -15,6 +15,7 @@ extern crate epoll;
extern crate kvm_ioctls; extern crate kvm_ioctls;
extern crate libc; extern crate libc;
extern crate linux_loader; extern crate linux_loader;
extern crate net_util;
extern crate vm_allocator; extern crate vm_allocator;
extern crate vm_memory; extern crate vm_memory;
extern crate vm_virtio; extern crate vm_virtio;
@ -25,6 +26,7 @@ use kvm_ioctls::*;
use libc::{c_void, siginfo_t, EFD_NONBLOCK}; use libc::{c_void, siginfo_t, EFD_NONBLOCK};
use linux_loader::cmdline; use linux_loader::cmdline;
use linux_loader::loader::KernelLoader; use linux_loader::loader::KernelLoader;
use net_util::{MacAddr, Tap};
use pci::{PciConfigIo, PciDevice, PciInterruptPin, PciRoot}; use pci::{PciConfigIo, PciDevice, PciInterruptPin, PciRoot};
use std::ffi::CString; use std::ffi::CString;
use std::fs::{File, OpenOptions}; use std::fs::{File, OpenOptions};
@ -153,8 +155,14 @@ pub enum Error {
/// Cannot create virtio-blk device /// Cannot create virtio-blk device
CreateVirtioBlock(io::Error), CreateVirtioBlock(io::Error),
/// Cannot create virtio-net device
CreateVirtioNet(vm_virtio::net::Error),
/// Cannot create the system allocator /// Cannot create the system allocator
CreateSystemAllocator, CreateSystemAllocator,
/// Failed parsing network parameters
ParseNetworkParameters,
} }
pub type Result<T> = result::Result<T, Error>; pub type Result<T> = result::Result<T, Error>;
@ -219,7 +227,7 @@ pub struct VmConfig<'a> {
disk_path: &'a Path, disk_path: &'a Path,
cmdline: cmdline::Cmdline, cmdline: cmdline::Cmdline,
cmdline_addr: GuestAddress, cmdline_addr: GuestAddress,
net_params: Option<String>,
memory_size: GuestUsize, memory_size: GuestUsize,
vcpu_count: u8, vcpu_count: u8,
} }
@ -229,6 +237,7 @@ impl<'a> VmConfig<'a> {
kernel_path: &'a Path, kernel_path: &'a Path,
disk_path: &'a Path, disk_path: &'a Path,
cmdline_str: String, cmdline_str: String,
net_params: Option<String>,
vcpus: u8, vcpus: u8,
memory_size: GuestUsize, memory_size: GuestUsize,
) -> Result<Self> { ) -> Result<Self> {
@ -240,12 +249,37 @@ impl<'a> VmConfig<'a> {
disk_path, disk_path,
cmdline, cmdline,
cmdline_addr: CMDLINE_OFFSET, cmdline_addr: CMDLINE_OFFSET,
net_params,
memory_size, memory_size,
vcpu_count: vcpus, vcpu_count: vcpus,
}) })
} }
} }
fn parse_net_params(net_params: &str) -> Result<(&str, &str)> {
// Split the parameters based on the comma delimiter
let params_list: Vec<&str> = net_params.split(',').collect();
let mut if_name: Option<&str> = None;
let mut mac: Option<&str> = None;
for param in params_list.iter() {
if param.starts_with("tap=") {
if_name = Some(&param[4..]);
} else if param.starts_with("mac=") {
mac = Some(&param[4..]);
}
}
if let Some(if_name) = if_name {
if let Some(mac) = mac {
return Ok((if_name, mac));
}
}
Err(Error::ParseNetworkParameters)
}
struct DeviceManager { struct DeviceManager {
io_bus: devices::Bus, io_bus: devices::Bus,
mmio_bus: devices::Bus, mmio_bus: devices::Bus,
@ -291,18 +325,18 @@ impl DeviceManager {
.open(&vm_cfg.disk_path) .open(&vm_cfg.disk_path)
.map_err(Error::Disk)?; .map_err(Error::Disk)?;
// Add virtio-blk
let virtio_block_device = let virtio_block_device =
vm_virtio::Block::new(raw_img, false).map_err(Error::CreateVirtioBlock)?; vm_virtio::Block::new(raw_img, false).map_err(Error::CreateVirtioBlock)?;
let virtio_block_device = Box::new(virtio_block_device); let virtio_block_device = Box::new(virtio_block_device);
let mut virtio_pci_device = let mut virtio_pci_device = VirtioPciDevice::new(memory.clone(), virtio_block_device)
VirtioPciDevice::new(memory, virtio_block_device).map_err(|_| Error::VirtioDevice)?; .map_err(|_| Error::VirtioDevice)?;
let bars = virtio_pci_device let bars = virtio_pci_device
.allocate_bars(allocator) .allocate_bars(allocator)
.map_err(Error::AllocateBars)?; .map_err(Error::AllocateBars)?;
for (event, addr, _) in virtio_pci_device.ioeventfds() { for (event, addr, _) in virtio_pci_device.ioeventfds() {
let io_addr = IoEventAddress::Mmio(addr); let io_addr = IoEventAddress::Mmio(addr);
println!("Register ioevent at 0x{:x}", addr);
vm_fd vm_fd
.register_ioevent(event.as_raw_fd(), &io_addr, NoDatamatch) .register_ioevent(event.as_raw_fd(), &io_addr, NoDatamatch)
.map_err(Error::RegisterIoevent)?; .map_err(Error::RegisterIoevent)?;
@ -323,6 +357,44 @@ impl DeviceManager {
.add_device(virtio_pci_device.clone(), &mut mmio_bus, bars) .add_device(virtio_pci_device.clone(), &mut mmio_bus, bars)
.map_err(Error::AddPciDevice)?; .map_err(Error::AddPciDevice)?;
// Add virtio-net if required
if let Some(net_params) = &vm_cfg.net_params {
if let Ok((tap_if_name, mac_addr)) = parse_net_params(net_params) {
let mac = MacAddr::parse_str(mac_addr).unwrap();
let tap = Tap::open_named(tap_if_name).unwrap();
let virtio_net_device = vm_virtio::Net::new_with_tap(tap, Some(&mac))
.map_err(Error::CreateVirtioNet)?;
let virtio_net_device = Box::new(virtio_net_device);
let mut virtio_pci_device = VirtioPciDevice::new(memory.clone(), virtio_net_device)
.map_err(|_| Error::VirtioDevice)?;
let bars = virtio_pci_device
.allocate_bars(allocator)
.map_err(Error::AllocateBars)?;
for (event, addr, _) in virtio_pci_device.ioeventfds() {
let io_addr = IoEventAddress::Mmio(addr);
vm_fd
.register_ioevent(event.as_raw_fd(), &io_addr, NoDatamatch)
.map_err(Error::RegisterIoevent)?;
}
// Assign IRQ to the virtio-blk device
let irqfd = EventFd::new(EFD_NONBLOCK).map_err(Error::EventFd)?;
let irq_num = allocator.allocate_irq().ok_or(Error::AllocateIrq)?;
vm_fd
.register_irqfd(irqfd.as_raw_fd(), irq_num)
.map_err(Error::Irq)?;
// Let's use irq line INTA for now.
virtio_pci_device.assign_irq(irqfd, irq_num as u32, PciInterruptPin::IntA);
let virtio_pci_device = Arc::new(Mutex::new(virtio_pci_device));
pci_root
.add_device(virtio_pci_device.clone(), &mut mmio_bus, bars)
.map_err(Error::AddPciDevice)?;
}
}
let pci = Arc::new(Mutex::new(PciConfigIo::new(pci_root))); let pci = Arc::new(Mutex::new(PciConfigIo::new(pci_root)));
Ok(DeviceManager { Ok(DeviceManager {