Skip to content

Commit

Permalink
[pfcwd]: Configuration commands (sonic-net#147)
Browse files Browse the repository at this point in the history
Add 'start/stop' commands
Create 'show' group
Move 'stats' to 'show' and add 'show config'

Signed-off-by: marian-pritsak <[email protected]>
  • Loading branch information
marian-pritsak authored and lguohan committed Nov 9, 2017
1 parent 0a720ca commit e87bb97
Showing 1 changed file with 101 additions and 5 deletions.
106 changes: 101 additions & 5 deletions pfcwd/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import click
import swsssdk
from tabulate import tabulate
from natsort import natsorted

STATS_DESCRIPTION = [
('STORM DETECTED CNT', 'PFC_WD_QUEUE_STATS_DEADLOCK_DETECTED'),
Expand All @@ -17,7 +18,14 @@
('RX PACKETS LAST DROP', 'PFC_WD_QUEUE_STATS_RX_DROPPED_PACKETS_LAST')
]

CONFIG_DESCRIPTION = [
('ACTION', 'action', 'drop'),
('DETECTION TIME', 'detection_time', 'N/A'),
('RESTORATION TIME', 'restoration_time', 'infinite')
]

STATS_HEADER = ('QUEUE',) + zip(*STATS_DESCRIPTION)[0]
CONFIG_HEADER = ('PORT',) + zip(*CONFIG_DESCRIPTION)[0]

# Main entrypoint
@click.group()
Expand All @@ -26,13 +34,22 @@ def cli():

def get_all_queues(db):
queue_names = db.get_all(db.COUNTERS_DB, 'COUNTERS_QUEUE_NAME_MAP')
return sorted(queue_names.keys())
return natsorted(queue_names.keys())

def get_all_ports(db):
port_names = db.get_all(db.COUNTERS_DB, 'COUNTERS_PORT_NAME_MAP')
return natsorted(port_names.keys())

# Show commands
@cli.group()
def show():
""" Show PFC Watchdog information"""

# Show stats
@cli.command()
@show.command()
@click.argument('queues', nargs = -1)
def stats(queues):
""" Show PFC WD stats per queue """
""" Show PFC Watchdog stats per queue """
db = swsssdk.SonicV2Connector(host='127.0.0.1')
db.connect(db.COUNTERS_DB)
table = []
Expand All @@ -49,10 +66,89 @@ def stats(queues):
for stat in STATS_DESCRIPTION:
line = stats.get(stat[1], '0')
stats_list.append(line)
#click.echo("Queue ID " + queue)
table.append([queue] + stats_list)

print(tabulate(table, STATS_HEADER, stralign='right', numalign='right', tablefmt='simple'))
click.echo(tabulate(table, STATS_HEADER, stralign='right', numalign='right', tablefmt='simple'))

# Show stats
@show.command()
@click.argument('ports', nargs = -1)
def config(ports):
""" Show PFC Watchdog configuration """
configdb = swsssdk.ConfigDBConnector()
configdb.connect()
countersdb = swsssdk.SonicV2Connector(host='127.0.0.1')
countersdb.connect(countersdb.COUNTERS_DB)
table = []

all_ports = get_all_ports(countersdb)

if len(ports) == 0:
ports = all_ports

for port in ports:
config_list = []
config_entry = configdb.get_entry('PFC_WD_TABLE', port)
if config_entry is None or config_entry == {}:
continue
for config in CONFIG_DESCRIPTION:
line = config_entry.get(config[1], config[2])
config_list.append(line)
table.append([port] + config_list)

click.echo(tabulate(table, CONFIG_HEADER, stralign='right', numalign='right', tablefmt='simple'))

# Start WD
@cli.command()
@click.option('--action', '-a', type=click.Choice(['drop', 'forward', 'alert']))
@click.option('--restoration-time', '-r', type=click.IntRange(100, 5000))
@click.argument('ports', nargs = -1)
@click.argument('detection-time', type=click.IntRange(100, 60000))
def start(action, restoration_time, ports, detection_time):
""" Start PFC watchdog on port(s) """
configdb = swsssdk.ConfigDBConnector()
configdb.connect()
countersdb = swsssdk.SonicV2Connector(host='127.0.0.1')
countersdb.connect(countersdb.COUNTERS_DB)

all_ports = get_all_ports(countersdb)

if len(ports) == 0:
ports = all_ports

pfcwd_info = {
'detection_time': detection_time,
}
if action is not None:
pfcwd_info['action'] = action
if restoration_time is not None:
pfcwd_info['restoration_time'] = restoration_time

for port in ports:
if port not in all_ports:
continue
configdb.set_entry("PFC_WD_TABLE", port, None)
configdb.set_entry("PFC_WD_TABLE", port, pfcwd_info)

# Stop WD
@cli.command()
@click.argument('ports', nargs = -1)
def stop(ports):
""" Stop PFC watchdog on port(s) """
configdb = swsssdk.ConfigDBConnector()
configdb.connect()
countersdb = swsssdk.SonicV2Connector(host='127.0.0.1')
countersdb.connect(countersdb.COUNTERS_DB)

all_ports = get_all_ports(countersdb)

if len(ports) == 0:
ports = all_ports

for port in ports:
if port not in all_ports:
continue
configdb.set_entry("PFC_WD_TABLE", port, None)

if __name__ == '__main__':
cli()

0 comments on commit e87bb97

Please sign in to comment.