]> git.ipfire.org Git - people/ms/network.git/blob - functions.stp
Move all of the device renaming code to network-hotplug-rename.
[people/ms/network.git] / functions.stp
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 # The default mode.
23 # We default to RSTP, because it has the better user experience and
24 # faster convergence times. Despite of that, it completely downgradeable
25 # to plain STP.
26 STP_DEFAULT_MODE="rstp"
27
28 # Allowed modes of the spanning tree protocol.
29 STP_ALLOWED_MODES="rstp stp"
30
31 function stp_enable() {
32 local bridge=${1}
33 local mode=${2}
34
35 assert isset bridge
36 assert zone_exists ${bridge}
37
38 # Tell the kernel to enable STP.
39 brctl stp ${bridge} on
40
41 # Set the correct protocol version.
42 if [ -z "${mode}" ]; then
43 mode="${STP_DEFAULT_MODE}"
44 fi
45 stp_bridge_set_protocol ${bridge} ${mode}
46 }
47
48 function stp_disable() {
49 local bridge=${1}
50
51 assert isset bridge
52 assert zone_exists ${bridge}
53
54 brctl stp ${bridge} off
55 }
56
57 function stp_get_name() {
58 local proto=${1}
59
60 case "${proto}" in
61 stp)
62 echo "Spanning Tree Protocol"
63 ;;
64 rstp)
65 echo "Rapid Spanning Tree Protocol"
66 ;;
67 mstp)
68 echo "Multiple Spanning Tree Protocol"
69 ;;
70 esac
71
72 return ${EXIT_OK}
73 }
74
75 function stp_bridge_set_protocol() {
76 local bridge=${1}
77 local mode=${2}
78
79 assert isset bridge
80 assert isset mode
81
82 if ! listmatch ${mode} ${STP_ALLOWED_MODES}; then
83 log WARNING "Unknown protocol version: ${mode}."
84 log WARNING "Using default mode."
85
86 mode="${STP_DEFAULT_MODE}"
87 fi
88
89 mstpctl setforcevers ${bridge} ${mode}
90 assert [ $? -eq 0 ]
91 }
92
93 function stp_bridge_get_protocol() {
94 local bridge=${1}
95
96 assert isset bridge
97
98 # Let's check what the kernel is telling us about it's STP state.
99 local state=$(__device_get_file ${bridge} "bridge/stp_state")
100
101 case "${state}" in
102 0)
103 # STP is disabled.
104 return ${EXIT_OK}
105 ;;
106 1)
107 # Kernel mode STP is running.
108 echo "stp"
109 return ${EXIT_OK}
110 ;;
111 2)
112 # User-space STP is running.
113 ;;
114 *)
115 log ERROR "Kernel is running in an unknown STP state."
116 return ${EXIT_ERROR}
117 ;;
118 esac
119
120 # We get here, when STP is running in user-space mode.
121
122 # Get the current protocol version.
123 mstpctl showbridge ${bridge} force-protocol-version
124
125 return ${EXIT_OK}
126 }
127
128 function stp_bridge_get_id() {
129 local bridge=${1}
130
131 assert isset bridge
132
133 __device_get_file ${bridge} "bridge/bridge_id"
134
135 return $?
136 }
137
138 function stp_bridge_get_forward_delay() {
139 local bridge=${1}
140
141 assert isset bridge
142
143 mstpctl showbridge ${bridge} forward-delay
144 }
145
146 function stp_bridge_set_forward_delay() {
147 : # XXX WANTED
148 }
149
150 function stp_bridge_get_designated_root() {
151 local bridge=${1}
152 local output
153
154 assert isset bridge
155
156 local output=$(mstpctl showbridge ${bridge} designated-root)
157
158 if ! isset output; then
159 return ${EXIT_ERROR}
160 fi
161
162 echo "${output,,}"
163 return ${EXIT_OK}
164 }
165
166 function stp_bridge_get_root_path_cost() {
167 local bridge=${1}
168
169 assert isset bridge
170
171 mstpctl showbridge ${bridge} path-cost
172 }
173
174 function stp_bridge_get_root_port_id() {
175 local bridge=${1}
176
177 assert isset bridge
178
179 local root_port=$(mstpctl showbridge ${bridge} root-port)
180
181 # Return error, when there is no root port.
182 if [ "${root_port}" = "none" ]; then
183 return ${EXIT_ERROR}
184 fi
185
186 echo "${root_port}"
187 return ${EXIT_OK}
188 }
189
190 function stp_bridge_get_root_port() {
191 local bridge=${1}
192
193 assert isset bridge
194
195 local id=$(stp_bridge_get_root_port_id ${bridge})
196
197 local member
198 local member_id
199 for member in $(bridge_get_members ${bridge}); do
200 member_id=$(stp_port_get_id ${bridge} ${member})
201
202 if [ "${id}" = "${member_id}" ]; then
203 echo "${member}"
204 return ${EXIT_OK}
205 fi
206 done
207
208 return ${EXIT_ERROR}
209 }
210
211 function stp_bridge_is_root() {
212 stp_bridge_get_root_port_id $@ >/dev/null
213 local ret=$?
214
215 if [ ${ret} -eq ${EXIT_ERROR} ]; then
216 return ${EXIT_OK}
217 fi
218
219 return ${EXIT_ERROR}
220 }
221
222 function stp_bridge_get_priority() {
223 local bridge=${1}
224
225 assert isset bridge
226
227 local id=$(stp_bridge_get_id ${bridge})
228
229 dec "${id:0:4}"
230 }
231
232 function stp_bridge_get_topology_change_count() {
233 local bridge=${1}
234
235 assert isset bridge
236
237 mstpctl showbridge ${bridge} topology-change-count
238 }
239
240 function stp_bridge_get_topology_change_timer() {
241 local bridge=${1}
242
243 assert isset bridge
244
245 mstpctl showbridge ${bridge} time-since-topology-change
246 }
247
248 function stp_bridge_get_topology_change_detected() {
249 local bridge=${1}
250
251 assert isset bridge
252
253 local change=$(mstpctl showbridge ${bridge} topology-change)
254
255 echo "${change}"
256 case "${change}" in
257 yes)
258 return ${EXIT_OK}
259 ;;
260 *)
261 return ${EXIT_ERROR}
262 ;;
263 esac
264 }
265
266 function stp_port_get_state() {
267 local bridge=${1}
268 local port=${2}
269
270 assert isset bridge
271 assert isset port
272
273 local state=$(mstpctl showportdetail ${bridge} ${port} state)
274
275 echo "${state^^}"
276 }
277
278 function stp_port_get_id() {
279 local bridge=${1}
280 local port=${2}
281
282 assert isset bridge
283 assert isset port
284
285 local id=$(__device_get_file ${bridge} "brif/${port}/port_no")
286
287 dec ${id}
288
289 return ${EXIT_OK}
290 }
291
292 function stp_port_get_cost() {
293 local bridge=${1}
294 local port=${2}
295
296 assert isset bridge
297 assert isset port
298
299 mstpctl showportdetail ${bridge} ${port} external-port-cost
300
301 return ${EXIT_ERROR}
302 }
303
304 function stp_port_get_designated_root() {
305 local bridge=${1}
306 local port=${2}
307 local output
308
309 assert isset bridge
310 assert isset port
311
312 output=$(mstpctl showportdetail ${bridge} ${port} designated-root)
313
314 if [ -n "${output}" ]; then
315 echo "${output,,}"
316 return ${EXIT_OK}
317 fi
318
319 return ${EXIT_ERROR}
320 }