1
"""Bridge control utilities.
8
CMD_IFCONFIG = 'ifconfig'
11
CMD_IPTABLES = "iptables"
17
def __init__(self, defaults):
18
for (k, v) in defaults.items():
23
"""Print and execute command 'p' with args 's'.
27
if opts.verbose: print c
31
bridgeRE = re.compile(r'([^\t]*)\t*[^\t]*\t*[^\t]*\t*([^\t]*)')
33
fin = os.popen(CMD_BRCTL + ' show', 'r')
43
brlist.append(line.strip())
46
bridges[brname] = brlist
47
m = bridgeRE.match(line)
49
brlist = [m.group(2).strip()]
51
bridges[brname] = brlist
56
def vif_bridge_add(params):
57
"""Add the network interface for vif on dom to a bridge.
59
cmd(CMD_BRCTL, 'addif %(bridge)s %(vif)s' % params)
61
def vif_bridge_rem(params):
62
"""Remove the network interface for vif on dom from a bridge.
64
cmd(CMD_BRCTL, 'delif %(bridge)s %(vif)s' % params)
66
def vif_restrict_addr(vif, addr, delete=0):
67
d = { 'vif': vif, 'addr': addr}
72
cmd(CMD_IPTABLES, '-P FORWARD DROP')
73
cmd(CMD_IPTABLES, '%(flag)s FORWARD -m physdev --physdev-in %(vif)s -s %(addr)s -j ACCEPT' % d)
74
cmd(CMD_IPTABLES, '%(flag)s FORWARD -m physdev --physdev-out %(vif)s -d %(addr)s -j ACCEPT' % d)
76
def bridge_create(bridge, **kwd):
78
Defaults hello time to 0, forward delay to 0 and stp off.
80
cmd(CMD_BRCTL, 'addbr %s' % bridge)
81
if kwd.get('hello', None) is None:
83
if kwd.get('fd', None) is None:
85
if kwd.get('stp', None) is None:
87
bridge_set(bridge, **kwd)
88
cmd(CMD_IFCONFIG, "%s up" % bridge)
90
def bridge_set(bridge, hello=None, fd=None, stp=None):
91
"""Set bridge parameters.
94
cmd(CMD_BRCTL, 'sethello %s %d' % (bridge, hello))
96
cmd(CMD_BRCTL, 'setfd %s %d' % (bridge, fd))
98
cmd(CMD_BRCTL, 'stp %s %s' % (bridge, stp))
100
def bridge_del(bridge):
103
cmd(CMD_IFCONFIG, '%s down' % bridge)
104
cmd(CMD_BRCTL, 'delbr %s' % bridge)
107
"""Return a list of the routes.
109
fin = os.popen(CMD_ROUTE + ' -n', 'r')
112
if x.startswith('Kernel'): continue
113
if x.startswith('Destination'): continue
116
z = { 'destination': y[0],
127
def ifconfig(interface):
128
"""Return the ip config for an interface,
130
fin = os.popen(CMD_IFCONFIG + ' %s' % interface, 'r')
131
inetre = re.compile('\s*inet\s*addr:(?P<address>\S*)\s*Bcast:(?P<broadcast>\S*)\s*Mask:(?P<mask>\S*)')
137
info['interface'] = interface
141
def reconfigure(interface, bridge):
142
"""Reconfigure an interface to be attached to a bridge, and give the bridge
143
the IP address etc. from interface. Move the default route to the interface
148
intf_info = ifconfig(interface)
150
print >>sys.stderr, 'Interface not found:', interface
152
#bridge_info = ifconfig(bridge)
154
# print >>sys.stderr, 'Bridge not found:', bridge
156
route_info = routes()
157
intf_info['bridge'] = bridge
158
intf_info['gateway'] = None
160
if (r['destination'] == '0.0.0.0' and
161
'G' in r['flags'] and
162
r['interface'] == interface):
163
intf_info['gateway'] = r['gateway']
164
if not intf_info['gateway']:
165
print >>sys.stderr, 'Gateway not found: ', interface
168
'%(bridge)s %(address)s netmask %(mask)s broadcast %(broadcast)s up'
171
'add default gateway %(gateway)s dev %(bridge)s'
173
cmd(CMD_BRCTL, 'addif %(bridge)s %(interface)s' % intf_info)
174
cmd(CMD_IFCONFIG, '%(interface)s 0.0.0.0' % intf_info)
181
opts = Opts(defaults)