Skip to content

Commit fcfb72e

Browse files
committed
fix(virtio-net): free up send capacity before checking it
Before checking the send capacity to issue a send token, we need to poll to get the capacity back from completed sends.
1 parent ee6e5d2 commit fcfb72e

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/drivers/net/virtio/mod.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,6 @@ impl smoltcp::phy::TxToken for TxToken<'_> {
253253
// Thus, we bypass the Drop implementation that would do that prematurely and let the call to poll in the next
254254
// call to this function return the capacity.
255255
let mut token = ManuallyDrop::new(self);
256-
// We need to poll to get the queue to remove elements from the table and make space for
257-
// what we are about to add
258-
*token.send_capacity += token.send_vqs.poll() * u32::from(BUFF_PER_PACKET);
259-
260256
assert!(len <= usize::try_from(token.send_vqs.buf_size).unwrap());
261257
let mut packet = Vec::with_capacity_in(len, DeviceAlloc);
262258
let result = unsafe {
@@ -428,9 +424,10 @@ impl smoltcp::phy::Device for VirtioNetDriver {
428424
&mut self,
429425
_timestamp: smoltcp::time::Instant,
430426
) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)> {
431-
if self.inner.recv_vqs.has_packet()
432-
&& self.inner.send_capacity >= u32::from(BUFF_PER_PACKET)
433-
{
427+
if self.inner.recv_vqs.has_packet() && {
428+
self.free_up_send_capacity();
429+
self.inner.send_capacity >= u32::from(BUFF_PER_PACKET)
430+
} {
434431
self.inner.send_capacity -= u32::from(BUFF_PER_PACKET);
435432
Some((
436433
RxToken {
@@ -449,6 +446,7 @@ impl smoltcp::phy::Device for VirtioNetDriver {
449446
}
450447

451448
fn transmit(&mut self, _timestamp: smoltcp::time::Instant) -> Option<Self::TxToken<'_>> {
449+
self.free_up_send_capacity();
452450
if self.inner.send_capacity >= u32::from(BUFF_PER_PACKET) {
453451
self.inner.send_capacity -= u32::from(BUFF_PER_PACKET);
454452
Some(TxToken {
@@ -609,6 +607,11 @@ impl VirtioNetDriver<Init> {
609607

610608
Some((ip_header_len, csum_offset))
611609
}
610+
611+
fn free_up_send_capacity(&mut self) {
612+
// We need to poll to get the queue to remove elements from the table and open up capacity if possible.
613+
self.inner.send_capacity += self.inner.send_vqs.poll() * u32::from(BUFF_PER_PACKET);
614+
}
612615
}
613616

614617
impl VirtioNetDriver<Uninit> {

0 commit comments

Comments
 (0)