@@ -12,11 +12,12 @@ use std::str::FromStr;
1212use std:: sync:: atomic:: { AtomicUsize , Ordering } ;
1313use std:: sync:: { Arc , Mutex } ;
1414
15- use crate :: devices:: virtio:: net:: Net ;
15+ use crate :: devices:: virtio:: net:: device :: NetDevBackendType ;
1616#[ cfg( test) ]
1717use crate :: devices:: virtio:: net:: device:: vnet_hdr_len;
1818use 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} ;
2021use crate :: devices:: virtio:: queue:: Queue ;
2122use crate :: devices:: virtio:: test_utils:: VirtQueue ;
2223use 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
99112impl 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