Skip to content

Commit

Permalink
docs: update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
suyuan32 committed Feb 16, 2024
1 parent f4502ee commit c49f602
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 12 deletions.
58 changes: 54 additions & 4 deletions src/en/guide/concepts/network/2-tcp-udp.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ If the client sends a `SYN` command, and drops the line before the server return

:::

##### Sequence numbers and acknowledgment signals
##### **Sequence numbers and acknowledgment signals**

TCP uses sequence numbers to sort and deduplicate packets, and through the ACK acknowledgment mechanism, it ensures that packets are successfully delivered, ensuring the integrity of the data.

##### Timeout retransmission
##### **Timeout retransmission**

TCP can retransmit packets until an ACK acknowledgment is received when packets are lost or delayed, through the timeout retransmission mechanism.

Expand Down Expand Up @@ -137,9 +137,59 @@ The SACK method ([RFC 2018](https://www.ietf.org/rfc/rfc2883.txt)) adds a buffer
D-SACK ([RFC 2883](https://datatracker.ietf.org/doc/html/rfc2018)) mainly solves the problem of ACK loss. D-SACK uses the first segment of SACK as a flag to mark the packets that have been ACKed. When the receiving end receives a duplicate packet, it will write the packet into the D-SACK flag, telling the sender that the packet has been received, and the ACK may have been lost.
:::

#### Checksum
##### **Flow Control**

TCP also calculates a checksum on the data to ensure that the data was not lost or corrupted during transmission.
TCP uses a sliding window to control traffic, allowing the sender to control the speed of data transmission based on the receiver's receiving capacity.

::: warning
The receiver will write the currently available window size into the TCP header when responding with `ACK`, and the sender adjusts the transmission rate based on the window size.
:::

##### **Congestion Control**

::: tip Algorithms
- Slow Start
- Congestion Avoidance
- Fast Retransmit
- Fast Recovery
:::

![Congestion Control](/assets/image/article/network/tcp-congestion-control.png)

::: info Related Terms
- cwnd - Congestion Window
- ssthresh - Slow Start Threshold
:::

::: important

- If `cwnd < ssthresh`, use the Slow Start algorithm.

- If `cwnd > ssthresh`, use the Congestion Avoidance algorithm.

- If `cwnd = ssthresh`, use either the Slow Start or Congestion Avoidance algorithm.
:::

###### **Slow Start**

In the Slow Start phase, the initial value of `cwnd` is `1`, and `cwnd` will double every time a propagation round passes, until it reaches `ssthresh`.

###### **Congestion Avoidance**

When `cwnd` reaches `ssthresh`, the Congestion Avoidance algorithm will be executed. At this time, the growth of `cwnd` changes from doubling to adding `1` every time a propagation round passes. If the sender detects network congestion (i.e., the sent message does not get a timely response), it will set `ssthresh` to half of the `cwnd` value when network congestion occurs, and the value of `cwnd` will be reset to `1`, and Slow Start will be re-executed.

###### **Fast Retransmit and Fast Recovery**

Fast Retransmit has been introduced earlier, that is, if three consecutive duplicate acknowledgments are received, the yet-to-be-received packet segment will be sent immediately. The Fast Recovery algorithm needs to be used in conjunction with the Fast Retransmit algorithm.

::: info Fast Recovery Algorithm
- When the sender receives three consecutive duplicate acknowledgments, it halves `ssthresh`.
- Set `cwnd` to the size of `ssthresh`
:::

##### **Checksum**

TCP will also calculate a checksum for the data to ensure that the data is not lost or erroneous during transmission.

### UDP

Expand Down
16 changes: 8 additions & 8 deletions src/guide/concepts/network/2-tcp-udp.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ TCP 建立连接的过程就是同步序列号的过程,SYN (Synchronize Seque

:::

##### 序列号和确认应答信号
##### **序列号和确认应答信号**

TCP 通过序列号可以对数据包进行排序和去重,同时通过 ACK 应答机制确保数据包成功送达,保证了数据的完整性。

#### 超时重传
##### **超时重传**

TCP 通过超时重传机制可以在数据包丢失或者延迟时,重新发送数据包直到收到 ACK 应答。

Expand Down Expand Up @@ -137,7 +137,7 @@ SACK 方法 ([RFC 2018](https://www.ietf.org/rfc/rfc2883.txt)) 在 tcp 的 optio
D-SACK ([RFC 2883](https://datatracker.ietf.org/doc/html/rfc2018)) 主要解决了 ACK 丢失的问题, D-SACK 使用 SACK 第一个段作为标志位,用于标记已经 ACK 的数据包。 当接收端接收到重复的报文段时会将该报文段写入 D-SACK 的标志位,告诉发送方已接收到报文段, ACK 可能已丢失。
:::

#### 流量控制
##### **流量控制**

TCP 使用滑动窗口来控制流量,使得发送端可以根据接收端的接收能力控制发送数据的速度。

Expand All @@ -148,7 +148,7 @@ TCP 使用滑动窗口来控制流量,使得发送端可以根据接收端的
接收端在应答 `ACK` 的时候会将当前可用的窗口大小写入 tcp 头部,发送端根据窗口大小调整发送速率。
:::

#### 拥塞控制
##### **拥塞控制**

::: tip 算法
- 慢开始
Expand All @@ -173,15 +173,15 @@ TCP 使用滑动窗口来控制流量,使得发送端可以根据接收端的
- `cwnd = ssthresh`,使用慢开始与拥塞避免算法任意一个。
:::

##### 慢开始
###### **慢开始**

在慢开始阶段, `cwnd` 初始值为 `1`, 每经过一个传播轮次,`cwnd` 都会翻倍,直到达到 `ssthresh`

##### 拥塞避免
###### **拥塞避免**

`cwnd` 达到 `ssthresh` ,则会执行拥塞避免算法,此时 `cwnd` 的增长从翻倍增长变为每经过一个传播轮次就加 `1` 。若发送方检测到网络拥塞(即发送的消息没得到及时的回应),就会将 `ssthresh` 设置为发生网络拥塞时的 `cwnd` 值的一半,同时 `cwnd` 的值将会重置为 `1` ,重新执行慢开始。

##### 快重传和快恢复
###### **快重传和快恢复**

快重传在前面已介绍过,即如果连续收到三个重复的确认就会立即发送尚未接收到的报文段。快恢复算法需要配合快重传算法使用。图中的 5 号为快恢复阶段。

Expand All @@ -190,7 +190,7 @@ TCP 使用滑动窗口来控制流量,使得发送端可以根据接收端的
-`cwnd` 设置为 `ssthresh` 的大小
:::

#### 校验和
##### **校验和**

TCP 还会对数据计算校验和,确保数据在传输过程中未丢失或出现错误。

Expand Down

0 comments on commit c49f602

Please sign in to comment.