1 // Copyright (C) 2019-2021 Alibaba Cloud. All rights reserved. 2 // SPDX-License-Identifier: Apache-2.0 or BSD-3-Clause 3 // 4 // Portions Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. 5 // 6 // Portions Copyright 2017 The Chromium OS Authors. All rights reserved. 7 // Use of this source code is governed by a BSD-style license that can be 8 // found in the LICENSE-BSD-Google file. 9 10 //! Common traits and structs for vhost-user backend drivers. 11 12 use base::RawDescriptor; 13 14 /// Maximum number of memory regions supported. 15 pub const VHOST_MAX_MEMORY_REGIONS: usize = 255; 16 17 /// Vring configuration data. 18 pub struct VringConfigData { 19 /// Actual queue size negotiated by the driver. 20 pub queue_size: u16, 21 /// Bitmask of vring flags. 22 pub flags: u32, 23 /// Descriptor table address. 24 pub desc_table_addr: u64, 25 /// Used ring buffer address. 26 pub used_ring_addr: u64, 27 /// Available ring buffer address. 28 pub avail_ring_addr: u64, 29 /// Optional address for logging. 30 pub log_addr: Option<u64>, 31 } 32 33 impl VringConfigData { 34 /// Check whether the log (flag, address) pair is valid. is_log_addr_valid(&self) -> bool35 pub fn is_log_addr_valid(&self) -> bool { 36 if self.flags & 0x1 != 0 && self.log_addr.is_none() { 37 return false; 38 } 39 40 true 41 } 42 43 /// Get the log address, default to zero if not available. get_log_addr(&self) -> u6444 pub fn get_log_addr(&self) -> u64 { 45 if self.flags & 0x1 != 0 && self.log_addr.is_some() { 46 self.log_addr.unwrap() 47 } else { 48 0 49 } 50 } 51 } 52 53 /// Memory region configuration data. 54 pub struct VhostUserMemoryRegionInfo { 55 /// Guest physical address of the memory region. 56 pub guest_phys_addr: u64, 57 /// Size of the memory region. 58 pub memory_size: u64, 59 /// Virtual address in the current process. 60 pub userspace_addr: u64, 61 /// Offset where region starts in the mapped memory. 62 pub mmap_offset: u64, 63 /// File descriptor for mmap. 64 pub mmap_handle: RawDescriptor, 65 } 66 67 #[cfg(test)] 68 mod tests { 69 use super::*; 70 71 #[test] test_vring_config_data()72 fn test_vring_config_data() { 73 let mut config = VringConfigData { 74 queue_size: 0x2000, 75 flags: 0x0, 76 desc_table_addr: 0x4000, 77 used_ring_addr: 0x5000, 78 avail_ring_addr: 0x6000, 79 log_addr: None, 80 }; 81 82 assert!(config.is_log_addr_valid()); 83 assert_eq!(config.get_log_addr(), 0); 84 85 config.flags = 0x1; 86 assert!(!config.is_log_addr_valid()); 87 assert_eq!(config.get_log_addr(), 0); 88 89 config.log_addr = Some(0x7000); 90 assert!(config.is_log_addr_valid()); 91 assert_eq!(config.get_log_addr(), 0x7000); 92 93 config.flags = 0x0; 94 assert!(config.is_log_addr_valid()); 95 assert_eq!(config.get_log_addr(), 0); 96 } 97 } 98