Skip to content

Commit

Permalink
Allow vsock per-connection buffer capacity to be configured.
Browse files Browse the repository at this point in the history
  • Loading branch information
qwandor committed Jun 5, 2024
1 parent 6528d02 commit 7997277
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/device/socket/connectionmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use core::hint::spin_loop;
use log::debug;
use zerocopy::FromZeroes;

const PER_CONNECTION_BUFFER_CAPACITY: usize = 1024;
const DEFAULT_PER_CONNECTION_BUFFER_CAPACITY: usize = 1024;

/// A higher level interface for VirtIO socket (vsock) devices.
///
Expand Down Expand Up @@ -49,6 +49,7 @@ pub struct VsockConnectionManager<
const RX_BUFFER_SIZE: usize = DEFAULT_RX_BUFFER_SIZE,
> {
driver: VirtIOSocket<H, T, RX_BUFFER_SIZE>,
per_connection_buffer_capacity: usize,
connections: Vec<Connection>,
listening_ports: Vec<u32>,
}
Expand All @@ -63,12 +64,12 @@ struct Connection {
}

impl Connection {
fn new(peer: VsockAddr, local_port: u32) -> Self {
fn new(peer: VsockAddr, local_port: u32, buffer_capacity: usize) -> Self {
let mut info = ConnectionInfo::new(peer, local_port);
info.buf_alloc = PER_CONNECTION_BUFFER_CAPACITY.try_into().unwrap();
info.buf_alloc = buffer_capacity.try_into().unwrap();
Self {
info,
buffer: RingBuffer::new(PER_CONNECTION_BUFFER_CAPACITY),
buffer: RingBuffer::new(buffer_capacity),
peer_requested_shutdown: false,
}
}
Expand All @@ -77,10 +78,20 @@ impl Connection {
impl<H: Hal, T: Transport> VsockConnectionManager<H, T> {
/// Construct a new connection manager wrapping the given low-level VirtIO socket driver.
pub fn new(driver: VirtIOSocket<H, T>) -> Self {
Self::new_with_capacity(driver, DEFAULT_PER_CONNECTION_BUFFER_CAPACITY)
}

/// Construct a new connection manager wrapping the given low-level VirtIO socket driver, with
/// the given per-connection buffer capacity.
pub fn new_with_capacity(
driver: VirtIOSocket<H, T>,
per_connection_buffer_capacity: usize,
) -> Self {
Self {
driver,
connections: Vec::new(),
listening_ports: Vec::new(),
per_connection_buffer_capacity,
}
}

Expand Down Expand Up @@ -113,7 +124,8 @@ impl<H: Hal, T: Transport> VsockConnectionManager<H, T> {
return Err(SocketError::ConnectionExists.into());
}

let new_connection = Connection::new(destination, src_port);
let new_connection =
Connection::new(destination, src_port, self.per_connection_buffer_capacity);

self.driver.connect(&new_connection.info)?;
debug!("Connection requested: {:?}", new_connection.info);
Expand All @@ -132,6 +144,7 @@ impl<H: Hal, T: Transport> VsockConnectionManager<H, T> {
pub fn poll(&mut self) -> Result<Option<VsockEvent>> {
let guest_cid = self.driver.guest_cid();
let connections = &mut self.connections;
let per_connection_buffer_capacity = self.per_connection_buffer_capacity;

let result = self.driver.poll(|event, body| {
let connection = get_connection_for_event(connections, &event, guest_cid);
Expand All @@ -147,7 +160,11 @@ impl<H: Hal, T: Transport> VsockConnectionManager<H, T> {
}
// Add the new connection to our list, at least for now. It will be removed again
// below if we weren't listening on the port.
connections.push(Connection::new(event.source, event.destination.port));
connections.push(Connection::new(
event.source,
event.destination.port,
per_connection_buffer_capacity,
));
connections.last_mut().unwrap()
} else {
return Ok(None);
Expand Down

0 comments on commit 7997277

Please sign in to comment.