mod.rs 2.05 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
12
13
use alloc::boxed::Box;
use core::ffi::c_void;
use synch::spinlock::SpinlockIrqSave;
14

15
static NIC: SpinlockIrqSave<Option<Box<dyn NetworkInterface>>> = SpinlockIrqSave::new(None);
16

17
18
19
pub fn init() -> Result<(), ()> {
	let nic = uhyve::init()?;
	*NIC.lock() = Some(nic);
20

21
	info!("Network initialized!");
Stefan Lankes's avatar
Stefan Lankes committed
22

23
24
	Ok(())
}
Stefan Lankes's avatar
Stefan Lankes committed
25

26
27
28
29
30
31
32
33
34
35
36
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);
	/// initialize network and returns basic network configuration
	fn init(
		&mut self,
		sem: *const c_void,
		ip: &mut [u8; 4],
		gateway: &mut [u8; 4],
Stefan Lankes's avatar
Stefan Lankes committed
37
		mac: &mut [u8; 18],
38
39
40
41
42
43
	) -> i32;
	/// read packet from network interface
	fn read(&mut self, buf: usize, len: usize) -> usize;
	/// writr packet to the network interface
	fn write(&self, buf: usize, len: usize) -> usize;
}
Stefan Lankes's avatar
Stefan Lankes committed
44

45
46
47
48
49
#[no_mangle]
pub extern "C" fn sys_network_init(
	sem: *const c_void,
	ip: &mut [u8; 4],
	gateway: &mut [u8; 4],
Stefan Lankes's avatar
Stefan Lankes committed
50
	mac: &mut [u8; 18],
51
52
53
54
) -> i32 {
	match &mut *NIC.lock() {
		Some(nic) => nic.init(sem, ip, gateway, mac),
		None => -1,
Stefan Lankes's avatar
Stefan Lankes committed
55
	}
56
}
57

58
59
60
61
62
63
64
#[no_mangle]
pub extern "C" fn sys_is_polling() -> bool {
	match &*NIC.lock() {
		Some(nic) => nic.is_polling(),
		None => false,
	}
}
Stefan Lankes's avatar
Stefan Lankes committed
65

66
67
68
69
70
71
72
#[no_mangle]
pub extern "C" fn sys_set_polling(mode: bool) {
	match &mut *NIC.lock() {
		Some(nic) => nic.set_polling(mode),
		None => {}
	}
}
Stefan Lankes's avatar
Stefan Lankes committed
73

74
75
76
77
78
79
80
#[no_mangle]
pub extern "C" fn sys_netread(buf: usize, len: usize) -> usize {
	match &mut *NIC.lock() {
		Some(nic) => nic.read(buf, len),
		None => 0,
	}
}
81

82
83
84
85
86
#[no_mangle]
pub extern "C" fn sys_netwrite(buf: usize, len: usize) -> usize {
	match &*NIC.lock() {
		Some(nic) => nic.write(buf, len),
		None => 0,
87
	}
88
}