From: tcollet Date: Wed, 21 Jan 2026 08:45:24 +0000 (+0100) Subject: config: add portdescription-source option X-Git-Tag: 1.0.21~18^2~3 X-Git-Url: http://git.ipfire.org/gitweb/index.cgi?a=commitdiff_plain;h=bf71e500b5ab47f894a3aee6e6518b3f76537bbf;p=thirdparty%2Flldpd.git config: add portdescription-source option This new option allows to control the port description source (alias vs ifname) when the portidsubtype is macaddress. Usage: lldpcli configure lldp portidsubtype macaddress lldpcli configure lldp portdescription-source alias Result: PortID = MAC address, PortDescr = interface alias --- diff --git a/src/client/conf-lldp.c b/src/client/conf-lldp.c index c5ec33a0..1b168769 100644 --- a/src/client/conf-lldp.c +++ b/src/client/conf-lldp.c @@ -224,6 +224,52 @@ cmd_agent_type(struct lldpctl_conn_t *conn, struct writer *w, struct cmd_env *en return 1; } +static int +cmd_portdescr_type(struct lldpctl_conn_t *conn, struct writer *w, + struct cmd_env *env, const void *arg) +{ + const char *value_str = arg; + int value = -1; + + log_debug("lldpctl", "lldp port description source"); + + lldpctl_atom_t *config = lldpctl_get_configuration(conn); + if (config == NULL) { + log_warnx("lldpctl", "unable to get configuration from lldpd. %s", + lldpctl_last_strerror(conn)); + return 0; + } + + for (lldpctl_map_t *b_map = + lldpctl_key_get_map(lldpctl_k_config_lldp_portdescr_type); + b_map->string; b_map++) { + if (!strcmp(b_map->string, value_str)) { + value = b_map->value; + break; + } + } + + if (value == -1) { + log_warnx("lldpctl", "invalid value"); + lldpctl_atom_dec_ref(config); + return 0; + } + + if (lldpctl_atom_set_int(config, lldpctl_k_config_lldp_portdescr_type, value) == + NULL) { + log_warnx("lldpctl", + "unable to set port description source. %s", + lldpctl_last_strerror(conn)); + lldpctl_atom_dec_ref(config); + return 0; + } + + log_info("lldpctl", "port description source set to %s", value_str); + lldpctl_atom_dec_ref(config); + + return 1; +} + static int cmd_portid_type_local(struct lldpctl_conn_t *conn, struct writer *w, struct cmd_env *env, const void *arg) @@ -765,6 +811,19 @@ register_commands_configure_lldp(struct cmd_node *configure, b_map->string); } + /* Configure port description source */ + struct cmd_node *configure_lldp_portdescr_type = + commands_new(configure_lldp, "portdescription-source", + "Port description source", NULL, NULL, NULL); + for (lldpctl_map_t *b_map = + lldpctl_key_get_map(lldpctl_k_config_lldp_portdescr_type); + b_map->string; b_map++) { + commands_new(commands_new(configure_lldp_portdescr_type, + b_map->string, b_map->string, NULL, NULL, NULL), + NEWLINE, "Set port description source", NULL, + cmd_portdescr_type, b_map->string); + } + /* Now handle the various portid subtypes we can configure. */ struct cmd_node *configure_lldp_portid_type = commands_new(configure_lldp, "portidsubtype", "LLDP PortID TLV Subtype", NULL, NULL, NULL); diff --git a/src/client/display.c b/src/client/display.c index 6d9be2a5..8a9d355e 100644 --- a/src/client/display.c +++ b/src/client/display.c @@ -1034,6 +1034,10 @@ display_configuration(lldpctl_conn_t *conn, struct writer *w) lldpctl_k_config_bond_slave_src_mac_type)); tag_datatag(w, "lldp-portid-type", "Port ID TLV subtype for LLDP frames", lldpctl_atom_get_str(configuration, lldpctl_k_config_lldp_portid_type)); + tag_datatag(w, "lldp-portdescr-type", + "Port description source for LLDP frames", + lldpctl_atom_get_str(configuration, + lldpctl_k_config_lldp_portdescr_type)); tag_datatag(w, "lldp-agent-type", "Agent type", lldpctl_atom_get_str(configuration, lldpctl_k_config_lldp_agent_type)); diff --git a/src/daemon/client.c b/src/daemon/client.c index efe20b57..348d1f4c 100644 --- a/src/daemon/client.c +++ b/src/daemon/client.c @@ -115,6 +115,14 @@ client_handle_set_configuration(struct lldpd *cfg, enum hmsg_type *type, void *i cfg->g_config.c_lldp_agent_type = config->c_lldp_agent_type; levent_update_now(cfg); } + if (CHANGED(c_lldp_portdescr_type) && + config->c_lldp_portdescr_type >= LLDP_PORTDESCR_SRC_AUTO && + config->c_lldp_portdescr_type <= LLDP_PORTDESCR_SRC_MAX) { + log_debug("rpc", "change lldp port description source to %d", + config->c_lldp_portdescr_type); + cfg->g_config.c_lldp_portdescr_type = config->c_lldp_portdescr_type; + levent_update_now(cfg); + } /* Pause/resume */ if (CHANGED(c_paused)) { log_debug("rpc", "client asked to %s lldpd", diff --git a/src/daemon/interfaces.c b/src/daemon/interfaces.c index 2aaec769..f54e059f 100644 --- a/src/daemon/interfaces.c +++ b/src/daemon/interfaces.c @@ -599,6 +599,7 @@ interfaces_helper_port_name_desc(struct lldpd *cfg, struct lldpd_hardware *hardw int has_alias = (iface->alias != NULL && strlen(iface->alias) != 0 && strncmp("lldpd: ", iface->alias, 7)); int portid_type = cfg->g_config.c_lldp_portid_type; + int portdescr_type = cfg->g_config.c_lldp_portdescr_type; if (portid_type == LLDP_PORTID_SUBTYPE_IFNAME || (portid_type == LLDP_PORTID_SUBTYPE_UNKNOWN && has_alias) || (port->p_id_subtype == LLDP_PORTID_SUBTYPE_LOCAL && has_alias)) { @@ -615,14 +616,16 @@ interfaces_helper_port_name_desc(struct lldpd *cfg, struct lldpd_hardware *hardw 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) { + if (portdescr_type != LLDP_PORTDESCR_SRC_IFNAME && has_alias) { + log_debug("interfaces", "using alias in description for %s", + hardware->h_ifname); port->p_descr = strdup(iface->alias); } else { /* We don't have anything else to put here and for CDP - * with need something non-NULL */ + * with need something non-NULL even if alias is requested */ + log_debug("interfaces", "using ifname in description for %s", + hardware->h_ifname); port->p_descr = strdup(hardware->h_ifname); } } @@ -639,11 +642,18 @@ interfaces_helper_port_name_desc(struct lldpd *cfg, struct lldpd_hardware *hardw } 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); + /* use the actual alias in the port description + * if requested and set */ free(port->p_descr); - port->p_descr = strdup(hardware->h_ifname); + if (portdescr_type == LLDP_PORTDESCR_SRC_ALIAS && has_alias) { + log_debug("interfaces", "using alias in description for %s", + hardware->h_ifname); + port->p_descr = strdup(iface->alias); + } else { + log_debug("interfaces", "using ifname in description for %s", + hardware->h_ifname); + port->p_descr = strdup(hardware->h_ifname); + } } } } diff --git a/src/lib/atoms/config.c b/src/lib/atoms/config.c index e2ed8352..5cef9109 100644 --- a/src/lib/atoms/config.c +++ b/src/lib/atoms/config.c @@ -56,9 +56,20 @@ static struct atom_map lldp_agent_map = { }, }; +static struct atom_map lldp_portdescr_map = { + .key = lldpctl_k_config_lldp_portdescr_type, + .map = { + { LLDP_PORTDESCR_SRC_AUTO, "auto"}, + { LLDP_PORTDESCR_SRC_IFNAME, "ifname"}, + { LLDP_PORTDESCR_SRC_ALIAS, "alias"}, + { 0, NULL}, + }, +}; + ATOM_MAP_REGISTER(bond_slave_src_mac_map, 1); ATOM_MAP_REGISTER(lldp_portid_map, 2); ATOM_MAP_REGISTER(lldp_agent_map, 3); +ATOM_MAP_REGISTER(lldp_portdescr_map, 4); static int _lldpctl_atom_new_config(lldpctl_atom_t *atom, va_list ap) @@ -113,6 +124,8 @@ _lldpctl_atom_get_str_config(lldpctl_atom_t *atom, lldpctl_key_t key) return map_lookup(lldp_portid_map.map, c->config->c_lldp_portid_type); case lldpctl_k_config_lldp_agent_type: return map_lookup(lldp_agent_map.map, c->config->c_lldp_agent_type); + case lldpctl_k_config_lldp_portdescr_type: + return map_lookup(lldp_portdescr_map.map, c->config->c_lldp_portdescr_type); default: SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); return NULL; @@ -242,6 +255,8 @@ _lldpctl_atom_get_int_config(lldpctl_atom_t *atom, lldpctl_key_t key) return c->config->c_tx_hold; case lldpctl_k_config_max_neighbors: return c->config->c_max_neighbors; + case lldpctl_k_config_lldp_portid_type: + return c->config->c_lldp_portid_type; default: return SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); } @@ -311,6 +326,10 @@ _lldpctl_atom_set_int_config(lldpctl_atom_t *atom, lldpctl_key_t key, long int v config.c_lldp_agent_type = value; c->config->c_lldp_agent_type = value; break; + case lldpctl_k_config_lldp_portdescr_type: + config.c_lldp_portdescr_type = value; + c->config->c_lldp_portdescr_type = value; + break; default: SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); return NULL; diff --git a/src/lib/lldpctl.h b/src/lib/lldpctl.h index 0eb0265d..9e08f312 100644 --- a/src/lib/lldpctl.h +++ b/src/lib/lldpctl.h @@ -920,6 +920,7 @@ typedef enum { lldpctl_k_config_lldp_agent_type, /**< `(I,WO)` LLDP agent type */ lldpctl_k_config_max_neighbors, /**< `(I,WO)`Maximum number of neighbors per port. */ + lldpctl_k_config_lldp_portdescr_type, /**< `(I,WO)` LLDP port description source */ lldpctl_k_custom_tlvs = 5000, /**< `(AL)` custom TLVs */ lldpctl_k_custom_tlvs_clear, /**< `(WO)` clear list of custom TLVs */ diff --git a/src/lldp-const.h b/src/lldp-const.h index 591b47d3..b858b379 100644 --- a/src/lldp-const.h +++ b/src/lldp-const.h @@ -47,6 +47,12 @@ #define LLDP_PORTID_SUBTYPE_LOCAL 7 #define LLDP_PORTID_SUBTYPE_MAX LLDP_PORTID_SUBTYPE_LOCAL +/* Port description source type */ +#define LLDP_PORTDESCR_SRC_AUTO 0 +#define LLDP_PORTDESCR_SRC_IFNAME 1 +#define LLDP_PORTDESCR_SRC_ALIAS 2 +#define LLDP_PORTDESCR_SRC_MAX LLDP_PORTDESCR_SRC_ALIAS + /* Operational MAU Type field. See: * https://www.iana.org/assignments/ianamau-mib/ianamau-mib */ #define LLDP_DOT3_MAU_AUI 1 diff --git a/src/lldpd-structs.h b/src/lldpd-structs.h index a7ef70b6..d1f7e9b3 100644 --- a/src/lldpd-structs.h +++ b/src/lldpd-structs.h @@ -422,6 +422,7 @@ struct lldpd_config { slaves */ int c_lldp_portid_type; /* The PortID type */ int c_lldp_agent_type; /* The agent type */ + int c_lldp_portdescr_type; /* The port description source type */ }; MARSHAL_BEGIN(lldpd_config) MARSHAL_STR(lldpd_config, c_mgmt_pattern)