• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

google / alioth / 17114607742

21 Aug 2025 01:31AM UTC coverage: 10.411%. Remained the same
17114607742

Pull #273

github

web-flow
Merge 897d3fdbf into 7925c9625
Pull Request #273: feat: virtio packed queue

30 of 57 new or added lines in 9 files covered. (52.63%)

50 existing lines in 4 files now uncovered.

714 of 6858 relevant lines covered (10.41%)

16.1 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/alioth/src/virtio/dev/entropy.rs
1
// Copyright 2024 Google LLC
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
use std::fmt::Debug;
16
use std::fs::{File, OpenOptions};
17
use std::os::unix::prelude::OpenOptionsExt;
18
use std::sync::Arc;
19
use std::sync::mpsc::Receiver;
20
use std::thread::JoinHandle;
21

22
use bitflags::bitflags;
23
use libc::O_NONBLOCK;
24
use mio::Registry;
25
use mio::event::Event;
26
use snafu::ResultExt;
27

28
use crate::hv::IoeventFd;
29
use crate::mem;
30
use crate::mem::emulated::{Action, Mmio};
31
use crate::mem::mapped::RamBus;
32
use crate::virtio::dev::{DevParam, DeviceId, Virtio, WakeEvent};
33
use crate::virtio::queue::{Queue, VirtQueue, copy_from_reader};
34
use crate::virtio::worker::Waker;
35
use crate::virtio::worker::mio::{ActiveMio, Mio, VirtioMio};
36
use crate::virtio::{FEATURE_BUILT_IN, IrqSender, Result, error};
37

38
#[derive(Debug, Clone)]
39
pub struct EntropyConfig;
40

41
impl Mmio for EntropyConfig {
42
    fn size(&self) -> u64 {
×
43
        0
44
    }
45

46
    fn read(&self, _offset: u64, _size: u8) -> mem::Result<u64> {
×
47
        Ok(0)
×
48
    }
49

50
    fn write(&self, _offset: u64, _size: u8, _val: u64) -> mem::Result<Action> {
×
51
        Ok(Action::None)
×
52
    }
53
}
54

55
bitflags! {
56
    #[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash)]
57
    pub struct EntropyFeature: u128 { }
58
}
59

60
#[derive(Debug)]
61
pub struct Entropy {
62
    name: Arc<str>,
63
    source: File,
64
    config: Arc<EntropyConfig>,
65
}
66

67
impl Entropy {
68
    pub fn new(name: impl Into<Arc<str>>) -> Result<Self> {
×
69
        let mut options = OpenOptions::new();
×
70
        options.custom_flags(O_NONBLOCK).read(true);
×
71
        let path = "/dev/urandom";
×
72
        let file = options.open(path).context(error::AccessFile { path })?;
×
73
        Ok(Entropy {
×
74
            name: name.into(),
×
75
            source: file,
×
76
            config: Arc::new(EntropyConfig),
×
77
        })
78
    }
79
}
80

81
impl Virtio for Entropy {
82
    type Config = EntropyConfig;
83
    type Feature = EntropyFeature;
84

85
    fn id(&self) -> DeviceId {
×
86
        DeviceId::Entropy
×
87
    }
88

89
    fn name(&self) -> &str {
×
90
        &self.name
×
91
    }
92

93
    fn spawn_worker<S, E>(
×
94
        self,
95
        event_rx: Receiver<WakeEvent<S, E>>,
96
        memory: Arc<RamBus>,
97
        queue_regs: Arc<[Queue]>,
98
    ) -> Result<(JoinHandle<()>, Arc<Waker>)>
99
    where
100
        S: IrqSender,
101
        E: IoeventFd,
102
    {
103
        Mio::spawn_worker(self, event_rx, memory, queue_regs)
×
104
    }
105

106
    fn num_queues(&self) -> u16 {
×
107
        1
108
    }
109

110
    fn config(&self) -> Arc<EntropyConfig> {
×
111
        self.config.clone()
×
112
    }
113

114
    fn feature(&self) -> u128 {
×
115
        FEATURE_BUILT_IN
116
    }
117
}
118

119
impl VirtioMio for Entropy {
120
    fn activate<'a, 'm, Q, S, E>(
×
121
        &mut self,
122
        _feature: u128,
123
        _active_mio: &mut ActiveMio<'a, 'm, Q, S, E>,
124
    ) -> Result<()>
125
    where
126
        Q: VirtQueue<'m>,
127
        S: IrqSender,
128
        E: IoeventFd,
129
    {
130
        Ok(())
×
131
    }
132

133
    fn handle_queue<'a, 'm, Q, S, E>(
×
134
        &mut self,
135
        index: u16,
136
        active_mio: &mut ActiveMio<'a, 'm, Q, S, E>,
137
    ) -> Result<()>
138
    where
139
        Q: VirtQueue<'m>,
140
        S: IrqSender,
141
        E: IoeventFd,
142
    {
143
        let Some(Some(queue)) = active_mio.queues.get_mut(index as usize) else {
×
144
            log::error!("{}: invalid queue index {index}", self.name);
×
145
            return Ok(());
×
146
        };
NEW
147
        queue.handle_desc(index, active_mio.irq_sender, copy_from_reader(&self.source))
×
148
    }
149

150
    fn handle_event<'a, 'm, Q, S, E>(
×
151
        &mut self,
152
        _event: &Event,
153
        _active_mio: &mut ActiveMio<'a, 'm, Q, S, E>,
154
    ) -> Result<()>
155
    where
156
        Q: VirtQueue<'m>,
157
        S: IrqSender,
158
        E: IoeventFd,
159
    {
160
        Ok(())
×
161
    }
162

163
    fn reset(&mut self, _registry: &Registry) {}
×
164
}
165

166
pub struct EntropyParam;
167

168
impl DevParam for EntropyParam {
169
    type Device = Entropy;
170

171
    fn build(self, name: impl Into<Arc<str>>) -> Result<Self::Device> {
×
172
        Entropy::new(name)
×
173
    }
174
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc