virtio/net: refactored net device creation
Implemented an additional way of constructing a Net struct, by providing
an already initialized Tap parameter.
@@ -18,7 +18,7 @@ use libc::EAGAIN;
use {DeviceEventT, EpollHandler};
use super::{ActivateError, ActivateResult};
use epoll;
-use net_util::{TapError, Tap};
+use net_util::{Tap, TapError};
use net_sys;
use super::{Queue, VirtioDevice, INTERRUPT_STATUS_USED_RING, TYPE_NET};
use sys_util::{Error as SysError, EventFd, GuestMemory};
@@ -322,19 +322,9 @@ pub struct Net {
}
impl Net {
- /// Create a new virtio network device with the given IP address and
- /// netmask.
- pub fn new(
- ip_addr: Ipv4Addr,
- netmask: Ipv4Addr,
- epoll_config: EpollConfig,
- ) -> Result<Net, NetError> {
+ pub fn new_with_tap(tap: Tap, epoll_config: EpollConfig) -> Result<Self, NetError> {
let kill_evt = EventFd::new().map_err(NetError::CreateKillEventFd)?;
- let tap = Tap::new().map_err(NetError::TapOpen)?;
- tap.set_ip_addr(ip_addr).map_err(NetError::TapSetIp)?;
- tap.set_netmask(netmask).map_err(NetError::TapSetNetmask)?;
-
// Set offload flags to match the virtio features below.
tap.set_offload(
net_sys::TUN_F_CSUM | net_sys::TUN_F_UFO | net_sys::TUN_F_TSO4 | net_sys::TUN_F_TSO6,
@@ -344,8 +334,6 @@ impl Net {
tap.set_vnet_hdr_size(vnet_hdr_size)
.map_err(NetError::TapSetVnetHdrSize)?;
- tap.enable().map_err(NetError::TapEnable)?;
-
let avail_features = 1 << virtio_net::VIRTIO_NET_F_GUEST_CSUM
| 1 << virtio_net::VIRTIO_NET_F_CSUM
| 1 << virtio_net::VIRTIO_NET_F_GUEST_TSO4
@@ -363,6 +351,21 @@ impl Net {
epoll_config,
})
}
+
+ /// Create a new virtio network device with the given IP address and
+ /// netmask.
+ pub fn new(
+ ip_addr: Ipv4Addr,
+ netmask: Ipv4Addr,
+ epoll_config: EpollConfig,
+ ) -> Result<Self, NetError> {
+ let tap = Tap::new().map_err(NetError::TapOpen)?;
+ tap.set_ip_addr(ip_addr).map_err(NetError::TapSetIp)?;
+ tap.set_netmask(netmask).map_err(NetError::TapSetNetmask)?;
+ tap.enable().map_err(NetError::TapEnable)?;
+
+ Self::new_with_tap(tap, epoll_config)
+ }
}
impl Drop for Net {
