virtio_queue/desc/
packed.rs1use virtio_bindings::bindings::virtio_ring::{
3 VRING_DESC_F_INDIRECT, VRING_DESC_F_NEXT, VRING_DESC_F_WRITE,
4};
5use vm_memory::{ByteValued, GuestAddress, Le16, Le32, Le64};
6
7#[repr(C)]
9#[derive(Default, Clone, Copy, Debug)]
10pub struct Descriptor {
11 addr: Le64,
13 len: Le32,
15 id: Le16,
17 flags: Le16,
19}
20
21#[allow(clippy::len_without_is_empty)]
22impl Descriptor {
23 pub fn addr(&self) -> GuestAddress {
25 GuestAddress(self.addr.into())
26 }
27
28 pub fn len(&self) -> u32 {
30 self.len.into()
31 }
32
33 pub fn flags(&self) -> u16 {
35 self.flags.into()
36 }
37
38 pub fn id(&self) -> u16 {
40 self.id.into()
41 }
42
43 pub fn refers_to_indirect_table(&self) -> bool {
45 self.flags() & VRING_DESC_F_INDIRECT as u16 != 0
46 }
47
48 pub fn has_next(&self) -> bool {
50 self.flags() & VRING_DESC_F_NEXT as u16 != 0
51 }
52
53 pub fn is_write_only(&self) -> bool {
58 self.flags() & VRING_DESC_F_WRITE as u16 != 0
59 }
60}
61
62impl Descriptor {
63 pub fn new(addr: u64, len: u32, id: u16, flags: u16) -> Self {
71 Descriptor {
72 addr: addr.into(),
73 len: len.into(),
74 id: id.into(),
75 flags: flags.into(),
76 }
77 }
78
79 pub fn set_addr(&mut self, addr: u64) {
81 self.addr = addr.into();
82 }
83
84 pub fn set_len(&mut self, len: u32) {
86 self.len = len.into();
87 }
88
89 pub fn set_flags(&mut self, flags: u16) {
91 self.flags = flags.into();
92 }
93
94 pub fn set_id(&mut self, id: u16) {
96 self.id = id.into();
97 }
98}
99
100unsafe impl ByteValued for Descriptor {}
104
105#[repr(C)]
107#[derive(Clone, Copy, Debug)]
108pub struct PackedDescEvent {
109 off_wrap: Le16,
110 flags: Le16,
111}
112
113impl PackedDescEvent {
114 #[allow(unused)]
120 pub(crate) fn new(off_wrap: u16, flags: u16) -> Self {
121 PackedDescEvent {
122 off_wrap: off_wrap.into(),
123 flags: flags.into(),
124 }
125 }
126}
127
128unsafe impl ByteValued for PackedDescEvent {}
132
133#[cfg(test)]
134mod tests {
135 use super::*;
136 use memoffset::offset_of;
137 use std::mem::{align_of, size_of};
138
139 #[test]
140 fn test_descriptor_offset() {
141 assert_eq!(size_of::<Descriptor>(), 16);
142 assert_eq!(offset_of!(Descriptor, addr), 0);
143 assert_eq!(offset_of!(Descriptor, len), 8);
144 assert_eq!(offset_of!(Descriptor, id), 12);
145 assert_eq!(offset_of!(Descriptor, flags), 14);
146 assert!(align_of::<Descriptor>() <= 16);
147 }
148
149 #[test]
150 fn test_descriptor_getter_setter() {
151 let mut desc = Descriptor::new(0, 0, 0, 0);
152
153 desc.set_addr(0x1000);
154 assert_eq!(desc.addr(), GuestAddress(0x1000));
155 desc.set_len(0x2000);
156 assert_eq!(desc.len(), 0x2000);
157 desc.set_flags(VRING_DESC_F_NEXT as u16);
158 assert_eq!(desc.flags(), VRING_DESC_F_NEXT as u16);
159 assert!(desc.has_next());
160 assert!(!desc.is_write_only());
161 assert!(!desc.refers_to_indirect_table());
162 desc.set_flags(VRING_DESC_F_WRITE as u16);
163 assert_eq!(desc.flags(), VRING_DESC_F_WRITE as u16);
164 assert!(!desc.has_next());
165 assert!(desc.is_write_only());
166 assert!(!desc.refers_to_indirect_table());
167 desc.set_flags(VRING_DESC_F_INDIRECT as u16);
168 assert_eq!(desc.flags(), VRING_DESC_F_INDIRECT as u16);
169 assert!(!desc.is_write_only());
170 assert!(desc.refers_to_indirect_table());
171 desc.set_id(1);
172 assert_eq!(desc.id(), 1);
173 }
174
175 #[test]
176 fn test_descriptor_copy() {
177 let e1 = Descriptor::new(1, 2, 0, 3);
178 let mut e2 = Descriptor::default();
179
180 e2.as_mut_slice().copy_from_slice(e1.as_slice());
181 assert_eq!(e1.addr(), e2.addr());
182 assert_eq!(e1.len(), e2.len());
183 assert_eq!(e1.id(), e2.id());
184 assert_eq!(e1.flags(), e2.flags());
185 }
186
187 #[test]
188 fn test_packed_desc_event_offset() {
189 assert_eq!(offset_of!(PackedDescEvent, off_wrap), 0);
190 assert_eq!(offset_of!(PackedDescEvent, flags), 2);
191 assert_eq!(size_of::<PackedDescEvent>(), 4);
192 }
193
194 #[test]
195 fn test_packed_desc_event_copy() {
196 let e1 = PackedDescEvent::new(1, 2);
197 let mut e2 = PackedDescEvent::new(0, 0);
198
199 e2.as_mut_slice().copy_from_slice(e1.as_slice());
200 assert_eq!(e1.off_wrap, e2.off_wrap);
201 assert_eq!(e1.flags, e2.flags);
202 }
203}