Skip to content

Commit 6d00d14

Browse files
wendanilguohan
authored andcommitted
pfcwd cmd check (sonic-net#342)
* require root privilege for all pfcwd configuration commands (sonic-net#330) Signed-off-by: Guohan Lu <[email protected]> * 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 <[email protected]> * Check if ports list has invalid string element; Correct typo restoration-time Signed-off-by: Wenda <[email protected]>
1 parent eb92560 commit 6d00d14

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

pfcwd/main.py

+18-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import click
44
import swsssdk
5+
import os
56
from tabulate import tabulate
67
from natsort import natsorted
78

@@ -124,18 +125,30 @@ def config(ports):
124125
@cli.command()
125126
@click.option('--action', '-a', type=click.Choice(['drop', 'forward', 'alert']))
126127
@click.option('--restoration-time', '-r', type=click.IntRange(100, 60000))
127-
@click.argument('ports', nargs = -1)
128+
@click.argument('ports', nargs=-1)
128129
@click.argument('detection-time', type=click.IntRange(100, 5000))
129130
def start(action, restoration_time, ports, detection_time):
130-
""" Start PFC watchdog on port(s). To config all ports, use all as input. """
131+
"""
132+
Start PFC watchdog on port(s). To config all ports, use all as input.
133+
134+
Example:
135+
136+
sudo pfcwd start --action drop ports all detection-time 400 --restoration-time 400
137+
138+
"""
131139
if os.geteuid() != 0:
132140
exit("Root privileges are required for this operation")
141+
allowed_strs = ['ports', 'all', 'detection-time']
133142
configdb = swsssdk.ConfigDBConnector()
134143
configdb.connect()
135144
countersdb = swsssdk.SonicV2Connector(host='127.0.0.1')
136145
countersdb.connect(countersdb.COUNTERS_DB)
137146

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

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

151167
for port in ports:
152168
if port == "all":

0 commit comments

Comments
 (0)