Commit dfc6759576 for frr

commit dfc67595761b51f191ea0f5dc7cdfac41abc72d1
Author: Lincoln Thurlow <lincoln@isi.edu>
Date:   Tue Dec 1 11:00:46 2026 -0800

    pathd: track PCEs whose session timeout is running

    pathd's main thread cannot tell an orphaned PCE-initiated path (RFC
    8281 section 6) from one whose PCE is still connected.

    The session timeout runs on the PCEP controller thread, and the main
    thread only hears of it when it expires.

    This commit has the controller tell the main thread when the session
    timeout of a PCE starts and stops, and the main thread record the
    originators of those PCEs. At shutdown, a pending start or stop is
    freed with the other pending main thread events.

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

diff --git a/pathd/path_pcep.c b/pathd/path_pcep.c
index 4567f67950..a29aec000f 100644
--- a/pathd/path_pcep.c
+++ b/pathd/path_pcep.c
@@ -39,6 +39,14 @@ static struct pcep_glob pcep_glob_space = {
 };
 struct pcep_glob *pcep_g = &pcep_glob_space;

+/* PCCs whose session timeout is running, at most one slot per PCC */
+static struct {
+	int pcc_id;
+	char originator[64];
+} pcep_orphaned[MAX_PCC];
+
+static void pcep_orphaned_set(int pcc_id, const char *originator);
+
 /* Main Thread Even Handler */
 static int pcep_main_event_handler(enum pcep_main_event_type type, int pcc_id,
 				   void *payload);
@@ -198,8 +206,16 @@ int pcep_main_event_handler(enum pcep_main_event_type type, int pcc_id,
 		ret = pcep_main_event_update_candidate((struct path *)payload);
 		break;
 	case PCEP_MAIN_EVENT_REMOVE_CANDIDATE_LSP:
+		pcep_orphaned_set(pcc_id, NULL);
 		ret = pcep_main_event_remove_candidate_segments((const char *)payload);
 		break;
+	case PCEP_MAIN_EVENT_SESSION_DOWN:
+		pcep_orphaned_set(pcc_id, payload);
+		XFREE(MTYPE_PCEP, payload);
+		break;
+	case PCEP_MAIN_EVENT_SESSION_UP:
+		pcep_orphaned_set(pcc_id, NULL);
+		break;
 	case PCEP_MAIN_EVENT_UNDEFINED:
 		flog_warn(EC_PATH_PCEP_RECOVERABLE_INTERNAL_ERROR,
 			  "Unexpected event received in the main thread: %u",
@@ -312,6 +328,44 @@ int pcep_main_event_update_candidate(struct path *path)
 	return ret;
 }

+/* The paths of this originator are orphans (RFC 8281 section 6) */
+bool pcep_originator_orphaned(const char *originator)
+{
+	for (int i = 0; i < MAX_PCC; i++) {
+		if (pcep_orphaned[i].pcc_id != 0 &&
+		    strcmp(pcep_orphaned[i].originator, originator) == 0)
+			return true;
+	}
+	return false;
+}
+
+/* Record the originator of a PCC, or forget it when NULL */
+void pcep_orphaned_set(int pcc_id, const char *originator)
+{
+	int slot = -1;
+
+	for (int i = 0; i < MAX_PCC; i++) {
+		if (pcep_orphaned[i].pcc_id == pcc_id) {
+			slot = i;
+			break;
+		}
+		if (slot < 0 && pcep_orphaned[i].pcc_id == 0)
+			slot = i;
+	}
+	if (slot < 0) {
+		zlog_warn("%s: no free slot to record PCC %d", __func__, pcc_id);
+		return;
+	}
+
+	if (originator == NULL) {
+		if (pcep_orphaned[slot].pcc_id == pcc_id)
+			pcep_orphaned[slot].pcc_id = 0;
+		return;
+	}
+	pcep_orphaned[slot].pcc_id = pcc_id;
+	strlcpy(pcep_orphaned[slot].originator, originator, sizeof(pcep_orphaned[slot].originator));
+}
+
 int pcep_main_event_remove_candidate_segments(const char *originator)
 {
 	srte_candidate_unset_segment_list(originator);
diff --git a/pathd/path_pcep.h b/pathd/path_pcep.h
index 4d90a5a7cb..83a2fa9a41 100644
--- a/pathd/path_pcep.h
+++ b/pathd/path_pcep.h
@@ -310,6 +310,7 @@ struct path_hop *pcep_new_hop(void);
 struct path_metric *pcep_new_metric(void);
 struct path *pcep_copy_path(struct path *path);
 void pcep_free_path(struct path *path);
+bool pcep_originator_orphaned(const char *originator);


 #endif // _PATH_PCEP_H_
diff --git a/pathd/path_pcep_controller.c b/pathd/path_pcep_controller.c
index 8691e4e898..a62240a0bf 100644
--- a/pathd/path_pcep_controller.c
+++ b/pathd/path_pcep_controller.c
@@ -406,6 +406,15 @@ void pcep_thread_remove_candidate_path_segments(struct ctrl_state *ctrl_state,
 		     PCEP_MAIN_EVENT_REMOVE_CANDIDATE_LSP, originator);
 }

+/* Tell the main thread a PCE's paths are orphaned, or no longer */
+void pcep_thread_session_state(struct ctrl_state *ctrl_state, struct pcc_state *pcc_state,
+			       bool down)
+{
+	send_to_main(ctrl_state, pcep_pcc_get_pcc_id(pcc_state),
+		     down ? PCEP_MAIN_EVENT_SESSION_DOWN : PCEP_MAIN_EVENT_SESSION_UP,
+		     down ? XSTRDUP(MTYPE_PCEP, pcc_state->originator) : NULL);
+}
+
 void pcep_thread_schedule_sync_best_pce(struct ctrl_state *ctrl_state,
 					int pcc_id, int delay,
 					struct event **thread)
@@ -1095,13 +1104,15 @@ void pcep_ctrl_finalize_main_events(void)
 		case PCEP_MAIN_EVENT_UPDATE_CANDIDATE:
 			pcep_free_path((struct path *)data->payload);
 			break;
-		case PCEP_MAIN_EVENT_REMOVE_CANDIDATE_LSP: {
+		case PCEP_MAIN_EVENT_REMOVE_CANDIDATE_LSP:
+		case PCEP_MAIN_EVENT_SESSION_DOWN: {
 			char *originator = (char *)data->payload;

 			XFREE(MTYPE_PCEP, originator);
 			break;
 		}
 		case PCEP_MAIN_EVENT_START_SYNC:
+		case PCEP_MAIN_EVENT_SESSION_UP:
 		case PCEP_MAIN_EVENT_UNDEFINED:
 			break;
 		}
diff --git a/pathd/path_pcep_controller.h b/pathd/path_pcep_controller.h
index da55bc6bc3..3a1666f1bb 100644
--- a/pathd/path_pcep_controller.h
+++ b/pathd/path_pcep_controller.h
@@ -18,6 +18,8 @@ enum pcep_main_event_type {
 	PCEP_MAIN_EVENT_INITIATE_CANDIDATE,
 	PCEP_MAIN_EVENT_UPDATE_CANDIDATE,
 	PCEP_MAIN_EVENT_REMOVE_CANDIDATE_LSP,
+	PCEP_MAIN_EVENT_SESSION_DOWN,
+	PCEP_MAIN_EVENT_SESSION_UP,
 };

 typedef int (*pcep_main_event_handler_t)(enum pcep_main_event_type type,
@@ -150,6 +152,8 @@ void pcep_thread_schedule_session_timeout(struct ctrl_state *ctrl_state,
 					  struct event **thread);
 void pcep_thread_remove_candidate_path_segments(struct ctrl_state *ctrl_state,
 						struct pcc_state *pcc_state);
+void pcep_thread_session_state(struct ctrl_state *ctrl_state, struct pcc_state *pcc_state,
+			       bool down);

 void pcep_thread_schedule_sync_best_pce(struct ctrl_state *ctrl_state,
 					int pcc_id, int delay,
diff --git a/pathd/path_pcep_pcc.c b/pathd/path_pcep_pcc.c
index d897ae59a5..9827af92bc 100644
--- a/pathd/path_pcep_pcc.c
+++ b/pathd/path_pcep_pcc.c
@@ -1551,11 +1551,15 @@ void schedule_session_timeout(struct ctrl_state *ctrl_state,
 			      struct pcc_state *pcc_state)
 {
 	/* Each PCE has its own timeout, even when other PCEs are connected */
+	if (event_is_scheduled(pcc_state->t_session_timeout))
+		return;
+
 	pcep_thread_schedule_session_timeout(
 		ctrl_state, pcep_pcc_get_pcc_id(pcc_state),
 		pcc_state->pce_opts->config_opts
 			.session_timeout_inteval_seconds,
 		&pcc_state->t_session_timeout);
+	pcep_thread_session_state(ctrl_state, pcc_state, true);
 }

 void cancel_session_timeout(struct ctrl_state *ctrl_state,
@@ -1568,6 +1572,7 @@ void cancel_session_timeout(struct ctrl_state *ctrl_state,

 	PCEP_DEBUG_PCEP("Cancel session_timeout timer");
 	pcep_thread_cancel_timer(&pcc_state->t_session_timeout);
+	pcep_thread_session_state(ctrl_state, pcc_state, false);
 }

 void send_pcep_message(struct pcc_state *pcc_state, struct pcep_message *msg)