Remove dummy device type

This commit is contained in:
Dennis Schwerdel 2020-11-28 23:47:43 +01:00
parent d3402e1f50
commit ea049e4a4c
6 changed files with 17 additions and 51 deletions

View File

@ -2,6 +2,10 @@
This project follows [semantic versioning](http://semver.org). This project follows [semantic versioning](http://semver.org).
### UNRELEASED
- [removed] Removed dummy device type
### v2.0.1 (2020-11-07) ### v2.0.1 (2020-11-07)
- [changed] Changed documentation - [changed] Changed documentation

View File

@ -106,7 +106,7 @@ impl<D: Device, P: Protocol, S: Socket, TS: TimeSource> GenericCloud<D, P, S, TS
Mode::Normal => { Mode::Normal => {
match config.device_type { match config.device_type {
Type::Tap => (true, true), Type::Tap => (true, true),
Type::Tun | Type::Dummy => (false, false) Type::Tun => (false, false)
} }
} }
Mode::Router => (false, false), Mode::Router => (false, false),

View File

@ -589,7 +589,7 @@ statsd:
#[test] #[test]
fn default_config_as_default() { fn default_config_as_default() {
let mut default_config = Config { let mut default_config = Config {
device_type: Type::Dummy, device_type: Type::Tun,
device_name: "".to_string(), device_name: "".to_string(),
device_path: None, device_path: None,
fix_rp_filter: false, fix_rp_filter: false,

View File

@ -51,18 +51,14 @@ pub enum Type {
Tun, Tun,
/// Tap interface: This interface transports Ethernet frames. /// Tap interface: This interface transports Ethernet frames.
#[serde(rename = "tap")] #[serde(rename = "tap")]
Tap, Tap
/// Dummy interface: This interface does nothing.
#[serde(rename = "dummy")]
Dummy
} }
impl fmt::Display for Type { impl fmt::Display for Type {
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> { fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match *self { match *self {
Type::Tun => write!(formatter, "tun"), Type::Tun => write!(formatter, "tun"),
Type::Tap => write!(formatter, "tap"), Type::Tap => write!(formatter, "tap")
Type::Dummy => write!(formatter, "dummy")
} }
} }
} }
@ -74,7 +70,6 @@ impl FromStr for Type {
Ok(match &text.to_lowercase() as &str { Ok(match &text.to_lowercase() as &str {
"tun" => Self::Tun, "tun" => Self::Tun,
"tap" => Self::Tap, "tap" => Self::Tap,
"dummy" => Self::Dummy,
_ => return Err("Unknown device type") _ => return Err("Unknown device type")
}) })
} }
@ -144,14 +139,10 @@ impl TunTapDevice {
#[allow(clippy::useless_conversion)] #[allow(clippy::useless_conversion)]
pub fn new(ifname: &str, type_: Type, path: Option<&str>) -> io::Result<Self> { pub fn new(ifname: &str, type_: Type, path: Option<&str>) -> io::Result<Self> {
let path = path.unwrap_or_else(|| Self::default_path(type_)); let path = path.unwrap_or_else(|| Self::default_path(type_));
if type_ == Type::Dummy {
return Self::dummy(ifname, path, type_)
}
let fd = fs::OpenOptions::new().read(true).write(true).open(path)?; let fd = fs::OpenOptions::new().read(true).write(true).open(path)?;
let flags = match type_ { let flags = match type_ {
Type::Tun => libc::IFF_TUN | libc::IFF_NO_PI, Type::Tun => libc::IFF_TUN | libc::IFF_NO_PI,
Type::Tap => libc::IFF_TAP | libc::IFF_NO_PI, Type::Tap => libc::IFF_TAP | libc::IFF_NO_PI
Type::Dummy => unreachable!()
}; };
let mut ifreq = IfReq::new(ifname); let mut ifreq = IfReq::new(ifname);
ifreq.data.flags = flags as libc::c_short; ifreq.data.flags = flags as libc::c_short;
@ -172,33 +163,10 @@ impl TunTapDevice {
#[inline] #[inline]
pub fn default_path(type_: Type) -> &'static str { pub fn default_path(type_: Type) -> &'static str {
match type_ { match type_ {
Type::Tun | Type::Tap => "/dev/net/tun", Type::Tun | Type::Tap => "/dev/net/tun"
Type::Dummy => "/dev/null"
} }
} }
/// Creates a dummy device based on an existing file
///
/// This method opens a regular or special file and reads from it to receive packets and
/// writes to it to send packets. This method does not use a networking device and therefore
/// can be used for testing.
///
/// The parameter `path` is the file that should be used. Special files like `/dev/null`,
/// named pipes and unix sockets can be used with this method.
///
/// Both `ifname` and `type_` parameters have no effect.
///
/// # Errors
/// This method will return an error if the file can not be opened for reading and writing.
#[allow(dead_code)]
pub fn dummy(ifname: &str, path: &str, type_: Type) -> io::Result<Self> {
Ok(TunTapDevice {
fd: fs::OpenOptions::new().create(true).read(true).write(true).open(path)?,
ifname: ifname.to_string(),
type_
})
}
#[cfg(any(target_os = "linux", target_os = "android"))] #[cfg(any(target_os = "linux", target_os = "android"))]
#[inline] #[inline]
fn correct_data_after_read(&mut self, _buffer: &mut MsgBuffer) {} fn correct_data_after_read(&mut self, _buffer: &mut MsgBuffer) {}
@ -255,7 +223,7 @@ impl TunTapDevice {
+ 1 /* message type header */ + 1 /* message type header */
+ match self.type_ { + match self.type_ {
Type::Tap => 14, /* inner ethernet header */ Type::Tap => 14, /* inner ethernet header */
Type::Tun | Type::Dummy => 0 Type::Tun => 0
} }
} }
@ -357,7 +325,7 @@ impl MockDevice {
impl Device for MockDevice { impl Device for MockDevice {
fn get_type(&self) -> Type { fn get_type(&self) -> Type {
Type::Dummy Type::Tun
} }
fn ifname(&self) -> &str { fn ifname(&self) -> &str {

View File

@ -286,7 +286,6 @@ fn main() {
debug!("Config: {:?}", config); debug!("Config: {:?}", config);
match config.device_type { match config.device_type {
Type::Tap => run::<payload::Frame>(config), Type::Tap => run::<payload::Frame>(config),
Type::Tun => run::<payload::Packet>(config), Type::Tun => run::<payload::Packet>(config)
Type::Dummy => run::<payload::Frame>(config)
} }
} }

View File

@ -6,7 +6,7 @@ use crate::device::Device;
use std::{io, os::unix::io::RawFd}; use std::{io, os::unix::io::RawFd};
use super::WaitResult; use super::WaitResult;
use crate::{device::Type, net::Socket}; use crate::net::Socket;
pub struct EpollWait { pub struct EpollWait {
poll_fd: RawFd, poll_fd: RawFd,
@ -31,15 +31,10 @@ impl EpollWait {
if poll_fd == -1 { if poll_fd == -1 {
return Err(io::Error::last_os_error()) return Err(io::Error::last_os_error())
} }
let raw_fds = if device.get_type() != Type::Dummy { for fd in &[socket.as_raw_fd(), device.as_raw_fd()] {
vec![socket.as_raw_fd(), device.as_raw_fd()] event.u64 = *fd as u64;
} else {
vec![socket.as_raw_fd()]
};
for fd in raw_fds {
event.u64 = fd as u64;
event.events = flags; event.events = flags;
let res = unsafe { libc::epoll_ctl(poll_fd, libc::EPOLL_CTL_ADD, fd, &mut event) }; let res = unsafe { libc::epoll_ctl(poll_fd, libc::EPOLL_CTL_ADD, *fd, &mut event) };
if res == -1 { if res == -1 {
return Err(io::Error::last_os_error()) return Err(io::Error::last_os_error())
} }