From 46741fcd2a6ba0c680e478b523705d779c4e4253 Mon Sep 17 00:00:00 2001 From: keboliu Date: Fri, 12 Jan 2018 13:27:44 +0200 Subject: [PATCH 1/3] add code to support clear fdb cli --- clear/main.py | 30 +++++++++++++++++++++++ scripts/fdbclear | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ setup.py | 1 + 3 files changed, 93 insertions(+) create mode 100644 scripts/fdbclear 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..3664980a44 --- /dev/null +++ b/scripts/fdbclear @@ -0,0 +1,62 @@ +#!/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): + + """ + HEADER = ['No.', 'Vlan', 'MacAddress', 'Port'] + FDB_COUNT = 0 + """ + + def __init__(self): + super(FdbClear,self).__init__() + self.db = SonicV2Connector(host="127.0.0.1") + self.db.connect(self.db.ASIC_DB) + return + + def send_notification(self, op, data): + opdata = [op,data] + msg = json.dumps(opdata,separators=(',',':')) + self.db.publish('ASIC_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', From dadfb83089644f3b06aadfcf9aaa486b53a0eae2 Mon Sep 17 00:00:00 2001 From: keboliu Date: Tue, 16 Jan 2018 04:53:07 +0200 Subject: [PATCH 2/3] remove redundant comments --- scripts/fdbclear | 4 ---- 1 file changed, 4 deletions(-) diff --git a/scripts/fdbclear b/scripts/fdbclear index 3664980a44..ea9c4178f9 100644 --- a/scripts/fdbclear +++ b/scripts/fdbclear @@ -21,10 +21,6 @@ from tabulate import tabulate class FdbClear(object): - """ - HEADER = ['No.', 'Vlan', 'MacAddress', 'Port'] - FDB_COUNT = 0 - """ def __init__(self): super(FdbClear,self).__init__() From 8cfb715d9f008210f890e99fb5fb5c4df70bb91e Mon Sep 17 00:00:00 2001 From: keboliu Date: Wed, 24 Jan 2018 12:24:57 +0200 Subject: [PATCH 3/3] publish the fdb flush msg via APPL_DB instead of ASIC_DB --- scripts/fdbclear | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/fdbclear b/scripts/fdbclear index ea9c4178f9..a8100af2cb 100644 --- a/scripts/fdbclear +++ b/scripts/fdbclear @@ -25,13 +25,13 @@ class FdbClear(object): def __init__(self): super(FdbClear,self).__init__() self.db = SonicV2Connector(host="127.0.0.1") - self.db.connect(self.db.ASIC_DB) + 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('ASIC_DB','FLUSHFDBREQUEST', msg) + self.db.publish('APPL_DB','FLUSHFDBREQUEST', msg) return def main():