]> git.ipfire.org Git - people/stevee/network.git/blob - src/hooks/ports/bonding
Split port hooks in to (create|remove|up|down) actions
[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 ADDRESS=$(mac_generate)
27 SLAVES=""
28 MIIMON=100
29 MODE="balance-rr"
30
31 function hook_check() {
32 assert isset ADDRESS
33 assert ismac ADDRESS
34
35 #assert isset SLAVES
36 assert isinteger MIIMON
37 }
38
39 function hook_new() {
40 hook_edit $@
41 }
42
43 function hook_edit() {
44 local port=${1}
45 assert isset port
46 shift
47
48 while [ $# -gt 0 ]; do
49 case "${1}" in
50 --address=*)
51 ADDRESS=$(cli_get_val ${1})
52 ;;
53 --miimon=*)
54 MIIMON=$(cli_get_val ${1})
55 ;;
56 --mode=*)
57 MODE=$(cli_get_val ${1})
58 ;;
59 --slave=*)
60 slave=$(cli_get_val ${1})
61 SLAVES="${SLAVES} ${slave}"
62 ;;
63 *)
64 warning "Unknown argument '${1}'"
65 ;;
66 esac
67 shift
68 done
69
70 DEVICE=${port}
71
72 # XXX think this must move to _check()
73 if ! isset DEVICE; then
74 error "You must set a device name."
75 exit ${EXIT_ERROR}
76 fi
77
78 if ! isset SLAVES; then
79 error "You need to specify at least one slave port (e.g. --slave=port0)."
80 exit ${EXIT_ERROR}
81 fi
82
83 local slave
84 for slave in $(unquote ${SLAVES}); do
85 if ! device_is_ethernet ${slave}; then
86 error "Slave device '${slave}' is not an ethernet device."
87 exit ${EXIT_ERROR}
88 fi
89 done
90
91 # Remove any whitespace
92 SLAVES=$(echo ${SLAVES})
93
94 port_settings_write "${port}" ${HOOK_SETTINGS}
95
96 exit ${EXIT_OK}
97 }
98
99 function hook_create() {
100 local port="${1}"
101 assert isset port
102
103 # Exit silently if the device already exists
104 device_exists "${port}" && exit ${EXIT_OK}
105
106 port_settings_read "${port}" ${HOOK_SETTINGS}
107
108 # Create the bonding devices
109 bonding_create "${port}" \
110 --address="${ADDRESS}" \
111 --mode="${MODE}" || exit ${EXIT_ERROR}
112
113 bonding_set_miimon "${port}" "${MIIMON}"
114
115 exit ${EXIT_OK}
116 }
117
118 function hook_remove() {
119 local port="${1}"
120 assert isset port
121
122 port_settings_read "${port}" ${HOOK_SETTINGS}
123
124 # Remove the bonding device
125 if device_exists "${port}"; then
126 bonding_remove "${port}"
127 fi
128 }
129
130 function hook_up() {
131 local port="${1}"
132 assert isset port
133
134 port_settings_read "${port}" ${HOOK_SETTINGS}
135
136 # Execute the default action
137 hook_default_up "${port}"
138
139 # Bring up all slaves
140 local slave
141 for slave in $(unquote ${SLAVES}); do
142 port_up "${slave}"
143 done
144 }
145
146 function hook_down() {
147 local port="${1}"
148 assert isset port
149
150 port_settings_read "${port}" ${HOOK_SETTINGS}
151
152 # Bring down all slaves
153 local slave
154 for slave in $(unquote ${SLAVES}); do
155 port_down "${slave}"
156 done
157
158 # Execute the default action
159 hook_default_down "${port}"
160 }
161
162 function hook_hotplug() {
163 local port="${1}"
164 assert isset port
165
166 case "$(hotplug_action)" in
167 add)
168 # Handle events of the same interface
169 if hotplug_event_port_is_interface "${port}"; then
170 # Read configuration
171 port_settings_read "${port}" ${HOOK_SETTINGS}
172
173 # Bring up all slaves
174 # Attach those which already exist and try to create
175 # those which don't exist yet. They will be attached
176 # in their own hotplug event.
177 local slave
178 for slave in $(unquote ${SLAVES}); do
179 if device_exists "${slave}"; then
180 bonding_enslave_device "${port}" "${slave}"
181 else
182 port_create "${slave}"
183 fi
184 done
185
186 exit ${EXIT_OK}
187
188 # Handle slave devices that have just been created and
189 # attach them.
190 elif hotplug_event_interface_is_slave_of_port "${port}"; then
191 bonding_enslave_device "${port}" "${INTERFACE}"
192
193 # If the parent device has been set up, we will
194 # bring up the slave device as well.
195 if device_is_up "${port}"; then
196 port_up "${INTERFACE}"
197 fi
198 fi
199
200 exit ${EXIT_OK}
201 ;;
202
203 remove)
204 if hotplug_event_port_is_interface "${port}"; then
205 # Bring down all slaves after the parent device went away
206 local slave
207 for slave in $(port_get_slaves "${port}"); do
208 port_remove "${slave}"
209 done
210
211 exit ${EXIT_OK}
212 fi
213 ;;
214 esac
215
216 exit ${EXIT_NOT_HANDLED}
217 }