-
Notifications
You must be signed in to change notification settings - Fork 661
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
CLI support for Layer 2 MAC/FDB show #106
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
83a6a85
CLI support to show MAC/FDB entries learnt in Hardware
prsunny 0626d7d
Minor fix
prsunny 76b493d
Review comments, some restructuring
prsunny 7e32eee
Exception is caught in main function
prsunny 9ec32a2
Fix for a case where -p and -v are same
prsunny 25f704d
Updated to use common library API to get bridge port mapping
prsunny 3bf4b8a
Add install dependency for swsssdk
prsunny 592e17b
Rearranged imports
prsunny 67b52a2
Addressed review comments, check-logic modified
prsunny 96356c5
Merge remote-tracking branch 'upstream/master'
prsunny 8edf4e7
Show command hookup added
prsunny 508fb78
Added check for None type and remove try-catch
prsunny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
#!/usr/bin/python | ||
""" | ||
Script to show MAC/FDB entries learnt in Hardware | ||
|
||
usage: fdbshow [-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: | ||
admin@str~$ fdbshow | ||
No. Vlan MacAddress Port | ||
----- ------ ----------------- ---------- | ||
1 1000 7C:FE:90:80:9F:05 Ethernet20 | ||
2 1000 7C:FE:90:80:9F:10 Ethernet40 | ||
3 1000 7C:FE:90:80:9F:01 Ethernet4 | ||
4 1000 7C:FE:90:80:9F:02 Ethernet8 | ||
Total number of entries 4 | ||
admin@str:~$ fdbshow -p Ethernet4 | ||
No. Vlan MacAddress Port | ||
----- ------ ----------------- --------- | ||
1 1000 7C:FE:90:80:9F:01 Ethernet4 | ||
Total number of entries 1 | ||
admin@str:~$ fdbshow -v 1001 | ||
1001 is not in list | ||
|
||
""" | ||
import argparse | ||
import json | ||
import sys | ||
|
||
from natsort import natsorted | ||
from swsssdk import SonicV2Connector, port_util | ||
from tabulate import tabulate | ||
|
||
class FdbShow(object): | ||
|
||
HEADER = ['No.', 'Vlan', 'MacAddress', 'Port'] | ||
FDB_COUNT = 0 | ||
|
||
def __init__(self): | ||
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() | ||
return | ||
|
||
def fetch_fdb_data(self): | ||
""" | ||
Fetch FDB entries from ASIC DB. | ||
FDB entries are sorted on "VlanID" and stored as a list of tuples | ||
""" | ||
self.db.connect(self.db.ASIC_DB) | ||
self.bridge_mac_list = [] | ||
|
||
fdb_str = self.db.keys('ASIC_DB', "ASIC_STATE:SAI_OBJECT_TYPE_FDB_ENTRY:*") | ||
if not fdb_str: | ||
return | ||
|
||
if self.if_br_oid_map is None: | ||
return | ||
|
||
oid_pfx = len("oid:0x") | ||
for s in fdb_str: | ||
fdb_entry = s.decode() | ||
fdb = json.loads(fdb_entry .split(":", 2)[-1]) | ||
if not fdb: | ||
continue | ||
|
||
ent = self.db.get_all('ASIC_DB', s, blocking=True) | ||
br_port_id = ent[b"SAI_FDB_ENTRY_ATTR_BRIDGE_PORT_ID"][oid_pfx:] | ||
port_id = self.if_br_oid_map[br_port_id] | ||
if_name = self.if_oid_map[port_id] | ||
|
||
self.bridge_mac_list.append((int(fdb["vlan"]),) + (fdb["mac"],) + (if_name,)) | ||
|
||
self.bridge_mac_list.sort(key = lambda x: x[0]) | ||
return | ||
|
||
|
||
def get_iter_index(self, key_value=0, pos=0): | ||
""" | ||
Get the starting index of matched entry | ||
""" | ||
if pos != 0: | ||
self.bridge_mac_list = natsorted(self.bridge_mac_list, key = lambda x: x[pos]) | ||
|
||
if key_value == 0: | ||
return 0 | ||
|
||
keys = [r[pos] for r in self.bridge_mac_list] | ||
return keys.index(key_value) | ||
|
||
|
||
def display(self, vlan, port): | ||
""" | ||
Display the FDB entries for specified vlan/port. | ||
@todo: - PortChannel support | ||
""" | ||
output = [] | ||
|
||
if vlan is not None: | ||
vlan = int(vlan) | ||
s_index = self.get_iter_index(vlan) | ||
self.bridge_mac_list = [fdb for fdb in self.bridge_mac_list[s_index:] | ||
if fdb[0] == vlan] | ||
if port is not None: | ||
s_index = self.get_iter_index(port, 2) | ||
self.bridge_mac_list = [fdb for fdb in self.bridge_mac_list[s_index:] | ||
if fdb[2] == port] | ||
|
||
for fdb in self.bridge_mac_list: | ||
self.FDB_COUNT += 1 | ||
output.append([self.FDB_COUNT, fdb[0], fdb[1], fdb[2]]) | ||
|
||
print tabulate(output, self.HEADER) | ||
print "Total number of entries {0} ".format(self.FDB_COUNT) | ||
|
||
|
||
def main(): | ||
|
||
parser = argparse.ArgumentParser(description='Display ASIC FDB entries', | ||
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) | ||
args = parser.parse_args() | ||
|
||
try: | ||
fdb = FdbShow() | ||
fdb.display(args.vlan, args.port) | ||
except Exception as e: | ||
print e.message | ||
sys.exit(1) | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure about other vendors' CLI. Is it more accurate to call it 'show fdb'? #Closed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lguohan , can you suggest?