用途
本技能面向 MikroTik RouterOS 7 的 IP 路由配置,讲解路由表与路由选择机制,以及静态路由、策略路由、VRF、路由过滤器等内容。OSPF、BGP 和 MPLS 有各自独立的技能(routeros-ospf、routeros-bgp、routeros-mpls),本技能覆盖它们共用的部分。
路由表与路由选择
在 RouterOS 7 中,路由存放在路由表里,main 是默认表。路由器构建 RIB(所有候选路由),为每个目的地选出最佳路由(可达候选中 distance 最低者),并将其安装到用于转发的 FIB 中。
静态路由与网关检测
/ip/route/print where dst-address=0.0.0.0/0
/ip/route/add dst-address=0.0.0.0/0 gateway=192.0.2.1 distance=1 check-gateway=ping comment=primary
/ip/route/add dst-address=0.0.0.0/0 gateway=198.51.100.1 distance=2 comment=backup
/ip/route/add dst-address=203.0.113.0/24 blackhole
一条路由只有在启用且网关可达时才是候选路由。gateway 可以是地址、接口名或 address%interface。gateway=192.0.2.1@main 表示在另一个表中解析网关。
故障切换通过 distance 实现:当首选路由变为非活动时使用备份路由。以太网链路上的下一跳失效,若不使用 check-gateway(ping、arp 或 bfd;每 10 秒探测一次,两次超时后认为网关 down)则无法检测。另见 routeros-high-availability。
相同 distance 的等价路由构成 ECMP 组(标记 +);哈希默认使用三层,可在 /ip/settings 和 /ipv6/settings 中改为四层或内层三层。
连接路由由 /ip/address 创建,其首选源地址从出接口选择。
scope 和 target-scope 限制哪些路由可以解析网关(scope 高于 target-scope 的路由不被使用)。默认只允许连接路由,iBGP 例外,它还可以通过 IGP 和静态路由解析。实际值可在 /routing/nexthop 中查看。在单条路由上修改它们不会影响其他路由,因为它们属于 nexthop 对象。
切勿删除或替换你正在使用的会话所依赖的默认路由;应先添加新路由、测试,再删除旧路由。
多路由表、规则与策略路由
引用表之前先创建表,若需安装路由用于转发则加 fib。然后用路由规则或 mangle 引导流量,不要同时使用两者:
/routing/table/add name=wan2 fib
/ip/route/add dst-address=0.0.0.0/0 gateway=198.51.100.1 routing-table=wan2
/routing/rule/add src-address=192.168.88.50/32 action=lookup table=wan2
/routing/rule/add dst-address=8.8.8.8 action=lookup-only-in-table table=wan2
lookup 在表中无匹配时回退到 main,lookup-only-in-table 不回退(网关失败时目的地变为不可达),drop 和 unreachable 则丢弃。
Mangle(mark-routing)在路由规则之前运行,两者都匹配时 mangle 优先。若 mangle 规则同时标记了发往路由器自身地址的流量,会破坏本地访问:可添加 !dst-address-type=local,或在 /routing/rule 中把 action=mangle 规则移到 lookup table=local 之下。
最多支持 4096 张表。规则取代了 v6 的 /ip route rule 菜单。
多 WAN 负载均衡与故障切换模式见 routeros-high-availability(PCC 与 WAN 备份)。
VRF
VRF 在 /ip/vrf 中创建,包含一组接口;每个活动 VRF 会获得一个同名路由表。条目像防火墙规则一样自上而下匹配,内置的 main VRF 匹配所有接口,因此要把你的 VRF 移到它上面(move 1 0)。接口的连接路由会落入该 VRF 表。服务只有在支持 VRF 且你进行了配置时才会使用它(例如 SSH vrf=、WireGuard vrf=)。路由泄露、VPNv4 和路由目标参见 references/guides/user-guides/routing-and-networking-protocols/vrf.md 和 route-distinguisher-and-route-target.md。
路由过滤器(v7 语法)
过滤器是命名链中的类脚本规则;协议将链引用为 input 或 output 过滤器。链末尾的默认动作是 reject,因此有用的链应以显式 accept 结尾。
/routing/filter/rule/add chain=ospf_in rule="if (dst==0.0.0.0/0 && protocol static) { accept }"
/routing/filter/rule/add chain=bgp_out rule="if (dst-len>13 && dst-len<31 && dst in 172.16.0.0/16) { set bgp-med 20; set bgp-path-prepend 2; accept }"
只读属性(dst、dst-len、protocol、bgp-as-path、rpki、ospf-type、origin 等)只能匹配;可写属性(distance、bgp-med、bgp-local-pref、bgp-path-prepend、communities 等)还可以设置。v6 的前缀列表已移除。完整属性表见 references/guides/user-guides/routing-and-networking-protocols/route-selection-and-filtering.md。在生产 BGP 会话上使用前,请在实验环境或用 /routing/filter/test-as-path-regexp 测试过滤器。
本技能涉及的其他协议
RIP:仅 v2。先 /routing/rip/instance/add name=r1 redistribute=connected,static,再 /routing/rip/interface-template/add instance=r1 interfaces=ether1(v7 中没有 network 语句;只有存在可交换的路由时邻居才会出现)。
IS-IS:/routing/isis/instance/add name=i1 afi=ip areas=49.2222 system-id=1111.2222.cded 和 /routing/isis/interface-template/add instance=i1 interfaces=ether1 levels=l1,l2。
BFD:/routing/bfd/configuration 选择接口或地址;按协议启用 use-bfd=yes(BGP 连接、OSPF 接口模板),并查看 /routing/bfd/session。
组播:IGMP 代理(/routing/igmp-proxy/interface,其中一个 upstream=yes)、PIM-SM(/routing/pimsm,v7 中无独立软件包)。
Router ID:/routing/id 为所有协议选举或设置它。
多核:BGP 和路由表更新并行运行;见 routing-protocol-multi-core-support.md。
从 v6 到 v7
表必须先存在于 /routing/table 才能使用;/ip route rule 变为 /routing/rule;Rules、VRF 和 Nexthop 选项卡已从 IP route 菜单移出(/routing/rule、/ip/vrf、/routing/nexthop);BGP、OSPF 和过滤器被重新设计(见各自技能);PIM-SM 配置不会转换。完整示例和 nexthop 查找变化见 moving-from-rosv6-to-rosv7.md。
查阅位置
| 需求 | 打开 |
|---|---|
| RIB、FIB、ECMP、路由选择、scope、硬件卸载 | references/help/ip-routing.md 和 references/guides/user-guides/routing-and-networking-protocols/routing-decision.md |
| 使用表和规则的策略路由 | references/guides/user-guides/routing-and-networking-protocols/policy-routing.md |
| 按协议划分的路由功能状态概览 | references/help/routing-protocol-overview.md |
| 过滤器 | references/guides/user-guides/routing-and-networking-protocols/route-selection-and-filtering.md |
| /ip/route、/routing/* 的属性 | references/cli/ip/route.md、references/cli/routing/ |
| 其他 | references/INDEX.md |
相关技能:routeros-ospf、routeros-bgp、routeros-mpls、routeros-firewall(mangle)、routeros-high-availability、routeros-ip-addressing、routeros-vpn。