Quellcode durchsuchen

[vsock] Add method to send credit update manually (#110)

* [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]>
Alice Wang vor 1 Jahr
Ursprung
Commit
321e56ab66
2 geänderte Dateien mit 13 neuen und 1 gelöschten Zeilen
  1. 1 1
      examples/aarch64/src/main.rs
  2. 12 0
      src/device/socket/connectionmanager.rs

+ 1 - 1
examples/aarch64/src/main.rs

@@ -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)?;

+ 12 - 0
src/device/socket/connectionmanager.rs

@@ -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 {