Skip to content

Commit 75e74a6

Browse files
committed
virtio-net: allow specifying the buffer size
It is possible to set a receive buffer size when MRG_RXBUF is negotiated. Expose this ability to the user through the environment variable `HERMIT_MRG_RXBUF_SIZE`.
1 parent f1f7d93 commit 75e74a6

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/drivers/net/virtio/mod.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ cfg_if::cfg_if! {
1313
use alloc::boxed::Box;
1414
use alloc::vec::Vec;
1515
use core::mem::{MaybeUninit, transmute};
16+
use core::str::FromStr;
1617

1718
use smallvec::SmallVec;
1819
use smoltcp::phy::{Checksum, ChecksumCapabilities};
@@ -59,15 +60,24 @@ fn determine_mtu(dev_cfg: &NetDevCfg) -> u16 {
5960
}
6061
}
6162

62-
fn determine_buf_size(dev_cfg: &NetDevCfg) -> u32 {
63+
fn determine_rx_buf_size(dev_cfg: &NetDevCfg) -> u32 {
6364
// See Virtio specification v1.1 - 5.1.6.3.1 and 5.1.4.2
6465

6566
// Our desired minimum buffer size - we want it to be at least the MTU generally
6667
let mut min_buf_size = determine_mtu(dev_cfg).into();
6768

6869
// If VIRTIO_NET_F_MRG_RXBUF is negotiated, each buffer MUST be at least the size of the struct virtio_net_hdr.
6970
// We just use MTU in that case, but otherwise...
70-
if !dev_cfg.features.contains(virtio::net::F::MRG_RXBUF) {
71+
if dev_cfg.features.contains(virtio::net::F::MRG_RXBUF)
72+
&& let Some(my_mrg_rxbuf_size) = hermit_var!("HERMIT_MRG_RXBUF_SIZE")
73+
{
74+
let my_mrg_rxbuf_size = u32::from_str(&my_mrg_rxbuf_size).unwrap();
75+
assert!(
76+
my_mrg_rxbuf_size > 0,
77+
"VIRTIO does not allow buffer elements of size 0."
78+
);
79+
min_buf_size = my_mrg_rxbuf_size;
80+
} else {
7181
// If [...] are negotiated, the driver SHOULD populate the receive queue(s) with buffers of at least 65562 bytes.
7282
if dev_cfg.features.contains(virtio::net::F::GUEST_TSO4)
7383
|| dev_cfg.features.contains(virtio::net::F::GUEST_TSO6)
@@ -92,7 +102,7 @@ impl RxQueues {
92102
pub fn new(vqs: Vec<VirtQueue>, dev_cfg: &NetDevCfg) -> Self {
93103
Self {
94104
vqs,
95-
buf_size: determine_buf_size(dev_cfg),
105+
buf_size: determine_rx_buf_size(dev_cfg),
96106
}
97107
}
98108

@@ -167,7 +177,7 @@ impl TxQueues {
167177
pub fn new(vqs: Vec<VirtQueue>, dev_cfg: &NetDevCfg) -> Self {
168178
Self {
169179
vqs,
170-
buf_size: determine_buf_size(dev_cfg),
180+
buf_size: determine_mtu(dev_cfg).into(),
171181
}
172182
}
173183

0 commit comments

Comments
 (0)