Fix a potential indefinite blocking in packetimpact testbench

1. setsockopt(SO_RCVTIMEO, 0) == never timeout
2. float64(time.Microsecond/time.Second) == 0
3. packetimpact tests use a lot of 1s timeouts

This becomes a more significant problem because of a recent change that binds
the sniffer only on the specific testNet interface so now the traffic on the
ctrlNet cannot wake up the blocking call anymore.

PiperOrigin-RevId: 344123465
This commit is contained in:
Zeling Feng
2020-11-24 13:42:07 -08:00
committed by gVisor bot
parent 732e989855
commit d492b21319
+8 -3
View File
@@ -74,7 +74,8 @@ func (n *DUTTestNet) NewSniffer(t *testing.T) (Sniffer, error) {
// packet too large for the buffer arrives, the test will get a fatal error.
const maxReadSize int = 65536
// Recv tries to read one frame until the timeout is up.
// Recv tries to read one frame until the timeout is up. If the timeout given
// is 0, then no read attempt will be made.
func (s *Sniffer) Recv(t *testing.T, timeout time.Duration) []byte {
t.Helper()
@@ -87,9 +88,13 @@ func (s *Sniffer) Recv(t *testing.T, timeout time.Duration) []byte {
whole, frac := math.Modf(timeout.Seconds())
tv := unix.Timeval{
Sec: int64(whole),
Usec: int64(frac * float64(time.Microsecond/time.Second)),
Usec: int64(frac * float64(time.Second/time.Microsecond)),
}
// The following should never happen, but having this guard here is better
// than blocking indefinitely in the future.
if tv.Sec == 0 && tv.Usec == 0 {
t.Fatal("setting SO_RCVTIMEO to 0 means blocking indefinitely")
}
if err := unix.SetsockoptTimeval(s.fd, unix.SOL_SOCKET, unix.SO_RCVTIMEO, &tv); err != nil {
t.Fatalf("can't setsockopt SO_RCVTIMEO: %s", err)
}