From afceda49faef13e5a506afe2ce2ff3b3f3e8c2f3 Mon Sep 17 00:00:00 2001 From: Andriy Yurkiv <70649192+ayurkiv-nvda@users.noreply.github.com> Date: Wed, 22 Jun 2022 19:40:35 +0300 Subject: [PATCH] [201911] [Flex Counters] add CLI for PG drop packets counters (counterpoll, show/clear counters) (#2155) Signed-off-by: Andriy Yurkiv Should be merged after Azure/sonic-swss#2263 Appropriate PR in master:#1355, #1461, #1583 What I did Added new option for "counterpoll" utility Added new CLI commands to view and clear PG dropped packet statistics. Added the new CLI commands to the command reference guide. How I did it Need to merge PG drop functionality to 201911 How to verify it admin@arc-switch1041:~$ counterpoll pg-drop enable --> to enable the new counter admin@arc-switch1041:~$ counterpoll show --> check new INGRESS_PG_STAT_DROP counter status Check counters admin@arc-switch1041:~$ redis-cli -n 2 127.0.0.1:6379[2]> HGETALL COUNTERS:oid:0x1a000000000062 1) "SAI_INGRESS_PRIORITY_GROUP_STAT_XOFF_ROOM_WATERMARK_BYTES" 2) "0" 3) "SAI_INGRESS_PRIORITY_GROUP_STAT_SHARED_WATERMARK_BYTES" 4) "0" 5) "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" 6) "0" show priority-group drop counters Ingress PG dropped packets: Port PG0 PG1 PG2 PG3 PG4 PG5 PG6 PG7 --------- ----- ----- ----- ----- ----- ----- ----- ----- Ethernet0 800 801 802 803 804 805 806 807 Ethernet4 400 401 402 403 404 405 406 407 Ethernet8 100 101 102 103 104 105 106 107 ... sonic-clear priority-group drop counters Cleared PG drop counters --- clear/main.py | 14 + config/main.py | 5 + counterpoll/main.py | 43 +++ doc/Command-Reference.md | 19 +- scripts/pg-drop | 262 ++++++++++++++++++ setup.py | 2 + show/main.py | 11 + sonic-utilities-tests/counterpoll_test.py | 35 +++ .../mock_tables/config_db.json | 4 + .../mock_tables/counters_db.json | 150 ++++++++++ .../mock_tables/pgdrop_input/config_db.json | 27 ++ sonic-utilities-tests/pgdropstat_test.py | 98 +++++++ 12 files changed, 668 insertions(+), 2 deletions(-) create mode 100755 scripts/pg-drop create mode 100644 sonic-utilities-tests/mock_tables/pgdrop_input/config_db.json create mode 100644 sonic-utilities-tests/pgdropstat_test.py diff --git a/clear/main.py b/clear/main.py index 9798b7b5a5..1cff046184 100755 --- a/clear/main.py +++ b/clear/main.py @@ -227,6 +227,20 @@ def clear_wm_pg_shared(): command = 'watermarkstat -c -t pg_shared' run_command(command) +@priority_group.group() +def drop(): + """Clear priority-group dropped packets stats""" + pass + +@drop.command('counters') +def clear_pg_counters(): + """Clear priority-group dropped packets counter """ + + if os.geteuid() != 0 and os.environ.get("UTILITIES_UNIT_TESTING", "0") != "2": + exit("Root privileges are required for this operation") + command = 'pg-drop -c clear' + run_command(command) + @priority_group.group(name='persistent-watermark') def persistent_watermark(): """Clear queue persistent WM. One does not simply clear WM, root is required""" diff --git a/config/main.py b/config/main.py index 4b301d9115..e8940d7be0 100755 --- a/config/main.py +++ b/config/main.py @@ -824,6 +824,11 @@ def reload(filename, yes, load_sysinfo, no_service_restart): if multi_asic.is_multi_asic(): num_cfg_file += num_asic + # Remove cached PG drop counters data + dropstat_dir_prefix = '/tmp/dropstat' + command = "rm -rf {}-*".format(dropstat_dir_prefix) + run_command(command, display_cmd=True) + # If the user give the filename[s], extract the file names. if filename is not None: cfg_files = filename.split(',') diff --git a/counterpoll/main.py b/counterpoll/main.py index d0e410369c..aecb00e764 100644 --- a/counterpoll/main.py +++ b/counterpoll/main.py @@ -6,6 +6,7 @@ BUFFER_POOL_WATERMARK = "BUFFER_POOL_WATERMARK" PORT_BUFFER_DROP = "PORT_BUFFER_DROP" +PG_DROP = "PG_DROP" DISABLE = "disable" ENABLE = "enable" DEFLT_60_SEC= "default (60000)" @@ -124,6 +125,45 @@ def disable(): port_info['FLEX_COUNTER_STATUS'] = DISABLE configdb.mod_entry("FLEX_COUNTER_TABLE", PORT_BUFFER_DROP, port_info) +# Ingress PG drop packet stat +@cli.group() +@click.pass_context +def pg_drop(ctx): + """ Ingress PG drop counter commands """ + ctx.obj = swsssdk.ConfigDBConnector() + ctx.obj.connect() + +@pg_drop.command() +@click.argument('poll_interval', type=click.IntRange(1000, 30000)) +@click.pass_context +def interval(ctx, poll_interval): + """ + Set pg_drop packets counter query interval + interval is between 1s and 30s. + """ + + port_info = {} + port_info['POLL_INTERVAL'] = poll_interval + ctx.obj.mod_entry("FLEX_COUNTER_TABLE", PG_DROP, port_info) + +@pg_drop.command() +@click.pass_context +def enable(ctx): + """ Enable pg_drop counter query """ + + port_info = {} + port_info['FLEX_COUNTER_STATUS'] = ENABLE + ctx.obj.mod_entry("FLEX_COUNTER_TABLE", PG_DROP, port_info) + +@pg_drop.command() +@click.pass_context +def disable(ctx): + """ Disable pg_drop counter query """ + + port_info = {} + port_info['FLEX_COUNTER_STATUS'] = DISABLE + ctx.obj.mod_entry("FLEX_COUNTER_TABLE", PG_DROP, port_info) + # RIF counter commands @cli.group() def rif(): @@ -213,6 +253,7 @@ def show(): rif_info = configdb.get_entry('FLEX_COUNTER_TABLE', 'RIF') queue_wm_info = configdb.get_entry('FLEX_COUNTER_TABLE', 'QUEUE_WATERMARK') pg_wm_info = configdb.get_entry('FLEX_COUNTER_TABLE', 'PG_WATERMARK') + pg_drop_info = configdb.get_entry('FLEX_COUNTER_TABLE', PG_DROP) buffer_pool_wm_info = configdb.get_entry('FLEX_COUNTER_TABLE', BUFFER_POOL_WATERMARK) header = ("Type", "Interval (in ms)", "Status") @@ -229,6 +270,8 @@ def show(): data.append(["QUEUE_WATERMARK_STAT", queue_wm_info.get("POLL_INTERVAL", DEFLT_10_SEC), queue_wm_info.get("FLEX_COUNTER_STATUS", DISABLE)]) if pg_wm_info: data.append(["PG_WATERMARK_STAT", pg_wm_info.get("POLL_INTERVAL", DEFLT_10_SEC), pg_wm_info.get("FLEX_COUNTER_STATUS", DISABLE)]) + if pg_drop_info: + data.append(['PG_DROP_STAT', pg_drop_info.get("POLL_INTERVAL", DEFLT_10_SEC), pg_drop_info.get("FLEX_COUNTER_STATUS", DISABLE)]) if buffer_pool_wm_info: data.append(["BUFFER_POOL_WATERMARK_STAT", buffer_pool_wm_info.get("POLL_INTERVAL", DEFLT_10_SEC), buffer_pool_wm_info.get("FLEX_COUNTER_STATUS", DISABLE)]) diff --git a/doc/Command-Reference.md b/doc/Command-Reference.md index 5ae17e29d8..81ef59bbb0 100644 --- a/doc/Command-Reference.md +++ b/doc/Command-Reference.md @@ -4465,11 +4465,14 @@ This command displays the user watermark for the queues (Egress shared pool occu **show priority-group** -This command displays the user watermark or persistent-watermark for the Ingress "headroom" or "shared pool occupancy" per priority-group for all ports +This command displays: +1) The user watermark or persistent-watermark for the Ingress "headroom" or "shared pool occupancy" per priority-group for all ports. +2) Dropped packets per priority-group for all ports - Usage: ``` show priority-group (watermark | persistent-watermark) (headroom | shared) + show priority-group drop counters ``` - Example: @@ -4499,6 +4502,18 @@ This command displays the user watermark or persistent-watermark for the Ingress admin@sonic:~$ show priority-group persistent-watermark headroom ``` +- Example (Ingress dropped packets per PG): + ``` + admin@sonic:~$ show priority-group drop counters + Ingress PG dropped packets: + Port PG0 PG1 PG2 PG3 PG4 PG5 PG6 PG7 + ----------- ----- ----- ----- ----- ----- ----- ----- ----- + Ethernet0 0 0 0 0 0 0 0 0 + Ethernet4 0 0 0 0 0 0 0 0 + Ethernet8 0 0 0 0 0 0 0 0 + Ethernet12 0 0 0 0 0 0 0 0 + ``` + In addition to user watermark("show queue|priority-group watermark ..."), a persistent watermark is available. It hold values independently of user watermark. This way user can use "user watermark" for debugging, clear it, etc, but the "persistent watermark" will not be affected. @@ -4528,7 +4543,7 @@ This command displays the user persistet-watermark for the queues (Egress shared admin@sonic:~$ show queue persistent-watermark multicast ``` -- NOTE: Both "user watermark" and "persistent watermark" can be cleared by user: +- NOTE: "user watermark", "persistent watermark" and "ingress dropped packets" can be cleared by user: ``` root@sonic:~# sonic-clear queue persistent-watermark unicast diff --git a/scripts/pg-drop b/scripts/pg-drop new file mode 100755 index 0000000000..477d883db6 --- /dev/null +++ b/scripts/pg-drop @@ -0,0 +1,262 @@ +#!/usr/bin/python + +##################################################################### +# +# pg-drop is a tool for show/clear ingress pg dropped packet stats. +# +##################################################################### +import pickle +import argparse +import os +import sys +from collections import OrderedDict + +from natsort import natsorted +from tabulate import tabulate + +# mock the redis for unit test purposes # +try: + if os.environ["UTILITIES_UNIT_TESTING"] == "2": + modules_path = os.path.join(os.path.dirname(__file__), "..") + tests_path = os.path.join(modules_path, "sonic-utilities-tests") + sys.path.insert(0, modules_path) + sys.path.insert(0, tests_path) + import mock_tables.dbconnector + +except KeyError: + pass + +from swsssdk import SonicV2Connector,ConfigDBConnector + +STATUS_NA = 'N/A' + +COUNTER_TABLE_PREFIX = "COUNTERS:" + +COUNTERS_PORT_NAME_MAP = "COUNTERS_PORT_NAME_MAP" +COUNTERS_PG_NAME_MAP = "COUNTERS_PG_NAME_MAP" +COUNTERS_PG_PORT_MAP = "COUNTERS_PG_PORT_MAP" +COUNTERS_PG_INDEX_MAP = "COUNTERS_PG_INDEX_MAP" + +def get_dropstat_dir(): + dropstat_dir_prefix = '/tmp/dropstat' + return "{}-{}/".format(dropstat_dir_prefix, os.getuid()) + +class PgDropStat(object): + + def __init__(self): + self.counters_db = SonicV2Connector(host='127.0.0.1') + self.counters_db.connect(self.counters_db.COUNTERS_DB) + + self.configdb = ConfigDBConnector() + self.configdb.connect() + + dropstat_dir = get_dropstat_dir() + self.port_drop_stats_file = os.path.join(dropstat_dir, 'pg_drop_stats') + + def get_port_id(oid): + """ + Get port ID using object ID + """ + port_id = self.counters_db.get(self.counters_db.COUNTERS_DB, COUNTERS_PG_PORT_MAP, oid) + if port_id is None: + print "Port is not available for oid '{}'".format(oid), sys.stderr + sys.exit(1) + return port_id + + # Get all ports + self.counter_port_name_map = self.counters_db.get_all(self.counters_db.COUNTERS_DB, COUNTERS_PORT_NAME_MAP) + if self.counter_port_name_map is None: + print "COUNTERS_PORT_NAME_MAP is empty!", sys.stderr + sys.exit(1) + + self.port_pg_map = {} + self.port_name_map = {} + + for port in self.counter_port_name_map: + self.port_pg_map[port] = {} + self.port_name_map[self.counter_port_name_map[port]] = port + + # Get PGs for each port + counter_pg_name_map = self.counters_db.get_all(self.counters_db.COUNTERS_DB, COUNTERS_PG_NAME_MAP) + if counter_pg_name_map is None: + print "COUNTERS_PG_NAME_MAP is empty!", sys.stderr + sys.exit(1) + + for pg in counter_pg_name_map: + port = self.port_name_map[get_port_id(counter_pg_name_map[pg])] + self.port_pg_map[port][pg] = counter_pg_name_map[pg] + + self.pg_drop_types = { + "pg_drop" : {"message" : "Ingress PG dropped packets:", + "obj_map" : self.port_pg_map, + "idx_func": self.get_pg_index, + "counter_name" : "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS", + "header_prefix": "PG"}, + } + + def get_pg_index(self, oid): + """ + return PG index (0-7) + + oid - object ID for entry in redis + """ + pg_index = self.counters_db.get(self.counters_db.COUNTERS_DB, COUNTERS_PG_INDEX_MAP, oid) + if pg_index is None: + print "Priority group index is not available for oid '{}'".format(table_id), sys.stderr + sys.exit(1) + return pg_index + + def build_header(self, pg_drop_type): + """ + Construct header for table with PG counters + """ + if pg_drop_type is None: + print "Header info is not available!", sys.stderr + sys.exit(1) + + self.header_list = ['Port'] + header_map = pg_drop_type["obj_map"] + single_key = list(header_map.keys())[0] + header_len = len(header_map[single_key]) + min_idx = sys.maxsize + + for name, counter_oid in header_map[single_key].items(): + curr_idx = int(pg_drop_type["idx_func"](counter_oid)) + min_idx = min(min_idx, curr_idx) + + self.min_idx = min_idx + self.header_list += ["{}{}".format(pg_drop_type["header_prefix"], idx) for idx in range(self.min_idx, self.min_idx + header_len)] + + def get_counters(self, table_prefix, port_obj, idx_func, counter_name): + """ + Get the counters of a specific table. + """ + port_drop_ckpt = {} + # Grab the latest clear checkpoint, if it exists + if os.path.isfile(self.port_drop_stats_file): + port_drop_ckpt = pickle.load(open(self.port_drop_stats_file, 'rb')) + + # Header list contains the port name followed by the PGs. Fields is used to populate the pg values + fields = ["0"]* (len(self.header_list) - 1) + + for name, obj_id in port_obj.items(): + full_table_id = table_prefix + obj_id + old_collected_data = port_drop_ckpt.get(name,{})[full_table_id] if len(port_drop_ckpt) > 0 else 0 + idx = int(idx_func(obj_id)) + pos = idx - self.min_idx + counter_data = self.counters_db.get(self.counters_db.COUNTERS_DB, full_table_id, counter_name) + if counter_data is None: + fields[pos] = STATUS_NA + elif fields[pos] != STATUS_NA: + fields[pos] = str(int(counter_data) - old_collected_data) + return fields + + def print_all_stat(self, table_prefix, key): + """ + Print table that show stats per PG + """ + table = [] + type = self.pg_drop_types[key] + self.build_header(type) + # Get stat for each port + for port in natsorted(self.counter_port_name_map): + row_data = list() + data = self.get_counters(table_prefix, type["obj_map"][port], type["idx_func"], type["counter_name"]) + row_data.append(port) + row_data.extend(data) + table.append(tuple(row_data)) + + print type["message"] + print tabulate(table, self.header_list, tablefmt='simple', stralign='right') + + def get_counts(self, counters, oid): + """ + Get the PG drop counts for an individual counter. + """ + counts = {} + table_id = COUNTER_TABLE_PREFIX + oid + for counter in counters: + counter_data = self.counters_db.get(self.counters_db.COUNTERS_DB, table_id, counter) + if counter_data is None: + counts[table_id] = 0 + else: + counts[table_id] = int(counter_data) + return counts + + def get_counts_table(self, counters, object_table): + """ + Returns a dictionary containing a mapping from an object (like a port) + to its PG drop counts. Counts are contained in a dictionary that maps + counter oid to its counts. + """ + counter_object_name_map = self.counters_db.get_all(self.counters_db.COUNTERS_DB, object_table) + current_stat_dict = OrderedDict() + + if counter_object_name_map is None: + return current_stat_dict + + for obj in natsorted(counter_object_name_map): + current_stat_dict[obj] = self.get_counts(counters, counter_object_name_map[obj]) + return current_stat_dict + + def clear_drop_counts(self): + """ + Clears the current PG drop counter. + """ + + counter_pg_drop_array = [ "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS"] + try: + pickle.dump(self.get_counts_table( + counter_pg_drop_array, + COUNTERS_PG_NAME_MAP), + open(self.port_drop_stats_file, 'wb+')) + except IOError as e: + print e + sys.exit(e.errno) + print "Cleared PG drop counter" + + def check_if_stats_enabled(self): + pg_drop_info = self.configdb.get_entry('FLEX_COUNTER_TABLE', 'PG_DROP') + if pg_drop_info: + status = pg_drop_info.get("FLEX_COUNTER_STATUS", 'disable') + if status == "disable": + print "Warning: PG counters are disabled. Use 'counterpoll pg-drop enable' to enable polling" + sys.exit(0) + +def main(): + parser = argparse.ArgumentParser(description='Display PG drop counter', + formatter_class=argparse.RawTextHelpFormatter, + epilog=""" +Examples: +pg-drop -c show +pg-drop -c clear +""") + + parser.add_argument('-c', '--command', type=str, help='Desired action to perform') + + args = parser.parse_args() + command = args.command + + dropstat_dir = get_dropstat_dir() + # Create the directory to hold clear results + if not os.path.exists(dropstat_dir): + try: + os.makedirs(dropstat_dir) + except IOError as e: + print e + sys.exit(e.errno) + + pgdropstat = PgDropStat() + + if command == 'clear': + pgdropstat.clear_drop_counts() + elif command == 'show': + pgdropstat.check_if_stats_enabled() + pgdropstat.print_all_stat(COUNTER_TABLE_PREFIX, "pg_drop" ) + else: + print "Command not recognized" + sys.exit(0) + + +if __name__ == "__main__": + main() diff --git a/setup.py b/setup.py index a7c6d623b7..5929c7d8ca 100644 --- a/setup.py +++ b/setup.py @@ -65,6 +65,7 @@ 'mock_tables/asic0/*', 'mock_tables/asic1/*', 'mock_tables/asic2/*', + 'mock_tables/pgdrop_input/*', 'bgp_commands_input/*', 'filter_fdb_input/*', 'pfcwd_input/*', @@ -109,6 +110,7 @@ 'scripts/nbrshow', 'scripts/neighbor_advertiser', 'scripts/pcmping', + 'scripts/pg-drop', 'scripts/port2alias', 'scripts/portconfig', 'scripts/portstat', diff --git a/show/main.py b/show/main.py index 50233f3d0c..8bd39fa1f6 100755 --- a/show/main.py +++ b/show/main.py @@ -1109,6 +1109,17 @@ def wm_pg_shared(): command = 'watermarkstat -t pg_shared' run_command(command) +@priority_group.group() +def drop(): + """Show priority-group""" + pass + +@drop.command('counters') +def pg_drop_counters(): + """Show dropped packets for priority-group""" + command = 'pg-drop -c show' + run_command(command) + @priority_group.group(name='persistent-watermark') def persistent_watermark(): """Show priority-group persistent WM""" diff --git a/sonic-utilities-tests/counterpoll_test.py b/sonic-utilities-tests/counterpoll_test.py index b2c806b95c..2cd4c42ec0 100644 --- a/sonic-utilities-tests/counterpoll_test.py +++ b/sonic-utilities-tests/counterpoll_test.py @@ -5,6 +5,7 @@ import click import swsssdk from click.testing import CliRunner +from utilities_common.db import Db test_path = os.path.dirname(os.path.abspath(__file__)) modules_path = os.path.dirname(test_path) @@ -22,8 +23,10 @@ PORT_BUFFER_DROP 60000 enable QUEUE_WATERMARK_STAT 10000 enable PG_WATERMARK_STAT 10000 enable +PG_DROP_STAT 10000 enable """ + class TestCounterpoll(object): @classmethod def setup_class(cls): @@ -51,6 +54,38 @@ def test_port_buffer_drop_interval_too_short(self): assert result.exit_code == 2 assert expected in result.output + def test_pg_drop_interval_too_long(self): + runner = CliRunner() + result = runner.invoke(counterpoll.cli.commands["pg-drop"].commands["interval"], ["50000"]) + print(result.output) + expected = "Invalid value for 'POLL_INTERVAL': 50000 is not in the valid range of 1000 to 30000." + assert result.exit_code == 2 + assert expected in result.output + + @pytest.mark.parametrize("status", ["disable", "enable"]) + def test_update_pg_drop_status(self, status): + runner = CliRunner() + db = Db() + + result = runner.invoke(counterpoll.cli.commands["pg-drop"].commands[status], [], obj=db.cfgdb) + print(result.exit_code, result.output) + assert result.exit_code == 0 + + table = db.cfgdb.get_table('FLEX_COUNTER_TABLE') + assert status == table["PG_DROP"]["FLEX_COUNTER_STATUS"] + + def test_update_pg_drop_interval(self): + runner = CliRunner() + db = Db() + test_interval = "20000" + + result = runner.invoke(counterpoll.cli.commands["pg-drop"].commands["interval"], [test_interval], obj=db.cfgdb) + print(result.exit_code, result.output) + assert result.exit_code == 0 + + table = db.cfgdb.get_table('FLEX_COUNTER_TABLE') + assert test_interval == table["PG_DROP"]["POLL_INTERVAL"] + @classmethod def teardown_class(cls): print("TEARDOWN") diff --git a/sonic-utilities-tests/mock_tables/config_db.json b/sonic-utilities-tests/mock_tables/config_db.json index 0807fe0976..2b1472eb75 100644 --- a/sonic-utilities-tests/mock_tables/config_db.json +++ b/sonic-utilities-tests/mock_tables/config_db.json @@ -752,6 +752,10 @@ "POLL_INTERVAL": "10000", "FLEX_COUNTER_STATUS": "enable" }, + "FLEX_COUNTER_TABLE|PG_DROP": { + "POLL_INTERVAL": "10000", + "FLEX_COUNTER_STATUS": "enable" + }, "PFC_WD|Ethernet0": { "action": "drop", "detection_time": "600", diff --git a/sonic-utilities-tests/mock_tables/counters_db.json b/sonic-utilities-tests/mock_tables/counters_db.json index 4790a23c40..2cc042431f 100644 --- a/sonic-utilities-tests/mock_tables/counters_db.json +++ b/sonic-utilities-tests/mock_tables/counters_db.json @@ -203,6 +203,78 @@ "SAI_SWITCH_STAT_OUT_DROP_REASON_RANGE_BASE": "1000", "SAI_SWITCH_STAT_OUT_CONFIGURED_DROP_REASONS_1_DROPPED_PKTS": "0" }, + "COUNTERS:oid:0x1a00000000034f": { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "800" + }, + "COUNTERS:oid:0x1a000000000350" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "801" + }, + "COUNTERS:oid:0x1a000000000351" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "802" + }, + "COUNTERS:oid:0x1a000000000352" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "803" + }, + "COUNTERS:oid:0x1a000000000353" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "804" + }, + "COUNTERS:oid:0x1a000000000354" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "805" + }, + "COUNTERS:oid:0x1a000000000355" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "806" + }, + "COUNTERS:oid:0x1a000000000356" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "807" + }, + "COUNTERS:oid:0x1a000000000377" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "400" + }, + "COUNTERS:oid:0x1a000000000378" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "401" + }, + "COUNTERS:oid:0x1a000000000379" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "402" + }, + "COUNTERS:oid:0x1a00000000037a" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "403" + }, + "COUNTERS:oid:0x1a00000000037b" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "404" + }, + "COUNTERS:oid:0x1a00000000037c" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "405" + }, + "COUNTERS:oid:0x1a00000000037d" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "406" + }, + "COUNTERS:oid:0x1a00000000037e" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "407" + }, + "COUNTERS:oid:0x1a00000000039f" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "100" + }, + "COUNTERS:oid:0x1a0000000003a0" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "101" + }, + "COUNTERS:oid:0x1a0000000003a1" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "102" + }, + "COUNTERS:oid:0x1a0000000003a2" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "103" + }, + "COUNTERS:oid:0x1a0000000003a3" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "104" + }, + "COUNTERS:oid:0x1a0000000003a4" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "105" + }, + "COUNTERS:oid:0x1a0000000003a5" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "106" + }, + "COUNTERS:oid:0x1a0000000003a6" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "107" + }, "COUNTERS_PORT_NAME_MAP": { "Ethernet0": "oid:0x1000000000002", "Ethernet4": "oid:0x1000000000004", @@ -213,6 +285,84 @@ "Ethernet4:3": "oid:0x15000000000383", "Ethernet8:4": "oid:0x150000000003ac" }, + "COUNTERS_PG_NAME_MAP": { + "Ethernet0:0": "oid:0x1a00000000034f", + "Ethernet0:1": "oid:0x1a000000000350", + "Ethernet0:2": "oid:0x1a000000000351", + "Ethernet0:3": "oid:0x1a000000000352", + "Ethernet0:4": "oid:0x1a000000000353", + "Ethernet0:5": "oid:0x1a000000000354", + "Ethernet0:6": "oid:0x1a000000000355", + "Ethernet0:7": "oid:0x1a000000000356", + "Ethernet4:0": "oid:0x1a000000000377", + "Ethernet4:1": "oid:0x1a000000000378", + "Ethernet4:2": "oid:0x1a000000000379", + "Ethernet4:3": "oid:0x1a00000000037a", + "Ethernet4:4": "oid:0x1a00000000037b", + "Ethernet4:5": "oid:0x1a00000000037c", + "Ethernet4:6": "oid:0x1a00000000037d", + "Ethernet4:7": "oid:0x1a00000000037e", + "Ethernet8:0": "oid:0x1a00000000039f", + "Ethernet8:1": "oid:0x1a0000000003a0", + "Ethernet8:2": "oid:0x1a0000000003a1", + "Ethernet8:3": "oid:0x1a0000000003a2", + "Ethernet8:4": "oid:0x1a0000000003a3", + "Ethernet8:5": "oid:0x1a0000000003a4", + "Ethernet8:6": "oid:0x1a0000000003a5", + "Ethernet8:7": "oid:0x1a0000000003a6" + }, + "COUNTERS_PG_PORT_MAP": { + "oid:0x1a00000000034f": "oid:0x1000000000002", + "oid:0x1a000000000350": "oid:0x1000000000002", + "oid:0x1a000000000351": "oid:0x1000000000002", + "oid:0x1a000000000352": "oid:0x1000000000002", + "oid:0x1a000000000353": "oid:0x1000000000002", + "oid:0x1a000000000354": "oid:0x1000000000002", + "oid:0x1a000000000355": "oid:0x1000000000002", + "oid:0x1a000000000356": "oid:0x1000000000002", + "oid:0x1a000000000377": "oid:0x1000000000004", + "oid:0x1a000000000378": "oid:0x1000000000004", + "oid:0x1a000000000379": "oid:0x1000000000004", + "oid:0x1a00000000037a": "oid:0x1000000000004", + "oid:0x1a00000000037b": "oid:0x1000000000004", + "oid:0x1a00000000037c": "oid:0x1000000000004", + "oid:0x1a00000000037d": "oid:0x1000000000004", + "oid:0x1a00000000037e": "oid:0x1000000000004", + "oid:0x1a00000000039f": "oid:0x1000000000006", + "oid:0x1a0000000003a0": "oid:0x1000000000006", + "oid:0x1a0000000003a1": "oid:0x1000000000006", + "oid:0x1a0000000003a2": "oid:0x1000000000006", + "oid:0x1a0000000003a3": "oid:0x1000000000006", + "oid:0x1a0000000003a4": "oid:0x1000000000006", + "oid:0x1a0000000003a5": "oid:0x1000000000006", + "oid:0x1a0000000003a6": "oid:0x1000000000006" + }, + "COUNTERS_PG_INDEX_MAP": { + "oid:0x1a00000000034f": "0", + "oid:0x1a000000000350": "1", + "oid:0x1a000000000351": "2", + "oid:0x1a000000000352": "3", + "oid:0x1a000000000353": "4", + "oid:0x1a000000000354": "5", + "oid:0x1a000000000355": "6", + "oid:0x1a000000000356": "7", + "oid:0x1a000000000377": "0", + "oid:0x1a000000000378": "1", + "oid:0x1a000000000379": "2", + "oid:0x1a00000000037a": "3", + "oid:0x1a00000000037b": "4", + "oid:0x1a00000000037c": "5", + "oid:0x1a00000000037d": "6", + "oid:0x1a00000000037e": "7", + "oid:0x1a00000000039f": "0", + "oid:0x1a0000000003a0": "1", + "oid:0x1a0000000003a1": "2", + "oid:0x1a0000000003a2": "3", + "oid:0x1a0000000003a3": "4", + "oid:0x1a0000000003a4": "5", + "oid:0x1a0000000003a5": "6", + "oid:0x1a0000000003a6": "7" + }, "COUNTERS_DEBUG_NAME_PORT_STAT_MAP": { "DEBUG_0": "SAI_PORT_STAT_IN_DROP_REASON_RANGE_BASE", "DEBUG_2": "SAI_PORT_STAT_OUT_CONFIGURED_DROP_REASONS_1_DROPPED_PKTS" diff --git a/sonic-utilities-tests/mock_tables/pgdrop_input/config_db.json b/sonic-utilities-tests/mock_tables/pgdrop_input/config_db.json new file mode 100644 index 0000000000..fe9cc977b2 --- /dev/null +++ b/sonic-utilities-tests/mock_tables/pgdrop_input/config_db.json @@ -0,0 +1,27 @@ +{ + "FLEX_COUNTER_TABLE|QUEUE": { + "POLL_INTERVAL": "10000", + "FLEX_COUNTER_STATUS": "enable" + }, + "FLEX_COUNTER_TABLE|PORT": { + "POLL_INTERVAL": "1000", + "FLEX_COUNTER_STATUS": "enable" + }, + "FLEX_COUNTER_TABLE|PORT_BUFFER_DROP": { + "POLL_INTERVAL": "60000", + "FLEX_COUNTER_STATUS": "enable" + }, + "FLEX_COUNTER_TABLE|QUEUE_WATERMARK": { + "POLL_INTERVAL": "10000", + "FLEX_COUNTER_STATUS": "enable" + }, + "FLEX_COUNTER_TABLE|PG_WATERMARK": { + "POLL_INTERVAL": "10000", + "FLEX_COUNTER_STATUS": "enable" + }, + "FLEX_COUNTER_TABLE|PG_DROP": { + "POLL_INTERVAL": "10000", + "FLEX_COUNTER_STATUS": "disable" + } +} + diff --git a/sonic-utilities-tests/pgdropstat_test.py b/sonic-utilities-tests/pgdropstat_test.py new file mode 100644 index 0000000000..3d8259813f --- /dev/null +++ b/sonic-utilities-tests/pgdropstat_test.py @@ -0,0 +1,98 @@ +import os +import sys +import pytest + +import show.main as show +import clear.main as clear +import config.main as config + +from click.testing import CliRunner +from shutil import copyfile + +test_path = os.path.dirname(os.path.abspath(__file__)) +modules_path = os.path.dirname(test_path) +scripts_path = os.path.join(modules_path, "scripts") +sys.path.insert(0, test_path) +sys.path.insert(0, modules_path) + + +show_pg_dropped_packet_stat="""\ +Ingress PG dropped packets: + Port PG0 PG1 PG2 PG3 PG4 PG5 PG6 PG7 +--------- ----- ----- ----- ----- ----- ----- ----- ----- +Ethernet0 800 801 802 803 804 805 806 807 +Ethernet4 400 401 402 403 404 405 406 407 +Ethernet8 100 101 102 103 104 105 106 107 +""" + +show_cleared_pg_dropped_packet_stat="""\ +Ingress PG dropped packets: + Port PG0 PG1 PG2 PG3 PG4 PG5 PG6 PG7 +--------- ----- ----- ----- ----- ----- ----- ----- ----- +Ethernet0 0 0 0 0 0 0 0 0 +Ethernet4 0 0 0 0 0 0 0 0 +Ethernet8 0 0 0 0 0 0 0 0 +""" + +class TestPgDropstat(object): + @classmethod + def setup_class(cls): + os.environ["PATH"] += os.pathsep + scripts_path + os.environ['UTILITIES_UNIT_TESTING'] = "2" + print("SETUP") + + @pytest.fixture(scope='function') + def replace_config_db_file(self): + sample_config_db_file = os.path.join(test_path, "mock_tables/pgdrop_input", "config_db.json") + mock_config_db_file = os.path.join(test_path, "mock_tables", "config_db.json") + + #Backup origin config_db and replace it with config_db file with disabled PG_DROP counters + copyfile(mock_config_db_file, "/tmp/config_db.json") + copyfile(sample_config_db_file, mock_config_db_file) + + yield + + copyfile("/tmp/config_db.json", mock_config_db_file) + + def test_show_pg_drop_disabled(self, replace_config_db_file): + runner = CliRunner() + + result = runner.invoke(show.cli.commands["priority-group"].commands["drop"].commands["counters"]) + assert result.exit_code == 0 + print(result.exit_code) + + assert result.output == "Warning: PG counters are disabled. Use 'counterpoll pg-drop enable' to enable polling\n" + print(result.output) + + def test_show_pg_drop_show(self): + self.executor(clear_before_show = False) + + def test_show_pg_drop_clear(self): + self.executor(clear_before_show = True) + + def executor(self, clear_before_show): + runner = CliRunner() + show_output = show_pg_dropped_packet_stat + + # Clear stats + if clear_before_show: + result = runner.invoke(clear.cli.commands["priority-group"].commands["drop"].commands["counters"], []) + assert result.exit_code == 0 + show_output = show_cleared_pg_dropped_packet_stat + + result = runner.invoke(show.cli.commands["priority-group"].commands["drop"].commands["counters"], []) + + print(result.exit_code) + print(result.output) + + assert result.exit_code == 0 + assert result.output == show_output + + @classmethod + def teardown_class(cls): + os.environ["PATH"] = os.pathsep.join(os.environ["PATH"].split(os.pathsep)[:-1]) + os.environ['UTILITIES_UNIT_TESTING'] = "0" + dropstat_dir_prefix = '/tmp/dropstat' + dir_path = "{}-{}/".format(dropstat_dir_prefix, os.getuid()) + os.system("rm -rf {}".format(dir_path)) + print("TEARDOWN")