Skip to content

Commit 5bb6f8f

Browse files
committed
refactor(tests): update unit tests to handle netdev changes
- Add the backend_type field where applicable. - Pass the netdevice backend to TapTrafficSimulator::new rather than the interface index. Signed-off-by: aerosouund <aerosound161@gmail.com>
1 parent dcb63eb commit 5bb6f8f

8 files changed

Lines changed: 47 additions & 29 deletions

File tree

src/vmm/src/builder.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,7 @@ pub(crate) mod tests {
785785
use crate::device_manager::tests::default_device_manager;
786786
use crate::devices::virtio::block::CacheType;
787787
use crate::devices::virtio::device::VirtioDeviceType;
788+
use crate::devices::virtio::net::device::NetDevBackendType;
788789
use crate::devices::virtio::rng::device::ENTROPY_DEV_ID;
789790
use crate::devices::virtio::vsock::VSOCK_DEV_ID;
790791
use crate::mmds::data_store::{Mmds, MmdsVersion};
@@ -1094,6 +1095,7 @@ pub(crate) mod tests {
10941095
guest_mac: None,
10951096
rx_rate_limiter: None,
10961097
tx_rate_limiter: None,
1098+
backend_type: NetDevBackendType::Tap("hostname".to_string()),
10971099
};
10981100

10991101
let mut cmdline = default_kernel_cmdline();

src/vmm/src/device_manager/pci_mngr.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,7 @@ mod tests {
627627
use crate::builder::tests::*;
628628
use crate::device_manager;
629629
use crate::devices::virtio::block::CacheType;
630+
use crate::devices::virtio::net::device::NetDevBackendType;
630631
use crate::mmds::data_store::MmdsVersion;
631632
use crate::resources::VmmConfig;
632633
use crate::vmm_config::balloon::BalloonDeviceConfig;
@@ -679,6 +680,7 @@ mod tests {
679680
guest_mac: None,
680681
rx_rate_limiter: None,
681682
tx_rate_limiter: None,
683+
backend_type: NetDevBackendType::Tap(String::from("hostname")),
682684
};
683685
insert_net_device_with_mmds(
684686
&mut vmm,

src/vmm/src/device_manager/persist.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,7 @@ mod tests {
619619
use crate::builder::tests::*;
620620
use crate::device_manager;
621621
use crate::devices::virtio::block::CacheType;
622+
use crate::devices::virtio::net::device::NetDevBackendType;
622623
use crate::resources::VmmConfig;
623624
use crate::vmm_config::balloon::BalloonDeviceConfig;
624625
use crate::vmm_config::entropy::EntropyDeviceConfig;
@@ -710,6 +711,7 @@ mod tests {
710711
guest_mac: None,
711712
rx_rate_limiter: None,
712713
tx_rate_limiter: None,
714+
backend_type: NetDevBackendType::Tap("hostname".to_string()),
713715
};
714716
insert_net_device_with_mmds(
715717
&mut vmm,

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,7 +1669,7 @@ pub mod tests {
16691669
let mem = single_region_mem(2 * MAX_BUFFER_SIZE);
16701670
let mut th = TestHelper::get_default(&mem);
16711671
th.activate_net();
1672-
let tap_traffic_simulator = TapTrafficSimulator::new(if_index(&th.net().tap));
1672+
let tap_traffic_simulator = TapTrafficSimulator::new(&th.net().backend);
16731673

16741674
th.add_desc_chain(NetQueue::Tx, 0, &[(0, 4096, 0)]);
16751675
th.net().queue_evts[TX_INDEX].read().unwrap();
@@ -1690,7 +1690,7 @@ pub mod tests {
16901690
let mem = single_region_mem(2 * MAX_BUFFER_SIZE);
16911691
let mut th = TestHelper::get_default(&mem);
16921692
th.activate_net();
1693-
let tap_traffic_simulator = TapTrafficSimulator::new(if_index(&th.net().tap));
1693+
let tap_traffic_simulator = TapTrafficSimulator::new(&th.net().backend);
16941694

16951695
let desc_list = [(0, 100, 0), (1, 100, VIRTQ_DESC_F_WRITE), (2, 500, 0)];
16961696
th.add_desc_chain(NetQueue::Tx, 0, &desc_list);
@@ -1714,7 +1714,7 @@ pub mod tests {
17141714
let mem = single_region_mem(2 * MAX_BUFFER_SIZE);
17151715
let mut th = TestHelper::get_default(&mem);
17161716
th.activate_net();
1717-
let tap_traffic_simulator = TapTrafficSimulator::new(if_index(&th.net().tap));
1717+
let tap_traffic_simulator = TapTrafficSimulator::new(&th.net().backend);
17181718

17191719
// Send an invalid frame (too small, VNET header missing).
17201720
th.add_desc_chain(NetQueue::Tx, 0, &[(0, 1, 0)]);
@@ -1741,7 +1741,7 @@ pub mod tests {
17411741
let mem = single_region_mem(2 * MAX_BUFFER_SIZE);
17421742
let mut th = TestHelper::get_default(&mem);
17431743
th.activate_net();
1744-
let tap_traffic_simulator = TapTrafficSimulator::new(if_index(&th.net().tap));
1744+
let tap_traffic_simulator = TapTrafficSimulator::new(&th.net().backend);
17451745

17461746
// Send an invalid frame (too big, maximum buffer is MAX_BUFFER_SIZE).
17471747
th.add_desc_chain(
@@ -1772,7 +1772,7 @@ pub mod tests {
17721772
let mem = single_region_mem(2 * MAX_BUFFER_SIZE);
17731773
let mut th = TestHelper::get_default(&mem);
17741774
th.activate_net();
1775-
let tap_traffic_simulator = TapTrafficSimulator::new(if_index(&th.net().tap));
1775+
let tap_traffic_simulator = TapTrafficSimulator::new(&th.net().backend);
17761776

17771777
// Send an invalid frame (too small, VNET header missing).
17781778
th.add_desc_chain(NetQueue::Tx, 0, &[(0, 0, 0)]);
@@ -1799,7 +1799,7 @@ pub mod tests {
17991799
let mem = single_region_mem(2 * MAX_BUFFER_SIZE);
18001800
let mut th = TestHelper::get_default(&mem);
18011801
th.activate_net();
1802-
let tap_traffic_simulator = TapTrafficSimulator::new(if_index(&th.net().tap));
1802+
let tap_traffic_simulator = TapTrafficSimulator::new(&th.net().backend);
18031803

18041804
// Add invalid descriptor chain - writeable descriptor.
18051805
th.add_desc_chain(
@@ -1846,7 +1846,7 @@ pub mod tests {
18461846
let mem = single_region_mem(2 * MAX_BUFFER_SIZE);
18471847
let mut th = TestHelper::get_default(&mem);
18481848
th.activate_net();
1849-
let tap_traffic_simulator = TapTrafficSimulator::new(if_index(&th.net().tap));
1849+
let tap_traffic_simulator = TapTrafficSimulator::new(&th.net().backend);
18501850

18511851
// Add gaps between the descriptor ids in order to ensure that we follow
18521852
// the `next` field.
@@ -1881,7 +1881,7 @@ pub mod tests {
18811881
th.activate_net();
18821882
// force the next write to the tap to return an error by simply closing the fd
18831883
// SAFETY: its a valid fd
1884-
unsafe { libc::close(th.net.lock().unwrap().tap.as_raw_fd()) };
1884+
unsafe { libc::close(th.net.lock().unwrap().backend.as_raw_fd()) };
18851885

18861886
let desc_list = [(0, 1000, 0)];
18871887
th.add_desc_chain(NetQueue::Tx, 0, &desc_list);
@@ -1911,7 +1911,7 @@ pub mod tests {
19111911
let mem = single_region_mem(2 * MAX_BUFFER_SIZE);
19121912
let mut th = TestHelper::get_default(&mem);
19131913
th.activate_net();
1914-
let tap_traffic_simulator = TapTrafficSimulator::new(if_index(&th.net().tap));
1914+
let tap_traffic_simulator = TapTrafficSimulator::new(&th.net().backend);
19151915

19161916
// Write the first frame to the Tx queue
19171917
let desc_list = [(0, 50, 0), (1, 100, 0), (2, 150, 0)];
@@ -2022,7 +2022,7 @@ pub mod tests {
20222022
&mut net.tx_rate_limiter,
20232023
&mut headers,
20242024
&buffer,
2025-
&mut net.tap,
2025+
&mut net.backend,
20262026
Some(src_mac),
20272027
&net.metrics,
20282028
)
@@ -2061,7 +2061,7 @@ pub mod tests {
20612061
&mut net.tx_rate_limiter,
20622062
&mut headers,
20632063
&buffer,
2064-
&mut net.tap,
2064+
&mut net.backend,
20652065
Some(guest_mac),
20662066
&net.metrics,
20672067
)
@@ -2076,7 +2076,7 @@ pub mod tests {
20762076
&mut net.tx_rate_limiter,
20772077
&mut headers,
20782078
&buffer,
2079-
&mut net.tap,
2079+
&mut net.backend,
20802080
Some(not_guest_mac),
20812081
&net.metrics,
20822082
)
@@ -2116,7 +2116,7 @@ pub mod tests {
21162116
th.activate_net();
21172117
// force the next write to the tap to return an error by simply closing the fd
21182118
// SAFETY: its a valid fd
2119-
unsafe { libc::close(th.net.lock().unwrap().tap.as_raw_fd()) };
2119+
unsafe { libc::close(th.net.lock().unwrap().backend.as_raw_fd()) };
21202120

21212121
// The RX queue is empty and there is a deferred frame.
21222122
th.net().rx_buffer.used_descriptors = 1;

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub mod device;
2121
mod event_handler;
2222
pub mod metrics;
2323
pub mod persist;
24-
mod tap;
24+
pub mod tap;
2525
pub mod test_utils;
2626

2727
mod generated;
@@ -47,6 +47,8 @@ pub enum NetQueue {
4747
pub enum NetError {
4848
/// Open tap device failed: {0}
4949
TapOpen(TapError),
50+
/// Socket backend opening failed
51+
SocketOpen(),
5052
/// Setting vnet header size failed: {0}
5153
TapSetVnetHdrSize(TapError),
5254
/// EventFd error: {0}

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -505,17 +505,19 @@ pub mod tests {
505505

506506
#[test]
507507
fn test_write_iovec() {
508-
let mut tap = Tap::open_named("").unwrap();
509-
enable(&tap);
510-
let tap_traffic_simulator = TapTrafficSimulator::new(if_index(&tap));
508+
let tap_dev = Tap::open_named("").unwrap();
509+
let if_name = tap_dev.if_name;
510+
let mut tap = NetDevBackend::Tap(Tap::open_named("").unwrap());
511+
enable(&if_name);
512+
let tap_traffic_simulator = TapTrafficSimulator::new(&tap);
511513

512514
let mut fragment1 = vmm_sys_util::rand::rand_bytes(PAYLOAD_SIZE);
513515
fragment1.as_mut_slice()[..generated::ETH_HLEN as usize]
514516
.copy_from_slice(&[0; generated::ETH_HLEN as usize]);
515517
let fragment2 = vmm_sys_util::rand::rand_bytes(PAYLOAD_SIZE);
516518
let fragment3 = vmm_sys_util::rand::rand_bytes(PAYLOAD_SIZE);
517519

518-
let scattered = IoVecBuffer::from(vec![
520+
let mut scattered = IoVecBuffer::from(vec![
519521
fragment1.as_slice(),
520522
fragment2.as_slice(),
521523
fragment3.as_slice(),
@@ -542,9 +544,12 @@ pub mod tests {
542544

543545
#[test]
544546
fn test_read_iovec() {
545-
let mut tap = Tap::open_named("").unwrap();
546-
enable(&tap);
547-
let tap_traffic_simulator = TapTrafficSimulator::new(if_index(&tap));
547+
let tap_dev = Tap::open_named("").unwrap();
548+
let if_name = tap_dev.if_name;
549+
let mut tap = NetDevBackend::Tap(Tap::open_named("").unwrap());
550+
enable(&if_name);
551+
552+
let tap_traffic_simulator = TapTrafficSimulator::new(&tap);
548553

549554
let mut buff1 = vec![0; PAYLOAD_SIZE + VNET_HDR_SIZE];
550555
let mut buff2 = vec![0; 2 * PAYLOAD_SIZE];

src/vmm/src/resources.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ mod tests {
574574
use crate::devices::virtio::block::virtio::VirtioBlockError;
575575
use crate::devices::virtio::block::{BlockError, CacheType};
576576
use crate::devices::virtio::device::VirtioDevice;
577+
use crate::devices::virtio::net::device::NetDevBackendType;
577578
use crate::devices::virtio::vsock::VSOCK_DEV_ID;
578579
use crate::resources::VmResources;
579580
use crate::utils::net::mac::MacAddr;
@@ -587,19 +588,21 @@ mod tests {
587588
use crate::vmm_config::vsock::tests::default_config;
588589

589590
fn default_net_cfg() -> NetworkInterfaceConfig {
591+
// TempFile::new_with_prefix("") generates a random file name used as random net_if
592+
// name.
593+
let dev_name = TempFile::new_with_prefix("")
594+
.unwrap()
595+
.as_path()
596+
.to_str()
597+
.unwrap()
598+
.to_string();
590599
NetworkInterfaceConfig {
591600
iface_id: "net_if1".to_string(),
592-
// TempFile::new_with_prefix("") generates a random file name used as random net_if
593-
// name.
594-
host_dev_name: TempFile::new_with_prefix("")
595-
.unwrap()
596-
.as_path()
597-
.to_str()
598-
.unwrap()
599-
.to_string(),
601+
host_dev_name: dev_name.clone(),
600602
guest_mac: Some(MacAddr::from_str("01:23:45:67:89:0a").unwrap()),
601603
rx_rate_limiter: Some(RateLimiterConfig::default()),
602604
tx_rate_limiter: Some(RateLimiterConfig::default()),
605+
backend_type: NetDevBackendType::Tap(dev_name),
603606
}
604607
}
605608

src/vmm/tests/integration_tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::time::Duration;
1010

1111
use vmm::builder::build_and_boot_microvm;
1212
use vmm::devices::virtio::block::CacheType;
13+
use vmm::devices::virtio::net::device::NetDevBackendType;
1314
use vmm::persist::{MicrovmState, MicrovmStateError, VmInfo, snapshot_state_sanity_check};
1415
use vmm::resources::VmResources;
1516
use vmm::rpc_interface::{
@@ -442,6 +443,7 @@ fn test_preboot_load_snap_disallowed_after_boot_resources() {
442443
guest_mac: None,
443444
rx_rate_limiter: None,
444445
tx_rate_limiter: None,
446+
backend_type: NetDevBackendType::Tap(String::new()),
445447
});
446448
verify_load_snap_disallowed_after_boot_resources(req, "InsertNetworkDevice");
447449

0 commit comments

Comments
 (0)