5
5
// Use of this source code is governed by a BSD-style license that can be
6
6
// found in the THIRD-PARTY file.
7
7
8
- #[ cfg( not( test) ) ]
9
8
use std:: io:: Read ;
10
9
use std:: mem;
11
10
use std:: net:: Ipv4Addr ;
@@ -716,12 +715,10 @@ impl Net {
716
715
self . tx_rate_limiter . update_buckets ( tx_bytes, tx_ops) ;
717
716
}
718
717
719
- #[ cfg( not( test) ) ]
720
718
fn read_tap ( & mut self ) -> std:: io:: Result < usize > {
721
719
self . tap . read ( & mut self . rx_frame_buf )
722
720
}
723
721
724
- #[ cfg( not( test) ) ]
725
722
fn write_tap ( tap : & mut Tap , buf : & IoVecBuffer ) -> std:: io:: Result < usize > {
726
723
tap. write_iovec ( buf)
727
724
}
@@ -932,11 +929,11 @@ impl VirtioDevice for Net {
932
929
#[ cfg( test) ]
933
930
#[ macro_use]
934
931
pub mod tests {
935
- use std:: io:: Read ;
936
932
use std:: net:: Ipv4Addr ;
933
+ use std:: os:: fd:: AsRawFd ;
937
934
use std:: str:: FromStr ;
938
935
use std:: time:: Duration ;
939
- use std:: { io , mem, thread} ;
936
+ use std:: { mem, thread} ;
940
937
941
938
use utils:: net:: mac:: { MacAddr , MAC_ADDR_LEN } ;
942
939
@@ -949,8 +946,8 @@ pub mod tests {
949
946
} ;
950
947
use crate :: devices:: virtio:: net:: test_utils:: test:: TestHelper ;
951
948
use crate :: devices:: virtio:: net:: test_utils:: {
952
- default_net, if_index, inject_tap_tx_frame, set_mac, NetEvent , NetQueue , ReadTapMock ,
953
- TapTrafficSimulator , WriteTapMock ,
949
+ default_net, if_index, inject_tap_tx_frame, set_mac, NetEvent , NetQueue ,
950
+ TapTrafficSimulator ,
954
951
} ;
955
952
use crate :: devices:: virtio:: net:: NET_QUEUE_SIZES ;
956
953
use crate :: devices:: virtio:: queue:: VIRTQ_DESC_F_WRITE ;
@@ -961,28 +958,6 @@ pub mod tests {
961
958
use crate :: rate_limiter:: { BucketUpdate , RateLimiter , TokenBucket , TokenType } ;
962
959
use crate :: vstate:: memory:: { Address , GuestMemory } ;
963
960
964
- impl Net {
965
- pub ( crate ) fn read_tap ( & mut self ) -> io:: Result < usize > {
966
- match & self . tap . mocks . read_tap {
967
- ReadTapMock :: Failure => Err ( io:: Error :: new (
968
- io:: ErrorKind :: Other ,
969
- "Read tap synthetically failed." ,
970
- ) ) ,
971
- ReadTapMock :: TapFrame => self . tap . read ( & mut self . rx_frame_buf ) ,
972
- }
973
- }
974
-
975
- pub ( crate ) fn write_tap ( tap : & mut Tap , buf : & IoVecBuffer ) -> io:: Result < usize > {
976
- match tap. mocks . write_tap {
977
- WriteTapMock :: Success => tap. write_iovec ( buf) ,
978
- WriteTapMock :: Failure => Err ( io:: Error :: new (
979
- io:: ErrorKind :: Other ,
980
- "Write tap mock failure." ,
981
- ) ) ,
982
- }
983
- }
984
- }
985
-
986
961
#[ test]
987
962
fn test_vnet_helpers ( ) {
988
963
let mut frame_buf = vec ! [ 42u8 ; vnet_hdr_len( ) - 1 ] ;
@@ -1212,7 +1187,6 @@ pub mod tests {
1212
1187
fn test_rx_retry ( ) {
1213
1188
let mut th = TestHelper :: get_default ( ) ;
1214
1189
th. activate_net ( ) ;
1215
- th. net ( ) . tap . mocks . set_read_tap ( ReadTapMock :: TapFrame ) ;
1216
1190
1217
1191
// Add invalid descriptor chain - read only descriptor.
1218
1192
th. add_desc_chain (
@@ -1263,7 +1237,6 @@ pub mod tests {
1263
1237
fn test_rx_complex_desc_chain ( ) {
1264
1238
let mut th = TestHelper :: get_default ( ) ;
1265
1239
th. activate_net ( ) ;
1266
- th. net ( ) . tap . mocks . set_read_tap ( ReadTapMock :: TapFrame ) ;
1267
1240
1268
1241
// Create a valid Rx avail descriptor chain with multiple descriptors.
1269
1242
th. add_desc_chain (
@@ -1302,7 +1275,6 @@ pub mod tests {
1302
1275
fn test_rx_multiple_frames ( ) {
1303
1276
let mut th = TestHelper :: get_default ( ) ;
1304
1277
th. activate_net ( ) ;
1305
- th. net ( ) . tap . mocks . set_read_tap ( ReadTapMock :: TapFrame ) ;
1306
1278
1307
1279
// Create 2 valid Rx avail descriptor chains. Each one has enough space to fit the
1308
1280
// following 2 frames. But only 1 frame has to be written to each chain.
@@ -1525,7 +1497,9 @@ pub mod tests {
1525
1497
fn test_tx_tap_failure ( ) {
1526
1498
let mut th = TestHelper :: get_default ( ) ;
1527
1499
th. activate_net ( ) ;
1528
- th. net ( ) . tap . mocks . set_write_tap ( WriteTapMock :: Failure ) ;
1500
+ // force the next write to the tap to return an error by simply closing the fd
1501
+ // SAFETY: its a valid fd
1502
+ unsafe { libc:: close ( th. net . lock ( ) . unwrap ( ) . tap . as_raw_fd ( ) ) } ;
1529
1503
1530
1504
let desc_list = [ ( 0 , 1000 , 0 ) ] ;
1531
1505
th. add_desc_chain ( NetQueue :: Tx , 0 , & desc_list) ;
@@ -1725,7 +1699,9 @@ pub mod tests {
1725
1699
fn test_read_tap_fail_event_handler ( ) {
1726
1700
let mut th = TestHelper :: get_default ( ) ;
1727
1701
th. activate_net ( ) ;
1728
- th. net ( ) . tap . mocks . set_read_tap ( ReadTapMock :: Failure ) ;
1702
+ // force the next write to the tap to return an error by simply closing the fd
1703
+ // SAFETY: its a valid fd
1704
+ unsafe { libc:: close ( th. net . lock ( ) . unwrap ( ) . tap . as_raw_fd ( ) ) } ;
1729
1705
1730
1706
// The RX queue is empty and rx_deffered_frame is set.
1731
1707
th. net ( ) . rx_deferred_frame = true ;
@@ -1753,7 +1729,6 @@ pub mod tests {
1753
1729
fn test_deferred_frame ( ) {
1754
1730
let mut th = TestHelper :: get_default ( ) ;
1755
1731
th. activate_net ( ) ;
1756
- th. net ( ) . tap . mocks . set_read_tap ( ReadTapMock :: TapFrame ) ;
1757
1732
1758
1733
let rx_packets_count = th. net ( ) . metrics . rx_packets_count . count ( ) ;
1759
1734
let _ = inject_tap_tx_frame ( & th. net ( ) , 1000 ) ;
0 commit comments