diff --git a/doc/Command-Reference.md b/doc/Command-Reference.md index 211074bc6c..c4a28a5460 100644 --- a/doc/Command-Reference.md +++ b/doc/Command-Reference.md @@ -132,6 +132,7 @@ * [Platform Specific Commands](#platform-specific-commands) * [Mellanox Platform Specific Commands](#mellanox-platform-specific-commands) * [Barefoot Platform Specific Commands](#barefoot-platform-specific-commands) +* [PINS](#pins-show commands) * [PortChannels](#portchannels) * [PortChannel Show commands](#portchannel-show-commands) * [PortChannel Config commands](#portchannel-config-commands) @@ -8384,6 +8385,63 @@ It supports add/update/remove operations. Go Back To [Beginning of the document](#) or [Beginning of this section](#pbh) +## PINS + +### PINS Show commands + +#### P4RT Table + +**show p4-table** + +This command displays the P4RT (P4 Runtime) tables in the application database. + +These tables are used by PINS (P4 Integrated Network Stack) for orchagent to +communicate with the P4RT application. + +- Usage: + ```bash + show p4-table + show p4-table + ``` + +- Example: + + ```bash + admin@sonic:~$ show p4-table + { + "P4RT_TABLE:ACL_TABLE_DEFINITION_TABLE:ACL_ACL_PRE_INGRESS_TABLE": { + "stage":"PRE_INGRESS", + "match/dst_ipv6":"{\"bitwidth\":128,\"format\":\"IPV6\",\"kind\":\"sai_field\",\"sai_field\":\"SAI_ACL_TABLE_ATTR_FIELD_DST_IPV6\"}", + "match/in_port":"{\"format\":\"STRING\",\"kind\":\"sai_field\",\"sai_field\":\"SAI_ACL_TABLE_ATTR_FIELD_IN_PORT\"}", + "match/is_ipv4":"{\"bitwidth\":1,\"format\":\"HEX_STRING\",\"kind\":\"sai_field\",\"sai_field\":\"SAI_ACL_TABLE_ATTR_FIELD_ACL_IP_TYPE/IPV4ANY\"}", + "action/set_vrf": "[{\"action\":\"SAI_PACKET_ACTION_FORWARD\"},{\"action\":\"SAI_ACL_ENTRY_ATTR_ACTION_SET_VRF\",\"param\":\"vrf_id\"}]" + }, + "P4RT_TABLE:ACL_ACL_PRE_INGRESS_TABLE:{\"match/dst_ip\":\"10.53.192.0&255.255.240.0\",\"match/is_ipv4\":\"0x1\",\"priority\":1132}": { + "action": "set_vrf", + "param/vrf_id": "p4rt-vrf-80", + "controller_metadata": "my metadata" + }, + ... + } + ``` + + The command supports filtering entries by table name. If a prefix is + specified, only p4 table entries matching that prefix will be displayed. + + ```bash + admin@sonic:~$ show p4-table ACL_ACL_PRE_INGRESS_TABLE + { + "P4RT_TABLE:ACL_ACL_PRE_INGRESS_TABLE:{\"match/dst_ip\":\"10.53.192.0&255.255.240.0\",\"match/is_ipv4\":\"0x1\",\"priority\":1132}": { + "action": "set_vrf", + "param/vrf_id": "p4rt-vrf-80", + "controller_metadata": "my metadata" + }, + ... + } + ``` + +Go Back To [Beginning of the document](#) or [Beginning of this section](#pbh) + ## QoS ### QoS Show commands diff --git a/show/main.py b/show/main.py index aa1b5257b4..35303b6eda 100755 --- a/show/main.py +++ b/show/main.py @@ -54,6 +54,7 @@ from . import muxcable from . import nat from . import platform +from . import p4_table from . import processes from . import reboot_cause from . import sflow @@ -288,6 +289,7 @@ def cli(ctx): cli.add_command(muxcable.muxcable) cli.add_command(nat.nat) cli.add_command(platform.platform) +cli.add_command(p4_table.p4_table) cli.add_command(processes.processes) cli.add_command(reboot_cause.reboot_cause) cli.add_command(sflow.sflow) diff --git a/show/p4_table.py b/show/p4_table.py new file mode 100644 index 0000000000..ea5b5e4484 --- /dev/null +++ b/show/p4_table.py @@ -0,0 +1,34 @@ +import json +import click +import utilities_common.cli as clicommon +from swsscommon.swsscommon import SonicV2Connector +from tabulate import tabulate + + +# +# 'p4_table' command ("show p4-table") +# +@click.command() +@click.argument('table_name', required=False) +@click.option('--verbose', is_flag=True, help='Enable verbose output') +def p4_table(table_name, verbose): + """Display all P4RT tables""" + appDB = SonicV2Connector(use_unix_socket_path=True) + + if appDB is None: + click.echo('Failed to connect to the application database.') + return -1 + + db = appDB.APPL_DB + appDB.connect(db) + + if table_name is None: + table_name = '' + + keys = appDB.keys(db, f'P4RT_TABLE:{table_name}*') + db_info = {} + + for k in keys: + db_info[k] = appDB.get_all(db, k) + + click.echo(json.dumps(db_info, indent=4)) diff --git a/tests/mock_tables/appl_db.json b/tests/mock_tables/appl_db.json index e330bdaddc..71b9e7f249 100644 --- a/tests/mock_tables/appl_db.json +++ b/tests/mock_tables/appl_db.json @@ -342,5 +342,33 @@ }, "BGP_PROFILE_TABLE:FROM_SDN_SLB_ROUTES": { "community_id" : "1234:1235" + }, + "P4RT_TABLE:ACL_TABLE_DEFINITION_TABLE:ACL_ACL_PRE_INGRESS_TABLE": { + "stage": "PRE_INGRESS", + "match/dst_ipv6": "{\"bitwidth\":128,\"format\":\"IPV6\",\"kind\":\"sai_field\",\"sai_field\":\"SAI_ACL_TABLE_ATTR_FIELD_DST_IPV6\"}", + "match/in_port": "{\"format\":\"STRING\",\"kind\":\"sai_field\",\"sai_field\":\"SAI_ACL_TABLE_ATTR_FIELD_IN_PORT\"}", + "match/is_ipv4": "{\"bitwidth\":1,\"format\":\"HEX_STRING\",\"kind\":\"sai_field\",\"sai_field\":\"SAI_ACL_TABLE_ATTR_FIELD_ACL_IP_TYPE/IPV4ANY\"}", + "action/set_vrf": "[{\"action\":\"SAI_PACKET_ACTION_FORWARD\"},{\"action\":\"SAI_ACL_ENTRY_ATTR_ACTION_SET_VRF\",\"param\":\"vrf_id\"}]" + }, + "P4RT_TABLE:ACL_TABLE_DEFINITION_TABLE:ACL_ACL_INGRESS_TABLE": { + "stage": "INGRESS", + "match/arp_tpa": "{\"bitwidth\":32,\"elements\":[{\"base\":\"SAI_UDF_BASE_L3\",\"bitwidth\":16,\"kind\":\"udf\",\"offset\":24},{\"base\":\"SAI_UDF_BASE_L3\",\"bitwidth\":16,\"kind\":\"udf\",\"offset\":26}],\"format\":\"HEX_STRING\",\"kind\":\"composite\"}", + "action/mirror": "[{\"action\":\"SAI_PACKET_ACTION_FORWARD\"},{\"action\":\"SAI_ACL_ENTRY_ATTR_ACTION_MIRROR_INGRESS\",\"param\":\"mirror_session_id\"}]", + "match/is_ipv4": "{\"bitwidth\":1,\"format\":\"HEX_STRING\",\"kind\":\"sai_field\",\"sai_field\":\"SAI_ACL_TABLE_ATTR_FIELD_ACL_IP_TYPE/IPV4ANY\"}", + "action/trap": "[{\"action\":\"SAI_PACKET_ACTION_TRAP\"},{\"action\":\"QOS_QUEUE\",\"param\":\"qos_queue\"}]" + }, + "P4RT_TABLE:ACL_ACL_INGRESS_TABLE:{\"match/dst_mac\":\"33:33:00:00:00:02&ff:ff:ff:ff:ff:ff\",\"match/icmpv6_type\":\"0x87&0xff\",\"match/ip_protocol\":\"0x3a&0xff\",\"match/is_ipv6\":\"0x1\",\"priority\":2070}": { + "action": "trap", + "param/qos_queue": "0x6", + "meter/cir": "28000", + "meter/cburst": "7000", + "meter/pir": "28000", + "meter/pburst": "7000", + "controller_metadata": "my metadata" + }, + "P4RT_TABLE:ACL_ACL_PRE_INGRESS_TABLE:{\"match/dst_ip\":\"10.53.192.0&255.255.240.0\",\"match/is_ipv4\":\"0x1\",\"priority\":1132}": { + "action": "set_vrf", + "param/vrf_id": "p4rt-vrf-80", + "controller_metadata": "my metadata" } } diff --git a/tests/p4_table_test.py b/tests/p4_table_test.py new file mode 100644 index 0000000000..1341ea796b --- /dev/null +++ b/tests/p4_table_test.py @@ -0,0 +1,69 @@ +from click.testing import CliRunner +import show.main as show + +show_p4_table_output = r"""{ + "P4RT_TABLE:ACL_TABLE_DEFINITION_TABLE:ACL_ACL_PRE_INGRESS_TABLE": { + "stage": "PRE_INGRESS", + "match/dst_ipv6": "{\"bitwidth\":128,\"format\":\"IPV6\",\"kind\":\"sai_field\",\"sai_field\":\"SAI_ACL_TABLE_ATTR_FIELD_DST_IPV6\"}", + "match/in_port": "{\"format\":\"STRING\",\"kind\":\"sai_field\",\"sai_field\":\"SAI_ACL_TABLE_ATTR_FIELD_IN_PORT\"}", + "match/is_ipv4": "{\"bitwidth\":1,\"format\":\"HEX_STRING\",\"kind\":\"sai_field\",\"sai_field\":\"SAI_ACL_TABLE_ATTR_FIELD_ACL_IP_TYPE/IPV4ANY\"}", + "action/set_vrf": "[{\"action\":\"SAI_PACKET_ACTION_FORWARD\"},{\"action\":\"SAI_ACL_ENTRY_ATTR_ACTION_SET_VRF\",\"param\":\"vrf_id\"}]" + }, + "P4RT_TABLE:ACL_TABLE_DEFINITION_TABLE:ACL_ACL_INGRESS_TABLE": { + "stage": "INGRESS", + "match/arp_tpa": "{\"bitwidth\":32,\"elements\":[{\"base\":\"SAI_UDF_BASE_L3\",\"bitwidth\":16,\"kind\":\"udf\",\"offset\":24},{\"base\":\"SAI_UDF_BASE_L3\",\"bitwidth\":16,\"kind\":\"udf\",\"offset\":26}],\"format\":\"HEX_STRING\",\"kind\":\"composite\"}", + "action/mirror": "[{\"action\":\"SAI_PACKET_ACTION_FORWARD\"},{\"action\":\"SAI_ACL_ENTRY_ATTR_ACTION_MIRROR_INGRESS\",\"param\":\"mirror_session_id\"}]", + "match/is_ipv4": "{\"bitwidth\":1,\"format\":\"HEX_STRING\",\"kind\":\"sai_field\",\"sai_field\":\"SAI_ACL_TABLE_ATTR_FIELD_ACL_IP_TYPE/IPV4ANY\"}", + "action/trap": "[{\"action\":\"SAI_PACKET_ACTION_TRAP\"},{\"action\":\"QOS_QUEUE\",\"param\":\"qos_queue\"}]" + }, + "P4RT_TABLE:ACL_ACL_INGRESS_TABLE:{\"match/dst_mac\":\"33:33:00:00:00:02&ff:ff:ff:ff:ff:ff\",\"match/icmpv6_type\":\"0x87&0xff\",\"match/ip_protocol\":\"0x3a&0xff\",\"match/is_ipv6\":\"0x1\",\"priority\":2070}": { + "action": "trap", + "param/qos_queue": "0x6", + "meter/cir": "28000", + "meter/cburst": "7000", + "meter/pir": "28000", + "meter/pburst": "7000", + "controller_metadata": "my metadata" + }, + "P4RT_TABLE:ACL_ACL_PRE_INGRESS_TABLE:{\"match/dst_ip\":\"10.53.192.0&255.255.240.0\",\"match/is_ipv4\":\"0x1\",\"priority\":1132}": { + "action": "set_vrf", + "param/vrf_id": "p4rt-vrf-80", + "controller_metadata": "my metadata" + } +} +""" + +show_p4_table_filter_acl_table_output = r"""{ + "P4RT_TABLE:ACL_ACL_INGRESS_TABLE:{\"match/dst_mac\":\"33:33:00:00:00:02&ff:ff:ff:ff:ff:ff\",\"match/icmpv6_type\":\"0x87&0xff\",\"match/ip_protocol\":\"0x3a&0xff\",\"match/is_ipv6\":\"0x1\",\"priority\":2070}": { + "action": "trap", + "param/qos_queue": "0x6", + "meter/cir": "28000", + "meter/cburst": "7000", + "meter/pir": "28000", + "meter/pburst": "7000", + "controller_metadata": "my metadata" + }, + "P4RT_TABLE:ACL_ACL_PRE_INGRESS_TABLE:{\"match/dst_ip\":\"10.53.192.0&255.255.240.0\",\"match/is_ipv4\":\"0x1\",\"priority\":1132}": { + "action": "set_vrf", + "param/vrf_id": "p4rt-vrf-80", + "controller_metadata": "my metadata" + } +} +""" + + +class TestP4Table(object): + + def test_show_p4_table(self): + runner = CliRunner() + result = runner.invoke(show.cli.commands["p4-table"], []) + print(result.output) + assert result.exit_code == 0 + assert result.output == show_p4_table_output + + def test_show_p4_table_filter_acl_table(self): + runner = CliRunner() + result = runner.invoke(show.cli.commands["p4-table"], ["ACL_ACL"]) + print(result.output) + assert result.exit_code == 0 + assert result.output == show_p4_table_filter_acl_table_output