Commit 574326bc authored by Stefan Lankes's avatar Stefan Lankes
Browse files

switch to smoltcp 0.5

parent cb5341cb
......@@ -30,10 +30,10 @@ default-features = false
#features = ["release_max_level_info"]
[dependencies.smoltcp]
#version = "0.5"
git = "https://github.com/hermitcore/smoltcp.git"
version = "0.5"
#git = "https://github.com/hermitcore/smoltcp.git"
default-features = false
features = ["log", "verbose", "alloc", "socket-udp", "socket-tcp", "proto-ipv4", "proto-dhcpv4"]
features = ["log", "verbose", "alloc", "socket-udp", "socket-tcp", "proto-ipv4"]
# for debugging: "log", "verbose"
[target.'cfg(target_arch = "x86_64")'.dependencies.hermit-multiboot]
......
......@@ -6,11 +6,15 @@
// copied, modified, or distributed except according to those terms.
pub mod uhyve;
pub mod rtl8139;
use smoltcp::time::Instant;
use smoltcp::socket::SocketSet;
use smoltcp::iface::EthernetInterface;
use smoltcp::phy::Device;
//use smoltcp::wire::{IpCidr, Ipv4Address, Ipv4Cidr};
//use smoltcp::socket::{RawSocketBuffer, RawPacketMetadata};
//use smoltcp::dhcp::Dhcpv4Client;
use synch::semaphore::*;
use scheduler::task::TaskId;
......@@ -24,34 +28,115 @@ pub fn get_network_task_id() -> TaskId {
}
}
pub fn networkd<'b, 'c, 'e, DeviceT: for<'d> Device<'d>>(iface: &mut EthernetInterface<'b, 'c, 'e, DeviceT>) -> ! {
/*pub fn networkd_with_dhcp<'b, 'c, 'e, DeviceT: for<'d> Device<'d>, F>(iface: &mut EthernetInterface<'b, 'c, 'e, DeviceT>, is_polling: F) -> !
where F: Fn() -> bool {
let dhcp_rx_buffer = RawSocketBuffer::new(
[RawPacketMetadata::EMPTY; 1],
vec![0; 900]
);
let dhcp_tx_buffer = RawSocketBuffer::new(
[RawPacketMetadata::EMPTY; 1],
vec![0; 600]
);
let mut sockets = SocketSet::new(vec![]);
let boot_time = crate::arch::get_boot_time();
let mut counter: usize = 0;
let microseconds = ::arch::processor::get_timer_ticks() - boot_time;
let timestamp = Instant::from_millis(microseconds as i64 / 1000i64);
let mut dhcp = Dhcpv4Client::new(&mut sockets, dhcp_rx_buffer, dhcp_tx_buffer, timestamp);
let mut prev_cidr = Ipv4Cidr::new(Ipv4Address::UNSPECIFIED, 0);
loop {
let microseconds = ::arch::processor::get_timer_ticks() - boot_time;
let timestamp = Instant::from_millis(microseconds as i64 / 1000i64);
iface.poll(&mut sockets, timestamp)
.map(|_| {trace!("receive message {}", counter); counter = counter+1;})
.unwrap_or_else(|e| info!("Poll: {:?}", e));
let config = dhcp.poll(iface, &mut sockets, timestamp)
.unwrap_or_else(|e| {
debug!("DHCP: {:?}", e);
None
});
config.map(|config| {
info!("DHCP config: {:?}", config);
match config.address {
Some(cidr) => if cidr != prev_cidr {
iface.update_ip_addrs(|addrs| {
addrs.iter_mut().nth(0)
.map(|addr| {
*addr = IpCidr::Ipv4(cidr);
});
});
prev_cidr = cidr;
info!("Assigned a new IPv4 address: {}", cidr);
}
_ => {}
}
match iface.poll(&mut sockets, timestamp) {
Ok(ready) => {
if ready == true {
trace!("receive message {}", counter);
} else {
match iface.poll_delay(&sockets, timestamp) {
Some(duration) => {
trace!("duration {}", duration);
NET_SEM.acquire(Some(duration.millis()))
},
None => NET_SEM.acquire(None),
};
}
},
Err(e) => {
debug!("poll error: {}", e);
}
config.router.map(|router| iface.routes_mut()
.add_default_ipv4_route(router.into())
.unwrap()
);
iface.routes_mut()
.update(|routes_map| {
routes_map.get(&IpCidr::new(Ipv4Address::UNSPECIFIED.into(), 0))
.map(|default_route| {
info!("Default gateway: {}", default_route.via_router);
});
});
if config.dns_servers.iter().any(|s| s.is_some()) {
info!("DNS servers:");
for dns_server in config.dns_servers.iter().filter_map(|s| *s) {
info!("- {}", dns_server);
}
}
});
if is_polling() == false {
let mut timeout = dhcp.next_poll(timestamp);
iface.poll_delay(&sockets, timestamp)
.map(|sockets_timeout| timeout = sockets_timeout);
debug!("networkd timeout {}", timeout.millis());
// Calculate the absolute wakeup time in processor timer ticks out of the relative timeout in milliseconds.
let wakeup_time = if timeout.millis() > 0 {
Some(::arch::processor::get_timer_ticks() + (timeout.millis() as u64) * 1000)
} else {
Some(::arch::processor::get_timer_ticks() + 100)
};
NET_SEM.acquire(wakeup_time);
}
}
}*/
counter = counter+1;
pub fn networkd<'b, 'c, 'e, DeviceT: for<'d> Device<'d>, F>(iface: &mut EthernetInterface<'b, 'c, 'e, DeviceT>, is_polling: F) -> !
where F: Fn() -> bool {
let mut sockets = SocketSet::new(vec![]);
let boot_time = crate::arch::get_boot_time();
let mut counter: usize = 0;
loop {
let microseconds = ::arch::processor::get_timer_ticks() - boot_time;
let timestamp = Instant::from_millis(microseconds as i64 / 1000i64);
iface.poll(&mut sockets, timestamp)
.map(|_| {trace!("receive message {}", counter); counter = counter+1;})
.unwrap_or_else(|e| debug!("Poll: {:?}", e));
if is_polling() == false {
let wakeup_time = match iface.poll_delay(&sockets, timestamp) {
Some(duration) => {
// Calculate the absolute wakeup time in processor timer ticks out of the relative timeout in milliseconds.
if duration.millis() > 0 {
Some(::arch::processor::get_timer_ticks() + (duration.millis() as u64) * 1000)
} else {
Some(::arch::processor::get_timer_ticks() + 100)
}
},
None => None,
};
NET_SEM.acquire(wakeup_time);
}
}
}
\ No newline at end of file
......@@ -8,6 +8,7 @@
use alloc::collections::BTreeMap;
use core::ptr::read_volatile;
use core::str;
use core::sync::atomic::{AtomicBool, Ordering};
use smoltcp::time::Instant;
use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, Ipv4Address};
......@@ -31,6 +32,12 @@ use arch::x86_64::mm::paging::virt_to_phys;
#[cfg(target_arch="x86_64")]
use x86::io::*;
static POOLING: AtomicBool = AtomicBool::new(false);
fn is_pooling() -> bool {
POOLING.load(Ordering::SeqCst)
}
const UHYVE_IRQ_NET: u32 = 11;
const UHYVE_PORT_NETINFO: u16 = 0x600;
const UHYVE_PORT_NETWRITE: u16 = 0x640;
......@@ -160,7 +167,7 @@ extern "C" fn uhyve_thread(_arg: usize) {
::arch::irq::enable();
networkd(&mut iface);
networkd(&mut iface, is_pooling);
}
pub fn init() {
......@@ -177,7 +184,8 @@ pub fn init() {
#[cfg(target_arch="x86_64")]
extern "x86-interrupt" fn uhyve_irqhandler(_stack_frame: &mut ExceptionStackFrame) {
trace!("Receive network interrupt from uhyve");
debug!("Receive network interrupt from uhyve");
POOLING.store(true, Ordering::SeqCst);
crate::drivers::net::NET_SEM.release();
apic::eoi();
core_scheduler().scheduler();
......@@ -225,6 +233,7 @@ impl<'a> Device<'a> for UhyveNet {
Some((rx, tx))
} else {
POOLING.store(false, Ordering::SeqCst);
None
}
}
......@@ -263,10 +272,10 @@ impl RxToken {
}
impl phy::RxToken for RxToken {
fn consume<R, F>(mut self, _timestamp: Instant, f: F) -> Result<R>
where F: FnOnce(&mut [u8]) -> Result<R>
fn consume<R, F>(self, _timestamp: Instant, f: F) -> Result<R>
where F: FnOnce(&[u8]) -> Result<R>
{
let (first, _) = self.buffer.split_at_mut(self.len);
let (first, _) = self.buffer.split_at(self.len);
f(first)
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment