当前位置: 首页 > 工具软件 > Zebra > 使用案例 >

Quagga(Zebra)基本配置

贺华容
2023-12-01

Zebra使用ospfd程序实现OSPF路由功能,但ospfd需要从zebra程序获得接口信息,所以zebra程序必须在 ospfd

程序之前运行。ospfd不支持多个OSPF进程,我们不能指定OSPF进程号。

初始化第一台机器:

shell_1> cd /usr/local/etc
shell_1> cp zebra.conf.sample zebra.conf
shell_1> cp ospfd.conf.sample ospfd.conf
shell_1> zebra -d

进入zebra设置IP

shell_1> telnet localhost 2601
Password:
Router> en
Password:
Router# conf t
Router(config)# hostname r1
r1(config)# int eth0
r1(config-if)# ip address 192.168.5.121/24
r1(config-if)# ctrl+z
r1# copy run start

进入第一台机器的ospf设置

shell_1> ospfd -d
shell_1> telnet localhost 2604
Password:
ospfd> en
ospfd# conf t
ospfd(config)# hostname r1_ospfd !改个名字好辨认
r1_ospfd(config)# router ospf !启动ospf
r1_ospfd(config-router)# ospf router-id 192.168.5.121 !设置router-id
r1_ospfd(config-router)# network 192.168.5.0/24 area 0
!最关键的,来标识路由器上哪些IP网络号是OSPF的一部分,对于每个网络,我们必须标识该网络所属 的区域

。由于我们只有两台机器,当然只有一个网络,所以只需执行一个network命令就够了。

对于我们的小网络,ospf就算配好了,下面来检验一下:

r1_ospfd(config-router)# ctrl+z
r1_ospfd# sh ip ospf route
============ OSPF network routing table ============
N    192.168.5.0/24        [10] area: 0.0.0.0
                           directly attached to eth0

============ OSPF router routing table =============

============ OSPF external routing table ===========

r1_ospfd# sh ip ospf database

       OSPF Router with ID (192.168.5.121)

                Router Link States (Area 0.0.0.0)

Link ID         ADV Router      Age Seq#       CkSum Link count
192.168.5.121   192.168.5.121    126 0x80000002 0x8584 1

r1_ospfd# sh ip ospf int eth0
eth0 is up, line protocol is up

Internet Address 192.168.5.121/24, Area 0.0.0.0
Router ID 192.168.5.121, Network Type BROADCAST, Cost: 10
Transmit Delay is 1 sec, State DR, Priority 1
Designated Router (ID) 192.168.5.121, Interface Address 192.168.5.121
No backup designated router on this network
Timer intarvals configured, Hello 10, Dead 40, Wait 40, Retransmit 5
    Hello due in 00:00:07
Neighbor Count is 0, Adjacent neighbor count is 0

由于网络里没有其它的路由器,r1就把自己选为DR(指定路由器)了。Zebra对log处理可能有些问题,使 用

#p#

log stdout不能显示各种debug信息,所以只能记录到文件,在shell下用tail命令查看。而且debug命令和 实

际路由器也有不同。

r1_ospfd# debug ospf event
r1_ospfd(config)# log file /usr/local/etc/ospfd.log

然后我们在shell下查看debug信息

shell_1> tail -f /usr/local/etc/ospfd.log
--------------------------------8<---------------------------------------
2002/04/28 14:24:27 OSPF: make_hello: options: 2, int: eth0:192.168.5.121
2002/04/28 14:24:37 OSPF: make_hello: options: 2, int: eth0:192.168.5.121
2002/04/28 14:24:47 OSPF: make_hello: options: 2, int: eth0:192.168.5.121
2002/04/28 14:24:57 OSPF: make_hello: options: 2, int: eth0:192.168.5.121
2002/04/28 14:25:07 OSPF: make_hello: options: 2, int: eth0:192.168.5.121
--------------------------------8<---------------------------------------

我们错过了最开始的信息,看到路由器每隔10秒发送一个hello数据包。hello数据包通过多目组播地址

224.0.0.5被发送出去,如果我们打开debug ospf packet all就能很清楚的看到。

第二台机器的设置

前面的初始化和第一台一样,不过这里名字设成r2便于辨认,IP设成了192.168.5.123/24。

进入第二台机器的ospf设置

shell_2> ospfd -d
shell_2> telnet localhost 2604
Password:
ospfd> en
ospfd# conf t
ospfd(config)# hostname r2_ospfd
r2_ospfd(config)# router ospf
r2_ospfd(config-router)# ospf router-id 192.168.5.123
r2_ospfd(config-router)# network 192.168.5.0/24 area 0

执行完network命令,我们看到第一台机器的tail -f /usr/local/etc/ospfd.log输出下面的信息:

--------------------------------8<---------------------------------------
2002/04/28 14:25:51 OSPF: Packet 192.168.5.123 [Hello:RECV]: Options *|*|-|-|-|-|E|*
2002/04/28 14:25:51 OSPF: NSM[eth0:192.168.5.121:0.0.0.0]: start
2002/04/28 14:25:52 OSPF: make_hello: options: 2, int: eth0:192.168.5.121
2002/04/28 14:25:52 OSPF: couldn't find any VL to associate the packet with
2002/04/28 14:25:52 OSPF: DR-Election[1st]: Backup 192.168.5.123
2002/04/28 14:25:52 OSPF: DR-Election[1st]: DR     192.168.5.121
2002/04/28 14:25:52 OSPF: Packet[DD]: Negotiation done (Slave).
--------------------------------8<---------------------------------------

r1收到r2(192.168.5.123)发过来的hello数据包,交换信息后选举DR,由于本身192.168.5.121是DR了,所以

只选举了BDR就好了。这时在r1上就能看到r2了。

r1_ospfd# sh ip ospf neig

Neighbor ID     Pri   State           Dead Time   Address         Interface              RXmtL

RqstL DBsmL
192.168.5.123     1   Full/Backup     00:00:37    192.168.5.123   eth0:192.168.5.121     0     0

   0

检验其它信息

r1_ospfd# sh ip ospf database

       OSPF Router with ID (192.168.5.121)

                Router Link States (Area 0.0.0.0)

Link ID         ADV Router      Age Seq#       CkSum Link count
192.168.5.121   192.168.5.121   1259 0x80000008 0x534e 1
192.168.5.123   192.168.5.123   1265 0x80000006 0x534a 1

                Net Link States (Area 0.0.0.0)

Link ID         ADV Router      Age Seq#       CkSum
192.168.5.123   192.168.5.123   1265 0x80000001 0x5a5a

r1_ospfd# sh ip ospf int eth0
eth0 is up, line protocol is up

Internet Address 192.168.5.121/24, Area 0.0.0.0
Router ID 192.168.5.121, Network Type BROADCAST, Cost: 10
Transmit Delay is 1 sec, State DR, Priority 1
Designated Router (ID) 192.168.5.121, Interface Address 192.168.5.121
Backup Designated Router (ID) 192.168.5.123, Interface Address 192.168.5.123
Timer intarvals configured, Hello 10, Dead 40, Wait 40, Retransmit 5
    Hello due in 00:00:01
Neighbor Count is 1, Adjacent neighbor count is 1

#p#

和前面的输出信息相比,发生了很多变化,两台路由器已经相互识别了。OSPF不象RIP一样,每隔30秒 给所有

的邻居广播一次完整的路由表,而是通过IP多目组播地址224.0.0.5每隔10秒发送一个很小的hello 数据包来维

护邻居关系,当链路发生变化的时候,才重新计算。

拔掉两台机器连接的网线,看ospfd.log的记录:

--------------------------------8<---------------------------------------
2002/04/28 16:25:53 OSPF: make_hello: options: 2, int: eth0:192.168.5.121
2002/04/28 16:25:57 OSPF: Packet 192.168.5.123 [Hello:RECV]: Options *|*|-|-|-|-|E|*
2002/04/28 16:26:03 OSPF: make_hello: options: 2, int: eth0:192.168.5.121
2002/04/28 16:26:13 OSPF: make_hello: options: 2, int: eth0:192.168.5.121
2002/04/28 16:26:23 OSPF: make_hello: options: 2, int: eth0:192.168.5.121
2002/04/28 16:26:33 OSPF: make_hello: options: 2, int: eth0:192.168.5.121
2002/04/28 16:26:37 OSPF: ospf_check_abr_status(): Start
2002/04/28 16:26:37 OSPF: ospf_check_abr_status(): looked through areas
2002/04/28 16:26:37 OSPF: ospf_check_abr_status(): bb_configured: 1
2002/04/28 16:26:37 OSPF: ospf_check_abr_status(): bb_act_attached: 1
2002/04/28 16:26:37 OSPF: ospf_check_abr_status(): areas_configured: 1
2002/04/28 16:26:37 OSPF: ospf_check_abr_status(): areas_act_attached: 1
2002/04/28 16:26:37 OSPF: nsm_change_status(): scheduling new router-LSA origination
2002/04/28 16:26:37 OSPF: DR-Election[1nd]: Backup 0.0.0.0
2002/04/28 16:26:37 OSPF: DR-Election[1nd]: DR     192.168.5.121
2002/04/28 16:26:37 OSPF: ospf_check_abr_status(): Start
2002/04/28 16:26:37 OSPF: ospf_check_abr_status(): looked through areas
2002/04/28 16:26:37 OSPF: ospf_check_abr_status(): bb_configured: 1
2002/04/28 16:26:37 OSPF: ospf_check_abr_status(): bb_act_attached: 1
2002/04/28 16:26:37 OSPF: ospf_check_abr_status(): areas_configured: 1
2002/04/28 16:26:37 OSPF: ospf_check_abr_status(): areas_act_attached: 1
2002/04/28 16:26:37 OSPF: Timer[router-LSA]: (router-LSA Refresh expire)
2002/04/28 16:26:37 OSPF: counting fully adjacent virtual neighbors in area 0.0.0.0
2002/04/28 16:26:37 OSPF: there are 0 of them
2002/04/28 16:26:37 OSPF: SPF: calculation timer scheduled
2002/04/28 16:26:37 OSPF: SPF: calculation timer delay = 5
2002/04/28 16:26:37 OSPF: ospf_flood_through_interface(): considering int eth0:192.168.5.121
2002/04/28 16:26:37 OSPF: ospf_flood_through_interface(): considering nbr 192.168.5.121
2002/04/28 16:26:42 OSPF: SPF: Timer (SPF calculation expire)
2002/04/28 16:26:42 OSPF: ospf_spf_calculate: Start
2002/04/28 16:26:42 OSPF: ospf_spf_calculate: running Dijkstra for area 0.0.0.0
2002/04/28 16:26:42 OSPF: SPF Result: 0 [R] 192.168.5.121
2002/04/28 16:26:42 OSPF: ========== OSPF routing table ==========
2002/04/28 16:26:42 OSPF: ========================================
2002/04/28 16:26:42 OSPF: ospf_process_stub():processing stubs for area 0.0.0.0
2002/04/28 16:26:42 OSPF: ospf_process_stub():processing router LSA, id: 192.168.5.121
2002/04/28 16:26:42 OSPF: ospf_process_stub(): we have 1 links to process
2002/04/28 16:26:42 OSPF: ospf_intra_add_stub(): Start
2002/04/28 16:26:42 OSPF: ospf_intra_add_stub(): processing route to 192.168.5.0/24
2002/04/28 16:26:42 OSPF: ospf_intra_add_stub(): calculated cost is 0 + 10 = 10
2002/04/28 16:26:42 OSPF: ospf_intra_add_stub(): installing new route
2002/04/28 16:26:42 OSPF: ospf_intra_add_stub(): this network is on this router
2002/04/28 16:26:42 OSPF: ospf_intra_add_stub(): the interface is eth0:192.168.5.121
2002/04/28 16:26:42 OSPF: ospf_intra_add_stub(): Stop
2002/04/28 16:26:42 OSPF: children of V:
2002/04/28 16:26:42 OSPF: ospf_spf_calculate: Stop
2002/04/28 16:26:42 OSPF: ospf_ia_routing():start
2002/04/28 16:26:42 OSPF: ospf_ia_routing():not ABR, considering all areas
2002/04/28 16:26:42 OSPF: Pruning unreachable networks
2002/04/28 16:26:42 OSPF: Pruning unreachable routers
2002/04/28 16:26:42 OSPF: Route: Router Routing Table free
2002/04/28 16:26:42 OSPF: SPF: calculation complete
--------------------------------8<---------------------------------------

我们看到r1生成一个LSA包,通知其它路由器,由于网络里只有自己了,又选自己为DR。r2也是一样。 我们再

插上网线,查看ospfd.log:

--------------------------------8<---------------------------------------
2002/04/28 16:52:08 OSPF: Packet 192.168.5.123 [Hello:RECV]: Options *|*|-|-|-|-|E|*
2002/04/28 16:52:08 OSPF: NSM[eth0:192.168.5.121:0.0.0.0]: start
2002/04/28 16:52:08 OSPF: DR-Election[1st]: Backup 192.168.5.123
2002/04/28 16:52:08 OSPF: DR-Election[1st]: DR     192.168.5.121
2002/04/28 16:52:08 OSPF: DR-Election[1st]: Backup 0.0.0.0
2002/04/28 16:52:08 OSPF: DR-Election[1st]: DR     192.168.5.123
2002/04/28 16:52:08 OSPF: DR-Election[2nd]: Backup 192.168.5.121
2002/04/28 16:52:08 OSPF: DR-Election[2nd]: DR     192.168.5.123
--------------------------------8<---------------------------------------

#p#

由于拔了网线,r1和r2都把自己选为DR,一个网络只能有一个DR,所以恢复连接后它们重新进行了DR选 举,由

于192.168.5.123的router id大,所以它被选为DR。

保存一下配置

r1_ospfd# copy run start
Configuration saved to /usr/local/etc/ospfd.conf
r2_ospfd# copy run start
Configuration saved to /usr/local/etc/ospfd.conf

以上只是演示了最简单的OSPF的配置,而OSPF在大型网络才广泛的使用,配置也复杂多很多。即使是 Zebra,

也还可用做复杂的多的OSPF实验。

4、用Zebra做BGP实验

RIP和OSPF都是内部网关协议(IGP),BGP属于外部网关协议(EGP)。BGP广泛用于Internet以连接 ISP,并将

企业与ISP互连。

当BGP的影响被完全了解,并且至少下列情况之一存在时,在AS中使用BGP才是最恰当的:
1 AS允许数据包穿过它到达其它自治系统(例如,某个服务提供商)。
2 AS有到其它自治系统的多条连接。
3 必须对进入和离开AS的数据流进行控制。

对于互连的自治系统来说,BGP并不总是恰当的解决方案,如果有如下情况中的一个或多个时,不要使 用BGP:
1 只有到Internet或另一AS的单一连接。
2 无需考虑路由策略或路由。
3 路由器缺乏经常性的BGP更新的内存或处理器。
4 对路由过滤和BGP路径选择过程的了解十分有限。
5 在自治系统间的带宽较低。
在这些情况下,应该使用静态路由。

Zebra支持BGP-4和BGP-4+,下面实验只是演示BGP的基本命令,以及debug的一些信息。一个比较复杂 的用

Zebra做BGP实验见http://www.unixreview.com/print/documentID=15977,有条件可以做一下。

Zebra使用bgpd程序实现BGP路由功能,但bgpd需要从zebra程序获得接口信息,所以zebra程序必须在 bgpd程序

之前运行。

初始化第一台机器:

shell_1> cd /usr/local/etc
shell_1> cp zebra.conf.sample zebra.conf
shell_1> cp bgpd.conf.sample bgpd.conf
shell_1> zebra -d

还有一个bgpd.conf.sample2配置样例是用于IPv6的。

进入zebra设置IP

shell_1> telnet localhost 2601
Password:
Router> en
Password:
Router# conf t
Router(config)# hostname r1
r1(config)# int eth0
r1(config-if)# ip address 192.168.5.121/24
r1(config-if)# ctrl+z
r1# copy run start

进入第一台机器的bgp设置

shell_1> bgpd -d

启动bgpd,我们看到TCP端口179已经打开。两台BGP路由器相互间建立一条TCP连接,交换消息以打开 和确认连

接参数。这两台路由器被称为对等路由器,或者邻居。

shell_1> telnet localhost 2605
Password:
bgpd> en
bgpd# conf t
bgpd(config)# hostname r1_bgpd
r1_bgpd(config)# router bgp 7675

#p#

配置样例里已经指定了AS为7675,我们懒的改就拿来用。AS是一个16bit的数字,其范围从1到 65535。RFC

1930给出了AS编号使用指南。从64512到65535的AS编号范围是留作私用的,类似私有IP。

r1_bgpd(config-router)# network 192.168.5.0/24
r1_bgpd(config-router)# neighbor 192.168.5.121 remote-as 7676

查看bgp信息:

r1_bgpd# sh ip bgp
BGP table version is 0, local router ID is 192.168.5.123
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal
Origin codes: i - IGP, e - EGP, ? - incomplete

   Network          Next Hop            Metric LocPrf Weight Path
*> 192.168.5.0      0.0.0.0                            32768 i

Total number of prefixes 1

把log记录到文件:

r1_bgpd# conf t
r1_bgpd(config)# log file /usr/local/etc/bgpd.log

打开debug选项:

r1_bgpd(config)# exit
r1_bgpd debug bgp events
r1_bgpd debug bgp keepalives
r1_bgpd debug bgp updates

然后在shell下用tail查看log记录:

shell_1> tail -f /usr/local/etc/bgpd.log
--------------------------------8<---------------------------------------
2002/04/29 19:13:08 BGP: 192.168.5.121 [Event] Connect start to 192.168.5.121 fd 10
2002/04/29 19:13:11 BGP: 192.168.5.121 [Event] Connect failed (Operation now in progress)
--------------------------------8<---------------------------------------

r1不能连接邻居192.168.5.121。

第二台机器的设置

前面的初始化和第一台一样,不过这里名字设成r2便于辨认,IP设成了192.168.5.123/24。

进入第二台机器的bgp设置

shell_2> bgpd -d
shell_2> telnet localhost 2605
Password:
bgpd> en
bgpd# conf t
bgpd(config)# hostname r2_bgpd

AS要设成不一样,所以修改一下:

r2_bgpd(config)# no router bgp 7675
r2_bgpd(config)# router bgp 7676
r2_bgpd(config-router)# network 192.168.5.0/24
r2_bgpd(config-router)# neighbor 192.168.5.123 remote-as 7675

这时第一台机器的log出现如下信息:

--------------------------------8<---------------------------------------
2002/04/29 19:16:35 BGP: [Event] BGP connection from host 192.168.5.121
2002/04/29 19:16:35 BGP: [Event] Make dummy peer structure until read Open packet
2002/04/29 19:16:35 BGP: 192.168.5.121 [Event] Transfer temporary BGP peer to existing one
2002/04/29 19:16:35 BGP: 192.168.5.121 [Event] Accepting BGP peer delete
2002/04/29 19:16:35 BGP: 192.168.5.121 send UPDATE 192.168.5.0/24 nexthop 192.168.5.123, origin

i, path
2002/04/29 19:16:35 BGP: 192.168.5.121 rcvd UPDATE w/ attr: nexthop 192.168.5.121, origin i, path

7676
2002/04/29 19:16:35 BGP: 192.168.5.121 rcvd 192.168.5.0/24
--------------------------------8<---------------------------------------

两台bgp已经互连了。再看一下第一台机器的bgp信息:

r1_bgpd# sh ip bgp
BGP table version is 0, local router ID is 192.168.5.123
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal
Origin codes: i - IGP, e - EGP, ? - incomplete

   Network          Next Hop            Metric LocPrf Weight Path
* 192.168.5.0      192.168.5.121                          0 7676 i
*>                  0.0.0.0                            32768 i

Total number of prefixes 1

r1_bgpd# sh ip bgp neighbors
BGP neighbor is 192.168.5.121, remote AS 7676, local AS 7675, external link
BGP version 4, remote router ID 192.168.5.121
BGP state = Established, up for 00:01:13
Last read 00:00:13, hold time is 180, keepalive interval is 60 seconds
Neighbor capabilities:
    Route refresh: advertised and received (old and new)
    Address family IPv4 Unicast: advertised and received
Received 98 messages, 0 notifications, 0 in queue
Sent 103 messages, 0 notifications, 0 in queue
Route refresh request: received 0, sent 0
Minimum time between advertisement runs is 0 seconds

For address family: IPv4 Unicast
Community attribute sent to this neighbor (both)
1 accepted prefixes

Connections established 2; dropped 1
Local host: 192.168.5.123, Local port: 179
Foreign host: 192.168.5.121, Foreign port: 1029
Nexthop: 192.168.5.123
Read thread: on Write thread: off

Zebra还支持很多BGP的特性,请参考GNU Zebra Manual,有条件的可以做一下那些实验。

Zebra的Mailing List比较活跃,有许多人在那里讨论Zebra的开发和配置等等,有问题的话,在那里应该 能得到解答。

 类似资料: