device_virtio_console/
output.rs

1// Copyright 2024 The ChromiumOS Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5//! Virtio console device output handling.
6
7use std::io;
8
9use base::error;
10use devices::virtio::Queue;
11use devices::virtio::Reader;
12
13/// Writes the available data from the reader into the given output queue.
14///
15/// # Arguments
16///
17/// * `reader` - The Reader with the data we want to write.
18/// * `output` - The output sink we are going to write the data to.
19fn process_transmit_request(reader: &mut Reader, output: &mut dyn io::Write) -> io::Result<()> {
20    // The guest sets the chain's size, up to 4 GiB, so stream it.
21    io::copy(reader, output)?;
22    output.flush()
23}
24
25/// Processes the data taken from the given transmit queue into the output sink.
26///
27/// # Arguments
28///
29/// * `interrupt` - Interrupt used to signal (if required) that the queue has been used
30/// * `transmit_queue` - The transmit virtio Queue
31/// * `output` - The output sink we are going to write the data into
32pub fn process_transmit_queue(transmit_queue: &mut Queue, output: &mut dyn io::Write) {
33    let mut needs_interrupt = false;
34    while let Some(mut avail_desc) = transmit_queue.pop() {
35        if let Err(e) = process_transmit_request(&mut avail_desc.reader, output) {
36            error!("console: process_transmit_request failed: {}", e);
37        }
38
39        transmit_queue.add_used(avail_desc);
40        needs_interrupt = true;
41    }
42
43    if needs_interrupt {
44        transmit_queue.trigger_interrupt();
45    }
46}
47
48#[cfg(test)]
49mod tests {
50    use devices::virtio::create_descriptor_chain;
51    use devices::virtio::DescriptorType;
52    use vm_memory::GuestAddress;
53    use vm_memory::GuestMemory;
54
55    use super::*;
56
57    #[derive(Default)]
58    struct RecordingSink {
59        data: Vec<u8>,
60        largest_write: usize,
61    }
62
63    impl io::Write for RecordingSink {
64        fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
65            self.largest_write = self.largest_write.max(buf.len());
66            self.data.extend_from_slice(buf);
67            Ok(buf.len())
68        }
69
70        fn flush(&mut self) -> io::Result<()> {
71            Ok(())
72        }
73    }
74
75    #[test]
76    fn transmit_request_is_streamed() {
77        const LEN: usize = 0x10_0000;
78        let mem = GuestMemory::new(&[(GuestAddress(0), 2 * LEN as u64)]).unwrap();
79        let payload: Vec<u8> = (0..LEN).map(|i| i as u8).collect();
80        mem.write_all_at_addr(&payload, GuestAddress(LEN as u64))
81            .unwrap();
82        let mut chain = create_descriptor_chain(
83            &mem,
84            GuestAddress(0),
85            GuestAddress(LEN as u64),
86            vec![(DescriptorType::Readable, LEN as u32)],
87            0,
88        )
89        .unwrap();
90
91        let mut sink = RecordingSink::default();
92        process_transmit_request(&mut chain.reader, &mut sink).unwrap();
93
94        assert_eq!(sink.data, payload);
95        // The chain's data is not gathered into one buffer.
96        assert!(sink.largest_write < LEN);
97    }
98}