Refactor Virtio

This commit is contained in:
Yuke Peng
2023-08-28 15:03:28 +08:00
committed by Tate, Hongliang Tian
parent df42397cea
commit 7d5e67e368
37 changed files with 1471 additions and 1413 deletions

View File

@ -13,6 +13,7 @@ jinux-input = { path = "../../comps/input" }
jinux-block = { path = "../../comps/block" }
jinux-network = { path = "../../comps/network" }
jinux-time = { path = "../../comps/time" }
jinux-virtio = { path = "../../comps/virtio" }
jinux-rights = { path = "../jinux-rights" }
controlled = { path = "../../libs/comp-sys/controlled" }
typeflags = { path = "../typeflags" }
@ -25,7 +26,20 @@ virtio-input-decoder = "0.1.4"
ascii = { version = "1.1", default-features = false, features = ["alloc"] }
intrusive-collections = "0.9.5"
time = { version = "0.3", default-features = false, features = ["alloc"] }
smoltcp = { version = "0.9.1", default-features = false, features = ["alloc", "log", "medium-ethernet", "medium-ip", "proto-dhcpv4", "proto-ipv4", "proto-igmp", "socket-icmp", "socket-udp", "socket-tcp", "socket-raw", "socket-dhcpv4"] }
smoltcp = { version = "0.9.1", default-features = false, features = [
"alloc",
"log",
"medium-ethernet",
"medium-ip",
"proto-dhcpv4",
"proto-ipv4",
"proto-igmp",
"socket-icmp",
"socket-udp",
"socket-tcp",
"socket-raw",
"socket-dhcpv4",
] }
# parse elf file
xmas-elf = "0.8.0"
@ -35,14 +49,18 @@ bitflags = "1.3"
ringbuf = { version = "0.3.2", default-features = false, features = ["alloc"] }
keyable-arc = { path = "../keyable-arc" }
# unzip initramfs
libflate = { git = "https://github.com/jinzhao-dev/libflate", rev = "b781da6", features = ["no_std"] }
libflate = { git = "https://github.com/jinzhao-dev/libflate", rev = "b781da6", features = [
"no_std",
] }
core2 = { version = "0.4", default_features = false, features = ["alloc"] }
lending-iterator = "0.1.7"
spin = "0.9.4"
vte = "0.10"
lru = "0.9.0"
log = "0.4"
getrandom = { version = "0.2.10", default-features = false, features = ["rdrand"] }
getrandom = { version = "0.2.10", default-features = false, features = [
"rdrand",
] }
[dependencies.lazy_static]
version = "1.0"

View File

@ -1,27 +1,27 @@
use jinux_input::INPUT_COMPONENT;
use log::info;
pub fn init() {
// print all the input device to make sure input crate will compile
for comp in INPUT_COMPONENT.get().unwrap().get_input_device() {
info!("input device name:{}", comp.name());
for (name, _) in jinux_input::all_devices() {
info!("Found Input device, name:{}", name);
}
}
#[allow(unused)]
fn block_device_test() {
let block_device = jinux_block::BLK_COMPONENT.get().unwrap().get_device();
let mut write_buffer = [0u8; 512];
let mut read_buffer = [0u8; 512];
info!("write_buffer address:{:x}", write_buffer.as_ptr() as usize);
info!("read_buffer address:{:x}", read_buffer.as_ptr() as usize);
for i in 0..512 {
for byte in write_buffer.iter_mut() {
*byte = i as u8;
for (_, device) in jinux_block::all_devices() {
let mut write_buffer = [0u8; 512];
let mut read_buffer = [0u8; 512];
info!("write_buffer address:{:x}", write_buffer.as_ptr() as usize);
info!("read_buffer address:{:x}", read_buffer.as_ptr() as usize);
for i in 0..512 {
for byte in write_buffer.iter_mut() {
*byte = i as u8;
}
device.write_block(i as usize, &write_buffer);
device.read_block(i as usize, &mut read_buffer);
assert_eq!(write_buffer, read_buffer);
}
block_device.write_block(i as usize, &write_buffer);
block_device.read_block(i as usize, &mut read_buffer);
assert_eq!(write_buffer, read_buffer);
info!("block device test passed!");
}
info!("block device test passed!");
}

View File

@ -1,6 +1,7 @@
use crate::prelude::*;
use jinux_frame::sync::SpinLock;
use jinux_network::{probe_virtio_net, NetworkDevice, VirtioNet};
use jinux_network::NetworkDevice;
use jinux_virtio::device::network::DEVICE_NAME;
use smoltcp::{
iface::{Config, Routes, SocketHandle, SocketSet},
socket::dhcpv4,
@ -10,7 +11,7 @@ use smoltcp::{
use super::{common::IfaceCommon, internal::IfaceInternal, Iface};
pub struct IfaceVirtio {
driver: SpinLock<VirtioNet>,
driver: Arc<SpinLock<Box<dyn NetworkDevice>>>,
common: IfaceCommon,
dhcp_handle: SocketHandle,
weak_self: Weak<Self>,
@ -18,9 +19,9 @@ pub struct IfaceVirtio {
impl IfaceVirtio {
pub fn new() -> Arc<Self> {
let mut virtio_net = probe_virtio_net().unwrap();
let mut virtio_net = jinux_network::get_device(&(DEVICE_NAME).to_string()).unwrap();
let interface = {
let mac_addr = virtio_net.mac_addr();
let mac_addr = virtio_net.lock().mac_addr();
let ip_addr = IpCidr::new(wire::IpAddress::Ipv4(wire::Ipv4Address::UNSPECIFIED), 0);
let routes = Routes::new();
let config = {
@ -30,7 +31,7 @@ impl IfaceVirtio {
));
config
};
let mut interface = smoltcp::iface::Interface::new(config, &mut virtio_net);
let mut interface = smoltcp::iface::Interface::new(config, &mut **virtio_net.lock());
interface.update_ip_addrs(|ip_addrs| {
debug_assert!(ip_addrs.len() == 0);
ip_addrs.push(ip_addr).unwrap();
@ -42,7 +43,7 @@ impl IfaceVirtio {
let dhcp_handle = init_dhcp_client(&mut socket_set);
drop(socket_set);
Arc::new_cyclic(|weak| Self {
driver: SpinLock::new(virtio_net),
driver: virtio_net,
common,
dhcp_handle,
weak_self: weak.clone(),
@ -113,7 +114,7 @@ impl Iface for IfaceVirtio {
fn poll(&self) {
let mut driver = self.driver.lock_irq_disabled();
self.common.poll(&mut *driver);
self.common.poll(&mut **driver);
self.process_dhcp();
}
}

View File

@ -2,7 +2,6 @@ use crate::{
net::iface::{Iface, IfaceLoopback, IfaceVirtio},
prelude::*,
};
use jinux_network::register_net_device_irq_handler;
use spin::Once;
use self::iface::spawn_background_poll_thread;
@ -18,12 +17,14 @@ pub fn init() {
let iface_loopback = IfaceLoopback::new();
vec![iface_virtio, iface_loopback]
});
register_net_device_irq_handler(|irq_num| {
debug!("irq num = {}", irq_num);
// TODO: further check that the irq num is the same as iface's irq num
let iface_virtio = &IFACES.get().unwrap()[0];
iface_virtio.poll();
});
for (name, _) in jinux_network::all_devices() {
jinux_network::register_recv_callback(&name, || {
// TODO: further check that the irq num is the same as iface's irq num
let iface_virtio = &IFACES.get().unwrap()[0];
iface_virtio.poll();
})
}
poll_ifaces();
}