傳輸層
多路複用
多路分用
傳輸層的目標:
- provide logical communication between app processes running on different hosts
傳輸層和網絡層的關係
network layer:
- logical communication between hosts 主機之間的傳輸
transport layer:
- logical communication between processes 進程之間的傳輸
傳輸層的工作流程
transport protocols(TCP、UDP) run in end systems 傳輸協議工作在端系統
- Send side: breaks app messages into segments, passes to network layer 把應用層的報文分段發送,體現了多路複用
- Receive side: reassembles segments into messages, passes to app layer 再把報文組合起來,發送給應用層,體現了多路分用
傳輸層的多路複用和多路分用
multiplexing at sender 多路複用:
將一個數據包分爲多段(在應用層),併爲每個數據封裝加入頭部數據生成一個segment報文段,通過socket接口發送給網絡層
demultiplexing at receiver 多路分用:
收到多個報文段,傳輸層根據頭部數據的目標端口號分給不同的socket,再給到相應的進程
Connectionless demultiplexing 不建立連接的多路分用(UDP)
UDP protocol is unreliable, unordered delivery
二元組標識:
- destination IP address
- destination port
跟目標的ip地址和端口號有關,跟源地址和端口號無關
Connection-oriented demux 面向連接的多路複用(TCP)
TCP protocol is reliable, in-order delivery
四元組標識:
- source IP address
- source port number
- dest IP address
- dest port number
會爲每個連接開啓不同的socket
服務端有主進程,當外部有報文進來的時候,主進程會創建一個子進程與之通信,所以一個主進程會對應多個socket,每個socket通過四元組屬性標識一個源主機(的某個socket)
一般採用多線程機制,線程耗用資源比進程少得多
一個進程對應多個線程,一個線程對應一個socket
可靠的數據傳輸原則
top-10 list of important networking topics
reliable data transfer protocol (rdt)可靠數據傳輸協議
可以令數據在不可靠的信道上進行可靠傳輸
udt_send() :被可靠傳輸協議調用,作用是在不可靠信道中傳輸數據
rdt_rcv() :接收到不可靠信道中數據
FSM(finite state machines )有限狀態機表示
事件或者操作引起狀態1向狀態2轉變
可靠傳輸協議1.0版本(rdt 1.0)
- no bit errors
- no loss of packets
可靠傳輸協議2.0版本(rdt 2.0)
- checksum to detect bit errors
差錯檢測,並且發送反饋,如果有錯誤,發送端重傳數據
control message:
- acknowledgements (ACKs): receiver explicitly tells sender that pkt received OK
- negative acknowledgements (NAKs): receiver explicitly tells sender that pkt had errors
- sender retransmits pkt on receipt of NAK
FSM 表示
當沒有錯誤時:
當有錯誤時:
可靠傳輸協議2.1版本(rdt 2.1)
發送方的FSM
有錯誤時的處理過程:
無錯時的處理過程:
接收方的FSM
有錯誤時的處理過程:
無錯時的處理過程:
2.1和2.0版本的區別
- 放送方兩個序號就足夠用了
- 2.0發送方需要兩個狀態機表示,
- 2.1沒辦法判斷接受方是否知道發送方?????
可靠傳輸協議2.2版本(rdt 2.2)
只使用ACK肯定報文
可靠傳輸協議3.0版本(rdt 3.0)
考慮到丟包和錯誤
流水線傳輸協議(Pipelined protocols)
傳輸層的UDP協議
UDP segments may be:
- lost packet
- delivered out-of-order 有可能導致接收方接受報文時無序,這時候應該
UDP used:
- streaming multimedia apps
- DNS
- SNMP
UDP 報文頭
length:in bytes of UDP segment, including header 用戶數據報的總長度,以字節爲單位
checksum:detect “errors” in transmitted segment 檢測 UDP 用戶數據報在傳輸中是否有錯
UDP中的校驗和機制
UDP的校驗和 = 僞首部 + 首部 + 數據部分,一起都檢驗
僞首部:
UDP檢驗和的計算方法是:
- 按每16位求和得出一個32位的數;
- 如果這個32位的數,高16位不爲0,則高16位加低16位再得到一個32位的數;
- 重複第2步直到高16位爲0,將低16位取反,得到校驗和。