1 /* BGP advertisement and adjacency
2 * Copyright (C) 1996, 97, 98, 99, 2000 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
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
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.
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
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"
36 #include "bgpd/bgp_debug.h"
37 #include "bgpd/bgp_aspath.h"
38 #include "bgpd/bgp_packet.h"
39 #include "bgpd/bgp_fsm.h"
40 #include "bgpd/bgp_mplsvpn.h"
41 #include "bgpd/bgp_updgrp.h"
43 /* BGP advertise attribute is used for pack same attribute update into
44 one packet. To do that we maintain attribute hash in struct
46 struct bgp_advertise_attr
*bgp_advertise_attr_new(void)
48 return XCALLOC(MTYPE_BGP_ADVERTISE_ATTR
,
49 sizeof(struct bgp_advertise_attr
));
52 void bgp_advertise_attr_free(struct bgp_advertise_attr
*baa
)
54 XFREE(MTYPE_BGP_ADVERTISE_ATTR
, baa
);
57 static void *bgp_advertise_attr_hash_alloc(void *p
)
59 struct bgp_advertise_attr
*ref
= (struct bgp_advertise_attr
*)p
;
60 struct bgp_advertise_attr
*baa
;
62 baa
= bgp_advertise_attr_new();
63 baa
->attr
= ref
->attr
;
67 unsigned int bgp_advertise_attr_hash_key(const void *p
)
69 const struct bgp_advertise_attr
*baa
= p
;
71 return attrhash_key_make(baa
->attr
);
74 bool bgp_advertise_attr_hash_cmp(const void *p1
, const void *p2
)
76 const struct bgp_advertise_attr
*baa1
= p1
;
77 const struct bgp_advertise_attr
*baa2
= p2
;
79 return attrhash_cmp(baa1
->attr
, baa2
->attr
);
82 /* BGP update and withdraw information is stored in BGP advertise
83 structure. This structure is referred from BGP adjacency
85 struct bgp_advertise
*bgp_advertise_new(void)
87 return XCALLOC(MTYPE_BGP_ADVERTISE
, sizeof(struct bgp_advertise
));
90 void bgp_advertise_free(struct bgp_advertise
*adv
)
93 /* bgp_advertise bgp_path_info reference */
94 bgp_path_info_unlock(adv
->pathi
);
95 XFREE(MTYPE_BGP_ADVERTISE
, adv
);
98 void bgp_advertise_add(struct bgp_advertise_attr
*baa
,
99 struct bgp_advertise
*adv
)
101 adv
->next
= baa
->adv
;
103 baa
->adv
->prev
= adv
;
107 void bgp_advertise_delete(struct bgp_advertise_attr
*baa
,
108 struct bgp_advertise
*adv
)
111 adv
->next
->prev
= adv
->prev
;
113 adv
->prev
->next
= adv
->next
;
115 baa
->adv
= adv
->next
;
118 struct bgp_advertise_attr
*bgp_advertise_attr_intern(struct hash
*hash
,
121 struct bgp_advertise_attr ref
;
122 struct bgp_advertise_attr
*baa
;
124 ref
.attr
= bgp_attr_intern(attr
);
125 baa
= (struct bgp_advertise_attr
*)hash_get(
126 hash
, &ref
, bgp_advertise_attr_hash_alloc
);
132 void bgp_advertise_attr_unintern(struct hash
*hash
,
133 struct bgp_advertise_attr
*baa
)
138 if (baa
->refcnt
&& baa
->attr
)
139 bgp_attr_unintern(&baa
->attr
);
142 hash_release(hash
, baa
);
143 bgp_attr_unintern(&baa
->attr
);
145 bgp_advertise_attr_free(baa
);
149 bool bgp_adj_out_lookup(struct peer
*peer
, struct bgp_dest
*dest
,
150 uint32_t addpath_tx_id
)
152 struct bgp_adj_out
*adj
;
156 bool addpath_capable
;
158 RB_FOREACH (adj
, bgp_adj_out_rb
, &dest
->adj_out
)
159 SUBGRP_FOREACH_PEER (adj
->subgroup
, paf
)
160 if (paf
->peer
== peer
) {
161 afi
= SUBGRP_AFI(adj
->subgroup
);
162 safi
= SUBGRP_SAFI(adj
->subgroup
);
164 bgp_addpath_encode_tx(peer
, afi
, safi
);
166 /* Match on a specific addpath_tx_id if we are
169 * peer and if an addpath_tx_id was specified */
170 if (addpath_capable
&& addpath_tx_id
171 && adj
->addpath_tx_id
!= addpath_tx_id
)
175 ? (adj
->adv
->baa
? true : false)
176 : (adj
->attr
? true : false));
183 void bgp_adj_in_set(struct bgp_dest
*dest
, struct peer
*peer
, struct attr
*attr
,
186 struct bgp_adj_in
*adj
;
188 for (adj
= dest
->adj_in
; adj
; adj
= adj
->next
) {
189 if (adj
->peer
== peer
&& adj
->addpath_rx_id
== addpath_id
) {
190 if (adj
->attr
!= attr
) {
191 bgp_attr_unintern(&adj
->attr
);
192 adj
->attr
= bgp_attr_intern(attr
);
197 adj
= XCALLOC(MTYPE_BGP_ADJ_IN
, sizeof(struct bgp_adj_in
));
198 adj
->peer
= peer_lock(peer
); /* adj_in peer reference */
199 adj
->attr
= bgp_attr_intern(attr
);
200 adj
->uptime
= monotime(NULL
);
201 adj
->addpath_rx_id
= addpath_id
;
202 BGP_ADJ_IN_ADD(dest
, adj
);
203 bgp_dest_lock_node(dest
);
206 void bgp_adj_in_remove(struct bgp_dest
*dest
, struct bgp_adj_in
*bai
)
208 bgp_attr_unintern(&bai
->attr
);
209 BGP_ADJ_IN_DEL(dest
, bai
);
210 bgp_dest_unlock_node(dest
);
211 peer_unlock(bai
->peer
); /* adj_in peer reference */
212 XFREE(MTYPE_BGP_ADJ_IN
, bai
);
215 bool bgp_adj_in_unset(struct bgp_dest
*dest
, struct peer
*peer
,
218 struct bgp_adj_in
*adj
;
219 struct bgp_adj_in
*adj_next
;
227 adj_next
= adj
->next
;
229 if (adj
->peer
== peer
&& adj
->addpath_rx_id
== addpath_id
)
230 bgp_adj_in_remove(dest
, adj
);
238 void bgp_sync_init(struct peer
*peer
)
242 struct bgp_synchronize
*sync
;
244 FOREACH_AFI_SAFI (afi
, safi
) {
245 sync
= XCALLOC(MTYPE_BGP_SYNCHRONISE
,
246 sizeof(struct bgp_synchronize
));
247 bgp_adv_fifo_init(&sync
->update
);
248 bgp_adv_fifo_init(&sync
->withdraw
);
249 bgp_adv_fifo_init(&sync
->withdraw_low
);
250 peer
->sync
[afi
][safi
] = sync
;
254 void bgp_sync_delete(struct peer
*peer
)
259 FOREACH_AFI_SAFI (afi
, safi
) {
260 XFREE(MTYPE_BGP_SYNCHRONISE
, peer
->sync
[afi
][safi
]);