Skip to content

Commit 7c24305

Browse files
committed
revise vsock driver to support the new virtio interface
1 parent 2472726 commit 7c24305

File tree

8 files changed

+195
-259
lines changed

8 files changed

+195
-259
lines changed

src/drivers/net/gem.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ use tock_registers::{register_bitfields, register_structs};
1616

1717
use crate::arch::kernel::core_local::core_scheduler;
1818
use crate::arch::kernel::interrupts::*;
19+
#[cfg(all(any(feature = "tcp", feature = "udp"), not(feature = "pci")))]
20+
use crate::arch::kernel::mmio as hardware;
1921
use crate::arch::mm::paging::virt_to_phys;
2022
use crate::arch::mm::VirtAddr;
2123
use crate::drivers::error::DriverError;
2224
use crate::drivers::net::NetworkDriver;
25+
#[cfg(all(any(feature = "tcp", feature = "udp"), feature = "pci"))]
26+
use crate::drivers::pci as hardware;
2327
use crate::executor::device::{RxToken, TxToken};
2428

2529
//Base address of the control registers
@@ -202,14 +206,14 @@ fn gem_irqhandler() {
202206

203207
debug!("Receive network interrupt");
204208

205-
crate::executor::run();
206-
207209
// PLIC end of interrupt
208210
crate::arch::kernel::interrupts::external_eoi();
209211
if let Some(driver) = hardware::get_network_driver() {
210212
driver.lock().handle_interrupt()
211213
}
212214

215+
crate::executor::run();
216+
213217
core_scheduler().reschedule();
214218
}
215219

@@ -365,7 +369,7 @@ impl NetworkDriver for GEMDriver {
365369
}
366370
}
367371

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

371375
let receive_status = unsafe { (*self.gem).receive_status.extract() };
@@ -409,8 +413,8 @@ impl NetworkDriver for GEMDriver {
409413
// handle incoming packets
410414
todo!();
411415
}
412-
// increment_irq_counter((32 + self.irq).into());
413-
ret
416+
417+
//increment_irq_counter((32 + self.irq).into());
414418
}
415419
}
416420

src/drivers/net/rtl8139.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use crate::arch::mm::VirtAddr;
1717
use crate::arch::pci::PciConfigRegion;
1818
use crate::drivers::error::DriverError;
1919
use crate::drivers::net::NetworkDriver;
20+
use crate::drivers::pci as hardware;
2021
use crate::drivers::pci::PciDevice;
2122
use crate::executor::device::{RxToken, TxToken};
2223

@@ -319,7 +320,7 @@ impl NetworkDriver for RTL8139Driver {
319320
}
320321
}
321322

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

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

343-
let ret = (isr_contents & ISR_ROK) == ISR_ROK;
344-
345-
crate::executor::run();
346-
347344
unsafe {
348345
outw(
349346
self.iobase + ISR,
350347
isr_contents & (ISR_RXOVW | ISR_TER | ISR_RER | ISR_TOK | ISR_ROK),
351348
);
352349
}
353-
354-
ret
355350
}
356351
}
357352

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

434+
crate::executor::run();
435+
439436
core_scheduler().reschedule();
440437
crate::arch::x86_64::swapgs(&stack_frame);
441438
}

src/drivers/net/virtio/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use alloc::boxed::Box;
1414
use alloc::vec::Vec;
1515
use core::mem::MaybeUninit;
1616

17-
use align_address::Align;
1817
use smoltcp::phy::{Checksum, ChecksumCapabilities};
1918
use smoltcp::wire::{EthernetFrame, Ipv4Packet, Ipv6Packet, ETHERNET_HEADER_LEN};
2019
use virtio::net::{ConfigVolatileFieldAccess, Hdr, HdrF};

src/drivers/virtio/transport/mmio.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,13 +381,16 @@ pub(crate) fn init_device(
381381
match registers.as_ptr().device_id().read() {
382382
#[cfg(any(feature = "tcp", feature = "udp"))]
383383
virtio::Id::Net => {
384-
match VirtioNetDriver::init(dev_id, registers, irq_no) {
384+
match VirtioNetDriver::init(dev_id, registers) {
385385
Ok(virt_net_drv) => {
386+
use crate::drivers::virtio::transport::VIRTIO_IRQ;
387+
386388
info!("Virtio network driver initialized.");
387389
// Install interrupt handler
388390
irq_install_handler(irq_no, virtio_irqhandler);
389391
#[cfg(not(target_arch = "riscv64"))]
390392
add_irq_name(irq_no, "virtio");
393+
let _ = VIRTIO_IRQ.try_insert(irq_no);
391394

392395
Ok(VirtioDriver::Network(virt_net_drv))
393396
}
@@ -399,13 +402,16 @@ pub(crate) fn init_device(
399402
}
400403
#[cfg(feature = "vsock")]
401404
virtio::Id::Vsock => {
402-
match VirtioVsockDriver::init(dev_id, registers, irq_no) {
405+
match VirtioVsockDriver::init(dev_id, registers) {
403406
Ok(virt_net_drv) => {
407+
use crate::drivers::virtio::transport::VIRTIO_IRQ;
408+
404409
info!("Virtio sock driver initialized.");
405410
// Install interrupt handler
406411
irq_install_handler(irq_no, virtio_irqhandler);
407412
#[cfg(not(target_arch = "riscv64"))]
408413
add_irq_name(irq_no, "virtio");
414+
let _ = VIRTIO_IRQ.try_insert(irq_no);
409415

410416
Ok(VirtioDriver::Vsock(virt_vsock_drv))
411417
}

src/drivers/virtio/transport/mod.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,19 @@ use hermit_sync::OnceCell;
1515
use crate::arch::kernel::core_local::increment_irq_counter;
1616
#[cfg(target_arch = "x86_64")]
1717
use crate::arch::kernel::interrupts::ExceptionStackFrame;
18-
#[cfg(all(feature = "vsock", not(feature = "pci")))]
18+
#[cfg(all(
19+
any(feature = "vsock", feature = "tcp", feature = "udp"),
20+
not(feature = "pci")
21+
))]
1922
use crate::arch::kernel::mmio as hardware;
2023
#[cfg(target_arch = "aarch64")]
2124
use crate::arch::scheduler::State;
2225
#[cfg(any(feature = "tcp", feature = "udp"))]
2326
use crate::drivers::net::NetworkDriver;
24-
#[cfg(all(feature = "vsock", feature = "pci"))]
27+
#[cfg(all(
28+
any(feature = "vsock", feature = "tcp", feature = "udp"),
29+
feature = "pci"
30+
))]
2531
use crate::drivers::pci as hardware;
2632

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

34-
crate::executor::run();
40+
increment_irq_counter(32 + VIRTIO_IRQ.get().unwrap());
3541

3642
#[cfg(any(feature = "tcp", feature = "udp"))]
3743
if let Some(driver) = hardware::get_network_driver() {
@@ -42,6 +48,10 @@ pub(crate) fn virtio_irqhandler(_state: &State) -> bool {
4248
if let Some(driver) = hardware::get_vsock_driver() {
4349
driver.lock().handle_interrupt();
4450
}
51+
52+
crate::executor::run();
53+
54+
true
4555
}
4656

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

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

57-
crate::executor::run();
5867
crate::kernel::apic::eoi();
5968

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

79+
crate::executor::run();
80+
7081
core_scheduler().reschedule();
7182
crate::arch::x86_64::swapgs(&stack_frame);
7283
}
@@ -78,10 +89,6 @@ pub(crate) fn virtio_irqhandler() {
7889

7990
debug!("Receive virtio interrupt");
8091

81-
increment_irq_counter(32 + VIRTIO_IRQ.get().unwrap());
82-
83-
crate::executor::run();
84-
8592
// PLIC end of interrupt
8693
crate::arch::kernel::interrupts::external_eoi();
8794
#[cfg(any(feature = "tcp", feature = "udp"))]
@@ -94,5 +101,7 @@ pub(crate) fn virtio_irqhandler() {
94101
driver.lock().handle_interrupt();
95102
}
96103

104+
crate::executor::run();
105+
97106
core_scheduler().reschedule();
98107
}

0 commit comments

Comments
 (0)