# ch10: Dynamic Routing Protocols

## MindMap

![Dynamic Routing Protocols](https://tva1.sinaimg.cn/large/007S8ZIlly1ggdk8o1560j30vz0u0n7k.jpg)

## Dynamic Routing

IP层来查找路由，这叫做路由机制（routing mechanism）。

routing daemon来动态修改路由表，这叫做路由策略（routing policy）。

路由策略决定添加什么、修改什么以及删除什么到路由表中。

自治系统（AS，Autonomous System），一共有两种路由协议：

* 内部网关协议（IGP，Interior Gateway Protocol），有RIP、OSPF；
* 外部网关协议（EGP，Exterior Gateway Protocol），有EGP、BGP。

## Unix Routing Daemons

有两个daemon：

* *routed*：只支持RIP；
* *gated*：支持IGPs和EGPs。

## RIP: Routing Information Protocol

应用最广泛的路由协议。

### 格式

消息格式：

![](https://tva1.sinaimg.cn/large/007S8ZIlly1ggdkkoxj80j30o00aemy8.jpg)

使用的是UDP。

RIP报文的格式如下：

![](https://tva1.sinaimg.cn/large/007S8ZIlly1ggdkljdgzkj31000pidif.jpg)

command有六种，只要关注下面两种就可以了：

* 1：请求；
* 2：应答；

版本有1和2。

最多可以存25条路由记录。

### 操作

RIP协议使用的UDP端口是520。

#### 初始化

daemon启动的时候，发送一个请求广播，请求旁边路由器的**所有记录**。

这时，command=1，address family=0， metric=16。

#### 接收到请求

如果收到的请求metric=16，那么就返回所有的路由记录。

否则，就要一条条记录检查，如果该记录满足请求，设置metric为自己的值，然后返回；如果不满足，metric设置为16。

#### 接收到响应

响应生效，更新路由表。

#### 定期路由更新

每隔30秒，全部或部分路由记录会发送到相邻的路由器中。

#### 触发更新

当一个路由的metric发生变化，就对它进行更新。只需要发送有变化的记录。

每一条记录都有一个有效时间，如果三分钟没有更新，那么metric就设置成16，然后60秒之后就会被删除。

### Metric

metric是hop count，以跳来度量的。

下面是一个例子：

![](https://tva1.sinaimg.cn/large/007S8ZIlly1ggdl36r9zcj30py0dy0u8.jpg)

metric最大是15，16表示无法路由到该IP地址。

### 缺点

* RIP没有子网地址的概念；
* 路由器出错后需要很长的时间恢复；
* metric忽略了很多其它的因素，同时15太低了。

## RIP V2

版本2在1之上增加了许多信息，格式如下：

![](https://tva1.sinaimg.cn/large/007S8ZIlly1ggdlb4wve9j31160p4q5m.jpg)

## OSFP: Open Shortest Path First

OSFP是RIP的更新替换选择，OSFP不使用距离，而使用连接的状态。

* 每一个路由器和相邻的路由器交换的信息是连接的状态；
* 直接使用IP，而不是UDP或TCP；
* 收敛更快；
* 支持子网；
* 采用多播。

## BGP: Border Gateway Protocol

* BGP是一个EGP的路由协议，用来决定AS间的路由。
* 源IP地址或目的IP地址是本AS内的主机，这个流量就叫做**local traffic**；否则就是**transit traffic**。
* AS间的路由需要考虑各种**路由策略**。
* BGP使用TCP来交换信息。
* BGP也使用距离，但度量是AS。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://valineliu.gitbook.io/deuterium-wiki/reading/cs-jing-dian-shu-ji/tcpv1/ch10-dynamic-routing-protocols.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
