tcp

package
v0.0.0-...-494ab94 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 10, 2020 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Overview

Package tcp contains the implementation of the TCP transport protocol. To use it in the networking stack, this package must be added to the project, and activated on the stack by passing tcp.NewProtocol() as one of the transport protocols when calling stack.New(). Then endpoints can be created by passing tcp.ProtocolNumber as the transport protocol number when calling Stack.NewEndpoint().

Index

Constants

View Source
const (
	// ProtocolNumber is the tcp protocol number.
	ProtocolNumber = header.TCPProtocolNumber

	// MinBufferSize is the smallest size of a receive or send buffer.
	MinBufferSize = 4 << 10 // 4096 bytes.

	// DefaultSendBufferSize is the default size of the send buffer for
	// an endpoint.
	DefaultSendBufferSize = 1 << 20 // 1MB

	// DefaultReceiveBufferSize is the default size of the receive buffer
	// for an endpoint.
	DefaultReceiveBufferSize = 1 << 20 // 1MB

	// MaxBufferSize is the largest size a receive/send buffer can grow to.
	MaxBufferSize = 4 << 20 // 4MB

	// MaxUnprocessedSegments is the maximum number of unprocessed segments
	// that can be queued for a given endpoint.
	MaxUnprocessedSegments = 300

	// DefaultTCPLingerTimeout is the amount of time that sockets linger in
	// FIN_WAIT_2 state before being marked closed.
	DefaultTCPLingerTimeout = 60 * time.Second

	// DefaultTCPTimeWaitTimeout is the amount of time that sockets linger
	// in TIME_WAIT state before being marked closed.
	DefaultTCPTimeWaitTimeout = 60 * time.Second
)
View Source
const (

	// Open indicates that the sender is receiving acks in order and
	// no loss or dupACK's etc have been detected.
	//
	// Open 表示发送方正在按顺序接收 ACK ,没有发现丢失或 dupACKs 等情况。
	// 当网络中没有发生丢包,也就不需要重传,sender 按照正常的流程处理到来的ACK。
	Open ccState = iota

	// RTORecovery indicates that an RTO has occurred and the sender
	// has entered an RTO based recovery phase.
	//
	// RTORecovery 表示发生了超时(RTO),发送方进入了基于 RTO 的恢复阶段。
	//
	// 当超时后,TCP sender 进入 RTORecovery 状态,所有在网络中的包被标记为 lost ,
	// cwnd 重置为1,通过 slow start 重新增加 cwnd ,
	// RTORecovery 与 FastRecovery 状态的不同点在于,cwnd 会重置为 1 ,
	// 但是 FastRecovery 状态不会,它会降到之前的一半。
	//
	// RTORecovery 状态不能被其它任何状态中断,只有当网络中所有的包被成功 ACK 后,才能重新进入 Open 状态。
	RTORecovery

	// FastRecovery indicates that the sender has entered FastRecovery
	// based on receiving nDupAck's. This state is entered only when
	// SACK is not in use.
	//
	// FastRecovery 表示发送方在收到 nDupAck 的基础上进入 FastRecovery 状态。
	// 这个状态只有在 SACK 不使用时才会进入。
	//
	// 当 sender 连续收到多个(默认3)Dup ACK 时,意味着丢包了,
	// 这时 sender 会重传第一个未被 ACK 的包,并进入 Recovery 状态。
	// 在 Recovery 状态期间,cwnd 不会降低,要么重传标记了 lost 的包,要么根据保守原则发送新包。
	// 直到网络中所有的包都被 ACK ,才会退出 Recovery 进入 Open 状态,Recovery 状态可以被 RTORecovery 状态打断。
	FastRecovery

	// SACKRecovery indicates that the sender has entered SACK based recovery.
	//
	// SACKRecovery 表示发送方已进入基于 SACK 的恢复。
	SACKRecovery

	// Disorder indicates the sender either received some SACK blocks or dupACK's.
	//
	// Disorder 表示发送方要么收到一些 SACK 块,要么收到 dup ACK 。
	//
	// 当 sender 检测到 Dup ACK 或者 SACK ,将会转移到 Disorder 状态,当处在这个这个状态中时,
	// cwnd 将不做调整,但每收到一个 Dup ACK 或 SACK ,sender 将发送一个新包。
	Disorder
)
View Source
const (

	// InitialCwnd is the initial congestion window.
	//
	// InitialCwnd 是初始拥塞窗口。
	InitialCwnd = 10
)
View Source
const (
	// MaxSACKBlocks is the maximum number of SACK blocks stored at receiver side.
	MaxSACKBlocks = 6
)

Variables

View Source
var (

	// SynRcvdCountThreshold is the global maximum number of connections
	// that are allowed to be in SYN-RCVD state before TCP starts using SYN
	// cookies to accept connections.
	//
	// It is an exported variable only for testing, and should not otherwise
	// be used by importers of this package.
	//
	// SynRcvdCountThreshold 是指在 TCP 开始使用 SYN cookies 接受连接之前,
	// 允许处于 SYN-RCVD 状态的全局最大连接数。这个导出变量只用于测试,正常情况下不应该被包的引入者使用。
	SynRcvdCountThreshold uint64 = 1000
)

Functions

func FindWndScale

func FindWndScale(wnd seqnum.Size) int

FindWndScale determines the window scale to use for the given maximum window size.

FindWndScale() 根据给定的最大窗口大小 wnd 确定窗口的缩放比例。

备注:

0xffff  = 65535
0x10000 = 65536

func NewProtocol

func NewProtocol() stack.TransportProtocol

NewProtocol returns a TCP transport protocol.

func TrimSACKBlockList

func TrimSACKBlockList(sack *SACKInfo, rcvNxt seqnum.Value)

TrimSACKBlockList updates the sack block list by removing/modifying any block where start is < rcvNxt.

func UpdateSACKBlocks

func UpdateSACKBlocks(sack *SACKInfo, segStart seqnum.Value, segEnd seqnum.Value, rcvNxt seqnum.Value)

UpdateSACKBlocks updates the list of SACK blocks to include the segment specified by segStart->segEnd. If the segment happens to be an out of order delivery then the first block in the sack.blocks always includes the segment identified by segStart->segEnd.

Types

type DelayEnabled

type DelayEnabled bool

DelayEnabled option can be used to enable Nagle's algorithm in the TCP protocol.

type EndpointInfo

type EndpointInfo struct {
	stack.TransportEndpointInfo

	// HardError is meaningful only when state is stateError. It stores the
	// error to be returned when read/write syscalls are called and the
	// endpoint is in this state. HardError is protected by endpoint mu.
	HardError *tcpip.Error
}

EndpointInfo holds useful information about a transport endpoint which can be queried by monitoring tools.

+stateify savable

func (*EndpointInfo) IsEndpointInfo

func (*EndpointInfo) IsEndpointInfo()

IsEndpointInfo is an empty method to implement the tcpip.EndpointInfo marker interface.

type EndpointState

type EndpointState uint32

EndpointState represents the state of a TCP endpoint.

const (

	// Endpoint states internal to netstack. These map to the TCP state CLOSED.
	StateInitial EndpointState = iota
	StateBound
	StateConnecting // Connect() called, but the initial SYN hasn't been sent.
	StateError

	// TCP protocol states.
	StateEstablished
	StateSynSent
	StateSynRecv
	StateFinWait1
	StateFinWait2
	StateTimeWait
	StateClose
	StateCloseWait
	StateLastAck
	StateListen
	StateClosing
)

Endpoint states. Note that are represented in a netstack-specific manner and may not be meaningful externally. Specifically, they need to be translated to Linux's representation for these states if presented to userspace.

func (EndpointState) String

func (s EndpointState) String() string

String implements fmt.Stringer.String.

type Forwarder

type Forwarder struct {
	// contains filtered or unexported fields
}

Forwarder is a connection request forwarder, which allows clients to decide what to do with a connection request, for example: ignore it, send a RST, or attempt to complete the 3-way handshake.

The canonical way of using it is to pass the Forwarder.HandlePacket function to stack.SetTransportProtocolHandler.

func NewForwarder

func NewForwarder(s *stack.Stack, rcvWnd, maxInFlight int, handler func(*ForwarderRequest)) *Forwarder

NewForwarder allocates and initializes a new forwarder with the given maximum number of in-flight connection attempts. Once the maximum is reached new incoming connection requests will be ignored.

If rcvWnd is set to zero, the default buffer size is used instead.

func (*Forwarder) HandlePacket

func (f *Forwarder) HandlePacket(r *stack.Route, id stack.TransportEndpointID, pkt tcpip.PacketBuffer) bool

HandlePacket handles a packet if it is of interest to the forwarder (i.e., if it's a SYN packet), returning true if it's the case. Otherwise the packet is not handled and false is returned.

This function is expected to be passed as an argument to the stack.SetTransportProtocolHandler function.

type ForwarderRequest

type ForwarderRequest struct {
	// contains filtered or unexported fields
}

ForwarderRequest represents a connection request received by the forwarder and passed to the client. Clients must eventually call Complete() on it, and may optionally create an endpoint to represent it via CreateEndpoint.

func (*ForwarderRequest) Complete

func (r *ForwarderRequest) Complete(sendReset bool)

Complete completes the request, and optionally sends a RST segment back to the sender.

func (*ForwarderRequest) CreateEndpoint

func (r *ForwarderRequest) CreateEndpoint(queue *waiter.Queue) (tcpip.Endpoint, *tcpip.Error)

CreateEndpoint creates a TCP endpoint for the connection request, performing the 3-way handshake in the process.

func (*ForwarderRequest) ID

ID returns the 4-tuple (src address, src port, dst address, dst port) that represents the connection request.

type ReceiveBufferSizeOption

type ReceiveBufferSizeOption struct {
	Min     int
	Default int
	Max     int
}

ReceiveBufferSizeOption allows the default, min and max receive buffer size for TCP endpoints to be queried or configured.

type ReceiveErrors

type ReceiveErrors struct {
	tcpip.ReceiveErrors

	// SegmentQueueDropped is the number of segments dropped due to a full segment queue.
	// SegmentQueueDropped 是指由于段队列满而丢弃的 segment 数。
	SegmentQueueDropped tcpip.StatCounter

	// ChecksumErrors is the number of segments dropped due to bad checksums.
	// ChecksumErrors 是指由于校验和错误而丢失的 segment 数。
	ChecksumErrors tcpip.StatCounter

	// ListenOverflowSynDrop is the number of times the listen queue overflowed and a SYN was dropped.
	// ListenOverflowSynDrop 是监听队列溢出和 SYN 被丢弃的次数。
	ListenOverflowSynDrop tcpip.StatCounter

	// ListenOverflowAckDrop is the number of times the final ACK in the handshake was dropped due to overflow.
	// ListenOverflowAckDrop 是指握手中最后一次 ACK 因溢出而丢弃的次数。
	ListenOverflowAckDrop tcpip.StatCounter

	// ZeroRcvWindowState is the number of times we advertised a zero receive window when rcvList is full.
	// 零接收窗口状态(ZeroRcvWindowState)是指当 rcvList 已满时,我们报告零接收窗口的次数。
	ZeroRcvWindowState tcpip.StatCounter
}

ReceiveErrors collect segment receive errors within transport layer. ReceiveErrors 收集传输层内的 segment 接收错误。

type SACKEnabled

type SACKEnabled bool

SACKEnabled option can be used to enable SACK support in the TCP protocol. See: https://tools.ietf.org/html/rfc2018.

type SACKInfo

type SACKInfo struct {
	// Blocks is the maximum number of SACK blocks we track per endpoint.
	// Blocks 是我们跟踪每个端点的最大 SACK 块数。
	Blocks [MaxSACKBlocks]header.SACKBlock

	// NumBlocks is the number of valid SACK blocks stored in the blocks array above.
	// NumBlocks 是指存储在上述块数组中的有效 SACK 块的数量。
	NumBlocks int
}

SACKInfo holds TCP SACK related information for a given endpoint. SACKInfo 保存了给定端点的 TCP SACK 相关信息。

+stateify savable

type SACKScoreboard

type SACKScoreboard struct {
	// contains filtered or unexported fields
}

SACKScoreboard stores a set of disjoint SACK ranges. SACKScoreboard 存储一组不连续的 SACK 范围。

+stateify savable

func NewSACKScoreboard

func NewSACKScoreboard(smss uint16, iss seqnum.Value) *SACKScoreboard

NewSACKScoreboard returns a new SACK Scoreboard.

func (*SACKScoreboard) Copy

func (s *SACKScoreboard) Copy() (sackBlocks []header.SACKBlock, maxSACKED seqnum.Value)

Copy provides a copy of the SACK scoreboard.

func (*SACKScoreboard) Delete

func (s *SACKScoreboard) Delete(seq seqnum.Value)

Delete removes all SACK information prior to seq.

func (*SACKScoreboard) Empty

func (s *SACKScoreboard) Empty() bool

Empty returns true if the SACK scoreboard has no entries, false otherwise.

func (*SACKScoreboard) Insert

func (s *SACKScoreboard) Insert(r header.SACKBlock)

Insert inserts/merges the provided SACKBlock into the scoreboard.

func (*SACKScoreboard) IsLost

func (s *SACKScoreboard) IsLost(seq seqnum.Value) bool

IsLost implements the IsLost(SeqNum) operation defined in RFC3517 section 4.

This routine returns whether the given sequence number is considered to be lost.

The routine returns true when either nDupAckThreshold discontiguous SACKed sequences have arrived above 'SeqNum' or (nDupAckThreshold * SMSS) bytes with sequence numbers greater than 'SeqNum' have been SACKed. Otherwise, the routine returns false.

IsLost() 实现了 RFC3517 第 4 节中定义的 IsLost(SeqNum) 操作,它返回给定序号 seq 是否被认为是丢失的。

当 nDupAckThreshold 不连续时,这个例程返回 true 。 当 nDupAckThreshold 不连续的 SACKed 序列到达的时间超过 'SeqNum' 或者 (nDupAckThreshold * SMSS) 序列号大于 'SeqNum' 的字节被SACKed时, 该例程返回true。

返回真的情况: (a) 当 nDupAckThreshold 个超过 SeqNum 的不连续的 SACKed 序号到达, (b)

否则,该函数返回 false 。

这个函数返回是否给定的序列号已经丢失。 * 返回真的条件:DupThresh 个超过 SeqNum 的不连续的 SACKed 序号到达,或者超过 (DupThresh - 1) * SMSS 字节的序列号被选择性应答。 * 返回假的条件:其他情况。

func (*SACKScoreboard) IsRangeLost

func (s *SACKScoreboard) IsRangeLost(r header.SACKBlock) bool

IsRangeLost implements the IsLost(SeqNum) operation defined in RFC 6675 section 4 but operates on a range of sequence numbers and returns true if there are at least nDupAckThreshold SACK blocks greater than the range being checked or if at least (nDupAckThreshold-1)*s.smss bytes have been SACKED with sequence numbers greater than the block being checked.

func (*SACKScoreboard) IsSACKED

func (s *SACKScoreboard) IsSACKED(r header.SACKBlock) bool

IsSACKED returns true if the a given range of sequence numbers denoted by r are already covered by SACK information in the scoreboard.

如果由 r 表示的给定序列号范围已经被记分板中的 SACK 信息所覆盖,则 IsSACKED 返回 true ,此时无需重复添加 sack 块。

func (*SACKScoreboard) MaxSACKED

func (s *SACKScoreboard) MaxSACKED() seqnum.Value

MaxSACKED returns the highest sequence number ever inserted in the SACK scoreboard.

func (*SACKScoreboard) Reset

func (s *SACKScoreboard) Reset()

Reset erases all known range information from the SACK scoreboard.

func (*SACKScoreboard) SMSS

func (s *SACKScoreboard) SMSS() uint16

SMSS returns the sender's MSS as held by the SACK scoreboard.

func (*SACKScoreboard) Sacked

func (s *SACKScoreboard) Sacked() seqnum.Size

Sacked returns the current number of bytes held in the SACK scoreboard.

func (*SACKScoreboard) String

func (s *SACKScoreboard) String() string

Dump prints the state of the scoreboard structure.

type SendBufferSizeOption

type SendBufferSizeOption struct {
	Min     int
	Default int
	Max     int
}

SendBufferSizeOption allows the default, min and max send buffer sizes for TCP endpoints to be queried or configured.

type SendErrors

type SendErrors struct {
	tcpip.SendErrors

	// SegmentSendToNetworkFailed is the number of TCP segments failed to be sent
	// to the network endpoint.
	SegmentSendToNetworkFailed tcpip.StatCounter

	// SynSendToNetworkFailed is the number of TCP SYNs failed to be sent
	// to the network endpoint.
	SynSendToNetworkFailed tcpip.StatCounter

	// Retransmits is the number of TCP segments retransmitted.
	Retransmits tcpip.StatCounter

	// FastRetransmit is the number of segments retransmitted in fast
	// recovery.
	FastRetransmit tcpip.StatCounter

	// Timeouts is the number of times the RTO expired.
	Timeouts tcpip.StatCounter
}

SendErrors collect segment send errors within the transport layer.

type Stats

type Stats struct {
	// SegmentsReceived is the number of TCP segments received that
	// the transport layer successfully parsed.
	SegmentsReceived tcpip.StatCounter

	// SegmentsSent is the number of TCP segments sent.
	SegmentsSent tcpip.StatCounter

	// FailedConnectionAttempts is the number of times we saw Connect and
	// Accept errors.
	FailedConnectionAttempts tcpip.StatCounter

	// ReceiveErrors collects segment receive errors within the
	// transport layer.
	ReceiveErrors ReceiveErrors

	// ReadErrors collects segment read errors from an endpoint read call.
	ReadErrors tcpip.ReadErrors

	// SendErrors collects segment send errors within the transport layer.
	SendErrors SendErrors

	// WriteErrors collects segment write errors from an endpoint write call.
	WriteErrors tcpip.WriteErrors
}

Stats holds statistics about the endpoint.

func (*Stats) IsEndpointStats

func (*Stats) IsEndpointStats()

IsEndpointStats is an empty method to implement the tcpip.EndpointStats marker interface.

Directories

Path Synopsis
testing
context
Package context provides a test context for use in tcp tests.
Package context provides a test context for use in tcp tests.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL