---
title: 湖科大教书匠考研计算机网络强化复习
author: 萑澈
pubDatetime: 2026-11-09T08:29:46Z
featured: false
draft: false
printable: true
copyable: true
tags:
  - 计算机网络
  - cs408
  - 考研
  - 湖科大教书匠
description: 湖科大教书匠《408考研计算机网络强化复习》总结。视频：https://www.bilibili.com/video/BV1XHs3zPEuW/。覆盖编码调制、差错检测、可靠传输、子网划分、路由、TCP 拥塞控制与计时器，含 49 张 SVG
---



## 第1章 课程介绍与复习策略

### 1.1 强化复习的内容组织与刷题方法

强化复习指导书共<strong>六章</strong>：第一章为计算机网络概述（性能指标、体系结构等后续各章都会用到的公共知识点），其后五章按五层原理体系结构<strong>自下而上</strong>展开——物理层、数据链路层、网络层、传输层、应用层各成一章。内容在基础教材之上<strong>删去大纲外的部分、补上大纲规定但历年从未考察的部分</strong>，做到大纲全覆盖，并按应试视角把知识点串成线。

配套习题册独立成册，每章四种题型：

| 题型 | 作用 | 建议 |
|---|---|---|
| 填空题 | 做完即成为背诵手册 | 时间紧张可不做，改听背诵手册 15–20 分钟/天 |
| 单项选择题 | 由易到难，一般只考 1–3 个知识点 | 出现多个“似乎都对”时选<strong>最符合题意</strong>的，不钻牛角尖 |
| 综合应用题 | 一道题含 5–8 小题，综合多个知识点 | 训练知识迁移能力 |
| 历年真题 | 重中之重 | 至少做<strong>三遍</strong>，体会命题人换了什么花样 |

> <strong>★考点</strong>：真题要反复做，目的是形成“拿到题立刻意识到命题人意图”的条件反射；近年真题尤其要看是否有风格变化。

复习节奏建议：以微课为主者，指导书快速浏览；以看书为主者，只在个别卡点看微课。每天必须给计算机网络留时间以保持状态；12 月初起每天快速回看已做过的题，做到举一反三。

<figure class="cpd-figure not-content" id="cnq-w1-fig-1">
  <svg viewBox="0 0 700 276" role="img" aria-label="计算机网络强化六章体系结构与四阶刷题推进路线图">
    <defs>
      <marker id="cnq-w1-arr1" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="356" height="244" rx="8"/>
    <rect class="fig-box-subtle" x="388" y="16" width="296" height="244" rx="8"/>
    <line class="fig-line-accent" x1="348" y1="210" x2="348" y2="54" stroke-width="1.8" marker-end="url(#cnq-w1-arr1)"/>
    <line class="fig-divider" x1="32" y1="218" x2="356" y2="218" stroke-dasharray="4 3"/>
    <line class="fig-line" x1="536" y1="88" x2="536" y2="100" stroke-width="1.5" marker-end="url(#cnq-w1-arr1)"/>
    <line class="fig-line" x1="536" y1="140" x2="536" y2="152" stroke-width="1.5" marker-end="url(#cnq-w1-arr1)"/>
    <line class="fig-line" x1="536" y1="192" x2="536" y2="204" stroke-width="1.5" marker-end="url(#cnq-w1-arr1)"/>
    <rect class="fig-box-purple" x="30" y="48" width="300" height="28" rx="5"/>
    <rect class="fig-box-blue" x="30" y="81" width="300" height="28" rx="5"/>
    <rect class="fig-box-accent" x="30" y="114" width="300" height="28" rx="5"/>
    <rect class="fig-box-green" x="30" y="147" width="300" height="28" rx="5"/>
    <rect class="fig-box-orange" x="30" y="180" width="300" height="28" rx="5"/>
    <rect class="fig-box" x="30" y="224" width="326" height="26" rx="5"/>
    <rect class="fig-box" x="404" y="48" width="264" height="40" rx="5"/>
    <rect class="fig-box-blue" x="404" y="100" width="264" height="40" rx="5"/>
    <rect class="fig-box-orange" x="404" y="152" width="264" height="40" rx="5"/>
    <rect class="fig-box-red" x="404" y="204" width="264" height="46" rx="5"/>
    <rect class="fig-box" x="338" y="92" width="20" height="64" rx="3"/>
    <text class="fig-title" x="194" y="36" font-size="13" text-anchor="middle">五层原理体系结构（第2–6章自下而上）</text>
    <text class="fig-purple" x="42" y="66" font-size="12">第6章 应用层 · 报文 (HTTP / DNS / FTP)</text>
    <text class="fig-blue" x="42" y="99" font-size="12">第5章 传输层 · 报文段 (TCP 可靠传输 / UDP)</text>
    <text class="fig-accent" x="42" y="132" font-size="12">第4章 网络层 · IP 数据报 / 子网与路由 ★</text>
    <text class="fig-green" x="42" y="165" font-size="12">第3章 数据链路层 · 帧 (差错控制 / MAC)</text>
    <text class="fig-orange" x="42" y="198" font-size="12">第2章 物理层 · 比特流编码调制与极限容量</text>
    <text class="fig-label" x="193" y="241" font-size="11.5" text-anchor="middle">第1章 公共基石：性能指标与分层参考模型</text>
    <text class="fig-accent" x="348" y="124" font-size="10.5" text-anchor="middle" writing-mode="tb">自下而上</text>
    <text class="fig-title" x="536" y="36" font-size="13" text-anchor="middle">配套习题四阶闭环刷题路线</text>
    <text class="fig-label" x="416" y="65" font-size="12">① 填空题：串联概念作背诵手册</text>
    <text class="fig-muted" x="416" y="81" font-size="10.5">时间紧可改听背诵音频 15–20 min/天</text>
    <text class="fig-blue" x="416" y="117" font-size="12">② 单项选择题：单题聚焦 1–3 个考点</text>
    <text class="fig-muted" x="416" y="133" font-size="10.5">多选近似时抓“最符合题意”，不钻牛角尖</text>
    <text class="fig-orange" x="416" y="169" font-size="12">③ 综合应用题：5–8 小题跨层综合</text>
    <text class="fig-muted" x="416" y="185" font-size="10.5">训练协议协同分析与大题知识迁移能力</text>
    <text class="fig-red" x="416" y="222" font-size="12">④ 历年真题：至少精做三遍 ★</text>
    <text class="fig-muted" x="416" y="239" font-size="10.5">形成命题条件反射，12 月起每日快翻复盘</text>
  </svg>
  <figcaption>图 1-1：强化复习六章五层结构与四类题型进阶闭环（网络层、数据链路层与传输层为综合大题核心）</figcaption>
</figure>

## 第2章 物理层：编码、调制与信道极限容量

### 2.1 编码与调制的基本概念

物理层在协议控制下<strong>逻辑上</strong>传输比特流，为数据链路层提供透明传输比特流的服务；而实际在传输媒体上跑的是经过编码或调制后的<strong>信号</strong>。物理层的“协议”在工程上就体现为具体的编码或调制方法。

| 概念 | 输入 → 输出 | 典型例子 |
|---|---|---|
| 基带信号 | 计算机输出的文字/图像/音频/视频数字信号 | — |
| 基带调制（<strong>编码</strong>） | 数字信号 → 另一种数字信号，与信道特性相适应 | 曼彻斯特编码 |
| 带通调制（<strong>调制</strong>） | 数字基带信号搬移到较高频段并转为模拟信号 | ASK / FSK / PSK |

<strong>码元</strong>：代表不同离散数值的基本波形。时间轴上每两条虚线之间的一段基本波形就是一个码元，这段时长称为<strong>码元时间（码元期间）</strong>，一个码元要么表示 1、要么表示 0。

> <strong>注意</strong>：编码与调制都由<strong>网卡（网络适配器）的物理层</strong>实现；看到“网卡”应同时联想到它横跨<strong>物理层与数据链路层</strong>。

<figure class="cpd-figure not-content" id="cnq-w1-fig-2">
  <svg viewBox="0 0 700 266" role="img" aria-label="数字数据通信系统中的基带编码与带通调制对比及网卡跨层定位">
    <defs>
      <marker id="cnq-w1-arr2" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="232" height="186" rx="8"/>
    <rect class="fig-box" x="284" y="16" width="400" height="86" rx="8"/>
    <rect class="fig-box" x="284" y="116" width="400" height="86" rx="8"/>
    <rect class="fig-box-subtle" x="522" y="26" width="148" height="66" rx="5"/>
    <rect class="fig-box-subtle" x="522" y="126" width="148" height="66" rx="5"/>
    <rect class="fig-box-subtle" x="16" y="214" width="668" height="36" rx="6"/>
    <line class="fig-line" x1="132" y1="88" x2="132" y2="104" stroke-width="1.5" marker-end="url(#cnq-w1-arr2)"/>
    <path class="fig-line-accent" d="M 234 128 L 260 128 L 260 59 L 282 59" stroke-width="1.6" marker-end="url(#cnq-w1-arr2)"/>
    <path class="fig-line-accent" d="M 234 128 L 260 128 L 260 159 L 282 159" stroke-width="1.6" marker-end="url(#cnq-w1-arr2)"/>
    <line class="fig-line-dashed" x1="554" y1="32" x2="554" y2="76"/>
    <line class="fig-line-dashed" x1="582" y1="32" x2="582" y2="76"/>
    <line class="fig-line-dashed" x1="610" y1="32" x2="610" y2="76"/>
    <line class="fig-line-dashed" x1="638" y1="32" x2="638" y2="76"/>
    <polyline class="fig-line" stroke="var(--sl-color-blue)" stroke-width="2" points="532,40 554,40 554,68 582,68 582,40 610,40 610,54 624,54 624,40 638,40 638,68 658,68"/>
    <line class="fig-divider" x1="530" y1="154" x2="662" y2="154"/>
    <path class="fig-line" stroke="var(--sl-color-orange)" stroke-width="1.8" d="M 532 154 Q 540 134 548 154 T 564 154 Q 572 134 580 154 T 596 154 L 626 154 Q 634 134 642 154 T 658 154"/>
    <rect class="fig-box-green" x="30" y="46" width="204" height="42" rx="5"/>
    <rect class="fig-box-accent" x="30" y="106" width="204" height="44" rx="5"/>
    <rect class="fig-box-purple" x="30" y="160" width="204" height="30" rx="5"/>
    <text class="fig-title" x="132" y="35" font-size="12.5" text-anchor="middle">主机网卡（网络适配器）</text>
    <text class="fig-green" x="132" y="64" font-size="12" text-anchor="middle">数据链路层：封装成帧</text>
    <text class="fig-muted" x="132" y="80" font-size="10.5" text-anchor="middle">向下交付数字比特流 1 0 1 1 0</text>
    <text class="fig-accent" x="132" y="125" font-size="12" text-anchor="middle">物理层：编码或调制变换</text>
    <text class="fig-muted" x="132" y="142" font-size="10.5" text-anchor="middle">逻辑传比特流，媒体跑物理信号</text>
    <text class="fig-purple" x="132" y="179" font-size="11" text-anchor="middle">★ 网卡同时横跨数据链路层与物理层</text>
    <text class="fig-blue" x="298" y="40" font-size="12.5">① 基带调制（编码 Coding）</text>
    <text class="fig-label" x="298" y="60" font-size="11.5">数字基带信号 → 另一种数字方波信号</text>
    <text class="fig-muted" x="298" y="79" font-size="11">适配近距基带信道（曼彻斯特 / NRZI / 4B5B）</text>
    <text class="fig-muted" x="596" y="87" font-size="10.5" text-anchor="middle">离散数字电平码元</text>
    <text class="fig-orange" x="298" y="140" font-size="12.5">② 带通调制（调制 Modulation）</text>
    <text class="fig-label" x="298" y="160" font-size="11.5">数字基带信号 → 搬移至高频模拟载波</text>
    <text class="fig-muted" x="298" y="179" font-size="11">适配远距/无线带通信道（ASK / FSK / PSK / QAM）</text>
    <text class="fig-muted" x="596" y="187" font-size="10.5" text-anchor="middle">连续模拟正弦码元</text>
    <text class="fig-label" x="350" y="236" font-size="11.5" text-anchor="middle">码元（Symbol）：时间轴相邻虚线间代表离散数值的一段基本波形（时长称码元期间），可携带 1 或多比特</text>
  </svg>
  <figcaption>图 2-1：网卡跨层边界与基带调制（编码）、带通调制（调制）的信号变换对比</figcaption>
</figure>

### 2.2 常用编码方法

<figure class="cpd-figure not-content" id="cnq-w1-fig-3">
  <svg viewBox="0 0 700 326" role="img" aria-label="比特流 10110 在 NRZ、RZ、NRZI、曼彻斯特与差分曼彻斯特五种基带编码下的同步波形对比">
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="298" rx="8"/>
    <line class="fig-line-dashed" x1="164" y1="44" x2="164" y2="296"/>
    <line class="fig-line-dashed" x1="244" y1="44" x2="244" y2="296"/>
    <line class="fig-line-dashed" x1="324" y1="44" x2="324" y2="296"/>
    <line class="fig-line-dashed" x1="404" y1="44" x2="404" y2="296"/>
    <line class="fig-line-dashed" x1="484" y1="44" x2="484" y2="296"/>
    <line class="fig-line-dashed" x1="564" y1="44" x2="564" y2="296"/>
    <line class="fig-divider" x1="164" y1="68" x2="564" y2="68" stroke-dasharray="2 3"/>
    <line class="fig-divider" x1="164" y1="118" x2="564" y2="118" stroke-dasharray="2 3"/>
    <line class="fig-divider" x1="164" y1="168" x2="564" y2="168" stroke-dasharray="2 3"/>
    <line class="fig-divider" x1="164" y1="218" x2="564" y2="218" stroke-dasharray="2 3"/>
    <line class="fig-divider" x1="164" y1="270" x2="564" y2="270" stroke-dasharray="2 3"/>
    <line class="fig-line-dashed" x1="146" y1="152" x2="164" y2="152" stroke-width="1.8"/>
    <line class="fig-line-dashed" x1="146" y1="254" x2="164" y2="254" stroke-width="1.8"/>
    <polyline class="fig-line" stroke="var(--sl-color-blue)" stroke-width="2.2" points="164,52 244,52 244,84 324,84 324,52 484,52 484,84 564,84"/>
    <polyline class="fig-line" stroke="var(--sl-color-green)" stroke-width="2.2" points="164,102 204,102 204,118 244,118 244,134 284,134 284,118 324,118 324,102 364,102 364,118 404,118 404,102 444,102 444,118 484,118 484,134 524,134 524,118 564,118"/>
    <polyline class="fig-line" stroke="var(--sl-color-purple)" stroke-width="2.2" points="164,152 244,152 244,184 484,184 484,152 564,152"/>
    <polyline class="fig-line-accent" stroke-width="2.2" points="164,234 204,234 204,202 244,202 284,202 284,234 324,234 364,234 364,202 404,202 404,234 444,234 444,202 484,202 524,202 524,234 564,234"/>
    <polyline class="fig-line" stroke="var(--sl-color-orange)" stroke-width="2.2" points="164,254 204,254 204,286 244,286 244,254 284,254 284,286 364,286 364,254 444,254 444,286 484,286 484,254 524,254 524,286 564,286"/>
    <circle class="fig-dot" cx="244" cy="168" r="3"/>
    <circle class="fig-dot" cx="484" cy="168" r="3"/>
    <circle class="fig-dot" cx="244" cy="270" r="3"/>
    <circle class="fig-dot" cx="484" cy="270" r="3"/>
    <rect class="fig-box-accent" x="172" y="19" width="64" height="22" rx="4"/>
    <rect class="fig-box" x="252" y="19" width="64" height="22" rx="4"/>
    <rect class="fig-box-accent" x="332" y="19" width="64" height="22" rx="4"/>
    <rect class="fig-box-accent" x="412" y="19" width="64" height="22" rx="4"/>
    <rect class="fig-box" x="492" y="19" width="64" height="22" rx="4"/>
    <rect class="fig-box" x="572" y="55" width="104" height="24" rx="4"/>
    <rect class="fig-box-green" x="572" y="105" width="104" height="24" rx="4"/>
    <rect class="fig-box-purple" x="572" y="155" width="104" height="24" rx="4"/>
    <rect class="fig-box-accent" x="572" y="205" width="104" height="24" rx="4"/>
    <rect class="fig-box-orange" x="572" y="257" width="104" height="24" rx="4"/>
    <text class="fig-title" x="28" y="34" font-size="12">输入比特流</text>
    <text class="fig-accent" x="204" y="34" font-size="12.5" text-anchor="middle">1</text>
    <text class="fig-label" x="284" y="34" font-size="12.5" text-anchor="middle">0</text>
    <text class="fig-accent" x="364" y="34" font-size="12.5" text-anchor="middle">1</text>
    <text class="fig-accent" x="444" y="34" font-size="12.5" text-anchor="middle">1</text>
    <text class="fig-label" x="524" y="34" font-size="12.5" text-anchor="middle">0</text>
    <text class="fig-blue" x="28" y="66" font-size="12">NRZ 不归零</text>
    <text class="fig-muted" x="28" y="81" font-size="10.5">高=1 / 低=0</text>
    <text class="fig-green" x="28" y="116" font-size="12">RZ 归零</text>
    <text class="fig-muted" x="28" y="131" font-size="10.5">半周期归零</text>
    <text class="fig-purple" x="28" y="166" font-size="12">NRZI 反转</text>
    <text class="fig-muted" x="28" y="181" font-size="10.5">不变=1 / 翻=0</text>
    <text class="fig-accent" x="28" y="216" font-size="12">曼彻斯特</text>
    <text class="fig-muted" x="28" y="231" font-size="10.5">IEEE 低→高=1</text>
    <text class="fig-orange" x="28" y="268" font-size="12">差分曼彻斯特</text>
    <text class="fig-muted" x="28" y="283" font-size="10.5">始跳=0 / 不跳=1</text>
    <text class="fig-muted" x="624" y="71" font-size="10.5" text-anchor="middle">无自同步能力</text>
    <text class="fig-green" x="624" y="121" font-size="10.5" text-anchor="middle">归零携带时钟</text>
    <text class="fig-purple" x="624" y="171" font-size="10.5" text-anchor="middle">遇 0 边界翻转</text>
    <text class="fig-accent" x="624" y="221" font-size="10.5" text-anchor="middle">中心跳变=时钟</text>
    <text class="fig-orange" x="624" y="273" font-size="10.5" text-anchor="middle">抗极性反转强</text>
  </svg>
  <figcaption>图 2-2：五种常用数字基带编码同步波形对比（虚线短横为初始参考电平，圆点标出边界翻转位置）</figcaption>
</figure>

| 编码 | 规则 | 自同步 | 优缺点 |
|---|---|---|---|
| <strong>NRZ 不归零 | 整个码元期间保持正电平 = 1，负电平 = 0，不回归零电平</strong> | 否 | 编码效率最高；收发需另加时钟线 |
| <strong>RZ</strong> 归零 | 码元中间时刻必归零；正电平 = 1，负电平 = 0 | 是 | 时钟编码进数据；相同数据率需更大带宽（每码元电平变化一次） |
| <strong>NRZI</strong> 不归零反转 | 码元边界<strong>电平不变 = 1</strong>，电平<strong>翻转 = 0</strong> | 部分（连 1 多时失效） | NRZ 的高效 + RZ 的同步机制；USB 2.0 使用；连 1 过长仍会失步，需位填充或扰码 |
| <strong>曼彻斯特</strong> | 每个码元<strong>中间时刻跳变</strong>，跳变既表时钟又表数据 | 是 | 传统 10 Mb/s 以太网（10BASE5/2/T）采用 |
| <strong>差分曼彻斯特</strong> | 中间时刻跳变<strong>仅表时钟</strong>；码元<strong>起始处</strong>无跳变 = 1、有跳变 = 0 | 是 | 抗极性翻转、抗干扰更强 |

<strong>为什么需要自同步</strong>：接收方只能看到一段正电平，无法判断它代表几个 1；若另加时钟线，长距离传输无法保证时钟与数据绝对同步，且布线成本上升。RZ 用“每码元必归零”、曼彻斯特用“每码元中间必跳变”把时钟藏进数据。

<strong>曼彻斯特的两种标准</strong>：Thomas 标准高→低为 1、低→高为 0；IEEE 802.3 标准相反（高→低为 0、低→高为 1）。

> <strong>注意：408 不要求区分这两种标准，只考“中间跳变既表时钟也表数据”。写比特串时若与选项都不符，按位取反</strong>即可；差分曼彻斯特若没给最左侧码元的前一码元，第一个比特无法判定，但<strong>不影响选出答案</strong>。

> <strong>★考点</strong>：题目给“10 Mb/s 传统以太网”，要立刻联想两件事——采用<strong>曼彻斯特编码</strong>、争用期为 <strong>51.2 μs</strong>。

<figure class="cpd-figure not-content" id="cnq-w1-fig-4">
  <svg viewBox="0 0 700 220" role="img" aria-label="传统以太网曼彻斯特编码波特率代价与百兆以太网 4B/5B 编码效率对比">
    <defs>
      <marker id="cnq-w1-arr4" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="14" width="326" height="192" rx="8"/>
    <rect class="fig-box-subtle" x="358" y="14" width="326" height="192" rx="8"/>
    <line class="fig-line" x1="464" y1="80" x2="492" y2="80" stroke-width="1.5" marker-end="url(#cnq-w1-arr4)"/>
    <line class="fig-line" x1="590" y1="80" x2="612" y2="80" stroke-width="1.5" marker-end="url(#cnq-w1-arr4)"/>
    <rect class="fig-box" x="32" y="48" width="294" height="54" rx="5"/>
    <rect class="fig-box-accent" x="32" y="114" width="294" height="36" rx="5"/>
    <rect class="fig-box-orange" x="32" y="158" width="294" height="36" rx="5"/>
    <rect class="fig-box-blue" x="372" y="54" width="92" height="52" rx="5"/>
    <rect class="fig-box-green" x="494" y="54" width="96" height="52" rx="5"/>
    <rect class="fig-box-purple" x="614" y="54" width="56" height="52" rx="5"/>
    <rect class="fig-box" x="372" y="118" width="298" height="34" rx="5"/>
    <rect class="fig-box-green" x="372" y="160" width="298" height="34" rx="5"/>
    <text class="fig-title" x="179" y="36" font-size="12.5" text-anchor="middle">10 Mb/s 传统以太网（曼彻斯特）</text>
    <text class="fig-label" x="179" y="69" font-size="11.5" text-anchor="middle">每 1 bit 中心必跳变 → 拆成 2 个脉冲码元</text>
    <text class="fig-muted" x="179" y="89" font-size="11" text-anchor="middle">数据率 Rb = 10 Mb/s，调制速率 RB = 20 MBaud</text>
    <text class="fig-accent" x="179" y="136" font-size="11.5" text-anchor="middle">编码效率 = 10 / 20 = 50%（频带开销翻倍）</text>
    <text class="fig-orange" x="179" y="180" font-size="11.5" text-anchor="middle">★ 必考联动：争用期 51.2 μs ↔ 最短帧 64 B</text>
    <text class="fig-title" x="521" y="36" font-size="12.5" text-anchor="middle">百兆以太网演进：4B/5B 块编码 + NRZI</text>
    <text class="fig-blue" x="418" y="76" font-size="11.5" text-anchor="middle">4 bit 数据</text>
    <text class="fig-muted" x="418" y="94" font-size="10.5" text-anchor="middle">16 种组合</text>
    <text class="fig-green" x="542" y="76" font-size="11.5" text-anchor="middle">5 bit 码组</text>
    <text class="fig-muted" x="542" y="94" font-size="10.5" text-anchor="middle">禁全 0 码组</text>
    <text class="fig-purple" x="642" y="76" font-size="11.5" text-anchor="middle">NRZI</text>
    <text class="fig-muted" x="642" y="94" font-size="10" text-anchor="middle">防失步</text>
    <text class="fig-label" x="521" y="139" font-size="11.5" text-anchor="middle">100BASE-TX：Rb = 100 Mb/s 仅需 RB = 125 MBaud</text>
    <text class="fig-green" x="521" y="181" font-size="11.5" text-anchor="middle">编码效率提升至 4 / 5 = 80%（冗余 16 码作控制）</text>
  </svg>
  <figcaption>图 2-3：曼彻斯特编码 50% 频谱效率代价与快速以太网 4B/5B + NRZI 编码对比</figcaption>
</figure>

### 2.3 基本带通调制与混合调制

<figure class="cpd-figure not-content" id="cnq-w1-fig-5">
  <svg viewBox="0 0 700 278" role="img" aria-label="比特流 1010 在 ASK 幅移键控、FSK 频移键控与 PSK 相移键控下的同步载波波形对比">
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="250" rx="8"/>
    <line class="fig-line-dashed" x1="144" y1="46" x2="144" y2="252"/>
    <line class="fig-line-dashed" x1="240" y1="46" x2="240" y2="252"/>
    <line class="fig-line-dashed" x1="336" y1="46" x2="336" y2="252"/>
    <line class="fig-line-dashed" x1="432" y1="46" x2="432" y2="252"/>
    <line class="fig-line-dashed" x1="528" y1="46" x2="528" y2="252"/>
    <line class="fig-divider" x1="144" y1="80" x2="528" y2="80" stroke-dasharray="2 3"/>
    <line class="fig-divider" x1="144" y1="150" x2="528" y2="150" stroke-dasharray="2 3"/>
    <line class="fig-divider" x1="144" y1="220" x2="528" y2="220" stroke-dasharray="2 3"/>
    <path class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.9" d="M 144 80 Q 150 58 156 80 T 168 80 T 180 80 T 192 80 T 204 80 T 216 80 T 228 80 T 240 80 L 336 80 Q 342 58 348 80 T 360 80 T 372 80 T 384 80 T 396 80 T 408 80 T 420 80 T 432 80 L 528 80"/>
    <path class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.9" d="M 144 150 Q 150 128 156 150 T 168 150 T 180 150 T 192 150 T 204 150 T 216 150 T 228 150 T 240 150 Q 252 128 264 150 T 288 150 T 312 150 T 336 150 Q 342 128 348 150 T 360 150 T 372 150 T 384 150 T 396 150 T 408 150 T 420 150 T 432 150 Q 444 128 456 150 T 480 150 T 504 150 T 528 150"/>
    <path class="fig-line" stroke="var(--sl-color-orange)" stroke-width="1.9" d="M 144 220 Q 150 198 156 220 T 168 220 T 180 220 T 192 220 T 204 220 T 216 220 T 228 220 T 240 220 M 240 220 Q 246 242 252 220 T 264 220 T 276 220 T 288 220 T 300 220 T 312 220 T 324 220 T 336 220 M 336 220 Q 342 198 348 220 T 360 220 T 372 220 T 384 220 T 396 220 T 408 220 T 420 220 T 432 220 M 432 220 Q 438 242 444 220 T 456 220 T 468 220 T 480 220 T 492 220 T 504 220 T 516 220 T 528 220"/>
    <circle class="fig-dot" cx="240" cy="220" r="3.2"/>
    <circle class="fig-dot" cx="336" cy="220" r="3.2"/>
    <circle class="fig-dot" cx="432" cy="220" r="3.2"/>
    <rect class="fig-box-accent" x="154" y="20" width="76" height="22" rx="4"/>
    <rect class="fig-box" x="250" y="20" width="76" height="22" rx="4"/>
    <rect class="fig-box-accent" x="346" y="20" width="76" height="22" rx="4"/>
    <rect class="fig-box" x="442" y="20" width="76" height="22" rx="4"/>
    <rect class="fig-box-blue" x="538" y="66" width="134" height="28" rx="4"/>
    <rect class="fig-box-green" x="538" y="136" width="134" height="28" rx="4"/>
    <rect class="fig-box-orange" x="538" y="206" width="134" height="28" rx="4"/>
    <text class="fig-title" x="28" y="35" font-size="12">基带比特流</text>
    <text class="fig-accent" x="192" y="35" font-size="12.5" text-anchor="middle">1</text>
    <text class="fig-label" x="288" y="35" font-size="12.5" text-anchor="middle">0</text>
    <text class="fig-accent" x="384" y="35" font-size="12.5" text-anchor="middle">1</text>
    <text class="fig-label" x="480" y="35" font-size="12.5" text-anchor="middle">0</text>
    <text class="fig-blue" x="28" y="77" font-size="12">2ASK 幅移</text>
    <text class="fig-muted" x="28" y="93" font-size="10.5">控制振幅 A</text>
    <text class="fig-green" x="28" y="147" font-size="12">2FSK 频移</text>
    <text class="fig-muted" x="28" y="163" font-size="10.5">控制频率 f</text>
    <text class="fig-orange" x="28" y="217" font-size="12">2PSK 相移</text>
    <text class="fig-muted" x="28" y="233" font-size="10.5">控制初相 φ</text>
    <text class="fig-blue" x="605" y="84" font-size="10.5" text-anchor="middle">有载波=1 / 无=0</text>
    <text class="fig-green" x="605" y="154" font-size="10.5" text-anchor="middle">高频=1 / 低频=0</text>
    <text class="fig-orange" x="605" y="224" font-size="10.5" text-anchor="middle">边界 180° 反相跳变</text>
  </svg>
  <figcaption>图 2-4：三种基本带通调制（2ASK / 2FSK / 2PSK）波形对比（均仅含 2 种码元状态，每码元携带 1 bit）</figcaption>
</figure>

- <strong>ASK 幅移键控</strong>：控制载波<strong>振幅</strong>，在非零振幅与零振幅（无载波）之间切换。
- <strong>FSK 频移键控</strong>：控制载波<strong>频率</strong>，在两种频率之间切换。
- <strong>PSK 相移键控</strong>：控制载波<strong>初相位</strong>，在两种初相位之间切换。

三者都只调制出<strong>两种</strong>码元，故一个码元只携带 1 bit。若把<strong>模拟</strong>信号（如原始语音）转成另一种模拟信号，对应的三种基本方法称为<strong>调幅 AM / 调频 FM / 调相 PM</strong>——名字不同，本质是同一组参数。

<strong>混合调制</strong>：技术上更复杂，但能调制出多种码元，使一个码元携带多个比特，从而提高数据传输速率。QAM（正交振幅调制）同时调制两个<strong>正交</strong>（时域相差 90°）载波的幅度，间接实现幅度与相位的混合调制。

<figure class="cpd-figure not-content" id="cnq-w1-fig-6">
  <svg viewBox="0 0 700 302" role="img" aria-label="QAM-16 正交振幅调制极坐标星座图与格雷码相邻单比特差异抗误码原理">
    <defs>
      <marker id="cnq-w1-arr6" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="14" width="332" height="274" rx="8"/>
    <rect class="fig-box-subtle" x="364" y="14" width="320" height="274" rx="8"/>
    <circle class="fig-line-dashed" cx="182" cy="148" r="92"/>
    <circle class="fig-line-dashed" cx="182" cy="148" r="52"/>
    <line class="fig-divider" x1="68" y1="148" x2="296" y2="148"/>
    <line class="fig-divider" x1="182" y1="42" x2="182" y2="254"/>
    <line class="fig-line-accent" x1="182" y1="148" x2="262" y2="102" stroke-width="1.6"/>
    <path class="fig-line" stroke="var(--sl-color-orange)" stroke-width="1.6" d="M 218 148 A 36 36 0 0 0 213 130"/>
    <line class="fig-line-dashed" x1="484" y1="78" x2="560" y2="78" marker-start="url(#cnq-w1-arr6)" marker-end="url(#cnq-w1-arr6)"/>
    <line class="fig-line-dashed" x1="600" y1="98" x2="600" y2="136" marker-start="url(#cnq-w1-arr6)" marker-end="url(#cnq-w1-arr6)"/>
    <line class="fig-line-dashed" x1="444" y1="98" x2="444" y2="136"/>
    <line class="fig-line-dashed" x1="484" y1="154" x2="560" y2="154"/>
    <circle class="fig-dot" cx="182" cy="148" r="2.8"/>
    <circle class="fig-box-blue" cx="274" cy="148" r="5"/>
    <circle class="fig-box-blue" cx="262" cy="102" r="5"/>
    <circle class="fig-box-blue" cx="228" cy="68" r="5"/>
    <circle class="fig-box-blue" cx="182" cy="56" r="5"/>
    <circle class="fig-box-blue" cx="136" cy="68" r="5"/>
    <circle class="fig-box-blue" cx="102" cy="102" r="5"/>
    <circle class="fig-box-blue" cx="90" cy="148" r="5"/>
    <circle class="fig-box-blue" cx="102" cy="194" r="5"/>
    <circle class="fig-box-blue" cx="136" cy="228" r="5"/>
    <circle class="fig-box-blue" cx="182" cy="240" r="5"/>
    <circle class="fig-box-blue" cx="228" cy="228" r="5"/>
    <circle class="fig-box-blue" cx="262" cy="194" r="5"/>
    <circle class="fig-box-green" cx="234" cy="148" r="5.5"/>
    <circle class="fig-box-green" cx="182" cy="96" r="5.5"/>
    <circle class="fig-box-green" cx="130" cy="148" r="5.5"/>
    <circle class="fig-box-green" cx="182" cy="200" r="5.5"/>
    <rect class="fig-box" x="198" y="102" width="46" height="18" rx="3"/>
    <rect class="fig-box-blue" x="406" y="62" width="76" height="32" rx="5"/>
    <rect class="fig-box-accent" x="562" y="62" width="76" height="32" rx="5"/>
    <rect class="fig-box-blue" x="406" y="138" width="76" height="32" rx="5"/>
    <rect class="fig-box-blue" x="562" y="138" width="76" height="32" rx="5"/>
    <rect class="fig-box-green" x="380" y="186" width="288" height="44" rx="5"/>
    <rect class="fig-box-orange" x="380" y="238" width="288" height="38" rx="5"/>
    <text class="fig-title" x="182" y="34" font-size="12.5" text-anchor="middle">QAM-16 星座图（振幅 + 相位混合）</text>
    <text class="fig-accent" x="221" y="115" font-size="10.5" text-anchor="middle">振幅 A</text>
    <text class="fig-orange" x="234" y="140" font-size="10.5">φ</text>
    <text class="fig-muted" x="302" y="152" font-size="10.5">I</text>
    <text class="fig-muted" x="190" y="48" font-size="10.5">Q</text>
    <text class="fig-label" x="182" y="266" font-size="11" text-anchor="middle">12 种相位 × (1 或 2 种振幅) = 16 个星座点</text>
    <text class="fig-muted" x="182" y="281" font-size="10.5" text-anchor="middle">蓝圈外环 12 点 + 绿圈内环 4 点 → 4 bit/码元</text>
    <text class="fig-title" x="524" y="34" font-size="12.5" text-anchor="middle">格雷码映射：相邻星座点仅差 1 bit</text>
    <text class="fig-blue" x="444" y="82" font-size="12" text-anchor="middle">0010</text>
    <text class="fig-accent" x="600" y="82" font-size="12" text-anchor="middle">0011 ★</text>
    <text class="fig-blue" x="444" y="158" font-size="12" text-anchor="middle">0000</text>
    <text class="fig-blue" x="600" y="158" font-size="12" text-anchor="middle">0001</text>
    <text class="fig-muted" x="522" y="71" font-size="10" text-anchor="middle">差 1 bit</text>
    <text class="fig-muted" x="634" y="121" font-size="10" text-anchor="middle">差 1 bit</text>
    <text class="fig-green" x="524" y="204" font-size="11.5" text-anchor="middle">噪声致邻点误判仅错 1 bit → 契合海明码纠错</text>
    <text class="fig-muted" x="524" y="221" font-size="10.5" text-anchor="middle">避免单次符号误判引发多比特集中突发错误</text>
    <text class="fig-orange" x="524" y="261" font-size="11.5" text-anchor="middle">★ 必背换算：k = log₂N，Rb = RB · log₂N</text>
  </svg>
  <figcaption>图 2-5：QAM-16 星座图结构（距原点距离为振幅、夹角为相位）与相邻码元格雷码单比特容错原理</figcaption>
</figure>

<strong>星座图</strong>比波形图更适合分析：波形图含冗余的时域细节，难以体现调制状态之间的关键差异。星座图中每个<strong>星座点就是一个码元</strong>，到圆心的距离是<strong>振幅</strong>，与水平轴的夹角是<strong>相位</strong>。上例 12 种相位、每种 1 或 2 种振幅，共 <strong>16 种码元</strong>，即 <strong>QAM-16</strong>，每个码元需 4 bit 编码。

> <strong>★考点</strong>：不管题目给的调制方法名称多陌生，只关心它<strong>能产生多少种码元 N</strong>；每码元比特数、比特率与波特率的关系是固定的。

$$
k=\log_{2}N
$$

$$
R_{b}=R_{B}\cdot \log_{2}N
$$

（$R_B$ 为码元传输速率，单位 Baud；$R_b$ 为信息传输速率，单位 bit/s。）

<strong>格雷码映射</strong>：相邻码元只有 1 个比特不同。这样噪声导致码元误判时，只产生<strong>1 个比特错误</strong>——单个比特错误可用纠错码（如海明码）处理，而海明码擅长纠正<strong>少量分散</strong>的比特错误，而非集中爆发的多比特错误。这对高噪声信道（如无线信道）尤为重要。

### 2.4 奈氏准则与香农公式

<figure class="cpd-figure not-content" id="cnq-w1-fig-7">
  <svg viewBox="0 0 700 284" role="img" aria-label="奈氏准则与香农公式双路约束决策流与分贝换算速查图">
    <defs>
      <marker id="cnq-w1-arr7" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="86" rx="8"/>
    <rect class="fig-box-subtle" x="16" y="110" width="668" height="90" rx="8"/>
    <rect class="fig-box" x="16" y="210" width="668" height="58" rx="6"/>
    <line class="fig-line" x1="172" y1="65" x2="198" y2="65" stroke-width="1.5" marker-end="url(#cnq-w1-arr7)"/>
    <line class="fig-line" x1="396" y1="65" x2="422" y2="65" stroke-width="1.5" marker-end="url(#cnq-w1-arr7)"/>
    <line class="fig-line" x1="216" y1="163" x2="244" y2="163" stroke-width="1.5" marker-end="url(#cnq-w1-arr7)"/>
    <line class="fig-line-accent" x1="460" y1="163" x2="488" y2="163" stroke-width="1.6" marker-end="url(#cnq-w1-arr7)"/>
    <path class="fig-line-accent" d="M 579 88 L 579 136" stroke-width="1.6" marker-end="url(#cnq-w1-arr7)"/>
    <rect class="fig-box" x="30" y="42" width="142" height="46" rx="5"/>
    <rect class="fig-box-blue" x="200" y="42" width="196" height="46" rx="5"/>
    <rect class="fig-box-accent" x="424" y="42" width="244" height="46" rx="5"/>
    <rect class="fig-box-orange" x="30" y="140" width="186" height="46" rx="5"/>
    <rect class="fig-box-green" x="246" y="140" width="214" height="46" rx="5"/>
    <rect class="fig-box-red" x="490" y="138" width="178" height="50" rx="5"/>
    <text class="fig-title" x="30" y="32" font-size="12">路径 A：理想低通信道（无噪声，防码间串扰）</text>
    <text class="fig-label" x="101" y="61" font-size="11.5" text-anchor="middle">信道带宽 W (Hz)</text>
    <text class="fig-muted" x="101" y="78" font-size="10" text-anchor="middle">决定波形最高变化率</text>
    <text class="fig-blue" x="298" y="61" font-size="11.5" text-anchor="middle">奈氏极限波特率 2W Baud</text>
    <text class="fig-muted" x="298" y="78" font-size="10" text-anchor="middle">超限则波形重叠串扰</text>
    <text class="fig-accent" x="546" y="61" font-size="11.5" text-anchor="middle">奈氏比特率上限 C1 = 2W·log₂N</text>
    <text class="fig-muted" x="546" y="78" font-size="10" text-anchor="middle">N 为每个码元离散状态数</text>
    <text class="fig-title" x="30" y="129" font-size="12">路径 B：带宽受限且含高斯白噪声信道</text>
    <text class="fig-orange" x="123" y="159" font-size="11.5" text-anchor="middle">信噪比 dB = 10·lg(S/N)</text>
    <text class="fig-muted" x="123" y="176" font-size="10" text-anchor="middle">先反推无量纲比值 S/N</text>
    <text class="fig-green" x="353" y="159" font-size="11.5" text-anchor="middle">香农上限 C2 = W·log₂(1+S/N)</text>
    <text class="fig-muted" x="353" y="176" font-size="10" text-anchor="middle">与调制阶数 N 无直接关系</text>
    <text class="fig-red" x="579" y="158" font-size="11.5" text-anchor="middle">双约束综合上限 ★</text>
    <text class="fig-label" x="579" y="176" font-size="11" text-anchor="middle">Cmax = min(C1, C2)</text>
    <text class="fig-label" x="350" y="232" font-size="11.5" text-anchor="middle">审题抓字眼：“理想/无噪声/N进制” → 奈氏；“信噪比/dB” → 香农；同时出现 → 两式分别求值取 min</text>
    <text class="fig-muted" x="350" y="253" font-size="10.5" text-anchor="middle">速算：30 dB → S/N = 1000（log₂1001 ≈ 10）；60 dB → 10⁶（≈ 20）｜提升上限唯二途径：高阶调制 + 提高信噪比</text>
  </svg>
  <figcaption>图 2-6：奈氏准则与香农公式双路径推导及综合约束取最小值（min）决策模型</figcaption>
</figure>

<strong>奈氏准则</strong>：理想低通信道的最高码元传输速率

$$
C_{\max}=2W\ (\text{Baud})
$$

其中 $W$ 为信道频率带宽（Hz），Baud 即波特，是码元传输速率单位。意义：<strong>为避免码间串扰，码元传输速率不能超过此上限</strong>。当码元速率超过上限，相邻码元的波形界限消失，接收方无法正确识别一个个码元，即码间串扰。

<strong>香农公式</strong>：频率带宽受限且有高斯白噪声干扰的信道的极限信息传输速率

$$
C=W\log_{2}\left(1+\frac{S}{N}\right)
$$

$S$ 为信号平均功率，$N$ 为高斯噪声功率，二者单位相同故 $S/N$ 无量纲。分贝与比值的换算：

$$
x_{\mathrm{dB}}=10\log_{10}\frac{S}{N}
$$

常用值：30 dB → 1000；60 dB → $10^6$；−30 dB → 0.001。

> <strong>注意</strong>：题目给的往往是<strong>分贝</strong>形式，必须先用上式换算成比值再代入香农公式。

<strong>近似计算技巧</strong>：$\log_2(1+S/N)$ 一般不要求精确求解，把真数换成最接近的 2 的幂即可。如 $S/N=1000$，真数为 1001，取 $2^{10}=1024$，对数近似为 <strong>10</strong>。

<strong>香农公式的实际意义</strong>：只要信息传输速率低于极限值，就<strong>一定存在某种方法</strong>实现无差错传输——但香农并未给出具体实现方法。这也解释了为什么香农公式发表后各种信号处理与调制技术层出不穷：都是为了让实际比特率逼近这个上限。

<strong>综合结论</strong>：带宽一定时，要提高极限信息传输速率，只有两条路——<strong>① 让每个码元携带更多比特（更复杂的调制技术）；② 提高信道中的信噪比</strong>。

> <strong>★考点</strong>：三类典型考法——奈氏准则与调制技术（如 QAM）综合考察波特率/比特率/调制阶数三者关系；只考奈氏；奈氏与香农同时考（题中既有“理想信道”又有“信噪比”）。

<figure class="cpd-figure not-content" id="cnq-w1-fig-8">
  <svg viewBox="0 0 700 226" role="img" aria-label="码间串扰波形展宽重叠机理与奈氏香农综合真题计算示例">
    <defs>
      <marker id="cnq-w1-arr8" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="14" width="328" height="198" rx="8"/>
    <rect class="fig-box-subtle" x="360" y="14" width="324" height="198" rx="8"/>
    <rect class="fig-box" x="30" y="42" width="300" height="72" rx="5"/>
    <rect class="fig-box-red" x="30" y="124" width="300" height="74" rx="5"/>
    <line class="fig-divider" x1="44" y1="98" x2="316" y2="98"/>
    <line class="fig-divider" x1="44" y1="178" x2="316" y2="178"/>
    <path class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.8" d="M 56 98 Q 86 98 96 68 Q 106 98 136 98"/>
    <path class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.8" d="M 140 98 Q 170 98 180 68 Q 190 98 220 98"/>
    <path class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.8" d="M 224 98 Q 254 98 264 68 Q 274 98 304 98"/>
    <path class="fig-line" stroke="var(--sl-color-red)" stroke-width="1.8" d="M 48 178 Q 92 176 122 150 Q 152 176 196 178"/>
    <path class="fig-line" stroke="var(--sl-color-orange)" stroke-width="1.8" d="M 106 178 Q 150 176 180 150 Q 210 176 254 178"/>
    <path class="fig-line" stroke="var(--sl-color-red)" stroke-width="1.8" d="M 164 178 Q 208 176 238 150 Q 268 176 312 178"/>
    <line class="fig-line" x1="522" y1="86" x2="522" y2="96" stroke-width="1.4" marker-end="url(#cnq-w1-arr8)"/>
    <line class="fig-line" x1="522" y1="140" x2="522" y2="150" stroke-width="1.4" marker-end="url(#cnq-w1-arr8)"/>
    <rect class="fig-box-blue" x="376" y="42" width="292" height="44" rx="5"/>
    <rect class="fig-box-green" x="376" y="96" width="292" height="44" rx="5"/>
    <rect class="fig-box-accent" x="376" y="150" width="292" height="48" rx="5"/>
    <text class="fig-title" x="180" y="34" font-size="12.5" text-anchor="middle">码间串扰（ISI）波形对比</text>
    <text class="fig-green" x="180" y="57" font-size="11" text-anchor="middle">RB ≤ 2W：相邻码元界限清晰，采样无误判</text>
    <text class="fig-red" x="180" y="140" font-size="11" text-anchor="middle">RB &gt; 2W：高频受限致波形展宽重叠（码间串扰）</text>
    <text class="fig-muted" x="180" y="192" font-size="10" text-anchor="middle">接收端无法分辨独立码元，提高信噪比亦无法消除</text>
    <text class="fig-title" x="522" y="34" font-size="12.5" text-anchor="middle">典型算例：W = 3 kHz，SNR = 30 dB，16-QAM</text>
    <text class="fig-blue" x="522" y="60" font-size="11.5" text-anchor="middle">① 奈氏上限：2 × 3k × log₂16 = 24 kb/s</text>
    <text class="fig-muted" x="522" y="77" font-size="10" text-anchor="middle">受限于波特率 6 kBaud 与每码元 4 bit</text>
    <text class="fig-green" x="522" y="114" font-size="11.5" text-anchor="middle">② 香农上限：3k × log₂(1+1000) ≈ 30 kb/s</text>
    <text class="fig-muted" x="522" y="131" font-size="10" text-anchor="middle">30 dB 换算 S/N = 10³，log₂1001 ≈ 10</text>
    <text class="fig-accent" x="522" y="169" font-size="11.5" text-anchor="middle">③ 最终最大速率：min(24, 30) = 24 kb/s ★</text>
    <text class="fig-muted" x="522" y="187" font-size="10" text-anchor="middle">若改用 32-QAM（奈氏 30 kb/s），方可跑满香农极限</text>
  </svg>
  <figcaption>图 2-7：码间串扰（ISI）重叠机理与奈氏准则、香农公式联立求解典型算例</figcaption>
</figure>

## 第3章 数据链路层：透明传输与差错检测

### 3.1 透明传输：字节填充与零比特填充

<strong>透明的含义</strong>：数据链路层<strong>不对上层交付下来的 PDU 内容做任何限制</strong>。PPP 用帧首部与尾部的<strong>标志字段 0x7E</strong> 做帧定界；若数据载荷恰好含 0x7E，接收方就会误判定界，因此必须填充。

以太网走的是另一条路：物理层前导码中的<strong>帧开始定界符</strong>之后紧跟以太网帧，一帧传完传输线上有 <strong>96 比特时间无信号</strong>，所以载荷含相同内容也不会误判——以太网<strong>不需要</strong>填充。

<figure class="cpd-figure not-content" id="cnq-w2-fig-1">
  <svg viewBox="0 0 700 296" role="img" aria-label="PPP异步链路字节填充与异或0x20硬件还原机理">
    <defs>
      <marker id="cnq-w2-arr-1" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="182" rx="8"/>
    <rect class="fig-box-subtle" x="16" y="206" width="356" height="76" rx="8"/>
    <rect class="fig-box-subtle" x="384" y="206" width="300" height="76" rx="8"/>
    <line class="fig-line" stroke="var(--sl-color-orange)" stroke-width="1.4" x1="154" y1="70" x2="130" y2="104" marker-end="url(#cnq-w2-arr-1)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.4" x1="170" y1="70" x2="188" y2="104" marker-end="url(#cnq-w2-arr-1)"/>
    <line class="fig-line" stroke="var(--sl-color-orange)" stroke-width="1.4" x1="268" y1="70" x2="250" y2="104" marker-end="url(#cnq-w2-arr-1)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.4" x1="284" y1="70" x2="308" y2="104" marker-end="url(#cnq-w2-arr-1)"/>
    <line class="fig-line" stroke="var(--sl-color-orange)" stroke-width="1.4" x1="382" y1="70" x2="370" y2="104" marker-end="url(#cnq-w2-arr-1)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.4" x1="398" y1="70" x2="428" y2="104" marker-end="url(#cnq-w2-arr-1)"/>
    <line class="fig-line" stroke-width="1.4" x1="504" y1="70" x2="492" y2="104" marker-end="url(#cnq-w2-arr-1)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.4" x1="350" y1="132" x2="350" y2="152" marker-end="url(#cnq-w2-arr-1)"/>
    <rect class="fig-box-red" x="112" y="42" width="100" height="28" rx="4"/>
    <rect class="fig-box-orange" x="226" y="42" width="100" height="28" rx="4"/>
    <rect class="fig-box-purple" x="340" y="42" width="100" height="28" rx="4"/>
    <rect class="fig-box" x="454" y="42" width="100" height="28" rx="4"/>
    <rect class="fig-box-accent" x="30" y="104" width="66" height="28" rx="4"/>
    <rect class="fig-box-orange" x="104" y="104" width="52" height="28" rx="4"/>
    <rect class="fig-box-blue" x="160" y="104" width="56" height="28" rx="4"/>
    <rect class="fig-box-orange" x="224" y="104" width="52" height="28" rx="4"/>
    <rect class="fig-box-blue" x="280" y="104" width="56" height="28" rx="4"/>
    <rect class="fig-box-orange" x="344" y="104" width="52" height="28" rx="4"/>
    <rect class="fig-box-blue" x="400" y="104" width="56" height="28" rx="4"/>
    <rect class="fig-box" x="464" y="104" width="56" height="28" rx="4"/>
    <rect class="fig-box-accent" x="528" y="104" width="66" height="28" rx="4"/>
    <rect class="fig-box-green" x="602" y="104" width="68" height="28" rx="4"/>
    <rect class="fig-box" x="30" y="152" width="640" height="32" rx="4"/>
    <text class="fig-title" x="350" y="32" font-size="12.5" text-anchor="middle">PPP 异步链路字节填充流水线（定界符 / 转义符 / 控制符 / 普通字节）</text>
    <text class="fig-muted" x="30" y="60" font-size="11">上层载荷：</text>
    <text class="fig-red" x="162" y="60" font-size="11.5" text-anchor="middle">7E (定界撞车)</text>
    <text class="fig-orange" x="276" y="60" font-size="11.5" text-anchor="middle">7D (转义本身)</text>
    <text class="fig-purple" x="390" y="60" font-size="11.5" text-anchor="middle">03 (&lt;0x20)</text>
    <text class="fig-label" x="504" y="60" font-size="11.5" text-anchor="middle">5A (普通字节)</text>
    <text class="fig-accent" x="63" y="122" font-size="11.5" text-anchor="middle">F: 7E</text>
    <text class="fig-orange" x="130" y="122" font-size="11.5" text-anchor="middle">7D</text>
    <text class="fig-blue" x="188" y="122" font-size="11.5" text-anchor="middle">5E</text>
    <text class="fig-orange" x="250" y="122" font-size="11.5" text-anchor="middle">7D</text>
    <text class="fig-blue" x="308" y="122" font-size="11.5" text-anchor="middle">5D</text>
    <text class="fig-orange" x="370" y="122" font-size="11.5" text-anchor="middle">7D</text>
    <text class="fig-blue" x="428" y="122" font-size="11.5" text-anchor="middle">23</text>
    <text class="fig-label" x="492" y="122" font-size="11.5" text-anchor="middle">5A</text>
    <text class="fig-accent" x="561" y="122" font-size="11.5" text-anchor="middle">F: 7E</text>
    <text class="fig-green" x="636" y="122" font-size="11" text-anchor="middle">透明成帧</text>
    <text class="fig-label" x="350" y="172" font-size="11" text-anchor="middle">接收方逆处理：凭首尾 0x7E 定界 → 遇 0x7D 删除转义符，并将后一字节异或 0x20 还原为 7E / 7D / 03</text>
    <text class="fig-title" x="194" y="225" font-size="11.5" text-anchor="middle">异或 0x20 硬件本质（右起第 5 位翻转，b₀ 起算）</text>
    <text class="fig-blue" x="194" y="246" font-size="11" text-anchor="middle">7E (0111 1110) ⊕ 20 (0010 0000) = 5E (0101 1110)</text>
    <text class="fig-purple" x="194" y="266" font-size="11" text-anchor="middle">03 (0000 0011) ⊕ 20 (0010 0000) = 23 (0010 0011)</text>
    <text class="fig-title" x="534" y="225" font-size="11.5" text-anchor="middle">对比：以太网为何无需填充？</text>
    <text class="fig-green" x="534" y="246" font-size="11" text-anchor="middle">帧头靠物理层前导码 SFD 定界</text>
    <text class="fig-muted" x="534" y="266" font-size="10.5" text-anchor="middle">帧尾靠 96 bit 无信号空闲间隔自然区分</text>
  </svg>
  <figcaption>图 3-1：PPP 面向字节异步链路的字节填充规则、异或 0x20 位翻转机理与以太网定界对比</figcaption>
</figure>

<strong>面向字节的异步链路 → 字节填充</strong>（“异步”指各字节在链路上出现的时间任意，接收方靠每个字节自带的起始位识别）：

| 遇到底层数据中的 | 发送方处理 | 接收方处理 |
|---|---|---|
| 0x7E（帧定界符） | 前插 0x7D，并把 0x7E <strong>减 0x20</strong> 得 <strong>0x5E</strong> | 删 0x7D，再把后一字节加 0x20 |
| 0x7D（转义字符） | 前插 0x7D，并把 0x7D 减 0x20 得 <strong>0x5D</strong> | 删 0x7D，再把后一字节加 0x20 |
| 小于 0x20 的 ASCII 控制字符 | <strong>先加 0x20 变成非控制字符，再前插 0x7D</strong> | 删 0x7D，再减 0x20 |

> <strong>注意</strong>：加/减 0x20 等价于<strong>异或 0x20</strong>，即把二进制形式右起第 5 位（最低位为第 0 位）翻转。收发双方对 0x7D 后一字节做的是<strong>同一操作</strong>（异或 0x20），只有“插入 0x7D”与“丢弃 0x7D”互为逆操作——软硬件实现都因此简化。

<strong>面向比特的同步链路 → 零比特填充</strong>（“同步”指发送方连续不断发送比特流，所有比特在统一时钟节拍下传输）：

<figure class="cpd-figure not-content" id="cnq-w2-fig-2">
  <svg viewBox="0 0 700 288" role="img" aria-label="同步链路HDLC与PPP零比特填充收发对称流水线">
    <defs>
      <marker id="cnq-w2-arr-2" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="170" rx="8"/>
    <rect class="fig-box-subtle" x="16" y="194" width="668" height="80" rx="8"/>
    <line class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.3" x1="278" y1="68" x2="294" y2="96" marker-end="url(#cnq-w2-arr-2)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.3" x1="488" y1="68" x2="526" y2="96" marker-end="url(#cnq-w2-arr-2)"/>
    <line class="fig-line" stroke-width="1.3" x1="186" y1="234" x2="218" y2="222" marker-end="url(#cnq-w2-arr-2)"/>
    <line class="fig-line" stroke-width="1.3" x1="186" y1="242" x2="218" y2="252" marker-end="url(#cnq-w2-arr-2)"/>
    <rect class="fig-box" x="150" y="42" width="26" height="24" rx="3"/>
    <rect class="fig-box-orange" x="180" y="42" width="26" height="24" rx="3"/>
    <rect class="fig-box-orange" x="210" y="42" width="26" height="24" rx="3"/>
    <rect class="fig-box-orange" x="240" y="42" width="26" height="24" rx="3"/>
    <rect class="fig-box-orange" x="270" y="42" width="26" height="24" rx="3"/>
    <rect class="fig-box-orange" x="300" y="42" width="26" height="24" rx="3"/>
    <rect class="fig-box-red" x="330" y="42" width="26" height="24" rx="3"/>
    <rect class="fig-box" x="360" y="42" width="26" height="24" rx="3"/>
    <rect class="fig-box-orange" x="390" y="42" width="26" height="24" rx="3"/>
    <rect class="fig-box-orange" x="420" y="42" width="26" height="24" rx="3"/>
    <rect class="fig-box-orange" x="450" y="42" width="26" height="24" rx="3"/>
    <rect class="fig-box-orange" x="480" y="42" width="26" height="24" rx="3"/>
    <rect class="fig-box-orange" x="510" y="42" width="26" height="24" rx="3"/>
    <rect class="fig-box" x="540" y="42" width="26" height="24" rx="3"/>
    <rect class="fig-box" x="570" y="42" width="26" height="24" rx="3"/>
    <rect class="fig-box" x="150" y="96" width="25" height="24" rx="3"/>
    <rect class="fig-box-orange" x="179" y="96" width="25" height="24" rx="3"/>
    <rect class="fig-box-orange" x="208" y="96" width="25" height="24" rx="3"/>
    <rect class="fig-box-orange" x="237" y="96" width="25" height="24" rx="3"/>
    <rect class="fig-box-orange" x="266" y="96" width="25" height="24" rx="3"/>
    <rect class="fig-box-orange" x="295" y="96" width="25" height="24" rx="3"/>
    <rect class="fig-box-green" x="324" y="96" width="25" height="24" rx="3"/>
    <rect class="fig-box" x="353" y="96" width="25" height="24" rx="3"/>
    <rect class="fig-box" x="382" y="96" width="25" height="24" rx="3"/>
    <rect class="fig-box-orange" x="411" y="96" width="25" height="24" rx="3"/>
    <rect class="fig-box-orange" x="440" y="96" width="25" height="24" rx="3"/>
    <rect class="fig-box-orange" x="469" y="96" width="25" height="24" rx="3"/>
    <rect class="fig-box-orange" x="498" y="96" width="25" height="24" rx="3"/>
    <rect class="fig-box-orange" x="527" y="96" width="25" height="24" rx="3"/>
    <rect class="fig-box-green" x="556" y="96" width="25" height="24" rx="3"/>
    <rect class="fig-box" x="585" y="96" width="25" height="24" rx="3"/>
    <rect class="fig-box" x="614" y="96" width="25" height="24" rx="3"/>
    <rect class="fig-box-accent" x="30" y="140" width="140" height="30" rx="4"/>
    <rect class="fig-box-blue" x="178" y="140" width="344" height="30" rx="4"/>
    <rect class="fig-box-accent" x="530" y="140" width="140" height="30" rx="4"/>
    <rect class="fig-box-blue" x="30" y="220" width="156" height="38" rx="5"/>
    <rect class="fig-box-green" x="220" y="210" width="224" height="24" rx="4"/>
    <rect class="fig-box-accent" x="220" y="240" width="224" height="24" rx="4"/>
    <rect class="fig-box-red" x="456" y="210" width="214" height="54" rx="5"/>
    <text class="fig-title" x="350" y="32" font-size="12.5" text-anchor="middle">同步链路零比特填充（每遇 5 个连续 1 强制插 0，无论第 6 位原本是 1 还是 0）</text>
    <text class="fig-muted" x="30" y="58" font-size="11">① 原始比特流：</text>
    <text class="fig-label" x="163" y="58" font-size="11.5" text-anchor="middle">0</text>
    <text class="fig-orange" x="193" y="58" font-size="11.5" text-anchor="middle">1</text>
    <text class="fig-orange" x="223" y="58" font-size="11.5" text-anchor="middle">1</text>
    <text class="fig-orange" x="253" y="58" font-size="11.5" text-anchor="middle">1</text>
    <text class="fig-orange" x="283" y="58" font-size="11.5" text-anchor="middle">1</text>
    <text class="fig-orange" x="313" y="58" font-size="11.5" text-anchor="middle">1</text>
    <text class="fig-red" x="343" y="58" font-size="11.5" text-anchor="middle">1</text>
    <text class="fig-label" x="373" y="58" font-size="11.5" text-anchor="middle">0</text>
    <text class="fig-orange" x="403" y="58" font-size="11.5" text-anchor="middle">1</text>
    <text class="fig-orange" x="433" y="58" font-size="11.5" text-anchor="middle">1</text>
    <text class="fig-orange" x="463" y="58" font-size="11.5" text-anchor="middle">1</text>
    <text class="fig-orange" x="493" y="58" font-size="11.5" text-anchor="middle">1</text>
    <text class="fig-orange" x="523" y="58" font-size="11.5" text-anchor="middle">1</text>
    <text class="fig-label" x="553" y="58" font-size="11.5" text-anchor="middle">0</text>
    <text class="fig-label" x="583" y="58" font-size="11.5" text-anchor="middle">1</text>
    <text class="fig-muted" x="30" y="112" font-size="11">② 填充后载荷：</text>
    <text class="fig-label" x="162.5" y="112" font-size="11.5" text-anchor="middle">0</text>
    <text class="fig-orange" x="191.5" y="112" font-size="11.5" text-anchor="middle">1</text>
    <text class="fig-orange" x="220.5" y="112" font-size="11.5" text-anchor="middle">1</text>
    <text class="fig-orange" x="249.5" y="112" font-size="11.5" text-anchor="middle">1</text>
    <text class="fig-orange" x="278.5" y="112" font-size="11.5" text-anchor="middle">1</text>
    <text class="fig-orange" x="307.5" y="112" font-size="11.5" text-anchor="middle">1</text>
    <text class="fig-green" x="336.5" y="112" font-size="11.5" text-anchor="middle">0</text>
    <text class="fig-label" x="365.5" y="112" font-size="11.5" text-anchor="middle">1</text>
    <text class="fig-label" x="394.5" y="112" font-size="11.5" text-anchor="middle">0</text>
    <text class="fig-orange" x="423.5" y="112" font-size="11.5" text-anchor="middle">1</text>
    <text class="fig-orange" x="452.5" y="112" font-size="11.5" text-anchor="middle">1</text>
    <text class="fig-orange" x="481.5" y="112" font-size="11.5" text-anchor="middle">1</text>
    <text class="fig-orange" x="510.5" y="112" font-size="11.5" text-anchor="middle">1</text>
    <text class="fig-orange" x="539.5" y="112" font-size="11.5" text-anchor="middle">1</text>
    <text class="fig-green" x="568.5" y="112" font-size="11.5" text-anchor="middle">0</text>
    <text class="fig-label" x="597.5" y="112" font-size="11.5" text-anchor="middle">0</text>
    <text class="fig-label" x="626.5" y="112" font-size="11.5" text-anchor="middle">1</text>
    <text class="fig-accent" x="100" y="159" font-size="11" text-anchor="middle">首标志 01111110</text>
    <text class="fig-blue" x="350" y="159" font-size="11" text-anchor="middle">载荷区：011111[0]1011111[0]01（永无六连 1）</text>
    <text class="fig-accent" x="600" y="159" font-size="11" text-anchor="middle">尾标志 01111110</text>
    <text class="fig-title" x="237" y="206" font-size="11.5" text-anchor="middle">③ 接收方扫描遇到连续 5 个 1 时的分支判定</text>
    <text class="fig-blue" x="108" y="243" font-size="11.5" text-anchor="middle">扫描出连续 5 个 1</text>
    <text class="fig-green" x="332" y="226" font-size="11" text-anchor="middle">若第 6 位 = 0 → 删去该填充 0</text>
    <text class="fig-accent" x="332" y="256" font-size="11" text-anchor="middle">若第 6、7 位 = 10 → 帧边界 7E</text>
    <text class="fig-red" x="563" y="232" font-size="11" text-anchor="middle">★ 易错：原数据 111110 仍须插 0</text>
    <text class="fig-muted" x="563" y="251" font-size="10.5" text-anchor="middle">若遇连续 7 个 1（1111111）判异常</text>
  </svg>
  <figcaption>图 3-2：面向比特同步链路的零比特填充、易错边界（111110 → 1111100）与接收方逆向还原判定</figcaption>
</figure>

发送方扫描数据载荷，<strong>只要出现 5 个连续 1 就填入一个 0</strong>；标志字段 01111110 含 6 个连续 1，填充后载荷中永不出现该组合。接收方每发现 5 个连续 1 就删掉其后那个 0，还原原始数据。

> <strong>注意</strong>：此类题也可能反向考——给定组帧后的比特串，要求写出原比特串。

### 3.2 校验码、码距与检错纠错能力

<strong>为什么检错</strong>：实际信道不理想，信号传播必然失真；轻度过失不影响还原，严重失真产生<strong>误码</strong>（1 变 0、0 变 1）。校验码 = <strong>原始数据 + 校验数据</strong>，校验数据是冗余、是额外开销；误码会破坏原编码规则，从而使误码可被<strong>检测</strong>出来；若还能<strong>定位</strong>到具体哪一位出错，就能纠正。

<strong>码距</strong>（海明距离 / 汉明距离）：两个<strong>长度相同</strong>的编码对应二进制位<strong>不同</strong>的数量。编码集的码距 = 任意两个编码之间码距的<strong>最小值</strong>。

$$
d\ge e+1\quad(\text{detect }e\text{ errors})
$$

$$
d\ge 2t+1\quad(\text{correct }t\text{ errors})
$$

$$
d\ge e+t+1\quad(\text{detect }e\text{ and correct }t\text{ errors},\ e\ge t)
$$

| 码距 $d$ | 检错能力 | 纠错能力 | 典型编码 |
|---|---|---|---|
| 1 | 0（一位错都检不出） | 0 | 8 位全组合（256 个编码） |
| 2 | 1 位 | 0 | 奇校验 / 偶校验（128 个编码） |
| 3 | 2 位 | 1 位 | 标准海明码 |

<figure class="cpd-figure not-content" id="cnq-w2-fig-3">
  <svg viewBox="0 0 700 288" role="img" aria-label="三位编码海明立方体与码距检错纠错球几何模型">
    <defs>
      <marker id="cnq-w2-arr-3" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="14" width="330" height="260" rx="8"/>
    <rect class="fig-box-subtle" x="358" y="14" width="326" height="260" rx="8"/>
    <rect class="fig-box" x="372" y="42" width="298" height="72" rx="5"/>
    <rect class="fig-box" x="372" y="122" width="298" height="84" rx="5"/>
    <rect class="fig-box-orange" x="372" y="214" width="298" height="48" rx="5"/>
    <line class="fig-line-dashed" x1="150" y1="162" x2="266" y2="162"/>
    <line class="fig-line-dashed" x1="150" y1="64" x2="150" y2="162"/>
    <line class="fig-line-dashed" x1="80" y1="204" x2="150" y2="162"/>
    <line class="fig-line" x1="80" y1="106" x2="196" y2="106"/>
    <line class="fig-line" x1="196" y1="106" x2="196" y2="204"/>
    <line class="fig-line" x1="80" y1="204" x2="196" y2="204"/>
    <line class="fig-line" x1="80" y1="106" x2="80" y2="204"/>
    <line class="fig-line" x1="150" y1="64" x2="266" y2="64"/>
    <line class="fig-line" x1="266" y1="64" x2="266" y2="162"/>
    <line class="fig-line" x1="80" y1="106" x2="150" y2="64"/>
    <line class="fig-line" x1="196" y1="106" x2="266" y2="64"/>
    <line class="fig-line" x1="196" y1="204" x2="266" y2="162"/>
    <path class="fig-line" stroke="var(--sl-color-red)" stroke-width="1.5" d="M 80 204 L 188 204" marker-end="url(#cnq-w2-arr-3)"/>
    <line class="fig-line" x1="428" y1="86" x2="512" y2="86"/>
    <line class="fig-line" x1="530" y1="86" x2="614" y2="86"/>
    <circle class="fig-box-dashed" cx="420" cy="174" r="22"/>
    <circle class="fig-box-dashed" cx="622" cy="174" r="22"/>
    <line class="fig-line" x1="428" y1="174" x2="480" y2="174"/>
    <line class="fig-line-dashed" x1="496" y1="174" x2="546" y2="174"/>
    <line class="fig-line" x1="562" y1="174" x2="614" y2="174"/>
    <path class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.4" d="M 488 164 Q 456 146 428 164" marker-end="url(#cnq-w2-arr-3)"/>
    <circle class="fig-box-blue" cx="80" cy="204" r="6"/>
    <circle class="fig-box-blue" cx="196" cy="106" r="6"/>
    <circle class="fig-box-blue" cx="150" cy="64" r="6"/>
    <circle class="fig-box-blue" cx="266" cy="162" r="6"/>
    <circle class="fig-box-red" cx="196" cy="204" r="6"/>
    <circle class="fig-box" cx="80" cy="106" r="5.5"/>
    <circle class="fig-box" cx="150" cy="162" r="5.5"/>
    <circle class="fig-box" cx="266" cy="64" r="5.5"/>
    <circle class="fig-box-blue" cx="420" cy="86" r="7"/>
    <circle class="fig-box-red" cx="521" cy="86" r="6"/>
    <circle class="fig-box-blue" cx="622" cy="86" r="7"/>
    <circle class="fig-box-blue" cx="420" cy="174" r="7"/>
    <circle class="fig-box-orange" cx="488" cy="174" r="5.5"/>
    <circle class="fig-box" cx="554" cy="174" r="5.5"/>
    <circle class="fig-box-blue" cx="622" cy="174" r="7"/>
    <text class="fig-title" x="181" y="33" font-size="12" text-anchor="middle">三位编码立方体（棱 = 1 位差异，d = 2）</text>
    <text class="fig-blue" x="68" y="212" font-size="11" text-anchor="end">000 (合法)</text>
    <text class="fig-red" x="206" y="216" font-size="11">001 (错1位)</text>
    <text class="fig-muted" x="68" y="108" font-size="11" text-anchor="end">010</text>
    <text class="fig-blue" x="206" y="112" font-size="11">011 (合法)</text>
    <text class="fig-muted" x="140" y="158" font-size="10.5" text-anchor="end">100</text>
    <text class="fig-blue" x="275" y="166" font-size="11">101 (合法)</text>
    <text class="fig-blue" x="140" y="58" font-size="11" text-anchor="end">110 (合法)</text>
    <text class="fig-muted" x="275" y="66" font-size="11">111</text>
    <text class="fig-blue" x="181" y="241" font-size="10.5" text-anchor="middle">蓝点：偶校验合法码 {000,011,101,110}，两两相隔 2 条棱</text>
    <text class="fig-muted" x="181" y="259" font-size="10.5" text-anchor="middle">错 1 位必落非法点（可检）；错 2 位直达另一蓝点（漏检）</text>
    <text class="fig-title" x="521" y="33" font-size="12" text-anchor="middle">码距 d 与检错 / 纠错能力的几何判据</text>
    <text class="fig-blue" x="521" y="58" font-size="11" text-anchor="middle">检错 d ≥ e + 1（例 d=2：错点距 A、B 均为 1，可检不可纠）</text>
    <text class="fig-blue" x="420" y="105" font-size="10.5" text-anchor="middle">合法 A</text>
    <text class="fig-red" x="521" y="105" font-size="10.5" text-anchor="middle">1 位错 X</text>
    <text class="fig-blue" x="622" y="105" font-size="10.5" text-anchor="middle">合法 B</text>
    <text class="fig-green" x="521" y="138" font-size="11" text-anchor="middle">纠错 d ≥ 2t + 1（例 d=3：t=1 纠错球不重叠，最近邻归位）</text>
    <text class="fig-blue" x="420" y="198" font-size="10" text-anchor="middle">合法 A</text>
    <text class="fig-orange" x="488" y="198" font-size="10" text-anchor="middle">距A=1</text>
    <text class="fig-muted" x="554" y="198" font-size="10" text-anchor="middle">距B=1</text>
    <text class="fig-blue" x="622" y="198" font-size="10" text-anchor="middle">合法 B</text>
    <text class="fig-orange" x="521" y="233" font-size="10.5" text-anchor="middle">★ 2025 考点：d=3 纯检错可检 2 位；若用于纠 1 位错，</text>
    <text class="fig-muted" x="521" y="250" font-size="10" text-anchor="middle">一旦发生 2 位错将落入对方纠错球内，被“误纠”为错误合法码</text>
  </svg>
  <figcaption>图 3-3：三位编码海明立方体（偶校验 d=2）与检错（d≥e+1）、纠错（d≥2t+1）距离球模型</figcaption>
</figure>

由此可见：码距 1 的编码集<strong>连一位错都检不出</strong>（错后仍落在合法编码上），不能用作校验码；码距 2 可检 1 位错但检不出 2 位错、且无法纠错。想提高检错/纠错能力，只能<strong>增加冗余校验数据</strong>来增大码距，代价是开销上升——选码要结合应用环境的误码特征（单比特为主还是突发性连续误码）。

> <strong>★考点</strong>：2025 年题 34 考的是<strong>编码集码距与检错/纠错能力的关系</strong>，不是海明码本身；四个选项里常见“码距 d 可纠 d 位错”之类的陷阱。

### 3.3 奇偶校验

规则：给原始数据加 <strong>1 位</strong>校验数据，使校验码中<strong>比特 1 的个数为奇数</strong>（奇校验）或<strong>偶数</strong>（偶校验）。两类编码集的码距都是 <strong>2</strong>，因此<strong>只能检出 1 位错，检不出 2 位错，且无法定位、无法纠错</strong>。

| 位数 | 全组合编码集 | 奇（偶）校验编码集 |
|---|---|---|
| 8 位 | $2^{8}=256$ 个，$d=1$ | $2^{7}=128$ 个，$d=2$ |

<strong>硬件实现</strong>：偶校验位 = 原始数据<strong>逐位异或</strong>（“相同异或为 0、不同异或为 1”恰好对应需补的校验位）。接收方把原始数据位与校验位<strong>全部异或，结果为 1 表明收到的不是偶校验码（非法）。奇校验</strong>异或后还需<strong>取反</strong>，电路比偶校验稍麻烦——这正是海明校验中各校验组采用<strong>偶校验</strong>的原因。

> <strong>注意</strong>：接收方能判断“非法”，但<strong>不能确定哪一位错</strong>，只能检错不能纠错。

<figure class="cpd-figure not-content" id="cnq-w2-fig-4">
  <svg viewBox="0 0 700 228" role="img" aria-label="偶校验硬件异或生成判错电路与单双比特误码对比">
    <defs>
      <marker id="cnq-w2-arr-4" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="14" width="330" height="200" rx="8"/>
    <rect class="fig-box-subtle" x="358" y="14" width="326" height="200" rx="8"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.4" x1="181" y1="84" x2="181" y2="104" marker-end="url(#cnq-w2-arr-4)"/>
    <line class="fig-line" stroke-width="1.4" x1="181" y1="142" x2="181" y2="160" marker-end="url(#cnq-w2-arr-4)"/>
    <rect class="fig-box" x="30" y="44" width="302" height="40" rx="5"/>
    <rect class="fig-box-blue" x="30" y="104" width="302" height="38" rx="5"/>
    <rect class="fig-box-purple" x="30" y="160" width="302" height="42" rx="5"/>
    <rect class="fig-box-green" x="372" y="44" width="298" height="74" rx="5"/>
    <rect class="fig-box-red" x="372" y="128" width="298" height="74" rx="5"/>
    <text class="fig-title" x="181" y="33" font-size="12" text-anchor="middle">偶校验硬件异或（XOR）生成与判错流水线</text>
    <text class="fig-label" x="181" y="61" font-size="11" text-anchor="middle">原始 7 位数据：1 0 1 1 0 1 1（含 5 个 1）</text>
    <text class="fig-muted" x="181" y="77" font-size="10" text-anchor="middle">发送端计算：P = 1⊕0⊕1⊕1⊕0⊕1⊕1 = 1</text>
    <text class="fig-blue" x="181" y="127" font-size="11.5" text-anchor="middle">发送 8 位偶校验码：1 0 1 1 0 1 1 [1]（共 6 个 1）</text>
    <text class="fig-purple" x="181" y="178" font-size="11" text-anchor="middle">接收端全位异或 S = D₁⊕…⊕D₇⊕P</text>
    <text class="fig-muted" x="181" y="194" font-size="10" text-anchor="middle">S = 0 判合法；S = 1 判奇数位出错（无法定位哪一位）</text>
    <text class="fig-title" x="521" y="33" font-size="12" text-anchor="middle">检错能力验证：奇数位错 vs 偶数位错</text>
    <text class="fig-green" x="521" y="63" font-size="11.5" text-anchor="middle">① 1 位误码：收到 1 0 [0] 1 0 1 1 | 1</text>
    <text class="fig-label" x="521" y="83" font-size="10.5" text-anchor="middle">剩余 5 个 1（奇数）→ 全位异或 S = 1 ≠ 0</text>
    <text class="fig-muted" x="521" y="102" font-size="10" text-anchor="middle">成功检出误码（任何奇数位翻转均必检出）</text>
    <text class="fig-red" x="521" y="147" font-size="11.5" text-anchor="middle">② 2 位误码：收到 1 0 [0] [0] 0 1 1 | 1</text>
    <text class="fig-label" x="521" y="167" font-size="10.5" text-anchor="middle">剩余 4 个 1（仍为偶数）→ 全位异或 S = 0</text>
    <text class="fig-muted" x="521" y="186" font-size="10" text-anchor="middle">彻底漏检！突发噪声易产生连续双位错，故网络层不用</text>
  </svg>
  <figcaption>图 3-4：偶校验硬件逐位异或实现与奇数位误码检出、偶数位误码漏检对比</figcaption>
</figure>

应用场景：低成本、低复杂度、以单比特误码为主的场合——早期内存、硬盘、磁带、RS-232 低速串行通信、传感器与物联网设备；<strong>一般计算机网络几乎不用</strong>。

### 3.4 海明校验

<strong>长度关系</strong>：$m$ 位数据配 $r$ 位校验位，须满足

$$
2^{r}\ge m+r+1
$$

例：$m=7$ 解得 $r=4$，共 <strong>11 位</strong>海明码，记 $H_1\sim H_{11}$。

<strong>位置安排</strong>：$r$ 位校验位依次放在编号为 <strong>2 的幂次方</strong>的位（1、2、4、8），其余位依次放 $m$ 位数据。

<figure class="cpd-figure not-content" id="cnq-w2-fig-5">
  <svg viewBox="0 0 700 304" role="img" aria-label="11位海明码位置分布与四组偶校验位纠错定位实例">
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="202" rx="8"/>
    <rect class="fig-box-subtle" x="16" y="224" width="668" height="66" rx="8"/>
    <rect class="fig-box-accent" x="98" y="42" width="48" height="28" rx="4"/>
    <rect class="fig-box-accent" x="150" y="42" width="48" height="28" rx="4"/>
    <rect class="fig-box" x="202" y="42" width="48" height="28" rx="4"/>
    <rect class="fig-box-accent" x="254" y="42" width="48" height="28" rx="4"/>
    <rect class="fig-box" x="306" y="42" width="48" height="28" rx="4"/>
    <rect class="fig-box-red" x="358" y="42" width="48" height="28" rx="4"/>
    <rect class="fig-box" x="410" y="42" width="48" height="28" rx="4"/>
    <rect class="fig-box-accent" x="462" y="42" width="48" height="28" rx="4"/>
    <rect class="fig-box" x="514" y="42" width="48" height="28" rx="4"/>
    <rect class="fig-box" x="566" y="42" width="48" height="28" rx="4"/>
    <rect class="fig-box" x="618" y="42" width="48" height="28" rx="4"/>
    <rect class="fig-box-blue" x="98" y="80" width="48" height="24" rx="3"/>
    <rect class="fig-box-blue" x="202" y="80" width="48" height="24" rx="3"/>
    <rect class="fig-box-blue" x="306" y="80" width="48" height="24" rx="3"/>
    <rect class="fig-box-blue" x="410" y="80" width="48" height="24" rx="3"/>
    <rect class="fig-box-blue" x="514" y="80" width="48" height="24" rx="3"/>
    <rect class="fig-box-blue" x="618" y="80" width="48" height="24" rx="3"/>
    <rect class="fig-box-green" x="150" y="112" width="48" height="24" rx="3"/>
    <rect class="fig-box-green" x="202" y="112" width="48" height="24" rx="3"/>
    <rect class="fig-box-red" x="358" y="112" width="48" height="24" rx="3"/>
    <rect class="fig-box-green" x="410" y="112" width="48" height="24" rx="3"/>
    <rect class="fig-box-green" x="566" y="112" width="48" height="24" rx="3"/>
    <rect class="fig-box-green" x="618" y="112" width="48" height="24" rx="3"/>
    <rect class="fig-box-orange" x="254" y="144" width="48" height="24" rx="3"/>
    <rect class="fig-box-orange" x="306" y="144" width="48" height="24" rx="3"/>
    <rect class="fig-box-red" x="358" y="144" width="48" height="24" rx="3"/>
    <rect class="fig-box-orange" x="410" y="144" width="48" height="24" rx="3"/>
    <rect class="fig-box-purple" x="462" y="176" width="48" height="24" rx="3"/>
    <rect class="fig-box-purple" x="514" y="176" width="48" height="24" rx="3"/>
    <rect class="fig-box-purple" x="566" y="176" width="48" height="24" rx="3"/>
    <rect class="fig-box-purple" x="618" y="176" width="48" height="24" rx="3"/>
    <text class="fig-muted" x="28" y="34" font-size="10.5">位号 Hᵢ</text>
    <text class="fig-accent" x="122" y="34" font-size="10.5" text-anchor="middle">1(0001)</text>
    <text class="fig-accent" x="174" y="34" font-size="10.5" text-anchor="middle">2(0010)</text>
    <text class="fig-muted" x="226" y="34" font-size="10.5" text-anchor="middle">3(0011)</text>
    <text class="fig-accent" x="278" y="34" font-size="10.5" text-anchor="middle">4(0100)</text>
    <text class="fig-muted" x="330" y="34" font-size="10.5" text-anchor="middle">5(0101)</text>
    <text class="fig-red" x="382" y="34" font-size="10.5" text-anchor="middle">6(0110)</text>
    <text class="fig-muted" x="434" y="34" font-size="10.5" text-anchor="middle">7(0111)</text>
    <text class="fig-accent" x="486" y="34" font-size="10.5" text-anchor="middle">8(1000)</text>
    <text class="fig-muted" x="538" y="34" font-size="10.5" text-anchor="middle">9(1001)</text>
    <text class="fig-muted" x="590" y="34" font-size="10.5" text-anchor="middle">10(1010)</text>
    <text class="fig-muted" x="642" y="34" font-size="10.5" text-anchor="middle">11(1011)</text>
    <text class="fig-muted" x="28" y="60" font-size="10.5">位分配</text>
    <text class="fig-accent" x="122" y="60" font-size="11.5" text-anchor="middle">P₁=0</text>
    <text class="fig-accent" x="174" y="60" font-size="11.5" text-anchor="middle">P₂=0</text>
    <text class="fig-label" x="226" y="60" font-size="11.5" text-anchor="middle">D₁=1</text>
    <text class="fig-accent" x="278" y="60" font-size="11.5" text-anchor="middle">P₃=0</text>
    <text class="fig-label" x="330" y="60" font-size="11.5" text-anchor="middle">D₂=0</text>
    <text class="fig-red" x="382" y="60" font-size="11.5" text-anchor="middle">D₃=1*</text>
    <text class="fig-label" x="434" y="60" font-size="11.5" text-anchor="middle">D₄=1</text>
    <text class="fig-accent" x="486" y="60" font-size="11.5" text-anchor="middle">P₄=1</text>
    <text class="fig-label" x="538" y="60" font-size="11.5" text-anchor="middle">D₅=0</text>
    <text class="fig-label" x="590" y="60" font-size="11.5" text-anchor="middle">D₆=1</text>
    <text class="fig-label" x="642" y="60" font-size="11.5" text-anchor="middle">D₇=0</text>
    <text class="fig-blue" x="28" y="96" font-size="11">P₁ 组(b₀=1)</text>
    <text class="fig-blue" x="122" y="96" font-size="10.5" text-anchor="middle">选1</text>
    <text class="fig-blue" x="226" y="96" font-size="10.5" text-anchor="middle">选3</text>
    <text class="fig-blue" x="330" y="96" font-size="10.5" text-anchor="middle">选5</text>
    <text class="fig-blue" x="434" y="96" font-size="10.5" text-anchor="middle">选7</text>
    <text class="fig-blue" x="538" y="96" font-size="10.5" text-anchor="middle">选9</text>
    <text class="fig-blue" x="642" y="96" font-size="10.5" text-anchor="middle">选11</text>
    <text class="fig-green" x="28" y="128" font-size="11">P₂ 组(b₁=1)</text>
    <text class="fig-green" x="174" y="128" font-size="10.5" text-anchor="middle">选2</text>
    <text class="fig-green" x="226" y="128" font-size="10.5" text-anchor="middle">选3</text>
    <text class="fig-red" x="382" y="128" font-size="10.5" text-anchor="middle">选6(错)</text>
    <text class="fig-green" x="434" y="128" font-size="10.5" text-anchor="middle">选7</text>
    <text class="fig-green" x="590" y="128" font-size="10.5" text-anchor="middle">选10</text>
    <text class="fig-green" x="642" y="128" font-size="10.5" text-anchor="middle">选11</text>
    <text class="fig-orange" x="28" y="160" font-size="11">P₃ 组(b₂=1)</text>
    <text class="fig-orange" x="278" y="160" font-size="10.5" text-anchor="middle">选4</text>
    <text class="fig-orange" x="330" y="160" font-size="10.5" text-anchor="middle">选5</text>
    <text class="fig-red" x="382" y="160" font-size="10.5" text-anchor="middle">选6(错)</text>
    <text class="fig-orange" x="434" y="160" font-size="10.5" text-anchor="middle">选7</text>
    <text class="fig-purple" x="28" y="192" font-size="11">P₄ 组(b₃=1)</text>
    <text class="fig-purple" x="486" y="192" font-size="10.5" text-anchor="middle">选8</text>
    <text class="fig-purple" x="538" y="192" font-size="10.5" text-anchor="middle">选9</text>
    <text class="fig-purple" x="590" y="192" font-size="10.5" text-anchor="middle">选10</text>
    <text class="fig-purple" x="642" y="192" font-size="10.5" text-anchor="middle">选11</text>
    <text class="fig-title" x="350" y="244" font-size="11.5" text-anchor="middle">纠错定位算例（数据 1011010，设第 6 位 D₃ 发生误码翻转 1 → 0）</text>
    <text class="fig-label" x="350" y="264" font-size="11" text-anchor="middle">接收端四组偶校验异或得指错字：G₄=0，G₃=1，G₂=1，G₁=0 ⇒ (G₄G₃G₂G₁)₂ = (0110)₂ = 6</text>
    <text class="fig-muted" x="350" y="281" font-size="10.5" text-anchor="middle">非 0 指错字十进制值直指第 6 位出错，将 H₆ 取反即可完成 1 位自纠错</text>
  </svg>
  <figcaption>图 3-5：海明码（m=7，r=4）2ᵏ 校验位排布、分组覆盖矩阵与指错字（0110₂=6）定位纠错实例</figcaption>
</figure>

<strong>分组快速法</strong>（无需列二进制）：从 $P_i$ 所在位号出发——

- $P_1$（位 1）：选 1 个、跳 1 个、选 1 个、跳 1 个…… → 1,3,5,7,9,11
- $P_2$（位 2）：选 2 个、跳 2 个…… → 2,3,6,7,10,11
- $P_3$（位 4）：选 4 个、跳 4 个…… → 4,5,6,7
- $P_4$（位 8）：选 8 个、跳 8 个…… → 8,9,10,11

<strong>校验位的值</strong>：采用偶校验时 $P_i$ = 该组内所有位（含数据位与其它校验位）的异或。解题时应<strong>数 1 的个数</strong>而非真的做异或链：数据中 1 的个数为奇数则补 1，为偶数则补 0。

<strong>接收方校验</strong>：把收到的 11 位按同样分组做异或，四个结果按 $G_4G_3G_2G_1$ 拼成二进制数：为 <strong>0 表示无错</strong>，非 0 则<strong>该数值就是出错位号</strong>。数据位错 1 位可纠（取反即可）；校验位错 1 位同样能定位；<strong>2 位错</strong>无法定位，会误判成某个单位错。

> <strong>注意</strong>：海明码的设计目标正是“检测并纠正计算机内部传输/存储中的误码”，因此更适合放在计算机组成原理；而 408 大纲把海明校验从组成原理删除、又未明确列入计算机网络，只在“纠错码”处隐含。计算机网络实际采用的是 <strong>CRC</strong>（PPP 帧、以太网 MAC 帧、802.11 帧）与<strong>因特网校验和</strong>（IPv4 首部；UDP 用户数据报与 TCP 报文段则对<strong>整个 PDU</strong> 校验）。<strong>IPv6 取消了首部校验和</strong>，收到分组不再校验，以提高路由效率、避免逐跳校验。

<figure class="cpd-figure not-content" id="cnq-w2-fig-6">
  <svg viewBox="0 0 700 308" role="img" aria-label="CRC循环冗余校验模2异或长除法完整推演与各层校验机制对比">
    <defs>
      <marker id="cnq-w2-arr-6" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="14" width="372" height="280" rx="8"/>
    <rect class="fig-box-subtle" x="400" y="14" width="284" height="280" rx="8"/>
    <rect class="fig-box" x="28" y="42" width="348" height="210" rx="5"/>
    <rect class="fig-box-green" x="28" y="258" width="348" height="28" rx="4"/>
    <line class="fig-line" stroke-width="1.4" x1="106" y1="84" x2="294" y2="84"/>
    <path class="fig-line" stroke-width="1.4" d="M 106 84 Q 100 94 94 102"/>
    <line class="fig-divider" x1="110" y1="120" x2="206" y2="120"/>
    <line class="fig-divider" x1="126" y1="154" x2="222" y2="154"/>
    <line class="fig-divider" x1="158" y1="188" x2="254" y2="188"/>
    <line class="fig-divider" x1="190" y1="222" x2="290" y2="222"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.3" x1="542" y1="98" x2="542" y2="112" marker-end="url(#cnq-w2-arr-6)"/>
    <rect class="fig-box-blue" x="414" y="42" width="256" height="56" rx="5"/>
    <rect class="fig-box-accent" x="414" y="112" width="256" height="58" rx="5"/>
    <rect class="fig-box-orange" x="414" y="180" width="256" height="52" rx="5"/>
    <rect class="fig-box-purple" x="414" y="240" width="256" height="44" rx="5"/>
    <text class="fig-title" x="202" y="33" font-size="12" text-anchor="middle">CRC 模 2 异或长除法（M=101001，G(x)=x³+x²+1 → P=1101）</text>
    <text class="fig-muted" x="116" y="77" font-size="11.5" letter-spacing="5">110101</text>
    <text class="fig-muted" x="234" y="77" font-size="10">← 商（丢弃不用）</text>
    <text class="fig-accent" x="52" y="100" font-size="11.5" letter-spacing="3">1101</text>
    <text class="fig-label" x="116" y="100" font-size="11.5" letter-spacing="5">101001</text>
    <text class="fig-orange" x="218" y="100" font-size="11.5" letter-spacing="5">000</text>
    <text class="fig-muted" x="278" y="100" font-size="10">← 补 r=3 个 0</text>
    <text class="fig-muted" x="116" y="115" font-size="11.5" letter-spacing="5">1101</text>
    <text class="fig-label" x="133" y="134" font-size="11.5" letter-spacing="5">1110</text>
    <text class="fig-muted" x="133" y="149" font-size="11.5" letter-spacing="5">1101</text>
    <text class="fig-label" x="167" y="168" font-size="11.5" letter-spacing="5">1110</text>
    <text class="fig-muted" x="250" y="168" font-size="10">← 首位 0 商 0 跳过</text>
    <text class="fig-muted" x="167" y="183" font-size="11.5" letter-spacing="5">1101</text>
    <text class="fig-label" x="201" y="202" font-size="11.5" letter-spacing="5">1100</text>
    <text class="fig-muted" x="201" y="217" font-size="11.5" letter-spacing="5">1101</text>
    <text class="fig-green" x="218" y="239" font-size="12" letter-spacing="5">001</text>
    <text class="fig-green" x="276" y="239" font-size="10.5">← 余数 FCS (r=3位)</text>
    <text class="fig-green" x="202" y="276" font-size="11" text-anchor="middle">发送帧：101001 [001] ｜ 接收方除以 1101 余 000 判无误码</text>
    <text class="fig-title" x="542" y="33" font-size="12" text-anchor="middle">网络各层检错机制核心考点对比</text>
    <text class="fig-blue" x="542" y="61" font-size="11" text-anchor="middle">① 模 2 运算铁律：加法不进位、减法不借位</text>
    <text class="fig-muted" x="542" y="79" font-size="10" text-anchor="middle">本质即逐位异或（XOR）；余数位数必比除数少 1 位</text>
    <text class="fig-accent" x="542" y="131" font-size="11" text-anchor="middle">② 链路层 CRC（PPP / 802.3 / 802.11 帧尾 FCS）</text>
    <text class="fig-muted" x="542" y="149" font-size="10" text-anchor="middle">仅实现“无比特差错接受”（检错丢弃），不负责纠错或重传！</text>
    <text class="fig-orange" x="542" y="199" font-size="11" text-anchor="middle">③ 网络层校验：IPv4 仅校首部；IPv6 彻底取消</text>
    <text class="fig-muted" x="542" y="217" font-size="10" text-anchor="middle">IPv6 省去路由器逐跳重算首部校验和，大幅提升转发速率</text>
    <text class="fig-purple" x="542" y="258" font-size="11" text-anchor="middle">④ 传输层校验：UDP / TCP 校验首部 + 数据载荷</text>
    <text class="fig-muted" x="542" y="275" font-size="10" text-anchor="middle">辅以伪首部防误投递，实现端到端完整性兜底</text>
  </svg>
  <figcaption>图 3-6：CRC 循环冗余校验模 2 异或长除法求 FCS 余数（001）推演与跨层校验范围对比</figcaption>
</figure>

## 第4章 数据链路层：可靠传输机制

### 4.1 连接、可靠性与四类服务

- <strong>连接</strong>：数据传输前为高效或可靠传输而建立的<strong>逻辑关联或状态</strong>（如 TCP 三报文握手）。
- <strong>可靠</strong>：确保数据传输本身的<strong>正确性、完整性、有序性</strong>。

| 服务类型 | 典型协议 |
|---|---|
| 面向连接的<strong>可靠</strong>传输 | TCP；网络层虚电路服务 |
| 无连接的<strong>不可靠</strong>传输 | UDP、IP（IPv4/IPv6）、802.3 以太网 MAC |
| 面向连接的<strong>不可靠</strong>传输 | PPP；应用层某些实时应用 |
| 无连接的<strong>可靠</strong>传输 | 802.11 无线局域网 MAC |

<strong>为什么有线不可靠、无线可靠</strong>：有线链路误码率低，为减小开销不要求链路层提供可靠传输，出了差错交给上层（TCP）处理；无线链路易受干扰、误码率高，故一般要求链路层自己保证。

> <strong>注意</strong>：以太网 CSMA/CD 会重传遭遇碰撞的帧，但这只是为了让接收方最终收到<strong>没有碰撞的帧</strong>，与“确认 + 超时重传”意义上的可靠传输<strong>不是一回事</strong>。

<figure class="cpd-figure not-content" id="cnq-w2-fig-7">
  <svg viewBox="0 0 700 244" role="img" aria-label="连接与可靠性四象限服务矩阵及有线无线链路层可靠性对比">
    <rect class="fig-box-subtle" x="16" y="14" width="356" height="216" rx="8"/>
    <rect class="fig-box-subtle" x="384" y="14" width="300" height="216" rx="8"/>
    <rect class="fig-box-green" x="96" y="64" width="130" height="68" rx="5"/>
    <rect class="fig-box-orange" x="232" y="64" width="128" height="68" rx="5"/>
    <rect class="fig-box-blue" x="96" y="140" width="130" height="68" rx="5"/>
    <rect class="fig-box" x="232" y="140" width="128" height="68" rx="5"/>
    <rect class="fig-box" x="396" y="42" width="276" height="52" rx="5"/>
    <rect class="fig-box-blue" x="396" y="102" width="276" height="52" rx="5"/>
    <rect class="fig-box-red" x="396" y="162" width="276" height="56" rx="5"/>
    <text class="fig-title" x="194" y="33" font-size="12" text-anchor="middle">连接性 × 可靠性 四类传输服务象限</text>
    <text class="fig-green" x="161" y="54" font-size="10.5" text-anchor="middle">可靠传输（确认+重传）</text>
    <text class="fig-muted" x="296" y="54" font-size="10.5" text-anchor="middle">不可靠传输（仅检错）</text>
    <text class="fig-label" x="58" y="96" font-size="11" text-anchor="middle">面向</text>
    <text class="fig-label" x="58" y="112" font-size="11" text-anchor="middle">连接</text>
    <text class="fig-label" x="58" y="178" font-size="11" text-anchor="middle">无连接</text>
    <text class="fig-green" x="161" y="92" font-size="11.5" text-anchor="middle">TCP 协议</text>
    <text class="fig-muted" x="161" y="112" font-size="10" text-anchor="middle">网络层虚电路 / HDLC</text>
    <text class="fig-orange" x="296" y="92" font-size="11.5" text-anchor="middle">PPP 协议 ★</text>
    <text class="fig-muted" x="296" y="112" font-size="10" text-anchor="middle">LCP/NCP 建连但无序号确认</text>
    <text class="fig-blue" x="161" y="168" font-size="11.5" text-anchor="middle">802.11 无线 MAC ★</text>
    <text class="fig-muted" x="161" y="188" font-size="10" text-anchor="middle">帧间 SIFS + 逐帧 ACK</text>
    <text class="fig-label" x="296" y="168" font-size="11.5" text-anchor="middle">UDP / IP / 802.3</text>
    <text class="fig-muted" x="296" y="188" font-size="10" text-anchor="middle">有线以太网 MAC 尽最大努力</text>
    <text class="fig-title" x="534" y="33" font-size="12" text-anchor="middle">有线 vs 无线链路层设计逻辑与高频陷阱</text>
    <text class="fig-label" x="534" y="61" font-size="11" text-anchor="middle">有线 802.3：信道误码率极低 → 省去链路层确认</text>
    <text class="fig-muted" x="534" y="79" font-size="10" text-anchor="middle">偶发丢帧交由高层 TCP 重传，换取极简帧结构与高吞吐</text>
    <text class="fig-blue" x="534" y="121" font-size="11" text-anchor="middle">无线 802.11：易衰减受干扰 → 链路层当场确认重传</text>
    <text class="fig-muted" x="534" y="139" font-size="10" text-anchor="middle">避免单跳无线丢包触发端到端长路径 TCP 超时降速</text>
    <text class="fig-red" x="534" y="182" font-size="11" text-anchor="middle">★ 避坑：CSMA/CD 碰撞重传 ≠ 可靠传输机制</text>
    <text class="fig-muted" x="534" y="201" font-size="10" text-anchor="middle">碰撞重传仅管发送竞争；收端遇 CRC 误码帧直接丢弃不通知</text>
  </svg>
  <figcaption>图 4-1：连接性与可靠性四象限协议归属（注意 PPP 与 802.11 特例）及有线/无线链路差异</figcaption>
</figure>

### 4.2 窗口尺寸与编号比特数

设用 $n$ 个比特给帧编号，发送窗口 $W_T$、接收窗口 $W_R$ 必须满足

$$
W_{T}+W_{R}\le 2^{n}
$$

这是为了使接收方能够<strong>分辨新旧数据帧</strong>（否则新旧帧序号重叠，接收方无法判断是重传还是新帧）。

| 协议 | $n$ | $W_T$ | $W_R$ |
|---|---|---|---|
| 停止等待 SAW | 1 | 1 | 1 |
| 回退 N 帧 GBN | $\ge 2$ | $1\le W_T\le 2^{n}-1$ | 1 |
| 选择重传 SR | $\ge 2$ | $2\le W_T\le 2^{n}-1$ | $2\le W_R\le W_T$，且 $W_T+W_R\le 2^n$ |

SR 中 $W_R$ 不必超过 $W_T$（超过也没有作用），且当 $W_R$ 取最大值时 $W_T$ 的最大值为 $2^{n-1}$（此时 $W_T=W_R=2^{n-1}$）；若 $W_R$ 取较小值，$W_T$ 的上限可以更大。

> <strong>★考点：题目给“滑动窗口协议”而未点名时，看选项值即可判定——SAW 与 GBN 的接收窗口都是 1，只有 SR 的 $W_R>1$</strong>。

> <strong>注意</strong>：上述窗口内的序号是<strong>帧的编号</strong>（整个 PDU 的编号）；TCP 面向字节流，窗口内是<strong>字节序号</strong>，教学与考试中常以 MSS 为单位讨论，而 MSS 是 TCP 报文段<strong>数据载荷</strong>的最大长度，不是整个报文段的长度。

<figure class="cpd-figure not-content" id="cnq-w2-fig-8">
  <svg viewBox="0 0 700 292" role="img" aria-label="GBN与SR滑动窗口上限约束Wt加Wr小于等于2的n次方反证推演">
    <defs>
      <marker id="cnq-w2-arr-8" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="14" width="332" height="264" rx="8"/>
    <rect class="fig-box-subtle" x="360" y="14" width="324" height="264" rx="8"/>
    <line class="fig-line" stroke="var(--sl-color-red)" stroke-width="1.3" x1="182" y1="92" x2="182" y2="108" marker-end="url(#cnq-w2-arr-8)"/>
    <line class="fig-line" stroke="var(--sl-color-red)" stroke-width="1.3" x1="182" y1="154" x2="182" y2="170" marker-end="url(#cnq-w2-arr-8)"/>
    <rect class="fig-box-orange" x="30" y="42" width="304" height="50" rx="5"/>
    <rect class="fig-box" x="30" y="108" width="304" height="46" rx="5"/>
    <rect class="fig-box-red" x="30" y="170" width="304" height="52" rx="5"/>
    <rect class="fig-box-green" x="30" y="230" width="304" height="36" rx="5"/>
    <rect class="fig-box-green" x="374" y="42" width="296" height="104" rx="5"/>
    <rect class="fig-box-red" x="374" y="158" width="296" height="108" rx="5"/>
    <text class="fig-title" x="182" y="33" font-size="12" text-anchor="middle">GBN 为何要求 Wt ≤ 2ⁿ − 1？（设 n=2，序号 0～3）</text>
    <text class="fig-orange" x="182" y="61" font-size="11" text-anchor="middle">① 反证：若违规令 Wt = 2² = 4（发窗 [0,1,2,3]）</text>
    <text class="fig-muted" x="182" y="79" font-size="10" text-anchor="middle">发送方一口气连发 F0, F1, F2, F3，收方全部按序收下</text>
    <text class="fig-label" x="182" y="127" font-size="11" text-anchor="middle">② 收方 Wr=1 依次滑过 0→1→2→3，回绕至期待新 [0]</text>
    <text class="fig-muted" x="182" y="144" font-size="10" text-anchor="middle">不幸反向信道 ACK0 ～ ACK3 全部丢失，发窗仍停在 [0,1,2,3]</text>
    <text class="fig-red" x="182" y="190" font-size="11" text-anchor="middle">③ 发方超时重传“旧 F0”，恰落入收方新窗 [0]！</text>
    <text class="fig-muted" x="182" y="209" font-size="10" text-anchor="middle">收方无法区分重传旧帧与下一轮新帧 → 重复上交致命错误</text>
    <text class="fig-green" x="182" y="252" font-size="10.5" text-anchor="middle">✓ 若 Wt=3（发 0,1,2），收窗停在 [3]，重传旧 0 必被识破丢弃</text>
    <text class="fig-title" x="522" y="33" font-size="12" text-anchor="middle">SR 约束 Wt + Wr ≤ 2ⁿ（n=2 对称上限 Wt=Wr=2）</text>
    <text class="fig-green" x="522" y="62" font-size="11" text-anchor="middle">✓ 合规配置：Wt = 2，Wr = 2（2 + 2 ≤ 4）</text>
    <text class="fig-label" x="522" y="83" font-size="10.5" text-anchor="middle">发窗 [0, 1] 全到达 → 收窗前移至 [2, 3]</text>
    <text class="fig-muted" x="522" y="102" font-size="10" text-anchor="middle">即便 ACK0、ACK1 全丢，重传的旧 F0、F1 序号不在</text>
    <text class="fig-muted" x="522" y="119" font-size="10" text-anchor="middle">新收窗 [2, 3] 内，接收方安全回 ACK 并丢弃重复帧</text>
    <text class="fig-red" x="522" y="178" font-size="11" text-anchor="middle">✗ 越界配置：若令 Wt = 3，Wr = 2（3 + 2 = 5 &gt; 4）</text>
    <text class="fig-label" x="522" y="199" font-size="10.5" text-anchor="middle">连发 F0, F1, F2 全到达 → 收窗滑 3 格至 [3, 0]</text>
    <text class="fig-muted" x="522" y="219" font-size="10" text-anchor="middle">若 3 个 ACK 全丢，发送方超时重传“旧 F0”</text>
    <text class="fig-red" x="522" y="238" font-size="10.5" text-anchor="middle">旧 F0 落入当前收窗 [3, 0] 第二格 → 误当新帧缓存！</text>
    <text class="fig-muted" x="522" y="255" font-size="10" text-anchor="middle">通用死记公式：Wt + Wr ≤ 2ⁿ（GBN 代入 Wr=1 即得）</text>
  </svg>
  <figcaption>图 4-2：滑动窗口大小与序号空间约束（Wt + Wr ≤ 2ⁿ）的新旧帧序号重叠反证推演</figcaption>
</figure>

### 4.3 三种协议的发送与接收操作

<figure class="cpd-figure not-content" id="cnq-w2-fig-9">
  <svg viewBox="0 0 700 296" role="img" aria-label="SAW与GBN与SR三种可靠传输协议发送窗口与接收窗口结构对比">
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="268" rx="8"/>
    <line class="fig-divider" x1="28" y1="106" x2="672" y2="106"/>
    <line class="fig-divider" x1="28" y1="184" x2="672" y2="184"/>
    <rect class="fig-box-orange" x="172" y="42" width="56" height="24" rx="3"/>
    <rect class="fig-box" x="234" y="42" width="56" height="24" rx="3"/>
    <rect class="fig-box" x="296" y="42" width="56" height="24" rx="3"/>
    <rect class="fig-box" x="358" y="42" width="56" height="24" rx="3"/>
    <rect class="fig-box" x="420" y="42" width="56" height="24" rx="3"/>
    <rect class="fig-box" x="482" y="42" width="56" height="24" rx="3"/>
    <rect class="fig-box" x="544" y="42" width="56" height="24" rx="3"/>
    <rect class="fig-box" x="606" y="42" width="56" height="24" rx="3"/>
    <rect class="fig-box-green" x="172" y="72" width="56" height="24" rx="3"/>
    <rect class="fig-box" x="234" y="72" width="56" height="24" rx="3"/>
    <rect class="fig-box" x="296" y="72" width="56" height="24" rx="3"/>
    <rect class="fig-box" x="358" y="72" width="56" height="24" rx="3"/>
    <rect class="fig-box" x="420" y="72" width="56" height="24" rx="3"/>
    <rect class="fig-box" x="482" y="72" width="56" height="24" rx="3"/>
    <rect class="fig-box" x="544" y="72" width="56" height="24" rx="3"/>
    <rect class="fig-box" x="606" y="72" width="56" height="24" rx="3"/>
    <rect class="fig-box" x="172" y="118" width="56" height="24" rx="3"/>
    <rect class="fig-box-orange" x="234" y="118" width="56" height="24" rx="3"/>
    <rect class="fig-box-orange" x="296" y="118" width="56" height="24" rx="3"/>
    <rect class="fig-box-orange" x="358" y="118" width="56" height="24" rx="3"/>
    <rect class="fig-box-blue" x="420" y="118" width="56" height="24" rx="3"/>
    <rect class="fig-box-blue" x="482" y="118" width="56" height="24" rx="3"/>
    <rect class="fig-box" x="544" y="118" width="56" height="24" rx="3"/>
    <rect class="fig-box" x="606" y="118" width="56" height="24" rx="3"/>
    <rect class="fig-box-dashed" x="231" y="115" width="310" height="30" rx="4"/>
    <rect class="fig-box" x="172" y="150" width="56" height="24" rx="3"/>
    <rect class="fig-box-green" x="234" y="150" width="56" height="24" rx="3"/>
    <rect class="fig-box" x="296" y="150" width="56" height="24" rx="3"/>
    <rect class="fig-box" x="358" y="150" width="56" height="24" rx="3"/>
    <rect class="fig-box" x="420" y="150" width="56" height="24" rx="3"/>
    <rect class="fig-box" x="482" y="150" width="56" height="24" rx="3"/>
    <rect class="fig-box" x="544" y="150" width="56" height="24" rx="3"/>
    <rect class="fig-box" x="606" y="150" width="56" height="24" rx="3"/>
    <rect class="fig-box" x="172" y="196" width="56" height="24" rx="3"/>
    <rect class="fig-box-orange" x="234" y="196" width="56" height="24" rx="3"/>
    <rect class="fig-box-green" x="296" y="196" width="56" height="24" rx="3"/>
    <rect class="fig-box-orange" x="358" y="196" width="56" height="24" rx="3"/>
    <rect class="fig-box-blue" x="420" y="196" width="56" height="24" rx="3"/>
    <rect class="fig-box" x="482" y="196" width="56" height="24" rx="3"/>
    <rect class="fig-box" x="544" y="196" width="56" height="24" rx="3"/>
    <rect class="fig-box" x="606" y="196" width="56" height="24" rx="3"/>
    <rect class="fig-box-dashed" x="231" y="193" width="248" height="30" rx="4"/>
    <rect class="fig-box" x="172" y="228" width="56" height="24" rx="3"/>
    <rect class="fig-box-red" x="234" y="228" width="56" height="24" rx="3"/>
    <rect class="fig-box-purple" x="296" y="228" width="56" height="24" rx="3"/>
    <rect class="fig-box-green" x="358" y="228" width="56" height="24" rx="3"/>
    <rect class="fig-box-green" x="420" y="228" width="56" height="24" rx="3"/>
    <rect class="fig-box" x="482" y="228" width="56" height="24" rx="3"/>
    <rect class="fig-box" x="544" y="228" width="56" height="24" rx="3"/>
    <rect class="fig-box" x="606" y="228" width="56" height="24" rx="3"/>
    <rect class="fig-box-dashed" x="231" y="225" width="248" height="30" rx="4"/>
    <text class="fig-title" x="350" y="31" font-size="12" text-anchor="middle">SAW / GBN / SR 发送窗口（Wt）与接收窗口（Wr）状态对比（n=3 序号空间 0～7）</text>
    <text class="fig-label" x="28" y="58" font-size="11">SAW 发 (Wt=1)</text>
    <text class="fig-label" x="28" y="88" font-size="11">SAW 收 (Wr=1)</text>
    <text class="fig-orange" x="200" y="58" font-size="10.5" text-anchor="middle">0 待ACK</text>
    <text class="fig-muted" x="262" y="58" font-size="10.5" text-anchor="middle">1</text>
    <text class="fig-muted" x="324" y="58" font-size="10.5" text-anchor="middle">0</text>
    <text class="fig-muted" x="386" y="58" font-size="10.5" text-anchor="middle">1</text>
    <text class="fig-muted" x="448" y="58" font-size="10.5" text-anchor="middle">0</text>
    <text class="fig-muted" x="510" y="58" font-size="10.5" text-anchor="middle">1</text>
    <text class="fig-muted" x="572" y="58" font-size="10.5" text-anchor="middle">0</text>
    <text class="fig-muted" x="634" y="58" font-size="10.5" text-anchor="middle">1</text>
    <text class="fig-green" x="200" y="88" font-size="10.5" text-anchor="middle">0 期待</text>
    <text class="fig-muted" x="262" y="88" font-size="10.5" text-anchor="middle">1</text>
    <text class="fig-muted" x="324" y="88" font-size="10.5" text-anchor="middle">0</text>
    <text class="fig-muted" x="386" y="88" font-size="10.5" text-anchor="middle">1</text>
    <text class="fig-muted" x="448" y="88" font-size="10.5" text-anchor="middle">0</text>
    <text class="fig-muted" x="510" y="88" font-size="10.5" text-anchor="middle">1</text>
    <text class="fig-muted" x="572" y="88" font-size="10.5" text-anchor="middle">0</text>
    <text class="fig-muted" x="634" y="88" font-size="10.5" text-anchor="middle">1</text>
    <text class="fig-label" x="28" y="134" font-size="11">GBN 发 (Wt=5)</text>
    <text class="fig-label" x="28" y="166" font-size="11">GBN 收 (Wr=1)</text>
    <text class="fig-muted" x="200" y="134" font-size="10" text-anchor="middle">0 已确认</text>
    <text class="fig-orange" x="262" y="134" font-size="10.5" text-anchor="middle">1 已发</text>
    <text class="fig-orange" x="324" y="134" font-size="10.5" text-anchor="middle">2 已发</text>
    <text class="fig-orange" x="386" y="134" font-size="10.5" text-anchor="middle">3 已发</text>
    <text class="fig-blue" x="448" y="134" font-size="10.5" text-anchor="middle">4 可发</text>
    <text class="fig-blue" x="510" y="134" font-size="10.5" text-anchor="middle">5 可发</text>
    <text class="fig-muted" x="572" y="134" font-size="10.5" text-anchor="middle">6 禁发</text>
    <text class="fig-muted" x="634" y="134" font-size="10.5" text-anchor="middle">7 禁发</text>
    <text class="fig-muted" x="200" y="166" font-size="10" text-anchor="middle">0 已交</text>
    <text class="fig-green" x="262" y="166" font-size="10.5" text-anchor="middle">1 期待</text>
    <text class="fig-muted" x="324" y="166" font-size="10" text-anchor="middle">2 拒收</text>
    <text class="fig-muted" x="386" y="166" font-size="10" text-anchor="middle">3 拒收</text>
    <text class="fig-muted" x="448" y="166" font-size="10.5" text-anchor="middle">4</text>
    <text class="fig-muted" x="510" y="166" font-size="10.5" text-anchor="middle">5</text>
    <text class="fig-muted" x="572" y="166" font-size="10.5" text-anchor="middle">6</text>
    <text class="fig-muted" x="634" y="166" font-size="10.5" text-anchor="middle">7</text>
    <text class="fig-label" x="28" y="212" font-size="11">SR 发 (Wt=4)</text>
    <text class="fig-label" x="28" y="244" font-size="11">SR 收 (Wr=4)</text>
    <text class="fig-muted" x="200" y="212" font-size="10" text-anchor="middle">0 已确认</text>
    <text class="fig-orange" x="262" y="212" font-size="10.5" text-anchor="middle">1 待ACK</text>
    <text class="fig-green" x="324" y="212" font-size="10" text-anchor="middle">2 已ACK</text>
    <text class="fig-orange" x="386" y="212" font-size="10.5" text-anchor="middle">3 待ACK</text>
    <text class="fig-blue" x="448" y="212" font-size="10.5" text-anchor="middle">4 可发</text>
    <text class="fig-muted" x="510" y="212" font-size="10.5" text-anchor="middle">5 禁发</text>
    <text class="fig-muted" x="572" y="212" font-size="10.5" text-anchor="middle">6</text>
    <text class="fig-muted" x="634" y="212" font-size="10.5" text-anchor="middle">7</text>
    <text class="fig-muted" x="200" y="244" font-size="10" text-anchor="middle">0 已交</text>
    <text class="fig-red" x="262" y="244" font-size="10.5" text-anchor="middle">1 等到达</text>
    <text class="fig-purple" x="324" y="244" font-size="10" text-anchor="middle">2 已缓存</text>
    <text class="fig-green" x="386" y="244" font-size="10.5" text-anchor="middle">3 可收</text>
    <text class="fig-green" x="448" y="244" font-size="10.5" text-anchor="middle">4 可收</text>
    <text class="fig-muted" x="510" y="244" font-size="10.5" text-anchor="middle">5</text>
    <text class="fig-muted" x="572" y="244" font-size="10.5" text-anchor="middle">6</text>
    <text class="fig-muted" x="634" y="244" font-size="10.5" text-anchor="middle">7</text>
    <text class="fig-muted" x="350" y="272" font-size="10.5" text-anchor="middle">★ 判题秒杀：GBN 的 Wr 恒为 1（乱序必丢）；仅 SR 的 Wr &gt; 1，支持缓存乱序帧（如 2 号）并单独标记 ACK</text>
  </svg>
  <figcaption>图 4-3：停止等待（SAW）、回退 N 帧（GBN）与选择重传（SR）滑动窗口内部状态对比</figcaption>
</figure>

<strong>SAW 停止等待</strong>：发一个帧就停下来等确认，收到确认才发下一个，否则超时重传。接收方收到无误码帧即回确认。

<strong>GBN 回退 N 帧：发送方在未收确认时可把窗口内的多个帧全部发出；只有收到对某已发帧的确认，窗口才前移到位。窗口内某帧超时重传时，该帧之后所有已发送但未确认的帧必须全部重传</strong>——这就是“回退 N 帧”名字的由来。接收方只接收<strong>序号落在接收窗口内且无误码</strong>的帧，收下即前移一格并回确认；为减少开销采用<strong>累计确认</strong>（连续收到多个按序无误码帧后，只对最后一个回确认）。

<strong>SR 选择重传</strong>：发送方可连续发送窗口内多个帧；只有<strong>按序</strong>收到确认，窗口才前移。收到未按序的确认要<strong>记录（防止其对应帧被超时误重传），但窗口不动；谁超时就重传谁</strong>。接收方可接收<strong>未按序到达但无误码且序号落在接收窗口内</strong>的帧并缓存；采用<strong>逐一确认</strong>（不累计）；只有按序收下帧后接收窗口才前移。

> <strong>注意</strong>：GBN 的接收窗口为 1，故落入接收窗口的帧必定按序；SR 的接收窗口大于 1，才有“乱序到达也收下”的能力——这是增大接收窗口带来的直接好处。

<figure class="cpd-figure not-content" id="cnq-w2-fig-10">
  <svg viewBox="0 0 700 314" role="img" aria-label="GBN回退N帧与SR选择重传在1号帧丢失时的时空交互与重传行为对比">
    <defs>
      <marker id="cnq-w2-arr-10" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="14" width="332" height="286" rx="8"/>
    <rect class="fig-box-subtle" x="360" y="14" width="324" height="286" rx="8"/>
    <line class="fig-line" stroke-width="1.5" x1="74" y1="54" x2="74" y2="262"/>
    <line class="fig-line" stroke-width="1.5" x1="280" y1="54" x2="280" y2="262"/>
    <line class="fig-line" stroke-width="1.5" x1="418" y1="54" x2="418" y2="262"/>
    <line class="fig-line" stroke-width="1.5" x1="624" y1="54" x2="624" y2="262"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.3" x1="74" y1="62" x2="278" y2="84" marker-end="url(#cnq-w2-arr-10)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.2" x1="280" y1="88" x2="76" y2="108" marker-end="url(#cnq-w2-arr-10)"/>
    <line class="fig-line-dashed" stroke="var(--sl-color-red)" stroke-width="1.4" x1="74" y1="80" x2="178" y2="92"/>
    <line class="fig-line" stroke="var(--sl-color-orange)" stroke-width="1.3" x1="74" y1="98" x2="278" y2="120" marker-end="url(#cnq-w2-arr-10)"/>
    <line class="fig-line-dashed" stroke="var(--sl-color-green)" stroke-width="1.1" x1="280" y1="124" x2="76" y2="144" marker-end="url(#cnq-w2-arr-10)"/>
    <line class="fig-line" stroke="var(--sl-color-orange)" stroke-width="1.3" x1="74" y1="116" x2="278" y2="138" marker-end="url(#cnq-w2-arr-10)"/>
    <line class="fig-line" stroke="var(--sl-color-red)" stroke-width="1.4" x1="74" y1="172" x2="278" y2="194" marker-end="url(#cnq-w2-arr-10)"/>
    <line class="fig-line" stroke="var(--sl-color-red)" stroke-width="1.4" x1="74" y1="190" x2="278" y2="212" marker-end="url(#cnq-w2-arr-10)"/>
    <line class="fig-line" stroke="var(--sl-color-red)" stroke-width="1.4" x1="74" y1="208" x2="278" y2="230" marker-end="url(#cnq-w2-arr-10)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.3" x1="280" y1="234" x2="76" y2="254" marker-end="url(#cnq-w2-arr-10)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.3" x1="418" y1="62" x2="622" y2="84" marker-end="url(#cnq-w2-arr-10)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.2" x1="624" y1="88" x2="420" y2="108" marker-end="url(#cnq-w2-arr-10)"/>
    <line class="fig-line-dashed" stroke="var(--sl-color-red)" stroke-width="1.4" x1="418" y1="80" x2="522" y2="92"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.3" x1="418" y1="98" x2="622" y2="120" marker-end="url(#cnq-w2-arr-10)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.2" x1="624" y1="124" x2="420" y2="144" marker-end="url(#cnq-w2-arr-10)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.3" x1="418" y1="116" x2="622" y2="138" marker-end="url(#cnq-w2-arr-10)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.2" x1="624" y1="142" x2="420" y2="162" marker-end="url(#cnq-w2-arr-10)"/>
    <line class="fig-line" stroke="var(--sl-color-red)" stroke-width="1.4" x1="418" y1="184" x2="622" y2="206" marker-end="url(#cnq-w2-arr-10)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.3" x1="624" y1="214" x2="420" y2="236" marker-end="url(#cnq-w2-arr-10)"/>
    <rect class="fig-box-red" x="166" y="82" width="44" height="18" rx="3"/>
    <rect class="fig-box-orange" x="26" y="156" width="56" height="18" rx="3"/>
    <rect class="fig-box" x="136" y="170" width="90" height="18" rx="3"/>
    <rect class="fig-box-red" x="510" y="82" width="44" height="18" rx="3"/>
    <rect class="fig-box-orange" x="368" y="170" width="56" height="18" rx="3"/>
    <rect class="fig-box" x="480" y="182" width="84" height="18" rx="3"/>
    <rect class="fig-box-green" x="488" y="216" width="66" height="18" rx="3"/>
    <rect class="fig-box" x="216" y="118" width="52" height="15" rx="3"/>
    <rect class="fig-box" x="112" y="135" width="56" height="15" rx="3"/>
    <rect class="fig-box" x="136" y="241" width="90" height="16" rx="3"/>
    <rect class="fig-box" x="438" y="129" width="36" height="14" rx="3"/>
    <rect class="fig-box" x="438" y="149" width="36" height="14" rx="3"/>
    <text class="fig-title" x="182" y="32" font-size="12" text-anchor="middle">GBN 回退 N 帧（累计确认 + 连坐重传）</text>
    <text class="fig-label" x="74" y="48" font-size="10.5" text-anchor="middle">发送方</text>
    <text class="fig-label" x="280" y="48" font-size="10.5" text-anchor="middle">接收方 (Wr=1)</text>
    <text class="fig-blue" x="134" y="66" font-size="10">F0</text>
    <text class="fig-red" x="188" y="95" font-size="9.5" text-anchor="middle">F1 丢</text>
    <text class="fig-orange" x="220" y="111" font-size="9.5">F2(丢弃)</text>
    <text class="fig-orange" x="220" y="129" font-size="9.5">F3(丢弃)</text>
    <text class="fig-green" x="116" y="146" font-size="9.5">重复 ACK0</text>
    <text class="fig-orange" x="54" y="169" font-size="9.5" text-anchor="middle">F1超时</text>
    <text class="fig-red" x="181" y="183" font-size="9.5" text-anchor="middle">回退重传 F1～F3</text>
    <text class="fig-green" x="181" y="253" font-size="10" text-anchor="middle">累计确认 ACK3</text>
    <text class="fig-muted" x="182" y="284" font-size="10" text-anchor="middle">F2、F3 虽无误码到达仍被丢弃，超时须回退重传 3 帧</text>
    <text class="fig-title" x="522" y="32" font-size="12" text-anchor="middle">SR 选择重传（逐一确认 + 乱序缓存）</text>
    <text class="fig-label" x="418" y="48" font-size="10.5" text-anchor="middle">发送方</text>
    <text class="fig-label" x="624" y="48" font-size="10.5" text-anchor="middle">接收方 (Wr=4)</text>
    <text class="fig-blue" x="478" y="66" font-size="10">F0</text>
    <text class="fig-red" x="532" y="95" font-size="9.5" text-anchor="middle">F1 丢</text>
    <text class="fig-purple" x="630" y="122" font-size="9.5">缓存F2</text>
    <text class="fig-purple" x="630" y="140" font-size="9.5">缓存F3</text>
    <text class="fig-green" x="442" y="140" font-size="9.5">ACK2</text>
    <text class="fig-green" x="442" y="160" font-size="9.5">ACK3</text>
    <text class="fig-orange" x="396" y="183" font-size="9.5" text-anchor="middle">F1超时</text>
    <text class="fig-red" x="522" y="195" font-size="9.5" text-anchor="middle">仅单独重传 F1</text>
    <text class="fig-green" x="521" y="229" font-size="9.5" text-anchor="middle">ACK1</text>
    <text class="fig-green" x="630" y="210" font-size="9.5">齐备上交</text>
    <text class="fig-muted" x="522" y="284" font-size="10" text-anchor="middle">收方缓存乱序 F2/F3，发方仅重传丢失的 F1，效率最高</text>
  </svg>
  <figcaption>图 4-4：1 号帧丢失时 GBN（丢弃乱序、累计确认、回退重传）与 SR（缓存乱序、逐一确认、单帧重传）对比</figcaption>
</figure>

### 4.4 信道利用率

<strong>停止等待</strong>：一次交互总时间 = 数据帧发送时延 $T_D$ + 信号往返传播时延 RTT + 确认帧发送时延 $T_A$，其中只有 $T_D$ 用于发送数据。

$$
U_{\mathrm{SAW}}=\frac{T_{D}}{T_{D}+\mathrm{RTT}+T_{A}}\approx \frac{T_{D}}{T_{D}+\mathrm{RTT}}
$$

（多数情况下 $T_A \ll T_D$，有些题直接令其不计。）卫星链路 RTT 远大于 $T_D$，利用率很低；同一无线局域网内 RTT 远小于 $T_D$，利用率不会很低。

<figure class="cpd-figure not-content" id="cnq-w2-fig-11">
  <svg viewBox="0 0 700 284" role="img" aria-label="停止等待协议SAW四种工作场景正常交互与数据丢失与ACK丢失与ACK迟到时空图">
    <defs>
      <marker id="cnq-w2-arr-11" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="14" width="158" height="256" rx="8"/>
    <rect class="fig-box-subtle" x="186" y="14" width="158" height="256" rx="8"/>
    <rect class="fig-box-subtle" x="356" y="14" width="158" height="256" rx="8"/>
    <rect class="fig-box-subtle" x="526" y="14" width="158" height="256" rx="8"/>
    <line class="fig-line" stroke-width="1.4" x1="48" y1="54" x2="48" y2="226"/>
    <line class="fig-line" stroke-width="1.4" x1="142" y1="54" x2="142" y2="226"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.3" x1="48" y1="64" x2="140" y2="90" marker-end="url(#cnq-w2-arr-11)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.3" x1="142" y1="98" x2="50" y2="124" marker-end="url(#cnq-w2-arr-11)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.3" x1="48" y1="138" x2="140" y2="164" marker-end="url(#cnq-w2-arr-11)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.3" x1="142" y1="172" x2="50" y2="198" marker-end="url(#cnq-w2-arr-11)"/>
    <line class="fig-line" stroke-width="1.4" x1="218" y1="54" x2="218" y2="226"/>
    <line class="fig-line" stroke-width="1.4" x1="312" y1="54" x2="312" y2="226"/>
    <line class="fig-line-dashed" stroke="var(--sl-color-red)" stroke-width="1.4" x1="218" y1="64" x2="270" y2="80"/>
    <path class="fig-line-dashed" stroke="var(--sl-color-orange)" d="M 212 64 L 204 64 L 204 142 L 212 142"/>
    <line class="fig-line" stroke="var(--sl-color-red)" stroke-width="1.3" x1="218" y1="142" x2="310" y2="168" marker-end="url(#cnq-w2-arr-11)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.3" x1="312" y1="176" x2="220" y2="202" marker-end="url(#cnq-w2-arr-11)"/>
    <line class="fig-line" stroke-width="1.4" x1="388" y1="54" x2="388" y2="226"/>
    <line class="fig-line" stroke-width="1.4" x1="482" y1="54" x2="482" y2="226"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.3" x1="388" y1="64" x2="480" y2="90" marker-end="url(#cnq-w2-arr-11)"/>
    <line class="fig-line-dashed" stroke="var(--sl-color-red)" stroke-width="1.4" x1="482" y1="98" x2="434" y2="112"/>
    <path class="fig-line-dashed" stroke="var(--sl-color-orange)" d="M 382 64 L 374 64 L 374 144 L 382 144"/>
    <line class="fig-line" stroke="var(--sl-color-red)" stroke-width="1.3" x1="388" y1="144" x2="480" y2="170" marker-end="url(#cnq-w2-arr-11)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.3" x1="482" y1="178" x2="390" y2="204" marker-end="url(#cnq-w2-arr-11)"/>
    <line class="fig-line" stroke-width="1.4" x1="558" y1="54" x2="558" y2="226"/>
    <line class="fig-line" stroke-width="1.4" x1="652" y1="54" x2="652" y2="226"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.3" x1="558" y1="64" x2="650" y2="88" marker-end="url(#cnq-w2-arr-11)"/>
    <line class="fig-line-dashed" stroke="var(--sl-color-orange)" stroke-width="1.3" x1="652" y1="94" x2="560" y2="162" marker-end="url(#cnq-w2-arr-11)"/>
    <line class="fig-line" stroke="var(--sl-color-red)" stroke-width="1.3" x1="558" y1="130" x2="650" y2="156" marker-end="url(#cnq-w2-arr-11)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.3" x1="652" y1="164" x2="560" y2="194" marker-end="url(#cnq-w2-arr-11)"/>
    <rect class="fig-box-red" x="256" y="72" width="40" height="17" rx="3"/>
    <rect class="fig-box-red" x="414" y="104" width="42" height="17" rx="3"/>
    <rect class="fig-box" x="582" y="132" width="48" height="17" rx="3"/>
    <rect class="fig-box" x="182" y="97" width="28" height="15" rx="3"/>
    <rect class="fig-box" x="352" y="97" width="28" height="15" rx="3"/>
    <rect class="fig-box" x="410" y="191" width="50" height="15" rx="3"/>
    <rect class="fig-box" x="536" y="121" width="28" height="14" rx="3"/>
    <rect class="fig-box" x="536" y="187" width="28" height="14" rx="3"/>
    <text class="fig-title" x="95" y="32" font-size="11.5" text-anchor="middle">① 正常无差错</text>
    <text class="fig-muted" x="48" y="48" font-size="10" text-anchor="middle">发</text>
    <text class="fig-muted" x="142" y="48" font-size="10" text-anchor="middle">收</text>
    <text class="fig-blue" x="95" y="72" font-size="9.5" text-anchor="middle">DATA 0</text>
    <text class="fig-green" x="95" y="122" font-size="9.5" text-anchor="middle">ACK 0</text>
    <text class="fig-blue" x="95" y="146" font-size="9.5" text-anchor="middle">DATA 1</text>
    <text class="fig-green" x="95" y="196" font-size="9.5" text-anchor="middle">ACK 1</text>
    <text class="fig-muted" x="95" y="244" font-size="9.5" text-anchor="middle">1 bit 序号 0/1 交替</text>
    <text class="fig-muted" x="95" y="259" font-size="9.5" text-anchor="middle">收一确认才发下一帧</text>
    <text class="fig-title" x="265" y="32" font-size="11.5" text-anchor="middle">② 数据丢失或误码</text>
    <text class="fig-muted" x="218" y="48" font-size="10" text-anchor="middle">发</text>
    <text class="fig-muted" x="312" y="48" font-size="10" text-anchor="middle">收</text>
    <text class="fig-red" x="276" y="84" font-size="9.5" text-anchor="middle">D0丢</text>
    <text class="fig-orange" x="196" y="108" font-size="9.5" text-anchor="middle">超时</text>
    <text class="fig-red" x="266" y="150" font-size="9.5" text-anchor="middle">重传 D0</text>
    <text class="fig-green" x="266" y="200" font-size="9.5" text-anchor="middle">ACK 0</text>
    <text class="fig-muted" x="265" y="244" font-size="9.5" text-anchor="middle">误码帧静默丢弃不回NAK</text>
    <text class="fig-muted" x="265" y="259" font-size="9.5" text-anchor="middle">全靠超时计时器触发重传</text>
    <text class="fig-title" x="435" y="32" font-size="11.5" text-anchor="middle">③ ACK 确认丢失</text>
    <text class="fig-muted" x="388" y="48" font-size="10" text-anchor="middle">发</text>
    <text class="fig-muted" x="482" y="48" font-size="10" text-anchor="middle">收</text>
    <text class="fig-blue" x="435" y="72" font-size="9.5" text-anchor="middle">DATA 0</text>
    <text class="fig-red" x="435" y="116" font-size="9.5" text-anchor="middle">ACK丢</text>
    <text class="fig-orange" x="366" y="108" font-size="9.5" text-anchor="middle">超时</text>
    <text class="fig-red" x="435" y="152" font-size="9.5" text-anchor="middle">重传 D0</text>
    <text class="fig-purple" x="486" y="172" font-size="9">判重丢</text>
    <text class="fig-green" x="435" y="202" font-size="9.5" text-anchor="middle">补 ACK0</text>
    <text class="fig-muted" x="435" y="244" font-size="9.5" text-anchor="middle">收端凭序号丢弃重复 D0</text>
    <text class="fig-muted" x="435" y="259" font-size="9.5" text-anchor="middle">但必须重发 ACK0 解锁发端</text>
    <text class="fig-title" x="605" y="32" font-size="11.5" text-anchor="middle">④ ACK 迟到早超时</text>
    <text class="fig-muted" x="558" y="48" font-size="10" text-anchor="middle">发</text>
    <text class="fig-muted" x="652" y="48" font-size="10" text-anchor="middle">收</text>
    <text class="fig-blue" x="605" y="71" font-size="9.5" text-anchor="middle">DATA 0</text>
    <text class="fig-orange" x="542" y="132" font-size="9">超时</text>
    <text class="fig-red" x="606" y="144" font-size="9.5" text-anchor="middle">重传D0</text>
    <text class="fig-green" x="608" y="192" font-size="9.5" text-anchor="middle">重ACK0</text>
    <text class="fig-purple" x="542" y="198" font-size="9">丢弃</text>
    <text class="fig-muted" x="605" y="244" font-size="9.5" text-anchor="middle">确认帧也必须带序号！</text>
    <text class="fig-muted" x="605" y="259" font-size="9.5" text-anchor="middle">防止迟到 ACK 误确认新帧</text>
  </svg>
  <figcaption>图 4-5：停止等待协议（SAW）正常交互与三类差错重传（数据丢失、ACK 丢失、ACK 迟到）时空图</figcaption>
</figure>

<strong>GBN / SR</strong>：采用流水线传输，发送方不必每发一帧就停下等待，可连续发送 $W_T$ 个帧。无差错时

$$
U=\frac{W_{T}T_{D}}{T_{D}+\mathrm{RTT}}
$$

$W_T$ 足够大时利用率可达 100%。

<strong>三种协议的比较</strong>：SAW 利用率最低；GBN 无差错时利用率高，但<strong>一旦超时重传就要回退 N 帧</strong>，同等条件（相同窗口大小、传播时延、带宽）下，GBN 的利用率<strong>不一定</strong>比 SAW 高；SR 与 GBN 在无差错时的理论最大利用率相同，但 SR 的<strong>乱序接收 + 选择性重传</strong>使其<strong>实际</strong>利用率更高。

> <strong>★考点</strong>：2014 年题不必先验证“一次交互能否发完 1000 个帧”——直接算，结果只能<strong>等于</strong>或<strong>小于</strong>信道带宽（木桶效应：发送速率不可能超过带宽）。

<figure class="cpd-figure not-content" id="cnq-w2-fig-12">
  <svg viewBox="0 0 700 268" role="img" aria-label="停止等待SAW与流水线协议GBN和SR信道利用率计算时空图对比">
    <defs>
      <marker id="cnq-w2-arr-12" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="14" width="332" height="240" rx="8"/>
    <rect class="fig-box-subtle" x="360" y="14" width="324" height="240" rx="8"/>
    <line class="fig-line" stroke-width="1.5" x1="94" y1="48" x2="94" y2="204"/>
    <line class="fig-line" stroke-width="1.5" x1="278" y1="48" x2="278" y2="204"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.3" x1="94" y1="56" x2="276" y2="88" marker-end="url(#cnq-w2-arr-12)"/>
    <line class="fig-line-dashed" stroke="var(--sl-color-blue)" stroke-width="1.1" x1="94" y1="84" x2="276" y2="116"/>
    <line class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.2" x1="278" y1="116" x2="96" y2="148"/>
    <line class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.3" x1="278" y1="130" x2="96" y2="162" marker-end="url(#cnq-w2-arr-12)"/>
    <path class="fig-line-dashed" stroke="var(--sl-color-orange)" d="M 80 84 L 70 84 L 70 162 L 80 162"/>
    <line class="fig-line" stroke-width="1.5" x1="436" y1="48" x2="436" y2="204"/>
    <line class="fig-line" stroke-width="1.5" x1="620" y1="48" x2="620" y2="204"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.2" x1="436" y1="56" x2="618" y2="88" marker-end="url(#cnq-w2-arr-12)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.2" x1="436" y1="78" x2="618" y2="110" marker-end="url(#cnq-w2-arr-12)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.2" x1="436" y1="100" x2="618" y2="132" marker-end="url(#cnq-w2-arr-12)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.2" x1="436" y1="122" x2="618" y2="154" marker-end="url(#cnq-w2-arr-12)"/>
    <line class="fig-line-dashed" stroke="var(--sl-color-blue)" stroke-width="1.1" x1="436" y1="144" x2="618" y2="176"/>
    <line class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.3" x1="620" y1="116" x2="438" y2="152" marker-end="url(#cnq-w2-arr-12)"/>
    <rect class="fig-box-blue" x="87" y="56" width="14" height="28" rx="2"/>
    <rect class="fig-box-green" x="271" y="116" width="14" height="14" rx="2"/>
    <rect class="fig-box-blue" x="87" y="162" width="14" height="28" rx="2"/>
    <rect class="fig-box" x="28" y="210" width="308" height="34" rx="4"/>
    <rect class="fig-box-blue" x="429" y="56" width="14" height="22" rx="2"/>
    <rect class="fig-box-accent" x="429" y="78" width="14" height="22" rx="2"/>
    <rect class="fig-box-blue" x="429" y="100" width="14" height="22" rx="2"/>
    <rect class="fig-box-accent" x="429" y="122" width="14" height="22" rx="2"/>
    <rect class="fig-box-green" x="372" y="210" width="300" height="34" rx="4"/>
    <rect class="fig-box" x="552" y="136" width="64" height="15" rx="3"/>
    <text class="fig-title" x="182" y="32" font-size="12" text-anchor="middle">SAW 信道利用率（单帧阻塞等待 RTT）</text>
    <text class="fig-label" x="94" y="44" font-size="10" text-anchor="middle">发送方</text>
    <text class="fig-label" x="278" y="44" font-size="10" text-anchor="middle">接收方</text>
    <text class="fig-blue" x="78" y="74" font-size="10" text-anchor="end">TD</text>
    <text class="fig-orange" x="64" y="126" font-size="10" text-anchor="end">RTT+TA</text>
    <text class="fig-muted" x="64" y="140" font-size="9.5" text-anchor="end">(空闲)</text>
    <text class="fig-green" x="290" y="127" font-size="10">TA</text>
    <text class="fig-blue" x="78" y="180" font-size="10" text-anchor="end">下一TD</text>
    <text class="fig-label" x="182" y="231" font-size="11" text-anchor="middle">U_SAW = TD / (TD + RTT + TA)</text>
    <text class="fig-title" x="522" y="32" font-size="12" text-anchor="middle">GBN / SR 流水线传输（连发 Wt 帧填满管道）</text>
    <text class="fig-label" x="436" y="44" font-size="10" text-anchor="middle">发送方</text>
    <text class="fig-label" x="620" y="44" font-size="10" text-anchor="middle">接收方</text>
    <text class="fig-blue" x="422" y="71" font-size="9.5" text-anchor="end">F0</text>
    <text class="fig-accent" x="422" y="93" font-size="9.5" text-anchor="end">F1</text>
    <text class="fig-blue" x="422" y="115" font-size="9.5" text-anchor="end">F2</text>
    <text class="fig-accent" x="422" y="137" font-size="9.5" text-anchor="end">F3</text>
    <text class="fig-muted" x="422" y="160" font-size="9.5" text-anchor="end">Wt·TD</text>
    <text class="fig-green" x="556" y="147" font-size="9.5">首帧ACK返回</text>
    <text class="fig-green" x="522" y="231" font-size="10.5" text-anchor="middle">U = min(1, Wt·TD / (TD + RTT + TA)) ≤ 100%</text>
  </svg>
  <figcaption>图 4-6：停止等待（SAW）单帧周期与流水线（GBN/SR）连续发送 Wt 帧的信道利用率时空推导</figcaption>
</figure>

## 第5章 网络层：IPv4 编址与子网划分

### 5.1 编址方法的演进

<figure class="cpd-figure not-content" id="cnq-w3-fig-1">
  <svg viewBox="0 0 700 308" role="img" aria-label="IPv4 编址方法三阶段演进：分类编址、划分子网与无分类编址 CIDR">
    <defs>
      <marker id="cnq-w3-arr-1" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="248" width="668" height="46" rx="6" class="fig-box-subtle"/>
    <line x1="152" y1="42" x2="664" y2="42" class="fig-divider"/>
    <line x1="152" y1="38" x2="152" y2="46" class="fig-divider"/>
    <line x1="328" y1="38" x2="328" y2="46" class="fig-divider"/>
    <line x1="664" y1="38" x2="664" y2="46" class="fig-divider"/>
    <line x1="328" y1="90" x2="328" y2="124" class="fig-line-dashed"/>
    <line x1="440" y1="90" x2="440" y2="122" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#cnq-w3-arr-1)"/>
    <line x1="384" y1="90" x2="384" y2="122" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#cnq-w3-arr-1)"/>
    <line x1="152" y1="162" x2="152" y2="196" class="fig-line-dashed"/>
    <line x1="240" y1="162" x2="260" y2="194" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#cnq-w3-arr-1)"/>
    <line x1="384" y1="162" x2="364" y2="194" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#cnq-w3-arr-1)"/>
    <line x1="472" y1="176" x2="472" y2="194" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#cnq-w3-arr-1)"/>
    <rect x="16" y="52" width="122" height="38" rx="5" class="fig-box-blue"/>
    <rect x="152" y="52" width="176" height="38" rx="4" class="fig-box-blue"/>
    <rect x="328" y="52" width="336" height="38" rx="4" class="fig-box"/>
    <rect x="454" y="97" width="210" height="20" rx="4" class="fig-box"/>
    <rect x="16" y="124" width="122" height="38" rx="5" class="fig-box-orange"/>
    <rect x="152" y="124" width="176" height="38" rx="4" class="fig-box-blue"/>
    <rect x="328" y="124" width="112" height="38" rx="4" class="fig-box-orange"/>
    <rect x="440" y="124" width="224" height="38" rx="4" class="fig-box"/>
    <rect x="414" y="169" width="250" height="20" rx="4" class="fig-box"/>
    <rect x="16" y="196" width="122" height="38" rx="5" class="fig-box-green"/>
    <rect x="152" y="196" width="320" height="38" rx="4" class="fig-box-green"/>
    <rect x="472" y="196" width="192" height="38" rx="4" class="fig-box"/>
    <text x="152" y="28" font-size="13" class="fig-title">32 位 IPv4 地址结构的三阶段演进</text>
    <text x="664" y="28" font-size="11" text-anchor="end" class="fig-muted">Bit 0 ────────── 32 bits ────────── Bit 31</text>
    <text x="77" y="68" font-size="12" text-anchor="middle" class="fig-blue">① 分类编址</text>
    <text x="77" y="83" font-size="10.5" text-anchor="middle" class="fig-muted">两级定长</text>
    <text x="240" y="68" font-size="12" text-anchor="middle" class="fig-blue">网络号 (net-id)</text>
    <text x="240" y="83" font-size="10.5" text-anchor="middle" class="fig-muted">A: 8b / B: 16b / C: 24b</text>
    <text x="496" y="68" font-size="12" text-anchor="middle" class="fig-label">主机号 (host-id)</text>
    <text x="496" y="83" font-size="10.5" text-anchor="middle" class="fig-muted">剩余 24b / 16b / 8b</text>
    <text x="559" y="111" font-size="10.5" text-anchor="middle" class="fig-orange">从主机号借 k 位作子网号</text>
    <text x="77" y="140" font-size="12" text-anchor="middle" class="fig-orange">② 划分子网</text>
    <text x="77" y="155" font-size="10.5" text-anchor="middle" class="fig-muted">三级结构</text>
    <text x="240" y="140" font-size="12" text-anchor="middle" class="fig-blue">网络号 (net-id)</text>
    <text x="240" y="155" font-size="10.5" text-anchor="middle" class="fig-muted">原分类网络号不变</text>
    <text x="384" y="140" font-size="12" text-anchor="middle" class="fig-orange">子网号</text>
    <text x="384" y="155" font-size="10.5" text-anchor="middle" class="fig-muted">借 k 位</text>
    <text x="552" y="140" font-size="12" text-anchor="middle" class="fig-label">主机号 (host-id)</text>
    <text x="552" y="155" font-size="10.5" text-anchor="middle" class="fig-muted">掩码连续 1 覆盖前两段</text>
    <text x="539" y="183" font-size="10.5" text-anchor="middle" class="fig-green">取消分类，合并为变长网络前缀 /x</text>
    <text x="77" y="212" font-size="12" text-anchor="middle" class="fig-green">③ 无分类 CIDR</text>
    <text x="77" y="227" font-size="10.5" text-anchor="middle" class="fig-muted">两级变长</text>
    <text x="312" y="212" font-size="12" text-anchor="middle" class="fig-green">网络前缀 (network-prefix，长度 x 可变)</text>
    <text x="312" y="227" font-size="10.5" text-anchor="middle" class="fig-muted">斜线记法 IP/x（掩码高 x 位全 1）</text>
    <text x="568" y="212" font-size="12" text-anchor="middle" class="fig-label">主机号 (32 - x 位)</text>
    <text x="568" y="227" font-size="10.5" text-anchor="middle" class="fig-muted">可分配主机 = 2^(32-x) - 2</text>
    <text x="32" y="267" font-size="11.5" class="fig-label">演进脉络：每次均在 32 位地址上重划前缀边界，用子网掩码（连续 1 + 连续 0）统一提取网络地址</text>
    <text x="32" y="285" font-size="11.5" class="fig-muted">核心铁律：主机号全 0 = 网络地址，主机号全 1 = 直接广播地址，二者均不能分配给主机或路由器接口</text>
  </svg>
  <figcaption>图 5-1：IPv4 编址从“分类两级”到“子网三级”再到“CIDR 变长前缀两级”的结构演进</figcaption>
</figure>

<strong>分类编址</strong>（最早）：32 位地址分为网络号与主机号，按 A/B/C/D/E 五类划分，其中 A、B、C 可分配给网络使用，D 类用于多播，E 类保留。

| 类别 | 最高位特征 | 网络号位数 | 可用网络数 | 每个网络地址数 | 地址范围 |
|---|---|---|---|---|---|
| A | 0 | 8 | $2^{7}-2=126$ | $2^{24}$ | 1.0.0.0 – 126.255.255.255 |
| B | 10 | 16 | $2^{14}$ | $2^{16}$ | 128.0.0.0 – 191.255.255.255 |
| C | 110 | 24 | $2^{21}$ | $2^{8}$ | 192.0.0.0 – 223.255.255.255 |
| D | 1110 | — | — | — | 224.0.0.0 – 239.255.255.255（多播） |
| E | 1111 | — | — | — | 保留 |

A 类最高位固定为 0，故可变位为 $32-1=31$ 位，整个 A 类地址空间含 $2^{31}$ 个地址；其中<strong>网络号 0 与 127 不能指派</strong>（有特殊用途）。“指派”强调用户须向互联网号码分配管理机构申请网络号，拿到后可自行分配其下的主机号。

<strong>分类编址的致命缺点</strong>：只能以 A/B/C 类网络（8/16/24 位网络号）为单位分配，既不灵活又极易浪费。

<figure class="cpd-figure not-content" id="cnq-w3-fig-2">
  <svg viewBox="0 0 700 296" role="img" aria-label="IPv4 分类编址 A B C D E 五类地址最高位特征与特殊指派边界">
    <rect x="16" y="244" width="668" height="38" rx="6" class="fig-box-subtle"/>
    <rect x="16" y="42" width="52" height="32" rx="4" class="fig-box-blue"/>
    <rect x="76" y="42" width="32" height="32" rx="3" class="fig-box-purple"/>
    <rect x="108" y="42" width="84" height="32" rx="3" class="fig-box-blue"/>
    <rect x="192" y="42" width="236" height="32" rx="3" class="fig-box"/>
    <rect x="436" y="42" width="248" height="32" rx="4" class="fig-box-subtle"/>
    <rect x="16" y="82" width="52" height="32" rx="4" class="fig-box-blue"/>
    <rect x="76" y="82" width="42" height="32" rx="3" class="fig-box-purple"/>
    <rect x="118" y="82" width="136" height="32" rx="3" class="fig-box-blue"/>
    <rect x="254" y="82" width="174" height="32" rx="3" class="fig-box"/>
    <rect x="436" y="82" width="248" height="32" rx="4" class="fig-box-subtle"/>
    <rect x="16" y="122" width="52" height="32" rx="4" class="fig-box-blue"/>
    <rect x="76" y="122" width="52" height="32" rx="3" class="fig-box-purple"/>
    <rect x="128" y="122" width="204" height="32" rx="3" class="fig-box-blue"/>
    <rect x="332" y="122" width="96" height="32" rx="3" class="fig-box"/>
    <rect x="436" y="122" width="248" height="32" rx="4" class="fig-box-subtle"/>
    <rect x="16" y="162" width="52" height="32" rx="4" class="fig-box-orange"/>
    <rect x="76" y="162" width="62" height="32" rx="3" class="fig-box-purple"/>
    <rect x="138" y="162" width="290" height="32" rx="3" class="fig-box-orange"/>
    <rect x="436" y="162" width="248" height="32" rx="4" class="fig-box-subtle"/>
    <rect x="16" y="202" width="52" height="32" rx="4" class="fig-box-red"/>
    <rect x="76" y="202" width="62" height="32" rx="3" class="fig-box-purple"/>
    <rect x="138" y="202" width="290" height="32" rx="3" class="fig-box-subtle"/>
    <rect x="436" y="202" width="248" height="32" rx="4" class="fig-box-subtle"/>
    <text x="16" y="28" font-size="13" class="fig-title">分类编址五类地址（A–E）最高位特征与指派边界</text>
    <text x="684" y="28" font-size="11" text-anchor="end" class="fig-muted">紫框 = 固定前缀位 · 蓝框 = 可指派网络号</text>
    <text x="42" y="63" font-size="12" text-anchor="middle" class="fig-blue">A 类</text>
    <text x="92" y="63" font-size="12" text-anchor="middle" class="fig-purple">0</text>
    <text x="150" y="63" font-size="11.5" text-anchor="middle" class="fig-blue">net (7b)</text>
    <text x="310" y="63" font-size="11.5" text-anchor="middle" class="fig-label">host-id (24b，每网 2^24 - 2 台主机)</text>
    <text x="560" y="63" font-size="11" text-anchor="middle" class="fig-label">1~126.x | 网数 2^7-2 (扣 0 与 127)</text>
    <text x="42" y="103" font-size="12" text-anchor="middle" class="fig-blue">B 类</text>
    <text x="97" y="103" font-size="12" text-anchor="middle" class="fig-purple">10</text>
    <text x="186" y="103" font-size="11.5" text-anchor="middle" class="fig-blue">net-id (14b)</text>
    <text x="341" y="103" font-size="11.5" text-anchor="middle" class="fig-label">host-id (16b，65534 台)</text>
    <text x="560" y="103" font-size="11" text-anchor="middle" class="fig-label">128.0~191.255 | 可用网数 2^14</text>
    <text x="42" y="143" font-size="12" text-anchor="middle" class="fig-blue">C 类</text>
    <text x="102" y="143" font-size="12" text-anchor="middle" class="fig-purple">110</text>
    <text x="230" y="143" font-size="11.5" text-anchor="middle" class="fig-blue">net-id (21b)</text>
    <text x="380" y="143" font-size="11.5" text-anchor="middle" class="fig-label">host (8b)</text>
    <text x="560" y="143" font-size="11" text-anchor="middle" class="fig-label">192.0.0~223.255.255 | 每网 254 台</text>
    <text x="42" y="183" font-size="12" text-anchor="middle" class="fig-orange">D 类</text>
    <text x="107" y="183" font-size="12" text-anchor="middle" class="fig-purple">1110</text>
    <text x="283" y="183" font-size="11.5" text-anchor="middle" class="fig-orange">多播地址 (28b，不区分网络号与主机号)</text>
    <text x="560" y="183" font-size="11" text-anchor="middle" class="fig-orange">224.0.0.0~239.x | 仅作目的地址</text>
    <text x="42" y="223" font-size="12" text-anchor="middle" class="fig-red">E 类</text>
    <text x="107" y="223" font-size="12" text-anchor="middle" class="fig-purple">1111</text>
    <text x="283" y="223" font-size="11.5" text-anchor="middle" class="fig-muted">保留地址空间 (28b)</text>
    <text x="560" y="223" font-size="11" text-anchor="middle" class="fig-muted">240.0.0.0~255.255.255.255</text>
    <text x="32" y="268" font-size="11.5" class="fig-label">速记首字节分界：0(特殊) | 1~126(A) | 127(环回) | 128~191(B) | 192~223(C) | 224~239(D) | 240~255(E)</text>
  </svg>
  <figcaption>图 5-2：IPv4 五类地址比特前缀、首字节十进制区间与单网可分配主机数对比</figcaption>
</figure>

<strong>划分子网</strong>：在分类编址基础上，从 32 位地址的<strong>主机号</strong>部分借若干比特作<strong>子网号</strong>，借多少由<strong>子网掩码</strong>指出，于是两级结构变三级结构。

> <strong>注意：早期规定子网号不能全 0 或全 1，故子网号不能只有 1 位；随着无分类编址普及，全 0/全 1 子网号已可使用</strong>，因此允许只借 1 位。这与“主机号全 0 作网络地址、全 1 作广播地址因而不能分配”是<strong>两码事</strong>，别混。

<strong>无分类编址（CIDR）</strong>：取消分类概念，把地址重新划为<strong>网络前缀 + 主机号</strong>，前缀长度可在有意义范围内变化（<strong>变长网络号</strong>），用斜线记法（CIDR）斜线后的数值或地址掩码指出。

> <strong>注意</strong>：<strong>定长/变长网络号</strong>（分类 vs 无分类）与<strong>定长/变长子网划分</strong>是两个不同层面的概念，不要混淆。

<strong>地址数量 vs 可分配地址数量</strong>：说“某网络包含的地址数量”<strong>不减</strong>网络地址与广播地址；说“可分配的地址数量”则<strong>要减 2</strong>。

### 5.2 特殊地址与默认子网掩码

| 特殊地址 | 含义与用途 |
|---|---|
| 0.0.0.0 | 可作<strong>源</strong>地址、不能作目的地址。联想三处：DHCP 客户端未取得地址前临时使用；路由表中的<strong>默认路由</strong>条目（目的网络与掩码均为 0.0.0.0）；OSPF <strong>主干区域</strong>标识符 |
| 127.0.0.0/8 | 环回地址 |
| 10.0.0.0/8 | 私有地址 |
| 172.16.0.0/12（172.16 – 172.31） | 私有地址 |
| 192.168.0.0/16 | 私有地址 |

私有地址的速记法：<strong>10 开头、192.168 开头</strong>都是私有；稍麻烦的 172.16 至 172.31 开头也是私有（等价写法 172.16/12）。

<figure class="cpd-figure not-content" id="cnq-w3-fig-3">
  <svg viewBox="0 0 712 334" role="img" aria-label="IPv4 特殊地址作源地址与目的地址的合法性矩阵及三大私有地址块">
    <rect x="16" y="248" width="680" height="70" rx="6" class="fig-box-subtle"/>
    <rect x="16" y="40" width="188" height="28" rx="3" class="fig-box"/>
    <rect x="204" y="40" width="96" height="28" rx="3" class="fig-box"/>
    <rect x="300" y="40" width="104" height="28" rx="3" class="fig-box"/>
    <rect x="404" y="40" width="292" height="28" rx="3" class="fig-box"/>
    <rect x="16" y="68" width="188" height="34" rx="2" class="fig-box"/>
    <rect x="204" y="68" width="96" height="34" rx="2" class="fig-box-green"/>
    <rect x="300" y="68" width="104" height="34" rx="2" class="fig-box-red"/>
    <rect x="404" y="68" width="292" height="34" rx="2" class="fig-box-subtle"/>
    <rect x="16" y="102" width="188" height="34" rx="2" class="fig-box"/>
    <rect x="204" y="102" width="96" height="34" rx="2" class="fig-box-green"/>
    <rect x="300" y="102" width="104" height="34" rx="2" class="fig-box-red"/>
    <rect x="404" y="102" width="292" height="34" rx="2" class="fig-box-subtle"/>
    <rect x="16" y="136" width="188" height="34" rx="2" class="fig-box"/>
    <rect x="204" y="136" width="96" height="34" rx="2" class="fig-box-red"/>
    <rect x="300" y="136" width="104" height="34" rx="2" class="fig-box-green"/>
    <rect x="404" y="136" width="292" height="34" rx="2" class="fig-box-subtle"/>
    <rect x="16" y="170" width="188" height="34" rx="2" class="fig-box"/>
    <rect x="204" y="170" width="96" height="34" rx="2" class="fig-box-red"/>
    <rect x="300" y="170" width="104" height="34" rx="2" class="fig-box-green"/>
    <rect x="404" y="170" width="292" height="34" rx="2" class="fig-box-subtle"/>
    <rect x="16" y="204" width="188" height="34" rx="2" class="fig-box"/>
    <rect x="204" y="204" width="96" height="34" rx="2" class="fig-box-orange"/>
    <rect x="300" y="204" width="104" height="34" rx="2" class="fig-box-orange"/>
    <rect x="404" y="204" width="292" height="34" rx="2" class="fig-box-subtle"/>
    <rect x="28" y="276" width="190" height="32" rx="4" class="fig-box-blue"/>
    <rect x="228" y="276" width="236" height="32" rx="4" class="fig-box-purple"/>
    <rect x="474" y="276" width="210" height="32" rx="4" class="fig-box-green"/>
    <text x="16" y="28" font-size="13" class="fig-title">IPv4 特殊地址“源 / 目的”合法性矩阵与三大私有地址块</text>
    <text x="110" y="59" font-size="11.5" text-anchor="middle" class="fig-label">特殊地址（网络号 + 主机号）</text>
    <text x="252" y="59" font-size="11.5" text-anchor="middle" class="fig-label">作源 IP</text>
    <text x="352" y="59" font-size="11.5" text-anchor="middle" class="fig-label">作目的 IP</text>
    <text x="550" y="59" font-size="11.5" text-anchor="middle" class="fig-label">408 核心用途与转发规则</text>
    <text x="110" y="89" font-size="11.5" text-anchor="middle" class="fig-label">全 0 (0.0.0.0)</text>
    <text x="252" y="89" font-size="11.5" text-anchor="middle" class="fig-green">✔ 允许</text>
    <text x="352" y="89" font-size="11.5" text-anchor="middle" class="fig-red">✘ 禁止</text>
    <text x="550" y="89" font-size="11" text-anchor="middle" class="fig-label">DHCP 发现报文源地址；另作默认路由 / Area 0</text>
    <text x="110" y="123" font-size="11.5" text-anchor="middle" class="fig-label">net 全 0 + 特定 host</text>
    <text x="252" y="123" font-size="11.5" text-anchor="middle" class="fig-green">✔ 允许</text>
    <text x="352" y="123" font-size="11.5" text-anchor="middle" class="fig-red">✘ 禁止</text>
    <text x="550" y="123" font-size="11" text-anchor="middle" class="fig-label">本网络上的特定主机（仅限初始化阶段）</text>
    <text x="110" y="157" font-size="11.5" text-anchor="middle" class="fig-label">全 1 (255.255.255.255)</text>
    <text x="252" y="157" font-size="11.5" text-anchor="middle" class="fig-red">✘ 禁止</text>
    <text x="352" y="157" font-size="11.5" text-anchor="middle" class="fig-green">✔ 受限广播</text>
    <text x="550" y="157" font-size="11" text-anchor="middle" class="fig-label">仅限本网广播（如 DHCP），路由器隔离不转发</text>
    <text x="110" y="191" font-size="11.5" text-anchor="middle" class="fig-label">特定 net + host 全 1</text>
    <text x="252" y="191" font-size="11.5" text-anchor="middle" class="fig-red">✘ 禁止</text>
    <text x="352" y="191" font-size="11.5" text-anchor="middle" class="fig-green">✔ 直接广播</text>
    <text x="550" y="191" font-size="11" text-anchor="middle" class="fig-label">对特定网络广播（可跨路由器定向转发）</text>
    <text x="110" y="225" font-size="11.5" text-anchor="middle" class="fig-label">127.x.y.z (非全0/全1)</text>
    <text x="252" y="225" font-size="11.5" text-anchor="middle" class="fig-orange">✔ 本机环回</text>
    <text x="352" y="225" font-size="11.5" text-anchor="middle" class="fig-orange">✔ 本机环回</text>
    <text x="550" y="225" font-size="11" text-anchor="middle" class="fig-label">本机软件环回测试，绝不进入物理网络</text>
    <text x="28" y="267" font-size="11.5" class="fig-label">三大专用（私有）地址块（公网路由器一律不转发目的为私有 IP 的分组，出公网须经 NAT）：</text>
    <text x="123" y="297" font-size="11.5" text-anchor="middle" class="fig-blue">10.0.0.0/8（1 个 A 类）</text>
    <text x="346" y="297" font-size="11.5" text-anchor="middle" class="fig-purple">172.16.0.0/12（172.16~31，16 个 B）</text>
    <text x="579" y="297" font-size="11.5" text-anchor="middle" class="fig-green">192.168.0.0/16（256 个 C 类）</text>
  </svg>
  <figcaption>图 5-3：特殊 IPv4 地址作源/目的地址的合法性判定及 RFC 1918 三大私有地址块</figcaption>
</figure>

<strong>默认子网掩码</strong>（未划分子网时使用）：A 类 255.0.0.0，B 类 255.255.0.0，C 类 255.255.255.0。保留它的意义是<strong>统一操作</strong>：操作系统网络配置界面不必为“划/不划子网”设计两套界面；求网络地址时一律用“IP 地址与掩码逻辑与”，不必分情况讨论。

<strong>划分子网后地址数的变化：某 C 类网络划成两个子网后，总地址数不变，可分配地址数少 2</strong>——原来网络中的两个可分配地址，一个变成了第一个子网的广播地址，另一个变成了第二个子网的网络地址。

<strong>由 IP + 掩码求子网信息</strong>：网络地址 = IP 与掩码相与；广播地址 = 网络地址的主机号全置 1；最小可分配地址 = 网络地址 + 1；最大可分配地址 = 广播地址 − 1。

> <strong>注意</strong>：写答案时最精确的形式是“IP 地址/前缀长度”（CIDR）；若上下文已能准确描述，也可省略前缀长度。做题要<strong>跟着题目的格式走</strong>——路由表用点分十进制掩码给出，就不要强行改成斜线形式。

<figure class="cpd-figure not-content" id="cnq-w3-fig-4">
  <svg viewBox="0 0 712 316" role="img" aria-label="IP 地址与子网掩码按位逻辑与求子网信息及划分子网后可用地址数减少原理">
    <defs>
      <marker id="cnq-w3-arr-4" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="40" width="424" height="260" rx="6" class="fig-box-subtle"/>
    <rect x="452" y="40" width="244" height="260" rx="6" class="fig-box-subtle"/>
    <line x1="28" y1="140" x2="428" y2="140" class="fig-divider"/>
    <line x1="574" y1="118" x2="574" y2="148" class="fig-line-accent" marker-end="url(#cnq-w3-arr-4)"/>
    <rect x="104" y="70" width="92" height="28" rx="3" class="fig-box"/>
    <rect x="196" y="70" width="44" height="28" rx="3" class="fig-box-blue"/>
    <rect x="240" y="70" width="112" height="28" rx="3" class="fig-box"/>
    <rect x="104" y="104" width="92" height="28" rx="3" class="fig-box"/>
    <rect x="196" y="104" width="44" height="28" rx="3" class="fig-box-purple"/>
    <rect x="240" y="104" width="112" height="28" rx="3" class="fig-box"/>
    <rect x="104" y="148" width="92" height="28" rx="3" class="fig-box"/>
    <rect x="196" y="148" width="44" height="28" rx="3" class="fig-box-blue"/>
    <rect x="240" y="148" width="112" height="28" rx="3" class="fig-box-orange"/>
    <rect x="104" y="182" width="92" height="28" rx="3" class="fig-box"/>
    <rect x="196" y="182" width="44" height="28" rx="3" class="fig-box-blue"/>
    <rect x="240" y="182" width="112" height="28" rx="3" class="fig-box-red"/>
    <rect x="28" y="220" width="400" height="36" rx="4" class="fig-box-green"/>
    <rect x="464" y="84" width="220" height="32" rx="4" class="fig-box-blue"/>
    <rect x="464" y="152" width="106" height="40" rx="4" class="fig-box-green"/>
    <rect x="578" y="152" width="106" height="40" rx="4" class="fig-box-green"/>
    <rect x="464" y="204" width="220" height="84" rx="4" class="fig-box"/>
    <text x="16" y="28" font-size="13" class="fig-title">由 IP + 掩码按位与求子网四要素（例：192.168.1.100/26）</text>
    <text x="452" y="28" font-size="13" class="fig-title">划分子网后可用数减 2</text>
    <text x="28" y="59" font-size="11" class="fig-muted">展开第 4 字节（前 3 字节不变，借 2 位子网号、剩 6 位主机号）：</text>
    <text x="28" y="89" font-size="11.5" class="fig-label">IP 地址</text>
    <text x="150" y="89" font-size="11" text-anchor="middle" class="fig-label">192.168.1.</text>
    <text x="218" y="89" font-size="11.5" text-anchor="middle" class="fig-blue">01</text>
    <text x="296" y="89" font-size="11.5" text-anchor="middle" class="fig-label">100100</text>
    <text x="360" y="89" font-size="11" class="fig-muted">= .100</text>
    <text x="28" y="123" font-size="11.5" class="fig-label">子网掩码</text>
    <text x="150" y="123" font-size="11" text-anchor="middle" class="fig-label">255.255.255.</text>
    <text x="218" y="123" font-size="11.5" text-anchor="middle" class="fig-purple">11</text>
    <text x="296" y="123" font-size="11.5" text-anchor="middle" class="fig-label">000000</text>
    <text x="360" y="123" font-size="11" class="fig-muted">= .192</text>
    <text x="28" y="167" font-size="11.5" class="fig-orange">网络地址</text>
    <text x="150" y="167" font-size="11" text-anchor="middle" class="fig-label">192.168.1.</text>
    <text x="218" y="167" font-size="11.5" text-anchor="middle" class="fig-blue">01</text>
    <text x="296" y="167" font-size="11.5" text-anchor="middle" class="fig-orange">000000 (全0)</text>
    <text x="360" y="167" font-size="11" class="fig-orange">= .64/26</text>
    <text x="28" y="201" font-size="11.5" class="fig-red">广播地址</text>
    <text x="150" y="201" font-size="11" text-anchor="middle" class="fig-label">192.168.1.</text>
    <text x="218" y="201" font-size="11.5" text-anchor="middle" class="fig-blue">01</text>
    <text x="296" y="201" font-size="11.5" text-anchor="middle" class="fig-red">111111 (全1)</text>
    <text x="360" y="201" font-size="11" class="fig-red">= .127</text>
    <text x="228" y="243" font-size="11.5" text-anchor="middle" class="fig-green">可分配地址：192.168.1.65 ~ .126（共 2^6 - 2 = 62 个）</text>
    <text x="28" y="280" font-size="11" class="fig-muted">规律：按位与 (&amp;) 清零主机位得网络号；主机位置 1 得直接广播</text>
    <text x="464" y="68" font-size="11" class="fig-muted">原 /24：可用 .1 ~ .254 (254 个)</text>
    <text x="574" y="105" font-size="11" text-anchor="middle" class="fig-blue">.0(网) | .1 ~ .254 | .255(广)</text>
    <text x="590" y="138" font-size="10.5" class="fig-accent">借 1 位二分</text>
    <text x="517" y="169" font-size="10.5" text-anchor="middle" class="fig-green">子网 0 (/25)</text>
    <text x="517" y="184" font-size="10" text-anchor="middle" class="fig-orange">.127 变广播</text>
    <text x="631" y="169" font-size="10.5" text-anchor="middle" class="fig-green">子网 1 (/25)</text>
    <text x="631" y="184" font-size="10" text-anchor="middle" class="fig-orange">.128 变网号</text>
    <text x="474" y="225" font-size="11" class="fig-label">• 总地址数：128×2 = 256 (不变)</text>
    <text x="474" y="247" font-size="11" class="fig-orange">• .127 与 .128 转为广播/网络号</text>
    <text x="474" y="269" font-size="11" class="fig-green">• 可分配数：126×2 = 252 (少 2)</text>
  </svg>
  <figcaption>图 5-4：IP 地址与子网掩码按位逻辑与求解子网四要素及子网二分后可分配地址数减 2 的原因</figcaption>
</figure>

### 5.3 定长与变长子网划分（二叉树法）

| 对比项 | 定长子网划分 | 变长子网划分 |
|---|---|---|
| 子网掩码 | 全部相同 | 可以不同 |
| 网络前缀长度 | 全部相同 | 可以不同 |
| 子网个数 | 只能是 $2^{k}$ 个 | 可以是任意个（非 2 的幂也行） |
| 每个子网地址数 | 相同 | 可能不同 |
| 灵活性 | 低，只适合规模相同的子网 | 高，接近按需分配 |
| 地址利用率 | 可能较低 | 较高 |

<strong>统一用二叉树看</strong>：把待划分的 CIDR 地址块作为<strong>根节点</strong>，从主机号中<strong>取走 1 位</strong>即分成两个子块（取 0 与取 1），取走的这一位与原网络前缀共同构成子块的网络前缀，前缀长度 +1、每块地址数减半。继续往下分就是继续均分。

例：`/24` 地址块含 $2^{8}=256$ 个地址。

- 借 1 位 → 2 个 `/25`，各 $2^{7}=128$ 个；
- 借 2 位 → 4 个 `/26`，各 $2^{32-26}=2^{6}=64$ 个，掩码 255.255.255.192；
- 要“恰好分成 4 个子块且不考虑各网络所需地址数”，最简做法就是定长划分，取同一层的 4 个节点。

<figure class="cpd-figure not-content" id="cnq-w3-fig-5">
  <svg viewBox="0 0 720 356" role="img" aria-label="变长子网划分 VLSM 二叉树堵水法：将 192.168.1.0/24 划分为 3 个 /26 与 2 个 /27 子网">
    <rect x="24" y="248" width="380" height="40" rx="5" class="fig-box-subtle"/>
    <rect x="24" y="300" width="672" height="42" rx="6" class="fig-box-subtle"/>
    <line x1="324" y1="78" x2="164" y2="108" class="fig-line"/>
    <line x1="324" y1="78" x2="483" y2="108" class="fig-line"/>
    <line x1="164" y1="144" x2="90" y2="176" class="fig-line" stroke="var(--sl-color-green)"/>
    <line x1="164" y1="144" x2="238" y2="176" class="fig-line" stroke="var(--sl-color-green)"/>
    <line x1="483" y1="144" x2="406" y2="176" class="fig-line" stroke="var(--sl-color-green)"/>
    <line x1="483" y1="144" x2="560" y2="176" class="fig-line-dashed"/>
    <line x1="560" y1="216" x2="489" y2="248" class="fig-line" stroke="var(--sl-color-blue)"/>
    <line x1="560" y1="216" x2="631" y2="248" class="fig-line" stroke="var(--sl-color-blue)"/>
    <rect x="244" y="42" width="160" height="36" rx="5" class="fig-box-purple"/>
    <rect x="96" y="108" width="136" height="36" rx="4" class="fig-box"/>
    <rect x="415" y="108" width="136" height="36" rx="4" class="fig-box"/>
    <rect x="24" y="176" width="132" height="40" rx="4" class="fig-box-green"/>
    <rect x="172" y="176" width="132" height="40" rx="4" class="fig-box-green"/>
    <rect x="340" y="176" width="132" height="40" rx="4" class="fig-box-green"/>
    <rect x="494" y="176" width="132" height="40" rx="4" class="fig-box-dashed"/>
    <rect x="424" y="248" width="130" height="40" rx="4" class="fig-box-blue"/>
    <rect x="566" y="248" width="130" height="40" rx="4" class="fig-box-blue"/>
    <rect x="230" y="84" width="22" height="16" rx="3" class="fig-box"/>
    <rect x="396" y="84" width="22" height="16" rx="3" class="fig-box"/>
    <rect x="114" y="152" width="22" height="16" rx="3" class="fig-box"/>
    <rect x="192" y="152" width="22" height="16" rx="3" class="fig-box"/>
    <rect x="432" y="152" width="22" height="16" rx="3" class="fig-box"/>
    <rect x="512" y="152" width="22" height="16" rx="3" class="fig-box"/>
    <rect x="512" y="224" width="22" height="16" rx="3" class="fig-box"/>
    <rect x="586" y="224" width="22" height="16" rx="3" class="fig-box"/>
    <text x="24" y="28" font-size="13" class="fig-title">变长子网划分（VLSM）二叉树“堵水法”：192.168.1.0/24 划为 5 个子块</text>
    <text x="324" y="57" font-size="11.5" text-anchor="middle" class="fig-purple">192.168.1.0/24</text>
    <text x="324" y="72" font-size="10.5" text-anchor="middle" class="fig-muted">根块：256 个地址 (.0~.255)</text>
    <text x="241" y="96" font-size="10.5" text-anchor="middle" class="fig-muted">0</text>
    <text x="407" y="96" font-size="10.5" text-anchor="middle" class="fig-muted">1</text>
    <text x="164" y="123" font-size="11" text-anchor="middle" class="fig-label">前缀 +0 → .0/25</text>
    <text x="164" y="138" font-size="10" text-anchor="middle" class="fig-muted">128 个 (.0~.127)</text>
    <text x="483" y="123" font-size="11" text-anchor="middle" class="fig-label">前缀 +1 → .128/25</text>
    <text x="483" y="138" font-size="10" text-anchor="middle" class="fig-muted">128 个 (.128~.255)</text>
    <text x="125" y="164" font-size="10.5" text-anchor="middle" class="fig-green">0</text>
    <text x="203" y="164" font-size="10.5" text-anchor="middle" class="fig-green">1</text>
    <text x="443" y="164" font-size="10.5" text-anchor="middle" class="fig-green">0</text>
    <text x="523" y="164" font-size="10.5" text-anchor="middle" class="fig-muted">1</text>
    <text x="90" y="193" font-size="11" text-anchor="middle" class="fig-green">① 00 → .0/26</text>
    <text x="90" y="209" font-size="10" text-anchor="middle" class="fig-muted">64 个 (.0~.63)</text>
    <text x="238" y="193" font-size="11" text-anchor="middle" class="fig-green">② 01 → .64/26</text>
    <text x="238" y="209" font-size="10" text-anchor="middle" class="fig-muted">64 个 (.64~.127)</text>
    <text x="406" y="193" font-size="11" text-anchor="middle" class="fig-green">③ 10 → .128/26</text>
    <text x="406" y="209" font-size="10" text-anchor="middle" class="fig-muted">64 个 (.128~.191)</text>
    <text x="560" y="193" font-size="11" text-anchor="middle" class="fig-muted">11 → .192/26</text>
    <text x="560" y="209" font-size="10" text-anchor="middle" class="fig-orange">继续二分（不选中）</text>
    <text x="523" y="236" font-size="10.5" text-anchor="middle" class="fig-blue">0</text>
    <text x="597" y="236" font-size="10.5" text-anchor="middle" class="fig-blue">1</text>
    <text x="489" y="265" font-size="11" text-anchor="middle" class="fig-blue">④ 110 → .192/27</text>
    <text x="489" y="281" font-size="10" text-anchor="middle" class="fig-muted">32 个 (.192~.223)</text>
    <text x="631" y="265" font-size="11" text-anchor="middle" class="fig-blue">⑤ 111 → .224/27</text>
    <text x="631" y="281" font-size="10" text-anchor="middle" class="fig-muted">32 个 (.224~.255)</text>
    <text x="36" y="265" font-size="10.5" class="fig-label">定长划分：同层全选，仅能分出 2^k 个等大子网</text>
    <text x="36" y="281" font-size="10.5" class="fig-muted">变长划分：跨层按需截取，灵活分出 5 个子网</text>
    <text x="36" y="317" font-size="11" class="fig-label">选中 3 个 /26（绿）+ 2 个 /27（蓝）：64×3 + 32×2 = 256 全覆盖（不漏水），同分支无祖先/后代并存（不重堵）</text>
    <text x="36" y="334" font-size="11" class="fig-muted">全网可分配主机总数 = (64 - 2)×3 + (32 - 2)×2 = 246 台（共扣除 5 个子网的网络地址与广播地址共 10 个）</text>
  </svg>
  <figcaption>图 5-5：用二叉树“堵水法”进行变长子网划分（VLSM）——前缀逐层加 1、地址块逐层折半</figcaption>
</figure>

<strong>变长划分的选取规则（“堵水法”）</strong>：在二叉树上选取子块时——

1. <strong>不能漏水</strong>：所有叶子必须被选中的子块覆盖，不能有地址块没分出去；
2. <strong>同一分支上不能重复堵水</strong>：一个节点若被选中，其祖先或后代不能再被选中（否则应合并或应继续细分）。

即：选中的子块构成原地址块的一个<strong>划分</strong>。例：要恰好划分出 5 个（非 2 的幂）子块，就只能变长划分，可取其一半按 `/26` 分、另一半继续分成 `/26` 与两个 `/27`。

<strong>子块的 CIDR 写法</strong>：原网络前缀 + 从主机号取走的若干位。例如取走两位且取值为 `00`，子块前缀长度即为原前缀 + 2。

> <strong>★考点</strong>：现代网络设计应<strong>优先采用变长子网划分</strong>；408 目前不考“让你选策略”，而是直接给定划分条件，考的是把条件落到二叉树上。

<figure class="cpd-figure not-content" id="cnq-w3-fig-6">
  <svg viewBox="0 0 712 318" role="img" aria-label="CIDR 路由聚合最长公共前缀计算与路由器最长前缀匹配查表转发">
    <defs>
      <marker id="cnq-w3-arr-6" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="40" width="342" height="262" rx="6" class="fig-box-subtle"/>
    <rect x="370" y="40" width="326" height="262" rx="6" class="fig-box-subtle"/>
    <line x1="234" y1="62" x2="234" y2="188" class="fig-line-dashed"/>
    <line x1="187" y1="186" x2="187" y2="206" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#cnq-w3-arr-6)"/>
    <rect x="28" y="68" width="124" height="24" rx="3" class="fig-box"/>
    <rect x="152" y="68" width="80" height="24" rx="3" class="fig-box-green"/>
    <rect x="236" y="68" width="44" height="24" rx="3" class="fig-box"/>
    <rect x="280" y="68" width="66" height="24" rx="3" class="fig-box"/>
    <rect x="28" y="96" width="124" height="24" rx="3" class="fig-box"/>
    <rect x="152" y="96" width="80" height="24" rx="3" class="fig-box-green"/>
    <rect x="236" y="96" width="44" height="24" rx="3" class="fig-box"/>
    <rect x="280" y="96" width="66" height="24" rx="3" class="fig-box"/>
    <rect x="28" y="124" width="124" height="24" rx="3" class="fig-box"/>
    <rect x="152" y="124" width="80" height="24" rx="3" class="fig-box-green"/>
    <rect x="236" y="124" width="44" height="24" rx="3" class="fig-box"/>
    <rect x="280" y="124" width="66" height="24" rx="3" class="fig-box"/>
    <rect x="28" y="152" width="124" height="24" rx="3" class="fig-box"/>
    <rect x="152" y="152" width="80" height="24" rx="3" class="fig-box-green"/>
    <rect x="236" y="152" width="44" height="24" rx="3" class="fig-box"/>
    <rect x="280" y="152" width="66" height="24" rx="3" class="fig-box"/>
    <rect x="28" y="208" width="318" height="44" rx="5" class="fig-box-green"/>
    <rect x="382" y="52" width="302" height="30" rx="4" class="fig-box-purple"/>
    <rect x="382" y="92" width="302" height="34" rx="4" class="fig-box"/>
    <rect x="382" y="132" width="302" height="34" rx="4" class="fig-box-blue"/>
    <rect x="382" y="172" width="302" height="34" rx="4" class="fig-box"/>
    <rect x="382" y="212" width="302" height="34" rx="4" class="fig-box"/>
    <rect x="382" y="256" width="302" height="34" rx="4" class="fig-box-green"/>
    <text x="16" y="28" font-size="13" class="fig-title">① CIDR 路由聚合（提取最长公共前缀）</text>
    <text x="370" y="28" font-size="13" class="fig-title">② 转发表查表：最长前缀匹配原则</text>
    <text x="28" y="58" font-size="10.5" class="fig-muted">展开第 3 字节二进制（前 16 位均为 206.0）：</text>
    <text x="90" y="84" font-size="10.5" text-anchor="middle" class="fig-label">206.0.68.0/24</text>
    <text x="192" y="84" font-size="11" text-anchor="middle" class="fig-green">010001</text>
    <text x="258" y="84" font-size="11" text-anchor="middle" class="fig-label">00</text>
    <text x="313" y="84" font-size="10.5" text-anchor="middle" class="fig-muted">.00000000</text>
    <text x="90" y="112" font-size="10.5" text-anchor="middle" class="fig-label">206.0.69.0/24</text>
    <text x="192" y="112" font-size="11" text-anchor="middle" class="fig-green">010001</text>
    <text x="258" y="112" font-size="11" text-anchor="middle" class="fig-label">01</text>
    <text x="313" y="112" font-size="10.5" text-anchor="middle" class="fig-muted">.00000000</text>
    <text x="90" y="140" font-size="10.5" text-anchor="middle" class="fig-label">206.0.70.0/24</text>
    <text x="192" y="140" font-size="11" text-anchor="middle" class="fig-green">010001</text>
    <text x="258" y="140" font-size="11" text-anchor="middle" class="fig-label">10</text>
    <text x="313" y="140" font-size="10.5" text-anchor="middle" class="fig-muted">.00000000</text>
    <text x="90" y="168" font-size="10.5" text-anchor="middle" class="fig-label">206.0.71.0/24</text>
    <text x="192" y="168" font-size="11" text-anchor="middle" class="fig-green">010001</text>
    <text x="258" y="168" font-size="11" text-anchor="middle" class="fig-label">11</text>
    <text x="313" y="168" font-size="10.5" text-anchor="middle" class="fig-muted">.00000000</text>
    <text x="242" y="196" font-size="10" class="fig-muted">前 16+6=22 位全同</text>
    <text x="187" y="226" font-size="11.5" text-anchor="middle" class="fig-green">聚合超网：206.0.68.0/22</text>
    <text x="187" y="243" font-size="10" text-anchor="middle" class="fig-muted">掩码 255.255.252.0（4 条明细合并为 1 条）</text>
    <text x="28" y="272" font-size="10.5" class="fig-orange">口诀：找最长公共前缀位 /k，剩余 32-k 位全清 0</text>
    <text x="28" y="290" font-size="10.5" class="fig-muted">效果：缩减路由表规模，抑制拓扑抖动扩散</text>
    <text x="533" y="72" font-size="11.5" text-anchor="middle" class="fig-purple">待转发目的 IP = 206.0.71.130</text>
    <text x="392" y="113" font-size="11" class="fig-label">206.0.68.0/22 → E0</text>
    <text x="674" y="113" font-size="10.5" text-anchor="end" class="fig-green">✔ 匹配 22 位</text>
    <text x="392" y="153" font-size="11" class="fig-blue">206.0.71.128/25 → E1</text>
    <text x="674" y="153" font-size="10.5" text-anchor="end" class="fig-blue">★ 匹配 25 位（最长）</text>
    <text x="392" y="193" font-size="11" class="fig-muted">206.0.70.0/24 → E2</text>
    <text x="674" y="193" font-size="10.5" text-anchor="end" class="fig-red">✘ 第 23 位失配</text>
    <text x="392" y="233" font-size="11" class="fig-label">0.0.0.0/0 (默认) → E3</text>
    <text x="674" y="233" font-size="10.5" text-anchor="end" class="fig-orange">✔ 匹配 0 位（兜底）</text>
    <text x="533" y="278" font-size="11.5" text-anchor="middle" class="fig-green">命中多条时选网络前缀最长 (/25) → 接口 E1</text>
  </svg>
  <figcaption>图 5-6：CIDR 路由聚合（最长公共前缀构造超网）与路由器“最长前缀匹配”查表机制</figcaption>
</figure>

## 第6章 网络层：ICMP、NAT 与 OSPF

### 6.1 网际控制报文协议 ICMP

ICMP 是网际层的重要协议，主机或路由器用它发送<strong>差错报告报文</strong>与<strong>询问报文</strong>。ICMP 报文封装在 IPv4 数据报中，<strong>IPv4 首部协议字段值为 1</strong>。

<figure class="cpd-figure not-content" id="cnq-w3-fig-7">
  <svg viewBox="0 0 712 344" role="img" aria-label="ICMP 报文分类体系与差错报告报文的 IPv4 载荷封装结构">
    <defs>
      <marker id="cnq-w3-arr-7" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="284" width="680" height="46" rx="6" class="fig-box-subtle"/>
    <path d="M 140 153 L 158 153 L 158 123 L 174 123" class="fig-line" marker-end="url(#cnq-w3-arr-7)"/>
    <path d="M 140 153 L 158 153 L 158 241 L 174 241" class="fig-line" marker-end="url(#cnq-w3-arr-7)"/>
    <path d="M 308 123 L 326 123 L 326 55 L 342 55" class="fig-line"/>
    <path d="M 326 89 L 342 89" class="fig-line"/>
    <path d="M 308 123 L 342 123" class="fig-line"/>
    <path d="M 326 123 L 326 191 L 342 191" class="fig-line"/>
    <path d="M 326 157 L 342 157" class="fig-line"/>
    <path d="M 308 241 L 326 241 L 326 225 L 342 225" class="fig-line"/>
    <path d="M 326 241 L 326 259 L 342 259" class="fig-line"/>
    <rect x="16" y="132" width="124" height="42" rx="5" class="fig-box-purple"/>
    <rect x="176" y="104" width="132" height="38" rx="5" class="fig-box-red"/>
    <rect x="176" y="222" width="132" height="38" rx="5" class="fig-box-green"/>
    <rect x="344" y="40" width="352" height="30" rx="4" class="fig-box-red"/>
    <rect x="344" y="74" width="352" height="30" rx="4" class="fig-box-orange"/>
    <rect x="344" y="108" width="352" height="30" rx="4" class="fig-box-orange"/>
    <rect x="344" y="142" width="352" height="30" rx="4" class="fig-box-subtle"/>
    <rect x="344" y="176" width="352" height="30" rx="4" class="fig-box-blue"/>
    <rect x="344" y="210" width="352" height="30" rx="4" class="fig-box-green"/>
    <rect x="344" y="244" width="352" height="30" rx="4" class="fig-box-green"/>
    <rect x="134" y="293" width="140" height="28" rx="3" class="fig-box-purple"/>
    <rect x="274" y="293" width="104" height="28" rx="3" class="fig-box-red"/>
    <rect x="378" y="293" width="144" height="28" rx="3" class="fig-box-orange"/>
    <rect x="522" y="293" width="162" height="28" rx="3" class="fig-box-blue"/>
    <text x="16" y="26" font-size="13" class="fig-title">ICMP 报文分类体系与差错报告封装格式</text>
    <text x="78" y="150" font-size="12" text-anchor="middle" class="fig-purple">ICMP 报文</text>
    <text x="78" y="166" font-size="10.5" text-anchor="middle" class="fig-muted">IP 协议字段 = 1</text>
    <text x="242" y="128" font-size="12" text-anchor="middle" class="fig-red">差错报告报文 (5)</text>
    <text x="242" y="246" font-size="12" text-anchor="middle" class="fig-green">询问报文 (2)</text>
    <text x="356" y="60" font-size="11.5" class="fig-red">① 目的不可达（网络 / 主机 / 协议 / 端口不可达）</text>
    <text x="356" y="94" font-size="11.5" class="fig-orange">② 时间超过（路由器 TTL 减至 0 / 终点分片重组超时）</text>
    <text x="356" y="128" font-size="11.5" class="fig-orange">③ 参数问题（IPv4 首部字段或选项非法）</text>
    <text x="356" y="162" font-size="11.5" class="fig-muted">④ 源点抑制（拥塞降速通知，RFC 6633 已废弃）</text>
    <text x="356" y="196" font-size="11.5" class="fig-blue">⑤ 改变路由 / 重定向（路由器指示主机改走更优下一跳）</text>
    <text x="356" y="230" font-size="11.5" class="fig-green">① 回送请求与回答（Echo Request/Reply，ping 使用）</text>
    <text x="356" y="264" font-size="11.5" class="fig-green">② 时间戳请求与回答（自 1900-01-01 秒数，测延迟/校时）</text>
    <text x="28" y="311" font-size="11" class="fig-label">差错封装：</text>
    <text x="204" y="311" font-size="10.5" text-anchor="middle" class="fig-purple">外层 IP 首部 (协议=1)</text>
    <text x="326" y="311" font-size="10.5" text-anchor="middle" class="fig-red">ICMP 首部 8B</text>
    <text x="450" y="311" font-size="10.5" text-anchor="middle" class="fig-orange">出错原 IP 首部</text>
    <text x="603" y="311" font-size="10.5" text-anchor="middle" class="fig-blue">原载荷前 8B (含端口号)</text>
  </svg>
  <figcaption>图 6-1：ICMP 两大类七种报文体系及差错报告报文封装“原 IP 首部 + 前 8 字节传输层信息”结构</figcaption>
</figure>

<strong>差错报告报文</strong>：

- <strong>目的不可达</strong>（目标/终点不可达）：路由器查表转发找不到匹配条目，丢弃并回送；细分目的网络不可达、目的主机不可达、目的协议不可达、目的端口不可达。
- <strong>时间超过</strong>（超时）：路由器收到目的 IP 不是自己的数据报，将首部 <strong>TTL 减 1</strong>；结果不为 0 可继续转发，为 0 则丢弃并回送。另外终点在规定时间内未收齐全部分片时，把已收到的分片丢弃并回送。
- <strong>参数问题</strong>：首部有某种错误导致无法处理，如版本号不是 4、无法识别的选项、长度与声称不符的选项。
- <strong>源点抑制</strong>：路由器/主机因拥塞丢包时通知源点降速。机制简单粗暴、效率低，已被 <strong>RFC 6633（2012 年 7 月）废弃</strong>；除 2010 年题 36 外不再考。现在因拥塞丢弃也改为<strong>静默丢弃</strong>。
- <strong>改变路由（重定向）</strong>：路由器发现主机选择的下一跳并非最优，通知其改走更佳路由。

> <strong>注意</strong>：若路由器或目的主机用因特网校验和校验 IPv4 首部时<strong>发现首部误码</strong>，则<strong>静默丢弃</strong>——不发 ICMP 差错报告报文。

<strong>不应发送 ICMP 差错报告报文的情况</strong>（多选题式单选题的高发陷阱）：① 对 ICMP 差错报告报文本身不再发送；② 对不是第一个分片的数据报分片不发送；③ 对具有多播地址的数据报不发送；④ 对特殊地址（如 127.0.0.0 或 0.0.0.0）的数据报不发送。

<strong>询问报文</strong>：<strong>回送请求与回送回答——测试目的站是否可达并了解其状态；时间戳请求与回答</strong>——请求对方回答当前日期时间，回答报文中有 32 比特字段，写入从 <strong>1900 年 1 月 1 日</strong>起至当前的秒数，常用于测量延迟与时间偏差。

<strong>格式与封装</strong>：ICMP 报文用<strong>因特网校验和</strong>校验（IPv4 首部、UDP 用户数据报整报、TCP 报文段整段同理）。对被差错报告的 IPv4 数据报，其<strong>首部 + 数据载荷前 8 字节</strong>作为 ICMP 差错报告报文的数据部分：首部提供网际层关键信息（让源主机知道是哪个数据报引发错误），前 8 字节提供传输层必要信息（TCP/UDP 的源端口、目的端口；TCP 还有序号）。

<strong>典型应用</strong>：

| 应用 | 使用的 ICMP 报文 |
|---|---|
| ping（分组网间探测） | 回送请求 + 回送回答，测试连通性 |
| tracert（Windows） | 时间超过 + 回送请求/回答，逐跳探测路径 |
| traceroute（UNIX/Linux） | 时间超过 + UDP（目的端口选一般不被监听的高端口，触发“目的端口不可达”） |

> <strong>★考点</strong>：ICMP 相关题历年都是送分题，但它是网际层重要协议，不排除深入考察——尤其“哪些情况不发差错报告”和“协议字段 = 1”。

<figure class="cpd-figure not-content" id="cnq-w3-fig-8">
  <svg viewBox="0 0 712 332" role="img" aria-label="不发送 ICMP 差错报告的五种情况与 traceroute 逐跳探测时序图">
    <defs>
      <marker id="cnq-w3-arr-8" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="40" width="282" height="276" rx="6" class="fig-box-subtle"/>
    <rect x="310" y="40" width="386" height="276" rx="6" class="fig-box-subtle"/>
    <line x1="354" y1="80" x2="354" y2="268" class="fig-line-dashed"/>
    <line x1="454" y1="80" x2="454" y2="268" class="fig-line-dashed"/>
    <line x1="554" y1="80" x2="554" y2="268" class="fig-line-dashed"/>
    <line x1="654" y1="80" x2="654" y2="268" class="fig-line-dashed"/>
    <line x1="354" y1="96" x2="452" y2="106" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#cnq-w3-arr-8)"/>
    <line x1="454" y1="118" x2="356" y2="128" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#cnq-w3-arr-8)"/>
    <line x1="354" y1="146" x2="552" y2="158" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#cnq-w3-arr-8)"/>
    <line x1="554" y1="170" x2="356" y2="182" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#cnq-w3-arr-8)"/>
    <line x1="354" y1="202" x2="652" y2="216" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#cnq-w3-arr-8)"/>
    <line x1="654" y1="228" x2="356" y2="242" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#cnq-w3-arr-8)"/>
    <rect x="28" y="54" width="258" height="44" rx="4" class="fig-box-red"/>
    <rect x="28" y="106" width="258" height="44" rx="4" class="fig-box-orange"/>
    <rect x="28" y="158" width="258" height="44" rx="4" class="fig-box-orange"/>
    <rect x="28" y="210" width="258" height="44" rx="4" class="fig-box-orange"/>
    <rect x="28" y="262" width="258" height="44" rx="4" class="fig-box-purple"/>
    <rect x="320" y="54" width="68" height="26" rx="4" class="fig-box-blue"/>
    <rect x="420" y="54" width="68" height="26" rx="4" class="fig-box"/>
    <rect x="520" y="54" width="68" height="26" rx="4" class="fig-box"/>
    <rect x="618" y="54" width="68" height="26" rx="4" class="fig-box-green"/>
    <rect x="366" y="86" width="76" height="16" rx="3" class="fig-box"/>
    <rect x="364" y="114" width="84" height="16" rx="3" class="fig-box"/>
    <rect x="410" y="140" width="88" height="16" rx="3" class="fig-box"/>
    <rect x="404" y="168" width="104" height="16" rx="3" class="fig-box"/>
    <rect x="444" y="196" width="128" height="16" rx="3" class="fig-box"/>
    <rect x="428" y="227" width="154" height="16" rx="3" class="fig-box"/>
    <rect x="322" y="274" width="362" height="32" rx="4" class="fig-box"/>
    <text x="16" y="28" font-size="13" class="fig-title">不发 ICMP 差错的五种高频陷阱</text>
    <text x="310" y="28" font-size="13" class="fig-title">traceroute 递增 TTL 逐跳探测时序</text>
    <text x="38" y="72" font-size="11" class="fig-red">① ICMP 差错报告报文本身出错</text>
    <text x="38" y="89" font-size="10" class="fig-muted">防差错报文递归触发风暴</text>
    <text x="38" y="124" font-size="11" class="fig-orange">② 非第 1 个分片的后续 IP 分片</text>
    <text x="38" y="141" font-size="10" class="fig-muted">后续分片不含传输层端口号前 8B</text>
    <text x="38" y="176" font-size="11" class="fig-orange">③ 具有多播地址 (D 类) 的数据报</text>
    <text x="38" y="193" font-size="10" class="fig-muted">防止大量接收者同时回送差错</text>
    <text x="38" y="228" font-size="11" class="fig-orange">④ 具有特殊地址 (0.0.0.0 / 127.x)</text>
    <text x="38" y="245" font-size="10" class="fig-muted">特殊源或环回报文不产生网际差错</text>
    <text x="38" y="280" font-size="11" class="fig-purple">★ IPv4 首部校验和检出比特误码</text>
    <text x="38" y="297" font-size="10" class="fig-muted">源 IP 本身可能已错，一律静默丢弃</text>
    <text x="354" y="71" font-size="11" text-anchor="middle" class="fig-blue">源 H1</text>
    <text x="454" y="71" font-size="11" text-anchor="middle" class="fig-label">路由 R1</text>
    <text x="554" y="71" font-size="11" text-anchor="middle" class="fig-label">路由 R2</text>
    <text x="652" y="71" font-size="11" text-anchor="middle" class="fig-green">终点 H2</text>
    <text x="404" y="98" font-size="10" text-anchor="middle" class="fig-blue">UDP TTL=1</text>
    <text x="406" y="126" font-size="10" text-anchor="middle" class="fig-orange">超时 (TTL=0)</text>
    <text x="454" y="152" font-size="10" text-anchor="middle" class="fig-blue">UDP TTL=2</text>
    <text x="456" y="180" font-size="10" text-anchor="middle" class="fig-orange">R2 回送超时</text>
    <text x="508" y="208" font-size="10" text-anchor="middle" class="fig-blue">UDP TTL=3 (高端口)</text>
    <text x="505" y="239" font-size="10" text-anchor="middle" class="fig-green">H2 回送端口不可达</text>
    <text x="503" y="294" font-size="10.5" text-anchor="middle" class="fig-label">Windows tracert 改发 ICMP 回送请求，终点回送回答</text>
  </svg>
  <figcaption>图 6-2：不发送 ICMP 差错报告的五类情形与 traceroute 利用 TTL 超时 + 端口不可达定位路径</figcaption>
</figure>

### 6.2 NAT 与 NAPT

<strong>作用</strong>：缓解 IPv4 地址紧张——让大量使用<strong>内部私有地址</strong>的私有网络用户<strong>共享少量外部全球地址</strong>访问因特网。

<strong>三点必须记住</strong>：私有地址块就是 10/8、172.16/12、192.168/16。全世界许多机构的私有网络可以有<strong>相同的私有地址而不冲突，因为这些地址只在机构内部使用；因特网上的路由器对目的地址是私有地址的 IP 数据报一律不转发</strong>（由 ISP 设置其路由器实现）。

> <strong>注意</strong>：上述“一律不转发”的前提是<strong>因特网（公网）</strong>。私有网络<strong>内部</strong>的路由器使用的都是私有地址，其路由表里当然可以有以私有地址为目的网络的条目。

<strong>NAT 路由器：装在私有网络连接因特网的路由器上，至少要有一个有效的外部全球地址（公有地址）</strong>。内网主机与外部通信时，源地址在 NAT 路由器上被换成该公有地址，并记入 <strong>NAT 转换表</strong>。

<figure class="cpd-figure not-content" id="cnq-w3-fig-9">
  <svg viewBox="0 0 712 360" role="img" aria-label="NAPT 网络地址与端口号转换双向报头改写及外网主动发起拦截原理">
    <defs>
      <marker id="cnq-w3-arr-9" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="42" width="196" height="166" rx="6" class="fig-box-subtle"/>
    <rect x="500" y="42" width="196" height="166" rx="6" class="fig-box-subtle"/>
    <rect x="16" y="222" width="680" height="124" rx="6" class="fig-box-subtle"/>
    <line x1="202" y1="92" x2="268" y2="104" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#cnq-w3-arr-9)"/>
    <line x1="202" y1="164" x2="268" y2="144" class="fig-line" stroke="var(--sl-color-purple)" marker-end="url(#cnq-w3-arr-9)"/>
    <line x1="424" y1="84" x2="508" y2="84" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#cnq-w3-arr-9)"/>
    <line x1="510" y1="112" x2="426" y2="112" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#cnq-w3-arr-9)"/>
    <line x1="510" y1="168" x2="426" y2="156" class="fig-line-dashed" stroke="var(--sl-color-red)" marker-end="url(#cnq-w3-arr-9)"/>
    <rect x="26" y="68" width="176" height="48" rx="4" class="fig-box-blue"/>
    <rect x="26" y="140" width="176" height="48" rx="4" class="fig-box-purple"/>
    <rect x="270" y="66" width="154" height="118" rx="6" class="fig-box-orange"/>
    <rect x="510" y="68" width="176" height="54" rx="4" class="fig-box-green"/>
    <rect x="510" y="140" width="176" height="54" rx="4" class="fig-box-red"/>
    <rect x="428" y="62" width="78" height="16" rx="3" class="fig-box"/>
    <rect x="428" y="118" width="78" height="16" rx="3" class="fig-box"/>
    <rect x="430" y="168" width="76" height="16" rx="3" class="fig-box"/>
    <rect x="28" y="232" width="164" height="26" rx="3" class="fig-box"/>
    <rect x="192" y="232" width="204" height="26" rx="3" class="fig-box"/>
    <rect x="396" y="232" width="204" height="26" rx="3" class="fig-box"/>
    <rect x="600" y="232" width="84" height="26" rx="3" class="fig-box"/>
    <rect x="28" y="258" width="164" height="28" rx="2" class="fig-box"/>
    <rect x="192" y="258" width="204" height="28" rx="2" class="fig-box-blue"/>
    <rect x="396" y="258" width="204" height="28" rx="2" class="fig-box-green"/>
    <rect x="600" y="258" width="84" height="28" rx="2" class="fig-box-green"/>
    <rect x="28" y="286" width="164" height="28" rx="2" class="fig-box"/>
    <rect x="192" y="286" width="204" height="28" rx="2" class="fig-box-purple"/>
    <rect x="396" y="286" width="204" height="28" rx="2" class="fig-box-green"/>
    <rect x="600" y="286" width="84" height="28" rx="2" class="fig-box-green"/>
    <text x="16" y="28" font-size="13" class="fig-title">NAPT 双向地址/端口转换与外网主动发起拦截机制</text>
    <text x="114" y="58" font-size="11" text-anchor="middle" class="fig-muted">私有网络 192.168.1.0/24</text>
    <text x="598" y="58" font-size="11" text-anchor="middle" class="fig-muted">因特网（公网）</text>
    <text x="114" y="88" font-size="11" text-anchor="middle" class="fig-blue">H1: 192.168.1.2:50000</text>
    <text x="114" y="105" font-size="10" text-anchor="middle" class="fig-muted">出站改源 → :40000</text>
    <text x="114" y="160" font-size="11" text-anchor="middle" class="fig-purple">H2: 192.168.1.3:50000</text>
    <text x="114" y="177" font-size="10" text-anchor="middle" class="fig-muted">同源端口不冲突 → :40001</text>
    <text x="347" y="92" font-size="12" text-anchor="middle" class="fig-orange">NAPT 路由器</text>
    <text x="347" y="114" font-size="10.5" text-anchor="middle" class="fig-label">内口: 192.168.1.1</text>
    <text x="347" y="132" font-size="10.5" text-anchor="middle" class="fig-label">外口: 203.0.113.1</text>
    <text x="347" y="154" font-size="10" text-anchor="middle" class="fig-muted">同时重算 IP 首部与</text>
    <text x="347" y="169" font-size="10" text-anchor="middle" class="fig-muted">TCP/UDP 校验和</text>
    <text x="467" y="74" font-size="9.5" text-anchor="middle" class="fig-blue">改源 IP:Port</text>
    <text x="467" y="130" font-size="9.5" text-anchor="middle" class="fig-green">查表改目的</text>
    <text x="468" y="180" font-size="9.5" text-anchor="middle" class="fig-red">无表项丢弃</text>
    <text x="598" y="90" font-size="11" text-anchor="middle" class="fig-green">公网服务器 S</text>
    <text x="598" y="108" font-size="10" text-anchor="middle" class="fig-muted">198.51.100.8 : 80</text>
    <text x="598" y="162" font-size="11" text-anchor="middle" class="fig-red">外网主机主动发起</text>
    <text x="598" y="180" font-size="10" text-anchor="middle" class="fig-muted">无静态映射无法进内网</text>
    <text x="110" y="249" font-size="11" text-anchor="middle" class="fig-label">NAPT 转换表项</text>
    <text x="294" y="249" font-size="11" text-anchor="middle" class="fig-label">私有网络侧 (内网 IP : Port)</text>
    <text x="498" y="249" font-size="11" text-anchor="middle" class="fig-label">因特网侧 (全球 IP : Port)</text>
    <text x="642" y="249" font-size="11" text-anchor="middle" class="fig-label">状态</text>
    <text x="110" y="276" font-size="11" text-anchor="middle" class="fig-label">会话 ① (H1 发起)</text>
    <text x="294" y="276" font-size="11" text-anchor="middle" class="fig-blue">192.168.1.2 : 50000</text>
    <text x="498" y="276" font-size="11" text-anchor="middle" class="fig-green">203.0.113.1 : 40000</text>
    <text x="642" y="276" font-size="10.5" text-anchor="middle" class="fig-green">已建立</text>
    <text x="110" y="304" font-size="11" text-anchor="middle" class="fig-label">会话 ② (H2 发起)</text>
    <text x="294" y="304" font-size="11" text-anchor="middle" class="fig-purple">192.168.1.3 : 50000</text>
    <text x="498" y="304" font-size="11" text-anchor="middle" class="fig-green">203.0.113.1 : 40001</text>
    <text x="642" y="304" font-size="10.5" text-anchor="middle" class="fig-green">已建立</text>
    <text x="28" y="334" font-size="11" class="fig-muted">对比：基本 NAT 仅转换 IP（N 个全球地址至多 N 台并发）；NAPT 结合传输层端口号实现多台主机共享单全球 IP</text>
  </svg>
  <figcaption>图 6-3：NAPT 转换表（IP:Port 映射）工作原理及外网主机无法主动向内网发起连接的原因</figcaption>
</figure>

<strong>基本 NAT 的容量</strong>：NAT 路由器有 $N$ 个全球地址，则私有网络<strong>最多同时有 $N$ 台主机</strong>接入因特网；超过就轮流使用。$N$ 越大性能越好，但对全球地址的节约效果越有限。

<strong>NAPT（网络地址和端口号转换）</strong>：把地址转换与<strong>传输层端口号</strong>结合，使大量内网主机<strong>共享一个</strong>全球地址同时通信。因为绝大多数基于 TCP/IP 的应用都用 TCP 或 UDP，端口号足以区分不同会话。人们仍习惯称之为 NAT——看到 “NAT” 要想到它可能<strong>仅指 NAT，也可能包含 NAPT</strong>。

> <strong>★考点</strong>：家用路由器把手机、平板、电脑、物联网设备接入因特网，实际就是一个 <strong>NAPT 路由器</strong>，一般<strong>不运行路由选择协议</strong>。

<strong>局限</strong>：NAT 对网络应用<strong>不完全透明</strong>，会影响某些应用；通信<strong>必须由私有网络内部发起</strong>，因此内网主机<strong>不能直接充当因特网中的服务器</strong>；点对点这类需要外网主动连接内网的应用，需应用自身使用 <strong>NAT 穿透</strong>技术。

> <strong>注意</strong>：NAT 设备还可以修改 ICMP 报文内部的特定字段（如查询标识符）及其包含的 IP 首部来转换地址，核心逻辑与处理 TCP/UDP 相同——建立并维护基于查询标识符的转换表项。彻底解决地址紧张的终极办法仍是过渡到 <strong>128 位的 IPv6</strong>。

### 6.3 开放最短路径优先 OSPF

<strong>定位</strong>：与基于<strong>距离向量</strong>的 RIP 不同，OSPF 基于<strong>链路状态（LS, Link State）</strong>，采用 Dijkstra 的最短路径优先（SPF）算法计算路由。

由此带来三个 RIP 无法达到的优点：① 从算法上保证<strong>不会产生路由环路</strong>；② <strong>不限制网络规模</strong>；③ <strong>更新效率高、收敛速度快</strong>。

<strong>链路状态 LS 指什么</strong>：两部分——路由器<strong>有哪些邻居路由器</strong>；路由器与各邻居之间<strong>链路的代价</strong>（可用距离、时延、带宽度量，由网络管理人员决定）。

> <strong>注意</strong>：408 真题中每段链路的 OSPF 代价都会<strong>直接给定具体数值</strong>，不需要自己算。

<strong>邻居关系的建立与维护</strong>：与相邻路由器交互 <strong>Hello 分组，封装在 IPv4 数据报中，发送周期 10 秒</strong>；若 <strong>40 秒</strong>仍未收到某邻居的 Hello 分组，就认为该邻居不可达。每个路由器据此建立自己的<strong>邻居表</strong>。<strong>Router ID</strong> 是 32 位无符号整数，格式与 IP 地址相同，用于在 OSPF 中唯一标识一台路由器，可手工配置或自动从接口 IP 地址中选取。

<strong>链路状态通告 LSA</strong>：每台路由器都产生，包含两类状态——与<strong>邻居路由器</strong>的链路状态、与<strong>直连网络</strong>的链路状态。产生 LSA 的目的是让<strong>整个 AS 内所有路由器</strong>都知道自己的链路状态。

<strong>可靠洪泛</strong>：LSA 封装在<strong>链路状态更新 LSU 分组</strong>中洪泛发送。洪泛 = 路由器通过<strong>自己的所有接口</strong>向其所有邻居发送 LSU，每个邻居转发时要<strong>把上游路由器排除在外</strong>；“可靠” = 收到 LSU 后要回送<strong>链路状态确认 LSACK 分组</strong>。

<figure class="cpd-figure not-content" id="cnq-w3-fig-10">
  <svg viewBox="0 0 712 336" role="img" aria-label="OSPF 两层区域层次结构：主干区域 0.0.0.0、区域边界路由器 ABR 与自治系统边界路由器 ASBR">
    <defs>
      <marker id="cnq-w3-arr-10" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="116" y="42" width="376" height="86" rx="8" class="fig-box-subtle"/>
    <rect x="16" y="192" width="316" height="80" rx="8" class="fig-box-subtle"/>
    <rect x="364" y="192" width="332" height="80" rx="8" class="fig-box-subtle"/>
    <rect x="16" y="284" width="680" height="36" rx="6" class="fig-box-subtle"/>
    <line x1="224" y1="98" x2="274" y2="98" class="fig-line"/>
    <line x1="354" y1="98" x2="390" y2="98" class="fig-line"/>
    <line x1="478" y1="94" x2="530" y2="76" class="fig-line" stroke="var(--sl-color-purple)" marker-end="url(#cnq-w3-arr-10)"/>
    <line x1="184" y1="114" x2="184" y2="144" class="fig-line" stroke="var(--sl-color-orange)"/>
    <line x1="434" y1="114" x2="434" y2="144" class="fig-line" stroke="var(--sl-color-orange)"/>
    <line x1="184" y1="178" x2="112" y2="224" class="fig-line" stroke="var(--sl-color-green)"/>
    <line x1="184" y1="178" x2="236" y2="224" class="fig-line" stroke="var(--sl-color-green)"/>
    <line x1="434" y1="178" x2="444" y2="224" class="fig-line" stroke="var(--sl-color-green)"/>
    <line x1="434" y1="178" x2="584" y2="224" class="fig-line" stroke="var(--sl-color-green)"/>
    <rect x="532" y="46" width="164" height="54" rx="5" class="fig-box-purple"/>
    <rect x="144" y="82" width="80" height="32" rx="4" class="fig-box-blue"/>
    <rect x="274" y="82" width="80" height="32" rx="4" class="fig-box-blue"/>
    <rect x="390" y="82" width="88" height="32" rx="4" class="fig-box-purple"/>
    <rect x="122" y="144" width="124" height="34" rx="5" class="fig-box-orange"/>
    <rect x="372" y="144" width="124" height="34" rx="5" class="fig-box-orange"/>
    <rect x="54" y="224" width="108" height="34" rx="4" class="fig-box-green"/>
    <rect x="188" y="224" width="118" height="34" rx="4" class="fig-box-green"/>
    <rect x="390" y="224" width="118" height="34" rx="4" class="fig-box-green"/>
    <rect x="536" y="224" width="134" height="34" rx="4" class="fig-box-green"/>
    <rect x="24" y="198" width="256" height="18" rx="3" class="fig-box"/>
    <rect x="372" y="198" width="286" height="18" rx="3" class="fig-box"/>
    <text x="16" y="28" font-size="13" class="fig-title">OSPF 自治系统内两层区域划分与路由器角色体系</text>
    <text x="130" y="64" font-size="12" class="fig-blue">主干区域 Area 0（标识符必须为 0.0.0.0，负责连通各非主干区域）</text>
    <text x="184" y="103" font-size="11" text-anchor="middle" class="fig-blue">R1 (BR)</text>
    <text x="314" y="103" font-size="11" text-anchor="middle" class="fig-blue">R2 (BR)</text>
    <text x="434" y="103" font-size="11" text-anchor="middle" class="fig-purple">R5 (ASBR)</text>
    <text x="614" y="69" font-size="11" text-anchor="middle" class="fig-purple">外部自治系统 (其他 AS)</text>
    <text x="614" y="88" font-size="10" text-anchor="middle" class="fig-muted">运行 BGP 交换外部路由</text>
    <text x="184" y="166" font-size="11" text-anchor="middle" class="fig-orange">R3：区域边界 ABR</text>
    <text x="434" y="166" font-size="11" text-anchor="middle" class="fig-orange">R4：区域边界 ABR</text>
    <text x="30" y="212" font-size="11.5" class="fig-green">区域 1 (Area 0.0.0.1) — 洪泛限本区域内</text>
    <text x="108" y="246" font-size="11" text-anchor="middle" class="fig-green">R6 (区域内 IR)</text>
    <text x="247" y="246" font-size="11" text-anchor="middle" class="fig-green">R7 (区域内 IR)</text>
    <text x="378" y="212" font-size="11.5" class="fig-green">区域 2 (Area 0.0.0.2) — 独立维护区域 LSDB</text>
    <text x="449" y="246" font-size="11" text-anchor="middle" class="fig-green">R8 (区域内 IR)</text>
    <text x="603" y="246" font-size="11" text-anchor="middle" class="fig-green">R9 (区域内 IR)</text>
    <text x="32" y="307" font-size="11" class="fig-label">划分区域核心目的：把链路状态洪泛交换限制在单个区域内部，显著减少大型 AS 内的 OSPF 通信量</text>
  </svg>
  <figcaption>图 6-4：OSPF 层次化区域结构（Area 0 主干区域、ABR 区域边界路由器与 ASBR 自治系统边界路由器）</figcaption>
</figure>

<strong>五种分组</strong>：

| 分组 | 作用 |
|---|---|
| Hello（问候） | 建立、维护邻居关系，10 s 周期 |
| DBD（数据库描述） | 把自己 LSDB 中所有链路状态项目的<strong>摘要</strong>发给邻居 |
| LSR（链路状态请求） | 发现自己缺少某些项目，向邻居请求详细信息 |
| LSU（链路状态更新） | 把对方缺少的链路状态项目详细信息发过去；可靠洪泛时也用它 |
| LSACK（链路状态确认） | 对收到的 LSU 回送确认 |

<strong>工作过程</strong>：Hello 建邻居 → 互发 DBD 摘要 → 一方发现缺项就发 LSR → 对方用 LSU 补齐 → 收到后更新自己的 LSDB 并回 LSACK。双方 LSDB 最终一致，称为 <strong>LSDB 同步</strong>。此后<strong>每 30 分钟或链路状态发生变化</strong>时，路由器重新洪泛 LSU，其他路由器据此更新 LSDB、继续洪泛并回 LSACK。

<strong>封装</strong>：OSPF 分组<strong>直接封装在 IP 数据报中，协议字段值为 89</strong>，目的 IP 地址一般为<strong>多播地址</strong>。从 PDU 逐层封装看，RIP、BGP 属应用层（分别封装在 UDP、TCP 中），OSPF 属网际层；但从路由选择协议的核心功能看，三者都属于网络层。

<strong>DR 与 BDR</strong>：在多点接入网络（如多个 OSPF 路由器连在同一以太网）中，任意两台都互为邻居，邻居关系数为组合数 $n(n-1)/2$，将产生大量多播的 Hello、LSU 分组。为此选举<strong>指定路由器 DR</strong> 与<strong>备用指定路由器 BDR</strong>，普通路由器只与 DR/BDR 建立邻居关系，普通路由器之间<strong>不能直接交换</strong>信息而必须经 DR 或 BDR 中转，从而显著减少分组数量。DR 出问题由 BDR 顶替；选举方法不作要求。

<figure class="cpd-figure not-content" id="cnq-w3-fig-11">
  <svg viewBox="0 0 712 332" role="img" aria-label="OSPF 五种分组 LSDB 同步交互过程与多点接入网络 DR BDR 邻居优化">
    <defs>
      <marker id="cnq-w3-arr-11" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="40" width="346" height="276" rx="6" class="fig-box-subtle"/>
    <rect x="374" y="40" width="322" height="276" rx="6" class="fig-box-subtle"/>
    <line x1="80" y1="80" x2="80" y2="266" class="fig-line-dashed"/>
    <line x1="298" y1="80" x2="298" y2="266" class="fig-line-dashed"/>
    <line x1="84" y1="98" x2="294" y2="98" class="fig-line" stroke="var(--sl-color-green)" marker-start="url(#cnq-w3-arr-11)" marker-end="url(#cnq-w3-arr-11)"/>
    <line x1="84" y1="132" x2="294" y2="132" class="fig-line" stroke="var(--sl-color-purple)" marker-start="url(#cnq-w3-arr-11)" marker-end="url(#cnq-w3-arr-11)"/>
    <line x1="80" y1="164" x2="296" y2="172" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#cnq-w3-arr-11)"/>
    <line x1="298" y1="198" x2="82" y2="206" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#cnq-w3-arr-11)"/>
    <line x1="80" y1="232" x2="296" y2="240" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#cnq-w3-arr-11)"/>
    <line x1="535" y1="116" x2="535" y2="140" class="fig-line-accent" marker-end="url(#cnq-w3-arr-11)"/>
    <line x1="431" y1="224" x2="462" y2="178" class="fig-line" stroke="var(--sl-color-orange)"/>
    <line x1="535" y1="224" x2="462" y2="178" class="fig-line" stroke="var(--sl-color-orange)"/>
    <line x1="639" y1="224" x2="462" y2="178" class="fig-line" stroke="var(--sl-color-orange)"/>
    <line x1="431" y1="224" x2="608" y2="178" class="fig-line-dashed" stroke="var(--sl-color-purple)"/>
    <line x1="535" y1="224" x2="608" y2="178" class="fig-line-dashed" stroke="var(--sl-color-purple)"/>
    <line x1="639" y1="224" x2="608" y2="178" class="fig-line-dashed" stroke="var(--sl-color-purple)"/>
    <rect x="40" y="54" width="80" height="26" rx="4" class="fig-box-blue"/>
    <rect x="258" y="54" width="80" height="26" rx="4" class="fig-box-blue"/>
    <rect x="112" y="88" width="154" height="18" rx="3" class="fig-box"/>
    <rect x="112" y="122" width="154" height="18" rx="3" class="fig-box"/>
    <rect x="112" y="158" width="154" height="18" rx="3" class="fig-box"/>
    <rect x="112" y="192" width="154" height="18" rx="3" class="fig-box"/>
    <rect x="112" y="226" width="154" height="18" rx="3" class="fig-box"/>
    <rect x="28" y="272" width="322" height="32" rx="4" class="fig-box-green"/>
    <rect x="388" y="64" width="294" height="50" rx="4" class="fig-box-red"/>
    <rect x="400" y="144" width="124" height="34" rx="4" class="fig-box-orange"/>
    <rect x="546" y="144" width="124" height="34" rx="4" class="fig-box-purple"/>
    <rect x="390" y="224" width="82" height="30" rx="4" class="fig-box"/>
    <rect x="494" y="224" width="82" height="30" rx="4" class="fig-box"/>
    <rect x="598" y="224" width="82" height="30" rx="4" class="fig-box"/>
    <rect x="388" y="264" width="294" height="40" rx="4" class="fig-box"/>
    <text x="16" y="28" font-size="13" class="fig-title">OSPF 五种分组 LSDB 同步握手</text>
    <text x="374" y="28" font-size="13" class="fig-title">广播网选举 DR/BDR 降邻接数</text>
    <text x="80" y="71" font-size="11" text-anchor="middle" class="fig-blue">路由器 R1</text>
    <text x="298" y="71" font-size="11" text-anchor="middle" class="fig-blue">路由器 R2</text>
    <text x="189" y="101" font-size="10" text-anchor="middle" class="fig-green">① Hello 建邻 (10s/40s)</text>
    <text x="189" y="135" font-size="10" text-anchor="middle" class="fig-purple">② DBD 交换 LSDB 摘要</text>
    <text x="189" y="171" font-size="10" text-anchor="middle" class="fig-orange">③ LSR 请求缺失项</text>
    <text x="189" y="205" font-size="10" text-anchor="middle" class="fig-blue">④ LSU 发送完整 LSA</text>
    <text x="189" y="239" font-size="10" text-anchor="middle" class="fig-green">⑤ LSACK 确认收到</text>
    <text x="189" y="292" font-size="10.5" text-anchor="middle" class="fig-green">同步后：状态变化或每 30 min 洪泛 LSU+LSACK</text>
    <text x="535" y="84" font-size="10.5" text-anchor="middle" class="fig-red">无 DR 时：n 台两两建邻 = n(n-1)/2 对</text>
    <text x="535" y="102" font-size="10" text-anchor="middle" class="fig-muted">例：6 台以太网路由器需维护 15 对邻接</text>
    <text x="462" y="165" font-size="11" text-anchor="middle" class="fig-orange">指定路由器 DR</text>
    <text x="608" y="165" font-size="11" text-anchor="middle" class="fig-purple">备用 BDR</text>
    <text x="431" y="243" font-size="10.5" text-anchor="middle" class="fig-label">DROther A</text>
    <text x="535" y="243" font-size="10.5" text-anchor="middle" class="fig-label">DROther B</text>
    <text x="639" y="243" font-size="10.5" text-anchor="middle" class="fig-label">DROther C</text>
    <text x="535" y="281" font-size="10" text-anchor="middle" class="fig-label">普通路由器仅与 DR/BDR 同步（彼此不直通）</text>
    <text x="535" y="297" font-size="10" text-anchor="middle" class="fig-muted">复杂度由 O(n^2) 降至 O(n)；DR 失效由 BDR 顶替</text>
  </svg>
  <figcaption>图 6-5：OSPF 五种分组（Hello/DBD/LSR/LSU/LSACK）同步 LSDB 及多点接入网 DR/BDR 中转机制</figcaption>
</figure>

<strong>区域的划分</strong>：为使 OSPF 能用于规模很大的 AS，把 AS 再划分为若干更小的<strong>区域</strong>，每个区域有 32 位<strong>区域标识符（点分十进制）。主干区域标识符必须为 0</strong>，即 0.0.0.0；其他区域标识符不能为 0 且互不相同。

> <strong>注意</strong>：0.0.0.0 在 408 中有三处含义——默认路由条目的目的网络与地址掩码、DHCP 客户端未取得地址前的临时源地址、OSPF 主干区域标识符。

<strong>优点对比</strong>：OSPF 进行的是<strong>增量更新</strong>（只发变化的部分），RIP 则把<strong>整个路由表</strong>发给邻居；OSPF 各路由器知道 AS 内<strong>完整拓扑</strong>，RIP 只知道去往某目的网络的<strong>下一跳</strong>——这正是 OSPF 无环、RIP 可能产生环路的根源。

<figure class="cpd-figure not-content" id="cnq-w3-fig-12">
  <svg viewBox="0 0 712 318" role="img" aria-label="OSPF Dijkstra 最短路径树与 RIP 距离向量对比及三大路由协议封装层次">
    <defs>
      <marker id="cnq-w3-arr-12" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="40" width="346" height="204" rx="6" class="fig-box-subtle"/>
    <rect x="374" y="40" width="322" height="204" rx="6" class="fig-box-subtle"/>
    <rect x="16" y="256" width="680" height="48" rx="6" class="fig-box-subtle"/>
    <line x1="106" y1="118" x2="148" y2="86" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#cnq-w3-arr-12)"/>
    <line x1="189" y1="100" x2="189" y2="146" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#cnq-w3-arr-12)"/>
    <line x1="230" y1="86" x2="270" y2="116" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#cnq-w3-arr-12)"/>
    <line x1="106" y1="132" x2="148" y2="162" class="fig-line-dashed"/>
    <line x1="230" y1="162" x2="270" y2="132" class="fig-line-dashed"/>
    <rect x="32" y="108" width="74" height="32" rx="4" class="fig-box-blue"/>
    <rect x="148" y="68" width="82" height="32" rx="4" class="fig-box-green"/>
    <rect x="148" y="148" width="82" height="32" rx="4" class="fig-box-green"/>
    <rect x="266" y="108" width="82" height="32" rx="4" class="fig-box-green"/>
    <rect x="114" y="88" width="24" height="16" rx="3" class="fig-box"/>
    <rect x="196" y="116" width="24" height="16" rx="3" class="fig-box"/>
    <rect x="238" y="88" width="24" height="16" rx="3" class="fig-box"/>
    <rect x="114" y="144" width="24" height="16" rx="3" class="fig-box"/>
    <rect x="238" y="144" width="24" height="16" rx="3" class="fig-box"/>
    <rect x="386" y="54" width="298" height="34" rx="4" class="fig-box"/>
    <rect x="386" y="94" width="298" height="34" rx="4" class="fig-box"/>
    <rect x="386" y="134" width="298" height="34" rx="4" class="fig-box"/>
    <rect x="386" y="174" width="298" height="58" rx="4" class="fig-box-red"/>
    <rect x="128" y="266" width="178" height="28" rx="4" class="fig-box-orange"/>
    <rect x="316" y="266" width="186" height="28" rx="4" class="fig-box-blue"/>
    <rect x="512" y="266" width="172" height="28" rx="4" class="fig-box-purple"/>
    <text x="16" y="28" font-size="13" class="fig-title">OSPF 全拓扑 SPF 最短路径树（以 R1 为根）</text>
    <text x="374" y="28" font-size="13" class="fig-title">RIP 距离向量 vs OSPF 链路状态</text>
    <text x="69" y="128" font-size="11" text-anchor="middle" class="fig-blue">R1 (根: 0)</text>
    <text x="189" y="88" font-size="11" text-anchor="middle" class="fig-green">R2 (代价 2)</text>
    <text x="189" y="168" font-size="11" text-anchor="middle" class="fig-green">R3 (代价 3)</text>
    <text x="307" y="128" font-size="11" text-anchor="middle" class="fig-green">R4 (代价 5)</text>
    <text x="126" y="100" font-size="10" text-anchor="middle" class="fig-green">2</text>
    <text x="208" y="128" font-size="10" text-anchor="middle" class="fig-green">1</text>
    <text x="250" y="100" font-size="10" text-anchor="middle" class="fig-green">3</text>
    <text x="126" y="156" font-size="10" text-anchor="middle" class="fig-muted">6</text>
    <text x="250" y="156" font-size="10" text-anchor="middle" class="fig-muted">4</text>
    <text x="32" y="206" font-size="10.5" class="fig-label">实线绿边构成 SPF 树（如 R1→R3 选 R1→R2→R3=3 &lt; 6）</text>
    <text x="32" y="226" font-size="10.5" class="fig-muted">全区域 LSDB 一致 → 各路由器算出的最短路径天然无环</text>
    <text x="396" y="75" font-size="10.5" class="fig-label">范围：RIP 仅发相邻路由器 | OSPF 洪泛全区域</text>
    <text x="396" y="115" font-size="10.5" class="fig-label">内容：RIP 发整张路由表 | OSPF 增量发链路状态</text>
    <text x="396" y="155" font-size="10.5" class="fig-label">时机：RIP 30s 周期更新 | OSPF 变化触发+30m刷新</text>
    <text x="396" y="196" font-size="10.5" class="fig-red">环路：RIP 仅知下一跳，故障时“坏消息传得慢”</text>
    <text x="396" y="216" font-size="10" class="fig-muted">（计数到 16 才判不可达）；OSPF 握有全局拓扑故无环</text>
    <text x="28" y="284" font-size="11" class="fig-label">协议封装对比：</text>
    <text x="217" y="284" font-size="10.5" text-anchor="middle" class="fig-orange">RIP → UDP 520（应用层）</text>
    <text x="409" y="284" font-size="10.5" text-anchor="middle" class="fig-blue">OSPF → IP 协议 89（网际层）</text>
    <text x="598" y="284" font-size="10.5" text-anchor="middle" class="fig-purple">BGP → TCP 179（应用层）</text>
  </svg>
  <figcaption>图 6-6：OSPF 基于全局 LSDB 的 Dijkstra 最短路径树计算、与 RIP 的机制对比及三大路由协议封装层次</figcaption>
</figure>

## 第7章 传输层：TCP

### 7.1 四报文挥手释放连接

<strong>前提</strong>：数据传输结束后，<strong>TCP 双方都可以主动释放连接</strong>；但<strong>建立连接必须由客户发起</strong>。TCP 通信是全双工的，因此也可能由服务器先发连接释放报文段。

<figure class="cpd-figure not-content" id="cnq-w4-fig-1">
  <svg viewBox="0 0 720 330" role="img" aria-label="TCP 四报文挥手释放连接时序、状态转换与 TIME-WAIT 双重作用">
    <defs>
      <marker id="cnq-w4-arr-1" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="16" width="468" height="298" rx="6" class="fig-box-subtle"/>
    <rect x="496" y="16" width="208" height="298" rx="6" class="fig-box-subtle"/>
    <line x1="148" y1="54" x2="148" y2="286" class="fig-line"/>
    <line x1="368" y1="54" x2="368" y2="286" class="fig-line"/>
    <line x1="148" y1="76" x2="366" y2="94" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#cnq-w4-arr-1)"/>
    <line x1="368" y1="108" x2="150" y2="126" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#cnq-w4-arr-1)"/>
    <line x1="368" y1="146" x2="150" y2="158" class="fig-line-dashed" marker-end="url(#cnq-w4-arr-1)"/>
    <line x1="368" y1="174" x2="150" y2="192" class="fig-line" stroke="var(--sl-color-purple)" marker-end="url(#cnq-w4-arr-1)"/>
    <line x1="148" y1="204" x2="366" y2="222" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#cnq-w4-arr-1)"/>
    <line x1="136" y1="204" x2="160" y2="204" class="fig-line-dashed"/>
    <line x1="136" y1="262" x2="160" y2="262" class="fig-line-dashed"/>
    <rect x="90" y="26" width="116" height="26" rx="4" class="fig-box-blue"/>
    <rect x="304" y="26" width="128" height="26" rx="4" class="fig-box-purple"/>
    <rect x="26" y="56" width="110" height="20" rx="3" class="fig-box"/>
    <rect x="26" y="84" width="110" height="20" rx="3" class="fig-box-blue"/>
    <rect x="26" y="122" width="110" height="22" rx="3" class="fig-box-orange"/>
    <rect x="26" y="204" width="110" height="54" rx="4" class="fig-box-red"/>
    <rect x="26" y="266" width="110" height="20" rx="3" class="fig-box"/>
    <rect x="380" y="64" width="96" height="20" rx="3" class="fig-box"/>
    <rect x="380" y="106" width="96" height="44" rx="3" class="fig-box-orange"/>
    <rect x="380" y="168" width="96" height="22" rx="3" class="fig-box-purple"/>
    <rect x="380" y="218" width="96" height="20" rx="3" class="fig-box"/>
    <rect x="174" y="75" width="168" height="18" rx="3" class="fig-box"/>
    <rect x="172" y="108" width="172" height="18" rx="3" class="fig-box"/>
    <rect x="172" y="142" width="172" height="18" rx="3" class="fig-box"/>
    <rect x="164" y="174" width="188" height="18" rx="3" class="fig-box"/>
    <rect x="168" y="204" width="180" height="18" rx="3" class="fig-box"/>
    <rect x="508" y="50" width="184" height="92" rx="4" class="fig-box-red"/>
    <rect x="508" y="152" width="184" height="72" rx="4" class="fig-box-orange"/>
    <rect x="508" y="234" width="184" height="68" rx="4" class="fig-box-green"/>
    <text x="148" y="43" font-size="11.5" text-anchor="middle" class="fig-blue">TCP 客户（主动）</text>
    <text x="368" y="43" font-size="11.5" text-anchor="middle" class="fig-purple">TCP 服务器（被动）</text>
    <text x="81" y="70" font-size="10" text-anchor="middle" class="fig-muted">ESTABLISHED</text>
    <text x="81" y="98" font-size="10" text-anchor="middle" class="fig-blue">FIN-WAIT-1</text>
    <text x="81" y="137" font-size="10" text-anchor="middle" class="fig-orange">FIN-WAIT-2</text>
    <text x="81" y="224" font-size="10.5" text-anchor="middle" class="fig-red">TIME-WAIT</text>
    <text x="81" y="244" font-size="9.5" text-anchor="middle" class="fig-red">等待 2MSL (4 min)</text>
    <text x="81" y="280" font-size="10" text-anchor="middle" class="fig-muted">CLOSED</text>
    <text x="428" y="78" font-size="10" text-anchor="middle" class="fig-muted">ESTABLISHED</text>
    <text x="428" y="124" font-size="10" text-anchor="middle" class="fig-orange">CLOSE-WAIT</text>
    <text x="428" y="142" font-size="9.5" text-anchor="middle" class="fig-muted">（半关闭可发）</text>
    <text x="428" y="183" font-size="10" text-anchor="middle" class="fig-purple">LAST-ACK</text>
    <text x="428" y="232" font-size="10" text-anchor="middle" class="fig-muted">CLOSED</text>
    <text x="258" y="88" font-size="10" text-anchor="middle" class="fig-blue">① FIN=1, ACK=1, seq=u</text>
    <text x="258" y="121" font-size="10" text-anchor="middle" class="fig-green">② ACK=1, seq=v, ack=u+1</text>
    <text x="258" y="155" font-size="9.5" text-anchor="middle" class="fig-muted">半关闭：服务器继续发余下数据</text>
    <text x="258" y="187" font-size="10" text-anchor="middle" class="fig-purple">③ FIN=1, ACK=1, seq=w, ack=u+1</text>
    <text x="258" y="217" font-size="10" text-anchor="middle" class="fig-green">④ ACK=1, seq=u+1, ack=w+1</text>
    <text x="250" y="302" font-size="10" text-anchor="middle" class="fig-muted">注：FIN=1 报文段通常不带数据，但必消耗 1 个序号（u → u+1，w → w+1）</text>
    <text x="600" y="36" font-size="12" text-anchor="middle" class="fig-title">挥手核心考点</text>
    <text x="518" y="68" font-size="10.5" class="fig-red">TIME-WAIT = 2MSL 作用：</text>
    <text x="518" y="88" font-size="10" class="fig-label">① 防 ④ 丢失：确保服务器</text>
    <text x="518" y="104" font-size="10" class="fig-label">能收到重传 FIN 后的 ACK</text>
    <text x="518" y="124" font-size="10" class="fig-label">② 清空旧报文：防新连接混</text>
    <text x="518" y="138" font-size="10" class="fig-muted">入同序号旧段（MSL=2min）</text>
    <text x="518" y="170" font-size="10.5" class="fig-orange">半关闭（Half-Close）：</text>
    <text x="518" y="188" font-size="10" class="fig-label">客户发 FIN 后不再发数据，</text>
    <text x="518" y="206" font-size="10" class="fig-label">但仍可接收服务器未发完段</text>
    <text x="518" y="252" font-size="10.5" class="fig-green">何时简化为三报文挥手？</text>
    <text x="518" y="270" font-size="10" class="fig-label">服务器收到 ① 时已无数据</text>
    <text x="518" y="288" font-size="10" class="fig-muted">要发，将 ② 与 ③ 合并发送</text>
  </svg>
  <figcaption>图 7-1：TCP 四报文挥手释放连接时序、英文状态转换、半关闭区间及 TIME-WAIT = 2MSL 双重作用</figcaption>
</figure>

<strong>四个报文段</strong>：

1. 客户发送 <strong>FIN=1、ACK=1</strong> 的连接释放报文段（seq=u），FIN 表示这是释放请求，ACK 是对服务器此前数据的确认。
2. 服务器回一个<strong>普通确认报文段</strong>（ACK=1、ack=u+1）。此时客户→服务器方向释放，连接进入<strong>半关闭</strong>状态：客户不再发送数据，服务器还可以继续发。
3. 服务器没有数据要发时，发送 <strong>FIN=1、ACK=1</strong> 的释放报文段（seq=w）。
4. 客户回 <strong>ACK=1、ack=w+1</strong>，进入 <strong>TIME-WAIT</strong>。

<strong>状态转换</strong>（2021 年题 38 考过英文状态名）：

| 客户 | 服务器 |
|---|---|
| ESTABLISHED → <strong>FIN-WAIT-1</strong>（发 FIN） | ESTABLISHED → <strong>CLOSE-WAIT</strong>（收 FIN，发 ACK） |
| FIN-WAIT-1 → <strong>FIN-WAIT-2</strong>（收 ACK） | CLOSE-WAIT → <strong>LAST-ACK</strong>（发 FIN） |
| FIN-WAIT-2 → <strong>TIME-WAIT</strong>（收 FIN，发 ACK） | LAST-ACK → <strong>CLOSED</strong>（收 ACK） |
| TIME-WAIT →（经 2MSL）→ <strong>CLOSED</strong> | — |

> <strong>注意</strong>：FIN=1 的报文段<strong>通常不携带数据，但要消耗一个序号</strong>；RFC 1644 讨论过同时携带数据的情况，408 除明确说明或暗示外一律按不携带数据处理。

<strong>为什么必须等待 2MSL</strong>：若客户收到服务器 FIN 后直接 CLOSED 而不进 TIME-WAIT，一旦自己发出的最后一个 ACK 丢失，服务器会超时重传 FIN 并停留在 LAST-ACK，而客户已关闭、无法再确认，服务器便永远无法关闭、一直占资源。等 2MSL 有两层作用：① 确保服务器能收到最后一个 ACK 并进入 CLOSED；② 使本次连接产生的所有报文段都从网络中消失，下一个新连接不会出现<strong>序号相同的旧报文段</strong>（防序号绕回的措施之一）。

RFC 793 建议 <strong>MSL = 2 分钟</strong>，故 TIME-WAIT 要持续 <strong>4 分钟</strong>；这纯是工程考虑，对现在的网络可能太长，TCP 允许实现使用更小的 MSL。

<strong>三报文挥手</strong>：若客户发完数据请求关闭，而服务器也没有数据要发给客户，标准四报文可简化为三报文（服务器把 ACK 与自己的 FIN 合并在一个报文段里）。

> <strong>注意</strong>：四报文挥手在特定场景下可简化为三报文，但<strong>三报文握手建立连接不能简化为两报文握手</strong>。

<strong>保活计时器</strong>：TCP 双方已建立连接，客户主机突然故障，服务器不能白白等下去。服务器每收到一次客户数据就<strong>重新设置并启动</strong>保活计时器（通常 <strong>2 小时</strong>）；到期未收到数据就发一个<strong>探测报文段</strong>，此后<strong>每隔 75 秒</strong>发一次；连续发 <strong>10 个</strong>探测报文段仍无响应，就认为客户主机故障，关闭连接。

<figure class="cpd-figure not-content" id="cnq-w4-fig-2">
  <svg viewBox="0 0 720 328" role="img" aria-label="TCP 三报文握手建立连接、为什么不能用两报文握手及保活计时器探测机制">
    <defs>
      <marker id="cnq-w4-arr-2" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="16" width="360" height="296" rx="6" class="fig-box-subtle"/>
    <rect x="388" y="16" width="316" height="162" rx="6" class="fig-box-subtle"/>
    <rect x="388" y="188" width="316" height="124" rx="6" class="fig-box-subtle"/>
    <line x1="118" y1="68" x2="118" y2="274" class="fig-line"/>
    <line x1="278" y1="68" x2="278" y2="274" class="fig-line"/>
    <line x1="118" y1="92" x2="276" y2="116" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#cnq-w4-arr-2)"/>
    <line x1="278" y1="142" x2="120" y2="166" class="fig-line" stroke="var(--sl-color-purple)" marker-end="url(#cnq-w4-arr-2)"/>
    <line x1="118" y1="194" x2="276" y2="218" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#cnq-w4-arr-2)"/>
    <line x1="406" y1="254" x2="686" y2="254" class="fig-line" marker-end="url(#cnq-w4-arr-2)"/>
    <rect x="78" y="42" width="80" height="24" rx="4" class="fig-box-blue"/>
    <rect x="234" y="42" width="88" height="24" rx="4" class="fig-box-purple"/>
    <rect x="24" y="72" width="84" height="20" rx="3" class="fig-box"/>
    <rect x="24" y="104" width="84" height="22" rx="3" class="fig-box-blue"/>
    <rect x="24" y="184" width="84" height="22" rx="3" class="fig-box-green"/>
    <rect x="288" y="72" width="80" height="20" rx="3" class="fig-box"/>
    <rect x="288" y="132" width="80" height="22" rx="3" class="fig-box-purple"/>
    <rect x="288" y="214" width="80" height="22" rx="3" class="fig-box-green"/>
    <rect x="132" y="94" width="132" height="18" rx="3" class="fig-box"/>
    <rect x="106" y="144" width="184" height="18" rx="3" class="fig-box"/>
    <rect x="118" y="196" width="160" height="18" rx="3" class="fig-box"/>
    <rect x="402" y="50" width="288" height="54" rx="4" class="fig-box-red"/>
    <rect x="402" y="112" width="288" height="54" rx="4" class="fig-box-green"/>
    <rect x="410" y="232" width="96" height="28" rx="4" class="fig-box-blue"/>
    <rect x="518" y="232" width="96" height="28" rx="4" class="fig-box-orange"/>
    <rect x="626" y="232" width="64" height="28" rx="4" class="fig-box-red"/>
    <text x="196" y="34" font-size="12" text-anchor="middle" class="fig-title">TCP 三报文握手建立连接（客户发起）</text>
    <text x="118" y="58" font-size="11" text-anchor="middle" class="fig-blue">TCP 客户</text>
    <text x="278" y="58" font-size="11" text-anchor="middle" class="fig-purple">TCP 服务器</text>
    <text x="66" y="86" font-size="9.5" text-anchor="middle" class="fig-muted">CLOSED</text>
    <text x="66" y="119" font-size="9.5" text-anchor="middle" class="fig-blue">SYN-SENT</text>
    <text x="66" y="199" font-size="9" text-anchor="middle" class="fig-green">ESTABLISHED</text>
    <text x="328" y="86" font-size="9.5" text-anchor="middle" class="fig-muted">LISTEN</text>
    <text x="328" y="147" font-size="9.5" text-anchor="middle" class="fig-purple">SYN-RCVD</text>
    <text x="328" y="229" font-size="9" text-anchor="middle" class="fig-green">ESTABLISHED</text>
    <text x="198" y="107" font-size="9.5" text-anchor="middle" class="fig-blue">① SYN=1, seq=x</text>
    <text x="198" y="157" font-size="9.5" text-anchor="middle" class="fig-purple">② SYN=1, ACK=1, seq=y, ack=x+1</text>
    <text x="198" y="209" font-size="9.5" text-anchor="middle" class="fig-green">③ ACK=1, seq=x+1, ack=y+1</text>
    <text x="196" y="286" font-size="9.5" text-anchor="middle" class="fig-label">①② 的 SYN=1 禁带数据但各耗 1 个序号</text>
    <text x="196" y="302" font-size="9.5" text-anchor="middle" class="fig-muted">③ 可带数据；若不带数据则不消耗序号</text>
    <text x="546" y="36" font-size="12" text-anchor="middle" class="fig-title">为何不能简化为两报文握手？</text>
    <text x="412" y="68" font-size="10" class="fig-red">两报文隐患：已失效的连接请求迟到</text>
    <text x="412" y="86" font-size="9.5" class="fig-label">网络滞留的旧 SYN 在连接释放后到达服务器，</text>
    <text x="412" y="99" font-size="9.5" class="fig-muted">若发确认即建连，服务器将单向空等白占资源</text>
    <text x="412" y="130" font-size="10" class="fig-green">三报文防御：客户校验二次握手</text>
    <text x="412" y="148" font-size="9.5" class="fig-label">客户此时并未请求建连，直接忽略服务器确认</text>
    <text x="412" y="161" font-size="9.5" class="fig-muted">不回第 ③ 报文，服务器收不到 ACK 不会建连</text>
    <text x="546" y="210" font-size="12" text-anchor="middle" class="fig-title">保活计时器（Keepalive）防客户死机</text>
    <text x="458" y="250" font-size="10" text-anchor="middle" class="fig-blue">空闲满 2 小时</text>
    <text x="566" y="250" font-size="10" text-anchor="middle" class="fig-orange">每 75s 探测×10</text>
    <text x="658" y="250" font-size="10" text-anchor="middle" class="fig-red">关连接</text>
    <text x="546" y="282" font-size="9.5" text-anchor="middle" class="fig-label">每收到客户数据即重置 2h；10 次探测无响应</text>
    <text x="546" y="298" font-size="9.5" text-anchor="middle" class="fig-muted">（共 2h + 750s）则判定客户主机故障并释放</text>
  </svg>
  <figcaption>图 7-2：TCP 三报文握手建立连接（SYN 消耗序号规则）、拒绝两报文握手的原因及保活计时器参数</figcaption>
</figure>

### 7.2 拥塞控制：慢开始与拥塞避免

<strong>目的</strong>：防止发送方把过多数据注入网络，避免路由器或链路过载。

| 对比 | 流量控制 | 拥塞控制 |
|---|---|---|
| 依据 | <strong>接收方</strong>的接收能力 | 源点根据各方面因素<strong>自行</strong>判断 |
| 范围 | 特定点对点通信的收发双方 | <strong>全局</strong>，涉及所有主机与路由器 |
| 手段 | 接收窗口 rwnd | 拥塞窗口 cwnd + 拥塞控制算法 |

讨论前先假定：① 数据单向传送、另一方向只送确认；② 接收方总有可用缓存，故发送窗口<strong>只由网络拥塞程度决定</strong>（不考虑流量控制）；③ 以 <strong>TCP 报文段个数</strong>为单位，每段数据载荷长度都是 MSS。408 也正是这样考的。

<figure class="cpd-figure not-content" id="cnq-w4-fig-3">
  <svg viewBox="0 0 720 342" role="img" aria-label="TCP 拥塞控制 cwnd 完整曲线：慢开始、拥塞避免、超时重传与快恢复对比">
    <defs>
      <marker id="cnq-w4-arr-3" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="16" width="500" height="310" rx="6" class="fig-box-subtle"/>
    <rect x="528" y="16" width="176" height="310" rx="6" class="fig-box-subtle"/>
    <line x1="56" y1="282" x2="56" y2="42" class="fig-line" marker-end="url(#cnq-w4-arr-3)"/>
    <line x1="56" y1="282" x2="504" y2="282" class="fig-line" marker-end="url(#cnq-w4-arr-3)"/>
    <line x1="56" y1="138" x2="272" y2="138" class="fig-line-dashed"/>
    <line x1="290" y1="174" x2="416" y2="174" class="fig-line-dashed"/>
    <line x1="416" y1="219" x2="494" y2="219" class="fig-line-dashed"/>
    <line x1="272" y1="66" x2="272" y2="282" class="fig-line-dashed" stroke="var(--sl-color-red)"/>
    <line x1="272" y1="66" x2="290" y2="273" class="fig-line-dashed" stroke="var(--sl-color-red)"/>
    <line x1="416" y1="147" x2="416" y2="282" class="fig-line-dashed" stroke="var(--sl-color-orange)"/>
    <polyline points="56,273 74,264 92,246 110,210 128,138" class="fig-line" stroke="var(--sl-color-blue)" stroke-width="2.2"/>
    <polyline points="128,138 146,129 164,120 182,111 200,102 218,93 236,84 254,75 272,66" class="fig-line" stroke="var(--sl-color-green)" stroke-width="2.2"/>
    <polyline points="290,273 308,264 326,246 344,210 362,174" class="fig-line" stroke="var(--sl-color-blue)" stroke-width="2.2"/>
    <polyline points="362,174 380,165 398,156 416,147" class="fig-line" stroke="var(--sl-color-green)" stroke-width="2.2"/>
    <line x1="416" y1="147" x2="416" y2="219" class="fig-line" stroke="var(--sl-color-orange)" stroke-width="2.2"/>
    <polyline points="416,219 434,210 452,201 470,192 488,183" class="fig-line" stroke="var(--sl-color-purple)" stroke-width="2.2"/>
    <circle cx="128" cy="138" r="3.5" class="fig-box-blue"/>
    <circle cx="272" cy="66" r="4" class="fig-box-red"/>
    <circle cx="290" cy="273" r="3.5" class="fig-box-blue"/>
    <circle cx="362" cy="174" r="3.5" class="fig-box-orange"/>
    <circle cx="416" cy="147" r="4" class="fig-box-orange"/>
    <circle cx="416" cy="219" r="3.5" class="fig-box-purple"/>
    <rect x="58" y="116" width="92" height="18" rx="3" class="fig-box"/>
    <rect x="274" y="152" width="92" height="18" rx="3" class="fig-box"/>
    <rect x="417" y="224" width="88" height="18" rx="3" class="fig-box"/>
    <rect x="234" y="290" width="76" height="20" rx="3" class="fig-box-red"/>
    <rect x="372" y="290" width="92" height="20" rx="3" class="fig-box-orange"/>
    <rect x="538" y="48" width="156" height="86" rx="4" class="fig-box-red"/>
    <rect x="538" y="142" width="156" height="88" rx="4" class="fig-box-orange"/>
    <rect x="538" y="238" width="156" height="78" rx="4" class="fig-box-blue"/>
    <text x="64" y="36" font-size="11.5" class="fig-title">拥塞窗口 cwnd（单位：MSS）</text>
    <text x="502" y="274" font-size="10" text-anchor="end" class="fig-muted">传输轮次 RTT</text>
    <text x="48" y="276" font-size="9.5" text-anchor="end" class="fig-muted">1</text>
    <text x="48" y="213" font-size="9.5" text-anchor="end" class="fig-muted">8</text>
    <text x="48" y="177" font-size="9.5" text-anchor="end" class="fig-muted">12</text>
    <text x="48" y="141" font-size="9.5" text-anchor="end" class="fig-muted">16</text>
    <text x="48" y="69" font-size="9.5" text-anchor="end" class="fig-muted">24</text>
    <text x="104" y="129" font-size="9.5" text-anchor="middle" class="fig-label">ssthresh₁ = 16</text>
    <text x="320" y="165" font-size="9.5" text-anchor="middle" class="fig-label">ssthresh₂ = 12</text>
    <text x="461" y="237" font-size="9.5" text-anchor="middle" class="fig-label">ssthresh₃ = 7</text>
    <text x="92" y="266" font-size="9.5" text-anchor="middle" class="fig-blue">慢开始(×2)</text>
    <text x="196" y="92" font-size="9.5" text-anchor="middle" class="fig-green">拥塞避免(+1)</text>
    <text x="272" y="56" font-size="9.5" text-anchor="middle" class="fig-red">cwnd=24</text>
    <text x="334" y="264" font-size="9.5" text-anchor="middle" class="fig-blue">慢开始</text>
    <text x="362" y="190" font-size="9" text-anchor="middle" class="fig-orange">8→12即切</text>
    <text x="416" y="137" font-size="9.5" text-anchor="middle" class="fig-orange">cwnd=15</text>
    <text x="460" y="180" font-size="9.5" text-anchor="middle" class="fig-purple">快恢复(+1)</text>
    <text x="272" y="304" font-size="9.5" text-anchor="middle" class="fig-red">① 超时重传</text>
    <text x="418" y="304" font-size="9.5" text-anchor="middle" class="fig-orange">② 3 个重复 ACK</text>
    <text x="616" y="36" font-size="11.5" text-anchor="middle" class="fig-title">两条拥塞响应分支</text>
    <text x="546" y="66" font-size="10" class="fig-red">① 超时（Tahoe/Reno）：</text>
    <text x="546" y="84" font-size="9.5" class="fig-label">ssthresh = ⌊cwnd / 2⌋</text>
    <text x="546" y="102" font-size="9.5" class="fig-label">cwnd = 1</text>
    <text x="546" y="120" font-size="9.5" class="fig-muted">重新执行「慢开始」</text>
    <text x="546" y="160" font-size="10" class="fig-orange">② 3 重复 ACK（Reno）：</text>
    <text x="546" y="178" font-size="9.5" class="fig-label">ssthresh = ⌊cwnd / 2⌋</text>
    <text x="546" y="196" font-size="9.5" class="fig-label">cwnd = ssthresh（如15→7）</text>
    <text x="546" y="214" font-size="9.5" class="fig-muted">执行「快恢复→拥塞避免」</text>
    <text x="546" y="256" font-size="10" class="fig-blue">易错高频细节：</text>
    <text x="546" y="274" font-size="9.5" class="fig-label">• 慢开始：每收1确认 +1</text>
    <text x="546" y="292" font-size="9.5" class="fig-label">• 触门限立刻切线性 +1</text>
    <text x="546" y="308" font-size="9" class="fig-muted">（8→12 不得翻倍到 16）</text>
  </svg>
  <figcaption>图 7-3：TCP 拥塞控制完整曲线（慢开始、触门限切换拥塞避免、超时置 1 与 3 重复确认快恢复对比）</figcaption>
</figure>

<strong>慢开始</strong>：cwnd 初值为 <strong>1 个 MSS</strong>。规则是<strong>每收到一个对新数据报文段的确认，cwnd 就加 1</strong>——一个 RTT 内发出 cwnd 个报文段，全部确认回来后 cwnd 增加 cwnd，故<strong>每个 RTT 结束后 cwnd 翻倍</strong>。

> <strong>注意</strong>：图中横轴上某个 RTT 对应的纵坐标，是该轮次<strong>结束时</strong>的 cwnd 值。很多人只记住“每 RTT 翻倍”，忘了“每收到一个确认加 1”这个细节，而题目恰恰可能考它。

“慢开始”的“慢”指<strong>一开始注入网络的数据少</strong>，不是指 cwnd 增长慢。主机刚开始完全不知道网络拥塞情况，一次注入大量数据可能直接引起拥塞，经验做法是由小到大逐渐增大。

<strong>切换到拥塞避免</strong>：cwnd 增长到<strong>慢开始门限 ssthresh</strong> 时立即改用拥塞避免算法（这是 TCP 实现中的默认做法）。若 ssthresh 不是 2 的幂，如 14，则 cwnd 长到 14 就<strong>立刻切换</strong>，而不是等本轮翻倍到 16。

<strong>拥塞避免</strong>：<strong>每个 RTT 结束后 cwnd 只线性加 1</strong>。“避免”并非完全避免拥塞，而是让 cwnd 按线性规律增长，使网络较不容易出现拥塞。

> <strong>注意</strong>：与“慢开始不慢”同类的易错点——CSMA/CA 中的“碰撞避免”也并非完全避免碰撞。

<strong>超时重传时</strong>（无论处于慢开始还是拥塞避免阶段）：判断网络可能拥塞，于是

1. $ssthresh$ 调整为<strong>发生拥塞时 cwnd 值的一半</strong>（向下取整）；
2. $cwnd$ 置为 <strong>1</strong>；
3. <strong>重新执行慢开始</strong>。

### 7.3 快重传与快恢复

慢开始与拥塞避免是 1988 年的 TCP Tahoe 版本；1990 年增加快重传与快恢复，即 TCP Reno 版本。

<strong>为什么需要</strong>：某个 TCP 报文段可能因<strong>误码被路由器丢弃</strong>，而不是因为路由器繁忙。个别报文段丢失<strong>并不意味着网络拥塞</strong>，但发送方无法区分，只能按超时处理——误以为拥塞，把 cwnd 陡然降为 1 并错误启动慢开始，传输效率因此下降。

<strong>快重传</strong>：让发送方尽早知道“只是个别的报文段丢了”，从而尽快重传而不等超时。

- 接收方<strong>不要</strong>等自己发送数据时才捎带确认，而要<strong>立即发送确认</strong>；即使收到失序的报文段，也要立即发出对已收到报文段的<strong>重复确认</strong>。
- 发送方一旦收到<strong>三个连续的重复确认</strong>，就把相应报文段<strong>立即重传</strong>，不等重传计时器超时。

例：接收方已按序收到 M1、M2；随后收到 M4、M5、M6，每次都重复确认 M2。发送方收到对 M2 的<strong>三个重复确认</strong>，立即重传 M3，而不等 M3 超时。

> <strong>★考点</strong>：实践证明快重传可使整个网络吞吐量提高约 <strong>20%</strong>。

<strong>快恢复</strong>：发送方收到三个重复确认，就知道只丢了个别报文段，于是<strong>不启动慢开始</strong>，而是

1. $ssthresh$ 与 $cwnd$ 都调整为<strong>当前 cwnd 的一半</strong>；
2. 转而执行<strong>拥塞避免</strong>。

也有的实现把快恢复开始时的 cwnd 再增大一些，即 $cwnd = ssthresh + 3$：既然收到三个重复确认，说明有三个报文段已离开网络、停在接收方缓存里，网络中堆积的报文段少了三个，可以适当放大窗口。

> <strong>注意</strong>：408 目前尚未考过快恢复，若出现按题意具体分析。超时 → 慢开始；三个重复确认 → 快恢复，这是区分两条路径的关键。

<strong>三个窗口的关系</strong>：拥塞窗口 cwnd 取决于网络拥塞程度，由发送方用拥塞控制算法主动控制；接收窗口 rwnd 取决于接收方缓存可用空间，用于流量控制；发送窗口取二者的小者：

$$
swnd=\min\left(cwnd,\ rwnd\right)
$$

发送方在未收到确认时，可把序号落在发送窗口内的全部数据发出。

> <strong>★考点</strong>：题目出现“最大报文段长度 MSS”就要条件反射想到拥塞控制；同时再审题看是否还有“接收方通告的窗口/缓存容量”——那是在考流量控制，往往要取小者。

<figure class="cpd-figure not-content" id="cnq-w4-fig-4">
  <svg viewBox="0 0 720 318" role="img" aria-label="TCP 快重传 3 个重复确认时序与发送窗口 swnd = min(cwnd, rwnd) 双重约束">
    <defs>
      <marker id="cnq-w4-arr-4" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="16" width="388" height="286" rx="6" class="fig-box-subtle"/>
    <rect x="416" y="16" width="288" height="286" rx="6" class="fig-box-subtle"/>
    <line x1="76" y1="64" x2="76" y2="286" class="fig-line"/>
    <line x1="336" y1="64" x2="336" y2="286" class="fig-line"/>
    <line x1="76" y1="74" x2="334" y2="88" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#cnq-w4-arr-4)"/>
    <line x1="336" y1="92" x2="78" y2="106" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#cnq-w4-arr-4)"/>
    <line x1="76" y1="112" x2="188" y2="120" class="fig-line-dashed" stroke="var(--sl-color-red)" marker-end="url(#cnq-w4-arr-4)"/>
    <line x1="76" y1="132" x2="334" y2="146" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#cnq-w4-arr-4)"/>
    <line x1="76" y1="152" x2="334" y2="166" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#cnq-w4-arr-4)"/>
    <line x1="76" y1="172" x2="334" y2="186" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#cnq-w4-arr-4)"/>
    <line x1="336" y1="190" x2="78" y2="206" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#cnq-w4-arr-4)"/>
    <line x1="336" y1="210" x2="78" y2="226" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#cnq-w4-arr-4)"/>
    <line x1="336" y1="230" x2="78" y2="246" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#cnq-w4-arr-4)"/>
    <line x1="76" y1="256" x2="334" y2="274" class="fig-line" stroke="var(--sl-color-purple)" stroke-width="2" marker-end="url(#cnq-w4-arr-4)"/>
    <line x1="564" y1="68" x2="564" y2="212" class="fig-line-dashed" stroke="var(--sl-color-red)"/>
    <rect x="36" y="40" width="80" height="22" rx="4" class="fig-box-blue"/>
    <rect x="296" y="40" width="80" height="22" rx="4" class="fig-box-purple"/>
    <rect x="192" y="111" width="66" height="18" rx="3" class="fig-box-red"/>
    <rect x="148" y="68" width="112" height="16" rx="3" class="fig-box"/>
    <rect x="148" y="92" width="112" height="16" rx="3" class="fig-box"/>
    <rect x="260" y="132" width="66" height="16" rx="3" class="fig-box"/>
    <rect x="260" y="152" width="66" height="16" rx="3" class="fig-box"/>
    <rect x="260" y="172" width="66" height="16" rx="3" class="fig-box"/>
    <rect x="134" y="190" width="140" height="16" rx="3" class="fig-box"/>
    <rect x="134" y="210" width="140" height="16" rx="3" class="fig-box"/>
    <rect x="134" y="230" width="140" height="16" rx="3" class="fig-box"/>
    <rect x="118" y="256" width="176" height="20" rx="3" class="fig-box-purple"/>
    <rect x="432" y="76" width="204" height="30" rx="4" class="fig-box-blue"/>
    <rect x="432" y="122" width="132" height="30" rx="4" class="fig-box-orange"/>
    <rect x="564" y="122" width="72" height="30" rx="4" class="fig-box-dashed"/>
    <rect x="432" y="168" width="132" height="32" rx="4" class="fig-box-green"/>
    <rect x="432" y="216" width="256" height="72" rx="4" class="fig-box"/>
    <text x="210" y="32" font-size="12" text-anchor="middle" class="fig-title">快重传：3 个连续重复 ACK 立即重传</text>
    <text x="76" y="55" font-size="10.5" text-anchor="middle" class="fig-blue">发送方</text>
    <text x="336" y="55" font-size="10.5" text-anchor="middle" class="fig-purple">接收方</text>
    <text x="204" y="80" font-size="9.5" text-anchor="middle" class="fig-green">M1、M2 按序到达</text>
    <text x="204" y="104" font-size="9.5" text-anchor="middle" class="fig-green">正常确认 ACK M2</text>
    <text x="225" y="124" font-size="9.5" text-anchor="middle" class="fig-red">✕ M3 丢失</text>
    <text x="293" y="144" font-size="9.5" text-anchor="middle" class="fig-blue">M4 失序</text>
    <text x="293" y="164" font-size="9.5" text-anchor="middle" class="fig-blue">M5 失序</text>
    <text x="293" y="184" font-size="9.5" text-anchor="middle" class="fig-blue">M6 失序</text>
    <text x="204" y="202" font-size="9.5" text-anchor="middle" class="fig-orange">重复确认 ACK M2 ①</text>
    <text x="204" y="222" font-size="9.5" text-anchor="middle" class="fig-orange">重复确认 ACK M2 ②</text>
    <text x="204" y="242" font-size="9.5" text-anchor="middle" class="fig-orange">重复确认 ACK M2 ③</text>
    <text x="206" y="270" font-size="10" text-anchor="middle" class="fig-purple">不等超时，立即重传 M3</text>
    <text x="560" y="34" font-size="12" text-anchor="middle" class="fig-title">发送窗口 swnd 双重约束</text>
    <text x="560" y="56" font-size="10" text-anchor="middle" class="fig-muted">swnd = min(cwnd, rwnd)</text>
    <text x="534" y="95" font-size="10" text-anchor="middle" class="fig-blue">拥塞窗口 cwnd = 12 MSS</text>
    <text x="498" y="141" font-size="10" text-anchor="middle" class="fig-orange">接收窗口 rwnd = 8</text>
    <text x="600" y="141" font-size="9" text-anchor="middle" class="fig-muted">缓存已满</text>
    <text x="498" y="188" font-size="10.5" text-anchor="middle" class="fig-green">实际 swnd = 8 MSS</text>
    <text x="572" y="188" font-size="9.5" class="fig-red">← 受流量控制截断</text>
    <text x="442" y="236" font-size="10" class="fig-label">408 审题反射弧：</text>
    <text x="442" y="254" font-size="9.5" class="fig-blue">• 题干给 MSS / RTT → 算 cwnd</text>
    <text x="442" y="272" font-size="9.5" class="fig-orange">• 题干给通告窗口/缓存 → 看 rwnd</text>
  </svg>
  <figcaption>图 7-4：TCP 快重传（失序到达立即触发 3 个重复确认重传）与发送窗口 swnd = min(cwnd, rwnd) 约束</figcaption>
</figure>

### 7.4 TCP 可靠传输的要点

TCP 基于滑动窗口机制，并借鉴 GBN 与 SR 的思想实现可靠传输；三者核心都是<strong>序号 + 确认 + 重传策略</strong>。

| 对比项 | GBN | SR | TCP |
|---|---|---|---|
| 层次 | 数据链路层 | 数据链路层 | 传输层 |
| 编号对象 | 整个帧 | 整个帧 | <strong>每个字节</strong>（面向字节流） |
| 窗口 | 固定窗口 | 固定窗口 | <strong>动态窗口</strong>（受流量控制与拥塞控制调节） |
| 确认机制 | 累计确认 | 独立（逐一）确认 | 累计确认 + 双向通信时的捎带确认 + 可选 SACK（需扩展首部，增大开销） |
| 重传触发 | 仅超时 | 仅超时 | 超时 <strong>和</strong> 三个重复确认（快重传） |
| 重传范围 | 超时帧及其后续已发未确认帧 | 仅超时帧 | 仅超时的 TCP 报文段 |
| 接收缓存 | 仅缓存落入窗口的帧（窗口为 1，必按序） | 缓存窗口内按序/乱序到达的帧 | 缓存窗口内按序/乱序到达的字节数据 |

> <strong>注意</strong>：若用 $n$ 比特编号，GBN 与 SR 的窗口取值范围与“超出范围会出现什么错误”必须能立刻答出——这是本章与第四章的交叉考点。

<figure class="cpd-figure not-content" id="cnq-w4-fig-5">
  <svg viewBox="0 0 720 338" role="img" aria-label="TCP 20 字节首部关键字段与面向字节流的发送滑动窗口四区间结构">
    <defs>
      <marker id="cnq-w4-arr-5" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="16" width="688" height="162" rx="6" class="fig-box-subtle"/>
    <rect x="16" y="188" width="688" height="134" rx="6" class="fig-box-subtle"/>
    <line x1="636" y1="48" x2="646" y2="48" class="fig-line"/>
    <line x1="646" y1="48" x2="646" y2="148" class="fig-line"/>
    <line x1="636" y1="148" x2="646" y2="148" class="fig-line"/>
    <line x1="168" y1="212" x2="496" y2="212" class="fig-line-accent" marker-start="url(#cnq-w4-arr-5)" marker-end="url(#cnq-w4-arr-5)"/>
    <line x1="168" y1="206" x2="168" y2="282" class="fig-line-dashed" stroke="var(--sl-color-purple)"/>
    <line x1="344" y1="226" x2="344" y2="282" class="fig-line-dashed" stroke="var(--sl-color-purple)"/>
    <line x1="496" y1="206" x2="496" y2="282" class="fig-line-dashed" stroke="var(--sl-color-purple)"/>
    <rect x="96" y="48" width="264" height="20" class="fig-box"/>
    <rect x="360" y="48" width="264" height="20" class="fig-box"/>
    <rect x="96" y="68" width="528" height="20" class="fig-box-blue"/>
    <rect x="96" y="88" width="528" height="20" class="fig-box-green"/>
    <rect x="96" y="108" width="66" height="20" class="fig-box"/>
    <rect x="162" y="108" width="66" height="20" class="fig-box"/>
    <rect x="228" y="108" width="132" height="20" class="fig-box-purple"/>
    <rect x="360" y="108" width="264" height="20" class="fig-box-orange"/>
    <rect x="96" y="128" width="264" height="20" class="fig-box"/>
    <rect x="360" y="128" width="264" height="20" class="fig-box"/>
    <rect x="96" y="148" width="528" height="20" class="fig-box-dashed"/>
    <rect x="246" y="203" width="172" height="18" rx="3" class="fig-box"/>
    <rect x="32" y="236" width="136" height="34" rx="3" class="fig-box-green"/>
    <rect x="168" y="236" width="176" height="34" rx="3" class="fig-box-orange"/>
    <rect x="344" y="236" width="152" height="34" rx="3" class="fig-box-blue"/>
    <rect x="496" y="236" width="192" height="34" rx="3" class="fig-box"/>
    <text x="32" y="36" font-size="12" class="fig-title">TCP 报文段首部格式（20B 固定 + 最大 40B 选项）</text>
    <text x="96" y="43" font-size="9.5" class="fig-muted">0</text>
    <text x="360" y="43" font-size="9.5" text-anchor="middle" class="fig-muted">16</text>
    <text x="624" y="43" font-size="9.5" text-anchor="end" class="fig-muted">31 bit</text>
    <text x="228" y="62" font-size="10" text-anchor="middle" class="fig-label">源端口（16b）</text>
    <text x="492" y="62" font-size="10" text-anchor="middle" class="fig-label">目的端口（16b）</text>
    <text x="360" y="82" font-size="10" text-anchor="middle" class="fig-blue">序号 seq（32b）：本报文段数据第 1 个字节的字节流序号</text>
    <text x="360" y="102" font-size="10" text-anchor="middle" class="fig-green">确认号 ack（32b）：期望收到对方下一报文段第 1 个字节的序号（累积确认）</text>
    <text x="129" y="122" font-size="9" text-anchor="middle" class="fig-label">偏移4b</text>
    <text x="195" y="122" font-size="9" text-anchor="middle" class="fig-muted">保留6b</text>
    <text x="294" y="122" font-size="9" text-anchor="middle" class="fig-purple">URG ACK PSH RST SYN FIN</text>
    <text x="492" y="122" font-size="10" text-anchor="middle" class="fig-orange">窗口 rwnd（16b）：接收方可用缓存</text>
    <text x="228" y="142" font-size="10" text-anchor="middle" class="fig-label">检验和（16b，含伪首部）</text>
    <text x="492" y="142" font-size="10" text-anchor="middle" class="fig-label">紧急指针（16b）</text>
    <text x="360" y="162" font-size="9.5" text-anchor="middle" class="fig-muted">选项（最大 40B：MSS、窗口扩大、时间戳、SACK 选择确认块）+ 填充</text>
    <text x="654" y="102" font-size="10" class="fig-label">20B</text>
    <text x="654" y="116" font-size="9.5" class="fig-muted">固定</text>
    <text x="32" y="208" font-size="12" class="fig-title">面向字节流滑动窗口</text>
    <text x="332" y="216" font-size="10" text-anchor="middle" class="fig-accent">发送窗口 swnd = 20B (31~50)</text>
    <text x="100" y="251" font-size="9.5" text-anchor="middle" class="fig-green">已发已确认</text>
    <text x="100" y="264" font-size="9" text-anchor="middle" class="fig-muted">… 28 29 30</text>
    <text x="256" y="251" font-size="9.5" text-anchor="middle" class="fig-orange">已发未确认（暂存重传）</text>
    <text x="256" y="264" font-size="9" text-anchor="middle" class="fig-muted">31 32 … 40 41（11B）</text>
    <text x="420" y="251" font-size="9.5" text-anchor="middle" class="fig-blue">允许发但尚未发</text>
    <text x="420" y="264" font-size="9" text-anchor="middle" class="fig-muted">42 … 50（可用窗口 9B）</text>
    <text x="592" y="251" font-size="9.5" text-anchor="middle" class="fig-muted">不允许发送（超窗口）</text>
    <text x="592" y="264" font-size="9" text-anchor="middle" class="fig-muted">51 52 53 …</text>
    <text x="168" y="295" font-size="9.5" text-anchor="middle" class="fig-purple">P1(后沿: ack=31)</text>
    <text x="344" y="295" font-size="9.5" text-anchor="middle" class="fig-purple">P2(待发: seq=42)</text>
    <text x="496" y="295" font-size="9.5" text-anchor="middle" class="fig-purple">P3(前沿: P1+swnd)</text>
    <text x="360" y="313" font-size="9.5" text-anchor="middle" class="fig-muted">累积确认 ack=31 代表 30 号及以前全部收到；SACK 可额外标记窗口内已乱序收到的不连续字节块</text>
  </svg>
  <figcaption>图 7-5：TCP 首部关键字段（seq / 累积 ack / rwnd / SACK 选项）与发送滑动窗口三指针四区间结构</figcaption>
</figure>

### 7.5 TCP 的四种计时器

| 计时器 | 作用 | 关键参数 |
|---|---|---|
| <strong>重传计时器</strong> | 超时重传；超时重传时间 RTO 的计算涉及两个公式 | 最后一次课总结 |
| <strong>时间等待计时器</strong> | 连接释放时 TIME-WAIT 状态持续 2MSL | MSL 建议 2 min → 4 min（2022 题 39、2024 题 38 考过两次） |
| <strong>保活计时器</strong> | 探测对端是否存活 | 2 小时；之后每 75 s 一次；连续 10 次无响应则关闭连接 |
| <strong>持续计时器</strong> | 应对零窗口：接收方通告窗口为 0 后，发送方启动持续计时器，到期发零窗口探测报文段 | 见流量控制一节 |

<figure class="cpd-figure not-content" id="cnq-w4-fig-6">
  <svg viewBox="0 0 720 322" role="img" aria-label="TCP 流量控制零窗口死锁与持续计时器探测机制及四种计时器汇总">
    <defs>
      <marker id="cnq-w4-arr-6" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="16" width="400" height="290" rx="6" class="fig-box-subtle"/>
    <rect x="428" y="16" width="276" height="290" rx="6" class="fig-box-subtle"/>
    <line x1="80" y1="66" x2="80" y2="288" class="fig-line"/>
    <line x1="348" y1="66" x2="348" y2="288" class="fig-line"/>
    <line x1="80" y1="82" x2="346" y2="100" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#cnq-w4-arr-6)"/>
    <line x1="348" y1="116" x2="82" y2="134" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#cnq-w4-arr-6)"/>
    <line x1="348" y1="166" x2="218" y2="176" class="fig-line-dashed" stroke="var(--sl-color-red)" marker-end="url(#cnq-w4-arr-6)"/>
    <line x1="80" y1="208" x2="346" y2="226" class="fig-line" stroke="var(--sl-color-purple)" stroke-width="1.8" marker-end="url(#cnq-w4-arr-6)"/>
    <line x1="348" y1="244" x2="82" y2="262" class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.8" marker-end="url(#cnq-w4-arr-6)"/>
    <rect x="40" y="40" width="80" height="24" rx="4" class="fig-box-blue"/>
    <rect x="308" y="40" width="80" height="24" rx="4" class="fig-box-purple"/>
    <rect x="132" y="80" width="164" height="18" rx="3" class="fig-box"/>
    <rect x="118" y="116" width="192" height="18" rx="3" class="fig-box"/>
    <rect x="28" y="140" width="96" height="44" rx="4" class="fig-box-purple"/>
    <rect x="134" y="164" width="80" height="20" rx="3" class="fig-box-red"/>
    <rect x="234" y="158" width="104" height="18" rx="3" class="fig-box"/>
    <rect x="118" y="206" width="192" height="18" rx="3" class="fig-box"/>
    <rect x="116" y="244" width="196" height="18" rx="3" class="fig-box"/>
    <rect x="440" y="48" width="252" height="54" rx="4" class="fig-box-blue"/>
    <rect x="440" y="110" width="252" height="58" rx="4" class="fig-box-purple"/>
    <rect x="440" y="176" width="252" height="56" rx="4" class="fig-box-red"/>
    <rect x="440" y="240" width="252" height="56" rx="4" class="fig-box-orange"/>
    <text x="216" y="32" font-size="12" text-anchor="middle" class="fig-title">流量控制与零窗口持续计时器破死锁</text>
    <text x="80" y="56" font-size="10.5" text-anchor="middle" class="fig-blue">发送方 A</text>
    <text x="348" y="56" font-size="10.5" text-anchor="middle" class="fig-purple">接收方 B</text>
    <text x="214" y="93" font-size="9.5" text-anchor="middle" class="fig-blue">① seq=1, DATA (200B)</text>
    <text x="214" y="129" font-size="9.5" text-anchor="middle" class="fig-orange">② ACK=1, ack=201, rwnd=0 (满)</text>
    <text x="76" y="158" font-size="9.5" text-anchor="middle" class="fig-purple">启动持续</text>
    <text x="76" y="174" font-size="9.5" text-anchor="middle" class="fig-purple">计时器</text>
    <text x="286" y="171" font-size="9" text-anchor="middle" class="fig-red">rwnd=400 通知</text>
    <text x="174" y="178" font-size="9.5" text-anchor="middle" class="fig-red">✕ 报文丢失</text>
    <text x="214" y="219" font-size="9.5" text-anchor="middle" class="fig-purple">③ 到期发零窗口探测报文段 (1B)</text>
    <text x="214" y="257" font-size="9.5" text-anchor="middle" class="fig-green">④ ACK, rwnd=400（破死锁续传）</text>
    <text x="216" y="294" font-size="9.5" text-anchor="middle" class="fig-muted">无持续计时器时：A 等非零窗口、B 等 A 数据 → 双方死锁</text>
    <text x="566" y="34" font-size="12" text-anchor="middle" class="fig-title">TCP 四大计时器考点速查</text>
    <text x="450" y="66" font-size="10" class="fig-blue">1. 重传计时器（RTO）</text>
    <text x="450" y="84" font-size="9.5" class="fig-label">发出报文段即启动；到期未收确认则重传</text>
    <text x="450" y="97" font-size="9" class="fig-muted">RTO = RTT_S + 4 × RTT_D（自适应平滑）</text>
    <text x="450" y="128" font-size="10" class="fig-purple">2. 持续计时器（Persistence）</text>
    <text x="450" y="146" font-size="9.5" class="fig-label">收 rwnd=0 启动；到期发 1B 零窗口探测</text>
    <text x="450" y="160" font-size="9" class="fig-muted">若仍为 0 则重设持续计时器（指数退避）</text>
    <text x="450" y="194" font-size="10" class="fig-red">3. 时间等待计时器（Time-Wait）</text>
    <text x="450" y="212" font-size="9.5" class="fig-label">四次挥手发最后 ACK 后持续 2MSL（4 min）</text>
    <text x="450" y="225" font-size="9" class="fig-muted">保服务器正常 CLOSED + 清空网络残留旧段</text>
    <text x="450" y="258" font-size="10" class="fig-orange">4. 保活计时器（Keepalive）</text>
    <text x="450" y="276" font-size="9.5" class="fig-label">连接空闲 2h 后，每 75s 发探测 × 10 次</text>
    <text x="450" y="289" font-size="9" class="fig-muted">仍无响应判定客户端崩溃，主动关闭连接</text>
  </svg>
  <figcaption>图 7-6：TCP 零窗口死锁成因与持续计时器探测机制时序及四大计时器核心参数对照</figcaption>
</figure>

## 第8章 应用层与其他考点

### 8.1 应用层概述与网络应用模型

应用层是体系结构的<strong>最顶层、离用户最近</strong>，其功能是<strong>通过应用进程的交互实现特定网络应用</strong>；网络应用程序<strong>只运行在端系统</strong>上，传输层为其提供<strong>端到端的进程间逻辑通信</strong>服务。本章按考纲以因特网上的典型网络应用（DNS、FTP、电子邮件、WWW/HTTP、DHCP 等）为例复习其原理、协议与实现。

开发新应用首先要决定应用进程在各端系统上的<strong>组织方式与相互关系</strong>，目前主流两种模型：

| 对比项 | C/S 模型（客户/服务器） | P2P 模型（对等） |
|---|---|---|
| 角色 | 客户是服务请求方，服务器是提供方，角色<strong>固定</strong> | 每个节点既是客户又是服务器，角色<strong>对等</strong> |
| 服务器要求 | 需要一直在线的服务器、固定 IP 与端口 | 不依赖始终在线的服务器，节点间直接交换 |
| 扩展性 | 受服务器能力限制 | 节点越多能力越强，<strong>可扩展性好</strong> |
| 典型应用 | Web、FTP、电子邮件 | 文件分发、即时通信等 |

> <strong>注意</strong>：RIP 与 BGP 从 PDU 逐层封装看属应用层，但<strong>本质功能是路由选择</strong>，因此放在网络层复习——这个“双重身份”是常见陷阱。

<figure class="cpd-figure not-content" id="cnq-w4-fig-7">
  <svg viewBox="0 0 720 314" role="img" aria-label="C/S 与 P2P 网络应用模型对比及 408 应用层协议传输层封装与熟知端口矩阵">
    <defs>
      <marker id="cnq-w4-arr-7" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="16" width="338" height="140" rx="6" class="fig-box-subtle"/>
    <rect x="366" y="16" width="338" height="140" rx="6" class="fig-box-subtle"/>
    <rect x="16" y="166" width="688" height="132" rx="6" class="fig-box-subtle"/>
    <line x1="98" y1="80" x2="146" y2="92" class="fig-line" stroke="var(--sl-color-blue)" marker-start="url(#cnq-w4-arr-7)" marker-end="url(#cnq-w4-arr-7)"/>
    <line x1="98" y1="126" x2="146" y2="108" class="fig-line" stroke="var(--sl-color-blue)" marker-start="url(#cnq-w4-arr-7)" marker-end="url(#cnq-w4-arr-7)"/>
    <line x1="272" y1="98" x2="224" y2="98" class="fig-line" stroke="var(--sl-color-blue)" marker-start="url(#cnq-w4-arr-7)" marker-end="url(#cnq-w4-arr-7)"/>
    <line x1="454" y1="74" x2="526" y2="74" class="fig-line" stroke="var(--sl-color-green)" marker-start="url(#cnq-w4-arr-7)" marker-end="url(#cnq-w4-arr-7)"/>
    <line x1="422" y1="88" x2="466" y2="118" class="fig-line" stroke="var(--sl-color-green)" marker-start="url(#cnq-w4-arr-7)" marker-end="url(#cnq-w4-arr-7)"/>
    <line x1="558" y1="88" x2="514" y2="118" class="fig-line" stroke="var(--sl-color-green)" marker-start="url(#cnq-w4-arr-7)" marker-end="url(#cnq-w4-arr-7)"/>
    <rect x="32" y="64" width="66" height="24" rx="4" class="fig-box"/>
    <rect x="32" y="114" width="66" height="24" rx="4" class="fig-box"/>
    <rect x="272" y="86" width="68" height="24" rx="4" class="fig-box"/>
    <rect x="146" y="78" width="78" height="40" rx="5" class="fig-box-blue"/>
    <rect x="386" y="62" width="68" height="26" rx="4" class="fig-box-green"/>
    <rect x="526" y="62" width="68" height="26" rx="4" class="fig-box-green"/>
    <rect x="456" y="118" width="68" height="26" rx="4" class="fig-box-green"/>
    <rect x="28" y="198" width="96" height="38" rx="4" class="fig-box-blue"/>
    <rect x="132" y="198" width="106" height="38" rx="4" class="fig-box"/>
    <rect x="246" y="198" width="126" height="38" rx="4" class="fig-box"/>
    <rect x="380" y="198" width="104" height="38" rx="4" class="fig-box"/>
    <rect x="492" y="198" width="98" height="38" rx="4" class="fig-box"/>
    <rect x="598" y="198" width="94" height="38" rx="4" class="fig-box-purple"/>
    <rect x="28" y="246" width="96" height="38" rx="4" class="fig-box-orange"/>
    <rect x="132" y="246" width="178" height="38" rx="4" class="fig-box"/>
    <rect x="318" y="246" width="198" height="38" rx="4" class="fig-box"/>
    <rect x="524" y="246" width="168" height="38" rx="4" class="fig-box-purple"/>
    <text x="185" y="36" font-size="12" text-anchor="middle" class="fig-title">C/S 模型（客户角色与服务器固定）</text>
    <text x="65" y="80" font-size="9.5" text-anchor="middle" class="fig-label">客户 A</text>
    <text x="65" y="130" font-size="9.5" text-anchor="middle" class="fig-label">客户 B</text>
    <text x="306" y="102" font-size="9.5" text-anchor="middle" class="fig-label">客户 C</text>
    <text x="185" y="95" font-size="10" text-anchor="middle" class="fig-blue">中心服务器</text>
    <text x="185" y="110" font-size="9" text-anchor="middle" class="fig-muted">常在线·固定IP</text>
    <text x="185" y="148" font-size="9" text-anchor="middle" class="fig-muted">客户间不直接通信，扩展性受限于服务器</text>
    <text x="535" y="36" font-size="12" text-anchor="middle" class="fig-title">P2P 模型（对等连接·自扩展）</text>
    <text x="420" y="79" font-size="9.5" text-anchor="middle" class="fig-green">对等方 P1</text>
    <text x="560" y="79" font-size="9.5" text-anchor="middle" class="fig-green">对等方 P2</text>
    <text x="490" y="135" font-size="9.5" text-anchor="middle" class="fig-green">对等方 P3</text>
    <text x="648" y="96" font-size="9.5" text-anchor="middle" class="fig-label">既是客户</text>
    <text x="648" y="112" font-size="9.5" text-anchor="middle" class="fig-label">又是服务器</text>
    <text x="648" y="130" font-size="9" text-anchor="middle" class="fig-muted">节点越多越强</text>
    <text x="360" y="186" font-size="12" text-anchor="middle" class="fig-title">408 应用层高频协议底层传输协议与熟知端口号速查</text>
    <text x="76" y="215" font-size="10.5" text-anchor="middle" class="fig-blue">基于 TCP</text>
    <text x="76" y="229" font-size="9" text-anchor="middle" class="fig-muted">面向连接可靠</text>
    <text x="185" y="215" font-size="10" text-anchor="middle" class="fig-label">HTTP: 80</text>
    <text x="185" y="229" font-size="9" text-anchor="middle" class="fig-muted">HTTPS: 443</text>
    <text x="309" y="215" font-size="10" text-anchor="middle" class="fig-label">FTP: 21 / 20</text>
    <text x="309" y="229" font-size="9" text-anchor="middle" class="fig-muted">控制21 · 数据20</text>
    <text x="432" y="215" font-size="10" text-anchor="middle" class="fig-label">SMTP: 25</text>
    <text x="432" y="229" font-size="9" text-anchor="middle" class="fig-muted">推邮件协议</text>
    <text x="541" y="215" font-size="10" text-anchor="middle" class="fig-label">POP3: 110</text>
    <text x="541" y="229" font-size="9" text-anchor="middle" class="fig-muted">拉邮件协议</text>
    <text x="645" y="215" font-size="10" text-anchor="middle" class="fig-purple">BGP: 179</text>
    <text x="645" y="229" font-size="9" text-anchor="middle" class="fig-muted">路由选路陷阱</text>
    <text x="76" y="263" font-size="10.5" text-anchor="middle" class="fig-orange">基于 UDP</text>
    <text x="76" y="277" font-size="9" text-anchor="middle" class="fig-muted">无连接低时延</text>
    <text x="221" y="263" font-size="10" text-anchor="middle" class="fig-label">DNS: 53</text>
    <text x="221" y="277" font-size="9" text-anchor="middle" class="fig-muted">查询用UDP（主从同步TCP）</text>
    <text x="417" y="263" font-size="10" text-anchor="middle" class="fig-label">DHCP: 服务器 67 / 客户 68</text>
    <text x="417" y="277" font-size="9" text-anchor="middle" class="fig-muted">未获 IP 前全程局域网广播</text>
    <text x="608" y="263" font-size="10" text-anchor="middle" class="fig-purple">RIP: 520</text>
    <text x="608" y="277" font-size="9" text-anchor="middle" class="fig-muted">UDP封装·网络层功能</text>
  </svg>
  <figcaption>图 8-1：C/S 与 P2P 应用架构对比及 408 常考应用层协议（含 RIP/BGP 双重身份）端口矩阵</figcaption>
</figure>

<figure class="cpd-figure not-content" id="cnq-w4-fig-8">
  <svg viewBox="0 0 720 326" role="img" aria-label="DNS 域名解析递归查询与迭代查询完整报文流转图">
    <defs>
      <marker id="cnq-w4-arr-8" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="16" width="468" height="294" rx="6" class="fig-box-subtle"/>
    <rect x="496" y="16" width="208" height="294" rx="6" class="fig-box-subtle"/>
    <line x1="108" y1="164" x2="176" y2="164" class="fig-line" stroke="var(--sl-color-purple)" marker-end="url(#cnq-w4-arr-8)"/>
    <line x1="178" y1="184" x2="110" y2="184" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#cnq-w4-arr-8)"/>
    <line x1="274" y1="146" x2="344" y2="76" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#cnq-w4-arr-8)"/>
    <line x1="346" y1="92" x2="276" y2="158" class="fig-line-dashed" stroke="var(--sl-color-blue)" marker-end="url(#cnq-w4-arr-8)"/>
    <line x1="284" y1="166" x2="344" y2="166" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#cnq-w4-arr-8)"/>
    <line x1="346" y1="182" x2="286" y2="182" class="fig-line-dashed" stroke="var(--sl-color-orange)" marker-end="url(#cnq-w4-arr-8)"/>
    <line x1="276" y1="192" x2="344" y2="254" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#cnq-w4-arr-8)"/>
    <line x1="346" y1="270" x2="274" y2="204" class="fig-line-dashed" stroke="var(--sl-color-green)" marker-end="url(#cnq-w4-arr-8)"/>
    <rect x="28" y="150" width="80" height="48" rx="4" class="fig-box"/>
    <rect x="178" y="142" width="106" height="64" rx="5" class="fig-box-purple"/>
    <rect x="346" y="56" width="124" height="44" rx="4" class="fig-box-blue"/>
    <rect x="346" y="152" width="124" height="44" rx="4" class="fig-box-orange"/>
    <rect x="346" y="242" width="124" height="44" rx="4" class="fig-box-green"/>
    <rect x="116" y="142" width="54" height="16" rx="3" class="fig-box"/>
    <rect x="116" y="190" width="54" height="16" rx="3" class="fig-box"/>
    <rect x="278" y="92" width="54" height="16" rx="3" class="fig-box"/>
    <rect x="292" y="144" width="46" height="16" rx="3" class="fig-box"/>
    <rect x="280" y="232" width="54" height="16" rx="3" class="fig-box"/>
    <rect x="508" y="50" width="184" height="84" rx="4" class="fig-box-purple"/>
    <rect x="508" y="144" width="184" height="74" rx="4" class="fig-box-blue"/>
    <rect x="508" y="228" width="184" height="70" rx="4" class="fig-box-green"/>
    <text x="250" y="34" font-size="12" text-anchor="middle" class="fig-title">DNS 标准解析：主机递归 + 本地域名服务器迭代（共 8 报文）</text>
    <text x="68" y="170" font-size="10" text-anchor="middle" class="fig-label">请求主机</text>
    <text x="68" y="186" font-size="9" text-anchor="middle" class="fig-muted">查 y.abc.com</text>
    <text x="231" y="164" font-size="10" text-anchor="middle" class="fig-purple">本地域名服务器</text>
    <text x="231" y="180" font-size="9" text-anchor="middle" class="fig-muted">代客查询 +</text>
    <text x="231" y="194" font-size="9" text-anchor="middle" class="fig-muted">高速缓存 TTL</text>
    <text x="408" y="74" font-size="10" text-anchor="middle" class="fig-blue">根域名服务器</text>
    <text x="408" y="90" font-size="9" text-anchor="middle" class="fig-muted">告知 .com 地址</text>
    <text x="408" y="170" font-size="10" text-anchor="middle" class="fig-orange">顶级域名服务器</text>
    <text x="408" y="186" font-size="9" text-anchor="middle" class="fig-muted">告知 abc.com 地址</text>
    <text x="408" y="260" font-size="10" text-anchor="middle" class="fig-green">权限域名服务器</text>
    <text x="408" y="276" font-size="9" text-anchor="middle" class="fig-muted">给出最终目标 IP</text>
    <text x="143" y="154" font-size="9" text-anchor="middle" class="fig-purple">① 递归</text>
    <text x="143" y="202" font-size="9" text-anchor="middle" class="fig-green">⑧ 结果</text>
    <text x="305" y="104" font-size="9" text-anchor="middle" class="fig-blue">②③ 迭代</text>
    <text x="315" y="156" font-size="9" text-anchor="middle" class="fig-orange">④⑤</text>
    <text x="307" y="244" font-size="9" text-anchor="middle" class="fig-green">⑥⑦ 迭代</text>
    <text x="164" y="298" font-size="9.5" text-anchor="middle" class="fig-muted">UDP 53 端口：无连接免握手，解析时延最小</text>
    <text x="600" y="34" font-size="12" text-anchor="middle" class="fig-title">递归 vs 迭代辨析</text>
    <text x="518" y="68" font-size="10" class="fig-purple">递归查询（替你问到底）：</text>
    <text x="518" y="86" font-size="9.5" class="fig-label">主机 → 本地域名服务器。</text>
    <text x="518" y="102" font-size="9" class="fig-muted">若全递归：本地→根→顶级→权限</text>
    <text x="518" y="118" font-size="9" class="fig-muted">层层代问，根负载过重极少用</text>
    <text x="518" y="162" font-size="10" class="fig-blue">迭代查询（指路你自己问）：</text>
    <text x="518" y="180" font-size="9.5" class="fig-label">本地 DNS 依次向根、顶级、</text>
    <text x="518" y="196" font-size="9.5" class="fig-label">权限服务器查询下一跳地址</text>
    <text x="518" y="246" font-size="10" class="fig-green">高速缓存命中：</text>
    <text x="518" y="264" font-size="9.5" class="fig-label">本机 hosts / 浏览器缓存 /</text>
    <text x="518" y="280" font-size="9.5" class="fig-muted">本地 DNS 命中则直接返回 IP</text>
  </svg>
  <figcaption>图 8-2：DNS 域名解析递归查询与迭代查询（8 报文路径、全递归对比及高速缓存机制）</figcaption>
</figure>

<figure class="cpd-figure not-content" id="cnq-w4-fig-9">
  <svg viewBox="0 0 720 336" role="img" aria-label="HTTP/1.0 非持续连接与 HTTP/1.1 持续连接非流水线及流水线模式 RTT 时延对比">
    <defs>
      <marker id="cnq-w4-arr-9" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="16" width="220" height="304" rx="6" class="fig-box-subtle"/>
    <rect x="250" y="16" width="220" height="304" rx="6" class="fig-box-subtle"/>
    <rect x="484" y="16" width="220" height="304" rx="6" class="fig-box-subtle"/>
    <line x1="60" y1="68" x2="60" y2="254" class="fig-line"/>
    <line x1="192" y1="68" x2="192" y2="254" class="fig-line"/>
    <line x1="60" y1="76" x2="190" y2="86" class="fig-line-dashed" marker-end="url(#cnq-w4-arr-9)"/>
    <line x1="192" y1="88" x2="62" y2="98" class="fig-line-dashed" marker-end="url(#cnq-w4-arr-9)"/>
    <line x1="60" y1="100" x2="190" y2="112" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#cnq-w4-arr-9)"/>
    <line x1="192" y1="114" x2="62" y2="128" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#cnq-w4-arr-9)"/>
    <line x1="60" y1="146" x2="190" y2="156" class="fig-line-dashed" marker-end="url(#cnq-w4-arr-9)"/>
    <line x1="192" y1="158" x2="62" y2="168" class="fig-line-dashed" marker-end="url(#cnq-w4-arr-9)"/>
    <line x1="60" y1="170" x2="190" y2="182" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#cnq-w4-arr-9)"/>
    <line x1="192" y1="184" x2="62" y2="198" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#cnq-w4-arr-9)"/>
    <line x1="294" y1="68" x2="294" y2="254" class="fig-line"/>
    <line x1="426" y1="68" x2="426" y2="254" class="fig-line"/>
    <line x1="294" y1="76" x2="424" y2="88" class="fig-line-dashed" marker-end="url(#cnq-w4-arr-9)"/>
    <line x1="426" y1="90" x2="296" y2="102" class="fig-line-dashed" marker-end="url(#cnq-w4-arr-9)"/>
    <line x1="294" y1="106" x2="424" y2="120" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#cnq-w4-arr-9)"/>
    <line x1="426" y1="122" x2="296" y2="136" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#cnq-w4-arr-9)"/>
    <line x1="294" y1="146" x2="424" y2="160" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#cnq-w4-arr-9)"/>
    <line x1="426" y1="162" x2="296" y2="176" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#cnq-w4-arr-9)"/>
    <line x1="294" y1="186" x2="424" y2="200" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#cnq-w4-arr-9)"/>
    <line x1="426" y1="202" x2="296" y2="216" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#cnq-w4-arr-9)"/>
    <line x1="528" y1="68" x2="528" y2="254" class="fig-line"/>
    <line x1="660" y1="68" x2="660" y2="254" class="fig-line"/>
    <line x1="528" y1="76" x2="658" y2="88" class="fig-line-dashed" marker-end="url(#cnq-w4-arr-9)"/>
    <line x1="660" y1="90" x2="530" y2="102" class="fig-line-dashed" marker-end="url(#cnq-w4-arr-9)"/>
    <line x1="528" y1="108" x2="658" y2="122" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#cnq-w4-arr-9)"/>
    <line x1="660" y1="124" x2="530" y2="138" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#cnq-w4-arr-9)"/>
    <line x1="528" y1="152" x2="658" y2="168" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#cnq-w4-arr-9)"/>
    <line x1="528" y1="162" x2="658" y2="178" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#cnq-w4-arr-9)"/>
    <line x1="528" y1="172" x2="658" y2="188" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#cnq-w4-arr-9)"/>
    <line x1="660" y1="174" x2="530" y2="194" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#cnq-w4-arr-9)"/>
    <line x1="660" y1="184" x2="530" y2="204" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#cnq-w4-arr-9)"/>
    <line x1="660" y1="194" x2="530" y2="214" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#cnq-w4-arr-9)"/>
    <rect x="86" y="76" width="80" height="16" rx="3" class="fig-box"/>
    <rect x="84" y="106" width="84" height="16" rx="3" class="fig-box"/>
    <rect x="86" y="146" width="80" height="16" rx="3" class="fig-box"/>
    <rect x="84" y="176" width="84" height="16" rx="3" class="fig-box"/>
    <rect x="28" y="258" width="196" height="50" rx="4" class="fig-box-red"/>
    <rect x="318" y="80" width="84" height="16" rx="3" class="fig-box"/>
    <rect x="318" y="114" width="84" height="16" rx="3" class="fig-box"/>
    <rect x="318" y="154" width="84" height="16" rx="3" class="fig-box"/>
    <rect x="318" y="194" width="84" height="16" rx="3" class="fig-box"/>
    <rect x="262" y="258" width="196" height="50" rx="4" class="fig-box-orange"/>
    <rect x="552" y="80" width="84" height="16" rx="3" class="fig-box"/>
    <rect x="552" y="116" width="84" height="16" rx="3" class="fig-box"/>
    <rect x="544" y="174" width="100" height="20" rx="3" class="fig-box"/>
    <rect x="496" y="258" width="196" height="50" rx="4" class="fig-box-green"/>
    <text x="126" y="34" font-size="11.5" text-anchor="middle" class="fig-title">HTTP/1.0 非持续连接</text>
    <text x="60" y="60" font-size="9.5" text-anchor="middle" class="fig-muted">客户</text>
    <text x="192" y="60" font-size="9.5" text-anchor="middle" class="fig-muted">服务器</text>
    <text x="126" y="88" font-size="9" text-anchor="middle" class="fig-muted">TCP 握手 1RTT</text>
    <text x="126" y="118" font-size="9" text-anchor="middle" class="fig-blue">HTML 1RTT(关)</text>
    <text x="126" y="158" font-size="9" text-anchor="middle" class="fig-muted">再握手 1RTT</text>
    <text x="126" y="188" font-size="9" text-anchor="middle" class="fig-orange">对象1 1RTT(关)</text>
    <text x="126" y="232" font-size="9.5" text-anchor="middle" class="fig-muted">… 余下对象逐个重连 …</text>
    <text x="126" y="277" font-size="10" text-anchor="middle" class="fig-red">每对象 2 RTT（握手+请求）</text>
    <text x="126" y="297" font-size="9.5" text-anchor="middle" class="fig-label">串行总计 = 2(N + 1) RTT</text>
    <text x="360" y="34" font-size="11.5" text-anchor="middle" class="fig-title">HTTP/1.1 持续·非流水线</text>
    <text x="294" y="60" font-size="9.5" text-anchor="middle" class="fig-muted">客户</text>
    <text x="426" y="60" font-size="9.5" text-anchor="middle" class="fig-muted">服务器</text>
    <text x="360" y="92" font-size="9" text-anchor="middle" class="fig-muted">仅握手 1 次</text>
    <text x="360" y="126" font-size="9" text-anchor="middle" class="fig-blue">基础 HTML 1RTT</text>
    <text x="360" y="166" font-size="9" text-anchor="middle" class="fig-orange">引用对象1 1RTT</text>
    <text x="360" y="206" font-size="9" text-anchor="middle" class="fig-orange">引用对象2 1RTT</text>
    <text x="360" y="238" font-size="9.5" text-anchor="middle" class="fig-muted">… 收完前一个再发下一个 …</text>
    <text x="360" y="277" font-size="10" text-anchor="middle" class="fig-orange">复用连接，省去重复握手</text>
    <text x="360" y="297" font-size="9.5" text-anchor="middle" class="fig-label">总计 = (2 + N) RTT</text>
    <text x="594" y="34" font-size="11.5" text-anchor="middle" class="fig-title">HTTP/1.1 持续·流水线</text>
    <text x="528" y="60" font-size="9.5" text-anchor="middle" class="fig-muted">客户</text>
    <text x="660" y="60" font-size="9.5" text-anchor="middle" class="fig-muted">服务器</text>
    <text x="594" y="92" font-size="9" text-anchor="middle" class="fig-muted">仅握手 1 次</text>
    <text x="594" y="128" font-size="9" text-anchor="middle" class="fig-blue">基础 HTML 1RTT</text>
    <text x="594" y="188" font-size="9" text-anchor="middle" class="fig-green">N 对象连发共 1RTT</text>
    <text x="594" y="238" font-size="9.5" text-anchor="middle" class="fig-muted">连续发请求，服务器连续回</text>
    <text x="594" y="277" font-size="10" text-anchor="middle" class="fig-green">N 个内嵌对象合占 1 RTT</text>
    <text x="594" y="297" font-size="9.5" text-anchor="middle" class="fig-label">总计 = 3 RTT（含建连）</text>
  </svg>
  <figcaption>图 8-3：HTTP/1.0 非持续连接与 HTTP/1.1 持续连接（非流水线 vs 流水线）请求 1 个 HTML + N 个引用对象的 RTT 计算（注：第 3 次握手若捎带 HTTP 请求则各减 0，408 须审清“第 3 次握手是否捎带请求”）</figcaption>
</figure>

<figure class="cpd-figure not-content" id="cnq-w4-fig-10">
  <svg viewBox="0 0 720 304" role="img" aria-label="FTP 21 号控制连接与 20 号主动模式 PORT 及被动模式 PASV 数据连接对比">
    <defs>
      <marker id="cnq-w4-arr-10" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="16" width="338" height="272" rx="6" class="fig-box-subtle"/>
    <rect x="366" y="16" width="338" height="272" rx="6" class="fig-box-subtle"/>
    <line x1="118" y1="98" x2="250" y2="98" class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.8" marker-start="url(#cnq-w4-arr-10)" marker-end="url(#cnq-w4-arr-10)"/>
    <line x1="252" y1="168" x2="120" y2="168" class="fig-line" stroke="var(--sl-color-orange)" stroke-width="2" marker-end="url(#cnq-w4-arr-10)"/>
    <line x1="468" y1="98" x2="600" y2="98" class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.8" marker-start="url(#cnq-w4-arr-10)" marker-end="url(#cnq-w4-arr-10)"/>
    <line x1="468" y1="168" x2="600" y2="168" class="fig-line" stroke="var(--sl-color-green)" stroke-width="2" marker-end="url(#cnq-w4-arr-10)"/>
    <rect x="28" y="68" width="90" height="132" rx="5" class="fig-box"/>
    <rect x="252" y="68" width="90" height="132" rx="5" class="fig-box"/>
    <rect x="36" y="84" width="74" height="28" rx="3" class="fig-box-blue"/>
    <rect x="36" y="154" width="74" height="28" rx="3" class="fig-box-orange"/>
    <rect x="260" y="84" width="74" height="28" rx="3" class="fig-box-blue"/>
    <rect x="260" y="154" width="74" height="28" rx="3" class="fig-box-orange"/>
    <rect x="132" y="76" width="106" height="18" rx="3" class="fig-box"/>
    <rect x="128" y="146" width="114" height="18" rx="3" class="fig-box"/>
    <rect x="28" y="214" width="314" height="62" rx="4" class="fig-box-orange"/>
    <rect x="378" y="68" width="90" height="132" rx="5" class="fig-box"/>
    <rect x="602" y="68" width="90" height="132" rx="5" class="fig-box"/>
    <rect x="386" y="84" width="74" height="28" rx="3" class="fig-box-blue"/>
    <rect x="386" y="154" width="74" height="28" rx="3" class="fig-box-green"/>
    <rect x="610" y="84" width="74" height="28" rx="3" class="fig-box-blue"/>
    <rect x="610" y="154" width="74" height="28" rx="3" class="fig-box-green"/>
    <rect x="482" y="76" width="106" height="18" rx="3" class="fig-box"/>
    <rect x="478" y="146" width="114" height="18" rx="3" class="fig-box"/>
    <rect x="378" y="214" width="314" height="62" rx="4" class="fig-box-green"/>
    <text x="185" y="36" font-size="12" text-anchor="middle" class="fig-title">FTP 主动模式（PORT）：服务器 20 主动连</text>
    <text x="73" y="62" font-size="10" text-anchor="middle" class="fig-label">FTP 客户</text>
    <text x="297" y="62" font-size="10" text-anchor="middle" class="fig-label">FTP 服务器</text>
    <text x="73" y="102" font-size="9.5" text-anchor="middle" class="fig-blue">临时端口 P</text>
    <text x="297" y="102" font-size="9.5" text-anchor="middle" class="fig-blue">控制端口 21</text>
    <text x="185" y="89" font-size="9" text-anchor="middle" class="fig-blue">控制连接（全程保持）</text>
    <text x="185" y="114" font-size="8.5" text-anchor="middle" class="fig-muted">PORT 命令告知 P+1</text>
    <text x="73" y="172" font-size="9.5" text-anchor="middle" class="fig-orange">端口 P+1</text>
    <text x="297" y="172" font-size="9.5" text-anchor="middle" class="fig-orange">数据端口 20</text>
    <text x="185" y="159" font-size="9" text-anchor="middle" class="fig-orange">服务器主动建数据连接</text>
    <text x="185" y="184" font-size="8.5" text-anchor="middle" class="fig-muted">传完文件立即关闭</text>
    <text x="40" y="234" font-size="9.5" class="fig-orange">• 控制信息「带外传送」（不与文件走同一连接）</text>
    <text x="40" y="252" font-size="9.5" class="fig-label">• PORT 模式固定使用服务器熟知端口 TCP 20</text>
    <text x="40" y="268" font-size="9" class="fig-muted">• 缺点：服务器反向连客户易被客户端防火墙拦截</text>
    <text x="535" y="36" font-size="12" text-anchor="middle" class="fig-title">FTP 被动模式（PASV）：客户主动连临时口</text>
    <text x="423" y="62" font-size="10" text-anchor="middle" class="fig-label">FTP 客户</text>
    <text x="647" y="62" font-size="10" text-anchor="middle" class="fig-label">FTP 服务器</text>
    <text x="423" y="102" font-size="9.5" text-anchor="middle" class="fig-blue">临时端口 P</text>
    <text x="647" y="102" font-size="9.5" text-anchor="middle" class="fig-blue">控制端口 21</text>
    <text x="535" y="89" font-size="9" text-anchor="middle" class="fig-blue">控制连接（全程保持）</text>
    <text x="535" y="114" font-size="8.5" text-anchor="middle" class="fig-muted">PASV 返回随机端口 Q</text>
    <text x="423" y="172" font-size="9.5" text-anchor="middle" class="fig-green">端口 P+1</text>
    <text x="647" y="172" font-size="9.5" text-anchor="middle" class="fig-green">随机端口 Q</text>
    <text x="535" y="159" font-size="9" text-anchor="middle" class="fig-green">客户主动发起数据连接</text>
    <text x="535" y="184" font-size="8.5" text-anchor="middle" class="fig-muted">Q &gt; 1023（不用 20）</text>
    <text x="390" y="234" font-size="9.5" class="fig-green">• PASV 模式下服务器数据端口由双方协商（非 20！）</text>
    <text x="390" y="252" font-size="9.5" class="fig-label">• 传 N 个文件共需建立 1 + N 条 TCP 连接</text>
    <text x="390" y="268" font-size="9" class="fig-muted">• FTP 可屏蔽各操作系统文件目录异构细节</text>
  </svg>
  <figcaption>图 8-4：FTP 双 TCP 连接机制（21 号控制连接带外传送 vs 20 号主动 PORT / 随机端口被动 PASV 数据连接）</figcaption>
</figure>

<figure class="cpd-figure not-content" id="cnq-w4-fig-11">
  <svg viewBox="0 0 720 318" role="img" aria-label="DHCP 四报文广播分配 IP 时序与电子邮件 SMTP 推及 POP3 拉协议模型">
    <defs>
      <marker id="cnq-w4-arr-11" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="16" width="332" height="286" rx="6" class="fig-box-subtle"/>
    <rect x="360" y="16" width="344" height="286" rx="6" class="fig-box-subtle"/>
    <line x1="74" y1="68" x2="74" y2="256" class="fig-line"/>
    <line x1="286" y1="68" x2="286" y2="256" class="fig-line"/>
    <line x1="74" y1="84" x2="284" y2="102" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#cnq-w4-arr-11)"/>
    <line x1="286" y1="124" x2="76" y2="142" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#cnq-w4-arr-11)"/>
    <line x1="74" y1="164" x2="284" y2="182" class="fig-line" stroke="var(--sl-color-purple)" marker-end="url(#cnq-w4-arr-11)"/>
    <line x1="286" y1="204" x2="76" y2="222" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#cnq-w4-arr-11)"/>
    <line x1="484" y1="76" x2="570" y2="76" class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.8" marker-end="url(#cnq-w4-arr-11)"/>
    <line x1="632" y1="96" x2="632" y2="150" class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.8" marker-end="url(#cnq-w4-arr-11)"/>
    <line x1="572" y1="172" x2="486" y2="172" class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.8" marker-end="url(#cnq-w4-arr-11)"/>
    <rect x="30" y="42" width="88" height="24" rx="4" class="fig-box-blue"/>
    <rect x="240" y="42" width="92" height="24" rx="4" class="fig-box-purple"/>
    <rect x="94" y="82" width="172" height="18" rx="3" class="fig-box"/>
    <rect x="94" y="122" width="172" height="18" rx="3" class="fig-box"/>
    <rect x="94" y="162" width="172" height="18" rx="3" class="fig-box"/>
    <rect x="94" y="202" width="172" height="18" rx="3" class="fig-box"/>
    <rect x="374" y="56" width="110" height="40" rx="4" class="fig-box"/>
    <rect x="572" y="56" width="118" height="40" rx="4" class="fig-box-blue"/>
    <rect x="572" y="152" width="118" height="40" rx="4" class="fig-box-blue"/>
    <rect x="374" y="152" width="110" height="40" rx="4" class="fig-box"/>
    <rect x="492" y="56" width="72" height="18" rx="3" class="fig-box"/>
    <rect x="556" y="114" width="136" height="18" rx="3" class="fig-box"/>
    <rect x="492" y="152" width="72" height="18" rx="3" class="fig-box"/>
    <rect x="374" y="206" width="316" height="82" rx="4" class="fig-box-orange"/>
    <text x="182" y="34" font-size="12" text-anchor="middle" class="fig-title">DHCP 动态配置四步广播（UDP）</text>
    <text x="74" y="58" font-size="9.5" text-anchor="middle" class="fig-blue">客户 (UDP 68)</text>
    <text x="286" y="58" font-size="9.5" text-anchor="middle" class="fig-purple">服务器 (UDP 67)</text>
    <text x="180" y="95" font-size="9" text-anchor="middle" class="fig-blue">① DISCOVER 发现（广播）</text>
    <text x="180" y="135" font-size="9" text-anchor="middle" class="fig-orange">② OFFER 提供租约（广播）</text>
    <text x="180" y="175" font-size="9" text-anchor="middle" class="fig-purple">③ REQUEST 选择确认（广播）</text>
    <text x="180" y="215" font-size="9" text-anchor="middle" class="fig-green">④ ACK 正式绑定（广播）</text>
    <text x="182" y="272" font-size="9" text-anchor="middle" class="fig-label">源 0.0.0.0 → 目的 255.255.255.255</text>
    <text x="182" y="288" font-size="9" text-anchor="middle" class="fig-muted">跨网段分配需路由器配置 DHCP 中继代理</text>
    <text x="532" y="34" font-size="12" text-anchor="middle" class="fig-title">电子邮件系统：推协议 SMTP + 拉协议 POP3</text>
    <text x="429" y="74" font-size="9.5" text-anchor="middle" class="fig-label">发信人代理 UA</text>
    <text x="429" y="88" font-size="8.5" text-anchor="middle" class="fig-muted">写信客户端</text>
    <text x="631" y="74" font-size="9.5" text-anchor="middle" class="fig-blue">发送方邮件服务器</text>
    <text x="631" y="88" font-size="8.5" text-anchor="middle" class="fig-muted">发送缓存队列</text>
    <text x="631" y="170" font-size="9.5" text-anchor="middle" class="fig-blue">接收方邮件服务器</text>
    <text x="631" y="184" font-size="8.5" text-anchor="middle" class="fig-muted">收信人邮箱</text>
    <text x="429" y="170" font-size="9.5" text-anchor="middle" class="fig-label">收信人代理 UA</text>
    <text x="429" y="184" font-size="8.5" text-anchor="middle" class="fig-muted">读信客户端</text>
    <text x="528" y="69" font-size="9" text-anchor="middle" class="fig-blue">① SMTP:25</text>
    <text x="624" y="127" font-size="9" text-anchor="middle" class="fig-blue">② SMTP 推 (TCP 25)</text>
    <text x="528" y="165" font-size="9" text-anchor="middle" class="fig-green">③ POP3:110</text>
    <text x="386" y="226" font-size="9.5" class="fig-orange">• SMTP 仅传 7 位 ASCII 码，传附件/中文须靠 MIME 编码转换</text>
    <text x="386" y="246" font-size="9.5" class="fig-label">• SMTP 只能「推」不能「拉」，读取邮箱必须用 POP3 或 IMAP</text>
    <text x="386" y="266" font-size="9" class="fig-muted">• 网页邮箱（WebMail）：浏览器收发均用 HTTP，服务器间仍用 SMTP</text>
  </svg>
  <figcaption>图 8-5：DHCP 四步广播发现分配机制（UDP 67/68）与电子邮件 SMTP（推）/ POP3（拉）架构</figcaption>
</figure>

### 8.2 虚拟局域网 VLAN

2022 年新增考点，大纲只要求<strong>基本概念与基本工作原理</strong>。首次考察时把 VLAN 的作用与 <strong>ARP</strong> 综合在一起考。

<strong>核心机制</strong>：与 VLAN 相关的帧是 <strong>IEEE 802.1Q 帧</strong>——在以太网 V2 的 MAC 帧的<strong>源地址字段与类型字段之间插入 4 字节 VLAN 标签</strong>。标签中最重要的是 <strong>12 比特的 VLAN ID（VID），设备用 VID 识别帧属于哪个 VLAN；广播帧和单播帧只在同一 VLAN 内转发</strong>，从而把<strong>广播域限制在一个 VLAN 内</strong>。

<figure class="cpd-figure not-content" id="cnq-w4-fig-12">
  <svg viewBox="0 0 720 330" role="img" aria-label="IEEE 802.1Q VLAN 帧格式 4 字节标签展开及交换机 Access 与 Trunk 接口转发机制">
    <defs>
      <marker id="cnq-w4-arr-12" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="16" width="688" height="180" rx="6" class="fig-box-subtle"/>
    <rect x="16" y="206" width="688" height="108" rx="6" class="fig-box-subtle"/>
    <line x1="224" y1="118" x2="104" y2="148" class="fig-line-dashed" stroke="var(--sl-color-blue)"/>
    <line x1="308" y1="118" x2="616" y2="148" class="fig-line-dashed" stroke="var(--sl-color-blue)"/>
    <line x1="118" y1="264" x2="210" y2="264" class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.8" marker-end="url(#cnq-w4-arr-12)"/>
    <line x1="312" y1="264" x2="424" y2="264" class="fig-line" stroke="var(--sl-color-blue)" stroke-width="2.2" marker-end="url(#cnq-w4-arr-12)"/>
    <line x1="526" y1="264" x2="606" y2="264" class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.8" marker-end="url(#cnq-w4-arr-12)"/>
    <rect x="32" y="42" width="96" height="26" class="fig-box"/>
    <rect x="128" y="42" width="96" height="26" class="fig-box"/>
    <rect x="224" y="42" width="76" height="26" class="fig-box"/>
    <rect x="300" y="42" width="224" height="26" class="fig-box"/>
    <rect x="524" y="42" width="76" height="26" class="fig-box"/>
    <rect x="32" y="92" width="96" height="26" class="fig-box"/>
    <rect x="128" y="92" width="96" height="26" class="fig-box"/>
    <rect x="224" y="92" width="84" height="26" class="fig-box-blue"/>
    <rect x="308" y="92" width="76" height="26" class="fig-box"/>
    <rect x="384" y="92" width="224" height="26" class="fig-box"/>
    <rect x="608" y="92" width="76" height="26" class="fig-box"/>
    <rect x="104" y="148" width="156" height="28" class="fig-box-purple"/>
    <rect x="260" y="148" width="78" height="28" class="fig-box"/>
    <rect x="338" y="148" width="74" height="28" class="fig-box"/>
    <rect x="412" y="148" width="204" height="28" class="fig-box-orange"/>
    <rect x="32" y="246" width="86" height="36" rx="4" class="fig-box"/>
    <rect x="212" y="242" width="100" height="44" rx="4" class="fig-box-purple"/>
    <rect x="426" y="242" width="100" height="44" rx="4" class="fig-box-purple"/>
    <rect x="608" y="246" width="82" height="36" rx="4" class="fig-box"/>
    <rect x="126" y="242" width="78" height="16" rx="3" class="fig-box"/>
    <rect x="322" y="242" width="94" height="16" rx="3" class="fig-box"/>
    <rect x="530" y="242" width="72" height="16" rx="3" class="fig-box"/>
    <text x="32" y="35" font-size="11" class="fig-muted">标准以太网 V2 帧（未打标签，最大帧长 1518B）：</text>
    <text x="80" y="59" font-size="10" text-anchor="middle" class="fig-label">目的 MAC 6B</text>
    <text x="176" y="59" font-size="10" text-anchor="middle" class="fig-label">源 MAC 6B</text>
    <text x="262" y="59" font-size="10" text-anchor="middle" class="fig-label">类型 2B</text>
    <text x="412" y="59" font-size="10" text-anchor="middle" class="fig-label">数据载荷 46 ~ 1500B</text>
    <text x="562" y="59" font-size="10" text-anchor="middle" class="fig-label">FCS 4B</text>
    <text x="32" y="85" font-size="11" class="fig-title">IEEE 802.1Q 帧（源地址与类型之间插入 4B 标签，最大帧长 1522B）：</text>
    <text x="80" y="109" font-size="10" text-anchor="middle" class="fig-label">目的 MAC 6B</text>
    <text x="176" y="109" font-size="10" text-anchor="middle" class="fig-label">源 MAC 6B</text>
    <text x="266" y="109" font-size="10" text-anchor="middle" class="fig-blue">802.1Q 4B</text>
    <text x="346" y="109" font-size="10" text-anchor="middle" class="fig-label">类型 2B</text>
    <text x="496" y="109" font-size="10" text-anchor="middle" class="fig-label">数据载荷 46 ~ 1500B</text>
    <text x="646" y="109" font-size="10" text-anchor="middle" class="fig-label">FCS 4B</text>
    <text x="182" y="166" font-size="10" text-anchor="middle" class="fig-purple">TPID 2B（固定 0x8100）</text>
    <text x="299" y="166" font-size="9.5" text-anchor="middle" class="fig-label">PCP 3b</text>
    <text x="375" y="166" font-size="9.5" text-anchor="middle" class="fig-label">DEI 1b</text>
    <text x="514" y="166" font-size="10" text-anchor="middle" class="fig-orange">VID 12b（标识 VLAN，核心字段）</text>
    <text x="32" y="226" font-size="11.5" class="fig-title">接口处理规则与广播域隔离（ARP 广播仅限同 VID 转发）</text>
    <text x="75" y="262" font-size="9.5" text-anchor="middle" class="fig-label">主机 A</text>
    <text x="75" y="276" font-size="8.5" text-anchor="middle" class="fig-muted">只认 V2 帧</text>
    <text x="165" y="253" font-size="8.5" text-anchor="middle" class="fig-green">Access 进打标</text>
    <text x="262" y="260" font-size="10" text-anchor="middle" class="fig-purple">交换机 SW1</text>
    <text x="262" y="276" font-size="8.5" text-anchor="middle" class="fig-muted">查 VID 转发</text>
    <text x="369" y="253" font-size="8.5" text-anchor="middle" class="fig-blue">Trunk 带标签干道</text>
    <text x="476" y="260" font-size="10" text-anchor="middle" class="fig-purple">交换机 SW2</text>
    <text x="476" y="276" font-size="8.5" text-anchor="middle" class="fig-muted">隔离广播域</text>
    <text x="566" y="253" font-size="8.5" text-anchor="middle" class="fig-green">Access 出剥标</text>
    <text x="649" y="262" font-size="9.5" text-anchor="middle" class="fig-label">同VLAN主机</text>
    <text x="649" y="276" font-size="8.5" text-anchor="middle" class="fig-muted">收到 V2 帧</text>
    <text x="360" y="303" font-size="9.5" text-anchor="middle" class="fig-muted">Access 口连主机（进打 PVID、出剥标签，线上恒为 V2 帧）；Trunk 口连交换机（承载多 VLAN 802.1Q 帧）</text>
  </svg>
  <figcaption>图 8-6：IEEE 802.1Q 帧 4 字节标签结构（12b VID）及交换机 Access / Trunk 接口打标剥标与广播域隔离</figcaption>
</figure>

<strong>两种接口类型（按对帧的处理方式与连接对象区分）：`access` 接口一般进出都是以太网 V2 帧；`trunk` 接口可能是 V2 帧也可能是 802.1Q 帧。判断的关键就是：何时插入 4 字节标签、何时剥离 4 字节标签</strong>。

### 8.3 软件定义网络 SDN

<strong>基本概念</strong>：SDN 是一种<strong>新型网络体系结构</strong>，不是一个具体的计算机网络实体；它把网络的<strong>控制层面与数据层面分离</strong>，让控制层面<strong>用软件</strong>来控制数据层面中的许多设备。

对照传统网络：路由器同时承担控制（路由选择）与数据（分组转发）两部分工作。SDN 把二者拆开——<strong>SDN 控制器</strong>负责路由选择并把算出的转发表下发给设备，设备只按转发表转发。因此传统路由器在 SDN 中被称为<strong>数据层面的物理设备</strong>，最常见的是 <strong>OpenFlow 交换机</strong>。

<figure class="cpd-figure not-content" id="cnq-w4-fig-13">
  <svg viewBox="0 0 720 318" role="img" aria-label="软件定义网络 SDN 三层分离架构、南北向接口及 OpenFlow 流表广义转发结构">
    <defs>
      <marker id="cnq-w4-arr-13" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="16" width="420" height="286" rx="6" class="fig-box-subtle"/>
    <rect x="448" y="16" width="256" height="286" rx="6" class="fig-box-subtle"/>
    <line x1="226" y1="84" x2="226" y2="122" class="fig-line-dashed" stroke="var(--sl-color-purple)" marker-start="url(#cnq-w4-arr-13)" marker-end="url(#cnq-w4-arr-13)"/>
    <line x1="166" y1="172" x2="92" y2="224" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#cnq-w4-arr-13)"/>
    <line x1="226" y1="172" x2="226" y2="224" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#cnq-w4-arr-13)"/>
    <line x1="286" y1="172" x2="360" y2="224" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#cnq-w4-arr-13)"/>
    <rect x="56" y="44" width="340" height="38" rx="4" class="fig-box-purple"/>
    <rect x="56" y="124" width="340" height="48" rx="4" class="fig-box-blue"/>
    <rect x="34" y="226" width="116" height="46" rx="4" class="fig-box-green"/>
    <rect x="168" y="226" width="116" height="46" rx="4" class="fig-box-green"/>
    <rect x="302" y="226" width="116" height="46" rx="4" class="fig-box-green"/>
    <rect x="148" y="94" width="156" height="18" rx="3" class="fig-box"/>
    <rect x="128" y="190" width="196" height="18" rx="3" class="fig-box"/>
    <rect x="460" y="48" width="232" height="72" rx="4" class="fig-box"/>
    <rect x="460" y="132" width="232" height="156" rx="4" class="fig-box-blue"/>
    <rect x="470" y="168" width="68" height="36" rx="3" class="fig-box-orange"/>
    <rect x="542" y="168" width="60" height="36" rx="3" class="fig-box"/>
    <rect x="606" y="168" width="76" height="36" rx="3" class="fig-box-green"/>
    <text x="226" y="34" font-size="12" text-anchor="middle" class="fig-title">SDN 控制层面与数据层面分离架构</text>
    <text x="226" y="67" font-size="10.5" text-anchor="middle" class="fig-purple">网络应用层（路由策略 · 流量工程 · 安全控制）</text>
    <text x="226" y="107" font-size="9.5" text-anchor="middle" class="fig-purple">北向接口（Northbound API）</text>
    <text x="226" y="145" font-size="10.5" text-anchor="middle" class="fig-blue">SDN 控制器（控制层面：软件集中计算路由）</text>
    <text x="226" y="162" font-size="9" text-anchor="middle" class="fig-muted">维护全网拓扑视图，生成流表并下发</text>
    <text x="226" y="203" font-size="9.5" text-anchor="middle" class="fig-orange">南向接口（OpenFlow 协议 · 下发流表）</text>
    <text x="92" y="246" font-size="10" text-anchor="middle" class="fig-green">OpenFlow 交换机</text>
    <text x="92" y="262" font-size="9" text-anchor="middle" class="fig-muted">流表 · 数据层面</text>
    <text x="226" y="246" font-size="10" text-anchor="middle" class="fig-green">OpenFlow 交换机</text>
    <text x="226" y="262" font-size="9" text-anchor="middle" class="fig-muted">流表 · 数据层面</text>
    <text x="360" y="246" font-size="10" text-anchor="middle" class="fig-green">OpenFlow 交换机</text>
    <text x="360" y="262" font-size="9" text-anchor="middle" class="fig-muted">流表 · 数据层面</text>
    <text x="226" y="292" font-size="9.5" text-anchor="middle" class="fig-muted">2022 真题：控制器向交换机下发流表经由「南向接口」</text>
    <text x="576" y="34" font-size="12" text-anchor="middle" class="fig-title">广义转发与流表结构</text>
    <text x="470" y="68" font-size="10" class="fig-label">传统路由器（耦合）：</text>
    <text x="470" y="86" font-size="9.5" class="fig-muted">每台独立跑 OSPF/RIP 算路由表，</text>
    <text x="470" y="102" font-size="9.5" class="fig-muted">仅依据「目的 IP 地址」查表转发</text>
    <text x="470" y="154" font-size="10" class="fig-blue">OpenFlow 流表项（广义转发表）：</text>
    <text x="504" y="183" font-size="9.5" text-anchor="middle" class="fig-orange">首部匹配</text>
    <text x="504" y="197" font-size="8.5" text-anchor="middle" class="fig-muted">Match</text>
    <text x="572" y="183" font-size="9.5" text-anchor="middle" class="fig-label">计数器</text>
    <text x="572" y="197" font-size="8.5" text-anchor="middle" class="fig-muted">Stats</text>
    <text x="644" y="183" font-size="9.5" text-anchor="middle" class="fig-green">动作集</text>
    <text x="644" y="197" font-size="8.5" text-anchor="middle" class="fig-muted">Actions</text>
    <text x="470" y="224" font-size="9.5" class="fig-label">• 匹配跨多层：MAC / VID / IP / 端口</text>
    <text x="470" y="244" font-size="9.5" class="fig-label">• 动作多样：转发、丢弃、泛洪、改写</text>
    <text x="470" y="264" font-size="9" class="fig-muted">• 一台 OpenFlow 交换机可扮演路由器、</text>
    <text x="470" y="278" font-size="9" class="fig-muted">交换机、防火墙或负载均衡器</text>
  </svg>
  <figcaption>图 8-7：SDN 三层分离架构（北向 API / 控制器 / 南向 OpenFlow 接口）与流表广义转发三元组</figcaption>
</figure>

<strong>OpenFlow 协议</strong>是控制层面与数据层面之间的<strong>通信接口</strong>，使控制器能直接访问、控制 OpenFlow 交换机。OpenFlow 交换机中的<strong>流表</strong>可看作<strong>广义的转发表</strong>——与传统路由器只按分组目的地址转发不同，广义转发的<strong>匹配条件</strong>还可以包含数据链路层、传输层的字段。

<strong>四个关键特征</strong>：① 基于流的转发；② 数据层面与控制层面分离；③ 控制层面功能移到设备外部（软件实现）；④ 网络可编程。

### 8.4 IPv6

2023 年题 40 首次考察（也是至今唯一一次），恰好体现大纲要求。

<strong>与 IPv4 相比的主要变化</strong>：更大的地址空间（128 位）、扩展的地址层次结构、灵活的首部格式、改进的选项、允许协议继续扩充、支持即插即用（自动配置）、支持资源的预分配、首部改为 8 字节对齐。

<figure class="cpd-figure not-content" id="cnq-w4-fig-14">
  <svg viewBox="0 0 720 332" role="img" aria-label="IPv6 固定 40 字节基本首部格式、扩展首部载荷封装及与 IPv4 首部字段变化对比">
    <defs>
      <marker id="cnq-w4-arr-14" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="16" width="464" height="300" rx="6" class="fig-box-subtle"/>
    <rect x="492" y="16" width="212" height="300" rx="6" class="fig-box-subtle"/>
    <line x1="430" y1="52" x2="440" y2="52" class="fig-line"/>
    <line x1="440" y1="52" x2="440" y2="236" class="fig-line"/>
    <line x1="430" y1="236" x2="440" y2="236" class="fig-line"/>
    <line x1="292" y1="98" x2="292" y2="246" class="fig-line-dashed" stroke="var(--sl-color-green)" marker-end="url(#cnq-w4-arr-14)"/>
    <rect x="72" y="52" width="44" height="24" class="fig-box"/>
    <rect x="116" y="52" width="88" height="24" class="fig-box"/>
    <rect x="204" y="52" width="220" height="24" class="fig-box-purple"/>
    <rect x="72" y="76" width="176" height="24" class="fig-box-orange"/>
    <rect x="248" y="76" width="88" height="24" class="fig-box-green"/>
    <rect x="336" y="76" width="88" height="24" class="fig-box"/>
    <rect x="72" y="100" width="352" height="68" class="fig-box-blue"/>
    <rect x="72" y="168" width="352" height="68" class="fig-box-blue"/>
    <rect x="72" y="248" width="352" height="36" class="fig-box-dashed"/>
    <rect x="504" y="50" width="188" height="122" rx="4" class="fig-box-red"/>
    <rect x="504" y="182" width="188" height="122" rx="4" class="fig-box-green"/>
    <text x="248" y="34" font-size="12" text-anchor="middle" class="fig-title">IPv6 基本首部格式（固定 40B = 10 行 × 4B）</text>
    <text x="72" y="47" font-size="9" class="fig-muted">0</text>
    <text x="116" y="47" font-size="9" text-anchor="middle" class="fig-muted">4</text>
    <text x="204" y="47" font-size="9" text-anchor="middle" class="fig-muted">12</text>
    <text x="248" y="47" font-size="9" text-anchor="middle" class="fig-muted">16</text>
    <text x="336" y="47" font-size="9" text-anchor="middle" class="fig-muted">24</text>
    <text x="424" y="47" font-size="9" text-anchor="end" class="fig-muted">31b</text>
    <text x="94" y="68" font-size="9" text-anchor="middle" class="fig-label">版本4b</text>
    <text x="160" y="68" font-size="9.5" text-anchor="middle" class="fig-label">通信量类 8b</text>
    <text x="314" y="68" font-size="10" text-anchor="middle" class="fig-purple">流标号 20b（资源预分配）</text>
    <text x="160" y="92" font-size="9.5" text-anchor="middle" class="fig-orange">有效载荷长度 16b</text>
    <text x="292" y="92" font-size="9.5" text-anchor="middle" class="fig-green">下一个首部 8b</text>
    <text x="380" y="92" font-size="9.5" text-anchor="middle" class="fig-label">跳数限制 8b</text>
    <text x="248" y="132" font-size="10.5" text-anchor="middle" class="fig-blue">源地址（128 位 = 16 字节，4 行）</text>
    <text x="248" y="150" font-size="9" text-anchor="middle" class="fig-muted">例：2001:db8::1</text>
    <text x="248" y="200" font-size="10.5" text-anchor="middle" class="fig-blue">目的地址（128 位 = 16 字节，4 行）</text>
    <text x="248" y="218" font-size="9" text-anchor="middle" class="fig-muted">单播 / 多播 / 任播（无广播术语）</text>
    <text x="446" y="142" font-size="10" class="fig-label">40B</text>
    <text x="446" y="156" font-size="9" class="fig-muted">固定</text>
    <text x="248" y="264" font-size="9.5" text-anchor="middle" class="fig-green">扩展首部链 + 传输层报文段（统算作数据载荷！）</text>
    <text x="248" y="278" font-size="8.5" text-anchor="middle" class="fig-muted">不计入 40B 基本首部，而计入「有效载荷长度」</text>
    <text x="248" y="304" font-size="9.5" text-anchor="middle" class="fig-muted">对比：IPv4 基本 20B + 选项最大 40B（4B 对齐）；IPv6 改为 8B 对齐</text>
    <text x="598" y="34" font-size="12" text-anchor="middle" class="fig-title">相比 IPv4 首部删改</text>
    <text x="514" y="68" font-size="10" class="fig-red">删去字段（路由器极速转发）：</text>
    <text x="514" y="88" font-size="9.5" class="fig-label">• 首部长度（已固定 40B）</text>
    <text x="514" y="108" font-size="9.5" class="fig-label">• 首部检验和（免逐跳重算！）</text>
    <text x="514" y="128" font-size="9.5" class="fig-label">• 标识/标志/片偏移（途经路由</text>
    <text x="514" y="144" font-size="9" class="fig-muted">器不再分片，仅源主机可分片）</text>
    <text x="514" y="200" font-size="10" class="fig-green">改名与合并机制：</text>
    <text x="514" y="220" font-size="9.5" class="fig-label">• 生存时间 TTL → 跳数限制</text>
    <text x="514" y="240" font-size="9.5" class="fig-label">• 协议字段 → 下一个首部</text>
    <text x="514" y="260" font-size="9.5" class="fig-label">• ICMPv6 合并 ARP 与 IGMP</text>
    <text x="514" y="280" font-size="9" class="fig-muted">• 即插即用（无状态自动配置）</text>
  </svg>
  <figcaption>图 8-8：IPv6 固定 40 字节基本首部格式、扩展首部链载荷归属及相比 IPv4 的删改考点</figcaption>
</figure>

<strong>首部</strong>：IPv6 分组首部长度<strong>固定 40 字节</strong>，还可有多个<strong>扩展首部</strong>，但扩展首部算作分组的<strong>数据载荷</strong>。对照 IPv4：首部固定 20 字节 + 可选部分最大 40 字节，故最大 60 字节；TCP 报文段首部也是“固定 20 + 选项最大 40”。

<strong>地址表示</strong>：

- <strong>冒号十六进制记法</strong>：128 位分成 8 组，每组 4 位十六进制，用冒号分隔。
- <strong>左侧零省略</strong>：每组中开头的 0 可省略。
- <strong>连续零压缩</strong>：一串连续的 0 组可用 `::` 代替。

> <strong>注意</strong>：一个 IPv6 地址中<strong>双冒号最多只能出现一次</strong>，否则无法确定各段零的个数，产生歧义。

冒号十六进制还可与<strong>点分十进制后缀</strong>结合（IPv4 向 IPv6 过渡阶段有用），CIDR 的斜线表示法在 IPv6 中仍然可用。

<strong>地址分类</strong>：全球单播地址用得最多，采用<strong>三级结构</strong>（目的为使路由器更快查找路由）；另有链路本地地址、唯一本地地址等。IPv6 分组的<strong>目的地址有单播、多播、任播三种类型</strong>：

- IPv6 <strong>没有“广播”这个术语</strong>，把广播看作多播的一个特例；
- <strong>任播</strong>是 IPv6 新增的类型：终点是一组计算机，但分组只交付给其中一个，通常是按路由算法得出的<strong>距离最近</strong>的一个。

<strong>IPv4 → IPv6 过渡</strong>：<strong>双协议栈</strong>（主机同时运行两套协议）与<strong>隧道技术</strong>（把 IPv6 分组封装进 IPv4 数据报穿越 IPv4 区域）。

<strong>ICMPv6：IPv6 同样不保证可靠交付，也需要向源主机反馈差错，版本为 ICMPv6。它比 ICMPv4 复杂得多，合并了原 ARP 与 IGMP 的功能</strong>，因此与 IPv6 配套的网际层协议只有 ICMPv6 这一个。

> <strong>注意</strong>：<strong>IPv6 取消了首部校验和</strong>，收到的分组不再做首部校验，以提高路由效率、避免逐跳校验。

<figure class="cpd-figure not-content" id="cnq-w4-fig-15">
  <svg viewBox="0 0 720 318" role="img" aria-label="IPv6 冒号十六进制零压缩规则与双协议栈及隧道过渡技术">
    <defs>
      <marker id="cnq-w4-arr-15" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="16" width="688" height="142" rx="6" class="fig-box-subtle"/>
    <rect x="16" y="168" width="688" height="134" rx="6" class="fig-box-subtle"/>
    <line x1="348" y1="95" x2="368" y2="95" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#cnq-w4-arr-15)"/>
    <line x1="118" y1="234" x2="162" y2="234" class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.8" marker-end="url(#cnq-w4-arr-15)"/>
    <line x1="274" y1="234" x2="444" y2="234" class="fig-line-dashed" stroke="var(--sl-color-orange)" stroke-width="2" marker-end="url(#cnq-w4-arr-15)"/>
    <line x1="558" y1="234" x2="600" y2="234" class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.8" marker-end="url(#cnq-w4-arr-15)"/>
    <rect x="32" y="46" width="656" height="26" rx="3" class="fig-box"/>
    <rect x="32" y="82" width="316" height="26" rx="3" class="fig-box-blue"/>
    <rect x="370" y="82" width="318" height="26" rx="3" class="fig-box-green"/>
    <rect x="32" y="118" width="656" height="28" rx="3" class="fig-box-red"/>
    <rect x="32" y="214" width="86" height="40" rx="4" class="fig-box-blue"/>
    <rect x="164" y="210" width="110" height="48" rx="4" class="fig-box-purple"/>
    <rect x="292" y="206" width="136" height="22" rx="3" class="fig-box-orange"/>
    <rect x="292" y="240" width="136" height="20" rx="3" class="fig-box"/>
    <rect x="446" y="210" width="112" height="48" rx="4" class="fig-box-purple"/>
    <rect x="602" y="214" width="86" height="40" rx="4" class="fig-box-blue"/>
    <text x="360" y="35" font-size="12" text-anchor="middle" class="fig-title">IPv6 冒号十六进制记法（8 组 × 16b = 128b）与压缩陷阱</text>
    <text x="360" y="63" font-size="10" text-anchor="middle" class="fig-label">原始地址：FF01 : 0000 : 0000 : 0000 : 0000 : 0123 : 0000 : 4567</text>
    <text x="190" y="99" font-size="9.5" text-anchor="middle" class="fig-blue">① 省略前导零：FF01:0:0:0:0:123:0:4567</text>
    <text x="529" y="99" font-size="9.5" text-anchor="middle" class="fig-green">② 单次 :: 压缩：FF01::123:0:4567（占 4 组 0）</text>
    <text x="360" y="136" font-size="9.5" text-anchor="middle" class="fig-red">✕ 歧义禁用：FF01::123::4567（出现两次 :: 时无法唯一反推两处各省略了几组 0！）</text>
    <text x="360" y="190" font-size="12" text-anchor="middle" class="fig-title">IPv4 → IPv6 过渡：双协议栈边界路由器 + IPv6-over-IPv4 隧道</text>
    <text x="75" y="232" font-size="9.5" text-anchor="middle" class="fig-blue">IPv6 网络 A</text>
    <text x="75" y="246" font-size="8.5" text-anchor="middle" class="fig-muted">原生 IPv6 包</text>
    <text x="219" y="230" font-size="9.5" text-anchor="middle" class="fig-purple">双栈路由器 R1</text>
    <text x="219" y="246" font-size="8.5" text-anchor="middle" class="fig-muted">封装外层 IPv4 头</text>
    <text x="360" y="221" font-size="9" text-anchor="middle" class="fig-orange">[IPv4 首部 | IPv6 分组]</text>
    <text x="360" y="254" font-size="9" text-anchor="middle" class="fig-muted">穿越中间 IPv4 海洋</text>
    <text x="502" y="230" font-size="9.5" text-anchor="middle" class="fig-purple">双栈路由器 R2</text>
    <text x="502" y="246" font-size="8.5" text-anchor="middle" class="fig-muted">剥离外层还原 IPv6</text>
    <text x="645" y="232" font-size="9.5" text-anchor="middle" class="fig-blue">IPv6 网络 B</text>
    <text x="645" y="246" font-size="8.5" text-anchor="middle" class="fig-muted">交付目的主机</text>
    <text x="360" y="286" font-size="9.5" text-anchor="middle" class="fig-muted">区别：双协议栈不丢字段；纯协议转换（NAT-PT）会损失 IPv6 部分首部特性，隧道封装则完整保留原 IPv6 分组</text>
  </svg>
  <figcaption>图 8-9：IPv6 冒号十六进制压缩规则（双冒号唯一性限制）及双协议栈与隧道过渡机制</figcaption>
</figure>

### 8.5 PPP 协议、IP 组播与移动 IP

<strong>PPP 协议</strong>（大纲只给了名字，未规定具体考察点，故按四部分准备）由三部分组成：

1. 一个把 IP 数据报<strong>封装到串行链路</strong>的方法（封装成帧、帧定界、透明传输、差错检测）；
2. <strong>链路控制协议 LCP</strong>（LCP）——建立、配置、测试数据链路连接；
3. <strong>网络控制协议 NCP</strong>——支持不同的网络层协议。

<strong>两种应用：用户拨号接入 ISP；作为广域网链路连接路由器。透明传输的两种方法（字节填充、零比特填充）已在第三章强化复习，若今后考 PPP，考透明传输方法的可能性最大。状态转换</strong>：链路静止 → 链路建立 → 鉴别 → 网络层协议 → 链路打开 → 链路终止（时间紧张可放过）。

> <strong>注意</strong>：到目前为止 408 尚未正式考察 PPP——三次出现是作为干扰项，或把 HDLC 换成 PPP 的变体题。反向考法（给组帧后的比特串写原比特串）也要会。

<strong>IP 组播</strong>：

- <strong>组播地址</strong>：D 类地址，<strong>224.0.0.0 – 239.255.255.255</strong>。
- 需要的两种协议：<strong>IGMP（网际组管理协议）——让路由器知道本局域网上是否有主机参加/退出了某组播组；组播路由选择协议</strong>——在路由器之间建立组播转发树。

<figure class="cpd-figure not-content" id="cnq-w4-fig-16">
  <svg viewBox="0 0 720 322" role="img" aria-label="PPP 帧透明传输字节填充与零比特填充规则及 IP 组播 IGMP 与转发树机制">
    <defs>
      <marker id="cnq-w4-arr-16" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="16" width="688" height="148" rx="6" class="fig-box-subtle"/>
    <rect x="16" y="174" width="688" height="132" rx="6" class="fig-box-subtle"/>
    <line x1="94" y1="240" x2="148" y2="240" class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.8" marker-end="url(#cnq-w4-arr-16)"/>
    <line x1="216" y1="230" x2="278" y2="216" class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.8" marker-end="url(#cnq-w4-arr-16)"/>
    <line x1="216" y1="250" x2="278" y2="264" class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.8" marker-end="url(#cnq-w4-arr-16)"/>
    <line x1="358" y1="216" x2="432" y2="216" class="fig-line" stroke="var(--sl-color-orange)" stroke-width="1.8" marker-start="url(#cnq-w4-arr-16)" marker-end="url(#cnq-w4-arr-16)"/>
    <rect x="32" y="44" width="58" height="26" class="fig-box-orange"/>
    <rect x="90" y="44" width="52" height="26" class="fig-box"/>
    <rect x="142" y="44" width="52" height="26" class="fig-box"/>
    <rect x="194" y="44" width="76" height="26" class="fig-box-purple"/>
    <rect x="270" y="44" width="292" height="26" class="fig-box-blue"/>
    <rect x="562" y="44" width="68" height="26" class="fig-box"/>
    <rect x="630" y="44" width="58" height="26" class="fig-box-orange"/>
    <rect x="32" y="80" width="320" height="72" rx="4" class="fig-box-blue"/>
    <rect x="368" y="80" width="320" height="72" rx="4" class="fig-box-purple"/>
    <rect x="32" y="222" width="62" height="36" rx="4" class="fig-box"/>
    <rect x="150" y="222" width="66" height="36" rx="4" class="fig-box-green"/>
    <rect x="280" y="200" width="78" height="32" rx="4" class="fig-box-green"/>
    <rect x="280" y="250" width="78" height="32" rx="4" class="fig-box-green"/>
    <rect x="428" y="200" width="102" height="34" rx="4" class="fig-box-orange"/>
    <rect x="540" y="198" width="150" height="94" rx="4" class="fig-box"/>
    <text x="360" y="34" font-size="12" text-anchor="middle" class="fig-title">PPP 帧结构（定界符 0x7E）与异步/同步透明传输规则</text>
    <text x="61" y="61" font-size="9.5" text-anchor="middle" class="fig-orange">F 7E</text>
    <text x="116" y="61" font-size="9.5" text-anchor="middle" class="fig-muted">A FF</text>
    <text x="168" y="61" font-size="9.5" text-anchor="middle" class="fig-muted">C 03</text>
    <text x="232" y="61" font-size="9.5" text-anchor="middle" class="fig-purple">协议 2B</text>
    <text x="416" y="61" font-size="9.5" text-anchor="middle" class="fig-blue">信息部分（IP 数据报 / LCP / NCP，≤1500B）</text>
    <text x="596" y="61" font-size="9.5" text-anchor="middle" class="fig-label">FCS 2B</text>
    <text x="659" y="61" font-size="9.5" text-anchor="middle" class="fig-orange">F 7E</text>
    <text x="44" y="98" font-size="10" class="fig-blue">异步链路 → 字节填充（转义符 0x7D）：</text>
    <text x="44" y="116" font-size="9.5" class="fig-label">• 数据中出现 7E → 替换为 7D 5E</text>
    <text x="44" y="134" font-size="9.5" class="fig-label">• 数据中出现 7D → 替换为 7D 5D</text>
    <text x="380" y="98" font-size="10" class="fig-purple">同步链路（SONET/SDH）→ 零比特填充：</text>
    <text x="380" y="116" font-size="9.5" class="fig-label">• 发送端：连遇 5 个 1 立即插入 1 个 0</text>
    <text x="380" y="134" font-size="9.5" class="fig-muted">• 接收端：连见 5 个 1 删除紧随的 1 个 0（防 01111110）</text>
    <text x="360" y="192" font-size="12" text-anchor="middle" class="fig-title">IP 组播两阶段协议配合（组播路由选择协议 + 局域网 IGMP）</text>
    <text x="63" y="238" font-size="9.5" text-anchor="middle" class="fig-label">组播源</text>
    <text x="63" y="251" font-size="8.5" text-anchor="middle" class="fig-muted">发1份</text>
    <text x="183" y="238" font-size="9.5" text-anchor="middle" class="fig-green">R1 分叉</text>
    <text x="183" y="251" font-size="8.5" text-anchor="middle" class="fig-muted">按树复制</text>
    <text x="319" y="220" font-size="9.5" text-anchor="middle" class="fig-green">末跳 R2</text>
    <text x="319" y="270" font-size="9.5" text-anchor="middle" class="fig-green">末跳 R3</text>
    <text x="395" y="210" font-size="9" text-anchor="middle" class="fig-orange">IGMP</text>
    <text x="479" y="215" font-size="9.5" text-anchor="middle" class="fig-orange">局域网成员组</text>
    <text x="479" y="228" font-size="8.5" text-anchor="middle" class="fig-muted">加入/退出报告</text>
    <text x="236" y="296" font-size="9" text-anchor="middle" class="fig-muted">路由器间：组播路由选择协议建立组播转发树</text>
    <text x="548" y="218" font-size="9.5" class="fig-label">IP 组播核心参数：</text>
    <text x="548" y="238" font-size="9" class="fig-green">• D 类：224.0.0.0~239.255…</text>
    <text x="548" y="258" font-size="9" class="fig-orange">• IGMP 封装在 IP 数据报内</text>
    <text x="548" y="278" font-size="9" class="fig-muted">• 尽最大努力交付（基于UDP）</text>
  </svg>
  <figcaption>图 8-10：PPP 帧格式与两类透明传输规则及 IP 组播（组播路由转发树 + IGMP 成员管理）</figcaption>
</figure>

<strong>移动 IP</strong>：

<figure class="cpd-figure not-content" id="cnq-w4-fig-17">
  <svg viewBox="0 0 720 336" role="img" aria-label="移动 IP 通信过程：归属代理与外地代理之间的 IP 隧道再封装及移动主机直接回传">
    <defs>
      <marker id="cnq-w4-arr-17" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="16" width="688" height="222" rx="6" class="fig-box-subtle"/>
    <rect x="16" y="248" width="688" height="72" rx="6" class="fig-box-subtle"/>
    <rect x="28" y="48" width="184" height="176" rx="5" class="fig-box-dashed"/>
    <rect x="508" y="48" width="184" height="176" rx="5" class="fig-box-dashed"/>
    <line x1="272" y1="168" x2="156" y2="108" class="fig-line" stroke="var(--sl-color-purple)" stroke-width="1.8" marker-end="url(#cnq-w4-arr-17)"/>
    <line x1="198" y1="84" x2="520" y2="84" class="fig-line-dashed" stroke="var(--sl-color-blue)" stroke-width="2.2" marker-end="url(#cnq-w4-arr-17)"/>
    <line x1="600" y1="108" x2="600" y2="158" class="fig-line" stroke="var(--sl-color-orange)" stroke-width="1.8" marker-end="url(#cnq-w4-arr-17)"/>
    <line x1="522" y1="190" x2="450" y2="190" class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.8" marker-end="url(#cnq-w4-arr-17)"/>
    <rect x="42" y="66" width="156" height="40" rx="4" class="fig-box-blue"/>
    <rect x="42" y="164" width="156" height="44" rx="4" class="fig-box-dashed"/>
    <rect x="272" y="156" width="176" height="54" rx="4" class="fig-box"/>
    <rect x="522" y="66" width="156" height="42" rx="4" class="fig-box-orange"/>
    <rect x="522" y="160" width="156" height="48" rx="4" class="fig-box-green"/>
    <rect x="236" y="62" width="248" height="18" rx="3" class="fig-box"/>
    <rect x="176" y="126" width="108" height="18" rx="3" class="fig-box"/>
    <rect x="536" y="124" width="128" height="18" rx="3" class="fig-box"/>
    <rect x="454" y="166" width="64" height="18" rx="3" class="fig-box"/>
    <rect x="28" y="260" width="412" height="48" rx="4" class="fig-box-blue"/>
    <rect x="448" y="260" width="244" height="48" rx="4" class="fig-box-green"/>
    <text x="120" y="40" font-size="11" text-anchor="middle" class="fig-title">归属网络（Home Network）</text>
    <text x="360" y="40" font-size="11.5" text-anchor="middle" class="fig-title">移动 IP 三角路由与 IP 隧道通信机制</text>
    <text x="600" y="40" font-size="11" text-anchor="middle" class="fig-title">外地网络（Foreign Network）</text>
    <text x="120" y="84" font-size="10" text-anchor="middle" class="fig-blue">归属代理 HA</text>
    <text x="120" y="98" font-size="8.5" text-anchor="middle" class="fig-muted">代收发往永久地址的包</text>
    <text x="120" y="182" font-size="9.5" text-anchor="middle" class="fig-muted">移动主机原位置（已离开）</text>
    <text x="120" y="198" font-size="9" text-anchor="middle" class="fig-muted">永久地址：131.8.6.7</text>
    <text x="360" y="178" font-size="10" text-anchor="middle" class="fig-label">固定通信对端 CH（因特网）</text>
    <text x="360" y="196" font-size="9" text-anchor="middle" class="fig-muted">IP：202.4.9.5（仅知永久地址）</text>
    <text x="600" y="84" font-size="10" text-anchor="middle" class="fig-orange">外地代理 FA</text>
    <text x="600" y="99" font-size="8.5" text-anchor="middle" class="fig-muted">转交地址 COA：172.16.1.1</text>
    <text x="600" y="180" font-size="10" text-anchor="middle" class="fig-green">漫游移动主机 MH</text>
    <text x="600" y="196" font-size="9" text-anchor="middle" class="fig-muted">仍保持永久地址 131.8.6.7</text>
    <text x="230" y="139" font-size="9" text-anchor="middle" class="fig-purple">① 目的=永久地址</text>
    <text x="360" y="75" font-size="9.5" text-anchor="middle" class="fig-blue">② IP 隧道：再封装外层 IP 首部发往转交地址</text>
    <text x="600" y="137" font-size="9" text-anchor="middle" class="fig-orange">③ 解封装交付内层包</text>
    <text x="486" y="179" font-size="9" text-anchor="middle" class="fig-green">④ 直回</text>
    <text x="42" y="279" font-size="9.5" class="fig-blue">正向隧道包：[外层：源=归属代理，目的=转交地址] + [内层：源=对端，目的=永久地址]</text>
    <text x="42" y="297" font-size="9" class="fig-muted">归属代理截获 ① 后封装发送，外地代理剥去外层首部，把原数据报交给移动主机</text>
    <text x="458" y="279" font-size="9.5" class="fig-green">反向直发包：源=永久地址，目的=通信对端</text>
    <text x="458" y="297" font-size="9" class="fig-muted">无需走隧道，直接经外地代理路由送达对端</text>
  </svg>
  <figcaption>图 8-11：移动 IP 双向通信流程（正向经归属代理 IP 隧道再封装至转交地址、反向以永久地址直接回传）</figcaption>
</figure>

<strong>要素</strong>：移动主机、归属网络、归属网络中的<strong>归属代理</strong>、移动主机漫游到的外地网络、外地网络中的<strong>外地代理</strong>、<strong>转交地址</strong>、归属代理与外地代理之间的 <strong>IP 隧道</strong>。需要在归属代理、外地代理与移动主机上分别做相应配置后才能实现移动 IP 通信。

<strong>通信过程（两个方向）</strong>：

1. <strong>固定主机 → 移动主机：固定主机发出的 IP 数据报，源 = 固定主机 IP，目的 = 移动主机的永久地址</strong>。数据报被路由到归属代理，归属代理把它<strong>封装到一个新的 IP 数据报</strong>中发送，外层<strong>源 = 归属代理 IP，目的 = 转交地址</strong>；经隧道到达外地代理后解封装，再交付给移动主机。
2. <strong>移动主机 → 固定主机</strong>：移动主机发出的数据报<strong>源 = 永久地址，目的 = 固定主机 IP</strong>；经外地代理转发出外地网络后送达固定主机。这样对端始终只看到移动主机的<strong>永久地址</strong>，移动性对上层透明。
