Skip to content

Commit dcb63eb

Browse files
committed
refactor(tests): adapt the net test utils to the backend changes
- Make the TapTrafficSimulator take a NetDevBackend rather than a tap_index and make it call if_index internally if the device passed is a tap, panic otherwise. - Make the enable function take the bytes representing the tap name since this is all it needs from the Tap struct. Signed-off-by: aerosouund <aerosound161@gmail.com>
1 parent 2cf7400 commit dcb63eb

2 files changed

Lines changed: 31 additions & 9 deletions

File tree

src/vmm/src/device_manager/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,7 @@ pub(crate) mod tests {
776776
use crate::devices::acpi::vmclock::VmClock;
777777
use crate::devices::acpi::vmgenid::VmGenId;
778778
use crate::devices::virtio::block::CacheType;
779+
use crate::devices::virtio::net::device::NetDevBackendType;
779780
use crate::rpc_interface::VmmActionError;
780781
use crate::vmm_config::HotplugDeviceConfig;
781782
use crate::vmm_config::drive::{BlockDeviceConfig, DriveError};
@@ -1030,6 +1031,7 @@ pub(crate) mod tests {
10301031
guest_mac: Some(mac.parse().unwrap()),
10311032
rx_rate_limiter: None,
10321033
tx_rate_limiter: None,
1034+
backend_type: NetDevBackendType::Tap("hostname".to_string()),
10331035
});
10341036
vmm.hotplug_device(cfg, &mut evt_manager).unwrap();
10351037
assert!(
@@ -1046,6 +1048,7 @@ pub(crate) mod tests {
10461048
guest_mac: Some(mac.parse().unwrap()),
10471049
rx_rate_limiter: None,
10481050
tx_rate_limiter: None,
1051+
backend_type: NetDevBackendType::Tap("hostname".to_string()),
10491052
});
10501053
assert!(matches!(
10511054
vmm.hotplug_device(cfg2, &mut evt_manager),

src/vmm/src/devices/virtio/net/test_utils.rs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ use std::str::FromStr;
1212
use std::sync::atomic::{AtomicUsize, Ordering};
1313
use std::sync::{Arc, Mutex};
1414

15-
use crate::devices::virtio::net::Net;
15+
use crate::devices::virtio::net::device::NetDevBackendType;
1616
#[cfg(test)]
1717
use crate::devices::virtio::net::device::vnet_hdr_len;
1818
use crate::devices::virtio::net::generated::net_device_flags;
19-
use crate::devices::virtio::net::tap::{IfReqBuilder, Tap};
19+
use crate::devices::virtio::net::tap::{IfReqBuilder, NetDevBackend, PasstBackend, Tap};
20+
use crate::devices::virtio::net::{Net, tap};
2021
use crate::devices::virtio::queue::Queue;
2122
use crate::devices::virtio::test_utils::VirtQueue;
2223
use crate::mmds::data_store::Mmds;
@@ -45,13 +46,19 @@ pub fn default_net() -> Net {
4546
Some(guest_mac),
4647
RateLimiter::default(),
4748
RateLimiter::default(),
49+
NetDevBackendType::Tap("".to_string()),
4850
)
4951
.unwrap();
5052
net.configure_mmds_network_stack(
5153
MmdsNetworkStack::default_ipv4_addr(),
5254
Arc::new(Mutex::new(Mmds::default())),
5355
);
54-
enable(&net.tap);
56+
let if_name = if let NetDevBackend::Tap(tap) = &net.backend {
57+
tap.if_name
58+
} else {
59+
panic!("Expected the net device to be a TAP")
60+
};
61+
enable(&if_name);
5562

5663
net
5764
}
@@ -68,9 +75,15 @@ pub fn default_net_no_mmds() -> Net {
6875
Some(guest_mac),
6976
RateLimiter::default(),
7077
RateLimiter::default(),
78+
NetDevBackendType::Tap("".to_string()),
7179
)
7280
.unwrap();
73-
enable(&net.tap);
81+
let if_name = if let NetDevBackend::Tap(tap) = &net.backend {
82+
tap.if_name
83+
} else {
84+
panic!("Expected the net device to be a TAP")
85+
};
86+
enable(&if_name);
7487

7588
net
7689
}
@@ -97,12 +110,17 @@ pub struct TapTrafficSimulator {
97110
}
98111

99112
impl TapTrafficSimulator {
100-
pub fn new(tap_index: i32) -> Self {
113+
pub fn new(netdev: &NetDevBackend) -> Self {
101114
// Create sockaddr_ll struct.
102115
// SAFETY: sockaddr_storage has no invariants and can be safely zeroed.
103116
let mut storage: libc::sockaddr_storage = unsafe { mem::zeroed() };
104117

105118
let send_addr_ptr = &mut storage as *mut libc::sockaddr_storage;
119+
let tap_index = if let NetDevBackend::Tap(tap) = netdev {
120+
if_index(&tap)
121+
} else {
122+
panic!("Expected the net device to be a TAP")
123+
};
106124

107125
// SAFETY: `sock_addr` is a valid pointer and safe to dereference.
108126
unsafe {
@@ -222,20 +240,21 @@ pub fn if_index(tap: &Tap) -> i32 {
222240
}
223241

224242
/// Enable the tap interface.
225-
pub fn enable(tap: &Tap) {
243+
pub fn enable(tap_if_name: &[u8; 16]) {
226244
// Disable IPv6 router advertisement requests
245+
let ifname_str = String::from_utf8(tap_if_name.to_vec()).unwrap();
227246
Command::new("sh")
228247
.arg("-c")
229248
.arg(format!(
230249
"echo 0 > /proc/sys/net/ipv6/conf/{}/accept_ra",
231-
tap.if_name_as_str()
250+
ifname_str
232251
))
233252
.output()
234253
.unwrap();
235254

236255
let sock = create_socket();
237256
IfReqBuilder::new()
238-
.if_name(&tap.if_name)
257+
.if_name(tap_if_name)
239258
.flags(
240259
(net_device_flags::IFF_UP
241260
| net_device_flags::IFF_RUNNING
@@ -255,7 +274,7 @@ pub(crate) fn inject_tap_tx_frame(net: &Net, len: usize) -> Vec<u8> {
255274
use std::os::unix::ffi::OsStrExt;
256275

257276
assert!(len >= vnet_hdr_len());
258-
let tap_traffic_simulator = TapTrafficSimulator::new(if_index(&net.tap));
277+
let tap_traffic_simulator = TapTrafficSimulator::new(&net.backend);
259278
let mut frame = vmm_sys_util::rand::rand_alphanumerics(len - vnet_hdr_len())
260279
.as_bytes()
261280
.to_vec();

0 commit comments

Comments
 (0)