From 0255eb31e5c3ce31ed08069c4e5c0fcb1fc29074 Mon Sep 17 00:00:00 2001 From: zhaimengdong <429322003@qq.com> Date: Mon, 27 Dec 2021 14:06:39 +0800 Subject: [PATCH] Fix a error when l3-agent gets filter id for ip Before using filter_id in _get_filterid_for_ip(), we should assign it first. Closes-Bug: #1955775 Change-Id: Icea0a8b756f210761b635d2d34e2af04a939a640 --- neutron/agent/linux/l3_tc_lib.py | 3 +- .../tests/unit/agent/linux/test_l3_tc_lib.py | 29 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/neutron/agent/linux/l3_tc_lib.py b/neutron/agent/linux/l3_tc_lib.py index f8fd7177e17..aa9ec701148 100644 --- a/neutron/agent/linux/l3_tc_lib.py +++ b/neutron/agent/linux/l3_tc_lib.py @@ -63,6 +63,7 @@ class FloatingIPTcCommandBase(ip_lib.IPDevice): if not filters_output: raise exceptions.FilterIDForIPNotFound(ip=ip) filter_lines = filters_output.split('\n') + filter_id = None for line in filter_lines: line = line.strip() m = FILTER_ID_REGEX.match(line) @@ -73,7 +74,7 @@ class FloatingIPTcCommandBase(ip_lib.IPDevice): if not line.startswith('match'): continue parts = line.split(" ") - if ip + '/32' in parts: + if ip + '/32' in parts and filter_id: filterids_for_ip.append(filter_id) if len(filterids_for_ip) > 1: raise exceptions.MultipleFilterIDForIPFound(ip=ip) diff --git a/neutron/tests/unit/agent/linux/test_l3_tc_lib.py b/neutron/tests/unit/agent/linux/test_l3_tc_lib.py index 34c6e1ad41d..8311126b705 100644 --- a/neutron/tests/unit/agent/linux/test_l3_tc_lib.py +++ b/neutron/tests/unit/agent/linux/test_l3_tc_lib.py @@ -133,6 +133,26 @@ QDISC_IDS = {constants.INGRESS_DIRECTION: INGRESS_QSIC_ID, TC_QDISCS = [{'handle': '1:', 'qdisc_type': 'htb', 'parent': 'root'}, {'handle': 'ffff:', 'qdisc_type': 'ingress', 'parent': 'ingress'}] +TC_FILTERS_ADD_BY_OTHERS_BASE = ( + 'filter protocol %(protocol)s u32 %(chain_name)s \n' + 'filter protocol %(protocol)s u32 %(chain_name)s fh 800: ht divisor 1 \n' + 'filter protocol %(protocol)s u32 %(chain_name)s fh %(filter_id1)s order ' + '2048 key ht 800 bkt 0 flowid :1 not_in_hw (rule hit 0 success 0) \n' + 'match %(matches)s at 16 (success 0 ) \n' + 'police 0x3 rate 3000Kbit burst 3Mb mtu 64Kb action drop overhead 0b ' + 'linklayer ethernet \n' + 'ref 1 bind 1 installed 22 sec used 22 sec \n' + '\n' + ' Sent 111 bytes 222 pkts (dropped 0, overlimits 0)\n' +) + +TC_FILTERS_ADD_BY_OTHERS_FOR_ALL_PROTOCOL = TC_FILTERS_ADD_BY_OTHERS_BASE % { + "protocol": "all", + "chain_name": "chain 1 ", + "filter_id1": FILETER_ID_1, + "matches": "c0a8dc06/ffffffff" +} + class TestFloatingIPTcCommandBase(base.BaseTestCase): def setUp(self): @@ -227,6 +247,15 @@ class TestFloatingIPTcCommandBase(base.BaseTestCase): def test__get_filterid_for_ip_not_found_with_chain(self): self._test__get_filterid_for_ip_not_found(TC_EGRESS_FILTERS_WITH_CHAIN) + def test__get_filterid_for_ip_with_additional_filters(self): + with mock.patch.object(tc_lib.FloatingIPTcCommandBase, + '_get_filters') as get_filters: + get_filters.return_value = \ + TC_FILTERS_ADD_BY_OTHERS_FOR_ALL_PROTOCOL + self.assertRaises(exceptions.FilterIDForIPNotFound, + self.tc._get_filterid_for_ip, + INGRESS_QSIC_ID, "192.168.220.6") + def test__del_filter_by_id(self): self.tc._del_filter_by_id(INGRESS_QSIC_ID, FLOATING_IP_1) self.execute.assert_called_once_with(