2019-08-28 09:50:48 +00:00
|
|
|
// Copyright 2019 Intel Corporation. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2021-09-03 10:43:30 +00:00
|
|
|
use crate::seccomp_filters::Thread;
|
|
|
|
use crate::thread_helper::spawn_virtio_thread;
|
2021-07-28 12:52:53 +00:00
|
|
|
use crate::vhost_user::vu_common_ctrl::{VhostUserConfig, VhostUserHandle};
|
2021-08-11 08:56:29 +00:00
|
|
|
use crate::vhost_user::{Error, Result, VhostUserCommon};
|
2021-06-01 15:16:09 +00:00
|
|
|
use crate::{
|
2022-03-14 16:29:04 +00:00
|
|
|
ActivateResult, NetCtrlEpollHandler, VirtioCommon, VirtioDevice, VirtioDeviceType,
|
2022-03-29 19:27:05 +00:00
|
|
|
VirtioInterrupt, VIRTIO_F_IOMMU_PLATFORM, VIRTIO_F_RING_EVENT_IDX, VIRTIO_F_VERSION_1,
|
2021-06-01 10:06:23 +00:00
|
|
|
};
|
2021-06-02 19:08:04 +00:00
|
|
|
use crate::{GuestMemoryMmap, GuestRegionMmap};
|
2021-05-25 15:14:53 +00:00
|
|
|
use net_util::{build_net_config_space, CtrlQueue, MacAddr, VirtioNetConfig};
|
2021-09-03 10:43:30 +00:00
|
|
|
use seccompiler::SeccompAction;
|
2019-12-02 20:08:53 +00:00
|
|
|
use std::result;
|
2021-06-01 10:06:23 +00:00
|
|
|
use std::sync::{Arc, Barrier, Mutex};
|
2021-05-25 15:14:53 +00:00
|
|
|
use std::thread;
|
2019-08-28 09:50:48 +00:00
|
|
|
use std::vec::Vec;
|
2021-07-26 12:40:55 +00:00
|
|
|
use versionize::{VersionMap, Versionize, VersionizeResult};
|
|
|
|
use versionize_derive::Versionize;
|
2021-02-25 09:46:27 +00:00
|
|
|
use vhost::vhost_user::message::{VhostUserProtocolFeatures, VhostUserVirtioFeatures};
|
2021-07-28 12:52:53 +00:00
|
|
|
use vhost::vhost_user::{MasterReqHandler, VhostUserMaster, VhostUserMasterReqHandler};
|
2021-05-21 16:26:00 +00:00
|
|
|
use virtio_bindings::bindings::virtio_net::{
|
|
|
|
VIRTIO_NET_F_CSUM, VIRTIO_NET_F_CTRL_VQ, VIRTIO_NET_F_GUEST_CSUM, VIRTIO_NET_F_GUEST_ECN,
|
|
|
|
VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, VIRTIO_NET_F_GUEST_UFO,
|
|
|
|
VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_TSO6, VIRTIO_NET_F_HOST_UFO,
|
2021-05-25 15:14:53 +00:00
|
|
|
VIRTIO_NET_F_MAC, VIRTIO_NET_F_MRG_RXBUF,
|
2021-05-21 16:26:00 +00:00
|
|
|
};
|
2022-03-14 16:29:04 +00:00
|
|
|
use virtio_bindings::bindings::virtio_ring::VIRTIO_RING_F_EVENT_IDX;
|
2022-07-06 14:08:08 +00:00
|
|
|
use virtio_queue::{Queue, QueueT};
|
2021-10-21 10:41:16 +00:00
|
|
|
use vm_memory::{ByteValued, GuestMemoryAtomic};
|
2021-07-26 12:40:55 +00:00
|
|
|
use vm_migration::{
|
2021-08-04 14:16:54 +00:00
|
|
|
protocol::MemoryRangeTable, Migratable, MigratableError, Pausable, Snapshot, Snapshottable,
|
|
|
|
Transportable, VersionMapped,
|
2021-07-26 12:40:55 +00:00
|
|
|
};
|
2019-12-31 10:49:11 +00:00
|
|
|
use vmm_sys_util::eventfd::EventFd;
|
2019-08-28 09:50:48 +00:00
|
|
|
|
2020-01-09 17:29:00 +00:00
|
|
|
const DEFAULT_QUEUE_NUMBER: usize = 2;
|
|
|
|
|
2021-07-26 12:40:55 +00:00
|
|
|
#[derive(Versionize)]
|
|
|
|
pub struct State {
|
|
|
|
pub avail_features: u64,
|
|
|
|
pub acked_features: u64,
|
|
|
|
pub config: VirtioNetConfig,
|
2021-08-09 12:02:30 +00:00
|
|
|
pub acked_protocol_features: u64,
|
|
|
|
pub vu_num_queues: usize,
|
2021-07-26 12:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl VersionMapped for State {}
|
|
|
|
|
2019-08-30 18:47:24 +00:00
|
|
|
struct SlaveReqHandler {}
|
|
|
|
impl VhostUserMasterReqHandler for SlaveReqHandler {}
|
|
|
|
|
2019-08-28 09:50:48 +00:00
|
|
|
pub struct Net {
|
2020-09-03 09:37:36 +00:00
|
|
|
common: VirtioCommon,
|
2021-08-11 08:56:29 +00:00
|
|
|
vu_common: VhostUserCommon,
|
2020-04-27 11:58:10 +00:00
|
|
|
id: String,
|
2020-01-27 16:37:14 +00:00
|
|
|
config: VirtioNetConfig,
|
2021-03-05 13:32:57 +00:00
|
|
|
guest_memory: Option<GuestMemoryAtomic<GuestMemoryMmap>>,
|
2021-05-25 15:14:53 +00:00
|
|
|
ctrl_queue_epoll_thread: Option<thread::JoinHandle<()>>,
|
2021-06-14 08:46:44 +00:00
|
|
|
epoll_thread: Option<thread::JoinHandle<()>>,
|
2021-05-25 15:14:53 +00:00
|
|
|
seccomp_action: SeccompAction,
|
2021-09-07 15:10:48 +00:00
|
|
|
exit_evt: EventFd,
|
2022-03-29 19:27:05 +00:00
|
|
|
iommu: bool,
|
2019-08-28 09:50:48 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 17:42:52 +00:00
|
|
|
impl Net {
|
2019-08-28 09:50:48 +00:00
|
|
|
/// Create a new vhost-user-net device
|
2022-03-29 19:27:05 +00:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2020-08-18 04:44:51 +00:00
|
|
|
pub fn new(
|
|
|
|
id: String,
|
|
|
|
mac_addr: MacAddr,
|
|
|
|
vu_cfg: VhostUserConfig,
|
2021-05-04 08:33:35 +00:00
|
|
|
server: bool,
|
2021-05-25 15:14:53 +00:00
|
|
|
seccomp_action: SeccompAction,
|
2021-08-09 12:02:30 +00:00
|
|
|
restoring: bool,
|
2021-09-07 15:10:48 +00:00
|
|
|
exit_evt: EventFd,
|
2022-03-29 19:27:05 +00:00
|
|
|
iommu: bool,
|
2020-08-18 04:44:51 +00:00
|
|
|
) -> Result<Net> {
|
2021-05-18 10:24:35 +00:00
|
|
|
let mut num_queues = vu_cfg.num_queues;
|
2019-08-28 09:50:48 +00:00
|
|
|
|
2021-08-09 12:02:30 +00:00
|
|
|
if restoring {
|
|
|
|
// We need 'queue_sizes' to report a number of queues that will be
|
|
|
|
// enough to handle all the potential queues. Including the control
|
|
|
|
// queue (with +1) will guarantee that. VirtioPciDevice::new() will
|
|
|
|
// create the actual queues based on this information.
|
|
|
|
return Ok(Net {
|
|
|
|
common: VirtioCommon {
|
|
|
|
device_type: VirtioDeviceType::Net as u32,
|
|
|
|
queue_sizes: vec![vu_cfg.queue_size; num_queues + 1],
|
|
|
|
paused_sync: Some(Arc::new(Barrier::new(2))),
|
|
|
|
min_queues: DEFAULT_QUEUE_NUMBER as u16,
|
|
|
|
..Default::default()
|
|
|
|
},
|
2021-08-11 08:56:29 +00:00
|
|
|
vu_common: VhostUserCommon {
|
|
|
|
socket_path: vu_cfg.socket,
|
|
|
|
vu_num_queues: num_queues,
|
|
|
|
server,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
id,
|
2021-08-09 12:02:30 +00:00
|
|
|
config: VirtioNetConfig::default(),
|
|
|
|
guest_memory: None,
|
|
|
|
ctrl_queue_epoll_thread: None,
|
|
|
|
epoll_thread: None,
|
|
|
|
seccomp_action,
|
2021-09-07 15:10:48 +00:00
|
|
|
exit_evt,
|
2022-03-29 19:27:05 +00:00
|
|
|
iommu,
|
2021-08-09 12:02:30 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-28 09:50:48 +00:00
|
|
|
// Filling device and vring features VMM supports.
|
2021-05-21 16:26:00 +00:00
|
|
|
let mut avail_features = 1 << VIRTIO_NET_F_CSUM
|
|
|
|
| 1 << VIRTIO_NET_F_GUEST_CSUM
|
|
|
|
| 1 << VIRTIO_NET_F_GUEST_TSO4
|
|
|
|
| 1 << VIRTIO_NET_F_GUEST_TSO6
|
|
|
|
| 1 << VIRTIO_NET_F_GUEST_ECN
|
|
|
|
| 1 << VIRTIO_NET_F_GUEST_UFO
|
|
|
|
| 1 << VIRTIO_NET_F_HOST_TSO4
|
|
|
|
| 1 << VIRTIO_NET_F_HOST_TSO6
|
|
|
|
| 1 << VIRTIO_NET_F_HOST_ECN
|
|
|
|
| 1 << VIRTIO_NET_F_HOST_UFO
|
|
|
|
| 1 << VIRTIO_NET_F_MRG_RXBUF
|
|
|
|
| 1 << VIRTIO_NET_F_CTRL_VQ
|
2021-05-25 15:14:53 +00:00
|
|
|
| 1 << VIRTIO_F_RING_EVENT_IDX
|
|
|
|
| 1 << VIRTIO_F_VERSION_1
|
|
|
|
| VhostUserVirtioFeatures::PROTOCOL_FEATURES.bits();
|
2019-08-28 09:50:48 +00:00
|
|
|
|
2021-05-18 10:24:35 +00:00
|
|
|
let mut config = VirtioNetConfig::default();
|
|
|
|
build_net_config_space(&mut config, mac_addr, num_queues, &mut avail_features);
|
|
|
|
|
2021-07-28 12:52:53 +00:00
|
|
|
let mut vu =
|
|
|
|
VhostUserHandle::connect_vhost_user(server, &vu_cfg.socket, num_queues as u64, false)?;
|
2021-05-18 10:24:35 +00:00
|
|
|
|
|
|
|
let avail_protocol_features = VhostUserProtocolFeatures::MQ
|
|
|
|
| VhostUserProtocolFeatures::CONFIGURE_MEM_SLOTS
|
2021-06-11 13:55:38 +00:00
|
|
|
| VhostUserProtocolFeatures::REPLY_ACK
|
2021-08-06 13:01:37 +00:00
|
|
|
| VhostUserProtocolFeatures::INFLIGHT_SHMFD
|
|
|
|
| VhostUserProtocolFeatures::LOG_SHMFD;
|
2021-05-19 20:55:43 +00:00
|
|
|
|
2021-07-28 12:52:53 +00:00
|
|
|
let (mut acked_features, acked_protocol_features) =
|
|
|
|
vu.negotiate_features_vhost_user(avail_features, avail_protocol_features)?;
|
2021-03-05 13:32:57 +00:00
|
|
|
|
2021-05-18 10:24:35 +00:00
|
|
|
let backend_num_queues =
|
2021-05-19 20:55:43 +00:00
|
|
|
if acked_protocol_features & VhostUserProtocolFeatures::MQ.bits() != 0 {
|
2021-07-28 12:52:53 +00:00
|
|
|
vu.socket_handle()
|
2021-05-18 10:24:35 +00:00
|
|
|
.get_queue_num()
|
|
|
|
.map_err(Error::VhostUserGetQueueMaxNum)? as usize
|
2020-01-09 17:29:00 +00:00
|
|
|
} else {
|
2021-05-18 10:24:35 +00:00
|
|
|
DEFAULT_QUEUE_NUMBER
|
2020-01-09 17:29:00 +00:00
|
|
|
};
|
2021-03-05 13:32:57 +00:00
|
|
|
|
2021-05-18 10:24:35 +00:00
|
|
|
if num_queues > backend_num_queues {
|
|
|
|
error!("vhost-user-net requested too many queues ({}) since the backend only supports {}\n",
|
|
|
|
num_queues, backend_num_queues);
|
2020-01-09 17:29:00 +00:00
|
|
|
return Err(Error::BadQueueNum);
|
|
|
|
}
|
|
|
|
|
2021-05-25 15:14:53 +00:00
|
|
|
// If the control queue feature has been negotiated, let's increase
|
|
|
|
// the number of queues.
|
2021-07-26 20:06:54 +00:00
|
|
|
let vu_num_queues = num_queues;
|
2021-05-25 15:14:53 +00:00
|
|
|
if acked_features & (1 << VIRTIO_NET_F_CTRL_VQ) != 0 {
|
|
|
|
num_queues += 1;
|
2019-08-28 09:50:48 +00:00
|
|
|
}
|
|
|
|
|
2021-05-25 15:14:53 +00:00
|
|
|
// Make sure the virtio feature to set the MAC address is exposed to
|
|
|
|
// the guest, even if it hasn't been negotiated with the backend.
|
|
|
|
acked_features |= 1 << VIRTIO_NET_F_MAC;
|
|
|
|
|
2019-08-28 09:50:48 +00:00
|
|
|
Ok(Net {
|
2020-04-27 11:58:10 +00:00
|
|
|
id,
|
2020-09-03 09:37:36 +00:00
|
|
|
common: VirtioCommon {
|
2021-03-25 16:54:09 +00:00
|
|
|
device_type: VirtioDeviceType::Net as u32,
|
2021-05-18 10:24:35 +00:00
|
|
|
queue_sizes: vec![vu_cfg.queue_size; num_queues],
|
2021-05-19 20:55:43 +00:00
|
|
|
avail_features: acked_features,
|
2022-02-21 08:52:25 +00:00
|
|
|
// If part of the available features that have been acked, the
|
|
|
|
// PROTOCOL_FEATURES bit must be already set through the VIRTIO
|
|
|
|
// acked features as we know the guest would never ack it, thus
|
|
|
|
// the feature would be lost.
|
|
|
|
acked_features: acked_features & VhostUserVirtioFeatures::PROTOCOL_FEATURES.bits(),
|
2021-06-01 10:06:23 +00:00
|
|
|
paused_sync: Some(Arc::new(Barrier::new(2))),
|
2021-05-18 10:24:35 +00:00
|
|
|
min_queues: DEFAULT_QUEUE_NUMBER as u16,
|
2020-09-03 15:56:32 +00:00
|
|
|
..Default::default()
|
2020-09-03 09:37:36 +00:00
|
|
|
},
|
2021-08-11 08:56:29 +00:00
|
|
|
vu_common: VhostUserCommon {
|
|
|
|
vu: Some(Arc::new(Mutex::new(vu))),
|
|
|
|
acked_protocol_features,
|
|
|
|
socket_path: vu_cfg.socket,
|
|
|
|
vu_num_queues,
|
|
|
|
server,
|
|
|
|
..Default::default()
|
|
|
|
},
|
2020-01-27 16:37:14 +00:00
|
|
|
config,
|
2021-03-05 13:32:57 +00:00
|
|
|
guest_memory: None,
|
2021-05-25 15:14:53 +00:00
|
|
|
ctrl_queue_epoll_thread: None,
|
2021-06-14 08:46:44 +00:00
|
|
|
epoll_thread: None,
|
2021-05-25 15:14:53 +00:00
|
|
|
seccomp_action,
|
2021-09-07 15:10:48 +00:00
|
|
|
exit_evt,
|
2022-03-29 19:27:05 +00:00
|
|
|
iommu,
|
2019-08-28 09:50:48 +00:00
|
|
|
})
|
|
|
|
}
|
2021-07-26 12:40:55 +00:00
|
|
|
|
|
|
|
fn state(&self) -> State {
|
|
|
|
State {
|
|
|
|
avail_features: self.common.avail_features,
|
|
|
|
acked_features: self.common.acked_features,
|
|
|
|
config: self.config,
|
2021-08-11 08:56:29 +00:00
|
|
|
acked_protocol_features: self.vu_common.acked_protocol_features,
|
|
|
|
vu_num_queues: self.vu_common.vu_num_queues,
|
2021-07-26 12:40:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_state(&mut self, state: &State) {
|
|
|
|
self.common.avail_features = state.avail_features;
|
|
|
|
self.common.acked_features = state.acked_features;
|
|
|
|
self.config = state.config;
|
2021-08-11 08:56:29 +00:00
|
|
|
self.vu_common.acked_protocol_features = state.acked_protocol_features;
|
|
|
|
self.vu_common.vu_num_queues = state.vu_num_queues;
|
|
|
|
|
2022-02-21 08:52:25 +00:00
|
|
|
// The backend acknowledged features must not contain VIRTIO_NET_F_MAC
|
|
|
|
// since we don't expect the backend to handle it.
|
|
|
|
let backend_acked_features = self.common.acked_features & !(1 << VIRTIO_NET_F_MAC);
|
|
|
|
|
2021-08-11 08:56:29 +00:00
|
|
|
if let Err(e) = self
|
|
|
|
.vu_common
|
2022-02-21 08:52:25 +00:00
|
|
|
.restore_backend_connection(backend_acked_features)
|
2021-08-11 08:56:29 +00:00
|
|
|
{
|
|
|
|
error!(
|
|
|
|
"Failed restoring connection with vhost-user backend: {:?}",
|
|
|
|
e
|
|
|
|
);
|
2021-08-09 12:02:30 +00:00
|
|
|
}
|
2021-07-26 12:40:55 +00:00
|
|
|
}
|
2019-08-28 09:50:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Net {
|
|
|
|
fn drop(&mut self) {
|
2020-09-04 08:37:37 +00:00
|
|
|
if let Some(kill_evt) = self.common.kill_evt.take() {
|
2019-10-04 16:56:35 +00:00
|
|
|
if let Err(e) = kill_evt.write(1) {
|
|
|
|
error!("failed to kill vhost-user-net: {:?}", e);
|
|
|
|
}
|
2019-08-28 09:50:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl VirtioDevice for Net {
|
|
|
|
fn device_type(&self) -> u32 {
|
2020-09-04 08:37:37 +00:00
|
|
|
self.common.device_type
|
2019-08-28 09:50:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn queue_max_sizes(&self) -> &[u16] {
|
2020-09-04 08:37:37 +00:00
|
|
|
&self.common.queue_sizes
|
2019-08-28 09:50:48 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 10:14:38 +00:00
|
|
|
fn features(&self) -> u64 {
|
2022-03-29 19:27:05 +00:00
|
|
|
let mut features = self.common.avail_features;
|
|
|
|
if self.iommu {
|
|
|
|
features |= 1u64 << VIRTIO_F_IOMMU_PLATFORM;
|
|
|
|
}
|
|
|
|
features
|
2019-08-28 09:50:48 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 10:14:38 +00:00
|
|
|
fn ack_features(&mut self, value: u64) {
|
2020-09-03 09:37:36 +00:00
|
|
|
self.common.ack_features(value)
|
2019-08-28 09:50:48 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 09:34:51 +00:00
|
|
|
fn read_config(&self, offset: u64, data: &mut [u8]) {
|
|
|
|
self.read_config_from_slice(self.config.as_slice(), offset, data);
|
2019-08-28 09:50:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn activate(
|
|
|
|
&mut self,
|
2020-02-11 16:22:40 +00:00
|
|
|
mem: GuestMemoryAtomic<GuestMemoryMmap>,
|
2020-01-13 17:52:19 +00:00
|
|
|
interrupt_cb: Arc<dyn VirtioInterrupt>,
|
2022-07-06 14:08:08 +00:00
|
|
|
mut queues: Vec<(usize, Queue, EventFd)>,
|
2019-08-28 09:50:48 +00:00
|
|
|
) -> ActivateResult {
|
2022-07-20 14:45:49 +00:00
|
|
|
self.common.activate(&queues, &interrupt_cb)?;
|
2021-03-05 13:32:57 +00:00
|
|
|
self.guest_memory = Some(mem.clone());
|
|
|
|
|
2021-05-25 15:14:53 +00:00
|
|
|
let num_queues = queues.len();
|
2022-03-14 16:29:04 +00:00
|
|
|
let event_idx = self.common.feature_acked(VIRTIO_RING_F_EVENT_IDX.into());
|
2021-05-25 15:14:53 +00:00
|
|
|
if self.common.feature_acked(VIRTIO_NET_F_CTRL_VQ.into()) && num_queues % 2 != 0 {
|
2022-03-14 16:29:04 +00:00
|
|
|
let ctrl_queue_index = num_queues - 1;
|
2022-07-20 14:45:49 +00:00
|
|
|
let (_, mut ctrl_queue, ctrl_queue_evt) = queues.remove(ctrl_queue_index);
|
2022-03-14 16:29:04 +00:00
|
|
|
|
|
|
|
ctrl_queue.set_event_idx(event_idx);
|
2021-05-25 15:14:53 +00:00
|
|
|
|
2021-06-02 18:08:06 +00:00
|
|
|
let (kill_evt, pause_evt) = self.common.dup_eventfds();
|
2021-05-25 15:14:53 +00:00
|
|
|
|
|
|
|
let mut ctrl_handler = NetCtrlEpollHandler {
|
2022-07-06 14:08:08 +00:00
|
|
|
mem: mem.clone(),
|
2021-05-25 15:14:53 +00:00
|
|
|
kill_evt,
|
|
|
|
pause_evt,
|
|
|
|
ctrl_q: CtrlQueue::new(Vec::new()),
|
2022-03-14 16:29:04 +00:00
|
|
|
queue: ctrl_queue,
|
|
|
|
queue_evt: ctrl_queue_evt,
|
|
|
|
access_platform: None,
|
|
|
|
interrupt_cb: interrupt_cb.clone(),
|
|
|
|
queue_index: ctrl_queue_index as u16,
|
2021-05-25 15:14:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let paused = self.common.paused.clone();
|
2021-06-14 08:46:44 +00:00
|
|
|
// Let's update the barrier as we need 1 for the control queue
|
|
|
|
// thread + 1 for the common vhost-user thread + 1 for the main
|
|
|
|
// thread signalling the pause.
|
2021-06-01 10:06:23 +00:00
|
|
|
self.common.paused_sync = Some(Arc::new(Barrier::new(3)));
|
2021-05-25 15:14:53 +00:00
|
|
|
let paused_sync = self.common.paused_sync.clone();
|
|
|
|
|
2021-09-03 10:43:30 +00:00
|
|
|
let mut epoll_threads = Vec::new();
|
|
|
|
spawn_virtio_thread(
|
|
|
|
&format!("{}_ctrl", &self.id),
|
|
|
|
&self.seccomp_action,
|
|
|
|
Thread::VirtioVhostNetCtl,
|
|
|
|
&mut epoll_threads,
|
2021-09-07 15:10:48 +00:00
|
|
|
&self.exit_evt,
|
2021-09-03 10:43:30 +00:00
|
|
|
move || {
|
2021-08-17 00:20:11 +00:00
|
|
|
if let Err(e) = ctrl_handler.run_ctrl(paused, paused_sync.unwrap()) {
|
2021-05-25 15:14:53 +00:00
|
|
|
error!("Error running worker: {:?}", e);
|
|
|
|
}
|
2021-09-03 10:43:30 +00:00
|
|
|
},
|
|
|
|
)?;
|
|
|
|
self.ctrl_queue_epoll_thread = Some(epoll_threads.remove(0));
|
2021-05-25 15:14:53 +00:00
|
|
|
}
|
|
|
|
|
2021-06-09 15:27:14 +00:00
|
|
|
let slave_req_handler: Option<MasterReqHandler<SlaveReqHandler>> = None;
|
|
|
|
|
2022-02-21 08:52:25 +00:00
|
|
|
// The backend acknowledged features must not contain VIRTIO_NET_F_MAC
|
|
|
|
// since we don't expect the backend to handle it.
|
|
|
|
let backend_acked_features = self.common.acked_features & !(1 << VIRTIO_NET_F_MAC);
|
2021-05-25 15:14:53 +00:00
|
|
|
|
2021-06-01 10:06:23 +00:00
|
|
|
// Run a dedicated thread for handling potential reconnections with
|
|
|
|
// the backend.
|
2021-06-02 18:08:06 +00:00
|
|
|
let (kill_evt, pause_evt) = self.common.dup_eventfds();
|
2021-06-01 10:06:23 +00:00
|
|
|
|
2021-08-11 08:56:29 +00:00
|
|
|
let mut handler = self.vu_common.activate(
|
2021-06-01 10:06:23 +00:00
|
|
|
mem,
|
|
|
|
queues,
|
2021-08-11 08:56:29 +00:00
|
|
|
interrupt_cb,
|
|
|
|
backend_acked_features,
|
|
|
|
slave_req_handler,
|
|
|
|
kill_evt,
|
|
|
|
pause_evt,
|
|
|
|
)?;
|
2021-06-01 10:06:23 +00:00
|
|
|
|
|
|
|
let paused = self.common.paused.clone();
|
|
|
|
let paused_sync = self.common.paused_sync.clone();
|
|
|
|
|
2021-09-03 10:43:30 +00:00
|
|
|
let mut epoll_threads = Vec::new();
|
|
|
|
spawn_virtio_thread(
|
|
|
|
&self.id,
|
|
|
|
&self.seccomp_action,
|
|
|
|
Thread::VirtioVhostNet,
|
|
|
|
&mut epoll_threads,
|
2021-09-07 15:10:48 +00:00
|
|
|
&self.exit_evt,
|
2021-09-03 10:43:30 +00:00
|
|
|
move || {
|
2021-06-14 08:46:44 +00:00
|
|
|
if let Err(e) = handler.run(paused, paused_sync.unwrap()) {
|
2021-09-03 10:43:30 +00:00
|
|
|
error!("Error running worker: {:?}", e);
|
2021-06-01 10:06:23 +00:00
|
|
|
}
|
2021-09-03 10:43:30 +00:00
|
|
|
},
|
|
|
|
)?;
|
|
|
|
self.epoll_thread = Some(epoll_threads.remove(0));
|
2021-06-01 10:06:23 +00:00
|
|
|
|
2019-08-28 09:50:48 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2019-10-04 16:56:35 +00:00
|
|
|
|
2021-01-18 12:38:08 +00:00
|
|
|
fn reset(&mut self) -> Option<Arc<dyn VirtioInterrupt>> {
|
2019-12-02 20:08:53 +00:00
|
|
|
// We first must resume the virtio thread if it was paused.
|
2020-09-04 08:37:37 +00:00
|
|
|
if self.common.pause_evt.take().is_some() {
|
|
|
|
self.common.resume().ok()?;
|
2019-12-02 20:08:53 +00:00
|
|
|
}
|
|
|
|
|
2021-08-11 08:56:29 +00:00
|
|
|
if let Some(vu) = &self.vu_common.vu {
|
2022-07-20 15:19:45 +00:00
|
|
|
if let Err(e) = vu.lock().unwrap().reset_vhost_user() {
|
2021-08-06 15:43:53 +00:00
|
|
|
error!("Failed to reset vhost-user daemon: {:?}", e);
|
|
|
|
return None;
|
|
|
|
}
|
2019-10-04 16:56:35 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 08:37:37 +00:00
|
|
|
if let Some(kill_evt) = self.common.kill_evt.take() {
|
2019-10-04 16:56:35 +00:00
|
|
|
// Ignore the result because there is nothing we can do about it.
|
|
|
|
let _ = kill_evt.write(1);
|
|
|
|
}
|
|
|
|
|
2021-02-18 15:10:51 +00:00
|
|
|
event!("virtio-device", "reset", "id", &self.id);
|
|
|
|
|
2021-01-18 12:38:08 +00:00
|
|
|
// Return the interrupt
|
|
|
|
Some(self.common.interrupt_cb.take().unwrap())
|
2019-10-04 16:56:35 +00:00
|
|
|
}
|
2020-02-14 10:08:59 +00:00
|
|
|
|
|
|
|
fn shutdown(&mut self) {
|
2021-08-11 08:56:29 +00:00
|
|
|
self.vu_common.shutdown();
|
2020-02-14 10:08:59 +00:00
|
|
|
}
|
2020-03-23 12:47:55 +00:00
|
|
|
|
2021-03-05 13:32:57 +00:00
|
|
|
fn add_memory_region(
|
|
|
|
&mut self,
|
|
|
|
region: &Arc<GuestRegionMmap>,
|
|
|
|
) -> std::result::Result<(), crate::Error> {
|
2021-08-11 08:56:29 +00:00
|
|
|
self.vu_common.add_memory_region(&self.guest_memory, region)
|
2021-03-05 13:32:57 +00:00
|
|
|
}
|
2019-08-28 09:50:48 +00:00
|
|
|
}
|
2019-12-02 20:08:53 +00:00
|
|
|
|
2020-09-04 08:37:37 +00:00
|
|
|
impl Pausable for Net {
|
|
|
|
fn pause(&mut self) -> result::Result<(), MigratableError> {
|
2021-08-11 08:56:29 +00:00
|
|
|
self.vu_common.pause()?;
|
2020-09-04 08:37:37 +00:00
|
|
|
self.common.pause()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resume(&mut self) -> result::Result<(), MigratableError> {
|
2021-05-25 15:14:53 +00:00
|
|
|
self.common.resume()?;
|
|
|
|
|
2021-06-14 08:46:44 +00:00
|
|
|
if let Some(epoll_thread) = &self.epoll_thread {
|
|
|
|
epoll_thread.thread().unpark();
|
2021-06-01 10:06:23 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 20:06:54 +00:00
|
|
|
if let Some(ctrl_queue_epoll_thread) = &self.ctrl_queue_epoll_thread {
|
|
|
|
ctrl_queue_epoll_thread.thread().unpark();
|
|
|
|
}
|
|
|
|
|
2021-08-11 08:56:29 +00:00
|
|
|
self.vu_common.resume()
|
2020-09-04 08:37:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-27 11:58:10 +00:00
|
|
|
impl Snapshottable for Net {
|
|
|
|
fn id(&self) -> String {
|
|
|
|
self.id.clone()
|
|
|
|
}
|
2021-07-26 12:40:55 +00:00
|
|
|
|
|
|
|
fn snapshot(&mut self) -> std::result::Result<Snapshot, MigratableError> {
|
2021-08-11 08:56:29 +00:00
|
|
|
self.vu_common.snapshot(&self.id(), &self.state())
|
2021-07-26 12:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn restore(&mut self, snapshot: Snapshot) -> std::result::Result<(), MigratableError> {
|
|
|
|
self.set_state(&snapshot.to_versioned_state(&self.id)?);
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-04-27 11:58:10 +00:00
|
|
|
}
|
2019-05-01 16:59:51 +00:00
|
|
|
impl Transportable for Net {}
|
2021-08-04 14:16:54 +00:00
|
|
|
|
|
|
|
impl Migratable for Net {
|
|
|
|
fn start_dirty_log(&mut self) -> std::result::Result<(), MigratableError> {
|
2021-08-11 08:56:29 +00:00
|
|
|
self.vu_common.start_dirty_log(&self.guest_memory)
|
2021-08-04 14:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn stop_dirty_log(&mut self) -> std::result::Result<(), MigratableError> {
|
2021-08-11 08:56:29 +00:00
|
|
|
self.vu_common.stop_dirty_log()
|
2021-08-04 14:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn dirty_log(&mut self) -> std::result::Result<MemoryRangeTable, MigratableError> {
|
2021-08-11 08:56:29 +00:00
|
|
|
self.vu_common.dirty_log(&self.guest_memory)
|
2021-08-04 14:16:54 +00:00
|
|
|
}
|
2021-08-09 09:05:20 +00:00
|
|
|
|
2022-01-29 02:53:58 +00:00
|
|
|
fn start_migration(&mut self) -> std::result::Result<(), MigratableError> {
|
|
|
|
self.vu_common.start_migration()
|
|
|
|
}
|
|
|
|
|
2021-08-09 09:05:20 +00:00
|
|
|
fn complete_migration(&mut self) -> std::result::Result<(), MigratableError> {
|
2021-08-11 08:56:29 +00:00
|
|
|
self.vu_common
|
|
|
|
.complete_migration(self.common.kill_evt.take())
|
2021-08-09 09:05:20 +00:00
|
|
|
}
|
2021-08-04 14:16:54 +00:00
|
|
|
}
|