Skip to content

Commit cbd8ceb

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

File tree

24 files changed

+753
-140
lines changed

24 files changed

+753
-140
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/aarch64/kernel/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
pub mod core_local;
22
pub mod interrupts;
3-
#[cfg(all(not(feature = "pci"), any(feature = "tcp", feature = "udp")))]
3+
#[cfg(all(
4+
not(feature = "pci"),
5+
feature = "virtio-net",
6+
any(feature = "tcp", feature = "udp")
7+
))]
48
pub mod mmio;
59
#[cfg(feature = "pci")]
610
pub mod pci;

src/arch/riscv64/kernel/devicetree.rs

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
#[cfg(all(any(feature = "tcp", feature = "udp"), not(feature = "pci")))]
1+
#[cfg(all(
2+
any(feature = "tcp", feature = "udp"),
3+
feature = "virtio-net",
4+
not(feature = "pci")
5+
))]
26
use core::ptr::NonNull;
37

48
use fdt::Fdt;
@@ -9,14 +13,26 @@ use memory_addresses::PhysAddr;
913
not(feature = "pci")
1014
))]
1115
use memory_addresses::VirtAddr;
12-
#[cfg(all(any(feature = "tcp", feature = "udp"), not(feature = "pci")))]
16+
#[cfg(all(
17+
any(feature = "tcp", feature = "udp"),
18+
feature = "virtio-net",
19+
not(feature = "pci")
20+
))]
1321
use virtio::mmio::{DeviceRegisters, DeviceRegistersVolatileFieldAccess};
14-
#[cfg(all(any(feature = "tcp", feature = "udp"), not(feature = "pci")))]
22+
#[cfg(all(
23+
any(feature = "tcp", feature = "udp"),
24+
feature = "virtio-net",
25+
not(feature = "pci")
26+
))]
1527
use volatile::VolatileRef;
1628

1729
use crate::arch::riscv64::kernel::get_dtb_ptr;
1830
use crate::arch::riscv64::kernel::interrupts::init_plic;
19-
#[cfg(all(any(feature = "tcp", feature = "udp"), not(feature = "pci")))]
31+
#[cfg(all(
32+
any(feature = "tcp", feature = "udp"),
33+
any(feature = "gem-net", feature = "virtio-net"),
34+
not(feature = "pci")
35+
))]
2036
use crate::arch::riscv64::kernel::mmio::MmioDriver;
2137
use crate::arch::riscv64::mm::paging::{self, PageSize};
2238
#[cfg(all(
@@ -28,10 +44,15 @@ use crate::drivers::net::gem;
2844
#[cfg(all(
2945
any(feature = "tcp", feature = "udp"),
3046
not(feature = "pci"),
31-
not(feature = "gem-net")
47+
not(feature = "gem-net"),
48+
feature = "virtio-net",
3249
))]
3350
use crate::drivers::virtio::transport::mmio::{self as mmio_virtio, VirtioDriver};
34-
#[cfg(all(any(feature = "tcp", feature = "udp"), not(feature = "pci")))]
51+
#[cfg(all(
52+
any(feature = "tcp", feature = "udp"),
53+
any(feature = "gem-net", feature = "virtio-net"),
54+
not(feature = "pci")
55+
))]
3556
use crate::kernel::mmio::register_driver;
3657

3758
static mut PLATFORM_MODEL: Model = Model::Unknown;
@@ -115,7 +136,11 @@ pub fn init_drivers() {
115136
}
116137

117138
// Init GEM
118-
#[cfg(all(feature = "gem-net", not(feature = "pci")))]
139+
#[cfg(all(
140+
any(feature = "tcp", feature = "udp"),
141+
feature = "gem-net",
142+
not(feature = "pci")
143+
))]
119144
if let Some(gem_node) = fdt.find_compatible(&["sifive,fu540-c000-gem"]) {
120145
debug!("Found Ethernet controller");
121146

@@ -170,7 +195,11 @@ pub fn init_drivers() {
170195
}
171196

172197
// Init virtio-mmio
173-
#[cfg(all(any(feature = "tcp", feature = "udp"), not(feature = "pci")))]
198+
#[cfg(all(
199+
any(feature = "tcp", feature = "udp"),
200+
feature = "virtio-net",
201+
not(feature = "pci")
202+
))]
174203
if let Some(virtio_node) = fdt.find_compatible(&["virtio,mmio"]) {
175204
debug!("Found virtio mmio device");
176205
let virtio_region = virtio_node
@@ -228,7 +257,11 @@ pub fn init_drivers() {
228257
// BasePageSize::SIZE as usize,
229258
// );
230259

231-
#[cfg(all(any(feature = "tcp", feature = "udp"), not(feature = "gem-net")))]
260+
#[cfg(all(
261+
any(feature = "tcp", feature = "udp"),
262+
feature = "virtio-net",
263+
not(feature = "gem-net")
264+
))]
232265
if let Ok(VirtioDriver::Network(drv)) =
233266
mmio_virtio::init_device(mmio, irq.try_into().unwrap())
234267
{
@@ -240,6 +273,10 @@ pub fn init_drivers() {
240273
}
241274
}
242275

243-
#[cfg(all(feature = "tcp", not(feature = "pci")))]
276+
#[cfg(all(
277+
any(feature = "tcp", feature = "udp"),
278+
any(feature = "virtio-net", feature = "gem-net"),
279+
not(feature = "pci")
280+
))]
244281
super::mmio::MMIO_DRIVERS.finalize();
245282
}

src/arch/riscv64/kernel/mmio.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use hermit_sync::InterruptSpinMutex;
44

55
#[cfg(feature = "gem-net")]
66
use crate::drivers::net::gem::GEMDriver;
7-
#[cfg(not(feature = "gem-net"))]
7+
#[cfg(all(feature = "virtio-net", not(feature = "gem-net")))]
88
use crate::drivers::net::virtio::VirtioNetDriver;
99
use crate::init_cell::InitCell;
1010

@@ -13,7 +13,7 @@ pub(crate) static MMIO_DRIVERS: InitCell<Vec<MmioDriver>> = InitCell::new(Vec::n
1313
pub(crate) enum MmioDriver {
1414
#[cfg(feature = "gem-net")]
1515
GEMNet(InterruptSpinMutex<GEMDriver>),
16-
#[cfg(not(feature = "gem-net"))]
16+
#[cfg(all(feature = "virtio-net", not(feature = "gem-net")))]
1717
VirtioNet(InterruptSpinMutex<VirtioNetDriver>),
1818
}
1919

@@ -25,7 +25,7 @@ impl MmioDriver {
2525
}
2626
}
2727

28-
#[cfg(not(feature = "gem-net"))]
28+
#[cfg(all(feature = "virtio-net", not(feature = "gem-net")))]
2929
fn get_network_driver(&self) -> Option<&InterruptSpinMutex<VirtioNetDriver>> {
3030
match self {
3131
Self::VirtioNet(drv) => Some(drv),
@@ -44,7 +44,7 @@ pub(crate) fn get_network_driver() -> Option<&'static InterruptSpinMutex<GEMDriv
4444
.find_map(|drv| drv.get_network_driver())
4545
}
4646

47-
#[cfg(not(feature = "gem-net"))]
47+
#[cfg(all(feature = "virtio-net", not(feature = "gem-net")))]
4848
pub(crate) fn get_network_driver() -> Option<&'static InterruptSpinMutex<VirtioNetDriver>> {
4949
MMIO_DRIVERS
5050
.get()?

src/arch/riscv64/kernel/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
pub mod core_local;
22
mod devicetree;
33
pub mod interrupts;
4-
#[cfg(all(any(feature = "tcp", feature = "udp"), not(feature = "pci")))]
4+
#[cfg(all(
5+
any(feature = "tcp", feature = "udp"),
6+
any(feature = "virtio-net", feature = "gem-net"),
7+
not(feature = "pci")
8+
))]
59
pub mod mmio;
610
#[cfg(feature = "pci")]
711
pub mod pci;

src/arch/x86_64/kernel/mmio.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@ pub(crate) enum MmioDriver {
3232
}
3333

3434
impl MmioDriver {
35-
#[allow(unreachable_patterns)]
3635
fn get_network_driver(&self) -> Option<&InterruptTicketMutex<VirtioNetDriver>> {
37-
match self {
38-
Self::VirtioNet(drv) => Some(drv),
39-
_ => None,
36+
#[allow(clippy::irrefutable_let_patterns)]
37+
if let MmioDriver::VirtioNet(drv) = self {
38+
return Some(drv);
4039
}
40+
41+
None
4142
}
4243
}
4344

src/arch/x86_64/kernel/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ pub mod apic;
1818
pub mod core_local;
1919
pub mod gdt;
2020
pub mod interrupts;
21-
#[cfg(all(not(feature = "pci"), any(feature = "tcp", feature = "udp")))]
21+
#[cfg(all(
22+
not(feature = "pci"),
23+
feature = "virtio-net",
24+
any(feature = "tcp", feature = "udp")
25+
))]
2226
pub mod mmio;
2327
#[cfg(feature = "pci")]
2428
pub mod pci;

src/config.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,25 @@ pub const DEFAULT_STACK_SIZE: usize = 0x0001_0000;
55
pub(crate) const USER_STACK_SIZE: usize = 0x0010_0000;
66

77
#[cfg(any(
8-
all(any(feature = "tcp", feature = "udp"), not(feature = "rtl8139")),
8+
all(
9+
any(feature = "tcp", feature = "udp"),
10+
feature = "virtio-net",
11+
not(feature = "rtl8139")
12+
),
913
feature = "fuse",
1014
feature = "vsock"
1115
))]
1216
pub(crate) const VIRTIO_MAX_QUEUE_SIZE: u16 = if cfg!(feature = "pci") { 2048 } else { 1024 };
1317

1418
/// Default keep alive interval in milliseconds
15-
#[cfg(feature = "tcp")]
19+
#[cfg(all(
20+
feature = "tcp",
21+
any(
22+
feature = "virtio-net",
23+
all(target_arch = "riscv64", feature = "gem-net"),
24+
all(target_arch = "x86_64", feature = "rtl8139"),
25+
)
26+
))]
1627
pub(crate) const DEFAULT_KEEP_ALIVE_INTERVAL: u64 = 75000;
1728

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

src/drivers/mmio.rs

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,42 @@
1-
#[cfg(any(feature = "tcp", feature = "udp"))]
1+
#[cfg(all(
2+
any(feature = "tcp", feature = "udp"),
3+
any(
4+
feature = "virtio-net",
5+
all(target_arch = "riscv64", feature = "gem-net"),
6+
all(target_arch = "x86_64", feature = "rtl8139"),
7+
)
8+
))]
29
use alloc::collections::VecDeque;
310

411
use ahash::RandomState;
512
use hashbrown::HashMap;
613

7-
#[cfg(any(feature = "tcp", feature = "udp"))]
14+
#[cfg(all(
15+
any(feature = "tcp", feature = "udp"),
16+
any(
17+
feature = "virtio-net",
18+
all(target_arch = "riscv64", feature = "gem-net"),
19+
all(target_arch = "x86_64", feature = "rtl8139"),
20+
)
21+
))]
822
pub(crate) use crate::arch::kernel::mmio::get_network_driver;
9-
#[cfg(any(feature = "tcp", feature = "udp"))]
23+
#[cfg(all(
24+
any(feature = "tcp", feature = "udp"),
25+
any(
26+
feature = "virtio-net",
27+
all(target_arch = "riscv64", feature = "gem-net"),
28+
all(target_arch = "x86_64", feature = "rtl8139"),
29+
)
30+
))]
1031
use crate::drivers::Driver;
11-
#[cfg(any(feature = "tcp", feature = "udp"))]
32+
#[cfg(all(
33+
any(feature = "tcp", feature = "udp"),
34+
any(
35+
feature = "virtio-net",
36+
all(target_arch = "riscv64", feature = "gem-net"),
37+
all(target_arch = "x86_64", feature = "rtl8139"),
38+
)
39+
))]
1240
use crate::drivers::net::NetworkDriver;
1341
use crate::drivers::{InterruptHandlerQueue, InterruptLine};
1442

@@ -18,7 +46,14 @@ pub(crate) fn get_interrupt_handlers() -> HashMap<InterruptLine, InterruptHandle
1846
let mut handlers: HashMap<InterruptLine, InterruptHandlerQueue, RandomState> =
1947
HashMap::with_hasher(RandomState::with_seeds(0, 0, 0, 0));
2048

21-
#[cfg(any(feature = "tcp", feature = "udp"))]
49+
#[cfg(all(
50+
any(feature = "tcp", feature = "udp"),
51+
any(
52+
feature = "virtio-net",
53+
all(target_arch = "riscv64", feature = "gem-net"),
54+
all(target_arch = "x86_64", feature = "rtl8139"),
55+
)
56+
))]
2257
if let Some(drv) = get_network_driver() {
2358
fn network_handler() {
2459
if let Some(driver) = get_network_driver() {

src/drivers/mod.rs

Lines changed: 11 additions & 2 deletions
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;
@@ -123,12 +130,14 @@ pub(crate) fn init() {
123130
crate::drivers::pci::init();
124131
#[cfg(all(
125132
not(feature = "pci"),
133+
feature = "virtio-net",
126134
target_arch = "x86_64",
127-
any(feature = "tcp", feature = "udp")
135+
any(feature = "tcp", feature = "udp"),
128136
))]
129137
crate::arch::x86_64::kernel::mmio::init_drivers();
130138
#[cfg(all(
131139
not(feature = "pci"),
140+
feature = "virtio-net",
132141
target_arch = "aarch64",
133142
any(feature = "tcp", feature = "udp")
134143
))]

0 commit comments

Comments
 (0)