Using tc to decrease bandwidth to a specific destination address

Solution 1:

Yes, the tc is designed for this. TC works over any types of interfaces (physical and software). Most simple queue scheduler is the htb (Hierarchical Token Bucket).

Typical simple configuration:

  • htb root queue discipline
  • root class (100% bandwidth)
  • default leaf class (80% bandwidth guarantee, up to to 100% bandwidth)
  • bandwidth limited leaf class 1 (10% bandwidth guarantee)
  • bandwidth limited leaf class 2 (10% bandwidth guarantee, up to 100% bandwidth)
  • sfq queue discipline on every leaf class to fair sharing of limit between flows

Summary bandwidth of children classes should not exceed the bandwidth of parent class. Otherwise the scheduler will not be accurate.

Guess the interface bandwidth is 100Mbit/s. So commands for your configuration will look like:

tc qdisc add dev enp4s0f0 root handle 1: htb default 10

tc class add dev enp4s0f0 parent 1:  classid 1:1  htb rate 100Mbit ceil 100Mbit quantum 15000
tc class add dev enp4s0f0 parent 1:1 classid 1:10 htb rate 80Mbit  ceil 100Mbit quantum 15000
tc class add dev enp4s0f0 parent 1:1 classid 1:11 htb rate 10Mbit  ceil 10Mbit  quantum 15000
tc class add dev enp4s0f0 parent 1:1 classid 1:12 htb rate 10Mbit  ceil 100Mbit quantum 15000

tc qdisc add dev enp4s0f0 parent 1:10 handle 10: sfq
tc qdisc add dev enp4s0f0 parent 1:11 handle 11: sfq
tc qdisc add dev enp4s0f0 parent 1:12 handle 12: sfq

After the hierarchy of classes is configured, the classification should be configured. There are many types of classifiers with various features and various level of user-friendship. The classifier checks packets by configured criteria. If a packet matched, the class is assigned to the packet.

tc filter add dev <iface> parent 1: prio 1 protocol ip 800::1 u32 match dst ip 192.168.10.2/32 classid 1:11
tc filter add dev <iface> parent 1: prio 1 protocol ip 800::2 u32 match dst ip 192.168.10.0/24 classid 1:12

In this small example the packets to host 192.168.10.2 will be pass to class 1:11, and, consequentially, limited by 10Mbit/s. Packets to other addresses from 192.168.10.0/24 subnet will be passed to class 1:12. All other packets will be passed to default class (1:10).

This is a quick start point of usage of the tc tool.

If you have questions, ask it.