github.com/schwarzm/garden-linux@v0.0.0-20150507151835-33bca2147c47/old/linux_backend/skeleton/net_rate.sh (about) 1 #!/bin/bash 2 3 [ -n "$DEBUG" ] && set -o xtrace 4 set -o nounset 5 set -o errexit 6 shopt -s nullglob 7 8 cd $(dirname "${0}") 9 10 source ./etc/config 11 12 if [ -z "${RATE:-}" ]; then 13 echo "Please specify RATE..." 1>&2 14 exit 1 15 fi 16 17 if [ -z "${BURST:-}" ]; then 18 echo "Please specify BURST..." 1>&2 19 exit 1 20 fi 21 22 # clear rule if exist 23 # delete root egress tc qdisc 24 tc qdisc del dev ${network_host_iface} root 2> /dev/null || true 25 26 # delete root ingress tc qdisc 27 tc qdisc del dev ${network_host_iface} ingress 2> /dev/null || true 28 29 # set inbound(outside -> eth0 -> w-<cid>-0 -> w-<cid>-1) rule with tc's tbf(token bucket filter) qdisc 30 # rate is the bandwidth 31 # burst is the burst size 32 # latency is the maxium time the packet wait to enqueue while no token left 33 tc qdisc add dev ${network_host_iface} root tbf rate ${RATE}bit burst ${BURST} latency 25ms 34 35 # set outbound(w-<cid>-1 -> w-<cid>-0 -> eth0 -> outside) rule 36 tc qdisc add dev ${network_host_iface} ingress handle ffff: 37 38 # use u32 filter with target(0.0.0.0) mask (0) to filter all the ingress packets 39 tc filter add dev ${network_host_iface} parent ffff: protocol ip prio 1 u32 match ip src 0.0.0.0/0 police rate ${RATE}bit burst ${BURST} drop flowid :1