Skip to content

Commit

Permalink
revise vsock driver to support the new virtio interface
Browse files Browse the repository at this point in the history
  • Loading branch information
stlankes committed Aug 6, 2024
1 parent 2472726 commit c2adfae
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 256 deletions.
14 changes: 9 additions & 5 deletions src/drivers/net/gem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ use tock_registers::{register_bitfields, register_structs};

use crate::arch::kernel::core_local::core_scheduler;
use crate::arch::kernel::interrupts::*;
#[cfg(all(any(feature = "tcp", feature = "udp"), not(feature = "pci")))]
use crate::arch::kernel::mmio as hardware;
use crate::arch::mm::paging::virt_to_phys;
use crate::arch::mm::VirtAddr;
use crate::drivers::error::DriverError;
use crate::drivers::net::NetworkDriver;
#[cfg(all(any(feature = "tcp", feature = "udp"), feature = "pci"))]
use crate::drivers::pci as hardware;
use crate::executor::device::{RxToken, TxToken};

//Base address of the control registers
Expand Down Expand Up @@ -202,14 +206,14 @@ fn gem_irqhandler() {

debug!("Receive network interrupt");

crate::executor::run();

// PLIC end of interrupt
crate::arch::kernel::interrupts::external_eoi();
if let Some(driver) = hardware::get_network_driver() {
driver.lock().handle_interrupt()
}

crate::executor::run();

core_scheduler().reschedule();
}

Expand Down Expand Up @@ -365,7 +369,7 @@ impl NetworkDriver for GEMDriver {
}
}

fn handle_interrupt(&mut self) -> bool {
fn handle_interrupt(&mut self) {
let int_status = unsafe { (*self.gem).int_status.extract() };

let receive_status = unsafe { (*self.gem).receive_status.extract() };
Expand Down Expand Up @@ -409,8 +413,8 @@ impl NetworkDriver for GEMDriver {
// handle incoming packets
todo!();
}
// increment_irq_counter((32 + self.irq).into());
ret

//increment_irq_counter((32 + self.irq).into());
}
}

Expand Down
11 changes: 4 additions & 7 deletions src/drivers/net/rtl8139.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::arch::mm::VirtAddr;
use crate::arch::pci::PciConfigRegion;
use crate::drivers::error::DriverError;
use crate::drivers::net::NetworkDriver;
use crate::drivers::pci as hardware;
use crate::drivers::pci::PciDevice;
use crate::executor::device::{RxToken, TxToken};

Expand Down Expand Up @@ -319,7 +320,7 @@ impl NetworkDriver for RTL8139Driver {
}
}

fn handle_interrupt(&mut self) -> bool {
fn handle_interrupt(&mut self) {
increment_irq_counter(32 + self.irq);

let isr_contents = unsafe { inw(self.iobase + ISR) };
Expand All @@ -340,18 +341,12 @@ impl NetworkDriver for RTL8139Driver {
trace!("RTL88139: RX overflow detected!\n");
}

let ret = (isr_contents & ISR_ROK) == ISR_ROK;

crate::executor::run();

unsafe {
outw(
self.iobase + ISR,
isr_contents & (ISR_RXOVW | ISR_TER | ISR_RER | ISR_TOK | ISR_ROK),
);
}

ret
}
}

Expand Down Expand Up @@ -436,6 +431,8 @@ extern "x86-interrupt" fn rtl8139_irqhandler(stack_frame: ExceptionStackFrame) {
debug!("Unable to handle interrupt!");
}

crate::executor::run();

core_scheduler().reschedule();
crate::arch::x86_64::swapgs(&stack_frame);
}
Expand Down
1 change: 0 additions & 1 deletion src/drivers/net/virtio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use alloc::boxed::Box;
use alloc::vec::Vec;
use core::mem::MaybeUninit;

use align_address::Align;
use smoltcp::phy::{Checksum, ChecksumCapabilities};
use smoltcp::wire::{EthernetFrame, Ipv4Packet, Ipv6Packet, ETHERNET_HEADER_LEN};
use virtio::net::{ConfigVolatileFieldAccess, Hdr, HdrF};
Expand Down
10 changes: 8 additions & 2 deletions src/drivers/virtio/transport/mmio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,13 +381,16 @@ pub(crate) fn init_device(
match registers.as_ptr().device_id().read() {
#[cfg(any(feature = "tcp", feature = "udp"))]
virtio::Id::Net => {
match VirtioNetDriver::init(dev_id, registers, irq_no) {
match VirtioNetDriver::init(dev_id, registers) {
Ok(virt_net_drv) => {
use crate::drivers::virtio::transport::VIRTIO_IRQ;

info!("Virtio network driver initialized.");
// Install interrupt handler
irq_install_handler(irq_no, virtio_irqhandler);
#[cfg(not(target_arch = "riscv64"))]
add_irq_name(irq_no, "virtio");
let _ = VIRTIO_IRQ.try_insert(irq_no);

Ok(VirtioDriver::Network(virt_net_drv))
}
Expand All @@ -399,13 +402,16 @@ pub(crate) fn init_device(
}
#[cfg(feature = "vsock")]
virtio::Id::Vsock => {
match VirtioVsockDriver::init(dev_id, registers, irq_no) {
match VirtioVsockDriver::init(dev_id, registers) {
Ok(virt_net_drv) => {
use crate::drivers::virtio::transport::VIRTIO_IRQ;

info!("Virtio sock driver initialized.");
// Install interrupt handler
irq_install_handler(irq_no, virtio_irqhandler);
#[cfg(not(target_arch = "riscv64"))]
add_irq_name(irq_no, "virtio");
let _ = VIRTIO_IRQ.try_insert(irq_no);

Ok(VirtioDriver::Vsock(virt_vsock_drv))
}
Expand Down
25 changes: 17 additions & 8 deletions src/drivers/virtio/transport/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@ use hermit_sync::OnceCell;
use crate::arch::kernel::core_local::increment_irq_counter;
#[cfg(target_arch = "x86_64")]
use crate::arch::kernel::interrupts::ExceptionStackFrame;
#[cfg(all(feature = "vsock", not(feature = "pci")))]
#[cfg(all(
any(feature = "vsock", feature = "tcp", feature = "udp"),
not(feature = "pci")
))]
use crate::arch::kernel::mmio as hardware;
#[cfg(target_arch = "aarch64")]
use crate::arch::scheduler::State;
#[cfg(any(feature = "tcp", feature = "udp"))]
use crate::drivers::net::NetworkDriver;
#[cfg(all(feature = "vsock", feature = "pci"))]
#[cfg(all(
any(feature = "vsock", feature = "tcp", feature = "udp"),
feature = "pci"
))]
use crate::drivers::pci as hardware;

/// All virtio devices share the interrupt number `VIRTIO_IRQ`
Expand All @@ -31,7 +37,7 @@ static VIRTIO_IRQ: OnceCell<u8> = OnceCell::new();
pub(crate) fn virtio_irqhandler(_state: &State) -> bool {
debug!("Receive virtio interrupt");

crate::executor::run();
increment_irq_counter(32 + VIRTIO_IRQ.get().unwrap());

#[cfg(any(feature = "tcp", feature = "udp"))]
if let Some(driver) = hardware::get_network_driver() {
Expand All @@ -42,6 +48,10 @@ pub(crate) fn virtio_irqhandler(_state: &State) -> bool {
if let Some(driver) = hardware::get_vsock_driver() {
driver.lock().handle_interrupt();
}

crate::executor::run();

true
}

#[cfg(target_arch = "x86_64")]
Expand All @@ -54,7 +64,6 @@ pub(crate) extern "x86-interrupt" fn virtio_irqhandler(stack_frame: ExceptionSta

increment_irq_counter(32 + VIRTIO_IRQ.get().unwrap());

crate::executor::run();
crate::kernel::apic::eoi();

#[cfg(any(feature = "tcp", feature = "udp"))]
Expand All @@ -67,6 +76,8 @@ pub(crate) extern "x86-interrupt" fn virtio_irqhandler(stack_frame: ExceptionSta
driver.lock().handle_interrupt();
}

crate::executor::run();

core_scheduler().reschedule();
crate::arch::x86_64::swapgs(&stack_frame);
}
Expand All @@ -78,10 +89,6 @@ pub(crate) fn virtio_irqhandler() {

debug!("Receive virtio interrupt");

increment_irq_counter(32 + VIRTIO_IRQ.get().unwrap());

crate::executor::run();

// PLIC end of interrupt
crate::arch::kernel::interrupts::external_eoi();
#[cfg(any(feature = "tcp", feature = "udp"))]
Expand All @@ -94,5 +101,7 @@ pub(crate) fn virtio_irqhandler() {
driver.lock().handle_interrupt();
}

crate::executor::run();

core_scheduler().reschedule();
}
Loading

0 comments on commit c2adfae

Please sign in to comment.