# queue.rs - source
Source of the Rust file `/home/runner/.cargo/git/checkouts/virtio-drivers-4fdfaa862bcdc399/f30d426/src/queue.rs`.
Keywords: rust, rustlang, rust-lang

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261

    use core::mem::size_of;
    use core::slice;
    use core::sync::atomic::{fence, Ordering};

    use super::*;
    use crate::header::VirtIOHeader;
    use bitflags::*;

    use volatile::Volatile;

    /// The mechanism for bulk data transport on virtio devices.
    ///
    /// Each device can have zero or more virtqueues.
    #[repr(C)]
    pub struct VirtQueue<'a> {
        /// DMA guard
        dma: DMA,
        /// Descriptor table
        desc: &'a mut [Descriptor],
        /// Available ring
        avail: &'a mut AvailRing,
        /// Used ring
        used: &'a mut UsedRing,

        /// The index of queue
        queue_idx: u32,
        /// The size of queue
        queue_size: u16,
        /// The number of used queues.
        num_used: u16,
        /// The head desc index of the free list.
        free_head: u16,
        avail_idx: u16,
        last_used_idx: u16,
    }

    impl VirtQueue<'_> {
        /// Create a new VirtQueue.
        pub fn new(header: &mut VirtIOHeader, idx: usize, size: u16) -> Result<Self> {
            if header.queue_used(idx as u32) {
                return Err(Error::AlreadyUsed);
            }
            if !size.is_power_of_two() || header.max_queue_size() < size as u32 {
                return Err(Error::InvalidParam);
            }
            let layout = VirtQueueLayout::new(size);
            // alloc continuous pages
            let dma = DMA::new(layout.size / PAGE_SIZE)?;

            header.queue_set(idx as u32, size as u32, PAGE_SIZE as u32, dma.pfn());

            let desc =
                unsafe { slice::from_raw_parts_mut(dma.vaddr() as *mut Descriptor, size as usize) };
            let avail = unsafe { &mut *((dma.vaddr() + layout.avail_offset) as *mut AvailRing) };
            let used = unsafe { &mut *((dma.vaddr() + layout.used_offset) as *mut UsedRing) };

            // link descriptors together
            for i in 0..(size - 1) {
                desc[i as usize].next.write(i + 1);
            }

            Ok(VirtQueue {
                dma,
                desc,
                avail,
                used,
                queue_size: size,
                queue_idx: idx as u32,
                num_used: 0,
                free_head: 0,
                avail_idx: 0,
                last_used_idx: 0,
            })
        }

        /// Add buffers to the virtqueue, return a token.
        ///
        /// Ref: linux virtio_ring.c virtqueue_add
        pub fn add(&mut self, inputs: &[&[u8]], outputs: &[&mut [u8]]) -> Result<u16> {
            if inputs.is_empty() && outputs.is_empty() {
                return Err(Error::InvalidParam);
            }
            if inputs.len() + outputs.len() + self.num_used as usize > self.queue_size as usize {
                return Err(Error::BufferTooSmall);
            }

            // allocate descriptors from free list
            let head = self.free_head;
            let mut last = self.free_head;
            for input in inputs.iter() {
                let desc = &mut self.desc[self.free_head as usize];
                desc.set_buf(input);
                desc.flags.write(DescFlags::NEXT);
                last = self.free_head;
                self.free_head = desc.next.read();
            }
            for output in outputs.iter() {
                let desc = &mut self.desc[self.free_head as usize];
                desc.set_buf(output);
                desc.flags.write(DescFlags::NEXT | DescFlags::WRITE);
                last = self.free_head;
                self.free_head = desc.next.read();
            }
            // set last_elem.next = NULL
            {
                let desc = &mut self.desc[last as usize];
                let mut flags = desc.flags.read();
                flags.remove(DescFlags::NEXT);
                desc.flags.write(flags);
            }
            self.num_used += (inputs.len() + outputs.len()) as u16;

            let avail_slot = self.avail_idx & (self.queue_size - 1);
            self.avail.ring[avail_slot as usize].write(head);

            // write barrier
            fence(Ordering::SeqCst);

            // increase head of avail ring
            self.avail_idx = self.avail_idx.wrapping_add(1);
            self.avail.idx.write(self.avail_idx);
            Ok(head)
        }

        /// Whether there is a used element that can pop.
        pub fn can_pop(&self) -> bool {
            self.last_used_idx != self.used.idx.read()
        }

        /// The number of free descriptors.
        pub fn available_desc(&self) -> usize {
            (self.queue_size - self.num_used) as usize
        }

        /// Recycle descriptors in the list specified by head.
        ///
        /// This will push all linked descriptors at the front of the free list.
        fn recycle_descriptors(&mut self, mut head: u16) {
            let origin_free_head = self.free_head;
            self.free_head = head;
            loop {
                let desc = &mut self.desc[head as usize];
                let flags = desc.flags.read();
                self.num_used -= 1;
                if flags.contains(DescFlags::NEXT) {
                    head = desc.next.read();
                } else {
                    desc.next.write(origin_free_head);
                    return;
                }
            }
        }

        /// Get a token from device used buffers, return (token, len).
        ///
        /// Ref: linux virtio_ring.c virtqueue_get_buf_ctx
        pub fn pop_used(&mut self) -> Result<(u16, u32)> {
            if !self.can_pop() {
                return Err(Error::NotReady);
            }
            // read barrier
            fence(Ordering::SeqCst);

            let last_used_slot = self.last_used_idx & (self.queue_size - 1);
            let index = self.used.ring[last_used_slot as usize].id.read() as u16;
            let len = self.used.ring[last_used_slot as usize].len.read();

            self.recycle_descriptors(index);
            self.last_used_idx = self.last_used_idx.wrapping_add(1);

            Ok((index, len))
        }

        /// Return size of the queue.
        pub fn size(&self) -> u16 {
            self.queue_size
        }
    }

    /// The inner layout of a VirtQueue.
    ///
    /// Ref: 2.6.2 Legacy Interfaces: A Note on Virtqueue Layout
    struct VirtQueueLayout {
        avail_offset: usize,
        used_offset: usize,
        size: usize,
    }

    impl VirtQueueLayout {
        fn new(queue_size: u16) -> Self {
            assert!(
                queue_size.is_power_of_two(),
                "queue size should be a power of 2"
            );
            let queue_size = queue_size as usize;
            let desc = size_of::<Descriptor>() * queue_size;
            let avail = size_of::<u16>() * (3 + queue_size);
            let used = size_of::<u16>() * 3 + size_of::<UsedElem>() * queue_size;
            VirtQueueLayout {
                avail_offset: desc,
                used_offset: align_up(desc + avail),
                size: align_up(desc + avail) + align_up(used),
            }
        }
    }

    #[repr(C, align(16))]
    #[derive(Debug)]
    struct Descriptor {
        addr: Volatile<u64>,
        len: Volatile<u32>,
        flags: Volatile<DescFlags>,
        next: Volatile<u16>,
    }

    impl Descriptor {
        fn set_buf(&mut self, buf: &[u8]) {
            self.addr.write(virt_to_phys(buf.as_ptr() as usize) as u64);
            self.len.write(buf.len() as u32);
        }
    }

    bitflags! {
        /// Descriptor flags
        struct DescFlags: u16 {
            const NEXT = 1;
            const WRITE = 2;
            const INDIRECT = 4;
        }
    }

    /// The driver uses the available ring to offer buffers to the device:
    /// each ring entry refers to the head of a descriptor chain.
    /// It is only written by the driver and read by the device.
    #[repr(C)]
    #[derive(Debug)]
    struct AvailRing {
        flags: Volatile<u16>,
        /// A driver MUST NOT decrement the idx.
        idx: Volatile<u16>,
        ring: [Volatile<u16>; 32], // actual size: queue_size
        used_event: Volatile<u16>, // unused
    }

    /// The used ring is where the device returns buffers once it is done with them:
    /// it is only written to by the device, and read by the driver.
    #[repr(C)]
    #[derive(Debug)]
    struct UsedRing {
        flags: Volatile<u16>,
        idx: Volatile<u16>,
        ring: [UsedElem; 32],       // actual size: queue_size
        avail_event: Volatile<u16>, // unused
    }

    #[repr(C)]
    #[derive(Debug)]
    struct UsedElem {
        id: Volatile<u32>,
        len: Volatile<u32>,
    }