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