Deep Dive Series

MikroTik Queue QoS

Traffic Shaping และ Bandwidth Management ขั้นสูง

Simple Queue, Queue Tree, PCQ, FQ_CoDel, Burst, Priority Queues และ Troubleshooting

Bandwidth Limit
Fair Sharing
Queue Tree
Burst

1. Queue และ QoS คืออะไร?

Queue คือกลไกสำหรับควบคุมปริมาณ Traffic ที่ผ่าน Router ส่วน QoS (Quality of Service) คือการจัดลำดับความสำคัญของ Traffic เพื่อให้ Application ที่สำคัญได้รับ Bandwidth และ Latency ที่เหมาะสม

ใน MikroTik RouterOS v7 มี Queue System ที่ทรงพลัง รองรับการทำ Traffic Shaping, Bandwidth Limiting, และ Fair Sharing แบบอัตโนมัติ

Queue Flow Diagram

Incoming Traffic 1 Gbps Firewall Mangle Mark Packets Queue Tree P1: VoIP P3: Business P8: Default HTB Scheduler Priority + Limit Outgoing Traffic 100 Mbps Traffic Flow: Incoming → Mangle (Mark) → Queue (Shape) → HTB (Schedule) → Outgoing

Bandwidth Control

จำกัดความเร็ว User/Department ป้องกันการใช้ Bandwidth เกินไป

VoIP Quality

ให้ Priority สูงกับ VoIP เพื่อลด Latency และ Jitter

Fair Sharing

กระจาย Bandwidth อย่างเท่าเทียมด้วย PCQ

2. ประเภท Queue ใน RouterOS v7

Simple Queue

Queue แบบง่ายที่ใช้กำหนด Bandwidth Limit ตาม IP Address หรือ Subnet โดยตรง

Parameters สำคัญ:
  • target - IP/Subnet ที่ต้องการจำกัด
  • max-limit - Bandwidth สูงสุด (upload/download)
  • limit-at - Bandwidth ขั้นต่ำที่รับประกัน
  • priority - ลำดับความสำคัญ (1-8)
  • burst-* - Burst settings
ตัวอย่าง:
/queue simple
add name=user1 target=192.168.1.100/32 \
    max-limit=10M/20M \
    limit-at=2M/5M \
    priority=5

Queue Tree

Queue แบบลำดับชั้น (Hierarchical) ใช้ร่วมกับ Firewall Mangle เพื่อควบคุม Traffic แบบละเอียด

Parameters สำคัญ:
  • parent - Queue แม่ (global, interface, queue)
  • packet-mark - Packet mark จาก Mangle
  • limit-at - CIR (Committed Rate)
  • max-limit - MIR (Maximum Rate)
  • queue - Queue type (default, pcq, etc.)
ตัวอย่าง:
/queue tree
add name=download parent=ether2-LAN \
    max-limit=100M
add name=voip parent=download \
    packet-mark=voip-pkt \
    priority=1 max-limit=10M

เปรียบเทียบ Simple Queue vs Queue Tree

Feature Simple Queue Queue Tree
การใช้งาน ง่าย กำหนด IP โดยตรง ซับซ้อน ต้องใช้ Mangle Mark
Hierarchical จำกัด (Parent-Child) ได้หลายระดับ
Traffic Type ทุกประเภทจาก Target แยกตาม Packet Mark
Performance ดีกว่า (น้อย Rules) ต้องผ่าน Mangle
Use Case User/Department Limit Application QoS

3. Scheduling Algorithms (Queue Types)

Queue Type กำหนดวิธีการจัดการ Packet ใน Queue แต่ละประเภทมีข้อดีและการใช้งานที่ต่างกัน

Queue Types ใน RouterOS v7

Type ชื่อเต็ม ใช้กับ ข้อดี ข้อเสีย
pfifo Packet FIFO Low-latency, VoIP ง่าย, ต่ำมาก Overhead ไม่แยกแยะ Priority
bfifo Byte FIFO ง่ายที่สุด คาดเดาได้ ไม่มี QoS
sfq Stochastic Fair Queue Fair Sharing ป้องกัน Flow ใหญ่ครอบครอง ไม่จัดการ Latency
pcq Per Connection Queue ISP, Multi-user กระจาย Bandwidth อัตโนมัติ ต้อง Tuning เริ่มต้น
fq_codel Fair Queuing CoDel Bufferbloat Reduction ลด Latency ได้ดี Configuration ปานกลาง
cake Common Applications Kept Enhanced Home/SOHO Auto-tuning, Latency ดีมาก ซับซ้อน
red Random Early Detection Congestion Avoidance ป้องกัน Congestion Tuning ยาก

เลือก Queue Type ไหนดี?

VoIP / Real-time

pfifo (limit=10)

ISP / Many Users

pcq

Home / Gaming

fq_codel / cake

Enterprise

sfq / default

Low Latency

pfifo / default-small

General Purpose

default (sfq)

4. HTB (Hierarchical Token Bucket)

HTB เป็น Algorithm สำหรับจัดการ Bandwidth แบบลำดับชั้น ใช้หลักการ Token Bucket เพื่อควบคุม Rate และ Priority

HTB Hierarchy Structure

Root Queue 100 Mbps Download 95 Mbps Upload 45 Mbps VoIP P1: 10M limit-at: 5M Business P3: 50M limit-at: 30M Default P8: 35M limit-at: 0 VoIP P1: 5M limit-at: 2M Business P3: 25M limit-at: 15M Default P8: 15M limit-at: 0 HTB Rules: 1) ตอบสนอง limit-at ทั้งหมดก่อน → 2) กระจายตาม Priority → 3) Priority เท่ากันแบ่งตามสัดส่วน

HTB Priority Rules

  1. 1 ตอบสนอง limit-at ก่อน - ทุก Queue ที่มี limit-at จะได้รับ Bandwidth ขั้นต่ำที่รับประกัน
  2. 2 กระจายตาม Priority - Priority 1 จะได้รับก่อน Priority 2 และต่อไปเรื่อยๆ
  3. 3 Priority เท่ากัน - จะแบ่ง Bandwidth ตามสัดส่วนของ max-limit
  4. 4 สามารถยืมได้ - Queue ที่ใช้ไม่ถึง max-limit สามารถยืมจาก Parent ได้

5. PCQ (Per Connection Queue) Deep Dive

PCQ เป็น Queue Type พิเศษที่สร้าง Sub-Queue อัตโนมัติสำหรับแต่ละ Connection หรือ User ทำให้ Bandwidth ถูกกระจายอย่างเท่าเทียมโดยอัตโนมัติ

PCQ Architecture

PCQ Parent Queue max-limit: 100M User 1 10 Mbps User 2 10 Mbps User 3 10 Mbps User N 10 Mbps ... PCQ Rate per User = Total Bandwidth / Active Users หรือกำหนด pcq-rate สูงสุดต่อ User

PCQ Parameters

Parameter ค่า Default Description
pcq-rate 0 (unlimited) Bandwidth สูงสุดต่อ Sub-Queue (User/Connection)
pcq-limit 50 จำนวน Packets ต่อ Sub-Queue
pcq-classifier - วิธีจัดกลุ่ม: src-address, dst-address, src-port, dst-port
pcq-total-limit 2000 จำนวน Sub-Queue สูงสุดทั้งหมด

PCQ Configuration

PCQ Setup
# ========================================
# สร้าง PCQ Queue Types
# ========================================

# PCQ สำหรับ Download (กระจายตาม Destination IP)
/queue type
add name=pcq-download kind=pcq \
    pcq-classifier=dst-address \
    pcq-rate=0 \
    pcq-limit=50 \
    pcq-total-limit=2000

# PCQ สำหรับ Upload (กระจายตาม Source IP)
/queue type
add name=pcq-upload kind=pcq \
    pcq-classifier=src-address \
    pcq-rate=0 \
    pcq-limit=50 \
    pcq-total-limit=2000

# PCQ กำหนด Rate ต่อ User (10 Mbps)
/queue type
add name=pcq-10M kind=pcq \
    pcq-classifier=dst-address \
    pcq-rate=10M

# ========================================
# นำไปใช้กับ Simple Queue
# ========================================
/queue simple
add name=all-users target=192.168.1.0/24 \
    queue=pcq-upload/pcq-download \
    max-limit=50M/100M \
    comment="Fair sharing for all LAN users"

# หรือกำหนด Rate ต่อ User
/queue simple
add name=limited-users target=192.168.2.0/24 \
    queue=pcq-10M/pcq-10M \
    max-limit=100M/100M \
    comment="Max 10M per user"

6. Burst Configuration

Burst อนุญาตให้ User ใช้ Bandwidth เกิน max-limit ชั่วคราว ทำให้การโหลดหน้าเว็บเร็วขึ้น แต่ไม่เหมาะกับการ Download ไฟล์ใหญ่

Burst Parameters

burst-limit

Bandwidth สูงสุดระหว่าง Burst (ควร 1.5-2x max-limit)

burst-threshold

ค่าเฉลี่ยที่จะเริ่ม/หยุด Burst (ควรต่ำกว่า max-limit เล็กน้อย)

burst-time

ช่วงเวลาคำนวณค่าเฉลี่ย (16-32 วินาที)

Burst Formula

Burst เกิดขึ้นเมื่อ: average-rate < burst-threshold

Longest Burst Time: (burst-threshold × burst-time) / burst-limit

ตัวอย่าง: burst-threshold=8M, burst-time=16s, burst-limit=20M → Longest Burst = (8 × 16) / 20 = 6.4 วินาที

Burst Configuration Example

Burst Setup
# ========================================
# Burst Configuration
# ========================================

# Burst สำหรับ Web Browsing
/queue simple
add name=web-user target=192.168.1.100/32 \
    max-limit=10M/20M \
    burst-limit=20M/40M \
    burst-threshold=7M/15M \
    burst-time=16s/16s \
    comment="Burst for better web experience"

# Burst สำหรับ Premium User
/queue simple
add name=premium-user target=192.168.1.200/32 \
    limit-at=5M/10M \
    max-limit=20M/50M \
    burst-limit=50M/100M \
    burst-threshold=15M/40M \
    burst-time=32s/32s \
    priority=3 \
    comment="Premium with guaranteed + burst"

# ========================================
# Burst Best Practices:
# - burst-time: 16s สำหรับทั่วไป, 32s สำหรับ streaming
# - burst-threshold: 70-80% ของ max-limit
# - burst-limit: 1.5-2x ของ max-limit
# ========================================

7. Configuration Examples

Example 1: Basic Bandwidth Limit

Simple Queue for Users
# ========================================
# Basic User Bandwidth Limits
# ========================================

# Single User
/queue simple add name=user-john target=192.168.1.100/32 \
    max-limit=10M/20M comment="John's PC"

# Department
/queue simple add name=dept-sales target=192.168.10.0/24 \
    max-limit=50M/100M priority=5 comment="Sales Department"

# Server with Guaranteed Bandwidth
/queue simple add name=server-critical target=10.0.0.5/32 \
    limit-at=20M/20M max-limit=50M/50M priority=1 \
    comment="Critical Server"

Example 2: Queue Tree with Mangle

Queue Tree Setup
# ========================================
# Step 1: Mark Traffic with Mangle
# ========================================
/ip firewall mangle

# Mark VoIP (SIP + RTP)
add chain=prerouting protocol=udp dst-port=5060,5061,10000-20000 \
    action=mark-connection new-connection-mark=voip-conn \
    comment="VoIP Traffic"
add chain=prerouting connection-mark=voip-conn \
    action=mark-packet new-packet-mark=voip-pkt passthrough=no

# Mark Web Traffic
add chain=prerouting protocol=tcp dst-port=80,443 \
    action=mark-packet new-packet-mark=web-pkt passthrough=no

# Mark Default (Everything else)
add chain=prerouting action=mark-packet new-packet-mark=default-pkt \
    passthrough=no comment="Default Traffic"

# ========================================
# Step 2: Create Queue Tree
# ========================================
/queue tree

# Parent Queues
add name=download parent=ether2-LAN max-limit=95M
add name=upload parent=ether1-WAN max-limit=45M

# VoIP Priority 1
add name=voip-down parent=download packet-mark=voip-pkt \
    limit-at=5M max-limit=10M priority=1 queue=default-small
add name=voip-up parent=upload packet-mark=voip-pkt \
    limit-at=2M max-limit=5M priority=1 queue=default-small

# Web Priority 4
add name=web-down parent=download packet-mark=web-pkt \
    max-limit=50M priority=4
add name=web-up parent=upload packet-mark=web-pkt \
    max-limit=25M priority=4

# Default Priority 8
add name=default-down parent=download packet-mark=default-pkt \
    max-limit=30M priority=8
add name=default-up parent=upload packet-mark=default-pkt \
    max-limit=15M priority=8

Example 3: FQ_CoDel for Bufferbloat

FQ_CoDel Configuration
# ========================================
# FQ_CoDel - ลด Bufferbloat สำหรับ Gaming/VoIP
# ========================================

# สร้าง FQ_CoDel Queue Type
/queue type
add name=fq-codel kind=fq_codel \
    fq_codel-ecn=yes \
    fq_codel-flows=1024 \
    fq_codel-interval=100ms \
    fq_codel-limit=10240 \
    fq_codel-target=5ms

# นำไปใช้กับ Simple Queue
/queue simple
add name=anti-bufferbloat target=192.168.1.0/24 \
    max-limit=90M/45M \
    queue=fq-codel/fq-codel \
    comment="FQ_CoDel for gaming/voip"

# ========================================
# FQ_CoDel Parameters:
# - target: 5ms (Target latency)
# - interval: 100ms (Worst case RTT)
# - ecn: yes (Use ECN marking)
# - flows: 1024 (Number of flow buckets)
# ========================================

8. Real-World Use Cases

ISP Bandwidth Management

จัดการ Bandwidth สำหรับลูกค้า ISP 1,000+ คน

# PCQ for ISP
/queue type add name=pcq-isp kind=pcq \
    pcq-classifier=dst-address pcq-rate=0

/queue simple add name=isp-users \
    target=10.0.0.0/8 \
    queue=pcq-isp/pcq-isp \
    max-limit=1G

Office QoS

แยก Priority ระหว่าง VoIP, Business, Guest

# VoIP Subnet
/queue simple add name=voip-dept \
    target=10.0.10.0/24 \
    max-limit=10M/10M priority=1

# Business Apps
/queue simple add name=business \
    target=10.0.20.0/24 \
    max-limit=50M/50M priority=3

# Guest WiFi
/queue simple add name=guest \
    target=192.168.100.0/24 \
    max-limit=5M/10M priority=8

Gaming Priority

ลด Latency สำหรับ Gaming

# Gaming Ports
/ip firewall mangle
add chain=prerouting protocol=udp \
    dst-port=3074,3478,27015-27030 \
    action=mark-packet new-packet-mark=gaming

/queue type add name=gaming-queue \
    kind=pfifo pfifo-limit=10

/queue tree add name=gaming \
    parent=download packet-mark=gaming \
    priority=1 queue=gaming-queue

Streaming Limit

จำกัด Netflix/YouTube ช่วงเวลาทำงาน

# Streaming CDN IPs
/ip firewall address-list
add address=23.160.0.0/16 list=streaming
add address=151.101.0.0/16 list=streaming

/ip firewall mangle
add chain=prerouting dst-address-list=streaming \
    action=mark-packet new-packet-mark=streaming

/queue tree add name=streaming \
    parent=download packet-mark=streaming \
    max-limit=20M priority=6

9. Troubleshooting

1

ปัญหา: Queue ไม่ทำงาน - Fasttrack Bypass

อาการ: Queue Stats ไม่ขึ้น, Traffic ไม่ถูก Limit

สาเหตุหลัก: Fasttrack Connection จะข้าม Queue Tree และ Simple Queue ทั้งหมด!

วิธีแก้ Fasttrack
# วิธีที่ 1: Disable Fasttrack (ง่ายที่สุด)
/ip firewall filter disable [find action=fasttrack-connection]

# วิธีที่ 2: Exclude VoIP จาก Fasttrack
/ip firewall filter
add chain=forward connection-state=established,related \
    connection-mark=voip-conn action=accept place-before=0
add chain=forward connection-state=established,related \
    action=fasttrack-connection place-before=1

# วิธีที่ 3: ใช้ Simple Queue (ทำงานบางส่วนกับ Fasttrack)
# แต่จะไม่แม่นยำเท่า Queue Tree
2

ปัญหา: Packet Mark ไม่ตรงกับ Queue

อาการ: Mangle มี Traffic แต่ Queue ไม่มี

Debug Steps
# 1. ตรวจสอบ Mangle Rules
/ip firewall mangle print stats
# ดูว่า Bytes/Packets เพิ่มขึ้นไหม

# 2. ตรวจสอบ Packet Mark
/ip firewall mangle print where new-packet-mark!=""

# 3. ตรวจสอบ Queue packet-mark
/queue tree print where packet-mark!=""

# 4. ตรวจสอบว่า Chain ถูกต้อง
# - Download: prerouting (ต้อง mark ก่อน routing)
# - Upload: postrouting (หลัง routing decision)

# 5. ตรวจสอบ passthrough
# - ถ้า passthrough=yes, packet จะผ่าน rule ถัดไป
# - ถ้า passthrough=no, packet จะหยุดที่นี่
3

ปัญหา: CPU สูงเมื่อมีหลาย Queue

อาการ: Router ช้า, CPU 100%

Optimization
# วิธีแก้: ใช้ PCQ แทนหลาย Simple Queue

# แทนที่ 100 Simple Queues:
# /queue simple add name=user1 target=192.168.1.1 ...
# /queue simple add name=user2 target=192.168.1.2 ...
# ... (100 queues)

# ใช้ PCQ เดียว:
/queue type add name=pcq-fair kind=pcq \
    pcq-classifier=dst-address
/queue simple add name=all-users \
    target=192.168.1.0/24 \
    queue=pcq-fair/pcq-fair \
    max-limit=100M

# ลด Mangle Rules:
# ใช้ connection-mark แล้วค่อย mark packet
/ip firewall mangle
add chain=prerouting src-address=192.168.10.0/24 \
    action=mark-connection new-connection-mark=voip-conn
add chain=prerouting connection-mark=voip-conn \
    action=mark-packet new-packet-mark=voip-pkt passthrough=no

10. สรุป

สิ่งที่ได้เรียนรู้

Queue Types

  • • Simple Queue - ง่าย ใช้กับ IP/Subnet
  • • Queue Tree - Hierarchical + Mangle
  • • PCQ - Auto Fair Sharing
  • • FQ_CoDel - Bufferbloat Reduction

Key Concepts

  • • HTB - Priority + Guaranteed Bandwidth
  • • Burst - Temporary Speed Boost
  • • PCQ Classifier - Per User/Connection
  • • Fasttrack Bypass - ปัญหาหลัก

Configuration

  • • Mangle → Packet Mark → Queue
  • • limit-at (CIR) → max-limit (MIR)
  • • Priority 1-8 (1 = Highest)
  • • Queue Type Selection

Use Cases

  • • ISP: PCQ for Fair Sharing
  • • Office: VoIP Priority + Guest Limit
  • • Gaming: Low Latency Queue
  • • Streaming: Rate Limiting