BGP小实验——选路不困难,只要有方法

时间:2021-1-18 作者:admin

BGP小实验——选路不困难,只要有方法


前言

BGP小实验——选路不困难,只要有方法

一、需求

  • 全网通
  • R1到R4走R3
  • R4到R1走R3

二、命令

1、先配置基础命令,完成全网通

R1

<Huawei>u t m
Info: Current terminal monitor is off.
<Huawei>sys
Enter system view, return user view with Ctrl+Z.
[Huawei]int g0/0/0
[Huawei-GigabitEthernet0/0/0]ip add 12.0.0.1 24
[Huawei-GigabitEthernet0/0/0]int g0/0/1
[Huawei-GigabitEthernet0/0/1]ip add 13.0.0.1 24
[Huawei-GigabitEthernet0/0/1]int loop 0
[Huawei-LoopBack0]ip add 1.1.1.1 32
[Huawei-LoopBack0]q
[Huawei]ip route-static 2.2.2.2 32 12.0.0.2
[Huawei]ip route-static 3.3.3.3 32 13.0.0.3
[Huawei]bgp 100
[Huawei-bgp]router-id 1.1.1.1
[Huawei-bgp]peer 2.2.2.2 as-number 200
[Huawei-bgp]peer 3.3.3.3 as-number 200	
[Huawei-bgp]peer 2.2.2.2 connect-interface LoopBack 0
[Huawei-bgp]peer 3.3.3.3 connect-interface LoopBack 0
[Huawei-bgp]peer 2.2.2.2 ebgp-max-hop 2
[Huawei-bgp]peer 3.3.3.3 ebgp-max-hop 2
[Huawei-bgp]net 1.1.1.1 32
[Huawei-bgp]net 2.2.2.2 32
[Huawei-bgp]net 3.3.3.3 32
[Huawei-bgp]q
[Huawei]ping -a 1.1.1.1 5.5.5.5
  PING 5.5.5.5: 56  data bytes, press CTRL_C to break
    Reply from 5.5.5.5: bytes=56 Sequence=1 ttl=253 time=30 ms
    Reply from 5.5.5.5: bytes=56 Sequence=2 ttl=253 time=20 ms
    Reply from 5.5.5.5: bytes=56 Sequence=3 ttl=253 time=30 ms
    Reply from 5.5.5.5: bytes=56 Sequence=4 ttl=253 time=40 ms
    Reply from 5.5.5.5: bytes=56 Sequence=5 ttl=253 time=30 ms

  --- 5.5.5.5 ping statistics ---
    5 packet(s) transmitted
    5 packet(s) received
    0.00% packet loss
    round-trip min/avg/max = 20/30/40 ms

R2

<Huawei>u t m
Info: Current terminal monitor is off.
<Huawei>sys
Enter system view, return user view with Ctrl+Z.
[Huawei]int g0/0/0
[Huawei-GigabitEthernet0/0/0]ip add 12.0.0.2 24
[Huawei-GigabitEthernet0/0/0]int g0/0/1
[Huawei-GigabitEthernet0/0/1]ip add 24.0.0.2 24
[Huawei-GigabitEthernet0/0/1]int loop0
[Huawei-LoopBack0]ip add 2.2.2.2 32
[Huawei-LoopBack0]q
[Huawei]ip route-static 1.1.1.1 32 12.0.0.1
[Huawei]ospf
[Huawei-ospf-1]area 0
[Huawei-ospf-1-area-0.0.0.0]net 2.2.2.2 0.0.0.0 
[Huawei-ospf-1-area-0.0.0.0]net 24.0.0.0 0.0.0.255
[Huawei-ospf-1-area-0.0.0.0]q
[Huawei-ospf-1]q
[Huawei]bgp 200
[Huawei-bgp]router-id 2.2.2.2
[Huawei-bgp]peer 1.1.1.1 as-number 100
[Huawei-bgp]peer 4.4.4.4 as-number 200
[Huawei-bgp]peer 3.3.3.3 as-number 200
[Huawei-bgp]peer 1.1.1.1 connect-interface LoopBack 0
[Huawei-bgp]peer 3.3.3.3 connect-interface LoopBack 0
[Huawei-bgp]peer 4.4.4.4 connect-interface LoopBack 0
[Huawei-bgp]peer 1.1.1.1 ebgp-max-hop 2
[Huawei-bgp]peer 3.3.3.3 next-hop-local
[Huawei-bgp]peer 4.4.4.4 next-hop-local
[Huawei-bgp]net 1.1.1.1 32
[Huawei-bgp]net 2.2.2.2 32
[Huawei-bgp]net 3.3.3.3 32
[Huawei-bgp]net 4.4.4.4 32

R3

<Huawei>u t m
Info: Current terminal monitor is off.
<Huawei>sys
Enter system view, return user view with Ctrl+Z.
[Huawei]int g0/0/1
[Huawei-GigabitEthernet0/0/1]ip add 13.0.0.3 24
[Huawei-GigabitEthernet0/0/1]int g0/0/0
[Huawei-GigabitEthernet0/0/0]ip add 34.0.0.3 24
[Huawei-GigabitEthernet0/0/0]int loop0
[Huawei-LoopBack0]ip add 3.3.3.3 32
[Huawei-LoopBack0]q
[Huawei]ip route-static 1.1.1.1 32 13.0.0.1
[Huawei]ospf
[Huawei-ospf-1]area 0
[Huawei-ospf-1-area-0.0.0.0]net 3.3.3.3 0.0.0.0
[Huawei-ospf-1-area-0.0.0.0]net 34.0.0.0 0.0.0.255
[Huawei-ospf-1-area-0.0.0.0]q
[Huawei-ospf-1]q
[Huawei]bgp 200
[Huawei-bgp]router-id 3.3.3.3
[Huawei-bgp]peer 1.1.1.1 as-number 100
[Huawei-bgp]peer 4.4.4.4 as-number 200
[Huawei-bgp]peer 2.2.2.2 as-number 200
[Huawei-bgp]peer 1.1.1.1 connect-interface LoopBack 0
[Huawei-bgp]peer 4.4.4.4 connect-interface LoopBack 0
[Huawei-bgp]peer 2.2.2.2 connect-interface LoopBack 0
[Huawei-bgp]peer 1.1.1.1 ebgp-max-hop 2
[Huawei-bgp]peer 4.4.4.4 next-hop-local
[Huawei-bgp]peer 2.2.2.2 next-hop-local
[Huawei-bgp]net 1.1.1.1 32
[Huawei-bgp]net 2.2.2.2 32
[Huawei-bgp]net 3.3.3.3 32
[Huawei-bgp]net 4.4.4.4 32

R4

<Huawei>u t m
Info: Current terminal monitor is off.
<Huawei>sys
Enter system view, return user view with Ctrl+Z.
[Huawei]int g0/0/0
[Huawei-GigabitEthernet0/0/0]ip add 34.0.0.4 24
[Huawei-GigabitEthernet0/0/0]int g0/0/1
[Huawei-GigabitEthernet0/0/1]ip add 24.0.0.4 24
[Huawei-GigabitEthernet0/0/1]int g0/0/2
[Huawei-GigabitEthernet0/0/2]ip add 45.0.0.4 24
[Huawei-GigabitEthernet0/0/2]int loop0
[Huawei-LoopBack0]ip add 4.4.4.4 32
[Huawei-LoopBack0]q
[Huawei]ip route-static 5.5.5.5 32 45.0.0.5
[Huawei]ospf
[Huawei-ospf-1]area 0
[Huawei-ospf-1-area-0.0.0.0]net 4.4.4.4 0.0.0.0
[Huawei-ospf-1-area-0.0.0.0]net 34.0.0.0 0.0.0.255
[Huawei-ospf-1-area-0.0.0.0]net 24.0.0.0 0.0.0.255
[Huawei-ospf-1-area-0.0.0.0]q
[Huawei-ospf-1]q
[Huawei]bgp 200
[Huawei-bgp]router-id 4.4.4.4
[Huawei-bgp]peer 2.2.2.2 as-number 200
[Huawei-bgp]peer 3.3.3.3 as-number 200
[Huawei-bgp]peer 5.5.5.5 as-number 300
[Huawei-bgp]peer 2.2.2.2 connect-interface LoopBack 0
[Huawei-bgp]peer 3.3.3.3 connect-interface LoopBack 0
[Huawei-bgp]peer 5.5.5.5 connect-interface LoopBack 0
[Huawei-bgp]peer 2.2.2.2 next-hop-local 
[Huawei-bgp]peer 3.3.3.3 next-hop-local
[Huawei-bgp]peer 5.5.5.5 ebgp-max-hop 2
[Huawei-bgp]net 2.2.2.2 32
[Huawei-bgp]net 3.3.3.3 32
[Huawei-bgp]net 4.4.4.4 32
[Huawei-bgp]net 5.5.5.5 32

R5

<Huawei>u t m
Info: Current terminal monitor is off.
<Huawei>sys
Enter system view, return user view with Ctrl+Z.
[Huawei]int g 0/0/2
[Huawei-GigabitEthernet0/0/2]ip add 45.0.0.5 24
[Huawei-GigabitEthernet0/0/2]int loop0
[Huawei-LoopBack0]ip add 5.5.5.5 32
[Huawei-LoopBack0]q
[Huawei]ip route-static 4.4.4.4 32 45.0.0.4
[Huawei]bgp 300
[Huawei-bgp]router-id 5.5.5.5
[Huawei-bgp]peer 4.4.4.4 as-number 200
[Huawei-bgp]peer 4.4.4.4 connect-interface LoopBack 0
[Huawei-bgp]peer 4.4.4.4 ebgp-max-hop 2
[Huawei-bgp]net 4.4.4.4 32
[Huawei-bgp]net 5.5.5.5 32

2、查看BGP路由表

R1

[Huawei]dis bgp routing-table 

 BGP Local router ID is 1.1.1.1 
 Status codes: * - valid, > - best, d - damped,
               h - history,  i - internal, s - suppressed, S - Stale
               Origin : i - IGP, e - EGP, ? - incomplete


 Total Number of Routes: 13
      Network            NextHop        MED        LocPrf    PrefVal Path/Ogn

 *>   1.1.1.1/32         0.0.0.0         0                     0      i
 *                       2.2.2.2         0                     0      200i
 *                       3.3.3.3         0                     0      200i
 *>   2.2.2.2/32         0.0.0.0         0                     0      i
 *                       3.3.3.3         2                     0      200i
                         2.2.2.2         0                     0      200i
 *>   3.3.3.3/32         0.0.0.0         0                     0      i
 *                       2.2.2.2         2                     0      200i
                         3.3.3.3         0                     0      200i
 *>   4.4.4.4/32         2.2.2.2         1                     0      200i
 *                       3.3.3.3         1                     0      200i
 *>   5.5.5.5/32         2.2.2.2                               0      200i
 *                       3.3.3.3                               0      200i

R4

[Huawei]dis bgp routing-table 

 BGP Local router ID is 4.4.4.4 
 Status codes: * - valid, > - best, d - damped,
               h - history,  i - internal, s - suppressed, S - Stale
               Origin : i - IGP, e - EGP, ? - incomplete


 Total Number of Routes: 14
      Network            NextHop        MED        LocPrf    PrefVal Path/Ogn

 *>i  1.1.1.1/32         2.2.2.2         0          100        0      i
 * i                     3.3.3.3         0          100        0      i
 *>   2.2.2.2/32         0.0.0.0         1                     0      i
 * i                     3.3.3.3         2          100        0      i
   i                     2.2.2.2         0          100        0      i
 *>   3.3.3.3/32         0.0.0.0         1                     0      i
 * i                     2.2.2.2         2          100        0      i
   i                     3.3.3.3         0          100        0      i
 *>   4.4.4.4/32         0.0.0.0         0                     0      i
 * i                     2.2.2.2         1          100        0      i
 * i                     3.3.3.3         1          100        0      i
 *                       5.5.5.5         0                     0      300i
 *>   5.5.5.5/32         0.0.0.0         0                     0      i
                         5.5.5.5         0                     0      300i

这时候,我们可以看到,从R1到R4走的是R2,R4到R1走的也是R2,我们需要择路。

3、择路(我们用两种方式)

R1

[Huawei]route-policy as permit node 10
Info: New Sequence of this List.
[Huawei-route-policy]apply as-path 123 123 additive 
[Huawei-route-policy]q
[Huawei]bgp 100	
[Huawei-bgp]peer 2.2.2.2 route-policy as import 
[Huawei-bgp]q
[Huawei]q
<Huawei>refresh bgp all import 

R3

[Huawei]route-policy lop permit node 10
Info: New Sequence of this List.
[Huawei-route-policy]apply local-preference 222
[Huawei-route-policy]q
[Huawei]bgp 200	
[Huawei-bgp]peer 4.4.4.4 route-policy lop export 
[Huawei-bgp]q
[Huawei]q	
<Huawei>refresh bgp all export 

4、刷新下BGP,再次查看择路结果

R1

<Huawei>dis bgp routing-table 

 BGP Local router ID is 1.1.1.1 
 Status codes: * - valid, > - best, d - damped,
               h - history,  i - internal, s - suppressed, S - Stale
               Origin : i - IGP, e - EGP, ? - incomplete


 Total Number of Routes: 13
      Network            NextHop        MED        LocPrf    PrefVal Path/Ogn

 *>   1.1.1.1/32         0.0.0.0         0                     0      i
 *                       3.3.3.3         0                     0      200i
 *                       2.2.2.2         0                     0      123 123 20
0i
 *>   2.2.2.2/32         0.0.0.0         0                     0      i
 *                       3.3.3.3         2                     0      200i
                         2.2.2.2         0                     0      123 123 20
0i
 *>   3.3.3.3/32         0.0.0.0         0                     0      i
 *                       2.2.2.2         2                     0      123 123 20
0i
                         3.3.3.3         0                     0      200i
 *>   4.4.4.4/32         3.3.3.3         1                     0      200i
 *                       2.2.2.2         1                     0      123 123 20
0i
 *>   5.5.5.5/32         3.3.3.3                               0      200i
 *                       2.2.2.2                               0      123 123 20
0i

R4

[Huawei]dis bgp routing-table

 BGP Local router ID is 4.4.4.4 
 Status codes: * - valid, > - best, d - damped,
               h - history,  i - internal, s - suppressed, S - Stale
               Origin : i - IGP, e - EGP, ? - incomplete


 Total Number of Routes: 14
      Network            NextHop        MED        LocPrf    PrefVal Path/Ogn

 *>i  1.1.1.1/32         3.3.3.3         0          222        0      i
 * i                     2.2.2.2         0          100        0      i
 *>i  2.2.2.2/32         3.3.3.3         2          222        0      i
 *                       0.0.0.0         1                     0      i
   i                     2.2.2.2         0          100        0      i
 *>   3.3.3.3/32         0.0.0.0         1                     0      i
 * i                     2.2.2.2         2          100        0      i
   i                     3.3.3.3         0          222        0      i
 *>i  4.4.4.4/32         3.3.3.3         1          222        0      i
 *                       0.0.0.0         0                     0      i
 * i                     2.2.2.2         1          100        0      i
 *                       5.5.5.5         0                     0      300i
 *>   5.5.5.5/32         0.0.0.0         0                     0      i
                         5.5.5.5         0                     0      300i

5、结论

  • 我们可以看到,我们改变了R3的local-preference为222之后,优先级大于R2,然后对D4执行了出站export策略,所以R4到R1的时候选择了R3。
  • 我们在R1对R2执行了入站import策略,对R2已有的AS-path基础上追加了123和123,使得优先级低于R3,这样,从R1到R4是,自然而然的选择了R2这条线。
声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎进行举报,并提供相关证据,工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。