diff options
Diffstat (limited to 'os_net_config')
-rw-r--r-- | os_net_config/impl_eni.py | 5 | ||||
-rw-r--r-- | os_net_config/impl_ifcfg.py | 67 | ||||
-rw-r--r-- | os_net_config/objects.py | 21 | ||||
-rw-r--r-- | os_net_config/tests/test_impl_eni.py | 2 | ||||
-rw-r--r-- | os_net_config/tests/test_impl_ifcfg.py | 54 |
5 files changed, 113 insertions, 36 deletions
diff --git a/os_net_config/impl_eni.py b/os_net_config/impl_eni.py index 79671d0..20677da 100644 --- a/os_net_config/impl_eni.py +++ b/os_net_config/impl_eni.py @@ -15,6 +15,7 @@ import netaddr import os_net_config +from os_net_config import objects from os_net_config import utils from os_net_config.openstack.common import processutils @@ -61,7 +62,7 @@ class ENINetConfig(os_net_config.NetConfig): else: _iface += "manual\n" data = "" - if interface.type == 'ovs_bridge': + if isinstance(interface, objects.OvsBridge): data += "allow-ovs %s\n" % interface.name data += _iface data += _static_addresses(_v4, _v6) @@ -71,7 +72,7 @@ class ENINetConfig(os_net_config.NetConfig): for i in interface.members: data += " %s" % i.name data += "\n" - elif interface.type == 'ovs_port': + elif interface.ovs_port: data += "allow-%s %s\n" % (interface.bridge_name, interface.name) data += _iface data += _static_addresses(_v4, _v6) diff --git a/os_net_config/impl_ifcfg.py b/os_net_config/impl_ifcfg.py index b4da7bc..e9715b8 100644 --- a/os_net_config/impl_ifcfg.py +++ b/os_net_config/impl_ifcfg.py @@ -13,6 +13,7 @@ # under the License. import os_net_config +from os_net_config import objects from os_net_config import utils @@ -40,68 +41,80 @@ class IfcfgNetConfig(os_net_config.NetConfig): self.routes = {} self.bridges = {} - def _addCommon(self, interface): - data = "DEVICE=%s\n" % interface.name + def _addCommon(self, base_opt): + data = "DEVICE=%s\n" % base_opt.name data += "ONBOOT=yes\n" data += "HOTPLUG=no\n" - if interface.type == 'ovs_port': + if isinstance(base_opt, objects.Vlan): + data += "VLAN=yes\n" + data += "PHYSDEV=%s\n" % base_opt.device + if base_opt.ovs_port: data += "DEVICETYPE=ovs\n" - if interface.bridge_name: - data += "TYPE=OVSPort\n" - data += "OVS_BRIDGE=%s\n" % interface.bridge_name - if interface.type == 'ovs_bridge': + if base_opt.bridge_name: + if isinstance(base_opt, objects.Vlan): + data += "TYPE=OVSIntPort\n" + data += "OVS_BRIDGE=%s\n" % base_opt.bridge_name + data += "OVS_OPTIONS=\"tag=%s\"\n" % base_opt.vlan_id + data += "OVS_EXTRA=\"set Interface $DEVICE " + data += "external-ids:iface-id=$(hostname -s)" + data += "-$DEVICE-vif\"\n" + else: + data += "TYPE=OVSPort\n" + data += "OVS_BRIDGE=%s\n" % base_opt.bridge_name + if isinstance(base_opt, objects.OvsBridge): data += "DEVICETYPE=ovs\n" data += "TYPE=OVSBridge\n" - if interface.use_dhcp: + if base_opt.use_dhcp: data += "OVSBOOTPROTO=dhcp\n" - if interface.members: - members = [member.name for member in interface.members] + if base_opt.members: + members = [member.name for member in base_opt.members] data += ("OVSDHCPINTERFACES=%s\n" % " ".join(members)) else: - if interface.use_dhcp: + if base_opt.use_dhcp: data += "BOOTPROTO=dhcp\n" - elif not interface.addresses: + elif not base_opt.addresses: data += "BOOTPROTO=none\n" - if interface.mtu != 1500: - data += "MTU=%i\n" % interface.mtu - if interface.use_dhcpv6 or interface.v6_addresses(): + if base_opt.mtu != 1500: + data += "MTU=%i\n" % base_opt.mtu + if base_opt.use_dhcpv6 or base_opt.v6_addresses(): data += "IPV6INIT=yes\n" - if interface.mtu != 1500: - data += "IPV6_MTU=%i\n" % interface.mtu - if interface.use_dhcpv6: + if base_opt.mtu != 1500: + data += "IPV6_MTU=%i\n" % base_opt.mtu + if base_opt.use_dhcpv6: data += "DHCPV6C=yes\n" - elif interface.addresses: - #TODO(dprince): support multiple addresses for each type - v4_addresses = interface.v4_addresses() + elif base_opt.addresses: + #TODO(dprince): Do we want to support multiple addresses? + v4_addresses = base_opt.v4_addresses() if v4_addresses: first_v4 = v4_addresses[0] data += "BOOTPROTO=static\n" data += "IPADDR=%s\n" % first_v4.ip data += "NETMASK=%s\n" % first_v4.netmask - v6_addresses = interface.v6_addresses() + v6_addresses = base_opt.v6_addresses() if v6_addresses: first_v6 = v6_addresses[0] data += "IPV6_AUTOCONF=no\n" data += "IPV6ADDR=%s\n" % first_v6.ip - return data def addInterface(self, interface): data = self._addCommon(interface) - self.interfaces[interface.name] = data if interface.routes: self._addRoutes(interface.name, interface.routes) + def addVlan(self, vlan): + data = self._addCommon(vlan) + self.interfaces[vlan.name] = data + if vlan.routes: + self._addRoutes(vlan.name, vlan.routes) + def addBridge(self, bridge): data = self._addCommon(bridge) - self.bridges[bridge.name] = data if bridge.routes: self._addRoutes(bridge.name, bridge.routes) - if bridge.routes: - self._addRoutes(bridge.name, bridge.routes) def _addRoutes(self, interface_name, routes=[]): data = "" diff --git a/os_net_config/objects.py b/os_net_config/objects.py index c4f5600..81f441b 100644 --- a/os_net_config/objects.py +++ b/os_net_config/objects.py @@ -52,7 +52,7 @@ class _BaseOpts(object): self.addresses = addresses self.routes = routes self.bridge_name = None - self.type = None + self.ovs_port = False def v4_addresses(self): v4_addresses = [] @@ -80,6 +80,22 @@ class Interface(_BaseOpts): routes, mtu) +class Vlan(_BaseOpts): + """Base class for VLANs. + + NOTE: the name parameter must be formated w/ vlan#### where #### + matches the vlan ID being used. Example: vlan5 + """ + + def __init__(self, device, vlan_id, use_dhcp=False, use_dhcpv6=False, + addresses=[], routes=[], mtu=1500): + name = 'vlan%i' % vlan_id + super(Vlan, self).__init__(name, use_dhcp, use_dhcpv6, addresses, + routes, mtu) + self.vlan_id = int(vlan_id) + self.device = device + + class OvsBridge(_BaseOpts): """Base class for OVS bridges.""" @@ -87,8 +103,7 @@ class OvsBridge(_BaseOpts): routes=[], members=[], mtu=1500): super(OvsBridge, self).__init__(name, use_dhcp, use_dhcpv6, addresses, routes, mtu) - self.type = 'ovs_bridge' self.members = members for member in self.members: member.bridge_name = name - member.type = 'ovs_port' + member.ovs_port = True diff --git a/os_net_config/tests/test_impl_eni.py b/os_net_config/tests/test_impl_eni.py index ff4f827..55c9c44 100644 --- a/os_net_config/tests/test_impl_eni.py +++ b/os_net_config/tests/test_impl_eni.py @@ -82,7 +82,7 @@ class TestENINetConfig(base.TestCase): def test_add_ovs_port_interface(self): interface = self._default_interface() - interface.type = 'ovs_port' + interface.ovs_port = True interface.bridge_name = 'br0' interface.use_dhcp = True self.provider.addInterface(interface) diff --git a/os_net_config/tests/test_impl_ifcfg.py b/os_net_config/tests/test_impl_ifcfg.py index e0df5b4..1c64abb 100644 --- a/os_net_config/tests/test_impl_ifcfg.py +++ b/os_net_config/tests/test_impl_ifcfg.py @@ -63,6 +63,27 @@ OVSBOOTPROTO=dhcp OVSDHCPINTERFACES=em1 """ +_BASE_VLAN = """DEVICE=vlan5 +ONBOOT=yes +HOTPLUG=no +VLAN=yes +PHYSDEV=em1 +""" + +_VLAN_NO_IP = _BASE_VLAN + "BOOTPROTO=none\n" + + +_VLAN_OVS = _BASE_VLAN + "DEVICETYPE=ovs\nBOOTPROTO=none\n" + + +VLAN_EXTRA = "\"set Interface $DEVICE external-ids:iface-id=$(hostname -s" + \ + ")-$DEVICE-vif\"\n" +_VLAN_OVS_BRIDGE = _BASE_VLAN + """DEVICETYPE=ovs +TYPE=OVSIntPort +OVS_BRIDGE=br-ctlplane +OVS_OPTIONS="tag=5" +OVS_EXTRA=""" + VLAN_EXTRA + "BOOTPROTO=none\n" + class TestIfcfgNetConfig(base.TestCase): @@ -74,8 +95,8 @@ class TestIfcfgNetConfig(base.TestCase): def tearDown(self): super(TestIfcfgNetConfig, self).tearDown() - def get_interface_config(self): - return self.provider.interfaces['em1'] + def get_interface_config(self, name='em1'): + return self.provider.interfaces[name] def get_route_config(self): return self.provider.routes['em1'] @@ -87,7 +108,7 @@ class TestIfcfgNetConfig(base.TestCase): def test_add_ovs_interface(self): interface = objects.Interface('em1') - interface.type = 'ovs_port' + interface.ovs_port = True self.provider.addInterface(interface) self.assertEqual(_OVS_IFCFG, self.get_interface_config()) @@ -122,6 +143,25 @@ class TestIfcfgNetConfig(base.TestCase): self.assertEqual(_OVS_BRIDGE_DHCP, self.provider.bridges['br-ctlplane']) + def test_add_vlan(self): + vlan = objects.Vlan('em1', 5) + self.provider.addVlan(vlan) + self.assertEqual(_VLAN_NO_IP, self.get_interface_config('vlan5')) + + def test_add_vlan_ovs(self): + vlan = objects.Vlan('em1', 5) + vlan.ovs_port = True + self.provider.addVlan(vlan) + self.assertEqual(_VLAN_OVS, self.get_interface_config('vlan5')) + + def test_add_ovs_bridge_with_vlan(self): + vlan = objects.Vlan('em1', 5) + bridge = objects.OvsBridge('br-ctlplane', use_dhcp=True, + members=[vlan]) + self.provider.addVlan(vlan) + self.provider.addBridge(bridge) + self.assertEqual(_VLAN_OVS_BRIDGE, self.get_interface_config('vlan5')) + class TestIfcfgNetConfigApply(base.TestCase): @@ -182,3 +222,11 @@ class TestIfcfgNetConfigApply(base.TestCase): self.assertEqual(_OVS_INTERFACE, ifcfg_data) bridge_data = utils.get_file_data(self.temp_bridge_file.name) self.assertEqual(_OVS_BRIDGE_DHCP, bridge_data) + + def test_vlan_apply(self): + vlan = objects.Vlan('em1', 5) + self.provider.addVlan(vlan) + self.provider.apply() + + ifcfg_data = utils.get_file_data(self.temp_ifcfg_file.name) + self.assertEqual(_VLAN_NO_IP, ifcfg_data) |