Skip to content

Commit

Permalink
refactor(virtio): Simplify read_config
Browse files Browse the repository at this point in the history
`io::Write` has higher overhead and unnecessary error handling. Slices can be copied without it.

Signed-off-by: Kornel <kornel@cloudflare.com>
  • Loading branch information
kornelski committed Mar 20, 2024
1 parent 7701563 commit e55297a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 36 deletions.
18 changes: 5 additions & 13 deletions src/vmm/src/devices/virtio/balloon/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::io::Write;
use std::sync::atomic::AtomicU32;
use std::sync::Arc;
use std::time::Duration;
use std::{cmp, fmt};
use std::fmt;

use log::error;
use serde::Serialize;
Expand Down Expand Up @@ -594,19 +594,11 @@ impl VirtioDevice for Balloon {
}

fn read_config(&self, offset: u64, mut data: &mut [u8]) {
let config_space_bytes = self.config_space.as_slice();
let config_len = config_space_bytes.len() as u64;
if offset >= config_len {
if let Some(config_space_bytes) = self.config_space.as_slice().get(u64_to_usize(offset)..) {
let len = config_space_bytes.len().min(data.len());
data[..len].copy_from_slice(&config_space_bytes[..len]);
} else {
error!("Failed to read config space");
return;
}

if let Some(end) = offset.checked_add(data.len() as u64) {
// This write can't fail, offset and end are checked against config_len.
data.write_all(
&config_space_bytes[u64_to_usize(offset)..u64_to_usize(cmp::min(end, config_len))],
)
.unwrap();
}
}

Expand Down
15 changes: 4 additions & 11 deletions src/vmm/src/devices/virtio/block/vhost_user/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// Portions Copyright 2019 Intel Corporation. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

use std::cmp;
use std::io::Write;
use std::sync::atomic::AtomicU32;
use std::sync::Arc;
Expand Down Expand Up @@ -323,18 +322,12 @@ impl<T: VhostUserHandleBackend + Send + 'static> VirtioDevice for VhostUserBlock
}

fn read_config(&self, offset: u64, mut data: &mut [u8]) {
let config_len = self.config_space.len() as u64;
if offset >= config_len {
if let Some(config_space_bytes) = self.config_space.as_slice().get(u64_to_usize(offset)..) {
let len = config_space_bytes.len().min(data.len());
data[..len].copy_from_slice(&config_space_bytes[..len]);
} else {
error!("Failed to read config space");
self.metrics.cfg_fails.inc();
return;
}
if let Some(end) = offset.checked_add(data.len() as u64) {
// This write can't fail, offset and end are checked against config_len.
data.write_all(
&self.config_space[u64_to_usize(offset)..u64_to_usize(cmp::min(end, config_len))],
)
.unwrap();
}
}

Expand Down
16 changes: 4 additions & 12 deletions src/vmm/src/devices/virtio/net/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

#[cfg(not(test))]
use std::io::Read;
use std::io::Write;
use std::net::Ipv4Addr;
use std::sync::atomic::AtomicU32;
use std::sync::{Arc, Mutex};
Expand Down Expand Up @@ -822,19 +821,12 @@ impl VirtioDevice for Net {
}

fn read_config(&self, offset: u64, mut data: &mut [u8]) {
let config_space_bytes = self.config_space.as_slice();
let config_len = config_space_bytes.len() as u64;
if offset >= config_len {
if let Some(config_space_bytes) = self.config_space.as_slice().get(u64_to_usize(offset)..) {
let len = config_space_bytes.len().min(data.len());
data[..len].copy_from_slice(&config_space_bytes[..len]);
} else {
error!("Failed to read config space");
self.metrics.cfg_fails.inc();
return;
}
if let Some(end) = offset.checked_add(data.len() as u64) {
// This write can't fail, offset and end are checked against config_len.
data.write_all(
&config_space_bytes[u64_to_usize(offset)..u64_to_usize(cmp::min(end, config_len))],
)
.unwrap();
}
}

Expand Down

0 comments on commit e55297a

Please sign in to comment.