Commit bbcea33e by Bach Dániel

add bridge support (export BRIDGE_TYPE=BRIDGE)

parent 7597bad1
...@@ -4,7 +4,7 @@ import re ...@@ -4,7 +4,7 @@ import re
import json import json
import logging import logging
from ovs import Switch from ovs import Switch, Bridge
from utils import (ns_exec, sudo, ADDRESSES, from utils import (ns_exec, sudo, ADDRESSES,
dhcp_no_free_re, dhcp_ack_re) dhcp_no_free_re, dhcp_ack_re)
...@@ -26,6 +26,12 @@ celery.conf.update(CELERY_CACHE_BACKEND=CACHE_URI, ...@@ -26,6 +26,12 @@ celery.conf.update(CELERY_CACHE_BACKEND=CACHE_URI,
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
if getenv('BRIDGE_TYPE', 'OVS') == 'BRIDGE':
network_type = Bridge
else:
network_type = Switch
@task(name="firewall.reload_firewall") @task(name="firewall.reload_firewall")
def reload_firewall(data4, data6, save_config=True): def reload_firewall(data4, data6, save_config=True):
try: try:
...@@ -52,7 +58,8 @@ def reload_firewall_vlan(data, save_config=True): ...@@ -52,7 +58,8 @@ def reload_firewall_vlan(data, save_config=True):
if uplink: if uplink:
data[uplink] = {'interfaces': uplink} data[uplink] = {'interfaces': uplink}
br = Switch('firewall') print network_type
br = network_type('firewall')
br.migrate(data) br.migrate(data)
if save_config: if save_config:
......
...@@ -193,3 +193,81 @@ class Switch(object): ...@@ -193,3 +193,81 @@ class Switch(object):
interface.migrate() interface.migrate()
except CalledProcessError as e: except CalledProcessError as e:
logger.warning(e) logger.warning(e)
class Bridge(Switch):
def __init__(self, brname):
self.brname = brname
self.brifnum = brname
try:
sudo(('brctl', 'addbr', brname))
sudo(('ip', 'link', 'set', 'up', brname))
except:
pass
def find_data(self, data, tok):
try:
masteridx = data.index(tok)
return tuple(data[masteridx + 1:])
except (ValueError, IndexError):
return (None, )
def parse_ip_link(self, data):
port = None
ports = {}
for line in data.splitlines():
t = line.split()
if line.startswith(' '):
vlan = self.find_data(t, '802.1Q')
if port in ports and vlan and vlan[0] == 'id':
ports[port]['tag'] = vlan[1]
else:
port, sep, parent = t[1].rstrip(':').partition('@')
if self.find_data(t, 'master')[0] == self.brname:
type = 'external'
elif (parent in (self.brname, self.brifnum) or
port == self.brname):
type = 'internal'
else:
continue
ports[port] = {'type': type, 'ifnum': t[0].rstrip(':')}
return ports
def list_ports(self):
ports = self.parse_ip_link(sudo(('ip', '-d', 'link', 'show')))
brport = ports.pop(self.brname)
self.brifnum = 'if%s' % brport['ifnum']
ports.update(self.parse_ip_link(ns_exec(('ip', '-d', 'link', 'show'))))
return [Interface(name, data, with_show=True)
for name, data in ports.items()]
def delete_port(self, interface):
try:
if interface.is_internal:
ns_exec(('ip', 'link', 'del', interface.name))
else:
sudo(('brctl', 'delif', self.brname, interface.name))
except CalledProcessError:
pass
def add_port(self, interface):
try:
if interface.is_internal:
if not interface.untagged:
return
sudo(('ip', 'link', 'add', 'link', self.brname, 'name',
interface.name, 'type', 'vlan', 'id',
str(interface.untagged)))
self._setns(interface.name)
else:
sudo(('brctl', 'addif', self.brname, interface.name))
except:
logger.exception('Unhandled exception: ')
if __name__ == "__main__":
br = Bridge('br0')
print br.list_ports()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment