diff --git a/clear/main.py b/clear/main.py index d74947967d..b66e966c52 100755 --- a/clear/main.py +++ b/clear/main.py @@ -200,5 +200,35 @@ def arp(ipaddress): cli.add_command(arp) ip.add_command(arp) +# +# 'fdb' command #### +# +@cli.group() +def fdb(): + """Clear FDB table""" + pass + +@fdb.command('all') +def clear_all_fdb(): + """Clear All FDB entries""" + command = 'fdbclear' + run_command(command) + +# 'sonic-clear fdb port' and 'sonic-clear fdb vlan' will be added later +''' +@fdb.command('port') +@click.argument('portid', required=True) +def clear_port_fdb(portid): + """Clear FDB entries learned from one port""" + command = 'fdbclear' + ' -p ' + portid + run_command(command) + +@fdb.command('vlan') +@click.argument('vlanid', required=True) +def clear_vlan_fdb(vlanid): + """Clear FDB entries learned in one VLAN""" + command = 'fdbclear' + ' -v ' + vlanid + run_command(command) +''' if __name__ == '__main__': cli() diff --git a/scripts/fdbclear b/scripts/fdbclear new file mode 100644 index 0000000000..a8100af2cb --- /dev/null +++ b/scripts/fdbclear @@ -0,0 +1,58 @@ +#!/usr/bin/python +""" + Script to clear MAC/FDB entries learnt in Hardware + + usage: fdbclear [-p PORT] [-v VLAN] + optional arguments: + -p, --port FDB learned on specific port: Ethernet0 + -v, --vlan FDB learned on specific Vlan: 1000 + + Example of the output: + +""" + +import argparse +import json +import sys + +from natsort import natsorted +from swsssdk import SonicV2Connector, port_util +from tabulate import tabulate + +class FdbClear(object): + + + def __init__(self): + super(FdbClear,self).__init__() + self.db = SonicV2Connector(host="127.0.0.1") + self.db.connect(self.db.APPL_DB) + return + + def send_notification(self, op, data): + opdata = [op,data] + msg = json.dumps(opdata,separators=(',',':')) + self.db.publish('APPL_DB','FLUSHFDBREQUEST', msg) + return + +def main(): + + parser = argparse.ArgumentParser(description='Clear FDB entries', formatter_class=argparse.RawTextHelpFormatter) + parser.add_argument('-p', '--port', type=str, help='Clear FDB learned on specific port: Ethernet0', default=None) + parser.add_argument('-v', '--vlan', type=str, help='Clear FDB learned on specific Vlan: 1001', default=None) + args = parser.parse_args() + + try: + fdb = FdbClear() + if args.vlan is not None: + print("command not supported yet.") + elif args.port is not None: + print("command not supported yet.") + else: + fdb.send_notification("ALL", "ALL") + print("FDB entries are cleared.") + except Exception as e: + print e.message + sys.exit(1) + +if __name__ == "__main__": + main() diff --git a/setup.py b/setup.py index 6e5e524413..f9ed80351a 100644 --- a/setup.py +++ b/setup.py @@ -51,6 +51,7 @@ def get_test_suite(): 'scripts/ecnconfig', 'scripts/fast-reboot', 'scripts/fast-reboot-dump.py', + 'scripts/fdbclear', 'scripts/fdbshow', 'scripts/generate_dump', 'scripts/intfutil',