Commit a1a3a293e9 for frr

commit a1a3a293e9d758e73bb59a68a845c70ce11fc8db
Author: Sougata Barik <sougatab@nvidia.com>
Date:   Thu Sep 10 18:15:31 2026 +0530

    bfdd: cap AdminDown re-transmit to max(min, one Detection Time)

    The previous change kept transmitting AdminDown at slow rate after
    administrative shutdown so a lost single AdminDown could not silently
    time the peer out. That advertised state indefinitely, however, and
    kept a per-session 1 pps transmit timer running forever for every shut
    session.

    RFC 5880 Section 6.8.16 requires AdminDown to be transmitted for at
    least a Detection Time so the peer reliably learns the state change.
    Bound the burst to

        max(BFD_ADMIN_DOWN_TX_MIN,
            ceil(pre_slow_detect_TO / BFD_DEF_SLOWTX))

    slow-rate packets and stop the transmit timer afterwards. The
    Detection Time term literally satisfies the RFC SHOULD for any
    negotiated timers, and the min-floor (5 packets, ~5 s at slow rate)
    gives aggressive-timer sessions the loss-tolerance margin the previous
    fix intended. After the burst the shut session becomes silent as
    before, so no per-session 1 pps timer runs forever.

    Sample bs->detect_TO BEFORE bs_set_slow_timers() overwrites it, so
    detect_pkts is derived from the pre-shutdown negotiated detection
    window - the interval the peer expected packets in while we were Up.
    detect_TO == 0 (session never Up) naturally falls back to the floor.

    Changes:
    - Add BFD_ADMIN_DOWN_TX_MIN and per-session admindown_tx_left.
    - bfd_set_shutdown(true): capture pre-slow detect_TO, apply the
      max() formula, send the immediate AdminDown, and only re-arm the
      transmit timer while the budget is positive.
    - ptm_bfd_xmt_TO(): when in SHUTDOWN+ADM_DOWN, decrement the counter
      after each transmit; on reaching zero delete the xmt timer and stop
      re-arming.
    - bfd_set_shutdown(false): reset admindown_tx_left to 0 so a rapid
      shut/no-shut cannot leak stale state into the recovered session;
      the existing recv/xmt timer restart already cancels any pending
      slow-rate AdminDown slot.
    - bfd_xmttimer_update(): do not schedule a shut session whose budget
      is spent. A received Poll calls bs_final_handler(), which restarts
      the transmit timer, and ptm_bfd_xmt_TO() sends before it checks the
      budget, so every Poll drew one more AdminDown past the cap. The
      Final answering the Poll is sent from the receive path and is
      unaffected.
    - Keep admindown_tx_left in 32 bits: the Detection Time term reaches
      about 1.1 million packets (detect-multiplier 255 at the maximum
      interval), and an 8-bit budget wrapped for any Detection Time above
      255 s, cutting the burst as short as one packet.

    Ticket: #5433819

    Signed-off-by: Sougata Barik <sougatab@nvidia.com>

diff --git a/bfdd/bfd.c b/bfdd/bfd.c
index 9604f987a3..087378df92 100644
--- a/bfdd/bfd.c
+++ b/bfdd/bfd.c
@@ -723,6 +723,20 @@ void ptm_bfd_xmt_TO(struct bfd_session *bfd, int fbit)
 	/* Send the scheduled control packet */
 	ptm_bfd_snd(bfd, fbit);

+	/*
+	 * Cap AdminDown re-transmit to the admindown_tx_left budget. Once
+	 * it is exhausted, stop the transmit timer so a shut session no
+	 * longer burns 1 pps forever.
+	 */
+	if (CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_SHUTDOWN) && bfd->ses_state == PTM_BFD_ADM_DOWN) {
+		if (bfd->admindown_tx_left > 0)
+			bfd->admindown_tx_left--;
+		if (bfd->admindown_tx_left == 0) {
+			bfd_xmttimer_delete(bfd);
+			return;
+		}
+	}
+
 	/* Restart the timer for next time */
 	ptm_bfd_start_xmt_timer(bfd, false);
 }
@@ -1877,11 +1891,29 @@ void bfd_set_shutdown(struct bfd_session *bs, bool shutdown)
 		bs->ses_state = PTM_BFD_ADM_DOWN;
 		ptm_bfd_notify(bs, bs->ses_state);

-		/* Advertise AdminDown at slow rate while shut. */
+		/*
+		 * Advertise AdminDown at slow rate for a bounded burst:
+		 *   max(BFD_ADMIN_DOWN_TX_MIN,
+		 *       ceil(pre-slow detect_TO / BFD_DEF_SLOWTX))
+		 * so RFC 5880 6.8.16 ("SHOULD be transmitted for at least a
+		 * Detection Time") holds for any negotiated timers, and
+		 * aggressive-timer sessions still get the min-floor as a
+		 * loss-tolerance margin. Sample detect_TO BEFORE moving to
+		 * slow timers, since bs_set_slow_timers() resets it.
+		 * The first packet goes out immediately; the timer is
+		 * re-armed only while budget remains.
+		 */
 		if (bs->sock != -1) {
+			uint64_t pre_slow_detect_TO = bs->detect_TO;
+			uint32_t detect_pkts;
+
 			bs_set_slow_timers(bs);
+			detect_pkts = (pre_slow_detect_TO + BFD_DEF_SLOWTX - 1) / BFD_DEF_SLOWTX;
+			bs->admindown_tx_left = MAX(BFD_ADMIN_DOWN_TX_MIN, detect_pkts);
 			ptm_bfd_snd(bs, 0);
-			ptm_bfd_start_xmt_timer(bs, false);
+			bs->admindown_tx_left--;
+			if (bs->admindown_tx_left > 0)
+				ptm_bfd_start_xmt_timer(bs, false);
 		}
 	} else {
 		/* Already working. */
@@ -1890,6 +1922,14 @@ void bfd_set_shutdown(struct bfd_session *bs, bool shutdown)

 		UNSET_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN);

+		/*
+		 * Clear any pending AdminDown transmit budget so a rapid
+		 * shut/no-shut cannot leak stale state into the recovered
+		 * session; the timer restart below replaces any pending
+		 * slow-rate AdminDown slot.
+		 */
+		bs->admindown_tx_left = 0;
+
 		/* Handle data plane shutdown case. */
 		if (bs->bdc) {
 			bs->ses_state = PTM_BFD_DOWN;
diff --git a/bfdd/bfd.h b/bfdd/bfd.h
index 5ef75357f7..660034540f 100644
--- a/bfdd/bfd.h
+++ b/bfdd/bfd.h
@@ -419,6 +419,14 @@ struct bfd_session {
 	/* software object state */
 	uint8_t polling;

+	/*
+	 * AdminDown transmit budget: number of slow-rate AdminDown Control
+	 * packets still to send after administrative shutdown. Seeded on
+	 * entering shutdown, decremented after each send, and reset to 0 on
+	 * no-shutdown so a rapid shut/no-shut cannot leak stale state.
+	 */
+	uint32_t admindown_tx_left;
+
 	/* This and the localDiscr are the keys to state info */
 	struct bfd_key key;
 	struct peer_label *pl;
@@ -507,6 +515,20 @@ struct sbfd_reflector {
 #define BFD_DEF_REQ_MIN_ECHO_RX (50 * 1000) /* microseconds. */
 #define BFD_DEF_SLOWTX (1000 * 1000) /* microseconds. */
 #define SBFD_ECHO_DEF_SLOWTX	(1000 * 1000) /* microseconds. */
+/*
+ * Minimum number of AdminDown control packets to transmit at slow rate
+ * after administrative shutdown. The actual budget is:
+ *
+ *     max(BFD_ADMIN_DOWN_TX_MIN,
+ *         ceil(pre_slow_detect_TO / BFD_DEF_SLOWTX))
+ *
+ * so RFC 5880 Section 6.8.16 ("SHOULD be transmitted for at least a
+ * Detection Time") holds for any negotiated timers, while aggressive-
+ * timer sessions still get this floor as a loss-tolerance safety margin.
+ * Once the budget is spent the transmit timer stops, so a shut session
+ * no longer burns a packet-per-second forever.
+ */
+#define BFD_ADMIN_DOWN_TX_MIN 5
 /** Minimum multi hop TTL. */
 #define BFD_DEF_MHOP_TTL 254
 #define BFD_PKT_LEN 24 /* Length of control packet */
diff --git a/bfdd/event.c b/bfdd/event.c
index 6221329782..8993089299 100644
--- a/bfdd/event.c
+++ b/bfdd/event.c
@@ -136,9 +136,13 @@ void bfd_xmttimer_update(struct bfd_session *bs, uint64_t jitter)
 	/* Remove previous schedule if any. */
 	bfd_xmttimer_delete(bs);

-	/* Skip if inactive; allow scheduling while AdminDown. */
-	if (bs->sock == -1 ||
-	    (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN) && bs->ses_state != PTM_BFD_ADM_DOWN)) {
+	/*
+	 * Skip if inactive. A shut session transmits only its AdminDown
+	 * burst, so once that is spent a peer's Poll (bs_final_handler())
+	 * must not start the timer again.
+	 */
+	if (bs->sock == -1 || (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN) &&
+			       (bs->ses_state != PTM_BFD_ADM_DOWN || bs->admindown_tx_left == 0))) {
 		bs->xmt_TO_actual = 0; /* Unscheduled */
 		frrtrace(4, frr_bfd, xmttimer_schedule, bs, jitter, false);
 		return;