]> git.ipfire.org Git - people/stevee/network.git/blob - functions.switch
switch: Fix attaching/detaching ports.
[people/stevee/network.git] / functions.switch
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2012 IPFire Network Development Team #
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 function switch_create() {
23 local device=${1}
24 assert isset device
25
26 log DEBUG "Creating virtual switch: ${device}"
27 ovs-vsctl -- --may-exist add-br ${device}
28
29 assert device_exists ${device}
30
31 return ${EXIT_OK}
32 }
33
34 function switch_remove() {
35 local device=${1}
36 assert isset device
37
38 # Set device down.
39 device_set_down ${device}
40
41 log DEBUG "Removing virtual switch: ${device}"
42 ovs-vsctl -- --if-exists del-br ${device}
43
44 return ${EXIT_OK}
45 }
46
47 function switch_exists() {
48 local device=${1}
49 assert isset device
50
51 ovs-vsctl -- br-exists ${device}
52 case "$?" in
53 0)
54 return ${EXIT_TRUE}
55 ;;
56 2)
57 return ${EXIT_FALSE}
58 ;;
59 esac
60
61 return ${EXIT_ERROR}
62 }
63
64 function switch_get_members() {
65 local device=${1}
66 assert isset device
67
68 ovs-vsctl -- list-ports ${device}
69 return ${EXIT_OK}
70 }
71
72 function switch_attach_port() {
73 local device=${1}
74 assert isset device
75
76 local port=${2}
77 assert isset port
78
79 log DEBUG "Attaching port '${port}' to switch '${device}'"
80 ovs-vsctl -- --may-exist add-port "${device}" "${port}"
81
82 return ${EXIT_OK}
83 }
84
85 function switch_detach_port() {
86 local device=${1}
87 assert isset device
88
89 local port=${2}
90 assert isset port
91
92 log DEBUG "Detaching port '${port}' from switch '${device}'"
93 ovs-vsctl -- --if-exists del-port "${device}" "${port}"
94
95 return ${EXIT_OK}
96 }
97
98 function switch_stp_enable() {
99 local device=${1}
100 assert isset device
101
102 log DEBUG "Enable STP on switch ${device}"
103 ovs-vsctl set Bridge ${device} "stp_enable=true"
104
105 return ${EXIT_OK}
106 }
107
108 function switch_stp_disable() {
109 local device=${1}
110 assert isset device
111
112 log DEBUG "Disable STP on switch ${device}"
113 ovs-vsctl set Bridge ${device} "stp_enable=false"
114
115 return ${EXIT_OK}
116 }
117
118 function switch_stp_is_enabled() {
119 local device=${1}
120 assert isset device
121
122 local output=$(ovs-vsctl -- --if-exists get Bridge ${device} stp_enable)
123
124 if enabled output; then
125 return ${EXIT_TRUE}
126 fi
127
128 return ${EXIT_FALSE}
129 }