From a001824c19d7568e187eeb7e1d8f416a7983a2cf Mon Sep 17 00:00:00 2001 From: Liu Jiang Date: Fri, 19 Feb 2021 11:03:52 +0800 Subject: [PATCH] Move trait VhostUserBackend to dedicated file Move the VhostUserBackend trait to a dedicated file, preparing for coming changes. Signed-off-by: Liu Jiang --- src/backend.rs | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 74 ++----------------------------------------- 2 files changed, 89 insertions(+), 71 deletions(-) create mode 100644 src/backend.rs diff --git a/src/backend.rs b/src/backend.rs new file mode 100644 index 0000000..7d14144 --- /dev/null +++ b/src/backend.rs @@ -0,0 +1,86 @@ +// Copyright 2019 Intel Corporation. All Rights Reserved. +// Copyright 2019-2021 Alibaba Cloud. All rights reserved. +// +// SPDX-License-Identifier: Apache-2.0 + +use std::io; +use std::result; +use std::sync::{Arc, RwLock}; + +use vhost::vhost_user::message::VhostUserProtocolFeatures; +use vhost::vhost_user::SlaveFsCacheReq; +use vm_memory::{GuestMemoryAtomic, GuestMemoryMmap}; +use vmm_sys_util::eventfd::EventFd; + +use super::Vring; + +/// This trait must be implemented by the caller in order to provide backend +/// specific implementation. +pub trait VhostUserBackend: Send + Sync + 'static { + /// Number of queues. + fn num_queues(&self) -> usize; + + /// Depth of each queue. + fn max_queue_size(&self) -> usize; + + /// Available virtio features. + fn features(&self) -> u64; + + /// Acked virtio features. + fn acked_features(&mut self, _features: u64) {} + + /// Virtio protocol features. + fn protocol_features(&self) -> VhostUserProtocolFeatures; + + /// Tell the backend if EVENT_IDX has been negotiated. + fn set_event_idx(&mut self, enabled: bool); + + /// Update guest memory regions. + fn update_memory( + &mut self, + atomic_mem: GuestMemoryAtomic, + ) -> result::Result<(), io::Error>; + + /// This function gets called if the backend registered some additional + /// listeners onto specific file descriptors. The library can handle + /// virtqueues on its own, but does not know what to do with events + /// happening on custom listeners. + fn handle_event( + &self, + device_event: u16, + evset: epoll::Events, + vrings: &[Arc>], + thread_id: usize, + ) -> result::Result; + + /// Get virtio device configuration. + /// A default implementation is provided as we cannot expect all backends + /// to implement this function. + fn get_config(&self, _offset: u32, _size: u32) -> Vec { + Vec::new() + } + + /// Set virtio device configuration. + /// A default implementation is provided as we cannot expect all backends + /// to implement this function. + fn set_config(&mut self, _offset: u32, _buf: &[u8]) -> result::Result<(), io::Error> { + Ok(()) + } + + /// Provide an exit EventFd + /// When this EventFd is written to the worker thread will exit. An optional id may + /// also be provided, if it not provided then the exit event will be first event id + /// after the last queue + fn exit_event(&self, _thread_index: usize) -> Option<(EventFd, Option)> { + None + } + + /// Set slave fd. + /// A default implementation is provided as we cannot expect all backends + /// to implement this function. + fn set_slave_req_fd(&mut self, _vu_req: SlaveFsCacheReq) {} + + fn queues_per_thread(&self) -> Vec { + vec![0xffff_ffff] + } +} diff --git a/src/lib.rs b/src/lib.rs index 7c3a5b0..f7276f0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -34,6 +34,9 @@ use vm_memory::{ }; use vmm_sys_util::eventfd::EventFd; +mod backend; +pub use backend::VhostUserBackend; + const MAX_MEM_SLOTS: u64 = 32; #[derive(Debug)] @@ -62,77 +65,6 @@ pub enum Error { /// Result of vhost-user daemon operations. pub type Result = result::Result; -/// This trait must be implemented by the caller in order to provide backend -/// specific implementation. -pub trait VhostUserBackend: Send + Sync + 'static { - /// Number of queues. - fn num_queues(&self) -> usize; - - /// Depth of each queue. - fn max_queue_size(&self) -> usize; - - /// Available virtio features. - fn features(&self) -> u64; - - /// Acked virtio features. - fn acked_features(&mut self, _features: u64) {} - - /// Virtio protocol features. - fn protocol_features(&self) -> VhostUserProtocolFeatures; - - /// Tell the backend if EVENT_IDX has been negotiated. - fn set_event_idx(&mut self, enabled: bool); - - /// Update guest memory regions. - fn update_memory( - &mut self, - atomic_mem: GuestMemoryAtomic, - ) -> result::Result<(), io::Error>; - - /// This function gets called if the backend registered some additional - /// listeners onto specific file descriptors. The library can handle - /// virtqueues on its own, but does not know what to do with events - /// happening on custom listeners. - fn handle_event( - &self, - device_event: u16, - evset: epoll::Events, - vrings: &[Arc>], - thread_id: usize, - ) -> result::Result; - - /// Get virtio device configuration. - /// A default implementation is provided as we cannot expect all backends - /// to implement this function. - fn get_config(&self, _offset: u32, _size: u32) -> Vec { - Vec::new() - } - - /// Set virtio device configuration. - /// A default implementation is provided as we cannot expect all backends - /// to implement this function. - fn set_config(&mut self, _offset: u32, _buf: &[u8]) -> result::Result<(), io::Error> { - Ok(()) - } - - /// Provide an exit EventFd - /// When this EventFd is written to the worker thread will exit. An optional id may - /// also be provided, if it not provided then the exit event will be first event id - /// after the last queue - fn exit_event(&self, _thread_index: usize) -> Option<(EventFd, Option)> { - None - } - - /// Set slave fd. - /// A default implementation is provided as we cannot expect all backends - /// to implement this function. - fn set_slave_req_fd(&mut self, _vu_req: SlaveFsCacheReq) {} - - fn queues_per_thread(&self) -> Vec { - vec![0xffff_ffff] - } -} - /// This structure is the public API the backend is allowed to interact with /// in order to run a fully functional vhost-user daemon. pub struct VhostUserDaemon {