Skip to content

Commit

Permalink
Merge pull request #1096 from chonty/ios-non-exact-route-lookup
Browse files Browse the repository at this point in the history
Allow non-exact route lookup in IOS
  • Loading branch information
mirceaulinic authored Aug 19, 2020
2 parents 630a515 + bb8aad4 commit 14749bd
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 6 deletions.
24 changes: 18 additions & 6 deletions napalm/ios/ios.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
RE_BGP_REMOTE_AS = re.compile(r"remote AS (" + ASN_REGEX + r")")
RE_BGP_AS_PATH = re.compile(r"^[ ]{2}([\d\(]([\d\) ]+)|Local)")

RE_RP_ROUTE = re.compile(r"Routing entry for (" + IP_ADDR_REGEX + r"\/\d+)")
RE_RP_FROM = re.compile(r"Known via \"([a-z]+)[ \"]")
RE_RP_VIA = re.compile(r"via (\S+)")
RE_RP_METRIC = re.compile(r"[ ]+Route metric is (\d+)")
Expand Down Expand Up @@ -2773,7 +2774,7 @@ def _get_bgp_route_attr(self, destination, vrf, next_hop, ip_version=4):

search_re_dict = {
"aspath": {
"re": r"[^|\\n][ ]{2}([\d\(\)]([\d\(\) ])*)",
"re": r"[^|\\n][ ]{2}([\d\(\)]([\d\(\) ])*|Local)",
"group": 1,
"default": "",
},
Expand Down Expand Up @@ -2947,8 +2948,11 @@ def get_route_to(self, destination="", protocol="", longer=False):
vrfs.append("default") # global VRF
ipnet_dest = IPNetwork(destination)
prefix = str(ipnet_dest.network)
netmask = str(ipnet_dest.netmask)
routes = {destination: []}
netmask = ""
routes = {}
if "/" in destination:
netmask = str(ipnet_dest.netmask)
routes = {destination: []}
commands = []
for _vrf in vrfs:
if _vrf == "default":
Expand All @@ -2969,6 +2973,14 @@ def get_route_to(self, destination="", protocol="", longer=False):
for (outitem, _vrf) in zip(output, vrfs): # for all VRFs
route_proto_regex = RE_RP_FROM.search(outitem)
if route_proto_regex:
route_match = destination
if netmask == "":
# Get the matching route for a non-exact lookup
route_match_regex = RE_RP_ROUTE.search(outitem)
if route_match_regex:
route_match = route_match_regex.group(1)
if route_match not in routes:
routes[route_match] = []
# routing protocol name (bgp, ospf, ...)
route_proto = route_proto_regex.group(1)
rdb = outitem.split("Routing Descriptor Blocks:")
Expand Down Expand Up @@ -3035,9 +3047,9 @@ def get_route_to(self, destination="", protocol="", longer=False):
destination, _vrf, nh, ip_version
)
nh_line_found = (
False # for next RT entry processing ...
)
routes[destination].append(route_entry)
False
) # for next RT entry processing ...
routes[route_match].append(route_entry)
return routes

def get_snmp_information(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"1.0.4.0/24": [
{
"protocol": "bgp",
"outgoing_interface": "",
"age": 98,
"current_active": true,
"routing_table": "CustB",
"last_active": true,
"protocol_attributes": {
"as_path": "Local",
"remote_address": "169.254.255.1",
"communities": [],
"local_preference": 100,
"local_as": 1,
"remote_as": 1
},
"next_hop": "169.254.255.1",
"selected_next_hop": true,
"inactive_reason": "",
"preference": 0
},
{
"protocol": "bgp",
"outgoing_interface": "",
"age": 62,
"current_active": true,
"routing_table": "default",
"last_active": true,
"protocol_attributes": {
"as_path": "3 3 3 3",
"remote_address": "200.200.200.1",
"communities": [],
"local_preference": 100,
"local_as": 1,
"remote_as": 3
},
"next_hop": "200.200.200.1",
"selected_next_hop": true,
"inactive_reason": "",
"preference": 0
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
BGP routing table entry for 1.0.4.0/24, version 2
Paths: (1 available, best #1, table default)
Advertised to update-groups:
2
Refresh Epoch 1
3 3 3 3
200.200.200.1 from 200.200.200.1 (1.1.1.1)
Origin IGP, metric 0, localpref 100, valid, external, best
rx pathid: 0, tx pathid: 0x0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BGP neighbor is 200.200.200.1, remote AS 3, local AS 2 no-prepend replace-as, external link
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
BGP routing table entry for 3:3:1.0.4.0/24, version 3
Paths: (1 available, best #1, table CustB)
Not advertised to any peer
Refresh Epoch 1
Local
169.254.255.1 (via vrf CustB) from 169.254.255.1 (1.1.1.1)
Origin IGP, metric 0, localpref 100, valid, internal, best
rx pathid: 0, tx pathid: 0x0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BGP neighbor is 169.254.255.1, vrf CustB, remote AS 1, internal link
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Routing Protocol is "bgp 1"
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Routing entry for 1.0.4.0/24
Known via "bgp 1", distance 20, metric 0
Tag 3, type external
Last update from 200.200.200.1 00:01:02 ago
Routing Descriptor Blocks:
* 200.200.200.1, from 200.200.200.1, 00:01:02 ago
Route metric is 0, traffic share count is 1
AS Hops 4
Route tag 3
MPLS label: none
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
% Network not in table
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

Routing Table: CustB
Routing entry for 1.0.4.0/24
Known via "bgp 1", distance 200, metric 0, type internal
Last update from 169.254.255.1 00:01:38 ago
Routing Descriptor Blocks:
* 169.254.255.1, from 169.254.255.1, 00:01:38 ago
Route metric is 0, traffic share count is 1
AS Hops 0
MPLS label: none
3 changes: 3 additions & 0 deletions test/ios/mocked_data/test_get_route_to/non_exact/show_vrf.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Name Default RD Protocols Interfaces
CustA 2:2 ipv4 Et0/1
CustB 3:3 ipv4 Et0/2

0 comments on commit 14749bd

Please sign in to comment.