]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_updgrp_adv.c
Merge pull request #5427 from liam-mcb/igmp-join-any
[mirror_frr.git] / bgpd / bgp_updgrp_adv.c
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 *
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
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"
38 #include "filter.h"
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"
52 #include "bgpd/bgp_addpath.h"
53
54
55 /********************
56 * PRIVATE FUNCTIONS
57 ********************/
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);
70
71 static inline struct bgp_adj_out *adj_lookup(struct bgp_node *rn,
72 struct update_subgroup *subgrp,
73 uint32_t addpath_tx_id)
74 {
75 struct bgp_adj_out *adj, lookup;
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 */
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;
99 }
100 return NULL;
101 }
102
103 static void adj_free(struct bgp_adj_out *adj)
104 {
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);
108 }
109
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 */
122 RB_FOREACH_SAFE (adj, bgp_adj_out_rb, &ctx->rn->adj_out, adj_next) {
123
124 if (adj->subgroup == subgrp) {
125 for (pi = bgp_node_get_bgp_path_info(ctx->rn);
126 pi; pi = pi->next) {
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
144 static int group_announce_route_walkcb(struct update_group *updgrp, void *arg)
145 {
146 struct updwalk_context *ctx = arg;
147 struct update_subgroup *subgrp;
148 struct bgp_path_info *pi;
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
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
168 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
169
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) {
178 subgrp_withdraw_stale_addpath(ctx, subgrp);
179
180 for (pi = bgp_node_get_bgp_path_info(ctx->rn);
181 pi; pi = pi->next) {
182 /* Skip the bestpath for now */
183 if (pi == ctx->pi)
184 continue;
185
186 subgroup_process_announce_selected(
187 subgrp, pi, ctx->rn,
188 bgp_addpath_id_for_peer(
189 peer, afi, safi,
190 &pi->tx_addpath));
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 */
197 if (ctx->pi)
198 subgroup_process_announce_selected(
199 subgrp, ctx->pi, ctx->rn,
200 bgp_addpath_id_for_peer(
201 peer, afi, safi,
202 &ctx->pi->tx_addpath));
203 }
204
205 /* An update-group that does not use addpath */
206 else {
207 if (ctx->pi) {
208 subgroup_process_announce_selected(
209 subgrp, ctx->pi, ctx->rn,
210 bgp_addpath_id_for_peer(
211 peer, afi, safi,
212 &ctx->pi->tx_addpath));
213 } else {
214 /* Find the addpath_tx_id of the path we
215 * had advertised and
216 * send a withdraw */
217 RB_FOREACH_SAFE (adj, bgp_adj_out_rb,
218 &ctx->rn->adj_out,
219 adj_next) {
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 }
231
232 return UPDWALK_CONTINUE;
233 }
234
235 static void subgrp_show_adjq_vty(struct update_subgroup *subgrp,
236 struct vty *vty, uint8_t flags)
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))
255 RB_FOREACH (adj, bgp_adj_out_rb, &rn->adj_out)
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);
289 }
290
291 static int updgrp_show_adj_walkcb(struct update_group *updgrp, void *arg)
292 {
293 struct updwalk_context *ctx = arg;
294 struct update_subgroup *subgrp;
295 struct vty *vty;
296
297 vty = ctx->vty;
298 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
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;
306 }
307
308 static void updgrp_show_adj(struct bgp *bgp, afi_t afi, safi_t safi,
309 struct vty *vty, uint64_t id, uint8_t flags)
310 {
311 struct updwalk_context ctx;
312 memset(&ctx, 0, sizeof(ctx));
313 ctx.vty = vty;
314 ctx.subgrp_id = id;
315 ctx.flags = flags;
316
317 update_group_af_walk(bgp, afi, safi, updgrp_show_adj_walkcb, &ctx);
318 }
319
320 static int subgroup_coalesce_timer(struct thread *thread)
321 {
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(%u ms)",
328 (SUBGRP_UPDGRP(subgrp))->id, subgrp->id,
329 subgrp->v_coalesce),
330 subgrp->t_coalesce = NULL;
331 subgrp->v_coalesce = 0;
332 subgroup_announce_route(subgrp);
333
334
335 /* While the announce_route() may kick off the route advertisement timer
336 * for
337 * the members of the subgroup, we'd like to send the initial updates
338 * much
339 * faster (i.e., without enforcing MRAI). Also, if there were no routes
340 * to
341 * announce, this is the method currently employed to trigger the EOR.
342 */
343 if (!bgp_update_delay_active(SUBGRP_INST(subgrp))) {
344 struct peer_af *paf;
345 struct peer *peer;
346
347 SUBGRP_FOREACH_PEER (subgrp, paf) {
348 peer = PAF_PEER(paf);
349 BGP_TIMER_OFF(peer->t_routeadv);
350 BGP_TIMER_ON(peer->t_routeadv, bgp_routeadv_timer, 0);
351 }
352 }
353
354 return 0;
355 }
356
357 static int update_group_announce_walkcb(struct update_group *updgrp, void *arg)
358 {
359 struct update_subgroup *subgrp;
360
361 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
362 subgroup_announce_all(subgrp);
363 }
364
365 return UPDWALK_CONTINUE;
366 }
367
368 static int update_group_announce_rrc_walkcb(struct update_group *updgrp,
369 void *arg)
370 {
371 struct update_subgroup *subgrp;
372 afi_t afi;
373 safi_t safi;
374 struct peer *peer;
375
376 afi = UPDGRP_AFI(updgrp);
377 safi = UPDGRP_SAFI(updgrp);
378 peer = UPDGRP_PEER(updgrp);
379
380 /* Only announce if this is a group of route-reflector-clients */
381 if (CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_REFLECTOR_CLIENT)) {
382 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
383 subgroup_announce_all(subgrp);
384 }
385 }
386
387 return UPDWALK_CONTINUE;
388 }
389
390 /********************
391 * PUBLIC FUNCTIONS
392 ********************/
393
394 /**
395 * Allocate an adj-out object. Do proper initialization of its fields,
396 * primarily its association with the subgroup and the prefix.
397 */
398 struct bgp_adj_out *bgp_adj_out_alloc(struct update_subgroup *subgrp,
399 struct bgp_node *rn,
400 uint32_t addpath_tx_id)
401 {
402 struct bgp_adj_out *adj;
403
404 adj = XCALLOC(MTYPE_BGP_ADJ_OUT, sizeof(struct bgp_adj_out));
405 adj->subgroup = subgrp;
406 if (rn) {
407 RB_INSERT(bgp_adj_out_rb, &rn->adj_out, adj);
408 bgp_lock_node(rn);
409 adj->rn = rn;
410 }
411
412 adj->addpath_tx_id = addpath_tx_id;
413 TAILQ_INSERT_TAIL(&(subgrp->adjq), adj, subgrp_adj_train);
414 SUBGRP_INCR_STAT(subgrp, adj_count);
415 return adj;
416 }
417
418
419 struct bgp_advertise *
420 bgp_advertise_clean_subgroup(struct update_subgroup *subgrp,
421 struct bgp_adj_out *adj)
422 {
423 struct bgp_advertise *adv;
424 struct bgp_advertise_attr *baa;
425 struct bgp_advertise *next;
426 struct bgp_adv_fifo_head *fhead;
427
428 adv = adj->adv;
429 baa = adv->baa;
430 next = NULL;
431
432 if (baa) {
433 fhead = &subgrp->sync->update;
434
435 /* Unlink myself from advertise attribute FIFO. */
436 bgp_advertise_delete(baa, adv);
437
438 /* Fetch next advertise candidate. */
439 next = baa->adv;
440
441 /* Unintern BGP advertise attribute. */
442 bgp_advertise_unintern(subgrp->hash, baa);
443 } else
444 fhead = &subgrp->sync->withdraw;
445
446
447 /* Unlink myself from advertisement FIFO. */
448 bgp_adv_fifo_del(fhead, adv);
449
450 /* Free memory. */
451 bgp_advertise_free(adj->adv);
452 adj->adv = NULL;
453
454 return next;
455 }
456
457 void bgp_adj_out_set_subgroup(struct bgp_node *rn,
458 struct update_subgroup *subgrp, struct attr *attr,
459 struct bgp_path_info *path)
460 {
461 struct bgp_adj_out *adj = NULL;
462 struct bgp_advertise *adv;
463 struct peer *peer;
464 afi_t afi;
465 safi_t safi;
466
467 peer = SUBGRP_PEER(subgrp);
468 afi = SUBGRP_AFI(subgrp);
469 safi = SUBGRP_SAFI(subgrp);
470
471 if (DISABLE_BGP_ANNOUNCE)
472 return;
473
474 /* Look for adjacency information. */
475 adj = adj_lookup(
476 rn, subgrp,
477 bgp_addpath_id_for_peer(peer, afi, safi, &path->tx_addpath));
478
479 if (!adj) {
480 adj = bgp_adj_out_alloc(
481 subgrp, rn,
482 bgp_addpath_id_for_peer(peer, afi, safi,
483 &path->tx_addpath));
484 if (!adj)
485 return;
486 }
487
488 if (adj->adv)
489 bgp_advertise_clean_subgroup(subgrp, adj);
490 adj->adv = bgp_advertise_new();
491
492 adv = adj->adv;
493 adv->rn = rn;
494 assert(adv->pathi == NULL);
495 /* bgp_path_info adj_out reference */
496 adv->pathi = bgp_path_info_lock(path);
497
498 if (attr)
499 adv->baa = bgp_advertise_intern(subgrp->hash, attr);
500 else
501 adv->baa = baa_new();
502 adv->adj = adj;
503
504 /* Add new advertisement to advertisement attribute list. */
505 bgp_advertise_add(adv->baa, adv);
506
507 /*
508 * If the update adv list is empty, trigger the member peers'
509 * mrai timers so the socket writes can happen.
510 */
511 if (!bgp_adv_fifo_count(&subgrp->sync->update)) {
512 struct peer_af *paf;
513
514 SUBGRP_FOREACH_PEER (subgrp, paf) {
515 bgp_adjust_routeadv(PAF_PEER(paf));
516 }
517 }
518
519 bgp_adv_fifo_add_tail(&subgrp->sync->update, adv);
520
521 subgrp->version = max(subgrp->version, rn->version);
522 }
523
524 /* The only time 'withdraw' will be false is if we are sending
525 * the "neighbor x.x.x.x default-originate" default and need to clear
526 * bgp_adj_out for the 0.0.0.0/0 route in the BGP table.
527 */
528 void bgp_adj_out_unset_subgroup(struct bgp_node *rn,
529 struct update_subgroup *subgrp, char withdraw,
530 uint32_t addpath_tx_id)
531 {
532 struct bgp_adj_out *adj;
533 struct bgp_advertise *adv;
534 bool trigger_write;
535
536 if (DISABLE_BGP_ANNOUNCE)
537 return;
538
539 /* Lookup existing adjacency */
540 if ((adj = adj_lookup(rn, subgrp, addpath_tx_id)) != NULL) {
541 /* Clean up previous advertisement. */
542 if (adj->adv)
543 bgp_advertise_clean_subgroup(subgrp, adj);
544
545 if (adj->attr && withdraw) {
546 /* We need advertisement structure. */
547 adj->adv = bgp_advertise_new();
548 adv = adj->adv;
549 adv->rn = rn;
550 adv->adj = adj;
551
552 /* Note if we need to trigger a packet write */
553 trigger_write =
554 !bgp_adv_fifo_count(&subgrp->sync->withdraw);
555
556 /* Add to synchronization entry for withdraw
557 * announcement. */
558 bgp_adv_fifo_add_tail(&subgrp->sync->withdraw, adv);
559
560 if (trigger_write)
561 subgroup_trigger_write(subgrp);
562 } else {
563 /* Remove myself from adjacency. */
564 RB_REMOVE(bgp_adj_out_rb, &rn->adj_out, adj);
565
566 /* Free allocated information. */
567 adj_free(adj);
568
569 bgp_unlock_node(rn);
570 }
571 }
572
573 subgrp->version = max(subgrp->version, rn->version);
574 }
575
576 void bgp_adj_out_remove_subgroup(struct bgp_node *rn, struct bgp_adj_out *adj,
577 struct update_subgroup *subgrp)
578 {
579 if (adj->attr)
580 bgp_attr_unintern(&adj->attr);
581
582 if (adj->adv)
583 bgp_advertise_clean_subgroup(subgrp, adj);
584
585 RB_REMOVE(bgp_adj_out_rb, &rn->adj_out, adj);
586 adj_free(adj);
587 }
588
589 /*
590 * Go through all the routes and clean up the adj/adv structures corresponding
591 * to the subgroup.
592 */
593 void subgroup_clear_table(struct update_subgroup *subgrp)
594 {
595 struct bgp_adj_out *aout, *taout;
596
597 SUBGRP_FOREACH_ADJ_SAFE (subgrp, aout, taout) {
598 struct bgp_node *rn = aout->rn;
599 bgp_adj_out_remove_subgroup(rn, aout, subgrp);
600 bgp_unlock_node(rn);
601 }
602 }
603
604 /*
605 * subgroup_announce_table
606 */
607 void subgroup_announce_table(struct update_subgroup *subgrp,
608 struct bgp_table *table)
609 {
610 struct bgp_node *rn;
611 struct bgp_path_info *ri;
612 struct attr attr;
613 struct peer *peer;
614 afi_t afi;
615 safi_t safi;
616 int addpath_capable;
617
618 peer = SUBGRP_PEER(subgrp);
619 afi = SUBGRP_AFI(subgrp);
620 safi = SUBGRP_SAFI(subgrp);
621 addpath_capable = bgp_addpath_encode_tx(peer, afi, safi);
622
623 if (safi == SAFI_LABELED_UNICAST)
624 safi = SAFI_UNICAST;
625
626 if (!table)
627 table = peer->bgp->rib[afi][safi];
628
629 if (safi != SAFI_MPLS_VPN && safi != SAFI_ENCAP && safi != SAFI_EVPN
630 && CHECK_FLAG(peer->af_flags[afi][safi],
631 PEER_FLAG_DEFAULT_ORIGINATE))
632 subgroup_default_originate(subgrp, 0);
633
634 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn))
635 for (ri = bgp_node_get_bgp_path_info(rn); ri; ri = ri->next)
636
637 if (CHECK_FLAG(ri->flags, BGP_PATH_SELECTED)
638 || (addpath_capable
639 && bgp_addpath_tx_path(
640 peer->addpath_type[afi][safi],
641 ri))) {
642 if (subgroup_announce_check(rn, ri, subgrp,
643 &rn->p, &attr))
644 bgp_adj_out_set_subgroup(rn, subgrp,
645 &attr, ri);
646 else
647 bgp_adj_out_unset_subgroup(
648 rn, subgrp, 1,
649 bgp_addpath_id_for_peer(
650 peer, afi, safi,
651 &ri->tx_addpath));
652 }
653
654 /*
655 * We walked through the whole table -- make sure our version number
656 * is consistent with the one on the table. This should allow
657 * subgroups to merge sooner if a peer comes up when the route node
658 * with the largest version is no longer in the table. This also
659 * covers the pathological case where all routes in the table have
660 * now been deleted.
661 */
662 subgrp->version = max(subgrp->version, table->version);
663
664 /*
665 * Start a task to merge the subgroup if necessary.
666 */
667 update_subgroup_trigger_merge_check(subgrp, 0);
668 }
669
670 /*
671 * subgroup_announce_route
672 *
673 * Refresh all routes out to a subgroup.
674 */
675 void subgroup_announce_route(struct update_subgroup *subgrp)
676 {
677 struct bgp_node *rn;
678 struct bgp_table *table;
679 struct peer *onlypeer;
680
681 if (update_subgroup_needs_refresh(subgrp)) {
682 update_subgroup_set_needs_refresh(subgrp, 0);
683 }
684
685 /*
686 * First update is deferred until ORF or ROUTE-REFRESH is received
687 */
688 onlypeer = ((SUBGRP_PCOUNT(subgrp) == 1) ? (SUBGRP_PFIRST(subgrp))->peer
689 : NULL);
690 if (onlypeer && CHECK_FLAG(onlypeer->af_sflags[SUBGRP_AFI(subgrp)]
691 [SUBGRP_SAFI(subgrp)],
692 PEER_STATUS_ORF_WAIT_REFRESH))
693 return;
694
695 if (SUBGRP_SAFI(subgrp) != SAFI_MPLS_VPN
696 && SUBGRP_SAFI(subgrp) != SAFI_ENCAP
697 && SUBGRP_SAFI(subgrp) != SAFI_EVPN)
698 subgroup_announce_table(subgrp, NULL);
699 else
700 for (rn = bgp_table_top(update_subgroup_rib(subgrp)); rn;
701 rn = bgp_route_next(rn)) {
702 table = bgp_node_get_bgp_table_info(rn);
703 if (!table)
704 continue;
705 subgroup_announce_table(subgrp, table);
706 }
707 }
708
709 void subgroup_default_originate(struct update_subgroup *subgrp, int withdraw)
710 {
711 struct bgp *bgp;
712 struct attr attr;
713 struct aspath *aspath;
714 struct bgp_path_info tmp_info;
715 struct prefix p;
716 struct peer *from;
717 struct bgp_node *rn;
718 struct bgp_path_info *ri;
719 struct peer *peer;
720 route_map_result_t ret = RMAP_DENYMATCH;
721 afi_t afi;
722 safi_t safi;
723
724 if (!subgrp)
725 return;
726
727 peer = SUBGRP_PEER(subgrp);
728 afi = SUBGRP_AFI(subgrp);
729 safi = SUBGRP_SAFI(subgrp);
730
731 if (!(afi == AFI_IP || afi == AFI_IP6))
732 return;
733
734 bgp = peer->bgp;
735 from = bgp->peer_self;
736
737 bgp_attr_default_set(&attr, BGP_ORIGIN_IGP);
738 aspath = attr.aspath;
739
740 attr.local_pref = bgp->default_local_pref;
741
742 memset(&p, 0, sizeof(p));
743 p.family = afi2family(afi);
744 p.prefixlen = 0;
745
746 if ((afi == AFI_IP6) || peer_cap_enhe(peer, afi, safi)) {
747 /* IPv6 global nexthop must be included. */
748 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV6_GLOBAL;
749
750 /* If the peer is on shared nextwork and we have link-local
751 nexthop set it. */
752 if (peer->shared_network
753 && !IN6_IS_ADDR_UNSPECIFIED(&peer->nexthop.v6_local))
754 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL;
755 }
756
757 if (peer->default_rmap[afi][safi].name) {
758 SET_FLAG(bgp->peer_self->rmap_type, PEER_RMAP_TYPE_DEFAULT);
759 for (rn = bgp_table_top(bgp->rib[afi][safi]); rn;
760 rn = bgp_route_next(rn)) {
761 for (ri = bgp_node_get_bgp_path_info(rn);
762 ri; ri = ri->next) {
763 struct attr dummy_attr;
764
765 /* Provide dummy so the route-map can't modify
766 * the attributes */
767 dummy_attr = *ri->attr;
768 tmp_info.peer = ri->peer;
769 tmp_info.attr = &dummy_attr;
770
771 ret = route_map_apply(
772 peer->default_rmap[afi][safi].map,
773 &rn->p, RMAP_BGP, &tmp_info);
774
775 /* The route map might have set attributes. If
776 * we don't flush them
777 * here, they will be leaked. */
778 bgp_attr_flush(&dummy_attr);
779 if (ret != RMAP_DENYMATCH)
780 break;
781 }
782 if (ret != RMAP_DENYMATCH)
783 break;
784 }
785 bgp->peer_self->rmap_type = 0;
786
787 if (ret == RMAP_DENYMATCH)
788 withdraw = 1;
789 }
790
791 if (withdraw) {
792 if (CHECK_FLAG(subgrp->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE))
793 subgroup_default_withdraw_packet(subgrp);
794 UNSET_FLAG(subgrp->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE);
795 } else {
796 if (!CHECK_FLAG(subgrp->sflags,
797 SUBGRP_STATUS_DEFAULT_ORIGINATE)) {
798
799 if (bgp_flag_check(bgp, BGP_FLAG_GRACEFUL_SHUTDOWN)) {
800 bgp_attr_add_gshut_community(&attr);
801 }
802
803 SET_FLAG(subgrp->sflags,
804 SUBGRP_STATUS_DEFAULT_ORIGINATE);
805 subgroup_default_update_packet(subgrp, &attr, from);
806
807 /* The 'neighbor x.x.x.x default-originate' default will
808 * act as an
809 * implicit withdraw for any previous UPDATEs sent for
810 * 0.0.0.0/0 so
811 * clear adj_out for the 0.0.0.0/0 prefix in the BGP
812 * table.
813 */
814 memset(&p, 0, sizeof(p));
815 p.family = afi2family(afi);
816 p.prefixlen = 0;
817
818 rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi,
819 &p, NULL);
820 bgp_adj_out_unset_subgroup(
821 rn, subgrp, 0,
822 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE);
823 }
824 }
825
826 aspath_unintern(&aspath);
827 }
828
829 /*
830 * Announce the BGP table to a subgroup.
831 *
832 * At startup, we try to optimize route announcement by coalescing the
833 * peer-up events. This is done only the first time - from then on,
834 * subgrp->v_coalesce will be set to zero and the normal logic
835 * prevails.
836 */
837 void subgroup_announce_all(struct update_subgroup *subgrp)
838 {
839 if (!subgrp)
840 return;
841
842 /*
843 * If coalesce timer value is not set, announce routes immediately.
844 */
845 if (!subgrp->v_coalesce) {
846 if (bgp_debug_update(NULL, NULL, subgrp->update_group, 0))
847 zlog_debug("u%" PRIu64 ":s%" PRIu64
848 " announcing all routes",
849 subgrp->update_group->id, subgrp->id);
850 subgroup_announce_route(subgrp);
851 return;
852 }
853
854 /*
855 * We should wait for the coalesce timer. Arm the timer if not done.
856 */
857 if (!subgrp->t_coalesce) {
858 thread_add_timer_msec(bm->master, subgroup_coalesce_timer,
859 subgrp, subgrp->v_coalesce,
860 &subgrp->t_coalesce);
861 }
862 }
863
864 /*
865 * Go through all update subgroups and set up the adv queue for the
866 * input route.
867 */
868 void group_announce_route(struct bgp *bgp, afi_t afi, safi_t safi,
869 struct bgp_node *rn, struct bgp_path_info *pi)
870 {
871 struct updwalk_context ctx;
872 ctx.pi = pi;
873 ctx.rn = rn;
874 update_group_af_walk(bgp, afi, safi, group_announce_route_walkcb, &ctx);
875 }
876
877 void update_group_show_adj_queue(struct bgp *bgp, afi_t afi, safi_t safi,
878 struct vty *vty, uint64_t id)
879 {
880 updgrp_show_adj(bgp, afi, safi, vty, id, UPDWALK_FLAGS_ADVQUEUE);
881 }
882
883 void update_group_show_advertised(struct bgp *bgp, afi_t afi, safi_t safi,
884 struct vty *vty, uint64_t id)
885 {
886 updgrp_show_adj(bgp, afi, safi, vty, id, UPDWALK_FLAGS_ADVERTISED);
887 }
888
889 void update_group_announce(struct bgp *bgp)
890 {
891 update_group_walk(bgp, update_group_announce_walkcb, NULL);
892 }
893
894 void update_group_announce_rrclients(struct bgp *bgp)
895 {
896 update_group_walk(bgp, update_group_announce_rrc_walkcb, NULL);
897 }