-
Notifications
You must be signed in to change notification settings - Fork 819
show command for icmp echo offload sessions #3889
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b4232e0
show command for icmpecho sessions
manamand2020 cfbf694
fix pre-commit
manamand2020 976bb65
Merge branch 'master' into icmpshow
manamand2020 37619c7
fix pre-commit
manamand2020 5d19c9b
Change summary to display for all asics together
manamand2020 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| #!/usr/bin/env python3 | ||
|
|
||
| import argparse | ||
| import time | ||
| import click | ||
| import utilities_common.cli as clicommon | ||
|
|
||
| from swsscommon.swsscommon import SonicV2Connector | ||
| from sonic_py_common import multi_asic | ||
| from tabulate import tabulate | ||
|
|
||
|
|
||
| class IcmpShow: | ||
| def __init__(self, click): | ||
| self.click = click | ||
| namespaces = multi_asic.get_front_end_namespaces() | ||
| self.asic_ids = [] | ||
| self.per_npu_statedb = {} | ||
| self.icmp_echo_table_keys = {} | ||
| for namespace in namespaces: | ||
| self.ctx = self.click.get_current_context() | ||
| asic_id = multi_asic.get_asic_index_from_namespace(namespace) | ||
| self.asic_ids.append(asic_id) | ||
| self.per_npu_statedb[asic_id] = SonicV2Connector(use_unix_socket_path=False, namespace=namespace) | ||
| self.per_npu_statedb[asic_id].connect(self.per_npu_statedb[asic_id].STATE_DB) | ||
| try: | ||
| self.icmp_echo_table_keys[asic_id] = sorted(self.per_npu_statedb[asic_id].keys(self.per_npu_statedb[asic_id].STATE_DB, 'ICMP_ECHO_SESSION_TABLE|*')) | ||
| except Exception as e: | ||
| self.ctx.fail("ICMP_ECHO_SESSION_TABLE does not have keys!") | ||
|
|
||
| def get_icmp_echo_entry(self, asic_id, key): | ||
| """Show icmp echo session entry from state db.""" | ||
| state_db = self.per_npu_statedb[asic_id] | ||
| tbl_dict = state_db.get_all(state_db.STATE_DB, key) | ||
| if tbl_dict: | ||
| # Prepare data for tabulate | ||
| fields = {"key": key.removeprefix("ICMP_ECHO_SESSION_TABLE|"), "state": None, "dst_ip": None, "tx_interval": None, "rx_interval": None, "hw_lookup": None, "session_cookie": None} | ||
| for f in tbl_dict: | ||
| if f in fields: | ||
| fields[f] = tbl_dict[f] | ||
| return [fields["key"], fields["dst_ip"], fields["tx_interval"], fields["rx_interval"], fields["hw_lookup"], fields["session_cookie"], fields["state"]] | ||
| else: | ||
| return None | ||
|
|
||
| def show_icmp_sessions(self, key): | ||
| table_data = [] | ||
| for asic_id in self.asic_ids: | ||
| keys = [] | ||
| if key == None: | ||
| try: | ||
| keys = self.icmp_echo_table_keys[asic_id] | ||
| except Exception as e: | ||
| ctx.fail("ICMP_ECHO_SESSION_TABLE does not exist!") | ||
| else: | ||
| keys.append("ICMP_ECHO_SESSION_TABLE|" + key.replace(":", "|")) | ||
|
|
||
| for k in keys: | ||
| entry = self.get_icmp_echo_entry(asic_id, k) | ||
| if entry: | ||
| table_data.append(entry) | ||
|
|
||
| if table_data: | ||
| headers = ["Key", "Dst IP", "Tx Interval", "Rx Interval", "HW lookup", "Cookie", "State"] | ||
| click.echo(tabulate(table_data, headers=headers)) | ||
| else: | ||
| click.echo("Keys not found in ICMP_ECHO_SESSION_TABLE") | ||
|
|
||
| def show_summary(self): | ||
| total_sessions = 0 | ||
| total_up = 0 | ||
| total_rx = 0 | ||
| for asic_id in self.asic_ids: | ||
| keys = [] | ||
| try: | ||
| keys = self.icmp_echo_table_keys[asic_id] | ||
| except Exception as e: | ||
| ctx.fail("ICMP_ECHO_SESSION_TABLE does not exist!") | ||
|
|
||
| for k in keys: | ||
| if 'RX' in k: | ||
| total_rx = total_rx + 1 | ||
| entry = self.get_icmp_echo_entry(asic_id, k) | ||
| total_sessions = total_sessions + 1 | ||
| if entry and entry[6] == "Up": | ||
| total_up = total_up + 1 | ||
|
|
||
| self.click.echo("Total Sessions: {}".format(total_sessions)) | ||
| self.click.echo("Up sessions: {}".format(total_up)) | ||
| self.click.echo("RX sessions: {}".format(total_rx)) | ||
|
|
||
| @click.group(cls=clicommon.AliasedGroup) | ||
| def icmp(): | ||
| """Show icmp-offload information""" | ||
| pass | ||
|
|
||
| @icmp.command() | ||
| @click.argument('key', required=False) | ||
| def sessions(key): | ||
| s_icmp = IcmpShow(click) | ||
| s_icmp.show_icmp_sessions(key) | ||
|
|
||
| @icmp.command() | ||
| def summary(): | ||
| s_icmp = IcmpShow(click) | ||
| s_icmp.show_summary() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import os | ||
| import traceback | ||
|
|
||
| import traceback | ||
|
|
||
| from click.testing import CliRunner | ||
| from utilities_common.db import Db | ||
|
|
||
| import show.main as show | ||
|
|
||
|
|
||
| tabular_session_status_output_expected = """\ | ||
| Key Dst IP Tx Interval Rx Interval HW lookup Cookie State | ||
| ------------------------------------- ------------ ------------- ------------- ----------- ---------- ------- | ||
| default|Ethernet0|0x4eb39592|RX 192.168.0.3 0 300 false 0x58767e7a Up | ||
| default|Ethernet128|0x23ffb930|NORMAL 192.168.0.31 100 300 false 0x58767e7a Down | ||
| default|Ethernet152|0x39e05375|NORMAL 192.168.0.37 100 300 false 0x58767e7a Up | ||
| default|Ethernet8|0x69f578f5|NORMAL 192.168.0.5 100 300 false 0x58767e7a Up | ||
| """ | ||
|
|
||
| session_summary_output_expected = """\ | ||
| Total Sessions: 4 | ||
| Up sessions: 3 | ||
| RX sessions: 1 | ||
| """ | ||
|
|
||
| tabular_session_key_status_output_expected = """\ | ||
| Key Dst IP Tx Interval Rx Interval HW lookup Cookie State | ||
| ------------------------------- ----------- ------------- ------------- ----------- ---------- ------- | ||
| default|Ethernet0|0x4eb39592|RX 192.168.0.3 0 300 false 0x58767e7a Up | ||
| """ | ||
|
|
||
| class TestIcmpSession(object): | ||
| @classmethod | ||
| def setup_class(cls): | ||
| os.environ['UTILITIES_UNIT_TESTING'] = "1" | ||
| print("SETUP") | ||
|
|
||
| def test_icmpecho_summary(self): | ||
| runner = CliRunner() | ||
| db = Db() | ||
|
|
||
| result = runner.invoke(show.cli.commands["icmp"].commands["summary"], obj=db) | ||
|
|
||
| assert result.exit_code == 0 | ||
| assert result.output == session_summary_output_expected | ||
|
|
||
| def test_icmpecho_sessions(self): | ||
| runner = CliRunner() | ||
| db = Db() | ||
| result = runner.invoke(show.cli.commands["icmp"].commands["sessions"], obj=db) | ||
| print(result.exit_code) | ||
| print(result.output) | ||
|
|
||
| assert result.exit_code == 0 | ||
| assert result.output == tabular_session_status_output_expected | ||
|
|
||
| def test_icmpecho_key_sessions(self): | ||
| runner = CliRunner() | ||
| db = Db() | ||
| result = runner.invoke(show.cli.commands["icmp"].commands["sessions"], "default|Ethernet0|0x4eb39592|RX", obj=db) | ||
| print(result.exit_code) | ||
| print(result.output) | ||
|
|
||
| assert result.exit_code == 0 | ||
| assert result.output == tabular_session_key_status_output_expected | ||
|
|
||
| @classmethod | ||
| def teardown_class(cls): | ||
| os.environ['UTILITIES_UNIT_TESTING'] = "0" | ||
| print("TEARDOWN") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.