Skip to content

Commit

Permalink
vsock: pop all the received packets
Browse files Browse the repository at this point in the history
After receiving from vsock transport layer, pop all the vsock packet
from device and try to fill the caller's buffer as much as possible.

Signed-off-by: Jiaqi Gao <[email protected]>
  • Loading branch information
gaojiaqi7 authored and liuw1 committed Nov 14, 2024
1 parent 2af2773 commit 1e813ea
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions src/devices/vsock/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,25 +342,40 @@ impl VsockStream {
return Err(VsockError::Illegal);
}

while self.data_queue.is_empty() {
self.recv_packet_connected()?;
if self.data_queue.is_empty() {
loop {
self.recv_packet_connected()?;

// If there are received vsock packets, continue to pop them out and insert to the
// `data_queue`. If there is no vsock packet left in the device, break the loop.
if !VSOCK_DEVICE
.lock()
.get_mut()
.ok_or(VsockError::DeviceNotAvailable)?
.transport
.can_recv()
{
break;
}
}
}

let mut recvd = 0;
if !self.data_queue.is_empty() {
let mut used = 0;
while !self.data_queue.is_empty() && used < buf.len() {
let head = self.data_queue.front_mut().unwrap();
if head.len() <= buf.len() {
buf[..head.len()].copy_from_slice(head);
recvd = head.len();
let free = buf.len() - used;
if head.len() <= free {
buf[used..used + head.len()].copy_from_slice(head);
used += head.len();
self.data_queue.pop_front();
} else {
buf.copy_from_slice(&head[..buf.len()]);
recvd = buf.len();
head.drain(..buf.len());
buf[used..].copy_from_slice(&head[..free]);
used += free;
head.drain(..free);
}
}

Ok(recvd)
Ok(used)
}

fn reset(&mut self) -> Result {
Expand Down

0 comments on commit 1e813ea

Please sign in to comment.