# Ospf Config

> This skill should be used when the user asks to configure or troubleshoot OSPF on Huawei switches (S1720/S2700/S5700/S6720 V200R011C10+ and CloudEngine 5800-9800 V200R022C00+) or USG firewalls. Covers multi-area OSPF, VLANIF-based OSPF on switches, DR/BDR election, Stub/NSSA areas, route import, route summarization, OSPF-BFD linkage, OSPF GR, and IP FRR. Applicable to both switch (VLANIF interface) and firewall (Layer-3 interface) scenarios.

- Skill: `nanxiaoyao/ospf-config` (Agent Skill, multi-file: 3 files)
- Install (CLI): `npx skillmds@latest add nanxiaoyao/ospf-config`
- Raw SKILL.md: https://api.skillmd.com/api/skills/nanxiaoyao/ospf-config/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: nanxiaoyao (https://skillmd.com/u/nanxiaoyao)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/nanxiaoyao/ospf-config

---


# 华为交换机/防火墙 OSPF 配置技能 (ospf-config)

适用平台 S1720/S2700/S5700/S6720 V200R011C10+ ｜ CloudEngine 5800-9800 V200R022C00+ ｜ USG6000E V600R007C20+
文档来源 华为 S 系列与 CloudEngine 系列产品文档 (dc_cfg_ospf_*)

OSPF 是企业内网最常用的动态路由协议 交换机上跑在 VLANIF 三层接口 防火墙上跑在三层物理接口或子接口

## 触发场景
- 配置交换机/防火墙 OSPF 多区域
- VLANIF 接口跑 OSPF（交换机场景）
- 三层接口跑 OSPF（防火墙场景）
- OSPF 邻居建立不起来
- 配置 DR/BDR 选举优先级
- 配置 Stub / NSSA 区域
- OSPF 引入外部路由 / 默认路由通告
- OSPF 路由汇总
- OSPF 与 BFD 联动（毫秒级切换）
- OSPF GR（平滑重启）
- OSPF IP FRR（快速重路由）
- OSPF 路由过滤

## 交换机 vs 防火墙 接口差异

| 项目 | 交换机 | 防火墙 |
|---|---|---|
| 三层接口 | VLANIF | GigabitEthernet / 10GE 物理口或子接口 |
| IP 配置 | interface Vlanif 10 → ip address | interface GigabitEthernet 0/0/1 → ip address |
| 启 OSPF 前 | 必须先创建 VLAN + 加接口 | 必须先把接口加入安全区域 |
| OSPF network | 用 VLANIF 网段 | 用物理接口网段 |

## 配置六步走（交换机 VLANIF 场景）

### 1 创建 VLAN 与 VLANIF
```
system-view
vlan batch 10 20
interface Vlanif 10
 ip address 192.168.0.1 255.255.255.0
 quit
interface Vlanif 20
 ip address 192.168.1.1 255.255.255.0
 quit
```

### 2 物理接口加入 VLAN
```
interface GigabitEthernet 0/0/1
 port link-type trunk
 port trunk allow-pass vlan 10
 quit
interface GigabitEthernet 0/0/2
 port link-type trunk
 port trunk allow-pass vlan 20
 quit
```

### 3 创建 OSPF 进程与区域
```
ospf 1 router-id 10.1.1.1
 area 0.0.0.0
  network 192.168.0.0 0.0.0.255
  quit
 area 0.0.0.1
  network 192.168.1.0 0.0.0.255
  quit
 quit
```

### 4 验证
```
display ospf peer brief
display ospf routing
display ip routing-table protocol ospf
```

## 配置六步走（防火墙三层接口场景）

### 1 接口 IP 与安全区域
```
system-view
interface GigabitEthernet 0/0/1
 ip address 10.1.1.1 24
 quit
firewall zone untrust
 add interface GigabitEthernet 0/0/1
 quit
```

### 2 安全策略放通 OSPF
```
security-policy
 rule name ospf_out
  source-zone local
  destination-zone untrust
  service ospf
  action permit
 rule name ospf_in
  source-zone untrust
  destination-zone local
  service ospf
  action permit
 quit
```

### 3 OSPF 进程
```
ospf 1 router-id 1.1.1.1
 area 0.0.0.0
  network 10.1.1.0 0.0.0.255
  quit
 quit
```

## 高级场景

### DR/BDR 选举（交换机场景）
调整接口 OSPF 优先级 数值大优先 0 表示不参与选举
```
interface Vlanif 10
 ospf dr-priority 100           # 高优先 当 DR
 ospf dr-priority 0             # 不参与选举 永远不是 DR
 ospf dr-priority 2             # 默认 1 可微调
```
> ⚠️ 改了优先级后必须 `reset ospf process` 重启进程才生效

### Stub 区域
Area 内不传 Type-5 LSA（外部路由） ABR 下发默认路由
```
# ABR 端（连接 Area 0 和 Stub Area）
ospf 1
 area 0.0.0.1
  stub no-summary              # ABR 端：不发 Type-3 汇总
  quit

# Stub 区域内路由器
ospf 1
 area 0.0.0.1
  stub                         # 内部：声明 Stub
  quit
```
效果：Stub 区域路由器只收区域内路由 + 一条默认路由

### NSSA 区域
允许引入外部路由（Type-7 LSA） ABR 转 Type-5 传入骨干
```
# ABR 端
ospf 1
 area 0.0.0.2
  nssa no-summary              # ABR：不发 Type-3 + 下发默认路由
  quit

# NSSA 内部路由器
ospf 1
 area 0.0.0.2
  nssa                          # 内部：声明 NSSA
  quit
```

### 引入外部路由
```
ospf 1
 import-route static type 1    # 引入静态路由 type-1（开销累加）
 import-route direct           # 引入直连
 import-route rip 1            # 引入 RIP
 quit
```

### 默认路由通告
```
ospf 1
 default-route-advertise always   # 始终下发默认路由（即使本端没有）
 default-route-advertise cost 10   # 下发 + 指定开销
 quit
```

### 路由汇总（ABR 上配置）
```
ospf 1
 area 0.0.0.1
  abr-summary 192.168.0.0 255.255.252.0    # 把 Area 1 的多个网段汇总
  quit
```

### OSPF 与 BFD 联动（毫秒级切换）
```
# 1 全局启用 BFD
bfd
 quit

# 2 OSPF 进程启用 BFD
ospf 1
 bfd all-interfaces enable
 bfd all-interfaces min-tx-interval 100 min-rx-interval 100 detect-multiplier 3
 quit
```
BFD down → OSPF 立即感知邻居失效 毫秒级切换

### OSPF GR（平滑重启）
设备重启时邻居不拆除 重启后快速恢复
```
ospf 1
 graceful-restart               # 启用 GR
 graceful-restart period 600     # GR 周期（秒）
 quit
```

### OSPF IP FRR（快速重路由）
预计算备份路径 主路径故障时 50ms 内切换
```
ospf 1
 frr                            # 启用 FRR
 quit
```

### 路由过滤
```
# 过滤接收的路由
ospf 1
 filter-policy acl 2000 import
 quit

# 过滤发布的路由
ospf 1
 filter-policy acl 2001 export
 quit
```

## 命令体系

### 进程与区域
| 命令 | 视图 | 说明 |
|---|---|---|
| ospf <pid> [ router-id <rid> ] | system | 创建进程 |
| area <area-id> | ospf | 进入区域 |
| network <ip> <wildcard> | area | 宣告网段 |
| undo network <ip> <wildcard> | area | 撤销网段 |
| ospf <pid> vpn-instance <vpn> | system | VPN 实例 OSPF |

### 接口 OSPF 参数
| 命令 | 视图 | 说明 |
|---|---|---|
| ospf dr-priority <0-255> | interface | DR 优先级 默认 1 |
| ospf cost <1-65535> | interface | 接口开销 |
| ospf network-type { p2p \| broadcast \| p2mp \| nbma } | interface | 网络类型 |
| ospf timer hello <s> | interface | hello 间隔 默认 10s |
| ospf timer dead <s> | interface | dead 间隔 默认 40s |
| ospf authentication-mode { simple \| md5 \| hmac-sha256 } | interface | 接口认证 |
| ospf bfd enable | interface | 接口级 BFD |
| ospf frr block | interface | 接口禁止 FRR |

### 区域参数
| 命令 | 视图 | 说明 |
|---|---|---|
| stub [ no-summary ] | area | Stub 区域 |
| nssa [ no-summary ] | area | NSSA 区域 |
| abr-summary <ip> <mask> | area | ABR 汇总 |
| asbr-summary <ip> <mask> | ospf | ASBR 汇总 |
| import-route <protocol> [ type <1\|2> ] [ cost <n> ] [ tag <n> ] | ospf | 引入外部路由 |
| default-route-advertise [ always ] [ cost <n> ] | ospf | 通告默认路由 |
| filter-policy <acl/name> { import \| export } | ospf | 路由过滤 |
| vlink-peer <rid> | area | 虚链路 |

### 查看
| 命令 | 说明 |
|---|---|
| display ospf peer brief | 邻居概要 |
| display ospf peer | 邻居详情 |
| display ospf routing | OSPF 路由表 |
| display ospf lsdb | LSDB |
| display ospf interface | 接口 OSPF 信息 |
| display ospf error | 错误统计 |
| display ip routing-table protocol ospf | 全局路由表 OSPF 部分 |
| display ospf abr-asbr | ABR/ASBR 列表 |
| display ospf bfd session all | BFD 会话 |

## 经典 CLI 范式 交换机多区域 OSPF

来自 S 系列 CHM sec_cfg_ospf_1059

```
# Switch A（ABR 连 Area 0 + Area 1）
sysname SwitchA
vlan batch 10 20
interface Vlanif10
 ip address 192.168.0.1 255.255.255.0
interface Vlanif20
 ip address 192.168.1.1 255.255.255.0
interface GigabitEthernet 0/0/1
 port link-type trunk
 port trunk allow-pass vlan 10
interface GigabitEthernet 0/0/2
 port link-type trunk
 port trunk allow-pass vlan 20
ospf 1 router-id 10.1.1.1
 area 0.0.0.0
  network 192.168.0.0 0.0.0.255
 area 0.0.0.1
  network 192.168.1.0 0.0.0.255

# Switch B（Area 0 内部）
sysname SwitchB
vlan batch 10
interface Vlanif10
 ip address 192.168.0.2 255.255.255.0
interface GigabitEthernet 0/0/1
 port link-type trunk
 port trunk allow-pass vlan 10
ospf 1 router-id 10.2.2.2
 area 0.0.0.0
  network 192.168.0.0 0.0.0.255

# Switch C（Area 1 内部）
sysname SwitchC
vlan batch 20
interface Vlanif20
 ip address 192.168.1.2 255.255.255.0
interface GigabitEthernet 0/0/1
 port link-type trunk
 port trunk allow-pass vlan 20
ospf 1 router-id 10.3.3.3
 area 0.0.0.1
  network 192.168.1.0 0.0.0.255
```

## 常见陷阱
- 交换机忘了创建 VLAN 或没把物理口加入 VLAN → VLANIF down → OSPF 起不来
- 防火墙忘了放安全策略 → OSPF 报文被丢 → 邻居建不起来
- router-id 冲突 → 邻居异常
- 两端 hello/dead timer 不一致 → 邻居建不起来
- network 宣告网段写反掩码（如写 255.255.255.0 而非 0.0.0.255）
- Stub/NSSA 区域所有路由器必须一致配置（一端 stub 另一端没配 → 邻居拒绝）
- DR 优先级改了没重启进程 → 不生效
- VLANIF 没有 UP 的成员端口 → VLANIF 协议 down → OSPF 不生效

## 排查流程
```
OSPF 邻居建不起来
  |
  v
[1] display ospf peer brief → 有没有邻居？
  |
  ├─ 完全没有 → 走分支 A
  └─ 有但状态不是 Full → 走分支 B

分支 A 完全没有
  1. 两端 network 宣告是否正确（网段+反掩码）
  2. 接口 UP/UP？（VLANIF 有成员口？/ 物理口 up？）
  3. 防火墙：安全策略放通没？
  4. 两端区域 ID 是否一致

分支 B 非 Full 状态
  1. 2-Way → 广播网络正常（非 DR/BDR 只到 2-Way）
  2. Init → 单向 hello 检查 MTU / 认证
  3. ExStart/Exchange → MTU 不匹配 / 认证密钥错
```

## 相关文件
- references/ospf-commands.md   完整命令速查
- references/ospf-troubleshooting.md   故障排查

