Skip to content

Commit 8d874a1

Browse files
committed
routing: Add PBR
1 parent 3e410d8 commit 8d874a1

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

docs/dev/language/index.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ icon: material/xml
2727
!!! tip "IDE 推荐"
2828

2929
- [Visual Studio Code](https://code.visualstudio.com/): 其实 Visual Studio Code 是个代码编辑器,但是在众多插件的帮助下,可以有 IDE 一样的体验。Visual Studio Code 可以用于各种语言的开发,更是拥有非常出色的远程开发功能。
30-
- [Pycharm](https://www.jetbrains.com/pycharm/): Jetbrains 开发的 Python IDE,提供了开箱即用的 Python 开发体验,支持 Python 的各种框架
31-
- [Clion](https://www.jetbrains.com/clion/): Jetbrains 开发的 C++ IDE,使用 CMake 管理工程,拥有强大的静态检查功能,以及强大的代码重构功能
32-
- [QT Creator](https://www.qt.io/zh-cn/product/development-tools): 主要用于开发 Qt 应用的 IDE
30+
- [Pycharm](https://www.jetbrains.com/pycharm/): JetBrains 开发的 Python IDE,提供了开箱即用的 Python 开发体验,支持 Python 的各种框架
31+
- [Clion](https://www.jetbrains.com/clion/): JetBrains 开发的 C++ IDE,使用 CMake 管理工程,拥有强大的静态检查功能,以及强大的代码重构功能
32+
- [Qt Creator](https://www.qt.io/zh-cn/product/development-tools): 主要用于开发 Qt 应用的 IDE
3333
- [Cursor](https://www.cursor.com/): 深度集成了 AI 工具的代码编辑器,基于 Visual Studio Code 开发,兼容大部分 Visual Studio Code 的插件,Tab 键的体验谁用谁知道!

docs/ops/network/routing.md

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ throw
7272

7373
: 特殊的规则,表示跳出当前路由表,继续在后续的路由规则中查找匹配的规则(见下文「策略路由」部分)。
7474

75+
如果一个路由表中不包含 `default` 路由,则隐含一个 `throw default` 规则。
76+
7577
blackhole / unreachable / prohibit
7678

7779
: 拒绝路由,处理方式分别为静默丢弃、返回 ICMP unreachable 和 ICMP prohibited 响应。
@@ -91,6 +93,44 @@ broadcast 192.0.2.255 dev eth0 proto kernel scope link src 192.0.2.1
9193

9294
单一的路由表只能根据数据包的目的地址来决定去向,无法满足更复杂的路由需求。
9395
一个典型的场景是,同时接入多个网络的服务器在处理网络连接时,需要「源进源出」的路由方式才能确保与客户端正常通信。
94-
**策略路由**(Policy-based Routing,PBR)通过引入多个路由表和(不仅仅根据目的地址的)路由规则,实现了更灵活的路由控制。
96+
**策略路由**(Policy-Based Routing,PBR)通过引入多个路由表和(不仅仅根据目的地址的)路由规则,实现了更灵活的路由控制。
97+
98+
### 路由规则 {#routing-rules}
99+
100+
在 Linux 中,路由决策的过程分为两个阶段:
101+
102+
1. **路由规则匹配**:根据数据包的特征(如源地址、目的地址、TOS 等)在路由规则列表(Routing Policy DataBase,RPDB)中查找匹配的规则,确定使用哪个路由表;
103+
2. **路由表查找**:在选定的路由表中查找匹配的路由规则,决定数据包的去向。
104+
105+
特别地,如果路由规则的类型为 `throw`,则跳出当前路由表,继续考察后续的路由规则。
106+
107+
路由规则存储在内核中的 RPDB,可以通过 `ip rule` 命令查看当前的路由规则列表。
108+
每条路由规则都有一个优先级(priority),数值越小优先级越高,默认情况下,内核会维护三条基本的路由规则(IPv6 为两条):
109+
110+
```shell title="IPv4 默认路由规则"
111+
$ ip rule
112+
0: from all lookup local
113+
32766: from all lookup main
114+
32767: from all lookup default
115+
```
116+
117+
<!-- `shell` language is required for rendering annotation: https://squidfunk.github.io/mkdocs-material/reference/code-blocks/#fn:1 -->
118+
```shell title="IPv6 默认路由规则"
119+
$ ip -6 rule
120+
0: from all lookup local
121+
32766: from all lookup main # (1)!
122+
```
123+
124+
1. 注意到,IPv6 默认没有指向 `default` 路由表的规则
125+
126+
在内核中,每个路由表都有一个编号,默认的路由表编号如下:
127+
128+
| 路由表 | 编号 |
129+
| :-----: | :---: |
130+
| local | 255 |
131+
| main | 254 |
132+
| default | 253 |
133+
134+
路由表的编号仅用于区分,不影响路由表的功能和优先级(优先级由路由规则决定)。
95135

96136
### `ip rule` 命令 {#ip-rule}

0 commit comments

Comments
 (0)