aldy120 / High-Performance-Browser-Networking

A reading note from <High Performance Browser Networking by Ilya Grigorik>.
0 stars 0 forks source link

Building Blocks of TCP #2

Open aldy120 opened 5 years ago

aldy120 commented 5 years ago

Internet

IPv4

為什麼是從 v4 開始,之前的版本呢

之前的版本中,TCP 跟 IP 是攪和在一起的,直到 IPv4 的時候,才真正地被分成兩個 RFC 看獨立出來。因此並沒有 v1-v3 的版本。

TFO: TCP Fast Open

利用 cookie 記錄來分辨之前連線過的 TCP ,這樣可以 handshake syn 的時候就傳送部分的 data ,不用等到 handshake 完後。

Congestion

Flow Control

有時候 client 或 server 其中一方可能很忙,無法處理突然爆量的資訊了。為了避免其中一方清楚狀況,突然傳大量資訊把另一方弄爆,在傳資訊時可使用 rwnd (receive window) 來告知對方自己最多可以收多大量。

rwnd = 0 的時候,那下一次對方還會傳過來嗎?

Congestion Control

Flow Control 只能讓發送與接收方暸解對方最多可承受的資訊量,但是並不能提供背景線路頻寬最多的承受量。有可能發生的情形是,接收方還能應付,但是中間線路的某機器倍流量衝爆了。

server 先出一個 cwnd (congestion window) 來確保不會一次衝爆後面的機器。這個是傳資料方自己保留的,沒有要給另一端知道。如果成功回應的話,表示網路狀況還行,還可以傳更多試試看。

最大可以傳送的資料量,會在 max(rwnd, cwnd) 。

因此我們可以估算傳送資料量 N 需要多少時間。詳見 https://github.com/igrigorik/hpbn.co/issues/5

slow-start restart

即使是同一個位置,在一段時見過後,若要重新建立連線,也會重最低速度開始,兩倍往上增加。除非把這功能關閉。

Congerstion avoidance

TCP 利用 packet loss 來判斷是否發生 congestion ,所以 packet loss 是預期會發生的事情。

AIMD: Additive Increase and Multiplicative Decrease

若發現 packet loss 則砍半 cwnd ,若成功則增加常數的 cwnd 。

PRR: Proportional Rate Reduction

一個新的 congestion avoidance 演算法,比起 AIMD 恢復得更快。

aldy120 commented 5 years ago

BDP: Bandwidth-delay Product

根據 RTT 與 window size 算出的可使用最大的 bandwidth 。原因是每次送出一堆 packets ,都必須等待 ACKs 回來,理論上在一個 RTT 時間內,只能傳一個 window size 的 packets 。

Bandwidth 指的是一秒可以傳多少量的 packets ,而 window size 指出每個 RTT 可以傳多少 packets 。 Bandwidth = window size / RTT

aldy120 commented 5 years ago

Head-of-Line Blocking

TCP provides the abstraction of a reliable network running over an unreliable channel.

TCP packet 內含有 sequence number ,如果一個 packet 沒收到,後面的 packet 會先存放在 TCP buffer 等待。

WebRTC 使用的是 UDP 。有時候直播類型的 applicaiton 是寧願 loss 也不想 wait 。

aldy120 commented 5 years ago

Tuning application behavior

Checklist

Upgrade server kernel to latest version.

Ensure that cwnd size is set to 10.

Ensure that window scaling is enabled.

Disable slow-start after idle.

Investigate enabling TCP Fast Open.

Eliminate redundant data transfers.

Compress transferred data.

Position servers closer to the user to reduce roundtrip times.

Reuse TCP connections whenever possible.

Investigate "TCP Tuning for HTTP" recommendations.