]> git.ipfire.org Git - people/stevee/network.git/blob - src/hooks/ports/bonding
Move offloading code into an own file
[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 # Auto-enable hardware offloading
179 offloading_auto "${port}"
180
181 # Execute the default action
182 hook_default_up "${port}"
183
184 # Bring up all slaves
185 local slave
186 for slave in $(unquote ${SLAVES}); do
187 port_up "${slave}"
188 done
189 }
190
191 hook_down() {
192 local port="${1}"
193 assert isset port
194
195 port_settings_read "${port}" ${HOOK_SETTINGS}
196
197 # Bring down all slaves
198 local slave
199 for slave in $(unquote ${SLAVES}); do
200 port_down "${slave}"
201 done
202
203 # Execute the default action
204 hook_default_down "${port}"
205 }
206
207 hook_hotplug() {
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}
262 }