]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_advertise.c
Merge pull request #4192 from bisdhdh/biswajitfrr_4
[mirror_frr.git] / bgpd / bgp_advertise.c
1 /* BGP advertisement and adjacency
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 */
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"
28 #include "queue.h"
29 #include "filter.h"
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"
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"
42
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. */
46 struct bgp_advertise_attr *baa_new(void)
47 {
48 return XCALLOC(MTYPE_BGP_ADVERTISE_ATTR,
49 sizeof(struct bgp_advertise_attr));
50 }
51
52 static void baa_free(struct bgp_advertise_attr *baa)
53 {
54 XFREE(MTYPE_BGP_ADVERTISE_ATTR, baa);
55 }
56
57 static void *baa_hash_alloc(void *p)
58 {
59 struct bgp_advertise_attr *ref = (struct bgp_advertise_attr *)p;
60 struct bgp_advertise_attr *baa;
61
62 baa = baa_new();
63 baa->attr = ref->attr;
64 return baa;
65 }
66
67 unsigned int baa_hash_key(void *p)
68 {
69 struct bgp_advertise_attr *baa = (struct bgp_advertise_attr *)p;
70
71 return attrhash_key_make(baa->attr);
72 }
73
74 bool baa_hash_cmp(const void *p1, const void *p2)
75 {
76 const struct bgp_advertise_attr *baa1 = p1;
77 const struct bgp_advertise_attr *baa2 = p2;
78
79 return attrhash_cmp(baa1->attr, baa2->attr);
80 }
81
82 /* BGP update and withdraw information is stored in BGP advertise
83 structure. This structure is referred from BGP adjacency
84 information. */
85 struct bgp_advertise *bgp_advertise_new(void)
86 {
87 return XCALLOC(MTYPE_BGP_ADVERTISE, sizeof(struct bgp_advertise));
88 }
89
90 void bgp_advertise_free(struct bgp_advertise *adv)
91 {
92 if (adv->pathi)
93 /* bgp_advertise bgp_path_info reference */
94 bgp_path_info_unlock(adv->pathi);
95 XFREE(MTYPE_BGP_ADVERTISE, adv);
96 }
97
98 void bgp_advertise_add(struct bgp_advertise_attr *baa,
99 struct bgp_advertise *adv)
100 {
101 adv->next = baa->adv;
102 if (baa->adv)
103 baa->adv->prev = adv;
104 baa->adv = adv;
105 }
106
107 void bgp_advertise_delete(struct bgp_advertise_attr *baa,
108 struct bgp_advertise *adv)
109 {
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;
116 }
117
118 struct bgp_advertise_attr *bgp_advertise_intern(struct hash *hash,
119 struct attr *attr)
120 {
121 struct bgp_advertise_attr ref;
122 struct bgp_advertise_attr *baa;
123
124 ref.attr = bgp_attr_intern(attr);
125 baa = (struct bgp_advertise_attr *)hash_get(hash, &ref, baa_hash_alloc);
126 baa->refcnt++;
127
128 return baa;
129 }
130
131 void bgp_advertise_unintern(struct hash *hash, struct bgp_advertise_attr *baa)
132 {
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);
144 }
145 }
146
147 int bgp_adj_out_lookup(struct peer *peer, struct bgp_node *rn,
148 uint32_t addpath_tx_id)
149 {
150 struct bgp_adj_out *adj;
151 struct peer_af *paf;
152 afi_t afi;
153 safi_t safi;
154 int addpath_capable;
155
156 RB_FOREACH (adj, bgp_adj_out_rb, &rn->adj_out)
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
172 return (adj->adv ? (adj->adv->baa ? 1 : 0)
173 : (adj->attr ? 1 : 0));
174 }
175
176 return 0;
177 }
178
179
180 void bgp_adj_in_set(struct bgp_node *rn, struct peer *peer, struct attr *attr,
181 uint32_t addpath_id)
182 {
183 struct bgp_adj_in *adj;
184
185 for (adj = rn->adj_in; adj; adj = adj->next) {
186 if (adj->peer == peer && adj->addpath_rx_id == addpath_id) {
187 if (adj->attr != attr) {
188 bgp_attr_unintern(&adj->attr);
189 adj->attr = bgp_attr_intern(attr);
190 }
191 return;
192 }
193 }
194 adj = XCALLOC(MTYPE_BGP_ADJ_IN, sizeof(struct bgp_adj_in));
195 adj->peer = peer_lock(peer); /* adj_in peer reference */
196 adj->attr = bgp_attr_intern(attr);
197 adj->addpath_rx_id = addpath_id;
198 BGP_ADJ_IN_ADD(rn, adj);
199 bgp_lock_node(rn);
200 }
201
202 void bgp_adj_in_remove(struct bgp_node *rn, struct bgp_adj_in *bai)
203 {
204 bgp_attr_unintern(&bai->attr);
205 BGP_ADJ_IN_DEL(rn, bai);
206 peer_unlock(bai->peer); /* adj_in peer reference */
207 XFREE(MTYPE_BGP_ADJ_IN, bai);
208 }
209
210 int bgp_adj_in_unset(struct bgp_node *rn, struct peer *peer,
211 uint32_t addpath_id)
212 {
213 struct bgp_adj_in *adj;
214 struct bgp_adj_in *adj_next;
215
216 adj = rn->adj_in;
217
218 if (!adj)
219 return 0;
220
221 while (adj) {
222 adj_next = adj->next;
223
224 if (adj->peer == peer && adj->addpath_rx_id == addpath_id) {
225 bgp_adj_in_remove(rn, adj);
226 bgp_unlock_node(rn);
227 }
228
229 adj = adj_next;
230 }
231
232 return 1;
233 }
234
235 void bgp_sync_init(struct peer *peer)
236 {
237 afi_t afi;
238 safi_t safi;
239 struct bgp_synchronize *sync;
240
241 FOREACH_AFI_SAFI (afi, safi) {
242 sync = XCALLOC(MTYPE_BGP_SYNCHRONISE,
243 sizeof(struct bgp_synchronize));
244 bgp_adv_fifo_init(&sync->update);
245 bgp_adv_fifo_init(&sync->withdraw);
246 bgp_adv_fifo_init(&sync->withdraw_low);
247 peer->sync[afi][safi] = sync;
248 }
249 }
250
251 void bgp_sync_delete(struct peer *peer)
252 {
253 afi_t afi;
254 safi_t safi;
255
256 FOREACH_AFI_SAFI (afi, safi) {
257 XFREE(MTYPE_BGP_SYNCHRONISE, peer->sync[afi][safi]);
258 peer->sync[afi][safi] = NULL;
259 }
260 }