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