]> git.puffer.fish Git - mirror/frr.git/commitdiff
bgpd: Create destructor function for `struct peer_connection`
authorDonald Sharp <sharpd@nvidia.com>
Fri, 30 Apr 2021 19:26:08 +0000 (15:26 -0400)
committerDonald Sharp <sharpd@nvidia.com>
Fri, 18 Aug 2023 13:29:04 +0000 (09:29 -0400)
Create a destructor function to free up memory associated
with the io buffers.

Signed-off-by: Donald Sharp <sharpd@nvidia.com>
bgpd/bgpd.c
bgpd/bgpd.h
bgpd/rfapi/rfapi.c
bgpd/rfapi/vnc_zebra.c

index c9cd55ee2343714b8a425ad8925922fd5cfae413..76c7859bab0ca143eea804a13f9e7c0f5c69eab6 100644 (file)
@@ -1110,6 +1110,36 @@ enum bgp_peer_sort peer_sort_lookup(struct peer *peer)
        return peer->sort;
 }
 
+/*
+ * Mutex will be freed in peer_connection_free
+ * this is a convenience function to reduce cut-n-paste
+ */
+void bgp_peer_connection_buffers_free(struct peer_connection *connection)
+{
+       frr_with_mutex (&connection->io_mtx) {
+               if (connection->ibuf) {
+                       stream_fifo_free(connection->ibuf);
+                       connection->ibuf = NULL;
+               }
+
+               if (connection->obuf) {
+                       stream_fifo_free(connection->obuf);
+                       connection->obuf = NULL;
+               }
+
+               if (connection->ibuf_work) {
+                       ringbuf_del(connection->ibuf_work);
+                       connection->ibuf_work = NULL;
+               }
+       }
+}
+
+static void bgp_peer_connection_free(struct peer_connection *connection)
+{
+       bgp_peer_connection_buffers_free(connection);
+       pthread_mutex_destroy(&connection->io_mtx);
+}
+
 static void peer_free(struct peer *peer)
 {
        afi_t afi;
@@ -1132,7 +1162,7 @@ static void peer_free(struct peer *peer)
        assert(!peer->t_read);
        BGP_EVENT_FLUSH(peer);
 
-       pthread_mutex_destroy(&peer->connection.io_mtx);
+       bgp_peer_connection_free(&peer->connection);
 
        /* Free connected nexthop, if present */
        if (CHECK_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE)
@@ -2590,22 +2620,6 @@ int peer_delete(struct peer *peer)
                peer_unlock(peer); /* bgp peer list reference */
        }
 
-       /* Buffers.  */
-       if (peer->connection.ibuf) {
-               stream_fifo_free(peer->connection.ibuf);
-               peer->connection.ibuf = NULL;
-       }
-
-       if (peer->connection.obuf) {
-               stream_fifo_free(peer->connection.obuf);
-               peer->connection.obuf = NULL;
-       }
-
-       if (peer->connection.ibuf_work) {
-               ringbuf_del(peer->connection.ibuf_work);
-               peer->connection.ibuf_work = NULL;
-       }
-
        /* Local and remote addresses. */
        if (peer->su_local) {
                sockunion_free(peer->su_local);
index 78876f65e148b6d3dba7dfdc9262e8923343f090..7855d1d765fc9ad1f8b58a9278a1ca6127611d82 100644 (file)
@@ -1130,6 +1130,7 @@ struct peer_connection {
 
        struct ringbuf *ibuf_work; // WiP buffer used by bgp_read() only
 };
+extern void bgp_peer_connection_buffers_free(struct peer_connection *connection);
 
 /* BGP neighbor structure. */
 struct peer {
index 2de9508e9d1fdfb4d19cf3e52c55e56b14b146dc..d24a07cd8a829bc8c7fd50009c8643a66be03404 100644 (file)
@@ -1238,24 +1238,7 @@ static int rfapi_open_inner(struct rfapi_descriptor *rfd, struct bgp *bgp,
        rfd->peer = peer_new(bgp);
        rfd->peer->status = Established; /* keep bgp core happy */
 
-       /*
-        * since this peer is not on the I/O thread, this lock is not strictly
-        * necessary, but serves as a reminder to those who may meddle...
-        */
-       frr_with_mutex (&rfd->peer->connection.io_mtx) {
-               // we don't need any I/O related facilities
-               if (rfd->peer->connection.ibuf)
-                       stream_fifo_free(rfd->peer->connection.ibuf);
-               if (rfd->peer->connection.obuf)
-                       stream_fifo_free(rfd->peer->connection.obuf);
-
-               if (rfd->peer->connection.ibuf_work)
-                       ringbuf_del(rfd->peer->connection.ibuf_work);
-
-               rfd->peer->connection.ibuf = NULL;
-               rfd->peer->connection.obuf = NULL;
-               rfd->peer->connection.ibuf_work = NULL;
-       }
+       bgp_peer_connection_buffers_free(&rfd->peer->connection);
 
        { /* base code assumes have valid host pointer */
                char buf[INET6_ADDRSTRLEN];
index edbe2e9f31939a074df36116b424ffc70685c65f..8a30edce91ca854c9e5c68386463c4bead190e06 100644 (file)
@@ -174,28 +174,8 @@ static void vnc_redistribute_add(struct prefix *p, uint32_t metric,
                        vncHD1VR.peer->status =
                                Established; /* keep bgp core happy */
 
-                       /*
-                        * since this peer is not on the I/O thread, this lock
-                        * is not strictly necessary, but serves as a reminder
-                        * to those who may meddle...
-                        */
-                       frr_with_mutex (&vncHD1VR.peer->connection.io_mtx) {
-                               // we don't need any I/O related facilities
-                               if (vncHD1VR.peer->connection.ibuf)
-                                       stream_fifo_free(
-                                               vncHD1VR.peer->connection.ibuf);
-                               if (vncHD1VR.peer->connection.obuf)
-                                       stream_fifo_free(
-                                               vncHD1VR.peer->connection.obuf);
-
-                               if (vncHD1VR.peer->connection.ibuf_work)
-                                       ringbuf_del(vncHD1VR.peer->connection
-                                                           .ibuf_work);
-
-                               vncHD1VR.peer->connection.ibuf = NULL;
-                               vncHD1VR.peer->connection.obuf = NULL;
-                               vncHD1VR.peer->connection.ibuf_work = NULL;
-                       }
+                       bgp_peer_connection_buffers_free(
+                               &vncHD1VR.peer->connection);
 
                        /* base code assumes have valid host pointer */
                        vncHD1VR.peer->host =