Lab 7.1 : QoS

Lab 7.1 : QoS
Quality of Service on a MikroTik router

This lab will provide a demonstration of how to configure Quality of Service (QoS) on a MikroTik router, showcasing the different methods available for setting up traffic queuing.

MikroTik's QoS (Quality of Service) system helps implement fair queueing by allocating bandwidth and ensuring equitable distribution among different users, devices, or traffic flows. Here's how MikroTik’s QoS features facilitate fair queueing:

1. Equal Distribution of Bandwidth

  • With fair queueing, MikroTik ensures that each user or session gets a fair share of bandwidth. This is achieved by dividing available bandwidth into smaller, equal parts and assigning them to different traffic flows or users. It prevents any one user or application from monopolizing the network.

2. Dynamic Bandwidth Allocation

  • MikroTik uses queue types like PCQ (Per Connection Queue) to automatically allocate bandwidth equally to all active users or connections. PCQ dynamically adjusts bandwidth distribution based on the number of active users at any given time, ensuring fairness even as the number of users fluctuates.

For example:

  • If five users are on the network, the available bandwidth is equally divided among the five users.
  • If more users join, the bandwidth is automatically reallocated, ensuring everyone gets a fair share without manual intervention.

3. Avoiding Congestion and Bandwidth Hogging

  • Queue trees and simple queues can be configured to ensure that no single user or connection consumes more than their allocated bandwidth, preventing congestion or "hogging" of resources by heavy users.
  • Fair queueing ensures that network-intensive applications (such as streaming or file downloads) do not disproportionately impact the bandwidth available to others, maintaining network performance for all users.

We are going to use a following setup as to demonstrate queueing on a MikroTik router.

In this example, we have setup 5 MikroTik routers: 3 clients, one gateway router, and one server. We are going to establish queueing on the gateway router and see how it affects traffic going from the clients to the server.

Step 1: Configure "Server" Router:

/ip address add address=192.168.2.2/24 interface=ether2
/system identity set name=Server

We're also going to enable the bandwidth server to allow us to test bandwidth to this device

/tool bandwidth-server set enabled=yes

Step 2: Configure "Gateway" Router:

/ip address add address=192.168.2.1/24 interface=ether1
/system identity set name=Gateway
/interface bridge add name=bridge1
/interface bridge port
add bridge=bridge1 interface=ether2
add bridge=bridge1 interface=ether3
add bridge=bridge1 interface=ether4
/ip address add address=192.168.1.1/24 interface=bridge1
/ip firewall nat add chain=srcnat out-interface=ether1 action=masquerade

Step 3: Configure Client Routers:

ClientA:

/ip address add address=192.168.1.254/24 interface=ether1
/ip route add dst-address=192.168.2.0/24 gateway=192.168.1.1
/system identity set name=ClientA

ClientB:

/ip address add address=192.168.1.253/24 interface=ether1
/ip route add dst-address=192.168.2.0/24 gateway=192.168.1.1
/system identity set name=ClientB

ClientC:

/ip address add address=192.168.1.252/24 interface=ether1
/ip route add dst-address=192.168.2.0/24 gateway=192.168.1.1
/system identity set name=ClientC

Step 4: Run Bandwidth test on Clients

Connect to each of the clients, and in the CLI, run the following command

/tool bandwidth-test 192.168.2.2 protcol=tcp direction=both user=admin password=<admin password>

Observer the tx-current and rx-current values for each of the three Client Routers.

Simple Queues

Step 5: Establish a simple queue

Next, we're going to set up a 128Kbps bi-directional queue on the Gateway router, and then re-observe the traffic. With the bandwidth test still running on the Clients, connect to the Gateway router, and run the following commands:

/queue simple add name="limit_128k" target=192.168.1.0/24 max-limit=128K/128K

Before queue is enabled:

After queue is enabled: Notice the drop in rx and tx transmit rates.

Step 6: Enable bursting on the queue we just created. On the gateway, run the following command to enable bursting on the queue we just set up.

/queue simple set 0 burst-limit=256k/256k burst-threshold=128k/128k burst-time=10s/10s

By following these steps, you can enable bursting in a simple queue in RouterOS 7.15, allowing your devices to temporarily exceed their bandwidth limits when there’s available capacity.

Bursting in MikroTik RouterOS is a mechanism used in queues to temporarily allow traffic to exceed the configured bandwidth limit for a short period. It can be useful for improving user experience, especially in scenarios where short bursts of data (such as loading web pages or files) need faster speeds, even if the user is normally limited to a lower bandwidth.

How Bursting Works

Bursting is controlled by three key parameters in RouterOS:

  1. Burst Limit: The maximum speed the queue can allow during a burst. This defines the peak bandwidth a device can reach temporarily.
  2. Burst Threshold: The average traffic rate that must be exceeded for a burst to occur. If the actual traffic stays below this threshold, no bursting happens.
  3. Burst Time: The time period over which the average traffic rate is calculated. If the traffic exceeds the burst threshold during this period, the burst is allowed.

Key Concepts of Bursting

  • Average Traffic Rate: RouterOS calculates the average traffic rate over the burst-time period (in seconds). This average is compared against the burst-threshold.
  • Burst Activation: If the average traffic rate during the burst-time period exceeds the burst-threshold, the router allows the device to temporarily exceed its normal bandwidth limit up to the burst-limit.
  • Burst Termination: Once the burst period ends (i.e., the traffic falls below the burst threshold or the burst time expires), the device’s bandwidth will revert to the max-limit.

How Bursting is Calculated

Here’s a simplified version of how bursting is calculated and applied:

  1. Initial Traffic: When traffic starts, RouterOS monitors the average rate over the burst-time window (e.g., 10 seconds).
  2. Traffic Below Threshold: If the average traffic rate stays below the burst-threshold, the queue enforces the normal max-limit (e.g., 128Kbps).
  3. Traffic Exceeds Threshold: If the average traffic rate during the burst-time window exceeds the burst-threshold, bursting is activated, and the speed can temporarily increase up to the burst-limit (e.g., 256Kbps).
  4. Burst Duration: The device can maintain this higher speed for the remainder of the burst period, as long as the average traffic rate stays above the burst threshold.
  5. Returning to Normal Speed: Once the burst period ends (i.e., traffic falls below the threshold or burst time expires), the queue returns to enforcing the normal max-limit.

Bursting Example

Let’s use a simple example to explain how it works in practice.

Queue Configuration:

  • max-limit: 128Kbps (normal limit)
  • burst-limit: 256Kbps (burst speed)
  • burst-threshold: 128Kbps (threshold for burst to occur)
  • burst-time: 10 seconds (time window)

Scenario:

  1. Normal Traffic (Below Burst Threshold):
    • If the device’s traffic averages 100Kbps over the last 10 seconds (below the burst-threshold of 128Kbps), the device will be limited to the max-limit of 128Kbps. No burst will occur.
  2. Sudden Traffic Surge (Exceeds Burst Threshold):
    • Suppose the device suddenly starts downloading at 200Kbps, causing the average traffic over the 10-second window to exceed the burst-threshold of 128Kbps.
    • Since the average traffic now exceeds the burst threshold, the device is allowed to burst up to 256Kbps (burst-limit) for the remainder of the 10-second burst period.
    • During this time, the user can enjoy faster speeds temporarily (256Kbps instead of the normal 128Kbps).
  3. End of Burst:
    • If the traffic continues at a high rate, the burst will only last for a limited period (the burst-time of 10 seconds). Once the burst window expires, the router recalculates the average traffic.
    • If the average traffic falls back below the burst threshold (e.g., user activity drops), the queue reverts to enforcing the max-limit of 128Kbps.

Bursting Parameters Breakdown:

  1. Burst Limit:
    • Defines the maximum speed allowed during the burst.
    • Example: If the burst-limit is set to 256Kbps, the device can reach speeds of up to 256Kbps while bursting.
  2. Burst Threshold:
    • Defines the average traffic rate that must be exceeded to activate a burst.
    • Example: If the burst-threshold is 128Kbps, the device must have an average traffic rate exceeding 128Kbps to start bursting.
  3. Burst Time:
    • Defines the time window (in seconds) over which the average traffic is calculated.
    • Example: If the burst-time is set to 10 seconds, the router looks at the average traffic rate over the last 10 seconds to decide whether to allow a burst.

Why Use Bursting?

  • Improved User Experience: Bursting is particularly useful for web browsing and other short-duration tasks that require higher speeds momentarily.
  • Temporary Speed Boost: Allows users to get faster speeds temporarily without permanently increasing their bandwidth allocation.
  • Efficient Bandwidth Usage: Bursting ensures that users only get the speed boost when the network has enough capacity, preventing overloading the network.

PCQ (Per Connection Queue)

PCQ (Per Connection Queuing) is a specialized queuing algorithm in MikroTik RouterOS designed to divide available bandwidth equally among multiple users or connections. It automatically creates a dynamic queue for each IP address or connection, ensuring fair bandwidth distribution. PCQ is often used in scenarios where you want to manage bandwidth fairly across a large number of users or devices, such as in ISPs or networks with many users sharing the same internet connection.

PCQ works by splitting the traffic into different sub-queues based on specified criteria, such as source IP, destination IP, source port, or destination port. Each connection or user gets its own queue, and the available bandwidth is distributed equally among all active queues.

Key Characteristics of PCQ:

  1. Automatic Queue Creation: PCQ dynamically creates queues for each connection or user without the need for manual configuration of individual queues.
  2. Fair Bandwidth Distribution: It ensures that all users or connections get an equal share of the available bandwidth.
  3. Scalability: PCQ is highly scalable and can efficiently manage hundreds or thousands of users or connections, making it ideal for ISPs, large offices, or public Wi-Fi networks.
  4. Targeted Queuing: You can configure PCQ to control upload traffic (using pcq-upload classifier) or download traffic (using pcq-download classifier).

Common Use Cases for PCQ:

  • ISPs or public networks: To ensure all customers or users get equal bandwidth.
  • Large offices or apartment complexes: To distribute bandwidth fairly among all devices or connections.
  • Shared internet connections: Where multiple users share a single internet connection, and you want to prevent any single user from consuming all the bandwidth.

We are going to disable the simple queue that we created earlier, and create a per-connection queue and see how traffic differs between the simple queue and the PCQ.

Step 7: To disable the simple queue, issue the following command on the Gateway router.

/queue simple disable 0 

(0 is the ID of the queue that we set up earlier.) To verify which queue to disable, you can use the following command:

/queue simple print

Step 8: Create a PCQ, with a limit of 128K upload and 128K download.

/queue type add name=pcq-download kind=pcq pcq-rate=256k pcq-classifier=dst-address
/queue type add name=pcq-upload kind=pcq pcq-rate=256k pcq-classifier=src-address
/queue simple add name="PCQ1" target=192.168.1.0/24 queue=pcq-upload/pcq-download

Overall Summary of What These Commands Do:

  1. First Command: Creates a PCQ type named pcq-download for download traffic, where each user (based on the destination address) can download at a maximum speed of 256Kbps.
  2. Second Command: Creates a PCQ type named pcq-upload for upload traffic, where each user (based on the source address) can upload at a maximum speed of 256Kbps.
  3. Third Command: Creates a simple queue named PCQ1 that applies to all devices in the 192.168.88.0/24 subnet. It uses the previously created pcq-upload and pcq-download queue types to ensure each user within this subnet gets a maximum of 256Kbps for both upload and download.

Set a minimum guaranteed bandwidth

To modify the PCQ (Per Connection Queuing) configuration to ensure a minimum guaranteed bandwidth of 128Kbps for both upload and download while still allowing users to burst up to the maximum of 256Kbps, you need to introduce additional queue parameters such as limit-at.

The limit-at parameter in MikroTik RouterOS defines the guaranteed bandwidth for each user or connection. This means that the user will always get at least the amount specified in limit-at, even if the network is congested.

Here’s how you can modify the configuration to ensure a minimum guaranteed speed of 128Kbps while allowing users to burst up to 256Kbps. Run the following changes on the Gateway router:

/queue type set [find name=pcq-download] pcq-rate=256k pcq-limit=128k pcq-classifier=dst-address
/queue type set [find name=pcq-upload] pcq-rate=256k pcq-limit=128k pcq-classifier=src-address

Explanation of Changes:

  • pcq-rate=256k: This remains the same and represents the maximum bandwidth that each connection (or user) can receive, which is still 256Kbps.
  • pcq-limit=128k: This is the new parameter that ensures each connection (or user) gets a minimum guaranteed bandwidth of 128Kbps. If there is available bandwidth, the user can burst beyond this up to the pcq-rate (256Kbps), but they are always guaranteed at least 128Kbps even under heavy load.
  • pcq-classifier: This remains the same (dst-address for download and src-address for upload) and specifies how the PCQ will classify traffic for creating dynamic queues per user.
0:00
/0:46

Before and after enabling PCQ

Summary

  1. Create a Simple Queue to demonstrate how to control bandwidth
  2. Created PCQ (Per Connection Queuing) Types for both download and upload traffic:
    • Each user or connection is allowed a maximum bandwidth of 256Kbps for both upload and download.
    • We used the destination address for download traffic and the source address for upload traffic as classifiers to ensure that each unique user gets their own queue.
  3. Guaranteed Minimum Bandwidth with PCQ:
    • We modified the PCQ settings by adding the pcq-limit parameter to ensure a minimum guaranteed speed of 128Kbps for both upload and download. This guarantees that even if the network is congested, each user will still receive at least 128Kbps.
  4. Applied PCQ to a Simple Queue:
    • We created a simple queue named PCQ1 and applied it to the 192.168.88.0/24 subnet, ensuring that all users within this subnet are subject to the PCQ rules for upload and download.

Outcome:

  • Minimum Guaranteed Bandwidth: Each user gets at least 128Kbps for upload and download.
  • Maximum Burst Speed: Users can burst up to 256Kbps when there's available bandwidth.
  • Fair Bandwidth Distribution: PCQ ensures equal sharing of bandwidth among all active users within the specified subnet.

This setup is ideal for ensuring fair bandwidth usage while still allowing flexibility in network performance.