]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_updgrp_adv.c
Merge pull request #7270 from donaldsharp/isis_cleanup
[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 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
73 return 0;
74 }
75 RB_GENERATE(bgp_adj_out_rb, bgp_adj_out, adj_entry, bgp_adj_out_compare);
76
77 static inline struct bgp_adj_out *adj_lookup(struct bgp_dest *dest,
78 struct update_subgroup *subgrp,
79 uint32_t addpath_tx_id)
80 {
81 struct bgp_adj_out lookup;
82
83 if (!dest || !subgrp)
84 return NULL;
85
86 /* update-groups that do not support addpath will pass 0 for
87 * addpath_tx_id. */
88 lookup.subgroup = subgrp;
89 lookup.addpath_tx_id = addpath_tx_id;
90
91 return RB_FIND(bgp_adj_out_rb, &dest->adj_out, &lookup);
92 }
93
94 static void adj_free(struct bgp_adj_out *adj)
95 {
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);
99 }
100
101 static 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 */
113 RB_FOREACH_SAFE (adj, bgp_adj_out_rb, &ctx->dest->adj_out, adj_next) {
114
115 if (adj->subgroup == subgrp) {
116 for (pi = bgp_dest_get_bgp_path_info(ctx->dest); pi;
117 pi = pi->next) {
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->dest,
129 adj->addpath_tx_id);
130 }
131 }
132 }
133 }
134
135 static int group_announce_route_walkcb(struct update_group *updgrp, void *arg)
136 {
137 struct updwalk_context *ctx = arg;
138 struct update_subgroup *subgrp;
139 struct bgp_path_info *pi;
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
151 if (BGP_DEBUG(update, UPDATE_OUT))
152 zlog_debug("%s: afi=%s, safi=%s, p=%pRN", __func__,
153 afi2str(afi), safi2str(safi),
154 bgp_dest_to_rnode(ctx->dest));
155
156 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
157
158 /*
159 * Skip the subgroups that have coalesce timer running. We will
160 * walk the entire prefix table for those subgroups when the
161 * coalesce timer fires.
162 */
163 if (!subgrp->t_coalesce) {
164 /* An update-group that uses addpath */
165 if (addpath_capable) {
166 subgrp_withdraw_stale_addpath(ctx, subgrp);
167
168 for (pi = bgp_dest_get_bgp_path_info(ctx->dest);
169 pi; pi = pi->next) {
170 /* Skip the bestpath for now */
171 if (pi == ctx->pi)
172 continue;
173
174 subgroup_process_announce_selected(
175 subgrp, pi, ctx->dest,
176 bgp_addpath_id_for_peer(
177 peer, afi, safi,
178 &pi->tx_addpath));
179 }
180
181 /* Process the bestpath last so the "show [ip]
182 * bgp neighbor x.x.x.x advertised"
183 * output shows the attributes from the bestpath
184 */
185 if (ctx->pi)
186 subgroup_process_announce_selected(
187 subgrp, ctx->pi, ctx->dest,
188 bgp_addpath_id_for_peer(
189 peer, afi, safi,
190 &ctx->pi->tx_addpath));
191 }
192
193 /* An update-group that does not use addpath */
194 else {
195 if (ctx->pi) {
196 subgroup_process_announce_selected(
197 subgrp, ctx->pi, ctx->dest,
198 bgp_addpath_id_for_peer(
199 peer, afi, safi,
200 &ctx->pi->tx_addpath));
201 } else {
202 /* Find the addpath_tx_id of the path we
203 * had advertised and
204 * send a withdraw */
205 RB_FOREACH_SAFE (adj, bgp_adj_out_rb,
206 &ctx->dest->adj_out,
207 adj_next) {
208 if (adj->subgroup == subgrp) {
209 subgroup_process_announce_selected(
210 subgrp, NULL,
211 ctx->dest,
212 adj->addpath_tx_id);
213 }
214 }
215 }
216 }
217 }
218 }
219
220 return UPDWALK_CONTINUE;
221 }
222
223 static void subgrp_show_adjq_vty(struct update_subgroup *subgrp,
224 struct vty *vty, uint8_t flags)
225 {
226 struct bgp_table *table;
227 struct bgp_adj_out *adj;
228 unsigned long output_count;
229 struct bgp_dest *dest;
230 int header1 = 1;
231 struct bgp *bgp;
232 int header2 = 1;
233
234 bgp = SUBGRP_INST(subgrp);
235 if (!bgp)
236 return;
237
238 table = bgp->rib[SUBGRP_AFI(subgrp)][SUBGRP_SAFI(subgrp)];
239
240 output_count = 0;
241
242 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
243 const struct prefix *dest_p = bgp_dest_get_prefix(dest);
244
245 RB_FOREACH (adj, bgp_adj_out_rb, &dest->adj_out)
246 if (adj->subgroup == subgrp) {
247 if (header1) {
248 vty_out(vty,
249 "BGP table version is %" PRIu64", local router ID is %s\n",
250 table->version,
251 inet_ntoa(bgp->router_id));
252 vty_out(vty, BGP_SHOW_SCODE_HEADER);
253 vty_out(vty, BGP_SHOW_OCODE_HEADER);
254 header1 = 0;
255 }
256 if (header2) {
257 vty_out(vty, BGP_SHOW_HEADER);
258 header2 = 0;
259 }
260 if ((flags & UPDWALK_FLAGS_ADVQUEUE) && adj->adv
261 && adj->adv->baa) {
262 route_vty_out_tmp(vty, dest_p,
263 adj->adv->baa->attr,
264 SUBGRP_SAFI(subgrp),
265 0, NULL, false);
266 output_count++;
267 }
268 if ((flags & UPDWALK_FLAGS_ADVERTISED)
269 && adj->attr) {
270 route_vty_out_tmp(vty, dest_p,
271 adj->attr,
272 SUBGRP_SAFI(subgrp),
273 0, NULL, false);
274 output_count++;
275 }
276 }
277 }
278 if (output_count != 0)
279 vty_out(vty, "\nTotal number of prefixes %ld\n", output_count);
280 }
281
282 static int updgrp_show_adj_walkcb(struct update_group *updgrp, void *arg)
283 {
284 struct updwalk_context *ctx = arg;
285 struct update_subgroup *subgrp;
286 struct vty *vty;
287
288 vty = ctx->vty;
289 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
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;
297 }
298
299 static void updgrp_show_adj(struct bgp *bgp, afi_t afi, safi_t safi,
300 struct vty *vty, uint64_t id, uint8_t flags)
301 {
302 struct updwalk_context ctx;
303 memset(&ctx, 0, sizeof(ctx));
304 ctx.vty = vty;
305 ctx.subgrp_id = id;
306 ctx.flags = flags;
307
308 update_group_af_walk(bgp, afi, safi, updgrp_show_adj_walkcb, &ctx);
309 }
310
311 static int subgroup_coalesce_timer(struct thread *thread)
312 {
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" announcing routes upon coalesce timer expiry(%u ms)",
318 (SUBGRP_UPDGRP(subgrp))->id, subgrp->id,
319 subgrp->v_coalesce);
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
337 SUBGRP_FOREACH_PEER (subgrp, paf) {
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;
345 }
346
347 static int update_group_announce_walkcb(struct update_group *updgrp, void *arg)
348 {
349 struct update_subgroup *subgrp;
350
351 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
352 subgroup_announce_all(subgrp);
353 }
354
355 return UPDWALK_CONTINUE;
356 }
357
358 static int update_group_announce_rrc_walkcb(struct update_group *updgrp,
359 void *arg)
360 {
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)) {
372 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
373 subgroup_announce_all(subgrp);
374 }
375 }
376
377 return UPDWALK_CONTINUE;
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 */
388 struct bgp_adj_out *bgp_adj_out_alloc(struct update_subgroup *subgrp,
389 struct bgp_dest *dest,
390 uint32_t addpath_tx_id)
391 {
392 struct bgp_adj_out *adj;
393
394 adj = XCALLOC(MTYPE_BGP_ADJ_OUT, sizeof(struct bgp_adj_out));
395 adj->subgroup = subgrp;
396 adj->addpath_tx_id = addpath_tx_id;
397
398 if (dest) {
399 RB_INSERT(bgp_adj_out_rb, &dest->adj_out, adj);
400 bgp_dest_lock_node(dest);
401 adj->dest = dest;
402 }
403
404 TAILQ_INSERT_TAIL(&(subgrp->adjq), adj, subgrp_adj_train);
405 SUBGRP_INCR_STAT(subgrp, adj_count);
406 return adj;
407 }
408
409
410 struct bgp_advertise *
411 bgp_advertise_clean_subgroup(struct update_subgroup *subgrp,
412 struct bgp_adj_out *adj)
413 {
414 struct bgp_advertise *adv;
415 struct bgp_advertise_attr *baa;
416 struct bgp_advertise *next;
417 struct bgp_adv_fifo_head *fhead;
418
419 adv = adj->adv;
420 baa = adv->baa;
421 next = NULL;
422
423 if (baa) {
424 fhead = &subgrp->sync->update;
425
426 /* Unlink myself from advertise attribute FIFO. */
427 bgp_advertise_delete(baa, adv);
428
429 /* Fetch next advertise candidate. */
430 next = baa->adv;
431
432 /* Unintern BGP advertise attribute. */
433 bgp_advertise_unintern(subgrp->hash, baa);
434 } else
435 fhead = &subgrp->sync->withdraw;
436
437
438 /* Unlink myself from advertisement FIFO. */
439 bgp_adv_fifo_del(fhead, adv);
440
441 /* Free memory. */
442 bgp_advertise_free(adj->adv);
443 adj->adv = NULL;
444
445 return next;
446 }
447
448 void bgp_adj_out_set_subgroup(struct bgp_dest *dest,
449 struct update_subgroup *subgrp, struct attr *attr,
450 struct bgp_path_info *path)
451 {
452 struct bgp_adj_out *adj = NULL;
453 struct bgp_advertise *adv;
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);
461
462 if (DISABLE_BGP_ANNOUNCE)
463 return;
464
465 /* Look for adjacency information. */
466 adj = adj_lookup(
467 dest, subgrp,
468 bgp_addpath_id_for_peer(peer, afi, safi, &path->tx_addpath));
469
470 if (!adj) {
471 adj = bgp_adj_out_alloc(
472 subgrp, dest,
473 bgp_addpath_id_for_peer(peer, afi, safi,
474 &path->tx_addpath));
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->dest = dest;
485 assert(adv->pathi == NULL);
486 /* bgp_path_info adj_out reference */
487 adv->pathi = bgp_path_info_lock(path);
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 */
502 if (!bgp_adv_fifo_count(&subgrp->sync->update)) {
503 struct peer_af *paf;
504
505 SUBGRP_FOREACH_PEER (subgrp, paf) {
506 bgp_adjust_routeadv(PAF_PEER(paf));
507 }
508 }
509
510 bgp_adv_fifo_add_tail(&subgrp->sync->update, adv);
511
512 subgrp->version = max(subgrp->version, dest->version);
513 }
514
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 */
519 void bgp_adj_out_unset_subgroup(struct bgp_dest *dest,
520 struct update_subgroup *subgrp, char withdraw,
521 uint32_t addpath_tx_id)
522 {
523 struct bgp_adj_out *adj;
524 struct bgp_advertise *adv;
525 bool trigger_write;
526
527 if (DISABLE_BGP_ANNOUNCE)
528 return;
529
530 /* Lookup existing adjacency */
531 if ((adj = adj_lookup(dest, subgrp, addpath_tx_id)) != NULL) {
532 /* Clean up previous advertisement. */
533 if (adj->adv)
534 bgp_advertise_clean_subgroup(subgrp, adj);
535
536 /* If default originate is enabled and the route is default
537 * route, do not send withdraw. This will prevent deletion of
538 * the default route at the peer.
539 */
540 if (CHECK_FLAG(subgrp->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE)
541 && is_default_prefix(bgp_dest_get_prefix(dest)))
542 return;
543
544 if (adj->attr && withdraw) {
545 /* We need advertisement structure. */
546 adj->adv = bgp_advertise_new();
547 adv = adj->adv;
548 adv->dest = dest;
549 adv->adj = adj;
550
551 /* Note if we need to trigger a packet write */
552 trigger_write =
553 !bgp_adv_fifo_count(&subgrp->sync->withdraw);
554
555 /* Add to synchronization entry for withdraw
556 * announcement. */
557 bgp_adv_fifo_add_tail(&subgrp->sync->withdraw, adv);
558
559 if (trigger_write)
560 subgroup_trigger_write(subgrp);
561 } else {
562 /* Remove myself from adjacency. */
563 RB_REMOVE(bgp_adj_out_rb, &dest->adj_out, adj);
564
565 /* Free allocated information. */
566 adj_free(adj);
567
568 bgp_dest_unlock_node(dest);
569 }
570 }
571
572 subgrp->version = max(subgrp->version, dest->version);
573 }
574
575 void bgp_adj_out_remove_subgroup(struct bgp_dest *dest, struct bgp_adj_out *adj,
576 struct update_subgroup *subgrp)
577 {
578 if (adj->attr)
579 bgp_attr_unintern(&adj->attr);
580
581 if (adj->adv)
582 bgp_advertise_clean_subgroup(subgrp, adj);
583
584 RB_REMOVE(bgp_adj_out_rb, &dest->adj_out, adj);
585 adj_free(adj);
586 }
587
588 /*
589 * Go through all the routes and clean up the adj/adv structures corresponding
590 * to the subgroup.
591 */
592 void subgroup_clear_table(struct update_subgroup *subgrp)
593 {
594 struct bgp_adj_out *aout, *taout;
595
596 SUBGRP_FOREACH_ADJ_SAFE (subgrp, aout, taout) {
597 struct bgp_dest *dest = aout->dest;
598 bgp_adj_out_remove_subgroup(dest, aout, subgrp);
599 bgp_dest_unlock_node(dest);
600 }
601 }
602
603 /*
604 * subgroup_announce_table
605 */
606 void subgroup_announce_table(struct update_subgroup *subgrp,
607 struct bgp_table *table)
608 {
609 struct bgp_dest *dest;
610 struct bgp_path_info *ri;
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 (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
634 const struct prefix *dest_p = bgp_dest_get_prefix(dest);
635
636 for (ri = bgp_dest_get_bgp_path_info(dest); ri; ri = ri->next)
637
638 if (CHECK_FLAG(ri->flags, BGP_PATH_SELECTED)
639 || (addpath_capable
640 && bgp_addpath_tx_path(
641 peer->addpath_type[afi][safi],
642 ri))) {
643 if (subgroup_announce_check(dest, ri, subgrp,
644 dest_p, &attr))
645 bgp_adj_out_set_subgroup(dest, subgrp,
646 &attr, ri);
647 else {
648 /* If default originate is enabled for
649 * the peer, do not send explicit
650 * withdraw. This will prevent deletion
651 * of default route advertised through
652 * default originate
653 */
654 if (CHECK_FLAG(
655 peer->af_flags[afi][safi],
656 PEER_FLAG_DEFAULT_ORIGINATE)
657 && is_default_prefix(bgp_dest_get_prefix(dest)))
658 break;
659
660 bgp_adj_out_unset_subgroup(
661 dest, subgrp, 1,
662 bgp_addpath_id_for_peer(
663 peer, afi, safi,
664 &ri->tx_addpath));
665 }
666 }
667 }
668
669 /*
670 * We walked through the whole table -- make sure our version number
671 * is consistent with the one on the table. This should allow
672 * subgroups to merge sooner if a peer comes up when the route node
673 * with the largest version is no longer in the table. This also
674 * covers the pathological case where all routes in the table have
675 * now been deleted.
676 */
677 subgrp->version = max(subgrp->version, table->version);
678
679 /*
680 * Start a task to merge the subgroup if necessary.
681 */
682 update_subgroup_trigger_merge_check(subgrp, 0);
683 }
684
685 /*
686 * subgroup_announce_route
687 *
688 * Refresh all routes out to a subgroup.
689 */
690 void subgroup_announce_route(struct update_subgroup *subgrp)
691 {
692 struct bgp_dest *dest;
693 struct bgp_table *table;
694 struct peer *onlypeer;
695
696 if (update_subgroup_needs_refresh(subgrp)) {
697 update_subgroup_set_needs_refresh(subgrp, 0);
698 }
699
700 /*
701 * First update is deferred until ORF or ROUTE-REFRESH is received
702 */
703 onlypeer = ((SUBGRP_PCOUNT(subgrp) == 1) ? (SUBGRP_PFIRST(subgrp))->peer
704 : NULL);
705 if (onlypeer && CHECK_FLAG(onlypeer->af_sflags[SUBGRP_AFI(subgrp)]
706 [SUBGRP_SAFI(subgrp)],
707 PEER_STATUS_ORF_WAIT_REFRESH))
708 return;
709
710 if (SUBGRP_SAFI(subgrp) != SAFI_MPLS_VPN
711 && SUBGRP_SAFI(subgrp) != SAFI_ENCAP
712 && SUBGRP_SAFI(subgrp) != SAFI_EVPN)
713 subgroup_announce_table(subgrp, NULL);
714 else
715 for (dest = bgp_table_top(update_subgroup_rib(subgrp)); dest;
716 dest = bgp_route_next(dest)) {
717 table = bgp_dest_get_bgp_table_info(dest);
718 if (!table)
719 continue;
720 subgroup_announce_table(subgrp, table);
721 }
722 }
723
724 void subgroup_default_originate(struct update_subgroup *subgrp, int withdraw)
725 {
726 struct bgp *bgp;
727 struct attr attr;
728 struct attr *new_attr = &attr;
729 struct aspath *aspath;
730 struct prefix p;
731 struct peer *from;
732 struct bgp_dest *dest;
733 struct bgp_path_info *pi;
734 struct peer *peer;
735 struct bgp_adj_out *adj;
736 route_map_result_t ret = RMAP_DENYMATCH;
737 afi_t afi;
738 safi_t safi;
739
740 if (!subgrp)
741 return;
742
743 peer = SUBGRP_PEER(subgrp);
744 afi = SUBGRP_AFI(subgrp);
745 safi = SUBGRP_SAFI(subgrp);
746
747 if (!(afi == AFI_IP || afi == AFI_IP6))
748 return;
749
750 bgp = peer->bgp;
751 from = bgp->peer_self;
752
753 bgp_attr_default_set(&attr, BGP_ORIGIN_IGP);
754 aspath = attr.aspath;
755
756 attr.local_pref = bgp->default_local_pref;
757
758 if ((afi == AFI_IP6) || peer_cap_enhe(peer, afi, safi)) {
759 /* IPv6 global nexthop must be included. */
760 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV6_GLOBAL;
761
762 /* If the peer is on shared nextwork and we have link-local
763 nexthop set it. */
764 if (peer->shared_network
765 && !IN6_IS_ADDR_UNSPECIFIED(&peer->nexthop.v6_local))
766 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL;
767 }
768
769 if (peer->default_rmap[afi][safi].name) {
770 struct attr attr_tmp = attr;
771 struct bgp_path_info bpi_rmap = {0};
772
773 bpi_rmap.peer = bgp->peer_self;
774 bpi_rmap.attr = &attr_tmp;
775
776 SET_FLAG(bgp->peer_self->rmap_type, PEER_RMAP_TYPE_DEFAULT);
777
778 /* Iterate over the RIB to see if we can announce
779 * the default route. We announce the default
780 * route only if route-map has a match.
781 */
782 for (dest = bgp_table_top(bgp->rib[afi][safi]); dest;
783 dest = bgp_route_next(dest)) {
784 if (!bgp_dest_has_bgp_path_info_data(dest))
785 continue;
786
787 ret = route_map_apply(peer->default_rmap[afi][safi].map,
788 bgp_dest_get_prefix(dest),
789 RMAP_BGP, &bpi_rmap);
790
791 if (ret != RMAP_DENYMATCH)
792 break;
793 }
794 bgp->peer_self->rmap_type = 0;
795 new_attr = bgp_attr_intern(&attr_tmp);
796
797 if (ret == RMAP_DENYMATCH) {
798 bgp_attr_flush(&attr_tmp);
799 withdraw = 1;
800 }
801 }
802
803 /* Check if the default route is in local BGP RIB which is
804 * installed through redistribute or network command
805 */
806 memset(&p, 0, sizeof(p));
807 p.family = afi2family(afi);
808 p.prefixlen = 0;
809 dest = bgp_afi_node_lookup(bgp->rib[afi][safi], afi, safi, &p, NULL);
810
811 if (withdraw) {
812 /* Withdraw the default route advertised using default
813 * originate
814 */
815 if (CHECK_FLAG(subgrp->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE))
816 subgroup_default_withdraw_packet(subgrp);
817 UNSET_FLAG(subgrp->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE);
818
819 /* If default route is present in the local RIB, advertise the
820 * route
821 */
822 if (dest != NULL) {
823 for (pi = bgp_dest_get_bgp_path_info(dest); pi;
824 pi = pi->next) {
825 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED))
826 if (subgroup_announce_check(
827 dest, pi, subgrp,
828 bgp_dest_get_prefix(dest),
829 &attr))
830 bgp_adj_out_set_subgroup(
831 dest, subgrp, &attr,
832 pi);
833 }
834 }
835 } else {
836 if (!CHECK_FLAG(subgrp->sflags,
837 SUBGRP_STATUS_DEFAULT_ORIGINATE)) {
838
839 /* The 'neighbor x.x.x.x default-originate' default will
840 * act as an
841 * implicit withdraw for any previous UPDATEs sent for
842 * 0.0.0.0/0 so
843 * clear adj_out for the 0.0.0.0/0 prefix in the BGP
844 * table.
845 */
846 if (dest != NULL) {
847 /* Remove the adjacency for the previously
848 * advertised default route
849 */
850 adj = adj_lookup(
851 dest, subgrp,
852 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE);
853 if (adj != NULL) {
854 /* Clean up previous advertisement. */
855 if (adj->adv)
856 bgp_advertise_clean_subgroup(
857 subgrp, adj);
858
859 /* Remove from adjacency. */
860 RB_REMOVE(bgp_adj_out_rb,
861 &dest->adj_out, adj);
862
863 /* Free allocated information. */
864 adj_free(adj);
865
866 bgp_dest_unlock_node(dest);
867 }
868 }
869
870 /* Advertise the default route */
871 if (bgp_in_graceful_shutdown(bgp))
872 bgp_attr_add_gshut_community(new_attr);
873
874 SET_FLAG(subgrp->sflags,
875 SUBGRP_STATUS_DEFAULT_ORIGINATE);
876 subgroup_default_update_packet(subgrp, new_attr, from);
877 }
878 }
879
880 aspath_unintern(&aspath);
881 }
882
883 /*
884 * Announce the BGP table to a subgroup.
885 *
886 * At startup, we try to optimize route announcement by coalescing the
887 * peer-up events. This is done only the first time - from then on,
888 * subgrp->v_coalesce will be set to zero and the normal logic
889 * prevails.
890 */
891 void subgroup_announce_all(struct update_subgroup *subgrp)
892 {
893 if (!subgrp)
894 return;
895
896 /*
897 * If coalesce timer value is not set, announce routes immediately.
898 */
899 if (!subgrp->v_coalesce) {
900 if (bgp_debug_update(NULL, NULL, subgrp->update_group, 0))
901 zlog_debug("u%" PRIu64 ":s%" PRIu64" announcing all routes",
902 subgrp->update_group->id, subgrp->id);
903 subgroup_announce_route(subgrp);
904 return;
905 }
906
907 /*
908 * We should wait for the coalesce timer. Arm the timer if not done.
909 */
910 if (!subgrp->t_coalesce) {
911 thread_add_timer_msec(bm->master, subgroup_coalesce_timer,
912 subgrp, subgrp->v_coalesce,
913 &subgrp->t_coalesce);
914 }
915 }
916
917 /*
918 * Go through all update subgroups and set up the adv queue for the
919 * input route.
920 */
921 void group_announce_route(struct bgp *bgp, afi_t afi, safi_t safi,
922 struct bgp_dest *dest, struct bgp_path_info *pi)
923 {
924 struct updwalk_context ctx;
925 ctx.pi = pi;
926 ctx.dest = dest;
927 update_group_af_walk(bgp, afi, safi, group_announce_route_walkcb, &ctx);
928 }
929
930 void update_group_show_adj_queue(struct bgp *bgp, afi_t afi, safi_t safi,
931 struct vty *vty, uint64_t id)
932 {
933 updgrp_show_adj(bgp, afi, safi, vty, id, UPDWALK_FLAGS_ADVQUEUE);
934 }
935
936 void update_group_show_advertised(struct bgp *bgp, afi_t afi, safi_t safi,
937 struct vty *vty, uint64_t id)
938 {
939 updgrp_show_adj(bgp, afi, safi, vty, id, UPDWALK_FLAGS_ADVERTISED);
940 }
941
942 void update_group_announce(struct bgp *bgp)
943 {
944 update_group_walk(bgp, update_group_announce_walkcb, NULL);
945 }
946
947 void update_group_announce_rrclients(struct bgp *bgp)
948 {
949 update_group_walk(bgp, update_group_announce_rrc_walkcb, NULL);
950 }