NicoCaldo
NicoCaldo

Reputation: 1577

linux tc command don't limit download speed

I have written a script that should let me limit my bandwidth via ts tool on Ubuntu 18.04.5 LTS.

The issue is that, even if the upload speed is correctly limited to the limit I set on the script, the download speed won't be limited and I can't understand why.

here is the link

#!/bin/bash  
# Full path to tc binary 

TC=$(which tc)

interface=ens33
interface_speed=100mbit
ip=192.168.42.130 # The IP address bound to the interface

download_limit=512kbit
upload_limit=1mbit    
    
# Filter options for limiting the intended interface.
FILTER="$TC filter add dev $interface protocol ip parent 1: prio 1 u32"

function start_tc { 
    tc qdisc show dev $interface | grep -q "qdisc pfifo_fast 0"
    tc qdisc del dev $interface root; sleep 1
    #[ "$?" -gt "0" ] && tc qdisc del dev $interface root; sleep 1  

    # start the tc configuration
    $TC qdisc add dev $interface root handle 1: htb default 30
    $TC class add dev $interface parent 1: classid 1:1 htb rate $interface_speed burst 15k

    $TC class add dev $interface parent 1:1 classid 1:10 htb rate $download_limit burst 15k
    $TC class add dev $interface parent 1:1 classid 1:20 htb rate $upload_limit burst 15k

    $TC qdisc add dev $interface parent 1:10 handle 10: sfq perturb 10
    $TC qdisc add dev $interface parent 1:20 handle 20: sfq perturb 10

    # Apply the filter rules
    
    # Catch-all IP rules, which will set global limit on the server
    # for all IP addresses on the server. 
    $FILTER match ip dst 0.0.0.0/0 flowid 1:10
    $FILTER match ip src 0.0.0.0/0 flowid 1:20

    # If you want to limit the upload/download limit based on specific IP address
    # you can comment the above catch-all filter and uncomment these:
    #
    # $FILTER match ip dst $ip/32 flowid 1:10
    # $FILTER match ip src $ip/32 flowid 1:20
}

function show_status {
        $TC -s qdisc ls dev $interface
}
#
# Display help 
#
function display_help {
        echo "Usage: tc [OPTION]"
        echo -e "\tstart - Apply the tc limit"
        echo -e "\tstatus - Show status"
}

# Start
if [ -z "$1" ]; then
        display_help
elif [ "$1" == "start" ]; then
        start_tc
elif [ "$1" == "status" ]; then
        show_status
fi

Upvotes: 0

Views: 1337

Answers (1)

NicoCaldo
NicoCaldo

Reputation: 1577

A possible solution could be

$TC qdisc del dev $IF root
echo "== DELETE DONE =="

$TC qdisc add dev $IF root handle 1:0 htb default 10

$TC class add dev $IF parent 1:0 classid 1:1 htb rate $LIMIT

$TC class add dev $IF parent 1:1 classid 1:10 htb rate 5mbit ceil $LIMIT_1
$TC class add dev $IF parent 1:1 classid 1:30 htb rate 5mbit ceil $LIMIT_2

$TC filter add dev $IF protocol ip parent 1:0 prio 2 u32 match ip dst x.x.x.x flowid 1:10
$TC filter add dev $IF protocol ip parent 1:0 prio 1 u32 match ip dst x.x.x.x flowid 1:30

echo "== SHAPING DONE =="

where $IF is the network interface you want to use and $TC the path of tc command (make sure to set TC=/sbin/tc on the top of the script as it seems, in your code, there's no path.

PS x.x.x.x are IP addresses you want to bind with the bandwidth limit

Upvotes: 1

Related Questions