gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/packetimpact/tests/tcp_cork_mss_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_cork_mss_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  // TestTCPCorkMSS tests for segment coalesce and split as per MSS.
    32  func TestTCPCorkMSS(t *testing.T) {
    33  	dut := testbench.NewDUT(t)
    34  	listenFD, remotePort := dut.CreateListener(t, unix.SOCK_STREAM, unix.IPPROTO_TCP, 1)
    35  	defer dut.Close(t, listenFD)
    36  	conn := dut.Net.NewTCPIPv4(t, testbench.TCP{DstPort: &remotePort}, testbench.TCP{SrcPort: &remotePort})
    37  	defer conn.Close(t)
    38  
    39  	const mss = uint32(header.TCPDefaultMSS)
    40  	options := make([]byte, header.TCPOptionMSSLength)
    41  	header.EncodeMSSOption(mss, options)
    42  	conn.ConnectWithOptions(t, options)
    43  
    44  	acceptFD, _ := dut.Accept(t, listenFD)
    45  	defer dut.Close(t, acceptFD)
    46  
    47  	dut.SetSockOptInt(t, acceptFD, unix.IPPROTO_TCP, unix.TCP_CORK, 1)
    48  
    49  	// Let the dut application send 2 small segments to be held up and coalesced
    50  	// until the application sends a larger segment to fill up to > MSS.
    51  	sampleData := []byte("Sample Data")
    52  	dut.Send(t, acceptFD, sampleData, 0)
    53  	dut.Send(t, acceptFD, sampleData, 0)
    54  
    55  	expectedData := sampleData
    56  	expectedData = append(expectedData, sampleData...)
    57  	largeData := make([]byte, mss+1)
    58  	expectedData = append(expectedData, largeData...)
    59  	dut.Send(t, acceptFD, largeData, 0)
    60  
    61  	// Expect the segments to be coalesced and sent and capped to MSS.
    62  	expectedPayload := testbench.Payload{Bytes: expectedData[:mss]}
    63  	if _, err := conn.ExpectData(t, &testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagAck)}, &expectedPayload, time.Second); err != nil {
    64  		t.Fatalf("expected payload was not received: %s", err)
    65  	}
    66  	conn.Send(t, testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagAck)})
    67  	// Expect the coalesced segment to be split and transmitted.
    68  	expectedPayload = testbench.Payload{Bytes: expectedData[mss:]}
    69  	if _, err := conn.ExpectData(t, &testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagAck | header.TCPFlagPsh)}, &expectedPayload, time.Second); err != nil {
    70  		t.Fatalf("expected payload was not received: %s", err)
    71  	}
    72  
    73  	// Check for segments to *not* be held up because of TCP_CORK when
    74  	// the current send window is less than MSS.
    75  	conn.Send(t, testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagAck), WindowSize: testbench.Uint16(uint16(2 * len(sampleData)))})
    76  	dut.Send(t, acceptFD, sampleData, 0)
    77  	dut.Send(t, acceptFD, sampleData, 0)
    78  	expectedPayload = testbench.Payload{Bytes: append(sampleData, sampleData...)}
    79  	if _, err := conn.ExpectData(t, &testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagAck | header.TCPFlagPsh)}, &expectedPayload, time.Second); err != nil {
    80  		t.Fatalf("expected payload was not received: %s", err)
    81  	}
    82  	conn.Send(t, testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagAck)})
    83  }