Skip to content

Commit 30e4654

Browse files
authored
Add show command for BFD sessions (sonic-net#1942)
What I did Add show command for BFD sessions. How I did it Add show command for BFD session and unit tests. How to verify it Verify the show command locally and unit test passes.
1 parent e63f47e commit 30e4654

File tree

3 files changed

+200
-0
lines changed

3 files changed

+200
-0
lines changed

doc/Command-Reference.md

+41
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
* [ARP & NDP](#arp--ndp)
2929
* [ARP show commands](#arp-show-commands)
3030
* [NDP show commands](#ndp-show-commands)
31+
* [BFD](#bfd)
32+
* [BFD show commands](#bfd-show-commands)
3133
* [BGP](#bgp)
3234
* [BGP show commands](#bgp-show-commands)
3335
* [BGP config commands](#bgp-config-commands)
@@ -1546,6 +1548,45 @@ This command displays either all the IPv6 neighbor mac addresses, or for a parti
15461548
15471549
Go Back To [Beginning of the document](#) or [Beginning of this section](#arp--ndp)
15481550
1551+
## BFD
1552+
1553+
### BFD show commands
1554+
1555+
**show bfd summary**
1556+
1557+
This command displays the state and key parameters of all BFD sessions.
1558+
1559+
- Usage:
1560+
```
1561+
show bgp summary
1562+
```
1563+
- Example:
1564+
```
1565+
>> show bfd summary
1566+
Total number of BFD sessions: 3
1567+
Peer Addr Interface Vrf State Type Local Addr TX Interval RX Interval Multiplier Multihop
1568+
----------- ----------- ------- ------- ------------ ------------ ------------- ------------- ------------ ----------
1569+
10.0.1.1 default default DOWN async_active 10.0.0.1 300 500 3 true
1570+
10.0.2.1 Ethernet12 default UP async_active 10.0.0.1 200 600 3 false
1571+
2000::10:1 default default UP async_active 2000::1 100 700 3 false
1572+
```
1573+
1574+
**show bfd peer**
1575+
1576+
This command displays the state and key parameters of all BFD sessions that match an IP address.
1577+
1578+
- Usage:
1579+
```
1580+
show bgp peer <peer-ip>
1581+
```
1582+
- Example:
1583+
```
1584+
>> show bfd peer 10.0.1.1
1585+
Total number of BFD sessions for peer IP 10.0.1.1: 1
1586+
Peer Addr Interface Vrf State Type Local Addr TX Interval RX Interval Multiplier Multihop
1587+
----------- ----------- ------- ------- ------------ ------------ ------------- ------------- ------------ ----------
1588+
10.0.1.1 default default DOWN async_active 10.0.0.1 300 500 3 true
1589+
```
15491590
15501591
## BGP
15511592

show/main.py

+61
Original file line numberDiff line numberDiff line change
@@ -1701,6 +1701,67 @@ def ztp(status, verbose):
17011701
cmd = cmd + " --verbose"
17021702
run_command(cmd, display_cmd=verbose)
17031703

1704+
1705+
#
1706+
# 'bfd' group ("show bfd ...")
1707+
#
1708+
@cli.group(cls=clicommon.AliasedGroup)
1709+
def bfd():
1710+
"""Show details of the bfd sessions"""
1711+
pass
1712+
1713+
# 'summary' subcommand ("show bfd summary")
1714+
@bfd.command()
1715+
@clicommon.pass_db
1716+
def summary(db):
1717+
"""Show bfd session information"""
1718+
bfd_headers = ["Peer Addr", "Interface", "Vrf", "State", "Type", "Local Addr",
1719+
"TX Interval", "RX Interval", "Multiplier", "Multihop"]
1720+
1721+
bfd_keys = db.db.keys(db.db.STATE_DB, "BFD_SESSION_TABLE|*")
1722+
1723+
click.echo("Total number of BFD sessions: {}".format(0 if bfd_keys is None else len(bfd_keys)))
1724+
1725+
bfd_body = []
1726+
if bfd_keys is not None:
1727+
for key in bfd_keys:
1728+
key_values = key.split('|')
1729+
values = db.db.get_all(db.db.STATE_DB, key)
1730+
bfd_body.append([key_values[3], key_values[2], key_values[1], values["state"], values["type"], values["local_addr"],
1731+
values["tx_interval"], values["rx_interval"], values["multiplier"], values["multihop"]])
1732+
1733+
click.echo(tabulate(bfd_body, bfd_headers))
1734+
1735+
1736+
# 'peer' subcommand ("show bfd peer ...")
1737+
@bfd.command()
1738+
@clicommon.pass_db
1739+
@click.argument('peer_ip', required=True)
1740+
def peer(db, peer_ip):
1741+
"""Show bfd session information for BFD peer"""
1742+
bfd_headers = ["Peer Addr", "Interface", "Vrf", "State", "Type", "Local Addr",
1743+
"TX Interval", "RX Interval", "Multiplier", "Multihop"]
1744+
1745+
bfd_keys = db.db.keys(db.db.STATE_DB, "BFD_SESSION_TABLE|*|{}".format(peer_ip))
1746+
delimiter = db.db.get_db_separator(db.db.STATE_DB)
1747+
1748+
if bfd_keys is None or len(bfd_keys) == 0:
1749+
click.echo("No BFD sessions found for peer IP {}".format(peer_ip))
1750+
return
1751+
1752+
click.echo("Total number of BFD sessions for peer IP {}: {}".format(peer_ip, len(bfd_keys)))
1753+
1754+
bfd_body = []
1755+
if bfd_keys is not None:
1756+
for key in bfd_keys:
1757+
key_values = key.split(delimiter)
1758+
values = db.db.get_all(db.db.STATE_DB, key)
1759+
bfd_body.append([key_values[3], key_values[2], key_values[1], values.get("state"), values.get("type"), values.get("local_addr"),
1760+
values.get("tx_interval"), values.get("rx_interval"), values.get("multiplier"), values.get("multihop")])
1761+
1762+
click.echo(tabulate(bfd_body, bfd_headers))
1763+
1764+
17041765
# Load plugins and register them
17051766
helper = util_base.UtilHelper()
17061767
helper.load_and_register_plugins(plugins, cli)

tests/show_bfd_test.py

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import os
2+
import sys
3+
from click.testing import CliRunner
4+
from swsscommon.swsscommon import SonicV2Connector
5+
from utilities_common.db import Db
6+
7+
import show.main as show
8+
9+
test_path = os.path.dirname(os.path.abspath(__file__))
10+
mock_db_path = os.path.join(test_path, "bfd_input")
11+
12+
class TestShowBfd(object):
13+
@classmethod
14+
def setup_class(cls):
15+
print("SETUP")
16+
os.environ["UTILITIES_UNIT_TESTING"] = "1"
17+
18+
def set_db_values(self, db, key, kvs):
19+
for field, value in kvs.items():
20+
db.set(db.STATE_DB, key, field, value)
21+
22+
def test_bfd_show(self):
23+
runner = CliRunner()
24+
db = Db()
25+
dbconnector = db.db
26+
27+
self.set_db_values(dbconnector, "BFD_SESSION_TABLE|default|default|10.0.1.1",
28+
{"state": "DOWN", "type": "async_active", "local_addr" : "10.0.0.1",
29+
"tx_interval" :"300", "rx_interval" : "500", "multiplier" : "3", "multihop": "true"})
30+
self.set_db_values(dbconnector, "BFD_SESSION_TABLE|default|Ethernet12|10.0.2.1",
31+
{"state": "UP", "type": "async_active", "local_addr" : "10.0.0.1",
32+
"tx_interval" :"200", "rx_interval" : "600", "multiplier" : "3", "multihop": "false"})
33+
self.set_db_values(dbconnector, "BFD_SESSION_TABLE|default|default|2000::10:1",
34+
{"state": "UP", "type": "async_active", "local_addr" : "2000::1",
35+
"tx_interval" :"100", "rx_interval" : "700", "multiplier" : "3", "multihop": "false"})
36+
self.set_db_values(dbconnector, "BFD_SESSION_TABLE|VrfRed|default|10.0.1.1",
37+
{"state": "UP", "type": "async_active", "local_addr" : "10.0.0.1",
38+
"tx_interval" :"400", "rx_interval" : "500", "multiplier" : "5", "multihop": "false"})
39+
40+
expected_output = """\
41+
Total number of BFD sessions: 4
42+
Peer Addr Interface Vrf State Type Local Addr TX Interval RX Interval Multiplier Multihop
43+
----------- ----------- ------- ------- ------------ ------------ ------------- ------------- ------------ ----------
44+
10.0.1.1 default default DOWN async_active 10.0.0.1 300 500 3 true
45+
10.0.2.1 Ethernet12 default UP async_active 10.0.0.1 200 600 3 false
46+
2000::10:1 default default UP async_active 2000::1 100 700 3 false
47+
10.0.1.1 default VrfRed UP async_active 10.0.0.1 400 500 5 false
48+
"""
49+
50+
result = runner.invoke(show.cli.commands['bfd'].commands['summary'], [], obj=db)
51+
assert result.exit_code == 0
52+
assert result.output == expected_output
53+
54+
expected_output = """\
55+
Total number of BFD sessions for peer IP 10.0.1.1: 2
56+
Peer Addr Interface Vrf State Type Local Addr TX Interval RX Interval Multiplier Multihop
57+
----------- ----------- ------- ------- ------------ ------------ ------------- ------------- ------------ ----------
58+
10.0.1.1 default default DOWN async_active 10.0.0.1 300 500 3 true
59+
10.0.1.1 default VrfRed UP async_active 10.0.0.1 400 500 5 false
60+
"""
61+
62+
result = runner.invoke(show.cli.commands['bfd'].commands['peer'], ['10.0.1.1'], obj=db)
63+
assert result.exit_code == 0
64+
assert result.output == expected_output
65+
66+
expected_output = """\
67+
Total number of BFD sessions for peer IP 10.0.2.1: 1
68+
Peer Addr Interface Vrf State Type Local Addr TX Interval RX Interval Multiplier Multihop
69+
----------- ----------- ------- ------- ------------ ------------ ------------- ------------- ------------ ----------
70+
10.0.2.1 Ethernet12 default UP async_active 10.0.0.1 200 600 3 false
71+
"""
72+
73+
result = runner.invoke(show.cli.commands['bfd'].commands['peer'], ['10.0.2.1'], obj=db)
74+
assert result.exit_code == 0
75+
assert result.output == expected_output
76+
77+
expected_output = """\
78+
No BFD sessions found for peer IP 10.0.3.1
79+
"""
80+
81+
result = runner.invoke(show.cli.commands['bfd'].commands['peer'], ['10.0.3.1'], obj=db)
82+
assert result.exit_code == 0
83+
assert result.output == expected_output
84+
85+
86+
def test_bfd_show_no_session(self):
87+
runner = CliRunner()
88+
db = Db()
89+
90+
expected_output = """\
91+
Total number of BFD sessions: 0
92+
Peer Addr Interface Vrf State Type Local Addr TX Interval RX Interval Multiplier Multihop
93+
----------- ----------- ----- ------- ------ ------------ ------------- ------------- ------------ ----------
94+
"""
95+
96+
result = runner.invoke(show.cli.commands['bfd'].commands['summary'], [], obj=db)
97+
assert result.exit_code == 0
98+
assert result.output == expected_output

0 commit comments

Comments
 (0)