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