]> git.ipfire.org Git - people/ms/network.git/blame - src/hooks/ports/bonding
Move offloading code into an own file
[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
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 41 --address=*)
2212045f 42 ADDRESS=$(cli_get_val "${1}")
711ffac1
MT
43 ;;
44 --miimon=*)
2212045f 45 MIIMON=$(cli_get_val "${1}")
711ffac1
MT
46 ;;
47 --mode=*)
2212045f 48 MODE=$(cli_get_val "${1}")
711ffac1 49 ;;
a6fcc369 50 +*)
2212045f 51 local slave=$(cli_get_val "${1:1}")
a6fcc369
MT
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 -*)
2212045f 64 local slave=$(cli_get_val "${1:1}")
a6fcc369
MT
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() {
2212045f 87 if ! hook_parse_cmdline "$@"; then
a6fcc369
MT
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
2212045f 109 if ! hook_default_edit "$@"; then
a6fcc369 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
3608f641
MT
178 # Auto-enable hardware offloading
179 offloading_auto "${port}"
c3db1412 180
1ba6a2bb
MT
181 # Execute the default action
182 hook_default_up "${port}"
183
184 # Bring up all slaves
711ffac1 185 local slave
d1e71061 186 for slave in $(unquote ${SLAVES}); do
1ba6a2bb 187 port_up "${slave}"
711ffac1 188 done
711ffac1
MT
189}
190
1c6a4e30 191hook_down() {
1ba6a2bb
MT
192 local port="${1}"
193 assert isset port
711ffac1 194
1ba6a2bb 195 port_settings_read "${port}" ${HOOK_SETTINGS}
711ffac1 196
1ba6a2bb 197 # Bring down all slaves
711ffac1 198 local slave
1ba6a2bb
MT
199 for slave in $(unquote ${SLAVES}); do
200 port_down "${slave}"
711ffac1
MT
201 done
202
1ba6a2bb
MT
203 # Execute the default action
204 hook_default_down "${port}"
205}
206
1c6a4e30 207hook_hotplug() {
1ba6a2bb
MT
208 local port="${1}"
209 assert isset port
210
211 case "$(hotplug_action)" in
212 add)
213 # Handle events of the same interface
214 if hotplug_event_port_is_interface "${port}"; then
215 # Read configuration
216 port_settings_read "${port}" ${HOOK_SETTINGS}
217
218 # Bring up all slaves
219 # Attach those which already exist and try to create
220 # those which don't exist yet. They will be attached
221 # in their own hotplug event.
222 local slave
223 for slave in $(unquote ${SLAVES}); do
224 if device_exists "${slave}"; then
225 bonding_enslave_device "${port}" "${slave}"
226 else
227 port_create "${slave}"
228 fi
229 done
230
231 exit ${EXIT_OK}
232
233 # Handle slave devices that have just been created and
234 # attach them.
235 elif hotplug_event_interface_is_slave_of_port "${port}"; then
236 bonding_enslave_device "${port}" "${INTERFACE}"
237
238 # If the parent device has been set up, we will
239 # bring up the slave device as well.
240 if device_is_up "${port}"; then
241 port_up "${INTERFACE}"
242 fi
243 fi
244
245 exit ${EXIT_OK}
246 ;;
247
248 remove)
249 if hotplug_event_port_is_interface "${port}"; then
250 # Bring down all slaves after the parent device went away
251 local slave
252 for slave in $(port_get_slaves "${port}"); do
253 port_remove "${slave}"
254 done
255
256 exit ${EXIT_OK}
257 fi
258 ;;
259 esac
260
261 exit ${EXIT_NOT_HANDLED}
711ffac1 262}