]> git.ipfire.org Git - people/stevee/network.git/blame - src/hooks/ports/bonding
bonding: Use port_restart to restart a port
[people/stevee/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
d1e71061 24HOOK_SETTINGS="HOOK ADDRESS MIIMON MODE SLAVES"
711ffac1 25
d1e71061 26SLAVES=""
711ffac1 27MIIMON=100
4c4dd614 28MODE="balance-rr"
711ffac1 29
1c6a4e30 30hook_check_settings() {
d1e71061
MT
31 assert isset ADDRESS
32 assert ismac ADDRESS
711ffac1
MT
33
34 #assert isset SLAVES
35 assert isinteger MIIMON
36}
37
a6fcc369 38hook_parse_cmdline() {
711ffac1
MT
39 while [ $# -gt 0 ]; do
40 case "${1}" in
d1e71061
MT
41 --address=*)
42 ADDRESS=$(cli_get_val ${1})
711ffac1
MT
43 ;;
44 --miimon=*)
d1e71061 45 MIIMON=$(cli_get_val ${1})
711ffac1
MT
46 ;;
47 --mode=*)
d1e71061 48 MODE=$(cli_get_val ${1})
711ffac1 49 ;;
a6fcc369
MT
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
711ffac1
MT
68 ;;
69 *)
70 warning "Unknown argument '${1}'"
71 ;;
72 esac
73 shift
74 done
75
a6fcc369
MT
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
86hook_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
711ffac1 94
a6fcc369
MT
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}
711ffac1
MT
101 fi
102
a6fcc369
MT
103 return ${EXIT_OK}
104}
105
106hook_edit() {
107 local port=${1}
108
109 if ! hook_default_edit $@; then
110 return ${EXIT_ERROR}
711ffac1
MT
111 fi
112
a6fcc369
MT
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
62973f6b 120 port_restart "${port}"
a6fcc369 121 return ${EXIT_OK}
711ffac1 122 fi
711ffac1 123
a6fcc369
MT
124 # Set address
125 device_set_address "${port}" "${ADDRESS}"
711ffac1 126
a6fcc369
MT
127 # Set miimon
128 bonding_set_miimon "${port}" "${MIIMON}"
711ffac1 129
a6fcc369
MT
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
711ffac1
MT
139}
140
1c6a4e30 141hook_create() {
1ba6a2bb
MT
142 local port="${1}"
143 assert isset port
711ffac1 144
1ba6a2bb
MT
145 # Exit silently if the device already exists
146 device_exists "${port}" && exit ${EXIT_OK}
711ffac1 147
1ba6a2bb
MT
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
1c6a4e30 160hook_remove() {
1ba6a2bb
MT
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}"
711ffac1 169 fi
1ba6a2bb
MT
170}
171
1c6a4e30 172hook_up() {
1ba6a2bb
MT
173 local port="${1}"
174 assert isset port
711ffac1 175
1ba6a2bb 176 port_settings_read "${port}" ${HOOK_SETTINGS}
711ffac1 177
1ba6a2bb
MT
178 # Execute the default action
179 hook_default_up "${port}"
180
181 # Bring up all slaves
711ffac1 182 local slave
d1e71061 183 for slave in $(unquote ${SLAVES}); do
1ba6a2bb 184 port_up "${slave}"
711ffac1 185 done
711ffac1
MT
186}
187
1c6a4e30 188hook_down() {
1ba6a2bb
MT
189 local port="${1}"
190 assert isset port
711ffac1 191
1ba6a2bb 192 port_settings_read "${port}" ${HOOK_SETTINGS}
711ffac1 193
1ba6a2bb 194 # Bring down all slaves
711ffac1 195 local slave
1ba6a2bb
MT
196 for slave in $(unquote ${SLAVES}); do
197 port_down "${slave}"
711ffac1
MT
198 done
199
1ba6a2bb
MT
200 # Execute the default action
201 hook_default_down "${port}"
202}
203
1c6a4e30 204hook_hotplug() {
1ba6a2bb
MT
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}
711ffac1 259}