]>
Commit | Line | Data |
---|---|---|
3f9c7369 DS |
1 | /** |
2 | * bgp_updgrp_adv.c: BGP update group advertisement and adjacency | |
3 | * maintenance | |
4 | * | |
5 | * | |
6 | * @copyright Copyright (C) 2014 Cumulus Networks, Inc. | |
7 | * | |
8 | * @author Avneesh Sachdev <avneesh@sproute.net> | |
9 | * @author Rajesh Varadarajan <rajesh@sproute.net> | |
10 | * @author Pradosh Mohapatra <pradosh@sproute.net> | |
11 | * | |
12 | * This file is part of GNU Zebra. | |
13 | * | |
14 | * GNU Zebra is free software; you can redistribute it and/or modify it | |
15 | * under the terms of the GNU General Public License as published by the | |
16 | * Free Software Foundation; either version 2, or (at your option) any | |
17 | * later version. | |
18 | * | |
19 | * GNU Zebra is distributed in the hope that it will be useful, but | |
20 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
22 | * General Public License for more details. | |
23 | * | |
896014f4 DL |
24 | * You should have received a copy of the GNU General Public License along |
25 | * with this program; see the file COPYING; if not, write to the Free Software | |
26 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
3f9c7369 DS |
27 | */ |
28 | ||
29 | #include <zebra.h> | |
30 | ||
31 | #include "command.h" | |
32 | #include "memory.h" | |
33 | #include "prefix.h" | |
34 | #include "hash.h" | |
35 | #include "thread.h" | |
36 | #include "queue.h" | |
37 | #include "routemap.h" | |
039f3a34 | 38 | #include "filter.h" |
3f9c7369 DS |
39 | |
40 | #include "bgpd/bgpd.h" | |
41 | #include "bgpd/bgp_table.h" | |
42 | #include "bgpd/bgp_debug.h" | |
43 | #include "bgpd/bgp_route.h" | |
44 | #include "bgpd/bgp_advertise.h" | |
45 | #include "bgpd/bgp_attr.h" | |
46 | #include "bgpd/bgp_aspath.h" | |
47 | #include "bgpd/bgp_packet.h" | |
48 | #include "bgpd/bgp_fsm.h" | |
49 | #include "bgpd/bgp_mplsvpn.h" | |
50 | #include "bgpd/bgp_updgrp.h" | |
51 | #include "bgpd/bgp_advertise.h" | |
dcc68b5e | 52 | #include "bgpd/bgp_addpath.h" |
3f9c7369 DS |
53 | |
54 | ||
55 | /******************** | |
56 | * PRIVATE FUNCTIONS | |
57 | ********************/ | |
a79c04e7 DS |
58 | static int bgp_adj_out_compare(const struct bgp_adj_out *o1, |
59 | const struct bgp_adj_out *o2) | |
60 | { | |
61 | if (o1->subgroup < o2->subgroup) | |
62 | return -1; | |
63 | ||
64 | if (o1->subgroup > o2->subgroup) | |
65 | return 1; | |
66 | ||
67 | return 0; | |
68 | } | |
69 | RB_GENERATE(bgp_adj_out_rb, bgp_adj_out, adj_entry, bgp_adj_out_compare); | |
3f9c7369 | 70 | |
d62a17ae | 71 | static inline struct bgp_adj_out *adj_lookup(struct bgp_node *rn, |
72 | struct update_subgroup *subgrp, | |
d7c0a89a | 73 | uint32_t addpath_tx_id) |
3f9c7369 | 74 | { |
a79c04e7 | 75 | struct bgp_adj_out *adj, lookup; |
d62a17ae | 76 | struct peer *peer; |
77 | afi_t afi; | |
78 | safi_t safi; | |
79 | int addpath_capable; | |
80 | ||
81 | if (!rn || !subgrp) | |
82 | return NULL; | |
83 | ||
84 | peer = SUBGRP_PEER(subgrp); | |
85 | afi = SUBGRP_AFI(subgrp); | |
86 | safi = SUBGRP_SAFI(subgrp); | |
87 | addpath_capable = bgp_addpath_encode_tx(peer, afi, safi); | |
88 | ||
89 | /* update-groups that do not support addpath will pass 0 for | |
90 | * addpath_tx_id so do not both matching against it */ | |
a79c04e7 DS |
91 | lookup.subgroup = subgrp; |
92 | adj = RB_FIND(bgp_adj_out_rb, &rn->adj_out, &lookup); | |
93 | if (adj) { | |
94 | if (addpath_capable) { | |
95 | if (adj->addpath_tx_id == addpath_tx_id) | |
96 | return adj; | |
97 | } else | |
98 | return adj; | |
d62a17ae | 99 | } |
a79c04e7 | 100 | return NULL; |
3f9c7369 DS |
101 | } |
102 | ||
d62a17ae | 103 | static void adj_free(struct bgp_adj_out *adj) |
3f9c7369 | 104 | { |
d62a17ae | 105 | TAILQ_REMOVE(&(adj->subgroup->adjq), adj, subgrp_adj_train); |
106 | SUBGRP_DECR_STAT(adj->subgroup, adj_count); | |
107 | XFREE(MTYPE_BGP_ADJ_OUT, adj); | |
3f9c7369 DS |
108 | } |
109 | ||
dcc68b5e MS |
110 | static void subgrp_withdraw_stale_addpath(struct updwalk_context *ctx, |
111 | struct update_subgroup *subgrp) | |
112 | { | |
113 | struct bgp_adj_out *adj, *adj_next; | |
114 | uint32_t id; | |
115 | struct bgp_path_info *pi; | |
116 | afi_t afi = SUBGRP_AFI(subgrp); | |
117 | safi_t safi = SUBGRP_SAFI(subgrp); | |
118 | struct peer *peer = SUBGRP_PEER(subgrp); | |
119 | ||
120 | /* Look through all of the paths we have advertised for this rn and send | |
121 | * a withdraw for the ones that are no longer present */ | |
a79c04e7 | 122 | RB_FOREACH_SAFE (adj, bgp_adj_out_rb, &ctx->rn->adj_out, adj_next) { |
dcc68b5e MS |
123 | |
124 | if (adj->subgroup == subgrp) { | |
6f94b685 DS |
125 | for (pi = bgp_node_get_bgp_path_info(ctx->rn); |
126 | pi; pi = pi->next) { | |
dcc68b5e MS |
127 | id = bgp_addpath_id_for_peer(peer, afi, safi, |
128 | &pi->tx_addpath); | |
129 | ||
130 | if (id == adj->addpath_tx_id) { | |
131 | break; | |
132 | } | |
133 | } | |
134 | ||
135 | if (!pi) { | |
136 | subgroup_process_announce_selected( | |
137 | subgrp, NULL, ctx->rn, | |
138 | adj->addpath_tx_id); | |
139 | } | |
140 | } | |
141 | } | |
142 | } | |
143 | ||
d62a17ae | 144 | static int group_announce_route_walkcb(struct update_group *updgrp, void *arg) |
3f9c7369 | 145 | { |
d62a17ae | 146 | struct updwalk_context *ctx = arg; |
147 | struct update_subgroup *subgrp; | |
40381db7 | 148 | struct bgp_path_info *pi; |
d62a17ae | 149 | afi_t afi; |
150 | safi_t safi; | |
151 | struct peer *peer; | |
152 | struct bgp_adj_out *adj, *adj_next; | |
153 | int addpath_capable; | |
154 | ||
155 | afi = UPDGRP_AFI(updgrp); | |
156 | safi = UPDGRP_SAFI(updgrp); | |
157 | peer = UPDGRP_PEER(updgrp); | |
158 | addpath_capable = bgp_addpath_encode_tx(peer, afi, safi); | |
159 | ||
e0207895 PZ |
160 | if (BGP_DEBUG(update, UPDATE_OUT)) { |
161 | char buf_prefix[PREFIX_STRLEN]; | |
162 | prefix2str(&ctx->rn->p, buf_prefix, sizeof(buf_prefix)); | |
163 | zlog_debug("%s: afi=%s, safi=%s, p=%s", __func__, afi2str(afi), | |
164 | safi2str(safi), buf_prefix); | |
165 | } | |
166 | ||
167 | ||
a2addae8 | 168 | UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) { |
3f9c7369 | 169 | |
d62a17ae | 170 | /* |
171 | * Skip the subgroups that have coalesce timer running. We will | |
172 | * walk the entire prefix table for those subgroups when the | |
173 | * coalesce timer fires. | |
174 | */ | |
175 | if (!subgrp->t_coalesce) { | |
176 | /* An update-group that uses addpath */ | |
177 | if (addpath_capable) { | |
dcc68b5e | 178 | subgrp_withdraw_stale_addpath(ctx, subgrp); |
d62a17ae | 179 | |
6f94b685 DS |
180 | for (pi = bgp_node_get_bgp_path_info(ctx->rn); |
181 | pi; pi = pi->next) { | |
d62a17ae | 182 | /* Skip the bestpath for now */ |
40381db7 | 183 | if (pi == ctx->pi) |
d62a17ae | 184 | continue; |
185 | ||
186 | subgroup_process_announce_selected( | |
40381db7 | 187 | subgrp, pi, ctx->rn, |
dcc68b5e MS |
188 | bgp_addpath_id_for_peer( |
189 | peer, afi, safi, | |
190 | &pi->tx_addpath)); | |
d62a17ae | 191 | } |
192 | ||
193 | /* Process the bestpath last so the "show [ip] | |
194 | * bgp neighbor x.x.x.x advertised" | |
195 | * output shows the attributes from the bestpath | |
196 | */ | |
40381db7 | 197 | if (ctx->pi) |
d62a17ae | 198 | subgroup_process_announce_selected( |
40381db7 | 199 | subgrp, ctx->pi, ctx->rn, |
dcc68b5e MS |
200 | bgp_addpath_id_for_peer( |
201 | peer, afi, safi, | |
202 | &ctx->pi->tx_addpath)); | |
d62a17ae | 203 | } |
204 | ||
205 | /* An update-group that does not use addpath */ | |
206 | else { | |
40381db7 | 207 | if (ctx->pi) { |
d62a17ae | 208 | subgroup_process_announce_selected( |
40381db7 | 209 | subgrp, ctx->pi, ctx->rn, |
dcc68b5e MS |
210 | bgp_addpath_id_for_peer( |
211 | peer, afi, safi, | |
212 | &ctx->pi->tx_addpath)); | |
d62a17ae | 213 | } else { |
214 | /* Find the addpath_tx_id of the path we | |
215 | * had advertised and | |
216 | * send a withdraw */ | |
a79c04e7 DS |
217 | RB_FOREACH_SAFE (adj, bgp_adj_out_rb, |
218 | &ctx->rn->adj_out, | |
219 | adj_next) { | |
d62a17ae | 220 | if (adj->subgroup == subgrp) { |
221 | subgroup_process_announce_selected( | |
222 | subgrp, NULL, | |
223 | ctx->rn, | |
224 | adj->addpath_tx_id); | |
225 | } | |
226 | } | |
227 | } | |
228 | } | |
229 | } | |
230 | } | |
3f9c7369 | 231 | |
d62a17ae | 232 | return UPDWALK_CONTINUE; |
233 | } | |
3f9c7369 | 234 | |
d62a17ae | 235 | static void subgrp_show_adjq_vty(struct update_subgroup *subgrp, |
d7c0a89a | 236 | struct vty *vty, uint8_t flags) |
d62a17ae | 237 | { |
238 | struct bgp_table *table; | |
239 | struct bgp_adj_out *adj; | |
240 | unsigned long output_count; | |
241 | struct bgp_node *rn; | |
242 | int header1 = 1; | |
243 | struct bgp *bgp; | |
244 | int header2 = 1; | |
245 | ||
246 | bgp = SUBGRP_INST(subgrp); | |
247 | if (!bgp) | |
248 | return; | |
249 | ||
250 | table = bgp->rib[SUBGRP_AFI(subgrp)][SUBGRP_SAFI(subgrp)]; | |
251 | ||
252 | output_count = 0; | |
253 | ||
254 | for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) | |
a79c04e7 | 255 | RB_FOREACH (adj, bgp_adj_out_rb, &rn->adj_out) |
d62a17ae | 256 | if (adj->subgroup == subgrp) { |
257 | if (header1) { | |
258 | vty_out(vty, | |
259 | "BGP table version is %" PRIu64 | |
260 | ", local router ID is %s\n", | |
261 | table->version, | |
262 | inet_ntoa(bgp->router_id)); | |
263 | vty_out(vty, BGP_SHOW_SCODE_HEADER); | |
264 | vty_out(vty, BGP_SHOW_OCODE_HEADER); | |
265 | header1 = 0; | |
266 | } | |
267 | if (header2) { | |
268 | vty_out(vty, BGP_SHOW_HEADER); | |
269 | header2 = 0; | |
270 | } | |
271 | if ((flags & UPDWALK_FLAGS_ADVQUEUE) && adj->adv | |
272 | && adj->adv->baa) { | |
273 | route_vty_out_tmp(vty, &rn->p, | |
274 | adj->adv->baa->attr, | |
275 | SUBGRP_SAFI(subgrp), | |
276 | 0, NULL); | |
277 | output_count++; | |
278 | } | |
279 | if ((flags & UPDWALK_FLAGS_ADVERTISED) | |
280 | && adj->attr) { | |
281 | route_vty_out_tmp( | |
282 | vty, &rn->p, adj->attr, | |
283 | SUBGRP_SAFI(subgrp), 0, NULL); | |
284 | output_count++; | |
285 | } | |
286 | } | |
287 | if (output_count != 0) | |
288 | vty_out(vty, "\nTotal number of prefixes %ld\n", output_count); | |
3f9c7369 DS |
289 | } |
290 | ||
d62a17ae | 291 | static int updgrp_show_adj_walkcb(struct update_group *updgrp, void *arg) |
3f9c7369 | 292 | { |
d62a17ae | 293 | struct updwalk_context *ctx = arg; |
294 | struct update_subgroup *subgrp; | |
295 | struct vty *vty; | |
296 | ||
297 | vty = ctx->vty; | |
a2addae8 | 298 | UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) { |
d62a17ae | 299 | if (ctx->subgrp_id && (ctx->subgrp_id != subgrp->id)) |
300 | continue; | |
301 | vty_out(vty, "update group %" PRIu64 ", subgroup %" PRIu64 "\n", | |
302 | updgrp->id, subgrp->id); | |
303 | subgrp_show_adjq_vty(subgrp, vty, ctx->flags); | |
304 | } | |
305 | return UPDWALK_CONTINUE; | |
3f9c7369 DS |
306 | } |
307 | ||
d62a17ae | 308 | static void updgrp_show_adj(struct bgp *bgp, afi_t afi, safi_t safi, |
d7c0a89a | 309 | struct vty *vty, uint64_t id, uint8_t flags) |
3f9c7369 | 310 | { |
d62a17ae | 311 | struct updwalk_context ctx; |
312 | memset(&ctx, 0, sizeof(ctx)); | |
313 | ctx.vty = vty; | |
314 | ctx.subgrp_id = id; | |
315 | ctx.flags = flags; | |
3f9c7369 | 316 | |
d62a17ae | 317 | update_group_af_walk(bgp, afi, safi, updgrp_show_adj_walkcb, &ctx); |
3f9c7369 DS |
318 | } |
319 | ||
d62a17ae | 320 | static int subgroup_coalesce_timer(struct thread *thread) |
3f9c7369 | 321 | { |
d62a17ae | 322 | struct update_subgroup *subgrp; |
323 | ||
324 | subgrp = THREAD_ARG(thread); | |
325 | if (bgp_debug_update(NULL, NULL, subgrp->update_group, 0)) | |
326 | zlog_debug("u%" PRIu64 ":s%" PRIu64 | |
327 | " announcing routes upon coalesce timer expiry", | |
328 | (SUBGRP_UPDGRP(subgrp))->id, subgrp->id); | |
329 | subgrp->t_coalesce = NULL; | |
330 | subgrp->v_coalesce = 0; | |
331 | subgroup_announce_route(subgrp); | |
332 | ||
333 | ||
334 | /* While the announce_route() may kick off the route advertisement timer | |
335 | * for | |
336 | * the members of the subgroup, we'd like to send the initial updates | |
337 | * much | |
338 | * faster (i.e., without enforcing MRAI). Also, if there were no routes | |
339 | * to | |
340 | * announce, this is the method currently employed to trigger the EOR. | |
341 | */ | |
342 | if (!bgp_update_delay_active(SUBGRP_INST(subgrp))) { | |
343 | struct peer_af *paf; | |
344 | struct peer *peer; | |
345 | ||
a2addae8 | 346 | SUBGRP_FOREACH_PEER (subgrp, paf) { |
d62a17ae | 347 | peer = PAF_PEER(paf); |
348 | BGP_TIMER_OFF(peer->t_routeadv); | |
349 | BGP_TIMER_ON(peer->t_routeadv, bgp_routeadv_timer, 0); | |
350 | } | |
351 | } | |
352 | ||
353 | return 0; | |
3f9c7369 DS |
354 | } |
355 | ||
d62a17ae | 356 | static int update_group_announce_walkcb(struct update_group *updgrp, void *arg) |
3f9c7369 | 357 | { |
d62a17ae | 358 | struct update_subgroup *subgrp; |
3f9c7369 | 359 | |
a2addae8 | 360 | UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) { |
d62a17ae | 361 | subgroup_announce_all(subgrp); |
362 | } | |
3f9c7369 | 363 | |
d62a17ae | 364 | return UPDWALK_CONTINUE; |
3f9c7369 DS |
365 | } |
366 | ||
d62a17ae | 367 | static int update_group_announce_rrc_walkcb(struct update_group *updgrp, |
368 | void *arg) | |
3f9c7369 | 369 | { |
d62a17ae | 370 | struct update_subgroup *subgrp; |
371 | afi_t afi; | |
372 | safi_t safi; | |
373 | struct peer *peer; | |
374 | ||
375 | afi = UPDGRP_AFI(updgrp); | |
376 | safi = UPDGRP_SAFI(updgrp); | |
377 | peer = UPDGRP_PEER(updgrp); | |
378 | ||
379 | /* Only announce if this is a group of route-reflector-clients */ | |
380 | if (CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_REFLECTOR_CLIENT)) { | |
a2addae8 | 381 | UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) { |
d62a17ae | 382 | subgroup_announce_all(subgrp); |
383 | } | |
384 | } | |
385 | ||
386 | return UPDWALK_CONTINUE; | |
3f9c7369 DS |
387 | } |
388 | ||
389 | /******************** | |
390 | * PUBLIC FUNCTIONS | |
391 | ********************/ | |
392 | ||
393 | /** | |
394 | * Allocate an adj-out object. Do proper initialization of its fields, | |
395 | * primarily its association with the subgroup and the prefix. | |
396 | */ | |
d62a17ae | 397 | struct bgp_adj_out *bgp_adj_out_alloc(struct update_subgroup *subgrp, |
398 | struct bgp_node *rn, | |
d7c0a89a | 399 | uint32_t addpath_tx_id) |
3f9c7369 | 400 | { |
d62a17ae | 401 | struct bgp_adj_out *adj; |
402 | ||
403 | adj = XCALLOC(MTYPE_BGP_ADJ_OUT, sizeof(struct bgp_adj_out)); | |
404 | adj->subgroup = subgrp; | |
405 | if (rn) { | |
a79c04e7 | 406 | RB_INSERT(bgp_adj_out_rb, &rn->adj_out, adj); |
d62a17ae | 407 | bgp_lock_node(rn); |
408 | adj->rn = rn; | |
409 | } | |
410 | ||
411 | adj->addpath_tx_id = addpath_tx_id; | |
412 | TAILQ_INSERT_TAIL(&(subgrp->adjq), adj, subgrp_adj_train); | |
413 | SUBGRP_INCR_STAT(subgrp, adj_count); | |
414 | return adj; | |
3f9c7369 DS |
415 | } |
416 | ||
417 | ||
418 | struct bgp_advertise * | |
d62a17ae | 419 | bgp_advertise_clean_subgroup(struct update_subgroup *subgrp, |
420 | struct bgp_adj_out *adj) | |
3f9c7369 | 421 | { |
d62a17ae | 422 | struct bgp_advertise *adv; |
423 | struct bgp_advertise_attr *baa; | |
424 | struct bgp_advertise *next; | |
a274fef8 | 425 | struct bgp_adv_fifo_head *fhead; |
3f9c7369 | 426 | |
d62a17ae | 427 | adv = adj->adv; |
428 | baa = adv->baa; | |
429 | next = NULL; | |
3f9c7369 | 430 | |
d62a17ae | 431 | if (baa) { |
432 | fhead = &subgrp->sync->update; | |
3f9c7369 | 433 | |
d62a17ae | 434 | /* Unlink myself from advertise attribute FIFO. */ |
435 | bgp_advertise_delete(baa, adv); | |
3f9c7369 | 436 | |
d62a17ae | 437 | /* Fetch next advertise candidate. */ |
438 | next = baa->adv; | |
3f9c7369 | 439 | |
d62a17ae | 440 | /* Unintern BGP advertise attribute. */ |
441 | bgp_advertise_unintern(subgrp->hash, baa); | |
442 | } else | |
443 | fhead = &subgrp->sync->withdraw; | |
3f9c7369 DS |
444 | |
445 | ||
d62a17ae | 446 | /* Unlink myself from advertisement FIFO. */ |
a274fef8 | 447 | bgp_adv_fifo_del(fhead, adv); |
3f9c7369 | 448 | |
d62a17ae | 449 | /* Free memory. */ |
450 | bgp_advertise_free(adj->adv); | |
451 | adj->adv = NULL; | |
3f9c7369 | 452 | |
d62a17ae | 453 | return next; |
3f9c7369 DS |
454 | } |
455 | ||
d62a17ae | 456 | void bgp_adj_out_set_subgroup(struct bgp_node *rn, |
457 | struct update_subgroup *subgrp, struct attr *attr, | |
9b6d8fcf | 458 | struct bgp_path_info *path) |
3f9c7369 | 459 | { |
d62a17ae | 460 | struct bgp_adj_out *adj = NULL; |
461 | struct bgp_advertise *adv; | |
dcc68b5e MS |
462 | struct peer *peer; |
463 | afi_t afi; | |
464 | safi_t safi; | |
465 | ||
466 | peer = SUBGRP_PEER(subgrp); | |
467 | afi = SUBGRP_AFI(subgrp); | |
468 | safi = SUBGRP_SAFI(subgrp); | |
d62a17ae | 469 | |
470 | if (DISABLE_BGP_ANNOUNCE) | |
471 | return; | |
472 | ||
473 | /* Look for adjacency information. */ | |
dcc68b5e MS |
474 | adj = adj_lookup( |
475 | rn, subgrp, | |
476 | bgp_addpath_id_for_peer(peer, afi, safi, &path->tx_addpath)); | |
d62a17ae | 477 | |
478 | if (!adj) { | |
dcc68b5e MS |
479 | adj = bgp_adj_out_alloc( |
480 | subgrp, rn, | |
481 | bgp_addpath_id_for_peer(peer, afi, safi, | |
482 | &path->tx_addpath)); | |
d62a17ae | 483 | if (!adj) |
484 | return; | |
485 | } | |
486 | ||
487 | if (adj->adv) | |
488 | bgp_advertise_clean_subgroup(subgrp, adj); | |
489 | adj->adv = bgp_advertise_new(); | |
490 | ||
491 | adv = adj->adv; | |
492 | adv->rn = rn; | |
9b6d8fcf | 493 | assert(adv->pathi == NULL); |
4b7e6066 | 494 | /* bgp_path_info adj_out reference */ |
9b6d8fcf | 495 | adv->pathi = bgp_path_info_lock(path); |
d62a17ae | 496 | |
497 | if (attr) | |
498 | adv->baa = bgp_advertise_intern(subgrp->hash, attr); | |
499 | else | |
500 | adv->baa = baa_new(); | |
501 | adv->adj = adj; | |
502 | ||
503 | /* Add new advertisement to advertisement attribute list. */ | |
504 | bgp_advertise_add(adv->baa, adv); | |
505 | ||
506 | /* | |
507 | * If the update adv list is empty, trigger the member peers' | |
508 | * mrai timers so the socket writes can happen. | |
509 | */ | |
a274fef8 | 510 | if (!bgp_adv_fifo_count(&subgrp->sync->update)) { |
d62a17ae | 511 | struct peer_af *paf; |
512 | ||
a2addae8 | 513 | SUBGRP_FOREACH_PEER (subgrp, paf) { |
d62a17ae | 514 | bgp_adjust_routeadv(PAF_PEER(paf)); |
515 | } | |
3f9c7369 | 516 | } |
3f9c7369 | 517 | |
a274fef8 | 518 | bgp_adv_fifo_add_tail(&subgrp->sync->update, adv); |
3f9c7369 | 519 | |
d62a17ae | 520 | subgrp->version = max(subgrp->version, rn->version); |
3f9c7369 DS |
521 | } |
522 | ||
4125bb67 DS |
523 | /* The only time 'withdraw' will be false is if we are sending |
524 | * the "neighbor x.x.x.x default-originate" default and need to clear | |
525 | * bgp_adj_out for the 0.0.0.0/0 route in the BGP table. | |
526 | */ | |
d62a17ae | 527 | void bgp_adj_out_unset_subgroup(struct bgp_node *rn, |
528 | struct update_subgroup *subgrp, char withdraw, | |
d7c0a89a | 529 | uint32_t addpath_tx_id) |
3f9c7369 | 530 | { |
d62a17ae | 531 | struct bgp_adj_out *adj; |
532 | struct bgp_advertise *adv; | |
2fc102e1 | 533 | bool trigger_write; |
d62a17ae | 534 | |
535 | if (DISABLE_BGP_ANNOUNCE) | |
536 | return; | |
537 | ||
538 | /* Lookup existing adjacency */ | |
539 | if ((adj = adj_lookup(rn, subgrp, addpath_tx_id)) != NULL) { | |
540 | /* Clean up previous advertisement. */ | |
541 | if (adj->adv) | |
542 | bgp_advertise_clean_subgroup(subgrp, adj); | |
543 | ||
544 | if (adj->attr && withdraw) { | |
545 | /* We need advertisement structure. */ | |
546 | adj->adv = bgp_advertise_new(); | |
547 | adv = adj->adv; | |
548 | adv->rn = rn; | |
549 | adv->adj = adj; | |
550 | ||
2fc102e1 QY |
551 | /* Note if we need to trigger a packet write */ |
552 | trigger_write = | |
a274fef8 | 553 | !bgp_adv_fifo_count(&subgrp->sync->withdraw); |
2fc102e1 | 554 | |
d62a17ae | 555 | /* Add to synchronization entry for withdraw |
556 | * announcement. */ | |
a274fef8 | 557 | bgp_adv_fifo_add_tail(&subgrp->sync->withdraw, adv); |
2fc102e1 QY |
558 | |
559 | if (trigger_write) | |
560 | subgroup_trigger_write(subgrp); | |
d62a17ae | 561 | } else { |
562 | /* Remove myself from adjacency. */ | |
a79c04e7 | 563 | RB_REMOVE(bgp_adj_out_rb, &rn->adj_out, adj); |
d62a17ae | 564 | |
565 | /* Free allocated information. */ | |
566 | adj_free(adj); | |
567 | ||
568 | bgp_unlock_node(rn); | |
569 | } | |
570 | } | |
571 | ||
572 | subgrp->version = max(subgrp->version, rn->version); | |
3f9c7369 DS |
573 | } |
574 | ||
d62a17ae | 575 | void bgp_adj_out_remove_subgroup(struct bgp_node *rn, struct bgp_adj_out *adj, |
576 | struct update_subgroup *subgrp) | |
3f9c7369 | 577 | { |
d62a17ae | 578 | if (adj->attr) |
579 | bgp_attr_unintern(&adj->attr); | |
3f9c7369 | 580 | |
d62a17ae | 581 | if (adj->adv) |
582 | bgp_advertise_clean_subgroup(subgrp, adj); | |
3f9c7369 | 583 | |
a79c04e7 | 584 | RB_REMOVE(bgp_adj_out_rb, &rn->adj_out, adj); |
d62a17ae | 585 | adj_free(adj); |
3f9c7369 DS |
586 | } |
587 | ||
588 | /* | |
589 | * Go through all the routes and clean up the adj/adv structures corresponding | |
590 | * to the subgroup. | |
591 | */ | |
d62a17ae | 592 | void subgroup_clear_table(struct update_subgroup *subgrp) |
3f9c7369 | 593 | { |
d62a17ae | 594 | struct bgp_adj_out *aout, *taout; |
595 | ||
a2addae8 | 596 | SUBGRP_FOREACH_ADJ_SAFE (subgrp, aout, taout) { |
d62a17ae | 597 | struct bgp_node *rn = aout->rn; |
598 | bgp_adj_out_remove_subgroup(rn, aout, subgrp); | |
599 | bgp_unlock_node(rn); | |
600 | } | |
3f9c7369 DS |
601 | } |
602 | ||
603 | /* | |
604 | * subgroup_announce_table | |
605 | */ | |
d62a17ae | 606 | void subgroup_announce_table(struct update_subgroup *subgrp, |
607 | struct bgp_table *table) | |
3f9c7369 | 608 | { |
d62a17ae | 609 | struct bgp_node *rn; |
4b7e6066 | 610 | struct bgp_path_info *ri; |
d62a17ae | 611 | struct attr attr; |
612 | struct peer *peer; | |
613 | afi_t afi; | |
614 | safi_t safi; | |
615 | int addpath_capable; | |
616 | ||
617 | peer = SUBGRP_PEER(subgrp); | |
618 | afi = SUBGRP_AFI(subgrp); | |
619 | safi = SUBGRP_SAFI(subgrp); | |
620 | addpath_capable = bgp_addpath_encode_tx(peer, afi, safi); | |
621 | ||
622 | if (safi == SAFI_LABELED_UNICAST) | |
623 | safi = SAFI_UNICAST; | |
624 | ||
625 | if (!table) | |
626 | table = peer->bgp->rib[afi][safi]; | |
627 | ||
628 | if (safi != SAFI_MPLS_VPN && safi != SAFI_ENCAP && safi != SAFI_EVPN | |
629 | && CHECK_FLAG(peer->af_flags[afi][safi], | |
630 | PEER_FLAG_DEFAULT_ORIGINATE)) | |
631 | subgroup_default_originate(subgrp, 0); | |
632 | ||
633 | for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) | |
6f94b685 | 634 | for (ri = bgp_node_get_bgp_path_info(rn); ri; ri = ri->next) |
d62a17ae | 635 | |
1defdda8 | 636 | if (CHECK_FLAG(ri->flags, BGP_PATH_SELECTED) |
d62a17ae | 637 | || (addpath_capable |
dcc68b5e MS |
638 | && bgp_addpath_tx_path( |
639 | peer->addpath_type[afi][safi], | |
640 | ri))) { | |
d62a17ae | 641 | if (subgroup_announce_check(rn, ri, subgrp, |
642 | &rn->p, &attr)) | |
643 | bgp_adj_out_set_subgroup(rn, subgrp, | |
644 | &attr, ri); | |
645 | else | |
646 | bgp_adj_out_unset_subgroup( | |
647 | rn, subgrp, 1, | |
dcc68b5e MS |
648 | bgp_addpath_id_for_peer( |
649 | peer, afi, safi, | |
650 | &ri->tx_addpath)); | |
d62a17ae | 651 | } |
652 | ||
653 | /* | |
654 | * We walked through the whole table -- make sure our version number | |
655 | * is consistent with the one on the table. This should allow | |
656 | * subgroups to merge sooner if a peer comes up when the route node | |
657 | * with the largest version is no longer in the table. This also | |
658 | * covers the pathological case where all routes in the table have | |
659 | * now been deleted. | |
660 | */ | |
661 | subgrp->version = max(subgrp->version, table->version); | |
662 | ||
663 | /* | |
664 | * Start a task to merge the subgroup if necessary. | |
665 | */ | |
666 | update_subgroup_trigger_merge_check(subgrp, 0); | |
3f9c7369 DS |
667 | } |
668 | ||
669 | /* | |
670 | * subgroup_announce_route | |
671 | * | |
672 | * Refresh all routes out to a subgroup. | |
673 | */ | |
d62a17ae | 674 | void subgroup_announce_route(struct update_subgroup *subgrp) |
3f9c7369 | 675 | { |
d62a17ae | 676 | struct bgp_node *rn; |
677 | struct bgp_table *table; | |
678 | struct peer *onlypeer; | |
679 | ||
680 | if (update_subgroup_needs_refresh(subgrp)) { | |
681 | update_subgroup_set_needs_refresh(subgrp, 0); | |
682 | } | |
683 | ||
684 | /* | |
685 | * First update is deferred until ORF or ROUTE-REFRESH is received | |
686 | */ | |
687 | onlypeer = ((SUBGRP_PCOUNT(subgrp) == 1) ? (SUBGRP_PFIRST(subgrp))->peer | |
688 | : NULL); | |
9d303b37 DL |
689 | if (onlypeer && CHECK_FLAG(onlypeer->af_sflags[SUBGRP_AFI(subgrp)] |
690 | [SUBGRP_SAFI(subgrp)], | |
691 | PEER_STATUS_ORF_WAIT_REFRESH)) | |
d62a17ae | 692 | return; |
693 | ||
694 | if (SUBGRP_SAFI(subgrp) != SAFI_MPLS_VPN | |
695 | && SUBGRP_SAFI(subgrp) != SAFI_ENCAP | |
696 | && SUBGRP_SAFI(subgrp) != SAFI_EVPN) | |
697 | subgroup_announce_table(subgrp, NULL); | |
698 | else | |
699 | for (rn = bgp_table_top(update_subgroup_rib(subgrp)); rn; | |
67009e22 DS |
700 | rn = bgp_route_next(rn)) { |
701 | table = bgp_node_get_bgp_table_info(rn); | |
702 | if (!table) | |
703 | continue; | |
704 | subgroup_announce_table(subgrp, table); | |
705 | } | |
3f9c7369 DS |
706 | } |
707 | ||
d62a17ae | 708 | void subgroup_default_originate(struct update_subgroup *subgrp, int withdraw) |
3f9c7369 | 709 | { |
d62a17ae | 710 | struct bgp *bgp; |
e50c68b2 DS |
711 | struct attr attr; |
712 | struct aspath *aspath; | |
713 | struct bgp_path_info tmp_info; | |
d62a17ae | 714 | struct prefix p; |
715 | struct peer *from; | |
716 | struct bgp_node *rn; | |
4b7e6066 | 717 | struct bgp_path_info *ri; |
d62a17ae | 718 | struct peer *peer; |
2789041a | 719 | int ret = RMAP_DENYMATCH; |
d62a17ae | 720 | afi_t afi; |
721 | safi_t safi; | |
722 | ||
723 | if (!subgrp) | |
724 | return; | |
725 | ||
726 | peer = SUBGRP_PEER(subgrp); | |
727 | afi = SUBGRP_AFI(subgrp); | |
728 | safi = SUBGRP_SAFI(subgrp); | |
729 | ||
730 | if (!(afi == AFI_IP || afi == AFI_IP6)) | |
731 | return; | |
732 | ||
733 | bgp = peer->bgp; | |
734 | from = bgp->peer_self; | |
735 | ||
e50c68b2 DS |
736 | bgp_attr_default_set(&attr, BGP_ORIGIN_IGP); |
737 | aspath = attr.aspath; | |
738 | ||
739 | attr.local_pref = bgp->default_local_pref; | |
d62a17ae | 740 | |
cbb65f5e RW |
741 | memset(&p, 0, sizeof(p)); |
742 | p.family = afi2family(afi); | |
743 | p.prefixlen = 0; | |
d62a17ae | 744 | |
e50c68b2 DS |
745 | if ((afi == AFI_IP6) || peer_cap_enhe(peer, afi, safi)) { |
746 | /* IPv6 global nexthop must be included. */ | |
747 | attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV6_GLOBAL; | |
748 | ||
749 | /* If the peer is on shared nextwork and we have link-local | |
750 | nexthop set it. */ | |
751 | if (peer->shared_network | |
752 | && !IN6_IS_ADDR_UNSPECIFIED(&peer->nexthop.v6_local)) | |
753 | attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL; | |
754 | } | |
755 | ||
d62a17ae | 756 | if (peer->default_rmap[afi][safi].name) { |
757 | SET_FLAG(bgp->peer_self->rmap_type, PEER_RMAP_TYPE_DEFAULT); | |
758 | for (rn = bgp_table_top(bgp->rib[afi][safi]); rn; | |
759 | rn = bgp_route_next(rn)) { | |
6f94b685 DS |
760 | for (ri = bgp_node_get_bgp_path_info(rn); |
761 | ri; ri = ri->next) { | |
e50c68b2 DS |
762 | struct attr dummy_attr; |
763 | ||
764 | /* Provide dummy so the route-map can't modify | |
765 | * the attributes */ | |
766 | bgp_attr_dup(&dummy_attr, ri->attr); | |
74401e62 | 767 | tmp_info.peer = ri->peer; |
e50c68b2 | 768 | tmp_info.attr = &dummy_attr; |
d62a17ae | 769 | |
770 | ret = route_map_apply( | |
771 | peer->default_rmap[afi][safi].map, | |
74401e62 DA |
772 | &rn->p, RMAP_BGP, &tmp_info); |
773 | ||
e50c68b2 DS |
774 | /* The route map might have set attributes. If |
775 | * we don't flush them | |
776 | * here, they will be leaked. */ | |
777 | bgp_attr_flush(&dummy_attr); | |
d62a17ae | 778 | if (ret != RMAP_DENYMATCH) |
779 | break; | |
780 | } | |
781 | if (ret != RMAP_DENYMATCH) | |
782 | break; | |
783 | } | |
784 | bgp->peer_self->rmap_type = 0; | |
785 | ||
786 | if (ret == RMAP_DENYMATCH) | |
787 | withdraw = 1; | |
3f9c7369 | 788 | } |
3f9c7369 | 789 | |
d62a17ae | 790 | if (withdraw) { |
791 | if (CHECK_FLAG(subgrp->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE)) | |
792 | subgroup_default_withdraw_packet(subgrp); | |
793 | UNSET_FLAG(subgrp->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE); | |
794 | } else { | |
795 | if (!CHECK_FLAG(subgrp->sflags, | |
796 | SUBGRP_STATUS_DEFAULT_ORIGINATE)) { | |
7f323236 DW |
797 | |
798 | if (bgp_flag_check(bgp, BGP_FLAG_GRACEFUL_SHUTDOWN)) { | |
e50c68b2 | 799 | bgp_attr_add_gshut_community(&attr); |
7f323236 DW |
800 | } |
801 | ||
d62a17ae | 802 | SET_FLAG(subgrp->sflags, |
803 | SUBGRP_STATUS_DEFAULT_ORIGINATE); | |
e50c68b2 | 804 | subgroup_default_update_packet(subgrp, &attr, from); |
d62a17ae | 805 | |
806 | /* The 'neighbor x.x.x.x default-originate' default will | |
807 | * act as an | |
808 | * implicit withdraw for any previous UPDATEs sent for | |
809 | * 0.0.0.0/0 so | |
810 | * clear adj_out for the 0.0.0.0/0 prefix in the BGP | |
811 | * table. | |
812 | */ | |
cbb65f5e RW |
813 | memset(&p, 0, sizeof(p)); |
814 | p.family = afi2family(afi); | |
815 | p.prefixlen = 0; | |
d62a17ae | 816 | |
817 | rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi, | |
818 | &p, NULL); | |
819 | bgp_adj_out_unset_subgroup( | |
820 | rn, subgrp, 0, | |
821 | BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE); | |
822 | } | |
823 | } | |
e50c68b2 DS |
824 | |
825 | aspath_unintern(&aspath); | |
3f9c7369 DS |
826 | } |
827 | ||
828 | /* | |
829 | * Announce the BGP table to a subgroup. | |
830 | * | |
831 | * At startup, we try to optimize route announcement by coalescing the | |
832 | * peer-up events. This is done only the first time - from then on, | |
833 | * subgrp->v_coalesce will be set to zero and the normal logic | |
834 | * prevails. | |
835 | */ | |
d62a17ae | 836 | void subgroup_announce_all(struct update_subgroup *subgrp) |
3f9c7369 | 837 | { |
d62a17ae | 838 | if (!subgrp) |
839 | return; | |
840 | ||
841 | /* | |
842 | * If coalesce timer value is not set, announce routes immediately. | |
843 | */ | |
844 | if (!subgrp->v_coalesce) { | |
845 | if (bgp_debug_update(NULL, NULL, subgrp->update_group, 0)) | |
846 | zlog_debug("u%" PRIu64 ":s%" PRIu64 | |
847 | " announcing all routes", | |
848 | subgrp->update_group->id, subgrp->id); | |
849 | subgroup_announce_route(subgrp); | |
850 | return; | |
851 | } | |
852 | ||
853 | /* | |
854 | * We should wait for the coalesce timer. Arm the timer if not done. | |
855 | */ | |
856 | if (!subgrp->t_coalesce) { | |
857 | thread_add_timer_msec(bm->master, subgroup_coalesce_timer, | |
858 | subgrp, subgrp->v_coalesce, | |
859 | &subgrp->t_coalesce); | |
860 | } | |
3f9c7369 DS |
861 | } |
862 | ||
863 | /* | |
864 | * Go through all update subgroups and set up the adv queue for the | |
865 | * input route. | |
866 | */ | |
d62a17ae | 867 | void group_announce_route(struct bgp *bgp, afi_t afi, safi_t safi, |
40381db7 | 868 | struct bgp_node *rn, struct bgp_path_info *pi) |
3f9c7369 | 869 | { |
d62a17ae | 870 | struct updwalk_context ctx; |
40381db7 | 871 | ctx.pi = pi; |
d62a17ae | 872 | ctx.rn = rn; |
873 | update_group_af_walk(bgp, afi, safi, group_announce_route_walkcb, &ctx); | |
3f9c7369 DS |
874 | } |
875 | ||
d62a17ae | 876 | void update_group_show_adj_queue(struct bgp *bgp, afi_t afi, safi_t safi, |
877 | struct vty *vty, uint64_t id) | |
3f9c7369 | 878 | { |
d62a17ae | 879 | updgrp_show_adj(bgp, afi, safi, vty, id, UPDWALK_FLAGS_ADVQUEUE); |
3f9c7369 DS |
880 | } |
881 | ||
d62a17ae | 882 | void update_group_show_advertised(struct bgp *bgp, afi_t afi, safi_t safi, |
883 | struct vty *vty, uint64_t id) | |
3f9c7369 | 884 | { |
d62a17ae | 885 | updgrp_show_adj(bgp, afi, safi, vty, id, UPDWALK_FLAGS_ADVERTISED); |
3f9c7369 DS |
886 | } |
887 | ||
d62a17ae | 888 | void update_group_announce(struct bgp *bgp) |
3f9c7369 | 889 | { |
d62a17ae | 890 | update_group_walk(bgp, update_group_announce_walkcb, NULL); |
3f9c7369 DS |
891 | } |
892 | ||
d62a17ae | 893 | void update_group_announce_rrclients(struct bgp *bgp) |
3f9c7369 | 894 | { |
d62a17ae | 895 | update_group_walk(bgp, update_group_announce_rrc_walkcb, NULL); |
3f9c7369 | 896 | } |