]> git.ipfire.org Git - people/stevee/network.git/blobdiff - src/hooks/zones/bridge
Convert HOOK_SETTINGS into an array
[people/stevee/network.git] / src / hooks / zones / bridge
index 021e4402cda18dd70e425640b729f427fd0d8091..0b18331811dbdcd9086848d937026a7390cc6e68 100644 (file)
 
 HOOK_MANPAGE="network-zone-bridge"
 
-HOOK_SETTINGS="HOOK STP STP_FORWARD_DELAY STP_HELLO STP_MAXAGE STP_MODE"
-HOOK_SETTINGS="${HOOK_SETTINGS} STP_PRIORITY MAC MTU"
-
-HOOK_PORT_SETTINGS="COST PRIORITY"
+HOOK_SETTINGS=(
+       "ADDRESS"
+       "STP"
+       "STP_FORWARD_DELAY"
+       "STP_HELLO STP_MAXAGE"
+       "STP_PRIORITY"
+       "MTU"
+)
+
+HOOK_PORT_SETTINGS=(
+       "COST"
+       "PRIORITY"
+)
 
 # Default values
-MAC=$(mac_generate)
-MTU=1500
-STP="on"
-STP_MODE="rstp"
-STP_FORWARD_DELAY=0
-STP_HELLO=2
-STP_MAXAGE=20
-STP_PRIORITY=512
-
-function hook_check() {
-       assert ismac MAC
+DEFAULT_STP="on"
+DEFAULT_STP_FORWARD_DELAY=0
+DEFAULT_STP_HELLO=2
+DEFAULT_STP_MAXAGE=20
+DEFAULT_STP_PRIORITY=512
+
+hook_check_settings() {
+       assert ismac ADDRESS
+       assert isset MTU && assert mtu_is_valid "ethernet" "${MTU}"
+
+       # Spanning Tree Protocol
        assert isbool STP
-       assert isoneof STP_MODE stp rstp
        assert isinteger STP_HELLO
        assert isinteger STP_FORWARD_DELAY
        assert isinteger STP_PRIORITY
-       assert isinteger MTU
 }
 
-function hook_parse_cmdline() {
+hook_parse_cmdline() {
        while [ $# -gt 0 ]; do
                case "${1}" in
-                       --stp=*)
-                               STP=${1#--stp=}
+                       --address=*)
+                               ADDRESS="$(cli_get_val "${1}")"
+
+                               if ! mac_is_valid "${ADDRESS}"; then
+                                       error "Invalid MAC address: ${ADDRESS}"
+                                       return ${EXIT_ERROR}
+                               fi
                                ;;
-                       --stp-mode=*)
-                               STP_MODE=${1#--stp-mode=}
+
+                       --mtu=*)
+                               MTU="$(cli_get_val "${1}")"
+
+                               if ! mtu_is_valid "ethernet" "${MTU}"; then
+                                       error "Invalid MTU: ${MTU}"
+                                       return ${EXIT_ERROR}
+                               fi
                                ;;
-                       --stp-hello=*)
-                               STP_HELLO=${1#--stp-hello=}
+
+                       --stp=*)
+                               STP="$(cli_get_val "${1}")"
+
+                               if enabled STP; then
+                                       STP="on"
+                               elif disabled STP; then
+                                       STP="off"
+                               else
+                                       error "Invalid value for STP: ${STP}"
+                                       return ${EXIT_ERROR}
+                               fi
                                ;;
+
                        --stp-forward-delay=*)
-                               STP_FORWARD_DELAY=${1#--stp-forward-delay=}
+                               STP_FORWARD_DELAY="$(cli_get_val "${1}")"
+
+                               if ! isinteger STP_FORWARD_DELAY; then
+                                       error "Invalid STP forwarding delay: ${STP_FORWARD_DELAY}"
+                                       return ${EXIT_ERROR}
+                               fi
                                ;;
-                       --stp-priority=*)
-                               STP_PRIORITY=${1#--stp-priority=}
+
+                       --stp-hello=*)
+                               STP_HELLO="$(cli_get_val "${1}")"
+
+                               if ! isinteger STP_HELLO; then
+                                       error "Invalid STP hello time: ${STP_HELLO}"
+                                       return ${EXIT_ERROR}
+                               fi
                                ;;
-                       --mtu=*)
-                               MTU=${1#--mtu=}
+
+                       --stp-max-age=*)
+                               STP_MAXAGE="$(cli_get_val "${1}")"
+
+                               if ! isinteger STP_MAXAGE; then
+                                       error "Invalid STP max age: ${STP_MAXAGE}"
+                                       return ${EXIT_ERROR}
+                               fi
                                ;;
-                       --mac=*)
-                               MAC=${1#--mac=}
+
+                       --stp-priority=*)
+                               STP_PRIORITY="$(cli_get_val "${1}")"
+
+                               if ! isinteger STP_PRIORITY; then
+                                       error "Invalid STP priority: ${STP_PRIORITY}"
+                                       return ${EXIT_ERROR}
+                               fi
                                ;;
+
                        *)
-                               warning "Ignoring unknown option '${1}'"
+                               error "Unknown argument: ${1}"
+                               return ${EXIT_ERROR}
                                ;;
                esac
                shift
        done
+
+       # Generate a random MAC address if the user passed none
+       if ! isset ADDRESS; then
+               ADDRESS="$(mac_generate)"
+       fi
+
+       return ${EXIT_OK}
 }
 
-function hook_up() {
+hook_up() {
        local zone=${1}
        assert isset zone
 
-       zone_settings_read "${zone}" ${HOOK_SETTINGS}
+       zone_settings_read "${zone}"
 
        # Create the bridge if it does not already exist.
        if ! device_exists "${zone}"; then
                bridge_create "${zone}" \
-                       --address="${MAC}" \
+                       --address="${ADDRESS}" \
                        --mtu="${MTU}"
        fi
 
@@ -122,47 +183,13 @@ function hook_up() {
        # set our bridges to promisc mode.
        device_set_promisc "${zone}" on
 
-       # Bring up all ports
-       zone_ports_create "${zone}"
-       zone_ports_up "${zone}"
-
        # Bring up all configurations
        zone_configs_up "${zone}"
 
        exit ${EXIT_OK}
 }
 
-function hook_hotplug() {
-       local zone="${1}"
-       assert isset zone
-
-       case "$(hotplug_action)" in
-               add)
-                       # Handle ports of this zone that have just been added
-                       if hotplug_event_interface_is_port_of_zone "${zone}"; then
-                               # Bring up the zone if it is enabled but not active, yet.
-                               if zone_is_enabled "${zone}" && ! zone_is_active "${zone}"; then
-                                       zone_start "${zone}"
-                               fi
-
-                               hook_port_up "${zone}" "${INTERFACE}"
-                       fi
-                       ;;
-               remove)
-                       # Handle ports of this zone that have just been removed
-                       if hotplug_event_interface_is_port_of_zone "${zone}"; then
-                               hook_port_down "${zone}" "${INTERFACE}"
-                       fi
-                       ;;
-               *)
-                       exit ${EXIT_NOT_HANDLED}
-                       ;;
-       esac
-
-       exit ${EXIT_OK}
-}
-
-function hook_down() {
+hook_down() {
        local zone="${1}"
        assert isset zone
 
@@ -185,7 +212,7 @@ function hook_down() {
        exit ${EXIT_OK}
 }
 
-function hook_status() {
+hook_status() {
        local zone="${1}"
        assert isset zone
 
@@ -200,9 +227,6 @@ function hook_status() {
 
        cli_headline 2 "Spanning Tree Protocol information"
        if stp_is_enabled "${zone}"; then
-               local proto=$(stp_bridge_get_protocol ${zone})
-
-               cli_print_fmt1 2 "Version"      "$(stp_get_name ${proto})"
                cli_print_fmt1 2 "ID"           "$(stp_bridge_get_id ${zone})"
                cli_print_fmt1 2 "Priority"     "$(stp_bridge_get_priority ${zone})"
 
@@ -240,60 +264,98 @@ function hook_status() {
        exit ${EXIT_OK}
 }
 
-function __parse_cmdline_args() {
-       while [ $# -gt 0 ]; do
-               case "${1}" in
-                       --priority=*)
-                               PRIORITY="$(cli_get_val ${1})"
-                               ;;
-                       --cost=*)
-                               COST="$(cli_get_val ${1})"
-                               ;;
-               esac
-               shift
-       done
+hook_hotplug() {
+       local zone="${1}"
+       assert isset zone
 
-       return ${EXIT_OK}
-}
+       case "$(hotplug_action)" in
+               add)
+                       # Attach all ports when zone is coming up
+                       if hotplug_event_interface_is_zone "${zone}"; then
+                               # Bring up all ports
+                               local port
+                               for port in $(zone_get_ports "${zone}"); do
+                                       log DEBUG "Trying to attach port ${port} to ${zone}"
 
-function hook_port_add() {
-       # Excepting at least two arguments here
-       assert [ $# -ge 2 ]
+                                       hook_port_up "${zone}" "${port}"
+                               done
 
-       local zone="${1}"
-       local port="${2}"
-       shift 2
+                       # Handle ports of this zone that have just been added
+                       elif hotplug_event_interface_is_port_of_zone "${zone}"; then
+                               # Attach the device if the parent bridge is up
+                               if zone_is_active "${zone}"; then
+                                       hook_port_up "${zone}" "${INTERFACE}"
+                               fi
+                       fi
+                       ;;
 
-       __parse_cmdline_args "$@"
-       [ $? -eq ${EXIT_OK} ] || return ${EXIT_ERROR}
+               remove)
+                       if hotplug_event_interface_is_zone "${zone}"; then
+                               # Bring down/destroy all ports
+                               local port
+                               for port in $(zone_get_ports "${zone}"); do
+                                       log DEBUG "Trying to detach port ${port} from ${zone}"
+
+                                       hook_port_down "${zone}" "${port}"
+                               done
 
-       zone_port_settings_write "${zone}" "${port}" ${HOOK_PORT_SETTINGS}
+                       # Handle ports of this zone that have just been removed
+                       elif hotplug_event_interface_is_port_of_zone "${zone}"; then
+                               hook_port_down "${zone}" "${INTERFACE}"
+                       fi
+                       ;;
 
-       log INFO "Port '${port}' has been added to zone '${zone}'"
+               *)
+                       exit ${EXIT_NOT_HANDLED}
+                       ;;
+       esac
 
        exit ${EXIT_OK}
 }
 
-function hook_port_edit() {
+hook_check_port_settings() {
+       if isset COST; then
+               assert isinteger COST
+       fi
+
+       if isset PRIORITY; then
+               assert isinteger PRIORITY
+       fi
+}
+
+hook_port_attach() {
+       # Excepting at least two arguments here
        assert [ $# -ge 2 ]
 
        local zone="${1}"
        local port="${2}"
        shift 2
 
-       zone_port_settings_read "${zone}" "${port}" ${HOOK_PORT_SETTINGS}
-
-       __parse_cmdline_args "$@"
-       [ $? -eq ${EXIT_OK} ] || return ${EXIT_ERROR}
+       if zone_has_port "${zone}" "${port}"; then
+               zone_port_settings_read "${zone}" "${port}"
+       fi
 
-       zone_port_settings_write "${zone}" "${port}" ${HOOK_PORT_SETTINGS}
+       local arg
+       local val
+       while read arg; do
+               case "${arg}" in
+                       --cost=*)
+                               COST="$(cli_get_val "${arg}")"
+                               ;;
+                       --priority=*)
+                               PRIORITY="$(cli_get_val "${arg}")"
+                               ;;
+               esac
+       done <<< "$(args "$@")"
 
-       log INFO "Port '${port}' (member of zone '${zone}') has been edited"
+       if ! zone_port_settings_write "${zone}" "${port}"; then
+               exit ${EXIT_ERROR}
+       fi
 
        exit ${EXIT_OK}
 }
 
-function hook_port_destroy() {
+hook_port_detach() {
        assert [ $# -eq 2 ]
 
        local zone="${1}"
@@ -302,13 +364,18 @@ function hook_port_destroy() {
        # Shut down the port (if possible)
        port_down "${port}"
 
-       log INFO "Port '${port}' has been removed from zone '${zone}'"
-       zone_port_settings_remove "${zone}" "${port}"
+       if ! zone_port_settings_remove "${zone}" "${port}"; then
+               exit ${EXIT_ERROR}
+       fi
 
        exit ${EXIT_OK}
 }
 
-function hook_port_up() {
+hook_port_edit() {
+       hook_port_attach "$@"
+}
+
+hook_port_up() {
        assert [ $# -eq 2 ]
 
        local zone="${1}"
@@ -319,14 +386,17 @@ function hook_port_up() {
        # We will get here as soon as the port device has
        # been created and will then connect it with the bridge.
        if ! device_exists "${port}"; then
-               port_up "${port}"
+               port_create "${port}"
 
-               exit ${EXIT_OK}
+               return ${EXIT_OK}
        fi
 
        # Read configuration values
        zone_port_settings_read "${zone}" "${port}" ${HOOK_PORT_SETTINGS}
 
+       # Make sure that the port is up
+       port_up "${port}"
+
        # Attach the port to the bridge
        bridge_attach_device "${zone}" "${port}"
 
@@ -335,15 +405,14 @@ function hook_port_up() {
                stp_port_set_cost "${zone}" "${port}" "${COST}"
        fi
 
-       # TODO Apply priority (#10609)
-
-       # Make sure that the port is up
-       port_up "${port}"
+       if isset PRIORITY; then
+               stp_port_set_priority "${zone}" "${port}" "${PRIORITY}"
+       fi
 
-       exit ${EXIT_OK}
+       return ${EXIT_OK}
 }
 
-function hook_port_down() {
+hook_port_down() {
        assert [ $# -eq 2 ]
 
        local zone="${1}"
@@ -355,10 +424,10 @@ function hook_port_down() {
                port_down "${port}"
        fi
 
-       exit ${EXIT_OK}
+       return ${EXIT_OK}
 }
 
-function hook_port_status() {
+hook_port_status() {
        assert [ $# -eq 2 ]
 
        local zone="${1}"