Skip to content

Commit

Permalink
[lldp]: Add verification IPv4 address on LLDP conf Jinja2 Template (#…
Browse files Browse the repository at this point in the history
…5699)

Fix #5812

LLDP conf Jinja2 Template does not verify IPv4 address and can use IPv6 version. This issue does not effect control LLDP daemon. Issue can be reproduced via `test_snmp_lldp` test. LLDP conf Jinja2 Template selects first item from the list of mgmt interfaces.

TESTBED_1 LLDP conf

```
# cat /etc/lldpd.conf 
configure ports eth0 lldp portidsubtype local eth0
configure system ip management pattern FC00:3::32
configure system hostname dut-1
```
TESTBED_2  LLDP conf

```
# cat /etc/lldpd.conf
configure ports eth0 lldp portidsubtype local eth0
configure system ip management pattern 10.22.24.61
configure system hostname dut-2
```
TESTBED_1  MGMT_INTERFACE

```
$ redis-cli -n 4 keys "*" | grep MGMT_INTERFACE
MGMT_INTERFACE|eth0|10.22.24.53/23
MGMT_INTERFACE|eth0|FC00:3::32/64
```
TESTBED_2  MGMT_INTERFACE

```
$ redis-cli -n 4 keys "*" | grep MGMT_INTERFACE
MGMT_INTERFACE|eth0|FC00:3::32/64
MGMT_INTERFACE|eth0|10.22.24.61/23

```

Signed-off-by: Petro Bratash <petrox.bratash@intel.com>
  • Loading branch information
bratashX committed Nov 7, 2020
1 parent e6796da commit 32a832a
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 8 deletions.
19 changes: 13 additions & 6 deletions dockers/docker-lldp/lldpd.conf.j2
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
{% set mgmt_if = {} %}
{% if MGMT_INTERFACE %}
{% for (mgmt_name, mgmt_prefix) in MGMT_INTERFACE|pfx_filter %}
{% if mgmt_prefix|ipv4 %}
{% if mgmt_if.update({'port_name' : mgmt_name}) %} {% endif %}
{% if mgmt_if.update({'ipv4' : mgmt_prefix|ip}) %} {% endif %}
{% endif %}
{% endfor %}
{% endif %}
{% if mgmt_if %}
{# If MGMT port alias is available, use it for port ID subtype, otherwise use port name #}
{% set mgmt_port_name = (MGMT_INTERFACE.keys()|list)[0][0] %}
{% set ipv4 = (MGMT_INTERFACE.keys()|list)[0][1].split('/')[0] %}
{% if MGMT_PORT and MGMT_PORT[mgmt_port_name] and MGMT_PORT[mgmt_port_name].alias %}
configure ports eth0 lldp portidsubtype local {{ MGMT_PORT[mgmt_port_name].alias }}
{% if MGMT_PORT and MGMT_PORT[mgmt_if.port_name] and MGMT_PORT[mgmt_if.port_name].alias %}
configure ports eth0 lldp portidsubtype local {{ MGMT_PORT[mgmt_if.port_name].alias }}
{% else %}
configure ports eth0 lldp portidsubtype local {{ mgmt_port_name }}
configure ports eth0 lldp portidsubtype local {{ mgmt_if.port_name }}
{% endif %}
configure system ip management pattern {{ ipv4 }}
configure system ip management pattern {{ mgmt_if.ipv4 }}
{% endif %}
configure system hostname {{ DEVICE_METADATA['localhost']['hostname'] }}
{# pause lldpd operations until all interfaces are well configured, resume command will run in lldpmgrd #}
Expand Down
12 changes: 12 additions & 0 deletions src/sonic-config-engine/tests/data/lldp/mgmt_iface_ipv4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"DEVICE_METADATA": {
"localhost": {
"hostname": "switch-t0"
}
},
"MGMT_INTERFACE": {
"eth0|10.0.0.100/24": {
"gwaddr": "10.0.0.100"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"DEVICE_METADATA": {
"localhost": {
"hostname": "switch-t0"
}
},
"MGMT_INTERFACE": {
"eth0|10.0.0.100/24": {
"gwaddr": "10.0.0.100"
},
"eth0|2603:10e2:0:2902::8/64": {
"gwaddr": "2603:10e2:0:2902::8"
}
}
}
12 changes: 12 additions & 0 deletions src/sonic-config-engine/tests/data/lldp/mgmt_iface_ipv6.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"DEVICE_METADATA": {
"localhost": {
"hostname": "switch-t0"
}
},
"MGMT_INTERFACE": {
"eth0|2603:10e2:0:2902::8/64": {
"gwaddr": "2603:10e2:0:2902::8"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
configure system hostname switch-t0
pause
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
configure system hostname switch-t0
pause
23 changes: 21 additions & 2 deletions src/sonic-config-engine/tests/test_j2files.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,28 @@ def test_dhcp_relay(self):

def test_lldp(self):
lldpd_conf_template = os.path.join(self.test_dir, '..', '..', '..', 'dockers', 'docker-lldp', 'lldpd.conf.j2')
argument = '-m ' + self.t0_minigraph + ' -p ' + self.t0_port_config + ' -t ' + lldpd_conf_template + ' > ' + self.output_file

expected_mgmt_ipv4 = os.path.join(self.test_dir, 'sample_output', utils.PYvX_DIR, 'lldp_conf', 'lldpd-ipv4-iface.conf')
expected_mgmt_ipv6 = os.path.join(self.test_dir, 'sample_output', utils.PYvX_DIR, 'lldp_conf', 'lldpd-ipv6-iface.conf')
expected_mgmt_ipv4_and_ipv6 = expected_mgmt_ipv4

# Test generation of lldpd.conf if IPv4 and IPv6 management interfaces exist
mgmt_iface_ipv4_and_ipv6_json = os.path.join(self.test_dir, "data", "lldp", "mgmt_iface_ipv4_and_ipv6.json")
argument = '-j {} -t {} > {}'.format(mgmt_iface_ipv4_and_ipv6_json, lldpd_conf_template, self.output_file)
self.run_script(argument)
self.assertTrue(filecmp.cmp(expected_mgmt_ipv4_and_ipv6, self.output_file))

# Test generation of lldpd.conf if management interface IPv4 only exist
mgmt_iface_ipv4_json = os.path.join(self.test_dir, "data", "lldp", "mgmt_iface_ipv4.json")
argument = '-j {} -t {} > {}'.format(mgmt_iface_ipv4_json, lldpd_conf_template, self.output_file)
self.run_script(argument)
self.assertTrue(filecmp.cmp(expected_mgmt_ipv4, self.output_file))

# Test generation of lldpd.conf if Management interface IPv6 only exist
mgmt_iface_ipv6_json = os.path.join(self.test_dir, "data", "lldp", "mgmt_iface_ipv6.json")
argument = '-j {} -t {} > {}'.format(mgmt_iface_ipv6_json, lldpd_conf_template, self.output_file)
self.run_script(argument)
self.assertTrue(filecmp.cmp(os.path.join(self.test_dir, 'sample_output', utils.PYvX_DIR, 'lldpd.conf'), self.output_file))
self.assertTrue(filecmp.cmp(expected_mgmt_ipv6, self.output_file))

def test_bgpd_quagga(self):
conf_template = os.path.join(self.test_dir, '..', '..', '..', 'dockers', 'docker-fpm-quagga', 'bgpd.conf.j2')
Expand Down

0 comments on commit 32a832a

Please sign in to comment.