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