Skip to content

Commit cda325d

Browse files
committed
Handle ipv6 next-hop in IpMib rfc1213
Update the existing tests to verify IPv6 nexthop is not included in rfc1213 MIB response Signed-off-by: Huan V Le <[email protected]>
1 parent 6e0ff93 commit cda325d

File tree

4 files changed

+44
-7
lines changed

4 files changed

+44
-7
lines changed

src/sonic_ax_impl/mibs/ietf/rfc1213.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,12 @@ def update_data(self):
162162
continue
163163
for nh in nexthops.split(','):
164164
# TODO: if ipn contains IP range, create more sub_id here
165-
sub_id = ip2byte_tuple(ipn.network_address)
166-
self.route_list.append(sub_id)
167-
self.nexthop_map[sub_id] = ipaddress.ip_address(nh).packed
168-
break # Just need the first nexthop
165+
if type(ipaddress.ip_address(nh)) is ipaddress.IPv4Address:
166+
sub_id = ip2byte_tuple(ipn.network_address)
167+
self.route_list.append(sub_id)
168+
self.nexthop_map[sub_id] = ipaddress.ip_address(nh).packed
169+
break # Just need the first nexthop
170+
mibs.logger.warning("Route {} has non-IPv4 nexthop: {}".format(routestr, nh))
169171

170172
self.route_list.sort()
171173

tests/mock_tables/appl_db.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -648,8 +648,8 @@
648648
"speed": 100000
649649
},
650650
"ROUTE_TABLE:0.0.0.0/0": {
651-
"ifname": "Ethernet0,Ethernet4,Ethernet8,Ethernet12,Ethernet16,Ethernet20,Ethernet24,Ethernet28,Ethernet32,Ethernet36,Ethernet40,Ethernet44,Ethernet48,Ethernet52,Ethernet78",
652-
"nexthop": "10.0.0.1,10.0.0.3,10.0.0.5,10.0.0.7,10.0.0.9,10.0.0.11,10.0.0.13,10.0.0.15,10.0.0.17,10.0.0.19,10.0.0.21,10.0.0.23,10.0.0.25,10.0.0.27"
651+
"ifname": "Ethernet74,Ethernet0,Ethernet4,Ethernet8,Ethernet12,Ethernet16,Ethernet20,Ethernet24,Ethernet28,Ethernet32,Ethernet36,Ethernet40,Ethernet44,Ethernet48,Ethernet52,Ethernet78",
652+
"nexthop": "2001:0db8:85a3:0000:0000:8a2e:0370:7334,10.0.0.1,10.0.0.3,10.0.0.5,10.0.0.7,10.0.0.9,10.0.0.11,10.0.0.13,10.0.0.15,10.0.0.17,10.0.0.19,10.0.0.21,10.0.0.23,10.0.0.25,10.0.0.27"
653653
},
654654
"ROUTE_TABLE:10.1.0.32": {
655655
"nexthop": "",

tests/test_nexthop.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def test_getnextpdu(self):
6060
n = len(response.values)
6161
value0 = response.values[0]
6262
self.assertEqual(value0.type_, ValueType.IP_ADDRESS)
63+
self.assertEqual(type(ipaddress.ip_address(value0.data.string)), ipaddress.IPv4Address)
6364
self.assertEqual(str(value0.data), ipaddress.ip_address("10.0.0.1").packed.decode())
6465

6566
def test_getnextpdu_exactmatch(self):

tests/test_rfc1213.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,40 @@ def test_NextHopUpdater_route_no_next_hop(self):
4747

4848
self.assertTrue(len(updater.route_list) == 0)
4949

50+
@mock.patch('sonic_ax_impl.mibs.Namespace.dbs_keys', mock.MagicMock(return_value=(["ROUTE_TABLE:0.0.0.0/0"])))
51+
@mock.patch('sonic_ax_impl.mibs.Namespace.dbs_get_all', mock.MagicMock(return_value=({"nexthop": "2001:db8:1111:2222:3333:4444:5555:6666,2001:db8:3333:4444:5555:6666:7777:8888", "ifname": "Ethernet0,Ethernet4"})))
52+
def test_NextHopUpdater_route_ipv6_next_hop(self):
53+
updater = NextHopUpdater()
54+
55+
with mock.patch('sonic_ax_impl.mibs.logger.warning') as mocked_warning:
56+
updater.update_data()
57+
58+
# check warning
59+
expected = [
60+
mock.call("Route ROUTE_TABLE:0.0.0.0/0 has non-IPv4 nexthop: 2001:db8:1111:2222:3333:4444:5555:6666"),
61+
mock.call("Route ROUTE_TABLE:0.0.0.0/0 has non-IPv4 nexthop: 2001:db8:3333:4444:5555:6666:7777:8888")
62+
]
63+
mocked_warning.assert_has_calls(expected)
64+
65+
self.assertTrue(len(updater.route_list) == 0)
66+
self.assertTrue(len(updater.nexthop_map) == 0)
67+
68+
@mock.patch('sonic_ax_impl.mibs.Namespace.dbs_keys', mock.MagicMock(return_value=(["ROUTE_TABLE:0.0.0.0/0"])))
69+
@mock.patch('sonic_ax_impl.mibs.Namespace.dbs_get_all', mock.MagicMock(return_value=({"nexthop": "2001:db8:1111:2222:3333:4444:5555:6666,127.1.1.1", "ifname": "Ethernet0,Ethernet4"})))
70+
def test_NextHopUpdater_route_ipv4_ipv6_next_hop(self):
71+
updater = NextHopUpdater()
72+
73+
with mock.patch('sonic_ax_impl.mibs.logger.warning') as mocked_warning:
74+
updater.update_data()
75+
76+
# check warning
77+
expected = [
78+
mock.call("Route ROUTE_TABLE:0.0.0.0/0 has non-IPv4 nexthop: 2001:db8:1111:2222:3333:4444:5555:6666")
79+
]
80+
mocked_warning.assert_has_calls(expected)
81+
82+
self.assertTrue(len(updater.route_list) == 1)
83+
self.assertTrue(len(updater.nexthop_map) == 1)
5084

5185
class TestNextHopUpdaterRedisException(TestCase):
5286
def __init__(self, name):
@@ -104,4 +138,4 @@ def mock_get_sync_d_from_all_namespace(per_namespace_func, db_conn):
104138
updater.reinit_data()
105139

106140
# check re-init
107-
connect_namespace_dbs.assert_called()
141+
connect_namespace_dbs.assert_called()

0 commit comments

Comments
 (0)