3 # Copyright (c) 2009, 2010 Nicira Networks.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at:
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
25 BRCTL = "/usr/lib/openvswitch/xs-original/brctl"
26 VSCTL = "/usr/bin/ovs-vsctl"
27 OVSDB_SERVER = "unix:/var/run/openvswitch/db.sock"
29 # Execute the real brctl program, passing the same arguments that were passed
32 os.execl(BRCTL, BRCTL, *sys.argv[1:])
33 # execl should never return. We only arrive here if brctl failed to exec.
36 def call_vsctl(cmd, arg=""):
37 database = '--db=' + OVSDB_SERVER
38 command = [VSCTL, database, cmd]
41 return subprocess.Popen(command, stdout=subprocess.PIPE).communicate()[0].split()
43 # Returns a list of all the bridges
45 return call_vsctl('list-br')
47 # Returns a list of all ports on 'bridge'
48 def get_bridge_ports(bridge):
49 return call_vsctl('list-ports', bridge)
51 # Returns a list of all interfaces on 'bridge'
52 def get_bridge_ifaces(bridge):
53 return call_vsctl('list-ifaces', bridge)
55 # Returns the parent of 'bridge'. If 'bridge' does not have a parent,
56 # 'bridge' is returned.
57 def get_bridge_parent(bridge):
58 return call_vsctl('br-to-parent', bridge)
60 # Returns the first line of the file named 'name', with the trailing new-line
61 # (if any) stripped off.
62 def read_first_line_of_file(name):
65 file = open(name, 'r')
66 return file.readline().rstrip('\n')
71 # Returns a bridge ID constructed from the MAC address of network device
72 # 'netdev', in the format "8000.000102030405".
73 def get_bridge_id(netdev):
75 hwaddr = read_first_line_of_file("/sys/class/net/%s/address" % netdev)
76 return "8000.%s" % (hwaddr.replace(":", ""))
78 return "8000.002320ffffff"
81 print "bridge name\tbridge id\t\tSTP enabled\tinterfaces"
83 # Find all the bridges.
84 bridges = get_bridges()
86 # Find all the interfaces on each bridge.
87 for bridge in bridges:
88 bridge_ports = get_bridge_ports(bridge)
89 parent = get_bridge_parent(bridge)
90 if parent in bridge_ports:
91 bridge_ports.remove(parent)
93 bridge_id = get_bridge_id(bridge)
96 first_port = bridge_ports[0]
97 print "%s\t\t%s\t%s\t\t%s" % (bridge, bridge_id, "no", first_port)
98 for port in bridge_ports[1:]:
99 print "\t\t\t\t\t\t\t%s" % port
102 # Check the network configuration mode.
104 network_mode = read_first_line_of_file('/etc/xensource/network.conf')
105 if network_mode == 'bridge':
108 # File probably doesn't exist
111 # Parse the command line.
113 options, args = getopt.gnu_getopt(sys.argv[1:],
114 "hV", ["help", "version"])
115 except getopt.GetoptError, msg:
116 sys.stderr.write("%s: %s (use --help for help)\n" % (argv0, msg))
119 # Handle command-line options.
120 for opt, optarg in options:
121 if opt == "-h" or opt == "--help":
123 elif opt == "-V" or opt == "--version":
124 subprocess.call([BRCTL, "--version"])
125 print "Open vSwitch brctl wrapper"
128 # Execute commands. Most commands are delegated to the brctl binary that
129 # we are wrapping, but we implement the "show" command ourselves.
130 if args and args[0] == "show":
135 if __name__ == "__main__":