Skip to content

Commit 5833956

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "If method set_netns fails, restore previous device namespace" into stable/2023.1
2 parents 77ba5fd + 3eddcd9 commit 5833956

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

neutron/agent/linux/interface.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -374,11 +374,6 @@ def _add_device_to_namespace(self, ip_wrapper, device, namespace):
374374
LOG.warning("Failed to set interface %s into namespace %s. "
375375
"Interface not found, attempt: %s, retrying.",
376376
device, namespace, i + 1)
377-
# NOTE(slaweq) In such case it's required to reset device's
378-
# namespace as it was already set to the "namespace"
379-
# and after retry neutron will look for it in that namespace
380-
# which is wrong
381-
device.namespace = None
382377
time.sleep(1)
383378
except utils.WaitTimeout:
384379
# NOTE(slaweq): if the exception was WaitTimeout then it means

neutron/agent/linux/ip_lib.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from neutron_lib import exceptions
2626
from oslo_config import cfg
2727
from oslo_log import log as logging
28+
from oslo_utils import excutils
2829
from oslo_utils import netutils
2930
from pyroute2.netlink import exceptions as netlink_exceptions
3031
from pyroute2.netlink import rtnl
@@ -477,16 +478,16 @@ def set_down(self):
477478
self.name, self._parent.namespace, state='down')
478479

479480
def set_netns(self, namespace, is_ovs_port=False):
480-
privileged.set_link_attribute(
481-
self.name, self._parent.namespace, net_ns_fd=namespace)
482-
self._parent.namespace = namespace
483-
if is_ovs_port:
484-
# NOTE(slaweq): because of the "shy port" which may dissapear for
485-
# short time after it's moved to the namespace we need to wait
486-
# a bit before checking if port really exists in the namespace
487-
time.sleep(1)
488-
common_utils.wait_until_true(lambda: self.exists, timeout=5,
489-
sleep=0.5)
481+
old_namespace = self._parent.namespace
482+
try:
483+
privileged.set_link_attribute(
484+
self.name, self._parent.namespace, net_ns_fd=namespace)
485+
self._parent.namespace = namespace
486+
common_utils.wait_until_true(lambda: self.exists, timeout=3,
487+
sleep=0.5)
488+
except common_utils.WaitTimeout:
489+
with excutils.save_and_reraise_exception():
490+
self._parent.namespace = old_namespace
490491

491492
def set_name(self, name):
492493
privileged.set_link_attribute(

neutron/tests/unit/agent/linux/test_interface.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
# License for the specific language governing permissions and limitations
1414
# under the License.
1515

16+
import time
1617
from unittest import mock
1718

1819
from neutron_lib import constants
1920
from neutron_lib.plugins.ml2 import ovs_constants as ovs_const
2021
from oslo_utils import excutils
22+
from oslo_utils import uuidutils
2123
from pyroute2.netlink import exceptions as pyroute2_exc
2224

2325
from neutron.agent.common import ovs_lib
@@ -387,6 +389,10 @@ def test_set_mtu_logs_once(self):
387389

388390
class TestOVSInterfaceDriver(TestBase):
389391

392+
def setUp(self):
393+
super().setUp()
394+
self.mock_sleep = mock.patch.object(time, 'sleep').start()
395+
390396
def test_get_device_name(self):
391397
br = interface.OVSInterfaceDriver(self.conf)
392398
device_name = br.get_device_name(FakePort())
@@ -525,13 +531,14 @@ def test__add_device_to_namespace_retries(self):
525531
self.ip.ensure_namespace.return_value = namespace_obj
526532
namespace_obj.add_device_to_namespace.side_effect = (
527533
ip_lib.NetworkInterfaceNotFound)
528-
device = mock.MagicMock()
534+
namespace_name = uuidutils.generate_uuid()
535+
device = mock.MagicMock(namespace=namespace_name)
529536
self.assertRaises(
530537
ip_lib.NetworkInterfaceNotFound,
531538
ovs._add_device_to_namespace,
532539
self.ip, device, "test-ns")
533540
self.assertEqual(10, namespace_obj.add_device_to_namespace.call_count)
534-
self.assertIsNone(device.namespace)
541+
self.assertEqual(namespace_name, device.namespace)
535542

536543

537544
class TestOVSInterfaceDriverWithVeth(TestOVSInterfaceDriver):

0 commit comments

Comments
 (0)