gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/packetimpact/tests/tcp_zero_window_probe_usertimeout_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_usertimeout_test
    16  
    17  import (
    18  	"flag"
    19  	"testing"
    20  	"time"
    21  
    22  	"golang.org/x/sys/unix"
    23  	"gvisor.dev/gvisor/pkg/tcpip/header"
    24  	"gvisor.dev/gvisor/test/packetimpact/testbench"
    25  )
    26  
    27  func init() {
    28  	testbench.Initialize(flag.CommandLine)
    29  }
    30  
    31  // TestZeroWindowProbeUserTimeout sanity tests user timeout when we are
    32  // retransmitting zero window probes.
    33  func TestZeroWindowProbeUserTimeout(t *testing.T) {
    34  	dut := testbench.NewDUT(t)
    35  	listenFd, remotePort := dut.CreateListener(t, unix.SOCK_STREAM, unix.IPPROTO_TCP, 1)
    36  	defer dut.Close(t, listenFd)
    37  	conn := dut.Net.NewTCPIPv4(t, testbench.TCP{DstPort: &remotePort}, testbench.TCP{SrcPort: &remotePort})
    38  	defer conn.Close(t)
    39  
    40  	conn.Connect(t)
    41  	acceptFd, _ := dut.Accept(t, listenFd)
    42  	defer dut.Close(t, acceptFd)
    43  
    44  	dut.SetSockOptInt(t, acceptFd, unix.IPPROTO_TCP, unix.TCP_NODELAY, 1)
    45  
    46  	sampleData := []byte("Sample Data")
    47  	samplePayload := &testbench.Payload{Bytes: sampleData}
    48  
    49  	// Send and receive sample data to the dut.
    50  	dut.Send(t, acceptFd, sampleData, 0)
    51  	if _, err := conn.ExpectData(t, &testbench.TCP{}, samplePayload, time.Second); err != nil {
    52  		t.Fatalf("expected payload was not received: %s", err)
    53  	}
    54  	conn.Send(t, testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagAck | header.TCPFlagPsh)}, samplePayload)
    55  	if _, err := conn.ExpectData(t, &testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagAck)}, nil, time.Second); err != nil {
    56  		t.Fatalf("expected packet was not received: %s", err)
    57  	}
    58  
    59  	// Test 1: Check for receive of a zero window probe, record the duration for
    60  	//         probe to be sent.
    61  	//
    62  	// Advertize zero window to the dut.
    63  	conn.Send(t, testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagAck), WindowSize: testbench.Uint16(0)})
    64  
    65  	// Expected sequence number of the zero window probe.
    66  	probeSeq := testbench.Uint32(uint32(*conn.RemoteSeqNum(t) - 1))
    67  	start := time.Now()
    68  	// Ask the dut to send out data.
    69  	dut.Send(t, acceptFd, sampleData, 0)
    70  	// Expect zero-window probe from the dut.
    71  	if _, err := conn.ExpectData(t, &testbench.TCP{SeqNum: probeSeq}, nil, time.Second); err != nil {
    72  		t.Fatalf("expected a packet with sequence number %d: %s", probeSeq, err)
    73  	}
    74  	// Record the duration for first probe, the dut sends the zero window probe after
    75  	// a retransmission time interval.
    76  	startProbeDuration := time.Now().Sub(start)
    77  
    78  	// Test 2: Check if the dut times out the connection by honoring usertimeout
    79  	//         when the dut is sending zero-window probes.
    80  	//
    81  	// Reduce the retransmit timeout.
    82  	dut.SetSockOptInt(t, acceptFd, unix.IPPROTO_TCP, unix.TCP_USER_TIMEOUT, int32(startProbeDuration.Milliseconds()))
    83  	// Advertize zero window again.
    84  	conn.Send(t, testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagAck), WindowSize: testbench.Uint16(0)})
    85  	// Ask the dut to send out data that would trigger zero window probe retransmissions.
    86  	dut.Send(t, acceptFd, sampleData, 0)
    87  
    88  	// Wait for the connection to timeout after multiple zero-window probe retransmissions.
    89  	time.Sleep(8 * startProbeDuration)
    90  
    91  	// Expect the connection to have timed out and closed which would cause the dut
    92  	// to reply with a RST to the ACK we send.
    93  	conn.Send(t, testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagAck)})
    94  	if _, err := conn.ExpectData(t, &testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagRst)}, nil, time.Second); err != nil {
    95  		t.Fatalf("expected a TCP RST")
    96  	}
    97  }