|
| 1 | +// Copyright 2021 Red Hat, Inc. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause |
| 4 | + |
| 5 | +use std::num::Wrapping; |
| 6 | +use std::ops::DerefMut; |
| 7 | + |
| 8 | +use crate::{Error, Queue, QueueGuard, QueueState}; |
| 9 | +use std::sync::atomic::Ordering; |
| 10 | +use vm_memory::GuestAddressSpace; |
| 11 | + |
| 12 | +/// Lifetime-generic guard associated to a QueueStateT. In practice, |
| 13 | +/// instead of having a `QueueStateT::Guard<'g>` generic associated type, |
| 14 | +/// you have to write `<QueueStateT::Guard as QueueStateGuard<'g>>::Out`. |
| 15 | +pub(crate) trait QueueStateGuard<'g> { |
| 16 | + /// Type of the guard passed to the `with` method. |
| 17 | + type Out: 'g + DerefMut<Target = QueueState>; |
| 18 | +} |
| 19 | + |
| 20 | +pub(crate) trait QueueT<M: GuestAddressSpace> { |
| 21 | + /// Lifetime-generic guard. Usually this is just `Self`, and implementors |
| 22 | + /// of `QueueT` also implement `QueueStateGuard`. |
| 23 | + type Guard: for<'g> QueueStateGuard<'g>; |
| 24 | + |
| 25 | + fn construct(mem: M, state: QueueState) -> Self; |
| 26 | + fn with< |
| 27 | + 'a, |
| 28 | + 'g, |
| 29 | + U, |
| 30 | + F: FnOnce(QueueGuard<M::T, <Self::Guard as QueueStateGuard<'g>>::Out>) -> U, |
| 31 | + >( |
| 32 | + &'a mut self, |
| 33 | + f: F, |
| 34 | + ) -> U |
| 35 | + where |
| 36 | + 'a: 'g; |
| 37 | + |
| 38 | + /// Check whether the queue configuration is valid. |
| 39 | + fn is_valid(&mut self) -> bool { |
| 40 | + self.with(|qstate| qstate.is_valid()) |
| 41 | + } |
| 42 | + |
| 43 | + /// Reset the queue to the initial state. |
| 44 | + fn reset(&mut self) { |
| 45 | + self.with(|mut qstate| qstate.reset()) |
| 46 | + } |
| 47 | + |
| 48 | + /// Get the maximum size of the virtio queue. |
| 49 | + fn max_size(&mut self) -> u16 { |
| 50 | + self.with(|qstate| qstate.max_size()) |
| 51 | + } |
| 52 | + |
| 53 | + /// Return the actual size of the queue. |
| 54 | + /// |
| 55 | + /// The virtio driver may configure queue size smaller than the value reported by `max_size()`. |
| 56 | + fn actual_size(&mut self) -> u16 { |
| 57 | + self.with(|qstate| qstate.actual_size()) |
| 58 | + } |
| 59 | + |
| 60 | + /// Configure the queue size for the virtio queue. |
| 61 | + /// |
| 62 | + /// The `size` should power of two and less than or equal to value reported by `max_size()`, |
| 63 | + /// otherwise it will panic. |
| 64 | + fn set_size(&mut self, size: u16) { |
| 65 | + self.with(|mut qstate| qstate.set_size(size)) |
| 66 | + } |
| 67 | + |
| 68 | + /// Check whether the queue is ready to be processed. |
| 69 | + fn ready(&mut self) -> bool { |
| 70 | + self.with(|qstate| qstate.ready()) |
| 71 | + } |
| 72 | + |
| 73 | + /// Configure the queue to ready for processing. |
| 74 | + fn set_ready(&mut self, ready: bool) { |
| 75 | + self.with(|mut qstate| qstate.set_ready(ready)) |
| 76 | + } |
| 77 | + |
| 78 | + /// Set descriptor table address for the queue. |
| 79 | + /// |
| 80 | + /// The descriptor table address is 64-bit, the corresponding part will be updated if 'low' |
| 81 | + /// and/or `high` is valid. |
| 82 | + fn set_desc_table_address(&mut self, low: Option<u32>, high: Option<u32>) { |
| 83 | + self.with(|mut qstate| qstate.set_desc_table_address(low, high)) |
| 84 | + } |
| 85 | + |
| 86 | + /// Set available ring address for the queue. |
| 87 | + /// |
| 88 | + /// The available ring address is 64-bit, the corresponding part will be updated if 'low' |
| 89 | + /// and/or `high` is valid. |
| 90 | + fn set_avail_ring_address(&mut self, low: Option<u32>, high: Option<u32>) { |
| 91 | + self.with(|mut qstate| qstate.set_avail_ring_address(low, high)) |
| 92 | + } |
| 93 | + |
| 94 | + /// Set used ring address for the queue. |
| 95 | + /// |
| 96 | + /// The used ring address is 64-bit, the corresponding part will be updated if 'low' |
| 97 | + /// and/or `high` is valid. |
| 98 | + fn set_used_ring_address(&mut self, low: Option<u32>, high: Option<u32>) { |
| 99 | + self.with(|mut qstate| qstate.set_used_ring_address(low, high)) |
| 100 | + } |
| 101 | + |
| 102 | + /// Enable/disable the VIRTIO_F_RING_EVENT_IDX feature for interrupt coalescing. |
| 103 | + fn set_event_idx(&mut self, enabled: bool) { |
| 104 | + self.with(|mut qstate| qstate.set_event_idx(enabled)) |
| 105 | + } |
| 106 | + |
| 107 | + /// Read the `idx` field from the available ring. |
| 108 | + fn avail_idx(&mut self, order: Ordering) -> Result<Wrapping<u16>, Error> { |
| 109 | + self.with(|qstate| qstate.avail_idx(order)) |
| 110 | + } |
| 111 | + |
| 112 | + /// Put a used descriptor head into the used ring. |
| 113 | + fn add_used(&mut self, head_index: u16, len: u32) -> Result<(), Error> { |
| 114 | + self.with(|mut qstate| qstate.add_used(head_index, len)) |
| 115 | + } |
| 116 | + |
| 117 | + /// Enable notification events from the guest driver. |
| 118 | + /// |
| 119 | + /// Return true if one or more descriptors can be consumed from the available ring after |
| 120 | + /// notifications were enabled (and thus it's possible there will be no corresponding |
| 121 | + /// notification). |
| 122 | + fn enable_notification(&mut self) -> Result<bool, Error> { |
| 123 | + self.with(|mut qstate| qstate.enable_notification()) |
| 124 | + } |
| 125 | + |
| 126 | + /// Disable notification events from the guest driver. |
| 127 | + fn disable_notification(&mut self) -> Result<(), Error> { |
| 128 | + self.with(|mut qstate| qstate.disable_notification()) |
| 129 | + } |
| 130 | + |
| 131 | + /// Check whether a notification to the guest is needed. |
| 132 | + /// |
| 133 | + /// Please note this method has side effects: once it returns `true`, it considers the |
| 134 | + /// driver will actually be notified, remember the associated index in the used ring, and |
| 135 | + /// won't return `true` again until the driver updates `used_event` and/or the notification |
| 136 | + /// conditions hold once more. |
| 137 | + fn needs_notification(&mut self) -> Result<bool, Error> { |
| 138 | + self.with(|mut qstate| qstate.needs_notification()) |
| 139 | + } |
| 140 | + |
| 141 | + /// Return the index for the next descriptor in the available ring. |
| 142 | + fn next_avail(&mut self) -> u16 { |
| 143 | + self.with(|qstate| qstate.next_avail()) |
| 144 | + } |
| 145 | + |
| 146 | + /// Set the index for the next descriptor in the available ring. |
| 147 | + fn set_next_avail(&mut self, next_avail: u16) { |
| 148 | + self.with(|mut qstate| qstate.set_next_avail(next_avail)) |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +impl<'g, M: GuestAddressSpace> QueueStateGuard<'g> for Queue<M> { |
| 153 | + type Out = &'g mut QueueState; |
| 154 | +} |
| 155 | + |
| 156 | +impl<M: GuestAddressSpace> QueueT<M> for Queue<M> { |
| 157 | + type Guard = Self; |
| 158 | + |
| 159 | + fn construct(mem: M, state: QueueState) -> Self { |
| 160 | + Queue { mem, state } |
| 161 | + } |
| 162 | + fn with< |
| 163 | + 'a, |
| 164 | + 'g, |
| 165 | + U, |
| 166 | + F: FnOnce(QueueGuard<M::T, <Self::Guard as QueueStateGuard<'g>>::Out>) -> U, |
| 167 | + >( |
| 168 | + &'a mut self, |
| 169 | + f: F, |
| 170 | + ) -> U |
| 171 | + where |
| 172 | + 'a: 'g, |
| 173 | + { |
| 174 | + f(self.acquire()) |
| 175 | + } |
| 176 | +} |
0 commit comments