
Spanning Tree Protocol: ป้องกัน Loop ใน Network ด้วย STP RSTP MSTP
ใน network ที่มี switches หลายตัวเชื่อมต่อกัน ถ้ามี redundant links (สายเชื่อมต่อมากกว่า 1 เส้นระหว่าง switches) จะเกิด switching loop ที่ทำให้ broadcast frames วนไปมาไม่มีที่สิ้นสุด bandwidth ถูกใช้หมด switch CPU พุ่ง 100% network ล่มทั้งหมดภายในไม่กี่วินาที
Spanning Tree Protocol (STP) ป้องกัน loop โดย logically block redundant links ไว้ ให้เหลือเส้นทางเดียว (tree topology) ระหว่าง switches ทุกตัว เมื่อ active link เสียหาย STP จะ unblock redundant link ให้ traffic วิ่งเส้นทางสำรองอัตโนมัติ ทุก managed switch มี STP enable อยู่แล้วเป็น default แต่ถ้าไม่เข้าใจ STP อาจตั้งค่าผิดทำให้ network ช้าหรือล่มได้
STP ทำงานอย่างไร
Root Bridge Election
STP เลือก Root Bridge (switch 1 ตัวที่เป็นศูนย์กลางของ tree) โดยดูจาก Bridge Priority + MAC Address ค่า Bridge Priority default = 32768 switch ที่มี priority ต่ำที่สุดจะเป็น Root Bridge ถ้า priority เท่ากัน ดูจาก MAC Address ที่ต่ำสุด ควรกำหนด Root Bridge เอง (ตั้ง priority ให้ต่ำ) ไม่ควรปล่อยให้เลือกอัตโนมัติ เพราะอาจได้ switch ที่ไม่เหมาะสม (เช่น access switch แทนที่จะเป็น core switch)
Port Roles
STP กำหนด role ให้แต่ละ port: Root Port port ที่มี cost ต่ำที่สุดไปยัง Root Bridge (มี 1 port ต่อ switch) Designated Port port ที่ forward traffic ไปยัง segment นั้น (มี 1 port ต่อ segment) Blocked Port port ที่ถูก block เพื่อป้องกัน loop ไม่ forward traffic
Port States (STP Classic)
| State | ระยะเวลา | ส่ง/รับ Data | เรียนรู้ MAC | ส่ง/รับ BPDUs |
|---|---|---|---|---|
| Blocking | 20 วินาที (max age) | ไม่ | ไม่ | รับเท่านั้น |
| Listening | 15 วินาที (forward delay) | ไม่ | ไม่ | ส่งและรับ |
| Learning | 15 วินาที (forward delay) | ไม่ | ใช่ | ส่งและรับ |
| Forwarding | – | ใช่ | ใช่ | ส่งและรับ |
| Disabled | – | ไม่ | ไม่ | ไม่ |
ปัญหาของ STP Classic คือ convergence ช้ามาก เมื่อ topology เปลี่ยน (link down/up) ต้องรอ 30-50 วินาที (blocking → listening → learning → forwarding) กว่า traffic จะวิ่งได้ ในระหว่างนั้น network จะ partial outage
RSTP: Rapid Spanning Tree Protocol
ทำไมต้อง RSTP
RSTP (IEEE 802.1w) ปรับปรุง STP ให้ convergence เร็วขึ้นมาก จาก 30-50 วินาทีเหลือ 1-2 วินาที ด้วย proposal/agreement mechanism ที่ negotiate port roles อย่างรวดเร็ว RSTP เป็น default บน switches รุ่นใหม่ส่วนใหญ่ backward compatible กับ STP Classic
Port Roles เพิ่มเติม
RSTP เพิ่ม port roles: Alternate Port (backup สำหรับ Root Port เปลี่ยนเป็น Root Port ได้ทันทีเมื่อ Root Port ล่ม) Backup Port (backup สำหรับ Designated Port บน segment เดียวกัน)
MSTP: Multiple Spanning Tree Protocol
ปัญหาของ STP/RSTP
STP และ RSTP สร้าง spanning tree เดียว สำหรับทั้ง network หมายความว่า redundant links ที่ถูก block จะไม่ถูกใช้เลย bandwidth เสียเปล่า ถ้ามี VLANs หลายตัว traffic ทุก VLAN วิ่งเส้นทางเดียวกัน
MSTP แก้ปัญหาอย่างไร
MSTP (IEEE 802.1s) สร้าง spanning tree instances หลายตัว map VLANs เข้ากับ instances ต่างกัน ทำให้ VLANs ต่างกันใช้เส้นทางต่างกัน redundant links ถูกใช้งาน ใช้ bandwidth ได้เต็มประสิทธิภาพ load balance traffic ข้าม links ได้
ตารางเปรียบเทียบ STP Variants
| Protocol | Standard | Convergence | Instances | เหมาะกับ |
|---|---|---|---|---|
| STP (Classic) | 802.1D | 30-50 วินาที | 1 | Legacy networks |
| RSTP | 802.1w | 1-2 วินาที | 1 | Networks ทั่วไป |
| PVST+ (Cisco) | Cisco proprietary | 30-50 วินาที | 1 per VLAN | Cisco-only networks |
| Rapid PVST+ (Cisco) | Cisco proprietary | 1-2 วินาที | 1 per VLAN | Cisco networks ที่ต้อง per-VLAN |
| MSTP | 802.1s | 1-2 วินาที | Multiple (configurable) | Multi-vendor, enterprise |
Best Practices สำหรับ STP
กำหนด Root Bridge เอง
อย่าปล่อยให้ STP เลือก Root Bridge เอง ตั้ง core switch เป็น Root Bridge (priority 4096 หรือ 8192) ตั้ง distribution switch เป็น Secondary Root Bridge (priority 8192 หรือ 16384) ใช้คำสั่ง spanning-tree vlan X root primary/secondary บน Cisco
เปิด PortFast บน Access Ports
PortFast ทำให้ access ports (ที่ต่อกับ PC, printer) เข้าสู่ forwarding state ทันทีโดยไม่ต้องรอ STP convergence ป้องกัน DHCP timeout ที่เกิดจากการรอ STP 30 วินาที เปิดเฉพาะ access ports เท่านั้น ห้ามเปิดบน trunk ports (อาจทำให้เกิด loop)
เปิด BPDU Guard
BPDU Guard ปิด port ทันทีที่ได้รับ BPDU (Spanning Tree packet) เปิดบน access ports ที่มี PortFast ป้องกัน rogue switch ที่ถูกต่อเข้า access port ซึ่งอาจทำให้ STP topology เปลี่ยนและ network ล่ม
เปิด Root Guard
Root Guard ป้องกันไม่ให้ port กลายเป็น Root Port เปิดบน ports ที่ต่อกับ downstream switches ที่ไม่ควรเป็น Root Bridge ป้องกัน rogue switch ที่มี priority ต่ำกว่า Root Bridge มาแย่ง Root role
ทิ้งท้าย: STP เป็นเรื่องพื้นฐานที่ต้องเข้าใจ
Spanning Tree Protocol ทำงานเบื้องหลังบน switches ทุกตัว ถ้าไม่เข้าใจ STP อาจตั้งค่าผิดทำให้ network ช้าหรือล่ม ใช้ RSTP หรือ MSTP แทน STP Classic กำหนด Root Bridge เอง เปิด PortFast + BPDU Guard บน access ports เหล่านี้เป็น best practices ที่ต้องทำในทุก network
อ่านเพิ่มเติมเกี่ยวกับ VLAN Configuration และ Network Segmentation ที่ siamlancard.com หรือจาก icafeforex.com และ siam2r.com