Commit 5037b677c9 for frr

commit 5037b677c9d10997703f15aa9030ecc4be2b1230
Author: Lincoln Thurlow <lincoln@isi.edu>
Date:   Mon Nov 30 14:02:54 2026 -0800

    pathd: add route steering for pcep sr-te policies

    Currently when a pcep initiated policy is installed in pathd, the
    state is set as Active, but there is no mechanism built into pathd
    to steer the packets.

    This commit adds policy steering for pcep initiated policies into
    zebra.  Now, a PCInitiate will create a zebra colored route for
    steering based on the installed policy.  The route's administrative
    distance of 10 makes static routes take priority, and IGP routes
    defer to pce initiated policies.  When a policy is deleted or goes
    down, the route is removed.  This does not modify other
    policies.

    Signed-off-by: Lincoln Thurlow <lincoln@isi.edu>

diff --git a/pathd/path_zebra.c b/pathd/path_zebra.c
index 131f086526..2a7c80caaa 100644
--- a/pathd/path_zebra.c
+++ b/pathd/path_zebra.c
@@ -6,6 +6,7 @@
 #include <zebra.h>

 #include "frrevent.h"
+#include "frrdistance.h"
 #include "log.h"
 #include "lib_errors.h"
 #include "if.h"
@@ -107,6 +108,9 @@ static void path_zebra_connected(struct zclient *zclient)
 			continue;

 		path_zebra_add_sr_policy(policy, segment_list);
+
+		/* No UP change follows a zebra restart, so reinstall here */
+		path_zebra_update_steering_route(policy);
 	}
 }

@@ -217,9 +221,133 @@ void path_zebra_delete_sr_policy(struct srte_policy *policy)
 	zp.segment_list.label_num = 0;
 	policy->status = SRTE_POLICY_STATUS_DOWN;

+	/* Withdraw before zebra deletes the policy, or the route leaks */
+	path_zebra_update_steering_route(policy);
+
 	(void)zebra_send_sr_policy(pathd_zclient, ZEBRA_SR_POLICY_DELETE, &zp);
 }

+/*
+ * Builds and sends the steering route for a policy's endpoint.  The
+ * route carries one colored nexthop for every UP PCEP-originated policy,
+ * or one zebra is replacing, and F_POLICY_STEERING_ROUTE marks exactly
+ * those policies.  When no such policy remains, the route is deleted.
+ *
+ * @param policy The policy whose endpoint identifies the route
+ * @return The number of policies represented in the route
+ */
+static int path_zebra_send_steering_route(struct srte_policy *policy)
+{
+	struct zapi_route api;
+	struct zapi_nexthop *api_nh;
+	struct srte_policy *iter;
+	bool full = false;
+	int num = 0;
+
+	zapi_route_init(&api);
+	api.vrf_id = VRF_DEFAULT;
+	api.type = ZEBRA_ROUTE_SRTE;
+	api.safi = SAFI_UNICAST;
+
+	if (IS_IPADDR_V6(&policy->endpoint)) {
+		api.prefix.family = AF_INET6;
+		api.prefix.prefixlen = IPV6_MAX_BITLEN;
+		api.prefix.u.prefix6 = policy->endpoint.ipaddr_v6;
+	} else {
+		api.prefix.family = AF_INET;
+		api.prefix.prefixlen = IPV4_MAX_BITLEN;
+		api.prefix.u.prefix4 = policy->endpoint.ipaddr_v4;
+	}
+
+	SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
+	SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
+	SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
+	SET_FLAG(api.message, ZAPI_MESSAGE_SRTE);
+	api.distance = ZEBRA_SRTE_DISTANCE_DEFAULT;
+
+	RB_FOREACH (iter, srte_policy_head, &srte_policies) {
+		if (iter->protocol_origin != SRTE_ORIGIN_PCEP ||
+		    !ipaddr_is_same(&iter->endpoint, &policy->endpoint))
+			continue;
+
+		/* A policy zebra is replacing keeps steering until zebra reports */
+		bool steer = iter->status == SRTE_POLICY_STATUS_UP ||
+			     (iter->status == SRTE_POLICY_STATUS_GOING_UP &&
+			      CHECK_FLAG(iter->flags, F_POLICY_STEERING_ROUTE));
+
+		/* When full, the lowest colors steer and the rest are left out */
+		if (steer && num == MULTIPATH_NUM) {
+			if (!full)
+				zlog_warn("%s: steering route for %pIA already carries %d colored nexthops; policy color %u (and higher) not represented",
+					  __func__, &policy->endpoint, MULTIPATH_NUM, iter->color);
+			full = true;
+			steer = false;
+		}
+		if (!steer) {
+			UNSET_FLAG(iter->flags, F_POLICY_STEERING_ROUTE);
+			continue;
+		}
+
+		api_nh = &api.nexthops[num];
+		zapi_nexthop_init(api_nh);
+		api_nh->vrf_id = VRF_DEFAULT;
+		api_nh->srte_color = iter->color;
+		if (IS_IPADDR_V6(&iter->endpoint)) {
+			api_nh->type = NEXTHOP_TYPE_IPV6;
+			api_nh->gate.ipv6 = iter->endpoint.ipaddr_v6;
+		} else {
+			api_nh->type = NEXTHOP_TYPE_IPV4;
+			api_nh->gate.ipv4 = iter->endpoint.ipaddr_v4;
+		}
+		SET_FLAG(iter->flags, F_POLICY_STEERING_ROUTE);
+		num++;
+	}
+	api.nexthop_num = num;
+
+	if (zclient_route_send(num > 0 ? ZEBRA_ROUTE_ADD : ZEBRA_ROUTE_DELETE, pathd_zclient,
+			       &api) == ZCLIENT_SEND_FAILURE)
+		zlog_warn("%s: failed to send the steering route for %pIA", __func__,
+			  &policy->endpoint);
+
+	return num;
+}
+
+/*
+ * Synchronizes the steering route with the policy's operational status.
+ * Install the per-destination steering route when a PCEP-originated policy
+ * comes up, and withdraw it when the policy goes down or is deleted.
+ *
+ * @param policy The policy whose steering route should be updated
+ */
+void path_zebra_update_steering_route(struct srte_policy *policy)
+{
+	/* Local and BGP policies keep their own steering (statics, BGP color) */
+	if (policy->protocol_origin != SRTE_ORIGIN_PCEP)
+		return;
+
+	if (policy->status != SRTE_POLICY_STATUS_UP &&
+	    !CHECK_FLAG(policy->flags, F_POLICY_STEERING_ROUTE))
+		return;
+
+	/* Resend even if already sent: a restarted zebra may have lost it */
+	path_zebra_send_steering_route(policy);
+}
+
+/*
+ * Takes a policy out of its endpoint's steering route while it stays
+ * GOING_UP, for example when zebra failed to replace it.
+ *
+ * @param policy The policy to take out of the steering route
+ */
+void path_zebra_withdraw_steering_route(struct srte_policy *policy)
+{
+	if (!CHECK_FLAG(policy->flags, F_POLICY_STEERING_ROUTE))
+		return;
+
+	UNSET_FLAG(policy->flags, F_POLICY_STEERING_ROUTE);
+	path_zebra_send_steering_route(policy);
+}
+
 /**
  * Allocates a label from Zebra's label manager.
  *
diff --git a/pathd/path_zebra.h b/pathd/path_zebra.h
index 74a62e38b3..9f50092790 100644
--- a/pathd/path_zebra.h
+++ b/pathd/path_zebra.h
@@ -14,6 +14,8 @@ bool get_ipv6_router_id(struct in6_addr *router_id);
 void path_zebra_add_sr_policy(struct srte_policy *policy,
 			      struct srte_segment_list *segment_list);
 void path_zebra_delete_sr_policy(struct srte_policy *policy);
+void path_zebra_update_steering_route(struct srte_policy *policy);
+void path_zebra_withdraw_steering_route(struct srte_policy *policy);
 int path_zebra_request_label(mpls_label_t label);
 void path_zebra_release_label(mpls_label_t label);
 void path_zebra_init(struct event_loop *master);
diff --git a/pathd/pathd.c b/pathd/pathd.c
index e77d5ab227..475b29956a 100644
--- a/pathd/pathd.c
+++ b/pathd/pathd.c
@@ -1180,6 +1180,9 @@ void srte_candidate_status_update(struct srte_candidate *candidate, int status)
 				 get is back to DOWN and remove the
 				 policy */
 		case SRTE_POLICY_STATUS_GOING_UP:
+			/* Stop steering to it while zebra retries */
+			path_zebra_withdraw_steering_route(policy);
+			return;
 		case SRTE_POLICY_STATUS_DOWN:
 			return;
 		case SRTE_POLICY_STATUS_UNKNOWN:
@@ -1187,6 +1190,7 @@ void srte_candidate_status_update(struct srte_candidate *candidate, int status)
 		case SRTE_POLICY_STATUS_GOING_DOWN:
 			policy->status = SRTE_POLICY_STATUS_DOWN;
 			srte_policy_status_log(policy);
+			path_zebra_update_steering_route(policy);
 			break;
 		}
 		break;
@@ -1200,6 +1204,7 @@ void srte_candidate_status_update(struct srte_candidate *candidate, int status)
 		case SRTE_POLICY_STATUS_GOING_UP:
 			policy->status = SRTE_POLICY_STATUS_UP;
 			srte_policy_status_log(policy);
+			path_zebra_update_steering_route(policy);
 			break;
 		}
 		break;
diff --git a/pathd/pathd.h b/pathd/pathd.h
index 97070871e1..9d74ab9fb3 100644
--- a/pathd/pathd.h
+++ b/pathd/pathd.h
@@ -360,6 +360,7 @@ struct srte_policy {
 #define F_POLICY_NEW 0x0002
 #define F_POLICY_MODIFIED 0x0004
 #define F_POLICY_DELETED 0x0008
+#define F_POLICY_STEERING_ROUTE 0x0010
 	/* SRP id for PcInitiated support */
 	int srp_id;
 };