From e770b7204bf80b0a6b019bdc9cbfa050cd735e4e Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Sun, 4 Jan 2015 18:24:29 +0100 Subject: [PATCH] lldp: send a shutdown LLDPDU on exit With the previous commit, it is now easy to send a shutdown LLPDU when exiting lldpd. Let's do that. --- src/daemon/lldp.c | 8 +++++--- src/daemon/lldpd.c | 19 +++++++++++++++++++ src/daemon/lldpd.h | 1 + 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/daemon/lldp.c b/src/daemon/lldp.c index 0e6220e2..5b0e4771 100644 --- a/src/daemon/lldp.c +++ b/src/daemon/lldp.c @@ -469,12 +469,14 @@ toobig: return E2BIG; } -/* Send a shutdown LLDPDU. Should be called only if we have a previously sent - * LLDPDU. */ -static int +/* Send a shutdown LLDPDU. */ +int lldp_send_shutdown(struct lldpd *global, struct lldpd_hardware *hardware) { + if (hardware->h_lchassis_previous_id == NULL || + hardware->h_lport_previous_id == NULL) + return 0; return _lldp_send(global, hardware, hardware->h_lchassis_previous_id_subtype, hardware->h_lchassis_previous_id, diff --git a/src/daemon/lldpd.c b/src/daemon/lldpd.c index 8e97fe86..153514be 100644 --- a/src/daemon/lldpd.c +++ b/src/daemon/lldpd.c @@ -906,6 +906,21 @@ lldpd_recv(struct lldpd *cfg, struct lldpd_hardware *hardware, int fd) free(buffer); } +static void +lldpd_send_shutdown(struct lldpd_hardware *hardware) +{ + struct lldpd *cfg = hardware->h_cfg; + if (cfg->g_config.c_receiveonly || cfg->g_config.c_paused) return; + if ((hardware->h_flags & IFF_RUNNING) == 0) + return; + + /* It's safe to call `lldp_send_shutdown()` because shutdown LLDPU will + * only be emitted if LLDP was sent on that port. */ + if (lldp_send_shutdown(hardware->h_cfg, hardware) != 0) + log_warnx("send", "unable to send shutdown LLDPDU on %s", + hardware->h_ifname); +} + void lldpd_send(struct lldpd_hardware *hardware) { @@ -1120,6 +1135,10 @@ lldpd_exit(struct lldpd *cfg) { struct lldpd_hardware *hardware, *hardware_next; log_debug("main", "exit lldpd"); + + TAILQ_FOREACH(hardware, &cfg->g_hardware, h_entries) + lldpd_send_shutdown(hardware); + close(cfg->g_ctl); priv_ctl_cleanup(cfg->g_ctlname); log_debug("main", "cleanup hardware information"); diff --git a/src/daemon/lldpd.h b/src/daemon/lldpd.h index f5810676..9f986f31 100644 --- a/src/daemon/lldpd.h +++ b/src/daemon/lldpd.h @@ -160,6 +160,7 @@ void levent_schedule_cleanup(struct lldpd *); int levent_make_socket_nonblocking(int); /* lldp.c */ +int lldp_send_shutdown(PROTO_SEND_SIG); int lldp_send(PROTO_SEND_SIG); int lldp_decode(PROTO_DECODE_SIG); -- 2.39.5