Convert tcp_user_timeout test from packetdrill to packetimpact.

PiperOrigin-RevId: 307328289
This commit is contained in:
Eyal Soha
2020-04-19 20:49:06 -07:00
committed by gVisor bot
parent ea9bb1c4ea
commit 08b2fd9bc2
5 changed files with 111 additions and 88 deletions
+1 -11
View File
@@ -1,4 +1,4 @@
load("defs.bzl", "packetdrill_linux_test", "packetdrill_netstack_test", "packetdrill_test")
load("defs.bzl", "packetdrill_test")
package(licenses = ["notice"])
@@ -17,16 +17,6 @@ packetdrill_test(
scripts = ["fin_wait2_timeout.pkt"],
)
packetdrill_linux_test(
name = "tcp_user_timeout_test_linux_test",
scripts = ["linux/tcp_user_timeout.pkt"],
)
packetdrill_netstack_test(
name = "tcp_user_timeout_test_netstack_test",
scripts = ["netstack/tcp_user_timeout.pkt"],
)
packetdrill_test(
name = "listen_close_before_handshake_complete_test",
scripts = ["listen_close_before_handshake_complete.pkt"],
@@ -1,39 +0,0 @@
// Test that a socket w/ TCP_USER_TIMEOUT set aborts the connection
// if there is pending unacked data after the user specified timeout.
0 socket(..., SOCK_STREAM, IPPROTO_TCP) = 3
+0 bind(3, ..., ...) = 0
+0 listen(3, 1) = 0
// Establish a connection without timestamps.
+0 < S 0:0(0) win 32792 <mss 1460,sackOK,nop,nop,nop,wscale 7>
+0 > S. 0:0(0) ack 1 <...>
+0.1 < . 1:1(0) ack 1 win 32792
+0.100 accept(3, ..., ...) = 4
// Okay, we received nothing, and decide to close this idle socket.
// We set TCP_USER_TIMEOUT to 3 seconds because really it is not worth
// trying hard to cleanly close this flow, at the price of keeping
// a TCP structure in kernel for about 1 minute!
+2 setsockopt(4, SOL_TCP, TCP_USER_TIMEOUT, [3000], 4) = 0
// The write/ack is required mainly for netstack as netstack does
// not update its RTO during the handshake.
+0 write(4, ..., 100) = 100
+0 > P. 1:101(100) ack 1 <...>
+0 < . 1:1(0) ack 101 win 32792
+0 close(4) = 0
+0 > F. 101:101(0) ack 1 <...>
+.3~+.400 > F. 101:101(0) ack 1 <...>
+.3~+.400 > F. 101:101(0) ack 1 <...>
+.6~+.800 > F. 101:101(0) ack 1 <...>
+1.2~+1.300 > F. 101:101(0) ack 1 <...>
// We finally receive something from the peer, but it is way too late
// Our socket vanished because TCP_USER_TIMEOUT was really small.
+.1 < . 1:2(1) ack 102 win 32792
+0 > R 102:102(0) win 0
@@ -1,38 +0,0 @@
// Test that a socket w/ TCP_USER_TIMEOUT set aborts the connection
// if there is pending unacked data after the user specified timeout.
0 socket(..., SOCK_STREAM, IPPROTO_TCP) = 3
+0 bind(3, ..., ...) = 0
+0 listen(3, 1) = 0
// Establish a connection without timestamps.
+0 < S 0:0(0) win 32792 <mss 1460,sackOK,nop,nop,nop,wscale 7>
+0 > S. 0:0(0) ack 1 <...>
+0.1 < . 1:1(0) ack 1 win 32792
+0.100 accept(3, ..., ...) = 4
// Okay, we received nothing, and decide to close this idle socket.
// We set TCP_USER_TIMEOUT to 3 seconds because really it is not worth
// trying hard to cleanly close this flow, at the price of keeping
// a TCP structure in kernel for about 1 minute!
+2 setsockopt(4, SOL_TCP, TCP_USER_TIMEOUT, [3000], 4) = 0
// The write/ack is required mainly for netstack as netstack does
// not update its RTO during the handshake.
+0 write(4, ..., 100) = 100
+0 > P. 1:101(100) ack 1 <...>
+0 < . 1:1(0) ack 101 win 32792
+0 close(4) = 0
+0 > F. 101:101(0) ack 1 <...>
+.2~+.300 > F. 101:101(0) ack 1 <...>
+.4~+.500 > F. 101:101(0) ack 1 <...>
+.8~+.900 > F. 101:101(0) ack 1 <...>
// We finally receive something from the peer, but it is way too late
// Our socket vanished because TCP_USER_TIMEOUT was really small.
+1.61 < . 1:2(1) ack 102 win 32792
+0 > R 102:102(0) win 0
+10
View File
@@ -88,6 +88,16 @@ packetimpact_go_test(
],
)
packetimpact_go_test(
name = "tcp_user_timeout",
srcs = ["tcp_user_timeout_test.go"],
deps = [
"//pkg/tcpip/header",
"//test/packetimpact/testbench",
"@org_golang_x_sys//unix:go_default_library",
],
)
sh_binary(
name = "test_runner",
srcs = ["test_runner.sh"],
@@ -0,0 +1,100 @@
// 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_user_timeout_test
import (
"fmt"
"testing"
"time"
"golang.org/x/sys/unix"
"gvisor.dev/gvisor/pkg/tcpip/header"
tb "gvisor.dev/gvisor/test/packetimpact/testbench"
)
func sendPayload(conn *tb.TCPIPv4, dut *tb.DUT, fd int32) error {
sampleData := make([]byte, 100)
for i := range sampleData {
sampleData[i] = uint8(i)
}
conn.Drain()
dut.Send(fd, sampleData, 0)
if _, err := conn.ExpectData(&tb.TCP{Flags: tb.Uint8(header.TCPFlagAck | header.TCPFlagPsh)}, &tb.Payload{Bytes: sampleData}, time.Second); err != nil {
return fmt.Errorf("expected data but got none: %w", err)
}
return nil
}
func sendFIN(conn *tb.TCPIPv4, dut *tb.DUT, fd int32) error {
dut.Close(fd)
return nil
}
func TestTCPUserTimeout(t *testing.T) {
for _, tt := range []struct {
description string
userTimeout time.Duration
sendDelay time.Duration
}{
{"NoUserTimeout", 0, 3 * time.Second},
{"ACKBeforeUserTimeout", 5 * time.Second, 4 * time.Second},
{"ACKAfterUserTimeout", 5 * time.Second, 7 * time.Second},
} {
for _, ttf := range []struct {
description string
f func(conn *tb.TCPIPv4, dut *tb.DUT, fd int32) error
}{
{"AfterPayload", sendPayload},
{"AfterFIN", sendFIN},
} {
t.Run(tt.description+ttf.description, func(t *testing.T) {
// Create a socket, listen, TCP handshake, and accept.
dut := tb.NewDUT(t)
defer dut.TearDown()
listenFD, remotePort := dut.CreateListener(unix.SOCK_STREAM, unix.IPPROTO_TCP, 1)
defer dut.Close(listenFD)
conn := tb.NewTCPIPv4(t, tb.TCP{DstPort: &remotePort}, tb.TCP{SrcPort: &remotePort})
defer conn.Close()
conn.Handshake()
acceptFD, _ := dut.Accept(listenFD)
if tt.userTimeout != 0 {
dut.SetSockOptInt(acceptFD, unix.SOL_TCP, unix.TCP_USER_TIMEOUT, int32(tt.userTimeout.Milliseconds()))
}
if err := ttf.f(&conn, &dut, acceptFD); err != nil {
t.Fatal(err)
}
time.Sleep(tt.sendDelay)
conn.Drain()
conn.Send(tb.TCP{Flags: tb.Uint8(header.TCPFlagAck)})
// If TCP_USER_TIMEOUT was set and the above delay was longer than the
// TCP_USER_TIMEOUT then the DUT should send a RST in response to the
// testbench's packet.
expectRST := tt.userTimeout != 0 && tt.sendDelay > tt.userTimeout
expectTimeout := 5 * time.Second
got, err := conn.Expect(tb.TCP{Flags: tb.Uint8(header.TCPFlagRst)}, expectTimeout)
if expectRST && err != nil {
t.Errorf("expected RST packet within %s but got none: %s", expectTimeout, err)
}
if !expectRST && got != nil {
t.Errorf("expected no RST packet within %s but got one: %s", expectTimeout, got)
}
})
}
}
}