mirror of
https://git.proxmox.com/git/mirror_frr
synced 2025-08-04 02:46:34 +00:00

* bgpd/(general) refcount struct peer and bgp_info, hence allowing us add work_queues for bgp_process. * bgpd/bgp_route.h: (struct bgp_info) Add 'lock' field for refcount. Add bgp_info_{lock,unlock} helper functions. Add bgp_info_{add,delete} helpers, to remove need for users managing locking/freeing of bgp_info and bgp_node's. * bgpd/bgp_table.h: (struct bgp_node) Add a flags field, and BGP_NODE_PROCESS_SCHEDULED to merge redundant processing of nodes. * bgpd/bgp_fsm.h: Make the ON/OFF/ADD/REMOVE macros lock and unlock peer reference as appropriate. * bgpd/bgp_damp.c: Remove its internal prototypes for bgp_info_delete/free. Just use bgp_info_delete. * bgpd/bgpd.h: (struct bgp_master) Add work_queue pointers. (struct peer) Add reference count 'lock' (peer_lock,peer_unlock) New helpers to take/release reference on struct peer. * bgpd/bgp_advertise.c: (general) Add peer and bgp_info refcounting and balance how references are taken and released. (bgp_advertise_free) release bgp_info reference, if appropriate (bgp_adj_out_free) unlock peer (bgp_advertise_clean) leave the adv references alone, or else call bgp_advertise_free cant unlock them. (bgp_adj_out_set) lock the peer on new adj's, leave the reference alone otherwise. lock the new bgp_info reference. (bgp_adj_in_set) lock the peer reference (bgp_adj_in_remove) and unlock it here (bgp_sync_delete) make hash_free on peer conditional, just in case. * bgpd/bgp_fsm.c: (general) document that the timers depend on bgp_event to release a peer reference. (bgp_fsm_change_status) moved up the file, unchanged. (bgp_stop) Decrement peer lock as many times as cancel_event canceled - shouldnt be needed but just in case. stream_fifo_clean of obuf made conditional, just in case. (bgp_event) always unlock the peer, regardless of return value of bgp_fsm_change_status. * bgpd/bgp_packet.c: (general) change several bgp_stop's to BGP_EVENT's. (bgp_read) Add a mysterious extra peer_unlock for ACCEPT_PEERs along with a comment on it. * bgpd/bgp_route.c: (general) Add refcounting of bgp_info, cleanup some of the resource management around bgp_info. Refcount peer. Add workqueues for bgp_process and clear_table. (bgp_info_new) make static (bgp_info_free) Ditto, and unlock the peer reference. (bgp_info_lock,bgp_info_unlock) new exported functions (bgp_info_add) Add a bgp_info to a bgp_node in correct fashion, taking care of reference counts. (bgp_info_delete) do the opposite of bgp_info_add. (bgp_process_rsclient) Converted into a work_queue work function. (bgp_process_main) ditto. (bgp_processq_del) process work queue item deconstructor (bgp_process_queue_init) process work queue init (bgp_process) call init function if required, set up queue item and add to queue, rather than calling process functions directly. (bgp_rib_remove) let bgp_info_delete manage bgp_info refcounts (bgp_rib_withdraw) ditto (bgp_update_rsclient) let bgp_info_add manage refcounts (bgp_update_main) ditto (bgp_clear_route_node) clear_node_queue work function, does per-node aspects of what bgp_clear_route_table did previously (bgp_clear_node_queue_del) clear_node_queue item delete function (bgp_clear_node_complete) clear_node_queue completion function, it unplugs the process queues, which have to be blocked while clear_node_queue is being processed to prevent a race. (bgp_clear_node_queue_init) init function for clear_node_queue work queues (bgp_clear_route_table) Sets up items onto a workqueue now, rather than clearing each node directly. Plugs both process queues to avoid potential race. (bgp_static_withdraw_rsclient) let bgp_info_{add,delete} manage bgp_info refcounts. (bgp_static_update_rsclient) ditto (bgp_static_update_main) ditto (bgp_static_update_vpnv4) ditto, remove unneeded cast. (bgp_static_withdraw) see bgp_static_withdraw_rsclient (bgp_static_withdraw_vpnv4) ditto (bgp_aggregate_{route,add,delete}) ditto (bgp_redistribute_{add,delete,withdraw}) ditto * bgpd/bgp_vty.c: (peer_rsclient_set_vty) lock rsclient list peer reference (peer_rsclient_unset_vty) ditto, but unlock same reference * bgpd/bgpd.c: (peer_free) handle frees of info to be kept for lifetime of struct peer. (peer_lock,peer_unlock) peer refcount helpers (peer_new) add initial refcounts (peer_create,peer_create_accept) lock peer as appropriate (peer_delete) unlock as appropriate, move out some free's to peer_free. (peer_group_bind,peer_group_unbind) peer refcounting as appropriate. (bgp_create) check CALLOC return value. (bgp_terminate) free workqueues too. * lib/memtypes.c: Add MTYPE_BGP_PROCESS_QUEUE and MTYPE_BGP_CLEAR_NODE_QUEUE
412 lines
9.2 KiB
C
412 lines
9.2 KiB
C
/* BGP advertisement and adjacency
|
||
Copyright (C) 1996, 97, 98, 99, 2000 Kunihiro Ishiguro
|
||
|
||
This file is part of GNU Zebra.
|
||
|
||
GNU Zebra is free software; you can redistribute it and/or modify it
|
||
under the terms of the GNU General Public License as published by the
|
||
Free Software Foundation; either version 2, or (at your option) any
|
||
later version.
|
||
|
||
GNU Zebra is distributed in the hope that it will be useful, but
|
||
WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
General Public License for more details.
|
||
|
||
You should have received a copy of the GNU General Public License
|
||
along with GNU Zebra; see the file COPYING. If not, write to the Free
|
||
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
||
02111-1307, USA. */
|
||
|
||
#include <zebra.h>
|
||
|
||
#include "command.h"
|
||
#include "memory.h"
|
||
#include "prefix.h"
|
||
#include "hash.h"
|
||
#include "thread.h"
|
||
|
||
#include "bgpd/bgpd.h"
|
||
#include "bgpd/bgp_table.h"
|
||
#include "bgpd/bgp_route.h"
|
||
#include "bgpd/bgp_advertise.h"
|
||
#include "bgpd/bgp_attr.h"
|
||
#include "bgpd/bgp_aspath.h"
|
||
#include "bgpd/bgp_packet.h"
|
||
#include "bgpd/bgp_fsm.h"
|
||
#include "bgpd/bgp_mplsvpn.h"
|
||
|
||
/* BGP advertise attribute is used for pack same attribute update into
|
||
one packet. To do that we maintain attribute hash in struct
|
||
peer. */
|
||
static struct bgp_advertise_attr *
|
||
baa_new ()
|
||
{
|
||
return (struct bgp_advertise_attr *)
|
||
XCALLOC (MTYPE_BGP_ADVERTISE_ATTR, sizeof (struct bgp_advertise_attr));
|
||
}
|
||
|
||
static void
|
||
baa_free (struct bgp_advertise_attr *baa)
|
||
{
|
||
XFREE (MTYPE_BGP_ADVERTISE_ATTR, baa);
|
||
}
|
||
|
||
static void *
|
||
baa_hash_alloc (struct bgp_advertise_attr *ref)
|
||
{
|
||
struct bgp_advertise_attr *baa;
|
||
|
||
baa = baa_new ();
|
||
baa->attr = ref->attr;
|
||
return baa;
|
||
}
|
||
|
||
static unsigned int
|
||
baa_hash_key (struct bgp_advertise_attr *baa)
|
||
{
|
||
return attrhash_key_make (baa->attr);
|
||
}
|
||
|
||
static int
|
||
baa_hash_cmp (struct bgp_advertise_attr *baa1, struct bgp_advertise_attr *baa2)
|
||
{
|
||
return attrhash_cmp (baa1->attr, baa2->attr);
|
||
}
|
||
|
||
/* BGP update and withdraw information is stored in BGP advertise
|
||
structure. This structure is referred from BGP adjacency
|
||
information. */
|
||
static struct bgp_advertise *
|
||
bgp_advertise_new ()
|
||
{
|
||
return (struct bgp_advertise *)
|
||
XCALLOC (MTYPE_BGP_ADVERTISE, sizeof (struct bgp_advertise));
|
||
}
|
||
|
||
void
|
||
bgp_advertise_free (struct bgp_advertise *adv)
|
||
{
|
||
if (adv->binfo)
|
||
bgp_info_unlock (adv->binfo); /* bgp_advertise bgp_info reference */
|
||
XFREE (MTYPE_BGP_ADVERTISE, adv);
|
||
}
|
||
|
||
void
|
||
bgp_advertise_add (struct bgp_advertise_attr *baa,
|
||
struct bgp_advertise *adv)
|
||
{
|
||
adv->next = baa->adv;
|
||
if (baa->adv)
|
||
baa->adv->prev = adv;
|
||
baa->adv = adv;
|
||
}
|
||
|
||
void
|
||
bgp_advertise_delete (struct bgp_advertise_attr *baa,
|
||
struct bgp_advertise *adv)
|
||
{
|
||
if (adv->next)
|
||
adv->next->prev = adv->prev;
|
||
if (adv->prev)
|
||
adv->prev->next = adv->next;
|
||
else
|
||
baa->adv = adv->next;
|
||
}
|
||
|
||
static struct bgp_advertise_attr *
|
||
bgp_advertise_intern (struct hash *hash, struct attr *attr)
|
||
{
|
||
struct bgp_advertise_attr ref;
|
||
struct bgp_advertise_attr *baa;
|
||
|
||
ref.attr = bgp_attr_intern (attr);
|
||
baa = (struct bgp_advertise_attr *) hash_get (hash, &ref, baa_hash_alloc);
|
||
baa->refcnt++;
|
||
|
||
return baa;
|
||
}
|
||
|
||
void
|
||
bgp_advertise_unintern (struct hash *hash, struct bgp_advertise_attr *baa)
|
||
{
|
||
if (baa->refcnt)
|
||
baa->refcnt--;
|
||
|
||
if (baa->refcnt && baa->attr)
|
||
bgp_attr_unintern (baa->attr);
|
||
else
|
||
{
|
||
if (baa->attr)
|
||
{
|
||
hash_release (hash, baa);
|
||
bgp_attr_unintern (baa->attr);
|
||
}
|
||
baa_free (baa);
|
||
}
|
||
}
|
||
|
||
/* BGP adjacency keeps minimal advertisement information. */
|
||
void
|
||
bgp_adj_out_free (struct bgp_adj_out *adj)
|
||
{
|
||
peer_unlock (adj->peer); /* adj_out peer reference */
|
||
XFREE (MTYPE_BGP_ADJ_OUT, adj);
|
||
}
|
||
|
||
int
|
||
bgp_adj_out_lookup (struct peer *peer, struct prefix *p,
|
||
afi_t afi, safi_t safi, struct bgp_node *rn)
|
||
{
|
||
struct bgp_adj_out *adj;
|
||
|
||
for (adj = rn->adj_out; adj; adj = adj->next)
|
||
if (adj->peer == peer)
|
||
break;
|
||
|
||
if (! adj)
|
||
return 0;
|
||
|
||
return (adj->adv
|
||
? (adj->adv->baa ? 1 : 0)
|
||
: (adj->attr ? 1 : 0));
|
||
}
|
||
|
||
struct bgp_advertise *
|
||
bgp_advertise_clean (struct peer *peer, struct bgp_adj_out *adj,
|
||
afi_t afi, safi_t safi)
|
||
{
|
||
struct bgp_advertise *adv;
|
||
struct bgp_advertise_attr *baa;
|
||
struct bgp_advertise *next;
|
||
|
||
adv = adj->adv;
|
||
baa = adv->baa;
|
||
next = NULL;
|
||
|
||
if (baa)
|
||
{
|
||
/* Unlink myself from advertise attribute FIFO. */
|
||
bgp_advertise_delete (baa, adv);
|
||
|
||
/* Fetch next advertise candidate. */
|
||
next = baa->adv;
|
||
|
||
/* Unintern BGP advertise attribute. */
|
||
bgp_advertise_unintern (peer->hash[afi][safi], baa);
|
||
}
|
||
|
||
/* Unlink myself from advertisement FIFO. */
|
||
FIFO_DEL (adv);
|
||
|
||
/* Free memory. */
|
||
bgp_advertise_free (adj->adv);
|
||
adj->adv = NULL;
|
||
|
||
return next;
|
||
}
|
||
|
||
void
|
||
bgp_adj_out_set (struct bgp_node *rn, struct peer *peer, struct prefix *p,
|
||
struct attr *attr, afi_t afi, safi_t safi,
|
||
struct bgp_info *binfo)
|
||
{
|
||
struct bgp_adj_out *adj = NULL;
|
||
struct bgp_advertise *adv;
|
||
|
||
#ifdef DISABLE_BGP_ANNOUNCE
|
||
return;
|
||
#endif /* DISABLE_BGP_ANNOUNCE */
|
||
|
||
/* Look for adjacency information. */
|
||
if (rn)
|
||
{
|
||
for (adj = rn->adj_out; adj; adj = adj->next)
|
||
if (adj->peer == peer)
|
||
break;
|
||
}
|
||
|
||
if (! adj)
|
||
{
|
||
adj = XCALLOC (MTYPE_BGP_ADJ_OUT, sizeof (struct bgp_adj_out));
|
||
adj->peer = peer_lock (peer); /* adj_out peer reference */
|
||
|
||
if (rn)
|
||
{
|
||
BGP_ADJ_OUT_ADD (rn, adj);
|
||
bgp_lock_node (rn);
|
||
}
|
||
}
|
||
|
||
if (adj->adv)
|
||
bgp_advertise_clean (peer, adj, afi, safi);
|
||
|
||
adj->adv = bgp_advertise_new ();
|
||
|
||
adv = adj->adv;
|
||
adv->rn = rn;
|
||
|
||
assert (adv->binfo == NULL);
|
||
adv->binfo = bgp_info_lock (binfo); /* bgp_info adj_out reference */
|
||
|
||
if (attr)
|
||
adv->baa = bgp_advertise_intern (peer->hash[afi][safi], attr);
|
||
else
|
||
adv->baa = baa_new ();
|
||
adv->adj = adj;
|
||
|
||
/* Add new advertisement to advertisement attribute list. */
|
||
bgp_advertise_add (adv->baa, adv);
|
||
|
||
FIFO_ADD (&peer->sync[afi][safi]->update, &adv->fifo);
|
||
}
|
||
|
||
void
|
||
bgp_adj_out_unset (struct bgp_node *rn, struct peer *peer, struct prefix *p,
|
||
afi_t afi, safi_t safi)
|
||
{
|
||
struct bgp_adj_out *adj;
|
||
struct bgp_advertise *adv;
|
||
|
||
#ifdef DISABLE_BGP_ANNOUNCE
|
||
return;
|
||
#endif /* DISABLE_BGP_ANNOUNCE */
|
||
|
||
/* Lookup existing adjacency, if it is not there return immediately. */
|
||
for (adj = rn->adj_out; adj; adj = adj->next)
|
||
if (adj->peer == peer)
|
||
break;
|
||
|
||
if (! adj)
|
||
return;
|
||
|
||
/* Clearn up previous advertisement. */
|
||
if (adj->adv)
|
||
bgp_advertise_clean (peer, adj, afi, safi);
|
||
|
||
if (adj->attr)
|
||
{
|
||
/* We need advertisement structure. */
|
||
adj->adv = bgp_advertise_new ();
|
||
adv = adj->adv;
|
||
adv->rn = rn;
|
||
adv->adj = adj;
|
||
|
||
/* Add to synchronization entry for withdraw announcement. */
|
||
FIFO_ADD (&peer->sync[afi][safi]->withdraw, &adv->fifo);
|
||
|
||
/* Schedule packet write. */
|
||
BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
|
||
}
|
||
else
|
||
{
|
||
/* Remove myself from adjacency. */
|
||
BGP_ADJ_OUT_DEL (rn, adj);
|
||
|
||
/* Free allocated information. */
|
||
bgp_adj_out_free (adj);
|
||
|
||
bgp_unlock_node (rn);
|
||
}
|
||
}
|
||
|
||
void
|
||
bgp_adj_out_remove (struct bgp_node *rn, struct bgp_adj_out *adj,
|
||
struct peer *peer, afi_t afi, safi_t safi)
|
||
{
|
||
if (adj->attr)
|
||
bgp_attr_unintern (adj->attr);
|
||
|
||
if (adj->adv)
|
||
bgp_advertise_clean (peer, adj, afi, safi);
|
||
|
||
BGP_ADJ_OUT_DEL (rn, adj);
|
||
bgp_adj_out_free (adj);
|
||
}
|
||
|
||
void
|
||
bgp_adj_in_set (struct bgp_node *rn, struct peer *peer, struct attr *attr)
|
||
{
|
||
struct bgp_adj_in *adj;
|
||
|
||
for (adj = rn->adj_in; adj; adj = adj->next)
|
||
{
|
||
if (adj->peer == peer)
|
||
{
|
||
if (adj->attr != attr)
|
||
{
|
||
bgp_attr_unintern (adj->attr);
|
||
adj->attr = bgp_attr_intern (attr);
|
||
}
|
||
return;
|
||
}
|
||
}
|
||
adj = XCALLOC (MTYPE_BGP_ADJ_IN, sizeof (struct bgp_adj_in));
|
||
adj->peer = peer_lock (peer); /* adj_in peer reference */
|
||
adj->attr = bgp_attr_intern (attr);
|
||
BGP_ADJ_IN_ADD (rn, adj);
|
||
bgp_lock_node (rn);
|
||
}
|
||
|
||
void
|
||
bgp_adj_in_remove (struct bgp_node *rn, struct bgp_adj_in *bai)
|
||
{
|
||
bgp_attr_unintern (bai->attr);
|
||
BGP_ADJ_IN_DEL (rn, bai);
|
||
peer_unlock (bai->peer); /* adj_in peer reference */
|
||
XFREE (MTYPE_BGP_ADJ_IN, bai);
|
||
}
|
||
|
||
void
|
||
bgp_adj_in_unset (struct bgp_node *rn, struct peer *peer)
|
||
{
|
||
struct bgp_adj_in *adj;
|
||
|
||
for (adj = rn->adj_in; adj; adj = adj->next)
|
||
if (adj->peer == peer)
|
||
break;
|
||
|
||
if (! adj)
|
||
return;
|
||
|
||
bgp_adj_in_remove (rn, adj);
|
||
bgp_unlock_node (rn);
|
||
}
|
||
|
||
void
|
||
bgp_sync_init (struct peer *peer)
|
||
{
|
||
afi_t afi;
|
||
safi_t safi;
|
||
struct bgp_synchronize *sync;
|
||
|
||
for (afi = AFI_IP; afi < AFI_MAX; afi++)
|
||
for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
|
||
{
|
||
sync = XCALLOC (MTYPE_TMP, sizeof (struct bgp_synchronize));
|
||
FIFO_INIT (&sync->update);
|
||
FIFO_INIT (&sync->withdraw);
|
||
FIFO_INIT (&sync->withdraw_low);
|
||
peer->sync[afi][safi] = sync;
|
||
peer->hash[afi][safi] = hash_create (baa_hash_key, baa_hash_cmp);
|
||
}
|
||
}
|
||
|
||
void
|
||
bgp_sync_delete (struct peer *peer)
|
||
{
|
||
afi_t afi;
|
||
safi_t safi;
|
||
|
||
for (afi = AFI_IP; afi < AFI_MAX; afi++)
|
||
for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
|
||
{
|
||
if (peer->sync[afi][safi])
|
||
XFREE (MTYPE_TMP, peer->sync[afi][safi]);
|
||
peer->sync[afi][safi] = NULL;
|
||
|
||
if (peer->hash[afi][safi])
|
||
hash_free (peer->hash[afi][safi]);
|
||
}
|
||
}
|