Tuesday, December 14, 2010

ip route

笔记
route是什么意思?
这个词的字面意思一直不知道是什么意思。 其实这不是一个汉语词汇。汉语词汇里只有一个“路由单” ,为一名词。 路由(route)有两种意思:
1. 选择某路线
2. 与"路由单"的意思一致: 指旅途所经历的地名单

选择路线的依据是"目的地".
In the common case, route selection is based completely on the destination address. Conventional (as opposed to policy-based) IP networking relies on only the destination address to select a route for a packet.

但是随着发展, 仅仅是destination方式的route方式不能满足要求:
With the prevalence of low cost bandwidth, easily configured VPN tunnels, and increasing reliance on networks, the technique of selecting a route based solely on the destination IP address range no longer suffices for all situations.

linux对应这种发展的具体落实:
Since kernel 2.2, linux has supported policy based routing through the use of multiple routing tables and the routing policy database (RPDB). Together, they allow a network administrator to configure a machine select different routing tables and routes based on a number of criteria.

意思大概是如下两件事物:
1. linux支持多routing tables.  routing policy database (RPDB)
2. 每张表有独立的规则. policy based routing

平时使用的路由都是由destination成唯一条件(比如使用route命令打印的結果). 那么policy based routing有什么重要呢?
In fact, advanced routing could more accurately be called policy-based networking.

下面的一段话, 描述了linux在路由数据包时, policy based routing使用的多种实现方法
Selectors available for use in policy-based routing are attributes of a packet passing through the linux routing code. The source address of a packet, the ToS flags, an fwmark (a mark carried through the kernel in the data structure representing the packet), and the interface name on which the packet was received are attributes which can be used as selectors. By selecting a routing table based on packet attributes, an administrator can have granular control over the network path of any packet.
selector确定使用那张routing table.

使用人类文字描述Linux选择线路不是很容易理解, 如下一段伪代码比较好:
if packet.routeCacheLookupKey in routeCache :
    route = routeCache[ packet.routeCacheLookupKey ]
else
    for rule in rpdb :
        if packet.rpdbLookupKey in rule : (rule为下表的RPDB对象)
            routeTable = rule[ lookupTable ] (routeTable为下表的route table对象)
            if packet.routeLookupKey in routeTable :
                route = route_table[ packet.routeLookup_key ]

把rpdb为routing table, 规则(rule)都在DB中, 每条rule有不同的属性(这里的属性包括上面提到的attributes).

伪代码中的LookupKey是代表下表中具体的一条属性. 所以, 其实上面的伪代码是很N多if语句的.

* 斜体字的属性是可选的. 如果存在就判断, 不存在不判断.

从上面的可以知道, route table起到:
1. 组织rule的作用
2. 同类的rule会拥有一组属性.

从上面的表中可以知道, 每个packet的destination和source是必定被用于路由, 但是不唯一确定条件.

linux system administrator查看上面的三种数据的方法:
1. route cache 表:  ip route show cache
2. 每张RPDB表: ip rule list table 表名
3. 列出全部route table:  ip rule show

1 comment:

  1. hi,jessinio.订阅了你的blog才发现,原来sa也可以这么专业。刚入行的鸟蛋(还不是菜鸟...)前来致敬。

    ReplyDelete

Note: Only a member of this blog may post a comment.