]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_advertise.c
bgp: rename bgp_node to bgp_dest
[mirror_frr.git] / bgpd / bgp_advertise.c
CommitLineData
718e3744 1/* BGP advertisement and adjacency
896014f4
DL
2 * Copyright (C) 1996, 97, 98, 99, 2000 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
718e3744 20
21#include <zebra.h>
22
23#include "command.h"
24#include "memory.h"
25#include "prefix.h"
26#include "hash.h"
27#include "thread.h"
3f9c7369 28#include "queue.h"
039f3a34 29#include "filter.h"
718e3744 30
31#include "bgpd/bgpd.h"
32#include "bgpd/bgp_table.h"
33#include "bgpd/bgp_route.h"
34#include "bgpd/bgp_advertise.h"
35#include "bgpd/bgp_attr.h"
d889623f 36#include "bgpd/bgp_debug.h"
718e3744 37#include "bgpd/bgp_aspath.h"
38#include "bgpd/bgp_packet.h"
39#include "bgpd/bgp_fsm.h"
40#include "bgpd/bgp_mplsvpn.h"
3f9c7369 41#include "bgpd/bgp_updgrp.h"
6b0655a2 42
718e3744 43/* BGP advertise attribute is used for pack same attribute update into
44 one packet. To do that we maintain attribute hash in struct
45 peer. */
d62a17ae 46struct bgp_advertise_attr *baa_new(void)
718e3744 47{
9f5dc319
QY
48 return XCALLOC(MTYPE_BGP_ADVERTISE_ATTR,
49 sizeof(struct bgp_advertise_attr));
718e3744 50}
51
d62a17ae 52static void baa_free(struct bgp_advertise_attr *baa)
718e3744 53{
d62a17ae 54 XFREE(MTYPE_BGP_ADVERTISE_ATTR, baa);
718e3744 55}
56
d62a17ae 57static void *baa_hash_alloc(void *p)
718e3744 58{
d62a17ae 59 struct bgp_advertise_attr *ref = (struct bgp_advertise_attr *)p;
60 struct bgp_advertise_attr *baa;
718e3744 61
d62a17ae 62 baa = baa_new();
63 baa->attr = ref->attr;
64 return baa;
718e3744 65}
66
d8b87afe 67unsigned int baa_hash_key(const void *p)
718e3744 68{
d8b87afe 69 const struct bgp_advertise_attr *baa = p;
923de654 70
d62a17ae 71 return attrhash_key_make(baa->attr);
718e3744 72}
73
74df8d6d 74bool baa_hash_cmp(const void *p1, const void *p2)
718e3744 75{
d62a17ae 76 const struct bgp_advertise_attr *baa1 = p1;
77 const struct bgp_advertise_attr *baa2 = p2;
923de654 78
d62a17ae 79 return attrhash_cmp(baa1->attr, baa2->attr);
718e3744 80}
6b0655a2 81
718e3744 82/* BGP update and withdraw information is stored in BGP advertise
83 structure. This structure is referred from BGP adjacency
84 information. */
d62a17ae 85struct bgp_advertise *bgp_advertise_new(void)
718e3744 86{
9f5dc319 87 return XCALLOC(MTYPE_BGP_ADVERTISE, sizeof(struct bgp_advertise));
718e3744 88}
89
d62a17ae 90void bgp_advertise_free(struct bgp_advertise *adv)
718e3744 91{
9b6d8fcf
DS
92 if (adv->pathi)
93 /* bgp_advertise bgp_path_info reference */
94 bgp_path_info_unlock(adv->pathi);
d62a17ae 95 XFREE(MTYPE_BGP_ADVERTISE, adv);
718e3744 96}
97
d62a17ae 98void bgp_advertise_add(struct bgp_advertise_attr *baa,
99 struct bgp_advertise *adv)
718e3744 100{
d62a17ae 101 adv->next = baa->adv;
102 if (baa->adv)
103 baa->adv->prev = adv;
104 baa->adv = adv;
718e3744 105}
106
d62a17ae 107void bgp_advertise_delete(struct bgp_advertise_attr *baa,
108 struct bgp_advertise *adv)
718e3744 109{
d62a17ae 110 if (adv->next)
111 adv->next->prev = adv->prev;
112 if (adv->prev)
113 adv->prev->next = adv->next;
114 else
115 baa->adv = adv->next;
718e3744 116}
117
d62a17ae 118struct bgp_advertise_attr *bgp_advertise_intern(struct hash *hash,
119 struct attr *attr)
718e3744 120{
d62a17ae 121 struct bgp_advertise_attr ref;
122 struct bgp_advertise_attr *baa;
718e3744 123
d62a17ae 124 ref.attr = bgp_attr_intern(attr);
125 baa = (struct bgp_advertise_attr *)hash_get(hash, &ref, baa_hash_alloc);
126 baa->refcnt++;
718e3744 127
d62a17ae 128 return baa;
718e3744 129}
130
d62a17ae 131void bgp_advertise_unintern(struct hash *hash, struct bgp_advertise_attr *baa)
718e3744 132{
d62a17ae 133 if (baa->refcnt)
134 baa->refcnt--;
135
136 if (baa->refcnt && baa->attr)
137 bgp_attr_unintern(&baa->attr);
138 else {
139 if (baa->attr) {
140 hash_release(hash, baa);
141 bgp_attr_unintern(&baa->attr);
142 }
143 baa_free(baa);
718e3744 144 }
718e3744 145}
6b0655a2 146
9bcb3eef 147bool bgp_adj_out_lookup(struct peer *peer, struct bgp_dest *dest,
3dc339cd 148 uint32_t addpath_tx_id)
718e3744 149{
d62a17ae 150 struct bgp_adj_out *adj;
151 struct peer_af *paf;
152 afi_t afi;
153 safi_t safi;
154 int addpath_capable;
155
9bcb3eef 156 RB_FOREACH (adj, bgp_adj_out_rb, &dest->adj_out)
a2addae8
RW
157 SUBGRP_FOREACH_PEER (adj->subgroup, paf)
158 if (paf->peer == peer) {
159 afi = SUBGRP_AFI(adj->subgroup);
160 safi = SUBGRP_SAFI(adj->subgroup);
161 addpath_capable =
162 bgp_addpath_encode_tx(peer, afi, safi);
163
164 /* Match on a specific addpath_tx_id if we are
165 * using addpath for
166 * this
167 * peer and if an addpath_tx_id was specified */
168 if (addpath_capable && addpath_tx_id
169 && adj->addpath_tx_id != addpath_tx_id)
170 continue;
171
3dc339cd
DA
172 return (adj->adv
173 ? (adj->adv->baa ? true : false)
174 : (adj->attr ? true : false));
a2addae8 175 }
718e3744 176
3dc339cd 177 return false;
718e3744 178}
179
6b0655a2 180
9bcb3eef 181void bgp_adj_in_set(struct bgp_dest *dest, struct peer *peer, struct attr *attr,
d7c0a89a 182 uint32_t addpath_id)
718e3744 183{
d62a17ae 184 struct bgp_adj_in *adj;
185
9bcb3eef 186 for (adj = dest->adj_in; adj; adj = adj->next) {
d62a17ae 187 if (adj->peer == peer && adj->addpath_rx_id == addpath_id) {
188 if (adj->attr != attr) {
189 bgp_attr_unintern(&adj->attr);
190 adj->attr = bgp_attr_intern(attr);
191 }
192 return;
193 }
718e3744 194 }
d62a17ae 195 adj = XCALLOC(MTYPE_BGP_ADJ_IN, sizeof(struct bgp_adj_in));
196 adj->peer = peer_lock(peer); /* adj_in peer reference */
197 adj->attr = bgp_attr_intern(attr);
6566d669 198 adj->uptime = bgp_clock();
d62a17ae 199 adj->addpath_rx_id = addpath_id;
9bcb3eef
DS
200 BGP_ADJ_IN_ADD(dest, adj);
201 bgp_dest_lock_node(dest);
718e3744 202}
203
9bcb3eef 204void bgp_adj_in_remove(struct bgp_dest *dest, struct bgp_adj_in *bai)
718e3744 205{
d62a17ae 206 bgp_attr_unintern(&bai->attr);
9bcb3eef 207 BGP_ADJ_IN_DEL(dest, bai);
d62a17ae 208 peer_unlock(bai->peer); /* adj_in peer reference */
209 XFREE(MTYPE_BGP_ADJ_IN, bai);
718e3744 210}
211
9bcb3eef 212bool bgp_adj_in_unset(struct bgp_dest *dest, struct peer *peer,
3dc339cd 213 uint32_t addpath_id)
718e3744 214{
d62a17ae 215 struct bgp_adj_in *adj;
216 struct bgp_adj_in *adj_next;
718e3744 217
9bcb3eef 218 adj = dest->adj_in;
6b87f736 219
d62a17ae 220 if (!adj)
3dc339cd 221 return false;
6b87f736 222
d62a17ae 223 while (adj) {
224 adj_next = adj->next;
718e3744 225
d62a17ae 226 if (adj->peer == peer && adj->addpath_rx_id == addpath_id) {
9bcb3eef
DS
227 bgp_adj_in_remove(dest, adj);
228 bgp_dest_unlock_node(dest);
d62a17ae 229 }
718e3744 230
d62a17ae 231 adj = adj_next;
232 }
6b87f736 233
3dc339cd 234 return true;
718e3744 235}
6b0655a2 236
d62a17ae 237void bgp_sync_init(struct peer *peer)
718e3744 238{
d62a17ae 239 afi_t afi;
240 safi_t safi;
241 struct bgp_synchronize *sync;
242
05c7a1cc
QY
243 FOREACH_AFI_SAFI (afi, safi) {
244 sync = XCALLOC(MTYPE_BGP_SYNCHRONISE,
245 sizeof(struct bgp_synchronize));
a274fef8
DL
246 bgp_adv_fifo_init(&sync->update);
247 bgp_adv_fifo_init(&sync->withdraw);
248 bgp_adv_fifo_init(&sync->withdraw_low);
05c7a1cc 249 peer->sync[afi][safi] = sync;
05c7a1cc 250 }
718e3744 251}
252
d62a17ae 253void bgp_sync_delete(struct peer *peer)
718e3744 254{
d62a17ae 255 afi_t afi;
256 safi_t safi;
257
05c7a1cc 258 FOREACH_AFI_SAFI (afi, safi) {
0a22ddfb 259 XFREE(MTYPE_BGP_SYNCHRONISE, peer->sync[afi][safi]);
05c7a1cc 260 }
718e3744 261}