Skip to content

Commit

Permalink
Merge pull request #817 from stlankes/net2
Browse files Browse the repository at this point in the history
disable network interrupts in polling mode
  • Loading branch information
mkroening authored Aug 3, 2023
2 parents 0f4e8e3 + 8f36730 commit 03cd1cf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 46 deletions.
9 changes: 9 additions & 0 deletions src/executor/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,9 @@ where
// be sure that we are not interrupted by a timer, which is able
// to call `reschedule`
without_interrupts(|| {
// avoid network interrupts
get_network_driver().unwrap().lock().set_polling_mode(true);

let start = now();
let waker = core::task::Waker::noop();
let mut cx = Context::from_waker(&waker);
Expand All @@ -349,6 +352,9 @@ where
.map(|d| crate::arch::processor::get_timer_ticks() + d.total_micros());
core_scheduler().add_network_timer(wakeup_time);

// allow network interrupts
get_network_driver().unwrap().lock().set_polling_mode(false);

return t;
}

Expand All @@ -358,6 +364,9 @@ where
.map(|d| crate::arch::processor::get_timer_ticks() + d.total_micros());
core_scheduler().add_network_timer(wakeup_time);

// allow network interrupts
get_network_driver().unwrap().lock().set_polling_mode(false);

return Err(-crate::errno::ETIME);
}
}
Expand Down
64 changes: 18 additions & 46 deletions src/fd/socket/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,34 +69,21 @@ impl<T> Socket<T> {
async fn async_read(&self, buffer: &mut [u8]) -> Result<isize, i32> {
future::poll_fn(|cx| {
self.with(|socket| {
if socket.can_recv() {
return Poll::Ready(
if !socket.is_active() {
Poll::Ready(Err(-crate::errno::EIO))
} else if socket.can_recv() {
Poll::Ready(
socket
.recv(|data| {
let len = core::cmp::min(buffer.len(), data.len());
buffer[..len].copy_from_slice(&data[..len]);
(len, isize::try_from(len).unwrap())
})
.map_err(|_| -crate::errno::EIO),
);
}

match socket.state() {
tcp::State::FinWait1
| tcp::State::FinWait2
| tcp::State::Closed
| tcp::State::Closing
| tcp::State::CloseWait
| tcp::State::TimeWait => Poll::Ready(Err(-crate::errno::EIO)),
_ => {
if socket.can_recv() {
warn!("async_read: Unable to consume data");
Poll::Ready(Ok(0))
} else {
socket.register_recv_waker(cx.waker());
Poll::Pending
}
}
)
} else {
socket.register_recv_waker(cx.waker());
Poll::Pending
}
})
})
Expand All @@ -109,36 +96,21 @@ impl<T> Socket<T> {
while pos < buffer.len() {
let n = future::poll_fn(|cx| {
self.with(|socket| {
if socket.can_send() {
return Poll::Ready(
if !socket.is_active() {
Poll::Ready(Err(-crate::errno::EIO))
} else if socket.can_send() {
Poll::Ready(
socket
.send_slice(&buffer[pos..])
.map_err(|_| -crate::errno::EIO),
);
}

if pos > 0 {
)
} else if pos > 0 {
// we already send some data => return 0 as signal to stop the
// async write
return Poll::Ready(Ok(0));
}

match socket.state() {
tcp::State::FinWait1
| tcp::State::FinWait2
| tcp::State::Closed
| tcp::State::Closing
| tcp::State::CloseWait
| tcp::State::TimeWait => Poll::Ready(Err(-crate::errno::EIO)),
_ => {
if socket.can_send() {
warn!("async_write: Unable to consume data");
Poll::Ready(Ok(0))
} else {
socket.register_send_waker(cx.waker());
Poll::Pending
}
}
Poll::Ready(Ok(0))
} else {
socket.register_send_waker(cx.waker());
Poll::Pending
}
})
})
Expand Down

0 comments on commit 03cd1cf

Please sign in to comment.