From: Michael Tremer Date: Sat, 28 Mar 2009 21:08:58 +0000 (+0100) Subject: Worked on networking again. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cd1bc6844dd99f6e19a10f4d18ba56bc4b350f34;p=ipfire-3.x.git Worked on networking again. --- diff --git a/src/initscripts/core/network b/src/initscripts/core/network index 4e4410347..6eefa9a4f 100644 --- a/src/initscripts/core/network +++ b/src/initscripts/core/network @@ -44,8 +44,14 @@ case "${1}" in ${0} start ;; + reload) + if [ -e /var/lock/subsys/network ]; then + : # TODO + fi + ;; + *) - echo "Usage: ${0} {start|stop|restart}" + echo "Usage: ${0} {start|stop|restart|reload}" exit 1 ;; esac diff --git a/src/initscripts/networking/functions b/src/initscripts/networking/functions index e4b89f8e7..13619cfeb 100644 --- a/src/initscripts/networking/functions +++ b/src/initscripts/networking/functions @@ -24,6 +24,8 @@ CONFIG_DIR=/etc/sysconfig/networking CONFIG_ZONES=${CONFIG_DIR}/zones CONFIG_PORTS=${CONFIG_DIR}/ports +COMMON_DEVICE=black+ + function is_mac() { egrep -q "^[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]$" <<<$1 } @@ -133,6 +135,10 @@ function device_exists() { ip link show $(devicify ${1}) &>/dev/null } +function device_is_up() { + ip link show $(devicify ${1}) 2>/dev/null | grep -qE "<.*UP.*>" +} + function rename_device() { local source local destination @@ -169,6 +175,14 @@ function zone_exists() { [ -e "$CONFIG_ZONES/$1" ] #|| device_exists $@ } +function port_is_up() { + device_is_up $@ +} + +function zone_is_up() { + zone_exists $@ && device_is_up $@ +} + function bridge_devices() { local bridge bridge=$1 diff --git a/src/initscripts/networking/hooks/ethernet b/src/initscripts/networking/hooks/ethernet index b88a82add..392642092 100644 --- a/src/initscripts/networking/hooks/ethernet +++ b/src/initscripts/networking/hooks/ethernet @@ -18,13 +18,47 @@ port=$(macify ${1}) +function port_name() { + echo ${ZONE}s+ +} + +function do_up() { + : # Do nothing +} + +function do_down() { + : # Do nothing +} + +function do_attach() { + rename_device $(get_device ${port}) $(port_name) + zone_add_port ${ZONE} $(get_device_by_mac ${port}) +} + +function do_detach() { + zone_del_port ${ZONE} $(get_device_by_mac ${port}) + rename_device $(get_device_by_mac ${port}) ${COMMON_DEVICE} +} + +function do_status() { + device_is_up ${port} + RET=$? + if [ $RET -eq 0 ]; then + log_success_msg "Port $(port_name) is up" + else + log_failure_msg "Port $(port_name) is down" + fi + return $RET + # TODO: Check if device is attached to a bridge. +} + case "${2}" in up) - : # Do nothing + do_up ;; down) - : # Do nothing + do_down ;; add) @@ -36,22 +70,29 @@ EOF ;; remove) + do_detach + #do_down rm -f \ ${CONFIG_ZONES}/${ZONE}/port-${port}-ethernet \ ${CONFIG_PORTS}/${port}/ethernet ;; attach) - rename_device $(get_device ${port}) ${ZONE}s+ - zone_add_port ${ZONE} $(get_device_by_mac ${port}) + do_up + do_attach ;; detach) - zone_del_port ${ZONE} $(get_device_by_mac ${port}) + do_detach + ;; + + status) + do_status + exit ${?} ;; *) - echo "Usage: ${0} [interface] {up|down|add|remove|attach|detach}" + echo "Usage: ${0} [interface] {up|down|add|remove|attach|detach|status}" exit 1 ;; esac diff --git a/src/initscripts/networking/hooks/vlan b/src/initscripts/networking/hooks/vlan index 682c96bf5..3feaef976 100644 --- a/src/initscripts/networking/hooks/vlan +++ b/src/initscripts/networking/hooks/vlan @@ -19,22 +19,54 @@ port=$(macify ${1}) device=$(devicify ${port}) +function port_name() { + echo "${ZONE}v${ID}" +} + +function do_up() { + if ! port_is_up $(port_name); then + grep -q ^8021q /proc/modules || modprobe 8021q + MESSAGE="Adding VLAN ${ID} to port ${port}..." + vconfig add ${device} ${ID} >/dev/null + evaluate_retval + fi +} + +function do_down() { + if port_is_up $(port_name); then + MESSAGE="Removing VLAN ${ID} from port ${port}..." + vconfig rem ${device} ${ID} >/dev/null + evaluate_retval + fi +} + +function do_attach() { + rename_device $(get_device_by_mac_and_vid ${port} ${ID}) $(port_name) + zone_add_port ${ZONE} $(get_device ${port} ${ID}) +} + +function do_detach() { + zone_del_port ${ZONE} $(get_device_by_mac_and_vid ${port} ${ID}) +} + +function do_status() { + device_is_up $(port_name) + RET=$? + if [ $RET -eq 0 ]; then + log_success_msg "Port $(port_name) is up" + else + log_failure_msg "Port $(port_name) is down" + fi + return $RET +} + case "${2}" in up) - if ! get_device_by_mac_and_vid ${port} ${ID} >/dev/null; then - modprobe 8021q - MESSAGE="Adding VLAN ${ID} to port ${port}..." - vconfig add ${device} ${ID} >/dev/null - evaluate_retval - fi + do_up ;; down) - if get_device_by_mac_and_vid ${port} ${ID} >/dev/null; then - MESSAGE="Removing VLAN ${ID} from port ${port}..." - vconfig rem ${device} ${ID} >/dev/null - evaluate_retval - fi + do_down ;; add) @@ -48,24 +80,28 @@ EOF ;; remove) - # Should we do this here? What about detaching? - CONFIG=${CONFIG_ZONES}/${ZONE}/port-${port}-vlan-${ID} ${0} ${port} down + do_detach + do_down rm -f \ ${CONFIG_PORTS}/${port}/vlan-${ID} \ ${CONFIG_ZONES}/${ZONE}/port-${port}-vlan-${ID} ;; attach) - rename_device $(get_device_by_mac_and_vid ${port} ${ID}) ${ZONE}v${ID} - zone_add_port ${ZONE} $(get_device ${port} ${ID}) + do_attach ;; detach) - zone_del_port ${ZONE} $(get_device_by_mac_and_vid ${port} ${ID}) + do_detach + ;; + + status) + do_status + exit ${?} ;; *) - echo "Usage: ${0} [interface] {up|down|add|remove|attach|detach}" + echo "Usage: ${0} [interface] {up|down|add|remove|attach|detach|status}" exit 1 ;; esac diff --git a/src/initscripts/networking/port b/src/initscripts/networking/port index 2856e2fc7..3fbd2bd6e 100644 --- a/src/initscripts/networking/port +++ b/src/initscripts/networking/port @@ -28,7 +28,7 @@ action=$2 shift 2 if [ -z "${port}" ] || [ -z "${action}" ]; then - echo "Usage: $0 [hooks]" + echo "Usage: $0 [hooks]" echo exit 1 fi @@ -55,13 +55,24 @@ case "$action" in down) ;; - attach|detach) + attach) + ip link set $(devicify ${port}) up # is this required here? if [ -z "${ZONE}" ]; then log_failure_msg "ZONE is not set." exit 1 fi ;; + detach) + if [ -z "${ZONE}" ]; then + log_failure_msg "ZONE is not set." + exit 1 + fi + ;; + + status) + ;; + *) log_failure_msg "\"${action}\" is not a valid command." exit 1 @@ -74,6 +85,7 @@ for hook in ${hooks}; do . ${hook} if [ -n "${HOOK}" -a -x "/etc/init.d/networking/hooks/${HOOK}" ]; then CONFIG=${hook} /etc/init.d/networking/hooks/${HOOK} ${port} ${action} + RET=$? else echo -e "${FAILURE}Unable to process ${hook}. Either" echo -e "${FAILURE}the HOOK variable was not set," @@ -81,13 +93,20 @@ for hook in ${hooks}; do message="" log_failure_msg fi - ) + exit ${RET} + ) || failed=1 done case "${action}" in down) - message="Pushing down port ${port}..." - ip link set $(devicify ${port}) down - evaluate_retval + # If no ports are running yet, push device down. + if ! $0 ${port} status &>/dev/null; then + message="Pushing down port ${port}..." + ip link set $(devicify ${port}) down + evaluate_retval + fi + ;; + status) + exit ${failed} ;; esac diff --git a/src/initscripts/networking/zone b/src/initscripts/networking/zone index 075afa985..d2ae48b01 100644 --- a/src/initscripts/networking/zone +++ b/src/initscripts/networking/zone @@ -49,12 +49,6 @@ case "$action" in (exit ${failed}) evaluate_retval standard - # We should bring up every port - for config in ${CONFIG_ZONES}/${zone}/port-*; do - port=${config##*/}; port=${port#port-}; port=${port%%-*} - ZONE=${zone} /etc/init.d/networking/port ${port} up - done - # Attach ports for config in ${CONFIG_ZONES}/${zone}/port-*; do port=${config##*/}; port=${port#port-}; port=${port%%-*} @@ -78,9 +72,8 @@ case "$action" in # Detach ports for config in ${CONFIG_ZONES}/${zone}/port-*; do - port=${file#port-}; port=${port%%-*} - ZONE=${zone} CONFIG=${config} \ - /etc/init.d/networking/port ${port} detach + port=${config##*/}; port=${port#port-}; port=${port%%-*} + ZONE=${zone} /etc/init.d/networking/port ${port} detach ${config} done # Bring down the zone and delete it diff --git a/src/network/network b/src/network/network index a0faf0b0c..163ccd8a0 100644 --- a/src/network/network +++ b/src/network/network @@ -157,10 +157,25 @@ function error() { } function _exit() { + if [ $1 -eq 0 ] && [ "$DO_RELOAD" = "1" ]; then + # Reloading network to apply changes immediately + vecho "Reloading network settings..." + cmd $0 $(verbose && echo "-v") $(debug && echo "-d") reload + fi + decho "Exiting with code $1." exit $1 } +function cmd() { + decho "Running command: $@" + if verbose; then + $@ + else + $@ >/dev/null + fi +} + function port_show() { local port @@ -209,7 +224,7 @@ function port_add() { RET=$? if [ "$RET" -eq "0" ]; then vecho "Successfully added port ${BOLD}${port}${NORMAL} (${hook} $@) to ${BOLD}${zone}${NORMAL}." - /etc/init.d/networking/port ${port} up + cmd /etc/init.d/networking/port ${port} up else error "Hook ${BOLD}${hook}${NORMAL} exited with $RET." return $RET @@ -235,8 +250,8 @@ function port_del() { decho " Zone: ${zone} Port: ${port} Hook: ${hook}" if [ -x "/etc/init.d/networking/hooks/${hook}" ]; then - /etc/init.d/networking/port ${port} down ## XXX How do we identify only that one hook? - ZONE=${zone} /etc/init.d/networking/hooks/${hook} ${port} remove $@ + cmd /etc/init.d/networking/port ${port} down ## XXX How do we identify only that one hook? + ZONE=${zone} cmd /etc/init.d/networking/hooks/${hook} ${port} remove $@ RET=$? if [ "$RET" -eq "0" ]; then vecho "Successfully removed port ${BOLD}${port}${NORMAL} (${hook} $@) from ${BOLD}${zone}${NORMAL}." @@ -271,7 +286,7 @@ function zone_show() { echo "##################################################" # Up or down? - if device_exists ${zone}; then + if device_is_up ${zone}; then echo -e "# Device is ${ERROR}up${NORMAL}." else echo -e "# Device is ${ERROR}down${NORMAL}." @@ -280,11 +295,15 @@ function zone_show() { # Ports echo -e "# ${ERROR}Ports:${NORMAL}" + local config local port - for port in ${CONFIG_ZONES}/${zone}/port-*; do - port=${port##*/} - echo "# ${port#port-}" - debug && echo "# TODO: Is port up or down?" + for config in ${CONFIG_ZONES}/${zone}/port-*; do + port=${config##*/}; port=${port#port-}; port=${port%%-*} + if ZONE=${zone} cmd /etc/init.d/networking/port ${port} attach ${config} &>/dev/null; then + echo "# ${config#port-} is up" + else + echo "# ${config#port-} is down" + fi done echo "#" @@ -316,6 +335,9 @@ function zone_del() { vecho "Successfully removed zone ${zone}." } +DO_RELOAD=1 + +# See what to do while [ "$#" -gt 0 ]; do arg=$1 shift @@ -340,7 +362,7 @@ while [ "$#" -gt 0 ]; do case "$arg" in show) port_show $@ - _exit $? + DO_RELOAD=0 _exit $? ;; help) usage port 0 @@ -361,7 +383,7 @@ while [ "$#" -gt 0 ]; do ;; show) zone_show $@ - _exit $? + DO_RELOAD=0 _exit $? ;; addport) port_add $@ @@ -372,10 +394,13 @@ while [ "$#" -gt 0 ]; do _exit $? ;; help) - usage zone 0 + DO_RELOAD=0 usage zone 0 ;; esac ;; + -*) + DO_RELOAD=0 error "Option \"$arg\" is not known." + ;; esac done