Skip to content

Commit 085d6a0

Browse files
committed
test: improve test stability
Drain tap device on init to prevent reading some initial packets. Signed-off-by: ihciah <[email protected]>
1 parent 8e7332e commit 085d6a0

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,13 @@ pub fn if_index(tap: &Tap) -> i32 {
270270
unsafe { ifreq.ifr_ifru.ifru_ivalue }
271271
}
272272

273+
274+
pub fn drain_tap(tap: &Tap) {
275+
let mut buf = [0u8; 1024];
276+
// SAFETY: The call is safe since the parameters are valid.
277+
while unsafe { libc::read(tap.as_raw_fd(), buf.as_mut_ptr().cast(), buf.len()) != -1 } {}
278+
}
279+
273280
/// Enable the tap interface.
274281
pub fn enable(tap: &Tap) {
275282
// Disable IPv6 router advertisment requests
@@ -294,6 +301,8 @@ pub fn enable(tap: &Tap) {
294301
)
295302
.execute(&sock, c_ulong::from(super::gen::sockios::SIOCSIFFLAGS))
296303
.unwrap();
304+
// Drain the initial packets that might be in the tap interface.
305+
drain_tap(tap);
297306
}
298307

299308
#[cfg(test)]

0 commit comments

Comments
 (0)