This page looks best with JavaScript enabled

Transport Layer Note

 ·  ☕ 4 min read

Some notes

References:
https://github.com/forthright48/notes/blob/master/books/computer-networking.md
https://blog.yfxie.com/comparison-of-network-gbn-sr-and-tcp/

中文版在下面

— English Version —

Transport Layer

Two important protocols: UDP and TCP

This article will mainly focus on how reliable transmission works.

To be reliable

  1. Error checking, checksum
  2. Detect lost packets (timeout, etc.)
  3. Feedback from receiver to make sure they have received, which might lose as well.
  4. Retransmisison

It’ll be slow if it waits for receiver’s feedback after sending every package.
Pipelining mechanism can help. Sender allows to send a few more following packets without receiving the feedback of the current one.

Automatic Repeat Request (ARQ)

https://blog.yfxie.com/comparison-of-network-gbn-sr-and-tcp/
2 mechanisms to recover from pipelining error.

  1. Go-Back-N (GBN)

Starting from the unacknowledged packet(base), sender can send n packets -> sliding-window protocol.
Receiving ack x means packets before x are received.
When timeout, resend packets from the base.

If receiver get (n+1)th packet but nth is not arrived, it’ll drop it.
This allows easier implementation and no need to buffer out of order packets.
Though there could be more retransmission.
When we lose nth packet, everything after that will need to be resent.
The only thing to keep is the number that indicates everything before is received.

  1. selective repeat
    Sender and receiver both have buffer to keep the out of order packets.
    Only lost packets need to be sent again.
    But every sent packet needs to have ack(feed).

Internet is unreliable

  1. packet broken
  2. packet lost
  3. packet order

So we need the following mechanisms

  1. checksum, error checking
  2. ACK, feedback
  3. retransmission timer
  4. sequence number to handle duplicate and lost packets
  5. pipeline to avoid the Stop-and-wait and wasting the bandwidth
  6. sliding window to track packet transmission status
  7. Go-Back-N for simple implementation
  8. selective repeat to reduce retransmission cost
  9. TTL for ensuring packet flushing

Connection-Oriented Transport: TCP

Piggybacking: ack or feedback signal does not go back to sender immediately. Instead, it waits for real data to be transmitted together.

https://notfalse.net/28/tcp-congestion-control#-Fast-Retransmit

TimeoutInterval usually is EstimatedRTT + 4⋅DevRTT

Single Timer for all unacknowledged packets

When there’s timeout, timeout interval is increased, congestion controll. Reset timeout interval when reveiving ack.

TCP is a combination of GBN and SR

TCP uses ack like GBN
TCP’s ack mean “next-expected-packet”, which is slightly different from GBN
Receiver buffers some unorder packets

Fast retransmission

If receiver expects packet x but keeps receiving packets like x+1, x+2 …, it will buffer these packets. It will also send 3 ack x in a row to let the sender knows that this package might be lost. Sender will resend the packet x before timeout.

Details can be found
https://blog.yfxie.com/comparison-of-network-gbn-sr-and-tcp/

Flow Control

To avoid sener sending too much data that receiver cannot handle, sender remembers that the last acknowledged byte and the last sent byte.
The unacknowledged packets number will have to be smaller than the window (rwnd, window will be sent to to sender via header)

Congestion Control

To avoid having too many packets on the network causing traffic jam.
Sender maintains a window, the number of unacknowledged packets cannot exceed min(rwnd, cwnd)
If there’s no traffic jam then increase cwnd, else decrease.

slow start

cwnd starts small, then double when increasing.
Stop when there’s tranmission issue, and set ssthresh to cwnd/2.
cwnd starts from 1 and doules everytime, after reaching ssthresh, it only increases 1 at a time (Congestion Avoidance mode).
Receiving 3 acks does not mean traffic jame, because other packets arrived correctly. So it’s fast recovery mode.

https://github.com/forthright48/notes/blob/master/books/computer-networking.md

— 中文版 —

Transport Layer

兩個重點 protocol: UDP and TCP

這篇主要著重在可靠傳輸上

可靠必須要

  1. 錯誤檢查 checksum
  2. 掉包檢查(timeout, etc.)
  3. 接收者的 feedback (確認正確接收),這東西也有可能掉
  4. 重傳

如果 sender 傳完一個後就等回應,太慢
所以可以一次傳多個(pipelining)
兩方都需要 buffer 一些資料

Automatic Repeat Request (ARQ)

https://blog.yfxie.com/comparison-of-network-gbn-sr-and-tcp/
兩種 pipeline 錯誤恢復的機制

  1. Go-Back-N (GBN)

最多可以傳送 n 個沒被 ack 的 packets,從最早的 unack packet(base) 開始算 n 個之內是可以傳送的 -> sliding-window protocol.
得到 ack x,代表 x 之前都收到了,所以 sender 不會認知到有交叉掉 packet 的情況

timeout 時從 base 開始都重送

receiver 沒收到 n 卻收到 n+1 時,會丟掉 (receive buffer = 1)
receiver 這樣實作比較簡單,不用 buffer 一堆 out of order packets,只要記住下一個需要得到的 sequence num 是啥就好

缺點就是可能造成更多重送
當第 n 個 packet 遺失時,之後的都要重送

  1. selective repeat

收送兩端都需要有 buffer,receiver 可以暫存 out of order 封包,sender 可能在收到 n+1 的 ack 時,還沒收到 n 的 ack,某個 packet 壞掉或掉包時只要重送那個就好
每個 packet 都要各別 ack

總結,不可靠網路會有一些問題

  1. 封包損壞
  2. 封包遺失
  3. 封包順序

所以必須用以下機制來處理

  1. checksum 檢查錯誤
  2. ACK 確認收到(遺失)
  3. 重送 timer
  4. sequence number 處理重複、遺失封包
  5. pipeline 避免 Stop-and-wait 這種超慢,頻寬利用率不佳
  6. sliding window 追蹤封包情況
  7. 累積 ack,go back n
  8. selective retransmission 減少重送成本
  9. TTL for ensuring packet flushing

Connection-Oriented Transport: TCP

Piggybacking: ack 之類的確認訊號不會馬上傳回給發送端,而是等接收端也想傳資料去發送端時順便給

https://notfalse.net/28/tcp-congestion-control#-Fast-Retransmit

TimeoutInterval 通常為 EstimatedRTT+4⋅DevRTT

Single Timer for all unacknowledged packets

當 timeout 出現時,我們增加 timeout 的時間,congestion controll,收到 ack 時會恢復

TCP 是 GBN and SR 的混合

TCP 也是使用累計型的 ack (像GBN)
TCP 回應的 ack 代表 “預期接下來收到的資料”,與 GO back n 的 “之前的都已經收到” 稍微不同

接收端會 buffer 一些 unorder 的 packets,以及保持一個接下來應該要收到的資料的 seq num

Fast retransmission

如果目前希望收到 x,但是一直收到非預期的封包(x+1, x+2…),會暫存這些封包,但會連續發送 3 個同樣的 ack x 跟發送端通知這個封包可能遺失了
發送端看到這個重複的 ack,就會在 x timeout 前就重送這“一個”封包 (像是 selective repeat)

裡面講的滿清楚的可以看
https://blog.yfxie.com/comparison-of-network-gbn-sr-and-tcp/

Flow Control

避免發送端送太快,接收端的 buffer 爆掉,程式來不及處理
發送端會記住最後一個被 ack 的 byte 還有最後一個送出的 byte
這些 unacknowledged packets 必須小於接收端的 window (rwnd, window 會透過 header 告知發送端)

Congestion Control

避免網路擁塞

發送端保持一個 window,unacknowledged packets 不可超過 min(rwnd, cwnd)
網路上都沒雍塞現象的話cwnd會增加,反之若收到壅塞訊號(例如timeout)則減少

slow start

剛開始 cwnd 會很小,然後兩倍兩倍成長
發現問題時就停止,並設定一個 ssthresh 為發現問題時的 cwnd/2

cwnd 從 1 開始,一樣兩倍兩倍長大,達到 ssthresh就開始一次只 +1 (Congestion Avoidance mode)

收到三個重複的 ack 則是不會被認為是擁塞,因為其他封包其實都有到,所以會進入 Fast Recovery mode

參考下面的,細節不重要
https://github.com/forthright48/notes/blob/master/books/computer-networking.md

Share on

Marko Peng
WRITTEN BY
Marko Peng
Good man