]> git.puffer.fish Git - matthieu/frr.git/commitdiff
bgpd: On shutdown free up memory leak found by topotest
authorDonald Sharp <sharpd@nvidia.com>
Tue, 8 Apr 2025 15:47:50 +0000 (11:47 -0400)
committerMergify <37929162+mergify[bot]@users.noreply.github.com>
Tue, 8 Apr 2025 19:32:09 +0000 (19:32 +0000)
This commit fixes two types of problems:

a) Avoidance of cleaning up memory when a instance is
hidden, thus causing it never to be freed on shutdown

b) In some instances bgp_create is called 2 times
for some code.  We are double allocating memory
and dropping it on the second allocation.

Signed-off-by: Donald Sharp <sharpd@nvidia.com>
(cherry picked from commit b18c309015f3b91bc65af2f64ed3e6f845b855eb)

bgpd/bgpd.c

index 342c3c6be65cee437603babc8a964be5da5f4d43..c5d902521e19300796668ca86360672b8d3e2d10 100644 (file)
@@ -3538,7 +3538,8 @@ peer_init:
                bgp->vpn_policy[afi].tovpn_zebra_vrf_label_last_sent =
                        MPLS_LABEL_NONE;
 
-               bgp->vpn_policy[afi].import_vrf = list_new();
+               if (!bgp->vpn_policy[afi].import_vrf)
+                       bgp->vpn_policy[afi].import_vrf = list_new();
                bgp->vpn_policy[afi].import_vrf->del =
                        bgp_vrf_string_name_delete;
                if (!hidden) {
@@ -3556,7 +3557,7 @@ peer_init:
 
        bgp_mplsvpn_nh_label_bind_cache_init(&bgp->mplsvpn_nh_label_bind);
 
-       if (name)
+       if (name && !bgp->name)
                bgp->name = XSTRDUP(MTYPE_BGP_NAME, name);
 
        event_add_timer(bm->master, bgp_startup_timer_expire, bgp,
@@ -4177,7 +4178,7 @@ int bgp_delete(struct bgp *bgp)
                peer_delete(peer);
        }
 
-       if (bgp->peer_self && !IS_BGP_INSTANCE_HIDDEN(bgp)) {
+       if (bgp->peer_self && (!IS_BGP_INSTANCE_HIDDEN(bgp) || bm->terminating)) {
                peer_delete(bgp->peer_self);
                bgp->peer_self = NULL;
        }
@@ -4187,7 +4188,7 @@ int bgp_delete(struct bgp *bgp)
 /* TODO - Other memory may need to be freed - e.g., NHT */
 
 #ifdef ENABLE_BGP_VNC
-       if (!IS_BGP_INSTANCE_HIDDEN(bgp))
+       if (!IS_BGP_INSTANCE_HIDDEN(bgp) || bm->terminating)
                rfapi_delete(bgp);
 #endif
 
@@ -4238,7 +4239,7 @@ int bgp_delete(struct bgp *bgp)
                bgp_zebra_instance_deregister(bgp);
        }
 
-       if (!IS_BGP_INSTANCE_HIDDEN(bgp)) {
+       if (!IS_BGP_INSTANCE_HIDDEN(bgp) || bm->terminating) {
                /* Remove visibility via the master list -
                 * there may however still be routes to be processed
                 * still referencing the struct bgp.
@@ -4250,7 +4251,7 @@ int bgp_delete(struct bgp *bgp)
 
        vrf = bgp_vrf_lookup_by_instance_type(bgp);
        bgp_handle_socket(bgp, vrf, VRF_UNKNOWN, false);
-       if (vrf && !IS_BGP_INSTANCE_HIDDEN(bgp))
+       if (vrf && (!IS_BGP_INSTANCE_HIDDEN(bgp) || bm->terminating))
                bgp_vrf_unlink(bgp, vrf);
 
        /* Update EVPN VRF pointer */
@@ -4261,7 +4262,7 @@ int bgp_delete(struct bgp *bgp)
                        bgp_set_evpn(bgp_get_default());
        }
 
-       if (!IS_BGP_INSTANCE_HIDDEN(bgp)) {
+       if (!IS_BGP_INSTANCE_HIDDEN(bgp) || bm->terminating) {
                if (bgp->process_queue)
                        work_queue_free_and_null(&bgp->process_queue);
                bgp_unlock(bgp); /* initial reference */