跳到主要内容

9. Association 终止

SCTP 提供两种 association 终止机制: 优雅关闭和强制中止.

9.1. 中止 Association

ABORT chunk 用于立即终止 association. 这是一种异常终止, 不保证数据交付.

9.1.1. 发送 ABORT

端点可以在以下情况下发送 ABORT chunk:

  • 检测到协议违规
  • 收到不可恢复错误
  • 应用层请求立即终止
  • 资源耗尽

ABORT Chunk 格式:

ABORT chunk contains:
- Chunk Type = 6
- T bit: Indicates Verification Tag usage
- Error Causes (optional): Describes abort reason

9.1.2. 接收 ABORT

收到 ABORT chunk 时, 端点 必须:

1. Stop sending any new data to the association
2. Discard all unsent and unacknowledged data
3. Report association abort to upper layer
4. Destroy the TCB
5. Send no response

重要: ABORT 是单向的; 接收方不得发送任何响应.

9.1.3. ABORT 原因

常见 ABORT 原因包括:

Cause CodeNameDescription
1Invalid Stream Identifier无效流标识符
2Missing Mandatory Parameter缺少强制参数
3Stale Cookie Error过期 cookie
4Out of Resource资源不足
5Unresolvable Address无法解析的地址
6Unrecognized Chunk Type无法识别的 chunk 类型
7Invalid Mandatory Parameter无效强制参数
8Unrecognized Parameters无法识别的参数
9No User Data无用户数据
10Cookie Received While Shutting Down关闭期间收到 cookie
11Restart with New Addresses使用新地址重启
12User Initiated Abort用户发起的中止
13Protocol Violation协议违规

9.1.4. T Bit 使用

T bit 控制 ABORT chunk 中的 Verification Tag 选择:

T=0:

- Use normal Verification Tag
- Used for ABORT in normal association

T=1:

- Use Verification Tag from received packet
- Used for response to "Out of the Blue" packets
- Or when unsure of correct Tag

9.2. 关闭 Association

SHUTDOWN 是正常, 有序的 association 终止过程, 确保所有数据都被可靠交付.

9.2.1. 关闭过程概览

正常关闭涉及三个 chunk 的交换:

Endpoint A                                     Endpoint Z

(No more data to send)
--------[SHUTDOWN]--------->
(Transmit remaining data)
<------[SHUTDOWN ACK]-------
-----[SHUTDOWN COMPLETE]---->

Both sides destroy TCB

9.2.2. 发起 SHUTDOWN

当 ULP 请求关闭 association 时:

发送方行为:

1. Stop accepting new user data
2. Complete transmission of all unsent data
3. Wait for all unacknowledged data to be acknowledged
4. Send SHUTDOWN chunk with Cumulative TSN Ack
5. Start T2-shutdown timer

SHUTDOWN Chunk 格式:

0                   1                   2                   3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Type = 7 | Flags | Length = 8 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Cumulative TSN Ack |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

9.2.3. 接收 SHUTDOWN

收到 SHUTDOWN chunk 时, 端点 必须:

1. Stop accepting new user data
2. Verify all data up to Cumulative TSN Ack has been sent
3. If unsent data exists, continue transmission
4. Once all data is acknowledged, send SHUTDOWN ACK
5. Start T2-shutdown timer

状态转换:

ESTABLISHED -> SHUTDOWN-RECEIVED

9.2.4. 发送 SHUTDOWN ACK

SHUTDOWN ACK Chunk 格式:

0                   1                   2                   3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Type = 8 | Flags | Length = 4 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

发送时机:

  • 所有已接收数据已交付给 ULP
  • 所有本地数据已被对等方确认

9.2.5. 接收 SHUTDOWN ACK

收到 SHUTDOWN ACK 时, 端点 必须:

1. Send SHUTDOWN COMPLETE chunk
2. Stop T2-shutdown timer
3. Report association closure to ULP
4. Destroy TCB

状态转换:

SHUTDOWN-SENT -> CLOSED

9.2.6. SHUTDOWN COMPLETE

SHUTDOWN COMPLETE Chunk 格式:

0                   1                   2                   3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Type = 14 |Reserved |T| Length = 4 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

T Bit 使用:

  • T=0: 正常情况, 使用本地 Tag
  • T=1: 响应意外 SHUTDOWN ACK 时使用

接收 SHUTDOWN COMPLETE:

1. Stop T2-shutdown timer
2. Report association closure to ULP
3. Destroy TCB

状态转换:

SHUTDOWN-ACK-SENT -> CLOSED

9.2.7. T2-shutdown 定时器

用途: 确保 SHUTDOWN 过程不会无限期等待.

超时处理:

If in SHUTDOWN-SENT state:
- Retransmit SHUTDOWN chunk
- Double RTO
- Restart T2-shutdown timer

If in SHUTDOWN-ACK-SENT state:
- Retransmit SHUTDOWN ACK chunk
- Double RTO
- Restart T2-shutdown timer

If retransmissions exceed Association.Max.Retrans:
- Report error to ULP
- Destroy TCB

9.2.8. 关闭期间的数据处理

在 SHUTDOWN-PENDING 状态:

  • 继续正常发送和接收数据
  • 等待所有未确认数据被确认

在 SHUTDOWN-SENT 状态:

  • 不接受新的用户数据
  • 可以接收对等方的数据
  • 发送 SACK 以确认收到的数据

在 SHUTDOWN-RECEIVED 状态:

  • 不接受新的用户数据
  • 继续发送未发送数据
  • 接收并确认对等方的数据

在 SHUTDOWN-ACK-SENT 状态:

  • 不接受新的用户数据
  • 不发送新数据
  • 响应重复 SHUTDOWN 时重传 SHUTDOWN ACK

9.2.9. SHUTDOWN 与 ABORT 比较

FeatureSHUTDOWNABORT
数据保证所有已发送数据被确认无数据保证
终止速度较慢 (需要握手)立即
资源清理优雅清理立即清理
使用场景正常关闭错误或紧急情况
是否需要响应需要对等方响应单向, 无响应

总结

SCTP 提供灵活的 association 终止机制:

  1. SHUTDOWN: 用于正常关闭, 保证数据完整性
  2. ABORT: 用于异常情况, 立即终止

最佳实践:

  • 正常关闭优先使用 SHUTDOWN
  • 仅在错误或资源问题时使用 ABORT
  • 正确处理 T2-shutdown 定时器超时
  • 实现适当的重传机制