bgpd: Create destructor function for struct peer_connection

Create a destructor function to free up memory associated
with the io buffers.

Signed-off-by: Donald Sharp <sharpd@nvidia.com>
This commit is contained in:
Donald Sharp 2021-04-30 15:26:08 -04:00
parent 1f32eb30d9
commit 3b2d89b0a3
4 changed files with 35 additions and 57 deletions

View 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);

View 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 {

View 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];

View 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 =