路由 - OSPF概念
一、链路状态路由协议简介
距离矢量路由协议 就像使用路标指明目的地,仅为您提供关于距离和方向的信息。
链路状态路由协议 则如同使用地图一样,有了地图,您就可以看到所有潜在的路径并确定自己的首选路径。
Dijkstra 算法通常称为Shorst Path First(最短路径优先)算法。SPF会累计每条路径从源到目的地的开销。
链路状态的路由过程:
(1) 每台路由器了解其直连的网络。
(2) 每台路由器负责"问候"直连网络中的相邻路由器。 // Hello 数据包
(3) 每台路由器创建一个链路状态数据包LSP, 其中包含与该路由器直连的每条链路的状态。
(4) 每台路由器将LSP泛红到所有邻居,然后邻居将收到的所有LSP存储到数据库中。
(5) 每台路由器使用数据库构建一个完整的拓扑图并计算通向每个目的网络的最佳路径。
简而言之:
(1) 邻居发现
(2) 交换数据库
(3) 计算最短路径
链路的概念:
对于链路状态路由协议来说,链路是路由器上的一个接口。
链路状态的信息包括:
- 接口的IP地址和子网掩码
- 网络类型
- 链路开销
- 该链路上的所有相邻路由器
链路状态路由协议的优点:
- 创建拓扑表
- 快速收敛
- 由事件驱动的更新
- 层次式设计
链路状态路由协议要求:
- 内存
- CPU
- 带宽
主要供电信和ISP使用
推荐一些资源:
Interconnections, Bridges, Routers, Switches, and Internetworking Protocols(互连、网桥、路由器、交换机和网间协议),Radia Perlman 著
Cisco IP Routing(Cisco IP 路由),Alex Zinin 著
Routing the Internet(Internet 路由),Christian Huitema 著
OSPF(RFC 1247, 2328, 2740)
OSPF, Anatomy of an Internet Routing Protocol John Moy
OSPF历史
The initial development of OSPF began in 1987 by the IETF OSPF Working Group.
At that time, the Internet was largely an academic and research network funded by the U.S. government.
In 1989, the specification for OSPFv1 was published in RFC 1131. Two implementations were written. One implementation was developed to run on routers and the other to run on UNIX workstations. The latter implementation became a widespread UNIX process known as GATED. OSPFv1 was an experimental routing protocol and was never deployed.
In 1991, OSPFv2 was introduced in RFC 1247 by John Moy. OSPFv2 offered significant technical improvements over OSPFv1. It is classless by design; therefore, it supports VLSM and CIDR.
At the same time the OSPF was introduced, ISO was working on a link-state routing protocol of their own, Intermediate System-to-Intermediate System (IS-IS). IETF chose OSPF as their recommended Interior Gateway Protocol (IGP).
In 1998, the OSPFv2 specification was updated in RFC 2328, which remains the current RFC for OSPF.
In 1999, OSPFv3 for IPv6 was published in RFC 2740. OSPF for IPv6, created by John Moy, Rob Coltun, and Dennis Ferguson, is not only a new protocol implementation for IPv6, but also a major rewrite of the operation of the protocol.
In 2008, OSPFv3 was updated in RFC 5340 as OSPF for IPv6.
| Route Source | Administrative Distance |
| Connected | 0 |
| Static | 1 |
| EIGRP summary route | 5 |
| External BGP | 20 |
| internal EIGRP | 90 |
| IGRP | 100 |
| OSPF | 110 |
| IS-IS | 115 |
| RIP | 120 |
| External EIGRP | 170 |
| Internal BGP | 200 |
NOTE: Administrative distance (AD) is the trustworthiness (or preference) of the route source.
OSPF has a default administrative distance of 110.
Part 2: OSPF简介
OSPF消息封装
| 消息名称/数字 | 描述 |
| Hello(1) |
用来发现邻居并提供信息, 将状态变成two-way, 维持邻居关系 |
| Database Description(DBD)(2) |
used to exchange brief versions of each LSA, typically on initial topology exchange, so that a router knows a list of that neighbor's know LSAs. |
| LSR(3) |
A packet that lists the LSID of LSAs the sender of the LSR would like the receiver of the LSR to suppl during database exchange. |
| LSU(4) | A packet that contains fully detailed LSAs, typically sent in response to an LSR message |
| LSAck(5) | sent to confirm receipt of an LSU message. |
The OSPF Type 1 packet is the Hello packet. Hello packets are used to:
- Discover OSPF neighbors and establish neighbor adjacencies.
- Advertise parameters on which two routers must agree to become neighbors.
- Elect the Designated Router (DR) and Backup Designated Router (BDR) on multiaccess networks like Ethernet and Frame Relay. Point-to-point links do not require DR or BDR.
The figure displays the fields contained in the Type 1 Hello packet. Important fields shown in the figure include:
- Type - Identifies the type of packet. A one (1) indicates a Hello packet. A value 2 identifies a DBD packet, 3 an LSR packet, 4 an LSU packet, and 5 an LSAck packet.
- Router ID - A 32-bit value expressed in dotted decimal notation (an IPv4 address) used to uniquely identifying the originating router.
- Area ID - Area from which the packet originated.
- Network Mask - Subnet mask associated with the sending interface.
- Hello Interval - Specifies the frequency, in seconds, at which a router sends Hello packets. The default Hello interval on multiaccess networks is 10 seconds. This timer must be the same on neighboring routers; otherwise, an adjacency is not established.
- Router Priority - Used in a DR/BDR election. The default priority for all OSPF routers is 1, but can be manually altered from 0 to 255. The higher the value, the more likely the router becomes the DR on the link.
- Dead Interval - Is the time in seconds that a router waits to hear from a neighbor before declaring the neighboring router out of service. By default, the router Dead Interval is four times the Hello interval. This timer must be the same on neighboring routers; otherwise, an adjacency is not established.
- Designated Router (DR) - Router ID of the DR.
- Backup Designated Router (BDR) - Router ID of the BDR.
- List of Neighbors - List that identifies the router IDs of all adjacent routers.
Hello数据包类型:
- 发现OSPF邻居并建立相邻关系
- 通告两台路由器建立相邻关系所必须统一的参数
- 在NBMA网络中选举DR 和 BDR
OSPF 相邻关系前,必须统一三个值:
- Hello 间隔
- Dead 间隔
- 网络类型
OSPF show commands:
- show ip protocols
- show ip ospf interface bfief
- show ip route
- show ip ospf database
- show ip ospf neighbors
Link-State Data Structures
Neighbor Table (adjacency database) // show ip ospf neighbors
- contains list of recognized neighbors
Topology Table (LSDB)
- contains all routers and their attached links in the area or network
- identical LSDB for all routers within an area
Routing Table (forwarding database)
- Contains list of best paths to destinations
| Database | Table | Description |
| Adacency Database | Neighbor Table |
1. List of all neighbor routes to which a router has established bidirection communication 2. This table is unique for eatch router 3. Can be viewed using the show ip ospf neighbor command |
| Link-State Database(LSDB) | Topology Table |
1. Lists information about all other routers in the network 2. The database represents the netwrok topology. 3. All routers within an area have identical LSDB. 4. Can be viewed using the show ip ospf database command. |
| Forwarding Database | Routing Table |
1. List of routes generated when an algorithm is run on the link-state database. 2. Each router's routing table is unique and contains information on how and where to send packets to other routers 3. can be viewed using the show ip route command. |
| Feature | Description |
| Transport | IP, protocol type 89 |
| Metric(Cost) | Based on accumulative cost of all outgoing interfaces in a route. |
| Hello-interval | |
| Dead-interval | |
| Update destination address |
224.0.0.5(ALL SPF Routers) 224.0.0.6(All DR) |
| Full or Partial updates |
when new neighbors are discovered: Full updates Otherwise: Partial updates |
| Authentication | clear-text or MD5 authentication |
| VLSM/Classless | |
| Route Tags | Allows OSPF to tag routes as they are redistributed into OSPF |
| Next-hop filed | Supports the advertisement of routes with a different next-hop router than the advertising router. |
| Manual route summarization | Allows route summarization at ABR routers only. |
1.2 OSPF Feature
- Classless - It is classless by design; therefore, it supports VLSM and CIDR.
- Efficient - Routing changes trigger routing updates (no periodic updates). It uses the SPF algorithm to choose the best path.
- Fast convergence - It quickly propagates network changes.
- Scalable - It works well in small and large network sizes. Routers can be grouped into areas to support a hierarchical system.
- Secure - It supports Message Digest 5 (MD5) authentication. When enabled, OSPF routers only accept encrypted routing updates from peers with the same pre-shared password.
三、Neighbors and Adjacencies
two classes of neighbors exist:
- neighbors
- fully adjacent neighbors
OSPF must meet two requrements, than it can send Hello messages: [multicast address: 224.0.0.5]
- OSPF has been enabled on the interface (network)
- the interface has not been made passive (passive-interface)
OSPF Hello Messages:
- OSPF Router ID
- Stub area flag
- plus the following interface-specivic settings:
- Hello interval
- dead interval
- subnet mask
- area ID
- router priority
- DR IP address
- BDR IP address
- Authentication digest
|
因素 |
OSPF |
EIGRP |
|
网络掩码必须一致 |
Yes |
Yes |
|
没有被passive |
Yes |
Yes |
|
必须在相同区域 |
Yes |
N/A |
|
Hello-interval Dead interval 一致 |
Yes |
No |
|
Router ID 必须不一致 |
Yes |
No |
|
IP MTU 必须一致 |
Yes |
No |
|
必须通过认证 |
Yes |
Yes |
|
K-values 一致 |
N/A |
Yes |
|
必须使用相同ASN或Process-id |
NO |
Yes |
| >1.544Mb/s | Hello interval 10s Dead interval 40s |
| NBMA | Hello interval 30s Dead interval 120s |
ip ospf dead-interval minimal hello-multiplier 5
OSPF Authentication
| Type | Meaning | Enabling interface Subcommand | authenticatoin key configuration interface subcommmand |
| 0 | None | ip ospf authentication null | N/A |
| 1 | Clear text | ip ospf authentication | ip ospf authentication-key key-value |
| 2 | MD5 | ip ospf authentication message-digest | ip ospf message-digest-key key-number md5 key-value |
Authentication Confiugre steps:
step 1: authentication must be enabled.
if you want to enabling on all interfaces in an area : area ? [authenticaton] [authentication message-digest] authentication
step 2: The authentication keys must be configured per interface.
Part 4:多路访问网络中的OSPF
| Interface Type | uses DR/BDR? | Default Hello interval | Dynamic Discovery of Neighbors | More than two routers allowed in the subnet? |
| Broadcast | yes | 10 | yes | yes |
| Point-to-Point | No | 10 | yes | No |
| Loopback | No | - | - | No |
| NBMA | Yes | 30 | No | Yes |
| Point-to-multipoint | No | 30 | Yes | Yes |
| Point-to-multipoint nonbroadcast | No | 30 | No | Yes |
多路访问网络对OSPF的LSA泛红过程提出了两项挑战:
- 创建多边相邻关系,其中每对路由器都存在一项相邻关系。
- LSA的大量泛红。
解决方案:选举DR 与 BDR
多路访问网络中的路由器会选举出一个 DR 和一个 BDR。DROther 仅与网络中的 DR 和 BDR 建立完全的相邻关系。这意味着 DROther 无需向网络中的所有路由器泛洪 LSA,只需使用组播地址 224.0.0.6(ALLDRouters — 所有 DR 路由器)将其 LSA 发送给 DR 和 BDR 即可。DR 使用组播地址 224.0.0.5(AllSPFRouters — 所有 OSPF 路由器)。最终结果是,多路访问网络中仅有一台路由器负责泛洪所有 LSA。
DR/BDR 选举
DR 和 BDR 是如何选出的呢?选举过程遵循以下条件:
- DR:具有最高 OSPF 接口优先级的路由器
- BDR:具有第二高 OSPF 接口优先级的路由器
- 如果 OSPF 接口优先级相等,则取路由器 ID 最高者。
DR 一旦选出,将保持 DR 地位,直到出现下列条件之一为止:
- DR 发生故障。
- DR 上的 OSPF 进程发生故障。
- DR 上的多路访问接口发生故障。
route-id 的选举过程:
手工指定route-id x.x.x.x > 最高的loopback接口的IP地址 > 最高的物理接口的IP地址
五、LSDB and LSAs
|
LSA Type |
Common Name |
Description |
|
1 |
Router LSA |
Each router creates its own Type 1 LSA to represent itself for etch area to which it connects. The LSDB for one area contains one type 1 LSA per router per area, listing the RID and all interface IP addresses on that router that are in that area. Represents stub netwroks as well. |
|
2 |
Network LSA |
One per transit network. Created by the DR on the subnet, and represents the subnet and the router interfaces connected to the subnet. |
|
3 |
Net Summary LSA |
Created by ABRs to represent subnets listed in one area's type 1 and 2 LSAs when being advertised into another area. Dfines the links(subnets) in the origin area, and cost, but no topology data. |
|
4 |
ASBR Summary |
Like a type 3 LSA, except it advertises a host route used to reach an ASBR. |
|
5 |
AS External LSA |
Created by ASBRs for external routes injected into OSPF. |
|
6 |
Multicast OSPF LSA |
Defined for MOSPF. |
|
7 |
Defined for Not-So-Stubby Areas |
Created by ASBRs inside an NSSA area, instead of a type 5 LSA. |
|
8 |
External Attributes LSA for BGP |
|
|
9, 10, 11 |
Opaque LSA |
LSA Type 1: Router LSA: show ip ospf database router
- Type 1 LSA include a list of directly connected network prefixes and link types.
- all routers generate type 1 LSA.
- Type 1 LSA are flooded within the area and do not propagate beyond an ABR.
- A type 1 LSA link-state ID(LSID) is identified by the router ID of the originating router.
in particular, the LSA Type 1 lists:
- for each interface on which no DR has been elected, it lists the router's interface subnet number/mask and interface cost.
- for each interface on which a DR has been elected, it lists the IP address of the DR and notation that the link attaches to a transit network.
- for each interface with no DR, but for which a neighbor is reachable, it lists the neighbor's RID.
Internal routers each create a single Type 1 LSA for themselves, but ABRs create multiple type 1 LSA for themselves: one per area.
LSA Type 2: Network LSA: show ip ospf database network
- Type 2 LSA identifiy the routers and the network addresses of the multiaccesss links
- only a DR generates a type 2 LSA
- Typ 2 LSA are flooded within the multiaccess network and do not go beyond an ABR.
- a Type 2 LSA link-state ID is identified by the DR rotuer ID.
LSA Type 3: Summary LSA: show ip ospf database summary
- A type 3 LSA describe a network address learned by type 1 LSA.
- a type 3 LSA is required for every subnet.
- ABR flood type 3 LSA to other areas and are regenerated by other ABRs.
- a type 3 LSA link-state ID is identified by the network address.
- by default, routes are not summarized.
------------------------------------------------------------------------
| LSA Type (Number) | LSA Type(Name) | This Type Represents | Display show ip ospf database keyworkd.... | LSID is Equal to | Created By |
| 1 | Router | a router | router | RID of router | Each router creates its own |
| 2 | Network | a subnet in which a DR exists | network | DR's IP address in the subnet | the DR in that subnet |
| 3 | summary | subnet in another area | summary | subnet number | an ABR |
--------------------------------------------------------------------------
LSA Type 4: ASBR Summary LSA: show ip ospf database asbr-summary
- type 4 LSA are used to advertise an ASBR to other areas and provide a route to the ASBR.
- ASBR generate type 4 LSA
- a type 4 LSA is generated by the originating ABR and regenerated by other ABRs
- a type 4 LSA link-state ID is identified by the rotuer ID of the ASBR.
LSA Type 5: AS external LSA: show ip ospf database external
- type 5 LSA are used to advertise external network address.
- a ASBR generates a type 5 LSA
- Type 5 LSA are flooded throughout the area and regenerated by other ABR
- a type 5 LSA link-state ID is the external network address.
- by default, routes are not summarized.
LSA Type 7: NSSA External LSA: show ip opsf database nssa-external
- NSSA External LSA are originated by ASBR within not-so-stubby areas.
- NSSA external LSA are flooded only within the not-so-subby area.
LSDB 交互过程
| State | Meaning |
| Down | No Hellos have been received from this neighbor for more than the dead interval. |
| Attempt | Used when the neighbor is defined with the neighbor command, after sending a Hello, but before receiving a Hello from that neighbor. |
| Init | A Hello has been received from the neighbor, but it did not have the local router's RID in it or lists parameters that do not pass the neighbor verification checks. This is a permanent state when Hello parameters do not match. |
| 2Way | A Hello has been received from the neighbor, it has the router's RID in it. and all neighbor verification checks passed. |
| Exstart | Currently negotiating the DBD sequence numbers and master/slave logic used for DBD packets. |
| Exchange | Finished negotiating the DD process particulars, and currently exchanging DD packets |
| Loading | All DD packets are exhanged, and the routers are currently sending LSR, LSU, AND LSAck packets to exchange full LSAs. |
| Full | Neighbors are fully adjacent, meaning they believe that their LSDBs for that area are identical. Routing table calculations can begin. |
3. OSPF 邻居建立过程

Down State
- No Hello Packets received = Down
- Router sends Hello packets
- Transition to init state
init State
- Hello packets are received from the neighbor.
- They contain the sending router's Router ID
- Transition to Two-Way State
Two-Way State
- On Ethernet links, elect a DR and BDr
- Transiiton to Exstart State
ExStart State
- Negotiate master/slave relationship and DBD packet sequence number
- The master initiates the DBD packet exchange.
Exchange State
- Routers exchange DBD packets
- if additional router information is required then transition to loading; otherwise, tansition to Full
Loading State
- LSRs and LSUs are used to gain additional route information
- Routes are processed using the SPF algorithm.
- Transition to the Full State.
Full State
- Routers have converged.
Part 6: OSPF 汇总、默认路由与special Areas.
OSPF allows summarization at both ABR and ASBR but not other OSPF routers.
1. Manual Summarization at ABR
area area-id range ip-address mask [cost cost] [not-advertise]
- The consigured area number refers to the area where the subnets exists; the summary will be advertisd into all other areas connected to the ABR
- The ABR compares the summary route's range of address with all intra-area OSPF routes, in the origin area, for which the ABR is creating Type 3 LSAs. if at least one subordinate subnet exists, then the ABR advertises the summary route as a Type 3 LSA.
- The ABR does not advertise the subordinate subnet’s Type 3 LSAs.
- The ABR assigns a metric for the summary route’s Type 3 LSA, by default, to match the best metric among all subordinate subnets.
- The area range command can also explicitly set the cost of the summary.
- If no subordinate subnets exist, the ABR does not advertise the summary.
Note: not-advertise option on the area range command tells the ABR to not advertise the Type 3 LSA for the summary route.
2. Manual Summarization at ASBRs
summary-address prefix mask
- The ASBR compares the summary route’s range of addresses with all routes redistributed into OSPF on that ASBR to find any subordinate subnets (subnets that sit inside the summary route range). If at least one subordinate subnet exists, the ASBR advertises the summary route.
- The ASBR does not advertise the subordinate subnets.
- To create the summary, the ASBR actually creates a Type 5 LSA for the summary route.
- The ASBR assigns the summary route the same metric as the lowest metric route amongst all subordinate subnets.
- If no subordinate subnets exist, the ASBR does not advertise the summary.
- Unlike the area range command, the summary-address command cannot be used to directly set the metric of the summary route.
| Where Used | Command |
| ASBR | summary-address {{ip-address mask} | {prefix mask}} [not-advertise] |
| ABR | area area-id range ip-address mask [advertise | not-advertise] [cost cost] |
3. 默认路由
Enterprises typically use default routes in two different cases:
- to direct remot-site routers at the edge of the Enterprise netwrok to send all packets.
- to direct traffic on all Enterprise routers toward Internet-facing router so that all traffic destined for the Internet.
Domain-wide Defaults Using the default-information originate Command
The OSPF subcommand default-information originate tells OSPF to create a Type 5 LSA for a default route -0.0.0.0/0- and flood it like any other type 5 LSA.
default-information originate [always] [metric metric-value] [metric-type type-value] [route-map map-name]
The follwing list summarizes the features of the default-information originate OSPF subcommand:
- With all default parameters, it injects a default router into OSPF, as an External Type 2 route, using a Type 5 LSA, with metric 1, but only if a default route exists in that router's routing talbe.
- With the always parameter, the default route is advertised even if there is no default route in the router's routing table.
- The metric keyword defines the metric listed for the default route(default 1)
- The metric-type keyword defines whether the LSA is listed as external type 1 or exter type 2(default).
- The decision of when to advertise, and when to withdraw, the default route is based on matching the referenced route-map with a permit action.
OSPF 外部路由分为以下两类:第 1 类外部 (E1) 和第 2 类外部 (E2)。两种类型的差异在于路由的 OSPF 开销在每台路由器上的计算方式不同。当 E1 路由在整个 OSPF 区域内传播时,OSPF 会累计路由的开销。此过程与普通 OSPF 内部路由的计算过程相同。然而,E2 路由的开销却始终是外部开销,而与通向该路由的内部开销无关。
Special Areas
RFC 2328 defines OSPF Stub areas.
RFC 1587 defines NSSA.
Totally stubby areas are not defined in an RFC, but Cisco and other vendors' routers do use them.
| 区域类型 | 1和2 | 3 | 4 | 5 | 7 |
| 骨干区域(区域0) | 允许 | 允许 | 允许 | 允许 | 不允许 |
| 非骨干区域 && 非末梢区域 | 允许 | 允许 | 允许 | 允许 | 不允许 |
| 末梢区域 | 允许 | 允许 | 不允许 | 不允许 | 不允许 |
| 完全末梢区域 | 允许 | 不允许 | 不允许 | 不允许 | 不允许 |
| NSSA | 允许 | 允许 | 允许 | 不允许 | 允许 |
Part 7: OSPF Virtual Links
OSPF Virtual link allows two ABRs that connect to the same nonbackbone area to form a neighbor relationship through that nonbackbone area.

Part 8: OSPF Over Multipoint Frame Relay
OSPF over Multipoint Frame Relay Subinterfaces
interface s0/0/0
encapsulation frame-relay
!
interface s0/0/0.1 multipoint
ip address 10.1.123.1 255.255.255.248
frame-relay interface-dlci 102
frame-relay interface-dlci 102
frame-relay interface-dlci 102
The default OSPF network type on multipint interfaces: The nonbroadcast network type.
prevents OSPF from working without further configuration.
Three Questions related to OSPF over Frame Relay multipoint interfaces:
- Do the routers attempt to discover neighbors by sending and receiving multicast OSPF Hello messages, or do the neighbors require static definition?
- Do the routers attempt to elect a DR/BDR?
- Does a partial mesh exist or full mesh?
实验
LAB – CCNP Single-Area OSPF Link Costs and Interface priorities
LAB – CCNP Multi-Area OSPF with Stub Areas and Authentication
LAB - CCNP OSPF Virtual Links and Area Summarization
LAB - CCNP OSPF over Frame Relay
OSPFv3理论

浙公网安备 33010602011771号