mirror of
https://github.com/rust-vmm/vhost-device.git
synced 2025-12-26 22:48:17 +00:00
can: run rustfmt
Signed-off-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
This commit is contained in:
parent
ccfdb49348
commit
717fa3b7f3
@ -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>;
|
||||
|
||||
@ -164,12 +165,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 {
|
||||
|
||||
@ -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)]
|
||||
@ -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() {
|
||||
|
||||
@ -10,12 +10,11 @@ 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};
|
||||
@ -23,11 +22,13 @@ 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,
|
||||
|
||||
|
||||
@ -5,39 +5,43 @@
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0 or BSD-3-Clause
|
||||
|
||||
use crate::can::CanController;
|
||||
use crate::can::Error::QueueEmpty;
|
||||
use crate::virtio_can::{
|
||||
VirtioCanCtrlRequest, VirtioCanFrame, VirtioCanHeader, CANFD_VALID_LENGTHS, CAN_CS_STARTED,
|
||||
CAN_CS_STOPPED, CAN_EFF_FLAG, CAN_EFF_MASK, CAN_ERR_BUSOFF, CAN_ERR_FLAG, CAN_FRMF_TYPE_FD,
|
||||
CAN_RTR_FLAG, CAN_SFF_MASK, VIRTIO_CAN_FLAGS_EXTENDED, VIRTIO_CAN_FLAGS_FD,
|
||||
VIRTIO_CAN_FLAGS_RTR, VIRTIO_CAN_FLAGS_VALID_MASK, VIRTIO_CAN_F_CAN_CLASSIC,
|
||||
VIRTIO_CAN_F_CAN_FD, VIRTIO_CAN_F_RTR_FRAMES, VIRTIO_CAN_RESULT_NOT_OK, VIRTIO_CAN_RESULT_OK,
|
||||
VIRTIO_CAN_RX, VIRTIO_CAN_SET_CTRL_MODE_START, VIRTIO_CAN_SET_CTRL_MODE_STOP,
|
||||
VIRTIO_CAN_S_CTRL_BUSOFF, VIRTIO_CAN_TX,
|
||||
};
|
||||
use log::{error, trace, warn};
|
||||
use std::os::fd::AsRawFd;
|
||||
use std::slice::from_raw_parts;
|
||||
use std::sync::{Arc, RwLock};
|
||||
use std::{
|
||||
convert,
|
||||
io::{self, Result as IoResult},
|
||||
os::fd::AsRawFd,
|
||||
slice::from_raw_parts,
|
||||
sync::{Arc, RwLock},
|
||||
};
|
||||
|
||||
use log::{error, trace, warn};
|
||||
use thiserror::Error as ThisError;
|
||||
use vhost::vhost_user::message::{VhostUserProtocolFeatures, VhostUserVirtioFeatures};
|
||||
use vhost_user_backend::VringEpollHandler;
|
||||
use vhost_user_backend::{VhostUserBackendMut, VringRwLock, VringT};
|
||||
use virtio_bindings::bindings::virtio_config::{VIRTIO_F_NOTIFY_ON_EMPTY, VIRTIO_F_VERSION_1};
|
||||
use virtio_bindings::bindings::virtio_ring::{
|
||||
VIRTIO_RING_F_EVENT_IDX, VIRTIO_RING_F_INDIRECT_DESC,
|
||||
use vhost_user_backend::{VhostUserBackendMut, VringEpollHandler, VringRwLock, VringT};
|
||||
use virtio_bindings::bindings::{
|
||||
virtio_config::{VIRTIO_F_NOTIFY_ON_EMPTY, VIRTIO_F_VERSION_1},
|
||||
virtio_ring::{VIRTIO_RING_F_EVENT_IDX, VIRTIO_RING_F_INDIRECT_DESC},
|
||||
};
|
||||
use virtio_queue::{DescriptorChain, QueueOwnedT};
|
||||
use vm_memory::{
|
||||
ByteValued, GuestAddressSpace, GuestMemoryAtomic, GuestMemoryLoadGuard, GuestMemoryMmap,
|
||||
};
|
||||
use vmm_sys_util::epoll::EventSet;
|
||||
use vmm_sys_util::eventfd::{EventFd, EFD_NONBLOCK};
|
||||
use vmm_sys_util::{
|
||||
epoll::EventSet,
|
||||
eventfd::{EventFd, EFD_NONBLOCK},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
can::{CanController, Error::QueueEmpty},
|
||||
virtio_can::{
|
||||
VirtioCanCtrlRequest, VirtioCanFrame, VirtioCanHeader, CANFD_VALID_LENGTHS, CAN_CS_STARTED,
|
||||
CAN_CS_STOPPED, CAN_EFF_FLAG, CAN_EFF_MASK, CAN_ERR_BUSOFF, CAN_ERR_FLAG, CAN_FRMF_TYPE_FD,
|
||||
CAN_RTR_FLAG, CAN_SFF_MASK, VIRTIO_CAN_FLAGS_EXTENDED, VIRTIO_CAN_FLAGS_FD,
|
||||
VIRTIO_CAN_FLAGS_RTR, VIRTIO_CAN_FLAGS_VALID_MASK, VIRTIO_CAN_F_CAN_CLASSIC,
|
||||
VIRTIO_CAN_F_CAN_FD, VIRTIO_CAN_F_RTR_FRAMES, VIRTIO_CAN_RESULT_NOT_OK,
|
||||
VIRTIO_CAN_RESULT_OK, VIRTIO_CAN_RX, VIRTIO_CAN_SET_CTRL_MODE_START,
|
||||
VIRTIO_CAN_SET_CTRL_MODE_STOP, VIRTIO_CAN_S_CTRL_BUSOFF, VIRTIO_CAN_TX,
|
||||
},
|
||||
};
|
||||
|
||||
/// Virtio configuration
|
||||
const QUEUE_SIZE: usize = 64;
|
||||
@ -720,9 +724,8 @@ impl VhostUserBackendMut for VhostUserCanBackend {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::virtio_can::{VirtioCanCtrlResponse, VirtioCanTxResponse};
|
||||
use std::mem::size_of;
|
||||
|
||||
use virtio_bindings::virtio_ring::{VRING_DESC_F_NEXT, VRING_DESC_F_WRITE};
|
||||
use virtio_queue::{
|
||||
desc::{split::Descriptor as SplitDescriptor, RawDescriptor},
|
||||
@ -731,6 +734,9 @@ mod tests {
|
||||
};
|
||||
use vm_memory::{Bytes, GuestAddress, GuestMemoryAtomic, GuestMemoryMmap, Le16, Le32};
|
||||
|
||||
use super::*;
|
||||
use crate::virtio_can::{VirtioCanCtrlResponse, VirtioCanTxResponse};
|
||||
|
||||
#[test]
|
||||
fn test_virtio_can_tx_response_default() {
|
||||
let response = VirtioCanTxResponse::default();
|
||||
@ -1137,7 +1143,8 @@ mod tests {
|
||||
frame.length
|
||||
);
|
||||
|
||||
// Test 3: Return Error::InvalidCanLength(frame_length) when length is out-of-range (>8)
|
||||
// Test 3: Return Error::InvalidCanLength(frame_length) when length is
|
||||
// out-of-range (>8)
|
||||
let frame = VirtioCanFrame {
|
||||
msg_type: VIRTIO_CAN_TX.into(),
|
||||
can_id: 0.into(),
|
||||
@ -1286,7 +1293,8 @@ mod tests {
|
||||
);
|
||||
|
||||
// Test 2: Take a valid CAN / CANFD message and try to enable RTR in flags.
|
||||
// the test should fail because VIRTIO_CAN_F_CAN_CLASSIC is not negotiated.
|
||||
// the test should fail because VIRTIO_CAN_F_CAN_CLASSIC is not
|
||||
// negotiated.
|
||||
vu_can_backend.acked_features(1 << VIRTIO_CAN_F_RTR_FRAMES);
|
||||
|
||||
assert_eq!(
|
||||
@ -1295,7 +1303,8 @@ mod tests {
|
||||
);
|
||||
|
||||
// Test 3: Take a valid CAN / CANFD message and try to enable RTR in flags.
|
||||
// the test should succeed because VIRTIO_CAN_F_CAN_CLASSIC is negotiated.
|
||||
// the test should succeed because VIRTIO_CAN_F_CAN_CLASSIC is
|
||||
// negotiated.
|
||||
vu_can_backend
|
||||
.acked_features((1 << VIRTIO_CAN_F_RTR_FRAMES) | (1 << VIRTIO_CAN_F_CAN_CLASSIC));
|
||||
|
||||
@ -1310,8 +1319,8 @@ mod tests {
|
||||
);
|
||||
|
||||
// Test 4: Take a valid CAN / CANFD message and try to enable RTR in flags.
|
||||
// the test should fail because VIRTIO_CAN_F_CAN_CLASSIC is not negotiated,
|
||||
// and RTR does not work with VIRTIO_CAN_F_CAN_FD.
|
||||
// the test should fail because VIRTIO_CAN_F_CAN_CLASSIC is not
|
||||
// negotiated, and RTR does not work with VIRTIO_CAN_F_CAN_FD.
|
||||
vu_can_backend.acked_features((1 << VIRTIO_CAN_F_RTR_FRAMES) | (1 << VIRTIO_CAN_F_CAN_FD));
|
||||
|
||||
assert_eq!(
|
||||
@ -1812,7 +1821,8 @@ mod tests {
|
||||
};
|
||||
|
||||
// Test 1: Take a valid CAN / CANFD message and try to enable RTR in flags.
|
||||
// the test should fail because VIRTIO_CAN_F_CAN_CLASSIC is not negotiated.
|
||||
// the test should fail because VIRTIO_CAN_F_CAN_CLASSIC is not
|
||||
// negotiated.
|
||||
vu_can_backend.acked_features(1 << VIRTIO_CAN_F_CAN_CLASSIC);
|
||||
|
||||
assert_eq!(
|
||||
@ -1821,7 +1831,8 @@ mod tests {
|
||||
);
|
||||
|
||||
// Test 2: Take a valid CAN / CANFD message and try to enable RTR in flags.
|
||||
// the test should succeed because VIRTIO_CAN_F_CAN_CLASSIC is negotiated.
|
||||
// the test should succeed because VIRTIO_CAN_F_CAN_CLASSIC is
|
||||
// negotiated.
|
||||
vu_can_backend
|
||||
.acked_features((1 << VIRTIO_CAN_F_RTR_FRAMES) | (1 << VIRTIO_CAN_F_CAN_CLASSIC));
|
||||
|
||||
@ -1836,8 +1847,8 @@ mod tests {
|
||||
);
|
||||
|
||||
// Test 3: Take a valid CAN / CANFD message and try to enable RTR in flags.
|
||||
// the test should fail because VIRTIO_CAN_F_CAN_CLASSIC is not negotiated,
|
||||
// and RTR does not work with VIRTIO_CAN_F_CAN_FD.
|
||||
// the test should fail because VIRTIO_CAN_F_CAN_CLASSIC is not
|
||||
// negotiated, and RTR does not work with VIRTIO_CAN_F_CAN_FD.
|
||||
|
||||
let frame = VirtioCanFrame {
|
||||
msg_type: 0.into(),
|
||||
|
||||
Loading…
Reference in New Issue
Block a user