iopsys-feed/port-management/files/etc/init.d/port_management

149 lines
3.1 KiB
Bash
Executable file

#!/bin/sh /etc/rc.common
DEBUG=0
START=15
USE_PROCD=1
. /lib/functions.sh
. /lib/network/config.sh
check_for_config(){
if [ -s "/etc/config/ports" ]
then
debug_print "file exists and has content"
if uci -q get ports.@ethport[0] >/dev/null #are there any valid content then continue
then
return 0
else
rm -f /etc/config/ports
fi
fi
debug_print "ports config file doesn't exsist or is empty"
touch /etc/config/ports
local portnames="$(db get hw.board.ethernetPortNames)"
local portorder="$(db get hw.board.ethernetPortOrder)"
for port in $portorder
do
uci add ports ethport
uci rename ports.@ethport[-1]="$(interfacename $port)"
uci set ports.@ethport[-1].name="$(interfacename $port)"
uci set ports.@ethport[-1].ifname=$port
uci set ports.@ethport[-1].speed='auto'
if [ "$(interfacename $port)" = "WAN" ]
then
uci set ports.@ethport[-1].pause=1
else
uci set ports.@ethport[-1].pause=0
fi
done
uci commit ports
[ $DEBUG ] && cat /etc/config/ports
}
debug_print(){
if [ $DEBUG = "1" ]
then
echo -e $1 >/dev/console
fi
}
get_port_no() {
local ports="0 1 2 3 4 5 6 7"
local port="$1"
local ifname
for p in $ports; do
ifname="$(ethswctl getifname $p | awk '{print$NF}')"
if [ "$ifname" == "$port" ]; then
echo "$p"
break
fi
done
}
get_current_status() {
local port="$1"
local media="$(ethctl $port media-type 2>&1)"
#echo Media: $media >/dev/console
if echo $media | grep "Auto-negotiation enabled" >/dev/null; then
echo "auto"
elif echo $media | grep "100 mbps, full-duplex" >/dev/null; then
echo "100FD"
elif echo $media | grep "100 mbps, half-duplex" >/dev/null; then
echo "100HD"
elif echo $media | grep "10 mbps, full-duplex" >/dev/null; then
echo "10FD"
elif echo $media | grep "10 mbps, half-duplex" >/dev/null; then
echo "10HD"
fi
}
set_port_status() {
local port="$1"
local status="$2"
local curstatus=$(get_current_status $port)
local portno
ifconfig $port >/dev/null 2>&1 || return
case "$status" in
disabled)
ethctl $port phy-power down
;;
*)
if [ "$status" != "$curstatus" ]; then
case "$status" in
1000*)
portno=$(get_port_no $port)
case "$status" in
1000FD) ethswctl -c phymode -p $portno -y 1000 -z 1 ;;
1000HD) ethswctl -c phymode -p $portno -y 1000 -z 0 ;;
esac
;;
10*AUTO)
ethctl $port media-type advertise $status
;;
10*)
ethctl $port media-type $status
;;
*)
ethctl $port media-type advertise 1000FDAUTO
ethctl $port media-type auto
;;
esac
else
ethctl $port phy-power up
fi
;;
esac
}
configure_ethports(){
local port ifname pause speed
name=$1
config_get speed $name speed
config_get ifname $name ifname
config_get pause $name pause
set_port_status $ifname $speed
set_port_pause $ifname $pause
}
#arg1: ethX
#arg2: 0 or 1
set_port_pause() {
local pause="$2"
portno=$(get_port_no $1)
ethswctl -c pause -p $portno -v $pause
}
start_service() {
check_for_config
config_load ports
config_foreach configure_ethports ethport
}
service_triggers() {
procd_add_reload_trigger ports
}