forked from hermit-os/hermit-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.rs
515 lines (446 loc) · 12.4 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
pub mod device;
mod executor;
mod waker;
#[cfg(target_arch = "aarch64")]
use aarch64::regs::*;
#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::_rdtsc;
use std::convert::TryInto;
use std::ops::DerefMut;
use std::str::FromStr;
use std::sync::atomic::{AtomicU16, Ordering};
use std::sync::Mutex;
use std::task::{Context, Poll};
use std::u16;
#[cfg(feature = "dhcpv4")]
use smoltcp::dhcp::Dhcpv4Client;
use smoltcp::phy::Device;
#[cfg(feature = "trace")]
use smoltcp::phy::EthernetTracer;
use smoltcp::socket::{SocketHandle, SocketSet, TcpSocket, TcpSocketBuffer, TcpState};
use smoltcp::time::{Duration, Instant};
use smoltcp::wire::IpAddress;
#[cfg(feature = "dhcpv4")]
use smoltcp::wire::{IpCidr, Ipv4Address, Ipv4Cidr};
use smoltcp::Error;
use futures_lite::future;
use crate::net::device::HermitNet;
use crate::net::executor::{block_on, poll_on, spawn};
use crate::net::waker::WakerRegistration;
pub(crate) enum NetworkState {
Missing,
InitializationFailed,
Initialized(NetworkInterface<HermitNet>),
}
impl NetworkState {
fn as_nic_mut(&mut self) -> Result<&mut NetworkInterface<HermitNet>, &'static str> {
match self {
NetworkState::Initialized(nic) => Ok(nic),
_ => Err("Network is not initialized!"),
}
}
}
lazy_static! {
static ref NIC: Mutex<NetworkState> = Mutex::new(NetworkState::Missing);
}
extern "C" {
fn sys_yield();
fn sys_spawn(
id: *mut Tid,
func: extern "C" fn(usize),
arg: usize,
prio: u8,
selector: isize,
) -> i32;
fn sys_netwait();
}
pub type Handle = SocketHandle;
pub type Tid = u32;
/// Default keep alive interval in milliseconds
const DEFAULT_KEEP_ALIVE_INTERVAL: u64 = 75000;
static LOCAL_ENDPOINT: AtomicU16 = AtomicU16::new(0);
pub(crate) struct NetworkInterface<T: for<'a> Device<'a>> {
#[cfg(feature = "trace")]
pub iface: smoltcp::iface::EthernetInterface<'static, EthernetTracer<T>>,
#[cfg(not(feature = "trace"))]
pub iface: smoltcp::iface::EthernetInterface<'static, T>,
pub sockets: SocketSet<'static>,
#[cfg(feature = "dhcpv4")]
dhcp: Dhcpv4Client,
#[cfg(feature = "dhcpv4")]
prev_cidr: Ipv4Cidr,
waker: WakerRegistration,
}
impl<T> NetworkInterface<T>
where
T: for<'a> Device<'a>,
{
pub(crate) fn create_handle(&mut self) -> Result<Handle, ()> {
let tcp_rx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
let tcp_tx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
let tcp_socket = TcpSocket::new(tcp_rx_buffer, tcp_tx_buffer);
let tcp_handle = self.sockets.add(tcp_socket);
Ok(tcp_handle)
}
pub(crate) fn wake(&mut self) {
self.waker.wake()
}
pub(crate) fn poll_common(&mut self, timestamp: Instant) {
while self
.iface
.poll(&mut self.sockets, timestamp)
.unwrap_or(true)
{
// just to make progress
}
#[cfg(feature = "dhcpv4")]
let config = self
.dhcp
.poll(&mut self.iface, &mut self.sockets, timestamp)
.unwrap_or_else(|e| {
debug!("DHCP: {:?}", e);
None
});
#[cfg(feature = "dhcpv4")]
config.map(|config| {
debug!("DHCP config: {:?}", config);
if let Some(cidr) = config.address {
if cidr != self.prev_cidr && !cidr.address().is_unspecified() {
self.iface.update_ip_addrs(|addrs| {
addrs.iter_mut().next().map(|addr| {
*addr = IpCidr::Ipv4(cidr);
});
});
self.prev_cidr = cidr;
debug!("Assigned a new IPv4 address: {}", cidr);
}
}
config.router.map(|router| {
self.iface
.routes_mut()
.add_default_ipv4_route(router)
.unwrap()
});
self.iface.routes_mut().update(|routes_map| {
routes_map
.get(&IpCidr::new(Ipv4Address::UNSPECIFIED.into(), 0))
.map(|default_route| {
debug!("Default gateway: {}", default_route.via_router);
});
});
if config.dns_servers.iter().any(|s| s.is_some()) {
debug!("DNS servers:");
for dns_server in config.dns_servers.iter().filter_map(|s| *s) {
debug!("- {}", dns_server);
}
}
});
}
pub(crate) fn poll(&mut self, cx: &mut Context<'_>, timestamp: Instant) {
self.waker.register(cx.waker());
self.poll_common(timestamp);
}
pub(crate) fn poll_delay(&mut self, timestamp: Instant) -> Option<Duration> {
self.iface.poll_delay(&self.sockets, timestamp)
}
}
pub(crate) struct AsyncSocket(Handle);
impl AsyncSocket {
pub(crate) fn new() -> Self {
let handle = NIC
.lock()
.unwrap()
.as_nic_mut()
.unwrap()
.create_handle()
.unwrap();
Self(handle)
}
fn with<R>(&self, f: impl FnOnce(&mut TcpSocket) -> R) -> R {
let mut guard = NIC.lock().unwrap();
let nic = guard.as_nic_mut().unwrap();
let res = {
let mut s = nic.sockets.get::<TcpSocket>(self.0);
f(&mut *s)
};
nic.wake();
res
}
pub(crate) async fn connect(&self, ip: &[u8], port: u16) -> Result<Handle, Error> {
let address = IpAddress::from_str(std::str::from_utf8(ip).map_err(|_| Error::Illegal)?)
.map_err(|_| Error::Illegal)?;
self.with(|s| {
s.connect(
(address, port),
LOCAL_ENDPOINT.fetch_add(1, Ordering::SeqCst),
)
})
.map_err(|_| Error::Illegal)?;
future::poll_fn(|cx| {
self.with(|s| match s.state() {
TcpState::Closed | TcpState::TimeWait => Poll::Ready(Err(Error::Unaddressable)),
TcpState::Listen => Poll::Ready(Err(Error::Illegal)),
TcpState::SynSent | TcpState::SynReceived => {
s.register_send_waker(cx.waker());
Poll::Pending
}
_ => Poll::Ready(Ok(self.0)),
})
})
.await
}
pub(crate) async fn accept(&self, port: u16) -> Result<(IpAddress, u16), Error> {
self.with(|s| s.listen(port).map_err(|_| Error::Illegal))?;
future::poll_fn(|cx| {
self.with(|s| {
if s.is_active() {
Poll::Ready(Ok(()))
} else {
match s.state() {
TcpState::Closed
| TcpState::Closing
| TcpState::FinWait1
| TcpState::FinWait2 => Poll::Ready(Err(Error::Illegal)),
_ => {
s.register_recv_waker(cx.waker());
Poll::Pending
}
}
}
})
})
.await?;
let mut guard = NIC.lock().unwrap();
let nic = guard.as_nic_mut().map_err(|_| Error::Illegal)?;
let mut socket = nic.sockets.get::<TcpSocket>(self.0);
socket.set_keep_alive(Some(Duration::from_millis(DEFAULT_KEEP_ALIVE_INTERVAL)));
let endpoint = socket.remote_endpoint();
Ok((endpoint.addr, endpoint.port))
}
pub(crate) async fn read(&self, buffer: &mut [u8]) -> Result<usize, Error> {
future::poll_fn(|cx| {
self.with(|s| match s.state() {
TcpState::FinWait1
| TcpState::FinWait2
| TcpState::Closed
| TcpState::Closing
| TcpState::TimeWait => Poll::Ready(Err(Error::Illegal)),
_ => {
if s.may_recv() {
Poll::Ready(s.recv_slice(buffer))
} else {
s.register_recv_waker(cx.waker());
Poll::Pending
}
}
})
})
.await
.map_err(|_| Error::Illegal)
}
pub(crate) async fn write(&self, buffer: &[u8]) -> Result<usize, Error> {
future::poll_fn(|cx| {
self.with(|s| match s.state() {
TcpState::FinWait1
| TcpState::FinWait2
| TcpState::Closed
| TcpState::Closing
| TcpState::TimeWait => Poll::Ready(Err(Error::Illegal)),
_ => {
if !s.may_recv() {
Poll::Ready(Ok(0))
} else if s.can_send() {
Poll::Ready(s.send_slice(buffer).map_err(|_| Error::Illegal))
} else {
s.register_send_waker(cx.waker());
Poll::Pending
}
}
})
})
.await
}
pub(crate) async fn close(&self) -> Result<(), Error> {
future::poll_fn(|cx| {
self.with(|s| match s.state() {
TcpState::FinWait1
| TcpState::FinWait2
| TcpState::Closed
| TcpState::Closing
| TcpState::TimeWait => Poll::Ready(Err(Error::Illegal)),
_ => {
if s.send_queue() > 0 {
s.register_send_waker(cx.waker());
Poll::Pending
} else {
s.close();
Poll::Ready(Ok(()))
}
}
})
})
.await?;
future::poll_fn(|cx| {
self.with(|s| match s.state() {
TcpState::FinWait1
| TcpState::FinWait2
| TcpState::Closed
| TcpState::Closing
| TcpState::TimeWait => Poll::Ready(Ok(())),
_ => {
s.register_send_waker(cx.waker());
Poll::Pending
}
})
})
.await
}
}
impl From<Handle> for AsyncSocket {
fn from(handle: Handle) -> Self {
AsyncSocket(handle)
}
}
#[cfg(target_arch = "x86_64")]
fn start_endpoint() -> u16 {
((unsafe { _rdtsc() as u64 }) % (u16::MAX as u64))
.try_into()
.unwrap()
}
#[cfg(target_arch = "aarch64")]
fn start_endpoint() -> u16 {
(CNTPCT_EL0.get() % (u16::MAX as u64)).try_into().unwrap()
}
pub(crate) fn network_delay(timestamp: Instant) -> Option<Duration> {
NIC.lock().unwrap().as_nic_mut().ok()?.poll_delay(timestamp)
}
pub(crate) async fn network_run() {
future::poll_fn(|cx| match NIC.lock().unwrap().deref_mut() {
NetworkState::Initialized(nic) => {
nic.poll(cx, Instant::now());
Poll::Pending
}
_ => Poll::Ready(()),
})
.await
}
extern "C" fn nic_thread(_: usize) {
loop {
unsafe { sys_netwait() };
trace!("Network thread checks the devices");
if let NetworkState::Initialized(nic) = NIC.lock().unwrap().deref_mut() {
nic.poll_common(Instant::now());
}
}
}
pub(crate) fn network_init() -> Result<(), ()> {
// initialize variable, which contains the next local endpoint
LOCAL_ENDPOINT.store(start_endpoint(), Ordering::SeqCst);
let mut guard = NIC.lock().unwrap();
*guard = NetworkInterface::<HermitNet>::new();
if let NetworkState::Initialized(nic) = guard.deref_mut() {
nic.poll_common(Instant::now());
// create thread, which manages the network stack
// use a higher priority to reduce the network latency
let mut tid: Tid = 0;
let ret = unsafe { sys_spawn(&mut tid, nic_thread, 0, 3, 0) };
if ret >= 0 {
debug!("Spawn network thread with id {}", tid);
}
spawn(network_run()).detach();
// switch to network thread
unsafe { sys_yield() };
}
Ok(())
}
#[no_mangle]
pub fn sys_tcp_stream_connect(ip: &[u8], port: u16, timeout: Option<u64>) -> Result<Handle, ()> {
let socket = AsyncSocket::new();
block_on(socket.connect(ip, port), timeout.map(Duration::from_millis))?.map_err(|_| ())
}
#[no_mangle]
pub fn sys_tcp_stream_read(handle: Handle, buffer: &mut [u8]) -> Result<usize, ()> {
let socket = AsyncSocket::from(handle);
poll_on(socket.read(buffer), None)?.map_err(|_| ())
}
#[no_mangle]
pub fn sys_tcp_stream_write(handle: Handle, buffer: &[u8]) -> Result<usize, ()> {
let socket = AsyncSocket::from(handle);
poll_on(socket.write(buffer), None)?.map_err(|_| ())
}
#[no_mangle]
pub fn sys_tcp_stream_close(handle: Handle) -> Result<(), ()> {
let socket = AsyncSocket::from(handle);
block_on(socket.close(), None)?.map_err(|_| ())
}
//ToDo: an enum, or at least constants would be better
#[no_mangle]
pub fn sys_tcp_stream_shutdown(handle: Handle, how: i32) -> Result<(), ()> {
match how {
0 /* Read */ => {
trace!("Shutdown::Read is not implemented");
Ok(())
},
1 /* Write */ => {
sys_tcp_stream_close(handle)
},
2 /* Both */ => {
sys_tcp_stream_close(handle)
},
_ => {
panic!("Invalid shutdown argument {}", how);
},
}
}
#[no_mangle]
pub fn sys_tcp_stream_set_read_timeout(_handle: Handle, _timeout: Option<u64>) -> Result<(), ()> {
Err(())
}
#[no_mangle]
pub fn sys_tcp_stream_get_read_timeout(_handle: Handle) -> Result<Option<u64>, ()> {
Err(())
}
#[no_mangle]
pub fn sys_tcp_stream_set_write_timeout(_handle: Handle, _timeout: Option<u64>) -> Result<(), ()> {
Err(())
}
#[no_mangle]
pub fn sys_tcp_stream_get_write_timeout(_handle: Handle) -> Result<Option<u64>, ()> {
Err(())
}
#[deprecated(since = "0.1.14", note = "Please don't use this function")]
#[no_mangle]
pub fn sys_tcp_stream_duplicate(_handle: Handle) -> Result<Handle, ()> {
Err(())
}
#[no_mangle]
pub fn sys_tcp_stream_peek(_handle: Handle, _buf: &mut [u8]) -> Result<usize, ()> {
Err(())
}
#[no_mangle]
pub fn sys_tcp_stream_set_nonblocking(_handle: Handle, _mode: bool) -> Result<(), ()> {
Err(())
}
#[no_mangle]
pub fn sys_tcp_stream_set_tll(_handle: Handle, _ttl: u32) -> Result<(), ()> {
Err(())
}
#[no_mangle]
pub fn sys_tcp_stream_get_tll(_handle: Handle) -> Result<u32, ()> {
Err(())
}
#[no_mangle]
pub fn sys_tcp_stream_peer_addr(handle: Handle) -> Result<(IpAddress, u16), ()> {
let mut guard = NIC.lock().unwrap();
let nic = guard.as_nic_mut().map_err(drop)?;
let mut socket = nic.sockets.get::<TcpSocket>(handle);
socket.set_keep_alive(Some(Duration::from_millis(DEFAULT_KEEP_ALIVE_INTERVAL)));
let endpoint = socket.remote_endpoint();
Ok((endpoint.addr, endpoint.port))
}
#[no_mangle]
pub fn sys_tcp_listener_accept(port: u16) -> Result<(Handle, IpAddress, u16), ()> {
let socket = AsyncSocket::new();
let (addr, port) = block_on(socket.accept(port), None)?.map_err(|_| ())?;
Ok((socket.0, addr, port))
}