]> git.ipfire.org Git - people/ms/network.git/blame - src/hooks/ports/bonding
hooks: Automatically set defaults for all port hooks
[people/ms/network.git] / src / hooks / ports / bonding
CommitLineData
711ffac1
MT
1#!/bin/bash
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2010 Michael Tremer & Christian Schmidt #
6# #
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. #
11# #
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. #
16# #
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/>. #
19# #
20###############################################################################
21
987dfeb4 22. /usr/lib/network/header-port
711ffac1 23
8578e61d 24HOOK_SETTINGS="ADDRESS MIIMON MODE OFFLOADING SLAVES"
711ffac1 25
4637109c
MT
26DEFAULT_MIIMON=100
27DEFAULT_MODE="balance-rr"
711ffac1 28
1c6a4e30 29hook_check_settings() {
d1e71061
MT
30 assert isset ADDRESS
31 assert ismac ADDRESS
711ffac1
MT
32
33 #assert isset SLAVES
34 assert isinteger MIIMON
35}
36
a6fcc369 37hook_parse_cmdline() {
711ffac1
MT
38 while [ $# -gt 0 ]; do
39 case "${1}" in
d1e71061 40 --address=*)
7b955702
MT
41 ADDRESS="$(cli_get_val "${1}")"
42
43 if ! mac_is_valid "${ADDRESS}"; then
44 error "Invalid MAC address: ${ADDRESS}"
45 return ${EXIT_ERROR}
46 fi
711ffac1 47 ;;
7b955702 48
711ffac1 49 --miimon=*)
2212045f 50 MIIMON=$(cli_get_val "${1}")
711ffac1
MT
51 ;;
52 --mode=*)
2212045f 53 MODE=$(cli_get_val "${1}")
711ffac1 54 ;;
085a89dc
MT
55 --offloading=*)
56 OFFLOADING="$(cli_get_val "${1}")"
57
58 if enabled OFFLOADING; then
59 OFFLOADING="on"
0842edcc 60 elif disabled OFFLOADING; then
085a89dc 61 OFFLOADING="off"
0842edcc
MT
62 else
63 error "Invalid value for offloading: ${OFFLOADING}"
64 return ${EXIT_ERROR}
085a89dc
MT
65 fi
66 ;;
a6fcc369 67 +*)
2212045f 68 local slave=$(cli_get_val "${1:1}")
a6fcc369
MT
69
70 if port_exists "${slave}"; then
71 if list_match "${slave}" ${SLAVES}; then
72 warning "Port ${slave} is already enslaved"
73 else
74 list_append SLAVES "${slave}"
75 fi
76 else
77 warning "Port ${slave} does not exist"
78 fi
79 ;;
80 -*)
2212045f 81 local slave=$(cli_get_val "${1:1}")
a6fcc369
MT
82 if ! list_remove SLAVES "${slave}"; then
83 warning "Port ${slave} is not a slave of this bonding device"
84 fi
711ffac1
MT
85 ;;
86 *)
87 warning "Unknown argument '${1}'"
88 ;;
89 esac
90 shift
91 done
92
a6fcc369
MT
93 if isset ADDRESS; then
94 if ! ismac ADDRESS; then
95 error "The given MAC address is invalid: ${ADDRESS}"
96 return ${EXIT_ERROR}
97 fi
98 else
99 ADDRESS=$(mac_generate)
100 fi
101}
102
103hook_new() {
2212045f 104 if ! hook_parse_cmdline "$@"; then
a6fcc369
MT
105 return ${EXIT_ERROR}
106 fi
107
108 # Find a new name
109 local port=$(port_find_free ${BONDING_PORT_PATTERN})
110 assert isset port
711ffac1 111
a6fcc369
MT
112 # Save configuration
113 if port_settings_write "${port}" ${HOOK_SETTINGS}; then
114 log INFO "New port ${port} has been created"
115 else
116 error "Could not save configuration for ${port}"
117 return ${EXIT_ERROR}
711ffac1
MT
118 fi
119
a6fcc369
MT
120 return ${EXIT_OK}
121}
122
123hook_edit() {
124 local port=${1}
125
2212045f 126 if ! hook_default_edit "$@"; then
a6fcc369 127 return ${EXIT_ERROR}
711ffac1
MT
128 fi
129
a6fcc369
MT
130 # If the master device is up, make sure that all
131 # slaves are up, too
132 if device_exists "${port}"; then
133 # Setting the mode requires us to destroy the device
134 # and to recreate it again.
135 local mode=$(bonding_get_mode "${port}")
136 if [ "${mode}" != "${MODE}" ]; then
62973f6b 137 port_restart "${port}"
a6fcc369 138 return ${EXIT_OK}
711ffac1 139 fi
711ffac1 140
a6fcc369
MT
141 # Set address
142 device_set_address "${port}" "${ADDRESS}"
711ffac1 143
a6fcc369
MT
144 # Set miimon
145 bonding_set_miimon "${port}" "${MIIMON}"
711ffac1 146
a6fcc369
MT
147 local slave
148 for slave in ${SLAVES}; do
149 if device_exists "${slave}"; then
150 bonding_enslave_device "${port}" "${slave}"
151 else
152 port_create "${slave}"
153 fi
154 done
155 fi
711ffac1
MT
156}
157
1c6a4e30 158hook_create() {
1ba6a2bb
MT
159 local port="${1}"
160 assert isset port
711ffac1 161
1ba6a2bb
MT
162 # Exit silently if the device already exists
163 device_exists "${port}" && exit ${EXIT_OK}
711ffac1 164
1ba6a2bb
MT
165 port_settings_read "${port}" ${HOOK_SETTINGS}
166
167 # Create the bonding devices
168 bonding_create "${port}" \
169 --address="${ADDRESS}" \
170 --mode="${MODE}" || exit ${EXIT_ERROR}
171
172 bonding_set_miimon "${port}" "${MIIMON}"
173
174 exit ${EXIT_OK}
175}
176
1c6a4e30 177hook_remove() {
1ba6a2bb
MT
178 local port="${1}"
179 assert isset port
180
181 port_settings_read "${port}" ${HOOK_SETTINGS}
182
183 # Remove the bonding device
184 if device_exists "${port}"; then
185 bonding_remove "${port}"
711ffac1 186 fi
1ba6a2bb
MT
187}
188
1c6a4e30 189hook_up() {
1ba6a2bb
MT
190 local port="${1}"
191 assert isset port
711ffac1 192
1ba6a2bb 193 port_settings_read "${port}" ${HOOK_SETTINGS}
711ffac1 194
085a89dc
MT
195 # Auto-enable or disable hardware offloading
196 if ! isset OFFLOADING || enabled OFFLOADING; then
197 offloading_auto "${port}"
198 else
199 offloading_disable_all "${port}"
200 fi
c3db1412 201
1ba6a2bb
MT
202 # Execute the default action
203 hook_default_up "${port}"
204
205 # Bring up all slaves
711ffac1 206 local slave
d1e71061 207 for slave in $(unquote ${SLAVES}); do
1ba6a2bb 208 port_up "${slave}"
711ffac1 209 done
711ffac1
MT
210}
211
1c6a4e30 212hook_down() {
1ba6a2bb
MT
213 local port="${1}"
214 assert isset port
711ffac1 215
1ba6a2bb 216 port_settings_read "${port}" ${HOOK_SETTINGS}
711ffac1 217
1ba6a2bb 218 # Bring down all slaves
711ffac1 219 local slave
1ba6a2bb
MT
220 for slave in $(unquote ${SLAVES}); do
221 port_down "${slave}"
711ffac1
MT
222 done
223
1ba6a2bb
MT
224 # Execute the default action
225 hook_default_down "${port}"
226}
227
1c6a4e30 228hook_hotplug() {
1ba6a2bb
MT
229 local port="${1}"
230 assert isset port
231
232 case "$(hotplug_action)" in
233 add)
234 # Handle events of the same interface
235 if hotplug_event_port_is_interface "${port}"; then
236 # Read configuration
237 port_settings_read "${port}" ${HOOK_SETTINGS}
238
239 # Bring up all slaves
240 # Attach those which already exist and try to create
241 # those which don't exist yet. They will be attached
242 # in their own hotplug event.
243 local slave
244 for slave in $(unquote ${SLAVES}); do
245 if device_exists "${slave}"; then
246 bonding_enslave_device "${port}" "${slave}"
247 else
248 port_create "${slave}"
249 fi
250 done
251
252 exit ${EXIT_OK}
253
254 # Handle slave devices that have just been created and
255 # attach them.
256 elif hotplug_event_interface_is_slave_of_port "${port}"; then
257 bonding_enslave_device "${port}" "${INTERFACE}"
258
259 # If the parent device has been set up, we will
260 # bring up the slave device as well.
261 if device_is_up "${port}"; then
262 port_up "${INTERFACE}"
263 fi
264 fi
265
266 exit ${EXIT_OK}
267 ;;
268
269 remove)
270 if hotplug_event_port_is_interface "${port}"; then
271 # Bring down all slaves after the parent device went away
272 local slave
273 for slave in $(port_get_slaves "${port}"); do
274 port_remove "${slave}"
275 done
276
277 exit ${EXIT_OK}
278 fi
279 ;;
280 esac
281
282 exit ${EXIT_NOT_HANDLED}
711ffac1 283}