2019-05-08 00:26:37 +00:00
|
|
|
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//
|
|
|
|
// Portions Copyright 2017 The Chromium OS Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the THIRD-PARTY file.
|
|
|
|
|
2020-01-09 11:56:21 +00:00
|
|
|
use super::net_util::{
|
2020-07-07 15:06:54 +00:00
|
|
|
build_net_config_space, build_net_config_space_with_mq, CtrlVirtio, NetCtrlEpollHandler,
|
2020-07-23 16:16:10 +00:00
|
|
|
VirtioNetConfig,
|
2020-01-09 11:56:21 +00:00
|
|
|
};
|
2019-12-31 10:49:11 +00:00
|
|
|
use super::Error as DeviceError;
|
|
|
|
use super::{
|
2020-07-23 16:16:10 +00:00
|
|
|
ActivateError, ActivateResult, EpollHelper, EpollHelperError, EpollHelperHandler, Queue,
|
|
|
|
VirtioDevice, VirtioDeviceType, VirtioInterruptType, EPOLL_HELPER_EVENT_LAST,
|
2019-12-31 10:49:11 +00:00
|
|
|
};
|
2020-08-04 18:27:17 +00:00
|
|
|
use crate::seccomp_filters::{get_seccomp_filter, Thread};
|
2019-12-31 10:49:11 +00:00
|
|
|
use crate::VirtioInterrupt;
|
2020-04-21 15:28:59 +00:00
|
|
|
use anyhow::anyhow;
|
2019-05-08 00:26:37 +00:00
|
|
|
use libc::EFD_NONBLOCK;
|
2020-07-07 15:06:54 +00:00
|
|
|
use net_util::{
|
2020-07-07 15:50:13 +00:00
|
|
|
open_tap, MacAddr, NetCounters, NetQueuePair, OpenTapError, RxVirtio, Tap, TxVirtio,
|
2020-07-07 15:06:54 +00:00
|
|
|
};
|
2020-08-04 18:27:17 +00:00
|
|
|
use seccomp::{SeccompAction, SeccompFilter};
|
2020-06-24 09:50:04 +00:00
|
|
|
use std::collections::HashMap;
|
2019-05-08 00:26:37 +00:00
|
|
|
use std::net::Ipv4Addr;
|
2020-06-23 15:28:41 +00:00
|
|
|
use std::num::Wrapping;
|
2020-07-23 16:16:10 +00:00
|
|
|
use std::os::unix::io::AsRawFd;
|
2019-05-08 00:26:37 +00:00
|
|
|
use std::result;
|
2020-07-07 15:50:13 +00:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
2019-12-31 10:49:11 +00:00
|
|
|
use std::sync::Arc;
|
2019-05-08 00:26:37 +00:00
|
|
|
use std::thread;
|
|
|
|
use std::vec::Vec;
|
2019-09-19 13:42:29 +00:00
|
|
|
use virtio_bindings::bindings::virtio_net::*;
|
2020-06-01 12:08:53 +00:00
|
|
|
use virtio_bindings::bindings::virtio_ring::VIRTIO_RING_F_EVENT_IDX;
|
2020-02-11 16:22:40 +00:00
|
|
|
use vm_memory::{ByteValued, GuestAddressSpace, GuestMemoryAtomic, GuestMemoryMmap};
|
2020-04-21 15:28:59 +00:00
|
|
|
use vm_migration::{
|
|
|
|
Migratable, MigratableError, Pausable, Snapshot, SnapshotDataSection, Snapshottable,
|
|
|
|
Transportable,
|
|
|
|
};
|
2019-08-02 14:23:52 +00:00
|
|
|
use vmm_sys_util::eventfd::EventFd;
|
2019-05-08 00:26:37 +00:00
|
|
|
|
2020-07-23 16:16:10 +00:00
|
|
|
// The guest has made a buffer available to receive a frame into.
|
|
|
|
pub const RX_QUEUE_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 1;
|
|
|
|
// The transmit queue has a frame that is ready to send from the guest.
|
|
|
|
pub const TX_QUEUE_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 2;
|
|
|
|
// A frame is available for reading from the tap device to receive in the guest.
|
|
|
|
pub const RX_TAP_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 3;
|
|
|
|
|
2019-05-08 00:26:37 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
2020-01-09 11:56:21 +00:00
|
|
|
/// Failed to open taps.
|
2020-07-07 14:02:18 +00:00
|
|
|
OpenTap(OpenTapError),
|
2019-05-08 00:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub type Result<T> = result::Result<T, Error>;
|
|
|
|
|
2020-05-29 13:50:11 +00:00
|
|
|
struct NetEpollHandler {
|
|
|
|
net: NetQueuePair,
|
|
|
|
interrupt_cb: Arc<dyn VirtioInterrupt>,
|
|
|
|
kill_evt: EventFd,
|
|
|
|
pause_evt: EventFd,
|
2020-07-23 16:16:10 +00:00
|
|
|
queue_pair: Vec<Queue>,
|
|
|
|
queue_evt_pair: Vec<EventFd>,
|
2020-06-02 16:00:31 +00:00
|
|
|
// Always generate interrupts until the driver has signalled to the device.
|
|
|
|
// This mitigates a problem with interrupts from tap events being "lost" upon
|
|
|
|
// a restore as the vCPU thread isn't ready to handle the interrupt. This causes
|
|
|
|
// issues when combined with VIRTIO_RING_F_EVENT_IDX interrupt suppression.
|
|
|
|
driver_awake: bool,
|
2020-05-29 13:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl NetEpollHandler {
|
|
|
|
fn signal_used_queue(&self, queue: &Queue) -> result::Result<(), DeviceError> {
|
|
|
|
self.interrupt_cb
|
|
|
|
.trigger(&VirtioInterruptType::Queue, Some(queue))
|
|
|
|
.map_err(|e| {
|
|
|
|
error!("Failed to signal used queue: {:?}", e);
|
|
|
|
DeviceError::FailedSignalingUsedQueue(e)
|
|
|
|
})
|
|
|
|
}
|
2019-05-08 00:26:37 +00:00
|
|
|
|
2020-07-23 16:16:10 +00:00
|
|
|
fn handle_rx_event(&mut self) -> result::Result<(), DeviceError> {
|
|
|
|
let queue_evt = &self.queue_evt_pair[0];
|
2020-01-09 11:56:21 +00:00
|
|
|
if let Err(e) = queue_evt.read() {
|
|
|
|
error!("Failed to get rx queue event: {:?}", e);
|
|
|
|
}
|
|
|
|
|
2020-07-07 15:50:13 +00:00
|
|
|
if self
|
|
|
|
.net
|
2020-07-23 16:16:10 +00:00
|
|
|
.resume_rx(&mut self.queue_pair[0])
|
2020-07-07 15:50:13 +00:00
|
|
|
.map_err(DeviceError::NetQueuePair)?
|
|
|
|
|| !self.driver_awake
|
|
|
|
{
|
2020-07-23 16:16:10 +00:00
|
|
|
self.signal_used_queue(&self.queue_pair[0])?;
|
2020-06-02 15:59:22 +00:00
|
|
|
info!("Signalling RX queue");
|
|
|
|
} else {
|
|
|
|
info!("Not signalling RX queue");
|
2020-01-09 11:56:21 +00:00
|
|
|
}
|
2020-05-29 12:33:59 +00:00
|
|
|
|
|
|
|
Ok(())
|
2019-08-21 12:44:49 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 16:16:10 +00:00
|
|
|
fn handle_tx_event(&mut self) -> result::Result<(), DeviceError> {
|
|
|
|
let queue_evt = &self.queue_evt_pair[1];
|
2020-01-09 11:56:21 +00:00
|
|
|
if let Err(e) = queue_evt.read() {
|
|
|
|
error!("Failed to get tx queue event: {:?}", e);
|
|
|
|
}
|
2020-07-07 15:50:13 +00:00
|
|
|
if self
|
|
|
|
.net
|
2020-07-23 16:16:10 +00:00
|
|
|
.process_tx(&mut self.queue_pair[1])
|
2020-07-07 15:50:13 +00:00
|
|
|
.map_err(DeviceError::NetQueuePair)?
|
|
|
|
|| !self.driver_awake
|
|
|
|
{
|
2020-07-23 16:16:10 +00:00
|
|
|
self.signal_used_queue(&self.queue_pair[1])?;
|
2020-06-02 15:59:22 +00:00
|
|
|
info!("Signalling TX queue");
|
|
|
|
} else {
|
|
|
|
info!("Not signalling TX queue");
|
2020-05-29 12:33:59 +00:00
|
|
|
}
|
2020-05-29 13:50:11 +00:00
|
|
|
Ok(())
|
2020-01-09 11:56:21 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 16:16:10 +00:00
|
|
|
fn handle_rx_tap_event(&mut self) -> result::Result<(), DeviceError> {
|
2020-07-07 15:50:13 +00:00
|
|
|
if self
|
|
|
|
.net
|
2020-07-23 16:16:10 +00:00
|
|
|
.process_rx_tap(&mut self.queue_pair[0])
|
2020-07-07 15:50:13 +00:00
|
|
|
.map_err(DeviceError::NetQueuePair)?
|
|
|
|
|| !self.driver_awake
|
|
|
|
{
|
2020-07-23 16:16:10 +00:00
|
|
|
self.signal_used_queue(&self.queue_pair[0])?;
|
2020-06-02 15:59:22 +00:00
|
|
|
info!("Signalling RX queue");
|
|
|
|
} else {
|
|
|
|
info!("Not signalling RX queue");
|
2020-01-09 11:56:21 +00:00
|
|
|
}
|
2020-05-29 12:33:59 +00:00
|
|
|
Ok(())
|
2019-08-21 12:44:49 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 16:16:10 +00:00
|
|
|
fn run(&mut self, paused: Arc<AtomicBool>) -> result::Result<(), EpollHelperError> {
|
|
|
|
let mut helper = EpollHelper::new(&self.kill_evt, &self.pause_evt)?;
|
|
|
|
helper.add_event(self.queue_evt_pair[0].as_raw_fd(), RX_QUEUE_EVENT)?;
|
|
|
|
helper.add_event(self.queue_evt_pair[1].as_raw_fd(), TX_QUEUE_EVENT)?;
|
2019-05-08 00:26:37 +00:00
|
|
|
|
2020-04-21 15:28:59 +00:00
|
|
|
// If there are some already available descriptors on the RX queue,
|
|
|
|
// then we can start the thread while listening onto the TAP.
|
2020-07-23 16:16:10 +00:00
|
|
|
if self.queue_pair[0]
|
2020-05-29 14:13:31 +00:00
|
|
|
.available_descriptors(&self.net.mem.as_ref().unwrap().memory())
|
2020-05-29 13:50:11 +00:00
|
|
|
.unwrap()
|
|
|
|
{
|
2020-07-23 16:16:10 +00:00
|
|
|
helper.add_event(self.net.tap.as_raw_fd(), RX_TAP_EVENT)?;
|
2020-05-29 13:50:11 +00:00
|
|
|
self.net.rx_tap_listening = true;
|
2020-07-23 16:16:10 +00:00
|
|
|
info!("Listener registered at start");
|
2020-04-21 15:28:59 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 16:16:10 +00:00
|
|
|
// The NetQueuePair needs the epoll fd.
|
|
|
|
self.net.epoll_fd = Some(helper.as_raw_fd());
|
2019-05-08 00:26:37 +00:00
|
|
|
|
2020-07-23 16:16:10 +00:00
|
|
|
helper.run(paused, self)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2020-06-22 14:00:02 +00:00
|
|
|
|
2020-07-23 16:16:10 +00:00
|
|
|
impl EpollHelperHandler for NetEpollHandler {
|
|
|
|
fn handle_event(&mut self, _helper: &mut EpollHelper, event: u16) -> bool {
|
|
|
|
match event {
|
|
|
|
RX_QUEUE_EVENT => {
|
|
|
|
self.driver_awake = true;
|
|
|
|
if let Err(e) = self.handle_rx_event() {
|
|
|
|
error!("Error processing RX queue: {:?}", e);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TX_QUEUE_EVENT => {
|
|
|
|
self.driver_awake = true;
|
|
|
|
if let Err(e) = self.handle_tx_event() {
|
|
|
|
error!("Error processing TX queue: {:?}", e);
|
|
|
|
return true;
|
2019-08-01 20:08:47 +00:00
|
|
|
}
|
2020-07-23 16:16:10 +00:00
|
|
|
}
|
|
|
|
RX_TAP_EVENT => {
|
|
|
|
if let Err(e) = self.handle_rx_tap_event() {
|
|
|
|
error!("Error processing tap queue: {:?}", e);
|
|
|
|
return true;
|
2019-05-08 00:26:37 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-23 16:16:10 +00:00
|
|
|
_ => {
|
|
|
|
error!("Unknown event: {}", event);
|
|
|
|
return true;
|
|
|
|
}
|
2019-05-08 00:26:37 +00:00
|
|
|
}
|
2020-07-23 16:16:10 +00:00
|
|
|
false
|
2019-05-08 00:26:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Net {
|
2020-04-27 09:29:16 +00:00
|
|
|
id: String,
|
2019-05-08 00:26:37 +00:00
|
|
|
kill_evt: Option<EventFd>,
|
2019-11-19 00:42:31 +00:00
|
|
|
pause_evt: Option<EventFd>,
|
2020-01-09 17:29:00 +00:00
|
|
|
taps: Option<Vec<Tap>>,
|
2019-05-08 00:26:37 +00:00
|
|
|
avail_features: u64,
|
|
|
|
acked_features: u64,
|
2020-01-27 16:37:14 +00:00
|
|
|
config: VirtioNetConfig,
|
2019-10-02 18:21:34 +00:00
|
|
|
queue_evts: Option<Vec<EventFd>>,
|
2020-01-13 17:52:19 +00:00
|
|
|
interrupt_cb: Option<Arc<dyn VirtioInterrupt>>,
|
2020-07-23 16:16:10 +00:00
|
|
|
epoll_threads: Option<Vec<thread::JoinHandle<result::Result<(), EpollHelperError>>>>,
|
2020-01-15 09:32:05 +00:00
|
|
|
ctrl_queue_epoll_thread: Option<thread::JoinHandle<result::Result<(), DeviceError>>>,
|
2019-11-19 00:42:31 +00:00
|
|
|
paused: Arc<AtomicBool>,
|
2020-01-09 17:29:00 +00:00
|
|
|
queue_size: Vec<u16>,
|
2020-06-23 15:28:41 +00:00
|
|
|
counters: NetCounters,
|
2020-08-04 18:27:17 +00:00
|
|
|
seccomp_action: SeccompAction,
|
2019-05-08 00:26:37 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 15:28:59 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct NetState {
|
|
|
|
pub avail_features: u64,
|
|
|
|
pub acked_features: u64,
|
|
|
|
pub config: VirtioNetConfig,
|
|
|
|
pub queue_size: Vec<u16>,
|
|
|
|
}
|
|
|
|
|
2019-05-08 00:26:37 +00:00
|
|
|
impl Net {
|
|
|
|
/// Create a new virtio network device with the given TAP interface.
|
2020-01-09 17:29:00 +00:00
|
|
|
pub fn new_with_tap(
|
2020-04-27 09:29:16 +00:00
|
|
|
id: String,
|
2020-01-09 17:29:00 +00:00
|
|
|
taps: Vec<Tap>,
|
|
|
|
guest_mac: Option<MacAddr>,
|
|
|
|
iommu: bool,
|
|
|
|
num_queues: usize,
|
|
|
|
queue_size: u16,
|
2020-08-04 18:27:17 +00:00
|
|
|
seccomp_action: SeccompAction,
|
2020-01-09 17:29:00 +00:00
|
|
|
) -> Result<Self> {
|
2019-05-08 00:26:37 +00:00
|
|
|
let mut avail_features = 1 << VIRTIO_NET_F_GUEST_CSUM
|
|
|
|
| 1 << VIRTIO_NET_F_CSUM
|
|
|
|
| 1 << VIRTIO_NET_F_GUEST_TSO4
|
|
|
|
| 1 << VIRTIO_NET_F_GUEST_UFO
|
|
|
|
| 1 << VIRTIO_NET_F_HOST_TSO4
|
|
|
|
| 1 << VIRTIO_NET_F_HOST_UFO
|
2020-06-01 12:08:53 +00:00
|
|
|
| 1 << VIRTIO_RING_F_EVENT_IDX
|
2019-05-08 00:26:37 +00:00
|
|
|
| 1 << VIRTIO_F_VERSION_1;
|
|
|
|
|
2019-10-02 21:26:02 +00:00
|
|
|
if iommu {
|
|
|
|
avail_features |= 1u64 << VIRTIO_F_IOMMU_PLATFORM;
|
|
|
|
}
|
|
|
|
|
2020-01-15 09:32:05 +00:00
|
|
|
avail_features |= 1 << VIRTIO_NET_F_CTRL_VQ;
|
2020-01-09 17:29:00 +00:00
|
|
|
let queue_num = num_queues + 1;
|
2020-01-15 09:32:05 +00:00
|
|
|
|
2020-01-27 16:37:14 +00:00
|
|
|
let mut config = VirtioNetConfig::default();
|
2019-05-08 00:26:37 +00:00
|
|
|
if let Some(mac) = guest_mac {
|
2020-01-27 16:37:14 +00:00
|
|
|
build_net_config_space(&mut config, mac, num_queues, &mut avail_features);
|
2019-05-08 00:26:37 +00:00
|
|
|
} else {
|
2020-01-27 16:37:14 +00:00
|
|
|
build_net_config_space_with_mq(&mut config, num_queues, &mut avail_features);
|
2019-05-08 00:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Net {
|
2020-04-27 09:29:16 +00:00
|
|
|
id,
|
2019-05-08 00:26:37 +00:00
|
|
|
kill_evt: None,
|
2019-11-19 00:42:31 +00:00
|
|
|
pause_evt: None,
|
2020-01-09 17:29:00 +00:00
|
|
|
taps: Some(taps),
|
2019-05-08 00:26:37 +00:00
|
|
|
avail_features,
|
|
|
|
acked_features: 0u64,
|
2020-01-27 16:37:14 +00:00
|
|
|
config,
|
2019-10-02 18:21:34 +00:00
|
|
|
queue_evts: None,
|
|
|
|
interrupt_cb: None,
|
2020-01-27 13:14:56 +00:00
|
|
|
epoll_threads: None,
|
2020-01-15 09:32:05 +00:00
|
|
|
ctrl_queue_epoll_thread: None,
|
2019-11-19 00:42:31 +00:00
|
|
|
paused: Arc::new(AtomicBool::new(false)),
|
2020-01-09 17:29:00 +00:00
|
|
|
queue_size: vec![queue_size; queue_num],
|
2020-06-23 15:28:41 +00:00
|
|
|
counters: NetCounters::default(),
|
2020-08-04 18:27:17 +00:00
|
|
|
seccomp_action,
|
2019-05-08 00:26:37 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new virtio network device with the given IP address and
|
|
|
|
/// netmask.
|
2020-04-27 09:29:16 +00:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2019-10-02 21:26:02 +00:00
|
|
|
pub fn new(
|
2020-04-27 09:29:16 +00:00
|
|
|
id: String,
|
2020-01-09 17:29:00 +00:00
|
|
|
if_name: Option<&str>,
|
|
|
|
ip_addr: Option<Ipv4Addr>,
|
|
|
|
netmask: Option<Ipv4Addr>,
|
2020-01-09 11:56:21 +00:00
|
|
|
guest_mac: Option<MacAddr>,
|
2020-06-05 11:00:34 +00:00
|
|
|
host_mac: &mut Option<MacAddr>,
|
2019-10-02 21:26:02 +00:00
|
|
|
iommu: bool,
|
2020-01-09 17:29:00 +00:00
|
|
|
num_queues: usize,
|
|
|
|
queue_size: u16,
|
2020-08-04 18:27:17 +00:00
|
|
|
seccomp_action: SeccompAction,
|
2019-10-02 21:26:02 +00:00
|
|
|
) -> Result<Self> {
|
2020-05-15 09:00:38 +00:00
|
|
|
let taps = open_tap(if_name, ip_addr, netmask, host_mac, num_queues / 2)
|
|
|
|
.map_err(Error::OpenTap)?;
|
2019-05-08 00:26:37 +00:00
|
|
|
|
2020-08-04 18:27:17 +00:00
|
|
|
Self::new_with_tap(
|
|
|
|
id,
|
|
|
|
taps,
|
|
|
|
guest_mac,
|
|
|
|
iommu,
|
|
|
|
num_queues,
|
|
|
|
queue_size,
|
|
|
|
seccomp_action,
|
|
|
|
)
|
2019-05-08 00:26:37 +00:00
|
|
|
}
|
2020-04-21 15:28:59 +00:00
|
|
|
|
|
|
|
fn state(&self) -> NetState {
|
|
|
|
NetState {
|
|
|
|
avail_features: self.avail_features,
|
|
|
|
acked_features: self.acked_features,
|
|
|
|
config: self.config,
|
|
|
|
queue_size: self.queue_size.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_state(&mut self, state: &NetState) -> Result<()> {
|
|
|
|
self.avail_features = state.avail_features;
|
|
|
|
self.acked_features = state.acked_features;
|
|
|
|
self.config = state.config;
|
|
|
|
self.queue_size = state.queue_size.clone();
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-05-08 00:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Net {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if let Some(kill_evt) = self.kill_evt.take() {
|
|
|
|
// Ignore the result because there is nothing we can do about it.
|
|
|
|
let _ = kill_evt.write(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl VirtioDevice for Net {
|
|
|
|
fn device_type(&self) -> u32 {
|
|
|
|
VirtioDeviceType::TYPE_NET as u32
|
|
|
|
}
|
|
|
|
|
|
|
|
fn queue_max_sizes(&self) -> &[u16] {
|
2020-01-09 17:29:00 +00:00
|
|
|
&self.queue_size.as_slice()
|
2019-05-08 00:26:37 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 10:14:38 +00:00
|
|
|
fn features(&self) -> u64 {
|
|
|
|
self.avail_features
|
2019-05-08 00:26:37 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 10:14:38 +00:00
|
|
|
fn ack_features(&mut self, value: u64) {
|
|
|
|
let mut v = value;
|
2019-05-08 00:26:37 +00:00
|
|
|
// Check if the guest is ACK'ing a feature that we didn't claim to have.
|
|
|
|
let unrequested_features = v & !self.avail_features;
|
|
|
|
if unrequested_features != 0 {
|
|
|
|
warn!("Received acknowledge request for unknown feature: {:x}", v);
|
|
|
|
// Don't count these features as acked.
|
|
|
|
v &= !unrequested_features;
|
|
|
|
}
|
|
|
|
self.acked_features |= v;
|
|
|
|
}
|
|
|
|
|
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-05-08 00:26:37 +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>,
|
2019-05-08 00:26:37 +00:00
|
|
|
mut queues: Vec<Queue>,
|
|
|
|
mut queue_evts: Vec<EventFd>,
|
|
|
|
) -> ActivateResult {
|
2020-01-09 17:29:00 +00:00
|
|
|
if queues.len() != self.queue_size.len() || queue_evts.len() != self.queue_size.len() {
|
2019-05-08 00:26:37 +00:00
|
|
|
error!(
|
|
|
|
"Cannot perform activate. Expected {} queue(s), got {}",
|
2020-01-09 17:29:00 +00:00
|
|
|
self.queue_size.len(),
|
2019-05-08 00:26:37 +00:00
|
|
|
queues.len()
|
|
|
|
);
|
|
|
|
return Err(ActivateError::BadActivate);
|
|
|
|
}
|
|
|
|
|
2019-11-19 00:42:31 +00:00
|
|
|
let (self_kill_evt, kill_evt) = EventFd::new(EFD_NONBLOCK)
|
|
|
|
.and_then(|e| Ok((e.try_clone()?, e)))
|
|
|
|
.map_err(|e| {
|
|
|
|
error!("failed creating kill EventFd pair: {}", e);
|
|
|
|
ActivateError::BadActivate
|
|
|
|
})?;
|
2019-05-08 00:26:37 +00:00
|
|
|
self.kill_evt = Some(self_kill_evt);
|
|
|
|
|
2019-11-19 00:42:31 +00:00
|
|
|
let (self_pause_evt, pause_evt) = EventFd::new(EFD_NONBLOCK)
|
|
|
|
.and_then(|e| Ok((e.try_clone()?, e)))
|
|
|
|
.map_err(|e| {
|
|
|
|
error!("failed creating pause EventFd pair: {}", e);
|
|
|
|
ActivateError::BadActivate
|
|
|
|
})?;
|
|
|
|
self.pause_evt = Some(self_pause_evt);
|
|
|
|
|
2020-01-09 17:29:00 +00:00
|
|
|
if let Some(mut taps) = self.taps.clone() {
|
2019-10-02 18:21:34 +00:00
|
|
|
// Save the interrupt EventFD as we need to return it on reset
|
|
|
|
// but clone it to pass into the thread.
|
|
|
|
self.interrupt_cb = Some(interrupt_cb.clone());
|
|
|
|
|
|
|
|
let mut tmp_queue_evts: Vec<EventFd> = Vec::new();
|
|
|
|
for queue_evt in queue_evts.iter() {
|
|
|
|
// Save the queue EventFD as we need to return it on reset
|
|
|
|
// but clone it to pass into the thread.
|
|
|
|
tmp_queue_evts.push(queue_evt.try_clone().map_err(|e| {
|
|
|
|
error!("failed to clone queue EventFd: {}", e);
|
|
|
|
ActivateError::BadActivate
|
|
|
|
})?);
|
|
|
|
}
|
|
|
|
self.queue_evts = Some(tmp_queue_evts);
|
|
|
|
|
2020-01-15 09:32:05 +00:00
|
|
|
let queue_num = queues.len();
|
|
|
|
if (self.acked_features & 1 << VIRTIO_NET_F_CTRL_VQ) != 0 && queue_num % 2 != 0 {
|
|
|
|
let cvq_queue = queues.remove(queue_num - 1);
|
|
|
|
let cvq_queue_evt = queue_evts.remove(queue_num - 1);
|
|
|
|
|
|
|
|
let mut ctrl_handler = NetCtrlEpollHandler {
|
|
|
|
mem: mem.clone(),
|
|
|
|
kill_evt: kill_evt.try_clone().unwrap(),
|
|
|
|
pause_evt: pause_evt.try_clone().unwrap(),
|
|
|
|
ctrl_q: CtrlVirtio::new(cvq_queue, cvq_queue_evt),
|
|
|
|
epoll_fd: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
let paused = self.paused.clone();
|
2020-08-04 18:27:17 +00:00
|
|
|
// Retrieve seccomp filter for virtio_net thread
|
|
|
|
let virtio_net_seccomp_filter =
|
|
|
|
get_seccomp_filter(&self.seccomp_action, Thread::VirtioNet)
|
|
|
|
.map_err(ActivateError::CreateSeccompFilter)?;
|
2020-01-15 09:32:05 +00:00
|
|
|
thread::Builder::new()
|
|
|
|
.name("virtio_net".to_string())
|
2020-08-04 18:27:17 +00:00
|
|
|
.spawn(move || {
|
|
|
|
SeccompFilter::apply(virtio_net_seccomp_filter)
|
|
|
|
.map_err(DeviceError::ApplySeccompFilter)?;
|
|
|
|
|
|
|
|
ctrl_handler.run_ctrl(paused)
|
|
|
|
})
|
2020-01-15 09:32:05 +00:00
|
|
|
.map(|thread| self.ctrl_queue_epoll_thread = Some(thread))
|
|
|
|
.map_err(|e| {
|
|
|
|
error!("failed to clone queue EventFd: {}", e);
|
|
|
|
ActivateError::BadActivate
|
|
|
|
})?;
|
|
|
|
}
|
|
|
|
|
2020-06-01 12:08:53 +00:00
|
|
|
let event_idx = self.acked_features & 1 << VIRTIO_RING_F_EVENT_IDX != 0;
|
|
|
|
|
2020-01-27 13:14:56 +00:00
|
|
|
let mut epoll_threads = Vec::new();
|
2020-01-09 17:29:00 +00:00
|
|
|
for _ in 0..taps.len() {
|
|
|
|
let rx = RxVirtio::new();
|
|
|
|
let tx = TxVirtio::new();
|
|
|
|
let rx_tap_listening = false;
|
2019-05-08 00:26:37 +00:00
|
|
|
|
2020-01-09 17:29:00 +00:00
|
|
|
let mut queue_pair = Vec::new();
|
|
|
|
queue_pair.push(queues.remove(0));
|
|
|
|
queue_pair.push(queues.remove(0));
|
2020-06-01 12:08:53 +00:00
|
|
|
queue_pair[0].set_event_idx(event_idx);
|
|
|
|
queue_pair[1].set_event_idx(event_idx);
|
2020-01-09 17:29:00 +00:00
|
|
|
|
|
|
|
let mut queue_evt_pair = Vec::new();
|
|
|
|
queue_evt_pair.push(queue_evts.remove(0));
|
|
|
|
queue_evt_pair.push(queue_evts.remove(0));
|
|
|
|
|
|
|
|
let mut handler = NetEpollHandler {
|
2020-05-29 13:50:11 +00:00
|
|
|
net: NetQueuePair {
|
2020-05-29 14:13:31 +00:00
|
|
|
mem: Some(mem.clone()),
|
2020-05-29 13:50:11 +00:00
|
|
|
tap: taps.remove(0),
|
|
|
|
rx,
|
|
|
|
tx,
|
2020-05-29 14:13:31 +00:00
|
|
|
epoll_fd: None,
|
2020-05-29 13:50:11 +00:00
|
|
|
rx_tap_listening,
|
2020-06-23 15:28:41 +00:00
|
|
|
counters: self.counters.clone(),
|
2020-07-23 16:16:10 +00:00
|
|
|
tap_event_id: RX_TAP_EVENT,
|
2020-05-29 13:50:11 +00:00
|
|
|
},
|
2020-07-23 16:16:10 +00:00
|
|
|
queue_pair,
|
|
|
|
queue_evt_pair,
|
2020-01-09 17:29:00 +00:00
|
|
|
interrupt_cb: interrupt_cb.clone(),
|
|
|
|
kill_evt: kill_evt.try_clone().unwrap(),
|
|
|
|
pause_evt: pause_evt.try_clone().unwrap(),
|
2020-06-02 16:00:31 +00:00
|
|
|
driver_awake: false,
|
2020-01-09 17:29:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let paused = self.paused.clone();
|
|
|
|
thread::Builder::new()
|
|
|
|
.name("virtio_net".to_string())
|
2020-07-23 16:16:10 +00:00
|
|
|
.spawn(move || handler.run(paused))
|
2020-01-27 13:14:56 +00:00
|
|
|
.map(|thread| epoll_threads.push(thread))
|
2020-01-09 17:29:00 +00:00
|
|
|
.map_err(|e| {
|
|
|
|
error!("failed to clone queue EventFd: {}", e);
|
|
|
|
ActivateError::BadActivate
|
|
|
|
})?;
|
|
|
|
}
|
|
|
|
|
2020-01-27 13:14:56 +00:00
|
|
|
self.epoll_threads = Some(epoll_threads);
|
2019-05-08 00:26:37 +00:00
|
|
|
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
Err(ActivateError::BadActivate)
|
|
|
|
}
|
2019-10-02 18:21:34 +00:00
|
|
|
|
2020-01-13 17:52:19 +00:00
|
|
|
fn reset(&mut self) -> Option<(Arc<dyn VirtioInterrupt>, Vec<EventFd>)> {
|
2019-11-19 00:42:31 +00:00
|
|
|
// We first must resume the virtio thread if it was paused.
|
|
|
|
if self.pause_evt.take().is_some() {
|
|
|
|
self.resume().ok()?;
|
|
|
|
}
|
|
|
|
|
2019-10-02 18:21:34 +00:00
|
|
|
if let Some(kill_evt) = self.kill_evt.take() {
|
|
|
|
// Ignore the result because there is nothing we can do about it.
|
|
|
|
let _ = kill_evt.write(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the interrupt and queue EventFDs
|
|
|
|
Some((
|
|
|
|
self.interrupt_cb.take().unwrap(),
|
|
|
|
self.queue_evts.take().unwrap(),
|
|
|
|
))
|
|
|
|
}
|
2020-06-24 09:50:04 +00:00
|
|
|
|
|
|
|
fn counters(&self) -> Option<HashMap<&'static str, Wrapping<u64>>> {
|
|
|
|
let mut counters = HashMap::new();
|
|
|
|
|
|
|
|
counters.insert(
|
|
|
|
"rx_bytes",
|
|
|
|
Wrapping(self.counters.rx_bytes.load(Ordering::Acquire)),
|
|
|
|
);
|
|
|
|
counters.insert(
|
|
|
|
"rx_frames",
|
|
|
|
Wrapping(self.counters.rx_frames.load(Ordering::Acquire)),
|
|
|
|
);
|
|
|
|
counters.insert(
|
|
|
|
"tx_bytes",
|
|
|
|
Wrapping(self.counters.tx_bytes.load(Ordering::Acquire)),
|
|
|
|
);
|
|
|
|
counters.insert(
|
|
|
|
"tx_frames",
|
|
|
|
Wrapping(self.counters.tx_frames.load(Ordering::Acquire)),
|
|
|
|
);
|
|
|
|
|
|
|
|
Some(counters)
|
|
|
|
}
|
2019-05-08 00:26:37 +00:00
|
|
|
}
|
2019-11-19 00:42:31 +00:00
|
|
|
|
2020-01-27 18:38:27 +00:00
|
|
|
virtio_ctrl_q_pausable!(Net);
|
2020-04-21 15:28:59 +00:00
|
|
|
impl Snapshottable for Net {
|
|
|
|
fn id(&self) -> String {
|
2020-04-27 09:29:16 +00:00
|
|
|
self.id.clone()
|
2020-04-21 15:28:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn snapshot(&self) -> std::result::Result<Snapshot, MigratableError> {
|
|
|
|
let snapshot =
|
|
|
|
serde_json::to_vec(&self.state()).map_err(|e| MigratableError::Snapshot(e.into()))?;
|
|
|
|
|
2020-04-27 09:29:16 +00:00
|
|
|
let mut net_snapshot = Snapshot::new(self.id.as_str());
|
2020-04-21 15:28:59 +00:00
|
|
|
net_snapshot.add_data_section(SnapshotDataSection {
|
2020-04-27 09:29:16 +00:00
|
|
|
id: format!("{}-section", self.id),
|
2020-04-21 15:28:59 +00:00
|
|
|
snapshot,
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(net_snapshot)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn restore(&mut self, snapshot: Snapshot) -> std::result::Result<(), MigratableError> {
|
2020-04-27 09:29:16 +00:00
|
|
|
if let Some(net_section) = snapshot.snapshot_data.get(&format!("{}-section", self.id)) {
|
2020-04-21 15:28:59 +00:00
|
|
|
let net_state = match serde_json::from_slice(&net_section.snapshot) {
|
|
|
|
Ok(state) => state,
|
|
|
|
Err(error) => {
|
|
|
|
return Err(MigratableError::Restore(anyhow!(
|
|
|
|
"Could not deserialize NET {}",
|
|
|
|
error
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return self.set_state(&net_state).map_err(|e| {
|
|
|
|
MigratableError::Restore(anyhow!("Could not restore NET state {:?}", e))
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(MigratableError::Restore(anyhow!(
|
|
|
|
"Could not find NET snapshot section"
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
}
|
2019-05-01 16:59:51 +00:00
|
|
|
impl Transportable for Net {}
|
2019-11-19 00:42:31 +00:00
|
|
|
impl Migratable for Net {}
|