iproute/iproute2-tc.initscript

144 lines
2.8 KiB
Plaintext
Raw Normal View History

#!/bin/bash
#
# tc: apply the tc configuration rules
#
# Version: @(#) /etc/rc.d/init.d/tc
#
# chkconfig: 2345 90 10
# description: Starts and stops the tc at boot time and shutdown.
#
# processname: tc
#
# created: from Raptor
# path to the tc command
tc="/sbin/tc"
# where is the tc config file
tcConf="/etc/sysconfig/traffic_control"
offMsg=OFF
onMsg=ON
debug=0
[ -x "$tc" ] || exit 1
# source function library
. /etc/sysconfig/rc
[ -r "$rc_functions" ] && . $rc_functions
isUp () {
res=`$tc qdisc show dev $1`
[ "$res" ] || return 1
return 0
}
start () {
echo -n "Starting tc services: "
OLDIFS="$IFS"
IFS=""
while read line; do
cmd=`echo $line | grep -v "^#" | sed -e "s/^tc//" | sed -e "s/^[ \t]*//"`
if [ -n "$cmd" ]; then
[ $debug -eq 1 ] && echo "Execute: $cmd:"
eval "$tc $cmd"
fi
done < $tcConf
touch /var/lock/subsys/tc
IFS="$OLDIFS"
evaluate_retval
echo
}
stop () {
echo -n "Stopping some/all tc services: "
# if explictly specified shut only these devices
[ "$1" ] && devs=$*;
#echo $devs
for d in $devs; do
if isUp $d ; then
echo -n "Flushing: $d"
$tc qdisc del root dev $d
$CURS_UP
evaluate_retval
echo
else
echo -n "No traffic control running on: $d"
evaluate_retval
echo
fi
done
# this is not the correct behavior
rm -f /var/lock/subsys/tc
}
status () {
[ "$1" ] && devs=$*
#echo $devs
for d in $devs; do
if isUp $d; then
echo "traffic control on $d: [$onMsg]"
else
echo "traffic control on $d: [$offMsg]"
fi
done
}
show () {
if [ -z "$1" ]; then what=all; else what=$1; fi
shift
if [ "$1" ]; then devs=$*; fi
for d in $devs; do
if isUp $d; then
[ "$what" = "all" ] || [ "$what" = "qdisc" ] &&
echo "`$tc qdisc show dev $d`"
[ "$what" = "all" ] || [ "$what" = "class" ] &&
echo "`$tc class show dev $d`"
[ "$what" = "all" ] || [ "$what" = "filter" ] &&
echo "`$tc filter show dev $d`"
fi
done
}
parse () {
echo "$1" | perl -ne '
$_ =~ s/(\d+?:\d+?)/$1/gs;
print $_
'
}
stat () {
[ -z "$1" ] || [ -z "$2" ] &&
echo "qdisc|class or device has to be specified !!!" && exit
parse "`$tc -s $1 show dev $2`"
}
devs=`ifconfig -a | grep '^\w' | grep -v lo | cut -f 1 -d ' '`
command=$1
shift
case "$command" in
start) start ;;
stop) stop $@ ;;
status) status $@ ;;
show) show $@ ;;
stat) stat $@ ;;
restart)
echo -n "Restarting tc."
stop $@
start $@
;;
*)
echo "*** Usage:
tc {start|stop|status|restart} [devices]
tc show [all|qdisc|class|filter] [devices]
tc stat [qdisc|class] [devices]
"
exit 1
;;
esac
exit 0