diff --git a/etc/quantum.conf b/etc/quantum.conf index e55bca08cf..643b0be9e2 100644 --- a/etc/quantum.conf +++ b/etc/quantum.conf @@ -23,3 +23,10 @@ core_plugin = quantum.plugins.sample.SamplePlugin.FakePlugin # Paste configuration file api_paste_config = api-paste.ini + +# Base MAC address. The first 3 bytes will remain unchanged. The +# lower 3 bytes will be randomly generated. +# base_mac = fa:16:3e:00:00:00 + +# Maximum amount of retries to generate a unique MAC address +# mac_generation_retries = 16 diff --git a/quantum/common/config.py b/quantum/common/config.py index 505bf233bd..73f5a8926c 100644 --- a/quantum/common/config.py +++ b/quantum/common/config.py @@ -39,6 +39,8 @@ bind_opts = [ cfg.StrOpt('api_extensions_path', default=""), cfg.StrOpt('core_plugin', default='quantum.plugins.sample.SamplePlugin.FakePlugin'), + cfg.StrOpt('base_mac', default="fa:16:3e:00:00:00"), + cfg.IntOpt('mac_generation_retries', default=16) ] # Register the configuration options diff --git a/quantum/db/db_base_plugin_v2.py b/quantum/db/db_base_plugin_v2.py index a2072dcc74..e72cc328a2 100644 --- a/quantum/db/db_base_plugin_v2.py +++ b/quantum/db/db_base_plugin_v2.py @@ -24,6 +24,7 @@ from quantum.api.v2 import router as api_router from quantum.common import exceptions as q_exc from quantum.db import api as db from quantum.db import models_v2 +from quantum.openstack.common import cfg from quantum import quantum_plugin_base_v2 @@ -132,11 +133,11 @@ class QuantumDbPluginV2(quantum_plugin_base_v2.QuantumPluginBaseV2): @staticmethod def _generate_mac(context, network_id): - # TODO(garyk) read from configuration file (CONF) - max_retries = 16 + base_mac = cfg.CONF.base_mac.split(':') + max_retries = cfg.CONF.mac_generation_retries for i in range(max_retries): - # TODO(garyk) read base mac from configuration file (CONF) - mac = [0xfa, 0x16, 0x3e, random.randint(0x00, 0x7f), + mac = [int(base_mac[0], 16), int(base_mac[1], 16), + int(base_mac[2], 16), random.randint(0x00, 0x7f), random.randint(0x00, 0xff), random.randint(0x00, 0xff)] mac_address = ':'.join(map(lambda x: "%02x" % x, mac)) if QuantumDbPluginV2._check_unique_mac(context, network_id, diff --git a/quantum/tests/unit/test_db_plugin.py b/quantum/tests/unit/test_db_plugin.py index 621d95e93d..e97145ec46 100644 --- a/quantum/tests/unit/test_db_plugin.py +++ b/quantum/tests/unit/test_db_plugin.py @@ -61,6 +61,7 @@ class QuantumDbPluginV2TestCase(unittest.TestCase): config.parse(args=args) # Update the plugin cfg.CONF.set_override('core_plugin', plugin) + cfg.CONF.set_override('base_mac', "12:34:56:78:90:ab") self.api = APIRouter() def tearDown(self): @@ -267,10 +268,8 @@ class TestPortsV2(QuantumDbPluginV2TestCase): with self.port() as port: mac = port['port']['mac_address'] # check that MAC address matches base MAC - # TODO(garyk) read base mac from configuration file (CONF) - base_mac = [0xfa, 0x16, 0x3e] - base_mac_address = ':'.join(map(lambda x: "%02x" % x, base_mac)) - self.assertTrue(mac.startswith(base_mac_address)) + base_mac = cfg.CONF.base_mac[0:2] + self.assertTrue(mac.startswith(base_mac)) kwargs = {"mac_address": mac} net_id = port['port']['network_id'] res = self._create_port(fmt, net_id=net_id, **kwargs)