]> git.ipfire.org Git - people/ms/network.git/blame - src/hooks/ports/bonding
hooks: Use cli_get_bool convenience function where ever possible
[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
d389e96b
MT
24HOOK_SETTINGS=(
25 "ADDRESS"
26 "MIIMON"
27 "MODE"
28 "OFFLOADING"
29 "SLAVES"
30)
711ffac1 31
4637109c
MT
32DEFAULT_MIIMON=100
33DEFAULT_MODE="balance-rr"
711ffac1 34
1c6a4e30 35hook_check_settings() {
d1e71061
MT
36 assert isset ADDRESS
37 assert ismac ADDRESS
711ffac1
MT
38
39 #assert isset SLAVES
40 assert isinteger MIIMON
41}
42
a6fcc369 43hook_parse_cmdline() {
711ffac1
MT
44 while [ $# -gt 0 ]; do
45 case "${1}" in
d1e71061 46 --address=*)
7b955702
MT
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
711ffac1 53 ;;
7b955702 54
711ffac1 55 --miimon=*)
2212045f 56 MIIMON=$(cli_get_val "${1}")
711ffac1
MT
57 ;;
58 --mode=*)
2212045f 59 MODE=$(cli_get_val "${1}")
711ffac1 60 ;;
085a89dc 61 --offloading=*)
f6659cc5 62 OFFLOADING="$(cli_get_bool "${1}")"
085a89dc 63 ;;
a6fcc369 64 +*)
2212045f 65 local slave=$(cli_get_val "${1:1}")
a6fcc369
MT
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 -*)
2212045f 78 local slave=$(cli_get_val "${1:1}")
a6fcc369
MT
79 if ! list_remove SLAVES "${slave}"; then
80 warning "Port ${slave} is not a slave of this bonding device"
81 fi
711ffac1
MT
82 ;;
83 *)
84 warning "Unknown argument '${1}'"
85 ;;
86 esac
87 shift
88 done
89
a6fcc369
MT
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
100hook_new() {
2212045f 101 if ! hook_parse_cmdline "$@"; then
a6fcc369
MT
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
711ffac1 108
a6fcc369 109 # Save configuration
eba9fa9c 110 if port_settings_write "${port}"; then
a6fcc369
MT
111 log INFO "New port ${port} has been created"
112 else
113 error "Could not save configuration for ${port}"
114 return ${EXIT_ERROR}
711ffac1
MT
115 fi
116
a6fcc369
MT
117 return ${EXIT_OK}
118}
119
120hook_edit() {
121 local port=${1}
122
2212045f 123 if ! hook_default_edit "$@"; then
a6fcc369 124 return ${EXIT_ERROR}
711ffac1
MT
125 fi
126
a6fcc369
MT
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
62973f6b 134 port_restart "${port}"
a6fcc369 135 return ${EXIT_OK}
711ffac1 136 fi
711ffac1 137
a6fcc369
MT
138 # Set address
139 device_set_address "${port}" "${ADDRESS}"
711ffac1 140
a6fcc369
MT
141 # Set miimon
142 bonding_set_miimon "${port}" "${MIIMON}"
711ffac1 143
a6fcc369
MT
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
711ffac1
MT
153}
154
1c6a4e30 155hook_create() {
1ba6a2bb
MT
156 local port="${1}"
157 assert isset port
711ffac1 158
1ba6a2bb
MT
159 # Exit silently if the device already exists
160 device_exists "${port}" && exit ${EXIT_OK}
711ffac1 161
eba9fa9c 162 port_settings_read "${port}"
1ba6a2bb
MT
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
1c6a4e30 174hook_remove() {
1ba6a2bb
MT
175 local port="${1}"
176 assert isset port
177
eba9fa9c 178 port_settings_read "${port}"
1ba6a2bb
MT
179
180 # Remove the bonding device
181 if device_exists "${port}"; then
182 bonding_remove "${port}"
711ffac1 183 fi
1ba6a2bb
MT
184}
185
1c6a4e30 186hook_up() {
1ba6a2bb
MT
187 local port="${1}"
188 assert isset port
711ffac1 189
eba9fa9c 190 port_settings_read "${port}"
711ffac1 191
085a89dc
MT
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
c3db1412 198
1ba6a2bb
MT
199 # Execute the default action
200 hook_default_up "${port}"
201
202 # Bring up all slaves
711ffac1 203 local slave
d1e71061 204 for slave in $(unquote ${SLAVES}); do
1ba6a2bb 205 port_up "${slave}"
711ffac1 206 done
711ffac1
MT
207}
208
1c6a4e30 209hook_down() {
1ba6a2bb
MT
210 local port="${1}"
211 assert isset port
711ffac1 212
eba9fa9c 213 port_settings_read "${port}"
711ffac1 214
1ba6a2bb 215 # Bring down all slaves
711ffac1 216 local slave
1ba6a2bb
MT
217 for slave in $(unquote ${SLAVES}); do
218 port_down "${slave}"
711ffac1
MT
219 done
220
1ba6a2bb
MT
221 # Execute the default action
222 hook_default_down "${port}"
223}
224
1c6a4e30 225hook_hotplug() {
1ba6a2bb
MT
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
eba9fa9c 234 port_settings_read "${port}"
1ba6a2bb
MT
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}
711ffac1 280}