MHuiG

「看庭前花开花落,望天上云卷云舒」

IP 路由基础

# IP 路由基础

## 路由

路由：指导报文转发的路径信息，通过路由可以确认 IP 报文的转发路径.

路由表：路由信息库 (RIB)

每台路由器都维护着一张全局路由表，另外路由器所运行的每种路由协议也维护着自己的路由表.

路由器可以通过多种途径获取路由信息，获取的路由信息会先存储在协议自己的路由表中，然后路由器根据协议的优先级等信息来进行路由优选，并将优选路由加载到全局路由表中.

转发表：转发信息库 (FIB)

数据包根据路由转发需要下一跳地址和出接口.

如果路由表中有匹配的路由条目，则根据条目中的出接口或下一跳信息进行报文转发.

如果路由表内没有该路由信息，则丢弃数据包.

## 路由表字段内容

    [R1]display ip routing-table
    Route Flags: R - relay, D - download to fib
    ------------------------------------------------------------------------------
    Routing Tables: Public
             Destinations : 4        Routes : 4

    Destination/Mask    Proto   Pre  Cost      Flags NextHop         Interface

          127.0.0.0/8   Direct  0    0           D   127.0.0.1       InLoopBack0
          127.0.0.1/32  Direct  0    0           D   127.0.0.1       InLoopBack0
    127.255.255.255/32  Direct  0    0           D   127.0.0.1       InLoopBack0
    255.255.255.255/32  Direct  0    0           D   127.0.0.1       InLoopBack0

    [R1]

Destination/Mask: 目的和掩码.

Protocol: 该路由通过哪种方式学习到的.

Preference: 优先级，当路由器通过不同方式学习到相同的路由时，会比较不同协议之间的优先级，优先级数值越小，优先级越高.

Cost: 开销，到达目的网段所花费的开销，路由信息通过不同的方式学习到的路由，对于开销的计算方式也不同，不同协议之间的开销没有可比性，数据越小越优先.

Flags: 标记，标明当前路由的状态.

- D: 已经将该路由条目下载到转发表中.
- R: 迭代出的路由.

NextHop: 下一跳，标明数据从设备出发后，应交给下一台哪个设备.

Interface: 标明数据从该设备的哪个接口发送出去.

## 路由表的构建方式 (学习到路由的方式)

1. 直连路由 (direct): 路由器直接相连的网络。优先级 `0`.

- 1. 路由器的接口正确的配置了 IP 地址.
- 2. 接口的物理层状态为 UP.

2. 静态路由 (static): 由管理员手工添加的路由信息。优先级 `60`.

- 1. 下一跳必须可达 (或出接口状态必须 UP).
- 2. 没有优先级更高的路由.

3. 动态路由：路由器通过动态路由协议自动计算得到的路由。可以根据网络变化自动的进行路由表的改变.

动态路由协议: `OSPF`, `RIP`, `IS-IS`, `BGP`.

`OSPF` 优先级: `10` 或 `150`, 协议号 `89`.

`RIP` 优先级: `100`, 端口号 `520`.

## 路由选路原则

1. 如果一条路由是当前去往目的地的唯一路径信息，则直接优选.

1.5. 最长掩码匹配原则.

2. 比较优先级，不同的协议拥有不同的优先级，优先级数据越小越优.

3. 比较路由开销，越小越优.

4. 如果以上信息无法确定最优路径，则会进行负载分担.

最长掩码匹配原则：如果路由表中有两条目的网段相同的路由条目，掩码越长 (所匹配的越多), 表示网段越精准，所以掩码越长越优.

目的地: 192.168.1.1

路径:

192.168.1.0/24

192.168.1.0/16

192.168.1.0/29 (v)

20.1.0.0/16 OSPF 10 200 (v)

20.1.0.0/14 RIP 100 3

## 静态路由

静态路由网络管理员手动配置，配置方便，对系统要求低，适用于拓扑结构简单并且稳定的小型网络.

缺点是不能自动适应网络拓扑的变化需要人工干预.

静态路必须由网络管理员手动添加，适用于小型网络或网络结构相对稳定的网络。当网络拓扑发生改变时需要管理员手动进行调整.

R1

    <Huawei>sy
    Enter system view, return user view with Ctrl+Z.
    [Huawei] sy R1
    [R1]int g0/0/0
    [R1-GigabitEthernet0/0/0]ip add 192.168.1.254 24
    [R1-GigabitEthernet0/0/0]int g0/0/1
    [R1-GigabitEthernet0/0/1]ip add 12.1.1.1 24
    [R1-GigabitEthernet0/0/1]q
    [R1]display ip interface brief
    *down: administratively down
    ^down: standby
    (l): loopback
    (s): spoofing
    The number of interface that is UP in Physical is 3
    The number of interface that is DOWN in Physical is 1
    The number of interface that is UP in Protocol is 3
    The number of interface that is DOWN in Protocol is 1

    Interface                         IP Address/Mask      Physical   Protocol
    GigabitEthernet0/0/0              192.168.1.254/24     up         up
    GigabitEthernet0/0/1              12.1.1.1/24          up         up
    GigabitEthernet0/0/2              unassigned           down       down
    NULL0                             unassigned           up         up(s)
    [R1]display ip routing-table
    Route Flags: R - relay, D - download to fib
    ------------------------------------------------------------------------------
    Routing Tables: Public
             Destinations : 10       Routes : 10

    Destination/Mask    Proto   Pre  Cost      Flags NextHop         Interface

           12.1.1.0/24  Direct  0    0           D   12.1.1.1        GigabitEthernet0/0/1
           12.1.1.1/32  Direct  0    0           D   127.0.0.1       GigabitEthernet0/0/1
         12.1.1.255/32  Direct  0    0           D   127.0.0.1       GigabitEthernet0/0/1
          127.0.0.0/8   Direct  0    0           D   127.0.0.1       InLoopBack0
          127.0.0.1/32  Direct  0    0           D   127.0.0.1       InLoopBack0
    127.255.255.255/32  Direct  0    0           D   127.0.0.1       InLoopBack0
        192.168.1.0/24  Direct  0    0           D   192.168.1.254   GigabitEthernet0/0/0
      192.168.1.254/32  Direct  0    0           D   127.0.0.1       GigabitEthernet0/0/0
      192.168.1.255/32  Direct  0    0           D   127.0.0.1       GigabitEthernet0/0/0
    255.255.255.255/32  Direct  0    0           D   127.0.0.1       InLoopBack0
    [R1]ip route-static ?
      IP_ADDR<X.X.X.X>    Destination IP address
      default-preference  Preference-value for IPv4 static-routes
      selection-rule      Selection rule
      vpn-instance        VPN-Instance route information
    [R1]ip route-static 192.168.2.0 ?
      INTEGER<0-32>     Length of IP address mask
      IP_ADDR<X.X.X.X>  IP address mask
    [R1]ip route-static 192.168.2.0 24 ?
      IP_ADDR<X.X.X.X>  Gateway address
      Cellular          Cellular interface
      GigabitEthernet   GigabitEthernet interface
      NULL              NULL interface
      vpn-instance      Destination VPN-Instance for Gateway address
    [R1]ip route-static 192.168.2.0 24 12.1.1.2
    [R1]dis th
    [V200R003C00]
    #
     sysname R1
    #
     snmp-agent local-engineid 800007DB03000000000000
     snmp-agent
    #
     clock timezone China-Standard-Time minus 08:00:00
    #
    portal local-server load portalpage.zip
    #
     drop illegal-mac alarm
    #
     set cpu-usage threshold 80 restore 75
    #
    ip route-static 192.168.2.0 255.255.255.0 12.1.1.2
    #
    return
    [R1]

R2

    <Huawei>sy
    Enter system view, return user view with Ctrl+Z.
    [Huawei]sy R2
    [R2]int g0/0/1
    [R2-GigabitEthernet0/0/1]ip add 192.168.2.254 24
    [R2-GigabitEthernet0/0/1]int g0/0/0
    [R2-GigabitEthernet0/0/0]ip add 12.1.1.2 24
    [R2-GigabitEthernet0/0/0]dis ip int br
    *down: administratively down
    ^down: standby
    (l): loopback
    (s): spoofing
    The number of interface that is UP in Physical is 3
    The number of interface that is DOWN in Physical is 1
    The number of interface that is UP in Protocol is 3
    The number of interface that is DOWN in Protocol is 1

    Interface                         IP Address/Mask      Physical   Protocol
    GigabitEthernet0/0/0              12.1.1.2/24          up         up
    GigabitEthernet0/0/1              192.168.2.254/24     up         up
    GigabitEthernet0/0/2              unassigned           down       down
    NULL0                             unassigned           up         up(s)
    [R2]ip route-static 192.168.1.0 24 12.1.1.1
    [R2]dis th
    [V200R003C00]
    #
     sysname R2
    #
     snmp-agent local-engineid 800007DB03000000000000
     snmp-agent
    #
     clock timezone China-Standard-Time minus 08:00:00
    #
    portal local-server load portalpage.zip
    #
     drop illegal-mac alarm
    #
     set cpu-usage threshold 80 restore 75
    #
    ip route-static 192.168.1.0 255.255.255.0 12.1.1.1
    #
    return
    [R2]

## 等价路由

具有相同的网络和掩码，优先级以及开销值，路由器认为多条路由完全等价，则会将多条路由加入到路由表中，实现负载均衡.

## 浮动路由

在配置路由时，使其中一条路由优先级高于其他路由，从而实现路由的备份，主路由失效的情况下，路由器会将备份路由添加到路由表中.

    [R6]ip route-static 192.168.3.0 24 41.1.1.1 preference 70

R5

    <R5>sy
    Enter system view, return user view with Ctrl+Z.
    [R5]
    [R5]
    [R5]int g4/0/0
    [R5-GigabitEthernet4/0/0]ip add 41.1.1.1 24
    Dec 21 2023 09:33:38-08:00 R5 %%01IFNET/4/LINK_STATE(l)[0]:The line protocol IP
    on the interface GigabitEthernet4/0/0 has entered the UP state.
    [R5-GigabitEthernet4/0/0]q
    [R5]ip rou
    [R5]ip route-s
    [R5]ip route-static 192.168.4.0 24 41.1.1.2
    [R5]dis ip rou
    [R5]dis ip routing-table
    Route Flags: R - relay, D - download to fib
    ------------------------------------------------------------------------------
    Routing Tables: Public
             Destinations : 20       Routes : 21

    Destination/Mask    Proto   Pre  Cost      Flags NextHop         Interface

           12.1.1.0/24  Static  60   0          RD   13.1.1.1        GigabitEthernet0/0/0
           13.1.1.0/24  Direct  0    0           D   13.1.1.2        GigabitEthernet0/0/0
           13.1.1.2/32  Direct  0    0           D   127.0.0.1       GigabitEthernet0/0/0
         13.1.1.255/32  Direct  0    0           D   127.0.0.1       GigabitEthernet0/0/0
           14.1.1.0/24  Direct  0    0           D   14.1.1.1        GigabitEthernet0/0/1
           14.1.1.1/32  Direct  0    0           D   127.0.0.1       GigabitEthernet0/0/1
         14.1.1.255/32  Direct  0    0           D   127.0.0.1       GigabitEthernet0/0/1
           41.1.1.0/24  Direct  0    0           D   41.1.1.1        GigabitEthernet4/0/0
           41.1.1.1/32  Direct  0    0           D   127.0.0.1       GigabitEthernet4/0/0
         41.1.1.255/32  Direct  0    0           D   127.0.0.1       GigabitEthernet4/0/0
          127.0.0.0/8   Direct  0    0           D   127.0.0.1       InLoopBack0
          127.0.0.1/32  Direct  0    0           D   127.0.0.1       InLoopBack0
    127.255.255.255/32  Direct  0    0           D   127.0.0.1       InLoopBack0
        192.168.1.0/24  Static  60   0          RD   13.1.1.1        GigabitEthernet0/0/0
        192.168.2.0/24  Static  60   0          RD   13.1.1.1        GigabitEthernet0/0/0
        192.168.3.0/24  Direct  0    0           D   192.168.3.254   GigabitEthernet0/0/2
      192.168.3.254/32  Direct  0    0           D   127.0.0.1       GigabitEthernet0/0/2
      192.168.3.255/32  Direct  0    0           D   127.0.0.1       GigabitEthernet0/0/2
        192.168.4.0/24  Static  60   0          RD   14.1.1.2        GigabitEthernet0/0/1
                        Static  60   0          RD   41.1.1.2        GigabitEthernet4/0/0
    255.255.255.255/32  Direct  0    0           D   127.0.0.1       InLoopBack0

    [R5]ip route-static 192.168.4.0 24 41.1.1.2 preference 40
    Info: Succeeded in modifying route.
    [R5]dis ip routing-table
    Route Flags: R - relay, D - download to fib
    ------------------------------------------------------------------------------
    Routing Tables: Public
             Destinations : 20       Routes : 20

    Destination/Mask    Proto   Pre  Cost      Flags NextHop         Interface

           12.1.1.0/24  Static  60   0          RD   13.1.1.1        GigabitEthernet0/0/0
           13.1.1.0/24  Direct  0    0           D   13.1.1.2        GigabitEthernet0/0/0
           13.1.1.2/32  Direct  0    0           D   127.0.0.1       GigabitEthernet0/0/0
         13.1.1.255/32  Direct  0    0           D   127.0.0.1       GigabitEthernet0/0/0
           14.1.1.0/24  Direct  0    0           D   14.1.1.1        GigabitEthernet0/0/1
           14.1.1.1/32  Direct  0    0           D   127.0.0.1       GigabitEthernet0/0/1
         14.1.1.255/32  Direct  0    0           D   127.0.0.1       GigabitEthernet0/0/1
           41.1.1.0/24  Direct  0    0           D   41.1.1.1        GigabitEthernet4/0/0
           41.1.1.1/32  Direct  0    0           D   127.0.0.1       GigabitEthernet4/0/0
         41.1.1.255/32  Direct  0    0           D   127.0.0.1       GigabitEthernet4/0/0
          127.0.0.0/8   Direct  0    0           D   127.0.0.1       InLoopBack0
          127.0.0.1/32  Direct  0    0           D   127.0.0.1       InLoopBack0
    127.255.255.255/32  Direct  0    0           D   127.0.0.1       InLoopBack0
        192.168.1.0/24  Static  60   0          RD   13.1.1.1        GigabitEthernet0/0/0
        192.168.2.0/24  Static  60   0          RD   13.1.1.1        GigabitEthernet0/0/0
        192.168.3.0/24  Direct  0    0           D   192.168.3.254   GigabitEthernet0/0/2
      192.168.3.254/32  Direct  0    0           D   127.0.0.1       GigabitEthernet0/0/2
      192.168.3.255/32  Direct  0    0           D   127.0.0.1       GigabitEthernet0/0/2
        192.168.4.0/24  Static  40   0          RD   41.1.1.2        GigabitEthernet4/0/0
    255.255.255.255/32  Direct  0    0           D   127.0.0.1       InLoopBack0

    [R5]

R6

    <R6>sy
    Enter system view, return user view with Ctrl+Z.
    [R6]int g4/0/0
    [R6-GigabitEthernet4/0/0]ip add 41.1.1.2 24
    Dec 21 2023 09:34:36-08:00 R6 %%01IFNET/4/LINK_STATE(l)[0]:The line protocol IP
    on the interface GigabitEthernet4/0/0 has entered the UP state.
    [R6-GigabitEthernet4/0/0]q
    [R6]ip route-static 192.168.3.0 24 41.1.1.1
    [R6]display ip routing-table
    Route Flags: R - relay, D - download to fib
    ------------------------------------------------------------------------------
    Routing Tables: Public
             Destinations : 18       Routes : 19

    Destination/Mask    Proto   Pre  Cost      Flags NextHop         Interface

           12.1.1.0/24  Static  60   0          RD   14.1.1.1        GigabitEthernet0/0/0
           13.1.1.0/24  Static  60   0          RD   14.1.1.1        GigabitEthernet0/0/0
           14.1.1.0/24  Direct  0    0           D   14.1.1.2        GigabitEthernet0/0/0
           14.1.1.2/32  Direct  0    0           D   127.0.0.1       GigabitEthernet0/0/0
         14.1.1.255/32  Direct  0    0           D   127.0.0.1       GigabitEthernet0/0/0
           41.1.1.0/24  Direct  0    0           D   41.1.1.2        GigabitEthernet4/0/0
           41.1.1.2/32  Direct  0    0           D   127.0.0.1       GigabitEthernet4/0/0
         41.1.1.255/32  Direct  0    0           D   127.0.0.1       GigabitEthernet4/0/0
          127.0.0.0/8   Direct  0    0           D   127.0.0.1       InLoopBack0
          127.0.0.1/32  Direct  0    0           D   127.0.0.1       InLoopBack0
    127.255.255.255/32  Direct  0    0           D   127.0.0.1       InLoopBack0
        192.168.1.0/24  Static  60   0          RD   14.1.1.1        GigabitEthernet0/0/0
        192.168.2.0/24  Static  60   0          RD   14.1.1.1        GigabitEthernet0/0/0
        192.168.3.0/24  Static  60   0          RD   14.1.1.1        GigabitEthernet0/0/0
                        Static  60   0          RD   41.1.1.1        GigabitEthernet4/0/0
        192.168.4.0/24  Direct  0    0           D   192.168.4.254   GigabitEthernet0/0/1
      192.168.4.254/32  Direct  0    0           D   127.0.0.1       GigabitEthernet0/0/1
      192.168.4.255/32  Direct  0    0           D   127.0.0.1       GigabitEthernet0/0/1
    255.255.255.255/32  Direct  0    0           D   127.0.0.1       InLoopBack0

    [R6]ip route-static 192.168.3.0 24 41.1.1.1 preference 70
    Info: Succeeded in modifying route.
    [R6]display ip routing-table
    Route Flags: R - relay, D - download to fib
    ------------------------------------------------------------------------------
    Routing Tables: Public
             Destinations : 18       Routes : 18

    Destination/Mask    Proto   Pre  Cost      Flags NextHop         Interface

           12.1.1.0/24  Static  60   0          RD   14.1.1.1        GigabitEthernet0/0/0
           13.1.1.0/24  Static  60   0          RD   14.1.1.1        GigabitEthernet0/0/0
           14.1.1.0/24  Direct  0    0           D   14.1.1.2        GigabitEthernet0/0/0
           14.1.1.2/32  Direct  0    0           D   127.0.0.1       GigabitEthernet0/0/0
         14.1.1.255/32  Direct  0    0           D   127.0.0.1       GigabitEthernet0/0/0
           41.1.1.0/24  Direct  0    0           D   41.1.1.2        GigabitEthernet4/0/0
           41.1.1.2/32  Direct  0    0           D   127.0.0.1       GigabitEthernet4/0/0
         41.1.1.255/32  Direct  0    0           D   127.0.0.1       GigabitEthernet4/0/0
          127.0.0.0/8   Direct  0    0           D   127.0.0.1       InLoopBack0
          127.0.0.1/32  Direct  0    0           D   127.0.0.1       InLoopBack0
    127.255.255.255/32  Direct  0    0           D   127.0.0.1       InLoopBack0
        192.168.1.0/24  Static  60   0          RD   14.1.1.1        GigabitEthernet0/0/0
        192.168.2.0/24  Static  60   0          RD   14.1.1.1        GigabitEthernet0/0/0
        192.168.3.0/24  Static  60   0          RD   14.1.1.1        GigabitEthernet0/0/0
        192.168.4.0/24  Direct  0    0           D   192.168.4.254   GigabitEthernet0/0/1
      192.168.4.254/32  Direct  0    0           D   127.0.0.1       GigabitEthernet0/0/1
      192.168.4.255/32  Direct  0    0           D   127.0.0.1       GigabitEthernet0/0/1
    255.255.255.255/32  Direct  0    0           D   127.0.0.1       InLoopBack0

    [R6]int g0/0/0
    [R6-GigabitEthernet0/0/0]shutdown
    Dec 21 2023 09:45:33-08:00 R6 %%01IFPDT/4/IF_STATE(l)[1]:Interface GigabitEthern
    et0/0/0 has turned into DOWN state.
    [R6-GigabitEthernet0/0/0]
    Dec 21 2023 09:45:33-08:00 R6 %%01IFNET/4/LINK_STATE(l)[2]:The line protocol IP
    on the interface GigabitEthernet0/0/0 has entered the DOWN state.
    [R6-GigabitEthernet0/0/0]disp ip routing-table
    Route Flags: R - relay, D - download to fib
    ------------------------------------------------------------------------------
    Routing Tables: Public
             Destinations : 11       Routes : 11

    Destination/Mask    Proto   Pre  Cost      Flags NextHop         Interface

           41.1.1.0/24  Direct  0    0           D   41.1.1.2        GigabitEthernet4/0/0
           41.1.1.2/32  Direct  0    0           D   127.0.0.1       GigabitEthernet4/0/0
         41.1.1.255/32  Direct  0    0           D   127.0.0.1       GigabitEthernet4/0/0
          127.0.0.0/8   Direct  0    0           D   127.0.0.1       InLoopBack0
          127.0.0.1/32  Direct  0    0           D   127.0.0.1       InLoopBack0
    127.255.255.255/32  Direct  0    0           D   127.0.0.1       InLoopBack0
        192.168.3.0/24  Static  70   0          RD   41.1.1.1        GigabitEthernet4/0/0
        192.168.4.0/24  Direct  0    0           D   192.168.4.254   GigabitEthernet0/0/1
      192.168.4.254/32  Direct  0    0           D   127.0.0.1       GigabitEthernet0/0/1
      192.168.4.255/32  Direct  0    0           D   127.0.0.1       GigabitEthernet0/0/1
    255.255.255.255/32  Direct  0    0           D   127.0.0.1       InLoopBack0

    [R6-GigabitEthernet0/0/0]undo shutdown
    [R6-GigabitEthernet0/0/0]
    Dec 21 2023 09:48:22-08:00 R6 %%01IFPDT/4/IF_STATE(l)[3]:Interface GigabitEthern
    et0/0/0 has turned into UP state.
    [R6-GigabitEthernet0/0/0]
    Dec 21 2023 09:48:22-08:00 R6 %%01IFNET/4/LINK_STATE(l)[4]:The line protocol IP
    on the interface GigabitEthernet0/0/0 has entered the UP state.
    [R6-GigabitEthernet0/0/0]

## 缺省路由

去往任意网段的路由.

    ip route-static 0.0.0.0 0 下一跳地址

VLAN原理与配置

VLAN 原理与配置 VLAN 原理与配置 广播域过大造成的弊端交换机所处的位置是一个广播域. 1. 广播域的泛洪会导致网络传输效率降低. 2. 广播域过大可能会造成安全隐患. 3....

远程登录

远程登录 远程登录 telnet 线路认证[R2]telnet server enable // 开启Telent服务[R2]user-interface vty 0 4 // 进入...

推荐阅读
实现VLAN间通信 DHCP VLAN原理与配置 数据转发过程 远程登录 OSPF基础

留言区

Notes
数据通信网络
Are You A Robot?