Skip to content

Commit 21f446c

Browse files
committed
refactor(drivers): Make virtio-net optional and feature-gated
Signed-off-by: Jens Reidel <adrian@travitia.xyz>
1 parent be0a653 commit 21f446c

File tree

17 files changed

+593
-137
lines changed

17 files changed

+593
-137
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ tcp = ["smoltcp", "smoltcp/socket-tcp"]
6969
trace = ["smoltcp?/log", "smoltcp?/verbose"]
7070
udp = ["smoltcp", "smoltcp/socket-udp"]
7171
vga = []
72+
virtio-net = []
7273
vsock = ["pci"]
7374

7475
[lints.rust]

src/arch/x86_64/kernel/mmio.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::arch::x86_64::mm::paging;
1313
use crate::arch::x86_64::mm::paging::{
1414
BasePageSize, PageSize, PageTableEntryFlags, PageTableEntryFlagsExt,
1515
};
16+
#[cfg(feature = "virtio-net")]
1617
use crate::drivers::net::virtio::VirtioNetDriver;
1718
use crate::drivers::virtio::transport::mmio as mmio_virtio;
1819
use crate::drivers::virtio::transport::mmio::VirtioDriver;
@@ -28,16 +29,18 @@ const IRQ_NUMBER: u8 = 44 - 32;
2829
static MMIO_DRIVERS: InitCell<Vec<MmioDriver>> = InitCell::new(Vec::new());
2930

3031
pub(crate) enum MmioDriver {
32+
#[cfg(feature = "virtio-net")]
3133
VirtioNet(InterruptTicketMutex<VirtioNetDriver>),
3234
}
3335

3436
impl MmioDriver {
35-
#[allow(unreachable_patterns)]
3637
fn get_network_driver(&self) -> Option<&InterruptTicketMutex<VirtioNetDriver>> {
37-
match self {
38-
Self::VirtioNet(drv) => Some(drv),
39-
_ => None,
38+
#[allow(clippy::irrefutable_let_patterns)]
39+
if let MmioDriver::VirtioNet(drv) = self {
40+
return Some(drv);
4041
}
42+
43+
None
4144
}
4245
}
4346

@@ -186,6 +189,7 @@ pub(crate) fn register_driver(drv: MmioDriver) {
186189
MMIO_DRIVERS.with(|mmio_drivers| mmio_drivers.unwrap().push(drv));
187190
}
188191

192+
#[cfg(feature = "virtio-net")]
189193
pub(crate) fn get_network_driver() -> Option<&'static InterruptTicketMutex<VirtioNetDriver>> {
190194
MMIO_DRIVERS
191195
.get()?

src/config.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ pub(crate) const USER_STACK_SIZE: usize = 0x0010_0000;
1212
pub(crate) const VIRTIO_MAX_QUEUE_SIZE: u16 = if cfg!(feature = "pci") { 2048 } else { 1024 };
1313

1414
/// Default keep alive interval in milliseconds
15-
#[cfg(feature = "tcp")]
15+
#[cfg(all(
16+
feature = "tcp",
17+
any(
18+
feature = "virtio-net",
19+
all(target_arch = "riscv64", feature = "gem-net"),
20+
all(target_arch = "x86_64", feature = "rtl8139"),
21+
)
22+
))]
1623
pub(crate) const DEFAULT_KEEP_ALIVE_INTERVAL: u64 = 75000;
1724

1825
#[cfg(feature = "vsock")]

src/drivers/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@
44
pub mod fs;
55
#[cfg(not(feature = "pci"))]
66
pub mod mmio;
7-
#[cfg(any(feature = "tcp", feature = "udp"))]
7+
#[cfg(all(
8+
any(feature = "tcp", feature = "udp"),
9+
any(
10+
feature = "virtio-net",
11+
all(target_arch = "riscv64", feature = "gem-net"),
12+
all(target_arch = "x86_64", feature = "rtl8139"),
13+
)
14+
))]
815
pub mod net;
916
#[cfg(feature = "pci")]
1017
pub mod pci;

src/drivers/net/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
pub mod gem;
33
#[cfg(all(target_arch = "x86_64", feature = "rtl8139"))]
44
pub mod rtl8139;
5-
#[cfg(not(all(target_arch = "x86_64", feature = "rtl8139")))]
5+
#[cfg(feature = "virtio-net")]
66
pub mod virtio;
77

88
use core::str::FromStr;

src/drivers/pci.rs

Lines changed: 35 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,18 @@ use pci_types::{
1919
use crate::arch::pci::PciConfigRegion;
2020
#[cfg(feature = "fuse")]
2121
use crate::drivers::fs::virtio_fs::VirtioFsDriver;
22-
#[cfg(any(feature = "tcp", feature = "udp"))]
22+
#[cfg(all(
23+
any(feature = "tcp", feature = "udp"),
24+
any(
25+
feature = "virtio-net",
26+
all(target_arch = "riscv64", feature = "gem-net"),
27+
all(target_arch = "x86_64", feature = "rtl8139"),
28+
)
29+
))]
2330
use crate::drivers::net::NetworkDriver;
2431
#[cfg(all(target_arch = "x86_64", feature = "rtl8139"))]
2532
use crate::drivers::net::rtl8139::{self, RTL8139Driver};
26-
#[cfg(all(
27-
not(all(target_arch = "x86_64", feature = "rtl8139")),
28-
any(feature = "tcp", feature = "udp")
29-
))]
33+
#[cfg(all(feature = "virtio-net", any(feature = "tcp", feature = "udp")))]
3034
use crate::drivers::net::virtio::VirtioNetDriver;
3135
#[cfg(any(
3236
all(
@@ -333,10 +337,7 @@ pub(crate) enum PciDriver {
333337
VirtioFs(InterruptTicketMutex<VirtioFsDriver>),
334338
#[cfg(feature = "vsock")]
335339
VirtioVsock(InterruptTicketMutex<VirtioVsockDriver>),
336-
#[cfg(all(
337-
not(all(target_arch = "x86_64", feature = "rtl8139")),
338-
any(feature = "tcp", feature = "udp")
339-
))]
340+
#[cfg(all(feature = "virtio-net", any(feature = "tcp", feature = "udp")))]
340341
VirtioNet(InterruptTicketMutex<VirtioNetDriver>),
341342
#[cfg(all(
342343
target_arch = "x86_64",
@@ -347,16 +348,14 @@ pub(crate) enum PciDriver {
347348
}
348349

349350
impl PciDriver {
350-
#[cfg(all(
351-
not(all(target_arch = "x86_64", feature = "rtl8139")),
352-
any(feature = "tcp", feature = "udp")
353-
))]
351+
#[cfg(all(feature = "virtio-net", any(feature = "tcp", feature = "udp")))]
354352
fn get_network_driver(&self) -> Option<&InterruptTicketMutex<VirtioNetDriver>> {
355-
#[allow(unreachable_patterns)]
356-
match self {
357-
Self::VirtioNet(drv) => Some(drv),
358-
_ => None,
353+
#[allow(irrefutable_let_patterns)]
354+
if let Self::VirtioNet(drv) = self {
355+
return Some(drv);
359356
}
357+
358+
None
360359
}
361360

362361
#[cfg(all(
@@ -365,29 +364,32 @@ impl PciDriver {
365364
any(feature = "tcp", feature = "udp")
366365
))]
367366
fn get_network_driver(&self) -> Option<&InterruptTicketMutex<RTL8139Driver>> {
368-
#[allow(unreachable_patterns)]
369-
match self {
370-
Self::RTL8139Net(drv) => Some(drv),
371-
_ => None,
367+
#[allow(irrefutable_let_patterns)]
368+
if let Self::RTL8139Net(drv) = self {
369+
return Some(drv);
372370
}
371+
372+
None
373373
}
374374

375375
#[cfg(feature = "vsock")]
376376
fn get_vsock_driver(&self) -> Option<&InterruptTicketMutex<VirtioVsockDriver>> {
377-
#[allow(unreachable_patterns)]
378-
match self {
379-
Self::VirtioVsock(drv) => Some(drv),
380-
_ => None,
377+
#[allow(irrefutable_let_patterns)]
378+
if let Self::VirtioVsock(drv) = self {
379+
return Some(drv);
381380
}
381+
382+
None
382383
}
383384

384385
#[cfg(feature = "fuse")]
385386
fn get_filesystem_driver(&self) -> Option<&InterruptTicketMutex<VirtioFsDriver>> {
386-
match self {
387-
Self::VirtioFs(drv) => Some(drv),
388-
#[allow(unreachable_patterns)]
389-
_ => None,
387+
#[allow(irrefutable_let_patterns)]
388+
if let Self::VirtioFs(drv) = self {
389+
return Some(drv);
390390
}
391+
392+
None
391393
}
392394

393395
fn get_interrupt_handler(&self) -> (InterruptLine, fn()) {
@@ -421,10 +423,7 @@ impl PciDriver {
421423

422424
(irq_number, rtl8139_handler)
423425
}
424-
#[cfg(all(
425-
not(all(target_arch = "x86_64", feature = "rtl8139")),
426-
any(feature = "tcp", feature = "udp")
427-
))]
426+
#[cfg(all(feature = "virtio-net", any(feature = "tcp", feature = "udp")))]
428427
Self::VirtioNet(drv) => {
429428
fn network_handler() {
430429
if let Some(driver) = get_network_driver() {
@@ -473,10 +472,7 @@ pub(crate) fn get_interrupt_handlers() -> HashMap<InterruptLine, InterruptHandle
473472
handlers
474473
}
475474

476-
#[cfg(all(
477-
not(all(target_arch = "x86_64", feature = "rtl8139")),
478-
any(feature = "tcp", feature = "udp")
479-
))]
475+
#[cfg(all(feature = "virtio-net", any(feature = "tcp", feature = "udp")))]
480476
pub(crate) fn get_network_driver() -> Option<&'static InterruptTicketMutex<VirtioNetDriver>> {
481477
PCI_DRIVERS
482478
.get()?
@@ -525,18 +521,12 @@ pub(crate) fn init() {
525521
);
526522

527523
#[cfg(any(
528-
all(
529-
any(feature = "tcp", feature = "udp"),
530-
not(all(target_arch = "x86_64", feature = "rtl8139"))
531-
),
524+
all(any(feature = "tcp", feature = "udp"), feature = "virtio-net"),
532525
feature = "fuse",
533526
feature = "vsock"
534527
))]
535528
match pci_virtio::init_device(adapter) {
536-
#[cfg(all(
537-
not(all(target_arch = "x86_64", feature = "rtl8139")),
538-
any(feature = "tcp", feature = "udp")
539-
))]
529+
#[cfg(all(feature = "virtio-net", any(feature = "tcp", feature = "udp")))]
540530
Ok(VirtioDriver::Network(drv)) => {
541531
register_driver(PciDriver::VirtioNet(InterruptTicketMutex::new(drv)));
542532
}

src/drivers/virtio/mod.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ pub mod error {
99

1010
#[cfg(feature = "fuse")]
1111
pub use crate::drivers::fs::virtio_fs::error::VirtioFsError;
12-
#[cfg(all(
13-
not(all(target_arch = "x86_64", feature = "rtl8139")),
14-
any(feature = "tcp", feature = "udp")
15-
))]
12+
#[cfg(all(feature = "virtio-net", any(feature = "tcp", feature = "udp")))]
1613
pub use crate::drivers::net::virtio::error::VirtioNetError;
1714
#[cfg(feature = "pci")]
1815
use crate::drivers::pci::error::PciError;
@@ -31,10 +28,7 @@ pub mod error {
3128
#[cfg(feature = "pci")]
3229
NoNotifCfg(u16),
3330
DevNotSupported(u16),
34-
#[cfg(all(
35-
not(all(target_arch = "x86_64", feature = "rtl8139")),
36-
any(feature = "tcp", feature = "udp")
37-
))]
31+
#[cfg(all(feature = "virtio-net", any(feature = "tcp", feature = "udp")))]
3832
NetDriver(VirtioNetError),
3933
#[cfg(feature = "fuse")]
4034
FsDriver(VirtioFsError),
@@ -86,10 +80,7 @@ pub mod error {
8680
VirtioError::DevNotSupported(id) => {
8781
write!(f, "Device with id {id:#x} not supported.")
8882
}
89-
#[cfg(all(
90-
not(all(target_arch = "x86_64", feature = "rtl8139")),
91-
any(feature = "tcp", feature = "udp")
92-
))]
83+
#[cfg(all(feature = "virtio-net", any(feature = "tcp", feature = "udp")))]
9384
VirtioError::NetDriver(net_error) => match net_error {
9485
#[cfg(feature = "pci")]
9586
VirtioNetError::NoDevCfg(id) => write!(

src/drivers/virtio/transport/mmio.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use volatile::{VolatilePtr, VolatileRef};
1616

1717
use crate::drivers::InterruptLine;
1818
use crate::drivers::error::DriverError;
19-
#[cfg(any(feature = "tcp", feature = "udp"))]
19+
#[cfg(all(feature = "virtio-net", any(feature = "tcp", feature = "udp")))]
2020
use crate::drivers::net::virtio::VirtioNetDriver;
2121
use crate::drivers::virtio::error::VirtioError;
2222

@@ -360,7 +360,7 @@ impl IsrStatus {
360360
}
361361

362362
pub(crate) enum VirtioDriver {
363-
#[cfg(any(feature = "tcp", feature = "udp"))]
363+
#[cfg(all(feature = "virtio-net", any(feature = "tcp", feature = "udp")))]
364364
Network(VirtioNetDriver),
365365
}
366366

@@ -380,7 +380,7 @@ pub(crate) fn init_device(
380380

381381
// Verify the device-ID to find the network card
382382
match registers.as_ptr().device_id().read() {
383-
#[cfg(any(feature = "tcp", feature = "udp"))]
383+
#[cfg(all(feature = "virtio-net", any(feature = "tcp", feature = "udp")))]
384384
virtio::Id::Net => match VirtioNetDriver::init(dev_id, registers, irq_no) {
385385
Ok(virt_net_drv) => {
386386
info!("Virtio network driver initialized.");

src/drivers/virtio/transport/pci.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ use crate::arch::pci::PciConfigRegion;
2424
use crate::drivers::error::DriverError;
2525
#[cfg(feature = "fuse")]
2626
use crate::drivers::fs::virtio_fs::VirtioFsDriver;
27-
#[cfg(all(
28-
not(all(target_arch = "x86_64", feature = "rtl8139")),
29-
any(feature = "tcp", feature = "udp")
30-
))]
27+
#[cfg(all(feature = "virtio-net", any(feature = "tcp", feature = "udp")))]
3128
use crate::drivers::net::virtio::VirtioNetDriver;
3229
use crate::drivers::pci::PciDevice;
3330
use crate::drivers::pci::error::PciError;
@@ -808,10 +805,7 @@ pub(crate) fn init_device(
808805
let id = virtio::Id::from(u8::try_from(device_id - 0x1040).unwrap());
809806

810807
match id {
811-
#[cfg(all(
812-
not(all(target_arch = "x86_64", feature = "rtl8139")),
813-
any(feature = "tcp", feature = "udp")
814-
))]
808+
#[cfg(all(feature = "virtio-net", any(feature = "tcp", feature = "udp")))]
815809
virtio::Id::Net => match VirtioNetDriver::init(device) {
816810
Ok(virt_net_drv) => {
817811
info!("Virtio network driver initialized.");
@@ -874,10 +868,7 @@ pub(crate) fn init_device(
874868
}
875869

876870
pub(crate) enum VirtioDriver {
877-
#[cfg(all(
878-
not(all(target_arch = "x86_64", feature = "rtl8139")),
879-
any(feature = "tcp", feature = "udp")
880-
))]
871+
#[cfg(all(feature = "virtio-net", any(feature = "tcp", feature = "udp")))]
881872
Network(VirtioNetDriver),
882873
#[cfg(feature = "vsock")]
883874
Vsock(Box<VirtioVsockDriver>),

0 commit comments

Comments
 (0)