Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify read_config #4458

Merged
merged 5 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/utils/src/net/mac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub const MAC_ADDR_LEN: u8 = 6;

/// Represents a MAC address
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[repr(transparent)]
/// Representation of a MAC address.
pub struct MacAddr {
bytes: [u8; MAC_ADDR_LEN as usize],
Expand Down
21 changes: 6 additions & 15 deletions src/vmm/src/devices/virtio/balloon/device.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

use std::io::Write;
use std::fmt;
use std::sync::atomic::AtomicU32;
use std::sync::Arc;
use std::time::Duration;
use std::{cmp, fmt};

use log::error;
use serde::Serialize;
Expand Down Expand Up @@ -593,20 +592,12 @@ impl VirtioDevice for Balloon {
self.irq_trigger.irq_status.clone()
}

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 {
fn read_config(&self, offset: u64, data: &mut [u8]) {
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
18 changes: 5 additions & 13 deletions src/vmm/src/devices/virtio/block/vhost_user/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +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 @@ -322,19 +320,13 @@ impl<T: VhostUserHandleBackend + Send + 'static> VirtioDevice for VhostUserBlock
self.irq_trigger.irq_status.clone()
}

fn read_config(&self, offset: u64, mut data: &mut [u8]) {
let config_len = self.config_space.len() as u64;
if offset >= config_len {
fn read_config(&self, offset: u64, data: &mut [u8]) {
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
23 changes: 8 additions & 15 deletions src/vmm/src/devices/virtio/net/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@

#[cfg(not(test))]
use std::io::Read;
use std::io::Write;
use std::mem;
use std::net::Ipv4Addr;
use std::sync::atomic::AtomicU32;
use std::sync::{Arc, Mutex};
use std::{cmp, mem};

use libc::EAGAIN;
use log::{error, warn};
Expand Down Expand Up @@ -96,11 +95,12 @@ fn init_vnet_hdr(buf: &mut [u8]) {
}

#[derive(Debug, Default, Clone, Copy)]
#[repr(C)]
pub struct ConfigSpace {
pub guest_mac: MacAddr,
}

// SAFETY: `ConfigSpace` contains only PODs.
// SAFETY: `ConfigSpace` contains only PODs in `repr(C)` or `repr(transparent)`, without padding.
unsafe impl ByteValued for ConfigSpace {}

/// VirtIO network device.
Expand Down Expand Up @@ -831,20 +831,13 @@ impl VirtioDevice for Net {
self.irq_trigger.irq_status.clone()
}

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 {
fn read_config(&self, offset: u64, data: &mut [u8]) {
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