Skip to content

Commit

Permalink
[sonic-clear] add a clear fdb command (sonic-net#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
keboliu authored and lguohan committed Jan 26, 2018
1 parent 620a15c commit 5357d88
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
30 changes: 30 additions & 0 deletions clear/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
58 changes: 58 additions & 0 deletions scripts/fdbclear
Original file line number Diff line number Diff line change
@@ -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()
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def get_test_suite():
'scripts/ecnconfig',
'scripts/fast-reboot',
'scripts/fast-reboot-dump.py',
'scripts/fdbclear',
'scripts/fdbshow',
'scripts/generate_dump',
'scripts/intfutil',
Expand Down

0 comments on commit 5357d88

Please sign in to comment.