Skip to content

Commit

Permalink
Fix for Virtio_net probing
Browse files Browse the repository at this point in the history
  • Loading branch information
czoop committed May 18, 2021
1 parent 2be7bee commit e1a5c29
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 9 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions domains/sys/driver/virtio_net/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand All @@ -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"]
features = ["alloc", "proto-ipv4", "socket-tcp", "socket-icmp", "ethernet"]

[features]
default = [
]

virtio_net = []
22 changes: 15 additions & 7 deletions domains/sys/driver/virtio_net/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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
Expand Down
68 changes: 68 additions & 0 deletions domains/sys/driver/virtio_net/src/nullnet.rs
Original file line number Diff line number Diff line change
@@ -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<Box<dyn interface::net::Net>> {
Ok(box Self::new())
}

fn submit_and_poll(
&self,
packets: &mut VecDeque<Vec<u8>>,
collect: &mut VecDeque<Vec<u8>>,
_tx: bool,
) -> RpcResult<Result<usize>> {
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<Result<(usize, RRefDeque<[u8; 1514], 32>, 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<Vec<u8>>, _tx: bool) -> RpcResult<Result<usize>> {
Ok(Ok(0))
}

fn poll_rref(
&self,
collect: RRefDeque<[u8; 1514], 512>,
_tx: bool,
) -> RpcResult<Result<(usize, RRefDeque<[u8; 1514], 512>)>> {
Ok(Ok((0, collect)))
}

fn get_stats(&self) -> RpcResult<Result<NetworkStats>> {
Ok(Ok(NetworkStats::new()))
}

fn test_domain_crossing(&self) -> RpcResult<()> {
Ok(())
}
}

0 comments on commit e1a5c29

Please sign in to comment.