#!/bin/bash



#
# ACK_rate
# --------
# To utilize the DOWNSTREAM bandwidth, it is very important to reserve
# enough bandwidth to ACK packets.
#
# The latency of the ACK packets also influences the downstream
# utilization (due to the bandwidth-delay product and default window
# size).  The assure low latency using HTB, the ACK class must never
# be backlogged.  This is achived by reserving a big enough RATE to
# the class and assigning a "high" priority.
#
# The ACK_rate can be (calculated or) estimated based on the
# downstream capacity.
#
#  downstream_capacity / data_packet_size = num_data_packets
#
#  num_data_packets / ACK_packets_per_data_packet = Num_ACK_packets
#
#  num_ACK_packets * ACK_packet_size = Required_ACK_rate
#
#
function ACK_rate_calc() {
    if [ -z "$1" ]; then
        echo "[$FUNCNAME] ERROR need to know downstream capacity (in Kbit/s)."
        exit 1;
    fi
    local DOWNSTREAM_=$1

    # Delayed ACK factor, ACK_factor:
    # -------------------------------
    # The minimum delayed ACK factor is 1, since at most one ACK should be
    # sent for each data packet [rfc1122, rfc2581].  "Normal" behavior of
    # TCP bulk transfers is to ACK every second data packet.
    # (notice: ACK_scale=10  =>  10=1  16=1.6)
    #ACK_factor=12
    ACK_factor=15
    ACK_scale=10

    # ACK_size
    # --------
    # The ACK size on ADSL with ATM link-layer is two ATM cells (2*53)
    #
    # With out cell headers, only payload 2*48
    #ACK_size=96
    # Complete ATM cells with headers 2*53
    ACK_size=106

    # Data packet size
    # ----------------
    # The (average) data packet size.
    #
    Data_size=1500

    # Precision factor, doing integer part divisions we lose the
    # fractional part of the number.  To avoid this we "move" the decimal
    # point, when doing fixed point arithmetic
    scale=100000

    # Round off loss
    round_off_bonus=1

    # Old calc:
    # ACK_rate=$[ ($DOWNSTREAM_*$scale)/$Data_size/$ACK_factor * $ACK_size \
    #           * $ACK_scale / $scale ]

    # New calc: (multiply first, then divide, avoiding div loss)
    ACK_rate=$[ $DOWNSTREAM_ * $ACK_size * $ACK_scale \
                / $Data_size / $ACK_factor ]

    if [ $ACK_rate -lt 0 ]; then
        echo "ERROR negative ACK rate \"$ACK_rate\""
        exit 2
    fi

    if [ -n "${CEIL}" ]; then
        local RATE_=$[${CEIL}-${ACK_rate}]
        if [ ${RATE_} -lt 0 ]; then
            echo "ERROR ACK rate ($ACK_rate) greater that ceil ($CEIL)"
            echo "      Resulting in negative rate ($RATE_) to classes!"
            exit 2
        fi
    fi
    ACK_rate=$[ $ACK_rate + $round_off_bonus ]
}




##############################################################################
# Now set up traffic shaping based on the marks given to each packet

# Delete all old stuff first
tc qdisc del dev ppp0 root    2> /dev/null > /dev/null
tc qdisc del dev ifb0 root   2> /dev/null > /dev/null
tc qdisc del dev ifb0 ingress   2> /dev/null > /dev/null
tc qdisc del dev ppp0 root 2> /dev/null > /dev/null
tc qdisc del dev ifb0 root 2> /dev/null > /dev/null
tc qdisc del dev ppp0 ingress 2> /dev/null > /dev/null
tc qdisc del dev eth0 root 2> /dev/null > /dev/null
tc qdisc del dev eth0 ingress 2> /dev/null > /dev/null

tc qdisc del dev eth1 root    2> /dev/null > /dev/null

# Set variables
# The downlink should be slightly less than the ADSL line
# All the classes below should add up to this
DOWNLINK=1000
DOWNLINK2=2000
# Same for the uplink
UPLINK=450
UPLINK2=500




# ===================================================
#  Packet overhead per TCP/IP packet due to ATM/AAL5 
# ===================================================
#
# Overhead per AAL5 packet 
#  AAL5 tail    : 8 bytes per packet (incl. 4 bytes checksum)
#
aal5_tail=8

# AAL5 SSCS headers (SSCS = Service Specific Common Sublayer)
# -----------------
#
# VC (Virtual Circuit) vs. LLC (Logical Link Control) 
#  There is a basic choice between LLC and VC when configuring
#  the encapsulation mode on ADSL.  LLC gives more overhead.  

# Routed modes:
overhead_PPPoA_VC=2
overhead_PPPoA_LLC=6
#
overhead_rfc2684R_VC=0
overhead_rfc2684R_LLC=8

# Bridged modes:
#  There is a special feature bridged mode which some equipment 
#  support where the MAC-checksum (FCS) can be dropped, which   
#  reduces the overhead by 4 bytes.
#
overhead_rfc2684B_VC=20
overhead_rfc2684B_VC_noFCS=16
overhead_rfc2684B_LCC=28
overhead_rfc2684B_LCC_noFCS=24
#
overhead_PPPoE_VC=28
overhead_PPPoE_VC_noFCS=24
overhead_PPPoE_LLC=36
overhead_PPPoE_LLC_noFCS=32

# ***************************************************
# *** Choose the SSCS overhead encapsulation mode ***
# ***************************************************
overhead_SSCS=${overhead_PPPoA_VC}

# Encapsulation overhead:
# -----------------------
if [ -z "$overhead" ]; then
    overhead=$[ ${overhead_SSCS} + ${aal5_tail} ]
fi

linklayer="linklayer atm"
linklayer=""


LINE_DOWNSTREAM=3800
CEIL=680


# Calculate the ACK_rate from the $DOWNLINK
# ----------------------   
if [ -z "$ACK_rate" -a -n "$DOWNLINK" ]; then
    # Will set the variable $ACK_rate
    ACK_rate_calc $DOWNLINK
    #echo "ACK rate calculated to: $ACK_rate "
    #echo "(for a $LINE_DOWNSTREAM Kbit downstream link)"
fi
 
# Set ACK_rate to 1kbit if not set (zero makes tc command fail)
if [ -z "$ACK_rate" ]; then
    # echo "Cannot determine ACK rate to reserve, need more input"
    ACK_rate=1
fi

#ACK_ceil=$CEIL
ACK_ceil=$[90*${UPLINK}/100] 
#ACK_ceil=$[2*${ACK_rate}]
  
# Rate bandwidth left for Classes
# 
RATE=$[${UPLINK}-${ACK_rate}]

RATE=$DOWNLINK
RATE2=$DOWNLINK2

##############################################################################
## downlink via eth0 ##

DEV=eth0

# A qdisc is a whole set of shaping rules that we apply
# Here we add a HTB qdisc to the interface eth0
# This is known as the root for the interface
# We don't set a default class to stop us shaping local eth0 traffic
# We have to shape at eth0 and not ppp0 as we can only do egress shaping
#tc qdisc add dev eth0 root handle 1: htb

# Then we add to the root some overall rate limits
#tc class add dev eth0 parent 1: classid 1:1 htb rate ${DOWNLINK}kbit

#tc qdisc add dev ${DEV} root        handle 1: htb default 50 r2q 1
tc qdisc add dev ${DEV} root        handle 1: htb r2q 1

tc class add dev ${DEV} parent 1: classid 1:1 htb \
    rate ${DOWNLINK}kbit ceil ${DOWNLINK}kbit \
    overhead $overhead $linklayer

tc class add dev ${DEV} parent 1: classid 1:2 htb \
    rate ${DOWNLINK2}kbit ceil ${DOWNLINK2}kbit \


# Then we go through and add a number of classes to the
# root qdisc. With some qdiscs the classes are automatically
# created. With HTB they are not so we add 4 in total
# with different rate limits each

# [[[ example to divide between $[8*$DOWNLINK/10]kbit ]]]


##########################################
# Class:  1:10
# Mark :  10  
# Description: Interactive traffic
##########################################
procent=20
ceil_procent=20
tc class add dev ${DEV} parent 1:1 classid 1:10 htb \
    rate $[${procent}*${RATE}/100]kbit \
    ceil $[${ceil_procent}*${DOWNLINK}/100]kbit \
    prio 0 \
    overhead $overhead $linklayer

tc qdisc add dev ${DEV} parent 1:10 handle 4210: \
    sfq perturb 10 limit 50

tc filter add dev ${DEV} parent 1:0 protocol ip \
    prio 10 handle 0x10010 fw flowid 1:10


tc class add dev ${DEV} parent 1:2 classid 1:102 htb \
    rate $[${procent}*${RATE2}/100]kbit \
    ceil $[${ceil_procent}*${DOWNLINK2}/100]kbit \
    prio 0 \

tc qdisc add dev ${DEV} parent 1:102 handle 102: \
    sfq perturb 10 limit 50

tc filter add dev ${DEV} parent 1:0 protocol ip \
    prio 10 handle 0x20010 fw flowid 1:102


##########################################
# Class:  1:20
# Mark :  20  
# Description: ACK "class"
##########################################
tc class add dev ${DEV} parent 1:1 classid 1:20 htb \
    rate ${ACK_rate}kbit \
    ceil ${ACK_ceil}kbit \
    prio 1 \
    overhead $overhead $linklayer

tc qdisc add dev ${DEV} parent 1:20 handle 4220: \
    sfq perturb 10 limit 50

tc filter add dev ${DEV} parent 1:0 protocol ip \
    prio 20 handle 0x10020 fw flowid 1:20


tc class add dev ${DEV} parent 1:2 classid 1:202 htb \
    rate ${ACK_rate}kbit \
    ceil ${ACK_ceil}kbit \
    prio 1 \

tc qdisc add dev ${DEV} parent 1:202 handle 202: \
    sfq perturb 10 limit 50

tc filter add dev ${DEV} parent 1:0 protocol ip \
    prio 20 handle 0x20020 fw flowid 1:202


##########################################
# Class:  1:30
# Mark :  30  
# Description: Good traffic
##########################################
procent=28
ceil_procent=80
tc class add dev ${DEV} parent 1:1 classid 1:30 htb \
    rate $[${procent}*${RATE}/100]kbit \
    ceil $[${ceil_procent}*${DOWNLINK}/100]kbit \
    prio 4 \
    overhead $overhead $linklayer

tc qdisc add dev ${DEV} parent 1:30 handle 4230: \
    sfq perturb 10 limit 64

tc filter add dev ${DEV} parent 1:0 protocol ip \
    prio 30 handle 0x10030 fw flowid 1:30


procent=28
ceil_procent=80
tc class add dev ${DEV} parent 1:2 classid 1:302 htb \
    rate $[${procent}*${RATE2}/100]kbit \
    ceil $[${ceil_procent}*${DOWNLINK2}/100]kbit \
    prio 4 \

tc qdisc add dev ${DEV} parent 1:302 handle 302: \
    sfq perturb 10 limit 64

tc filter add dev ${DEV} parent 1:0 protocol ip \
    prio 30 handle 0x20030 fw flowid 1:302



# web browsing
#tc class add dev ppp0 parent 1:1  classid 1:30 htb \
#        rate 300kbit ceil 300kbit prio 1
#        
#tc qdisc add dev ppp0 parent 1:30 handle 30: sfq perturb 10
#tc filter add dev ppp0 parent 1:0 prio 0 protocol ip handle 30 fw flowid 1:30

##########################################
# Class:  1:40
# Mark :  40  
# Description: Default traffic
##########################################
procent=22
ceil_procent=80
tc class add dev ${DEV} parent 1:1 classid 1:40 htb \
    rate $[${procent}*${RATE}/100]kbit \
    ceil $[${ceil_procent}*${DOWNLINK}/100]kbit \
    prio 4 \
    overhead $overhead $linklayer

tc qdisc add dev ${DEV} parent 1:40 handle 4240: \
    sfq perturb 10 limit 128

tc filter add dev ${DEV} parent 1:0 protocol ip \
    prio 40 handle 0x10040 fw flowid 1:40


procent=22
ceil_procent=80
tc class add dev ${DEV} parent 1:2 classid 1:402 htb \
    rate $[${procent}*${RATE2}/100]kbit \
    ceil $[${ceil_procent}*${DOWNLINK2}/100]kbit \
    prio 4 \

tc qdisc add dev ${DEV} parent 1:402 handle 402: \
    sfq perturb 10 limit 128

tc filter add dev ${DEV} parent 1:0 protocol ip \
    prio 40 handle 0x20040 fw flowid 1:402



##########################################
# Class:  1:50
# Mark :  50  
# Description: Bulk
##########################################
procent=20
ceil_procent=95
tc class add dev ${DEV} parent 1:1 classid 1:50 htb \
    rate $[${procent}*${RATE}/100]kbit \
    ceil $[${ceil_procent}*${DOWNLINK}/100]kbit \
    prio 4 \
    overhead $overhead $linklayer

tc qdisc add dev ${DEV} parent 1:50 handle 4250: \
    sfq perturb 10 limit 64

tc filter add dev ${DEV} parent 1:0 protocol ip \
    prio 50 handle 0x10050 fw flowid 1:50


procent=20
ceil_procent=95
tc class add dev ${DEV} parent 1:2 classid 1:502 htb \
    rate $[${procent}*${RATE2}/100]kbit \
    ceil $[${ceil_procent}*${DOWNLINK2}/100]kbit \
    prio 4 \

tc qdisc add dev ${DEV} parent 1:502 handle 502: \
    sfq perturb 10 limit 64

tc filter add dev ${DEV} parent 1:0 protocol ip \
    prio 50 handle 0x20050 fw flowid 1:502


##########################################
# Class: 1:666 
# Mark : 666   
# Description: Bad traffic 
##########################################
procent=10
ceil_procent=100
tc class add dev ${DEV} parent 1:1 classid 1:666 htb \
    rate $[${procent}*${RATE}/100]kbit \
    ceil $[${ceil_procent}*${DOWNLINK}/100]kbit \
    prio 7 \
    overhead $overhead $linklayer

tc qdisc add dev ${DEV} parent 1:666 handle 666: \
    sfq perturb 10 limit 128
    #sfq perturb 10 limit 64

tc filter add dev ${DEV} parent 1:0 protocol ip \
    prio 666 handle 0x10666 fw flowid 1:666


procent=10
ceil_procent=10
tc class add dev ${DEV} parent 1:2 classid 1:6662 htb \
    rate $[${procent}*${RATE2}/100]kbit \
    ceil $[${ceil_procent}*${DOWNLINK2}/100]kbit \
    prio 7 \

tc qdisc add dev ${DEV} parent 1:6662 handle 6662: \
    sfq perturb 10 limit 128
    #sfq perturb 10 limit 64

tc filter add dev ${DEV} parent 1:0 protocol ip \
    prio 666 handle 0x20666 fw flowid 1:6662


# experimental hash filters
tc filter add dev eth0 parent 4210: protocol ip handle 0x10010 flow hash keys nfct-dst divisor 1024
tc filter add dev eth0 parent 4220: protocol ip handle 0x10020 flow hash keys nfct-dst divisor 1024
tc filter add dev eth0 parent 4230: protocol ip handle 0x10030 flow hash keys nfct-dst divisor 1024
tc filter add dev eth0 parent 4240: protocol ip handle 0x10040 flow hash keys nfct-dst divisor 1024
tc filter add dev eth0 parent 4250: protocol ip handle 0x10050 flow hash keys nfct-dst divisor 1024
tc filter add dev eth0 parent 666: protocol ip handle 0x10666 flow hash keys nfct-dst divisor 1024

tc filter add dev eth0 parent 102: protocol ip handle 0x20010 flow hash keys nfct-dst divisor 1024
tc filter add dev eth0 parent 202: protocol ip handle 0x20020 flow hash keys nfct-dst divisor 1024
tc filter add dev eth0 parent 302: protocol ip handle 0x20030 flow hash keys nfct-dst divisor 1024
tc filter add dev eth0 parent 402: protocol ip handle 0x20040 flow hash keys nfct-dst divisor 1024
tc filter add dev eth0 parent 502: protocol ip handle 0x20050 flow hash keys nfct-dst divisor 1024
tc filter add dev eth0 parent 6662: protocol ip handle 0x20666 flow hash keys nfct-dst divisor 1024

########################################## End of downlink configuration ##########################################



###################################################################################################################
## uplink via ppp0 ##

DEV=ppp0

# Calculate the ACK_rate from the $DOWNLINK
# ----------------------   
if [ -z "$ACK_rate" -a -n "$DOWNLINK" ]; then
    # Will set the variable $ACK_rate
    ACK_rate_calc $DOWNLINK
    #echo "ACK rate calculated to: $ACK_rate "
    #echo "(for a $LINE_DOWNSTREAM Kbit downstream link)"
fi
 
# Set ACK_rate to 1kbit if not set (zero makes tc command fail)
if [ -z "$ACK_rate" ]; then
    # echo "Cannot determine ACK rate to reserve, need more input"
    ACK_rate=1
fi

#ACK_ceil=$CEIL
ACK_ceil=$[90*${UPLINK}/100] 
#ACK_ceil=$[2*${ACK_rate}]
  
# Rate bandwidth left for Classes
# 
RATE=$[${UPLINK}-${ACK_rate}]

# A qdisc is a whole set of shaping rules that we apply
# Here we add a HTB qdisc to the interface ppp0
# This is known as the root for the interface
# Default class is 40
#tc qdisc add dev ppp0 root handle 1: htb default 40

# Then we add to the root some overall rate limits
#tc class add dev ppp0 parent 1: classid 1:1 htb rate ${UPLINK}kbit

tc qdisc add dev ${DEV} root        handle 1: htb default 50 r2q 1

tc class add dev ${DEV} parent 1: classid 1:1 htb \
    rate ${UPLINK}kbit ceil ${UPLINK}kbit \
    overhead $overhead $linklayer

# Then we go through and add a number of classes to the
# root qdisc. With some qdiscs the classes are automatically
# created. With HTB they are not so we add 4 in total
# with different rate limits each


# [[[ example to divide between $[8*$DOWNLINK/10]kbit ]]]

##########################################
# Class:  1:10
# Mark :  10  
# Description: Interactive traffic
##########################################
procent=20
ceil_procent=20
tc class add dev ${DEV} parent 1:1 classid 1:10 htb \
    rate $[${procent}*${RATE}/100]kbit \
    ceil $[${ceil_procent}*${UPLINK}/100]kbit \
    prio 0 \
    overhead $overhead $linklayer

tc qdisc add dev ${DEV} parent 1:10 handle 4210: \
    sfq perturb 10 limit 50

tc filter add dev ${DEV} parent 1:0 protocol ip \
    prio 10 handle 0x10/0xFFFF fw flowid 1:10


##########################################
# Class:  1:20
# Mark :  20  
# Description: ACK "class"
##########################################
tc class add dev ${DEV} parent 1:1 classid 1:20 htb \
    rate ${ACK_rate}kbit \
    ceil ${ACK_ceil}kbit \
    prio 1 \
    overhead $overhead $linklayer

tc qdisc add dev ${DEV} parent 1:20 handle 4220: \
    sfq perturb 10 limit 50

tc filter add dev ${DEV} parent 1:0 protocol ip \
    prio 20 handle 0x20/0xFFFF fw flowid 1:20


##########################################
# Class:  1:30
# Mark :  30  
# Description: Good traffic
##########################################
procent=28
ceil_procent=80
tc class add dev ${DEV} parent 1:1 classid 1:30 htb \
    rate $[${procent}*${RATE}/100]kbit \
    ceil $[${ceil_procent}*${UPLINK}/100]kbit \
    prio 4 \
    overhead $overhead $linklayer

tc qdisc add dev ${DEV} parent 1:30 handle 4230: \
    sfq perturb 10 limit 64

tc filter add dev ${DEV} parent 1:0 protocol ip \
    prio 30 handle 0x30/0xFFFF fw flowid 1:30



# web browsing
#tc class add dev ppp0 parent 1:1  classid 1:30 htb \
#        rate 300kbit ceil 300kbit prio 1
#        
#tc qdisc add dev ppp0 parent 1:30 handle 30: sfq perturb 10
#tc filter add dev ppp0 parent 1:0 prio 0 protocol ip handle 30 fw flowid 1:30

##########################################
# Class:  1:40
# Mark :  40  
# Description: Default traffic
##########################################
procent=22
ceil_procent=80
tc class add dev ${DEV} parent 1:1 classid 1:40 htb \
    rate $[${procent}*${RATE}/100]kbit \
    ceil $[${ceil_procent}*${UPLINK}/100]kbit \
    prio 4 \
    overhead $overhead $linklayer

tc qdisc add dev ${DEV} parent 1:40 handle 4240: \
    sfq perturb 10 limit 128

tc filter add dev ${DEV} parent 1:0 protocol ip \
    prio 40 handle 0x40/0xFFFF fw flowid 1:40



##########################################
# Class:  1:50
# Mark :  50  
# Description: Bulk
##########################################
procent=20
ceil_procent=95
tc class add dev ${DEV} parent 1:1 classid 1:50 htb \
    rate $[${procent}*${RATE}/100]kbit \
    ceil $[${ceil_procent}*${UPLINK}/100]kbit \
    prio 4 \
    overhead $overhead $linklayer

tc qdisc add dev ${DEV} parent 1:50 handle 4250: \
    sfq perturb 10 limit 64

tc filter add dev ${DEV} parent 1:0 protocol ip \
    prio 50 handle 0x50/0xFFFF fw flowid 1:50


##########################################
# Class: 1:666 
# Mark : 666   
# Description: Bad traffic 
##########################################
procent=10
ceil_procent=100
tc class add dev ${DEV} parent 1:1 classid 1:666 htb \
    rate $[${procent}*${RATE}/100]kbit \
    ceil $[${ceil_procent}*${UPLINK}/100]kbit \
    prio 7 \
    overhead $overhead $linklayer

tc qdisc add dev ${DEV} parent 1:666 handle 666: \
    sfq perturb 10 limit 128
    #sfq perturb 10 limit 64

tc filter add dev ${DEV} parent 1:0 protocol ip \
    prio 666 handle 0x666/0xFFFF fw flowid 1:666




tc filter add dev ppp0 parent 4210: protocol ip handle 10 flow hash keys nfct-src divisor 1024
tc filter add dev ppp0 parent 4220: protocol ip handle 20 flow hash keys nfct-src divisor 1024
tc filter add dev ppp0 parent 4230: protocol ip handle 30 flow hash keys nfct-src divisor 1024
tc filter add dev ppp0 parent 4240: protocol ip handle 40 flow hash keys nfct-src divisor 1024
tc filter add dev ppp0 parent 4250: protocol ip handle 50 flow hash keys nfct-src divisor 1024
tc filter add dev ppp0 parent 666: protocol ip handle 666 flow hash keys nfct-src divisor 1024

################################# End of uplink configuration #####################################################

###################################################################################################################
## uplink via eth1 ##

DEV=eth1

# Calculate the ACK_rate from the $DOWNLINK
# ----------------------   
if [ -z "$ACK_rate" -a -n "$DOWNLINK2" ]; then
    # Will set the variable $ACK_rate
    ACK_rate_calc $DOWNLINK2
    #echo "ACK rate calculated to: $ACK_rate "
    #echo "(for a $LINE_DOWNSTREAM Kbit downstream link)"
fi
 
# Set ACK_rate to 1kbit if not set (zero makes tc command fail)
if [ -z "$ACK_rate" ]; then
    # echo "Cannot determine ACK rate to reserve, need more input"
    ACK_rate=1
fi

#ACK_ceil=$CEIL
ACK_ceil=$[90*${UPLINK2}/100] 
#ACK_ceil=$[2*${ACK_rate}]
  
# Rate bandwidth left for Classes
# 
RATE2=$[${UPLINK2}-${ACK_rate}]

# A qdisc is a whole set of shaping rules that we apply
# Here we add a HTB qdisc to the interface ppp0
# This is known as the root for the interface
# Default class is 40
#tc qdisc add dev ppp0 root handle 1: htb default 40

# Then we add to the root some overall rate limits
#tc class add dev ppp0 parent 1: classid 1:1 htb rate ${UPLINK}kbit

tc qdisc add dev ${DEV} root        handle 1: htb default 50 r2q 1

tc class add dev ${DEV} parent 1: classid 1:1 htb \
    rate ${UPLINK2}kbit ceil ${UPLINK2}kbit \

# Then we go through and add a number of classes to the
# root qdisc. With some qdiscs the classes are automatically
# created. With HTB they are not so we add 4 in total
# with different rate limits each


# [[[ example to divide between $[8*$DOWNLINK/10]kbit ]]]

##########################################
# Class:  1:10
# Mark :  10  
# Description: Interactive traffic
##########################################
procent=20
ceil_procent=20
tc class add dev ${DEV} parent 1:1 classid 1:10 htb \
    rate $[${procent}*${RATE2}/100]kbit \
    ceil $[${ceil_procent}*${UPLINK2}/100]kbit \
    prio 0 \

tc qdisc add dev ${DEV} parent 1:10 handle 4210: \
    sfq perturb 10 limit 50

tc filter add dev ${DEV} parent 1:0 protocol ip \
    prio 10 handle 0x10/0xFFFF fw flowid 1:10


##########################################
# Class:  1:20
# Mark :  20  
# Description: ACK "class"
##########################################
tc class add dev ${DEV} parent 1:1 classid 1:20 htb \
    rate ${ACK_rate}kbit \
    ceil ${ACK_ceil}kbit \
    prio 1

tc qdisc add dev ${DEV} parent 1:20 handle 4220: \
    sfq perturb 10 limit 50

tc filter add dev ${DEV} parent 1:0 protocol ip \
    prio 20 handle 0x20/0xFFFF fw flowid 1:20


##########################################
# Class:  1:30
# Mark :  30  
# Description: Good traffic
##########################################
procent=28
ceil_procent=80
tc class add dev ${DEV} parent 1:1 classid 1:30 htb \
    rate $[${procent}*${RATE2}/100]kbit \
    ceil $[${ceil_procent}*${UPLINK2}/100]kbit \
    prio 4

tc qdisc add dev ${DEV} parent 1:30 handle 4230: \
    sfq perturb 10 limit 64

tc filter add dev ${DEV} parent 1:0 protocol ip \
    prio 30 handle 0x30/0xFFFF fw flowid 1:30



# web browsing
#tc class add dev ppp0 parent 1:1  classid 1:30 htb \
#        rate 300kbit ceil 300kbit prio 1
#        
#tc qdisc add dev ppp0 parent 1:30 handle 30: sfq perturb 10
#tc filter add dev ppp0 parent 1:0 prio 0 protocol ip handle 30 fw flowid 1:30

##########################################
# Class:  1:40
# Mark :  40  
# Description: Default traffic
##########################################
procent=22
ceil_procent=80
tc class add dev ${DEV} parent 1:1 classid 1:40 htb \
    rate $[${procent}*${RATE2}/100]kbit \
    ceil $[${ceil_procent}*${UPLINK2}/100]kbit \
    prio 4

tc qdisc add dev ${DEV} parent 1:40 handle 4240: \
    sfq perturb 10 limit 128

tc filter add dev ${DEV} parent 1:0 protocol ip \
    prio 40 handle 0x40/0xFFFF fw flowid 1:40



##########################################
# Class:  1:50
# Mark :  50  
# Description: Bulk
##########################################
procent=20
ceil_procent=95
tc class add dev ${DEV} parent 1:1 classid 1:50 htb \
    rate $[${procent}*${RATE2}/100]kbit \
    ceil $[${ceil_procent}*${UPLINK2}/100]kbit \
    prio 4

tc qdisc add dev ${DEV} parent 1:50 handle 4250: \
    sfq perturb 10 limit 64

tc filter add dev ${DEV} parent 1:0 protocol ip \
    prio 50 handle 0x50/0xFFFF fw flowid 1:50


##########################################
# Class: 1:666 
# Mark : 666   
# Description: Bad traffic 
##########################################
procent=10
ceil_procent=100
tc class add dev ${DEV} parent 1:1 classid 1:666 htb \
    rate $[${procent}*${RATE2}/100]kbit \
    ceil $[${ceil_procent}*${UPLINK2}/100]kbit \
    prio 7

tc qdisc add dev ${DEV} parent 1:666 handle 666: \
    sfq perturb 10 limit 128
    #sfq perturb 10 limit 64

tc filter add dev ${DEV} parent 1:0 protocol ip \
    prio 666 handle 0x666/0xFFFF fw flowid 1:666




tc filter add dev ppp0 parent 4210: protocol ip handle 10 flow hash keys nfct-src divisor 1024
tc filter add dev ppp0 parent 4220: protocol ip handle 20 flow hash keys nfct-src divisor 1024
tc filter add dev ppp0 parent 4230: protocol ip handle 30 flow hash keys nfct-src divisor 1024
tc filter add dev ppp0 parent 4240: protocol ip handle 40 flow hash keys nfct-src divisor 1024
tc filter add dev ppp0 parent 4250: protocol ip handle 50 flow hash keys nfct-src divisor 1024
tc filter add dev ppp0 parent 666: protocol ip handle 666 flow hash keys nfct-src divisor 1024

################################# End of uplink configuration #####################################################

