|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +""" |
| 4 | + Script to show dataplane/FIB entries |
| 5 | +
|
| 6 | + usage: fibshow [-ip IPADDR] v |
| 7 | + optional arguments: |
| 8 | + -ip IPADDR, --ipaddr IPADDR |
| 9 | + dataplane/FIB entry for a specific address |
| 10 | +
|
| 11 | + Example of the output: |
| 12 | + admin@str~$ fibshow -4 |
| 13 | + No. Vrf Route Nexthop Ifname |
| 14 | + ----- ------- ------------------ --------------------------------------- ----------------------------------------------------------- |
| 15 | + 1 Red 192.181.8.0/25 10.0.0.57,10.0.0.59,10.0.0.61,10.0.0.63 PortChannel101,PortChannel102,PortChannel103,PortChannel104 |
| 16 | + 2 192.184.56.0/25 10.0.0.57,10.0.0.59,10.0.0.61,10.0.0.63 PortChannel101,PortChannel102,PortChannel103,PortChannel104 |
| 17 | + ... |
| 18 | + Total number of entries 19 |
| 19 | +
|
| 20 | + admin@str:~$ fibshow -6 -ip 20c0:b560:0:80:: |
| 21 | + No. Vrf Route Nexthop Ifname |
| 22 | + ----- ------- ------------------- ----------------------------------- ----------------------------------------------------------- |
| 23 | + 1 20c0:b560:0:80::/64 fc00::72,fc00::76,fc00::7a,fc00::7e PortChannel101,PortChannel102,PortChannel103,PortChannel104 |
| 24 | + Total number of entries 1 |
| 25 | +""" |
| 26 | +import argparse |
| 27 | +import sys |
| 28 | +import os |
| 29 | +import re |
| 30 | + |
| 31 | +# mock the redis for unit test purposes # |
| 32 | +try: # pragma: no cover |
| 33 | + if os.environ["UTILITIES_UNIT_TESTING"] == "1": |
| 34 | + modules_path = os.path.join(os.path.dirname(__file__), "..") |
| 35 | + test_path = os.path.join(modules_path, "tests") |
| 36 | + sys.path.insert(0, modules_path) |
| 37 | + sys.path.insert(0, test_path) |
| 38 | + import mock_tables.dbconnector |
| 39 | + mock_variants = { "1": 'appl_db'} |
| 40 | + mock_db_path = os.path.join(test_path, "fibshow_input") |
| 41 | + file_name = mock_variants[os.environ["FIBSHOW_MOCK"]] |
| 42 | + jsonfile_asic = os.path.join(mock_db_path, file_name) |
| 43 | + mock_tables.dbconnector.dedicated_dbs['APPL_DB'] = jsonfile_asic |
| 44 | +except KeyError: # pragma: no cover |
| 45 | + pass |
| 46 | + |
| 47 | +import ipaddress |
| 48 | + |
| 49 | +from swsscommon.swsscommon import SonicV2Connector |
| 50 | +from tabulate import tabulate |
| 51 | + |
| 52 | +""" |
| 53 | + Base class for v4 and v6 FIB entries. |
| 54 | +""" |
| 55 | + |
| 56 | + |
| 57 | +class FibBase(object): |
| 58 | + |
| 59 | + HEADER = ["No.", "Vrf", "Route", "Nexthop", "Ifname"] |
| 60 | + |
| 61 | + def __init__(self): |
| 62 | + super(FibBase, self).__init__() |
| 63 | + self.db = SonicV2Connector(host="127.0.0.1") |
| 64 | + self.fetch_fib_data() |
| 65 | + |
| 66 | + def fetch_fib_data(self): |
| 67 | + """ |
| 68 | + Fetch FIB entries from APPL_DB |
| 69 | + """ |
| 70 | + self.db.connect(self.db.APPL_DB) |
| 71 | + self.fib_entry_list = [] |
| 72 | + |
| 73 | + fib_str = self.db.keys(self.db.APPL_DB, "ROUTE_TABLE:*") |
| 74 | + if not fib_str: |
| 75 | + return |
| 76 | + |
| 77 | + for s in fib_str: |
| 78 | + fib_entry = s |
| 79 | + fib = fib_entry.split(":", 1)[-1] |
| 80 | + if not fib: |
| 81 | + continue |
| 82 | + |
| 83 | + ent = self.db.get_all(self.db.APPL_DB, s) |
| 84 | + if not ent: |
| 85 | + continue |
| 86 | + |
| 87 | + self.fib_entry_list.append((fib,) + (ent["nexthop"],) + (ent["ifname"],) ) |
| 88 | + self.fib_entry_list.sort(key=lambda x: x[0]) |
| 89 | + return |
| 90 | + |
| 91 | + def display(self, version, address): |
| 92 | + """ |
| 93 | + Display FIB entries from APPL_DB |
| 94 | + """ |
| 95 | + output = [] |
| 96 | + fdb_index = 1 |
| 97 | + for fib in self.fib_entry_list: |
| 98 | + prefix = fib[0] |
| 99 | + |
| 100 | + if 'VRF' in fib[0]: |
| 101 | + vrf = re.match(r"VRF-(.*)",fib[0].split(":")[0]).group(1) |
| 102 | + prefix = fib[0].split(":")[1] |
| 103 | + else: |
| 104 | + vrf = "" |
| 105 | + ip = ipaddress.ip_address(prefix.split("/")[0]) |
| 106 | + |
| 107 | + if address is not None: |
| 108 | + if fib[0] == address: |
| 109 | + if ip.version == 4 and version == "-4": |
| 110 | + output.append([fdb_index, vrf, prefix, fib[1], fib[2]]) |
| 111 | + fdb_index += 1 |
| 112 | + elif ip.version == 6 and version == "-6": |
| 113 | + output.append([fdb_index, vrf, prefix, fib[1], fib[2]]) |
| 114 | + fdb_index += 1 |
| 115 | + break |
| 116 | + else: |
| 117 | + continue |
| 118 | + else: |
| 119 | + if ip.version == 4 and version == "-4": |
| 120 | + output.append([fdb_index, vrf, prefix, fib[1], fib[2]]) |
| 121 | + fdb_index += 1 |
| 122 | + elif ip.version == 6 and version == "-6": |
| 123 | + output.append([fdb_index, vrf, prefix, fib[1], fib[2]]) |
| 124 | + fdb_index += 1 |
| 125 | + print(tabulate(output, self.HEADER)) |
| 126 | + print("Total number of entries {0}".format(len(output))) |
| 127 | + |
| 128 | +def main(): |
| 129 | + |
| 130 | + parser = argparse.ArgumentParser(description='Show dataplane/FIB entries', |
| 131 | + formatter_class=argparse.RawTextHelpFormatter) |
| 132 | + parser.add_argument('-ip', '--ipaddr', type=str, |
| 133 | + help='dataplane/FIB route for a specific address', default=None) |
| 134 | + parser.add_argument('v', help='IP Version -4 or -6') |
| 135 | + |
| 136 | + args = parser.parse_args() |
| 137 | + |
| 138 | + try: |
| 139 | + fib = FibBase() |
| 140 | + fib.display(args.v, args.ipaddr) |
| 141 | + except Exception as e: |
| 142 | + print(str(e)) |
| 143 | + sys.exit(1) |
| 144 | + |
| 145 | + |
| 146 | +if __name__ == "__main__": |
| 147 | + main() |
0 commit comments