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,87 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
# 帧转发决策:泛洪/转发/丢弃。
|
|
5
|
+
#
|
|
6
|
+
# 实现原理:已知单播按端口转发;未知单播/广播泛洪(除入端口);
|
|
7
|
+
# 同端口进出过滤(hairpin 丢弃);可挂 ACL 钩子接口。
|
|
8
|
+
#
|
|
9
|
+
# 用法:
|
|
10
|
+
# fwd = Forward.new
|
|
11
|
+
# fwd.decide(frame, in_port: "Gi0/1", mac_table: table, vlan_ports: [1,2,3])
|
|
12
|
+
# # => {action: :forward, ports: ["Gi0/2"]}
|
|
13
|
+
|
|
14
|
+
require_relative '../basic/packet'
|
|
15
|
+
|
|
16
|
+
class Forward
|
|
17
|
+
# 转发决策结果。
|
|
18
|
+
Decision = Data.define(:action, :ports, :reason) do
|
|
19
|
+
def to_s
|
|
20
|
+
"#{action} ports=#{ports&.inspect} (#{reason})"
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
attr_reader :acl_hooks
|
|
25
|
+
|
|
26
|
+
def initialize
|
|
27
|
+
@acl_hooks = [] # ACL 钩子链
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# 添加 ACL 钩子。
|
|
31
|
+
# 钩子签名: ->(frame, in_port) { :drop | :flood | nil }
|
|
32
|
+
def add_acl(&blk)
|
|
33
|
+
@acl_hooks << blk
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# 帧转发决策。
|
|
37
|
+
# frame: EthernetFrame
|
|
38
|
+
# in_port: 入端口
|
|
39
|
+
# mac_table: MACTable 实例(用于查目的 MAC)
|
|
40
|
+
# vlan_ports: 同 VLAN 端口列表(用于泛洪)
|
|
41
|
+
def decide(frame, in_port:, mac_table:, vlan_ports:)
|
|
42
|
+
# ACL 钩子
|
|
43
|
+
@acl_hooks.each do |hook|
|
|
44
|
+
result = hook.call(frame, in_port)
|
|
45
|
+
case result
|
|
46
|
+
when :drop
|
|
47
|
+
return Decision.new(action: :drop, ports: nil, reason: 'ACL drop')
|
|
48
|
+
when :flood
|
|
49
|
+
return Decision.new(action: :flood, ports: vlan_ports - [in_port], reason: 'ACL flood')
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# 广播帧 → 泛洪
|
|
54
|
+
if frame.broadcast?
|
|
55
|
+
ports = vlan_ports - [in_port]
|
|
56
|
+
return Decision.new(action: :flood, ports: ports, reason: 'broadcast flood')
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# 组播帧 → 泛洪(简化)
|
|
60
|
+
if frame.multicast?
|
|
61
|
+
ports = vlan_ports - [in_port]
|
|
62
|
+
return Decision.new(action: :flood, ports: ports, reason: 'multicast flood')
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# 已知单播
|
|
66
|
+
entry = mac_table.lookup(frame.dst_mac)
|
|
67
|
+
if entry
|
|
68
|
+
out_port = entry.port
|
|
69
|
+
|
|
70
|
+
# 同端口进出 → 丢弃
|
|
71
|
+
if out_port == in_port
|
|
72
|
+
return Decision.new(action: :drop, ports: nil, reason: 'hairpin drop')
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# 目标端口不在 VLAN 允许列表 → 丢弃
|
|
76
|
+
unless vlan_ports.include?(out_port)
|
|
77
|
+
return Decision.new(action: :drop, ports: nil, reason: 'port not in VLAN')
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
Decision.new(action: :forward, ports: [out_port], reason: 'known unicast')
|
|
81
|
+
else
|
|
82
|
+
# 未知单播 → 泛洪
|
|
83
|
+
ports = vlan_ports - [in_port]
|
|
84
|
+
Decision.new(action: :flood, ports: ports, reason: 'unknown unicast flood')
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
# MAC 地址表:学习/老化。
|
|
5
|
+
#
|
|
6
|
+
# 实现原理:源 MAC + 入端口映射;老化定时(按 aging_time 清除);
|
|
7
|
+
# 静态表项;查询与统计。
|
|
8
|
+
#
|
|
9
|
+
# 设计要点:
|
|
10
|
+
# - 纯内存,不感知仿真环境
|
|
11
|
+
# - 通过 age_out(now) 被动老化,不自己计时
|
|
12
|
+
#
|
|
13
|
+
# 用法:
|
|
14
|
+
# table = MACTable.new(aging_time: 300)
|
|
15
|
+
# table.learn("00:1a:2b:3c:4d:5e", port: "Gi0/1", now: 0)
|
|
16
|
+
# table.lookup("00:1a:2b:3c:4d:5e") # => {port: "Gi0/1", learned_at: 0}
|
|
17
|
+
# table.age_out(now: 350) # 老化超过 300 秒的表项
|
|
18
|
+
|
|
19
|
+
require_relative '../basic/mac_address'
|
|
20
|
+
|
|
21
|
+
class MACTable
|
|
22
|
+
DEFAULT_AGING_TIME = 300 # 默认老化时间(秒)
|
|
23
|
+
|
|
24
|
+
# MAC 表项。
|
|
25
|
+
Entry = Data.define(:mac, :port, :learned_at, :static) do
|
|
26
|
+
def to_s
|
|
27
|
+
"#{mac} -> #{port}#{static ? ' (static)' : ''}"
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
attr_reader :entries, :aging_time
|
|
32
|
+
|
|
33
|
+
def initialize(aging_time: DEFAULT_AGING_TIME)
|
|
34
|
+
@entries = {} # mac_str => Entry
|
|
35
|
+
@aging_time = aging_time
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# 学习 MAC 地址。
|
|
39
|
+
# mac: MAC 地址字符串或 MacAddress 对象
|
|
40
|
+
# port: 端口标识
|
|
41
|
+
# now: 当前虚拟时间
|
|
42
|
+
def learn(mac, port:, now:)
|
|
43
|
+
mac_str = mac.is_a?(MacAddress) ? mac.to_s : MacAddress.new(mac).to_s
|
|
44
|
+
|
|
45
|
+
# 不覆盖静态表项
|
|
46
|
+
existing = @entries[mac_str]
|
|
47
|
+
if existing&.static
|
|
48
|
+
return unless port == existing.port
|
|
49
|
+
return # 静态表项不更新
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
@entries[mac_str] = Entry.new(
|
|
53
|
+
mac: mac_str,
|
|
54
|
+
port: port,
|
|
55
|
+
learned_at: now,
|
|
56
|
+
static: false
|
|
57
|
+
)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# 添加静态表项。
|
|
61
|
+
def add_static(mac, port)
|
|
62
|
+
mac_str = mac.is_a?(MacAddress) ? mac.to_s : MacAddress.new(mac).to_s
|
|
63
|
+
@entries[mac_str] = Entry.new(
|
|
64
|
+
mac: mac_str,
|
|
65
|
+
port: port,
|
|
66
|
+
learned_at: 0,
|
|
67
|
+
static: true
|
|
68
|
+
)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# 删除静态表项。
|
|
72
|
+
def remove_static(mac)
|
|
73
|
+
mac_str = mac.is_a?(MacAddress) ? mac.to_s : MacAddress.new(mac).to_s
|
|
74
|
+
entry = @entries[mac_str]
|
|
75
|
+
@entries.delete(mac_str) if entry&.static
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# 查找 MAC 地址对应的端口。
|
|
79
|
+
def lookup(mac)
|
|
80
|
+
mac_str = mac.is_a?(MacAddress) ? mac.to_s : MacAddress.new(mac).to_s
|
|
81
|
+
@entries[mac_str]
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# 老化处理:清除超过 aging_time 的动态表项。
|
|
85
|
+
def age_out(now:)
|
|
86
|
+
@entries.reject! do |_, entry|
|
|
87
|
+
!entry.static && (now - entry.learned_at) > @aging_time
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# 按端口删除表项(端口 down 时调用)。
|
|
92
|
+
def remove_by_port(port)
|
|
93
|
+
@entries.reject! { |_, entry| entry.port == port && !entry.static }
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# 表项总数。
|
|
97
|
+
def size
|
|
98
|
+
@entries.size
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# 动态表项数。
|
|
102
|
+
def dynamic_count
|
|
103
|
+
@entries.count { |_, e| !e.static }
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# 静态表项数。
|
|
107
|
+
def static_count
|
|
108
|
+
@entries.count { |_, e| e.static }
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# 清空所有动态表项。
|
|
112
|
+
def clear_dynamic
|
|
113
|
+
@entries.reject! { |_, e| !e.static }
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# 遍历所有表项。
|
|
117
|
+
def each(&blk)
|
|
118
|
+
@entries.each_value(&blk)
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# 快照为 Hash。
|
|
122
|
+
def to_h
|
|
123
|
+
@entries.transform_values do |e|
|
|
124
|
+
{ port: e.port, learned_at: e.learned_at, static: e.static }
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
# 生成树协议(简化 STP)。
|
|
5
|
+
#
|
|
6
|
+
# 实现原理:BPDU 抽象(根桥 ID、根路径开销、发送桥 ID、端口 ID);
|
|
7
|
+
# 根桥选举;根端口/指定端口/阻塞端口角色计算;拓扑变更事件输出。
|
|
8
|
+
#
|
|
9
|
+
# 设计要点:
|
|
10
|
+
# - 纯内存,不感知仿真环境
|
|
11
|
+
# - 通过 receive_bpdu(port, bpdu) 接收 BPDU
|
|
12
|
+
# - 通过 tick(now) 推进状态机
|
|
13
|
+
#
|
|
14
|
+
# 用法:
|
|
15
|
+
# stp = STP.new(bridge_id: 4096, bridge_priority: 32768)
|
|
16
|
+
# stp.add_port("Gi0/1", cost: 20000)
|
|
17
|
+
# stp.receive_bpdu(port: "Gi0/1", bpdu: bpdu)
|
|
18
|
+
# stp.roles # => {"Gi0/1" => :root, "Gi0/2" => :designated, ...}
|
|
19
|
+
|
|
20
|
+
require 'set'
|
|
21
|
+
|
|
22
|
+
# BPDU 报文。
|
|
23
|
+
BPDU = Data.define(:root_id, :root_path_cost, :bridge_id, :port_id, :message_age, :max_age, :hello_time, :forward_delay) do
|
|
24
|
+
def to_s
|
|
25
|
+
"BPDU[root=#{root_id} cost=#{root_path_cost} bridge=#{bridge_id} port=#{port_id}]"
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
class STP
|
|
30
|
+
DEFAULT_PRIORITY = 32768
|
|
31
|
+
DEFAULT_HELLO = 2 # 秒
|
|
32
|
+
DEFAULT_FORWARD_DELAY = 15 # 秒
|
|
33
|
+
DEFAULT_MAX_AGE = 20 # 秒
|
|
34
|
+
|
|
35
|
+
# 端口状态。
|
|
36
|
+
PORT_STATES = {
|
|
37
|
+
disabled: 0,
|
|
38
|
+
blocking: 1,
|
|
39
|
+
listening: 2,
|
|
40
|
+
learning: 3,
|
|
41
|
+
forwarding: 4
|
|
42
|
+
}.freeze
|
|
43
|
+
|
|
44
|
+
attr_reader :bridge_id, :ports, :root_id, :root_path_cost
|
|
45
|
+
|
|
46
|
+
def initialize(bridge_id:, bridge_priority: DEFAULT_PRIORITY)
|
|
47
|
+
@bridge_id = bridge_id
|
|
48
|
+
@bridge_priority = bridge_priority
|
|
49
|
+
@full_bridge_id = (bridge_priority << 16) | bridge_id
|
|
50
|
+
@root_id = @full_bridge_id
|
|
51
|
+
@root_path_cost = 0
|
|
52
|
+
@ports = {} # port_name => {cost:, state:, role:, designated_root:, designated_cost:, designated_bridge:, designated_port:}
|
|
53
|
+
@current_time = 0
|
|
54
|
+
@hello_time = DEFAULT_HELLO
|
|
55
|
+
@forward_delay = DEFAULT_FORWARD_DELAY
|
|
56
|
+
@max_age = DEFAULT_MAX_AGE
|
|
57
|
+
@topology_change = false
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# 添加端口。
|
|
61
|
+
def add_port(port, cost: 20000)
|
|
62
|
+
@ports[port] = {
|
|
63
|
+
cost: cost,
|
|
64
|
+
state: :blocking,
|
|
65
|
+
role: :designated,
|
|
66
|
+
designated_root: @full_bridge_id,
|
|
67
|
+
designated_cost: 0,
|
|
68
|
+
designated_bridge: @full_bridge_id,
|
|
69
|
+
designated_port: port.object_id,
|
|
70
|
+
last_bpdu_time: 0
|
|
71
|
+
}
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# 删除端口。
|
|
75
|
+
def remove_port(port)
|
|
76
|
+
@ports.delete(port)
|
|
77
|
+
recompute_roles
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# 接收 BPDU。
|
|
81
|
+
def receive_bpdu(port:, bpdu:)
|
|
82
|
+
port_info = @ports[port]
|
|
83
|
+
return unless port_info
|
|
84
|
+
|
|
85
|
+
port_info[:last_bpdu_time] = @current_time
|
|
86
|
+
|
|
87
|
+
# 更新端口收到的 BPDU 信息
|
|
88
|
+
port_info[:designated_root] = bpdu.root_id
|
|
89
|
+
port_info[:designated_cost] = bpdu.root_path_cost
|
|
90
|
+
port_info[:designated_bridge] = bpdu.bridge_id
|
|
91
|
+
port_info[:designated_port] = bpdu.port_id
|
|
92
|
+
|
|
93
|
+
# 根桥选举
|
|
94
|
+
if bpdu.root_id < @root_id
|
|
95
|
+
@root_id = bpdu.root_id
|
|
96
|
+
@root_path_cost = bpdu.root_path_cost + port_info[:cost]
|
|
97
|
+
recompute_roles
|
|
98
|
+
elsif bpdu.root_id == @root_id
|
|
99
|
+
# 相同根桥,比较路径开销
|
|
100
|
+
new_cost = bpdu.root_path_cost + port_info[:cost]
|
|
101
|
+
if new_cost < @root_path_cost
|
|
102
|
+
@root_path_cost = new_cost
|
|
103
|
+
recompute_roles
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# 拓扑变更检测
|
|
108
|
+
@topology_change = true if bpdu.root_id != @root_id
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# 生成自身 BPDU(根桥发送)。
|
|
112
|
+
def generate_bpdu(port)
|
|
113
|
+
BPDU.new(
|
|
114
|
+
root_id: @root_id,
|
|
115
|
+
root_path_cost: @root_path_cost,
|
|
116
|
+
bridge_id: @full_bridge_id,
|
|
117
|
+
port_id: port.object_id,
|
|
118
|
+
message_age: 0,
|
|
119
|
+
max_age: @max_age,
|
|
120
|
+
hello_time: @hello_time,
|
|
121
|
+
forward_delay: @forward_delay
|
|
122
|
+
)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# 重新计算端口角色。
|
|
126
|
+
def recompute_roles
|
|
127
|
+
if @root_id == @full_bridge_id
|
|
128
|
+
# 自身是根桥:所有端口为指定端口
|
|
129
|
+
@ports.each_value do |info|
|
|
130
|
+
info[:role] = :designated
|
|
131
|
+
info[:state] = :forwarding
|
|
132
|
+
end
|
|
133
|
+
@root_path_cost = 0
|
|
134
|
+
else
|
|
135
|
+
# 非根桥:选举根端口和指定端口
|
|
136
|
+
best_root_port = nil
|
|
137
|
+
best_cost = Float::INFINITY
|
|
138
|
+
|
|
139
|
+
@ports.each do |port_name, info|
|
|
140
|
+
# 计算通过此端口到根桥的开销
|
|
141
|
+
port_cost = info[:designated_cost] + info[:cost]
|
|
142
|
+
|
|
143
|
+
if info[:designated_bridge] != @full_bridge_id
|
|
144
|
+
# 对端 BPDU 来自其他桥
|
|
145
|
+
if port_cost < best_cost || (port_cost == best_cost && best_root_port.nil?)
|
|
146
|
+
best_cost = port_cost
|
|
147
|
+
best_root_port = port_name
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
@ports.each do |port_name, info|
|
|
153
|
+
if port_name == best_root_port
|
|
154
|
+
info[:role] = :root
|
|
155
|
+
info[:state] = :forwarding
|
|
156
|
+
elsif info[:designated_bridge] == @full_bridge_id
|
|
157
|
+
# 自身是指定桥
|
|
158
|
+
info[:role] = :designated
|
|
159
|
+
info[:state] = :forwarding
|
|
160
|
+
else
|
|
161
|
+
# 比较桥 ID 确定指定端口
|
|
162
|
+
port_cost = info[:designated_cost] + info[:cost]
|
|
163
|
+
if port_cost < info[:designated_cost]
|
|
164
|
+
info[:role] = :designated
|
|
165
|
+
info[:state] = :forwarding
|
|
166
|
+
else
|
|
167
|
+
info[:role] = :blocked
|
|
168
|
+
info[:state] = :blocking
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
# 由 engine 驱动的时钟推进。
|
|
176
|
+
def tick(now:)
|
|
177
|
+
@current_time = now
|
|
178
|
+
|
|
179
|
+
# 端口状态超时检查(简化)
|
|
180
|
+
@ports.each_value do |info|
|
|
181
|
+
if (now - info[:last_bpdu_time]) > @max_age && info[:role] != :designated
|
|
182
|
+
# 超时未收到 BPDU
|
|
183
|
+
info[:role] = :designated
|
|
184
|
+
info[:state] = :forwarding
|
|
185
|
+
@topology_change = true
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
recompute_roles if @topology_change
|
|
190
|
+
@topology_change = false
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
# 端口角色映射。
|
|
194
|
+
def roles
|
|
195
|
+
@ports.transform_values { |info| info[:role] }
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
# 端口状态映射。
|
|
199
|
+
def states
|
|
200
|
+
@ports.transform_values { |info| info[:state] }
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
# 是否为根桥。
|
|
204
|
+
def root_bridge?
|
|
205
|
+
@root_id == @full_bridge_id
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
# 转发端口列表。
|
|
209
|
+
def forwarding_ports
|
|
210
|
+
@ports.select { |_, info| info[:state] == :forwarding }.keys
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
# 阻塞端口列表。
|
|
214
|
+
def blocked_ports
|
|
215
|
+
@ports.select { |_, info| info[:state] == :blocking }.keys
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
# 快照。
|
|
219
|
+
def to_h
|
|
220
|
+
{
|
|
221
|
+
bridge_id: @full_bridge_id,
|
|
222
|
+
root_id: @root_id,
|
|
223
|
+
root_path_cost: @root_path_cost,
|
|
224
|
+
root_bridge: root_bridge?,
|
|
225
|
+
ports: @ports.transform_values do |info|
|
|
226
|
+
{ role: info[:role], state: info[:state], cost: info[:cost] }
|
|
227
|
+
end
|
|
228
|
+
}
|
|
229
|
+
end
|
|
230
|
+
end
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
# 802.1Q VLAN:tag 的添加/剥离/改写。
|
|
5
|
+
#
|
|
6
|
+
# 实现原理:access 端口(PVID 打标去标)与 trunk 端口
|
|
7
|
+
# (allowed vlan 白名单、native vlan);VLAN 内洪域隔离。
|
|
8
|
+
#
|
|
9
|
+
# 用法:
|
|
10
|
+
# vlan = VLAN.new
|
|
11
|
+
# vlan.add_access_port("Gi0/1", pvid: 10)
|
|
12
|
+
# vlan.add_trunk_port("Gi0/24", allowed: [10, 20, 30], native: 1)
|
|
13
|
+
# frame = vlan.ingress(raw_frame, port: "Gi0/1") # 入口处理
|
|
14
|
+
# out_frame = vlan.egress(frame, port: "Gi0/24") # 出口处理
|
|
15
|
+
|
|
16
|
+
require_relative '../basic/packet'
|
|
17
|
+
|
|
18
|
+
class VLAN
|
|
19
|
+
# 端口配置。
|
|
20
|
+
PortConfig = Data.define(:mode, :pvid, :allowed, :native_vlan) do
|
|
21
|
+
def access?
|
|
22
|
+
mode == :access
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def trunk?
|
|
26
|
+
mode == :trunk
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
attr_reader :ports, :vlans
|
|
31
|
+
|
|
32
|
+
def initialize
|
|
33
|
+
@ports = {} # port_name => PortConfig
|
|
34
|
+
@vlans = Set.new # 所有已定义的 VLAN ID
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# 添加 access 端口。
|
|
38
|
+
def add_access_port(port, pvid:)
|
|
39
|
+
@ports[port] = PortConfig.new(mode: :access, pvid: pvid, allowed: [pvid], native_vlan: nil)
|
|
40
|
+
@vlans.add(pvid)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# 添加 trunk 端口。
|
|
44
|
+
def add_trunk_port(port, allowed:, native: nil)
|
|
45
|
+
@ports[port] = PortConfig.new(mode: :trunk, pvid: nil, allowed: allowed, native_vlan: native)
|
|
46
|
+
allowed.each { |v| @vlans.add(v) }
|
|
47
|
+
@vlans.add(native) if native
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# 删除端口。
|
|
51
|
+
def remove_port(port)
|
|
52
|
+
@ports.delete(port)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# 入口处理:根据端口模式给帧打 VLAN tag。
|
|
56
|
+
# raw_frame: 未打标的 EthernetFrame
|
|
57
|
+
# 返回打了 VLAN tag 的帧
|
|
58
|
+
def ingress(raw_frame, port:)
|
|
59
|
+
config = @ports[port]
|
|
60
|
+
return raw_frame unless config # 未配置端口,透传
|
|
61
|
+
|
|
62
|
+
if config.access?
|
|
63
|
+
# access 端口:打上 PVID
|
|
64
|
+
raw_frame.vlan_tag ? raw_frame : EthernetFrame.new(
|
|
65
|
+
dst_mac: raw_frame.dst_mac,
|
|
66
|
+
src_mac: raw_frame.src_mac,
|
|
67
|
+
vlan_tag: config.pvid,
|
|
68
|
+
payload: raw_frame.payload
|
|
69
|
+
)
|
|
70
|
+
elsif config.trunk?
|
|
71
|
+
if raw_frame.vlan_tag
|
|
72
|
+
# trunk 端口已有 tag:检查是否在白名单
|
|
73
|
+
if config.allowed.include?(raw_frame.vlan_id)
|
|
74
|
+
raw_frame
|
|
75
|
+
else
|
|
76
|
+
nil # 不在白名单,丢弃
|
|
77
|
+
end
|
|
78
|
+
elsif config.native_vlan
|
|
79
|
+
# 无 tag,走 native VLAN
|
|
80
|
+
EthernetFrame.new(
|
|
81
|
+
dst_mac: raw_frame.dst_mac,
|
|
82
|
+
src_mac: raw_frame.src_mac,
|
|
83
|
+
vlan_tag: config.native_vlan,
|
|
84
|
+
payload: raw_frame.payload
|
|
85
|
+
)
|
|
86
|
+
else
|
|
87
|
+
nil # 无 tag 且无 native VLAN,丢弃
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# 出口处理:根据端口模式去 VLAN tag。
|
|
93
|
+
def egress(frame, port:)
|
|
94
|
+
config = @ports[port]
|
|
95
|
+
return frame unless config && frame.vlan_tag
|
|
96
|
+
|
|
97
|
+
if config.access?
|
|
98
|
+
# access 端口:去 tag
|
|
99
|
+
if frame.vlan_id == config.pvid
|
|
100
|
+
EthernetFrame.new(
|
|
101
|
+
dst_mac: frame.dst_mac,
|
|
102
|
+
src_mac: frame.src_mac,
|
|
103
|
+
vlan_tag: nil,
|
|
104
|
+
payload: frame.payload
|
|
105
|
+
)
|
|
106
|
+
else
|
|
107
|
+
nil # VLAN 不匹配 PVID,丢弃
|
|
108
|
+
end
|
|
109
|
+
elsif config.trunk?
|
|
110
|
+
if config.allowed.include?(frame.vlan_id)
|
|
111
|
+
if config.native_vlan && frame.vlan_id == config.native_vlan
|
|
112
|
+
# native VLAN:去 tag
|
|
113
|
+
EthernetFrame.new(
|
|
114
|
+
dst_mac: frame.dst_mac,
|
|
115
|
+
src_mac: frame.src_mac,
|
|
116
|
+
vlan_tag: nil,
|
|
117
|
+
payload: frame.payload
|
|
118
|
+
)
|
|
119
|
+
else
|
|
120
|
+
frame # 保留 tag
|
|
121
|
+
end
|
|
122
|
+
else
|
|
123
|
+
nil # 不在白名单,丢弃
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# 获取指定 VLAN 的端口列表。
|
|
129
|
+
def ports_for_vlan(vlan_id)
|
|
130
|
+
@ports.each_with_object([]) do |(port_name, config), result|
|
|
131
|
+
if config.access? && config.pvid == vlan_id
|
|
132
|
+
result << port_name
|
|
133
|
+
elsif config.trunk? && config.allowed.include?(vlan_id)
|
|
134
|
+
result << port_name
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# 端口属于哪些 VLAN。
|
|
140
|
+
def vlans_for_port(port)
|
|
141
|
+
config = @ports[port]
|
|
142
|
+
return [] unless config
|
|
143
|
+
|
|
144
|
+
config.access? ? [config.pvid] : config.allowed
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
# 端口配置快照。
|
|
148
|
+
def to_h
|
|
149
|
+
@ports.transform_values do |c|
|
|
150
|
+
{ mode: c.mode, pvid: c.pvid, allowed: c.allowed, native: c.native_vlan }
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
require 'set'
|