Commit cfa3e00025 for frr

commit cfa3e00025be22e47eb9dda1aa3430c5d33a77b7
Author: Enke Chen <enchen@paloaltonetworks.com>
Date:   Wed Nov 4 23:35:13 2026 -0800

    bgpd: set max-med MED value in subgroup_announce_check

    Set the MED value for "bgp max-med" in subgroup_announce_check()
    and subgroup_default_originate() instead of computing it at packet
    formation time. With an accurate outbound attribute cache:

    1) Reduce duplicate updates - attribute comparison correctly detects
       MED changes when maxmed_active transitions
    2) Display the MED correctly in "show bgp neighbor x.x.x.x
       advertised-routes"

    Signed-off-by: Enke Chen <enchen@paloaltonetworks.com>

diff --git a/bgpd/bgp_attr.c b/bgpd/bgp_attr.c
index f5f9d563c3..45ec7ae2c6 100644
--- a/bgpd/bgp_attr.c
+++ b/bgpd/bgp_attr.c
@@ -6186,12 +6186,11 @@ bgp_size_t bgp_packet_attribute(struct bgp *bgp, struct peer *peer, struct strea
 	}

 	/* MED attribute. */
-	if (bgp_attr_exists(attr, BGP_ATTR_MULTI_EXIT_DISC) || bgp->maxmed_active) {
+	if (bgp_attr_exists(attr, BGP_ATTR_MULTI_EXIT_DISC)) {
 		stream_putc(s, BGP_ATTR_FLAG_OPTIONAL);
 		stream_putc(s, BGP_ATTR_MULTI_EXIT_DISC);
 		stream_putc(s, 4);
-		stream_putl(s, (bgp->maxmed_active ? bgp->maxmed_value
-						   : attr->med));
+		stream_putl(s, attr->med);
 	}

 	/* Local preference. */
diff --git a/bgpd/bgp_route.c b/bgpd/bgp_route.c
index c8f2d9b057..34e785ba8a 100644
--- a/bgpd/bgp_route.c
+++ b/bgpd/bgp_route.c
@@ -3551,6 +3551,10 @@ bool subgroup_announce_check(struct bgp_dest *dest, struct bgp_path_info *pi,
 		bgp_attr_clear_reflection(attr);
 	}

+	/* Handle "bgp max-med" */
+	if (bgp->maxmed_active)
+		bgp_attr_set_med(attr, bgp->maxmed_value);
+
 	return true;
 }

diff --git a/bgpd/bgp_updgrp_adv.c b/bgpd/bgp_updgrp_adv.c
index 76b88e9465..f9b7c21891 100644
--- a/bgpd/bgp_updgrp_adv.c
+++ b/bgpd/bgp_updgrp_adv.c
@@ -1243,6 +1243,10 @@ void subgroup_default_originate(struct update_subgroup *subgrp, bool withdraw)
 			    (CHECK_FLAG(peer->flags, PEER_FLAG_GRACEFUL_SHUTDOWN)))
 				bgp_attr_add_gshut_community(&attr_copy);

+			/* Handle "bgp max-med" */
+			if (bgp->maxmed_active)
+				bgp_attr_set_med(&attr_copy, bgp->maxmed_value);
+
 			bgp_peer_prepend_local_as(peer, afi, safi, &attr_copy);

 			SET_FLAG(subgrp->sflags,