gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/packetimpact/tests/tcp_zero_window_probe_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_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  // TestZeroWindowProbe tests few cases of zero window probing over the
    32  // same connection.
    33  func TestZeroWindowProbe(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  	start := time.Now()
    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  	sendTime := time.Now().Sub(start)
    56  	conn.Send(t, testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagAck | header.TCPFlagPsh)}, samplePayload)
    57  	if _, err := conn.ExpectData(t, &testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagAck)}, nil, time.Second); err != nil {
    58  		t.Fatalf("expected packet was not received: %s", err)
    59  	}
    60  
    61  	// Test 1: Check for receive of a zero window probe, record the duration for
    62  	//         probe to be sent.
    63  	//
    64  	// Advertize zero window to the dut.
    65  	conn.Send(t, testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagAck), WindowSize: testbench.Uint16(0)})
    66  
    67  	// Expected sequence number of the zero window probe.
    68  	probeSeq := testbench.Uint32(uint32(*conn.RemoteSeqNum(t) - 1))
    69  	// Expected ack number of the ACK for the probe.
    70  	ackProbe := testbench.Uint32(uint32(*conn.RemoteSeqNum(t)))
    71  
    72  	// Expect there are no zero-window probes sent until there is data to be sent out
    73  	// from the dut.
    74  	if _, err := conn.ExpectData(t, &testbench.TCP{SeqNum: probeSeq}, nil, 2*time.Second); err == nil {
    75  		t.Fatalf("unexpected packet with sequence number %d: %s", probeSeq, err)
    76  	}
    77  
    78  	start = time.Now()
    79  	// Ask the dut to send out data.
    80  	dut.Send(t, acceptFd, sampleData, 0)
    81  	// Expect zero-window probe from the dut.
    82  	if _, err := conn.ExpectData(t, &testbench.TCP{SeqNum: probeSeq}, nil, time.Second); err != nil {
    83  		t.Fatalf("expected a packet with sequence number %d: %s", probeSeq, err)
    84  	}
    85  	// Expect the probe to be sent after some time. Compare against the previous
    86  	// time recorded when the dut immediately sends out data on receiving the
    87  	// send command.
    88  	if startProbeDuration := time.Now().Sub(start); startProbeDuration <= sendTime {
    89  		t.Fatalf("expected the first probe to be sent out after retransmission interval, got %s want > %s", startProbeDuration, sendTime)
    90  	}
    91  
    92  	// Test 2: Check if the dut recovers on advertizing non-zero receive window.
    93  	//         and sends out the sample payload after the send window opens.
    94  	//
    95  	// Advertize non-zero window to the dut and ack the zero window probe.
    96  	conn.Send(t, testbench.TCP{AckNum: ackProbe, Flags: testbench.TCPFlags(header.TCPFlagAck)})
    97  	// Expect the dut to recover and transmit data.
    98  	if _, err := conn.ExpectData(t, &testbench.TCP{SeqNum: ackProbe}, samplePayload, time.Second); err != nil {
    99  		t.Fatalf("expected payload was not received: %s", err)
   100  	}
   101  
   102  	// Test 3: Sanity check for dut's processing of a similar probe it sent.
   103  	//         Check if the dut responds as we do for a similar probe sent to it.
   104  	//         Basically with sequence number to one byte behind the unacknowledged
   105  	//         sequence number.
   106  	p := testbench.Uint32(uint32(*conn.LocalSeqNum(t)))
   107  	conn.Send(t, testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagAck), SeqNum: testbench.Uint32(uint32(*conn.LocalSeqNum(t) - 1))})
   108  	if _, err := conn.ExpectData(t, &testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagAck), AckNum: p}, nil, time.Second); err != nil {
   109  		t.Fatalf("expected a packet with ack number: %d: %s", p, err)
   110  	}
   111  }