From 50ded8ba751d356321c033888d81e10693d45efc Mon Sep 17 00:00:00 2001 From: Mykola Faryma Date: Fri, 14 Dec 2018 16:58:11 +0200 Subject: [PATCH 1/5] [lldpmgr] add mgmt ip to lldpd conf templte, handle port description/alias config Signed-off-by: Mykola Faryma --- dockers/docker-lldp-sv2/lldpd.conf.j2 | 2 ++ dockers/docker-lldp-sv2/lldpmgrd | 51 ++++++++++++--------------- 2 files changed, 25 insertions(+), 28 deletions(-) diff --git a/dockers/docker-lldp-sv2/lldpd.conf.j2 b/dockers/docker-lldp-sv2/lldpd.conf.j2 index 94a04596699f..5783c9eb28a1 100644 --- a/dockers/docker-lldp-sv2/lldpd.conf.j2 +++ b/dockers/docker-lldp-sv2/lldpd.conf.j2 @@ -1,3 +1,5 @@ {% if MGMT_INTERFACE %} +{% set ipv4 = MGMT_INTERFACE.keys()[0][1].split('/')[0] %} configure ports eth0 lldp portidsubtype local {{ MGMT_INTERFACE.keys()[0][0] }} +configure system ip management pattern {{ ipv4 }} {% endif %} diff --git a/dockers/docker-lldp-sv2/lldpmgrd b/dockers/docker-lldp-sv2/lldpmgrd index a044d5ed9d77..210bf0703410 100755 --- a/dockers/docker-lldp-sv2/lldpmgrd +++ b/dockers/docker-lldp-sv2/lldpmgrd @@ -119,9 +119,8 @@ class LldpManager(object): def generate_pending_lldp_config_cmd_for_port(self, port_name): """ - For port `port_name`, look up the neighboring device's hostname and - corresponding port alias in the Config database, then form the - appropriate lldpcli configuration command and run it. + For port `port_name`, look up the description and alias in the Config database, + then form the appropriate lldpcli configuration command and run it. """ # Retrieve all entires for this port from the Port table port_table = swsscommon.Table(self.config_db, swsscommon.CFG_PORT_TABLE_NAME) @@ -135,34 +134,21 @@ class LldpManager(object): if not port_alias: log_info("Unable to retrieve port alias for port '{}'. Using port name instead.".format(port_name)) port_alias = port_name + + # Get the port description. If None or empty string, we'll skip this configuration + port_desc = port_table_dict.get("description") + else: log_error("Port '{}' not found in {} table in Config DB. Using port name instead of port alias.".format(port_name, swsscommon.CFG_PORT_TABLE_NAME)) port_alias = port_name lldpcli_cmd = "lldpcli configure ports {0} lldp portidsubtype local {1}".format(port_name, port_alias) - # Retrieve all entires for this port from the Device Neighbor table - device_neighbor_table = swsscommon.Table(self.config_db, swsscommon.CFG_DEVICE_NEIGHBOR_TABLE_NAME) - (status, fvp) = device_neighbor_table.get(port_name) - if status: - # Convert list of tuples to a dictionary - device_neighbor_table_dict = dict(fvp) - - # Get neighbor host name and port name - neighbor_hostname = device_neighbor_table_dict.get("name") - neighbor_portname = device_neighbor_table_dict.get("port") - - # If we sucessfully obtained the neighbor's host name and port name, append a port description to the command - if neighbor_hostname and neighbor_portname: - lldpcli_cmd += " description {0}:{1}".format(neighbor_hostname, neighbor_portname) - else: - if not neighbor_hostname: - log_info("Failed to retrieve neighbor host name for port '{}'. Not adding port description.".format(port_name)) - - if not neighbor_portname: - log_info("Failed to retrieve neighbor port name for port '{}'. Not adding port description.".format(port_name)) + # if there is a description available, also configure that + if port_desc: + lldpcli_cmd += " description {}".format(port_desc) else: - log_info("Unable to retrieve neighbor information for port '{}'. Not adding port description.".format(port_name)) + log_info("Unable to retrive description for port '{}'. Not adding port description") # Add the command to our dictionary of pending commands, overwriting any # previous pending command for this port @@ -200,9 +186,11 @@ class LldpManager(object): def run(self): """ - Infinite loop. Subscribes to notifications of changes in the PORT table - of the Redis State database. When we are notified of the creation of an - interface, update LLDP configuration accordingly. + Subscribes to notifications of changes in the PORT table + of the Redis State database. + Subscribe to STATE_DB - get notified of the creation of an interface + Subscribe to CONFIG_DB - get notified of port config changes + Update LLDP configuration accordingly. """ # Set select timeout to 10 seconds SELECT_TIMEOUT_MS = 1000 * 10 @@ -211,8 +199,10 @@ class LldpManager(object): sel = swsscommon.Select() sst = swsscommon.SubscriberStateTable(self.state_db, swsscommon.STATE_PORT_TABLE_NAME) sel.addSelectable(sst) + sst = swsscommon.SubscriberStateTable(self.config_db, swsscommon.CFG_PORT_TABLE_NAME) + sel.addSelectable(sst) - # Listen indefinitely for changes to the PORT table in the State DB + # Listen for changes to the PORT table in the STATE_DB and CONFIG_DB while True: (state, c) = sel.select(SELECT_TIMEOUT_MS) @@ -221,9 +211,14 @@ class LldpManager(object): fvp_dict = dict(fvp) + # handle on creation if op == "SET" and fvp_dict.get("state") == "ok": self.generate_pending_lldp_config_cmd_for_port(key) + # handle on config change + if op in ["SET", "DEL"] and (fvp_dict.get("alias") or fvp_dict.get("description")) : + self.generate_pending_lldp_config_cmd_for_port(key) + # Process all pending commands self.process_pending_cmds() From 612ee6c2343b52378aa65b43820f7f32b575676f Mon Sep 17 00:00:00 2001 From: Mykola F <37578614+mykolaf@users.noreply.github.com> Date: Wed, 19 Dec 2018 17:22:29 +0200 Subject: [PATCH 2/5] fix typo --- dockers/docker-lldp-sv2/lldpmgrd | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dockers/docker-lldp-sv2/lldpmgrd b/dockers/docker-lldp-sv2/lldpmgrd index 210bf0703410..caf431f281e6 100755 --- a/dockers/docker-lldp-sv2/lldpmgrd +++ b/dockers/docker-lldp-sv2/lldpmgrd @@ -148,7 +148,7 @@ class LldpManager(object): if port_desc: lldpcli_cmd += " description {}".format(port_desc) else: - log_info("Unable to retrive description for port '{}'. Not adding port description") + log_info("Unable to retrieve description for port '{}'. Not adding port description") # Add the command to our dictionary of pending commands, overwriting any # previous pending command for this port @@ -211,11 +211,11 @@ class LldpManager(object): fvp_dict = dict(fvp) - # handle on creation + # handle creation if op == "SET" and fvp_dict.get("state") == "ok": self.generate_pending_lldp_config_cmd_for_port(key) - # handle on config change + # handle config change if op in ["SET", "DEL"] and (fvp_dict.get("alias") or fvp_dict.get("description")) : self.generate_pending_lldp_config_cmd_for_port(key) From 78052a3a7b11ac94213c02120681cdb323b1eb0b Mon Sep 17 00:00:00 2001 From: Mykola Faryma Date: Fri, 21 Dec 2018 17:54:01 +0200 Subject: [PATCH 3/5] [config-engine] update test sample output lldpd.conf Signed-off-by: Mykola Faryma --- src/sonic-config-engine/tests/sample_output/lldpd.conf | 1 + 1 file changed, 1 insertion(+) diff --git a/src/sonic-config-engine/tests/sample_output/lldpd.conf b/src/sonic-config-engine/tests/sample_output/lldpd.conf index 29739cb890d0..6320502ff88e 100644 --- a/src/sonic-config-engine/tests/sample_output/lldpd.conf +++ b/src/sonic-config-engine/tests/sample_output/lldpd.conf @@ -1,2 +1,3 @@ configure ports eth0 lldp portidsubtype local eth0 +configure system ip management pattern 10.0.0.100 From 29817073e2ea9888a341f81eaff8a554c419c8cc Mon Sep 17 00:00:00 2001 From: Mykola F <37578614+mykolaf@users.noreply.github.com> Date: Thu, 24 Jan 2019 15:37:16 +0200 Subject: [PATCH 4/5] fix the log message --- dockers/docker-lldp-sv2/lldpmgrd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dockers/docker-lldp-sv2/lldpmgrd b/dockers/docker-lldp-sv2/lldpmgrd index caf431f281e6..769bb3f2956b 100755 --- a/dockers/docker-lldp-sv2/lldpmgrd +++ b/dockers/docker-lldp-sv2/lldpmgrd @@ -148,7 +148,7 @@ class LldpManager(object): if port_desc: lldpcli_cmd += " description {}".format(port_desc) else: - log_info("Unable to retrieve description for port '{}'. Not adding port description") + log_info("Unable to retrieve description for port '{}'. Not adding port description".format(port_name)) # Add the command to our dictionary of pending commands, overwriting any # previous pending command for this port From 58c1396e1869d9e253a915efdea9deeca8f8cec2 Mon Sep 17 00:00:00 2001 From: Mykola F <37578614+mykolaf@users.noreply.github.com> Date: Thu, 24 Jan 2019 16:03:17 +0200 Subject: [PATCH 5/5] fix lldpd.conf.j2 --- dockers/docker-lldp-sv2/lldpd.conf.j2 | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/dockers/docker-lldp-sv2/lldpd.conf.j2 b/dockers/docker-lldp-sv2/lldpd.conf.j2 index 9be02982b3b2..4a6e3b578bb2 100644 --- a/dockers/docker-lldp-sv2/lldpd.conf.j2 +++ b/dockers/docker-lldp-sv2/lldpd.conf.j2 @@ -1,14 +1,11 @@ {% if MGMT_INTERFACE %} -{% set ipv4 = MGMT_INTERFACE.keys()[0][1].split('/')[0] %} -configure ports eth0 lldp portidsubtype local {{ MGMT_INTERFACE.keys()[0][0] }} -configure system ip management pattern {{ ipv4 }} {# If MGMT port alias is available, use it for port ID subtype, otherwise use port name #} {% set mgmt_port_name = MGMT_INTERFACE.keys()[0][0] %} +{% set ipv4 = MGMT_INTERFACE.keys()[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 }} -{% set ipv4 = MGMT_INTERFACE.keys()[0][1].split('/')[0] %} -configure system ip management pattern {{ ipv4 }} {% else %} configure ports eth0 lldp portidsubtype local {{ mgmt_port_name }} {% endif %} +configure system ip management pattern {{ ipv4 }} {% endif %}