2020-05-14 04:25:28 +00:00
|
|
|
// Copyright 2020 The gVisor Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package tcp_retransmits_test
|
|
|
|
|
|
|
|
import (
|
2021-03-15 05:28:10 +00:00
|
|
|
"bytes"
|
2020-05-14 04:25:28 +00:00
|
|
|
"flag"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
2021-02-17 22:23:15 +00:00
|
|
|
"gvisor.dev/gvisor/pkg/abi/linux"
|
|
|
|
"gvisor.dev/gvisor/pkg/binary"
|
2021-03-29 20:28:32 +00:00
|
|
|
"gvisor.dev/gvisor/pkg/hostarch"
|
2020-05-14 04:25:28 +00:00
|
|
|
"gvisor.dev/gvisor/pkg/tcpip/header"
|
2020-06-04 21:22:46 +00:00
|
|
|
"gvisor.dev/gvisor/test/packetimpact/testbench"
|
2020-05-14 04:25:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2020-11-24 02:11:00 +00:00
|
|
|
testbench.Initialize(flag.CommandLine)
|
2020-05-14 04:25:28 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 22:23:15 +00:00
|
|
|
func getRTO(t *testing.T, dut testbench.DUT, acceptFd int32) (rto time.Duration) {
|
|
|
|
info := linux.TCPInfo{}
|
|
|
|
infoBytes := dut.GetSockOpt(t, acceptFd, unix.SOL_TCP, unix.TCP_INFO, int32(linux.SizeOfTCPInfo))
|
|
|
|
if got, want := len(infoBytes), linux.SizeOfTCPInfo; got != want {
|
|
|
|
t.Fatalf("unexpected size for TCP_INFO, got %d bytes want %d bytes", got, want)
|
|
|
|
}
|
2021-03-29 20:28:32 +00:00
|
|
|
binary.Unmarshal(infoBytes, hostarch.ByteOrder, &info)
|
2021-02-17 22:23:15 +00:00
|
|
|
return time.Duration(info.RTO) * time.Microsecond
|
|
|
|
}
|
|
|
|
|
2020-05-14 04:25:28 +00:00
|
|
|
// TestRetransmits tests retransmits occur at exponentially increasing
|
|
|
|
// time intervals.
|
|
|
|
func TestRetransmits(t *testing.T) {
|
2020-06-04 21:22:46 +00:00
|
|
|
dut := testbench.NewDUT(t)
|
2020-07-27 13:37:11 +00:00
|
|
|
listenFd, remotePort := dut.CreateListener(t, unix.SOCK_STREAM, unix.IPPROTO_TCP, 1)
|
|
|
|
defer dut.Close(t, listenFd)
|
2020-11-24 02:11:00 +00:00
|
|
|
conn := dut.Net.NewTCPIPv4(t, testbench.TCP{DstPort: &remotePort}, testbench.TCP{SrcPort: &remotePort})
|
2020-07-27 13:37:11 +00:00
|
|
|
defer conn.Close(t)
|
2020-05-14 04:25:28 +00:00
|
|
|
|
2020-07-27 13:37:11 +00:00
|
|
|
conn.Connect(t)
|
|
|
|
acceptFd, _ := dut.Accept(t, listenFd)
|
|
|
|
defer dut.Close(t, acceptFd)
|
2020-05-14 04:25:28 +00:00
|
|
|
|
2020-07-27 13:37:11 +00:00
|
|
|
dut.SetSockOptInt(t, acceptFd, unix.IPPROTO_TCP, unix.TCP_NODELAY, 1)
|
2020-05-14 04:25:28 +00:00
|
|
|
|
|
|
|
sampleData := []byte("Sample Data")
|
2020-06-04 21:22:46 +00:00
|
|
|
samplePayload := &testbench.Payload{Bytes: sampleData}
|
2020-05-14 04:25:28 +00:00
|
|
|
|
2021-03-15 05:28:10 +00:00
|
|
|
// Give a chance for the dut to estimate RTO with RTT from the DATA-ACK.
|
|
|
|
// This is to reduce the test run-time from the default initial RTO of 1s.
|
|
|
|
// TODO(gvisor.dev/issue/2685) Estimate RTO during handshake, after which
|
|
|
|
// we can skip this data send/recv which is solely to estimate RTO.
|
2020-07-27 13:37:11 +00:00
|
|
|
dut.Send(t, acceptFd, sampleData, 0)
|
|
|
|
if _, err := conn.ExpectData(t, &testbench.TCP{}, samplePayload, time.Second); err != nil {
|
2020-06-12 02:45:06 +00:00
|
|
|
t.Fatalf("expected payload was not received: %s", err)
|
2020-05-14 04:25:28 +00:00
|
|
|
}
|
2021-03-15 05:28:10 +00:00
|
|
|
conn.Send(t, testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagAck | header.TCPFlagPsh)}, samplePayload)
|
|
|
|
if _, err := conn.ExpectData(t, &testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagAck)}, nil, time.Second); err != nil {
|
|
|
|
t.Fatalf("expected packet was not received: %s", err)
|
|
|
|
}
|
|
|
|
// Wait for the DUT to receive the data, thus ensuring that the stack has
|
|
|
|
// estimated RTO before we query RTO via TCP_INFO.
|
|
|
|
if got := dut.Recv(t, acceptFd, int32(len(sampleData)), 0); !bytes.Equal(got, sampleData) {
|
|
|
|
t.Fatalf("got dut.Recv(t, %d, %d, 0) = %s, want %s", acceptFd, len(sampleData), got, sampleData)
|
|
|
|
}
|
2020-05-14 04:25:28 +00:00
|
|
|
|
2021-02-17 22:23:15 +00:00
|
|
|
const timeoutCorrection = time.Second
|
2021-03-15 05:28:10 +00:00
|
|
|
const diffCorrection = 200 * time.Millisecond
|
2021-02-17 22:23:15 +00:00
|
|
|
rto := getRTO(t, dut, acceptFd)
|
|
|
|
|
2020-07-27 13:37:11 +00:00
|
|
|
dut.Send(t, acceptFd, sampleData, 0)
|
|
|
|
seq := testbench.Uint32(uint32(*conn.RemoteSeqNum(t)))
|
2021-03-15 05:28:10 +00:00
|
|
|
if _, err := conn.ExpectData(t, &testbench.TCP{SeqNum: seq}, samplePayload, rto+timeoutCorrection); err != nil {
|
2020-06-12 02:45:06 +00:00
|
|
|
t.Fatalf("expected payload was not received: %s", err)
|
2020-05-14 04:25:28 +00:00
|
|
|
}
|
2021-02-17 22:23:15 +00:00
|
|
|
|
2020-05-14 04:25:28 +00:00
|
|
|
// Expect retransmits of the same segment.
|
|
|
|
for i := 0; i < 5; i++ {
|
2021-03-15 05:28:10 +00:00
|
|
|
startTime := time.Now()
|
|
|
|
rto = getRTO(t, dut, acceptFd)
|
|
|
|
if _, err := conn.ExpectData(t, &testbench.TCP{SeqNum: seq}, samplePayload, rto+timeoutCorrection); err != nil {
|
|
|
|
t.Fatalf("expected payload was not received within %s loop %d err %s", rto+timeoutCorrection, i, err)
|
2020-05-14 04:25:28 +00:00
|
|
|
}
|
2021-02-17 22:23:15 +00:00
|
|
|
if diff := time.Since(startTime); diff+diffCorrection < rto {
|
2021-03-15 05:28:10 +00:00
|
|
|
t.Fatalf("retransmit came sooner got: %s want: >= %s probe %d", diff+diffCorrection, rto, i)
|
2020-05-14 04:25:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|