2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
22 .
/usr
/lib
/network
/header-port
33 DEFAULT_MODE
="balance-rr"
35 hook_check_settings
() {
40 assert isinteger MIIMON
43 hook_parse_cmdline
() {
44 while [ $# -gt 0 ]; do
47 ADDRESS
="$(cli_get_val "${1}")"
49 if ! mac_is_valid
"${ADDRESS}"; then
50 error
"Invalid MAC address: ${ADDRESS}"
56 MIIMON
=$
(cli_get_val
"${1}")
59 MODE
=$
(cli_get_val
"${1}")
62 OFFLOADING
="$(cli_get_bool "${1}")"
65 local slave
=$
(cli_get_val
"${1:1}")
67 if port_exists
"${slave}"; then
68 if list_match
"${slave}" ${SLAVES}; then
69 warning
"Port ${slave} is already enslaved"
71 list_append SLAVES
"${slave}"
74 warning
"Port ${slave} does not exist"
78 local slave
=$
(cli_get_val
"${1:1}")
79 if ! list_remove SLAVES
"${slave}"; then
80 warning
"Port ${slave} is not a slave of this bonding device"
84 warning
"Unknown argument '${1}'"
90 if isset ADDRESS
; then
91 if ! ismac ADDRESS
; then
92 error
"The given MAC address is invalid: ${ADDRESS}"
96 ADDRESS
=$
(mac_generate
)
101 if ! hook_parse_cmdline
"$@"; then
106 local port
=$
(port_find_free
${BONDING_PORT_PATTERN})
110 if port_settings_write
"${port}"; then
111 log INFO
"New port ${port} has been created"
113 error
"Could not save configuration for ${port}"
123 if ! hook_default_edit
"$@"; then
127 # If the master device is up, make sure that all
129 if device_exists
"${port}"; then
130 # Setting the mode requires us to destroy the device
131 # and to recreate it again.
132 local mode
=$
(bonding_get_mode
"${port}")
133 if [ "${mode}" != "${MODE}" ]; then
134 port_restart
"${port}"
139 device_set_address
"${port}" "${ADDRESS}"
142 bonding_set_miimon
"${port}" "${MIIMON}"
145 for slave
in ${SLAVES}; do
146 if device_exists
"${slave}"; then
147 bonding_enslave_device
"${port}" "${slave}"
149 port_create
"${slave}"
159 # Exit silently if the device already exists
160 device_exists
"${port}" && exit ${EXIT_OK}
162 port_settings_read
"${port}"
164 # Create the bonding devices
165 bonding_create
"${port}" \
166 --address="${ADDRESS}" \
167 --mode="${MODE}" ||
exit ${EXIT_ERROR}
169 bonding_set_miimon
"${port}" "${MIIMON}"
178 port_settings_read
"${port}"
180 # Remove the bonding device
181 if device_exists
"${port}"; then
182 bonding_remove
"${port}"
190 port_settings_read
"${port}"
192 # Auto-enable or disable hardware offloading
193 if ! isset OFFLOADING || enabled OFFLOADING
; then
194 offloading_auto
"${port}"
196 offloading_disable_all
"${port}"
199 # Execute the default action
200 hook_default_up
"${port}"
202 # Bring up all slaves
204 for slave
in $
(unquote
${SLAVES}); do
213 port_settings_read
"${port}"
215 # Bring down all slaves
217 for slave
in $
(unquote
${SLAVES}); do
221 # Execute the default action
222 hook_default_down
"${port}"
229 case "$(hotplug_action)" in
231 # Handle events of the same interface
232 if hotplug_event_port_is_interface
"${port}"; then
234 port_settings_read
"${port}"
236 # Bring up all slaves
237 # Attach those which already exist and try to create
238 # those which don't exist yet. They will be attached
239 # in their own hotplug event.
241 for slave
in $
(unquote
${SLAVES}); do
242 if device_exists
"${slave}"; then
243 bonding_enslave_device
"${port}" "${slave}"
245 port_create
"${slave}"
251 # Handle slave devices that have just been created and
253 elif hotplug_event_interface_is_slave_of_port
"${port}"; then
254 bonding_enslave_device
"${port}" "${INTERFACE}"
256 # If the parent device has been set up, we will
257 # bring up the slave device as well.
258 if device_is_up
"${port}"; then
259 port_up
"${INTERFACE}"
267 if hotplug_event_port_is_interface
"${port}"; then
268 # Bring down all slaves after the parent device went away
270 for slave
in $
(port_get_slaves
"${port}"); do
271 port_remove
"${slave}"
279 exit ${EXIT_NOT_HANDLED}