From cff6a2db8820e2d1d4ba461025de6bd7b882c663 Mon Sep 17 00:00:00 2001 From: Ihar Hrachyshka Date: Thu, 12 Apr 2018 21:51:30 +0000 Subject: [PATCH] ovs: raise RuntimeError in _get_dp if id is None If the switch misbehaves, we may receive None from db_get_val. In this case, int() on the return value will raise TypeError which is not expected by callers and may result in ovs agent crash. Instead of bubbling up the TypeError exception, we raise RuntimeError if datapath id is None. Change-Id: I53bea00b9a7302d694b8066e969c894bf64cb2d4 Closes-Bug: #1731494 (cherry picked from commit 38d0b2b52d7daf77cd3d5123bd2d9853fea7448f) --- .../openvswitch/agent/openflow/native/ovs_bridge.py | 8 +++++--- .../openvswitch/agent/openflow/native/test_ovs_bridge.py | 5 +++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/neutron/plugins/ml2/drivers/openvswitch/agent/openflow/native/ovs_bridge.py b/neutron/plugins/ml2/drivers/openvswitch/agent/openflow/native/ovs_bridge.py index 14be2687148..f639cd47f66 100644 --- a/neutron/plugins/ml2/drivers/openvswitch/agent/openflow/native/ovs_bridge.py +++ b/neutron/plugins/ml2/drivers/openvswitch/agent/openflow/native/ovs_bridge.py @@ -42,10 +42,12 @@ class OVSAgentBridge(ofswitch.OpenFlowSwitchMixin, """ while True: if self._cached_dpid is None: - dpid_str = self.get_datapath_id() + dpid = self.get_datapath_id() LOG.info("Bridge %(br_name)s has datapath-ID %(dpid)s", - {"br_name": self.br_name, "dpid": dpid_str}) - self._cached_dpid = int(dpid_str, 16) + {"br_name": self.br_name, "dpid": dpid}) + if dpid is None: + raise RuntimeError("Unknown datapath id.") + self._cached_dpid = int(dpid, 16) try: dp = self._get_dp_by_dpid(self._cached_dpid) return dp, dp.ofproto, dp.ofproto_parser diff --git a/neutron/tests/unit/plugins/ml2/drivers/openvswitch/agent/openflow/native/test_ovs_bridge.py b/neutron/tests/unit/plugins/ml2/drivers/openvswitch/agent/openflow/native/test_ovs_bridge.py index b022c067db5..438a61d7528 100644 --- a/neutron/tests/unit/plugins/ml2/drivers/openvswitch/agent/openflow/native/test_ovs_bridge.py +++ b/neutron/tests/unit/plugins/ml2/drivers/openvswitch/agent/openflow/native/test_ovs_bridge.py @@ -49,3 +49,8 @@ class OVSAgentBridgeTestCase(ovs_test_base.OVSRyuTestBase): # make sure that in case of any misconfiguration when no datapath is # found a proper exception, not a TypeError is raised self.assertRaises(RuntimeError, br._get_dp) + + def test__get_dp_when_get_datapath_id_returns_None(self): + br = self.br_int_cls('br-int') + with mock.patch.object(br, 'get_datapath_id', return_value=None): + self.assertRaises(RuntimeError, br._get_dp)