diff --git a/config/main.py b/config/main.py index 42bb4ac30a..ceb22c13c0 100755 --- a/config/main.py +++ b/config/main.py @@ -236,6 +236,7 @@ def load_minigraph(): command = "{} -H -m --write-to-db".format(SONIC_CFGGEN_PATH) run_command(command, display_cmd=True) client.set(config_db.INIT_INDICATOR, 1) + run_command('pfcwd start_default', display_cmd=True) if os.path.isfile('/etc/sonic/acl.json'): run_command("acl-loader update full /etc/sonic/acl.json", display_cmd=True) run_command("config qos reload", display_cmd=True) diff --git a/pfcwd/main.py b/pfcwd/main.py index 254d80f202..d5d39caaae 100644 --- a/pfcwd/main.py +++ b/pfcwd/main.py @@ -5,6 +5,13 @@ from tabulate import tabulate from natsort import natsorted +# Default configuration +DEFAULT_DETECTION_TIME = 200 +DEFAULT_RESTORATION_TIME = 200 +DEFAULT_POLL_INTERVAL = 200 +DEFAULT_PORT_NUM = 32 +DEFAULT_ACTION = 'drop' + STATS_DESCRIPTION = [ ('STORM DETECTED/RESTORED', 'PFC_WD_QUEUE_STATS_DEADLOCK_DETECTED', 'PFC_WD_QUEUE_STATS_DEADLOCK_RESTORED'), ('TX OK/DROP', 'PFC_WD_QUEUE_STATS_TX_PACKETS', 'PFC_WD_QUEUE_STATS_TX_DROPPED_PACKETS'), @@ -35,6 +42,17 @@ def get_all_ports(db): port_names = db.get_all(db.COUNTERS_DB, 'COUNTERS_PORT_NAME_MAP') return natsorted(port_names.keys()) +def get_server_facing_ports(db): + candidates = db.get_table('DEVICE_NEIGHBOR') + server_facing_ports = [] + for port in candidates.keys(): + neighbor = db.get_entry('DEVICE_NEIGHBOR_METADATA', candidates[port]['name']) + if neighbor and neighbor['type'].lower() == 'server': + server_facing_ports.append(port) + if not server_facing_ports: + server_facing_ports = [p[1] for p in db.get_table('VLAN_MEMBER').keys()] + return server_facing_ports + # Show commands @cli.group() def show(): @@ -162,5 +180,39 @@ def stop(ports): continue configdb.mod_entry("PFC_WD_TABLE", port, None) +# Set WD default configuration on server facing ports when enable flag is on +@cli.command() +def start_default(): + """ Start PFC WD by default configurations """ + configdb = swsssdk.ConfigDBConnector() + configdb.connect() + enable = configdb.get_entry('DEVICE_METADATA', 'localhost').get('default_pfcwd_status') + + server_facing_ports = get_server_facing_ports(configdb) + + if not enable or enable.lower() != "enable": + return + + device_type = configdb.get_entry('DEVICE_METADATA', 'localhost').get('type') + if device_type.lower() != "torrouter": + return + + port_num = len(configdb.get_table('PORT').keys()) + + # Paramter values positively correlate to the number of ports. + multiply = max(1, (port_num-1)/DEFAULT_PORT_NUM+1) + pfcwd_info = { + 'detection_time': DEFAULT_DETECTION_TIME * multiply, + 'restoration_time': DEFAULT_RESTORATION_TIME * multiply, + 'action': DEFAULT_ACTION + } + + for port in server_facing_ports: + configdb.set_entry("PFC_WD_TABLE", port, pfcwd_info) + + pfcwd_info = {} + pfcwd_info['POLL_INTERVAL'] = DEFAULT_POLL_INTERVAL * multiply + configdb.mod_entry("PFC_WD_TABLE", "GLOBAL", pfcwd_info) + if __name__ == '__main__': cli()