#!/bin/bash
#
#  All Rates are in Kbits, so in order to gets Bytes divide by 8
#  e.g. 25Kbps == 3.125KB/s
#
 
tc_start() {
    tc qdisc add dev enp0s25 root handle 1:0 cbq bandwidth 100Mbit avpkt 1000 mpu 64
 
    # MAC Address: 00:21:70:f1:e0:cd
    # 8192kbps download speed (~ 1MBytes)
    tc class add dev enp0s25 parent 1:0 classid 1:2 cbq rate 8192KBit allot 1514 prio 1 avpkt 1000 bounded
    tc filter add dev enp0s25 parent 1:0 protocol ip handle 2 fw flowid 1:2
    ebtables -A INPUT -d 00:21:70:f1:e0:cd -j mark --set-mark 2 --mark-target ACCEPT
}
 
tc_stop() {

    ebtables -F
 
    tc qdisc del dev enp0s25 root
}
 
tc_show() {
 
    echo ""
    echo "enp0s25"
    tc qdisc show dev enp0s25
    tc class show dev enp0s25
    tc filter show dev enp0s25
}
 
tc_restart() {
 
    tc_stop
    sleep 1
    tc_start
 
}
 
case "$1" in
 
  start)
 
    echo -n "Starting bandwidth shaping: "
    tc_start
    echo "done"
    ;;
 
  stop)
 
    echo -n "Stopping bandwidth shaping: "
    tc_stop
    echo "done"
    ;;
 
  restart)
 
    echo -n "Restarting bandwidth shaping: "
    tc_restart
    echo "done"
    ;;
 
  show)
 
    tc_show
    ;;
 
  *)
 
    echo "Usage: rateshape {start|stop|restart|show}"
    ;;
 
esac