Commit 8e1f9ba3b3 for frr
commit 8e1f9ba3b316d8b7b5706c4afc2398e3d47a6b71
Author: Lincoln Thurlow <lincoln@isi.edu>
Date: Mon Nov 30 13:21:53 2026 -0800
zebra: deactivate SR policies when LSP is freed
An SR policy keeps a pointer to the LSP that resolves its first
segment, and nexthop_active() walks the NHLFEs of that LSP while the
policy is UP. When zebra frees the LSP, from its work queue or when
the last NHLFE of the LSP is removed, nothing tells the policy, so it
stays UP and the next route revalidation reads freed memory.
This commit marks the policies using an LSP down and clears their
pointer to it when the LSP is freed. lsp_free() can run inside a walk
of the LSP table, and withdrawing a binding SID can free another LSP,
so the binding SID withdrawal and the DOWN notifications are deferred
to an event. It withdraws the binding SID the policy had when its LSP
was freed. A policy that comes back up first cancels the event and
withdraws that binding SID itself, and deleting the policy runs a
pending withdrawal.
Signed-off-by: Lincoln Thurlow <lincoln@isi.edu>
diff --git a/zebra/zebra_mpls.c b/zebra/zebra_mpls.c
index 72b5efe0bd..f4c72e250a 100644
--- a/zebra/zebra_mpls.c
+++ b/zebra/zebra_mpls.c
@@ -1213,6 +1213,8 @@ static void lsp_free(struct hash *lsp_table, struct zebra_lsp **plsp)
zlog_debug("Free LSP in-label %u flags 0x%x",
lsp->ile.in_label, lsp->flags);
+ zebra_sr_policy_lsp_freed(lsp);
+
lsp_free_nhlfe(lsp);
hash_release(lsp_table, &lsp->ile);
diff --git a/zebra/zebra_srte.c b/zebra/zebra_srte.c
index d21b3e01d0..2abe09abb8 100644
--- a/zebra/zebra_srte.c
+++ b/zebra/zebra_srte.c
@@ -12,10 +12,12 @@
#include "zebra/zebra_mpls.h"
#include "zebra/zebra_rnh.h"
#include "zebra/zapi_msg.h"
+#include "zebra/zebra_router.h"
DEFINE_MTYPE_STATIC(ZEBRA, ZEBRA_SR_POLICY, "SR Policy");
static void zebra_sr_policy_deactivate(struct zebra_sr_policy *policy);
+static void zebra_sr_policy_withdraw(struct zebra_sr_policy *policy, mpls_label_t bsid);
/* Generate rb-tree of SR Policy instances. */
static inline int
@@ -49,8 +51,12 @@ struct zebra_sr_policy *zebra_sr_policy_add(uint32_t color,
void zebra_sr_policy_del(struct zebra_sr_policy *policy)
{
- if (policy->status == ZEBRA_SR_POLICY_UP)
+ if (policy->status == ZEBRA_SR_POLICY_UP) {
zebra_sr_policy_deactivate(policy);
+ } else if (event_is_scheduled(policy->t_withdraw)) {
+ event_cancel(&policy->t_withdraw);
+ zebra_sr_policy_withdraw(policy, policy->withdraw_bsid);
+ }
RB_REMOVE(zebra_sr_policy_instance_head, &zebra_sr_policy_instances,
policy);
XFREE(MTYPE_ZEBRA_SR_POLICY, policy);
@@ -226,6 +232,11 @@ static void zebra_sr_policy_notify_update(struct zebra_sr_policy *policy)
static void zebra_sr_policy_activate(struct zebra_sr_policy *policy,
struct zebra_lsp *lsp)
{
+ /* Drop the BSID nexthops of a freed LSP not yet withdrawn */
+ if (event_is_scheduled(policy->t_withdraw)) {
+ event_cancel(&policy->t_withdraw);
+ zebra_sr_policy_bsid_uninstall(policy, policy->withdraw_bsid);
+ }
policy->status = ZEBRA_SR_POLICY_UP;
policy->lsp = lsp;
(void)zebra_sr_policy_bsid_install(policy);
@@ -266,17 +277,49 @@ static void zebra_sr_policy_update(struct zebra_sr_policy *policy,
zebra_sr_policy_notify_update(policy);
}
-static void zebra_sr_policy_deactivate(struct zebra_sr_policy *policy)
+static void zebra_sr_policy_withdraw(struct zebra_sr_policy *policy, mpls_label_t bsid)
{
- policy->status = ZEBRA_SR_POLICY_DOWN;
- policy->lsp = NULL;
- zebra_sr_policy_bsid_uninstall(policy,
- policy->segment_list.local_label);
+ zebra_sr_policy_bsid_uninstall(policy, bsid);
zsend_sr_policy_notify_status(policy->color, &policy->endpoint,
policy->name, ZEBRA_SR_POLICY_DOWN);
zebra_sr_policy_notify_update(policy);
}
+static void zebra_sr_policy_deactivate(struct zebra_sr_policy *policy)
+{
+ policy->status = ZEBRA_SR_POLICY_DOWN;
+ policy->lsp = NULL;
+ zebra_sr_policy_withdraw(policy, policy->segment_list.local_label);
+}
+
+/* Still down: activate() cancels this event */
+static void zebra_sr_policy_withdraw_event(struct event *event)
+{
+ struct zebra_sr_policy *policy = EVENT_ARG(event);
+
+ zebra_sr_policy_withdraw(policy, policy->withdraw_bsid);
+}
+
+/* Called from lsp_free(), possibly inside a walk of the LSP table */
+void zebra_sr_policy_lsp_freed(struct zebra_lsp *lsp)
+{
+ struct zebra_sr_policy *policy;
+
+ if (RB_EMPTY(zebra_sr_policy_instance_head, &zebra_sr_policy_instances))
+ return;
+
+ RB_FOREACH (policy, zebra_sr_policy_instance_head, &zebra_sr_policy_instances) {
+ if (policy->lsp != lsp)
+ continue;
+ policy->status = ZEBRA_SR_POLICY_DOWN;
+ policy->lsp = NULL;
+ /* The BSID may change before the withdrawal runs */
+ policy->withdraw_bsid = policy->segment_list.local_label;
+ event_add_event(zrouter.master, zebra_sr_policy_withdraw_event, policy, 0,
+ &policy->t_withdraw);
+ }
+}
+
int zebra_sr_policy_validate(struct zebra_sr_policy *policy,
struct zapi_srte_tunnel *new_tunnel)
{
diff --git a/zebra/zebra_srte.h b/zebra/zebra_srte.h
index 1cdee45404..8c5ecca8dc 100644
--- a/zebra/zebra_srte.h
+++ b/zebra/zebra_srte.h
@@ -31,6 +31,8 @@ struct zebra_sr_policy {
struct zebra_lsp *lsp;
struct zebra_vrf *zvrf;
int sock;
+ struct event *t_withdraw;
+ mpls_label_t withdraw_bsid;
};
RB_HEAD(zebra_sr_policy_instance_head, zebra_sr_policy);
RB_PROTOTYPE(zebra_sr_policy_instance_head, zebra_sr_policy, entry,
@@ -41,6 +43,7 @@ extern struct zebra_sr_policy_instance_head zebra_sr_policy_instances;
struct zebra_sr_policy *
zebra_sr_policy_add(uint32_t color, struct ipaddr *endpoint, char *name);
void zebra_sr_policy_del(struct zebra_sr_policy *policy);
+void zebra_sr_policy_lsp_freed(struct zebra_lsp *lsp);
struct zebra_sr_policy *zebra_sr_policy_find(uint32_t color,
struct ipaddr *endpoint);
struct zebra_sr_policy *zebra_sr_policy_find_by_name(char *name);