1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Rudy's QoS script

Discussion in 'Tomato Firmware' started by Felipeota, Nov 8, 2008.

  1. Felipeota

    Felipeota Guest

    Hi, this is Rudy's qos script ported to tomato.
    Configure it as you like (read Rudy's post), disable tomato's qos and put the script in the firewall script section.
    I removed ip, ipp2p, layer7 because I didn't need them, but it should be easy to add.
    Code:
    #!/bin/sh
    #configuration
    DOWNLOAD=500
    UPLOAD=85
    TCP_BULK="1024: 21"
    UDP_BULK="1024:"
    TCP_PRIO="22 23 80 443"
    UDP_PRIO=""
    TCP_EXPR="53 1723"
    UDP_EXPR=""
    MTU=1492
    #end configuration
    QOS_IF=ppp0
    DBURST_D=10
    DBURST_U=10
    MIN_RATE=10
    insmod imq >&- 2>&-
    insmod ipt_IMQ >&- 2>&-
    insmod ipt_length >&- 2>&-
    iptables -t mangle -F
    iptables -t mangle -X
    ip link set imq0 up
    sed -n 's/ *\(.*\):.*/\1/p' /proc/net/dev | while read INTERFACE; do
    tc qdisc del dev $INTERFACE root >&- 2>&-
    done
    BURST_U=$(($DBURST_U*$UPLOAD/8))
    [ $BURST_U -lt $((1*$MTU)) ] && BURST_U=$((1*$MTU))
    RTOQ_U=$(($MIN_RATE*$UPLOAD*10/(8*$MTU)))
    [ $RTOQ_U -gt 20 ] && RTOQ_U=20
    [ $RTOQ_U -eq 0 ] && RTOQ_U=1
    tc qdisc add dev $QOS_IF root handle 1: htb default 40 r2q $RTOQ_U
    tc class add dev $QOS_IF parent 1: classid 1:1 htb rate ${UPLOAD}kbit ceil ${UPLOAD}kbit burst $(($BURST_U*3)) cburst $(($BURST_U*3)) mtu $MTU
    tc class add dev $QOS_IF parent 1:1 classid 1:10 htb rate $(($UPLOAD*5/10))kbit ceil ${UPLOAD}kbit burst $(($BURST_U*1)) cburst $(($BURST_U*1)) prio 1 mtu $MTU
    tc class add dev $QOS_IF parent 1:1 classid 1:20 htb rate $(($UPLOAD*2/10))kbit ceil ${UPLOAD}kbit burst $(($BURST_U*1)) cburst $(($BURST_U*1)) prio 2 mtu $MTU
    tc class add dev $QOS_IF parent 1:1 classid 1:30 htb rate $(($UPLOAD*2/10))kbit ceil ${UPLOAD}kbit burst $(($BURST_U*1)) cburst $(($BURST_U*1)) prio 3 mtu $MTU
    tc class add dev $QOS_IF parent 1:1 classid 1:40 htb rate $(($UPLOAD*1/10))kbit ceil ${UPLOAD}kbit burst $(($BURST_U*1)) cburst $(($BURST_U*1)) prio 4 mtu $MTU
    tc qdisc add dev $QOS_IF parent 1:10 sfq quantum $MTU perturb 10
    tc qdisc add dev $QOS_IF parent 1:20 sfq quantum $MTU perturb 10
    tc qdisc add dev $QOS_IF parent 1:30 sfq quantum $MTU perturb 10
    tc qdisc add dev $QOS_IF parent 1:40 sfq quantum $MTU perturb 10
    tc filter add dev $QOS_IF parent 1: prio 1 protocol ip handle 1 fw flowid 1:10
    tc filter add dev $QOS_IF parent 1: prio 2 protocol ip handle 2 fw flowid 1:20
    tc filter add dev $QOS_IF parent 1: prio 3 protocol ip handle 3 fw flowid 1:30
    tc filter add dev $QOS_IF parent 1: prio 4 protocol ip handle 4 fw flowid 1:40
    BURST_D=$(($DBURST_D*$DOWNLOAD/8))
    [ $BURST_D -lt $((1*$MTU)) ] && BURST_D=$((1*$MTU))
    RTOQ_D=$(($MIN_RATE*$DOWNLOAD*10/(8*$MTU)))
    [ $RTOQ_D -gt 20 ] && RTOQ_D=20
    [ $RTOQ_D -eq 0 ] && RTOQ_D=1
    tc qdisc add dev imq0 root handle 1: htb default 40 r2q $RTOQ_D
    tc class add dev imq0 parent 1: classid 1:1 htb rate ${DOWNLOAD}kbit ceil ${DOWNLOAD}kbit burst $((BURST_D*3)) cburst $((BURST_D*3)) mtu $MTU
    tc class add dev imq0 parent 1:1 classid 1:10 htb rate $(($DOWNLOAD*5/10))kbit ceil ${DOWNLOAD}kbit burst $((BURST_D*1)) cburst $((BURST_D*1)) prio 1 mtu $MTU
    tc class add dev imq0 parent 1:1 classid 1:20 htb rate $(($DOWNLOAD*2/10))kbit ceil ${DOWNLOAD}kbit burst $((BURST_D*1)) cburst $((BURST_D*1)) prio 2 mtu $MTU
    tc class add dev imq0 parent 1:1 classid 1:30 htb rate $(($DOWNLOAD*2/10))kbit ceil ${DOWNLOAD}kbit burst $((BURST_D*1)) cburst $((BURST_D*1)) prio 3 mtu $MTU
    tc class add dev imq0 parent 1:1 classid 1:40 htb rate $(($DOWNLOAD*1/10))kbit ceil $(($DOWNLOAD*3/4))kbit burst $((BURST_D*1)) cburst $((BURST_D*1)) prio 4 mtu $MTU
    tc qdisc add dev imq0 parent 1:10 sfq perturb 10
    tc qdisc add dev imq0 parent 1:20 sfq perturb 10
    tc qdisc add dev imq0 parent 1:30 sfq perturb 10
    tc qdisc add dev imq0 parent 1:40 sfq perturb 10
    tc filter add dev imq0 parent 1: prio 1 protocol ip handle 1 fw flowid 1:10
    tc filter add dev imq0 parent 1: prio 2 protocol ip handle 2 fw flowid 1:20
    tc filter add dev imq0 parent 1: prio 3 protocol ip handle 3 fw flowid 1:30
    tc filter add dev imq0 parent 1: prio 4 protocol ip handle 4 fw flowid 1:40
    iptables -t mangle -N mark_chain
    iptables -t mangle -N egress_chain
    iptables -t mangle -N ingress_chain
    iptables -t mangle -A POSTROUTING -o $QOS_IF -j egress_chain
    iptables -t mangle -A FORWARD -i $QOS_IF -j ingress_chain
    iptables -t mangle -A FORWARD -i $QOS_IF -j IMQ --todev 0
    iptables -t mangle -A INPUT -i $QOS_IF -j ingress_chain
    iptables -t mangle -A INPUT -i $QOS_IF -j IMQ --todev 0
    iptables -t mangle -A mark_chain -m mark --mark 0 -j CONNMARK --restore-mark
    for PORT in $UDP_EXPR; do
    iptables -t mangle -A mark_chain -m mark --mark 0 -p udp --dport $PORT -j MARK --set-mark 1
    done
    for PORT in $TCP_EXPR; do
    iptables -t mangle -A mark_chain -m mark --mark 0 -p tcp --dport $PORT -j MARK --set-mark 1
    done
    for PORT in $UDP_PRIO; do
    iptables -t mangle -A mark_chain -m mark --mark 0 -p udp --dport $PORT -j MARK --set-mark 2
    done
    for PORT in $TCP_PRIO; do
    iptables -t mangle -A mark_chain -m mark --mark 0 -p tcp --dport $PORT -j MARK --set-mark 2
    done
    for PORT in $UDP_BULK; do
    iptables -t mangle -A mark_chain -m mark --mark 0 -p udp --dport $PORT -j MARK --set-mark 4
    done
    for PORT in $TCP_BULK; do
    iptables -t mangle -A mark_chain -m mark --mark 0 -p tcp --dport $PORT -j MARK --set-mark 4
    done
    iptables -t mangle -A mark_chain -m mark --mark 0 -j MARK --set-mark 3
    iptables -t mangle -A mark_chain -j CONNMARK --save-mark
    iptables -t mangle -A mark_chain -p icmp -j MARK --set-mark 1
    iptables -t mangle -A mark_chain -p ipv6-icmp -j MARK --set-mark 1
    [ "$UDP_LENGTH" -gt 0 ] && iptables -t mangle -A mark_chain -p udp -m length --length :$UDP_LENGTH -j MARK --set-mark 1
    iptables -t mangle -A ingress_chain -j mark_chain
    iptables -t mangle -A egress_chain -j mark_chain
    iptables -t mangle -A egress_chain -p tcp -m length --length :128 --tcp-flags SYN,RST,ACK ACK -j MARK --set-mark 1
    exit 0
     
  2. peyton

    peyton LI Guru Member

    Why is it better ?
     

Share This Page