Enable user to configure base mac address.

In addition to this enable the user to configure the amount of
retries to generate a unique MAC address

Change-Id: Ic109ae65993141c6dc7d63f394066a31e79e2b4c
This commit is contained in:
Gary Kotton 2012-06-26 02:15:46 -04:00
parent 6edab9de81
commit f54a788cae
4 changed files with 17 additions and 8 deletions

View File

@ -23,3 +23,10 @@ core_plugin = quantum.plugins.sample.SamplePlugin.FakePlugin
# Paste configuration file # Paste configuration file
api_paste_config = api-paste.ini 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

View File

@ -39,6 +39,8 @@ bind_opts = [
cfg.StrOpt('api_extensions_path', default=""), cfg.StrOpt('api_extensions_path', default=""),
cfg.StrOpt('core_plugin', cfg.StrOpt('core_plugin',
default='quantum.plugins.sample.SamplePlugin.FakePlugin'), 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 # Register the configuration options

View File

@ -24,6 +24,7 @@ from quantum.api.v2 import router as api_router
from quantum.common import exceptions as q_exc from quantum.common import exceptions as q_exc
from quantum.db import api as db from quantum.db import api as db
from quantum.db import models_v2 from quantum.db import models_v2
from quantum.openstack.common import cfg
from quantum import quantum_plugin_base_v2 from quantum import quantum_plugin_base_v2
@ -132,11 +133,11 @@ class QuantumDbPluginV2(quantum_plugin_base_v2.QuantumPluginBaseV2):
@staticmethod @staticmethod
def _generate_mac(context, network_id): def _generate_mac(context, network_id):
# TODO(garyk) read from configuration file (CONF) base_mac = cfg.CONF.base_mac.split(':')
max_retries = 16 max_retries = cfg.CONF.mac_generation_retries
for i in range(max_retries): for i in range(max_retries):
# TODO(garyk) read base mac from configuration file (CONF) mac = [int(base_mac[0], 16), int(base_mac[1], 16),
mac = [0xfa, 0x16, 0x3e, random.randint(0x00, 0x7f), int(base_mac[2], 16), random.randint(0x00, 0x7f),
random.randint(0x00, 0xff), random.randint(0x00, 0xff)] random.randint(0x00, 0xff), random.randint(0x00, 0xff)]
mac_address = ':'.join(map(lambda x: "%02x" % x, mac)) mac_address = ':'.join(map(lambda x: "%02x" % x, mac))
if QuantumDbPluginV2._check_unique_mac(context, network_id, if QuantumDbPluginV2._check_unique_mac(context, network_id,

View File

@ -61,6 +61,7 @@ class QuantumDbPluginV2TestCase(unittest.TestCase):
config.parse(args=args) config.parse(args=args)
# Update the plugin # Update the plugin
cfg.CONF.set_override('core_plugin', plugin) cfg.CONF.set_override('core_plugin', plugin)
cfg.CONF.set_override('base_mac', "12:34:56:78:90:ab")
self.api = APIRouter() self.api = APIRouter()
def tearDown(self): def tearDown(self):
@ -267,10 +268,8 @@ class TestPortsV2(QuantumDbPluginV2TestCase):
with self.port() as port: with self.port() as port:
mac = port['port']['mac_address'] mac = port['port']['mac_address']
# check that MAC address matches base MAC # check that MAC address matches base MAC
# TODO(garyk) read base mac from configuration file (CONF) base_mac = cfg.CONF.base_mac[0:2]
base_mac = [0xfa, 0x16, 0x3e] self.assertTrue(mac.startswith(base_mac))
base_mac_address = ':'.join(map(lambda x: "%02x" % x, base_mac))
self.assertTrue(mac.startswith(base_mac_address))
kwargs = {"mac_address": mac} kwargs = {"mac_address": mac}
net_id = port['port']['network_id'] net_id = port['port']['network_id']
res = self._create_port(fmt, net_id=net_id, **kwargs) res = self._create_port(fmt, net_id=net_id, **kwargs)