From b2261595de030df59698fd3a1095edb9b5796ee2 Mon Sep 17 00:00:00 2001 From: Mahesh Maddikayala <10645050+smaheshm@users.noreply.github.com> Date: Thu, 12 Nov 2020 09:11:06 -0800 Subject: [PATCH] [CLI][PFCWD] Fix issue with specifying ports in pfcwd start on masic platforms (#1203) When ports are specified as option to 'pfcwd start', port validation fails on multi ASIC platform as the validation logic is executed on each ASIC namespace. Validation for valid ports fails as the port exits in only one namespace and not in others. Changed logic to perform port validation after collecting ports from all namespaces. If port validation fails there is no config change on other valid ports --- pfcwd/main.py | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/pfcwd/main.py b/pfcwd/main.py index 2b8435c1d122..6c96c3b74986 100644 --- a/pfcwd/main.py +++ b/pfcwd/main.py @@ -105,6 +105,7 @@ def __init__(self, namespace=None, display=constants.DISPLAY_ALL): self.config_db = None self.multi_asic = multi_asic_util.MultiAsic(display, namespace) self.table = [] + self.all_ports = [] @multi_asic_util.run_on_multi_asic def collect_stats(self, empty, queues): @@ -147,9 +148,27 @@ def show_stats(self, empty, queues): tablefmt='simple' )) + @multi_asic_util.run_on_multi_asic + def get_all_namespace_ports(self): + ports = get_all_ports( + self.db, self.multi_asic.current_namespace, + self.multi_asic.display_option + ) + self.all_ports.extend(ports) + + def get_invalid_ports(self, ports=[]): + if len(ports) == 0: + return [] + self.get_all_namespace_ports() + port_set = set(ports) + # "all" is a valid option, remove before performing set diff + port_set.discard("all") + return port_set - set(self.all_ports) + @multi_asic_util.run_on_multi_asic def collect_config(self, ports): table = [] + if len(ports) == 0: ports = get_all_ports( self.db, self.multi_asic.current_namespace, @@ -208,23 +227,24 @@ def config(self, ports): tablefmt='simple' )) - @multi_asic_util.run_on_multi_asic def start(self, action, restoration_time, ports, detection_time): + invalid_ports = self.get_invalid_ports(ports) + if len(invalid_ports): + click.echo("Failed to run command, invalid options:") + for opt in invalid_ports: + click.echo(opt) + exit() + self.start_cmd(action, restoration_time, ports, detection_time) + + @multi_asic_util.run_on_multi_asic + def start_cmd(self, action, restoration_time, ports, detection_time): if os.geteuid() != 0: exit("Root privileges are required for this operation") - allowed_strs = ['ports', 'all', 'detection-time'] all_ports = get_all_ports( self.db, self.multi_asic.current_namespace, self.multi_asic.display_option ) - 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 @@ -379,7 +399,6 @@ def big_red_switch(self, big_red_switch): pfcwd_info ) - # Show stats class Show(object): # Show commands