github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/packetimpact/tests/tcp_zero_window_probe_retransmit_test.go (about)

     1  // Copyright 2020 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package tcp_zero_window_probe_retransmit_test
    16  
    17  import (
    18  	"bytes"
    19  	"flag"
    20  	"testing"
    21  	"time"
    22  
    23  	"golang.org/x/sys/unix"
    24  	"github.com/SagerNet/gvisor/pkg/tcpip/header"
    25  	"github.com/SagerNet/gvisor/test/packetimpact/testbench"
    26  )
    27  
    28  func init() {
    29  	testbench.Initialize(flag.CommandLine)
    30  }
    31  
    32  // TestZeroWindowProbeRetransmit tests retransmits of zero window probes
    33  // to be sent at exponentially inreasing time intervals.
    34  func TestZeroWindowProbeRetransmit(t *testing.T) {
    35  	dut := testbench.NewDUT(t)
    36  	listenFd, remotePort := dut.CreateListener(t, unix.SOCK_STREAM, unix.IPPROTO_TCP, 1)
    37  	defer dut.Close(t, listenFd)
    38  	conn := dut.Net.NewTCPIPv4(t, testbench.TCP{DstPort: &remotePort}, testbench.TCP{SrcPort: &remotePort})
    39  	defer conn.Close(t)
    40  
    41  	conn.Connect(t)
    42  	acceptFd, _ := dut.Accept(t, listenFd)
    43  	defer dut.Close(t, acceptFd)
    44  
    45  	dut.SetSockOptInt(t, acceptFd, unix.IPPROTO_TCP, unix.TCP_NODELAY, 1)
    46  
    47  	sampleData := []byte("Sample Data")
    48  	samplePayload := &testbench.Payload{Bytes: sampleData}
    49  
    50  	// Send and receive sample data to the dut.
    51  	dut.Send(t, acceptFd, sampleData, 0)
    52  	if _, err := conn.ExpectData(t, &testbench.TCP{}, samplePayload, time.Second); err != nil {
    53  		t.Fatalf("expected payload was not received: %s", err)
    54  	}
    55  
    56  	// Check for the dut to keep the connection alive as long as the zero window
    57  	// probes are acknowledged. Check if the zero window probes are sent at
    58  	// exponentially increasing intervals. The timeout intervals are function
    59  	// of the recorded first zero probe transmission duration.
    60  	//
    61  	// Advertize zero receive window along with a payload.
    62  	conn.Send(t, testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagAck | header.TCPFlagPsh), WindowSize: testbench.Uint16(0)}, samplePayload)
    63  	if _, err := conn.ExpectData(t, &testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagAck)}, nil, time.Second); err != nil {
    64  		t.Fatalf("expected packet was not received: %s", err)
    65  	}
    66  	// Wait for the payload to be received by the DUT, which is also an
    67  	// indication of receive of the peer window advertisement.
    68  	if got := dut.Recv(t, acceptFd, int32(len(sampleData)), 0); !bytes.Equal(got, sampleData) {
    69  		t.Fatalf("got dut.Recv(t, %d, %d, 0) = %s, want %s", acceptFd, len(sampleData), got, sampleData)
    70  	}
    71  
    72  	probeSeq := testbench.Uint32(uint32(*conn.RemoteSeqNum(t) - 1))
    73  	ackProbe := testbench.Uint32(uint32(*conn.RemoteSeqNum(t)))
    74  
    75  	// Ask the dut to send out data.
    76  	dut.Send(t, acceptFd, sampleData, 0)
    77  
    78  	var prev time.Duration
    79  	// Expect the dut to keep the connection alive as long as the remote is
    80  	// acknowledging the zero-window probes.
    81  	for i := 1; i <= 5; i++ {
    82  		start := time.Now()
    83  		// Expect zero-window probe with a timeout which is a function of the typical
    84  		// first retransmission time. The retransmission times is supposed to
    85  		// exponentially increase.
    86  		if _, err := conn.ExpectData(t, &testbench.TCP{SeqNum: probeSeq}, nil, time.Duration(i)*time.Second); err != nil {
    87  			t.Fatalf("%d: expected a probe with sequence number %d: %s", i, probeSeq, err)
    88  		}
    89  		if i == 1 {
    90  			// Skip the first probe as computing transmit time for that is
    91  			// non-deterministic because of the arbitrary time taken for
    92  			// the dut to receive a send command and issue a send.
    93  			continue
    94  		}
    95  
    96  		// Check if the time taken to receive the probe from the dut is
    97  		// increasing exponentially. To avoid flakes, use a correction
    98  		// factor for the expected duration which accounts for any
    99  		// scheduling non-determinism.
   100  		const timeCorrection = 200 * time.Millisecond
   101  		got := time.Since(start)
   102  		if want := (2 * prev) - timeCorrection; prev != 0 && got < want {
   103  			t.Errorf("got zero probe %d after %s, want >= %s", i, got, want)
   104  		}
   105  		prev = got
   106  		// Acknowledge the zero-window probes from the dut.
   107  		conn.Send(t, testbench.TCP{AckNum: ackProbe, Flags: testbench.TCPFlags(header.TCPFlagAck), WindowSize: testbench.Uint16(0)})
   108  	}
   109  	// Advertize non-zero window.
   110  	conn.Send(t, testbench.TCP{AckNum: ackProbe, Flags: testbench.TCPFlags(header.TCPFlagAck)})
   111  	// Expect the dut to recover and transmit data.
   112  	if _, err := conn.ExpectData(t, &testbench.TCP{SeqNum: ackProbe}, samplePayload, time.Second); err != nil {
   113  		t.Fatalf("expected payload was not received: %s", err)
   114  	}
   115  }