跳到主要内容

10. 与上层的接口

本章定义 SCTP 与上层协议 (Upper Layer Protocol, ULP) 之间的接口服务. 这些是抽象接口; 实际实现可能因操作系统而异.

10.1. ULP-to-SCTP

ULP 使用以下原语向 SCTP 发送命令和数据:

10.1.1. Initialize

格式: INITIALIZE([local port], [local eligible address list])

用途: 初始化 SCTP 端点.

参数:

  • local port: 本地端口号 (可选)
  • local eligible address list: 本地合格地址列表 (可选)

结果:

  • 成功: 返回本地端点句柄
  • 失败: 返回错误码

说明:

- Allocate resources for SCTP endpoint
- Bind to specified local port and addresses
- If no port specified, system auto-assigns
- If no addresses specified, use all available local addresses

10.1.2. Associate

格式: ASSOCIATE(local SCTP instance name, destination transport addr, outbound stream count)

用途: 请求与远端 SCTP 端点建立新的 association.

必需参数:

  • local SCTP instance name: 本地 SCTP 实例名
  • destination transport addr: 目的传输地址 (IP 地址和端口)
  • outbound stream count: 请求的出站流数量

可选参数:

  • destination transport addr list: 目的传输地址列表 (用于多宿主)
  • stream payload protocol identifier: 流 payload protocol identifier

结果:

  • COMMUNICATION UP: association 成功建立
  • COMMUNICATION LOST: association 建立失败

说明:

1. Generate INIT chunk
2. Start T1-init timer
3. Send INIT to destination address
4. Wait for INIT ACK
5. Send COOKIE ECHO
6. Wait for COOKIE ACK
7. Enter ESTABLISHED state

10.1.3. Send

格式: SEND(association id, buffer address, byte count [,context] [,stream id] [,life time] [,destination transport address] [,unordered flag] [,no-bundle flag] [,payload protocol-id])

用途: 将用户消息交给 SCTP 传输.

必需参数:

  • association id: Association 标识符
  • buffer address: 缓冲区地址
  • byte count: 字节数

可选参数:

  • context: 用户上下文 (用于错误报告)
  • stream id: 流标识符 (默认 0)
  • life time: 消息生存期 (毫秒)
  • destination transport address: 目的传输地址
  • unordered flag: 无序交付标志
  • no-bundle flag: 禁止捆绑标志
  • payload protocol-id: Payload protocol ID

结果:

  • 成功: 消息被接受用于传输
  • 失败: 返回错误码

说明:

- Segment user data into DATA chunks
- Assign according to stream ID and SSN
- If lifetime specified, discard on timeout
- Default to primary destination address

10.1.4. Shutdown

格式: SHUTDOWN(association id)

用途: 优雅关闭 association.

必需参数:

  • association id: Association 标识符

结果:

  • SHUTDOWN COMPLETE: 关闭成功
  • COMMUNICATION LOST: 关闭失败

说明:

1. Stop accepting new user data
2. Complete transmission of all unsent data
3. Wait for all data to be acknowledged
4. Send SHUTDOWN chunk
5. Wait for SHUTDOWN ACK
6. Send SHUTDOWN COMPLETE
7. Destroy TCB

10.1.5. Abort

格式: ABORT(association id [,Upper Layer Abort Reason])

用途: 立即终止 association.

必需参数:

  • association id: Association 标识符

可选参数:

  • Upper Layer Abort Reason: 上层中止原因

结果:

  • COMMUNICATION LOST: Association 已中止

说明:

1. Send ABORT chunk (with reason)
2. Discard all unsent and unacknowledged data
3. Destroy TCB
4. Report COMMUNICATION LOST to ULP

10.1.6. Set Primary

格式: SET PRIMARY(association id, destination transport address)

用途: 设置主目的地址.

必需参数:

  • association id: Association 标识符
  • destination transport address: 目的传输地址

结果:

  • 成功: 主地址已更新
  • 失败: 地址不在 association 的对等地址列表中

说明:

- Change current primary destination address
- Used for traffic engineering or failure recovery
- Address must be valid peer address in association

10.1.7. Receive

格式: RECEIVE(association id, buffer address, buffer size)

用途: 从 SCTP 接收数据.

必需参数:

  • association id: Association 标识符
  • buffer address: 接收缓冲区地址
  • buffer size: 缓冲区大小

返回值:

  • byte count: 接收的字节数
  • stream id: 流标识符
  • stream sequence number: 流序列号
  • partial flag: 部分交付标志
  • delivery number: 交付编号
  • transport address: 源传输地址

说明:

- Extract user message from receive queue
- May block if no data available (implementation-dependent)
- Deliver in order by stream ID and SSN (unless unordered)

10.1.8. Status

格式: STATUS(association id)

用途: 查询当前 association 状态.

必需参数:

  • association id: Association 标识符

返回值:

  • association connection state: Association 连接状态
  • destination transport address list: 目的传输地址列表
  • destination transport address reachability states: 目的传输地址可达性状态
  • current receiver window size: 当前接收方窗口大小
  • current congestion window sizes: 当前拥塞窗口大小
  • number of outbound streams: 出站流数量
  • number of inbound streams: 入站流数量
  • primary path: 主路径

10.1.9. Change Heartbeat

格式: CHANGE HEARTBEAT(association id, destination transport address, new state [,interval])

用途: 为特定目的地址启用或禁用 heartbeat.

必需参数:

  • association id: Association 标识符
  • destination transport address: 目的传输地址
  • new state: 新状态 (enable/disable)

可选参数:

  • interval: Heartbeat 间隔 (毫秒)

10.1.10. Request Heartbeat

格式: REQUEST HEARTBEAT(association id, destination transport address)

用途: 请求向特定目的地址发送 heartbeat.

必需参数:

  • association id: Association 标识符
  • destination transport address: 目的传输地址

用途说明:

  • 测试特定路径的可达性
  • 更新路径 RTT 估计

10.1.11. Get SRTT Report

格式: GET SRTT REPORT(association id, destination transport address)

用途: 获取特定目的地址的 SRTT (Smoothed Round-Trip Time).

返回值:

  • srtt: 当前 SRTT 值
  • last update time: 上次更新时间

10.1.12. Set Failure Threshold

格式: SET FAILURE THRESHOLD(association id, destination transport address, failure threshold)

用途: 设置路径故障阈值.

参数:

  • association id: Association 标识符
  • destination transport address: 目的传输地址
  • failure threshold: 故障阈值

10.1.13. Set Protocol Parameters

格式: SET PROTOCOL PARAMETERS(association id, [,destination transport address,] protocol parameter list)

用途: 设置 SCTP 协议参数.

可设置参数:

  • RTO.Initial: 初始重传超时
  • RTO.Min: 最小重传超时
  • RTO.Max: 最大重传超时
  • RTO.Alpha: SRTT 估计器的 Alpha 值
  • RTO.Beta: SRTT 估计器的 Beta 值
  • Max.Burst: 最大突发大小
  • Association.Max.Retrans: Association 最大重传次数
  • Path.Max.Retrans: 路径最大重传次数
  • Valid.Cookie.Life: 有效 cookie 生存期
  • SACK.Delay: SACK 延迟

10.1.14. Destroy SCTP Instance

格式: DESTROY(local SCTP instance name)

用途: 销毁 SCTP 实例.

说明:

1. Abort all active associations
2. Release all resources
3. Unbind ports and addresses

10.2. SCTP-to-ULP

SCTP 使用以下原语向 ULP 报告事件:

10.2.1. COMMUNICATION UP

格式: COMMUNICATION UP(association id, destination transport address list, outbound stream count, inbound stream count)

触发条件:

  • 新 association 成功建立 (ASSOCIATE 完成)
  • 作为被动方接受新 association (收到 INIT 并完成四次握手)

参数:

  • association id: 新建立的 Association 标识符
  • destination transport address list: 对等地址列表
  • outbound stream count: 协商后的出站流数量
  • inbound stream count: 协商后的入站流数量

10.2.2. DATA ARRIVE

格式: DATA ARRIVE(association id, stream id, stream sequence number, flags, payload protocol identifier, user data)

触发条件:

  • 从对等方收到用户数据

参数:

  • association id: Association 标识符
  • stream id: 流标识符
  • stream sequence number: 流序列号
  • flags: 标志 (例如 unordered flag)
  • payload protocol identifier: Payload protocol identifier
  • user data: 用户数据

说明:

- Notify ULP that new data is available
- ULP should use RECEIVE primitive to retrieve data
- Data delivered in order by stream ID and SSN (unless unordered)

10.2.3. COMMUNICATION LOST

格式: COMMUNICATION LOST(association id, status)

触发条件:

  • Association 失败或被中止
  • 检测到对等端点故障
  • 收到 ABORT chunk
  • SHUTDOWN 失败

参数:

  • association id: Association 标识符
  • status: 失败原因
    • Association exceeded maximum retransmissions: 超过最大重传次数
    • Peer sent ABORT: 对等方发送了 ABORT
    • Peer unexpectedly closed: 对等方意外关闭
    • Resources unavailable: 资源不可用

10.2.4. COMMUNICATION ERROR

格式: COMMUNICATION ERROR(association id, error info)

触发条件:

  • 检测到可恢复的通信错误
  • 收到 ERROR chunk
  • 路径故障 (但 association 仍 active)

参数:

  • association id: Association 标识符
  • error info: 错误信息

10.2.5. RESTART

格式: RESTART(association id, peer's new tag)

触发条件:

  • 检测到对等端点重启

参数:

  • association id: Association 标识符
  • peer's new tag: 对等方的新 tag

说明:

- Notify ULP that peer has restarted
- ULP should decide how to handle (re-establish or terminate)
- May need to resynchronize application layer state

10.2.6. SHUTDOWN COMPLETE

格式: SHUTDOWN COMPLETE(association id)

触发条件:

  • 优雅关闭过程成功完成

参数:

  • association id: Association 标识符

说明:

- Association successfully closed
- All data reliably delivered
- TCB destroyed

10.2.7. SEND FAILURE

格式: SEND FAILURE(association id, data retrieval id, cause code, context)

触发条件:

  • 数据因某种原因传输失败
  • 达到最大重传次数
  • 消息生存期到期

参数:

  • association id: Association 标识符
  • data retrieval id: 数据取回标识符
  • cause code: 失败原因码
  • context: 用户上下文 (来自 SEND 原语)

原因码:

  • Message expired: 消息已过期
  • Association aborted: Association 已中止
  • Send failed: 发送失败

10.2.8. NETWORK STATUS CHANGE

格式: NETWORK STATUS CHANGE(association id, destination transport address, new status)

触发条件:

  • 目的传输地址状态发生变化
  • 路径变为 active 或 inactive

参数:

  • association id: Association 标识符
  • destination transport address: 目的传输地址
  • new status: 新状态
    • Active: 路径现在可用
    • Inactive: 路径现在不可用
    • Unconfirmed: 路径尚未确认

总结

SCTP 的 ULP 接口提供:

  1. 完整的 Association 管理: 初始化, 建立, 关闭, 中止
  2. 灵活的数据传输: 支持多流, 有序/无序, 生存期
  3. 路径管理: Heartbeat, 故障检测, 主路径选择
  4. 状态监测: 查询 association 状态, 网络状态变化通知
  5. 参数配置: 运行时调整协议参数

最佳实践:

  • 正确处理所有 SCTP-to-ULP 通知
  • 使用 stream ID 进行逻辑数据隔离
  • 设置合理的消息生存期
  • 监测网络状态变化并作出响应
  • 优先使用优雅关闭而不是强制中止