]> git.ipfire.org Git - ipfire-3.x.git/commitdiff
Worked on networking again.
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 28 Mar 2009 21:08:58 +0000 (22:08 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 28 Mar 2009 21:08:58 +0000 (22:08 +0100)
src/initscripts/core/network
src/initscripts/networking/functions
src/initscripts/networking/hooks/ethernet
src/initscripts/networking/hooks/vlan
src/initscripts/networking/port
src/initscripts/networking/zone
src/network/network

index 4e4410347421d84295a5d92ca7b90004bf48137a..6eefa9a4f9b9b3b3775d5951db90132dba62d06e 100644 (file)
@@ -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
index e4b89f8e7df2f1e09f5972d352adc6c283d3b44a..13619cfeb895984a4298f962d3e083d26193a83c 100644 (file)
@@ -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
index b88a82addd1528ad2002a38c6bd573b84c6d515e..3926420922c0108d2285042997ef1e2f3a9f2b0a 100644 (file)
 
 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
index 682c96bf5eaae0869ce6d618e1e1258a2d996a4b..3feaef9760eb73a1fcab5382a846a3a3c518bdad 100644 (file)
 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
index 2856e2fc75874edbec8094bad5e7ee66b347bca7..3fbd2bd6e7b7adf95e4425e5b7811816d0e66243 100644 (file)
@@ -28,7 +28,7 @@ action=$2
 shift 2
 
 if [ -z "${port}" ] || [ -z "${action}" ]; then
-       echo "Usage: $0 <port> <up|down|attach|detach> [hooks]"
+       echo "Usage: $0 <port> <up|down|attach|detach|status> [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
index 075afa98580922a2da1dcc6362cd5876ea19cc40..d2ae48b01456d5e6cf7be3cfcd06a9f501be9ab4 100644 (file)
@@ -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
index a0faf0b0ccf45dcc09b637a58596d5123479bed8..163ccd8a0007e7b47444af63be160ba9debef848 100644 (file)
@@ -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