summary refs log tree commit diff
path: root/devices/src/virtio/rng.rs
diff options
context:
space:
mode:
authorJakub Staron <jstaron@google.com>2019-05-28 17:38:07 -0700
committerCommit Bot <commit-bot@chromium.org>2019-10-01 22:48:38 +0000
commitd9c4398a31b129a8e1eed10981d16351c2edcaf4 (patch)
treeb570ca5a08a523de65f36c635a12adbded40b7a0 /devices/src/virtio/rng.rs
parent719f2831ed394bf4f3fd777938261bda493b05fc (diff)
downloadcrosvm-d9c4398a31b129a8e1eed10981d16351c2edcaf4.tar
crosvm-d9c4398a31b129a8e1eed10981d16351c2edcaf4.tar.gz
crosvm-d9c4398a31b129a8e1eed10981d16351c2edcaf4.tar.bz2
crosvm-d9c4398a31b129a8e1eed10981d16351c2edcaf4.tar.lz
crosvm-d9c4398a31b129a8e1eed10981d16351c2edcaf4.tar.xz
crosvm-d9c4398a31b129a8e1eed10981d16351c2edcaf4.tar.zst
crosvm-d9c4398a31b129a8e1eed10981d16351c2edcaf4.zip
crosvm: Use Reader/Writer interfaces in various virtio devices.
Switching the devices to the new interface reduces code duplication and
will ease fuzzing the devices as they now have a common input and output
interface for descriptors.

BUG=chromium:966258
TEST=vm.CrostiniStartEverything

Change-Id: I823c04dfc24e017433f8e8ab167bbd5dfafd338b
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1647371
Reviewed-by: Stephen Barber <smbarber@chromium.org>
Reviewed-by: Zach Reizner <zachr@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Diffstat (limited to 'devices/src/virtio/rng.rs')
-rw-r--r--devices/src/virtio/rng.rs28
1 files changed, 12 insertions, 16 deletions
diff --git a/devices/src/virtio/rng.rs b/devices/src/virtio/rng.rs
index 1ad683a..4e6448f 100644
--- a/devices/src/virtio/rng.rs
+++ b/devices/src/virtio/rng.rs
@@ -11,9 +11,9 @@ use std::sync::atomic::{AtomicUsize, Ordering};
 use std::sync::Arc;
 use std::thread;
 
-use sys_util::{error, EventFd, GuestMemory, PollContext, PollToken};
+use sys_util::{error, warn, EventFd, GuestMemory, PollContext, PollToken};
 
-use super::{Queue, VirtioDevice, INTERRUPT_STATUS_USED_RING, TYPE_RNG};
+use super::{Queue, VirtioDevice, Writer, INTERRUPT_STATUS_USED_RING, TYPE_RNG};
 
 const QUEUE_SIZE: u16 = 256;
 const QUEUE_SIZES: &[u16] = &[QUEUE_SIZE];
@@ -50,21 +50,17 @@ impl Worker {
 
         let mut needs_interrupt = false;
         while let Some(avail_desc) = queue.pop(&self.mem) {
-            let mut len = 0;
-
-            // Drivers can only read from the random device.
-            if avail_desc.is_write_only() {
-                // Fill the read with data from the random device on the host.
-                if self
-                    .mem
-                    .read_to_memory(avail_desc.addr, &self.random_file, avail_desc.len as usize)
-                    .is_ok()
-                {
-                    len = avail_desc.len;
+            let index = avail_desc.index;
+            let mut writer = Writer::new(&self.mem, avail_desc);
+            // Fill the entire descriptor chain buffer with random bytes.
+            let written = match writer.write_from(&self.random_file, std::usize::MAX) {
+                Ok(n) => n,
+                Err(e) => {
+                    warn!("Failed to write random data to the guest: {}", e);
+                    0
                 }
-            }
-
-            queue.add_used(&self.mem, avail_desc.index, len);
+            };
+            queue.add_used(&self.mem, index, written as u32);
             needs_interrupt = true;
         }