Skip to content

Commit

Permalink
[vsock] Add method to send credit update manually (#110)
Browse files Browse the repository at this point in the history
* [vsock] Fix credit update in recv() to ensure message transimission

This PR addresses a bug in the vsock connection manager. Prior to
the PR, the guest doesn't update the host after it processes the
local buffer. As a result, when the receiving message length
reaches its buffer capacity, the host could mistakenly stop
sending the message, unaware that the guest still had available
buffer.
This fix ensures that the credit is correctly updated processing
the local buffer, allowing for continuous message transmission
between the host and the guest.

* Update test

* Minor adjustment to retrigger CI

* Minor change to retrigger CI

* Adjust format in examples/aarch64

* Add API to update credit

* Add API is_recv_buffer_empty() in ConnectionManager

* Change is_empty to available

---------

Co-authored-by: Alice Wang <[email protected]>
  • Loading branch information
aliciawyy and Alice Wang authored Sep 15, 2023
1 parent 9bc3201 commit 321e56a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/aarch64/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ fn virtio_socket<T: Transport>(transport: T) -> virtio_drivers::Result<()> {
for k in 0..EXCHANGE_NUM {
let mut buffer = [0u8; 24];
let socket_event = socket.wait_for_event()?;
let VsockEventType::Received {length, ..} = socket_event.event_type else {
let VsockEventType::Received { length, .. } = socket_event.event_type else {
panic!("Received unexpected socket event {:?}", socket_event);
};
let read_length = socket.recv(host_address, port, &mut buffer)?;
Expand Down
12 changes: 12 additions & 0 deletions src/device/socket/connectionmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,18 @@ impl<H: Hal, T: Transport> VsockConnectionManager<H, T> {
Ok(bytes_read)
}

/// Returns the number of bytes currently available in the recv buffer.
pub fn recv_buffer_available_bytes(&mut self, peer: VsockAddr, src_port: u32) -> Result<usize> {
let (_, connection) = get_connection(&mut self.connections, peer, src_port)?;
Ok(connection.buffer.available())
}

/// Sends a credit update to the given peer.
pub fn update_credit(&mut self, peer: VsockAddr, src_port: u32) -> Result {
let (_, connection) = get_connection(&mut self.connections, peer, src_port)?;
self.driver.credit_update(&connection.info)
}

/// Blocks until we get some event from the vsock device.
pub fn wait_for_event(&mut self) -> Result<VsockEvent> {
loop {
Expand Down

0 comments on commit 321e56a

Please sign in to comment.