Skip to content

Commit

Permalink
[show] Add 'show p4-table' command (sonic-net#2773)
Browse files Browse the repository at this point in the history
### What I did
Add 'show p4-table' command to the CLI. This is a new command to display P4RT tables used by PINS (P4 Integrated Network Stack).

#### How I did it
Add the command in show/p4_table.py to output the P4RT table entries in the Redis application database.

#### How to verify it
Run unit tests, manually test

#### Previous command output (if the output of a command-line utility has changed)
None

#### New command output (if the output of a command-line utility has changed)
See command reference
  • Loading branch information
royyi8 committed Aug 15, 2023
1 parent 9694c7d commit 6322389
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 0 deletions.
58 changes: 58 additions & 0 deletions doc/Command-Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 <table_prefix>
```
- 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
Expand Down
2 changes: 2 additions & 0 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
34 changes: 34 additions & 0 deletions show/p4_table.py
Original file line number Diff line number Diff line change
@@ -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))
28 changes: 28 additions & 0 deletions tests/mock_tables/appl_db.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
69 changes: 69 additions & 0 deletions tests/p4_table_test.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6322389

Please sign in to comment.