Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display large number of FDB entries fast from state_db and appl_db #1273

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 51 additions & 7 deletions scripts/fdbshow
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#!/usr/bin/env python3

"""
Script to show MAC/FDB entries learnt in Hardware
Script to show MAC/FDB entries learnt in Hardware and configured static MAC/FDB entries

usage: fdbshow [-p PORT] [-v VLAN]
usage: fdbshow [-p PORT] [-v VLAN] [-f]
optional arguments:
-p, --port FDB learned on specific port: Ethernet0
-v, --vlan FDB learned on specific Vlan: 1000

-f, --fast Display large number of FDB entries fast

Example of the output:
admin@str~$ fdbshow
No. Vlan MacAddress Port Type
Expand All @@ -24,6 +25,12 @@
Total number of entries 1
admin@str:~$ fdbshow -v 1001
1001 is not in list
admin@str:~$ fdbshow -f
No. Vlan MacAddress Port Type
----- ------ ----------------- --------- -------
1 1000 7C:FE:90:80:9F:01 Ethernet4 Dynamic
2 1000 7C:FE:90:80:9F:02 Ethernet8 Static
Total number of entries 2

"""
import argparse
Expand Down Expand Up @@ -62,14 +69,19 @@ class FdbShow(object):

HEADER = ['No.', 'Vlan', 'MacAddress', 'Port', 'Type']
FDB_COUNT = 0
fast = False

def __init__(self):
def __init__(self, fast):
super(FdbShow,self).__init__()
self.db = SonicV2Connector(host="127.0.0.1")
self.if_name_map, \
self.if_oid_map = port_util.get_interface_oid_map(self.db)
self.if_br_oid_map = port_util.get_bridge_port_map(self.db)
self.fetch_fdb_data()
self.fast = fast
if self.fast:
self.fast_fetch_fdb_data()
else:
self.fetch_fdb_data()
return

def fetch_fdb_data(self):
Expand Down Expand Up @@ -131,7 +143,38 @@ class FdbShow(object):
self.bridge_mac_list.sort(key = lambda x: x[0])
return


def fast_fetch_fdb_data(self):
"""
Fetch dynamic FDB entries from STATE DB.
"""
self.db.connect(self.db.STATE_DB)
self.bridge_mac_list = []
fdb_keys = self.db.keys(self.db.STATE_DB, "FDB_TABLE*")
if fdb_keys:
for key in natsorted(fdb_keys):
keyStr = key.split("|", -1)
vlanmac = keyStr[1].split(":", 1)
vlan_id = vlanmac[0].split("n")[1]
mac = vlanmac[1]
port = self.db.get(self.db.STATE_DB, key, "port")
type = self.db.get(self.db.STATE_DB, key, "type")
self.bridge_mac_list.append((int(vlan_id),) + (mac,) + (port,) + (type,))
"""
Fetch static FDB entries from APP DB.
"""
self.db.connect(self.db.APPL_DB)
fdb_keys = self.db.keys(self.db.APPL_DB, "FDB_TABLE*")
if fdb_keys:
for key in natsorted(fdb_keys):
keyStr = key.split(":", 1)
vlanmac = keyStr[1].split(":", 1)
vlan_id = vlanmac[0].split("n")[1]
mac = vlanmac[1]
port = self.db.get(self.db.APPL_DB, key, "port")
type = self.db.get(self.db.APPL_DB, key, "type")
if type == "static":
self.bridge_mac_list.append((int(vlan_id),) + (mac,) + (port,) + (type,))

def get_iter_index(self, key_value, pos=0):
"""
Get the starting index of matched entry
Expand Down Expand Up @@ -174,10 +217,11 @@ def main():
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('-p', '--port', type=str, help='FDB learned on specific port: Ethernet0', default=None)
parser.add_argument('-v', '--vlan', type=str, help='FDB learned on specific Vlan: 1001', default=None)
parser.add_argument('-f', '--fast', action='store_true', help='Display large number of FDB entries fast')
args = parser.parse_args()

try:
fdb = FdbShow()
fdb = FdbShow(args.fast)
fdb.display(args.vlan, args.port)
except Exception as e:
print(str(e))
Expand Down