|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import click |
| 4 | +import utilities_common.cli as clicommon |
| 5 | +import socket |
| 6 | + |
| 7 | +from swsscommon.swsscommon import SonicV2Connector |
| 8 | +from sonic_py_common import multi_asic |
| 9 | +from tabulate import tabulate |
| 10 | + |
| 11 | + |
| 12 | +class IcmpShow: |
| 13 | + def __init__(self, click): |
| 14 | + self.click = click |
| 15 | + namespaces = multi_asic.get_front_end_namespaces() |
| 16 | + self.asic_ids = [] |
| 17 | + self.per_npu_statedb = {} |
| 18 | + self.icmp_echo_table_keys = {} |
| 19 | + self.ctx = self.click.get_current_context() |
| 20 | + for namespace in namespaces: |
| 21 | + asic_id = multi_asic.get_asic_index_from_namespace(namespace) |
| 22 | + self.asic_ids.append(asic_id) |
| 23 | + self.per_npu_statedb[asic_id] = SonicV2Connector(use_unix_socket_path=False, namespace=namespace) |
| 24 | + try: |
| 25 | + self.per_npu_statedb[asic_id].connect(self.per_npu_statedb[asic_id].STATE_DB) |
| 26 | + self.icmp_echo_table_keys[asic_id] = sorted( |
| 27 | + self.per_npu_statedb[asic_id].keys(self.per_npu_statedb[asic_id].STATE_DB, |
| 28 | + 'ICMP_ECHO_SESSION_TABLE|*')) |
| 29 | + except (socket.error, IOError) as e: |
| 30 | + self.ctx.fail("Socket error in connecting with ICMP_ECHO_SESSION_TABLE: {}".format(str(e))) |
| 31 | + except (KeyError, ValueError) as e: |
| 32 | + self.ctx.fail("Error getting keys from ICMP_ECHO_SESSION_TABLE: {}".format(str(e))) |
| 33 | + |
| 34 | + def get_icmp_echo_entry(self, asic_id, key): |
| 35 | + """Show icmp echo session entry from state db.""" |
| 36 | + state_db = self.per_npu_statedb[asic_id] |
| 37 | + tbl_dict = state_db.get_all(state_db.STATE_DB, key) |
| 38 | + if tbl_dict: |
| 39 | + # Prepare data for tabulate |
| 40 | + fields = { |
| 41 | + "key": key.removeprefix("ICMP_ECHO_SESSION_TABLE|"), |
| 42 | + "state": None, |
| 43 | + "dst_ip": None, |
| 44 | + "tx_interval": None, |
| 45 | + "rx_interval": None, |
| 46 | + "hw_lookup": None, |
| 47 | + "session_cookie": None |
| 48 | + } |
| 49 | + for f in tbl_dict: |
| 50 | + if f in fields: |
| 51 | + fields[f] = tbl_dict[f] |
| 52 | + return [fields["key"], fields["dst_ip"], fields["tx_interval"], fields["rx_interval"], fields["hw_lookup"], |
| 53 | + fields["session_cookie"], fields["state"]] |
| 54 | + else: |
| 55 | + return None |
| 56 | + |
| 57 | + def show_icmp_sessions(self, key): |
| 58 | + table_data = [] |
| 59 | + for asic_id in self.asic_ids: |
| 60 | + keys = [] |
| 61 | + if key is None: |
| 62 | + keys = self.icmp_echo_table_keys[asic_id] |
| 63 | + else: |
| 64 | + keys.append("ICMP_ECHO_SESSION_TABLE|" + key.replace(":", "|")) |
| 65 | + |
| 66 | + for k in keys: |
| 67 | + entry = self.get_icmp_echo_entry(asic_id, k) |
| 68 | + if entry: |
| 69 | + table_data.append(entry) |
| 70 | + |
| 71 | + if table_data: |
| 72 | + headers = ["Key", "Dst IP", "Tx Interval", "Rx Interval", "HW lookup", "Cookie", "State"] |
| 73 | + click.echo(tabulate(table_data, headers=headers)) |
| 74 | + else: |
| 75 | + click.echo("Keys not found in ICMP_ECHO_SESSION_TABLE") |
| 76 | + |
| 77 | + def show_summary(self): |
| 78 | + total_sessions = 0 |
| 79 | + total_up = 0 |
| 80 | + total_rx = 0 |
| 81 | + for asic_id in self.asic_ids: |
| 82 | + keys = self.icmp_echo_table_keys[asic_id] |
| 83 | + |
| 84 | + for k in keys: |
| 85 | + if 'RX' in k: |
| 86 | + total_rx = total_rx + 1 |
| 87 | + entry = self.get_icmp_echo_entry(asic_id, k) |
| 88 | + total_sessions = total_sessions + 1 |
| 89 | + if entry and entry[6] == "Up": |
| 90 | + total_up = total_up + 1 |
| 91 | + |
| 92 | + self.click.echo("Total Sessions: {}".format(total_sessions)) |
| 93 | + self.click.echo("Up sessions: {}".format(total_up)) |
| 94 | + self.click.echo("RX sessions: {}".format(total_rx)) |
| 95 | + |
| 96 | + |
| 97 | +@click.group(cls=clicommon.AliasedGroup) |
| 98 | +def icmp(): |
| 99 | + """Show icmp-offload information""" |
| 100 | + pass |
| 101 | + |
| 102 | + |
| 103 | +@icmp.command() |
| 104 | +@click.argument('key', required=False) |
| 105 | +def sessions(key): |
| 106 | + s_icmp = IcmpShow(click) |
| 107 | + s_icmp.show_icmp_sessions(key) |
| 108 | + |
| 109 | + |
| 110 | +@icmp.command() |
| 111 | +def summary(): |
| 112 | + s_icmp = IcmpShow(click) |
| 113 | + s_icmp.show_summary() |
0 commit comments