Skip to content

Commit

Permalink
Merge pull request #947 from netenglabs/fix-sonic-routes
Browse files Browse the repository at this point in the history
Fix sonic routes version 4.2
  • Loading branch information
ddutt authored May 23, 2024
2 parents 3a96659 + 460eb1e commit a57d62c
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 5 deletions.
11 changes: 10 additions & 1 deletion suzieq/config/routes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,16 @@ apply:
copy: cumulus

sonic:
copy: cumulus
- version: '>= 4.2.0'
merge: False
command:
- command: ip vrf show
textfsm: textfsm_templates/sonic_vrf_show.tfsm
- command: ip route show table all
textfsm: textfsm_templates/sonic_routes_v42.tfsm
- version: all
command: ip route show table all
textfsm: textfsm_templates/linux_routes.tfsm

eos:
version: all
Expand Down
24 changes: 24 additions & 0 deletions suzieq/config/textfsm_templates/sonic_routes_v42.tfsm
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Value protocol (\w+)
Value Required prefix ([0-9./]*|[0-9a-f:/]*|default)
Value vrf_id (\d+)
Value source ([0-9./]*)
Value List nexthopIps ([0-9./]*|[0-9a-f:/]*)
Value List oifs (\S+)
Value List weights (\d+)
Value metric (\d+)
Value action (blackhole)

Start
^\S+ -> Continue.Record
^${action}\s+(?:nhid \d+\s+)?${prefix}\s+proto ${protocol}\s+metric ${metric}.*$$
^\s+nexthop via\s+${nexthopIps}\s+dev ${oifs}\s+weight ${weights}.*$$
^${prefix}\s+(?:nhid \d+\s+)?via\s+${nexthopIps}\s+dev ${oifs}\s+table ${vrf_id}\s+proto ${protocol}\s+metric ${metric}.*$$
^${prefix}\s+(?:nhid \d+\s+)?dev ${oifs}\s+table ${vrf_id}\s+proto ${protocol}.*src ${source}.*$$
^unreachable\s+(?:nhid \d+\s+)?${prefix}\s+table ${vrf_id}\s+metric ${metric}.*$$
^${prefix}\s+(?:nhid \d+\s+)?via\s+${nexthopIps}\s+dev ${oifs}\s+table ${vrf_id}
^${prefix}\s+(?:nhid \d+\s+)?proto ${protocol}\s+metric ${metric}.*$$
^${prefix}\s+(?:nhid \d+\s+)?proto ${protocol}\s+src ${source}\s+metric ${metric}.*$$
^${prefix}\s+(?:nhid \d+\s+)?via ${nexthopIps}\s+dev ${oifs}\s+proto ${protocol}\s+metric ${metric}.*$$
^${prefix}\s+(?:nhid \d+\s+)?dev ${oifs}\s+proto ${protocol}.*src ${source}.*$$
^${prefix}\s+(?:nhid \d+\s+)?via ${nexthopIps}\s+dev ${oifs}.*$$
^${prefix}\s+(?:nhid \d+\s+)?table ${vrf_id}\s+proto ${protocol}\s+metric ${metric}
5 changes: 5 additions & 0 deletions suzieq/config/textfsm_templates/sonic_vrf_show.tfsm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Value vrf (\S+)
Value table_id (\d+)

Start
^${vrf}\s+${table_id} -> Record
18 changes: 16 additions & 2 deletions suzieq/poller/worker/services/routes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
from typing import Dict, List
import numpy as np

from suzieq.poller.worker.services.service import Service
Expand Down Expand Up @@ -58,9 +59,18 @@ def _clean_eos_data(self, processed_data, _):

def _clean_linux_data(self, processed_data, _):
"""Clean Linux ip route data"""
drop_indices: List[int] = []
id_vrf_match: Dict[str, str] = {}

for entry in processed_data:
entry["vrf"] = entry["vrf"] or "default"
for i, entry in enumerate(processed_data):
if table_id := entry.get('table_id'):
id_vrf_match[table_id] = entry.get('vrf') or 'default'
drop_indices.append(i)
continue
if vrf_id := entry.get('vrf_id'):
entry["vrf"] = id_vrf_match.get(vrf_id) or 'default'
else:
entry["vrf"] = entry.get("vrf") or "default"
entry["metric"] = entry["metric"] or 20
entry['preference'] = entry['metric']
for ele in ["nexthopIps", "oifs"]:
Expand All @@ -87,6 +97,10 @@ def _clean_linux_data(self, processed_data, _):

entry['inHardware'] = True # Till the offload flag is here

if drop_indices:
processed_data = np.delete(
processed_data, drop_indices).tolist() # type: ignore

return processed_data

def _clean_cumulus_data(self, processed_data, raw_data):
Expand Down
4 changes: 2 additions & 2 deletions suzieq/poller/worker/services/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,8 @@ def _get_def_for_version(self, defn: List, version: str) -> Dict:
# There are various outputs that due to an old parsing bug
# return a node version of 0. Use 'all' for those
continue
opdict = {'>': operator.gt, '<': operator.lt,
'>=': operator.ge, '<=': operator.le,
opdict = {'>=': operator.ge, '<=': operator.le,
'>': operator.gt, '<': operator.lt,
'=': operator.eq, '!=': operator.ne}
op = operator.eq

Expand Down

0 comments on commit a57d62c

Please sign in to comment.