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
// Copyright 2023 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//! Packed virtqueue descriptor chain iterator

#![deny(missing_docs)]

use anyhow::bail;
use anyhow::Context;
use anyhow::Result;
use base::error;
use base::trace;
use data_model::Le16;
use data_model::Le32;
use data_model::Le64;
use vm_memory::GuestAddress;
use vm_memory::GuestMemory;
use zerocopy::AsBytes;
use zerocopy::FromBytes;
use zerocopy::FromZeroes;

use crate::virtio::descriptor_chain::Descriptor;
use crate::virtio::descriptor_chain::DescriptorAccess;
use crate::virtio::descriptor_chain::DescriptorChainIter;
use crate::virtio::descriptor_chain::VIRTQ_DESC_F_AVAIL;
use crate::virtio::descriptor_chain::VIRTQ_DESC_F_NEXT;
use crate::virtio::descriptor_chain::VIRTQ_DESC_F_USED;
use crate::virtio::descriptor_chain::VIRTQ_DESC_F_WRITE;

/// Enable events
pub const RING_EVENT_FLAGS_ENABLE: u16 = 0x0;
/// Disable events
pub const RING_EVENT_FLAGS_DISABLE: u16 = 0x1;

/// Enable events for a specific descriptor.
/// Only valid if VIRTIO_F_RING_EVENT_IDX has been negotiated.
pub const RING_EVENT_FLAGS_DESC: u16 = 0x2;

/// A packed virtio packed queue descriptor (`struct pvirtq_desc` in the spec).
#[derive(Copy, Clone, Debug, FromZeroes, FromBytes, AsBytes)]
#[repr(C)]
pub struct PackedDesc {
    /// Guest address of memory buffer address
    pub addr: Le64,

    /// Memory buffer length in bytes
    pub len: Le32,

    /// Buffer ID
    pub id: Le16,

    /// The flags depending on descriptor type
    pub flags: Le16,
}

impl PackedDesc {
    pub fn addr(&self) -> u64 {
        self.addr.into()
    }

    pub fn len(&self) -> u32 {
        self.len.into()
    }

    pub fn flags(&self) -> u16 {
        self.flags.into()
    }

    pub fn id(&self) -> u16 {
        self.id.into()
    }

    pub fn has_next(&self) -> bool {
        self.flags() & VIRTQ_DESC_F_NEXT != 0
    }

    pub fn is_available(&self, wrap_value: u16) -> bool {
        let avail = (self.flags() & VIRTQ_DESC_F_AVAIL) != 0;
        let used = (self.flags() & VIRTQ_DESC_F_USED) != 0;
        let wrap = wrap_value != 0;
        avail != used && avail == wrap
    }
}

#[derive(Copy, Clone, Debug, FromZeroes, FromBytes, AsBytes)]
#[repr(C)]
pub struct PackedDescEvent {
    pub desc: Le16,
    pub flag: Le16,
}

impl PackedDescEvent {
    pub fn notification_type(&self) -> PackedNotificationType {
        let flag: u16 = self.flag.into();

        if flag == RING_EVENT_FLAGS_DISABLE {
            PackedNotificationType::Disable
        } else if flag == RING_EVENT_FLAGS_DESC {
            PackedNotificationType::Desc(self.desc.into())
        } else if flag == RING_EVENT_FLAGS_ENABLE {
            PackedNotificationType::Enable
        } else {
            let desc: u16 = self.desc.into();
            error!("Unknown packed desc event flag:{:x}, desc:{:x}", flag, desc);
            PackedNotificationType::Enable
        }
    }
}

pub enum PackedNotificationType {
    Enable,
    Disable,
    Desc(u16),
}

pub struct PackedDescriptorChain<'m> {
    avail_wrap_counter: bool,

    /// Current descriptor index within `desc_table`, or `None` if the iterator is exhausted.
    index: Option<u16>,

    /// Number of descriptors returned by the iterator already.
    /// If `count` reaches `queue_size`, the chain has a loop and is therefore invalid.
    count: u16,

    /// Buffer Id, which locates at the last descriptor in the chain
    id: Option<u16>,

    queue_size: u16,

    mem: &'m GuestMemory,
    desc_table: GuestAddress,
}

impl<'m> PackedDescriptorChain<'m> {
    /// Construct a new iterator over a split virtqueue descriptor chain.
    ///
    /// # Arguments
    /// * `mem` - The [`GuestMemory`] containing the descriptor chain.
    /// * `desc_table` - Guest physical address of the descriptor table.
    /// * `queue_size` - Total number of entries in the descriptor table.
    /// * `index` - The index of the first descriptor in the chain.
    pub fn new(
        mem: &'m GuestMemory,
        desc_table: GuestAddress,
        queue_size: u16,
        avail_wrap_counter: bool,
        index: u16,
    ) -> PackedDescriptorChain<'m> {
        trace!("starting packed descriptor chain head={index}");
        PackedDescriptorChain {
            index: Some(index),
            count: 0,
            id: None,
            queue_size,
            mem,
            desc_table,
            avail_wrap_counter,
        }
    }
}

impl DescriptorChainIter for PackedDescriptorChain<'_> {
    fn next(&mut self) -> Result<Option<Descriptor>> {
        let index = match self.index {
            Some(index) => index,
            None => {
                return Ok(None);
            }
        };

        if index >= self.queue_size {
            bail!(
                "out of bounds descriptor index {} for queue size {}",
                index,
                self.queue_size
            );
        }

        if self.count >= self.queue_size {
            bail!("descriptor chain loop detected");
        }

        let desc_addr = self
            .desc_table
            .checked_add((index as u64) * 16)
            .context("integer overflow")?;
        let desc = self
            .mem
            .read_obj_from_addr::<PackedDesc>(desc_addr)
            .with_context(|| format!("failed to read desc {:#x}", desc_addr.offset()))?;

        let address: u64 = desc.addr();
        let len: u32 = desc.len();
        let flags: u16 = desc.flags();

        trace!("{index:5}: addr={address:#016x} len={len:#08x} flags={flags:#x}");

        if !desc.is_available(self.avail_wrap_counter as u16) {
            return Ok(None);
        }

        if len == 0 {
            bail!("invalid zero-length descriptor");
        }

        let unexpected_flags = flags
            & !(VIRTQ_DESC_F_WRITE | VIRTQ_DESC_F_NEXT | VIRTQ_DESC_F_AVAIL | VIRTQ_DESC_F_USED);
        if unexpected_flags != 0 {
            bail!("unexpected flags in descriptor {index}: {unexpected_flags:#x}")
        }

        let access = if flags & VIRTQ_DESC_F_WRITE != 0 {
            DescriptorAccess::DeviceWrite
        } else {
            DescriptorAccess::DeviceRead
        };

        // If VIRTQ_DESC_F_NEXT exists, the next descriptor in descriptor chain
        // is the next element in descriptor table. When index reaches the end of
        // descriptor table, we need to flip avail_wrap_counter.
        if desc.has_next() {
            if index + 1 < self.queue_size {
                self.index = Some(index + 1);
            } else {
                self.index = Some(0);
                self.avail_wrap_counter = !self.avail_wrap_counter;
            }
        } else {
            self.id = Some(desc.id());
            self.index = None;
        }

        self.count += 1;

        Ok(Some(Descriptor {
            address,
            len,
            access,
        }))
    }

    fn count(&self) -> u16 {
        self.count
    }

    fn id(&self) -> Option<u16> {
        self.id
    }
}