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::{
|
|
|
|
build_net_config_space, open_tap, RxVirtio, TxVirtio, KILL_EVENT, NET_EVENTS_COUNT,
|
|
|
|
PAUSE_EVENT, RX_QUEUE_EVENT, RX_TAP_EVENT, TX_QUEUE_EVENT,
|
|
|
|
};
|
2019-12-31 10:49:11 +00:00
|
|
|
use super::Error as DeviceError;
|
|
|
|
use super::{
|
2020-01-09 11:56:21 +00:00
|
|
|
ActivateError, ActivateResult, Queue, VirtioDevice, VirtioDeviceType, VirtioInterruptType,
|
2019-12-31 10:49:11 +00:00
|
|
|
};
|
|
|
|
use crate::VirtioInterrupt;
|
|
|
|
use arc_swap::ArcSwap;
|
2019-05-08 00:26:37 +00:00
|
|
|
use epoll;
|
|
|
|
use libc::EAGAIN;
|
|
|
|
use libc::EFD_NONBLOCK;
|
2020-01-09 11:56:21 +00:00
|
|
|
use net_util::{MacAddr, Tap};
|
2019-05-08 00:26:37 +00:00
|
|
|
use std::cmp;
|
|
|
|
use std::io::Read;
|
|
|
|
use std::io::{self, Write};
|
|
|
|
use std::net::Ipv4Addr;
|
2019-08-21 12:44:49 +00:00
|
|
|
use std::os::unix::io::{AsRawFd, RawFd};
|
2019-05-08 00:26:37 +00:00
|
|
|
use std::result;
|
2019-11-19 00:42:31 +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::*;
|
2019-11-19 00:42:31 +00:00
|
|
|
use vm_device::{Migratable, MigratableError, Pausable, Snapshotable};
|
2020-01-09 11:56:21 +00:00
|
|
|
use vm_memory::GuestMemoryMmap;
|
2019-08-02 14:23:52 +00:00
|
|
|
use vmm_sys_util::eventfd::EventFd;
|
2019-05-08 00:26:37 +00:00
|
|
|
|
|
|
|
const QUEUE_SIZE: u16 = 256;
|
|
|
|
const NUM_QUEUES: usize = 2;
|
|
|
|
const QUEUE_SIZES: &[u16] = &[QUEUE_SIZE; NUM_QUEUES];
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
2020-01-09 11:56:21 +00:00
|
|
|
/// Failed to open taps.
|
|
|
|
OpenTap(super::net_util::Error),
|
2019-05-08 00:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub type Result<T> = result::Result<T, Error>;
|
|
|
|
|
|
|
|
struct NetEpollHandler {
|
2019-12-31 10:49:11 +00:00
|
|
|
mem: Arc<ArcSwap<GuestMemoryMmap>>,
|
2019-05-08 00:26:37 +00:00
|
|
|
tap: Tap,
|
|
|
|
rx: RxVirtio,
|
|
|
|
tx: TxVirtio,
|
2019-06-03 20:57:26 +00:00
|
|
|
interrupt_cb: Arc<VirtioInterrupt>,
|
2019-05-08 00:26:37 +00:00
|
|
|
kill_evt: EventFd,
|
2019-11-19 00:42:31 +00:00
|
|
|
pause_evt: EventFd,
|
2019-08-21 12:44:49 +00:00
|
|
|
epoll_fd: RawFd,
|
|
|
|
rx_tap_listening: bool,
|
2019-05-08 00:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl NetEpollHandler {
|
2019-06-03 20:57:26 +00:00
|
|
|
fn signal_used_queue(&self, queue: &Queue) -> result::Result<(), DeviceError> {
|
2019-07-26 18:48:07 +00:00
|
|
|
(self.interrupt_cb)(&VirtioInterruptType::Queue, Some(queue)).map_err(|e| {
|
2019-05-08 00:26:37 +00:00
|
|
|
error!("Failed to signal used queue: {:?}", e);
|
|
|
|
DeviceError::FailedSignalingUsedQueue(e)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copies a single frame from `self.rx.frame_buf` into the guest. Returns true
|
|
|
|
// if a buffer was used, and false if the frame must be deferred until a buffer
|
|
|
|
// is made available by the driver.
|
2020-01-09 11:56:21 +00:00
|
|
|
fn rx_single_frame(&mut self, mut queue: &mut Queue) -> bool {
|
2019-12-31 10:49:11 +00:00
|
|
|
let mem = self.mem.load();
|
2020-01-09 11:56:21 +00:00
|
|
|
let next_desc = queue.iter(&mem).next();
|
2019-05-08 00:26:37 +00:00
|
|
|
|
|
|
|
if next_desc.is_none() {
|
2019-08-21 12:44:49 +00:00
|
|
|
// Queue has no available descriptors
|
|
|
|
if self.rx_tap_listening {
|
2020-01-09 11:56:21 +00:00
|
|
|
unregister_listener(
|
|
|
|
self.epoll_fd,
|
|
|
|
self.tap.as_raw_fd(),
|
|
|
|
epoll::Events::EPOLLIN,
|
|
|
|
u64::from(RX_TAP_EVENT),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2019-08-21 15:54:12 +00:00
|
|
|
self.rx_tap_listening = false;
|
2019-08-21 12:44:49 +00:00
|
|
|
}
|
2019-05-08 00:26:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-01-09 11:56:21 +00:00
|
|
|
self.rx.process_desc_chain(&mem, next_desc, &mut queue)
|
2019-05-08 00:26:37 +00:00
|
|
|
}
|
|
|
|
|
2020-01-09 11:56:21 +00:00
|
|
|
fn process_rx(&mut self, queue: &mut Queue) -> result::Result<(), DeviceError> {
|
2019-05-08 00:26:37 +00:00
|
|
|
// Read as many frames as possible.
|
|
|
|
loop {
|
|
|
|
match self.read_tap() {
|
|
|
|
Ok(count) => {
|
|
|
|
self.rx.bytes_read = count;
|
2020-01-09 11:56:21 +00:00
|
|
|
if !self.rx_single_frame(queue) {
|
2019-05-08 00:26:37 +00:00
|
|
|
self.rx.deferred_frame = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
// The tap device is non-blocking, so any error aside from EAGAIN is
|
|
|
|
// unexpected.
|
|
|
|
match e.raw_os_error() {
|
|
|
|
Some(err) if err == EAGAIN => (),
|
|
|
|
_ => {
|
|
|
|
error!("Failed to read tap: {:?}", e);
|
|
|
|
return Err(DeviceError::FailedReadTap);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if self.rx.deferred_irqs {
|
|
|
|
self.rx.deferred_irqs = false;
|
2020-01-09 11:56:21 +00:00
|
|
|
self.signal_used_queue(queue)
|
2019-05-08 00:26:37 +00:00
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-09 11:56:21 +00:00
|
|
|
fn resume_rx(&mut self, queue: &mut Queue) -> result::Result<(), DeviceError> {
|
2019-05-08 00:26:37 +00:00
|
|
|
if self.rx.deferred_frame {
|
2020-01-09 11:56:21 +00:00
|
|
|
if self.rx_single_frame(queue) {
|
2019-05-08 00:26:37 +00:00
|
|
|
self.rx.deferred_frame = false;
|
|
|
|
// process_rx() was interrupted possibly before consuming all
|
|
|
|
// packets in the tap; try continuing now.
|
2020-01-09 11:56:21 +00:00
|
|
|
self.process_rx(queue)
|
2019-05-08 00:26:37 +00:00
|
|
|
} else if self.rx.deferred_irqs {
|
|
|
|
self.rx.deferred_irqs = false;
|
2020-01-09 11:56:21 +00:00
|
|
|
self.signal_used_queue(queue)
|
2019-05-08 00:26:37 +00:00
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-09 11:56:21 +00:00
|
|
|
fn process_tx(&mut self, mut queue: &mut Queue) -> result::Result<(), DeviceError> {
|
2019-12-31 10:49:11 +00:00
|
|
|
let mem = self.mem.load();
|
2019-05-08 00:26:37 +00:00
|
|
|
|
2020-01-09 11:56:21 +00:00
|
|
|
self.tx.process_desc_chain(&mem, &mut self.tap, &mut queue);
|
2019-05-08 00:26:37 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_tap(&mut self) -> io::Result<usize> {
|
|
|
|
self.tap.read(&mut self.rx.frame_buf)
|
|
|
|
}
|
|
|
|
|
2020-01-09 11:56:21 +00:00
|
|
|
fn handle_rx_event(&mut self, mut queue: &mut Queue, queue_evt: &EventFd) {
|
|
|
|
if let Err(e) = queue_evt.read() {
|
|
|
|
error!("Failed to get rx queue event: {:?}", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.resume_rx(&mut queue).unwrap();
|
|
|
|
if !self.rx_tap_listening {
|
|
|
|
register_listener(
|
|
|
|
self.epoll_fd,
|
|
|
|
self.tap.as_raw_fd(),
|
|
|
|
epoll::Events::EPOLLIN,
|
|
|
|
u64::from(RX_TAP_EVENT),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
self.rx_tap_listening = true;
|
|
|
|
}
|
2019-08-21 12:44:49 +00:00
|
|
|
}
|
|
|
|
|
2020-01-09 11:56:21 +00:00
|
|
|
fn handle_tx_event(&mut self, mut queue: &mut Queue, queue_evt: &EventFd) {
|
|
|
|
if let Err(e) = queue_evt.read() {
|
|
|
|
error!("Failed to get tx queue event: {:?}", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.process_tx(&mut queue).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_rx_tap_event(&mut self, mut queue: &mut Queue) {
|
|
|
|
if self.rx.deferred_frame
|
|
|
|
// Process a deferred frame first if available. Don't read from tap again
|
|
|
|
// until we manage to receive this deferred frame.
|
|
|
|
{
|
|
|
|
if self.rx_single_frame(&mut queue) {
|
|
|
|
self.rx.deferred_frame = false;
|
|
|
|
self.process_rx(&mut queue).unwrap();
|
|
|
|
} else if self.rx.deferred_irqs {
|
|
|
|
self.rx.deferred_irqs = false;
|
|
|
|
self.signal_used_queue(&queue).unwrap();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.process_rx(&mut queue).unwrap();
|
|
|
|
}
|
2019-08-21 12:44:49 +00:00
|
|
|
}
|
|
|
|
|
2020-01-09 11:56:21 +00:00
|
|
|
fn run(
|
|
|
|
&mut self,
|
|
|
|
paused: Arc<AtomicBool>,
|
|
|
|
mut queues: Vec<Queue>,
|
|
|
|
queue_evts: Vec<EventFd>,
|
|
|
|
) -> result::Result<(), DeviceError> {
|
2019-05-08 00:26:37 +00:00
|
|
|
// Create the epoll file descriptor
|
2019-08-21 12:44:49 +00:00
|
|
|
self.epoll_fd = epoll::create(true).map_err(DeviceError::EpollCreateFd)?;
|
2019-05-08 00:26:37 +00:00
|
|
|
// Add events
|
2020-01-09 11:56:21 +00:00
|
|
|
// Add events
|
2019-05-08 00:26:37 +00:00
|
|
|
epoll::ctl(
|
2019-08-21 12:44:49 +00:00
|
|
|
self.epoll_fd,
|
2019-05-08 00:26:37 +00:00
|
|
|
epoll::ControlOptions::EPOLL_CTL_ADD,
|
2020-01-09 11:56:21 +00:00
|
|
|
queue_evts[0].as_raw_fd(),
|
2019-05-08 00:26:37 +00:00
|
|
|
epoll::Event::new(epoll::Events::EPOLLIN, u64::from(RX_QUEUE_EVENT)),
|
|
|
|
)
|
|
|
|
.map_err(DeviceError::EpollCtl)?;
|
|
|
|
epoll::ctl(
|
2019-08-21 12:44:49 +00:00
|
|
|
self.epoll_fd,
|
2019-05-08 00:26:37 +00:00
|
|
|
epoll::ControlOptions::EPOLL_CTL_ADD,
|
2020-01-09 11:56:21 +00:00
|
|
|
queue_evts[1].as_raw_fd(),
|
2019-05-08 00:26:37 +00:00
|
|
|
epoll::Event::new(epoll::Events::EPOLLIN, u64::from(TX_QUEUE_EVENT)),
|
|
|
|
)
|
|
|
|
.map_err(DeviceError::EpollCtl)?;
|
|
|
|
epoll::ctl(
|
2019-08-21 12:44:49 +00:00
|
|
|
self.epoll_fd,
|
2019-05-08 00:26:37 +00:00
|
|
|
epoll::ControlOptions::EPOLL_CTL_ADD,
|
|
|
|
self.kill_evt.as_raw_fd(),
|
|
|
|
epoll::Event::new(epoll::Events::EPOLLIN, u64::from(KILL_EVENT)),
|
|
|
|
)
|
|
|
|
.map_err(DeviceError::EpollCtl)?;
|
2019-11-19 00:42:31 +00:00
|
|
|
epoll::ctl(
|
|
|
|
self.epoll_fd,
|
|
|
|
epoll::ControlOptions::EPOLL_CTL_ADD,
|
|
|
|
self.pause_evt.as_raw_fd(),
|
|
|
|
epoll::Event::new(epoll::Events::EPOLLIN, u64::from(PAUSE_EVENT)),
|
|
|
|
)
|
|
|
|
.map_err(DeviceError::EpollCtl)?;
|
2019-05-08 00:26:37 +00:00
|
|
|
|
2020-01-09 11:56:21 +00:00
|
|
|
let mut events = vec![epoll::Event::new(epoll::Events::empty(), 0); NET_EVENTS_COUNT];
|
2019-05-08 00:26:37 +00:00
|
|
|
|
|
|
|
'epoll: loop {
|
2019-08-21 12:44:49 +00:00
|
|
|
let num_events = match epoll::wait(self.epoll_fd, -1, &mut events[..]) {
|
2019-08-01 20:08:47 +00:00
|
|
|
Ok(res) => res,
|
|
|
|
Err(e) => {
|
|
|
|
if e.kind() == io::ErrorKind::Interrupted {
|
|
|
|
// It's well defined from the epoll_wait() syscall
|
|
|
|
// documentation that the epoll loop can be interrupted
|
|
|
|
// before any of the requested events occurred or the
|
|
|
|
// timeout expired. In both those cases, epoll_wait()
|
|
|
|
// returns an error of type EINTR, but this should not
|
|
|
|
// be considered as a regular error. Instead it is more
|
|
|
|
// appropriate to retry, by calling into epoll_wait().
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return Err(DeviceError::EpollWait(e));
|
|
|
|
}
|
|
|
|
};
|
2019-05-08 00:26:37 +00:00
|
|
|
|
|
|
|
for event in events.iter().take(num_events) {
|
|
|
|
let ev_type = event.data as u16;
|
|
|
|
|
|
|
|
match ev_type {
|
|
|
|
RX_QUEUE_EVENT => {
|
2020-01-09 11:56:21 +00:00
|
|
|
self.handle_rx_event(&mut queues[0], &queue_evts[0]);
|
2019-05-08 00:26:37 +00:00
|
|
|
}
|
|
|
|
TX_QUEUE_EVENT => {
|
2020-01-09 11:56:21 +00:00
|
|
|
self.handle_tx_event(&mut queues[1], &queue_evts[1]);
|
2019-05-08 00:26:37 +00:00
|
|
|
}
|
|
|
|
RX_TAP_EVENT => {
|
2020-01-09 11:56:21 +00:00
|
|
|
self.handle_rx_tap_event(&mut queues[0]);
|
2019-05-08 00:26:37 +00:00
|
|
|
}
|
|
|
|
KILL_EVENT => {
|
|
|
|
debug!("KILL_EVENT received, stopping epoll loop");
|
|
|
|
break 'epoll;
|
|
|
|
}
|
2019-11-19 00:42:31 +00:00
|
|
|
PAUSE_EVENT => {
|
|
|
|
debug!("PAUSE_EVENT received, pausing virtio-net epoll loop");
|
|
|
|
// We loop here to handle spurious park() returns.
|
|
|
|
// Until we have not resumed, the paused boolean will
|
|
|
|
// be true.
|
|
|
|
while paused.load(Ordering::SeqCst) {
|
|
|
|
thread::park();
|
|
|
|
}
|
|
|
|
}
|
2019-05-08 00:26:37 +00:00
|
|
|
_ => {
|
|
|
|
error!("Unknown event for virtio-net");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-09 11:56:21 +00:00
|
|
|
pub fn register_listener(
|
|
|
|
epoll_fd: RawFd,
|
|
|
|
fd: RawFd,
|
|
|
|
ev_type: epoll::Events,
|
|
|
|
data: u64,
|
|
|
|
) -> result::Result<(), io::Error> {
|
|
|
|
epoll::ctl(
|
|
|
|
epoll_fd,
|
|
|
|
epoll::ControlOptions::EPOLL_CTL_ADD,
|
|
|
|
fd,
|
|
|
|
epoll::Event::new(ev_type, data),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unregister_listener(
|
|
|
|
epoll_fd: RawFd,
|
|
|
|
fd: RawFd,
|
|
|
|
ev_type: epoll::Events,
|
|
|
|
data: u64,
|
|
|
|
) -> result::Result<(), io::Error> {
|
|
|
|
epoll::ctl(
|
|
|
|
epoll_fd,
|
|
|
|
epoll::ControlOptions::EPOLL_CTL_DEL,
|
|
|
|
fd,
|
|
|
|
epoll::Event::new(ev_type, data),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-05-08 00:26:37 +00:00
|
|
|
pub struct Net {
|
|
|
|
kill_evt: Option<EventFd>,
|
2019-11-19 00:42:31 +00:00
|
|
|
pause_evt: Option<EventFd>,
|
2019-05-08 00:26:37 +00:00
|
|
|
tap: Option<Tap>,
|
|
|
|
avail_features: u64,
|
|
|
|
acked_features: u64,
|
|
|
|
// The config space will only consist of the MAC address specified by the user,
|
|
|
|
// or nothing, if no such address if provided.
|
|
|
|
config_space: Vec<u8>,
|
2019-10-02 18:21:34 +00:00
|
|
|
queue_evts: Option<Vec<EventFd>>,
|
|
|
|
interrupt_cb: Option<Arc<VirtioInterrupt>>,
|
2019-11-19 00:42:31 +00:00
|
|
|
epoll_thread: Option<thread::JoinHandle<result::Result<(), DeviceError>>>,
|
|
|
|
paused: Arc<AtomicBool>,
|
2019-05-08 00:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Net {
|
|
|
|
/// Create a new virtio network device with the given TAP interface.
|
2020-01-09 11:56:21 +00:00
|
|
|
pub fn new_with_tap(tap: Tap, guest_mac: Option<MacAddr>, iommu: bool) -> 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
|
|
|
|
| 1 << VIRTIO_F_VERSION_1;
|
|
|
|
|
2019-10-02 21:26:02 +00:00
|
|
|
if iommu {
|
|
|
|
avail_features |= 1u64 << VIRTIO_F_IOMMU_PLATFORM;
|
|
|
|
}
|
|
|
|
|
2020-01-09 11:56:21 +00:00
|
|
|
let config_space;
|
2019-05-08 00:26:37 +00:00
|
|
|
if let Some(mac) = guest_mac {
|
2020-01-09 11:56:21 +00:00
|
|
|
config_space = build_net_config_space(mac, &mut avail_features);
|
2019-05-08 00:26:37 +00:00
|
|
|
} else {
|
|
|
|
config_space = Vec::new();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Net {
|
|
|
|
kill_evt: None,
|
2019-11-19 00:42:31 +00:00
|
|
|
pause_evt: None,
|
2019-05-08 00:26:37 +00:00
|
|
|
tap: Some(tap),
|
|
|
|
avail_features,
|
|
|
|
acked_features: 0u64,
|
|
|
|
config_space,
|
2019-10-02 18:21:34 +00:00
|
|
|
queue_evts: None,
|
|
|
|
interrupt_cb: None,
|
2019-11-19 00:42:31 +00:00
|
|
|
epoll_thread: None,
|
|
|
|
paused: Arc::new(AtomicBool::new(false)),
|
2019-05-08 00:26:37 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new virtio network device with the given IP address and
|
|
|
|
/// netmask.
|
2019-10-02 21:26:02 +00:00
|
|
|
pub fn new(
|
|
|
|
ip_addr: Ipv4Addr,
|
|
|
|
netmask: Ipv4Addr,
|
2020-01-09 11:56:21 +00:00
|
|
|
guest_mac: Option<MacAddr>,
|
2019-10-02 21:26:02 +00:00
|
|
|
iommu: bool,
|
|
|
|
) -> Result<Self> {
|
2020-01-09 11:56:21 +00:00
|
|
|
let tap = open_tap(ip_addr, netmask).map_err(Error::OpenTap)?;
|
2019-05-08 00:26:37 +00:00
|
|
|
|
2019-10-02 21:26:02 +00:00
|
|
|
Self::new_with_tap(tap, guest_mac, iommu)
|
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] {
|
|
|
|
QUEUE_SIZES
|
|
|
|
}
|
|
|
|
|
|
|
|
fn features(&self, page: u32) -> u32 {
|
|
|
|
match page {
|
|
|
|
// Get the lower 32-bits of the features bitfield.
|
|
|
|
0 => self.avail_features as u32,
|
|
|
|
// Get the upper 32-bits of the features bitfield.
|
|
|
|
1 => (self.avail_features >> 32) as u32,
|
|
|
|
_ => {
|
|
|
|
warn!("Received request for unknown features page: {}", page);
|
|
|
|
0u32
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ack_features(&mut self, page: u32, value: u32) {
|
|
|
|
let mut v = match page {
|
|
|
|
0 => u64::from(value),
|
|
|
|
1 => u64::from(value) << 32,
|
|
|
|
_ => {
|
|
|
|
warn!("Cannot acknowledge unknown features page: {}", page);
|
|
|
|
0u64
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_config(&self, offset: u64, mut data: &mut [u8]) {
|
|
|
|
let config_len = self.config_space.len() as u64;
|
|
|
|
if offset >= config_len {
|
|
|
|
error!("Failed to read config space");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if let Some(end) = offset.checked_add(data.len() as u64) {
|
|
|
|
// This write can't fail, offset and end are checked against config_len.
|
|
|
|
data.write_all(&self.config_space[offset as usize..cmp::min(end, config_len) as usize])
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_config(&mut self, offset: u64, data: &[u8]) {
|
|
|
|
let data_len = data.len() as u64;
|
|
|
|
let config_len = self.config_space.len() as u64;
|
|
|
|
if offset + data_len > config_len {
|
|
|
|
error!("Failed to write config space");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let (_, right) = self.config_space.split_at_mut(offset as usize);
|
|
|
|
right.copy_from_slice(&data[..]);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn activate(
|
|
|
|
&mut self,
|
2019-12-31 10:49:11 +00:00
|
|
|
mem: Arc<ArcSwap<GuestMemoryMmap>>,
|
2019-06-03 20:57:26 +00:00
|
|
|
interrupt_cb: Arc<VirtioInterrupt>,
|
2019-05-08 00:26:37 +00:00
|
|
|
mut queues: Vec<Queue>,
|
|
|
|
mut queue_evts: Vec<EventFd>,
|
|
|
|
) -> ActivateResult {
|
|
|
|
if queues.len() != NUM_QUEUES || queue_evts.len() != NUM_QUEUES {
|
|
|
|
error!(
|
|
|
|
"Cannot perform activate. Expected {} queue(s), got {}",
|
|
|
|
NUM_QUEUES,
|
|
|
|
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);
|
|
|
|
|
2019-10-02 18:21:34 +00:00
|
|
|
if let Some(tap) = self.tap.clone() {
|
|
|
|
// 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-09 11:56:21 +00:00
|
|
|
let mut queues_v = Vec::new();
|
|
|
|
let mut queue_evts_v = Vec::new();
|
|
|
|
queues_v.push(queues.remove(0));
|
|
|
|
queues_v.push(queues.remove(0));
|
|
|
|
queue_evts_v.push(queue_evts.remove(0));
|
|
|
|
queue_evts_v.push(queue_evts.remove(0));
|
2019-05-08 00:26:37 +00:00
|
|
|
let mut handler = NetEpollHandler {
|
|
|
|
mem,
|
|
|
|
tap,
|
2020-01-09 11:56:21 +00:00
|
|
|
rx: RxVirtio::new(),
|
|
|
|
tx: TxVirtio::new(),
|
2019-06-03 20:57:26 +00:00
|
|
|
interrupt_cb,
|
2019-05-08 00:26:37 +00:00
|
|
|
kill_evt,
|
2019-11-19 00:42:31 +00:00
|
|
|
pause_evt,
|
2019-08-21 12:44:49 +00:00
|
|
|
epoll_fd: 0,
|
|
|
|
rx_tap_listening: false,
|
2019-05-08 00:26:37 +00:00
|
|
|
};
|
|
|
|
|
2019-11-19 00:42:31 +00:00
|
|
|
let paused = self.paused.clone();
|
|
|
|
thread::Builder::new()
|
2019-05-08 00:26:37 +00:00
|
|
|
.name("virtio_net".to_string())
|
2020-01-09 11:56:21 +00:00
|
|
|
.spawn(move || handler.run(paused, queues_v, queue_evts_v))
|
2019-11-19 00:42:31 +00:00
|
|
|
.map(|thread| self.epoll_thread = Some(thread))
|
|
|
|
.map_err(|e| {
|
|
|
|
error!("failed to clone the virtio-net epoll thread: {}", e);
|
|
|
|
ActivateError::BadActivate
|
|
|
|
})?;
|
2019-05-08 00:26:37 +00:00
|
|
|
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
Err(ActivateError::BadActivate)
|
|
|
|
}
|
2019-10-02 18:21:34 +00:00
|
|
|
|
|
|
|
fn reset(&mut self) -> Option<(Arc<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(),
|
|
|
|
))
|
|
|
|
}
|
2019-05-08 00:26:37 +00:00
|
|
|
}
|
2019-11-19 00:42:31 +00:00
|
|
|
|
|
|
|
virtio_pausable!(Net);
|
|
|
|
impl Snapshotable for Net {}
|
|
|
|
impl Migratable for Net {}
|