Skip to content

Misc janitorial revamps #852

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

Merged
merged 27 commits into from
Jul 18, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions vhost-device-console/rustfmt.toml → rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
edition = "2018"
edition = "2021"
format_generated_files = false
format_code_in_doc_comments = true
format_strings = true
imports_granularity = "Crate"
group_imports = "StdExternalCrate"
wrap_comments = true
2 changes: 1 addition & 1 deletion staging/vhost-device-video/rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
edition = "2018"
edition = "2021"
format_generated_files = false
format_code_in_doc_comments = true
format_strings = true
Expand Down
33 changes: 15 additions & 18 deletions vhost-device-can/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@
//
// SPDX-License-Identifier: Apache-2.0 or BSD-3-Clause

use log::{error, info, warn};
use std::any::Any;
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::{Arc, RwLock};
use std::thread;
use std::{
any::Any,
collections::HashMap,
path::PathBuf,
sync::{Arc, RwLock},
thread,
};

use log::{error, info, warn};
use thiserror::Error as ThisError;
use vhost_user_backend::VhostUserDaemon;
use vm_memory::{GuestMemoryAtomic, GuestMemoryMmap};

use crate::can::CanController;
use crate::vhu_can::VhostUserCanBackend;
use crate::{can::CanController, vhu_can::VhostUserCanBackend};

pub(crate) type Result<T> = std::result::Result<T, Error>;

Expand Down Expand Up @@ -111,7 +112,7 @@ pub(crate) fn start_backend_server(socket: PathBuf, can_devs: String) -> Result<
// Wait for read thread to exit
match read_handle.join() {
Ok(_) => info!("The read thread returned successfully"),
Err(e) => warn!("The read thread returned the error: {:?}", e),
Err(e) => warn!("The read thread returned the error: {e:?}"),
}
}
}
Expand All @@ -127,12 +128,9 @@ pub fn start_backend(config: VuCanConfig) -> Result<()> {
.map(|(a, b)| (a, b.to_string()))
.enumerate()
{
println!(
"thread_id: {}, socket: {:?}, can_devs: {:?}",
thread_id, socket, can_devs,
);
println!("thread_id: {thread_id}, socket: {socket:?}, can_devs: {can_devs:?}",);

let name = format!("vhu-can-{}", can_devs);
let name = format!("vhu-can-{can_devs}");
let sender = senders.clone();
let handle = thread::Builder::new()
.name(name.clone())
Expand Down Expand Up @@ -164,12 +162,11 @@ pub fn start_backend(config: VuCanConfig) -> Result<()> {

#[cfg(test)]
mod tests {
use super::*;
use crate::backend::Error::FailCreateCanControllerSocket;
use crate::can::Error::SocketOpen;
use crate::CanArgs;
use assert_matches::assert_matches;

use super::*;
use crate::{backend::Error::FailCreateCanControllerSocket, can::Error::SocketOpen, CanArgs};

#[test]
fn test_can_valid_configuration() {
let valid_args = CanArgs {
Expand Down
36 changes: 20 additions & 16 deletions vhost-device-can/src/can.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,27 @@
//
// SPDX-License-Identifier: Apache-2.0 or BSD-3-Clause

use log::{error, info, trace, warn};
use std::sync::{Arc, RwLock};
use std::{
sync::{Arc, RwLock},
thread::{spawn, JoinHandle},
};

use std::thread::{spawn, JoinHandle};
use log::{error, info, trace, warn};
use thiserror::Error as ThisError;
use vmm_sys_util::eventfd::{EventFd, EFD_NONBLOCK};
extern crate queues;
use queues::*;
extern crate socketcan;
use crate::virtio_can::{
VirtioCanConfig, VirtioCanFrame, CAN_CS_STARTED, CAN_CS_STOPPED, CAN_EFF_FLAG,
CAN_FRMF_TYPE_FD, VIRTIO_CAN_RX,
};
use socketcan::{
CanAnyFrame, CanDataFrame, CanFdFrame, CanFdSocket, EmbeddedFrame, ExtendedId, Frame, Id,
Socket, StandardId,
};

use crate::virtio_can::{
VirtioCanConfig, VirtioCanFrame, CAN_CS_STARTED, CAN_CS_STOPPED, CAN_EFF_FLAG,
CAN_FRMF_TYPE_FD, VIRTIO_CAN_RX,
};

type Result<T> = std::result::Result<T, Error>;

#[derive(Copy, Clone, Debug, PartialEq, ThisError)]
Expand Down Expand Up @@ -61,7 +64,7 @@ impl CanController {
// Creates a new controller corresponding to `device`.
pub(crate) fn new(can_name: String) -> Result<CanController> {
let can_name = can_name.to_owned();
info!("can_name: {:?}", can_name);
info!("can_name: {can_name:?}");

let rx_fifo = Queue::new();
let rx_efd = EventFd::new(EFD_NONBLOCK).map_err(|_| Error::EventFdFailed)?;
Expand Down Expand Up @@ -90,10 +93,10 @@ impl CanController {
let last_elem = canframe.length.to_native() as usize - 1;
for (index, sdu) in canframe.sdu.iter().enumerate() {
if index == last_elem {
trace!("0x{:x}", sdu);
trace!("0x{sdu:x}");
break;
}
trace!("0x{:x}, ", sdu);
trace!("0x{sdu:x}, ");
}
trace!("]");
}
Expand Down Expand Up @@ -195,19 +198,19 @@ impl CanController {
// Match and process frame variants
let read_can_frame = match frame {
CanAnyFrame::Normal(frame) => {
trace!("Received CAN frame: {:?}", frame);
trace!("Received CAN frame: {frame:?}");
Self::process_frame(frame, false)
}
CanAnyFrame::Fd(frame) => {
trace!("Received CAN FD frame: {:?}", frame);
trace!("Received CAN FD frame: {frame:?}");
Self::process_frame(frame, true)
}
CanAnyFrame::Remote(frame) => {
trace!("Received Remote CAN frame: {:?}", frame);
trace!("Received Remote CAN frame: {frame:?}");
Self::process_frame(frame, false)
}
CanAnyFrame::Error(frame) => {
trace!("Received Error frame: {:?}", frame);
trace!("Received Error frame: {frame:?}");
Self::process_frame(frame, false)
}
};
Expand Down Expand Up @@ -286,9 +289,10 @@ impl CanController {

#[cfg(test)]
mod tests {
use std::sync::{Arc, RwLock};

use super::*;
use crate::vhu_can::VhostUserCanBackend;
use std::sync::{Arc, RwLock};

#[test]
fn test_can_controller_creation() {
Expand Down Expand Up @@ -350,7 +354,7 @@ mod tests {
let operation_result = controller.can_out(frame);
assert!(operation_result.is_ok());
}
Err(_) => warn!("There is no CAN interface with {} name", can_name),
Err(_) => warn!("There is no CAN interface with {can_name} name"),
}
}

Expand Down
13 changes: 7 additions & 6 deletions vhost-device-can/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,25 @@ mod can;
mod vhu_can;
mod virtio_can;

use std::{convert::TryFrom, path::PathBuf, process::exit};

use clap::Parser;
use log::{error, info};
use socketcan::{CanSocket, Socket};
use std::convert::TryFrom;
use std::path::PathBuf;
use std::process::exit;

pub(crate) type Result<T> = std::result::Result<T, Error>;
use crate::backend::{start_backend, Error, VuCanConfig};

#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
struct CanArgs {
/// Location of vhost-user Unix domain socket. This is suffixed by 0,1,2..socket_count-1.
/// Location of vhost-user Unix domain socket. This is suffixed by
/// 0,1,2..socket_count-1.
#[clap(short, long, value_name = "SOCKET")]
socket_path: PathBuf,

/// A can device name to be used for reading (ex. vcan, can0, can1, ... etc.)
/// A can device name to be used for reading (ex. vcan, can0, can1, ...
/// etc.)
#[clap(short = 'd', long)]
can_devices: String,

Expand All @@ -39,7 +40,7 @@ struct CanArgs {
fn check_can_devices(can_devices: &[String]) -> Result<()> {
for can_dev in can_devices {
if CanSocket::open(can_dev).is_err() {
info!("There is no interface with the following name {}", can_dev);
info!("There is no interface with the following name {can_dev}");
return Err(Error::CouldNotFindCANDevs);
}
}
Expand Down
Loading