vsock: reduce credit update messages sent to guest

Earlier we were sending credit update messages to guest
everytime data was read, now we send it only if the available
space is less than 1/4th of the tx buffer size.

benchmarks:

host->guest[Gbps]

before   1.45
after    1.51

guest->host[Gbps]

before   5.68
after    6.07

Note: I did 3 runs of 10 secs and took average of all,
they are relative results.

Fixes #317

Signed-off-by: Ramyak Mehra <rmehra_be19@thapar.edu>

[SG: fixed S-o-b and small things in the commit description]

Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
This commit is contained in:
Ramyak Mehra 2023-04-15 01:44:39 +05:30 committed by Viresh Kumar
parent 0b7f536998
commit e24d3e52fc

View File

@ -52,6 +52,8 @@ pub(crate) struct VsockConnection<S> {
pub epoll_fd: RawFd,
/// Local tx buffer.
pub tx_buf: LocalTxBuf,
/// Local tx buffer size
tx_buffer_size: u32,
}
impl<S: AsRawFd + Read + Write> VsockConnection<S> {
@ -81,6 +83,7 @@ impl<S: AsRawFd + Read + Write> VsockConnection<S> {
rx_cnt: Wrapping(0),
epoll_fd,
tx_buf: LocalTxBuf::new(tx_buffer_size),
tx_buffer_size,
}
}
@ -114,6 +117,7 @@ impl<S: AsRawFd + Read + Write> VsockConnection<S> {
rx_cnt: Wrapping(0),
epoll_fd,
tx_buf: LocalTxBuf::new(tx_buffer_size),
tx_buffer_size,
}
}
@ -308,8 +312,18 @@ impl<S: AsRawFd + Read + Write> VsockConnection<S> {
if written_count > 0 {
// Increment forwarded count by number of bytes written to the stream
self.fwd_cnt += Wrapping(written_count as u32);
// TODO: https://github.com/torvalds/linux/commit/c69e6eafff5f725bc29dcb8b52b6782dca8ea8a2
self.rx_queue.enqueue(RxOps::CreditUpdate);
// At what point in available credits should we send a credit update.
// This is set to 1/4th of the tx buffer size. If we keep it too low,
// we will end up sending too many credit updates. If we keep it too
// high, we will end up sending too few credit updates and cause stalls.
// Stalls are more bad than too many credit updates.
let free_space = self
.tx_buffer_size
.wrapping_sub((self.fwd_cnt - self.last_fwd_cnt).0);
if free_space < self.tx_buffer_size / 4 {
self.rx_queue.enqueue(RxOps::CreditUpdate);
}
}
if written_count != buf.len() {
@ -332,7 +346,7 @@ impl<S: AsRawFd + Read + Write> VsockConnection<S> {
.set_src_port(self.local_port)
.set_dst_port(self.peer_port)
.set_type(VSOCK_TYPE_STREAM)
.set_buf_alloc(self.tx_buf.get_buf_size())
.set_buf_alloc(self.tx_buffer_size)
.set_fwd_cnt(self.fwd_cnt.0)
}