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
This commit is contained in:
Ihar Hrachyshka 2018-04-12 21:51:30 +00:00
parent c16d15fff2
commit 38d0b2b52d
2 changed files with 10 additions and 3 deletions

View File

@ -42,10 +42,12 @@ class OVSAgentBridge(ofswitch.OpenFlowSwitchMixin,
""" """
while True: while True:
if self._cached_dpid is None: 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", LOG.info("Bridge %(br_name)s has datapath-ID %(dpid)s",
{"br_name": self.br_name, "dpid": dpid_str}) {"br_name": self.br_name, "dpid": dpid})
self._cached_dpid = int(dpid_str, 16) if dpid is None:
raise RuntimeError("Unknown datapath id.")
self._cached_dpid = int(dpid, 16)
try: try:
dp = self._get_dp_by_dpid(self._cached_dpid) dp = self._get_dp_by_dpid(self._cached_dpid)
return dp, dp.ofproto, dp.ofproto_parser return dp, dp.ofproto, dp.ofproto_parser

View File

@ -49,3 +49,8 @@ class OVSAgentBridgeTestCase(ovs_test_base.OVSRyuTestBase):
# make sure that in case of any misconfiguration when no datapath is # make sure that in case of any misconfiguration when no datapath is
# found a proper exception, not a TypeError is raised # found a proper exception, not a TypeError is raised
self.assertRaises(RuntimeError, br._get_dp) 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)