From bfcad8eec717fbed99a3ece23fb5b62634d414ae Mon Sep 17 00:00:00 2001 From: Andreas Scheuring Date: Thu, 28 Jan 2016 15:17:42 +0100 Subject: [PATCH] macvtap: Common functions and constants Functions and constants that are shared between the macvtap ml2 driver and the macvtap agent. The review is submitted in three parts: - Part 1 (this part) Common functions that are used by the ml2 driver and the agent - Part 2 The Mechanism Driver to support port binding for macvtap attachments - Part 3 The Macvtap L2 Agent. Partial-Bug: #1480979 Change-Id: I63a095e6f592b94372ff018f2e73373ad9414d99 --- neutron/common/constants.py | 1 + .../plugins/ml2/drivers/macvtap/__init__.py | 0 .../ml2/drivers/macvtap/macvtap_common.py | 30 ++++++++++ .../plugins/ml2/drivers/macvtap/__init__.py | 0 .../drivers/macvtap/test_macvtap_common.py | 60 +++++++++++++++++++ 5 files changed, 91 insertions(+) create mode 100644 neutron/plugins/ml2/drivers/macvtap/__init__.py create mode 100644 neutron/plugins/ml2/drivers/macvtap/macvtap_common.py create mode 100644 neutron/tests/unit/plugins/ml2/drivers/macvtap/__init__.py create mode 100644 neutron/tests/unit/plugins/ml2/drivers/macvtap/test_macvtap_common.py diff --git a/neutron/common/constants.py b/neutron/common/constants.py index 6bdc3eab624..ade0b6e8356 100644 --- a/neutron/common/constants.py +++ b/neutron/common/constants.py @@ -42,6 +42,7 @@ MINIMUM_AGENTS_FOR_HA = 2 HA_ROUTER_STATE_ACTIVE = 'active' HA_ROUTER_STATE_STANDBY = 'standby' +AGENT_TYPE_MACVTAP = 'Macvtap agent' PAGINATION_INFINITE = 'infinite' SORT_DIRECTION_ASC = 'asc' diff --git a/neutron/plugins/ml2/drivers/macvtap/__init__.py b/neutron/plugins/ml2/drivers/macvtap/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/neutron/plugins/ml2/drivers/macvtap/macvtap_common.py b/neutron/plugins/ml2/drivers/macvtap/macvtap_common.py new file mode 100644 index 00000000000..fe395285e7a --- /dev/null +++ b/neutron/plugins/ml2/drivers/macvtap/macvtap_common.py @@ -0,0 +1,30 @@ +# Copyright (c) 2016 IBM Corp. +# +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from neutron.common import constants as n_const +from neutron.plugins.common import utils as p_utils + +MAX_VLAN_POSTFIX_LEN = 5 + + +def get_vlan_device_name(src_dev, vlan): + """Generating the vlan device name.""" + + # Ensure that independent of the vlan len the same name prefix is used. + src_dev = p_utils.get_interface_name(src_dev, + max_len=n_const.DEVICE_NAME_MAX_LEN - + MAX_VLAN_POSTFIX_LEN) + return "%s.%s" % (src_dev, vlan) diff --git a/neutron/tests/unit/plugins/ml2/drivers/macvtap/__init__.py b/neutron/tests/unit/plugins/ml2/drivers/macvtap/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/neutron/tests/unit/plugins/ml2/drivers/macvtap/test_macvtap_common.py b/neutron/tests/unit/plugins/ml2/drivers/macvtap/test_macvtap_common.py new file mode 100644 index 00000000000..ce25005c3d2 --- /dev/null +++ b/neutron/tests/unit/plugins/ml2/drivers/macvtap/test_macvtap_common.py @@ -0,0 +1,60 @@ +# Copyright (c) 2016 IBM Corp. +# +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import hashlib +import mock + +from neutron.plugins.ml2.drivers.macvtap import macvtap_common as m_common +from neutron.tests import base + +MOCKED_HASH = "MOCKEDHASH" + + +class MockSHA(object): + def hexdigest(self): + return MOCKED_HASH + + +class MacvtapCommonTestCase(base.BaseTestCase): + @mock.patch.object(hashlib, 'sha1', return_value=MockSHA()) + def test_get_vlan_device_name(self, mocked_hash): + # only the first six chars of the hash are being used in the algorithm + hash_used = MOCKED_HASH[0:6] + self.assertEqual('10charrrrr.1', + m_common.get_vlan_device_name('10charrrrr', "1")) + self.assertEqual('11ch' + hash_used + '.1', + m_common.get_vlan_device_name('11charrrrrr', "1")) + self.assertEqual('14ch' + hash_used + '.1', + m_common.get_vlan_device_name('14charrrrrrrrr', "1")) + self.assertEqual('14ch' + hash_used + '.1111', + m_common.get_vlan_device_name('14charrrrrrrrr', + "1111")) + + def test_get_vlan_subinterface_name_advanced(self): + """Ensure the same hash is used for long interface names. + + If the generated vlan device name would be too long, make sure that + everything before the '.' is equal. This might be helpful when + debugging problems. + """ + + max_device_name = "15charrrrrrrrrr" + vlan_dev_name1 = m_common.get_vlan_device_name(max_device_name, + "1") + vlan_dev_name2 = m_common.get_vlan_device_name(max_device_name, + "1111") + self.assertEqual(vlan_dev_name1.partition(".")[0], + vlan_dev_name2.partition(".")[0])