通俗理解IP路由

圖片描述

平常工做環境中,咱們習慣於使用 ping去測試網絡的連通性。若是 ping不通,咱們每每會去懷疑是否是路由配置錯了。

路由是什麼

咱們知道,IP地址是網絡世界裏的門牌號。你能夠經過IP地址訪問遠在天邊的網站,那麼數據是如何到達網站的呢?靠的就是路徑上每一個節點的路由。
路由,簡單的說就是指導IP報文該去哪的指示牌。ubuntu

圖片描述

通常說來,主機會在如下兩個時機進行路由查詢服務器

  1. 收到報文時,查詢路由決定是上送本機(LOCAL IN),或者從哪一個出接口轉發(FORWARD)
  2. 本機發送報文時,查詢報文出接口
注意,轉發須要開啓 net/ipv4/ip_forward

路由表長什麼樣

以一個典型的主機爲例,tristan有一個外部網卡eth0和一個內部還回網卡lo網絡

[root@tristan]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:80:C8:F8:4A:51  
          inet addr:192.168.99.35  Bcast:192.168.99.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:27849718 errors:1 dropped:0 overruns:0 frame:0
          TX packets:29968044 errors:5 dropped:0 overruns:2 carrier:3
          collisions:0 txqueuelen:100 
          RX bytes:943447653 (899.7 Mb)  TX bytes:2599122310 (2478.7 Mb)
          Interrupt:9 Base address:0x1000 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:7028982 errors:0 dropped:0 overruns:0 frame:0
          TX packets:7028982 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:1206918001 (1151.0 Mb)  TX bytes:1206918001 (1151.0 Mb)

[root@tristan]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.99.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
127.0.0.0       0.0.0.0         255.0.0.0       U     0      0        0 lo
0.0.0.0         192.168.99.254  0.0.0.0         UG    0      0        0 eth0

經過route -n咱們能夠看到主機上簡要的路由表信息(固然經過ip route也能夠),那麼上面的路由信息中的每一表項表明什麼意思呢?負載均衡

  • 若是報文的目的IP地址在192.168.99.0/24這個網段,那麼它應該從eth0進行轉發。
  • 若是報文的目的IP地址在127.0.0.1/8這個網段,那麼它應該從lo進行轉發。
  • 其餘狀況下(0.0.0.0/0),報文從eth0轉發,下一跳IP地址是192.168.99.254

第1,2條應該都很好理解,比較難以理解的是第3條,爲何它的Gateway不是全0.0.0.0, 這須要從網絡拓撲提及oop

Gateway

topo

上圖左邊是一幅典型的網絡拓撲。主機之間經過交換機組成一個小型局域網,再經過一個路由器鏈接到更大的網絡。 測試

圖中的路由器對於局域網內的主機來講,便可稱爲Gateway,通常翻譯爲網關。他的地位如同一個國家的海關,出了海關IP報文就在另外一個網絡之中了。網站

回到前面的路由表的第3條,當主機發現報文的目的IP地址不知足其餘表項時,就表示這個報文須要送到局域網外部,因此它會將報文發送或者轉發給網關,也就是192.168.99.254。這種狀況下,spa

192.168.99.254被稱爲默認網關,這條表項也稱爲默認路由。與這個名字對應的,第1,2條路由表項也被稱爲網段路由翻譯

局域網內部通訊

當咱們爲網卡配置IP地址時或者由DHCP服務器分配IP地址,Linux內核會根據IP地址和掩碼自動生成對應的網段路由。這至關於告訴系統,這個網段的主機在同一個局域網內部code

舉個例子,假設咱們在tristan主機去ping局域網內的susan主機。那麼這個ICMP request報文向下面這樣組裝就OK了。

圖片描述

若是 tristan還不知道susan主機上eth0MAC地址,那麼它首先要發送ARP廣播報文去得到。

局域網外通訊

與局域網外的主機通訊就須要經過(via)網關了

類似的例子,假設咱們在tristan主機去ping局域網外的paul主機的eth0, 在查詢路由後,內核選擇默認網關,那麼組裝的ICMP request的報文頭將是下面這樣:

圖片描述

設置靜態路由

網段路由

有時,系統生成的默認路由並不能知足咱們的要求,咱們須要手動設置路由

[root@tristan]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.99.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
127.0.0.0       0.0.0.0         255.0.0.0       U     0      0        0 lo
0.0.0.0         192.168.99.254  0.0.0.0         UG    0      0        0 eth0

好比剛纔的拓撲,tristan主機要與jerry主機所在的網絡通訊,顯然根據現有的路由表,顯然是不行的。所以咱們須要在tristan主機上配置額外的路由表項:

[root@tristan]# route add -net 192.168.98.0 netmask 255.255.255.0 gw 192.168.99.1
[root@tristan]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.99.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.98.0    192.168.99.1    255.255.255.0   UG    0      0        0 eth0
127.0.0.0       0.0.0.0         255.0.0.0       U     0      0        0 lo
0.0.0.0         192.168.99.254  0.0.0.0         UG    0      0        0 eth0

新添加的路由表項表示本機與192.168.98.0/24範圍內的主機主機通訊,須要途徑網關192.168.99.1

使用ip route命令也能夠達到相同的效果, 經過via設置下一跳網關,本質上沒有區別

[root@tristan]# ip route add 192.168.98.0/24 via 192.168.99.1

咱們甚至能夠添加或者刪除默認路由

[root@tristan]# route del default gw 192.168.99.254
[root@tristan]# route add default gw 192.168.99.1

主機路由

除了設置整個網段,咱們還能夠僅爲某個目的IP設置路由,即主機路由,主機路由實際上這是掩碼爲255.255.255.255的網段路由的特殊形式

[root@tristan]# route add -host 192.168.98.42 gw 192.168.99.1

或者

[root@tristan]# route add -net 192.168.98.42 netmask 255.255.255.255 gw 192.168.99.1

均可以生成一條特定主機的路由,Flags字段中的H表示Host

[root@tristan]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.99.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.98.42   192.168.99.1    255.255.255.255 UGH   0      0        0 eth0
127.0.0.0       0.0.0.0         255.0.0.0       U     0      0        0 lo
0.0.0.0         192.168.99.254  0.0.0.0         UG    0      0        0 eth0

等價路由

等價路由(ECMP)是指存在多條鏈路到達同一目的,鏈路之間能夠起到負載均衡和鏈路備份的目的,好比下面的拓撲:

topo2

在主機melo設置的路由以下

[root@melo]# route show scope global
default via 203.0.113.5 dev eth0
192.0.2.0/25
        nexthop via 203.0.113.7  dev eth1 weight 1
        nexthop via 203.0.113.9  dev eth2 weight 1

表示若是報文目的地址是192.0.2.0/25,那麼它將等可能地選擇out1或者out2做爲出接口。

策略路由

多張路由表

上面提到的全部路由選擇都是根據目的IP找到對應的路由表項進行的。除此以外,Linux還提供了一種更高級靈活的方式能夠完成更加複雜的路由選擇策略,這就是策略路由。它使得用戶能夠基於源IP等信息進行路由配置。

策略路由的基本原理是系統會根據IP報文的特徵使用不一樣的路由表。它須要在內核編譯時勾選CONFIG_IP_MULTIPLE_TABLES

Linux內核最多支持256張路由表,其中有4張是系統默認保留的,用戶能夠新建252張表

/etc/iproute2/rt_tables 能夠看到內核保留的4張表,其中有用的是local表和main

root@ubuntu-1:/home/user1# cat /etc/iproute2/rt_tables 
#
# reserved values
#
255    local
254    main
253    default
0    unspec

local表中存儲的是內核自動生成本機路由廣播路由

當咱們使用 route命令或者不指定路由表時的 ip route時,操做的表是 main表,因此咱們須要使用額外的參數才能操做和查看 local
[root@real-server]# ip address show dev eth1
6: eth1: <BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast qlen 100
   link/ether 00:80:c8:e8:1e:fc brd ff:ff:ff:ff:ff:ff
   inet 10.10.20.89/24 brd 10.10.20.255 scope global eth1
[root@real-server]# ip route show dev eth1
10.10.20.0/24  proto kernel  scope link  src 10.10.20.89
[root@real-server]# ip route show dev eth1 table local
broadcast 10.10.20.0  proto kernel  scope link  src 10.10.20.89 
broadcast 10.10.20.255  proto kernel  scope link  src 10.10.20.89
local 10.10.20.89  proto kernel  scope host  src 10.10.20.89
[root@real-server]# ip address add 192.168.254.254/24 brd+ dev eth1
[root@real-server]# ip address show dev eth1
6: eth1: <BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast qlen 100
   link/ether 00:80:c8:e8:1e:fc brd ff:ff:ff:ff:ff:ff
   inet 10.10.20.89/24 brd 10.10.20.255 scope global eth1
   inet 192.168.254.254/24 brd 192.168.254.255 scope global eth1
[root@real-server]# ip route show dev eth1
10.10.20.0/24  proto kernel  scope link  src 10.10.20.89 
192.168.254.0/24  proto kernel  scope link  src 192.168.254.254
[root@real-server]# ip route show dev eth1 table local
broadcast 10.10.20.0  proto kernel  scope link  src 10.10.20.89 
broadcast 192.168.254.0  proto kernel  scope link  src 192.168.254.254          
broadcast 10.10.20.255  proto kernel  scope link  src 10.10.20.89               
local 192.168.254.254  proto kernel  scope host  src 192.168.254.254            
local 10.10.20.89  proto kernel  scope host  src 10.10.20.89                    
broadcast 192.168.254.255  proto kernel  scope link  src 192.168.254.254

通常狀況下,咱們不該該也不必操做local表,雖然內核並無禁止用戶對local表的操做

ip route add table local local 10.10.20.64 dev eth0 proto kernel scope host src 10.10.20.67
ip route add table local local 192.168.43.12 dev eth4 proto kernel scope host src 192.168.43.14

路由表的選擇

使用ip rule命令,咱們能夠看到系統選擇路由表的規則

[root@real-server]# ip rule show
0:    from all lookup local 
32766:    from all lookup main 
32767:    from all lookup default

第一項表示默認查找的優先級,數字越小的優先級越高,因此係統默認會從local表進行查找。規則中的from all表示全部源地址。

咱們能夠ip rule命令添加自定義的規則,

ip rule add from 192.168.101.0/24 table 10  
ip rule add to 192.168.102.0/24 table 20 
ip rule add to 192.168.103.0/24 table 30 prio 123

上面的規則表示來自192.168.101.0/24網段的報文都查詢table 10,目的地址爲192.168.102.0/24網段的報文查詢table 20,目的地址爲192.168.103.0/24網段的報文查詢table 30且優先級爲123

[root@real-server]# ip rule show
0:    from all lookup local 
123:    from all to 192.168.103.0/24 lookup 30 
32764:    from all to 192.168.102.0/24 lookup 20 
32765:    from 192.168.101.0/24 lookup 10 
32766:    from all lookup main 
32767:    from all lookup default

以後,用戶就能夠經過ip route命令向具體的table添加路由表項了。

(完)

相關文章
相關標籤/搜索