標籤:des c style blog a http
Transmission Control Protocol, TCP是一種連線導向的、可靠的、基於位元組流的傳輸層通訊協定.
應用程式層向TCP層發送用於網間傳輸的、用8位位元組表示的資料流,然後TCP把資料流分區成適當長度的報文段(通常受該電腦串連的網路的資料連結層的傳輸單元最大值(MTU:Maximum Transmission Unit)的限制)。之後TCP把結果包傳給IP層,由它來通過網路將包傳送給接收端實體的TCP層。TCP為了保證不發生丟包,就給每個包一個序號,同時序號也保證了傳送到接收端實體的包的按序接收。然後接收端實體對已成功收到的包發回一個相應的確認(ACK);如果發送端實體在合理的往返時延(RTT:Round-Trip Time)內未收到確認,那麼對應的資料包就被假設為已丟失將會被進行重傳。TCP用一個校正和函數來檢驗資料是否有錯誤;在發送和接收時都要計算校正和。
TCP串連包括三個狀態:串連建立、資料傳送和串連終止。
MSS
The maximum segment size (MSS) is the largest amount of data, specified in bytes, that TCP is willing to receive in a single segment. For best performance, the MSS should be set small enough to avoid IP fragmentation, which can lead to packet loss and excessive retransmissions. To try to accomplish this, typically the MSS is announced by each side using the MSS option when the TCP connection is established, in which case it is derived from the maximum transmission unit (MTU) size of the data link layer of the networks to which the sender and receiver are directly attached.
MSS announcement is also often called "MSS negotiation". Strictly speaking, the MSS is not "negotiated" between the originator and the receiver, because that would imply that both originator and receiver will negotiate and agree upon a single, unified MSS that applies to all communication in both directions of the connection. In fact, two completely independent values of MSS are permitted for the two directions of data flow in a TCP connection.
Connection establishment串連建立
TCP用三路握手(three-way handshake)過程建立一個串連。在串連建立過程中,很多參數要被初始化,例如序號被初始化以保證按序傳輸和串連的強壯性。
一對終端同時初始化一個它們之間的串連是可能的。但通常是由一端開啟一個通訊端(socket)然後監聽來自另一方的串連,這就是通常所指的被動開啟(passive open)。伺服器端被被動開啟以後,使用者端就能開始建立主動開啟(active open)。
- SYN: The active open is performed by the client sending a SYN to the server. The client sets the segment‘s sequence number to a random value A.
- SYN-ACK: In response, the server replies with a SYN-ACK. The acknowledgment number is set to one more than the received sequence number i.e. A+1, and the sequence number that the server chooses for the packet is another random number, B.
- ACK: Finally, the client sends an ACK back to the server. The sequence number is set to the received acknowledgement value i.e. A+1, and the acknowledgement number is set to one more than the received sequence number i.e. B+1.
Data transfer資料轉送
在TCP的資料傳送狀態,很多重要的機制保證了TCP的可靠性和強壯性。它們包括:使用序號,對收到的TCP報文段進行排序以及檢測重複的資料;使用校正和來檢測報文段的錯誤;使用確認和計時器來檢測和糾正丟包或延時。
在TCP的串連建立狀態,兩個主機的TCP層間要交換初始序號(ISN:initial sequence number)。這些序號用於標識位元組流中的資料,並且還是對應用程式層的資料位元組進行記數的整數。通常在每個TCP報文段中都有一對序號和確認號。TCP報文寄件者認為自己的位元組編號為序號,而認為接收者的位元組編號為確認號。TCP報文的接收者為了確保可靠性,在接收到一定數量的連續位元組流後才發送確認。這是對TCP的一種擴充,通常稱為選擇確認(Selective Acknowledgement)。選擇確認使得TCP接收者可以對亂序到達的資料區塊進行確認。每一個位元組傳輸過後,ISN號都會遞增1。
通過使用序號和確認號,TCP層可以把收到的報文段中的位元組按正確的順序交付給應用程式層。序號是32位的無符號數,在它增大到232-1時,便會迴繞到0。對於ISN的選擇是TCP中關鍵的一個操作,它可以確保強壯性和安全性。
TCP資料轉送不同於UDP之處
- Ordered data transfer — the destination host rearranges according to sequence number
- Retransmission of lost packets — any cumulative stream not acknowledged is retransmitted
- Error-free data transfer
- Flow control — limits the rate a sender transfers data to guarantee reliable delivery. The receiver continually hints the sender on how much data can be received (controlled by the sliding window). When the receiving host‘s buffer fills, the next acknowledgment contains a 0 in the window size, to stop transfer and allow the data in the buffer to be processed.
- Congestion control
Connection termination通路的終結
串連終止使用了四路握手過程(four-way handshake),在這個過程中每個終端的串連都能獨立地被終止。因此,一個典型的拆接過程需要每個終端都提供一對FIN和ACK。
連接埠
TCP使用了連接埠號碼(Port number)的概念來標識發送方和接收方的應用程式層。對每個TCP串連的一端都有一個相關的16位的無符號連接埠號碼分配給它們。
Port numbers are categorized into three basic categories: well-known, registered, and dynamic/private. The well-known ports are assigned by the Internet Assigned Numbers Authority (IANA) and are typically used by system-level or root processes. Well-known applications running as servers and passively listening for connections typically use these ports. Some examples include: FTP (20 and 21), SSH (22), TELNET (23), SMTP (25), SSL (443) and HTTP (80). Registered ports are typically used by end user applications as ephemeral source ports when contacting servers, but they can also identify named services that have been registered by a third party. Dynamic/private ports can also be used by end user applications, but are less commonly so. Dynamic/private ports do not contain any meaning outside of any particular TCP connection.
Flow control流量控制
TCP uses an end-to-end flow control protocol to avoid having the sender send data too fast for the TCP receiver to receive and process it reliably. Having a mechanism for flow control is essential in an environment where machines of diverse network speeds communicate. For example, if a PC sends data to a smartphone that is slowly processing received data, the smartphone must regulate the data flow so as not to be overwhelmed.
TCP uses a sliding window flow control protocol. In each TCP segment, the receiver specifies in the receive window field the amount of additionally received data (in bytes) that it is willing to buffer for the connection. The sending host can send only up to that amount of data before it must wait for an acknowledgment and window update from the receiving host.
If a receiver is processing incoming data in small increments, it may repeatedly advertise a small receive window. This is referred to as the silly window syndrome, since it is inefficient to send only a few bytes of data in a TCP segment, given the relatively large overhead of the TCP header.
Congestion control擁塞控制
Modern implementations of TCP contain four intertwined algorithms: Slow-start, congestion avoidance, fast retransmit, and fast recovery.
總共只有兩種模式:Slow-start, congestion avoidance.
Basic slow-start
The algorithm begins in the exponential growth phase initially with a Congestion Window Size (CWND) of 1, 2 or 10 segments and increases it by one Segment Size (SS) for each new ACK received. If the receiver sends an ACK for every segment, this behavior effectively doubles the window size each round trip of the network. If the receiver supports delayed ACKs, the rate of increase is lower, but still increases by a minimum of one MSS each round-trip time. This behavior continues until the congestion window size (CWND) reaches the size of the receiver‘s advertised window or until a loss occurs.
When a loss occurs, half of the current CWND is saved as a Slow Start Threshold (SSThresh) and slow start begins again from its initial CWND. Once the CWND reaches the SSThresh, TCP goes into congestion avoidance mode where each new ACK increases the CWND by SS × SS / CWND. This results in a linear increase of the CWND.
慢啟動->loss occur->set ssthresh -> 慢啟動->congestion avoidance,線性增
通過half threshold來實現乘性減。
Fast recovery
There is a variation to the slow-start algorithm known as Fast Recovery, which uses fast retransmit followed by Congestion Avoidance. In the Fast Recovery algorithm, during Congestion Avoidance mode, when packets (detected through 3 duplicate ACKs) are not received, the congestion window size is reduced to the slow-start threshold, rather than the smaller initial value.
Fast Recovery也是一種慢啟動->loss occur->set ssthresh
這個快一點,就是直接half。
congestion avoidance
When the congestion window exceeds SSThresh the algorithm enters a new state, called congestion avoidance.
Transmission Control Protocol (TCP) uses a network congestion-avoidance algorithm that includes various aspects of an additive increase/multiplicative decrease (AIMD) scheme, with other schemes such as slow-start to achieve congestion avoidance.
AIMD有許多變種實現。
As long as non-duplicate ACKs are received, the congestion window is additively increased by one MSS every round trip time. When a packet is lost, the likelihood of duplicate ACKs being received is very high (it‘s possible though unlikely that the stream just underwent extreme packet reordering, which would also prompt duplicate ACKs). The behavior of Tahoe and Reno differ in how they detect and react to packet loss:
Tahoe: Triple duplicate ACKS are treated the same as a timeout. Tahoe will perform "fast retransmit", set the slow start threshold to half the current congestion window, reduce congestion window to 1 MSS, and reset to slow-start state. (同Basic slow-start)
Reno: If three duplicate ACKs are received (i.e., four ACKs acknowledging the same packet, which are not piggybacked on data, and do not change the receiver‘s advertised window), Reno will halve the congestion window (instead of setting it to 1 MSS like Tahoe), set the slow start threshold equal to the new congestion window, perform a fast retransmit, and enter a phase called Fast Recovery. If an ACK times out, slow start is used as it is with Tahoe.
Fast Recovery. (Reno Only) In this state, TCP retransmits the missing packet that was signaled by three duplicate ACKs, and waits for an acknowledgment of the entire transmit window before returning to congestion avoidance. If there is no acknowledgment, TCP Reno experiences a timeout and enters the slow-start state.
Both algorithms reduce congestion window to 1 MSS on a timeout event.
這兩種方式的區別在於怎麼處理loss。slow start是一種狀態,fast recovery是Reno在處理loss時的策略。