github.com/flowerwrong/netstack@v0.0.0-20191009141956-e5848263af28/tcpip/transport/tcp/tcp_test.go (about)

     1  // Copyright 2018 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_test
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"math"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/FlowerWrong/netstack/tcpip"
    25  	"github.com/FlowerWrong/netstack/tcpip/buffer"
    26  	"github.com/FlowerWrong/netstack/tcpip/checker"
    27  	"github.com/FlowerWrong/netstack/tcpip/header"
    28  	"github.com/FlowerWrong/netstack/tcpip/link/loopback"
    29  	"github.com/FlowerWrong/netstack/tcpip/link/sniffer"
    30  	"github.com/FlowerWrong/netstack/tcpip/network/ipv4"
    31  	"github.com/FlowerWrong/netstack/tcpip/network/ipv6"
    32  	"github.com/FlowerWrong/netstack/tcpip/ports"
    33  	"github.com/FlowerWrong/netstack/tcpip/seqnum"
    34  	"github.com/FlowerWrong/netstack/tcpip/stack"
    35  	"github.com/FlowerWrong/netstack/tcpip/transport/tcp"
    36  	"github.com/FlowerWrong/netstack/tcpip/transport/tcp/testing/context"
    37  	"github.com/FlowerWrong/netstack/waiter"
    38  )
    39  
    40  const (
    41  	// defaultMTU is the MTU, in bytes, used throughout the tests, except
    42  	// where another value is explicitly used. It is chosen to match the MTU
    43  	// of loopback interfaces on linux systems.
    44  	defaultMTU = 65535
    45  
    46  	// defaultIPv4MSS is the MSS sent by the network stack in SYN/SYN-ACK for an
    47  	// IPv4 endpoint when the MTU is set to defaultMTU in the test.
    48  	defaultIPv4MSS = defaultMTU - header.IPv4MinimumSize - header.TCPMinimumSize
    49  )
    50  
    51  func TestGiveUpConnect(t *testing.T) {
    52  	c := context.New(t, defaultMTU)
    53  	defer c.Cleanup()
    54  
    55  	var wq waiter.Queue
    56  	ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &wq)
    57  	if err != nil {
    58  		t.Fatalf("NewEndpoint failed: %v", err)
    59  	}
    60  
    61  	// Register for notification, then start connection attempt.
    62  	waitEntry, notifyCh := waiter.NewChannelEntry(nil)
    63  	wq.EventRegister(&waitEntry, waiter.EventOut)
    64  	defer wq.EventUnregister(&waitEntry)
    65  
    66  	if err := ep.Connect(tcpip.FullAddress{Addr: context.TestAddr, Port: context.TestPort}); err != tcpip.ErrConnectStarted {
    67  		t.Fatalf("got ep.Connect(...) = %v, want = %v", err, tcpip.ErrConnectStarted)
    68  	}
    69  
    70  	// Close the connection, wait for completion.
    71  	ep.Close()
    72  
    73  	// Wait for ep to become writable.
    74  	<-notifyCh
    75  	if err := ep.GetSockOpt(tcpip.ErrorOption{}); err != tcpip.ErrAborted {
    76  		t.Fatalf("got ep.GetSockOpt(tcpip.ErrorOption{}) = %v, want = %v", err, tcpip.ErrAborted)
    77  	}
    78  }
    79  
    80  func TestConnectIncrementActiveConnection(t *testing.T) {
    81  	c := context.New(t, defaultMTU)
    82  	defer c.Cleanup()
    83  
    84  	stats := c.Stack().Stats()
    85  	want := stats.TCP.ActiveConnectionOpenings.Value() + 1
    86  
    87  	c.CreateConnected(789, 30000, -1 /* epRcvBuf */)
    88  	if got := stats.TCP.ActiveConnectionOpenings.Value(); got != want {
    89  		t.Errorf("got stats.TCP.ActtiveConnectionOpenings.Value() = %v, want = %v", got, want)
    90  	}
    91  }
    92  
    93  func TestConnectDoesNotIncrementFailedConnectionAttempts(t *testing.T) {
    94  	c := context.New(t, defaultMTU)
    95  	defer c.Cleanup()
    96  
    97  	stats := c.Stack().Stats()
    98  	want := stats.TCP.FailedConnectionAttempts.Value()
    99  
   100  	c.CreateConnected(789, 30000, -1 /* epRcvBuf */)
   101  	if got := stats.TCP.FailedConnectionAttempts.Value(); got != want {
   102  		t.Errorf("got stats.TCP.FailedConnectionOpenings.Value() = %v, want = %v", got, want)
   103  	}
   104  }
   105  
   106  func TestActiveFailedConnectionAttemptIncrement(t *testing.T) {
   107  	c := context.New(t, defaultMTU)
   108  	defer c.Cleanup()
   109  
   110  	stats := c.Stack().Stats()
   111  	ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &c.WQ)
   112  	if err != nil {
   113  		t.Fatalf("NewEndpoint failed: %v", err)
   114  	}
   115  	c.EP = ep
   116  	want := stats.TCP.FailedConnectionAttempts.Value() + 1
   117  
   118  	if err := c.EP.Connect(tcpip.FullAddress{NIC: 2, Addr: context.TestAddr, Port: context.TestPort}); err != tcpip.ErrNoRoute {
   119  		t.Errorf("got c.EP.Connect(...) = %v, want = %v", err, tcpip.ErrNoRoute)
   120  	}
   121  
   122  	if got := stats.TCP.FailedConnectionAttempts.Value(); got != want {
   123  		t.Errorf("got stats.TCP.FailedConnectionAttempts.Value() = %v, want = %v", got, want)
   124  	}
   125  }
   126  
   127  func TestTCPSegmentsSentIncrement(t *testing.T) {
   128  	c := context.New(t, defaultMTU)
   129  	defer c.Cleanup()
   130  
   131  	stats := c.Stack().Stats()
   132  	// SYN and ACK
   133  	want := stats.TCP.SegmentsSent.Value() + 2
   134  	c.CreateConnected(789, 30000, -1 /* epRcvBuf */)
   135  
   136  	if got := stats.TCP.SegmentsSent.Value(); got != want {
   137  		t.Errorf("got stats.TCP.SegmentsSent.Value() = %v, want = %v", got, want)
   138  	}
   139  }
   140  
   141  func TestTCPResetsSentIncrement(t *testing.T) {
   142  	c := context.New(t, defaultMTU)
   143  	defer c.Cleanup()
   144  	stats := c.Stack().Stats()
   145  	wq := &waiter.Queue{}
   146  	ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, wq)
   147  	if err != nil {
   148  		t.Fatalf("NewEndpoint failed: %v", err)
   149  	}
   150  	want := stats.TCP.SegmentsSent.Value() + 1
   151  
   152  	if err := ep.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
   153  		t.Fatalf("Bind failed: %v", err)
   154  	}
   155  
   156  	if err := ep.Listen(10); err != nil {
   157  		t.Fatalf("Listen failed: %v", err)
   158  	}
   159  
   160  	// Send a SYN request.
   161  	iss := seqnum.Value(789)
   162  	c.SendPacket(nil, &context.Headers{
   163  		SrcPort: context.TestPort,
   164  		DstPort: context.StackPort,
   165  		Flags:   header.TCPFlagSyn,
   166  		SeqNum:  iss,
   167  	})
   168  
   169  	// Receive the SYN-ACK reply.
   170  	b := c.GetPacket()
   171  	tcpHdr := header.TCP(header.IPv4(b).Payload())
   172  	c.IRS = seqnum.Value(tcpHdr.SequenceNumber())
   173  
   174  	ackHeaders := &context.Headers{
   175  		SrcPort: context.TestPort,
   176  		DstPort: context.StackPort,
   177  		Flags:   header.TCPFlagAck,
   178  		SeqNum:  iss + 1,
   179  		// If the AckNum is not the increment of the last sequence number, a RST
   180  		// segment is sent back in response.
   181  		AckNum: c.IRS + 2,
   182  	}
   183  
   184  	// Send ACK.
   185  	c.SendPacket(nil, ackHeaders)
   186  
   187  	c.GetPacket()
   188  	if got := stats.TCP.ResetsSent.Value(); got != want {
   189  		t.Errorf("got stats.TCP.ResetsSent.Value() = %v, want = %v", got, want)
   190  	}
   191  }
   192  
   193  // TestTCPResetSentForACKWhenNotUsingSynCookies checks that the stack generates
   194  // a RST if an ACK is received on the listening socket for which there is no
   195  // active handshake in progress and we are not using SYN cookies.
   196  func TestTCPResetSentForACKWhenNotUsingSynCookies(t *testing.T) {
   197  	c := context.New(t, defaultMTU)
   198  	defer c.Cleanup()
   199  
   200  	wq := &waiter.Queue{}
   201  	ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, wq)
   202  	if err != nil {
   203  		t.Fatalf("NewEndpoint failed: %v", err)
   204  	}
   205  	if err := ep.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
   206  		t.Fatalf("Bind failed: %v", err)
   207  	}
   208  
   209  	if err := ep.Listen(10); err != nil {
   210  		t.Fatalf("Listen failed: %v", err)
   211  	}
   212  
   213  	// Send a SYN request.
   214  	iss := seqnum.Value(789)
   215  	c.SendPacket(nil, &context.Headers{
   216  		SrcPort: context.TestPort,
   217  		DstPort: context.StackPort,
   218  		Flags:   header.TCPFlagSyn,
   219  		SeqNum:  iss,
   220  	})
   221  
   222  	// Receive the SYN-ACK reply.
   223  	b := c.GetPacket()
   224  	tcpHdr := header.TCP(header.IPv4(b).Payload())
   225  	c.IRS = seqnum.Value(tcpHdr.SequenceNumber())
   226  
   227  	ackHeaders := &context.Headers{
   228  		SrcPort: context.TestPort,
   229  		DstPort: context.StackPort,
   230  		Flags:   header.TCPFlagAck,
   231  		SeqNum:  iss + 1,
   232  		AckNum:  c.IRS + 1,
   233  	}
   234  
   235  	// Send ACK.
   236  	c.SendPacket(nil, ackHeaders)
   237  
   238  	// Try to accept the connection.
   239  	we, ch := waiter.NewChannelEntry(nil)
   240  	wq.EventRegister(&we, waiter.EventIn)
   241  	defer wq.EventUnregister(&we)
   242  
   243  	c.EP, _, err = ep.Accept()
   244  	if err == tcpip.ErrWouldBlock {
   245  		// Wait for connection to be established.
   246  		select {
   247  		case <-ch:
   248  			c.EP, _, err = ep.Accept()
   249  			if err != nil {
   250  				t.Fatalf("Accept failed: %v", err)
   251  			}
   252  
   253  		case <-time.After(1 * time.Second):
   254  			t.Fatalf("Timed out waiting for accept")
   255  		}
   256  	}
   257  
   258  	c.EP.Close()
   259  	checker.IPv4(t, c.GetPacket(), checker.TCP(
   260  		checker.SrcPort(context.StackPort),
   261  		checker.DstPort(context.TestPort),
   262  		checker.SeqNum(uint32(c.IRS+1)),
   263  		checker.AckNum(uint32(iss)+1),
   264  		checker.TCPFlags(header.TCPFlagFin|header.TCPFlagAck)))
   265  
   266  	finHeaders := &context.Headers{
   267  		SrcPort: context.TestPort,
   268  		DstPort: context.StackPort,
   269  		Flags:   header.TCPFlagAck | header.TCPFlagFin,
   270  		SeqNum:  iss + 1,
   271  		AckNum:  c.IRS + 2,
   272  	}
   273  
   274  	c.SendPacket(nil, finHeaders)
   275  
   276  	// Get the ACK to the FIN we just sent.
   277  	c.GetPacket()
   278  
   279  	// Now resend the same ACK, this ACK should generate a RST as there
   280  	// should be no endpoint in SYN-RCVD state and we are not using
   281  	// syn-cookies yet. The reason we send the same ACK is we need a valid
   282  	// cookie(IRS) generated by the netstack without which the ACK will be
   283  	// rejected.
   284  	c.SendPacket(nil, ackHeaders)
   285  
   286  	checker.IPv4(t, c.GetPacket(), checker.TCP(
   287  		checker.SrcPort(context.StackPort),
   288  		checker.DstPort(context.TestPort),
   289  		checker.SeqNum(uint32(c.IRS+1)),
   290  		checker.AckNum(uint32(iss)+1),
   291  		checker.TCPFlags(header.TCPFlagRst|header.TCPFlagAck)))
   292  }
   293  
   294  func TestTCPResetsReceivedIncrement(t *testing.T) {
   295  	c := context.New(t, defaultMTU)
   296  	defer c.Cleanup()
   297  
   298  	stats := c.Stack().Stats()
   299  	want := stats.TCP.ResetsReceived.Value() + 1
   300  	iss := seqnum.Value(789)
   301  	rcvWnd := seqnum.Size(30000)
   302  	c.CreateConnected(iss, rcvWnd, -1 /* epRcvBuf */)
   303  
   304  	c.SendPacket(nil, &context.Headers{
   305  		SrcPort: context.TestPort,
   306  		DstPort: c.Port,
   307  		SeqNum:  iss.Add(1),
   308  		AckNum:  c.IRS.Add(1),
   309  		RcvWnd:  rcvWnd,
   310  		Flags:   header.TCPFlagRst,
   311  	})
   312  
   313  	if got := stats.TCP.ResetsReceived.Value(); got != want {
   314  		t.Errorf("got stats.TCP.ResetsReceived.Value() = %v, want = %v", got, want)
   315  	}
   316  }
   317  
   318  func TestTCPResetsDoNotGenerateResets(t *testing.T) {
   319  	c := context.New(t, defaultMTU)
   320  	defer c.Cleanup()
   321  
   322  	stats := c.Stack().Stats()
   323  	want := stats.TCP.ResetsReceived.Value() + 1
   324  	iss := seqnum.Value(789)
   325  	rcvWnd := seqnum.Size(30000)
   326  	c.CreateConnected(iss, rcvWnd, -1 /* epRcvBuf */)
   327  
   328  	c.SendPacket(nil, &context.Headers{
   329  		SrcPort: context.TestPort,
   330  		DstPort: c.Port,
   331  		SeqNum:  iss.Add(1),
   332  		AckNum:  c.IRS.Add(1),
   333  		RcvWnd:  rcvWnd,
   334  		Flags:   header.TCPFlagRst,
   335  	})
   336  
   337  	if got := stats.TCP.ResetsReceived.Value(); got != want {
   338  		t.Errorf("got stats.TCP.ResetsReceived.Value() = %v, want = %v", got, want)
   339  	}
   340  	c.CheckNoPacketTimeout("got an unexpected packet", 100*time.Millisecond)
   341  }
   342  
   343  func TestActiveHandshake(t *testing.T) {
   344  	c := context.New(t, defaultMTU)
   345  	defer c.Cleanup()
   346  
   347  	c.CreateConnected(789, 30000, -1 /* epRcvBuf */)
   348  }
   349  
   350  func TestNonBlockingClose(t *testing.T) {
   351  	c := context.New(t, defaultMTU)
   352  	defer c.Cleanup()
   353  
   354  	c.CreateConnected(789, 30000, -1 /* epRcvBuf */)
   355  	ep := c.EP
   356  	c.EP = nil
   357  
   358  	// Close the endpoint and measure how long it takes.
   359  	t0 := time.Now()
   360  	ep.Close()
   361  	if diff := time.Now().Sub(t0); diff > 3*time.Second {
   362  		t.Fatalf("Took too long to close: %v", diff)
   363  	}
   364  }
   365  
   366  func TestConnectResetAfterClose(t *testing.T) {
   367  	c := context.New(t, defaultMTU)
   368  	defer c.Cleanup()
   369  
   370  	c.CreateConnected(789, 30000, -1 /* epRcvBuf */)
   371  	ep := c.EP
   372  	c.EP = nil
   373  
   374  	// Close the endpoint, make sure we get a FIN segment, then acknowledge
   375  	// to complete closure of sender, but don't send our own FIN.
   376  	ep.Close()
   377  	checker.IPv4(t, c.GetPacket(),
   378  		checker.TCP(
   379  			checker.DstPort(context.TestPort),
   380  			checker.SeqNum(uint32(c.IRS)+1),
   381  			checker.AckNum(790),
   382  			checker.TCPFlags(header.TCPFlagAck|header.TCPFlagFin),
   383  		),
   384  	)
   385  	c.SendPacket(nil, &context.Headers{
   386  		SrcPort: context.TestPort,
   387  		DstPort: c.Port,
   388  		Flags:   header.TCPFlagAck,
   389  		SeqNum:  790,
   390  		AckNum:  c.IRS.Add(1),
   391  		RcvWnd:  30000,
   392  	})
   393  
   394  	// Wait for the ep to give up waiting for a FIN, and send a RST.
   395  	time.Sleep(3 * time.Second)
   396  	for {
   397  		b := c.GetPacket()
   398  		tcpHdr := header.TCP(header.IPv4(b).Payload())
   399  		if tcpHdr.Flags() == header.TCPFlagAck|header.TCPFlagFin {
   400  			// This is a retransmit of the FIN, ignore it.
   401  			continue
   402  		}
   403  
   404  		checker.IPv4(t, b,
   405  			checker.TCP(
   406  				checker.DstPort(context.TestPort),
   407  				checker.SeqNum(uint32(c.IRS)+1),
   408  				checker.AckNum(790),
   409  				checker.TCPFlags(header.TCPFlagAck|header.TCPFlagRst),
   410  			),
   411  		)
   412  		break
   413  	}
   414  }
   415  
   416  func TestSimpleReceive(t *testing.T) {
   417  	c := context.New(t, defaultMTU)
   418  	defer c.Cleanup()
   419  
   420  	c.CreateConnected(789, 30000, -1 /* epRcvBuf */)
   421  
   422  	we, ch := waiter.NewChannelEntry(nil)
   423  	c.WQ.EventRegister(&we, waiter.EventIn)
   424  	defer c.WQ.EventUnregister(&we)
   425  
   426  	if _, _, err := c.EP.Read(nil); err != tcpip.ErrWouldBlock {
   427  		t.Fatalf("got c.EP.Read(nil) = %v, want = %v", err, tcpip.ErrWouldBlock)
   428  	}
   429  
   430  	data := []byte{1, 2, 3}
   431  	c.SendPacket(data, &context.Headers{
   432  		SrcPort: context.TestPort,
   433  		DstPort: c.Port,
   434  		Flags:   header.TCPFlagAck,
   435  		SeqNum:  790,
   436  		AckNum:  c.IRS.Add(1),
   437  		RcvWnd:  30000,
   438  	})
   439  
   440  	// Wait for receive to be notified.
   441  	select {
   442  	case <-ch:
   443  	case <-time.After(1 * time.Second):
   444  		t.Fatalf("Timed out waiting for data to arrive")
   445  	}
   446  
   447  	// Receive data.
   448  	v, _, err := c.EP.Read(nil)
   449  	if err != nil {
   450  		t.Fatalf("Read failed: %v", err)
   451  	}
   452  
   453  	if !bytes.Equal(data, v) {
   454  		t.Fatalf("got data = %v, want = %v", v, data)
   455  	}
   456  
   457  	// Check that ACK is received.
   458  	checker.IPv4(t, c.GetPacket(),
   459  		checker.TCP(
   460  			checker.DstPort(context.TestPort),
   461  			checker.SeqNum(uint32(c.IRS)+1),
   462  			checker.AckNum(uint32(790+len(data))),
   463  			checker.TCPFlags(header.TCPFlagAck),
   464  		),
   465  	)
   466  }
   467  
   468  func TestConnectBindToDevice(t *testing.T) {
   469  	for _, test := range []struct {
   470  		name   string
   471  		device string
   472  		want   tcp.EndpointState
   473  	}{
   474  		{"RightDevice", "nic1", tcp.StateEstablished},
   475  		{"WrongDevice", "nic2", tcp.StateSynSent},
   476  		{"AnyDevice", "", tcp.StateEstablished},
   477  	} {
   478  		t.Run(test.name, func(t *testing.T) {
   479  			c := context.New(t, defaultMTU)
   480  			defer c.Cleanup()
   481  
   482  			c.Create(-1)
   483  			bindToDevice := tcpip.BindToDeviceOption(test.device)
   484  			c.EP.SetSockOpt(bindToDevice)
   485  			// Start connection attempt.
   486  			waitEntry, _ := waiter.NewChannelEntry(nil)
   487  			c.WQ.EventRegister(&waitEntry, waiter.EventOut)
   488  			defer c.WQ.EventUnregister(&waitEntry)
   489  
   490  			if err := c.EP.Connect(tcpip.FullAddress{Addr: context.TestAddr, Port: context.TestPort}); err != tcpip.ErrConnectStarted {
   491  				t.Fatalf("Unexpected return value from Connect: %v", err)
   492  			}
   493  
   494  			// Receive SYN packet.
   495  			b := c.GetPacket()
   496  			checker.IPv4(t, b,
   497  				checker.TCP(
   498  					checker.DstPort(context.TestPort),
   499  					checker.TCPFlags(header.TCPFlagSyn),
   500  				),
   501  			)
   502  			if got, want := tcp.EndpointState(c.EP.State()), tcp.StateSynSent; got != want {
   503  				t.Fatalf("Unexpected endpoint state: want %v, got %v", want, got)
   504  			}
   505  			tcpHdr := header.TCP(header.IPv4(b).Payload())
   506  			c.IRS = seqnum.Value(tcpHdr.SequenceNumber())
   507  
   508  			iss := seqnum.Value(789)
   509  			rcvWnd := seqnum.Size(30000)
   510  			c.SendPacket(nil, &context.Headers{
   511  				SrcPort: tcpHdr.DestinationPort(),
   512  				DstPort: tcpHdr.SourcePort(),
   513  				Flags:   header.TCPFlagSyn | header.TCPFlagAck,
   514  				SeqNum:  iss,
   515  				AckNum:  c.IRS.Add(1),
   516  				RcvWnd:  rcvWnd,
   517  				TCPOpts: nil,
   518  			})
   519  
   520  			c.GetPacket()
   521  			if got, want := tcp.EndpointState(c.EP.State()), test.want; got != want {
   522  				t.Fatalf("Unexpected endpoint state: want %v, got %v", want, got)
   523  			}
   524  		})
   525  	}
   526  }
   527  
   528  func TestOutOfOrderReceive(t *testing.T) {
   529  	c := context.New(t, defaultMTU)
   530  	defer c.Cleanup()
   531  
   532  	c.CreateConnected(789, 30000, -1 /* epRcvBuf */)
   533  
   534  	we, ch := waiter.NewChannelEntry(nil)
   535  	c.WQ.EventRegister(&we, waiter.EventIn)
   536  	defer c.WQ.EventUnregister(&we)
   537  
   538  	if _, _, err := c.EP.Read(nil); err != tcpip.ErrWouldBlock {
   539  		t.Fatalf("got c.EP.Read(nil) = %v, want = %v", err, tcpip.ErrWouldBlock)
   540  	}
   541  
   542  	// Send second half of data first, with seqnum 3 ahead of expected.
   543  	data := []byte{1, 2, 3, 4, 5, 6}
   544  	c.SendPacket(data[3:], &context.Headers{
   545  		SrcPort: context.TestPort,
   546  		DstPort: c.Port,
   547  		Flags:   header.TCPFlagAck,
   548  		SeqNum:  793,
   549  		AckNum:  c.IRS.Add(1),
   550  		RcvWnd:  30000,
   551  	})
   552  
   553  	// Check that we get an ACK specifying which seqnum is expected.
   554  	checker.IPv4(t, c.GetPacket(),
   555  		checker.TCP(
   556  			checker.DstPort(context.TestPort),
   557  			checker.SeqNum(uint32(c.IRS)+1),
   558  			checker.AckNum(790),
   559  			checker.TCPFlags(header.TCPFlagAck),
   560  		),
   561  	)
   562  
   563  	// Wait 200ms and check that no data has been received.
   564  	time.Sleep(200 * time.Millisecond)
   565  	if _, _, err := c.EP.Read(nil); err != tcpip.ErrWouldBlock {
   566  		t.Fatalf("got c.EP.Read(nil) = %v, want = %v", err, tcpip.ErrWouldBlock)
   567  	}
   568  
   569  	// Send the first 3 bytes now.
   570  	c.SendPacket(data[:3], &context.Headers{
   571  		SrcPort: context.TestPort,
   572  		DstPort: c.Port,
   573  		Flags:   header.TCPFlagAck,
   574  		SeqNum:  790,
   575  		AckNum:  c.IRS.Add(1),
   576  		RcvWnd:  30000,
   577  	})
   578  
   579  	// Receive data.
   580  	read := make([]byte, 0, 6)
   581  	for len(read) < len(data) {
   582  		v, _, err := c.EP.Read(nil)
   583  		if err != nil {
   584  			if err == tcpip.ErrWouldBlock {
   585  				// Wait for receive to be notified.
   586  				select {
   587  				case <-ch:
   588  				case <-time.After(5 * time.Second):
   589  					t.Fatalf("Timed out waiting for data to arrive")
   590  				}
   591  				continue
   592  			}
   593  			t.Fatalf("Read failed: %v", err)
   594  		}
   595  
   596  		read = append(read, v...)
   597  	}
   598  
   599  	// Check that we received the data in proper order.
   600  	if !bytes.Equal(data, read) {
   601  		t.Fatalf("got data = %v, want = %v", read, data)
   602  	}
   603  
   604  	// Check that the whole data is acknowledged.
   605  	checker.IPv4(t, c.GetPacket(),
   606  		checker.TCP(
   607  			checker.DstPort(context.TestPort),
   608  			checker.SeqNum(uint32(c.IRS)+1),
   609  			checker.AckNum(uint32(790+len(data))),
   610  			checker.TCPFlags(header.TCPFlagAck),
   611  		),
   612  	)
   613  }
   614  
   615  func TestOutOfOrderFlood(t *testing.T) {
   616  	c := context.New(t, defaultMTU)
   617  	defer c.Cleanup()
   618  
   619  	// Create a new connection with initial window size of 10.
   620  	c.CreateConnected(789, 30000, 10)
   621  
   622  	if _, _, err := c.EP.Read(nil); err != tcpip.ErrWouldBlock {
   623  		t.Fatalf("got c.EP.Read(nil) = %v, want = %v", err, tcpip.ErrWouldBlock)
   624  	}
   625  
   626  	// Send 100 packets before the actual one that is expected.
   627  	data := []byte{1, 2, 3, 4, 5, 6}
   628  	for i := 0; i < 100; i++ {
   629  		c.SendPacket(data[3:], &context.Headers{
   630  			SrcPort: context.TestPort,
   631  			DstPort: c.Port,
   632  			Flags:   header.TCPFlagAck,
   633  			SeqNum:  796,
   634  			AckNum:  c.IRS.Add(1),
   635  			RcvWnd:  30000,
   636  		})
   637  
   638  		checker.IPv4(t, c.GetPacket(),
   639  			checker.TCP(
   640  				checker.DstPort(context.TestPort),
   641  				checker.SeqNum(uint32(c.IRS)+1),
   642  				checker.AckNum(790),
   643  				checker.TCPFlags(header.TCPFlagAck),
   644  			),
   645  		)
   646  	}
   647  
   648  	// Send packet with seqnum 793. It must be discarded because the
   649  	// out-of-order buffer was filled by the previous packets.
   650  	c.SendPacket(data[3:], &context.Headers{
   651  		SrcPort: context.TestPort,
   652  		DstPort: c.Port,
   653  		Flags:   header.TCPFlagAck,
   654  		SeqNum:  793,
   655  		AckNum:  c.IRS.Add(1),
   656  		RcvWnd:  30000,
   657  	})
   658  
   659  	checker.IPv4(t, c.GetPacket(),
   660  		checker.TCP(
   661  			checker.DstPort(context.TestPort),
   662  			checker.SeqNum(uint32(c.IRS)+1),
   663  			checker.AckNum(790),
   664  			checker.TCPFlags(header.TCPFlagAck),
   665  		),
   666  	)
   667  
   668  	// Now send the expected packet, seqnum 790.
   669  	c.SendPacket(data[:3], &context.Headers{
   670  		SrcPort: context.TestPort,
   671  		DstPort: c.Port,
   672  		Flags:   header.TCPFlagAck,
   673  		SeqNum:  790,
   674  		AckNum:  c.IRS.Add(1),
   675  		RcvWnd:  30000,
   676  	})
   677  
   678  	// Check that only packet 790 is acknowledged.
   679  	checker.IPv4(t, c.GetPacket(),
   680  		checker.TCP(
   681  			checker.DstPort(context.TestPort),
   682  			checker.SeqNum(uint32(c.IRS)+1),
   683  			checker.AckNum(793),
   684  			checker.TCPFlags(header.TCPFlagAck),
   685  		),
   686  	)
   687  }
   688  
   689  func TestRstOnCloseWithUnreadData(t *testing.T) {
   690  	c := context.New(t, defaultMTU)
   691  	defer c.Cleanup()
   692  
   693  	c.CreateConnected(789, 30000, -1 /* epRcvBuf */)
   694  
   695  	we, ch := waiter.NewChannelEntry(nil)
   696  	c.WQ.EventRegister(&we, waiter.EventIn)
   697  	defer c.WQ.EventUnregister(&we)
   698  
   699  	if _, _, err := c.EP.Read(nil); err != tcpip.ErrWouldBlock {
   700  		t.Fatalf("got c.EP.Read(nil) = %v, want = %v", err, tcpip.ErrWouldBlock)
   701  	}
   702  
   703  	data := []byte{1, 2, 3}
   704  	c.SendPacket(data, &context.Headers{
   705  		SrcPort: context.TestPort,
   706  		DstPort: c.Port,
   707  		Flags:   header.TCPFlagAck,
   708  		SeqNum:  790,
   709  		AckNum:  c.IRS.Add(1),
   710  		RcvWnd:  30000,
   711  	})
   712  
   713  	// Wait for receive to be notified.
   714  	select {
   715  	case <-ch:
   716  	case <-time.After(3 * time.Second):
   717  		t.Fatalf("Timed out waiting for data to arrive")
   718  	}
   719  
   720  	// Check that ACK is received, this happens regardless of the read.
   721  	checker.IPv4(t, c.GetPacket(),
   722  		checker.TCP(
   723  			checker.DstPort(context.TestPort),
   724  			checker.SeqNum(uint32(c.IRS)+1),
   725  			checker.AckNum(uint32(790+len(data))),
   726  			checker.TCPFlags(header.TCPFlagAck),
   727  		),
   728  	)
   729  
   730  	// Now that we know we have unread data, let's just close the connection
   731  	// and verify that netstack sends an RST rather than a FIN.
   732  	c.EP.Close()
   733  
   734  	checker.IPv4(t, c.GetPacket(),
   735  		checker.TCP(
   736  			checker.DstPort(context.TestPort),
   737  			checker.TCPFlags(header.TCPFlagAck|header.TCPFlagRst),
   738  			// We shouldn't consume a sequence number on RST.
   739  			checker.SeqNum(uint32(c.IRS)+1),
   740  		))
   741  	// The RST puts the endpoint into an error state.
   742  	if got, want := tcp.EndpointState(c.EP.State()), tcp.StateError; got != want {
   743  		t.Errorf("Unexpected endpoint state: want %v, got %v", want, got)
   744  	}
   745  
   746  	// This final ACK should be ignored because an ACK on a reset doesn't mean
   747  	// anything.
   748  	c.SendPacket(nil, &context.Headers{
   749  		SrcPort: context.TestPort,
   750  		DstPort: c.Port,
   751  		Flags:   header.TCPFlagAck,
   752  		SeqNum:  seqnum.Value(790 + len(data)),
   753  		AckNum:  c.IRS.Add(seqnum.Size(2)),
   754  		RcvWnd:  30000,
   755  	})
   756  }
   757  
   758  func TestRstOnCloseWithUnreadDataFinConvertRst(t *testing.T) {
   759  	c := context.New(t, defaultMTU)
   760  	defer c.Cleanup()
   761  
   762  	c.CreateConnected(789, 30000, -1 /* epRcvBuf */)
   763  
   764  	we, ch := waiter.NewChannelEntry(nil)
   765  	c.WQ.EventRegister(&we, waiter.EventIn)
   766  	defer c.WQ.EventUnregister(&we)
   767  
   768  	if _, _, err := c.EP.Read(nil); err != tcpip.ErrWouldBlock {
   769  		t.Fatalf("got c.EP.Read(nil) = %v, want = %v", err, tcpip.ErrWouldBlock)
   770  	}
   771  
   772  	data := []byte{1, 2, 3}
   773  	c.SendPacket(data, &context.Headers{
   774  		SrcPort: context.TestPort,
   775  		DstPort: c.Port,
   776  		Flags:   header.TCPFlagAck,
   777  		SeqNum:  790,
   778  		AckNum:  c.IRS.Add(1),
   779  		RcvWnd:  30000,
   780  	})
   781  
   782  	// Wait for receive to be notified.
   783  	select {
   784  	case <-ch:
   785  	case <-time.After(3 * time.Second):
   786  		t.Fatalf("Timed out waiting for data to arrive")
   787  	}
   788  
   789  	// Check that ACK is received, this happens regardless of the read.
   790  	checker.IPv4(t, c.GetPacket(),
   791  		checker.TCP(
   792  			checker.DstPort(context.TestPort),
   793  			checker.SeqNum(uint32(c.IRS)+1),
   794  			checker.AckNum(uint32(790+len(data))),
   795  			checker.TCPFlags(header.TCPFlagAck),
   796  		),
   797  	)
   798  
   799  	// Cause a FIN to be generated.
   800  	c.EP.Shutdown(tcpip.ShutdownWrite)
   801  
   802  	// Make sure we get the FIN but DON't ACK IT.
   803  	checker.IPv4(t, c.GetPacket(),
   804  		checker.TCP(
   805  			checker.DstPort(context.TestPort),
   806  			checker.TCPFlags(header.TCPFlagAck|header.TCPFlagFin),
   807  			checker.SeqNum(uint32(c.IRS)+1),
   808  		))
   809  
   810  	if got, want := tcp.EndpointState(c.EP.State()), tcp.StateFinWait1; got != want {
   811  		t.Errorf("Unexpected endpoint state: want %v, got %v", want, got)
   812  	}
   813  
   814  	// Cause a RST to be generated by closing the read end now since we have
   815  	// unread data.
   816  	c.EP.Shutdown(tcpip.ShutdownRead)
   817  
   818  	// Make sure we get the RST
   819  	checker.IPv4(t, c.GetPacket(),
   820  		checker.TCP(
   821  			checker.DstPort(context.TestPort),
   822  			checker.TCPFlags(header.TCPFlagAck|header.TCPFlagRst),
   823  			// We shouldn't consume a sequence number on RST.
   824  			checker.SeqNum(uint32(c.IRS)+1),
   825  		))
   826  	// The RST puts the endpoint into an error state.
   827  	if got, want := tcp.EndpointState(c.EP.State()), tcp.StateError; got != want {
   828  		t.Errorf("Unexpected endpoint state: want %v, got %v", want, got)
   829  	}
   830  
   831  	// The ACK to the FIN should now be rejected since the connection has been
   832  	// closed by a RST.
   833  	c.SendPacket(nil, &context.Headers{
   834  		SrcPort: context.TestPort,
   835  		DstPort: c.Port,
   836  		Flags:   header.TCPFlagAck,
   837  		SeqNum:  seqnum.Value(790 + len(data)),
   838  		AckNum:  c.IRS.Add(seqnum.Size(2)),
   839  		RcvWnd:  30000,
   840  	})
   841  }
   842  
   843  func TestShutdownRead(t *testing.T) {
   844  	c := context.New(t, defaultMTU)
   845  	defer c.Cleanup()
   846  
   847  	c.CreateConnected(789, 30000, -1 /* epRcvBuf */)
   848  
   849  	if _, _, err := c.EP.Read(nil); err != tcpip.ErrWouldBlock {
   850  		t.Fatalf("got c.EP.Read(nil) = %v, want = %v", err, tcpip.ErrWouldBlock)
   851  	}
   852  
   853  	if err := c.EP.Shutdown(tcpip.ShutdownRead); err != nil {
   854  		t.Fatalf("Shutdown failed: %v", err)
   855  	}
   856  
   857  	if _, _, err := c.EP.Read(nil); err != tcpip.ErrClosedForReceive {
   858  		t.Fatalf("got c.EP.Read(nil) = %v, want = %v", err, tcpip.ErrClosedForReceive)
   859  	}
   860  }
   861  
   862  func TestFullWindowReceive(t *testing.T) {
   863  	c := context.New(t, defaultMTU)
   864  	defer c.Cleanup()
   865  
   866  	c.CreateConnected(789, 30000, 10)
   867  
   868  	we, ch := waiter.NewChannelEntry(nil)
   869  	c.WQ.EventRegister(&we, waiter.EventIn)
   870  	defer c.WQ.EventUnregister(&we)
   871  
   872  	_, _, err := c.EP.Read(nil)
   873  	if err != tcpip.ErrWouldBlock {
   874  		t.Fatalf("Read failed: %v", err)
   875  	}
   876  
   877  	// Fill up the window.
   878  	data := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
   879  	c.SendPacket(data, &context.Headers{
   880  		SrcPort: context.TestPort,
   881  		DstPort: c.Port,
   882  		Flags:   header.TCPFlagAck,
   883  		SeqNum:  790,
   884  		AckNum:  c.IRS.Add(1),
   885  		RcvWnd:  30000,
   886  	})
   887  
   888  	// Wait for receive to be notified.
   889  	select {
   890  	case <-ch:
   891  	case <-time.After(5 * time.Second):
   892  		t.Fatalf("Timed out waiting for data to arrive")
   893  	}
   894  
   895  	// Check that data is acknowledged, and window goes to zero.
   896  	checker.IPv4(t, c.GetPacket(),
   897  		checker.TCP(
   898  			checker.DstPort(context.TestPort),
   899  			checker.SeqNum(uint32(c.IRS)+1),
   900  			checker.AckNum(uint32(790+len(data))),
   901  			checker.TCPFlags(header.TCPFlagAck),
   902  			checker.Window(0),
   903  		),
   904  	)
   905  
   906  	// Receive data and check it.
   907  	v, _, err := c.EP.Read(nil)
   908  	if err != nil {
   909  		t.Fatalf("Read failed: %v", err)
   910  	}
   911  
   912  	if !bytes.Equal(data, v) {
   913  		t.Fatalf("got data = %v, want = %v", v, data)
   914  	}
   915  
   916  	// Check that we get an ACK for the newly non-zero window.
   917  	checker.IPv4(t, c.GetPacket(),
   918  		checker.TCP(
   919  			checker.DstPort(context.TestPort),
   920  			checker.SeqNum(uint32(c.IRS)+1),
   921  			checker.AckNum(uint32(790+len(data))),
   922  			checker.TCPFlags(header.TCPFlagAck),
   923  			checker.Window(10),
   924  		),
   925  	)
   926  }
   927  
   928  func TestNoWindowShrinking(t *testing.T) {
   929  	c := context.New(t, defaultMTU)
   930  	defer c.Cleanup()
   931  
   932  	// Start off with a window size of 10, then shrink it to 5.
   933  	c.CreateConnected(789, 30000, 10)
   934  
   935  	if err := c.EP.SetSockOptInt(tcpip.ReceiveBufferSizeOption, 5); err != nil {
   936  		t.Fatalf("SetSockOpt failed: %v", err)
   937  	}
   938  
   939  	we, ch := waiter.NewChannelEntry(nil)
   940  	c.WQ.EventRegister(&we, waiter.EventIn)
   941  	defer c.WQ.EventUnregister(&we)
   942  
   943  	if _, _, err := c.EP.Read(nil); err != tcpip.ErrWouldBlock {
   944  		t.Fatalf("got c.EP.Read(nil) = %v, want = %v", err, tcpip.ErrWouldBlock)
   945  	}
   946  
   947  	// Send 3 bytes, check that the peer acknowledges them.
   948  	data := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
   949  	c.SendPacket(data[:3], &context.Headers{
   950  		SrcPort: context.TestPort,
   951  		DstPort: c.Port,
   952  		Flags:   header.TCPFlagAck,
   953  		SeqNum:  790,
   954  		AckNum:  c.IRS.Add(1),
   955  		RcvWnd:  30000,
   956  	})
   957  
   958  	// Wait for receive to be notified.
   959  	select {
   960  	case <-ch:
   961  	case <-time.After(5 * time.Second):
   962  		t.Fatalf("Timed out waiting for data to arrive")
   963  	}
   964  
   965  	// Check that data is acknowledged, and that window doesn't go to zero
   966  	// just yet because it was previously set to 10. It must go to 7 now.
   967  	checker.IPv4(t, c.GetPacket(),
   968  		checker.TCP(
   969  			checker.DstPort(context.TestPort),
   970  			checker.SeqNum(uint32(c.IRS)+1),
   971  			checker.AckNum(793),
   972  			checker.TCPFlags(header.TCPFlagAck),
   973  			checker.Window(7),
   974  		),
   975  	)
   976  
   977  	// Send 7 more bytes, check that the window fills up.
   978  	c.SendPacket(data[3:], &context.Headers{
   979  		SrcPort: context.TestPort,
   980  		DstPort: c.Port,
   981  		Flags:   header.TCPFlagAck,
   982  		SeqNum:  793,
   983  		AckNum:  c.IRS.Add(1),
   984  		RcvWnd:  30000,
   985  	})
   986  
   987  	select {
   988  	case <-ch:
   989  	case <-time.After(5 * time.Second):
   990  		t.Fatalf("Timed out waiting for data to arrive")
   991  	}
   992  
   993  	checker.IPv4(t, c.GetPacket(),
   994  		checker.TCP(
   995  			checker.DstPort(context.TestPort),
   996  			checker.SeqNum(uint32(c.IRS)+1),
   997  			checker.AckNum(uint32(790+len(data))),
   998  			checker.TCPFlags(header.TCPFlagAck),
   999  			checker.Window(0),
  1000  		),
  1001  	)
  1002  
  1003  	// Receive data and check it.
  1004  	read := make([]byte, 0, 10)
  1005  	for len(read) < len(data) {
  1006  		v, _, err := c.EP.Read(nil)
  1007  		if err != nil {
  1008  			t.Fatalf("Read failed: %v", err)
  1009  		}
  1010  
  1011  		read = append(read, v...)
  1012  	}
  1013  
  1014  	if !bytes.Equal(data, read) {
  1015  		t.Fatalf("got data = %v, want = %v", read, data)
  1016  	}
  1017  
  1018  	// Check that we get an ACK for the newly non-zero window, which is the
  1019  	// new size.
  1020  	checker.IPv4(t, c.GetPacket(),
  1021  		checker.TCP(
  1022  			checker.DstPort(context.TestPort),
  1023  			checker.SeqNum(uint32(c.IRS)+1),
  1024  			checker.AckNum(uint32(790+len(data))),
  1025  			checker.TCPFlags(header.TCPFlagAck),
  1026  			checker.Window(5),
  1027  		),
  1028  	)
  1029  }
  1030  
  1031  func TestSimpleSend(t *testing.T) {
  1032  	c := context.New(t, defaultMTU)
  1033  	defer c.Cleanup()
  1034  
  1035  	c.CreateConnected(789, 30000, -1 /* epRcvBuf */)
  1036  
  1037  	data := []byte{1, 2, 3}
  1038  	view := buffer.NewView(len(data))
  1039  	copy(view, data)
  1040  
  1041  	if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil {
  1042  		t.Fatalf("Write failed: %v", err)
  1043  	}
  1044  
  1045  	// Check that data is received.
  1046  	b := c.GetPacket()
  1047  	checker.IPv4(t, b,
  1048  		checker.PayloadLen(len(data)+header.TCPMinimumSize),
  1049  		checker.TCP(
  1050  			checker.DstPort(context.TestPort),
  1051  			checker.SeqNum(uint32(c.IRS)+1),
  1052  			checker.AckNum(790),
  1053  			checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)),
  1054  		),
  1055  	)
  1056  
  1057  	if p := b[header.IPv4MinimumSize+header.TCPMinimumSize:]; !bytes.Equal(data, p) {
  1058  		t.Fatalf("got data = %v, want = %v", p, data)
  1059  	}
  1060  
  1061  	// Acknowledge the data.
  1062  	c.SendPacket(nil, &context.Headers{
  1063  		SrcPort: context.TestPort,
  1064  		DstPort: c.Port,
  1065  		Flags:   header.TCPFlagAck,
  1066  		SeqNum:  790,
  1067  		AckNum:  c.IRS.Add(1 + seqnum.Size(len(data))),
  1068  		RcvWnd:  30000,
  1069  	})
  1070  }
  1071  
  1072  func TestZeroWindowSend(t *testing.T) {
  1073  	c := context.New(t, defaultMTU)
  1074  	defer c.Cleanup()
  1075  
  1076  	c.CreateConnected(789, 0, -1 /* epRcvBuf */)
  1077  
  1078  	data := []byte{1, 2, 3}
  1079  	view := buffer.NewView(len(data))
  1080  	copy(view, data)
  1081  
  1082  	_, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{})
  1083  	if err != nil {
  1084  		t.Fatalf("Write failed: %v", err)
  1085  	}
  1086  
  1087  	// Since the window is currently zero, check that no packet is received.
  1088  	c.CheckNoPacket("Packet received when window is zero")
  1089  
  1090  	// Open up the window. Data should be received now.
  1091  	c.SendPacket(nil, &context.Headers{
  1092  		SrcPort: context.TestPort,
  1093  		DstPort: c.Port,
  1094  		Flags:   header.TCPFlagAck,
  1095  		SeqNum:  790,
  1096  		AckNum:  c.IRS.Add(1),
  1097  		RcvWnd:  30000,
  1098  	})
  1099  
  1100  	// Check that data is received.
  1101  	b := c.GetPacket()
  1102  	checker.IPv4(t, b,
  1103  		checker.PayloadLen(len(data)+header.TCPMinimumSize),
  1104  		checker.TCP(
  1105  			checker.DstPort(context.TestPort),
  1106  			checker.SeqNum(uint32(c.IRS)+1),
  1107  			checker.AckNum(790),
  1108  			checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)),
  1109  		),
  1110  	)
  1111  
  1112  	if p := b[header.IPv4MinimumSize+header.TCPMinimumSize:]; !bytes.Equal(data, p) {
  1113  		t.Fatalf("got data = %v, want = %v", p, data)
  1114  	}
  1115  
  1116  	// Acknowledge the data.
  1117  	c.SendPacket(nil, &context.Headers{
  1118  		SrcPort: context.TestPort,
  1119  		DstPort: c.Port,
  1120  		Flags:   header.TCPFlagAck,
  1121  		SeqNum:  790,
  1122  		AckNum:  c.IRS.Add(1 + seqnum.Size(len(data))),
  1123  		RcvWnd:  30000,
  1124  	})
  1125  }
  1126  
  1127  func TestScaledWindowConnect(t *testing.T) {
  1128  	// This test ensures that window scaling is used when the peer
  1129  	// does advertise it and connection is established with Connect().
  1130  	c := context.New(t, defaultMTU)
  1131  	defer c.Cleanup()
  1132  
  1133  	// Set the window size greater than the maximum non-scaled window.
  1134  	c.CreateConnectedWithRawOptions(789, 30000, 65535*3, []byte{
  1135  		header.TCPOptionWS, 3, 0, header.TCPOptionNOP,
  1136  	})
  1137  
  1138  	data := []byte{1, 2, 3}
  1139  	view := buffer.NewView(len(data))
  1140  	copy(view, data)
  1141  
  1142  	if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil {
  1143  		t.Fatalf("Write failed: %v", err)
  1144  	}
  1145  
  1146  	// Check that data is received, and that advertised window is 0xbfff,
  1147  	// that is, that it is scaled.
  1148  	b := c.GetPacket()
  1149  	checker.IPv4(t, b,
  1150  		checker.PayloadLen(len(data)+header.TCPMinimumSize),
  1151  		checker.TCP(
  1152  			checker.DstPort(context.TestPort),
  1153  			checker.SeqNum(uint32(c.IRS)+1),
  1154  			checker.AckNum(790),
  1155  			checker.Window(0xbfff),
  1156  			checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)),
  1157  		),
  1158  	)
  1159  }
  1160  
  1161  func TestNonScaledWindowConnect(t *testing.T) {
  1162  	// This test ensures that window scaling is not used when the peer
  1163  	// doesn't advertise it and connection is established with Connect().
  1164  	c := context.New(t, defaultMTU)
  1165  	defer c.Cleanup()
  1166  
  1167  	// Set the window size greater than the maximum non-scaled window.
  1168  	c.CreateConnected(789, 30000, 65535*3)
  1169  
  1170  	data := []byte{1, 2, 3}
  1171  	view := buffer.NewView(len(data))
  1172  	copy(view, data)
  1173  
  1174  	if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil {
  1175  		t.Fatalf("Write failed: %v", err)
  1176  	}
  1177  
  1178  	// Check that data is received, and that advertised window is 0xffff,
  1179  	// that is, that it's not scaled.
  1180  	b := c.GetPacket()
  1181  	checker.IPv4(t, b,
  1182  		checker.PayloadLen(len(data)+header.TCPMinimumSize),
  1183  		checker.TCP(
  1184  			checker.DstPort(context.TestPort),
  1185  			checker.SeqNum(uint32(c.IRS)+1),
  1186  			checker.AckNum(790),
  1187  			checker.Window(0xffff),
  1188  			checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)),
  1189  		),
  1190  	)
  1191  }
  1192  
  1193  func TestScaledWindowAccept(t *testing.T) {
  1194  	// This test ensures that window scaling is used when the peer
  1195  	// does advertise it and connection is established with Accept().
  1196  	c := context.New(t, defaultMTU)
  1197  	defer c.Cleanup()
  1198  
  1199  	// Create EP and start listening.
  1200  	wq := &waiter.Queue{}
  1201  	ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, wq)
  1202  	if err != nil {
  1203  		t.Fatalf("NewEndpoint failed: %v", err)
  1204  	}
  1205  	defer ep.Close()
  1206  
  1207  	// Set the window size greater than the maximum non-scaled window.
  1208  	if err := ep.SetSockOptInt(tcpip.ReceiveBufferSizeOption, 65535*3); err != nil {
  1209  		t.Fatalf("SetSockOpt failed failed: %v", err)
  1210  	}
  1211  
  1212  	if err := ep.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  1213  		t.Fatalf("Bind failed: %v", err)
  1214  	}
  1215  
  1216  	if err := ep.Listen(10); err != nil {
  1217  		t.Fatalf("Listen failed: %v", err)
  1218  	}
  1219  
  1220  	// Do 3-way handshake.
  1221  	c.PassiveConnectWithOptions(100, 2, header.TCPSynOptions{MSS: defaultIPv4MSS})
  1222  
  1223  	// Try to accept the connection.
  1224  	we, ch := waiter.NewChannelEntry(nil)
  1225  	wq.EventRegister(&we, waiter.EventIn)
  1226  	defer wq.EventUnregister(&we)
  1227  
  1228  	c.EP, _, err = ep.Accept()
  1229  	if err == tcpip.ErrWouldBlock {
  1230  		// Wait for connection to be established.
  1231  		select {
  1232  		case <-ch:
  1233  			c.EP, _, err = ep.Accept()
  1234  			if err != nil {
  1235  				t.Fatalf("Accept failed: %v", err)
  1236  			}
  1237  
  1238  		case <-time.After(1 * time.Second):
  1239  			t.Fatalf("Timed out waiting for accept")
  1240  		}
  1241  	}
  1242  
  1243  	data := []byte{1, 2, 3}
  1244  	view := buffer.NewView(len(data))
  1245  	copy(view, data)
  1246  
  1247  	if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil {
  1248  		t.Fatalf("Write failed: %v", err)
  1249  	}
  1250  
  1251  	// Check that data is received, and that advertised window is 0xbfff,
  1252  	// that is, that it is scaled.
  1253  	b := c.GetPacket()
  1254  	checker.IPv4(t, b,
  1255  		checker.PayloadLen(len(data)+header.TCPMinimumSize),
  1256  		checker.TCP(
  1257  			checker.DstPort(context.TestPort),
  1258  			checker.SeqNum(uint32(c.IRS)+1),
  1259  			checker.AckNum(790),
  1260  			checker.Window(0xbfff),
  1261  			checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)),
  1262  		),
  1263  	)
  1264  }
  1265  
  1266  func TestNonScaledWindowAccept(t *testing.T) {
  1267  	// This test ensures that window scaling is not used when the peer
  1268  	// doesn't advertise it and connection is established with Accept().
  1269  	c := context.New(t, defaultMTU)
  1270  	defer c.Cleanup()
  1271  
  1272  	// Create EP and start listening.
  1273  	wq := &waiter.Queue{}
  1274  	ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, wq)
  1275  	if err != nil {
  1276  		t.Fatalf("NewEndpoint failed: %v", err)
  1277  	}
  1278  	defer ep.Close()
  1279  
  1280  	// Set the window size greater than the maximum non-scaled window.
  1281  	if err := ep.SetSockOptInt(tcpip.ReceiveBufferSizeOption, 65535*3); err != nil {
  1282  		t.Fatalf("SetSockOpt failed failed: %v", err)
  1283  	}
  1284  
  1285  	if err := ep.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  1286  		t.Fatalf("Bind failed: %v", err)
  1287  	}
  1288  
  1289  	if err := ep.Listen(10); err != nil {
  1290  		t.Fatalf("Listen failed: %v", err)
  1291  	}
  1292  
  1293  	// Do 3-way handshake w/ window scaling disabled. The SYN-ACK to the SYN
  1294  	// should not carry the window scaling option.
  1295  	c.PassiveConnect(100, -1, header.TCPSynOptions{MSS: defaultIPv4MSS})
  1296  
  1297  	// Try to accept the connection.
  1298  	we, ch := waiter.NewChannelEntry(nil)
  1299  	wq.EventRegister(&we, waiter.EventIn)
  1300  	defer wq.EventUnregister(&we)
  1301  
  1302  	c.EP, _, err = ep.Accept()
  1303  	if err == tcpip.ErrWouldBlock {
  1304  		// Wait for connection to be established.
  1305  		select {
  1306  		case <-ch:
  1307  			c.EP, _, err = ep.Accept()
  1308  			if err != nil {
  1309  				t.Fatalf("Accept failed: %v", err)
  1310  			}
  1311  
  1312  		case <-time.After(1 * time.Second):
  1313  			t.Fatalf("Timed out waiting for accept")
  1314  		}
  1315  	}
  1316  
  1317  	data := []byte{1, 2, 3}
  1318  	view := buffer.NewView(len(data))
  1319  	copy(view, data)
  1320  
  1321  	if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil {
  1322  		t.Fatalf("Write failed: %v", err)
  1323  	}
  1324  
  1325  	// Check that data is received, and that advertised window is 0xffff,
  1326  	// that is, that it's not scaled.
  1327  	b := c.GetPacket()
  1328  	checker.IPv4(t, b,
  1329  		checker.PayloadLen(len(data)+header.TCPMinimumSize),
  1330  		checker.TCP(
  1331  			checker.DstPort(context.TestPort),
  1332  			checker.SeqNum(uint32(c.IRS)+1),
  1333  			checker.AckNum(790),
  1334  			checker.Window(0xffff),
  1335  			checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)),
  1336  		),
  1337  	)
  1338  }
  1339  
  1340  func TestZeroScaledWindowReceive(t *testing.T) {
  1341  	// This test ensures that the endpoint sends a non-zero window size
  1342  	// advertisement when the scaled window transitions from 0 to non-zero,
  1343  	// but the actual window (not scaled) hasn't gotten to zero.
  1344  	c := context.New(t, defaultMTU)
  1345  	defer c.Cleanup()
  1346  
  1347  	// Set the window size such that a window scale of 4 will be used.
  1348  	const wnd = 65535 * 10
  1349  	const ws = uint32(4)
  1350  	c.CreateConnectedWithRawOptions(789, 30000, wnd, []byte{
  1351  		header.TCPOptionWS, 3, 0, header.TCPOptionNOP,
  1352  	})
  1353  
  1354  	// Write chunks of 50000 bytes.
  1355  	remain := wnd
  1356  	sent := 0
  1357  	data := make([]byte, 50000)
  1358  	for remain > len(data) {
  1359  		c.SendPacket(data, &context.Headers{
  1360  			SrcPort: context.TestPort,
  1361  			DstPort: c.Port,
  1362  			Flags:   header.TCPFlagAck,
  1363  			SeqNum:  seqnum.Value(790 + sent),
  1364  			AckNum:  c.IRS.Add(1),
  1365  			RcvWnd:  30000,
  1366  		})
  1367  		sent += len(data)
  1368  		remain -= len(data)
  1369  		checker.IPv4(t, c.GetPacket(),
  1370  			checker.PayloadLen(header.TCPMinimumSize),
  1371  			checker.TCP(
  1372  				checker.DstPort(context.TestPort),
  1373  				checker.SeqNum(uint32(c.IRS)+1),
  1374  				checker.AckNum(uint32(790+sent)),
  1375  				checker.Window(uint16(remain>>ws)),
  1376  				checker.TCPFlags(header.TCPFlagAck),
  1377  			),
  1378  		)
  1379  	}
  1380  
  1381  	// Make the window non-zero, but the scaled window zero.
  1382  	if remain >= 16 {
  1383  		data = data[:remain-15]
  1384  		c.SendPacket(data, &context.Headers{
  1385  			SrcPort: context.TestPort,
  1386  			DstPort: c.Port,
  1387  			Flags:   header.TCPFlagAck,
  1388  			SeqNum:  seqnum.Value(790 + sent),
  1389  			AckNum:  c.IRS.Add(1),
  1390  			RcvWnd:  30000,
  1391  		})
  1392  		sent += len(data)
  1393  		remain -= len(data)
  1394  		checker.IPv4(t, c.GetPacket(),
  1395  			checker.PayloadLen(header.TCPMinimumSize),
  1396  			checker.TCP(
  1397  				checker.DstPort(context.TestPort),
  1398  				checker.SeqNum(uint32(c.IRS)+1),
  1399  				checker.AckNum(uint32(790+sent)),
  1400  				checker.Window(0),
  1401  				checker.TCPFlags(header.TCPFlagAck),
  1402  			),
  1403  		)
  1404  	}
  1405  
  1406  	// Read some data. An ack should be sent in response to that.
  1407  	v, _, err := c.EP.Read(nil)
  1408  	if err != nil {
  1409  		t.Fatalf("Read failed: %v", err)
  1410  	}
  1411  
  1412  	checker.IPv4(t, c.GetPacket(),
  1413  		checker.PayloadLen(header.TCPMinimumSize),
  1414  		checker.TCP(
  1415  			checker.DstPort(context.TestPort),
  1416  			checker.SeqNum(uint32(c.IRS)+1),
  1417  			checker.AckNum(uint32(790+sent)),
  1418  			checker.Window(uint16(len(v)>>ws)),
  1419  			checker.TCPFlags(header.TCPFlagAck),
  1420  		),
  1421  	)
  1422  }
  1423  
  1424  func TestSegmentMerging(t *testing.T) {
  1425  	tests := []struct {
  1426  		name   string
  1427  		stop   func(tcpip.Endpoint)
  1428  		resume func(tcpip.Endpoint)
  1429  	}{
  1430  		{
  1431  			"stop work",
  1432  			func(ep tcpip.Endpoint) {
  1433  				ep.(interface{ StopWork() }).StopWork()
  1434  			},
  1435  			func(ep tcpip.Endpoint) {
  1436  				ep.(interface{ ResumeWork() }).ResumeWork()
  1437  			},
  1438  		},
  1439  		{
  1440  			"cork",
  1441  			func(ep tcpip.Endpoint) {
  1442  				ep.SetSockOpt(tcpip.CorkOption(1))
  1443  			},
  1444  			func(ep tcpip.Endpoint) {
  1445  				ep.SetSockOpt(tcpip.CorkOption(0))
  1446  			},
  1447  		},
  1448  	}
  1449  
  1450  	for _, test := range tests {
  1451  		t.Run(test.name, func(t *testing.T) {
  1452  			c := context.New(t, defaultMTU)
  1453  			defer c.Cleanup()
  1454  
  1455  			c.CreateConnected(789, 30000, -1 /* epRcvBuf */)
  1456  
  1457  			// Prevent the endpoint from processing packets.
  1458  			test.stop(c.EP)
  1459  
  1460  			var allData []byte
  1461  			for i, data := range [][]byte{{1, 2, 3, 4}, {5, 6, 7}, {8, 9}, {10}, {11}} {
  1462  				allData = append(allData, data...)
  1463  				view := buffer.NewViewFromBytes(data)
  1464  				if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil {
  1465  					t.Fatalf("Write #%d failed: %v", i+1, err)
  1466  				}
  1467  			}
  1468  
  1469  			// Let the endpoint process the segments that we just sent.
  1470  			test.resume(c.EP)
  1471  
  1472  			// Check that data is received.
  1473  			b := c.GetPacket()
  1474  			checker.IPv4(t, b,
  1475  				checker.PayloadLen(len(allData)+header.TCPMinimumSize),
  1476  				checker.TCP(
  1477  					checker.DstPort(context.TestPort),
  1478  					checker.SeqNum(uint32(c.IRS)+1),
  1479  					checker.AckNum(790),
  1480  					checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)),
  1481  				),
  1482  			)
  1483  
  1484  			if got := b[header.IPv4MinimumSize+header.TCPMinimumSize:]; !bytes.Equal(got, allData) {
  1485  				t.Fatalf("got data = %v, want = %v", got, allData)
  1486  			}
  1487  
  1488  			// Acknowledge the data.
  1489  			c.SendPacket(nil, &context.Headers{
  1490  				SrcPort: context.TestPort,
  1491  				DstPort: c.Port,
  1492  				Flags:   header.TCPFlagAck,
  1493  				SeqNum:  790,
  1494  				AckNum:  c.IRS.Add(1 + seqnum.Size(len(allData))),
  1495  				RcvWnd:  30000,
  1496  			})
  1497  		})
  1498  	}
  1499  }
  1500  
  1501  func TestDelay(t *testing.T) {
  1502  	c := context.New(t, defaultMTU)
  1503  	defer c.Cleanup()
  1504  
  1505  	c.CreateConnected(789, 30000, -1 /* epRcvBuf */)
  1506  
  1507  	c.EP.SetSockOpt(tcpip.DelayOption(1))
  1508  
  1509  	var allData []byte
  1510  	for i, data := range [][]byte{{0}, {1, 2, 3, 4}, {5, 6, 7}, {8, 9}, {10}, {11}} {
  1511  		allData = append(allData, data...)
  1512  		view := buffer.NewViewFromBytes(data)
  1513  		if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil {
  1514  			t.Fatalf("Write #%d failed: %v", i+1, err)
  1515  		}
  1516  	}
  1517  
  1518  	seq := c.IRS.Add(1)
  1519  	for _, want := range [][]byte{allData[:1], allData[1:]} {
  1520  		// Check that data is received.
  1521  		b := c.GetPacket()
  1522  		checker.IPv4(t, b,
  1523  			checker.PayloadLen(len(want)+header.TCPMinimumSize),
  1524  			checker.TCP(
  1525  				checker.DstPort(context.TestPort),
  1526  				checker.SeqNum(uint32(seq)),
  1527  				checker.AckNum(790),
  1528  				checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)),
  1529  			),
  1530  		)
  1531  
  1532  		if got := b[header.IPv4MinimumSize+header.TCPMinimumSize:]; !bytes.Equal(got, want) {
  1533  			t.Fatalf("got data = %v, want = %v", got, want)
  1534  		}
  1535  
  1536  		seq = seq.Add(seqnum.Size(len(want)))
  1537  		// Acknowledge the data.
  1538  		c.SendPacket(nil, &context.Headers{
  1539  			SrcPort: context.TestPort,
  1540  			DstPort: c.Port,
  1541  			Flags:   header.TCPFlagAck,
  1542  			SeqNum:  790,
  1543  			AckNum:  seq,
  1544  			RcvWnd:  30000,
  1545  		})
  1546  	}
  1547  }
  1548  
  1549  func TestUndelay(t *testing.T) {
  1550  	c := context.New(t, defaultMTU)
  1551  	defer c.Cleanup()
  1552  
  1553  	c.CreateConnected(789, 30000, -1 /* epRcvBuf */)
  1554  
  1555  	c.EP.SetSockOpt(tcpip.DelayOption(1))
  1556  
  1557  	allData := [][]byte{{0}, {1, 2, 3}}
  1558  	for i, data := range allData {
  1559  		view := buffer.NewViewFromBytes(data)
  1560  		if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil {
  1561  			t.Fatalf("Write #%d failed: %v", i+1, err)
  1562  		}
  1563  	}
  1564  
  1565  	seq := c.IRS.Add(1)
  1566  
  1567  	// Check that data is received.
  1568  	first := c.GetPacket()
  1569  	checker.IPv4(t, first,
  1570  		checker.PayloadLen(len(allData[0])+header.TCPMinimumSize),
  1571  		checker.TCP(
  1572  			checker.DstPort(context.TestPort),
  1573  			checker.SeqNum(uint32(seq)),
  1574  			checker.AckNum(790),
  1575  			checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)),
  1576  		),
  1577  	)
  1578  
  1579  	if got, want := first[header.IPv4MinimumSize+header.TCPMinimumSize:], allData[0]; !bytes.Equal(got, want) {
  1580  		t.Fatalf("got first packet's data = %v, want = %v", got, want)
  1581  	}
  1582  
  1583  	seq = seq.Add(seqnum.Size(len(allData[0])))
  1584  
  1585  	// Check that we don't get the second packet yet.
  1586  	c.CheckNoPacketTimeout("delayed second packet transmitted", 100*time.Millisecond)
  1587  
  1588  	c.EP.SetSockOpt(tcpip.DelayOption(0))
  1589  
  1590  	// Check that data is received.
  1591  	second := c.GetPacket()
  1592  	checker.IPv4(t, second,
  1593  		checker.PayloadLen(len(allData[1])+header.TCPMinimumSize),
  1594  		checker.TCP(
  1595  			checker.DstPort(context.TestPort),
  1596  			checker.SeqNum(uint32(seq)),
  1597  			checker.AckNum(790),
  1598  			checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)),
  1599  		),
  1600  	)
  1601  
  1602  	if got, want := second[header.IPv4MinimumSize+header.TCPMinimumSize:], allData[1]; !bytes.Equal(got, want) {
  1603  		t.Fatalf("got second packet's data = %v, want = %v", got, want)
  1604  	}
  1605  
  1606  	seq = seq.Add(seqnum.Size(len(allData[1])))
  1607  
  1608  	// Acknowledge the data.
  1609  	c.SendPacket(nil, &context.Headers{
  1610  		SrcPort: context.TestPort,
  1611  		DstPort: c.Port,
  1612  		Flags:   header.TCPFlagAck,
  1613  		SeqNum:  790,
  1614  		AckNum:  seq,
  1615  		RcvWnd:  30000,
  1616  	})
  1617  }
  1618  
  1619  func TestMSSNotDelayed(t *testing.T) {
  1620  	tests := []struct {
  1621  		name string
  1622  		fn   func(tcpip.Endpoint)
  1623  	}{
  1624  		{"no-op", func(tcpip.Endpoint) {}},
  1625  		{"delay", func(ep tcpip.Endpoint) { ep.SetSockOpt(tcpip.DelayOption(1)) }},
  1626  		{"cork", func(ep tcpip.Endpoint) { ep.SetSockOpt(tcpip.CorkOption(1)) }},
  1627  	}
  1628  
  1629  	for _, test := range tests {
  1630  		t.Run(test.name, func(t *testing.T) {
  1631  			const maxPayload = 100
  1632  			c := context.New(t, defaultMTU)
  1633  			defer c.Cleanup()
  1634  
  1635  			c.CreateConnectedWithRawOptions(789, 30000, -1 /* epRcvBuf */, []byte{
  1636  				header.TCPOptionMSS, 4, byte(maxPayload / 256), byte(maxPayload % 256),
  1637  			})
  1638  
  1639  			test.fn(c.EP)
  1640  
  1641  			allData := [][]byte{{0}, make([]byte, maxPayload), make([]byte, maxPayload)}
  1642  			for i, data := range allData {
  1643  				view := buffer.NewViewFromBytes(data)
  1644  				if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil {
  1645  					t.Fatalf("Write #%d failed: %v", i+1, err)
  1646  				}
  1647  			}
  1648  
  1649  			seq := c.IRS.Add(1)
  1650  
  1651  			for i, data := range allData {
  1652  				// Check that data is received.
  1653  				packet := c.GetPacket()
  1654  				checker.IPv4(t, packet,
  1655  					checker.PayloadLen(len(data)+header.TCPMinimumSize),
  1656  					checker.TCP(
  1657  						checker.DstPort(context.TestPort),
  1658  						checker.SeqNum(uint32(seq)),
  1659  						checker.AckNum(790),
  1660  						checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)),
  1661  					),
  1662  				)
  1663  
  1664  				if got, want := packet[header.IPv4MinimumSize+header.TCPMinimumSize:], data; !bytes.Equal(got, want) {
  1665  					t.Fatalf("got packet #%d's data = %v, want = %v", i+1, got, want)
  1666  				}
  1667  
  1668  				seq = seq.Add(seqnum.Size(len(data)))
  1669  			}
  1670  
  1671  			// Acknowledge the data.
  1672  			c.SendPacket(nil, &context.Headers{
  1673  				SrcPort: context.TestPort,
  1674  				DstPort: c.Port,
  1675  				Flags:   header.TCPFlagAck,
  1676  				SeqNum:  790,
  1677  				AckNum:  seq,
  1678  				RcvWnd:  30000,
  1679  			})
  1680  		})
  1681  	}
  1682  }
  1683  
  1684  func testBrokenUpWrite(t *testing.T, c *context.Context, maxPayload int) {
  1685  	payloadMultiplier := 10
  1686  	dataLen := payloadMultiplier * maxPayload
  1687  	data := make([]byte, dataLen)
  1688  	for i := range data {
  1689  		data[i] = byte(i)
  1690  	}
  1691  
  1692  	view := buffer.NewView(len(data))
  1693  	copy(view, data)
  1694  
  1695  	if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil {
  1696  		t.Fatalf("Write failed: %v", err)
  1697  	}
  1698  
  1699  	// Check that data is received in chunks.
  1700  	bytesReceived := 0
  1701  	numPackets := 0
  1702  	for bytesReceived != dataLen {
  1703  		b := c.GetPacket()
  1704  		numPackets++
  1705  		tcpHdr := header.TCP(header.IPv4(b).Payload())
  1706  		payloadLen := len(tcpHdr.Payload())
  1707  		checker.IPv4(t, b,
  1708  			checker.TCP(
  1709  				checker.DstPort(context.TestPort),
  1710  				checker.SeqNum(uint32(c.IRS)+1+uint32(bytesReceived)),
  1711  				checker.AckNum(790),
  1712  				checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)),
  1713  			),
  1714  		)
  1715  
  1716  		pdata := data[bytesReceived : bytesReceived+payloadLen]
  1717  		if p := tcpHdr.Payload(); !bytes.Equal(pdata, p) {
  1718  			t.Fatalf("got data = %v, want = %v", p, pdata)
  1719  		}
  1720  		bytesReceived += payloadLen
  1721  		var options []byte
  1722  		if c.TimeStampEnabled {
  1723  			// If timestamp option is enabled, echo back the timestamp and increment
  1724  			// the TSEcr value included in the packet and send that back as the TSVal.
  1725  			parsedOpts := tcpHdr.ParsedOptions()
  1726  			tsOpt := [12]byte{header.TCPOptionNOP, header.TCPOptionNOP}
  1727  			header.EncodeTSOption(parsedOpts.TSEcr+1, parsedOpts.TSVal, tsOpt[2:])
  1728  			options = tsOpt[:]
  1729  		}
  1730  		// Acknowledge the data.
  1731  		c.SendPacket(nil, &context.Headers{
  1732  			SrcPort: context.TestPort,
  1733  			DstPort: c.Port,
  1734  			Flags:   header.TCPFlagAck,
  1735  			SeqNum:  790,
  1736  			AckNum:  c.IRS.Add(1 + seqnum.Size(bytesReceived)),
  1737  			RcvWnd:  30000,
  1738  			TCPOpts: options,
  1739  		})
  1740  	}
  1741  	if numPackets == 1 {
  1742  		t.Fatalf("expected write to be broken up into multiple packets, but got 1 packet")
  1743  	}
  1744  }
  1745  
  1746  func TestSendGreaterThanMTU(t *testing.T) {
  1747  	const maxPayload = 100
  1748  	c := context.New(t, uint32(header.TCPMinimumSize+header.IPv4MinimumSize+maxPayload))
  1749  	defer c.Cleanup()
  1750  
  1751  	c.CreateConnected(789, 30000, -1 /* epRcvBuf */)
  1752  	testBrokenUpWrite(t, c, maxPayload)
  1753  }
  1754  
  1755  func TestSetTTL(t *testing.T) {
  1756  	for _, wantTTL := range []uint8{1, 2, 50, 64, 128, 254, 255} {
  1757  		t.Run(fmt.Sprintf("TTL:%d", wantTTL), func(t *testing.T) {
  1758  			c := context.New(t, 65535)
  1759  			defer c.Cleanup()
  1760  
  1761  			var err *tcpip.Error
  1762  			c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &waiter.Queue{})
  1763  			if err != nil {
  1764  				t.Fatalf("NewEndpoint failed: %v", err)
  1765  			}
  1766  
  1767  			if err := c.EP.SetSockOpt(tcpip.TTLOption(wantTTL)); err != nil {
  1768  				t.Fatalf("SetSockOpt failed: %v", err)
  1769  			}
  1770  
  1771  			if err := c.EP.Connect(tcpip.FullAddress{Addr: context.TestAddr, Port: context.TestPort}); err != tcpip.ErrConnectStarted {
  1772  				t.Fatalf("Unexpected return value from Connect: %v", err)
  1773  			}
  1774  
  1775  			// Receive SYN packet.
  1776  			b := c.GetPacket()
  1777  
  1778  			checker.IPv4(t, b, checker.TTL(wantTTL))
  1779  		})
  1780  	}
  1781  }
  1782  
  1783  func TestActiveSendMSSLessThanMTU(t *testing.T) {
  1784  	const maxPayload = 100
  1785  	c := context.New(t, 65535)
  1786  	defer c.Cleanup()
  1787  
  1788  	c.CreateConnectedWithRawOptions(789, 30000, -1 /* epRcvBuf */, []byte{
  1789  		header.TCPOptionMSS, 4, byte(maxPayload / 256), byte(maxPayload % 256),
  1790  	})
  1791  	testBrokenUpWrite(t, c, maxPayload)
  1792  }
  1793  
  1794  func TestPassiveSendMSSLessThanMTU(t *testing.T) {
  1795  	const maxPayload = 100
  1796  	const mtu = 1200
  1797  	c := context.New(t, mtu)
  1798  	defer c.Cleanup()
  1799  
  1800  	// Create EP and start listening.
  1801  	wq := &waiter.Queue{}
  1802  	ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, wq)
  1803  	if err != nil {
  1804  		t.Fatalf("NewEndpoint failed: %v", err)
  1805  	}
  1806  	defer ep.Close()
  1807  
  1808  	// Set the buffer size to a deterministic size so that we can check the
  1809  	// window scaling option.
  1810  	const rcvBufferSize = 0x20000
  1811  	if err := ep.SetSockOptInt(tcpip.ReceiveBufferSizeOption, rcvBufferSize); err != nil {
  1812  		t.Fatalf("SetSockOpt failed failed: %v", err)
  1813  	}
  1814  
  1815  	if err := ep.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  1816  		t.Fatalf("Bind failed: %v", err)
  1817  	}
  1818  
  1819  	if err := ep.Listen(10); err != nil {
  1820  		t.Fatalf("Listen failed: %v", err)
  1821  	}
  1822  
  1823  	// Do 3-way handshake.
  1824  	c.PassiveConnect(maxPayload, -1, header.TCPSynOptions{MSS: mtu - header.IPv4MinimumSize - header.TCPMinimumSize})
  1825  
  1826  	// Try to accept the connection.
  1827  	we, ch := waiter.NewChannelEntry(nil)
  1828  	wq.EventRegister(&we, waiter.EventIn)
  1829  	defer wq.EventUnregister(&we)
  1830  
  1831  	c.EP, _, err = ep.Accept()
  1832  	if err == tcpip.ErrWouldBlock {
  1833  		// Wait for connection to be established.
  1834  		select {
  1835  		case <-ch:
  1836  			c.EP, _, err = ep.Accept()
  1837  			if err != nil {
  1838  				t.Fatalf("Accept failed: %v", err)
  1839  			}
  1840  
  1841  		case <-time.After(1 * time.Second):
  1842  			t.Fatalf("Timed out waiting for accept")
  1843  		}
  1844  	}
  1845  
  1846  	// Check that data gets properly segmented.
  1847  	testBrokenUpWrite(t, c, maxPayload)
  1848  }
  1849  
  1850  func TestSynCookiePassiveSendMSSLessThanMTU(t *testing.T) {
  1851  	const maxPayload = 536
  1852  	const mtu = 2000
  1853  	c := context.New(t, mtu)
  1854  	defer c.Cleanup()
  1855  
  1856  	// Set the SynRcvd threshold to zero to force a syn cookie based accept
  1857  	// to happen.
  1858  	saved := tcp.SynRcvdCountThreshold
  1859  	defer func() {
  1860  		tcp.SynRcvdCountThreshold = saved
  1861  	}()
  1862  	tcp.SynRcvdCountThreshold = 0
  1863  
  1864  	// Create EP and start listening.
  1865  	wq := &waiter.Queue{}
  1866  	ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, wq)
  1867  	if err != nil {
  1868  		t.Fatalf("NewEndpoint failed: %v", err)
  1869  	}
  1870  	defer ep.Close()
  1871  
  1872  	if err := ep.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  1873  		t.Fatalf("Bind failed: %v", err)
  1874  	}
  1875  
  1876  	if err := ep.Listen(10); err != nil {
  1877  		t.Fatalf("Listen failed: %v", err)
  1878  	}
  1879  
  1880  	// Do 3-way handshake.
  1881  	c.PassiveConnect(maxPayload, -1, header.TCPSynOptions{MSS: mtu - header.IPv4MinimumSize - header.TCPMinimumSize})
  1882  
  1883  	// Try to accept the connection.
  1884  	we, ch := waiter.NewChannelEntry(nil)
  1885  	wq.EventRegister(&we, waiter.EventIn)
  1886  	defer wq.EventUnregister(&we)
  1887  
  1888  	c.EP, _, err = ep.Accept()
  1889  	if err == tcpip.ErrWouldBlock {
  1890  		// Wait for connection to be established.
  1891  		select {
  1892  		case <-ch:
  1893  			c.EP, _, err = ep.Accept()
  1894  			if err != nil {
  1895  				t.Fatalf("Accept failed: %v", err)
  1896  			}
  1897  
  1898  		case <-time.After(1 * time.Second):
  1899  			t.Fatalf("Timed out waiting for accept")
  1900  		}
  1901  	}
  1902  
  1903  	// Check that data gets properly segmented.
  1904  	testBrokenUpWrite(t, c, maxPayload)
  1905  }
  1906  
  1907  func TestForwarderSendMSSLessThanMTU(t *testing.T) {
  1908  	const maxPayload = 100
  1909  	const mtu = 1200
  1910  	c := context.New(t, mtu)
  1911  	defer c.Cleanup()
  1912  
  1913  	s := c.Stack()
  1914  	ch := make(chan *tcpip.Error, 1)
  1915  	f := tcp.NewForwarder(s, 65536, 10, func(r *tcp.ForwarderRequest) {
  1916  		var err *tcpip.Error
  1917  		c.EP, err = r.CreateEndpoint(&c.WQ)
  1918  		ch <- err
  1919  	})
  1920  	s.SetTransportProtocolHandler(tcp.ProtocolNumber, f.HandlePacket)
  1921  
  1922  	// Do 3-way handshake.
  1923  	c.PassiveConnect(maxPayload, -1, header.TCPSynOptions{MSS: mtu - header.IPv4MinimumSize - header.TCPMinimumSize})
  1924  
  1925  	// Wait for connection to be available.
  1926  	select {
  1927  	case err := <-ch:
  1928  		if err != nil {
  1929  			t.Fatalf("Error creating endpoint: %v", err)
  1930  		}
  1931  	case <-time.After(2 * time.Second):
  1932  		t.Fatalf("Timed out waiting for connection")
  1933  	}
  1934  
  1935  	// Check that data gets properly segmented.
  1936  	testBrokenUpWrite(t, c, maxPayload)
  1937  }
  1938  
  1939  func TestSynOptionsOnActiveConnect(t *testing.T) {
  1940  	const mtu = 1400
  1941  	c := context.New(t, mtu)
  1942  	defer c.Cleanup()
  1943  
  1944  	// Create TCP endpoint.
  1945  	var err *tcpip.Error
  1946  	c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &c.WQ)
  1947  	if err != nil {
  1948  		t.Fatalf("NewEndpoint failed: %v", err)
  1949  	}
  1950  
  1951  	// Set the buffer size to a deterministic size so that we can check the
  1952  	// window scaling option.
  1953  	const rcvBufferSize = 0x20000
  1954  	const wndScale = 2
  1955  	if err := c.EP.SetSockOptInt(tcpip.ReceiveBufferSizeOption, rcvBufferSize); err != nil {
  1956  		t.Fatalf("SetSockOpt failed failed: %v", err)
  1957  	}
  1958  
  1959  	// Start connection attempt.
  1960  	we, ch := waiter.NewChannelEntry(nil)
  1961  	c.WQ.EventRegister(&we, waiter.EventOut)
  1962  	defer c.WQ.EventUnregister(&we)
  1963  
  1964  	if err := c.EP.Connect(tcpip.FullAddress{Addr: context.TestAddr, Port: context.TestPort}); err != tcpip.ErrConnectStarted {
  1965  		t.Fatalf("got c.EP.Connect(...) = %v, want = %v", err, tcpip.ErrConnectStarted)
  1966  	}
  1967  
  1968  	// Receive SYN packet.
  1969  	b := c.GetPacket()
  1970  	mss := uint16(mtu - header.IPv4MinimumSize - header.TCPMinimumSize)
  1971  	checker.IPv4(t, b,
  1972  		checker.TCP(
  1973  			checker.DstPort(context.TestPort),
  1974  			checker.TCPFlags(header.TCPFlagSyn),
  1975  			checker.TCPSynOptions(header.TCPSynOptions{MSS: mss, WS: wndScale}),
  1976  		),
  1977  	)
  1978  
  1979  	tcpHdr := header.TCP(header.IPv4(b).Payload())
  1980  	c.IRS = seqnum.Value(tcpHdr.SequenceNumber())
  1981  
  1982  	// Wait for retransmit.
  1983  	time.Sleep(1 * time.Second)
  1984  	checker.IPv4(t, c.GetPacket(),
  1985  		checker.TCP(
  1986  			checker.DstPort(context.TestPort),
  1987  			checker.TCPFlags(header.TCPFlagSyn),
  1988  			checker.SrcPort(tcpHdr.SourcePort()),
  1989  			checker.SeqNum(tcpHdr.SequenceNumber()),
  1990  			checker.TCPSynOptions(header.TCPSynOptions{MSS: mss, WS: wndScale}),
  1991  		),
  1992  	)
  1993  
  1994  	// Send SYN-ACK.
  1995  	iss := seqnum.Value(789)
  1996  	c.SendPacket(nil, &context.Headers{
  1997  		SrcPort: tcpHdr.DestinationPort(),
  1998  		DstPort: tcpHdr.SourcePort(),
  1999  		Flags:   header.TCPFlagSyn | header.TCPFlagAck,
  2000  		SeqNum:  iss,
  2001  		AckNum:  c.IRS.Add(1),
  2002  		RcvWnd:  30000,
  2003  	})
  2004  
  2005  	// Receive ACK packet.
  2006  	checker.IPv4(t, c.GetPacket(),
  2007  		checker.TCP(
  2008  			checker.DstPort(context.TestPort),
  2009  			checker.TCPFlags(header.TCPFlagAck),
  2010  			checker.SeqNum(uint32(c.IRS)+1),
  2011  			checker.AckNum(uint32(iss)+1),
  2012  		),
  2013  	)
  2014  
  2015  	// Wait for connection to be established.
  2016  	select {
  2017  	case <-ch:
  2018  		if err := c.EP.GetSockOpt(tcpip.ErrorOption{}); err != nil {
  2019  			t.Fatalf("GetSockOpt failed: %v", err)
  2020  		}
  2021  	case <-time.After(1 * time.Second):
  2022  		t.Fatalf("Timed out waiting for connection")
  2023  	}
  2024  }
  2025  
  2026  func TestCloseListener(t *testing.T) {
  2027  	c := context.New(t, defaultMTU)
  2028  	defer c.Cleanup()
  2029  
  2030  	// Create listener.
  2031  	var wq waiter.Queue
  2032  	ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &wq)
  2033  	if err != nil {
  2034  		t.Fatalf("NewEndpoint failed: %v", err)
  2035  	}
  2036  
  2037  	if err := ep.Bind(tcpip.FullAddress{}); err != nil {
  2038  		t.Fatalf("Bind failed: %v", err)
  2039  	}
  2040  
  2041  	if err := ep.Listen(10); err != nil {
  2042  		t.Fatalf("Listen failed: %v", err)
  2043  	}
  2044  
  2045  	// Close the listener and measure how long it takes.
  2046  	t0 := time.Now()
  2047  	ep.Close()
  2048  	if diff := time.Now().Sub(t0); diff > 3*time.Second {
  2049  		t.Fatalf("Took too long to close: %v", diff)
  2050  	}
  2051  }
  2052  
  2053  func TestReceiveOnResetConnection(t *testing.T) {
  2054  	c := context.New(t, defaultMTU)
  2055  	defer c.Cleanup()
  2056  
  2057  	c.CreateConnected(789, 30000, -1 /* epRcvBuf */)
  2058  
  2059  	// Send RST segment.
  2060  	c.SendPacket(nil, &context.Headers{
  2061  		SrcPort: context.TestPort,
  2062  		DstPort: c.Port,
  2063  		Flags:   header.TCPFlagRst,
  2064  		SeqNum:  790,
  2065  		RcvWnd:  30000,
  2066  	})
  2067  
  2068  	// Try to read.
  2069  	we, ch := waiter.NewChannelEntry(nil)
  2070  	c.WQ.EventRegister(&we, waiter.EventIn)
  2071  	defer c.WQ.EventUnregister(&we)
  2072  
  2073  loop:
  2074  	for {
  2075  		switch _, _, err := c.EP.Read(nil); err {
  2076  		case tcpip.ErrWouldBlock:
  2077  			select {
  2078  			case <-ch:
  2079  			case <-time.After(1 * time.Second):
  2080  				t.Fatalf("Timed out waiting for reset to arrive")
  2081  			}
  2082  		case tcpip.ErrConnectionReset:
  2083  			break loop
  2084  		default:
  2085  			t.Fatalf("got c.EP.Read(nil) = %v, want = %v", err, tcpip.ErrConnectionReset)
  2086  		}
  2087  	}
  2088  }
  2089  
  2090  func TestSendOnResetConnection(t *testing.T) {
  2091  	c := context.New(t, defaultMTU)
  2092  	defer c.Cleanup()
  2093  
  2094  	c.CreateConnected(789, 30000, -1 /* epRcvBuf */)
  2095  
  2096  	// Send RST segment.
  2097  	c.SendPacket(nil, &context.Headers{
  2098  		SrcPort: context.TestPort,
  2099  		DstPort: c.Port,
  2100  		Flags:   header.TCPFlagRst,
  2101  		SeqNum:  790,
  2102  		RcvWnd:  30000,
  2103  	})
  2104  
  2105  	// Wait for the RST to be received.
  2106  	time.Sleep(1 * time.Second)
  2107  
  2108  	// Try to write.
  2109  	view := buffer.NewView(10)
  2110  	if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != tcpip.ErrConnectionReset {
  2111  		t.Fatalf("got c.EP.Write(...) = %v, want = %v", err, tcpip.ErrConnectionReset)
  2112  	}
  2113  }
  2114  
  2115  func TestFinImmediately(t *testing.T) {
  2116  	c := context.New(t, defaultMTU)
  2117  	defer c.Cleanup()
  2118  
  2119  	c.CreateConnected(789, 30000, -1 /* epRcvBuf */)
  2120  
  2121  	// Shutdown immediately, check that we get a FIN.
  2122  	if err := c.EP.Shutdown(tcpip.ShutdownWrite); err != nil {
  2123  		t.Fatalf("Shutdown failed: %v", err)
  2124  	}
  2125  
  2126  	checker.IPv4(t, c.GetPacket(),
  2127  		checker.PayloadLen(header.TCPMinimumSize),
  2128  		checker.TCP(
  2129  			checker.DstPort(context.TestPort),
  2130  			checker.SeqNum(uint32(c.IRS)+1),
  2131  			checker.AckNum(790),
  2132  			checker.TCPFlags(header.TCPFlagAck|header.TCPFlagFin),
  2133  		),
  2134  	)
  2135  
  2136  	// Ack and send FIN as well.
  2137  	c.SendPacket(nil, &context.Headers{
  2138  		SrcPort: context.TestPort,
  2139  		DstPort: c.Port,
  2140  		Flags:   header.TCPFlagAck | header.TCPFlagFin,
  2141  		SeqNum:  790,
  2142  		AckNum:  c.IRS.Add(2),
  2143  		RcvWnd:  30000,
  2144  	})
  2145  
  2146  	// Check that the stack acks the FIN.
  2147  	checker.IPv4(t, c.GetPacket(),
  2148  		checker.PayloadLen(header.TCPMinimumSize),
  2149  		checker.TCP(
  2150  			checker.DstPort(context.TestPort),
  2151  			checker.SeqNum(uint32(c.IRS)+2),
  2152  			checker.AckNum(791),
  2153  			checker.TCPFlags(header.TCPFlagAck),
  2154  		),
  2155  	)
  2156  }
  2157  
  2158  func TestFinRetransmit(t *testing.T) {
  2159  	c := context.New(t, defaultMTU)
  2160  	defer c.Cleanup()
  2161  
  2162  	c.CreateConnected(789, 30000, -1 /* epRcvBuf */)
  2163  
  2164  	// Shutdown immediately, check that we get a FIN.
  2165  	if err := c.EP.Shutdown(tcpip.ShutdownWrite); err != nil {
  2166  		t.Fatalf("Shutdown failed: %v", err)
  2167  	}
  2168  
  2169  	checker.IPv4(t, c.GetPacket(),
  2170  		checker.PayloadLen(header.TCPMinimumSize),
  2171  		checker.TCP(
  2172  			checker.DstPort(context.TestPort),
  2173  			checker.SeqNum(uint32(c.IRS)+1),
  2174  			checker.AckNum(790),
  2175  			checker.TCPFlags(header.TCPFlagAck|header.TCPFlagFin),
  2176  		),
  2177  	)
  2178  
  2179  	// Don't acknowledge yet. We should get a retransmit of the FIN.
  2180  	checker.IPv4(t, c.GetPacket(),
  2181  		checker.PayloadLen(header.TCPMinimumSize),
  2182  		checker.TCP(
  2183  			checker.DstPort(context.TestPort),
  2184  			checker.SeqNum(uint32(c.IRS)+1),
  2185  			checker.AckNum(790),
  2186  			checker.TCPFlags(header.TCPFlagAck|header.TCPFlagFin),
  2187  		),
  2188  	)
  2189  
  2190  	// Ack and send FIN as well.
  2191  	c.SendPacket(nil, &context.Headers{
  2192  		SrcPort: context.TestPort,
  2193  		DstPort: c.Port,
  2194  		Flags:   header.TCPFlagAck | header.TCPFlagFin,
  2195  		SeqNum:  790,
  2196  		AckNum:  c.IRS.Add(2),
  2197  		RcvWnd:  30000,
  2198  	})
  2199  
  2200  	// Check that the stack acks the FIN.
  2201  	checker.IPv4(t, c.GetPacket(),
  2202  		checker.PayloadLen(header.TCPMinimumSize),
  2203  		checker.TCP(
  2204  			checker.DstPort(context.TestPort),
  2205  			checker.SeqNum(uint32(c.IRS)+2),
  2206  			checker.AckNum(791),
  2207  			checker.TCPFlags(header.TCPFlagAck),
  2208  		),
  2209  	)
  2210  }
  2211  
  2212  func TestFinWithNoPendingData(t *testing.T) {
  2213  	c := context.New(t, defaultMTU)
  2214  	defer c.Cleanup()
  2215  
  2216  	c.CreateConnected(789, 30000, -1 /* epRcvBuf */)
  2217  
  2218  	// Write something out, and have it acknowledged.
  2219  	view := buffer.NewView(10)
  2220  	if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil {
  2221  		t.Fatalf("Write failed: %v", err)
  2222  	}
  2223  
  2224  	next := uint32(c.IRS) + 1
  2225  	checker.IPv4(t, c.GetPacket(),
  2226  		checker.PayloadLen(len(view)+header.TCPMinimumSize),
  2227  		checker.TCP(
  2228  			checker.DstPort(context.TestPort),
  2229  			checker.SeqNum(next),
  2230  			checker.AckNum(790),
  2231  			checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)),
  2232  		),
  2233  	)
  2234  	next += uint32(len(view))
  2235  
  2236  	c.SendPacket(nil, &context.Headers{
  2237  		SrcPort: context.TestPort,
  2238  		DstPort: c.Port,
  2239  		Flags:   header.TCPFlagAck,
  2240  		SeqNum:  790,
  2241  		AckNum:  seqnum.Value(next),
  2242  		RcvWnd:  30000,
  2243  	})
  2244  
  2245  	// Shutdown, check that we get a FIN.
  2246  	if err := c.EP.Shutdown(tcpip.ShutdownWrite); err != nil {
  2247  		t.Fatalf("Shutdown failed: %v", err)
  2248  	}
  2249  
  2250  	checker.IPv4(t, c.GetPacket(),
  2251  		checker.PayloadLen(header.TCPMinimumSize),
  2252  		checker.TCP(
  2253  			checker.DstPort(context.TestPort),
  2254  			checker.SeqNum(next),
  2255  			checker.AckNum(790),
  2256  			checker.TCPFlags(header.TCPFlagAck|header.TCPFlagFin),
  2257  		),
  2258  	)
  2259  	next++
  2260  
  2261  	// Ack and send FIN as well.
  2262  	c.SendPacket(nil, &context.Headers{
  2263  		SrcPort: context.TestPort,
  2264  		DstPort: c.Port,
  2265  		Flags:   header.TCPFlagAck | header.TCPFlagFin,
  2266  		SeqNum:  790,
  2267  		AckNum:  seqnum.Value(next),
  2268  		RcvWnd:  30000,
  2269  	})
  2270  
  2271  	// Check that the stack acks the FIN.
  2272  	checker.IPv4(t, c.GetPacket(),
  2273  		checker.PayloadLen(header.TCPMinimumSize),
  2274  		checker.TCP(
  2275  			checker.DstPort(context.TestPort),
  2276  			checker.SeqNum(next),
  2277  			checker.AckNum(791),
  2278  			checker.TCPFlags(header.TCPFlagAck),
  2279  		),
  2280  	)
  2281  }
  2282  
  2283  func DisabledTestFinWithPendingDataCwndFull(t *testing.T) {
  2284  	c := context.New(t, defaultMTU)
  2285  	defer c.Cleanup()
  2286  
  2287  	c.CreateConnected(789, 30000, -1 /* epRcvBuf */)
  2288  
  2289  	// Write enough segments to fill the congestion window before ACK'ing
  2290  	// any of them.
  2291  	view := buffer.NewView(10)
  2292  	for i := tcp.InitialCwnd; i > 0; i-- {
  2293  		if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil {
  2294  			t.Fatalf("Write failed: %v", err)
  2295  		}
  2296  	}
  2297  
  2298  	next := uint32(c.IRS) + 1
  2299  	for i := tcp.InitialCwnd; i > 0; i-- {
  2300  		checker.IPv4(t, c.GetPacket(),
  2301  			checker.PayloadLen(len(view)+header.TCPMinimumSize),
  2302  			checker.TCP(
  2303  				checker.DstPort(context.TestPort),
  2304  				checker.SeqNum(next),
  2305  				checker.AckNum(790),
  2306  				checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)),
  2307  			),
  2308  		)
  2309  		next += uint32(len(view))
  2310  	}
  2311  
  2312  	// Shutdown the connection, check that the FIN segment isn't sent
  2313  	// because the congestion window doesn't allow it. Wait until a
  2314  	// retransmit is received.
  2315  	if err := c.EP.Shutdown(tcpip.ShutdownWrite); err != nil {
  2316  		t.Fatalf("Shutdown failed: %v", err)
  2317  	}
  2318  
  2319  	checker.IPv4(t, c.GetPacket(),
  2320  		checker.PayloadLen(len(view)+header.TCPMinimumSize),
  2321  		checker.TCP(
  2322  			checker.DstPort(context.TestPort),
  2323  			checker.SeqNum(uint32(c.IRS)+1),
  2324  			checker.AckNum(790),
  2325  			checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)),
  2326  		),
  2327  	)
  2328  
  2329  	// Send the ACK that will allow the FIN to be sent as well.
  2330  	c.SendPacket(nil, &context.Headers{
  2331  		SrcPort: context.TestPort,
  2332  		DstPort: c.Port,
  2333  		Flags:   header.TCPFlagAck,
  2334  		SeqNum:  790,
  2335  		AckNum:  seqnum.Value(next),
  2336  		RcvWnd:  30000,
  2337  	})
  2338  
  2339  	checker.IPv4(t, c.GetPacket(),
  2340  		checker.PayloadLen(header.TCPMinimumSize),
  2341  		checker.TCP(
  2342  			checker.DstPort(context.TestPort),
  2343  			checker.SeqNum(next),
  2344  			checker.AckNum(790),
  2345  			checker.TCPFlags(header.TCPFlagAck|header.TCPFlagFin),
  2346  		),
  2347  	)
  2348  	next++
  2349  
  2350  	// Send a FIN that acknowledges everything. Get an ACK back.
  2351  	c.SendPacket(nil, &context.Headers{
  2352  		SrcPort: context.TestPort,
  2353  		DstPort: c.Port,
  2354  		Flags:   header.TCPFlagAck | header.TCPFlagFin,
  2355  		SeqNum:  790,
  2356  		AckNum:  seqnum.Value(next),
  2357  		RcvWnd:  30000,
  2358  	})
  2359  
  2360  	checker.IPv4(t, c.GetPacket(),
  2361  		checker.PayloadLen(header.TCPMinimumSize),
  2362  		checker.TCP(
  2363  			checker.DstPort(context.TestPort),
  2364  			checker.SeqNum(next),
  2365  			checker.AckNum(791),
  2366  			checker.TCPFlags(header.TCPFlagAck),
  2367  		),
  2368  	)
  2369  }
  2370  
  2371  func TestFinWithPendingData(t *testing.T) {
  2372  	c := context.New(t, defaultMTU)
  2373  	defer c.Cleanup()
  2374  
  2375  	c.CreateConnected(789, 30000, -1 /* epRcvBuf */)
  2376  
  2377  	// Write something out, and acknowledge it to get cwnd to 2.
  2378  	view := buffer.NewView(10)
  2379  	if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil {
  2380  		t.Fatalf("Write failed: %v", err)
  2381  	}
  2382  
  2383  	next := uint32(c.IRS) + 1
  2384  	checker.IPv4(t, c.GetPacket(),
  2385  		checker.PayloadLen(len(view)+header.TCPMinimumSize),
  2386  		checker.TCP(
  2387  			checker.DstPort(context.TestPort),
  2388  			checker.SeqNum(next),
  2389  			checker.AckNum(790),
  2390  			checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)),
  2391  		),
  2392  	)
  2393  	next += uint32(len(view))
  2394  
  2395  	c.SendPacket(nil, &context.Headers{
  2396  		SrcPort: context.TestPort,
  2397  		DstPort: c.Port,
  2398  		Flags:   header.TCPFlagAck,
  2399  		SeqNum:  790,
  2400  		AckNum:  seqnum.Value(next),
  2401  		RcvWnd:  30000,
  2402  	})
  2403  
  2404  	// Write new data, but don't acknowledge it.
  2405  	if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil {
  2406  		t.Fatalf("Write failed: %v", err)
  2407  	}
  2408  
  2409  	checker.IPv4(t, c.GetPacket(),
  2410  		checker.PayloadLen(len(view)+header.TCPMinimumSize),
  2411  		checker.TCP(
  2412  			checker.DstPort(context.TestPort),
  2413  			checker.SeqNum(next),
  2414  			checker.AckNum(790),
  2415  			checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)),
  2416  		),
  2417  	)
  2418  	next += uint32(len(view))
  2419  
  2420  	// Shutdown the connection, check that we do get a FIN.
  2421  	if err := c.EP.Shutdown(tcpip.ShutdownWrite); err != nil {
  2422  		t.Fatalf("Shutdown failed: %v", err)
  2423  	}
  2424  
  2425  	checker.IPv4(t, c.GetPacket(),
  2426  		checker.PayloadLen(header.TCPMinimumSize),
  2427  		checker.TCP(
  2428  			checker.DstPort(context.TestPort),
  2429  			checker.SeqNum(next),
  2430  			checker.AckNum(790),
  2431  			checker.TCPFlags(header.TCPFlagAck|header.TCPFlagFin),
  2432  		),
  2433  	)
  2434  	next++
  2435  
  2436  	// Send a FIN that acknowledges everything. Get an ACK back.
  2437  	c.SendPacket(nil, &context.Headers{
  2438  		SrcPort: context.TestPort,
  2439  		DstPort: c.Port,
  2440  		Flags:   header.TCPFlagAck | header.TCPFlagFin,
  2441  		SeqNum:  790,
  2442  		AckNum:  seqnum.Value(next),
  2443  		RcvWnd:  30000,
  2444  	})
  2445  
  2446  	checker.IPv4(t, c.GetPacket(),
  2447  		checker.PayloadLen(header.TCPMinimumSize),
  2448  		checker.TCP(
  2449  			checker.DstPort(context.TestPort),
  2450  			checker.SeqNum(next),
  2451  			checker.AckNum(791),
  2452  			checker.TCPFlags(header.TCPFlagAck),
  2453  		),
  2454  	)
  2455  }
  2456  
  2457  func TestFinWithPartialAck(t *testing.T) {
  2458  	c := context.New(t, defaultMTU)
  2459  	defer c.Cleanup()
  2460  
  2461  	c.CreateConnected(789, 30000, -1 /* epRcvBuf */)
  2462  
  2463  	// Write something out, and acknowledge it to get cwnd to 2. Also send
  2464  	// FIN from the test side.
  2465  	view := buffer.NewView(10)
  2466  	if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil {
  2467  		t.Fatalf("Write failed: %v", err)
  2468  	}
  2469  
  2470  	next := uint32(c.IRS) + 1
  2471  	checker.IPv4(t, c.GetPacket(),
  2472  		checker.PayloadLen(len(view)+header.TCPMinimumSize),
  2473  		checker.TCP(
  2474  			checker.DstPort(context.TestPort),
  2475  			checker.SeqNum(next),
  2476  			checker.AckNum(790),
  2477  			checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)),
  2478  		),
  2479  	)
  2480  	next += uint32(len(view))
  2481  
  2482  	c.SendPacket(nil, &context.Headers{
  2483  		SrcPort: context.TestPort,
  2484  		DstPort: c.Port,
  2485  		Flags:   header.TCPFlagAck | header.TCPFlagFin,
  2486  		SeqNum:  790,
  2487  		AckNum:  seqnum.Value(next),
  2488  		RcvWnd:  30000,
  2489  	})
  2490  
  2491  	// Check that we get an ACK for the fin.
  2492  	checker.IPv4(t, c.GetPacket(),
  2493  		checker.PayloadLen(header.TCPMinimumSize),
  2494  		checker.TCP(
  2495  			checker.DstPort(context.TestPort),
  2496  			checker.SeqNum(next),
  2497  			checker.AckNum(791),
  2498  			checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)),
  2499  		),
  2500  	)
  2501  
  2502  	// Write new data, but don't acknowledge it.
  2503  	if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil {
  2504  		t.Fatalf("Write failed: %v", err)
  2505  	}
  2506  
  2507  	checker.IPv4(t, c.GetPacket(),
  2508  		checker.PayloadLen(len(view)+header.TCPMinimumSize),
  2509  		checker.TCP(
  2510  			checker.DstPort(context.TestPort),
  2511  			checker.SeqNum(next),
  2512  			checker.AckNum(791),
  2513  			checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)),
  2514  		),
  2515  	)
  2516  	next += uint32(len(view))
  2517  
  2518  	// Shutdown the connection, check that we do get a FIN.
  2519  	if err := c.EP.Shutdown(tcpip.ShutdownWrite); err != nil {
  2520  		t.Fatalf("Shutdown failed: %v", err)
  2521  	}
  2522  
  2523  	checker.IPv4(t, c.GetPacket(),
  2524  		checker.PayloadLen(header.TCPMinimumSize),
  2525  		checker.TCP(
  2526  			checker.DstPort(context.TestPort),
  2527  			checker.SeqNum(next),
  2528  			checker.AckNum(791),
  2529  			checker.TCPFlags(header.TCPFlagAck|header.TCPFlagFin),
  2530  		),
  2531  	)
  2532  	next++
  2533  
  2534  	// Send an ACK for the data, but not for the FIN yet.
  2535  	c.SendPacket(nil, &context.Headers{
  2536  		SrcPort: context.TestPort,
  2537  		DstPort: c.Port,
  2538  		Flags:   header.TCPFlagAck,
  2539  		SeqNum:  791,
  2540  		AckNum:  seqnum.Value(next - 1),
  2541  		RcvWnd:  30000,
  2542  	})
  2543  
  2544  	// Check that we don't get a retransmit of the FIN.
  2545  	c.CheckNoPacketTimeout("FIN retransmitted when data was ack'd", 100*time.Millisecond)
  2546  
  2547  	// Ack the FIN.
  2548  	c.SendPacket(nil, &context.Headers{
  2549  		SrcPort: context.TestPort,
  2550  		DstPort: c.Port,
  2551  		Flags:   header.TCPFlagAck | header.TCPFlagFin,
  2552  		SeqNum:  791,
  2553  		AckNum:  seqnum.Value(next),
  2554  		RcvWnd:  30000,
  2555  	})
  2556  }
  2557  
  2558  func TestUpdateListenBacklog(t *testing.T) {
  2559  	c := context.New(t, defaultMTU)
  2560  	defer c.Cleanup()
  2561  
  2562  	// Create listener.
  2563  	var wq waiter.Queue
  2564  	ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &wq)
  2565  	if err != nil {
  2566  		t.Fatalf("NewEndpoint failed: %v", err)
  2567  	}
  2568  
  2569  	if err := ep.Bind(tcpip.FullAddress{}); err != nil {
  2570  		t.Fatalf("Bind failed: %v", err)
  2571  	}
  2572  
  2573  	if err := ep.Listen(10); err != nil {
  2574  		t.Fatalf("Listen failed: %v", err)
  2575  	}
  2576  
  2577  	// Update the backlog with another Listen() on the same endpoint.
  2578  	if err := ep.Listen(20); err != nil {
  2579  		t.Fatalf("Listen failed to update backlog: %v", err)
  2580  	}
  2581  
  2582  	ep.Close()
  2583  }
  2584  
  2585  func scaledSendWindow(t *testing.T, scale uint8) {
  2586  	// This test ensures that the endpoint is using the right scaling by
  2587  	// sending a buffer that is larger than the window size, and ensuring
  2588  	// that the endpoint doesn't send more than allowed.
  2589  	c := context.New(t, defaultMTU)
  2590  	defer c.Cleanup()
  2591  
  2592  	maxPayload := defaultMTU - header.IPv4MinimumSize - header.TCPMinimumSize
  2593  	c.CreateConnectedWithRawOptions(789, 0, -1 /* epRcvBuf */, []byte{
  2594  		header.TCPOptionMSS, 4, byte(maxPayload / 256), byte(maxPayload % 256),
  2595  		header.TCPOptionWS, 3, scale, header.TCPOptionNOP,
  2596  	})
  2597  
  2598  	// Open up the window with a scaled value.
  2599  	c.SendPacket(nil, &context.Headers{
  2600  		SrcPort: context.TestPort,
  2601  		DstPort: c.Port,
  2602  		Flags:   header.TCPFlagAck,
  2603  		SeqNum:  790,
  2604  		AckNum:  c.IRS.Add(1),
  2605  		RcvWnd:  1,
  2606  	})
  2607  
  2608  	// Send some data. Check that it's capped by the window size.
  2609  	view := buffer.NewView(65535)
  2610  	if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil {
  2611  		t.Fatalf("Write failed: %v", err)
  2612  	}
  2613  
  2614  	// Check that only data that fits in the scaled window is sent.
  2615  	checker.IPv4(t, c.GetPacket(),
  2616  		checker.PayloadLen((1<<scale)+header.TCPMinimumSize),
  2617  		checker.TCP(
  2618  			checker.DstPort(context.TestPort),
  2619  			checker.SeqNum(uint32(c.IRS)+1),
  2620  			checker.AckNum(790),
  2621  			checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)),
  2622  		),
  2623  	)
  2624  
  2625  	// Reset the connection to free resources.
  2626  	c.SendPacket(nil, &context.Headers{
  2627  		SrcPort: context.TestPort,
  2628  		DstPort: c.Port,
  2629  		Flags:   header.TCPFlagRst,
  2630  		SeqNum:  790,
  2631  	})
  2632  }
  2633  
  2634  func TestScaledSendWindow(t *testing.T) {
  2635  	for scale := uint8(0); scale <= 14; scale++ {
  2636  		scaledSendWindow(t, scale)
  2637  	}
  2638  }
  2639  
  2640  func TestReceivedValidSegmentCountIncrement(t *testing.T) {
  2641  	c := context.New(t, defaultMTU)
  2642  	defer c.Cleanup()
  2643  	c.CreateConnected(789, 30000, -1 /* epRcvBuf */)
  2644  	stats := c.Stack().Stats()
  2645  	want := stats.TCP.ValidSegmentsReceived.Value() + 1
  2646  
  2647  	c.SendPacket(nil, &context.Headers{
  2648  		SrcPort: context.TestPort,
  2649  		DstPort: c.Port,
  2650  		Flags:   header.TCPFlagAck,
  2651  		SeqNum:  seqnum.Value(790),
  2652  		AckNum:  c.IRS.Add(1),
  2653  		RcvWnd:  30000,
  2654  	})
  2655  
  2656  	if got := stats.TCP.ValidSegmentsReceived.Value(); got != want {
  2657  		t.Errorf("got stats.TCP.ValidSegmentsReceived.Value() = %v, want = %v", got, want)
  2658  	}
  2659  }
  2660  
  2661  func TestReceivedInvalidSegmentCountIncrement(t *testing.T) {
  2662  	c := context.New(t, defaultMTU)
  2663  	defer c.Cleanup()
  2664  	c.CreateConnected(789, 30000, -1 /* epRcvBuf */)
  2665  	stats := c.Stack().Stats()
  2666  	want := stats.TCP.InvalidSegmentsReceived.Value() + 1
  2667  	vv := c.BuildSegment(nil, &context.Headers{
  2668  		SrcPort: context.TestPort,
  2669  		DstPort: c.Port,
  2670  		Flags:   header.TCPFlagAck,
  2671  		SeqNum:  seqnum.Value(790),
  2672  		AckNum:  c.IRS.Add(1),
  2673  		RcvWnd:  30000,
  2674  	})
  2675  	tcpbuf := vv.First()[header.IPv4MinimumSize:]
  2676  	tcpbuf[header.TCPDataOffset] = ((header.TCPMinimumSize - 1) / 4) << 4
  2677  
  2678  	c.SendSegment(vv)
  2679  
  2680  	if got := stats.TCP.InvalidSegmentsReceived.Value(); got != want {
  2681  		t.Errorf("got stats.TCP.InvalidSegmentsReceived.Value() = %v, want = %v", got, want)
  2682  	}
  2683  }
  2684  
  2685  func TestReceivedIncorrectChecksumIncrement(t *testing.T) {
  2686  	c := context.New(t, defaultMTU)
  2687  	defer c.Cleanup()
  2688  	c.CreateConnected(789, 30000, -1 /* epRcvBuf */)
  2689  	stats := c.Stack().Stats()
  2690  	want := stats.TCP.ChecksumErrors.Value() + 1
  2691  	vv := c.BuildSegment([]byte{0x1, 0x2, 0x3}, &context.Headers{
  2692  		SrcPort: context.TestPort,
  2693  		DstPort: c.Port,
  2694  		Flags:   header.TCPFlagAck,
  2695  		SeqNum:  seqnum.Value(790),
  2696  		AckNum:  c.IRS.Add(1),
  2697  		RcvWnd:  30000,
  2698  	})
  2699  	tcpbuf := vv.First()[header.IPv4MinimumSize:]
  2700  	// Overwrite a byte in the payload which should cause checksum
  2701  	// verification to fail.
  2702  	tcpbuf[(tcpbuf[header.TCPDataOffset]>>4)*4] = 0x4
  2703  
  2704  	c.SendSegment(vv)
  2705  
  2706  	if got := stats.TCP.ChecksumErrors.Value(); got != want {
  2707  		t.Errorf("got stats.TCP.ChecksumErrors.Value() = %d, want = %d", got, want)
  2708  	}
  2709  }
  2710  
  2711  func TestReceivedSegmentQueuing(t *testing.T) {
  2712  	// This test sends 200 segments containing a few bytes each to an
  2713  	// endpoint and checks that they're all received and acknowledged by
  2714  	// the endpoint, that is, that none of the segments are dropped by
  2715  	// internal queues.
  2716  	c := context.New(t, defaultMTU)
  2717  	defer c.Cleanup()
  2718  
  2719  	c.CreateConnected(789, 30000, -1 /* epRcvBuf */)
  2720  
  2721  	// Send 200 segments.
  2722  	data := []byte{1, 2, 3}
  2723  	for i := 0; i < 200; i++ {
  2724  		c.SendPacket(data, &context.Headers{
  2725  			SrcPort: context.TestPort,
  2726  			DstPort: c.Port,
  2727  			Flags:   header.TCPFlagAck,
  2728  			SeqNum:  seqnum.Value(790 + i*len(data)),
  2729  			AckNum:  c.IRS.Add(1),
  2730  			RcvWnd:  30000,
  2731  		})
  2732  	}
  2733  
  2734  	// Receive ACKs for all segments.
  2735  	last := seqnum.Value(790 + 200*len(data))
  2736  	for {
  2737  		b := c.GetPacket()
  2738  		checker.IPv4(t, b,
  2739  			checker.TCP(
  2740  				checker.DstPort(context.TestPort),
  2741  				checker.SeqNum(uint32(c.IRS)+1),
  2742  				checker.TCPFlags(header.TCPFlagAck),
  2743  			),
  2744  		)
  2745  		tcpHdr := header.TCP(header.IPv4(b).Payload())
  2746  		ack := seqnum.Value(tcpHdr.AckNumber())
  2747  		if ack == last {
  2748  			break
  2749  		}
  2750  
  2751  		if last.LessThan(ack) {
  2752  			t.Fatalf("Acknowledge (%v) beyond the expected (%v)", ack, last)
  2753  		}
  2754  	}
  2755  }
  2756  
  2757  func TestReadAfterClosedState(t *testing.T) {
  2758  	// This test ensures that calling Read() or Peek() after the endpoint
  2759  	// has transitioned to closedState still works if there is pending
  2760  	// data. To transition to stateClosed without calling Close(), we must
  2761  	// shutdown the send path and the peer must send its own FIN.
  2762  	c := context.New(t, defaultMTU)
  2763  	defer c.Cleanup()
  2764  
  2765  	c.CreateConnected(789, 30000, -1 /* epRcvBuf */)
  2766  
  2767  	we, ch := waiter.NewChannelEntry(nil)
  2768  	c.WQ.EventRegister(&we, waiter.EventIn)
  2769  	defer c.WQ.EventUnregister(&we)
  2770  
  2771  	if _, _, err := c.EP.Read(nil); err != tcpip.ErrWouldBlock {
  2772  		t.Fatalf("got c.EP.Read(nil) = %v, want = %v", err, tcpip.ErrWouldBlock)
  2773  	}
  2774  
  2775  	// Shutdown immediately for write, check that we get a FIN.
  2776  	if err := c.EP.Shutdown(tcpip.ShutdownWrite); err != nil {
  2777  		t.Fatalf("Shutdown failed: %v", err)
  2778  	}
  2779  
  2780  	checker.IPv4(t, c.GetPacket(),
  2781  		checker.PayloadLen(header.TCPMinimumSize),
  2782  		checker.TCP(
  2783  			checker.DstPort(context.TestPort),
  2784  			checker.SeqNum(uint32(c.IRS)+1),
  2785  			checker.AckNum(790),
  2786  			checker.TCPFlags(header.TCPFlagAck|header.TCPFlagFin),
  2787  		),
  2788  	)
  2789  
  2790  	if got, want := tcp.EndpointState(c.EP.State()), tcp.StateFinWait1; got != want {
  2791  		t.Errorf("Unexpected endpoint state: want %v, got %v", want, got)
  2792  	}
  2793  
  2794  	// Send some data and acknowledge the FIN.
  2795  	data := []byte{1, 2, 3}
  2796  	c.SendPacket(data, &context.Headers{
  2797  		SrcPort: context.TestPort,
  2798  		DstPort: c.Port,
  2799  		Flags:   header.TCPFlagAck | header.TCPFlagFin,
  2800  		SeqNum:  790,
  2801  		AckNum:  c.IRS.Add(2),
  2802  		RcvWnd:  30000,
  2803  	})
  2804  
  2805  	// Check that ACK is received.
  2806  	checker.IPv4(t, c.GetPacket(),
  2807  		checker.TCP(
  2808  			checker.DstPort(context.TestPort),
  2809  			checker.SeqNum(uint32(c.IRS)+2),
  2810  			checker.AckNum(uint32(791+len(data))),
  2811  			checker.TCPFlags(header.TCPFlagAck),
  2812  		),
  2813  	)
  2814  
  2815  	// Give the stack the chance to transition to closed state. Note that since
  2816  	// both the sender and receiver are now closed, we effectively skip the
  2817  	// TIME-WAIT state.
  2818  	time.Sleep(1 * time.Second)
  2819  
  2820  	if got, want := tcp.EndpointState(c.EP.State()), tcp.StateClose; got != want {
  2821  		t.Errorf("Unexpected endpoint state: want %v, got %v", want, got)
  2822  	}
  2823  
  2824  	// Wait for receive to be notified.
  2825  	select {
  2826  	case <-ch:
  2827  	case <-time.After(1 * time.Second):
  2828  		t.Fatalf("Timed out waiting for data to arrive")
  2829  	}
  2830  
  2831  	// Check that peek works.
  2832  	peekBuf := make([]byte, 10)
  2833  	n, _, err := c.EP.Peek([][]byte{peekBuf})
  2834  	if err != nil {
  2835  		t.Fatalf("Peek failed: %v", err)
  2836  	}
  2837  
  2838  	peekBuf = peekBuf[:n]
  2839  	if !bytes.Equal(data, peekBuf) {
  2840  		t.Fatalf("got data = %v, want = %v", peekBuf, data)
  2841  	}
  2842  
  2843  	// Receive data.
  2844  	v, _, err := c.EP.Read(nil)
  2845  	if err != nil {
  2846  		t.Fatalf("Read failed: %v", err)
  2847  	}
  2848  
  2849  	if !bytes.Equal(data, v) {
  2850  		t.Fatalf("got data = %v, want = %v", v, data)
  2851  	}
  2852  
  2853  	// Now that we drained the queue, check that functions fail with the
  2854  	// right error code.
  2855  	if _, _, err := c.EP.Read(nil); err != tcpip.ErrClosedForReceive {
  2856  		t.Fatalf("got c.EP.Read(nil) = %v, want = %v", err, tcpip.ErrClosedForReceive)
  2857  	}
  2858  
  2859  	if _, _, err := c.EP.Peek([][]byte{peekBuf}); err != tcpip.ErrClosedForReceive {
  2860  		t.Fatalf("got c.EP.Peek(...) = %v, want = %v", err, tcpip.ErrClosedForReceive)
  2861  	}
  2862  }
  2863  
  2864  func TestReusePort(t *testing.T) {
  2865  	// This test ensures that ports are immediately available for reuse
  2866  	// after Close on the endpoints using them returns.
  2867  	c := context.New(t, defaultMTU)
  2868  	defer c.Cleanup()
  2869  
  2870  	// First case, just an endpoint that was bound.
  2871  	var err *tcpip.Error
  2872  	c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &waiter.Queue{})
  2873  	if err != nil {
  2874  		t.Fatalf("NewEndpoint failed; %v", err)
  2875  	}
  2876  	if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  2877  		t.Fatalf("Bind failed: %v", err)
  2878  	}
  2879  
  2880  	c.EP.Close()
  2881  	c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &waiter.Queue{})
  2882  	if err != nil {
  2883  		t.Fatalf("NewEndpoint failed; %v", err)
  2884  	}
  2885  	if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  2886  		t.Fatalf("Bind failed: %v", err)
  2887  	}
  2888  	c.EP.Close()
  2889  
  2890  	// Second case, an endpoint that was bound and is connecting..
  2891  	c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &waiter.Queue{})
  2892  	if err != nil {
  2893  		t.Fatalf("NewEndpoint failed; %v", err)
  2894  	}
  2895  	if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  2896  		t.Fatalf("Bind failed: %v", err)
  2897  	}
  2898  	if err := c.EP.Connect(tcpip.FullAddress{Addr: context.TestAddr, Port: context.TestPort}); err != tcpip.ErrConnectStarted {
  2899  		t.Fatalf("got c.EP.Connect(...) = %v, want = %v", err, tcpip.ErrConnectStarted)
  2900  	}
  2901  	c.EP.Close()
  2902  
  2903  	c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &waiter.Queue{})
  2904  	if err != nil {
  2905  		t.Fatalf("NewEndpoint failed; %v", err)
  2906  	}
  2907  	if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  2908  		t.Fatalf("Bind failed: %v", err)
  2909  	}
  2910  	c.EP.Close()
  2911  
  2912  	// Third case, an endpoint that was bound and is listening.
  2913  	c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &waiter.Queue{})
  2914  	if err != nil {
  2915  		t.Fatalf("NewEndpoint failed; %v", err)
  2916  	}
  2917  	if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  2918  		t.Fatalf("Bind failed: %v", err)
  2919  	}
  2920  	if err := c.EP.Listen(10); err != nil {
  2921  		t.Fatalf("Listen failed: %v", err)
  2922  	}
  2923  	c.EP.Close()
  2924  
  2925  	c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &waiter.Queue{})
  2926  	if err != nil {
  2927  		t.Fatalf("NewEndpoint failed; %v", err)
  2928  	}
  2929  	if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  2930  		t.Fatalf("Bind failed: %v", err)
  2931  	}
  2932  	if err := c.EP.Listen(10); err != nil {
  2933  		t.Fatalf("Listen failed: %v", err)
  2934  	}
  2935  }
  2936  
  2937  func checkRecvBufferSize(t *testing.T, ep tcpip.Endpoint, v int) {
  2938  	t.Helper()
  2939  
  2940  	s, err := ep.GetSockOptInt(tcpip.ReceiveBufferSizeOption)
  2941  	if err != nil {
  2942  		t.Fatalf("GetSockOpt failed: %v", err)
  2943  	}
  2944  
  2945  	if int(s) != v {
  2946  		t.Fatalf("got receive buffer size = %v, want = %v", s, v)
  2947  	}
  2948  }
  2949  
  2950  func checkSendBufferSize(t *testing.T, ep tcpip.Endpoint, v int) {
  2951  	t.Helper()
  2952  
  2953  	s, err := ep.GetSockOptInt(tcpip.SendBufferSizeOption)
  2954  	if err != nil {
  2955  		t.Fatalf("GetSockOpt failed: %v", err)
  2956  	}
  2957  
  2958  	if int(s) != v {
  2959  		t.Fatalf("got send buffer size = %v, want = %v", s, v)
  2960  	}
  2961  }
  2962  
  2963  func TestDefaultBufferSizes(t *testing.T) {
  2964  	s := stack.New(stack.Options{
  2965  		NetworkProtocols:   []stack.NetworkProtocol{ipv4.NewProtocol()},
  2966  		TransportProtocols: []stack.TransportProtocol{tcp.NewProtocol()},
  2967  	})
  2968  
  2969  	// Check the default values.
  2970  	ep, err := s.NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &waiter.Queue{})
  2971  	if err != nil {
  2972  		t.Fatalf("NewEndpoint failed; %v", err)
  2973  	}
  2974  	defer func() {
  2975  		if ep != nil {
  2976  			ep.Close()
  2977  		}
  2978  	}()
  2979  
  2980  	checkSendBufferSize(t, ep, tcp.DefaultSendBufferSize)
  2981  	checkRecvBufferSize(t, ep, tcp.DefaultReceiveBufferSize)
  2982  
  2983  	// Change the default send buffer size.
  2984  	if err := s.SetTransportProtocolOption(tcp.ProtocolNumber, tcp.SendBufferSizeOption{1, tcp.DefaultSendBufferSize * 2, tcp.DefaultSendBufferSize * 20}); err != nil {
  2985  		t.Fatalf("SetTransportProtocolOption failed: %v", err)
  2986  	}
  2987  
  2988  	ep.Close()
  2989  	ep, err = s.NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &waiter.Queue{})
  2990  	if err != nil {
  2991  		t.Fatalf("NewEndpoint failed; %v", err)
  2992  	}
  2993  
  2994  	checkSendBufferSize(t, ep, tcp.DefaultSendBufferSize*2)
  2995  	checkRecvBufferSize(t, ep, tcp.DefaultReceiveBufferSize)
  2996  
  2997  	// Change the default receive buffer size.
  2998  	if err := s.SetTransportProtocolOption(tcp.ProtocolNumber, tcp.ReceiveBufferSizeOption{1, tcp.DefaultReceiveBufferSize * 3, tcp.DefaultReceiveBufferSize * 30}); err != nil {
  2999  		t.Fatalf("SetTransportProtocolOption failed: %v", err)
  3000  	}
  3001  
  3002  	ep.Close()
  3003  	ep, err = s.NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &waiter.Queue{})
  3004  	if err != nil {
  3005  		t.Fatalf("NewEndpoint failed; %v", err)
  3006  	}
  3007  
  3008  	checkSendBufferSize(t, ep, tcp.DefaultSendBufferSize*2)
  3009  	checkRecvBufferSize(t, ep, tcp.DefaultReceiveBufferSize*3)
  3010  }
  3011  
  3012  func TestMinMaxBufferSizes(t *testing.T) {
  3013  	s := stack.New(stack.Options{
  3014  		NetworkProtocols:   []stack.NetworkProtocol{ipv4.NewProtocol()},
  3015  		TransportProtocols: []stack.TransportProtocol{tcp.NewProtocol()},
  3016  	})
  3017  
  3018  	// Check the default values.
  3019  	ep, err := s.NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &waiter.Queue{})
  3020  	if err != nil {
  3021  		t.Fatalf("NewEndpoint failed; %v", err)
  3022  	}
  3023  	defer ep.Close()
  3024  
  3025  	// Change the min/max values for send/receive
  3026  	if err := s.SetTransportProtocolOption(tcp.ProtocolNumber, tcp.ReceiveBufferSizeOption{200, tcp.DefaultReceiveBufferSize * 2, tcp.DefaultReceiveBufferSize * 20}); err != nil {
  3027  		t.Fatalf("SetTransportProtocolOption failed: %v", err)
  3028  	}
  3029  
  3030  	if err := s.SetTransportProtocolOption(tcp.ProtocolNumber, tcp.SendBufferSizeOption{300, tcp.DefaultSendBufferSize * 3, tcp.DefaultSendBufferSize * 30}); err != nil {
  3031  		t.Fatalf("SetTransportProtocolOption failed: %v", err)
  3032  	}
  3033  
  3034  	// Set values below the min.
  3035  	if err := ep.SetSockOptInt(tcpip.ReceiveBufferSizeOption, 199); err != nil {
  3036  		t.Fatalf("GetSockOpt failed: %v", err)
  3037  	}
  3038  
  3039  	checkRecvBufferSize(t, ep, 200)
  3040  
  3041  	if err := ep.SetSockOptInt(tcpip.SendBufferSizeOption, 299); err != nil {
  3042  		t.Fatalf("GetSockOpt failed: %v", err)
  3043  	}
  3044  
  3045  	checkSendBufferSize(t, ep, 300)
  3046  
  3047  	// Set values above the max.
  3048  	if err := ep.SetSockOptInt(tcpip.ReceiveBufferSizeOption, 1+tcp.DefaultReceiveBufferSize*20); err != nil {
  3049  		t.Fatalf("GetSockOpt failed: %v", err)
  3050  	}
  3051  
  3052  	checkRecvBufferSize(t, ep, tcp.DefaultReceiveBufferSize*20)
  3053  
  3054  	if err := ep.SetSockOptInt(tcpip.SendBufferSizeOption, 1+tcp.DefaultSendBufferSize*30); err != nil {
  3055  		t.Fatalf("GetSockOpt failed: %v", err)
  3056  	}
  3057  
  3058  	checkSendBufferSize(t, ep, tcp.DefaultSendBufferSize*30)
  3059  }
  3060  
  3061  func TestBindToDeviceOption(t *testing.T) {
  3062  	s := stack.New(stack.Options{
  3063  		NetworkProtocols:   []stack.NetworkProtocol{ipv4.NewProtocol()},
  3064  		TransportProtocols: []stack.TransportProtocol{tcp.NewProtocol()}})
  3065  
  3066  	ep, err := s.NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &waiter.Queue{})
  3067  	if err != nil {
  3068  		t.Fatalf("NewEndpoint failed; %v", err)
  3069  	}
  3070  	defer ep.Close()
  3071  
  3072  	if err := s.CreateNamedNIC(321, "my_device", loopback.New()); err != nil {
  3073  		t.Errorf("CreateNamedNIC failed: %v", err)
  3074  	}
  3075  
  3076  	// Make an nameless NIC.
  3077  	if err := s.CreateNIC(54321, loopback.New()); err != nil {
  3078  		t.Errorf("CreateNIC failed: %v", err)
  3079  	}
  3080  
  3081  	// strPtr is used instead of taking the address of string literals, which is
  3082  	// a compiler error.
  3083  	strPtr := func(s string) *string {
  3084  		return &s
  3085  	}
  3086  
  3087  	testActions := []struct {
  3088  		name                 string
  3089  		setBindToDevice      *string
  3090  		setBindToDeviceError *tcpip.Error
  3091  		getBindToDevice      tcpip.BindToDeviceOption
  3092  	}{
  3093  		{"GetDefaultValue", nil, nil, ""},
  3094  		{"BindToNonExistent", strPtr("non_existent_device"), tcpip.ErrUnknownDevice, ""},
  3095  		{"BindToExistent", strPtr("my_device"), nil, "my_device"},
  3096  		{"UnbindToDevice", strPtr(""), nil, ""},
  3097  	}
  3098  	for _, testAction := range testActions {
  3099  		t.Run(testAction.name, func(t *testing.T) {
  3100  			if testAction.setBindToDevice != nil {
  3101  				bindToDevice := tcpip.BindToDeviceOption(*testAction.setBindToDevice)
  3102  				if got, want := ep.SetSockOpt(bindToDevice), testAction.setBindToDeviceError; got != want {
  3103  					t.Errorf("SetSockOpt(%v) got %v, want %v", bindToDevice, got, want)
  3104  				}
  3105  			}
  3106  			bindToDevice := tcpip.BindToDeviceOption("to be modified by GetSockOpt")
  3107  			if ep.GetSockOpt(&bindToDevice) != nil {
  3108  				t.Errorf("GetSockOpt got %v, want %v", ep.GetSockOpt(&bindToDevice), nil)
  3109  			}
  3110  			if got, want := bindToDevice, testAction.getBindToDevice; got != want {
  3111  				t.Errorf("bindToDevice got %q, want %q", got, want)
  3112  			}
  3113  		})
  3114  	}
  3115  }
  3116  
  3117  func makeStack() (*stack.Stack, *tcpip.Error) {
  3118  	s := stack.New(stack.Options{
  3119  		NetworkProtocols: []stack.NetworkProtocol{
  3120  			ipv4.NewProtocol(),
  3121  			ipv6.NewProtocol(),
  3122  		},
  3123  		TransportProtocols: []stack.TransportProtocol{tcp.NewProtocol()},
  3124  	})
  3125  
  3126  	id := loopback.New()
  3127  	if testing.Verbose() {
  3128  		id = sniffer.New(id)
  3129  	}
  3130  
  3131  	if err := s.CreateNIC(1, id); err != nil {
  3132  		return nil, err
  3133  	}
  3134  
  3135  	for _, ct := range []struct {
  3136  		number  tcpip.NetworkProtocolNumber
  3137  		address tcpip.Address
  3138  	}{
  3139  		{ipv4.ProtocolNumber, context.StackAddr},
  3140  		{ipv6.ProtocolNumber, context.StackV6Addr},
  3141  	} {
  3142  		if err := s.AddAddress(1, ct.number, ct.address); err != nil {
  3143  			return nil, err
  3144  		}
  3145  	}
  3146  
  3147  	s.SetRouteTable([]tcpip.Route{
  3148  		{
  3149  			Destination: header.IPv4EmptySubnet,
  3150  			NIC:         1,
  3151  		},
  3152  		{
  3153  			Destination: header.IPv6EmptySubnet,
  3154  			NIC:         1,
  3155  		},
  3156  	})
  3157  
  3158  	return s, nil
  3159  }
  3160  
  3161  func TestSelfConnect(t *testing.T) {
  3162  	// This test ensures that intentional self-connects work. In particular,
  3163  	// it checks that if an endpoint binds to say 127.0.0.1:1000 then
  3164  	// connects to 127.0.0.1:1000, then it will be connected to itself, and
  3165  	// is able to send and receive data through the same endpoint.
  3166  	s, err := makeStack()
  3167  	if err != nil {
  3168  		t.Fatal(err)
  3169  	}
  3170  
  3171  	var wq waiter.Queue
  3172  	ep, err := s.NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &wq)
  3173  	if err != nil {
  3174  		t.Fatalf("NewEndpoint failed: %v", err)
  3175  	}
  3176  	defer ep.Close()
  3177  
  3178  	if err := ep.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  3179  		t.Fatalf("Bind failed: %v", err)
  3180  	}
  3181  
  3182  	// Register for notification, then start connection attempt.
  3183  	waitEntry, notifyCh := waiter.NewChannelEntry(nil)
  3184  	wq.EventRegister(&waitEntry, waiter.EventOut)
  3185  	defer wq.EventUnregister(&waitEntry)
  3186  
  3187  	if err := ep.Connect(tcpip.FullAddress{Addr: context.StackAddr, Port: context.StackPort}); err != tcpip.ErrConnectStarted {
  3188  		t.Fatalf("got ep.Connect(...) = %v, want = %v", err, tcpip.ErrConnectStarted)
  3189  	}
  3190  
  3191  	<-notifyCh
  3192  	if err := ep.GetSockOpt(tcpip.ErrorOption{}); err != nil {
  3193  		t.Fatalf("Connect failed: %v", err)
  3194  	}
  3195  
  3196  	// Write something.
  3197  	data := []byte{1, 2, 3}
  3198  	view := buffer.NewView(len(data))
  3199  	copy(view, data)
  3200  	if _, _, err := ep.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil {
  3201  		t.Fatalf("Write failed: %v", err)
  3202  	}
  3203  
  3204  	// Read back what was written.
  3205  	wq.EventUnregister(&waitEntry)
  3206  	wq.EventRegister(&waitEntry, waiter.EventIn)
  3207  	rd, _, err := ep.Read(nil)
  3208  	if err != nil {
  3209  		if err != tcpip.ErrWouldBlock {
  3210  			t.Fatalf("Read failed: %v", err)
  3211  		}
  3212  		<-notifyCh
  3213  		rd, _, err = ep.Read(nil)
  3214  		if err != nil {
  3215  			t.Fatalf("Read failed: %v", err)
  3216  		}
  3217  	}
  3218  
  3219  	if !bytes.Equal(data, rd) {
  3220  		t.Fatalf("got data = %v, want = %v", rd, data)
  3221  	}
  3222  }
  3223  
  3224  func TestConnectAvoidsBoundPorts(t *testing.T) {
  3225  	addressTypes := func(t *testing.T, network string) []string {
  3226  		switch network {
  3227  		case "ipv4":
  3228  			return []string{"v4"}
  3229  		case "ipv6":
  3230  			return []string{"v6"}
  3231  		case "dual":
  3232  			return []string{"v6", "mapped"}
  3233  		default:
  3234  			t.Fatalf("unknown network: '%s'", network)
  3235  		}
  3236  
  3237  		panic("unreachable")
  3238  	}
  3239  
  3240  	address := func(t *testing.T, addressType string, isAny bool) tcpip.Address {
  3241  		switch addressType {
  3242  		case "v4":
  3243  			if isAny {
  3244  				return ""
  3245  			}
  3246  			return context.StackAddr
  3247  		case "v6":
  3248  			if isAny {
  3249  				return ""
  3250  			}
  3251  			return context.StackV6Addr
  3252  		case "mapped":
  3253  			if isAny {
  3254  				return context.V4MappedWildcardAddr
  3255  			}
  3256  			return context.StackV4MappedAddr
  3257  		default:
  3258  			t.Fatalf("unknown address type: '%s'", addressType)
  3259  		}
  3260  
  3261  		panic("unreachable")
  3262  	}
  3263  	// This test ensures that Endpoint.Connect doesn't select already-bound ports.
  3264  	networks := []string{"ipv4", "ipv6", "dual"}
  3265  	for _, exhaustedNetwork := range networks {
  3266  		t.Run(fmt.Sprintf("exhaustedNetwork=%s", exhaustedNetwork), func(t *testing.T) {
  3267  			for _, exhaustedAddressType := range addressTypes(t, exhaustedNetwork) {
  3268  				t.Run(fmt.Sprintf("exhaustedAddressType=%s", exhaustedAddressType), func(t *testing.T) {
  3269  					for _, isAny := range []bool{false, true} {
  3270  						t.Run(fmt.Sprintf("isAny=%t", isAny), func(t *testing.T) {
  3271  							for _, candidateNetwork := range networks {
  3272  								t.Run(fmt.Sprintf("candidateNetwork=%s", candidateNetwork), func(t *testing.T) {
  3273  									for _, candidateAddressType := range addressTypes(t, candidateNetwork) {
  3274  										t.Run(fmt.Sprintf("candidateAddressType=%s", candidateAddressType), func(t *testing.T) {
  3275  											s, err := makeStack()
  3276  											if err != nil {
  3277  												t.Fatal(err)
  3278  											}
  3279  
  3280  											var wq waiter.Queue
  3281  											var eps []tcpip.Endpoint
  3282  											defer func() {
  3283  												for _, ep := range eps {
  3284  													ep.Close()
  3285  												}
  3286  											}()
  3287  											makeEP := func(network string) tcpip.Endpoint {
  3288  												var networkProtocolNumber tcpip.NetworkProtocolNumber
  3289  												switch network {
  3290  												case "ipv4":
  3291  													networkProtocolNumber = ipv4.ProtocolNumber
  3292  												case "ipv6", "dual":
  3293  													networkProtocolNumber = ipv6.ProtocolNumber
  3294  												default:
  3295  													t.Fatalf("unknown network: '%s'", network)
  3296  												}
  3297  												ep, err := s.NewEndpoint(tcp.ProtocolNumber, networkProtocolNumber, &wq)
  3298  												if err != nil {
  3299  													t.Fatalf("NewEndpoint failed: %v", err)
  3300  												}
  3301  												eps = append(eps, ep)
  3302  												switch network {
  3303  												case "ipv4":
  3304  												case "ipv6":
  3305  													if err := ep.SetSockOpt(tcpip.V6OnlyOption(1)); err != nil {
  3306  														t.Fatalf("SetSockOpt(V6OnlyOption(1)) failed: %v", err)
  3307  													}
  3308  												case "dual":
  3309  													if err := ep.SetSockOpt(tcpip.V6OnlyOption(0)); err != nil {
  3310  														t.Fatalf("SetSockOpt(V6OnlyOption(0)) failed: %v", err)
  3311  													}
  3312  												default:
  3313  													t.Fatalf("unknown network: '%s'", network)
  3314  												}
  3315  												return ep
  3316  											}
  3317  
  3318  											var v4reserved, v6reserved bool
  3319  											switch exhaustedAddressType {
  3320  											case "v4", "mapped":
  3321  												v4reserved = true
  3322  											case "v6":
  3323  												v6reserved = true
  3324  												// Dual stack sockets bound to v6 any reserve on v4 as
  3325  												// well.
  3326  												if isAny {
  3327  													switch exhaustedNetwork {
  3328  													case "ipv6":
  3329  													case "dual":
  3330  														v4reserved = true
  3331  													default:
  3332  														t.Fatalf("unknown address type: '%s'", exhaustedNetwork)
  3333  													}
  3334  												}
  3335  											default:
  3336  												t.Fatalf("unknown address type: '%s'", exhaustedAddressType)
  3337  											}
  3338  											var collides bool
  3339  											switch candidateAddressType {
  3340  											case "v4", "mapped":
  3341  												collides = v4reserved
  3342  											case "v6":
  3343  												collides = v6reserved
  3344  											default:
  3345  												t.Fatalf("unknown address type: '%s'", candidateAddressType)
  3346  											}
  3347  
  3348  											for i := ports.FirstEphemeral; i <= math.MaxUint16; i++ {
  3349  												if makeEP(exhaustedNetwork).Bind(tcpip.FullAddress{Addr: address(t, exhaustedAddressType, isAny), Port: uint16(i)}); err != nil {
  3350  													t.Fatalf("Bind(%d) failed: %v", i, err)
  3351  												}
  3352  											}
  3353  											want := tcpip.ErrConnectStarted
  3354  											if collides {
  3355  												want = tcpip.ErrNoPortAvailable
  3356  											}
  3357  											if err := makeEP(candidateNetwork).Connect(tcpip.FullAddress{Addr: address(t, candidateAddressType, false), Port: 31337}); err != want {
  3358  												t.Fatalf("got ep.Connect(..) = %v, want = %v", err, want)
  3359  											}
  3360  										})
  3361  									}
  3362  								})
  3363  							}
  3364  						})
  3365  					}
  3366  				})
  3367  			}
  3368  		})
  3369  	}
  3370  }
  3371  
  3372  func TestPathMTUDiscovery(t *testing.T) {
  3373  	// This test verifies the stack retransmits packets after it receives an
  3374  	// ICMP packet indicating that the path MTU has been exceeded.
  3375  	c := context.New(t, 1500)
  3376  	defer c.Cleanup()
  3377  
  3378  	// Create new connection with MSS of 1460.
  3379  	const maxPayload = 1500 - header.TCPMinimumSize - header.IPv4MinimumSize
  3380  	c.CreateConnectedWithRawOptions(789, 30000, -1 /* epRcvBuf */, []byte{
  3381  		header.TCPOptionMSS, 4, byte(maxPayload / 256), byte(maxPayload % 256),
  3382  	})
  3383  
  3384  	// Send 3200 bytes of data.
  3385  	const writeSize = 3200
  3386  	data := buffer.NewView(writeSize)
  3387  	for i := range data {
  3388  		data[i] = byte(i)
  3389  	}
  3390  
  3391  	if _, _, err := c.EP.Write(tcpip.SlicePayload(data), tcpip.WriteOptions{}); err != nil {
  3392  		t.Fatalf("Write failed: %v", err)
  3393  	}
  3394  
  3395  	receivePackets := func(c *context.Context, sizes []int, which int, seqNum uint32) []byte {
  3396  		var ret []byte
  3397  		for i, size := range sizes {
  3398  			p := c.GetPacket()
  3399  			if i == which {
  3400  				ret = p
  3401  			}
  3402  			checker.IPv4(t, p,
  3403  				checker.PayloadLen(size+header.TCPMinimumSize),
  3404  				checker.TCP(
  3405  					checker.DstPort(context.TestPort),
  3406  					checker.SeqNum(seqNum),
  3407  					checker.AckNum(790),
  3408  					checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)),
  3409  				),
  3410  			)
  3411  			seqNum += uint32(size)
  3412  		}
  3413  		return ret
  3414  	}
  3415  
  3416  	// Receive three packets.
  3417  	sizes := []int{maxPayload, maxPayload, writeSize - 2*maxPayload}
  3418  	first := receivePackets(c, sizes, 0, uint32(c.IRS)+1)
  3419  
  3420  	// Send "packet too big" messages back to netstack.
  3421  	const newMTU = 1200
  3422  	const newMaxPayload = newMTU - header.IPv4MinimumSize - header.TCPMinimumSize
  3423  	mtu := []byte{0, 0, newMTU / 256, newMTU % 256}
  3424  	c.SendICMPPacket(header.ICMPv4DstUnreachable, header.ICMPv4FragmentationNeeded, mtu, first, newMTU)
  3425  
  3426  	// See retransmitted packets. None exceeding the new max.
  3427  	sizes = []int{newMaxPayload, maxPayload - newMaxPayload, newMaxPayload, maxPayload - newMaxPayload, writeSize - 2*maxPayload}
  3428  	receivePackets(c, sizes, -1, uint32(c.IRS)+1)
  3429  }
  3430  
  3431  func TestTCPEndpointProbe(t *testing.T) {
  3432  	c := context.New(t, 1500)
  3433  	defer c.Cleanup()
  3434  
  3435  	invoked := make(chan struct{})
  3436  	c.Stack().AddTCPProbe(func(state stack.TCPEndpointState) {
  3437  		// Validate that the endpoint ID is what we expect.
  3438  		//
  3439  		// We don't do an extensive validation of every field but a
  3440  		// basic sanity test.
  3441  		if got, want := state.ID.LocalAddress, tcpip.Address(context.StackAddr); got != want {
  3442  			t.Fatalf("got LocalAddress: %q, want: %q", got, want)
  3443  		}
  3444  		if got, want := state.ID.LocalPort, c.Port; got != want {
  3445  			t.Fatalf("got LocalPort: %d, want: %d", got, want)
  3446  		}
  3447  		if got, want := state.ID.RemoteAddress, tcpip.Address(context.TestAddr); got != want {
  3448  			t.Fatalf("got RemoteAddress: %q, want: %q", got, want)
  3449  		}
  3450  		if got, want := state.ID.RemotePort, uint16(context.TestPort); got != want {
  3451  			t.Fatalf("got RemotePort: %d, want: %d", got, want)
  3452  		}
  3453  
  3454  		invoked <- struct{}{}
  3455  	})
  3456  
  3457  	c.CreateConnected(789, 30000, -1 /* epRcvBuf */)
  3458  
  3459  	data := []byte{1, 2, 3}
  3460  	c.SendPacket(data, &context.Headers{
  3461  		SrcPort: context.TestPort,
  3462  		DstPort: c.Port,
  3463  		Flags:   header.TCPFlagAck,
  3464  		SeqNum:  790,
  3465  		AckNum:  c.IRS.Add(1),
  3466  		RcvWnd:  30000,
  3467  	})
  3468  
  3469  	select {
  3470  	case <-invoked:
  3471  	case <-time.After(100 * time.Millisecond):
  3472  		t.Fatalf("TCP Probe function was not called")
  3473  	}
  3474  }
  3475  
  3476  func TestStackSetCongestionControl(t *testing.T) {
  3477  	testCases := []struct {
  3478  		cc  tcpip.CongestionControlOption
  3479  		err *tcpip.Error
  3480  	}{
  3481  		{"reno", nil},
  3482  		{"cubic", nil},
  3483  		{"blahblah", tcpip.ErrNoSuchFile},
  3484  	}
  3485  
  3486  	for _, tc := range testCases {
  3487  		t.Run(fmt.Sprintf("SetTransportProtocolOption(.., %v)", tc.cc), func(t *testing.T) {
  3488  			c := context.New(t, 1500)
  3489  			defer c.Cleanup()
  3490  
  3491  			s := c.Stack()
  3492  
  3493  			var oldCC tcpip.CongestionControlOption
  3494  			if err := s.TransportProtocolOption(tcp.ProtocolNumber, &oldCC); err != nil {
  3495  				t.Fatalf("s.TransportProtocolOption(%v, %v) = %v", tcp.ProtocolNumber, &oldCC, err)
  3496  			}
  3497  
  3498  			if err := s.SetTransportProtocolOption(tcp.ProtocolNumber, tc.cc); err != tc.err {
  3499  				t.Fatalf("s.SetTransportProtocolOption(%v, %v) = %v, want %v", tcp.ProtocolNumber, tc.cc, err, tc.err)
  3500  			}
  3501  
  3502  			var cc tcpip.CongestionControlOption
  3503  			if err := s.TransportProtocolOption(tcp.ProtocolNumber, &cc); err != nil {
  3504  				t.Fatalf("s.TransportProtocolOption(%v, %v) = %v", tcp.ProtocolNumber, &cc, err)
  3505  			}
  3506  
  3507  			got, want := cc, oldCC
  3508  			// If SetTransportProtocolOption is expected to succeed
  3509  			// then the returned value for congestion control should
  3510  			// match the one specified in the
  3511  			// SetTransportProtocolOption call above, else it should
  3512  			// be what it was before the call to
  3513  			// SetTransportProtocolOption.
  3514  			if tc.err == nil {
  3515  				want = tc.cc
  3516  			}
  3517  			if got != want {
  3518  				t.Fatalf("got congestion control: %v, want: %v", got, want)
  3519  			}
  3520  		})
  3521  	}
  3522  }
  3523  
  3524  func TestStackAvailableCongestionControl(t *testing.T) {
  3525  	c := context.New(t, 1500)
  3526  	defer c.Cleanup()
  3527  
  3528  	s := c.Stack()
  3529  
  3530  	// Query permitted congestion control algorithms.
  3531  	var aCC tcpip.AvailableCongestionControlOption
  3532  	if err := s.TransportProtocolOption(tcp.ProtocolNumber, &aCC); err != nil {
  3533  		t.Fatalf("s.TransportProtocolOption(%v, %v) = %v", tcp.ProtocolNumber, &aCC, err)
  3534  	}
  3535  	if got, want := aCC, tcpip.AvailableCongestionControlOption("reno cubic"); got != want {
  3536  		t.Fatalf("got tcpip.AvailableCongestionControlOption: %v, want: %v", got, want)
  3537  	}
  3538  }
  3539  
  3540  func TestStackSetAvailableCongestionControl(t *testing.T) {
  3541  	c := context.New(t, 1500)
  3542  	defer c.Cleanup()
  3543  
  3544  	s := c.Stack()
  3545  
  3546  	// Setting AvailableCongestionControlOption should fail.
  3547  	aCC := tcpip.AvailableCongestionControlOption("xyz")
  3548  	if err := s.SetTransportProtocolOption(tcp.ProtocolNumber, &aCC); err == nil {
  3549  		t.Fatalf("s.TransportProtocolOption(%v, %v) = nil, want non-nil", tcp.ProtocolNumber, &aCC)
  3550  	}
  3551  
  3552  	// Verify that we still get the expected list of congestion control options.
  3553  	var cc tcpip.AvailableCongestionControlOption
  3554  	if err := s.TransportProtocolOption(tcp.ProtocolNumber, &cc); err != nil {
  3555  		t.Fatalf("s.TransportProtocolOption(%v, %v) = %v", tcp.ProtocolNumber, &cc, err)
  3556  	}
  3557  	if got, want := cc, tcpip.AvailableCongestionControlOption("reno cubic"); got != want {
  3558  		t.Fatalf("got tcpip.AvailableCongestionControlOption: %v, want: %v", got, want)
  3559  	}
  3560  }
  3561  
  3562  func TestEndpointSetCongestionControl(t *testing.T) {
  3563  	testCases := []struct {
  3564  		cc  tcpip.CongestionControlOption
  3565  		err *tcpip.Error
  3566  	}{
  3567  		{"reno", nil},
  3568  		{"cubic", nil},
  3569  		{"blahblah", tcpip.ErrNoSuchFile},
  3570  	}
  3571  
  3572  	for _, connected := range []bool{false, true} {
  3573  		for _, tc := range testCases {
  3574  			t.Run(fmt.Sprintf("SetSockOpt(.., %v) w/ connected = %v", tc.cc, connected), func(t *testing.T) {
  3575  				c := context.New(t, 1500)
  3576  				defer c.Cleanup()
  3577  
  3578  				// Create TCP endpoint.
  3579  				var err *tcpip.Error
  3580  				c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &c.WQ)
  3581  				if err != nil {
  3582  					t.Fatalf("NewEndpoint failed: %v", err)
  3583  				}
  3584  
  3585  				var oldCC tcpip.CongestionControlOption
  3586  				if err := c.EP.GetSockOpt(&oldCC); err != nil {
  3587  					t.Fatalf("c.EP.SockOpt(%v) = %v", &oldCC, err)
  3588  				}
  3589  
  3590  				if connected {
  3591  					c.Connect(789 /* iss */, 32768 /* rcvWnd */, nil)
  3592  				}
  3593  
  3594  				if err := c.EP.SetSockOpt(tc.cc); err != tc.err {
  3595  					t.Fatalf("c.EP.SetSockOpt(%v) = %v, want %v", tc.cc, err, tc.err)
  3596  				}
  3597  
  3598  				var cc tcpip.CongestionControlOption
  3599  				if err := c.EP.GetSockOpt(&cc); err != nil {
  3600  					t.Fatalf("c.EP.SockOpt(%v) = %v", &cc, err)
  3601  				}
  3602  
  3603  				got, want := cc, oldCC
  3604  				// If SetSockOpt is expected to succeed then the
  3605  				// returned value for congestion control should match
  3606  				// the one specified in the SetSockOpt above, else it
  3607  				// should be what it was before the call to SetSockOpt.
  3608  				if tc.err == nil {
  3609  					want = tc.cc
  3610  				}
  3611  				if got != want {
  3612  					t.Fatalf("got congestion control: %v, want: %v", got, want)
  3613  				}
  3614  			})
  3615  		}
  3616  	}
  3617  }
  3618  
  3619  func enableCUBIC(t *testing.T, c *context.Context) {
  3620  	t.Helper()
  3621  	opt := tcpip.CongestionControlOption("cubic")
  3622  	if err := c.Stack().SetTransportProtocolOption(tcp.ProtocolNumber, opt); err != nil {
  3623  		t.Fatalf("c.s.SetTransportProtocolOption(tcp.ProtocolNumber, %v = %v", opt, err)
  3624  	}
  3625  }
  3626  
  3627  func TestKeepalive(t *testing.T) {
  3628  	c := context.New(t, defaultMTU)
  3629  	defer c.Cleanup()
  3630  
  3631  	c.CreateConnected(789, 30000, -1 /* epRcvBuf */)
  3632  
  3633  	c.EP.SetSockOpt(tcpip.KeepaliveIdleOption(10 * time.Millisecond))
  3634  	c.EP.SetSockOpt(tcpip.KeepaliveIntervalOption(10 * time.Millisecond))
  3635  	c.EP.SetSockOpt(tcpip.KeepaliveCountOption(5))
  3636  	c.EP.SetSockOpt(tcpip.KeepaliveEnabledOption(1))
  3637  
  3638  	// 5 unacked keepalives are sent. ACK each one, and check that the
  3639  	// connection stays alive after 5.
  3640  	for i := 0; i < 10; i++ {
  3641  		b := c.GetPacket()
  3642  		checker.IPv4(t, b,
  3643  			checker.TCP(
  3644  				checker.DstPort(context.TestPort),
  3645  				checker.SeqNum(uint32(c.IRS)),
  3646  				checker.AckNum(uint32(790)),
  3647  				checker.TCPFlags(header.TCPFlagAck),
  3648  			),
  3649  		)
  3650  
  3651  		// Acknowledge the keepalive.
  3652  		c.SendPacket(nil, &context.Headers{
  3653  			SrcPort: context.TestPort,
  3654  			DstPort: c.Port,
  3655  			Flags:   header.TCPFlagAck,
  3656  			SeqNum:  790,
  3657  			AckNum:  c.IRS,
  3658  			RcvWnd:  30000,
  3659  		})
  3660  	}
  3661  
  3662  	// Check that the connection is still alive.
  3663  	if _, _, err := c.EP.Read(nil); err != tcpip.ErrWouldBlock {
  3664  		t.Fatalf("got c.EP.Read(nil) = %v, want = %v", err, tcpip.ErrWouldBlock)
  3665  	}
  3666  
  3667  	// Send some data and wait before ACKing it. Keepalives should be disabled
  3668  	// during this period.
  3669  	view := buffer.NewView(3)
  3670  	if _, _, err := c.EP.Write(tcpip.SlicePayload(view), tcpip.WriteOptions{}); err != nil {
  3671  		t.Fatalf("Write failed: %v", err)
  3672  	}
  3673  
  3674  	next := uint32(c.IRS) + 1
  3675  	checker.IPv4(t, c.GetPacket(),
  3676  		checker.PayloadLen(len(view)+header.TCPMinimumSize),
  3677  		checker.TCP(
  3678  			checker.DstPort(context.TestPort),
  3679  			checker.SeqNum(next),
  3680  			checker.AckNum(790),
  3681  			checker.TCPFlagsMatch(header.TCPFlagAck, ^uint8(header.TCPFlagPsh)),
  3682  		),
  3683  	)
  3684  
  3685  	// Wait for the packet to be retransmitted. Verify that no keepalives
  3686  	// were sent.
  3687  	checker.IPv4(t, c.GetPacket(),
  3688  		checker.PayloadLen(len(view)+header.TCPMinimumSize),
  3689  		checker.TCP(
  3690  			checker.DstPort(context.TestPort),
  3691  			checker.SeqNum(next),
  3692  			checker.AckNum(790),
  3693  			checker.TCPFlags(header.TCPFlagAck|header.TCPFlagPsh),
  3694  		),
  3695  	)
  3696  	c.CheckNoPacket("Keepalive packet received while unACKed data is pending")
  3697  
  3698  	next += uint32(len(view))
  3699  
  3700  	// Send ACK. Keepalives should start sending again.
  3701  	c.SendPacket(nil, &context.Headers{
  3702  		SrcPort: context.TestPort,
  3703  		DstPort: c.Port,
  3704  		Flags:   header.TCPFlagAck,
  3705  		SeqNum:  790,
  3706  		AckNum:  seqnum.Value(next),
  3707  		RcvWnd:  30000,
  3708  	})
  3709  
  3710  	// Now receive 5 keepalives, but don't ACK them. The connection
  3711  	// should be reset after 5.
  3712  	for i := 0; i < 5; i++ {
  3713  		b := c.GetPacket()
  3714  		checker.IPv4(t, b,
  3715  			checker.TCP(
  3716  				checker.DstPort(context.TestPort),
  3717  				checker.SeqNum(uint32(next-1)),
  3718  				checker.AckNum(uint32(790)),
  3719  				checker.TCPFlags(header.TCPFlagAck),
  3720  			),
  3721  		)
  3722  	}
  3723  
  3724  	// The connection should be terminated after 5 unacked keepalives.
  3725  	checker.IPv4(t, c.GetPacket(),
  3726  		checker.TCP(
  3727  			checker.DstPort(context.TestPort),
  3728  			checker.SeqNum(uint32(next)),
  3729  			checker.AckNum(uint32(790)),
  3730  			checker.TCPFlags(header.TCPFlagAck|header.TCPFlagRst),
  3731  		),
  3732  	)
  3733  
  3734  	if _, _, err := c.EP.Read(nil); err != tcpip.ErrTimeout {
  3735  		t.Fatalf("got c.EP.Read(nil) = %v, want = %v", err, tcpip.ErrTimeout)
  3736  	}
  3737  }
  3738  
  3739  func executeHandshake(t *testing.T, c *context.Context, srcPort uint16, synCookieInUse bool) (irs, iss seqnum.Value) {
  3740  	// Send a SYN request.
  3741  	irs = seqnum.Value(789)
  3742  	c.SendPacket(nil, &context.Headers{
  3743  		SrcPort: srcPort,
  3744  		DstPort: context.StackPort,
  3745  		Flags:   header.TCPFlagSyn,
  3746  		SeqNum:  irs,
  3747  		RcvWnd:  30000,
  3748  	})
  3749  
  3750  	// Receive the SYN-ACK reply.w
  3751  	b := c.GetPacket()
  3752  	tcp := header.TCP(header.IPv4(b).Payload())
  3753  	iss = seqnum.Value(tcp.SequenceNumber())
  3754  	tcpCheckers := []checker.TransportChecker{
  3755  		checker.SrcPort(context.StackPort),
  3756  		checker.DstPort(srcPort),
  3757  		checker.TCPFlags(header.TCPFlagAck | header.TCPFlagSyn),
  3758  		checker.AckNum(uint32(irs) + 1),
  3759  	}
  3760  
  3761  	if synCookieInUse {
  3762  		// When cookies are in use window scaling is disabled.
  3763  		tcpCheckers = append(tcpCheckers, checker.TCPSynOptions(header.TCPSynOptions{
  3764  			WS:  -1,
  3765  			MSS: c.MSSWithoutOptions(),
  3766  		}))
  3767  	}
  3768  
  3769  	checker.IPv4(t, b, checker.TCP(tcpCheckers...))
  3770  
  3771  	// Send ACK.
  3772  	c.SendPacket(nil, &context.Headers{
  3773  		SrcPort: srcPort,
  3774  		DstPort: context.StackPort,
  3775  		Flags:   header.TCPFlagAck,
  3776  		SeqNum:  irs + 1,
  3777  		AckNum:  iss + 1,
  3778  		RcvWnd:  30000,
  3779  	})
  3780  	return irs, iss
  3781  }
  3782  
  3783  // TestListenBacklogFull tests that netstack does not complete handshakes if the
  3784  // listen backlog for the endpoint is full.
  3785  func TestListenBacklogFull(t *testing.T) {
  3786  	c := context.New(t, defaultMTU)
  3787  	defer c.Cleanup()
  3788  
  3789  	// Create TCP endpoint.
  3790  	var err *tcpip.Error
  3791  	c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &c.WQ)
  3792  	if err != nil {
  3793  		t.Fatalf("NewEndpoint failed: %v", err)
  3794  	}
  3795  
  3796  	// Bind to wildcard.
  3797  	if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  3798  		t.Fatalf("Bind failed: %v", err)
  3799  	}
  3800  
  3801  	// Test acceptance.
  3802  	// Start listening.
  3803  	listenBacklog := 2
  3804  	if err := c.EP.Listen(listenBacklog); err != nil {
  3805  		t.Fatalf("Listen failed: %v", err)
  3806  	}
  3807  
  3808  	for i := 0; i < listenBacklog; i++ {
  3809  		executeHandshake(t, c, context.TestPort+uint16(i), false /*synCookieInUse */)
  3810  	}
  3811  
  3812  	time.Sleep(50 * time.Millisecond)
  3813  
  3814  	// Now execute send one more SYN. The stack should not respond as the backlog
  3815  	// is full at this point.
  3816  	c.SendPacket(nil, &context.Headers{
  3817  		SrcPort: context.TestPort + 2,
  3818  		DstPort: context.StackPort,
  3819  		Flags:   header.TCPFlagSyn,
  3820  		SeqNum:  seqnum.Value(789),
  3821  		RcvWnd:  30000,
  3822  	})
  3823  	c.CheckNoPacketTimeout("unexpected packet received", 50*time.Millisecond)
  3824  
  3825  	// Try to accept the connections in the backlog.
  3826  	we, ch := waiter.NewChannelEntry(nil)
  3827  	c.WQ.EventRegister(&we, waiter.EventIn)
  3828  	defer c.WQ.EventUnregister(&we)
  3829  
  3830  	for i := 0; i < listenBacklog; i++ {
  3831  		_, _, err = c.EP.Accept()
  3832  		if err == tcpip.ErrWouldBlock {
  3833  			// Wait for connection to be established.
  3834  			select {
  3835  			case <-ch:
  3836  				_, _, err = c.EP.Accept()
  3837  				if err != nil {
  3838  					t.Fatalf("Accept failed: %v", err)
  3839  				}
  3840  
  3841  			case <-time.After(1 * time.Second):
  3842  				t.Fatalf("Timed out waiting for accept")
  3843  			}
  3844  		}
  3845  	}
  3846  
  3847  	// Now verify that there are no more connections that can be accepted.
  3848  	_, _, err = c.EP.Accept()
  3849  	if err != tcpip.ErrWouldBlock {
  3850  		select {
  3851  		case <-ch:
  3852  			t.Fatalf("unexpected endpoint delivered on Accept: %+v", c.EP)
  3853  		case <-time.After(1 * time.Second):
  3854  		}
  3855  	}
  3856  
  3857  	// Now a new handshake must succeed.
  3858  	executeHandshake(t, c, context.TestPort+2, false /*synCookieInUse */)
  3859  
  3860  	newEP, _, err := c.EP.Accept()
  3861  	if err == tcpip.ErrWouldBlock {
  3862  		// Wait for connection to be established.
  3863  		select {
  3864  		case <-ch:
  3865  			newEP, _, err = c.EP.Accept()
  3866  			if err != nil {
  3867  				t.Fatalf("Accept failed: %v", err)
  3868  			}
  3869  
  3870  		case <-time.After(1 * time.Second):
  3871  			t.Fatalf("Timed out waiting for accept")
  3872  		}
  3873  	}
  3874  
  3875  	// Now verify that the TCP socket is usable and in a connected state.
  3876  	data := "Don't panic"
  3877  	newEP.Write(tcpip.SlicePayload(buffer.NewViewFromBytes([]byte(data))), tcpip.WriteOptions{})
  3878  	b := c.GetPacket()
  3879  	tcp := header.TCP(header.IPv4(b).Payload())
  3880  	if string(tcp.Payload()) != data {
  3881  		t.Fatalf("Unexpected data: got %v, want %v", string(tcp.Payload()), data)
  3882  	}
  3883  }
  3884  
  3885  func TestListenSynRcvdQueueFull(t *testing.T) {
  3886  	c := context.New(t, defaultMTU)
  3887  	defer c.Cleanup()
  3888  
  3889  	// Create TCP endpoint.
  3890  	var err *tcpip.Error
  3891  	c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &c.WQ)
  3892  	if err != nil {
  3893  		t.Fatalf("NewEndpoint failed: %v", err)
  3894  	}
  3895  
  3896  	// Bind to wildcard.
  3897  	if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  3898  		t.Fatalf("Bind failed: %v", err)
  3899  	}
  3900  
  3901  	// Test acceptance.
  3902  	// Start listening.
  3903  	listenBacklog := 1
  3904  	if err := c.EP.Listen(listenBacklog); err != nil {
  3905  		t.Fatalf("Listen failed: %v", err)
  3906  	}
  3907  
  3908  	// Send two SYN's the first one should get a SYN-ACK, the
  3909  	// second one should not get any response and is dropped as
  3910  	// the synRcvd count will be equal to backlog.
  3911  	irs := seqnum.Value(789)
  3912  	c.SendPacket(nil, &context.Headers{
  3913  		SrcPort: context.TestPort,
  3914  		DstPort: context.StackPort,
  3915  		Flags:   header.TCPFlagSyn,
  3916  		SeqNum:  seqnum.Value(789),
  3917  		RcvWnd:  30000,
  3918  	})
  3919  
  3920  	// Receive the SYN-ACK reply.
  3921  	b := c.GetPacket()
  3922  	tcp := header.TCP(header.IPv4(b).Payload())
  3923  	iss := seqnum.Value(tcp.SequenceNumber())
  3924  	tcpCheckers := []checker.TransportChecker{
  3925  		checker.SrcPort(context.StackPort),
  3926  		checker.DstPort(context.TestPort),
  3927  		checker.TCPFlags(header.TCPFlagAck | header.TCPFlagSyn),
  3928  		checker.AckNum(uint32(irs) + 1),
  3929  	}
  3930  	checker.IPv4(t, b, checker.TCP(tcpCheckers...))
  3931  
  3932  	// Now execute send one more SYN. The stack should not respond as the backlog
  3933  	// is full at this point.
  3934  	//
  3935  	// NOTE: we did not complete the handshake for the previous one so the
  3936  	// accept backlog should be empty and there should be one connection in
  3937  	// synRcvd state.
  3938  	c.SendPacket(nil, &context.Headers{
  3939  		SrcPort: context.TestPort + 1,
  3940  		DstPort: context.StackPort,
  3941  		Flags:   header.TCPFlagSyn,
  3942  		SeqNum:  seqnum.Value(889),
  3943  		RcvWnd:  30000,
  3944  	})
  3945  	c.CheckNoPacketTimeout("unexpected packet received", 50*time.Millisecond)
  3946  
  3947  	// Now complete the previous connection and verify that there is a connection
  3948  	// to accept.
  3949  	// Send ACK.
  3950  	c.SendPacket(nil, &context.Headers{
  3951  		SrcPort: context.TestPort,
  3952  		DstPort: context.StackPort,
  3953  		Flags:   header.TCPFlagAck,
  3954  		SeqNum:  irs + 1,
  3955  		AckNum:  iss + 1,
  3956  		RcvWnd:  30000,
  3957  	})
  3958  
  3959  	// Try to accept the connections in the backlog.
  3960  	we, ch := waiter.NewChannelEntry(nil)
  3961  	c.WQ.EventRegister(&we, waiter.EventIn)
  3962  	defer c.WQ.EventUnregister(&we)
  3963  
  3964  	newEP, _, err := c.EP.Accept()
  3965  	if err == tcpip.ErrWouldBlock {
  3966  		// Wait for connection to be established.
  3967  		select {
  3968  		case <-ch:
  3969  			newEP, _, err = c.EP.Accept()
  3970  			if err != nil {
  3971  				t.Fatalf("Accept failed: %v", err)
  3972  			}
  3973  
  3974  		case <-time.After(1 * time.Second):
  3975  			t.Fatalf("Timed out waiting for accept")
  3976  		}
  3977  	}
  3978  
  3979  	// Now verify that the TCP socket is usable and in a connected state.
  3980  	data := "Don't panic"
  3981  	newEP.Write(tcpip.SlicePayload(buffer.NewViewFromBytes([]byte(data))), tcpip.WriteOptions{})
  3982  	pkt := c.GetPacket()
  3983  	tcp = header.TCP(header.IPv4(pkt).Payload())
  3984  	if string(tcp.Payload()) != data {
  3985  		t.Fatalf("Unexpected data: got %v, want %v", string(tcp.Payload()), data)
  3986  	}
  3987  }
  3988  
  3989  func TestListenBacklogFullSynCookieInUse(t *testing.T) {
  3990  	saved := tcp.SynRcvdCountThreshold
  3991  	defer func() {
  3992  		tcp.SynRcvdCountThreshold = saved
  3993  	}()
  3994  	tcp.SynRcvdCountThreshold = 1
  3995  
  3996  	c := context.New(t, defaultMTU)
  3997  	defer c.Cleanup()
  3998  
  3999  	// Create TCP endpoint.
  4000  	var err *tcpip.Error
  4001  	c.EP, err = c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &c.WQ)
  4002  	if err != nil {
  4003  		t.Fatalf("NewEndpoint failed: %v", err)
  4004  	}
  4005  
  4006  	// Bind to wildcard.
  4007  	if err := c.EP.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  4008  		t.Fatalf("Bind failed: %v", err)
  4009  	}
  4010  
  4011  	// Test acceptance.
  4012  	// Start listening.
  4013  	listenBacklog := 1
  4014  	portOffset := uint16(0)
  4015  	if err := c.EP.Listen(listenBacklog); err != nil {
  4016  		t.Fatalf("Listen failed: %v", err)
  4017  	}
  4018  
  4019  	executeHandshake(t, c, context.TestPort+portOffset, false)
  4020  	portOffset++
  4021  	// Wait for this to be delivered to the accept queue.
  4022  	time.Sleep(50 * time.Millisecond)
  4023  
  4024  	// Send a SYN request.
  4025  	irs := seqnum.Value(789)
  4026  	c.SendPacket(nil, &context.Headers{
  4027  		SrcPort: context.TestPort,
  4028  		DstPort: context.StackPort,
  4029  		Flags:   header.TCPFlagSyn,
  4030  		SeqNum:  irs,
  4031  		RcvWnd:  30000,
  4032  	})
  4033  	// The Syn should be dropped as the endpoint's backlog is full.
  4034  	c.CheckNoPacketTimeout("unexpected packet received", 50*time.Millisecond)
  4035  
  4036  	// Verify that there is only one acceptable connection at this point.
  4037  	we, ch := waiter.NewChannelEntry(nil)
  4038  	c.WQ.EventRegister(&we, waiter.EventIn)
  4039  	defer c.WQ.EventUnregister(&we)
  4040  
  4041  	_, _, err = c.EP.Accept()
  4042  	if err == tcpip.ErrWouldBlock {
  4043  		// Wait for connection to be established.
  4044  		select {
  4045  		case <-ch:
  4046  			_, _, err = c.EP.Accept()
  4047  			if err != nil {
  4048  				t.Fatalf("Accept failed: %v", err)
  4049  			}
  4050  
  4051  		case <-time.After(1 * time.Second):
  4052  			t.Fatalf("Timed out waiting for accept")
  4053  		}
  4054  	}
  4055  
  4056  	// Now verify that there are no more connections that can be accepted.
  4057  	_, _, err = c.EP.Accept()
  4058  	if err != tcpip.ErrWouldBlock {
  4059  		select {
  4060  		case <-ch:
  4061  			t.Fatalf("unexpected endpoint delivered on Accept: %+v", c.EP)
  4062  		case <-time.After(1 * time.Second):
  4063  		}
  4064  	}
  4065  }
  4066  
  4067  func TestPassiveConnectionAttemptIncrement(t *testing.T) {
  4068  	c := context.New(t, defaultMTU)
  4069  	defer c.Cleanup()
  4070  
  4071  	ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &c.WQ)
  4072  	if err != nil {
  4073  		t.Fatalf("NewEndpoint failed: %v", err)
  4074  	}
  4075  	c.EP = ep
  4076  	if err := ep.Bind(tcpip.FullAddress{Addr: context.StackAddr, Port: context.StackPort}); err != nil {
  4077  		t.Fatalf("Bind failed: %v", err)
  4078  	}
  4079  	if got, want := tcp.EndpointState(ep.State()), tcp.StateBound; got != want {
  4080  		t.Errorf("Unexpected endpoint state: want %v, got %v", want, got)
  4081  	}
  4082  	if err := c.EP.Listen(1); err != nil {
  4083  		t.Fatalf("Listen failed: %v", err)
  4084  	}
  4085  	if got, want := tcp.EndpointState(c.EP.State()), tcp.StateListen; got != want {
  4086  		t.Errorf("Unexpected endpoint state: want %v, got %v", want, got)
  4087  	}
  4088  
  4089  	stats := c.Stack().Stats()
  4090  	want := stats.TCP.PassiveConnectionOpenings.Value() + 1
  4091  
  4092  	srcPort := uint16(context.TestPort)
  4093  	executeHandshake(t, c, srcPort+1, false)
  4094  
  4095  	we, ch := waiter.NewChannelEntry(nil)
  4096  	c.WQ.EventRegister(&we, waiter.EventIn)
  4097  	defer c.WQ.EventUnregister(&we)
  4098  
  4099  	// Verify that there is only one acceptable connection at this point.
  4100  	_, _, err = c.EP.Accept()
  4101  	if err == tcpip.ErrWouldBlock {
  4102  		// Wait for connection to be established.
  4103  		select {
  4104  		case <-ch:
  4105  			_, _, err = c.EP.Accept()
  4106  			if err != nil {
  4107  				t.Fatalf("Accept failed: %v", err)
  4108  			}
  4109  
  4110  		case <-time.After(1 * time.Second):
  4111  			t.Fatalf("Timed out waiting for accept")
  4112  		}
  4113  	}
  4114  
  4115  	if got := stats.TCP.PassiveConnectionOpenings.Value(); got != want {
  4116  		t.Errorf("got stats.TCP.PassiveConnectionOpenings.Value() = %v, want = %v", got, want)
  4117  	}
  4118  }
  4119  
  4120  func TestPassiveFailedConnectionAttemptIncrement(t *testing.T) {
  4121  	c := context.New(t, defaultMTU)
  4122  	defer c.Cleanup()
  4123  
  4124  	stats := c.Stack().Stats()
  4125  	ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &c.WQ)
  4126  	if err != nil {
  4127  		t.Fatalf("NewEndpoint failed: %v", err)
  4128  	}
  4129  	c.EP = ep
  4130  	if err := c.EP.Bind(tcpip.FullAddress{Addr: context.StackAddr, Port: context.StackPort}); err != nil {
  4131  		t.Fatalf("Bind failed: %v", err)
  4132  	}
  4133  	if err := c.EP.Listen(1); err != nil {
  4134  		t.Fatalf("Listen failed: %v", err)
  4135  	}
  4136  
  4137  	srcPort := uint16(context.TestPort)
  4138  	// Now attempt a handshakes it will fill up the accept backlog.
  4139  	executeHandshake(t, c, srcPort, false)
  4140  
  4141  	// Give time for the final ACK to be processed as otherwise the next handshake could
  4142  	// get accepted before the previous one based on goroutine scheduling.
  4143  	time.Sleep(50 * time.Millisecond)
  4144  
  4145  	want := stats.TCP.ListenOverflowSynDrop.Value() + 1
  4146  
  4147  	// Now we will send one more SYN and this one should get dropped
  4148  	// Send a SYN request.
  4149  	c.SendPacket(nil, &context.Headers{
  4150  		SrcPort: srcPort + 2,
  4151  		DstPort: context.StackPort,
  4152  		Flags:   header.TCPFlagSyn,
  4153  		SeqNum:  seqnum.Value(789),
  4154  		RcvWnd:  30000,
  4155  	})
  4156  
  4157  	time.Sleep(50 * time.Millisecond)
  4158  	if got := stats.TCP.ListenOverflowSynDrop.Value(); got != want {
  4159  		t.Errorf("got stats.TCP.ListenOverflowSynDrop.Value() = %v, want = %v", got, want)
  4160  	}
  4161  
  4162  	we, ch := waiter.NewChannelEntry(nil)
  4163  	c.WQ.EventRegister(&we, waiter.EventIn)
  4164  	defer c.WQ.EventUnregister(&we)
  4165  
  4166  	// Now check that there is one acceptable connections.
  4167  	_, _, err = c.EP.Accept()
  4168  	if err == tcpip.ErrWouldBlock {
  4169  		// Wait for connection to be established.
  4170  		select {
  4171  		case <-ch:
  4172  			_, _, err = c.EP.Accept()
  4173  			if err != nil {
  4174  				t.Fatalf("Accept failed: %v", err)
  4175  			}
  4176  
  4177  		case <-time.After(1 * time.Second):
  4178  			t.Fatalf("Timed out waiting for accept")
  4179  		}
  4180  	}
  4181  }
  4182  
  4183  func TestEndpointBindListenAcceptState(t *testing.T) {
  4184  	c := context.New(t, defaultMTU)
  4185  	defer c.Cleanup()
  4186  	wq := &waiter.Queue{}
  4187  	ep, err := c.Stack().NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, wq)
  4188  	if err != nil {
  4189  		t.Fatalf("NewEndpoint failed: %v", err)
  4190  	}
  4191  
  4192  	if err := ep.Bind(tcpip.FullAddress{Port: context.StackPort}); err != nil {
  4193  		t.Fatalf("Bind failed: %v", err)
  4194  	}
  4195  	if got, want := tcp.EndpointState(ep.State()), tcp.StateBound; got != want {
  4196  		t.Errorf("Unexpected endpoint state: want %v, got %v", want, got)
  4197  	}
  4198  
  4199  	if err := ep.Listen(10); err != nil {
  4200  		t.Fatalf("Listen failed: %v", err)
  4201  	}
  4202  	if got, want := tcp.EndpointState(ep.State()), tcp.StateListen; got != want {
  4203  		t.Errorf("Unexpected endpoint state: want %v, got %v", want, got)
  4204  	}
  4205  
  4206  	c.PassiveConnectWithOptions(100, 5, header.TCPSynOptions{MSS: defaultIPv4MSS})
  4207  
  4208  	// Try to accept the connection.
  4209  	we, ch := waiter.NewChannelEntry(nil)
  4210  	wq.EventRegister(&we, waiter.EventIn)
  4211  	defer wq.EventUnregister(&we)
  4212  
  4213  	aep, _, err := ep.Accept()
  4214  	if err == tcpip.ErrWouldBlock {
  4215  		// Wait for connection to be established.
  4216  		select {
  4217  		case <-ch:
  4218  			aep, _, err = ep.Accept()
  4219  			if err != nil {
  4220  				t.Fatalf("Accept failed: %v", err)
  4221  			}
  4222  
  4223  		case <-time.After(1 * time.Second):
  4224  			t.Fatalf("Timed out waiting for accept")
  4225  		}
  4226  	}
  4227  	if got, want := tcp.EndpointState(aep.State()), tcp.StateEstablished; got != want {
  4228  		t.Errorf("Unexpected endpoint state: want %v, got %v", want, got)
  4229  	}
  4230  	// Listening endpoint remains in listen state.
  4231  	if got, want := tcp.EndpointState(ep.State()), tcp.StateListen; got != want {
  4232  		t.Errorf("Unexpected endpoint state: want %v, got %v", want, got)
  4233  	}
  4234  
  4235  	ep.Close()
  4236  	// Give worker goroutines time to receive the close notification.
  4237  	time.Sleep(1 * time.Second)
  4238  	if got, want := tcp.EndpointState(ep.State()), tcp.StateClose; got != want {
  4239  		t.Errorf("Unexpected endpoint state: want %v, got %v", want, got)
  4240  	}
  4241  	// Accepted endpoint remains open when the listen endpoint is closed.
  4242  	if got, want := tcp.EndpointState(aep.State()), tcp.StateEstablished; got != want {
  4243  		t.Errorf("Unexpected endpoint state: want %v, got %v", want, got)
  4244  	}
  4245  
  4246  }
  4247  
  4248  // This test verifies that the auto tuning does not grow the receive buffer if
  4249  // the application is not reading the data actively.
  4250  func TestReceiveBufferAutoTuningApplicationLimited(t *testing.T) {
  4251  	const mtu = 1500
  4252  	const mss = mtu - header.IPv4MinimumSize - header.TCPMinimumSize
  4253  
  4254  	c := context.New(t, mtu)
  4255  	defer c.Cleanup()
  4256  
  4257  	stk := c.Stack()
  4258  	// Set lower limits for auto-tuning tests. This is required because the
  4259  	// test stops the worker which can cause packets to be dropped because
  4260  	// the segment queue holding unprocessed packets is limited to 500.
  4261  	const receiveBufferSize = 80 << 10 // 80KB.
  4262  	const maxReceiveBufferSize = receiveBufferSize * 10
  4263  	if err := stk.SetTransportProtocolOption(tcp.ProtocolNumber, tcp.ReceiveBufferSizeOption{1, receiveBufferSize, maxReceiveBufferSize}); err != nil {
  4264  		t.Fatalf("SetTransportProtocolOption failed: %v", err)
  4265  	}
  4266  
  4267  	// Enable auto-tuning.
  4268  	if err := stk.SetTransportProtocolOption(tcp.ProtocolNumber, tcpip.ModerateReceiveBufferOption(true)); err != nil {
  4269  		t.Fatalf("SetTransportProtocolOption failed: %v", err)
  4270  	}
  4271  	// Change the expected window scale to match the value needed for the
  4272  	// maximum buffer size defined above.
  4273  	c.WindowScale = uint8(tcp.FindWndScale(maxReceiveBufferSize))
  4274  
  4275  	rawEP := c.CreateConnectedWithOptions(header.TCPSynOptions{TS: true, WS: 4})
  4276  
  4277  	// NOTE: The timestamp values in the sent packets are meaningless to the
  4278  	// peer so we just increment the timestamp value by 1 every batch as we
  4279  	// are not really using them for anything. Send a single byte to verify
  4280  	// the advertised window.
  4281  	tsVal := rawEP.TSVal + 1
  4282  
  4283  	// Introduce a 25ms latency by delaying the first byte.
  4284  	latency := 25 * time.Millisecond
  4285  	time.Sleep(latency)
  4286  	rawEP.SendPacketWithTS([]byte{1}, tsVal)
  4287  
  4288  	// Verify that the ACK has the expected window.
  4289  	wantRcvWnd := receiveBufferSize
  4290  	wantRcvWnd = (wantRcvWnd >> uint32(c.WindowScale))
  4291  	rawEP.VerifyACKRcvWnd(uint16(wantRcvWnd - 1))
  4292  	time.Sleep(25 * time.Millisecond)
  4293  
  4294  	// Allocate a large enough payload for the test.
  4295  	b := make([]byte, int(receiveBufferSize)*2)
  4296  	offset := 0
  4297  	payloadSize := receiveBufferSize - 1
  4298  	worker := (c.EP).(interface {
  4299  		StopWork()
  4300  		ResumeWork()
  4301  	})
  4302  	tsVal++
  4303  
  4304  	// Stop the worker goroutine.
  4305  	worker.StopWork()
  4306  	start := offset
  4307  	end := offset + payloadSize
  4308  	packetsSent := 0
  4309  	for ; start < end; start += mss {
  4310  		rawEP.SendPacketWithTS(b[start:start+mss], tsVal)
  4311  		packetsSent++
  4312  	}
  4313  	// Resume the worker so that it only sees the packets once all of them
  4314  	// are waiting to be read.
  4315  	worker.ResumeWork()
  4316  
  4317  	// Since we read no bytes the window should goto zero till the
  4318  	// application reads some of the data.
  4319  	// Discard all intermediate acks except the last one.
  4320  	if packetsSent > 100 {
  4321  		for i := 0; i < (packetsSent / 100); i++ {
  4322  			_ = c.GetPacket()
  4323  		}
  4324  	}
  4325  	rawEP.VerifyACKRcvWnd(0)
  4326  
  4327  	time.Sleep(25 * time.Millisecond)
  4328  	// Verify that sending more data when window is closed is dropped and
  4329  	// not acked.
  4330  	rawEP.SendPacketWithTS(b[start:start+mss], tsVal)
  4331  
  4332  	// Verify that the stack sends us back an ACK with the sequence number
  4333  	// of the last packet sent indicating it was dropped.
  4334  	p := c.GetPacket()
  4335  	checker.IPv4(t, p, checker.TCP(
  4336  		checker.AckNum(uint32(rawEP.NextSeqNum)-uint32(mss)),
  4337  		checker.Window(0),
  4338  	))
  4339  
  4340  	// Now read all the data from the endpoint and verify that advertised
  4341  	// window increases to the full available buffer size.
  4342  	for {
  4343  		_, _, err := c.EP.Read(nil)
  4344  		if err == tcpip.ErrWouldBlock {
  4345  			break
  4346  		}
  4347  	}
  4348  
  4349  	// Verify that we receive a non-zero window update ACK. When running
  4350  	// under thread santizer this test can end up sending more than 1
  4351  	// ack, 1 for the non-zero window
  4352  	p = c.GetPacket()
  4353  	checker.IPv4(t, p, checker.TCP(
  4354  		checker.AckNum(uint32(rawEP.NextSeqNum)-uint32(mss)),
  4355  		func(t *testing.T, h header.Transport) {
  4356  			tcp, ok := h.(header.TCP)
  4357  			if !ok {
  4358  				return
  4359  			}
  4360  			if w := tcp.WindowSize(); w == 0 || w > uint16(wantRcvWnd) {
  4361  				t.Errorf("expected a non-zero window: got %d, want <= wantRcvWnd", w, wantRcvWnd)
  4362  			}
  4363  		},
  4364  	))
  4365  }
  4366  
  4367  // This test verifies that the auto tuning does not grow the receive buffer if
  4368  // the application is not reading the data actively.
  4369  func TestReceiveBufferAutoTuning(t *testing.T) {
  4370  	const mtu = 1500
  4371  	const mss = mtu - header.IPv4MinimumSize - header.TCPMinimumSize
  4372  
  4373  	c := context.New(t, mtu)
  4374  	defer c.Cleanup()
  4375  
  4376  	// Enable Auto-tuning.
  4377  	stk := c.Stack()
  4378  	// Set lower limits for auto-tuning tests. This is required because the
  4379  	// test stops the worker which can cause packets to be dropped because
  4380  	// the segment queue holding unprocessed packets is limited to 500.
  4381  	const receiveBufferSize = 80 << 10 // 80KB.
  4382  	const maxReceiveBufferSize = receiveBufferSize * 10
  4383  	if err := stk.SetTransportProtocolOption(tcp.ProtocolNumber, tcp.ReceiveBufferSizeOption{1, receiveBufferSize, maxReceiveBufferSize}); err != nil {
  4384  		t.Fatalf("SetTransportProtocolOption failed: %v", err)
  4385  	}
  4386  
  4387  	// Enable auto-tuning.
  4388  	if err := stk.SetTransportProtocolOption(tcp.ProtocolNumber, tcpip.ModerateReceiveBufferOption(true)); err != nil {
  4389  		t.Fatalf("SetTransportProtocolOption failed: %v", err)
  4390  	}
  4391  	// Change the expected window scale to match the value needed for the
  4392  	// maximum buffer size used by stack.
  4393  	c.WindowScale = uint8(tcp.FindWndScale(maxReceiveBufferSize))
  4394  
  4395  	rawEP := c.CreateConnectedWithOptions(header.TCPSynOptions{TS: true, WS: 4})
  4396  
  4397  	wantRcvWnd := receiveBufferSize
  4398  	scaleRcvWnd := func(rcvWnd int) uint16 {
  4399  		return uint16(rcvWnd >> uint16(c.WindowScale))
  4400  	}
  4401  	// Allocate a large array to send to the endpoint.
  4402  	b := make([]byte, receiveBufferSize*48)
  4403  
  4404  	// In every iteration we will send double the number of bytes sent in
  4405  	// the previous iteration and read the same from the app. The received
  4406  	// window should grow by at least 2x of bytes read by the app in every
  4407  	// RTT.
  4408  	offset := 0
  4409  	payloadSize := receiveBufferSize / 8
  4410  	worker := (c.EP).(interface {
  4411  		StopWork()
  4412  		ResumeWork()
  4413  	})
  4414  	tsVal := rawEP.TSVal
  4415  	// We are going to do our own computation of what the moderated receive
  4416  	// buffer should be based on sent/copied data per RTT and verify that
  4417  	// the advertised window by the stack matches our calculations.
  4418  	prevCopied := 0
  4419  	done := false
  4420  	latency := 1 * time.Millisecond
  4421  	for i := 0; !done; i++ {
  4422  		tsVal++
  4423  
  4424  		// Stop the worker goroutine.
  4425  		worker.StopWork()
  4426  		start := offset
  4427  		end := offset + payloadSize
  4428  		totalSent := 0
  4429  		packetsSent := 0
  4430  		for ; start < end; start += mss {
  4431  			rawEP.SendPacketWithTS(b[start:start+mss], tsVal)
  4432  			totalSent += mss
  4433  			packetsSent++
  4434  		}
  4435  		// Resume it so that it only sees the packets once all of them
  4436  		// are waiting to be read.
  4437  		worker.ResumeWork()
  4438  
  4439  		// Give 1ms for the worker to process the packets.
  4440  		time.Sleep(1 * time.Millisecond)
  4441  
  4442  		// Verify that the advertised window on the ACK is reduced by
  4443  		// the total bytes sent.
  4444  		expectedWnd := wantRcvWnd - totalSent
  4445  		if packetsSent > 100 {
  4446  			for i := 0; i < (packetsSent / 100); i++ {
  4447  				_ = c.GetPacket()
  4448  			}
  4449  		}
  4450  		rawEP.VerifyACKRcvWnd(scaleRcvWnd(expectedWnd))
  4451  
  4452  		// Now read all the data from the endpoint and invoke the
  4453  		// moderation API to allow for receive buffer auto-tuning
  4454  		// to happen before we measure the new window.
  4455  		totalCopied := 0
  4456  		for {
  4457  			b, _, err := c.EP.Read(nil)
  4458  			if err == tcpip.ErrWouldBlock {
  4459  				break
  4460  			}
  4461  			totalCopied += len(b)
  4462  		}
  4463  
  4464  		// Invoke the moderation API. This is required for auto-tuning
  4465  		// to happen. This method is normally expected to be invoked
  4466  		// from a higher layer than tcpip.Endpoint. So we simulate
  4467  		// copying to user-space by invoking it explicitly here.
  4468  		c.EP.ModerateRecvBuf(totalCopied)
  4469  
  4470  		// Now send a keep-alive packet to trigger an ACK so that we can
  4471  		// measure the new window.
  4472  		rawEP.NextSeqNum--
  4473  		rawEP.SendPacketWithTS(nil, tsVal)
  4474  		rawEP.NextSeqNum++
  4475  
  4476  		if i == 0 {
  4477  			// In the first iteration the receiver based RTT is not
  4478  			// yet known as a result the moderation code should not
  4479  			// increase the advertised window.
  4480  			rawEP.VerifyACKRcvWnd(scaleRcvWnd(wantRcvWnd))
  4481  			prevCopied = totalCopied
  4482  		} else {
  4483  			rttCopied := totalCopied
  4484  			if i == 1 {
  4485  				// The moderation code accumulates copied bytes till
  4486  				// RTT is established. So add in the bytes sent in
  4487  				// the first iteration to the total bytes for this
  4488  				// RTT.
  4489  				rttCopied += prevCopied
  4490  				// Now reset it to the initial value used by the
  4491  				// auto tuning logic.
  4492  				prevCopied = tcp.InitialCwnd * mss * 2
  4493  			}
  4494  			newWnd := rttCopied<<1 + 16*mss
  4495  			grow := (newWnd * (rttCopied - prevCopied)) / prevCopied
  4496  			newWnd += (grow << 1)
  4497  			if newWnd > maxReceiveBufferSize {
  4498  				newWnd = maxReceiveBufferSize
  4499  				done = true
  4500  			}
  4501  			rawEP.VerifyACKRcvWnd(scaleRcvWnd(newWnd))
  4502  			wantRcvWnd = newWnd
  4503  			prevCopied = rttCopied
  4504  			// Increase the latency after first two iterations to
  4505  			// establish a low RTT value in the receiver since it
  4506  			// only tracks the lowest value. This ensures that when
  4507  			// ModerateRcvBuf is called the elapsed time is always >
  4508  			// rtt. Without this the test is flaky due to delays due
  4509  			// to scheduling/wakeup etc.
  4510  			latency += 50 * time.Millisecond
  4511  		}
  4512  		time.Sleep(latency)
  4513  		offset += payloadSize
  4514  		payloadSize *= 2
  4515  	}
  4516  }