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