1 # Copyright (c) 2012 Nicira, Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
16 vswitch module allows its callers to interact with OVS DB.
24 def ovs_vsctl_add_bridge(bridge):
26 This function creates an OVS bridge.
28 ret, _out, _err = util.start_process(["ovs-vsctl", "add-br", bridge])
32 def ovs_vsctl_del_bridge(bridge):
34 This function deletes the OVS bridge.
36 ret, _out, _err = util.start_process(["ovs-vsctl", "del-br", bridge])
39 def ovs_vsctl_del_pbridge(bridge, iface):
41 This function deletes the OVS bridge and assigns the bridge IP address
44 (ip_addr, mask) = util.interface_get_ip(bridge)
45 util.interface_assign_ip(iface, ip_addr, mask)
46 util.move_routes(bridge, iface)
47 return ovs_vsctl_del_bridge(bridge)
50 def ovs_vsctl_is_ovs_bridge(bridge):
52 This function verifies whether given port is an OVS bridge. If it is an
53 OVS bridge then it will return True.
55 ret, _out, _err = util.start_process(["ovs-vsctl", "br-exists", bridge])
59 def ovs_vsctl_add_port_to_bridge(bridge, iface):
61 This function adds given interface to the bridge.
63 ret, _out, _err = util.start_process(["ovs-vsctl", "add-port", bridge,
68 def ovs_vsctl_del_port_from_bridge(port):
70 This function removes given port from a OVS bridge.
72 ret, _out, _err = util.start_process(["ovs-vsctl", "del-port", port])
76 def ovs_vsctl_set(table, record, column, key, value):
78 This function allows to alter the OVS database. If column is a map, then
79 caller should also set the key, otherwise the key should be left as an
85 index = "%s:%s" % (column, key)
86 index_value = "%s=%s" % (index, value)
87 ret, _out, _err = util.start_process(["ovs-vsctl", "set", table, record,
92 def ovs_get_physical_interface(bridge):
94 This function tries to figure out which is the physical interface that
95 belongs to the bridge. If there are multiple physical interfaces assigned
96 to this bridge then it will return the first match.
98 ret, out, _err = util.start_process(["ovs-vsctl", "list-ifaces", bridge])
101 ifaces = out.splitlines()
103 ret, out, _err = util.start_process(["ovs-vsctl", "get",
104 "Interface", iface, "type"])
106 if ('""' in out) or ('system' in out):
107 return iface # this should be the physical interface