#!/bin/sh

# AstShape
# Based off of WonderShaper (HTB)
# Enhanced by Kristian Kielhofner <kris@krisk.org>
# Make sure that all of your VoIP devices set tos on RTP to 0x18
# iax.conf: tos=0x18 sip.conf: tos=0x18

# Default values
QOS_ENABLE=1
QOS_DEVICE="eth0"
#VOIP priority ports
#INT priority ports

# configuration file
QOS_CONFIG_FILE=
QOS_COMMAND=$1

# qos configuration file

for DEV in $QOS_DEVICE
do
if [ "$QOS_COMMAND" = "status" ]
then
  echo $QOS_COMMAND of $DEV
	tc -s qdisc ls dev $DEV
	tc -s class ls dev $DEV
  echo
else
  # clean existing down- and uplink qdiscs, hide errors
  tc qdisc del dev $DEV root    2> /dev/null > /dev/null
  tc qdisc del dev $DEV ingress 2> /dev/null > /dev/null
  # Need to start QOS ?
  if [ "$QOS_COMMAND" = "start" ] && [ "$QOS_ENABLE" = 1 ]
  then
    ###### uplink

    #install root HTB, point default traffic to 1:3
    tc qdisc add dev $DEV root handle 1: prio

    #all get Stochastic Fairness
    tc qdisc add dev $DEV parent 1:1 handle 10: sfq
    tc qdisc add dev $DEV parent 1:2 handle 20: sfq 
    tc qdisc add dev $DEV parent 1:3 handle 30: sfq 
   
    echo "Setting 801.1q" 
    tc filter add dev $DEV parent 1:0 protocol 0x5200 prio 10 u32 match u32 0 0 flowid 1:1
    echo "Setting UDP IP" 
    tc filter add dev $DEV parent 1:0 protocol ip prio 20 u32 match ip protocol 17 0xff flowid 1:2

    #rest is 'non-interactive' ie 'bulk' and ends up in 1:3
    tc filter add dev $DEV parent 1: protocol ip prio 30 u32 match ip dst 0.0.0.0/0 flowid 1:3

  fi
fi

done


exit


