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