-
Notifications
You must be signed in to change notification settings - Fork 206
/
virtiofs.rs
417 lines (358 loc) · 12.4 KB
/
virtiofs.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// Copyright 2020 Ant Group. All rights reserved.
// Copyright (C) 2020 Alibaba Cloud. All rights reserved.
// Copyright 2019 Intel Corporation. All Rights Reserved.
//
// SPDX-License-Identifier: (Apache-2.0 AND BSD-3-Clause)
use std::any::Any;
use std::sync::atomic::{AtomicI32, Ordering};
use std::sync::mpsc::{channel, Receiver, Sender};
use std::sync::{Arc, Mutex, MutexGuard, RwLock};
use std::thread;
use fuse_backend_rs::api::{server::Server, Vfs};
use fuse_backend_rs::transport::{FsCacheReqHandler, Reader, VirtioFsWriter};
use vhost::vhost_user::{message::*, Listener, SlaveFsCacheReq};
use vhost_user_backend::{
VhostUserBackend, VhostUserBackendMut, VhostUserDaemon, VringMutex, VringState, VringT,
};
use virtio_bindings::bindings::virtio_ring::{
VIRTIO_RING_F_EVENT_IDX, VIRTIO_RING_F_INDIRECT_DESC,
};
use virtio_queue::DescriptorChain;
use virtio_queue::QueueOwnedT;
use vm_memory::{GuestAddressSpace, GuestMemoryAtomic, GuestMemoryLoadGuard, GuestMemoryMmap};
use vmm_sys_util::epoll::EventSet;
use vmm_sys_util::eventfd::EventFd;
use nydus::daemon::{
DaemonState, DaemonStateMachineContext, DaemonStateMachineInput, DaemonStateMachineSubscriber,
NydusDaemon,
};
use nydus::upgrade::UpgradeManager;
use nydus::{Error, FsBackendCollection, FsBackendMountCmd, FsService, Result};
use nydus_api::BuildTimeInfo;
const VIRTIO_F_VERSION_1: u32 = 32;
const QUEUE_SIZE: usize = 1024;
const NUM_QUEUES: usize = 2;
// The guest queued an available buffer for the high priority queue.
const HIPRIO_QUEUE_EVENT: u16 = 0;
// The guest queued an available buffer for the request queue.
const REQ_QUEUE_EVENT: u16 = 1;
// The device has been dropped.
// const KILL_EVENT: u16 = 2;
type VhostUserBackendResult<T> = std::io::Result<T>;
struct VhostUserFsBackend {
event_idx: bool,
kill_evt: EventFd,
mem: Option<GuestMemoryAtomic<GuestMemoryMmap>>,
server: Arc<Server<Arc<Vfs>>>,
// handle request from slave to master
vu_req: Option<SlaveFsCacheReq>,
}
impl VhostUserFsBackend {
// There's no way to recover if error happens during processing a virtq, let the caller
// to handle it.
fn process_queue(&mut self, vring_state: &mut MutexGuard<VringState>) -> std::io::Result<bool> {
let mut used_any = false;
let guest_mem = match &self.mem {
Some(m) => m,
None => return Err(Error::QueueMemoryUnset.into()),
};
let avail_chains: Vec<DescriptorChain<GuestMemoryLoadGuard<GuestMemoryMmap>>> = vring_state
.get_queue_mut()
.iter(guest_mem.memory())
.map_err(|_| Error::IterateQueue)?
.collect();
for chain in avail_chains {
used_any = true;
let head_index = chain.head_index();
let mem = chain.memory();
let reader = Reader::from_descriptor_chain(mem, chain.clone())
.map_err(Error::InvalidDescriptorChain)?;
let writer = VirtioFsWriter::new(mem, chain.clone())
.map(|w| w.into())
.map_err(Error::InvalidDescriptorChain)?;
self.server
.handle_message(
reader,
writer,
self.vu_req
.as_mut()
.map(|x| x as &mut dyn FsCacheReqHandler),
None,
)
.map_err(Error::ProcessQueue)?;
if self.event_idx {
if vring_state.add_used(head_index, 0).is_err() {
warn!("Couldn't return used descriptors to the ring");
}
match vring_state.needs_notification() {
Err(_) => {
warn!("Couldn't check if queue needs to be notified");
vring_state.signal_used_queue().unwrap();
}
Ok(needs_notification) => {
if needs_notification {
vring_state.signal_used_queue().unwrap();
}
}
}
} else {
if vring_state.add_used(head_index, 0).is_err() {
warn!("Couldn't return used descriptors to the ring");
}
vring_state.signal_used_queue().unwrap();
}
}
Ok(used_any)
}
}
struct VhostUserFsBackendHandler {
backend: Mutex<VhostUserFsBackend>,
}
impl VhostUserFsBackendHandler {
fn new(vfs: Arc<Vfs>) -> std::io::Result<Self> {
let backend = VhostUserFsBackend {
event_idx: false,
kill_evt: EventFd::new(libc::EFD_NONBLOCK).map_err(Error::Epoll)?,
mem: None,
server: Arc::new(Server::new(vfs)),
vu_req: None,
};
Ok(VhostUserFsBackendHandler {
backend: Mutex::new(backend),
})
}
}
impl VhostUserBackendMut<VringMutex> for VhostUserFsBackendHandler {
fn num_queues(&self) -> usize {
NUM_QUEUES
}
fn max_queue_size(&self) -> usize {
QUEUE_SIZE
}
fn features(&self) -> u64 {
1 << VIRTIO_F_VERSION_1
| 1 << VIRTIO_RING_F_INDIRECT_DESC
| 1 << VIRTIO_RING_F_EVENT_IDX
| VhostUserVirtioFeatures::PROTOCOL_FEATURES.bits()
}
fn protocol_features(&self) -> VhostUserProtocolFeatures {
VhostUserProtocolFeatures::MQ | VhostUserProtocolFeatures::SLAVE_REQ
}
fn set_event_idx(&mut self, _enabled: bool) {
self.backend.lock().unwrap().event_idx = true
}
fn update_memory(
&mut self,
mem: GuestMemoryAtomic<GuestMemoryMmap>,
) -> VhostUserBackendResult<()> {
self.backend.lock().unwrap().mem = Some(mem);
Ok(())
}
fn set_slave_req_fd(&mut self, vu_req: SlaveFsCacheReq) {
self.backend.lock().unwrap().vu_req = Some(vu_req);
}
fn exit_event(&self, _thread_index: usize) -> Option<EventFd> {
// FIXME: need to patch vhost-user-backend to return KILL_EVENT
// so that daemon stop event gets popped up.
Some(self.backend.lock().unwrap().kill_evt.try_clone().unwrap())
}
fn handle_event(
&mut self,
device_event: u16,
evset: EventSet,
vrings: &[VringMutex],
_thread_id: usize,
) -> VhostUserBackendResult<bool> {
if evset != EventSet::IN {
return Err(Error::HandleEventNotEpollIn.into());
}
let mut vring_state = match device_event {
HIPRIO_QUEUE_EVENT => {
debug!("HIPRIO_QUEUE_EVENT");
vrings[0].get_mut()
}
REQ_QUEUE_EVENT => {
debug!("QUEUE_EVENT");
vrings[1].get_mut()
}
_ => return Err(Error::HandleEventUnknownEvent.into()),
};
if self.backend.lock().unwrap().event_idx {
// vm-virtio's Queue implementation only checks avail_index
// once, so to properly support EVENT_IDX we need to keep
// calling process_queue() until it stops finding new
// requests on the queue.
loop {
vring_state.disable_notification().unwrap();
self.backend
.lock()
.unwrap()
.process_queue(&mut vring_state)?;
if !vring_state.enable_notification().unwrap() {
break;
}
}
} else {
// Without EVENT_IDX, a single call is enough.
self.backend
.lock()
.unwrap()
.process_queue(&mut vring_state)?;
}
Ok(false)
}
}
pub struct VirtioFsService {
vfs: Arc<Vfs>,
upgrade_mgr: Option<Mutex<UpgradeManager>>,
backend_collection: Mutex<FsBackendCollection>,
}
impl VirtioFsService {
fn new(vfs: Arc<Vfs>) -> Self {
VirtioFsService {
vfs,
upgrade_mgr: None,
backend_collection: Default::default(),
}
}
}
impl FsService for VirtioFsService {
fn get_vfs(&self) -> &Vfs {
&self.vfs
}
fn upgrade_mgr(&self) -> Option<MutexGuard<UpgradeManager>> {
self.upgrade_mgr.as_ref().map(|mgr| mgr.lock().unwrap())
}
fn backend_collection(&self) -> MutexGuard<FsBackendCollection> {
self.backend_collection.lock().unwrap()
}
fn export_inflight_ops(&self) -> Result<Option<String>> {
Err(Error::Unsupported)
}
fn as_any(&self) -> &dyn Any {
self
}
}
struct VirtiofsDaemon<S: 'static + VhostUserBackend<VringMutex> + Clone> {
bti: BuildTimeInfo,
id: Option<String>,
request_sender: Arc<Mutex<Sender<DaemonStateMachineInput>>>,
result_receiver: Mutex<Receiver<Result<()>>>,
service: Arc<VirtioFsService>,
state: AtomicI32,
supervisor: Option<String>,
daemon: Arc<Mutex<VhostUserDaemon<S, VringMutex>>>,
sock: String,
}
impl<S: 'static + VhostUserBackend<VringMutex> + Clone> NydusDaemon for VirtiofsDaemon<S> {
fn as_any(&self) -> &dyn Any {
self
}
fn id(&self) -> Option<String> {
self.id.clone()
}
fn get_state(&self) -> DaemonState {
self.state.load(Ordering::Relaxed).into()
}
fn set_state(&self, state: DaemonState) {
self.state.store(state as i32, Ordering::Relaxed);
}
fn version(&self) -> BuildTimeInfo {
self.bti.clone()
}
fn start(&self) -> Result<()> {
let listener =
Listener::new(&self.sock, true).map_err(|e| Error::StartService(format!("{}", e)))?;
let vu_daemon = self.daemon.clone();
let _ = thread::Builder::new()
.name("vhost_user_listener".to_string())
.spawn(move || {
vu_daemon
.lock()
.unwrap()
.start(listener)
.unwrap_or_else(|e| error!("{:?}", e));
})
.map_err(Error::ThreadSpawn)?;
Ok(())
}
fn umount(&self) -> Result<()> {
Ok(())
}
fn wait(&self) -> Result<()> {
self.daemon
.lock()
.unwrap()
.wait()
.map_err(|e| Error::WaitDaemon(eother!(e)))
}
fn supervisor(&self) -> Option<String> {
self.supervisor.clone()
}
fn save(&self) -> Result<()> {
Err(Error::Unsupported)
}
fn restore(&self) -> Result<()> {
Err(Error::Unsupported)
}
fn get_default_fs_service(&self) -> Option<Arc<dyn FsService>> {
Some(self.service.clone())
}
}
impl<S: 'static + VhostUserBackend<VringMutex> + Clone> DaemonStateMachineSubscriber
for VirtiofsDaemon<S>
{
fn on_event(&self, event: DaemonStateMachineInput) -> Result<()> {
self.request_sender
.lock()
.unwrap()
.send(event)
.map_err(Error::ChannelSend)?;
self.result_receiver
.lock()
.expect("Not expect poisoned lock!")
.recv()
.map_err(Error::ChannelReceive)?
}
}
pub fn create_virtiofs_daemon(
id: Option<String>,
supervisor: Option<String>,
sock: &str,
vfs: Arc<Vfs>,
mount_cmd: Option<FsBackendMountCmd>,
bti: BuildTimeInfo,
) -> std::io::Result<Arc<dyn NydusDaemon>> {
let vu_daemon = VhostUserDaemon::new(
String::from("vhost-user-fs-backend"),
Arc::new(RwLock::new(VhostUserFsBackendHandler::new(vfs.clone())?)),
GuestMemoryAtomic::new(GuestMemoryMmap::new()),
)
.map_err(|e| Error::VhostUser(format!("{:?}", e)))?;
let (trigger, events_rx) = channel::<DaemonStateMachineInput>();
let (result_sender, result_receiver) = channel::<Result<()>>();
let service = VirtioFsService::new(vfs);
let daemon = Arc::new(VirtiofsDaemon {
bti,
id,
request_sender: Arc::new(Mutex::new(trigger)),
result_receiver: Mutex::new(result_receiver),
service: Arc::new(service),
state: AtomicI32::new(DaemonState::INIT as i32),
supervisor,
daemon: Arc::new(Mutex::new(vu_daemon)),
sock: sock.to_string(),
});
let machine = DaemonStateMachineContext::new(daemon.clone(), events_rx, result_sender);
machine.kick_state_machine()?;
if let Some(cmd) = mount_cmd {
daemon.service.mount(cmd)?;
}
daemon
.on_event(DaemonStateMachineInput::Mount)
.map_err(|e| eother!(e))?;
daemon
.on_event(DaemonStateMachineInput::Start)
.map_err(|e| eother!(e))?;
Ok(daemon)
}