network-infra-utility 0.6.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +6 -0
- data/CHANGELOG.md +31 -0
- data/GUIDE.md +201 -212
- data/README.md +1 -6
- data/Rakefile +1 -1
- data/bin/gen-get +144 -0
- data/bin/geo-api +40 -1
- data/bin/geo-get +18 -36
- data/bin/ngeo-get +253 -0
- data/bin/packbit +227 -0
- data/bin/probe +134 -0
- data/document/reference/DNS/346/212/200/346/234/257/346/226/207/346/241/243.md +912 -0
- data/network-infra-utility.gemspec +6 -4
- data/network.rb +19 -1
- data/service/geodb/GeoAPI.md +64 -0
- data/service/geodb/api.rb +77 -19
- data/service/geoquery/GeoQuery.md +309 -0
- data/service/geoquery/cache.rb +78 -0
- data/service/geoquery/geo_cache.rb +233 -0
- data/service/geoquery/geoquery.rb +327 -0
- data/service/geoquery/local.rb +151 -0
- data/service/geoquery/merge.rb +52 -0
- data/service/geoquery/normalize.rb +102 -0
- data/service/geoquery/online.rb +136 -0
- data/service/simlab/SimLab.md +140 -0
- data/service/simlab/bin/simctl +152 -0
- data/service/simlab/lib/simlab.rb +124 -0
- data/service/simlab/lib/simlab_engine.rb +146 -0
- data/service/simlab/lib/simlab_observer.rb +176 -0
- data/service/simlab/lib/simlab_router.rb +330 -0
- data/service/simlab/lib/simlab_scenario.rb +200 -0
- data/service/simlab/lib/simlab_switch.rb +182 -0
- data/service/simlab/lib/simlab_topology.rb +196 -0
- data/service/simlab/lib/simlab_traffic.rb +167 -0
- data/support/basic/packet.rb +167 -0
- data/support/routing/bgp.rb +279 -0
- data/support/routing/lpm_trie.rb +145 -0
- data/support/routing/ospf.rb +243 -0
- data/support/routing/prefix.rb +155 -0
- data/support/routing/rib.rb +167 -0
- data/support/routing/rip.rb +223 -0
- data/support/routing/static_route.rb +102 -0
- data/support/switching/frame_forward.rb +87 -0
- data/support/switching/mac_table.rb +127 -0
- data/support/switching/stp.rb +230 -0
- data/support/switching/vlan.rb +155 -0
- data/tool/packbit/README.md +136 -0
- data/tool/packbit/config/five-tuple.yml +25 -0
- data/tool/packbit/config/flow.yml +17 -0
- data/tool/packbit/config/packbit.yml +41 -0
- data/tool/packbit/erl/packbit.erl +588 -0
- data/tool/probe/README.md +88 -0
- data/tool/probe/lib/probe.rb +54 -0
- data/tool/probe/lib/probe_base.rb +98 -0
- data/tool/probe/lib/probe_dns.rb +64 -0
- data/tool/probe/lib/probe_icmp.rb +71 -0
- data/tool/probe/lib/probe_ntp.rb +51 -0
- data/tool/probe/lib/probe_radius.rb +58 -0
- data/tool/probe/lib/probe_registry.rb +44 -0
- data/tool/probe/lib/probe_result.rb +75 -0
- data/tool/probe/lib/probe_snmp.rb +114 -0
- data/tool/probe/lib/probe_tcp.rb +82 -0
- data/tool/probe/lib/probe_version.rb +7 -0
- data/tool/webssh/README.md +42 -0
- data/tool/webssh/app.py +788 -0
- data/tool/webssh/cli.bat +17 -0
- data/tool/webssh/config.yml.template +84 -0
- data/tool/webssh/extension//350/256/276/345/244/207/350/257/212/346/226/255.md +103 -0
- data/tool/webssh/requirements.txt +7 -0
- data/tool/webssh/ssh_cli.py +449 -0
- data/tool/webssh/ssh_worker.py +782 -0
- data/tool/webssh/static/vendor/xterm-addon-fit.min.js +8 -0
- data/tool/webssh/static/vendor/xterm.css +209 -0
- data/tool/webssh/static/vendor/xterm.min.js +8 -0
- data/tool/webssh/templates/index.html +2674 -0
- data/tool/webssh/web.bat +41 -0
- data/version.rb +1 -1
- metadata +87 -2
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
# 路径矢量路由协议(简化 BGP)。
|
|
5
|
+
#
|
|
6
|
+
# 实现原理:UPDATE 报文抽象(NLRI + attributes);AS_PATH 防环;
|
|
7
|
+
# LOCAL_PREF / MED 优选顺序;简单的 import/export policy;
|
|
8
|
+
# iBGP/eBGP 角色区分。
|
|
9
|
+
#
|
|
10
|
+
# 设计要点:
|
|
11
|
+
# - 纯内存,不感知仿真环境
|
|
12
|
+
# - 通过 tick(now) 被动推进
|
|
13
|
+
# - 路由出口唯一:通过 rib 参数安装路由
|
|
14
|
+
#
|
|
15
|
+
# 用法:
|
|
16
|
+
# bgp = BGP.new(as: 65001, router_id: "1.1.1.1", rib: rib)
|
|
17
|
+
# bgp.add_peer("2.2.2.2", remote_as: 65002, port: "Gi0/0")
|
|
18
|
+
# bgp.receive_update(from: "2.2.2.2", nlri: ["10.0.0.0/8"], attributes: {...})
|
|
19
|
+
# bgp.decide # => 优选路由
|
|
20
|
+
|
|
21
|
+
require 'set'
|
|
22
|
+
require_relative 'rib'
|
|
23
|
+
|
|
24
|
+
# BGP UPDATE 报文。
|
|
25
|
+
BGPUpdate = Data.define(:from, :nlri, :withdrawn, :attributes) do
|
|
26
|
+
def to_s
|
|
27
|
+
"BGP-UPDATE[from=#{from} nlri=#{nlri.size} withdrawn=#{withdrawn.size}]"
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# BGP 路由条目。
|
|
32
|
+
BGPRoute = Data.define(:prefix, :next_hop, :as_path, :local_pref, :med, :origin, :communities, :from_peer) do
|
|
33
|
+
def as_path_length
|
|
34
|
+
as_path&.size || 0
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def as_path_includes?(as_num)
|
|
38
|
+
as_path&.include?(as_num) || false
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
class BGP
|
|
43
|
+
attr_reader :as, :router_id, :peers, :routes
|
|
44
|
+
|
|
45
|
+
def initialize(as:, router_id:, rib:)
|
|
46
|
+
@as = as
|
|
47
|
+
@router_id = router_id
|
|
48
|
+
@rib = rib
|
|
49
|
+
@peers = {} # peer_addr => {remote_as:, port:, type: :ebgp/:ibgp}
|
|
50
|
+
@routes = {} # prefix_str => [BGPRoute, ...]
|
|
51
|
+
@import_policies = []
|
|
52
|
+
@export_policies = []
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# 添加 BGP 邻居。
|
|
56
|
+
# remote_as: 对端 AS 号
|
|
57
|
+
# port: 连接端口标识
|
|
58
|
+
def add_peer(addr, remote_as:, port: nil)
|
|
59
|
+
peer_type = remote_as == @as ? :ibgp : :ebgp
|
|
60
|
+
@peers[addr] = {
|
|
61
|
+
remote_as: remote_as,
|
|
62
|
+
port: port,
|
|
63
|
+
type: peer_type,
|
|
64
|
+
state: :established
|
|
65
|
+
}
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# 删除邻居。
|
|
69
|
+
def remove_peer(addr)
|
|
70
|
+
@peers.delete(addr)
|
|
71
|
+
# 撤回该邻居发来的所有路由
|
|
72
|
+
@routes.each_value do |route_list|
|
|
73
|
+
route_list.reject! { |r| r.from_peer == addr }
|
|
74
|
+
end
|
|
75
|
+
@routes.delete_if { |_, list| list.empty? }
|
|
76
|
+
reinstall_all
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# 接收 UPDATE 报文。
|
|
80
|
+
# from: peer 地址
|
|
81
|
+
# nlri: 可达前缀列表 ["10.0.0.0/8", ...]
|
|
82
|
+
# withdrawn: 撤回前缀列表
|
|
83
|
+
# attributes: {next_hop:, as_path:, local_pref:, med:, origin:, communities:}
|
|
84
|
+
def receive_update(from:, nlri: [], withdrawn: [], attributes: {})
|
|
85
|
+
peer = peers[from]
|
|
86
|
+
return unless peer
|
|
87
|
+
|
|
88
|
+
# AS_PATH 防环
|
|
89
|
+
if peer[:type] == :ebgp && attributes[:as_path]&.include?(@as)
|
|
90
|
+
# 收到包含自身 AS 的路由,丢弃
|
|
91
|
+
nlri = []
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# 处理撤回
|
|
95
|
+
withdrawn.each do |prefix|
|
|
96
|
+
@routes[prefix]&.reject! { |r| r.from_peer == from }
|
|
97
|
+
@routes.delete(prefix) if @routes[prefix]&.empty?
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# 处理新路由
|
|
101
|
+
nlri.each do |prefix|
|
|
102
|
+
# 应用 import policy
|
|
103
|
+
next unless apply_import_policy(prefix, attributes, peer)
|
|
104
|
+
|
|
105
|
+
route = BGPRoute.new(
|
|
106
|
+
prefix: prefix,
|
|
107
|
+
next_hop: attributes[:next_hop],
|
|
108
|
+
as_path: attributes[:as_path] || [],
|
|
109
|
+
local_pref: attributes[:local_pref] || 100,
|
|
110
|
+
med: attributes[:med] || 0,
|
|
111
|
+
origin: attributes[:origin] || :igp,
|
|
112
|
+
communities: attributes[:communities] || [],
|
|
113
|
+
from_peer: from
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
@routes[prefix] ||= []
|
|
117
|
+
# 移除同来源旧路由
|
|
118
|
+
@routes[prefix].reject! { |r| r.from_peer == from }
|
|
119
|
+
@routes[prefix] << route
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
reinstall_all
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# 生成发送给指定邻居的 UPDATE。
|
|
126
|
+
# 返回 {nlri:, attributes:}
|
|
127
|
+
def advertise(peer_addr)
|
|
128
|
+
peer = peers[peer_addr]
|
|
129
|
+
return nil unless peer
|
|
130
|
+
|
|
131
|
+
nlri = []
|
|
132
|
+
@routes.each do |prefix, route_list|
|
|
133
|
+
best = decide_for_prefix(prefix)
|
|
134
|
+
next unless best
|
|
135
|
+
|
|
136
|
+
# iBGP 不转发从 iBGP 学到的路由(简化规则)
|
|
137
|
+
if peer[:type] == :ibgp
|
|
138
|
+
source_peer = peers[best.from_peer]
|
|
139
|
+
next if source_peer && source_peer[:type] == :ibgp
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# 应用 export policy
|
|
143
|
+
next unless apply_export_policy(prefix, best, peer)
|
|
144
|
+
|
|
145
|
+
nlri << prefix
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
return nil if nlri.empty?
|
|
149
|
+
|
|
150
|
+
# 构造属性
|
|
151
|
+
{
|
|
152
|
+
nlri: nlri,
|
|
153
|
+
attributes: {
|
|
154
|
+
next_hop: '0.0.0.0', # 简化:用自身
|
|
155
|
+
as_path: [@as], # 简化
|
|
156
|
+
local_pref: 100,
|
|
157
|
+
med: 0,
|
|
158
|
+
origin: :igp
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
# BGP 路由优选决策。
|
|
164
|
+
# 顺序:LOCAL_PREF > AS_PATH 长度 > ORIGIN > MED > eBGP > 下一跳
|
|
165
|
+
def decide
|
|
166
|
+
result = {}
|
|
167
|
+
@routes.each do |prefix, route_list|
|
|
168
|
+
best = decide_for_prefix(prefix)
|
|
169
|
+
result[prefix] = best if best
|
|
170
|
+
end
|
|
171
|
+
result
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
# 单个前缀的优选。
|
|
175
|
+
def decide_for_prefix(prefix)
|
|
176
|
+
routes = @routes[prefix]
|
|
177
|
+
return nil if routes.nil? || routes.empty?
|
|
178
|
+
|
|
179
|
+
routes.min do |a, b|
|
|
180
|
+
# 1. LOCAL_PREF(大优)
|
|
181
|
+
cmp = b.local_pref <=> a.local_pref
|
|
182
|
+
next cmp if cmp != 0
|
|
183
|
+
# 2. AS_PATH 长度(短优)
|
|
184
|
+
cmp = a.as_path_length <=> b.as_path_length
|
|
185
|
+
next cmp if cmp != 0
|
|
186
|
+
# 3. ORIGIN(IGP < EGP < INCOMPLETE)
|
|
187
|
+
origin_order = { igp: 0, egp: 1, incomplete: 2 }
|
|
188
|
+
cmp = origin_order[a.origin] <=> origin_order[b.origin]
|
|
189
|
+
next cmp if cmp != 0
|
|
190
|
+
# 4. MED(小优)
|
|
191
|
+
cmp = a.med <=> b.med
|
|
192
|
+
next cmp if cmp != 0
|
|
193
|
+
# 5. eBGP 优先于 iBGP
|
|
194
|
+
a_type = peers.dig(a.from_peer, :type) || :ibgp
|
|
195
|
+
b_type = peers.dig(b.from_peer, :type) || :ibgp
|
|
196
|
+
type_order = { ebgp: 0, ibgp: 1 }
|
|
197
|
+
cmp = type_order[a_type] <=> type_order[b_type]
|
|
198
|
+
next cmp if cmp != 0
|
|
199
|
+
# 6. 下一跳(更小的 IP 优先,简化用字符串比较)
|
|
200
|
+
a.next_hop <=> b.next_hop
|
|
201
|
+
end
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
# 由 engine 驱动的时钟推进。
|
|
205
|
+
def tick(now:)
|
|
206
|
+
# BGP 主要是事件驱动,tick 用于 keepalive 等
|
|
207
|
+
# 简化:不做实际操作
|
|
208
|
+
false
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
# 添加 import policy 钩子。
|
|
212
|
+
def add_import_policy(&blk)
|
|
213
|
+
@import_policies << blk
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
# 添加 export policy 钩子。
|
|
217
|
+
def add_export_policy(&blk)
|
|
218
|
+
@export_policies << blk
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
# 注入本地路由到 BGP(network 命令)。
|
|
222
|
+
def announce_network(prefix, next_hop: '0.0.0.0')
|
|
223
|
+
@routes[prefix] ||= []
|
|
224
|
+
@routes[prefix].reject! { |r| r.from_peer == :local }
|
|
225
|
+
@routes[prefix] << BGPRoute.new(
|
|
226
|
+
prefix: prefix,
|
|
227
|
+
next_hop: next_hop,
|
|
228
|
+
as_path: [],
|
|
229
|
+
local_pref: 100,
|
|
230
|
+
med: 0,
|
|
231
|
+
origin: :igp,
|
|
232
|
+
communities: [],
|
|
233
|
+
from_peer: :local
|
|
234
|
+
)
|
|
235
|
+
reinstall_all
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
# BGP 表快照。
|
|
239
|
+
def to_h
|
|
240
|
+
@routes.transform_values do |list|
|
|
241
|
+
list.map { |r| { next_hop: r.next_hop, as_path: r.as_path, from: r.from_peer, local_pref: r.local_pref } }
|
|
242
|
+
end
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
private
|
|
246
|
+
|
|
247
|
+
def apply_import_policy(prefix, attributes, peer)
|
|
248
|
+
@import_policies.each do |policy|
|
|
249
|
+
result = policy.call(prefix, attributes, peer)
|
|
250
|
+
return false if result == :reject
|
|
251
|
+
end
|
|
252
|
+
true
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
def apply_export_policy(prefix, route, peer)
|
|
256
|
+
@export_policies.each do |policy|
|
|
257
|
+
result = policy.call(prefix, route, peer)
|
|
258
|
+
return false if result == :reject
|
|
259
|
+
end
|
|
260
|
+
true
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
def reinstall_all
|
|
264
|
+
# 先清除所有 BGP 路由
|
|
265
|
+
rib_prefixes = @routes.keys
|
|
266
|
+
rib_prefixes.each { |p| @rib.delete(p, protocol: :bgp) }
|
|
267
|
+
|
|
268
|
+
# 重新安装优选路由
|
|
269
|
+
decide.each do |prefix, best|
|
|
270
|
+
@rib.add(
|
|
271
|
+
prefix: prefix,
|
|
272
|
+
next_hop: best.next_hop,
|
|
273
|
+
ifindex: peers.dig(best.from_peer, :port),
|
|
274
|
+
protocol: :bgp,
|
|
275
|
+
metric: best.as_path_length
|
|
276
|
+
)
|
|
277
|
+
end
|
|
278
|
+
end
|
|
279
|
+
end
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
# 最长前缀匹配(Longest Prefix Match)Trie。
|
|
5
|
+
#
|
|
6
|
+
# 基于二叉 Trie 实现,支持 IPv4/IPv6 统一位串。
|
|
7
|
+
# 查找复杂度 O(前缀长)。
|
|
8
|
+
#
|
|
9
|
+
# 用法:
|
|
10
|
+
# trie = LpmTrie.new
|
|
11
|
+
# trie.insert("10.0.0.0/8", data1)
|
|
12
|
+
# trie.insert("10.1.0.0/16", data2)
|
|
13
|
+
# trie.match(IPv4.new("10.1.1.1")) # => data2 (最长前缀匹配)
|
|
14
|
+
# trie.match("10.2.0.1") # => data1
|
|
15
|
+
|
|
16
|
+
require_relative '../basic/ip'
|
|
17
|
+
|
|
18
|
+
class LpmTrie
|
|
19
|
+
Node = Struct.new(:left, :right, :data) do
|
|
20
|
+
def initialize
|
|
21
|
+
self.left = nil
|
|
22
|
+
self.right = nil
|
|
23
|
+
self.data = nil
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
attr_reader :root, :count
|
|
28
|
+
|
|
29
|
+
def initialize
|
|
30
|
+
@root = Node.new
|
|
31
|
+
@count = 0
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# 插入前缀与关联数据。prefix 为 CIDR 字符串。
|
|
35
|
+
def insert(prefix, data)
|
|
36
|
+
bits, len = to_bits(prefix)
|
|
37
|
+
node = @root
|
|
38
|
+
|
|
39
|
+
len.times do |i|
|
|
40
|
+
bit = bits[i]
|
|
41
|
+
child = bit == 0 ? node.left : node.right
|
|
42
|
+
if child.nil?
|
|
43
|
+
child = Node.new
|
|
44
|
+
bit == 0 ? node.left = child : node.right = child
|
|
45
|
+
end
|
|
46
|
+
node = child
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
@count += 1 if node.data.nil?
|
|
50
|
+
node.data = data
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# 最长前缀匹配。ip 为 IPv4/IPv6/字符串。
|
|
54
|
+
def match(ip)
|
|
55
|
+
bits = ip_to_bits(ip)
|
|
56
|
+
node = @root
|
|
57
|
+
best = node.data # 检查根节点(处理默认路由 /0)
|
|
58
|
+
|
|
59
|
+
bits.each do |bit|
|
|
60
|
+
child = bit == 0 ? node.left : node.right
|
|
61
|
+
break if child.nil?
|
|
62
|
+
|
|
63
|
+
node = child
|
|
64
|
+
best = node.data if node.data
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
best
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# 删除指定前缀。
|
|
71
|
+
def delete(prefix)
|
|
72
|
+
bits, len = to_bits(prefix)
|
|
73
|
+
node = @root
|
|
74
|
+
|
|
75
|
+
path = []
|
|
76
|
+
len.times do |i|
|
|
77
|
+
bit = bits[i]
|
|
78
|
+
child = bit == 0 ? node.left : node.right
|
|
79
|
+
return false unless child
|
|
80
|
+
|
|
81
|
+
path << [node, bit]
|
|
82
|
+
node = child
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
return false unless node.data
|
|
86
|
+
|
|
87
|
+
node.data = nil
|
|
88
|
+
@count -= 1
|
|
89
|
+
|
|
90
|
+
# 清理空叶子节点
|
|
91
|
+
path.reverse_each do |parent_node, bit|
|
|
92
|
+
child = bit == 0 ? parent_node.left : parent_node.right
|
|
93
|
+
next unless child && child.data.nil? && child.left.nil? && child.right.nil?
|
|
94
|
+
|
|
95
|
+
bit == 0 ? parent_node.left = nil : parent_node.right = nil
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
true
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def empty?
|
|
102
|
+
@count.zero?
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# 遍历所有有数据的节点。
|
|
106
|
+
def each(&blk)
|
|
107
|
+
return to_enum(:each) unless block_given?
|
|
108
|
+
|
|
109
|
+
traverse(@root, '', &blk)
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
private
|
|
113
|
+
|
|
114
|
+
def traverse(node, path, &blk)
|
|
115
|
+
yield [path, node.data] if node.data
|
|
116
|
+
traverse(node.left, path + '0', &blk) if node.left
|
|
117
|
+
traverse(node.right, path + '1', &blk) if node.right
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def to_bits(prefix)
|
|
121
|
+
if prefix.is_a?(Prefix)
|
|
122
|
+
ip_to_bits(prefix.network) # 取前 len 位
|
|
123
|
+
else
|
|
124
|
+
addr_str, len_str = prefix.split('/')
|
|
125
|
+
len = len_str.to_i
|
|
126
|
+
ip = addr_str.include?(':') ? IPv6.new(addr_str) : IPv4.new(addr_str)
|
|
127
|
+
[ip_to_bits(ip).first(len), len]
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def ip_to_bits(ip)
|
|
132
|
+
case ip
|
|
133
|
+
when IPv4
|
|
134
|
+
ip.to_b.gsub('.', '').chars.map(&:to_i)
|
|
135
|
+
when IPv6
|
|
136
|
+
nums = ip.numbers
|
|
137
|
+
nums.flat_map { |n| '%08b' % n }.chars.map(&:to_i)
|
|
138
|
+
when String
|
|
139
|
+
ip_obj = ip.include?(':') ? IPv6.new(ip) : IPv4.new(ip)
|
|
140
|
+
ip_to_bits(ip_obj)
|
|
141
|
+
else
|
|
142
|
+
raise ArgumentError, "Cannot convert #{ip.class} to bits"
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
end
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
# 链路状态路由协议(简化 OSPF)。
|
|
5
|
+
#
|
|
6
|
+
# 实现原理:邻接与 LSA 抽象(简化:不实现真实 DR/BDR 选举,但保留接口);
|
|
7
|
+
# LSA 泛洪(收到新 LSA 转发给除来源外邻居);Dijkstra SPF 计算最短路径树并生成路由;
|
|
8
|
+
# LSA 序列号防旧。
|
|
9
|
+
#
|
|
10
|
+
# 设计要点:
|
|
11
|
+
# - 纯内存,不感知仿真环境
|
|
12
|
+
# - 通过 tick(now) 被动推进
|
|
13
|
+
# - 路由出口唯一:通过 rib 参数安装路由
|
|
14
|
+
#
|
|
15
|
+
# 用法:
|
|
16
|
+
# ospf = OSPF.new(router_id: "1.1.1.1", rib: rib)
|
|
17
|
+
# ospf.add_neighbor("2.2.2.2", port: "Gi0/0", cost: 10)
|
|
18
|
+
# ospf.receive_lsa(port: "Gi0/0", lsa: lsa)
|
|
19
|
+
# ospf.spf # => 计算最短路径并安装到 RIB
|
|
20
|
+
|
|
21
|
+
require 'set'
|
|
22
|
+
require_relative 'rib'
|
|
23
|
+
|
|
24
|
+
# 链路状态通告(LSA)。
|
|
25
|
+
LSA = Data.define(:router_id, :seq, :links, :age, :area) do
|
|
26
|
+
def to_s
|
|
27
|
+
"LSA[router=#{router_id} seq=#{seq} links=#{links.size} age=#{age}]"
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
alias inspect to_s
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# 链路描述:router_id => cost
|
|
34
|
+
# links: [{router_id:, cost:, prefix:, prefix_cost:}]
|
|
35
|
+
|
|
36
|
+
class OSPF
|
|
37
|
+
LSA_MAX_AGE = 3600
|
|
38
|
+
SPF_DELAY = 5 # SPF 延迟(秒)
|
|
39
|
+
LSA_REFRESH = 1800 # LSA 刷新间隔
|
|
40
|
+
|
|
41
|
+
attr_reader :router_id, :neighbors, :lsdb, :area
|
|
42
|
+
|
|
43
|
+
def initialize(router_id:, rib:, area: "0.0.0.0")
|
|
44
|
+
@router_id = router_id
|
|
45
|
+
@rib = rib
|
|
46
|
+
@area = area
|
|
47
|
+
@neighbors = {} # router_id => {port:, cost:, state:}
|
|
48
|
+
@lsdb = {} # router_id => LSA
|
|
49
|
+
@seq_num = 0
|
|
50
|
+
@last_spf = 0
|
|
51
|
+
@current_time = 0
|
|
52
|
+
@spf_pending = false
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# 添加邻居。
|
|
56
|
+
def add_neighbor(router_id, port:, cost: 10)
|
|
57
|
+
@neighbors[router_id] = { port: port, cost: cost, state: :full }
|
|
58
|
+
# 自身的 LSA 也会变化
|
|
59
|
+
originate_lsa
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# 删除邻居(链路 down)。
|
|
63
|
+
def remove_neighbor(router_id)
|
|
64
|
+
@neighbors.delete(router_id)
|
|
65
|
+
originate_lsa
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# 接收 LSA。
|
|
69
|
+
# port: 接收端口
|
|
70
|
+
# lsa: LSA 对象
|
|
71
|
+
# 返回需要泛洪给其他邻居的 LSA(如果 LSA 是新的或更新的)
|
|
72
|
+
def receive_lsa(port:, lsa:)
|
|
73
|
+
existing = @lsdb[lsa.router_id]
|
|
74
|
+
|
|
75
|
+
# 序列号防旧:只接受更新的 LSA
|
|
76
|
+
if existing && lsa.seq <= existing.seq
|
|
77
|
+
return nil
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
@lsdb[lsa.router_id] = lsa
|
|
81
|
+
@spf_pending = true
|
|
82
|
+
|
|
83
|
+
# 标记需要泛洪给除来源外的邻居
|
|
84
|
+
lsa
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# 泛洪 LSA 给除来源端口外的所有邻居。
|
|
88
|
+
# 返回 { neighbor_router_id => port } 表示需要发送的邻居列表。
|
|
89
|
+
def flood_targets(except_port:)
|
|
90
|
+
neighbors.each_with_object({}) do |(rid, info), hash|
|
|
91
|
+
next if info[:port] == except_port
|
|
92
|
+
hash[rid] = info[:port]
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# 生成自身的 LSA。
|
|
97
|
+
def originate_lsa
|
|
98
|
+
@seq_num += 1
|
|
99
|
+
links = neighbors.map do |rid, info|
|
|
100
|
+
{ router_id: rid, cost: info[:cost] }
|
|
101
|
+
end
|
|
102
|
+
# 加入直连前缀
|
|
103
|
+
@connected_prefixes&.each do |prefix, port_info|
|
|
104
|
+
links << { prefix: prefix, prefix_cost: port_info[:cost] || 1 }
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
lsa = LSA.new(
|
|
108
|
+
router_id: @router_id,
|
|
109
|
+
seq: @seq_num,
|
|
110
|
+
links: links,
|
|
111
|
+
age: 0,
|
|
112
|
+
area: @area
|
|
113
|
+
)
|
|
114
|
+
@lsdb[@router_id] = lsa
|
|
115
|
+
lsa
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# Dijkstra SPF 计算。
|
|
119
|
+
# 返回 { destination_router_id => {cost:, next_hop:} }
|
|
120
|
+
def dijkstra
|
|
121
|
+
return {} unless @lsdb[@router_id]
|
|
122
|
+
|
|
123
|
+
dist = {} # router_id => cost
|
|
124
|
+
prev = {} # router_id => prev_router_id
|
|
125
|
+
visited = Set.new
|
|
126
|
+
|
|
127
|
+
dist[@router_id] = 0
|
|
128
|
+
# 下一跳映射
|
|
129
|
+
first_hop = {} # router_id => first_hop_router_id
|
|
130
|
+
|
|
131
|
+
queue = [[0, @router_id]]
|
|
132
|
+
|
|
133
|
+
until queue.empty?
|
|
134
|
+
queue.sort_by! { |c, _| c }
|
|
135
|
+
cost, node = queue.shift
|
|
136
|
+
next if visited.include?(node)
|
|
137
|
+
|
|
138
|
+
visited.add(node)
|
|
139
|
+
|
|
140
|
+
lsa = @lsdb[node]
|
|
141
|
+
next unless lsa
|
|
142
|
+
|
|
143
|
+
lsa.links.each do |link|
|
|
144
|
+
next unless link[:router_id] # 只处理路由器间链路
|
|
145
|
+
next if visited.include?(link[:router_id])
|
|
146
|
+
|
|
147
|
+
new_cost = cost + link[:cost]
|
|
148
|
+
if dist[link[:router_id]].nil? || new_cost < dist[link[:router_id]]
|
|
149
|
+
dist[link[:router_id]] = new_cost
|
|
150
|
+
prev[link[:router_id]] = node
|
|
151
|
+
# 记录第一跳
|
|
152
|
+
if node == @router_id
|
|
153
|
+
first_hop[link[:router_id]] = link[:router_id]
|
|
154
|
+
else
|
|
155
|
+
first_hop[link[:router_id]] = first_hop[node]
|
|
156
|
+
end
|
|
157
|
+
queue << [new_cost, link[:router_id]]
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
{ distances: dist, previous: prev, first_hops: first_hop }
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
# 执行 SPF 并安装路由到 RIB。
|
|
166
|
+
# 返回安装的路由列表。
|
|
167
|
+
def spf
|
|
168
|
+
result = dijkstra
|
|
169
|
+
installed = []
|
|
170
|
+
|
|
171
|
+
result[:distances].each do |dest_rid, cost|
|
|
172
|
+
next if dest_rid == @router_id
|
|
173
|
+
next unless @lsdb[dest_rid]
|
|
174
|
+
|
|
175
|
+
# 从目的路由器的 LSA 中提取前缀
|
|
176
|
+
@lsdb[dest_rid].links.each do |link|
|
|
177
|
+
next unless link[:prefix]
|
|
178
|
+
|
|
179
|
+
next_hop_rid = result[:first_hops][dest_rid]
|
|
180
|
+
next unless next_hop_rid
|
|
181
|
+
next unless neighbors[next_hop_rid]
|
|
182
|
+
|
|
183
|
+
installed_prefix = link[:prefix]
|
|
184
|
+
@rib.add(
|
|
185
|
+
prefix: installed_prefix,
|
|
186
|
+
next_hop: neighbors[next_hop_rid][:port], # 实际应是对端 IP,这里用端口标识
|
|
187
|
+
ifindex: neighbors[next_hop_rid][:port],
|
|
188
|
+
protocol: :ospf,
|
|
189
|
+
metric: cost + (link[:prefix_cost] || 1)
|
|
190
|
+
)
|
|
191
|
+
installed << installed_prefix
|
|
192
|
+
end
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
installed
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
# 由 engine 驱动的时钟推进。
|
|
199
|
+
def tick(now:)
|
|
200
|
+
@current_time = now
|
|
201
|
+
|
|
202
|
+
# LSA 老化
|
|
203
|
+
@lsdb.each_value do |lsa|
|
|
204
|
+
# 在真实 OSPF 中 LSA age 会增长,这里简化
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
# SPF 延迟执行
|
|
208
|
+
if @spf_pending && (now - @last_spf) >= SPF_DELAY
|
|
209
|
+
@last_spf = now
|
|
210
|
+
@spf_pending = false
|
|
211
|
+
spf
|
|
212
|
+
true
|
|
213
|
+
else
|
|
214
|
+
false
|
|
215
|
+
end
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
# 添加直连前缀。
|
|
219
|
+
def add_connected(prefix, port, cost: 1)
|
|
220
|
+
@connected_prefixes ||= {}
|
|
221
|
+
@connected_prefixes[prefix] = { port: port, cost: cost }
|
|
222
|
+
@rib.add(
|
|
223
|
+
prefix: prefix,
|
|
224
|
+
next_hop: '0.0.0.0',
|
|
225
|
+
ifindex: port,
|
|
226
|
+
protocol: :connected,
|
|
227
|
+
metric: 0
|
|
228
|
+
)
|
|
229
|
+
originate_lsa
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
# LSDB 快照。
|
|
233
|
+
def lsdb_dump
|
|
234
|
+
@lsdb.transform_values do |lsa|
|
|
235
|
+
{ seq: lsa.seq, links: lsa.links, age: lsa.age }
|
|
236
|
+
end
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
# 获取自身最新 LSA。
|
|
240
|
+
def my_lsa
|
|
241
|
+
@lsdb[@router_id]
|
|
242
|
+
end
|
|
243
|
+
end
|