From: Vincent Bernat Date: Thu, 9 Jun 2016 16:07:00 +0000 (+0200) Subject: interfaces: fix setting of local value for port ID X-Git-Tag: 0.9.4~21 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e1717397eb15c1b378093891aa8f9381ff440939;p=thirdparty%2Flldpd.git interfaces: fix setting of local value for port ID This was broken in a403df6662cd506fbf7fee8a46ebbf209142ffd7 (0.9.1). Add some integration tests to ensure it works as expected. Fix #183. --- diff --git a/NEWS b/NEWS index 1cbcad24..bdbafddb 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,8 @@ lldpd (0.9.4) * Change: + Make lldpd accepts a `-p` option to specify the PID file. + lldpcli will now display chassis TTL when detailed view is enabled. + * Fix: + + Fix setting of local value for port ID. lldpd (0.9.3) * Change: diff --git a/src/daemon/client.c b/src/daemon/client.c index 42612402..34d403e2 100644 --- a/src/daemon/client.c +++ b/src/daemon/client.c @@ -327,11 +327,13 @@ _client_handle_set_port(struct lldpd *cfg, port->p_id = strdup(set->local_id); port->p_id_len = strlen(set->local_id); port->p_id_subtype = LLDP_PORTID_SUBTYPE_LOCAL; + port->p_descr_force = 0; } if (set->local_descr) { log_debug("rpc", "requested change to Port Description"); free(port->p_descr); port->p_descr = strdup(set->local_descr); + port->p_descr_force = 1; } switch (set->rxtx) { case LLDPD_RXTX_TXONLY: diff --git a/src/daemon/interfaces.c b/src/daemon/interfaces.c index bc3e41a5..cefbec4d 100644 --- a/src/daemon/interfaces.c +++ b/src/daemon/interfaces.c @@ -517,8 +517,8 @@ interfaces_helper_port_name_desc(struct lldpd *cfg, int portid_type = cfg->g_config.c_lldp_portid_type; if (portid_type == LLDP_PORTID_SUBTYPE_IFNAME || (portid_type == LLDP_PORTID_SUBTYPE_UNKNOWN && has_alias) || - (portid_type == LLDP_PORTID_SUBTYPE_LOCAL && has_alias)) { - if (portid_type != LLDP_PORTID_SUBTYPE_LOCAL) { + (port->p_id_subtype == LLDP_PORTID_SUBTYPE_LOCAL && has_alias)) { + if (port->p_id_subtype != LLDP_PORTID_SUBTYPE_LOCAL) { log_debug("interfaces", "use ifname for %s", hardware->h_ifname); port->p_id_subtype = LLDP_PORTID_SUBTYPE_IFNAME; @@ -529,19 +529,21 @@ interfaces_helper_port_name_desc(struct lldpd *cfg, memcpy(port->p_id, hardware->h_ifname, port->p_id_len); } - /* use the actual alias in the port description */ - log_debug("interfaces", "using alias in description for %s", - hardware->h_ifname); - free(port->p_descr); - if (has_alias) { - port->p_descr = strdup(iface->alias); - } else { - /* We don't have anything else to put here and for CDP - * with need something non-NULL */ - port->p_descr = strdup(hardware->h_ifname); + if (port->p_descr_force == 0) { + /* use the actual alias in the port description */ + log_debug("interfaces", "using alias in description for %s", + hardware->h_ifname); + free(port->p_descr); + if (has_alias) { + port->p_descr = strdup(iface->alias); + } else { + /* We don't have anything else to put here and for CDP + * with need something non-NULL */ + port->p_descr = strdup(hardware->h_ifname); + } } } else { - if (portid_type != LLDP_PORTID_SUBTYPE_LOCAL) { + if (port->p_id_subtype != LLDP_PORTID_SUBTYPE_LOCAL) { log_debug("interfaces", "use MAC address for %s", hardware->h_ifname); port->p_id_subtype = LLDP_PORTID_SUBTYPE_LLADDR; @@ -552,11 +554,13 @@ interfaces_helper_port_name_desc(struct lldpd *cfg, port->p_id_len = ETHER_ADDR_LEN; } - /* use the ifname in the port description until alias is set */ - log_debug("interfaces", "using ifname in description for %s", - hardware->h_ifname); - free(port->p_descr); - port->p_descr = strdup(hardware->h_ifname); + if (port->p_descr_force == 0) { + /* use the ifname in the port description until alias is set */ + log_debug("interfaces", "using ifname in description for %s", + hardware->h_ifname); + free(port->p_descr); + port->p_descr = strdup(hardware->h_ifname); + } } } diff --git a/src/lldpd-structs.h b/src/lldpd-structs.h index 0260ae7a..4c65db31 100644 --- a/src/lldpd-structs.h +++ b/src/lldpd-structs.h @@ -259,6 +259,7 @@ struct lldpd_port { char *p_id; int p_id_len; char *p_descr; + int p_descr_force; /* Description has been forced by user */ u_int16_t p_mfs; #ifdef ENABLE_DOT3 diff --git a/tests/integration/test_basic.py b/tests/integration/test_basic.py index f0043d85..84a81e20 100644 --- a/tests/integration/test_basic.py +++ b/tests/integration/test_basic.py @@ -88,3 +88,77 @@ def test_management_address(lldpd1, lldpd, lldpcli, links, namespaces): with namespaces(1): out = lldpcli("-f", "keyvalue", "show", "neighbors") assert out["lldp.eth0.chassis.mgmt-ip"] == "172.25.21.47" + + +def test_portid_subtype_ifname(lldpd1, lldpd, lldpcli, namespaces): + with namespaces(2): + lldpd() + result = lldpcli("configure", "lldp", "portidsubtype", "ifname") + time.sleep(3) + with namespaces(1): + out = lldpcli("-f", "keyvalue", "show", "neighbors") + assert out["lldp.eth0.port.ifname"] == "eth1" + assert out["lldp.eth0.port.descr"] == "eth1" + + +def test_portid_subtype_with_alias(lldpd1, lldpd, lldpcli, links, namespaces): + with namespaces(2): + ipr = pyroute2.IPRoute() + idx = ipr.link_lookup(ifname="eth1")[0] + ipr.link('set', index=idx, ifalias="alias of eth1") + lldpd() + with namespaces(1): + out = lldpcli("-f", "keyvalue", "show", "neighbors") + assert out["lldp.eth0.port.ifname"] == "eth1" + assert out["lldp.eth0.port.descr"] == "alias of eth1" + + +def test_portid_subtype_macaddress(lldpd1, lldpd, lldpcli, links, namespaces): + with namespaces(2): + ipr = pyroute2.IPRoute() + idx = ipr.link_lookup(ifname="eth1")[0] + ipr.link('set', index=idx, ifalias="alias of eth1") + lldpd() + result = lldpcli("configure", "lldp", "portidsubtype", "macaddress") + time.sleep(3) + with namespaces(1): + out = lldpcli("-f", "keyvalue", "show", "neighbors") + assert out["lldp.eth0.port.mac"] == "00:00:00:00:00:02" + assert out["lldp.eth0.port.descr"] == "eth1" + + +def test_portid_subtype_local(lldpd1, lldpd, lldpcli, namespaces): + with namespaces(2): + lldpd() + result = lldpcli("configure", "lldp", "portidsubtype", "local", "localname") + time.sleep(3) + with namespaces(1): + out = lldpcli("-f", "keyvalue", "show", "neighbors") + assert out["lldp.eth0.port.local"] == "localname" + assert out["lldp.eth0.port.descr"] == "eth1" + + +def test_portid_subtype_local_with_description(lldpd1, lldpd, lldpcli, namespaces): + with namespaces(2): + lldpd() + result = lldpcli("configure", "lldp", "portidsubtype", "local", "localname", "description", "localdescription") + time.sleep(3) + with namespaces(1): + out = lldpcli("-f", "keyvalue", "show", "neighbors") + assert out["lldp.eth0.port.local"] == "localname" + assert out["lldp.eth0.port.descr"] == "localdescription" + + +def test_portid_subtype_local_with_alias(lldpd1, lldpd, lldpcli, namespaces): + with namespaces(2): + ipr = pyroute2.IPRoute() + idx = ipr.link_lookup(ifname="eth1")[0] + ipr.link('set', index=idx, ifalias="alias of eth1") + lldpd() + lldpd() + result = lldpcli("configure", "lldp", "portidsubtype", "local", "localname") + time.sleep(3) + with namespaces(1): + out = lldpcli("-f", "keyvalue", "show", "neighbors") + assert out["lldp.eth0.port.local"] == "localname" + assert out["lldp.eth0.port.descr"] == "alias of eth1"