Skip to content

Commit 269c317

Browse files
authored
Mgmt vrf/SNMP agent validations and bug fixes (sonic-net#1289)
*Added ip address validation for add_snmp_agent_address in config/main.py *Added click choice for routes in show/main.py *Default kernel route via eth0 interface is still being show in the output 'show ip route' even after creating 'mgmt' VRF Added Fix for it .
1 parent 394b202 commit 269c317

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

config/main.py

+49-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import threading
1313
import time
1414

15+
from socket import AF_INET, AF_INET6
1516
from minigraph import parse_device_desc_xml
1617
from portconfig import get_child_ports
1718
from sonic_py_common import device_info, multi_asic
@@ -1814,6 +1815,21 @@ def vrf_add_management_vrf(config_db):
18141815
return None
18151816
config_db.mod_entry('MGMT_VRF_CONFIG', "vrf_global", {"mgmtVrfEnabled": "true"})
18161817
mvrf_restart_services()
1818+
"""
1819+
The regular expression for grep in below cmd is to match eth0 line in /proc/net/route, sample file:
1820+
$ cat /proc/net/route
1821+
Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
1822+
eth0 00000000 01803B0A 0003 0 0 202 00000000 0 0 0
1823+
"""
1824+
cmd = "cat /proc/net/route | grep -E \"eth0\s+00000000\s+[0-9A-Z]+\s+[0-9]+\s+[0-9]+\s+[0-9]+\s+202\" | wc -l"
1825+
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
1826+
output = proc.communicate()
1827+
if int(output[0]) >= 1:
1828+
cmd="ip -4 route del default dev eth0 metric 202"
1829+
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
1830+
proc.communicate()
1831+
if proc.returncode != 0:
1832+
click.echo("Could not delete eth0 route")
18171833

18181834
def vrf_delete_management_vrf(config_db):
18191835
"""Disable management vrf in config DB"""
@@ -1833,6 +1849,8 @@ def snmpagentaddress(ctx):
18331849
config_db.connect()
18341850
ctx.obj = {'db': config_db}
18351851

1852+
ip_family = {4: AF_INET, 6: AF_INET6}
1853+
18361854
@snmpagentaddress.command('add')
18371855
@click.argument('agentip', metavar='<SNMP AGENT LISTENING IP Address>', required=True)
18381856
@click.option('-p', '--port', help="SNMP AGENT LISTENING PORT")
@@ -1842,13 +1860,43 @@ def add_snmp_agent_address(ctx, agentip, port, vrf):
18421860
"""Add the SNMP agent listening IP:Port%Vrf configuration"""
18431861

18441862
#Construct SNMP_AGENT_ADDRESS_CONFIG table key in the format ip|<port>|<vrf>
1863+
if not clicommon.is_ipaddress(agentip):
1864+
click.echo("Invalid IP address")
1865+
return False
1866+
config_db = ctx.obj['db']
1867+
if not vrf:
1868+
entry = config_db.get_entry('MGMT_VRF_CONFIG', "vrf_global")
1869+
if entry and entry['mgmtVrfEnabled'] == 'true' :
1870+
click.echo("ManagementVRF is Enabled. Provide vrf.")
1871+
return False
1872+
found = 0
1873+
ip = ipaddress.ip_address(agentip)
1874+
for intf in netifaces.interfaces():
1875+
ipaddresses = netifaces.ifaddresses(intf)
1876+
if ip_family[ip.version] in ipaddresses:
1877+
for ipaddr in ipaddresses[ip_family[ip.version]]:
1878+
if agentip == ipaddr['addr']:
1879+
found = 1
1880+
break;
1881+
if found == 1:
1882+
break;
1883+
else:
1884+
click.echo("IP addfress is not available")
1885+
return
1886+
18451887
key = agentip+'|'
18461888
if port:
18471889
key = key+port
1890+
#snmpd does not start if we have two entries with same ip and port.
1891+
key1 = "SNMP_AGENT_ADDRESS_CONFIG|" + key + '*'
1892+
entry = config_db.get_keys(key1)
1893+
if entry:
1894+
ip_port = agentip + ":" + port
1895+
click.echo("entry with {} already exists ".format(ip_port))
1896+
return
18481897
key = key+'|'
18491898
if vrf:
18501899
key = key+vrf
1851-
config_db = ctx.obj['db']
18521900
config_db.set_entry('SNMP_AGENT_ADDRESS_CONFIG', key, {})
18531901

18541902
#Restarting the SNMP service will regenerate snmpd.conf and rerun snmpd

show/main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ def is_mgmt_vrf_enabled(ctx):
280280
#
281281

282282
@cli.group('mgmt-vrf', invoke_without_command=True)
283-
@click.argument('routes', required=False)
283+
@click.argument('routes', required=False, type=click.Choice(["routes"]))
284284
@click.pass_context
285285
def mgmt_vrf(ctx,routes):
286286
"""Show management VRF attributes"""

0 commit comments

Comments
 (0)