gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/packetimpact/tests/tcp_retransmits_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_retransmits_test
    16  
    17  import (
    18  	"bytes"
    19  	"flag"
    20  	"testing"
    21  	"time"
    22  
    23  	"golang.org/x/sys/unix"
    24  	"gvisor.dev/gvisor/pkg/tcpip/header"
    25  	"gvisor.dev/gvisor/test/packetimpact/testbench"
    26  )
    27  
    28  func init() {
    29  	testbench.Initialize(flag.CommandLine)
    30  }
    31  
    32  func getRTO(t *testing.T, dut testbench.DUT, acceptFd int32) (rto time.Duration) {
    33  	info := dut.GetSockOptTCPInfo(t, acceptFd)
    34  	return time.Duration(info.RTO) * time.Microsecond
    35  }
    36  
    37  // TestRetransmits tests retransmits occur at exponentially increasing
    38  // time intervals.
    39  func TestRetransmits(t *testing.T) {
    40  	dut := testbench.NewDUT(t)
    41  	listenFd, remotePort := dut.CreateListener(t, unix.SOCK_STREAM, unix.IPPROTO_TCP, 1)
    42  	defer dut.Close(t, listenFd)
    43  	conn := dut.Net.NewTCPIPv4(t, testbench.TCP{DstPort: &remotePort}, testbench.TCP{SrcPort: &remotePort})
    44  	defer conn.Close(t)
    45  
    46  	conn.Connect(t)
    47  	acceptFd, _ := dut.Accept(t, listenFd)
    48  	defer dut.Close(t, acceptFd)
    49  
    50  	dut.SetSockOptInt(t, acceptFd, unix.IPPROTO_TCP, unix.TCP_NODELAY, 1)
    51  
    52  	sampleData := []byte("Sample Data")
    53  	samplePayload := &testbench.Payload{Bytes: sampleData}
    54  
    55  	// Give a chance for the dut to estimate RTO with RTT from the DATA-ACK.
    56  	// This is to reduce the test run-time from the default initial RTO of 1s.
    57  	// TODO(gvisor.dev/issue/2685) Estimate RTO during handshake, after which
    58  	// we can skip this data send/recv which is solely to estimate RTO.
    59  	dut.Send(t, acceptFd, sampleData, 0)
    60  	if _, err := conn.ExpectData(t, &testbench.TCP{}, samplePayload, time.Second); err != nil {
    61  		t.Fatalf("expected payload was not received: %s", err)
    62  	}
    63  	conn.Send(t, testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagAck | header.TCPFlagPsh)}, samplePayload)
    64  	if _, err := conn.ExpectData(t, &testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagAck)}, nil, time.Second); err != nil {
    65  		t.Fatalf("expected packet was not received: %s", err)
    66  	}
    67  	// Wait for the DUT to receive the data, thus ensuring that the stack has
    68  	// estimated RTO before we query RTO via TCP_INFO.
    69  	if got := dut.Recv(t, acceptFd, int32(len(sampleData)), 0); !bytes.Equal(got, sampleData) {
    70  		t.Fatalf("got dut.Recv(t, %d, %d, 0) = %s, want %s", acceptFd, len(sampleData), got, sampleData)
    71  	}
    72  
    73  	const timeoutCorrection = time.Second
    74  	const diffCorrection = 200 * time.Millisecond
    75  	rto := getRTO(t, dut, acceptFd)
    76  
    77  	dut.Send(t, acceptFd, sampleData, 0)
    78  	seq := testbench.Uint32(uint32(*conn.RemoteSeqNum(t)))
    79  	if _, err := conn.ExpectData(t, &testbench.TCP{SeqNum: seq}, samplePayload, rto+timeoutCorrection); err != nil {
    80  		t.Fatalf("expected payload was not received: %s", err)
    81  	}
    82  
    83  	// Expect retransmits of the same segment.
    84  	for i := 0; i < 5; i++ {
    85  		startTime := time.Now()
    86  		rto = getRTO(t, dut, acceptFd)
    87  		if _, err := conn.ExpectData(t, &testbench.TCP{SeqNum: seq}, samplePayload, rto+timeoutCorrection); err != nil {
    88  			t.Fatalf("expected payload was not received within %s loop %d err %s", rto+timeoutCorrection, i, err)
    89  		}
    90  		if diff := time.Since(startTime); diff+diffCorrection < rto {
    91  			t.Fatalf("retransmit came sooner got: %s want: >= %s probe %d", diff+diffCorrection, rto, i)
    92  		}
    93  	}
    94  }