|
| 1 | +// Copyright 2026, Red Hat Inc. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//! Vhost-user RNG device implementation. |
| 5 | +
|
| 6 | +use std::io::Result as IoResult; |
| 7 | + |
| 8 | +use vm_memory::GuestMemoryMmap; |
| 9 | + |
| 10 | +use super::device::VhostUserDevice; |
| 11 | +use crate::virtio::{ActivateResult, InterruptTransport, Queue as VirtQueue, VirtioDevice}; |
| 12 | + |
| 13 | +// RNG device constants |
| 14 | +const RNG_DEV_ID: &str = "vhost_user_rng"; |
| 15 | +const NUM_QUEUES: usize = 1; |
| 16 | +const QUEUE_SIZES: &[u16] = &[256; NUM_QUEUES]; |
| 17 | +const VIRTIO_ID_RNG: u32 = 4; |
| 18 | + |
| 19 | +/// Vhost-user RNG device. |
| 20 | +/// |
| 21 | +/// This is a thin wrapper around VhostUserDevice specialized for the RNG device type. |
| 22 | +pub struct VhostUserRng { |
| 23 | + inner: VhostUserDevice, |
| 24 | +} |
| 25 | + |
| 26 | +impl VhostUserRng { |
| 27 | + /// Create a new vhost-user RNG device. |
| 28 | + pub fn new(socket_path: &str) -> IoResult<Self> { |
| 29 | + let queues: Vec<VirtQueue> = QUEUE_SIZES |
| 30 | + .iter() |
| 31 | + .map(|&max_size| VirtQueue::new(max_size)) |
| 32 | + .collect(); |
| 33 | + |
| 34 | + let inner = |
| 35 | + VhostUserDevice::new(socket_path, VIRTIO_ID_RNG, RNG_DEV_ID.to_string(), queues)?; |
| 36 | + |
| 37 | + Ok(VhostUserRng { inner }) |
| 38 | + } |
| 39 | + |
| 40 | + pub fn id(&self) -> &str { |
| 41 | + RNG_DEV_ID |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +impl VirtioDevice for VhostUserRng { |
| 46 | + fn device_type(&self) -> u32 { |
| 47 | + self.inner.device_type() |
| 48 | + } |
| 49 | + |
| 50 | + fn device_name(&self) -> &str { |
| 51 | + self.inner.device_name() |
| 52 | + } |
| 53 | + |
| 54 | + fn queues(&self) -> &[VirtQueue] { |
| 55 | + self.inner.queues() |
| 56 | + } |
| 57 | + |
| 58 | + fn queues_mut(&mut self) -> &mut [VirtQueue] { |
| 59 | + self.inner.queues_mut() |
| 60 | + } |
| 61 | + |
| 62 | + fn queue_events(&self) -> &[utils::eventfd::EventFd] { |
| 63 | + self.inner.queue_events() |
| 64 | + } |
| 65 | + |
| 66 | + fn avail_features(&self) -> u64 { |
| 67 | + self.inner.avail_features() |
| 68 | + } |
| 69 | + |
| 70 | + fn acked_features(&self) -> u64 { |
| 71 | + self.inner.acked_features() |
| 72 | + } |
| 73 | + |
| 74 | + fn set_acked_features(&mut self, acked_features: u64) { |
| 75 | + self.inner.set_acked_features(acked_features) |
| 76 | + } |
| 77 | + |
| 78 | + fn read_config(&self, offset: u64, data: &mut [u8]) { |
| 79 | + self.inner.read_config(offset, data) |
| 80 | + } |
| 81 | + |
| 82 | + fn write_config(&mut self, offset: u64, data: &[u8]) { |
| 83 | + self.inner.write_config(offset, data) |
| 84 | + } |
| 85 | + |
| 86 | + fn activate(&mut self, mem: GuestMemoryMmap, interrupt: InterruptTransport) -> ActivateResult { |
| 87 | + self.inner.activate(mem, interrupt) |
| 88 | + } |
| 89 | + |
| 90 | + fn is_activated(&self) -> bool { |
| 91 | + self.inner.is_activated() |
| 92 | + } |
| 93 | + |
| 94 | + fn reset(&mut self) -> bool { |
| 95 | + self.inner.reset() |
| 96 | + } |
| 97 | +} |
0 commit comments