Skip to content

Commit

Permalink
pfcwd cmd check (sonic-net#342)
Browse files Browse the repository at this point in the history
* require root privilege for all pfcwd configuration commands (sonic-net#330)

Signed-off-by: Guohan Lu <gulv@microsoft.com>

* pfcwd start: 1) default restoration time to 2 times detection time if
not defined; 2) check if options are prefixed with "--";

Signed-off-by: Wenda <wenni@microsoft.com>

* Check if ports list has invalid string element;

Correct typo restoration-time

Signed-off-by: Wenda <wenni@microsoft.com>
  • Loading branch information
wendani authored and lguohan committed Oct 13, 2018
1 parent eb92560 commit 6d00d14
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions pfcwd/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import click
import swsssdk
import os
from tabulate import tabulate
from natsort import natsorted

Expand Down Expand Up @@ -124,18 +125,30 @@ def config(ports):
@cli.command()
@click.option('--action', '-a', type=click.Choice(['drop', 'forward', 'alert']))
@click.option('--restoration-time', '-r', type=click.IntRange(100, 60000))
@click.argument('ports', nargs = -1)
@click.argument('ports', nargs=-1)
@click.argument('detection-time', type=click.IntRange(100, 5000))
def start(action, restoration_time, ports, detection_time):
""" Start PFC watchdog on port(s). To config all ports, use all as input. """
"""
Start PFC watchdog on port(s). To config all ports, use all as input.
Example:
sudo pfcwd start --action drop ports all detection-time 400 --restoration-time 400
"""
if os.geteuid() != 0:
exit("Root privileges are required for this operation")
allowed_strs = ['ports', 'all', 'detection-time']
configdb = swsssdk.ConfigDBConnector()
configdb.connect()
countersdb = swsssdk.SonicV2Connector(host='127.0.0.1')
countersdb.connect(countersdb.COUNTERS_DB)

all_ports = get_all_ports(countersdb)
allowed_strs = allowed_strs + all_ports
for p in ports:
if p not in allowed_strs:
raise click.BadOptionUsage("Bad command line format. Try 'pfcwd start --help' for usage")

if len(ports) == 0:
ports = all_ports
Expand All @@ -147,6 +160,9 @@ def start(action, restoration_time, ports, detection_time):
pfcwd_info['action'] = action
if restoration_time is not None:
pfcwd_info['restoration_time'] = restoration_time
else:
pfcwd_info['restoration_time'] = 2 * detection_time
print "restoration time not defined; default to 2 times detection time: %d ms" % (2 * detection_time)

for port in ports:
if port == "all":
Expand Down

0 comments on commit 6d00d14

Please sign in to comment.