8.2 Ethernet switching、VLAN 与环路防护
驿站内部新接入了一排交换机,广播开始绕圈,原本互相隔离的队伍也出现在同一网络里。
Ethernet switch 根据 destination MAC 在同一 VLAN/broadcast domain 中转发 frame,同时根据收到 frame 的 source MAC 学习 forwarding database(FDB)。这两句话是分析大部分 L2 behavior 的起点。
1. Switch 学 source,查 destination
Frame 从 port 3、VLAN 10 进入,source MAC 为 A,switch 将 (VLAN 10, A) -> port 3 写入/refresh FDB。然后查 destination MAC B:
- Known unicast:B 在 FDB 中,向对应 egress port 转发;
- Unknown unicast:在该 VLAN 内向除 ingress port 外的 eligible ports flood;
- Broadcast:在该 VLAN 内 flood;
- Multicast:取决于 multicast snooping/registration 状态,无相关状态时可能按 VLAN flood。
FDB entry 会 aging。同一 source MAC 从新 port 到达时,switch 通常更新 mapping;快速来回移动可显示为 MAC flapping,常见 root cause 包括 loop、错误 bond/bridge 或 duplicate address。
# Linux bridge FDB
bridge fdb show2. Collision domain 与 broadcast domain
Hub 是 physical-layer repeater,所有 port 共享 medium/collision domain。Switch 的每个 full-duplex point-to-point port 可独立收发,不使用 CSMA/CD 竞争共享介质。
Switch 默认不切分 broadcast domain:属于同一 VLAN 的 broadcast 会被 flood。VLAN 将一套 physical switching infrastructure 分成多个 logical broadcast domain,FDB key 也应理解为 (VLAN, MAC),不是全 switch 只按 MAC 一份 mapping。
3. 802.1Q tag
Untagged Ethernet II header:
Destination | Source | EtherType | Payload802.1Q-tagged header:
Destination | Source | TPID 0x8100 | TCI | inner EtherType | PayloadTag 插入 4 bytes。TCI 包含 priority code point(PCP)、drop eligible indicator(DEI)和 12-bit VLAN ID。常见 maximum tagged frame 因此从 1518 增为 1522 bytes(仍不含 preamble/SFD)。Provider bridging/QinQ 可有多层 tag,不应把 parser 写成“EtherType 永远在 offset 12 并且只有一个 tag”。
Access/trunk 是 switch configuration concept,不是 Ethernet frame 自带的 port type:
- Access port 通常将 untagged host traffic 归入一个 VLAN,egress 去 tag;
- Trunk port 通常允许一组 tagged VLAN 通过;
- Native/PVID/untagged VLAN 行为因 vendor/configuration 而异,两端不一致会导致 leakage 或 connectivity failure;
- Allowed VLAN list 应最小化,不要默认所有 VLAN 通过 trunk。
4. Inter-VLAN communication 是 routing
VLAN 10 host 要访问 VLAN 20 host,source host 将 packet 发给 VLAN 10 中的 default gateway MAC。Router/L3 switch 在 IP layer 选 route,然后在 VLAN 20 上封装新 Ethernet frame。
VLAN 提供 L2 broadcast separation,不自动提供 application authorization 或完整 security boundary。Inter-VLAN ACL/firewall、host security、802.1X/NAC 和 management-plane protection 仍需要独立设计。VLAN hopping 与 mis-tagging 也使“在不同 VLAN 就绝对安全”不成立。
5. L2 loop 为什么会放大
Ethernet frame 没有像 IP TTL/Hop Limit 那样每转发一次就递减的 loop limit。一个 broadcast/unknown-unicast frame 在 redundant L2 links 中循环时,switch 可不断 flood duplicate,占满 link/control-plane resource,同时让 FDB 观测到 source MAC 在多个 port 间来回移动。
Physical redundancy 如果没有 loop-prevention / multipath mechanism,会破坏 logical topology。单纯把两根 cable 同时接上不等于安全获得两倍 bandwidth;需要 STP/RSTP、link aggregation 或 routed fabric 等明确 mechanism。
6. STP/RSTP 的目标是建立 loop-free active topology
STP family 通过 Bridge Protocol Data Unit(BPDU)选 root bridge,计算到 root 的 path cost,为 port 分配 role/state,让某些 redundant path 不转发 ordinary data frame。Topology 变化后 protocol 重新 convergence,恢复一条 loop-free path。
Classic 802.1D STP 的 default timer 可造成数十秒 convergence,但“固定 50 秒”不是所有 topology/event/configuration 的保证。RSTP 改进 role/state 和 proposal/agreement mechanism,在 point-to-point link 上可快速 convergence,但实际时间取决于 topology、edge-port configuration、failure detection 与 compatibility,不应保证“一定 1–3 秒”。
Operational safeguards:
- 明确 root bridge placement 与 priority,不让随机 access switch 成 root;
- Edge/PortFast 只用于不应接 switch 的 edge port,并结合 BPDU Guard;
- Root Guard / Loop Guard 根据 failure model 使用;
- 不在没有替代 loop prevention 的情况下全局关闭 STP;
- Datacenter routed leaf-spine 常将 L3 边界下沉以减少 L2 failure domain,但 server-facing segment 仍可能存在 L2 mechanism。
7. L2 security 需要独立控制
ARP/NDP、DHCP 和 source MAC 都不是 cryptographic identity。Managed network 可使用 DHCP snooping、Dynamic ARP Inspection、RA Guard、port security、802.1X 与 control-plane policing,但每项都有 topology/trust-port assumption。配置不当会阻断 legitimate failover、static host 或 IPv6 control traffic。
不要在未授权 network 上使用 packet-crafting tool 发 ARP/BPDU/tagged frame 做实验。教学实验应在 isolated namespace、virtual lab 或自有 switch 上完成。
8. 诊断顺序
- 确认 interface link state、speed/duplex 与 error counter。
- 确认 ingress port 的 VLAN/PVID 和 trunk allowed list。
- 查
(VLAN, source MAC)在 FDB 中学到哪个 port,是否 flapping。 - 区分 known unicast、unknown unicast、broadcast 和 multicast flooding。
- 查 STP root、port role/state、topology-change 与 BPDU guard event。
- 跨 VLAN 问题转到 route/ACL/firewall,不继续只查 FDB。
9. 验收问题
- Switch 从 frame 的哪个 address 学 FDB,用哪个 address 查 egress?
- 为什么 unknown unicast 会 flood,FDB 学到后会怎样变化?
- 画出 802.1Q tag 插入的位置,说明 TPID 和 TCI。
- 为什么 VLAN separation 不能替代 firewall authorization?
- 一个 source MAC 在两个 trunk port 间 flapping,你会先查哪三类 root cause?
下一课进入8.3 MTU、IP fragmentation 与 Path MTU Discovery。