路由
引入
7:25
计算机网络
网络将你的字节传送到遍布全球、彼此互不隶属的机器上。前三个单元介绍帧的传输:分层、链路层与 IP,以及路由。接下来的两个单元介绍如何通过传输层和应用层协议,使交付可靠且有用。最后两个单元介绍如何保障网络安全并扩展网络规模。本课程中的每个首部都会逐字节解码,方便你核对。
英文讲解 · 可切换中英文字幕
本课内容
| English | 释义 | 拼音 |
|---|---|---|
| routing table/ˈraʊtɪŋ ˈteɪbl/ | 路由表 | lù yóu biǎo |
| longest prefix match/ˈlɒŋɡɪst ˈpriːfɪks mætʃ/ | 最长前缀匹配 | zuì zhǎng qián zhuì pǐ pèi |
| Prefix aggregation/ˈpriːfɪks ˌæɡrɪˈɡeɪʃn/ | 前缀聚合 | qián zhuì jù hé |
| Link-state/lɪŋk steɪt/ | 链路状态 | liàn lù zhuàng tài |
| Distance-vector/ˈdɪstəns ˈvektə/ | 距离向量 | jù lí xiàng liàng |
| link cost | 链路开销 | liàn lù kāi xiāo |
| BGP/ˌbiː dʒiː ˈpiː/ | 边界网关协议 | biān jiè wǎng guān xié yì |
| count to infinity/kaʊnt tʊ ɪnˈfɪnɪti/ | 无穷计数 | wú qióng jì shù |
| settled node | 已确定节点 | yǐ què dìng jié diǎn |
表与前缀
- 路由器的路由表将目标前缀映射到下一跳;最长前缀匹配规则选择最具体的路由,默认路由捕获其余所有情况。
- 表
10.0.0.0/8 → X,10.1.0.0/16 → Y,10.1.2.0/24 → Z,默认 → W:10.1.2.200发往 Z,10.9.9.9发往 X,192.168.1.1发往 W。
Destination 10.1.2.200 with /8, /16 and /24 entries present goes to —
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Longest prefix match: three match, the /24 is most specific.
Table 10.0.0.0/8 → X, 10.1.0.0/16 → Y, 10.1.2.0/24 → Z. Where does 10.1.3.7 go?
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
It misses the /24 but matches the /16, which beats the /8.
Dijkstra 算法数值演示
- 路径权重:A–B 4,A–C 2,B–C 1,B–D 5,C–D 8,C–E 10,D–E 2,E–F 3,D–F 6。从 A 出发的确定顺序为 C, B, D, E, F,距离分别为 2,3,8,10,13。
- F 的最佳路径是 A→C→B→D→E→F,距离为 $2+1+5+2+3 = 13$ — 经由 E($10 + 3$)优于 D 的直接连接 $8 + 6 = 14$。已确定的节点即最终节点:不再通过它们进行松弛操作。


On the course graph, node F settles at —
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
A C B D E F: 2 + 1 + 5 + 2 + 3 = 13. Going through E is cheaper than the direct link from D (14).
On the course graph, put the nodes in the order Dijkstra settles them from A.
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
Each round settles the closest unsettled node: B is reached through C ($2 + 1$), D through B ($3 + 5$), E through D ($8 + 2$) and F through E ($10 + 3$), which beats D's direct $8 + 6 = 14$.
为何采用分层结构
- 扁平表需为每台主机保留一项条目:$4 \times 10^9$ 行 IPv4 记录。前缀聚合宣布一个 /12 覆盖 $2^{20}$ 个地址,使表规模维持在约 $10^5$ 项左右。
- 聚合故意丢弃主机位 — 因为没人需要知道具体主机信息,所以表保持人类可读的大小。
A /12 announcement covers — addresses
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
32 - 12 = 20 host bits: 2^20 addresses aggregated away.
这是一张新卡片,翻译完成前会显示英文。
Share the map or share the distances
- Link-state 链路状态 routing (OSPF): every router floods its own links to all the others, so each one holds the whole map and runs Dijkstra itself.
- Distance-vector 距离向量 routing (RIP): a router tells only its neighbours its current distance to each destination, and takes the best "link cost + neighbour's distance". It is simpler and needs no map, but it learns bad news slowly (next steps).
Router X reaches Y at cost 3 and Z at cost 1. Y reports distance 4 to D; Z reports distance 8. What is X's distance to D?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
min(3 + 4, 1 + 8) = min(7, 9) = 7, via Y.
In link-state routing, each router holds the whole network map.
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Flooding gives every router every link; each runs Dijkstra.
这是一张新卡片,翻译完成前会显示英文。
Worked example: a distance-vector update
- Router X has neighbours Y (link cost 链路开销 1) and Z (cost 4). Y reports destination D at 5; Z reports D at 1.
- X takes the best total: $\min(1 + 5, 4 + 1) = \mathbf{5}$, via Z. This is the Bellman–Ford step, repeated whenever a neighbour's report changes.
Costs X–Y 1 and X–Z 4; Y reports D at 5, Z reports D at 1. X's distance to D?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
$\min(1 + 5, 4 + 1) = 5$, via Z.
这是一张新卡片,翻译完成前会显示英文。
Bad news travels slowly, and BGP
- Routers A–B–C sit in a line, each link cost 1, so B is 1 from C and A is 2 (via B). The B–C link fails. B now hears A say "C is 2" and believes it: B sets 3, via A.
- A hears 3 from B and sets 4; B then sets 5, and so on. They count to infinity 无穷计数, and RIP stops at 16, which means "unreachable".
- Split horizon, never telling a neighbour about a route learned from it, stops this two-router loop.
- Between organisations' networks (autonomous systems), BGP 边界网关协议 advertises whole paths of autonomous systems, not just distances. Seeing the full path lets a router reject loops, and it chooses routes by business policy, not only by shortest distance.
In the line A–B–C, after the B–C link fails, B can believe A's old report "C is 2" and set its own distance to 3, via A.
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
B cannot tell that A's route to C went through B itself. That is the start of count to infinity; split horizon stops it by not reporting a route back to the neighbour it came from.
Which protocol routes between autonomous systems on the internet?
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
BGP is the path-vector protocol between networks; OSPF runs inside one.
这是一张新卡片,翻译完成前会显示英文。
Marks that slip away
- First match instead of longest match.
10.1.2.200goes to the /24 route, even though the /8 also matches. - Updating a settled node 已确定节点. In Dijkstra a settled distance is final; only unsettled nodes change.
- Taking the fewest hops. F's best path A C B D E F has five hops at cost $13$; A C D F has three hops but costs $2 + 8 + 6 = 16$.
这是一张新卡片,翻译完成前会显示英文。
Recap
- Longest prefix match 最长前缀匹配 picks the most specific route; the default catches the rest.
- Dijkstra from A settles the nearest node first:
| Settled | C | B | D | E | F |
|---|---|---|---|---|---|
| From A | 2 | 3 | 8 | 10 | 13 |
- Aggregation keeps the global table near a million routes instead of $4 \times 10^9$.
出了什么问题?只有网站团队能看到这些信息。