Skip to content

Commit

Permalink
add net support
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandru Agache <[email protected]>
  • Loading branch information
alexandruag committed Mar 5, 2021
1 parent 4f949bb commit 4f8c596
Show file tree
Hide file tree
Showing 11 changed files with 1,061 additions and 11 deletions.
4 changes: 2 additions & 2 deletions coverage_config_x86_64.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"coverage_score": 80.8,
"exclude_path": "msr_index.rs,mpspec.rs,tests/",
"coverage_score": 69.3,
"exclude_path": "msr_index.rs,mpspec.rs,tests/,src/devices/src/virtio/net/bindings.rs",
"crate_features": ""
}
10 changes: 5 additions & 5 deletions src/devices/src/virtio/block/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ where
let config_space = build_config_space(&args.file_path)?;
let virtio_cfg = VirtioConfig::new(device_features, queues, config_space);

let common_cfg = CommonConfig::new(virtio_cfg, env).map_err(Error::Generic)?;
let common_cfg = CommonConfig::new(virtio_cfg, env).map_err(Error::Virtio)?;

Ok(Block {
cfg: common_cfg,
Expand All @@ -70,10 +70,10 @@ where

// Register the device on the MMIO bus.
env.register_mmio_device(block.clone())
.map_err(Error::Generic)?;
.map_err(Error::Virtio)?;

env.insert_cmdline_str(args.cmdline_config_substring())
.map_err(Error::Generic)?;
.map_err(Error::Virtio)?;

Ok(block)
}
Expand Down Expand Up @@ -128,14 +128,14 @@ impl<M: GuestAddressSpace + Clone + Send + 'static> VirtioDeviceActions for Bloc
disk,
};

let mut ioevents = self.cfg.prepare_activate().map_err(Error::Generic)?;
let mut ioevents = self.cfg.prepare_activate().map_err(Error::Virtio)?;

let handler = Arc::new(Mutex::new(QueueHandler {
inner,
ioeventfd: ioevents.remove(0),
}));

self.cfg.finalize_activate(handler).map_err(Error::Generic)
self.cfg.finalize_activate(handler).map_err(Error::Virtio)
}

fn reset(&mut self) -> Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion src/devices/src/virtio/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const SECTOR_SHIFT: u8 = 9;
#[derive(Debug)]
pub enum Error {
Backend(stdio_executor::Error),
Generic(crate::virtio::Error),
Virtio(crate::virtio::Error),
OpenFile(io::Error),
Seek(io::Error),
}
Expand Down
27 changes: 26 additions & 1 deletion src/devices/src/virtio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// We're only providing virtio over MMIO devices for now, but we aim to add PCI support as well.

pub mod block;
pub mod net;

use std::convert::TryFrom;
use std::io;
Expand All @@ -17,7 +18,7 @@ use event_manager::{
};
use kvm_ioctls::{IoEventAddress, VmFd};
use linux_loader::cmdline::Cmdline;
use vm_device::bus::{self, MmioRange};
use vm_device::bus::{self, MmioAddress, MmioRange};
use vm_device::device_manager::MmioManager;
use vm_device::DeviceMmio;
use vm_memory::{GuestAddress, GuestAddressSpace};
Expand Down Expand Up @@ -58,6 +59,7 @@ pub enum Error {
Cmdline(linux_loader::cmdline::Error),
Endpoint(EvmgrError),
EventFd(io::Error),
Overflow,
QueuesNotValid,
RegisterIoevent(errno::Error),
RegisterIrqfd(errno::Error),
Expand All @@ -73,6 +75,24 @@ pub struct MmioConfig {
pub gsi: u32,
}

impl MmioConfig {
pub fn new(base: u64, size: u64, gsi: u32) -> Result<Self> {
MmioRange::new(MmioAddress(base), size)
.map(|range| MmioConfig { range, gsi })
.map_err(Error::Bus)
}

pub fn next(&self) -> Result<Self> {
let range = self.range;
let next_start = range
.base()
.0
.checked_add(range.size())
.ok_or(Error::Overflow)?;
Self::new(next_start, range.size(), self.gsi + 1)
}
}

// Represents the environment the devices in this crate current expect in order to be created
// and registered with the appropriate buses/handlers/etc. We're always passing a mmio_cfg object
// for now, and we'll re-evaluate the mechanism for exposing environment (i.e. maybe we'll do it
Expand Down Expand Up @@ -313,6 +333,9 @@ pub(crate) mod tests {
}
}

// Skipping until adding Arm support and figuring out how make the irqchip creation in the
// `Mock` object work there as well.
#[cfg_attr(target_arch = "aarch64", ignore)]
#[test]
fn test_env() {
// Just a dummy device we're going to register on the bus.
Expand Down Expand Up @@ -348,6 +371,8 @@ pub(crate) mod tests {
assert!(mock.kernel_cmdline.as_str().ends_with("ending_string"));
}

// Ignoring until aarch64 support is here.
#[cfg_attr(target_arch = "aarch64", ignore)]
#[test]
fn test_common_config() {
let mut mock = EnvMock::new();
Expand Down
269 changes: 269 additions & 0 deletions src/devices/src/virtio/net/bindings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//
// Portions Copyright 2017 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the THIRD-PARTY file.

// The following are manually copied from crosvm/firecracker. In the latter, they can be found as
// part of the `net_gen` local crate. We should figure out how to proceed going forward (i.e.
// create some bindings of our own, put them in a common crate, etc).

#![allow(clippy::all)]
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]

pub const TUN_F_CSUM: ::std::os::raw::c_uint = 1;
pub const TUN_F_TSO4: ::std::os::raw::c_uint = 2;
pub const TUN_F_TSO6: ::std::os::raw::c_uint = 4;
pub const TUN_F_UFO: ::std::os::raw::c_uint = 16;

#[repr(C)]
pub struct __BindgenUnionField<T>(::std::marker::PhantomData<T>);
impl<T> __BindgenUnionField<T> {
#[inline]
pub fn new() -> Self {
__BindgenUnionField(::std::marker::PhantomData)
}
#[inline]
pub unsafe fn as_ref(&self) -> &T {
::std::mem::transmute(self)
}
#[inline]
pub unsafe fn as_mut(&mut self) -> &mut T {
::std::mem::transmute(self)
}
}
impl<T> ::std::default::Default for __BindgenUnionField<T> {
#[inline]
fn default() -> Self {
Self::new()
}
}
impl<T> ::std::clone::Clone for __BindgenUnionField<T> {
#[inline]
fn clone(&self) -> Self {
Self::new()
}
}
impl<T> ::std::marker::Copy for __BindgenUnionField<T> {}
impl<T> ::std::fmt::Debug for __BindgenUnionField<T> {
fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
fmt.write_str("__BindgenUnionField")
}
}

#[repr(C)]
#[derive(Debug, Default, Copy)]
pub struct ifreq {
pub ifr_ifrn: ifreq__bindgen_ty_1,
pub ifr_ifru: ifreq__bindgen_ty_2,
}

impl Clone for ifreq {
fn clone(&self) -> Self {
*self
}
}

#[repr(C)]
#[derive(Debug, Default, Copy)]
pub struct ifreq__bindgen_ty_1 {
pub ifrn_name: __BindgenUnionField<[::std::os::raw::c_uchar; 16usize]>,
pub bindgen_union_field: [u8; 16usize],
}

impl Clone for ifreq__bindgen_ty_1 {
fn clone(&self) -> Self {
*self
}
}

#[repr(C)]
#[derive(Debug, Default, Copy)]
pub struct ifreq__bindgen_ty_2 {
pub ifru_addr: __BindgenUnionField<sockaddr>,
pub ifru_dstaddr: __BindgenUnionField<sockaddr>,
pub ifru_broadaddr: __BindgenUnionField<sockaddr>,
pub ifru_netmask: __BindgenUnionField<sockaddr>,
pub ifru_hwaddr: __BindgenUnionField<sockaddr>,
pub ifru_flags: __BindgenUnionField<::std::os::raw::c_short>,
pub ifru_ivalue: __BindgenUnionField<::std::os::raw::c_int>,
pub ifru_mtu: __BindgenUnionField<::std::os::raw::c_int>,
pub ifru_map: __BindgenUnionField<ifmap>,
pub ifru_slave: __BindgenUnionField<[::std::os::raw::c_char; 16usize]>,
pub ifru_newname: __BindgenUnionField<[::std::os::raw::c_char; 16usize]>,
pub ifru_data: __BindgenUnionField<*mut ::std::os::raw::c_void>,
pub ifru_settings: __BindgenUnionField<if_settings>,
pub bindgen_union_field: [u64; 3usize],
}

impl Clone for ifreq__bindgen_ty_2 {
fn clone(&self) -> Self {
*self
}
}

pub type sa_family_t = ::std::os::raw::c_ushort;

#[repr(C)]
#[derive(Debug, Default, Copy)]
pub struct sockaddr {
pub sa_family: sa_family_t,
pub sa_data: [::std::os::raw::c_char; 14usize],
}

impl Clone for sockaddr {
fn clone(&self) -> Self {
*self
}
}

#[repr(C)]
#[derive(Debug, Default, Copy)]
pub struct if_settings {
pub type_: ::std::os::raw::c_uint,
pub size: ::std::os::raw::c_uint,
pub ifs_ifsu: if_settings__bindgen_ty_1,
}

impl Clone for if_settings {
fn clone(&self) -> Self {
*self
}
}

#[repr(C)]
#[derive(Debug, Default, Copy)]
pub struct if_settings__bindgen_ty_1 {
pub raw_hdlc: __BindgenUnionField<*mut raw_hdlc_proto>,
pub cisco: __BindgenUnionField<*mut cisco_proto>,
pub fr: __BindgenUnionField<*mut fr_proto>,
pub fr_pvc: __BindgenUnionField<*mut fr_proto_pvc>,
pub fr_pvc_info: __BindgenUnionField<*mut fr_proto_pvc_info>,
pub sync: __BindgenUnionField<*mut sync_serial_settings>,
pub te1: __BindgenUnionField<*mut te1_settings>,
pub bindgen_union_field: u64,
}

impl Clone for if_settings__bindgen_ty_1 {
fn clone(&self) -> Self {
*self
}
}

#[repr(C)]
#[derive(Debug, Default, Copy)]
pub struct ifmap {
pub mem_start: ::std::os::raw::c_ulong,
pub mem_end: ::std::os::raw::c_ulong,
pub base_addr: ::std::os::raw::c_ushort,
pub irq: ::std::os::raw::c_uchar,
pub dma: ::std::os::raw::c_uchar,
pub port: ::std::os::raw::c_uchar,
}

impl Clone for ifmap {
fn clone(&self) -> Self {
*self
}
}

#[repr(C)]
#[derive(Debug, Default, Copy)]
pub struct raw_hdlc_proto {
pub encoding: ::std::os::raw::c_ushort,
pub parity: ::std::os::raw::c_ushort,
}

impl Clone for raw_hdlc_proto {
fn clone(&self) -> Self {
*self
}
}

#[repr(C)]
#[derive(Debug, Default, Copy)]
pub struct cisco_proto {
pub interval: ::std::os::raw::c_uint,
pub timeout: ::std::os::raw::c_uint,
}

impl Clone for cisco_proto {
fn clone(&self) -> Self {
*self
}
}

#[repr(C)]
#[derive(Debug, Default, Copy)]
pub struct fr_proto {
pub t391: ::std::os::raw::c_uint,
pub t392: ::std::os::raw::c_uint,
pub n391: ::std::os::raw::c_uint,
pub n392: ::std::os::raw::c_uint,
pub n393: ::std::os::raw::c_uint,
pub lmi: ::std::os::raw::c_ushort,
pub dce: ::std::os::raw::c_ushort,
}

impl Clone for fr_proto {
fn clone(&self) -> Self {
*self
}
}

#[repr(C)]
#[derive(Debug, Default, Copy)]
pub struct fr_proto_pvc {
pub dlci: ::std::os::raw::c_uint,
}

impl Clone for fr_proto_pvc {
fn clone(&self) -> Self {
*self
}
}

#[repr(C)]
#[derive(Debug, Default, Copy)]
pub struct fr_proto_pvc_info {
pub dlci: ::std::os::raw::c_uint,
pub master: [::std::os::raw::c_char; 16usize],
}

impl Clone for fr_proto_pvc_info {
fn clone(&self) -> Self {
*self
}
}

#[repr(C)]
#[derive(Debug, Default, Copy)]
pub struct sync_serial_settings {
pub clock_rate: ::std::os::raw::c_uint,
pub clock_type: ::std::os::raw::c_uint,
pub loopback: ::std::os::raw::c_ushort,
}

impl Clone for sync_serial_settings {
fn clone(&self) -> Self {
*self
}
}

#[repr(C)]
#[derive(Debug, Default, Copy)]
pub struct te1_settings {
pub clock_rate: ::std::os::raw::c_uint,
pub clock_type: ::std::os::raw::c_uint,
pub loopback: ::std::os::raw::c_ushort,
pub slot_map: ::std::os::raw::c_uint,
}

impl Clone for te1_settings {
fn clone(&self) -> Self {
*self
}
}
Loading

0 comments on commit 4f8c596

Please sign in to comment.