计算机网络第6章--传输层
生活以质朴为上,心以丰饶为高。
——《凉宫春日的忧郁》
Transport service
- It provides reliable data transport end to end 提供端到端可靠传输。
- 只用位于网络边缘部分的主机的协议栈才有传输层
- 计算机中的通信是进程到进程的
- 端口号:两个计算机中的进程要相互通信,不仅必须要知道对方的IP地址(找到计算机),还需要知道对方的端口号(找到进程),
Socket(套接字)
套接字是应用层和传输层的接口,也是应用程序和网络之间的API。
- 应用程序需要显式地创建、使用和释放套接字
- 采用客户-服务器模式:Client-Server
- 客户端总是主动发起通信的一方,服务器始终在等待客户的服务请求
- 通过Scoket可以发起两种传输服务:
- unreliable:UDP
- reliable:TCP
Elements of transport protocols
Addressing
NSAP:network service access point 网络层服务接入点(ip地址)
TSAP:transport service access point 传输层服务接入点(端口号)
Connection Establishment 连接建立
重复连接
Problem
Network can lose(丢失), store(存储), and duplicate(复制) packets
发生图片中情况时,连接可能会被重复建立
Solution of old and duplicated initial seq
Each connection starts numbering its segments with a different initial sequence number
每个连接都从一个不同的初始序列号开始编它的分段。并且保证在一个T的时间内不被重用。
- T为一个数据报的生命周期,即一次请求的数据包最多可在网络中被延迟的时间为T。
- 在T时间内不使用重复序号,即可保证2不会收到两个序号一样的连接请求。
Connection Release
Two Styles
- Asymmetric Release 非对称释放
- when one party hangs up(挂断), the connection is broken
- Asymmetric release is abrupt and may result in data loss
- Symmetric Release 对称释放
- Treats connection as two separate unidirectional(单向的) connections
- Require each one to be released separately
Asymmetric Release:
- 尽管Host1 还有数据发送,但是2已经hangs up,所以最后的数据包丢失
Means
The transport entity(实体) removes the information about the connection from its table of currently open conncetions and signals(发出信号) the connection’s owner
Half-open Connections
半开连接:
初始的断开连接请求DR以及后续的N次重传全部丢失了,发送方会放弃并释放连接。然而,另一方对这些断开尝试一无所知,连接的一端已经关闭,而另一端仍然认为连接是打开的,这就形成了半开连接。
终止半开连接:
- If no TPDUs have arrived for a certain number of sconds, the connection is then automatically disconnected.
- Dummy TPDU: 只是为了防止另一端断开连接,发送一些探测报文,以确认另一端仍然存活。如果一段时间没有相应,则认为连接中断,进行清理。
Error Control
- Error-detectiong code(e.g. CRC or checksum)
- ARQ(Automatic Repeat reqQuest)
- Sliding window protocol
- Stop-and-wait
Source Buffering & Destination Buffering
Variable sized windows:Dynamic buffer management
- The sender and receiver need to dyanmically adjust their buffer allocations(分配)
- The receiver piggybacks(捎带) both acknowledgements and buffer allocations(缓冲区分配) onto the reverse traffic(反向流量)
Congestion Control
Responsibility of the network and transport layers:
- network:负责检测拥塞的发生
- Transport:负责减慢给网络层传递包的速度
拥塞控制最终由传输层来做
Goal of congestion control Algorithm:
- Avoid congestion 避免拥塞
- Efficiency 效率
- Fairness 公平
- Convergence 收敛
Efficiency and Power
The load with the highest power represents an efficient load for the transport entity to place on the network.
$$
Power = \frac{load}{delay}
$$
Max -Min Fairness 最大最小公平
- 原则:提高所有用户的最低带宽分配
步骤:
- 从所有流速率为 0 开始
- 增加所有流的速率,直到网络中出现一个新的瓶颈
- 保持已经受瓶颈限制的流的速率不变
- 对所有剩余的流重复步骤 2
首先给每个流分配带宽为1/3。
但是流A还可以继续增长,让A增长到2/3。若A再继续增大就必须减小B,所以A为2/3。
最终我们得到最大最小公平分配:A:2/3,BCD均为1/3
Convergence 收敛性
Converge quickly to a fair and efficient allocation of bandwidth
快速收敛到一个公平且高效的带宽分配
Flow control vs Congestion Control
AIMD Control Law
AIMD(Additive Increase Multiplicative Decrease) 加性增加,乘性减少。缓增急减。
- 加性增加:在网络没有拥塞时,缓慢的增加发送速率,以充分利用可用带宽。
- 乘性减少:当检测到拥塞时,迅速的减小发送速率,以免网络过载。
AIMD is Used by TCP
TCP并不直接调整发送速率,而是在实践中通过调整滑动窗口的大小来控制发送速率。
如果窗口大小为W,往返时间为RTT,那么等效的发送速率大约是W/RTT。
注意:此处的窗口大小W单位为字节,即一个窗口中的字节数。
The Transport Layer in Internet :UDP/TCP
- TCP(Transmission Control Protocol)
- UDP(User Datagram Protocol)
UDP(User Datagram Protocol)
- Connectionless
- No flow control, No congestion, No retransmission upon receipt(收到) of bad segment(片段)
- Provide an interface to IP protocol with the added feature of demultiplexing(多路分解) multiple process using the ports.
- Broadcast & Multicast 支持广播或多播
- User datagram boundary:面向报文
UDP Header
Checksum 校验和可选:
- Stored as 0 if not computed(a true computed 0 is stored as all 1)
UDP Applications
- DNS
- DHCP
- RIP
TCP(Transmission Control Protocol)
The TCP Service Model
Socket:
- socket address:IP address and Port
- Each connection is identified by a pair of sockets
TCP connection
- Full-duplex
- TCP does not support multicasting or broadcasting
- Byte-stream,not mussage stream
IT常识科普:
- 字节流:
- 基本单位:字节
- 组织方式:数据被视为一个连续的、无结构的字节序列。发送方只是将一个个字节放入流中,接收方也只是从流中读取一个个字节。
- 边界:字节流本身不包含任何关于数据边界的信息。接收方需要知道数据的结构和长度,才能正确的将字节流解析成有意义的信息。
- 消息流:
- 基本单位:消息
- 组织方式:数据被分割为一个个独立的消息单元。每个消息通常包含消息头和消息体。
- 边界:在消息流中,消息之间有明确的边界,接收方可以很容易的区分出不同的消息。
TCP:Byte stream
虽然应用程序和TCP的交互是一次一个数据块,但是TCP把应用程序交下来的数据仅仅看成一连串无结构的字节流,接收方应用程序收到的字节流必须和发送方应用程序发出的字节流完全一样。
这样TCP传输层只需负责将数据正确的送发到目的地即可,而应用层自己负责从字节流中分割信息。
TCP Protocol
TCP Entity:
- a port plus its host’s IP address forms a 48-bit unique end point/entity
- IP+port 识别一个连接
32-bits sequence
- 以byte计数,而非报文
- 使用序列号(Seuqence numbers)和确认号(acknowledgements number)来保证可靠传输
- TCP发送方可以将多次write操作的数据积累起来放入一个TCP报文段中发送,也可以将一次write 操作的数据分为多个TCP报文发送。
- 重传报文段可能包含与原始传输不同的字节范围。
MSS(Maximum Segment Size) 最大段长
- TCP数据段的最大长度
- MTU:数据链路层所能接收的最大帧大小。
- MSS:TCP传输层数据的最大大小。
- 最小 TCP MSS 为536字节
- TCP头最少20字节
- IP包头最少20字节
TCP Segment Header
Source port:该报文段中包含的第一个第一个数据字节的序号。
Acknowledge number:发送这个报文段的源希望收到的下一个字节号。
TCP header length:length of header, unit by 4 Bytes(单位是4字节)即实际长度为:$length * 4bytes$
Window Size:size of receiver’s window, number of bytes receiver can accept。
TCP协议中,由于是字节流,窗口大小以字节为单位
SYN:for SYNchronize;synchronize sequence number of the new-connection handshake. 连接请求标志,携带初始连接序号。
- 这意味着当发送方想要发起一个新的 TCP 连接时,会将 SYN 标志位置为 1,并且会在序列号字段中携带初始序列号 (ISN)。
ACK:the Acknowledgemant field is valid。 标识Acknowledgement number字段有效
- 初次握手时,没有要确认的内容,ACK为0。
FIN:for FINsh; terminate the connection. 释放连接请求标志。
- 当发送方完成数据传输,想要关闭连接时,会将 FIN 标志位置为 1。
TCP checksum
- It checksums the header(头), the data(数据) ,and the concepual pseudoheader(概念性伪头)
- Pseudoheader: helps detect misdelivered packets,伪头也包含在checksum字段中
- Checksum is mandatory(强制的).
TCP Connection Establishment建立连接
Client:
- 客户端只需
CONNECT
原语,指定了要连接的服务器的IP地址和端口号 - 客户端发送一个TCP报文段,其中:
- SYN 标志位为1,表示发起连接
- ACK标志位为0,因为是第一个发送,没有要确认的内容
- 同时发送自己的初始序列号
- 客户端等待服务器端响应。
服务器端:
- 当服务器接收到客户端发送的SYN报文段时,会检查是否有进程在该端口上执行了
LISTEN
操作。 - 如果有进程正在监听该端口:
- 该进程会接收来自客户端的TCP报文
- 服务器会建立连接并发送一个ACK(通常SYN和ACK都为1),作为响应来确认客户端的SYN,并同时发送自己的初始序列号。4
- 没有进程在监听:
- 服务器发送一个RST标志位为1的TCP报文,拒绝连接
TCP Connection Release
TCP连接释放时,通常需要连接的两端分别发送一次FIN报文。
这是因为TCP连接是双全工的,意味着数据可以在两个方向上同时传输。因此,当一方完成数据想要关闭连接时,它只是表示自己不再发送新的数据,但仍可能接收来自对方的数据。
User timer to avoid the two-army pronlem: 使用定时器来避免双军问题。
- If a response to a FIN is not forthcoming(到来的) within two maximum packet lifetimes, the sender of the FIN releases the connection.
- 如果在两个最大报文生存时间内没有收到对于FIN帧的确认,则发送FIN方直接释放连接。
two-army 问题:
两位将军(A和B)在山谷两侧指挥军队,要同时进攻敌军才能获胜。他们只能通过信使通信,但信使可能会被敌人截获。他们如何确保彼此在同一时间发起攻击。
这个问题说明了在不可靠通信系统中不可能实现完全的确认。
释放连接的三种情况:
Window managemant in TCP
When the Window is 0: (接收窗口大小为0)
- Seander may not normally send segment(发送方通常不会再发送数据段)
- Urgent(紧急的) data may be sent
- 窗口探测:发送端可能会重新发送一个1字节的报文段,目的是探测接收端ack和窗口大小,防止死锁。
两种增大线路利用路的方法
- Nagle algorithm
- 核心思想:发送方延迟发送,积累更多数据一次发送。
- Sily Window Syndrome
- 核心思想:接收方延迟窗口更新,留出更多的空间后在发送窗口更新
Retransmission Timer 重传计时器
Used a dynamic algorithm that adjusts the timeout interval(间隔), based on continuous measurements of network performance.
Jacobson’s Algorithm
Enhanced Algorithm
Other TCP Timers
Persistence timer 持续定时器
- It is designed to prevent the 0-window deadlock
- When timer gose off, the sender transmits a probe(探测) to the receiver. The response to the probe gives the window size.
Keepalive timer 保活定时器
- It is designed to prevent the half-open connection
- When a connection has been idle for a long time, the keepalive timer may go off to cause one side to check whether the other side is still there.
The timer used in the TIMED WAIT state
- Avoid the two-army problem
- It runs for twice the maximum packet lifetime to make sure that when a connection is closed, all packets created by it hace died off.
TCP Congestion Control
- RTT: 对往返时间的估计值。
Maintaining a congestion window。(rwin/cwnd)
Maintaining a flow control receiver window (rwin/rwnd)
TCP Congestion Window
cwdn:
- a variable held by the TCP source for each connection.
- Denotes(表示) how many bytes network is able to absorb.
- $吞吐率: rate = \frac{cwnd}{RTT} \text{ Bytes/sec}$
- 拥塞窗口表示网络能够接收的最大速率
- 接收窗口表示自己能够接收的最大速率
- 最终的发送窗口: min(cwnd,rwnd)
- 拥塞窗口和接收窗口种的较小值。
ACK clock
- ACK的返回速率可以反映最慢链路上的速率。
- 如果TCP按照ACK返回的速率注入新的数据包,那么发送速率就会与慢速链路相匹配,这样数据包就不会在路径上的路由器处排队造成拥塞。
TCP Congestion Control
Slow Strat 慢启动阶段
- When a connection is established, the cwnd is initiallized to a value of 1 segment.
- The sender sends the initial window, packets will take a RTT(往返时间) to be acknowledge.
- For each ACK, adds one segment to cwnd.
- 1->2->4->8(exponential growth 指数增长)(一个RTT时间内)
- 使用ACK clock使发送速率与链路速率想匹配。
Slow Start Threshold
TCP connection maintains a threshold(阈值) to keep slow start under control
- 超过慢启动阈值,进入拥塞避免(加法递增)阶段
- 检测到丢包,慢启动阈值减半
Congestion Avoidance 拥塞避免:Additive Increase 加法递增
- the cwdn is increased by one segment every round-trip time。一个RTT时间
cwnd
只增加1。 cwnd
增加(MSS × MSS) / cwnd
Fast Retransmission 快速重传
让发送方更快地识别并重传丢失报文段的方法,而无需等待重传定时器超时。
- As packets beyond the lost packet arrive at the receiver, they tigger ACKs with the same ACK number.
- TCP assumes that 3 duplicate(重复的) ACKs imply that a packet has been lost.
- 在重传计时器超时之前,发送方就会重传被认为丢弃的报文段。
原理解释:
接收方收到乱序的报文段时,仍然会发送 ACK,但这个 ACK 的确认号指向它期望收到的下一个报文段(也就是丢失的那个)。连续收到三个相同的 ACK 表明在接收方期望的那个报文段之后,有其他的报文段已经到达了,这强烈暗示了期望的那个报文段丢失了。
Fast Recovery 快速恢复
快速恢复旨在避免在发生丢包后立即进入慢启动,从而更平滑地恢复发送速率。
- 在快速重传发生时,将慢启动阈值 (ssthresh) 设置为当前拥塞窗口 (cwnd) 的一半。
- 将拥塞窗口 (cwnd) 设置为新的阈值,并开始线性增加(加性增加模式)。
丢包事件总结 重点
超时和收到3个重复的ACK,它们反应出来的网络拥塞程度不同:
超时:说明网络交付能力很差
- 设置慢启动门限 = cwnd/2
- cwnd = 1MSS
- 使用慢启动增大cwnd至门限
- 使用AIMD调节cwnd
收到3个重复的ACK:说明网络仍有一定的交付能力
- 将cwnd降至一半
- 使用AIMD调节cwnd
TCP congestion control summary 重点
拥塞检测:
- 隐式拥塞通知,TCP发送端根据丢包自行判断
Slow Start:
- 启动速率很低,TCP建立连接时,拥塞窗口 = 1MSS
- 按指数级增加发送速率(每一轮发送结束,拥塞窗口加倍)直至拥塞窗口达到阈值或发现数据丢失。
Congestion Avoidance 避免拥塞(Additive increase)
- 拥塞窗口达到阈值时,降低发送速度的增长,按线性增长。(每一轮发送结束,拥塞窗口增加一个MSS)
- 出现定时器超时,拥塞窗口将为一个MSS,开始新的慢启动(阈值降为超时时的一半)
- 收到三个重复的ACK,拥塞窗口将为当前值的一半(MD),开始新的拥塞避免(AI)
TCP option:SACK
SACK(Selective ACKnowledgements)
- When TCP connection is set up, both the sender and receiver send the SACK permitted TCP option to signal that they understand SACK.
- SACK 选项可以列出多达 3 个已成功接收到的字节范围。这使得接收方能够明确地告知发送方哪些数据已经到达,即使中间有报文丢失。
TCP option:ECN
- 当 TCP 连接建立时,发送方和接收方通过设置 TCP 头部中的 ECE (ECN-Echo) 和 CWR (Congestion Window Reduced) 比特来表明它们支持 ECN。
- 如果接收到的任何 IP 包携带 ECN 拥塞信号(由支持 ECN 的路由器设置),那么 TCP 接收方会设置 TCP 头部中的 ECE (ECN-Echo) 标志来通知发送方网络中发生了拥塞。
- 发送方在收到带有 ECE 标志的 ACK 后,会设置 TCP 头部中的 CWR (Congestion Window Reduced) 标志来告知接收方它已经响应了拥塞通知。
- 发送方对 ECN 通知的反应与通过重复 ACK 检测到丢包时的反应相同,即进行乘性减小(将拥塞窗口减半)。
ECN 是一种允许路由器在不丢弃报文的情况下通知通信双方网络拥塞的机制。当路由器检测到拥塞时,它会在 IP 头部中设置一个 ECN 标记。接收方收到带有这个标记的报文后,通过设置 TCP 头部中的 ECE 标志通知发送方。发送方收到带有 ECE 标志的 ACK 后,会像发生丢包一样降低其发送速率。