xref: /cloud-hypervisor/virtio-devices/src/vhost_user/net.rs (revision 5e52729453cb62edbe4fb3a4aa24f8cca31e667e)
1 // Copyright 2019 Intel Corporation. All Rights Reserved.
2 // SPDX-License-Identifier: Apache-2.0
3 
4 use crate::seccomp_filters::Thread;
5 use crate::thread_helper::spawn_virtio_thread;
6 use crate::vhost_user::vu_common_ctrl::{VhostUserConfig, VhostUserHandle};
7 use crate::vhost_user::{Error, Result, VhostUserCommon};
8 use crate::{
9     ActivateResult, NetCtrlEpollHandler, VirtioCommon, VirtioDevice, VirtioDeviceType,
10     VirtioInterrupt, VIRTIO_F_IOMMU_PLATFORM, VIRTIO_F_RING_EVENT_IDX, VIRTIO_F_VERSION_1,
11 };
12 use crate::{GuestMemoryMmap, GuestRegionMmap};
13 use net_util::{build_net_config_space, CtrlQueue, MacAddr, VirtioNetConfig};
14 use seccompiler::SeccompAction;
15 use std::result;
16 use std::sync::atomic::AtomicBool;
17 use std::sync::{Arc, Barrier, Mutex};
18 use std::thread;
19 use std::vec::Vec;
20 use versionize::{VersionMap, Versionize, VersionizeResult};
21 use versionize_derive::Versionize;
22 use vhost::vhost_user::message::{VhostUserProtocolFeatures, VhostUserVirtioFeatures};
23 use vhost::vhost_user::{MasterReqHandler, VhostUserMaster, VhostUserMasterReqHandler};
24 use virtio_bindings::bindings::virtio_net::{
25     VIRTIO_NET_F_CSUM, VIRTIO_NET_F_CTRL_VQ, VIRTIO_NET_F_GUEST_CSUM, VIRTIO_NET_F_GUEST_ECN,
26     VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, VIRTIO_NET_F_GUEST_UFO,
27     VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_TSO6, VIRTIO_NET_F_HOST_UFO,
28     VIRTIO_NET_F_MAC, VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_MTU,
29 };
30 use virtio_bindings::bindings::virtio_ring::VIRTIO_RING_F_EVENT_IDX;
31 use virtio_queue::{Queue, QueueT};
32 use vm_memory::{ByteValued, GuestMemoryAtomic};
33 use vm_migration::{
34     protocol::MemoryRangeTable, Migratable, MigratableError, Pausable, Snapshot, Snapshottable,
35     Transportable, VersionMapped,
36 };
37 use vmm_sys_util::eventfd::EventFd;
38 
39 const DEFAULT_QUEUE_NUMBER: usize = 2;
40 
41 #[derive(Versionize)]
42 pub struct State {
43     pub avail_features: u64,
44     pub acked_features: u64,
45     pub config: VirtioNetConfig,
46     pub acked_protocol_features: u64,
47     pub vu_num_queues: usize,
48 }
49 
50 impl VersionMapped for State {}
51 
52 struct SlaveReqHandler {}
53 impl VhostUserMasterReqHandler for SlaveReqHandler {}
54 
55 pub struct Net {
56     common: VirtioCommon,
57     vu_common: VhostUserCommon,
58     id: String,
59     config: VirtioNetConfig,
60     guest_memory: Option<GuestMemoryAtomic<GuestMemoryMmap>>,
61     ctrl_queue_epoll_thread: Option<thread::JoinHandle<()>>,
62     epoll_thread: Option<thread::JoinHandle<()>>,
63     seccomp_action: SeccompAction,
64     exit_evt: EventFd,
65     iommu: bool,
66 }
67 
68 impl Net {
69     /// Create a new vhost-user-net device
70     #[allow(clippy::too_many_arguments)]
71     pub fn new(
72         id: String,
73         mac_addr: MacAddr,
74         mtu: Option<u16>,
75         vu_cfg: VhostUserConfig,
76         server: bool,
77         seccomp_action: SeccompAction,
78         exit_evt: EventFd,
79         iommu: bool,
80         state: Option<State>,
81     ) -> Result<Net> {
82         let mut num_queues = vu_cfg.num_queues;
83 
84         let mut vu =
85             VhostUserHandle::connect_vhost_user(server, &vu_cfg.socket, num_queues as u64, false)?;
86 
87         let (
88             avail_features,
89             acked_features,
90             acked_protocol_features,
91             vu_num_queues,
92             config,
93             paused,
94         ) = if let Some(state) = state {
95             info!("Restoring vhost-user-net {}", id);
96 
97             // The backend acknowledged features must not contain
98             // VIRTIO_NET_F_MAC since we don't expect the backend
99             // to handle it.
100             let backend_acked_features = state.acked_features & !(1 << VIRTIO_NET_F_MAC);
101 
102             vu.set_protocol_features_vhost_user(
103                 backend_acked_features,
104                 state.acked_protocol_features,
105             )?;
106 
107             // If the control queue feature has been negotiated, let's
108             // increase the number of queues.
109             if state.acked_features & (1 << VIRTIO_NET_F_CTRL_VQ) != 0 {
110                 num_queues += 1;
111             }
112 
113             (
114                 state.avail_features,
115                 state.acked_features,
116                 state.acked_protocol_features,
117                 state.vu_num_queues,
118                 state.config,
119                 true,
120             )
121         } else {
122             // Filling device and vring features VMM supports.
123             let mut avail_features = 1 << VIRTIO_NET_F_CSUM
124                 | 1 << VIRTIO_NET_F_GUEST_CSUM
125                 | 1 << VIRTIO_NET_F_GUEST_TSO4
126                 | 1 << VIRTIO_NET_F_GUEST_TSO6
127                 | 1 << VIRTIO_NET_F_GUEST_ECN
128                 | 1 << VIRTIO_NET_F_GUEST_UFO
129                 | 1 << VIRTIO_NET_F_HOST_TSO4
130                 | 1 << VIRTIO_NET_F_HOST_TSO6
131                 | 1 << VIRTIO_NET_F_HOST_ECN
132                 | 1 << VIRTIO_NET_F_HOST_UFO
133                 | 1 << VIRTIO_NET_F_MRG_RXBUF
134                 | 1 << VIRTIO_NET_F_CTRL_VQ
135                 | 1 << VIRTIO_F_RING_EVENT_IDX
136                 | 1 << VIRTIO_F_VERSION_1
137                 | VhostUserVirtioFeatures::PROTOCOL_FEATURES.bits();
138 
139             if mtu.is_some() {
140                 avail_features |= 1u64 << VIRTIO_NET_F_MTU;
141             }
142 
143             let mut config = VirtioNetConfig::default();
144             build_net_config_space(&mut config, mac_addr, num_queues, mtu, &mut avail_features);
145 
146             let avail_protocol_features = VhostUserProtocolFeatures::MQ
147                 | VhostUserProtocolFeatures::CONFIGURE_MEM_SLOTS
148                 | VhostUserProtocolFeatures::REPLY_ACK
149                 | VhostUserProtocolFeatures::INFLIGHT_SHMFD
150                 | VhostUserProtocolFeatures::LOG_SHMFD;
151 
152             let (mut acked_features, acked_protocol_features) =
153                 vu.negotiate_features_vhost_user(avail_features, avail_protocol_features)?;
154 
155             let backend_num_queues =
156                 if acked_protocol_features & VhostUserProtocolFeatures::MQ.bits() != 0 {
157                     vu.socket_handle()
158                         .get_queue_num()
159                         .map_err(Error::VhostUserGetQueueMaxNum)? as usize
160                 } else {
161                     DEFAULT_QUEUE_NUMBER
162                 };
163 
164             if num_queues > backend_num_queues {
165                 error!("vhost-user-net requested too many queues ({}) since the backend only supports {}\n",
166                 num_queues, backend_num_queues);
167                 return Err(Error::BadQueueNum);
168             }
169 
170             // If the control queue feature has been negotiated, let's increase
171             // the number of queues.
172             let vu_num_queues = num_queues;
173             if acked_features & (1 << VIRTIO_NET_F_CTRL_VQ) != 0 {
174                 num_queues += 1;
175             }
176 
177             // Make sure the virtio feature to set the MAC address is exposed to
178             // the guest, even if it hasn't been negotiated with the backend.
179             acked_features |= 1 << VIRTIO_NET_F_MAC;
180 
181             (
182                 acked_features,
183                 // If part of the available features that have been acked,
184                 // the PROTOCOL_FEATURES bit must be already set through
185                 // the VIRTIO acked features as we know the guest would
186                 // never ack it, thus the feature would be lost.
187                 acked_features & VhostUserVirtioFeatures::PROTOCOL_FEATURES.bits(),
188                 acked_protocol_features,
189                 vu_num_queues,
190                 config,
191                 false,
192             )
193         };
194 
195         Ok(Net {
196             id,
197             common: VirtioCommon {
198                 device_type: VirtioDeviceType::Net as u32,
199                 queue_sizes: vec![vu_cfg.queue_size; num_queues],
200                 avail_features,
201                 acked_features,
202                 paused_sync: Some(Arc::new(Barrier::new(2))),
203                 min_queues: DEFAULT_QUEUE_NUMBER as u16,
204                 paused: Arc::new(AtomicBool::new(paused)),
205                 ..Default::default()
206             },
207             vu_common: VhostUserCommon {
208                 vu: Some(Arc::new(Mutex::new(vu))),
209                 acked_protocol_features,
210                 socket_path: vu_cfg.socket,
211                 vu_num_queues,
212                 server,
213                 ..Default::default()
214             },
215             config,
216             guest_memory: None,
217             ctrl_queue_epoll_thread: None,
218             epoll_thread: None,
219             seccomp_action,
220             exit_evt,
221             iommu,
222         })
223     }
224 
225     fn state(&self) -> State {
226         State {
227             avail_features: self.common.avail_features,
228             acked_features: self.common.acked_features,
229             config: self.config,
230             acked_protocol_features: self.vu_common.acked_protocol_features,
231             vu_num_queues: self.vu_common.vu_num_queues,
232         }
233     }
234 }
235 
236 impl Drop for Net {
237     fn drop(&mut self) {
238         if let Some(kill_evt) = self.common.kill_evt.take() {
239             if let Err(e) = kill_evt.write(1) {
240                 error!("failed to kill vhost-user-net: {:?}", e);
241             }
242         }
243     }
244 }
245 
246 impl VirtioDevice for Net {
247     fn device_type(&self) -> u32 {
248         self.common.device_type
249     }
250 
251     fn queue_max_sizes(&self) -> &[u16] {
252         &self.common.queue_sizes
253     }
254 
255     fn features(&self) -> u64 {
256         let mut features = self.common.avail_features;
257         if self.iommu {
258             features |= 1u64 << VIRTIO_F_IOMMU_PLATFORM;
259         }
260         features
261     }
262 
263     fn ack_features(&mut self, value: u64) {
264         self.common.ack_features(value)
265     }
266 
267     fn read_config(&self, offset: u64, data: &mut [u8]) {
268         self.read_config_from_slice(self.config.as_slice(), offset, data);
269     }
270 
271     fn activate(
272         &mut self,
273         mem: GuestMemoryAtomic<GuestMemoryMmap>,
274         interrupt_cb: Arc<dyn VirtioInterrupt>,
275         mut queues: Vec<(usize, Queue, EventFd)>,
276     ) -> ActivateResult {
277         self.common.activate(&queues, &interrupt_cb)?;
278         self.guest_memory = Some(mem.clone());
279 
280         let num_queues = queues.len();
281         let event_idx = self.common.feature_acked(VIRTIO_RING_F_EVENT_IDX.into());
282         if self.common.feature_acked(VIRTIO_NET_F_CTRL_VQ.into()) && num_queues % 2 != 0 {
283             let ctrl_queue_index = num_queues - 1;
284             let (_, mut ctrl_queue, ctrl_queue_evt) = queues.remove(ctrl_queue_index);
285 
286             ctrl_queue.set_event_idx(event_idx);
287 
288             let (kill_evt, pause_evt) = self.common.dup_eventfds();
289 
290             let mut ctrl_handler = NetCtrlEpollHandler {
291                 mem: mem.clone(),
292                 kill_evt,
293                 pause_evt,
294                 ctrl_q: CtrlQueue::new(Vec::new()),
295                 queue: ctrl_queue,
296                 queue_evt: ctrl_queue_evt,
297                 access_platform: None,
298                 interrupt_cb: interrupt_cb.clone(),
299                 queue_index: ctrl_queue_index as u16,
300             };
301 
302             let paused = self.common.paused.clone();
303             // Let's update the barrier as we need 1 for the control queue
304             // thread + 1 for the common vhost-user thread + 1 for the main
305             // thread signalling the pause.
306             self.common.paused_sync = Some(Arc::new(Barrier::new(3)));
307             let paused_sync = self.common.paused_sync.clone();
308 
309             let mut epoll_threads = Vec::new();
310             spawn_virtio_thread(
311                 &format!("{}_ctrl", &self.id),
312                 &self.seccomp_action,
313                 Thread::VirtioVhostNetCtl,
314                 &mut epoll_threads,
315                 &self.exit_evt,
316                 move || ctrl_handler.run_ctrl(paused, paused_sync.unwrap()),
317             )?;
318             self.ctrl_queue_epoll_thread = Some(epoll_threads.remove(0));
319         }
320 
321         let slave_req_handler: Option<MasterReqHandler<SlaveReqHandler>> = None;
322 
323         // The backend acknowledged features must not contain VIRTIO_NET_F_MAC
324         // since we don't expect the backend to handle it.
325         let backend_acked_features = self.common.acked_features & !(1 << VIRTIO_NET_F_MAC);
326 
327         // Run a dedicated thread for handling potential reconnections with
328         // the backend.
329         let (kill_evt, pause_evt) = self.common.dup_eventfds();
330 
331         let mut handler = self.vu_common.activate(
332             mem,
333             queues,
334             interrupt_cb,
335             backend_acked_features,
336             slave_req_handler,
337             kill_evt,
338             pause_evt,
339         )?;
340 
341         let paused = self.common.paused.clone();
342         let paused_sync = self.common.paused_sync.clone();
343 
344         let mut epoll_threads = Vec::new();
345         spawn_virtio_thread(
346             &self.id,
347             &self.seccomp_action,
348             Thread::VirtioVhostNet,
349             &mut epoll_threads,
350             &self.exit_evt,
351             move || handler.run(paused, paused_sync.unwrap()),
352         )?;
353         self.epoll_thread = Some(epoll_threads.remove(0));
354 
355         Ok(())
356     }
357 
358     fn reset(&mut self) -> Option<Arc<dyn VirtioInterrupt>> {
359         // We first must resume the virtio thread if it was paused.
360         if self.common.pause_evt.take().is_some() {
361             self.common.resume().ok()?;
362         }
363 
364         if let Some(vu) = &self.vu_common.vu {
365             if let Err(e) = vu.lock().unwrap().reset_vhost_user() {
366                 error!("Failed to reset vhost-user daemon: {:?}", e);
367                 return None;
368             }
369         }
370 
371         if let Some(kill_evt) = self.common.kill_evt.take() {
372             // Ignore the result because there is nothing we can do about it.
373             let _ = kill_evt.write(1);
374         }
375 
376         event!("virtio-device", "reset", "id", &self.id);
377 
378         // Return the interrupt
379         Some(self.common.interrupt_cb.take().unwrap())
380     }
381 
382     fn shutdown(&mut self) {
383         self.vu_common.shutdown();
384     }
385 
386     fn add_memory_region(
387         &mut self,
388         region: &Arc<GuestRegionMmap>,
389     ) -> std::result::Result<(), crate::Error> {
390         self.vu_common.add_memory_region(&self.guest_memory, region)
391     }
392 }
393 
394 impl Pausable for Net {
395     fn pause(&mut self) -> result::Result<(), MigratableError> {
396         self.vu_common.pause()?;
397         self.common.pause()
398     }
399 
400     fn resume(&mut self) -> result::Result<(), MigratableError> {
401         self.common.resume()?;
402 
403         if let Some(epoll_thread) = &self.epoll_thread {
404             epoll_thread.thread().unpark();
405         }
406 
407         if let Some(ctrl_queue_epoll_thread) = &self.ctrl_queue_epoll_thread {
408             ctrl_queue_epoll_thread.thread().unpark();
409         }
410 
411         self.vu_common.resume()
412     }
413 }
414 
415 impl Snapshottable for Net {
416     fn id(&self) -> String {
417         self.id.clone()
418     }
419 
420     fn snapshot(&mut self) -> std::result::Result<Snapshot, MigratableError> {
421         self.vu_common.snapshot(&self.state())
422     }
423 }
424 impl Transportable for Net {}
425 
426 impl Migratable for Net {
427     fn start_dirty_log(&mut self) -> std::result::Result<(), MigratableError> {
428         self.vu_common.start_dirty_log(&self.guest_memory)
429     }
430 
431     fn stop_dirty_log(&mut self) -> std::result::Result<(), MigratableError> {
432         self.vu_common.stop_dirty_log()
433     }
434 
435     fn dirty_log(&mut self) -> std::result::Result<MemoryRangeTable, MigratableError> {
436         self.vu_common.dirty_log(&self.guest_memory)
437     }
438 
439     fn start_migration(&mut self) -> std::result::Result<(), MigratableError> {
440         self.vu_common.start_migration()
441     }
442 
443     fn complete_migration(&mut self) -> std::result::Result<(), MigratableError> {
444         self.vu_common
445             .complete_migration(self.common.kill_evt.take())
446     }
447 }
448