From e1a5c29739cafc5f5c574f6077954f210d0bf034 Mon Sep 17 00:00:00 2001 From: Connor Zwick Date: Tue, 18 May 2021 13:35:55 -0700 Subject: [PATCH] Fix for Virtio_net probing --- Makefile | 1 + domains/sys/driver/virtio_net/Cargo.toml | 9 ++- domains/sys/driver/virtio_net/src/main.rs | 22 +++++-- domains/sys/driver/virtio_net/src/nullnet.rs | 68 ++++++++++++++++++++ 4 files changed, 91 insertions(+), 9 deletions(-) create mode 100644 domains/sys/driver/virtio_net/src/nullnet.rs diff --git a/Makefile b/Makefile index d92fe363..bcdbd9d9 100644 --- a/Makefile +++ b/Makefile @@ -119,6 +119,7 @@ endif ifeq ($(VIRTIO_NET),true) qemu_common += -device virtio-net-pci,netdev=net0 qemu_common += -netdev tap,id=net0,ifname=virtio,script=no,downscript=no +DOMAIN_FEATURES += --features "virtio_net" endif QEMU ?= $(shell which qemu-system-x86_64) diff --git a/domains/sys/driver/virtio_net/Cargo.toml b/domains/sys/driver/virtio_net/Cargo.toml index 4c42934b..49f850c8 100644 --- a/domains/sys/driver/virtio_net/Cargo.toml +++ b/domains/sys/driver/virtio_net/Cargo.toml @@ -18,7 +18,6 @@ redhttpd = { path = "../../../lib/redhttpd" } virtio_device = { path = "../../../../lib/devices/virtio" } virtio_network_device = { path = "../../../../lib/devices/virtio_net" } - # Interfaces syscalls = { path = "../../../../lib/core/interfaces/syscalls" } pci_driver = { path = "../../../../lib/core/interfaces/dev/pci/pci_driver" } @@ -30,4 +29,10 @@ interface = { path = "../../../../interface/generated" } [dependencies.smoltcp] path = "../../../../lib/external/smoltcp" default-features = false -features = ["alloc", "proto-ipv4", "socket-tcp", "socket-icmp", "ethernet"] \ No newline at end of file +features = ["alloc", "proto-ipv4", "socket-tcp", "socket-icmp", "ethernet"] + +[features] +default = [ +] + +virtio_net = [] \ No newline at end of file diff --git a/domains/sys/driver/virtio_net/src/main.rs b/domains/sys/driver/virtio_net/src/main.rs index 4b4b756b..2d61655a 100644 --- a/domains/sys/driver/virtio_net/src/main.rs +++ b/domains/sys/driver/virtio_net/src/main.rs @@ -30,6 +30,8 @@ pub use interface::error::{ErrorKind, Result}; use virtio_network_device::pci::PciFactory; use virtio_network_device::VirtioNetInner; +mod nullnet; + use interface::rref::{RRef, RRefDeque}; use smolnet::{self, SmolPhy}; @@ -105,16 +107,22 @@ pub fn trusted_entry( libsyscalls::syscalls::init(s); interface::rref::init(heap, libsyscalls::syscalls::sys_get_current_domain_id()); + #[cfg(feature = "virtio_net")] let net = { - let mut pci_factory = PciFactory::new(); - if pci.pci_register_driver(&mut pci_factory, 4, None).is_err() { - panic!("Failed to probe VirtioNet PCI"); - } - let dev = pci_factory.to_device().unwrap(); - VirtioNet(Arc::new(Mutex::new(dev))) + let net = { + let mut pci_factory = PciFactory::new(); + if pci.pci_register_driver(&mut pci_factory, 4, None).is_err() { + panic!("Failed to probe VirtioNet PCI"); + } + let dev = pci_factory.to_device().unwrap(); + VirtioNet(Arc::new(Mutex::new(dev))) + }; + net.0.lock().init(); + net }; - net.0.lock().init(); + #[cfg(not(feature = "virtio_net"))] + let net = { nullnet::NullNet::new() }; /* // VIRTIO DEMO LOOP diff --git a/domains/sys/driver/virtio_net/src/nullnet.rs b/domains/sys/driver/virtio_net/src/nullnet.rs new file mode 100644 index 00000000..9b19a8ee --- /dev/null +++ b/domains/sys/driver/virtio_net/src/nullnet.rs @@ -0,0 +1,68 @@ +use alloc::boxed::Box; +use alloc::collections::VecDeque; +use alloc::vec::Vec; +use interface::error::Result; +use interface::net::NetworkStats; +use interface::rpc::RpcResult; +use interface::rref::RRefDeque; + +pub struct NullNet {} + +impl NullNet { + pub fn new() -> Self { + Self {} + } +} + +impl interface::net::Net for NullNet { + fn clone_net(&self) -> RpcResult> { + Ok(box Self::new()) + } + + fn submit_and_poll( + &self, + packets: &mut VecDeque>, + collect: &mut VecDeque>, + _tx: bool, + ) -> RpcResult> { + let ret = packets.len(); + while let Some(pkt) = packets.pop_front() { + collect.push_back(pkt); + } + Ok(Ok(ret)) + } + + fn submit_and_poll_rref( + &self, + mut packets: RRefDeque<[u8; 1514], 32>, + mut collect: RRefDeque<[u8; 1514], 32>, + _tx: bool, + _pkt_len: usize, + ) -> RpcResult, RRefDeque<[u8; 1514], 32>)>> { + while let Some(pkt) = packets.pop_front() { + collect.push_back(pkt); + } + + Ok(Ok((collect.len(), packets, collect))) + } + + fn poll(&self, _collect: &mut VecDeque>, _tx: bool) -> RpcResult> { + Ok(Ok(0)) + } + + fn poll_rref( + &self, + collect: RRefDeque<[u8; 1514], 512>, + _tx: bool, + ) -> RpcResult)>> { + Ok(Ok((0, collect))) + } + + fn get_stats(&self) -> RpcResult> { + Ok(Ok(NetworkStats::new())) + } + + fn test_domain_crossing(&self) -> RpcResult<()> { + Ok(()) + } +}