Repository: firecracker-microvm/firecracker
File: src/vmm/src/devices/virtio/vsock/csm/connection.rs (Rust)

Code before:
        // attempt to drain the TX buffer then.
        if !self.tx_buf.is_empty() {
            return pkt
                .write_from_offset_to(&mut self.tx_buf, 0, len)
                .map(|_| ());
        }

        // The TX buffer is empty, so we can try to write straight to the host stream.
        let written = match pkt.write_from_offset_to(&mut self.stream, 0, len) {
            Ok(cnt) => cnt,
            Err(VsockError::GuestMemoryMmap(GuestMemoryError::IOError(err)))
                if err.kind() == ErrorKind::WouldBlock =>
            {
                // Absorb any would-block errors, since we can always try again later.
                0
            }
            Err(err) => {
                // We don't know how to handle any other write error, so we'll send it up
                // the call chain.
                METRICS.tx_write_fails.inc();
                return Err(err);
            }
        };
        // Move the "forwarded bytes" counter ahead by how much we were able to send out.
        // Safe to unwrap because the maximum value is pkt.len(), which is a u32.
        self.fwd_cnt += wrap_usize_to_u32(written.try_into().unwrap());
        METRICS.tx_bytes_count.add(written as u64);

        // If we couldn't write the whole slice, we'll need to push the remaining data to our
        // buffer.
        if written < len {
            pkt.write_from_offset_to(&mut self.tx_buf, written, len - written)?;
        }

        Ok(())
    }

    /// Check if the credit information the peer has last received from us is outdated.
    fn peer_needs_credit_update(&self) -> bool {
        let peer_seen_free_buf =
            Wrapping(defs::CONN_TX_BUF_SIZE) - (self.fwd_cnt - self.last_fwd_cnt_to_peer);
        peer_seen_free_buf < Wrapping(defs::CONN_CREDIT_UPDATE_THRESHOLD)
    }

    /// Check if we need to ask the peer for a credit update before sending any more data its
    /// way.
    fn need_credit_update_from_peer(&self) -> bool {
        self.peer_avail_credit() == 0
    }

    /// Get the maximum number of bytes that we can send to our peer, without overflowing its

Review comment (suggestion):
```suggestion
        self.fwd_cnt += written;
```

Code after:
        if !self.tx_buf.is_empty() {
            return pkt
                .write_from_offset_to(&mut self.tx_buf, 0, len)
                .map(|_| ());
        }

        // The TX buffer is empty, so we can try to write straight to the host stream.
        let written = match pkt.write_from_offset_to(&mut self.stream, 0, len) {
            Ok(cnt) => cnt,
            Err(VsockError::GuestMemoryMmap(GuestMemoryError::IOError(err)))
                if err.kind() == ErrorKind::WouldBlock =>
            {
                // Absorb any would-block errors, since we can always try again later.
                0
            }
            Err(err) => {
                // We don't know how to handle any other write error, so we'll send it up
                // the call chain.
                METRICS.tx_write_fails.inc();
                return Err(err);
            }
        };
        // Move the "forwarded bytes" counter ahead by how much we were able to send out.
        // Safe to unwrap because the maximum value is pkt.len(), which is a u32.
        self.fwd_cnt += written;
        METRICS.tx_bytes_count.add(written as u64);

        // If we couldn't write the whole slice, we'll need to push the remaining data to our
        // buffer.
        if written < len {
            pkt.write_from_offset_to(&mut self.tx_buf, written, len - written)?;
        }

        Ok(())
    }

    /// Check if the credit information the peer has last received from us is outdated.
    fn peer_needs_credit_update(&self) -> bool {
        let peer_seen_free_buf =
            Wrapping(defs::CONN_TX_BUF_SIZE) - (self.fwd_cnt - self.last_fwd_cnt_to_peer);
        peer_seen_free_buf < Wrapping(defs::CONN_CREDIT_UPDATE_THRESHOLD)
    }

    /// Check if we need to ask the peer for a credit update before sending any more data its
    /// way.
    fn need_credit_update_from_peer(&self) -> bool {
        self.peer_avail_credit() == 0
    }

    /// Get the maximum number of bytes that we can send to our peer, without overflowing its
    /// buffer.