]> git.ipfire.org Git - people/ms/network.git/commitdiff
vlan: Refactor vlan_create()
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 3 Jun 2019 10:28:17 +0000 (12:28 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 3 Jun 2019 10:28:17 +0000 (12:28 +0200)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/functions/functions.vlan
src/hooks/ports/vlan

index d83e3ad30a9d02f00c2ca7d86d2060de22414a9b..99a8baa375d513f0b664cd3d7e838d8127bdff87 100644 (file)
@@ -38,53 +38,76 @@ EOF
 }
 
 vlan_create() {
 }
 
 vlan_create() {
-       local device=${1}
-       assert isset device
+       local device="${1}"
+       shift
 
 
-       local parent=${2}
-       assert isset parent
+       assert isset device
 
 
-       local tag=${3}
-       assert isinteger tag
+       local address
+       local parent
+       local tag
+
+       # Parse command line arguments
+       while [ $# -gt 0 ]; do
+               case "${1}" in
+                       --address=*)
+                               address=$(cli_get_val "${1}")
+                               ;;
+                       --parent=*)
+                               parent=$(cli_get_val "${1}")
+                               ;;
+                       --tag=*)
+                               tag=$(cli_get_val "${1}")
+                               ;;
+                       *)
+                               error "Unrecognized argument: ${1}"
+                               return ${EXIT_ERROR}
+                               ;;
+               esac
+               shift
+       done
+
+       # Generate a random MAC address if none was passed
+       if ! isset address; then
+               address="$(mac_generate)"
+       fi
 
 
-       local address=${4}
-       if isset address; then
-               assert ismac address
+       # Check if address is valid
+       if ! ismac address; then
+               log ERROR "Invalid mac address: ${address}"
+               return ${EXIT_ERROR}
        fi
 
        fi
 
-       # Check if a device with the name does already exist.
-       if device_exists ${device}; then
-               log ERROR "device '${device}' does already exist"
+       # Check if a device with the name does already exist
+       if device_exists "${device}"; then
+               log ERROR "Device '${device}' already exists"
                return ${EXIT_ERROR}
        fi
 
                return ${EXIT_ERROR}
        fi
 
-       # Check if the parent device exists.
-       if ! device_exists ${parent}; then
-               log ERROR "parent device '${parent}' does not exist"
+       # Check if the parent device exists
+       if ! device_exists "${parent}"; then
+               log ERROR "Parent device '${parent}' does not exist"
                return ${EXIT_ERROR}
        fi
 
        # Load ebtables stuff.
        vlan_init
 
                return ${EXIT_ERROR}
        fi
 
        # Load ebtables stuff.
        vlan_init
 
-       local command="ip link add link ${parent} name ${device}"
+       # Make the command
+       local command=(
+               ip link add link "${parent}" name "${device}"
+                       address "${address}" type vlan id "${tag}"
+       )
 
 
-       if isset address; then
-               command="${command} address ${address}"
+       # Run the command
+       if ! cmd_quiet "${command[*]}"; then
+               log ERROR "Could not create VLAN device ${device}: $?"
+               return ${EXIT_ERROR}
        fi
 
        fi
 
-       command="${command} type vlan id ${tag}"
-
-       cmd_quiet ${command}
-       local ret=$?
-
-       if [ ${ret} -eq ${EXIT_OK} ]; then
-               log DEBUG "vlan device '${device}' has been created"
-       else
-               log ERROR "could not create vlan device '${device}': ${ret}"
-       fi
+       log DEBUG "Created VLAN device ${device} (parent = ${parent}, id = ${tag})"
 
 
-       return ${ret}
+       return ${EXIT_OK}
 }
 
 vlan_remove() {
 }
 
 vlan_remove() {
index 98178e3d823ef38b5499ca849bdc73e92becf933..4715b1f6c9d1828aedca52e6c6e65867ff4b4702 100644 (file)
@@ -114,9 +114,15 @@ hook_create() {
        fi
 
        # Create the VLAN device
        fi
 
        # Create the VLAN device
-       vlan_create "${port}" "${PARENT_PORT}" "${TAG}" "${ADDRESS}"
+       if ! vlan_create "${port}" \
+                       --address="${ADDRESS}" \
+                       --parent="${PARENT_PORT}" \
+                       --tag="${TAG}"; then
+               error "Could not create port: ${port}"
+               return ${EXIT_ERROR}
+       fi
 
 
-       exit ${EXIT_OK}
+       return ${EXIT_OK}
 }
 
 hook_remove() {
 }
 
 hook_remove() {