#!/bin/sh

################################################################################
# Modification History;
#
# Date------ Author----- Description--------------------------------------------
# 2007/11/26 M. Pepin    Initial creation
# 2008/09/12 M. Pepin    Support 8 queues instead of 4
#
################################################################################

TC=/sbin/tc

# We always expect either 2 or 10 parameters (0..9) where the first $0 parameter is the script path name
if [[ $# -ne 1 && $# -ne 9 ]];
then
        echo "Usage: "
        echo ""
        echo "  $0 <DEV> <Q1_LEN> [<Q2_LEN> <Q3_LEN> <Q4_LEN> <Q5_LEN> <Q6_LEN> <Q7_LEN> <Q8_LEN>]"
        echo "     where:"
        echo "          <DEV>       -- net device name to be configured with PRIO queuing discipline"
        echo "          <Q1..8_LEN> -- the length of the NNSC queue"
        echo ""
        echo "Example:"
        echo ""
        echo "  $0 eth1"
        echo "  $0 eth1 100 100 600 100 100 100 100 100"
        echo ""
        echo "To print network device statistics use the 'tc' utility instead:"
        echo ""
        echo "  $TC -s qdisc show dev eth1"
        echo "  $TC -s class show dev eth1"

        exit 1
fi

EXIT_CODE=0

DEV=$1

if [ $# -eq 1 ];
then

source /nn/etc/qos/tcprio_default_settings.conf

else

Q1_LEN=$2
Q2_LEN=$3
Q3_LEN=$4
Q4_LEN=$5
Q5_LEN=$6
Q6_LEN=$7
Q7_LEN=$8
Q8_LEN=$9

fi

# Free previous QoS Settings
$TC qdisc del dev $DEV root handle 1: prio  2> /dev/null
$TC qdisc del dev $DEV root handle 1: htb default 4 2> /dev/null 

# Attach prio to <network device>
$TC qdisc add dev $DEV root handle 1: prio bands 8 priomap  7 0 1 2 3 4 5 6 7 7 7 7 7 7 7 7 ; EXIT_CODE=$(($EXIT_CODE+$?))

# Attach queuing disciplines to the leaf classes
$TC qdisc add dev $DEV parent 1:1 handle 10: pfifo limit $Q1_LEN; EXIT_CODE=$(($EXIT_CODE+$?))
$TC qdisc add dev $DEV parent 1:2 handle 20: pfifo limit $Q2_LEN; EXIT_CODE=$(($EXIT_CODE+$?))
$TC qdisc add dev $DEV parent 1:3 handle 30: pfifo limit $Q3_LEN; EXIT_CODE=$(($EXIT_CODE+$?))
$TC qdisc add dev $DEV parent 1:4 handle 40: pfifo limit $Q4_LEN; EXIT_CODE=$(($EXIT_CODE+$?))
$TC qdisc add dev $DEV parent 1:5 handle 50: pfifo limit $Q5_LEN; EXIT_CODE=$(($EXIT_CODE+$?))
$TC qdisc add dev $DEV parent 1:6 handle 60: pfifo limit $Q6_LEN; EXIT_CODE=$(($EXIT_CODE+$?))
$TC qdisc add dev $DEV parent 1:7 handle 70: pfifo limit $Q7_LEN; EXIT_CODE=$(($EXIT_CODE+$?))
$TC qdisc add dev $DEV parent 1:8 handle 80: pfifo limit $Q8_LEN; EXIT_CODE=$(($EXIT_CODE+$?))

# List the stats, see if queues were programmed correctly
# $TC -s -d qdisc ls dev $DEV

exit $EXIT_CODE

