Skip to content

Commit 1437bf2

Browse files
authored
[202012] Add DHCPv6 Relay counter and ipv6 helper CLI (sonic-net#1917)
1 parent 09ebac5 commit 1437bf2

9 files changed

+300
-0
lines changed

clear/main.py

+14
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,20 @@ def dropcounters():
188188
"""Clear drop counters"""
189189
command = "dropstat -c clear"
190190
run_command(command)
191+
192+
@click.command()
193+
@click.argument('interface', required=False)
194+
def dhcp6relay_counters(interface):
195+
""" Clear dhcp6relay message counts """
196+
197+
if interface:
198+
cmd = "show dhcp6relay_counters clear -i " + interface
199+
else:
200+
cmd = "show dhcp6relay_counters clear"
201+
202+
run_command(cmd)
203+
204+
cli.add_command(dhcp6relay_counters)
191205

192206
#
193207
# 'clear watermarks

doc/Command-Reference.md

+79
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
* [Console clear commands](#console-clear-commands)
3939
* [DHCP Relay](#dhcp-relay)
4040
* [DHCP Relay config commands](#dhcp-relay-config-commands)
41+
* [DHCPv6 Relay show commands](#dhcpv6-relay-show-commands)
42+
* [DHCPv6 Relay show counter commands](#dhcpv6-relay-show-counter-commands)
43+
* [DHCPv6 Relay counter clear commands](#dhcpv6-relay-counter-clear-commands)
4144
* [Drop Counters](#drop-counters)
4245
* [Drop Counter show commands](#drop-counters-show-commands)
4346
* [Drop Counter config commands](#drop-counters-config-commands)
@@ -2205,6 +2208,82 @@ This command is used to delete a configured DHCP Relay Destination IP address fr
22052208
Restarting DHCP relay service...
22062209
```
22072210
2211+
### DHCPv6 Relay show commands
2212+
2213+
This sub-section of commands is used to show the DHCPv6 Relay Destination IPv6 address for VLAN interfaces.
2214+
2215+
**show dhcprelay_helper ipv6**
2216+
2217+
This command is used to show DHCPv6 Relay Destination IPv6 address to Vlans.
2218+
2219+
- Usage:
2220+
```
2221+
show dhcprelay_helper ipv6
2222+
```
2223+
2224+
- Example:
2225+
admin@sonic:~$ sudo show dhcprelay_helper ipv6
2226+
```
2227+
------- ------------
2228+
Vlan1000 fc02:2000::1
2229+
fc02:2000::2
2230+
fc02:2000::3
2231+
fc02:2000::4
2232+
------- ------------
2233+
```
2234+
2235+
### DHCPv6 Relay show counter commands
2236+
2237+
This sub-section of commands is used to show the DHCPv6 Relay counters for VLAN interfaces.
2238+
2239+
**show dhcp6relay_counters counts**
2240+
2241+
This command is used to show the DHCPv6 Relay counter to Vlans.
2242+
2243+
- Usage:
2244+
```
2245+
show dhcp6relay_counters counts
2246+
```
2247+
2248+
- Example:
2249+
```
2250+
admin@sonic:~$ sudo show dhcp6relay_counters counts
2251+
Message Type Vlan1000
2252+
-------------- ---------
2253+
Unknown 0
2254+
Solicit 0
2255+
Advertise 0
2256+
Request 0
2257+
Confirm 0
2258+
Renew 0
2259+
Rebind 0
2260+
Reply 0
2261+
Release 0
2262+
Decline 0
2263+
Relay-Forward 0
2264+
Relay-Reply 0
2265+
```
2266+
### DHCPv6 Relay counter clear commands
2267+
2268+
**sonic-clear dhcp6relay-counters**
2269+
2270+
This comnmand is used to clear DHCPv6 Relay Counters. This is done by clearing all counters or per-vlan basis.
2271+
2272+
- Usage:
2273+
```
2274+
sonic-clear dhcp6relay-counters
2275+
sonic-clear dhcp6relay-counters "interface"
2276+
```
2277+
2278+
- Example:
2279+
```
2280+
admin@sonic:~$ sonic-clear dhcp6relay-counters
2281+
Cleared DHCPv6 Relay Counters
2282+
2283+
admin@sonic:~$ sonic-clear dhcp6relay-counters Vlan1000
2284+
Cleared DHCPv6 Relay Counter Vlan1000
2285+
```
2286+
22082287
Go Back To [Beginning of the document](#) or [Beginning of this section](#dhcp-relay)
22092288
22102289

show/dhcp6relay_counters.py

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import click
2+
import utilities_common.cli as clicommon
3+
from tabulate import tabulate
4+
5+
from swsscommon.swsscommon import SonicV2Connector
6+
7+
8+
# STATE_DB Table
9+
DHCPv6_COUNTER_TABLE = 'DHCPv6_COUNTER_TABLE'
10+
11+
# DHCPv6 Counter Messages
12+
messages = ["Unknown", "Solicit", "Advertise", "Request", "Confirm", "Renew", "Rebind", "Reply", "Release", "Decline", "Relay-Forward", "Relay-Reply"]
13+
14+
class DHCPv6_Counter(object):
15+
def __init__(self):
16+
self.db = SonicV2Connector(use_unix_socket_path=False)
17+
self.db.connect(self.db.STATE_DB)
18+
self.table_name = DHCPv6_COUNTER_TABLE + self.db.get_db_separator(self.db.STATE_DB)
19+
20+
21+
def get_interface(self):
22+
""" Get all names of all interfaces in DHCPv6_COUNTER_TABLE """
23+
vlans = []
24+
for key in self.db.keys(self.db.STATE_DB):
25+
if DHCPv6_COUNTER_TABLE in key:
26+
vlans.append(key[21:])
27+
return vlans
28+
29+
30+
def get_dhcp6relay_msg_count(self, interface, msg):
31+
""" Get count of a dhcp6relay message """
32+
count = self.db.get(self.db.STATE_DB, self.table_name + str(interface), str(msg))
33+
data = [str(msg), count]
34+
return data
35+
36+
37+
def clear_table(self, interface):
38+
""" Reset all message counts to 0 """
39+
for msg in messages:
40+
self.db.set(self.db.STATE_DB, self.table_name + str(interface), str(msg), '0')
41+
42+
def print_count(counter, intf):
43+
"""Print count of each message"""
44+
data = []
45+
for i in messages:
46+
data.append(counter.get_dhcp6relay_msg_count(intf, i))
47+
print(tabulate(data, headers = ["Message Type", intf], tablefmt='simple', stralign='right') + "\n")
48+
49+
50+
#
51+
# 'dhcp6relay_counters' group ###
52+
#
53+
54+
55+
@click.group(cls=clicommon.AliasedGroup, name="dhcp6relay_counters")
56+
def dhcp6relay_counters():
57+
"""Show DHCPv6 counter"""
58+
pass
59+
60+
61+
# 'counts' subcommand ("show dhcp6relay_counters counts")
62+
@dhcp6relay_counters.command('counts')
63+
@click.option('-i', '--interface', required=False)
64+
@click.option('--verbose', is_flag=True, help="Enable verbose output")
65+
def counts(interface, verbose):
66+
"""Show dhcp6relay message counts"""
67+
68+
counter = DHCPv6_Counter()
69+
counter_intf = counter.get_interface()
70+
71+
if interface:
72+
print_count(counter, interface)
73+
else:
74+
for intf in counter_intf:
75+
print_count(counter, intf)
76+
77+
78+
# 'clear' subcommand ("clear dhcp6relay_counters counts")
79+
@dhcp6relay_counters.command('clear')
80+
@click.option('-i', '--interface', required=False)
81+
@click.option('--verbose', is_flag=True, help="Enable verbose output")
82+
def clear(interface, verbose):
83+
"""Clear dhcp6relay message counts"""
84+
85+
counter = DHCPv6_Counter()
86+
counter_intf = counter.get_interface()
87+
88+
if interface:
89+
counter.clear_table(interface)
90+
print("Cleared DHCPv6 Relay Counter " + interface)
91+
else:
92+
for intf in counter_intf:
93+
counter.clear_table(intf)
94+
print("Cleared DHCPv6 Relay Counters")
95+

show/dhcp_relay_helper.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import click
2+
from tabulate import tabulate
3+
from swsscommon.swsscommon import ConfigDBConnector
4+
5+
import utilities_common.cli as clicommon
6+
7+
DHCP_RELAY = 'DHCP_RELAY'
8+
config_db = ConfigDBConnector()
9+
10+
@click.group(cls=clicommon.AliasedGroup, name="dhcprelay_helper")
11+
def dhcp_relay_helper():
12+
"""Show DHCP_Relay helper information"""
13+
pass
14+
15+
@dhcp_relay_helper.command('ipv6')
16+
def get_dhcpv6_helper_address():
17+
"""Parse through DHCP_RELAY table for each interface in config_db.json and print dhcpv6 helpers in table format"""
18+
if config_db is not None:
19+
config_db.connect()
20+
table_data = config_db.get_table(DHCP_RELAY)
21+
if table_data is not None:
22+
vlans = config_db.get_keys(DHCP_RELAY)
23+
for vlan in vlans:
24+
output = get_data(table_data, vlan)
25+
print(output)
26+
27+
28+
def get_data(table_data, vlan):
29+
vlan_data = table_data.get(vlan)
30+
helpers_data = vlan_data.get('dhcpv6_servers')
31+
if helpers_data is not None:
32+
addr = {vlan:[]}
33+
for ip in helpers_data:
34+
addr[vlan].append(ip)
35+
output = tabulate({'Interface':[vlan], vlan:addr.get(vlan)}, tablefmt='simple', stralign='right') + '\n'
36+
return output
37+

show/main.py

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
from . import acl
2020
from . import bgp_common
2121
from . import chassis_modules
22+
from . import dhcp6relay_counters
23+
from . import dhcp_relay_helper
2224
from . import dropcounters
2325
from . import feature
2426
from . import fgnhg
@@ -156,6 +158,8 @@ def cli(ctx):
156158
# Add groups from other modules
157159
cli.add_command(acl.acl)
158160
cli.add_command(chassis_modules.chassis_modules)
161+
cli.add_command(dhcp_relay_helper.dhcp_relay_helper)
162+
cli.add_command(dhcp6relay_counters.dhcp6relay_counters)
159163
cli.add_command(dropcounters.dropcounters)
160164
cli.add_command(feature.feature)
161165
cli.add_command(fgnhg.fgnhg)

tests/dhcp6relay_counter_test.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import sys
2+
import os
3+
4+
import show.main as show
5+
from click.testing import CliRunner
6+
7+
expected_counts = """\
8+
Message Type Vlan1000
9+
-------------- ----------
10+
Unknown 0
11+
Solicit 0
12+
Advertise 0
13+
Request 0
14+
Confirm 0
15+
Renew 0
16+
Rebind 0
17+
Reply 0
18+
Release 0
19+
Decline 0
20+
Relay-Forward 0
21+
Relay-Reply 0
22+
23+
"""
24+
25+
class TestDhcp6RelayCounters(object):
26+
27+
def test_show_counts(self):
28+
runner = CliRunner()
29+
result = runner.invoke(show.cli.commands['dhcp6relay_counters'].commands["counts"], ["-i", "Vlan1000"])
30+
print(result.output)
31+
assert result.output == expected_counts
32+

tests/dhcp_relay_helper_test.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import sys
2+
import os
3+
4+
import show.main as show
5+
from click.testing import CliRunner
6+
7+
expected_table = """\
8+
-------- ------------
9+
Vlan1000 fc02:2000::1
10+
fc02:2000::2
11+
-------- ------------
12+
13+
"""
14+
15+
class TestDhcpRelayHelper(object):
16+
17+
def test_show_dhcpv6_helper(self):
18+
runner = CliRunner()
19+
result = runner.invoke(show.cli.commands["dhcprelay_helper"].commands["ipv6"])
20+
print(result.output)
21+
assert result.output == expected_table
22+

tests/mock_tables/config_db.json

+3
Original file line numberDiff line numberDiff line change
@@ -2399,5 +2399,8 @@
23992399
},
24002400
"QUEUE|Ethernet96|6": {
24012401
"scheduler": "[SCHEDULER|scheduler.0]"
2402+
},
2403+
"DHCP_RELAY|Vlan1000": {
2404+
"dhcpv6_servers@": "fc02:2000::1,fc02:2000::2"
24022405
}
24032406
}

tests/mock_tables/state_db.json

+14
Original file line numberDiff line numberDiff line change
@@ -621,5 +621,19 @@
621621
"linkmgrd_switch_standby_end": "2021-May-13 10:01:15.696728",
622622
"xcvrd_switch_standby_end": "2021-May-13 10:01:15.696051",
623623
"xcvrd_switch_standby_start": "2021-May-13 10:01:15.690835"
624+
},
625+
"DHCPv6_COUNTER_TABLE|Vlan1000": {
626+
"Unknown": "0",
627+
"Solicit": "0",
628+
"Advertise": "0",
629+
"Request": "0",
630+
"Confirm": "0",
631+
"Renew": "0",
632+
"Rebind": "0",
633+
"Reply": "0",
634+
"Release": "0",
635+
"Decline": "0",
636+
"Relay-Forward": "0",
637+
"Relay-Reply": "0"
624638
}
625639
}

0 commit comments

Comments
 (0)