Skip to content

Instantly share code, notes, and snippets.

@agran
Last active August 19, 2022 17:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save agran/a10f6ccdd1eb855d5d1bab3f1d07728b to your computer and use it in GitHub Desktop.
Save agran/a10f6ccdd1eb855d5d1bab3f1d07728b to your computer and use it in GitHub Desktop.
Speed limit per socket on server
#!/bin/bash
function speed_limit {
dev=$1
speed=$2
for i in $(seq 1 1024); do
h=$(printf %x $i)
tc class add dev $dev parent 1025: classid 1025:$h htb rate $speed burst 100k
done
tc filter add dev $dev protocol ip handle 1025 parent 1025: flow hash keys src,dst,proto,proto-src,proto-dst divisor 1024 perturb 10
}
function speed_limit_total {
dev_down=$1
speed_down=$2
dev_up=$3
speed_up=$4
tc qdisc del dev $dev_down root
tc qdisc del dev $dev_up root
tc qdisc del dev $dev_down ingress
tc qdisc add dev $dev_down ingress
tc filter add dev $dev_down parent ffff: protocol ip u32 match u32 0 0 action mirred egress redirect dev $dev_up
tc qdisc add dev $dev_down root handle 1025: htb r2q 100
tc qdisc add dev $dev_up root handle 1025: htb r2q 100
speed_limit $dev_down $speed_down
speed_limit $dev_up $speed_up
}
modprobe ifb
ip link set dev ifb0 up
speed_from_server_to_client=2.2Mbit
speed_from_client_to_server=1.8Mbit
speed_limit_total eth0 $speed_from_server_to_client ifb0 $speed_from_client_to_server
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment