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

google / alioth / 18638570188

20 Oct 2025 12:38AM UTC coverage: 20.202% (-0.01%) from 20.213%
18638570188

Pull #308

github

web-flow
Merge 73a1640e9 into 416357998
Pull Request #308: Add tests for PciSegment

0 of 59 new or added lines in 5 files covered. (0.0%)

1163 existing lines in 25 files now uncovered.

1578 of 7811 relevant lines covered (20.2%)

19.85 hits per line

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

0.0
/alioth/src/virtio/vhost.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::fs::File;
16
use std::os::fd::AsRawFd;
17
use std::path::{Path, PathBuf};
18
use std::sync::Arc;
19

20
use snafu::{ResultExt, Snafu};
21

22
use crate::errors::{BoxTrace, DebugTrace, trace_error};
23
use crate::mem::mapped::Ram;
24
use crate::mem::{self, LayoutUpdated};
25
use crate::sys::vhost::{
26
    MemoryMultipleRegion, MemoryRegion, VhostFeature, VirtqAddr, VirtqFile, VirtqState,
27
    vhost_get_backend_features, vhost_get_features, vhost_set_backend_features, vhost_set_features,
28
    vhost_set_mem_table, vhost_set_owner, vhost_set_virtq_addr, vhost_set_virtq_base,
29
    vhost_set_virtq_call, vhost_set_virtq_err, vhost_set_virtq_kick, vhost_set_virtq_num,
30
    vhost_vsock_set_guest_cid, vhost_vsock_set_running,
31
};
32

33
#[trace_error]
34
#[derive(Snafu, DebugTrace)]
35
#[snafu(module, visibility(pub(crate)), context(suffix(false)))]
36
pub enum Error {
37
    #[snafu(display("Error from OS"), context(false))]
38
    System { error: std::io::Error },
39
    #[snafu(display("Cannot access device {path:?}"))]
40
    AccessDevice {
41
        path: PathBuf,
42
        error: std::io::Error,
43
    },
44
    #[snafu(display("vhost backend is missing device feature {feature:#x}"))]
45
    VhostMissingDeviceFeature { feature: u128 },
46
    #[snafu(display("vhost-{dev} signals an error of queue {index:#x}"))]
47
    VhostQueueErr { dev: &'static str, index: u16 },
48
}
49

50
type Result<T, E = Error> = std::result::Result<T, E>;
51

52
#[derive(Debug)]
53
pub struct VhostDev {
54
    fd: File,
55
}
56

57
impl VhostDev {
58
    pub fn new<P: AsRef<Path>>(path: P) -> Result<Self> {
×
59
        let fd = File::open(&path).context(error::AccessDevice {
×
60
            path: path.as_ref(),
×
61
        })?;
62
        Ok(VhostDev { fd })
×
63
    }
64

UNCOV
65
    pub fn get_features(&self) -> Result<u64> {
UNCOV
66
        let feat = unsafe { vhost_get_features(&self.fd) }?;
UNCOV
67
        Ok(feat)
68
    }
69

UNCOV
70
    pub fn set_features(&self, val: &u64) -> Result<()> {
UNCOV
71
        unsafe { vhost_set_features(&self.fd, val) }?;
UNCOV
72
        Ok(())
73
    }
74

UNCOV
75
    pub fn get_backend_features(&self) -> Result<VhostFeature> {
UNCOV
76
        let feat = unsafe { vhost_get_backend_features(&self.fd) }?;
UNCOV
77
        Ok(VhostFeature::from_bits_retain(feat))
78
    }
79

UNCOV
80
    pub fn set_backend_features(&self, val: &VhostFeature) -> Result<()> {
UNCOV
81
        unsafe { vhost_set_backend_features(&self.fd, &val.bits()) }?;
UNCOV
82
        Ok(())
83
    }
84

UNCOV
85
    pub fn set_owner(&self) -> Result<()> {
UNCOV
86
        unsafe { vhost_set_owner(&self.fd) }?;
UNCOV
87
        Ok(())
88
    }
89

UNCOV
90
    pub fn set_virtq_num(&self, state: &VirtqState) -> Result<()> {
UNCOV
91
        unsafe { vhost_set_virtq_num(&self.fd, state) }?;
UNCOV
92
        Ok(())
93
    }
94

UNCOV
95
    pub fn set_virtq_addr(&self, addr: &VirtqAddr) -> Result<()> {
UNCOV
96
        unsafe { vhost_set_virtq_addr(&self.fd, addr) }?;
UNCOV
97
        Ok(())
98
    }
99

UNCOV
100
    pub fn set_virtq_base(&self, state: &VirtqState) -> Result<()> {
UNCOV
101
        unsafe { vhost_set_virtq_base(&self.fd, state) }?;
UNCOV
102
        Ok(())
103
    }
104

UNCOV
105
    pub fn set_virtq_kick(&self, file: &VirtqFile) -> Result<()> {
UNCOV
106
        unsafe { vhost_set_virtq_kick(&self.fd, file) }?;
UNCOV
107
        Ok(())
108
    }
109

UNCOV
110
    pub fn set_virtq_call(&self, file: &VirtqFile) -> Result<()> {
UNCOV
111
        unsafe { vhost_set_virtq_call(&self.fd, file) }?;
UNCOV
112
        Ok(())
113
    }
114

UNCOV
115
    pub fn set_virtq_err(&self, file: &VirtqFile) -> Result<()> {
UNCOV
116
        unsafe { vhost_set_virtq_err(&self.fd, file) }?;
UNCOV
117
        Ok(())
118
    }
119

120
    pub fn set_mem_table<const N: usize>(&self, table: &MemoryMultipleRegion<N>) -> Result<()> {
×
121
        unsafe { vhost_set_mem_table(&self.fd, table) }?;
×
122
        Ok(())
×
123
    }
124

UNCOV
125
    pub fn vsock_set_guest_cid(&self, cid: u64) -> Result<()> {
UNCOV
126
        unsafe { vhost_vsock_set_guest_cid(&self.fd, &cid) }?;
UNCOV
127
        Ok(())
128
    }
129

UNCOV
130
    pub fn vsock_set_running(&self, val: bool) -> Result<()> {
UNCOV
131
        unsafe { vhost_vsock_set_running(&self.fd, &(val as _)) }?;
UNCOV
132
        Ok(())
133
    }
134
}
135

136
#[derive(Debug)]
137
pub struct UpdateVsockMem {
138
    pub dev: Arc<VhostDev>,
139
}
140

141
impl LayoutUpdated for UpdateVsockMem {
UNCOV
142
    fn ram_updated(&self, ram: &Ram) -> mem::Result<()> {
143
        let mut table = MemoryMultipleRegion {
144
            num: 0,
145
            _padding: 0,
UNCOV
146
            regions: [MemoryRegion::default(); 64],
147
        };
UNCOV
148
        for (index, (gpa, user_mem)) in ram.iter().enumerate() {
UNCOV
149
            table.num += 1;
UNCOV
150
            table.regions[index].gpa = gpa;
UNCOV
151
            table.regions[index].hva = user_mem.addr() as u64;
UNCOV
152
            table.regions[index].size = user_mem.size();
153
        }
UNCOV
154
        let ret = self.dev.set_mem_table(&table);
UNCOV
155
        ret.box_trace(mem::error::ChangeLayout)?;
UNCOV
156
        log::trace!(
UNCOV
157
            "vhost-{}: updated mem table to {:x?}",
UNCOV
158
            self.dev.fd.as_raw_fd(),
UNCOV
159
            &table.regions[..table.num as usize]
160
        );
UNCOV
161
        Ok(())
162
    }
163
}
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