]> git.ipfire.org Git - people/ms/network.git/blob - src/hooks/ports/bonding
hooks: Use cli_get_bool convenience function where ever possible
[people/ms/network.git] / src / hooks / ports / bonding
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
22 . /usr/lib/network/header-port
23
24 HOOK_SETTINGS=(
25 "ADDRESS"
26 "MIIMON"
27 "MODE"
28 "OFFLOADING"
29 "SLAVES"
30 )
31
32 DEFAULT_MIIMON=100
33 DEFAULT_MODE="balance-rr"
34
35 hook_check_settings() {
36 assert isset ADDRESS
37 assert ismac ADDRESS
38
39 #assert isset SLAVES
40 assert isinteger MIIMON
41 }
42
43 hook_parse_cmdline() {
44 while [ $# -gt 0 ]; do
45 case "${1}" in
46 --address=*)
47 ADDRESS="$(cli_get_val "${1}")"
48
49 if ! mac_is_valid "${ADDRESS}"; then
50 error "Invalid MAC address: ${ADDRESS}"
51 return ${EXIT_ERROR}
52 fi
53 ;;
54
55 --miimon=*)
56 MIIMON=$(cli_get_val "${1}")
57 ;;
58 --mode=*)
59 MODE=$(cli_get_val "${1}")
60 ;;
61 --offloading=*)
62 OFFLOADING="$(cli_get_bool "${1}")"
63 ;;
64 +*)
65 local slave=$(cli_get_val "${1:1}")
66
67 if port_exists "${slave}"; then
68 if list_match "${slave}" ${SLAVES}; then
69 warning "Port ${slave} is already enslaved"
70 else
71 list_append SLAVES "${slave}"
72 fi
73 else
74 warning "Port ${slave} does not exist"
75 fi
76 ;;
77 -*)
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"
81 fi
82 ;;
83 *)
84 warning "Unknown argument '${1}'"
85 ;;
86 esac
87 shift
88 done
89
90 if isset ADDRESS; then
91 if ! ismac ADDRESS; then
92 error "The given MAC address is invalid: ${ADDRESS}"
93 return ${EXIT_ERROR}
94 fi
95 else
96 ADDRESS=$(mac_generate)
97 fi
98 }
99
100 hook_new() {
101 if ! hook_parse_cmdline "$@"; then
102 return ${EXIT_ERROR}
103 fi
104
105 # Find a new name
106 local port=$(port_find_free ${BONDING_PORT_PATTERN})
107 assert isset port
108
109 # Save configuration
110 if port_settings_write "${port}"; then
111 log INFO "New port ${port} has been created"
112 else
113 error "Could not save configuration for ${port}"
114 return ${EXIT_ERROR}
115 fi
116
117 return ${EXIT_OK}
118 }
119
120 hook_edit() {
121 local port=${1}
122
123 if ! hook_default_edit "$@"; then
124 return ${EXIT_ERROR}
125 fi
126
127 # If the master device is up, make sure that all
128 # slaves are up, too
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}"
135 return ${EXIT_OK}
136 fi
137
138 # Set address
139 device_set_address "${port}" "${ADDRESS}"
140
141 # Set miimon
142 bonding_set_miimon "${port}" "${MIIMON}"
143
144 local slave
145 for slave in ${SLAVES}; do
146 if device_exists "${slave}"; then
147 bonding_enslave_device "${port}" "${slave}"
148 else
149 port_create "${slave}"
150 fi
151 done
152 fi
153 }
154
155 hook_create() {
156 local port="${1}"
157 assert isset port
158
159 # Exit silently if the device already exists
160 device_exists "${port}" && exit ${EXIT_OK}
161
162 port_settings_read "${port}"
163
164 # Create the bonding devices
165 bonding_create "${port}" \
166 --address="${ADDRESS}" \
167 --mode="${MODE}" || exit ${EXIT_ERROR}
168
169 bonding_set_miimon "${port}" "${MIIMON}"
170
171 exit ${EXIT_OK}
172 }
173
174 hook_remove() {
175 local port="${1}"
176 assert isset port
177
178 port_settings_read "${port}"
179
180 # Remove the bonding device
181 if device_exists "${port}"; then
182 bonding_remove "${port}"
183 fi
184 }
185
186 hook_up() {
187 local port="${1}"
188 assert isset port
189
190 port_settings_read "${port}"
191
192 # Auto-enable or disable hardware offloading
193 if ! isset OFFLOADING || enabled OFFLOADING; then
194 offloading_auto "${port}"
195 else
196 offloading_disable_all "${port}"
197 fi
198
199 # Execute the default action
200 hook_default_up "${port}"
201
202 # Bring up all slaves
203 local slave
204 for slave in $(unquote ${SLAVES}); do
205 port_up "${slave}"
206 done
207 }
208
209 hook_down() {
210 local port="${1}"
211 assert isset port
212
213 port_settings_read "${port}"
214
215 # Bring down all slaves
216 local slave
217 for slave in $(unquote ${SLAVES}); do
218 port_down "${slave}"
219 done
220
221 # Execute the default action
222 hook_default_down "${port}"
223 }
224
225 hook_hotplug() {
226 local port="${1}"
227 assert isset port
228
229 case "$(hotplug_action)" in
230 add)
231 # Handle events of the same interface
232 if hotplug_event_port_is_interface "${port}"; then
233 # Read configuration
234 port_settings_read "${port}"
235
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.
240 local slave
241 for slave in $(unquote ${SLAVES}); do
242 if device_exists "${slave}"; then
243 bonding_enslave_device "${port}" "${slave}"
244 else
245 port_create "${slave}"
246 fi
247 done
248
249 exit ${EXIT_OK}
250
251 # Handle slave devices that have just been created and
252 # attach them.
253 elif hotplug_event_interface_is_slave_of_port "${port}"; then
254 bonding_enslave_device "${port}" "${INTERFACE}"
255
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}"
260 fi
261 fi
262
263 exit ${EXIT_OK}
264 ;;
265
266 remove)
267 if hotplug_event_port_is_interface "${port}"; then
268 # Bring down all slaves after the parent device went away
269 local slave
270 for slave in $(port_get_slaves "${port}"); do
271 port_remove "${slave}"
272 done
273
274 exit ${EXIT_OK}
275 fi
276 ;;
277 esac
278
279 exit ${EXIT_NOT_HANDLED}
280 }