mod.rs 1.58 KB
Newer Older
1
2
3
4
5
6
7
// Copyright (c) 2019 Stefan Lankes, RWTH Aachen University
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

8
//pub mod rtl8139;
9
pub mod uhyve;
10

11
use alloc::boxed::Box;
12
use synch::semaphore::*;
13

14
15
static mut NIC: Option<Box<dyn NetworkInterface>> = None;
static NET_SEM: Semaphore = Semaphore::new(0);
16

17
18
pub fn init() -> Result<(), ()> {
	let nic = uhyve::init()?;
19
20
21
	unsafe {
		NIC = Some(nic);
	}
22

23
	info!("Network initialized!");
Stefan Lankes's avatar
Stefan Lankes committed
24

25
26
	Ok(())
}
Stefan Lankes's avatar
Stefan Lankes committed
27

28
29
30
31
32
pub trait NetworkInterface {
	/// check if the driver in polling mode
	fn is_polling(&self) -> bool;
	/// set driver in polling/non-polling mode
	fn set_polling(&mut self, mode: bool);
33
34
	/// get mac address
	fn get_mac_address(&self) -> [u8; 6];
35
}
36

37
#[no_mangle]
38
39
40
41
42
43
pub fn uhyve_is_polling() -> bool {
	unsafe {
		match &NIC {
			Some(nic) => nic.is_polling(),
			None => false,
		}
44
45
	}
}
Stefan Lankes's avatar
Stefan Lankes committed
46

47
#[no_mangle]
48
49
50
51
52
53
pub fn uhyve_set_polling(mode: bool) {
	unsafe {
		match &mut NIC {
			Some(nic) => nic.set_polling(mode),
			None => {}
		}
54
55
	}
}
Stefan Lankes's avatar
Stefan Lankes committed
56

57
#[no_mangle]
58
59
60
61
62
63
64
pub fn uhyve_netwait(millis: Option<u64>) {
	if uhyve_is_polling() == false {
		let wakeup_time = match millis {
			Some(ms) => Some(::arch::processor::get_timer_ticks() + ms * 1000),
			None => None,
		};
		NET_SEM.acquire(wakeup_time);
65
66
	}
}
67

68
#[no_mangle]
69
70
71
72
73
74
pub fn uhyve_get_mac_address() -> [u8; 6] {
	unsafe {
		match &NIC {
			Some(nic) => nic.get_mac_address(),
			None => [0; 6],
		}
75
	}
76
}