]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_updgrp_adv.c
Merge pull request #10447 from ton31337/fix/json_with_whitespaces
[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
99 RB_REMOVE(bgp_adj_out_rb, &adj->dest->adj_out, adj);
100 bgp_dest_unlock_node(adj->dest);
101
102 XFREE(MTYPE_BGP_ADJ_OUT, adj);
103 }
104
105 static void subgrp_withdraw_stale_addpath(struct updwalk_context *ctx,
106 struct update_subgroup *subgrp)
107 {
108 struct bgp_adj_out *adj, *adj_next;
109 uint32_t id;
110 struct bgp_path_info *pi;
111 afi_t afi = SUBGRP_AFI(subgrp);
112 safi_t safi = SUBGRP_SAFI(subgrp);
113 struct peer *peer = SUBGRP_PEER(subgrp);
114
115 /* Look through all of the paths we have advertised for this rn and send
116 * a withdraw for the ones that are no longer present */
117 RB_FOREACH_SAFE (adj, bgp_adj_out_rb, &ctx->dest->adj_out, adj_next) {
118
119 if (adj->subgroup == subgrp) {
120 for (pi = bgp_dest_get_bgp_path_info(ctx->dest); pi;
121 pi = pi->next) {
122 id = bgp_addpath_id_for_peer(peer, afi, safi,
123 &pi->tx_addpath);
124
125 if (id == adj->addpath_tx_id) {
126 break;
127 }
128 }
129
130 if (!pi) {
131 subgroup_process_announce_selected(
132 subgrp, NULL, ctx->dest,
133 adj->addpath_tx_id);
134 }
135 }
136 }
137 }
138
139 static int group_announce_route_walkcb(struct update_group *updgrp, void *arg)
140 {
141 struct updwalk_context *ctx = arg;
142 struct update_subgroup *subgrp;
143 struct bgp_path_info *pi;
144 afi_t afi;
145 safi_t safi;
146 struct peer *peer;
147 struct bgp_adj_out *adj, *adj_next;
148 bool addpath_capable;
149
150 afi = UPDGRP_AFI(updgrp);
151 safi = UPDGRP_SAFI(updgrp);
152 peer = UPDGRP_PEER(updgrp);
153 addpath_capable = bgp_addpath_encode_tx(peer, afi, safi);
154
155 if (BGP_DEBUG(update, UPDATE_OUT))
156 zlog_debug("%s: afi=%s, safi=%s, p=%pRN", __func__,
157 afi2str(afi), safi2str(safi),
158 bgp_dest_to_rnode(ctx->dest));
159
160 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
161
162 /*
163 * Skip the subgroups that have coalesce timer running. We will
164 * walk the entire prefix table for those subgroups when the
165 * coalesce timer fires.
166 */
167 if (!subgrp->t_coalesce) {
168 /* An update-group that uses addpath */
169 if (addpath_capable) {
170 subgrp_withdraw_stale_addpath(ctx, subgrp);
171
172 for (pi = bgp_dest_get_bgp_path_info(ctx->dest);
173 pi; pi = pi->next) {
174 /* Skip the bestpath for now */
175 if (pi == ctx->pi)
176 continue;
177
178 subgroup_process_announce_selected(
179 subgrp, pi, ctx->dest,
180 bgp_addpath_id_for_peer(
181 peer, afi, safi,
182 &pi->tx_addpath));
183 }
184
185 /* Process the bestpath last so the "show [ip]
186 * bgp neighbor x.x.x.x advertised"
187 * output shows the attributes from the bestpath
188 */
189 if (ctx->pi)
190 subgroup_process_announce_selected(
191 subgrp, ctx->pi, ctx->dest,
192 bgp_addpath_id_for_peer(
193 peer, afi, safi,
194 &ctx->pi->tx_addpath));
195 }
196
197 /* An update-group that does not use addpath */
198 else {
199 if (ctx->pi) {
200 subgroup_process_announce_selected(
201 subgrp, ctx->pi, ctx->dest,
202 bgp_addpath_id_for_peer(
203 peer, afi, safi,
204 &ctx->pi->tx_addpath));
205 } else {
206 /* Find the addpath_tx_id of the path we
207 * had advertised and
208 * send a withdraw */
209 RB_FOREACH_SAFE (adj, bgp_adj_out_rb,
210 &ctx->dest->adj_out,
211 adj_next) {
212 if (adj->subgroup == subgrp) {
213 subgroup_process_announce_selected(
214 subgrp, NULL,
215 ctx->dest,
216 adj->addpath_tx_id);
217 }
218 }
219 }
220 }
221 }
222
223 /* Notify BGP Conditional advertisement */
224 bgp_notify_conditional_adv_scanner(subgrp);
225 }
226
227 return UPDWALK_CONTINUE;
228 }
229
230 static void subgrp_show_adjq_vty(struct update_subgroup *subgrp,
231 struct vty *vty, uint8_t flags)
232 {
233 struct bgp_table *table;
234 struct bgp_adj_out *adj;
235 unsigned long output_count;
236 struct bgp_dest *dest;
237 int header1 = 1;
238 struct bgp *bgp;
239 int header2 = 1;
240
241 bgp = SUBGRP_INST(subgrp);
242 if (!bgp)
243 return;
244
245 table = bgp->rib[SUBGRP_AFI(subgrp)][SUBGRP_SAFI(subgrp)];
246
247 output_count = 0;
248
249 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
250 const struct prefix *dest_p = bgp_dest_get_prefix(dest);
251
252 RB_FOREACH (adj, bgp_adj_out_rb, &dest->adj_out)
253 if (adj->subgroup == subgrp) {
254 if (header1) {
255 vty_out(vty,
256 "BGP table version is %" PRIu64
257 ", local router ID is %pI4\n",
258 table->version,
259 &bgp->router_id);
260 vty_out(vty, BGP_SHOW_SCODE_HEADER);
261 vty_out(vty, BGP_SHOW_OCODE_HEADER);
262 header1 = 0;
263 }
264 if (header2) {
265 vty_out(vty, BGP_SHOW_HEADER);
266 header2 = 0;
267 }
268 if ((flags & UPDWALK_FLAGS_ADVQUEUE) && adj->adv
269 && adj->adv->baa) {
270 route_vty_out_tmp(vty, dest, dest_p,
271 adj->adv->baa->attr,
272 SUBGRP_SAFI(subgrp),
273 0, NULL, false);
274 output_count++;
275 }
276 if ((flags & UPDWALK_FLAGS_ADVERTISED)
277 && adj->attr) {
278 route_vty_out_tmp(vty, dest, dest_p,
279 adj->attr,
280 SUBGRP_SAFI(subgrp),
281 0, NULL, false);
282 output_count++;
283 }
284 }
285 }
286 if (output_count != 0)
287 vty_out(vty, "\nTotal number of prefixes %ld\n", output_count);
288 }
289
290 static int updgrp_show_adj_walkcb(struct update_group *updgrp, void *arg)
291 {
292 struct updwalk_context *ctx = arg;
293 struct update_subgroup *subgrp;
294 struct vty *vty;
295
296 vty = ctx->vty;
297 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
298 if (ctx->subgrp_id && (ctx->subgrp_id != subgrp->id))
299 continue;
300 vty_out(vty, "update group %" PRIu64 ", subgroup %" PRIu64 "\n",
301 updgrp->id, subgrp->id);
302 subgrp_show_adjq_vty(subgrp, vty, ctx->flags);
303 }
304 return UPDWALK_CONTINUE;
305 }
306
307 static void updgrp_show_adj(struct bgp *bgp, afi_t afi, safi_t safi,
308 struct vty *vty, uint64_t id, uint8_t flags)
309 {
310 struct updwalk_context ctx;
311 memset(&ctx, 0, sizeof(ctx));
312 ctx.vty = vty;
313 ctx.subgrp_id = id;
314 ctx.flags = flags;
315
316 update_group_af_walk(bgp, afi, safi, updgrp_show_adj_walkcb, &ctx);
317 }
318
319 static void subgroup_coalesce_timer(struct thread *thread)
320 {
321 struct update_subgroup *subgrp;
322 struct bgp *bgp;
323
324 subgrp = THREAD_ARG(thread);
325 if (bgp_debug_update(NULL, NULL, subgrp->update_group, 0))
326 zlog_debug("u%" PRIu64 ":s%" PRIu64" announcing routes upon coalesce timer expiry(%u ms)",
327 (SUBGRP_UPDGRP(subgrp))->id, subgrp->id,
328 subgrp->v_coalesce);
329 subgrp->t_coalesce = NULL;
330 subgrp->v_coalesce = 0;
331 bgp = SUBGRP_INST(subgrp);
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 !(BGP_SUPPRESS_FIB_ENABLED(bgp))) {
345 struct peer_af *paf;
346 struct peer *peer;
347
348 SUBGRP_FOREACH_PEER (subgrp, paf) {
349 peer = PAF_PEER(paf);
350 BGP_TIMER_OFF(peer->t_routeadv);
351 BGP_TIMER_ON(peer->t_routeadv, bgp_routeadv_timer, 0);
352 }
353 }
354 }
355
356 static int update_group_announce_walkcb(struct update_group *updgrp, void *arg)
357 {
358 struct update_subgroup *subgrp;
359
360 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
361 subgroup_announce_all(subgrp);
362 }
363
364 return UPDWALK_CONTINUE;
365 }
366
367 static int update_group_announce_rrc_walkcb(struct update_group *updgrp,
368 void *arg)
369 {
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)) {
381 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
382 subgroup_announce_all(subgrp);
383 }
384 }
385
386 return UPDWALK_CONTINUE;
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 */
397 struct bgp_adj_out *bgp_adj_out_alloc(struct update_subgroup *subgrp,
398 struct bgp_dest *dest,
399 uint32_t addpath_tx_id)
400 {
401 struct bgp_adj_out *adj;
402
403 adj = XCALLOC(MTYPE_BGP_ADJ_OUT, sizeof(struct bgp_adj_out));
404 adj->subgroup = subgrp;
405 adj->addpath_tx_id = addpath_tx_id;
406
407 RB_INSERT(bgp_adj_out_rb, &dest->adj_out, adj);
408 bgp_dest_lock_node(dest);
409 adj->dest = dest;
410
411 TAILQ_INSERT_TAIL(&(subgrp->adjq), adj, subgrp_adj_train);
412 SUBGRP_INCR_STAT(subgrp, adj_count);
413 return adj;
414 }
415
416
417 struct bgp_advertise *
418 bgp_advertise_clean_subgroup(struct update_subgroup *subgrp,
419 struct bgp_adj_out *adj)
420 {
421 struct bgp_advertise *adv;
422 struct bgp_advertise_attr *baa;
423 struct bgp_advertise *next;
424 struct bgp_adv_fifo_head *fhead;
425
426 adv = adj->adv;
427 baa = adv->baa;
428 next = NULL;
429
430 if (baa) {
431 fhead = &subgrp->sync->update;
432
433 /* Unlink myself from advertise attribute FIFO. */
434 bgp_advertise_delete(baa, adv);
435
436 /* Fetch next advertise candidate. */
437 next = baa->adv;
438
439 /* Unintern BGP advertise attribute. */
440 bgp_advertise_unintern(subgrp->hash, baa);
441 } else
442 fhead = &subgrp->sync->withdraw;
443
444
445 /* Unlink myself from advertisement FIFO. */
446 bgp_adv_fifo_del(fhead, adv);
447
448 /* Free memory. */
449 bgp_advertise_free(adj->adv);
450 adj->adv = NULL;
451
452 return next;
453 }
454
455 void bgp_adj_out_set_subgroup(struct bgp_dest *dest,
456 struct update_subgroup *subgrp, struct attr *attr,
457 struct bgp_path_info *path)
458 {
459 struct bgp_adj_out *adj = NULL;
460 struct bgp_advertise *adv;
461 struct peer *peer;
462 afi_t afi;
463 safi_t safi;
464 struct peer *adv_peer;
465 struct peer_af *paf;
466 struct bgp *bgp;
467 uint32_t attr_hash = attrhash_key_make(attr);
468
469 peer = SUBGRP_PEER(subgrp);
470 afi = SUBGRP_AFI(subgrp);
471 safi = SUBGRP_SAFI(subgrp);
472 bgp = SUBGRP_INST(subgrp);
473
474 if (DISABLE_BGP_ANNOUNCE)
475 return;
476
477 /* Look for adjacency information. */
478 adj = adj_lookup(
479 dest, subgrp,
480 bgp_addpath_id_for_peer(peer, afi, safi, &path->tx_addpath));
481
482 if (adj) {
483 if (CHECK_FLAG(subgrp->sflags, SUBGRP_STATUS_TABLE_REPARSING))
484 subgrp->pscount++;
485 } else {
486 adj = bgp_adj_out_alloc(
487 subgrp, dest,
488 bgp_addpath_id_for_peer(peer, afi, safi,
489 &path->tx_addpath));
490 if (!adj)
491 return;
492
493 subgrp->pscount++;
494 }
495
496 /* Check if we are sending the same route. This is needed to
497 * avoid duplicate UPDATES. For instance, filtering communities
498 * at egress, neighbors will see duplicate UPDATES despite
499 * the route wasn't changed actually.
500 * Do not suppress BGP UPDATES for route-refresh.
501 */
502 if (CHECK_FLAG(bgp->flags, BGP_FLAG_SUPPRESS_DUPLICATES)
503 && !CHECK_FLAG(subgrp->sflags, SUBGRP_STATUS_FORCE_UPDATES)
504 && adj->attr_hash == attr_hash) {
505 if (BGP_DEBUG(update, UPDATE_OUT)) {
506 char attr_str[BUFSIZ] = {0};
507
508 bgp_dump_attr(attr, attr_str, sizeof(attr_str));
509
510 zlog_debug("%s suppress UPDATE w/ attr: %s", peer->host,
511 attr_str);
512 }
513 return;
514 }
515
516 if (adj->adv)
517 bgp_advertise_clean_subgroup(subgrp, adj);
518 adj->adv = bgp_advertise_new();
519
520 adv = adj->adv;
521 adv->dest = dest;
522 assert(adv->pathi == NULL);
523 /* bgp_path_info adj_out reference */
524 adv->pathi = bgp_path_info_lock(path);
525
526 adv->baa = bgp_advertise_intern(subgrp->hash, attr);
527 adv->adj = adj;
528 adj->attr_hash = attr_hash;
529
530 /* Add new advertisement to advertisement attribute list. */
531 bgp_advertise_add(adv->baa, adv);
532
533 /*
534 * If the update adv list is empty, trigger the member peers'
535 * mrai timers so the socket writes can happen.
536 */
537 if (!bgp_adv_fifo_count(&subgrp->sync->update)) {
538 SUBGRP_FOREACH_PEER (subgrp, paf) {
539 /* If there are no routes in the withdraw list, set
540 * the flag PEER_STATUS_ADV_DELAY which will allow
541 * more routes to be sent in the update message
542 */
543 if (BGP_SUPPRESS_FIB_ENABLED(bgp)) {
544 adv_peer = PAF_PEER(paf);
545 if (!bgp_adv_fifo_count(
546 &subgrp->sync->withdraw))
547 SET_FLAG(adv_peer->thread_flags,
548 PEER_THREAD_SUBGRP_ADV_DELAY);
549 else
550 UNSET_FLAG(adv_peer->thread_flags,
551 PEER_THREAD_SUBGRP_ADV_DELAY);
552 }
553 bgp_adjust_routeadv(PAF_PEER(paf));
554 }
555 }
556
557 bgp_adv_fifo_add_tail(&subgrp->sync->update, adv);
558
559 subgrp->version = MAX(subgrp->version, dest->version);
560 }
561
562 /* The only time 'withdraw' will be false is if we are sending
563 * the "neighbor x.x.x.x default-originate" default and need to clear
564 * bgp_adj_out for the 0.0.0.0/0 route in the BGP table.
565 */
566 void bgp_adj_out_unset_subgroup(struct bgp_dest *dest,
567 struct update_subgroup *subgrp, char withdraw,
568 uint32_t addpath_tx_id)
569 {
570 struct bgp_adj_out *adj;
571 struct bgp_advertise *adv;
572 bool trigger_write;
573
574 if (DISABLE_BGP_ANNOUNCE)
575 return;
576
577 /* Lookup existing adjacency */
578 adj = adj_lookup(dest, subgrp, addpath_tx_id);
579 if (adj != NULL) {
580 /* Clean up previous advertisement. */
581 if (adj->adv)
582 bgp_advertise_clean_subgroup(subgrp, adj);
583
584 /* If default originate is enabled and the route is default
585 * route, do not send withdraw. This will prevent deletion of
586 * the default route at the peer.
587 */
588 if (CHECK_FLAG(subgrp->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE)
589 && is_default_prefix(bgp_dest_get_prefix(dest)))
590 return;
591
592 if (adj->attr && withdraw) {
593 /* We need advertisement structure. */
594 adj->adv = bgp_advertise_new();
595 adv = adj->adv;
596 adv->dest = dest;
597 adv->adj = adj;
598
599 /* Note if we need to trigger a packet write */
600 trigger_write =
601 !bgp_adv_fifo_count(&subgrp->sync->withdraw);
602
603 /* Add to synchronization entry for withdraw
604 * announcement. */
605 bgp_adv_fifo_add_tail(&subgrp->sync->withdraw, adv);
606
607 if (trigger_write)
608 subgroup_trigger_write(subgrp);
609 } else {
610 /* Free allocated information. */
611 adj_free(adj);
612 }
613 if (!CHECK_FLAG(subgrp->sflags, SUBGRP_STATUS_TABLE_REPARSING))
614 subgrp->pscount--;
615 }
616
617 subgrp->version = MAX(subgrp->version, dest->version);
618 }
619
620 void bgp_adj_out_remove_subgroup(struct bgp_dest *dest, struct bgp_adj_out *adj,
621 struct update_subgroup *subgrp)
622 {
623 if (adj->attr)
624 bgp_attr_unintern(&adj->attr);
625
626 if (adj->adv)
627 bgp_advertise_clean_subgroup(subgrp, adj);
628
629 adj_free(adj);
630 }
631
632 /*
633 * Go through all the routes and clean up the adj/adv structures corresponding
634 * to the subgroup.
635 */
636 void subgroup_clear_table(struct update_subgroup *subgrp)
637 {
638 struct bgp_adj_out *aout, *taout;
639
640 SUBGRP_FOREACH_ADJ_SAFE (subgrp, aout, taout)
641 bgp_adj_out_remove_subgroup(aout->dest, aout, subgrp);
642 }
643
644 /*
645 * subgroup_announce_table
646 */
647 void subgroup_announce_table(struct update_subgroup *subgrp,
648 struct bgp_table *table)
649 {
650 struct bgp_dest *dest;
651 struct bgp_path_info *ri;
652 struct attr attr;
653 struct peer *peer;
654 afi_t afi;
655 safi_t safi;
656 bool addpath_capable;
657 struct bgp *bgp;
658 bool advertise;
659
660 peer = SUBGRP_PEER(subgrp);
661 afi = SUBGRP_AFI(subgrp);
662 safi = SUBGRP_SAFI(subgrp);
663 bgp = SUBGRP_INST(subgrp);
664 addpath_capable = bgp_addpath_encode_tx(peer, afi, safi);
665
666 if (safi == SAFI_LABELED_UNICAST)
667 safi = SAFI_UNICAST;
668
669 if (!table)
670 table = peer->bgp->rib[afi][safi];
671
672 if (safi != SAFI_MPLS_VPN && safi != SAFI_ENCAP && safi != SAFI_EVPN
673 && CHECK_FLAG(peer->af_flags[afi][safi],
674 PEER_FLAG_DEFAULT_ORIGINATE))
675 subgroup_default_originate(subgrp, 0);
676
677 subgrp->pscount = 0;
678 SET_FLAG(subgrp->sflags, SUBGRP_STATUS_TABLE_REPARSING);
679
680 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
681 const struct prefix *dest_p = bgp_dest_get_prefix(dest);
682
683 /* Check if the route can be advertised */
684 advertise = bgp_check_advertise(bgp, dest);
685
686 for (ri = bgp_dest_get_bgp_path_info(dest); ri; ri = ri->next)
687
688 if (bgp_check_selected(ri, peer, addpath_capable, afi,
689 safi)) {
690 if (subgroup_announce_check(dest, ri, subgrp,
691 dest_p, &attr,
692 NULL)) {
693 /* Check if route can be advertised */
694 if (advertise) {
695 if (!bgp_check_withdrawal(bgp,
696 dest))
697 bgp_adj_out_set_subgroup(
698 dest, subgrp,
699 &attr, ri);
700 else
701 bgp_adj_out_unset_subgroup(
702 dest, subgrp, 1,
703 bgp_addpath_id_for_peer(
704 peer,
705 afi,
706 safi,
707 &ri->tx_addpath));
708 }
709 } else {
710 /* If default originate is enabled for
711 * the peer, do not send explicit
712 * withdraw. This will prevent deletion
713 * of default route advertised through
714 * default originate
715 */
716 if (CHECK_FLAG(
717 peer->af_flags[afi][safi],
718 PEER_FLAG_DEFAULT_ORIGINATE)
719 && is_default_prefix(bgp_dest_get_prefix(dest)))
720 break;
721
722 bgp_adj_out_unset_subgroup(
723 dest, subgrp, 1,
724 bgp_addpath_id_for_peer(
725 peer, afi, safi,
726 &ri->tx_addpath));
727 }
728 }
729 }
730 UNSET_FLAG(subgrp->sflags, SUBGRP_STATUS_TABLE_REPARSING);
731
732 /*
733 * We walked through the whole table -- make sure our version number
734 * is consistent with the one on the table. This should allow
735 * subgroups to merge sooner if a peer comes up when the route node
736 * with the largest version is no longer in the table. This also
737 * covers the pathological case where all routes in the table have
738 * now been deleted.
739 */
740 subgrp->version = MAX(subgrp->version, table->version);
741
742 /*
743 * Start a task to merge the subgroup if necessary.
744 */
745 update_subgroup_trigger_merge_check(subgrp, 0);
746 }
747
748 /*
749 * subgroup_announce_route
750 *
751 * Refresh all routes out to a subgroup.
752 */
753 void subgroup_announce_route(struct update_subgroup *subgrp)
754 {
755 struct bgp_dest *dest;
756 struct bgp_table *table;
757 struct peer *onlypeer;
758
759 if (update_subgroup_needs_refresh(subgrp)) {
760 update_subgroup_set_needs_refresh(subgrp, 0);
761 }
762
763 /*
764 * First update is deferred until ORF or ROUTE-REFRESH is received
765 */
766 onlypeer = ((SUBGRP_PCOUNT(subgrp) == 1) ? (SUBGRP_PFIRST(subgrp))->peer
767 : NULL);
768 if (onlypeer && CHECK_FLAG(onlypeer->af_sflags[SUBGRP_AFI(subgrp)]
769 [SUBGRP_SAFI(subgrp)],
770 PEER_STATUS_ORF_WAIT_REFRESH))
771 return;
772
773 if (SUBGRP_SAFI(subgrp) != SAFI_MPLS_VPN
774 && SUBGRP_SAFI(subgrp) != SAFI_ENCAP
775 && SUBGRP_SAFI(subgrp) != SAFI_EVPN)
776 subgroup_announce_table(subgrp, NULL);
777 else
778 for (dest = bgp_table_top(update_subgroup_rib(subgrp)); dest;
779 dest = bgp_route_next(dest)) {
780 table = bgp_dest_get_bgp_table_info(dest);
781 if (!table)
782 continue;
783 subgroup_announce_table(subgrp, table);
784 }
785 }
786
787 void subgroup_default_originate(struct update_subgroup *subgrp, int withdraw)
788 {
789 struct bgp *bgp;
790 struct attr attr;
791 struct attr *new_attr = &attr;
792 struct prefix p;
793 struct peer *from;
794 struct bgp_dest *dest;
795 struct bgp_path_info *pi;
796 struct peer *peer;
797 struct bgp_adj_out *adj;
798 route_map_result_t ret = RMAP_DENYMATCH;
799 afi_t afi;
800 safi_t safi;
801
802 if (!subgrp)
803 return;
804
805 peer = SUBGRP_PEER(subgrp);
806 afi = SUBGRP_AFI(subgrp);
807 safi = SUBGRP_SAFI(subgrp);
808
809 if (!(afi == AFI_IP || afi == AFI_IP6))
810 return;
811
812 bgp = peer->bgp;
813 from = bgp->peer_self;
814
815 bgp_attr_default_set(&attr, BGP_ORIGIN_IGP);
816
817 /* make coverity happy */
818 assert(attr.aspath);
819
820 attr.local_pref = bgp->default_local_pref;
821
822 if ((afi == AFI_IP6) || peer_cap_enhe(peer, afi, safi)) {
823 /* IPv6 global nexthop must be included. */
824 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV6_GLOBAL;
825
826 /* If the peer is on shared nextwork and we have link-local
827 nexthop set it. */
828 if (peer->shared_network
829 && !IN6_IS_ADDR_UNSPECIFIED(&peer->nexthop.v6_local))
830 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL;
831 }
832
833 if (peer->default_rmap[afi][safi].name) {
834 struct bgp_path_info tmp_pi = {0};
835
836 tmp_pi.peer = bgp->peer_self;
837
838 SET_FLAG(bgp->peer_self->rmap_type, PEER_RMAP_TYPE_DEFAULT);
839
840 /* Iterate over the RIB to see if we can announce
841 * the default route. We announce the default
842 * route only if route-map has a match.
843 */
844 for (dest = bgp_table_top(bgp->rib[afi][safi]); dest;
845 dest = bgp_route_next(dest)) {
846 if (!bgp_dest_has_bgp_path_info_data(dest))
847 continue;
848
849 for (pi = bgp_dest_get_bgp_path_info(dest); pi;
850 pi = pi->next) {
851 struct attr tmp_attr = attr;
852
853 tmp_pi.attr = &tmp_attr;
854
855 ret = route_map_apply_ext(
856 peer->default_rmap[afi][safi].map,
857 bgp_dest_get_prefix(dest), pi, &tmp_pi);
858
859 if (ret == RMAP_DENYMATCH) {
860 bgp_attr_flush(&tmp_attr);
861 continue;
862 } else {
863 new_attr = bgp_attr_intern(&tmp_attr);
864
865 subgroup_announce_reset_nhop(
866 (peer_cap_enhe(peer, afi, safi)
867 ? AF_INET6
868 : AF_INET),
869 new_attr);
870
871 break;
872 }
873 }
874 if (ret == RMAP_PERMITMATCH) {
875 bgp_dest_unlock_node(dest);
876 break;
877 }
878 }
879 bgp->peer_self->rmap_type = 0;
880
881 if (ret == RMAP_DENYMATCH)
882 withdraw = 1;
883 }
884
885 /* Check if the default route is in local BGP RIB which is
886 * installed through redistribute or network command
887 */
888 memset(&p, 0, sizeof(p));
889 p.family = afi2family(afi);
890 p.prefixlen = 0;
891 dest = bgp_afi_node_lookup(bgp->rib[afi][safi], afi, safi, &p, NULL);
892
893 if (withdraw) {
894 /* Withdraw the default route advertised using default
895 * originate
896 */
897 if (CHECK_FLAG(subgrp->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE))
898 subgroup_default_withdraw_packet(subgrp);
899 UNSET_FLAG(subgrp->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE);
900
901 /* If default route is present in the local RIB, advertise the
902 * route
903 */
904 if (dest) {
905 for (pi = bgp_dest_get_bgp_path_info(dest); pi;
906 pi = pi->next) {
907 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED))
908 if (subgroup_announce_check(
909 dest, pi, subgrp,
910 bgp_dest_get_prefix(dest),
911 &attr, NULL))
912 bgp_adj_out_set_subgroup(
913 dest, subgrp, &attr,
914 pi);
915 }
916 bgp_dest_unlock_node(dest);
917 }
918 } else {
919 if (!CHECK_FLAG(subgrp->sflags,
920 SUBGRP_STATUS_DEFAULT_ORIGINATE)) {
921
922 /* The 'neighbor x.x.x.x default-originate' default will
923 * act as an
924 * implicit withdraw for any previous UPDATEs sent for
925 * 0.0.0.0/0 so
926 * clear adj_out for the 0.0.0.0/0 prefix in the BGP
927 * table.
928 */
929 if (dest) {
930 /* Remove the adjacency for the previously
931 * advertised default route
932 */
933 adj = adj_lookup(
934 dest, subgrp,
935 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE);
936 if (adj != NULL) {
937 /* Clean up previous advertisement. */
938 if (adj->adv)
939 bgp_advertise_clean_subgroup(
940 subgrp, adj);
941
942 /* Free allocated information. */
943 adj_free(adj);
944 }
945 bgp_dest_unlock_node(dest);
946 }
947
948 /* Advertise the default route */
949 if (bgp_in_graceful_shutdown(bgp))
950 bgp_attr_add_gshut_community(new_attr);
951
952 SET_FLAG(subgrp->sflags,
953 SUBGRP_STATUS_DEFAULT_ORIGINATE);
954 subgroup_default_update_packet(subgrp, new_attr, from);
955 }
956 }
957
958 aspath_unintern(&attr.aspath);
959 }
960
961 /*
962 * Announce the BGP table to a subgroup.
963 *
964 * At startup, we try to optimize route announcement by coalescing the
965 * peer-up events. This is done only the first time - from then on,
966 * subgrp->v_coalesce will be set to zero and the normal logic
967 * prevails.
968 */
969 void subgroup_announce_all(struct update_subgroup *subgrp)
970 {
971 if (!subgrp)
972 return;
973
974 /*
975 * If coalesce timer value is not set, announce routes immediately.
976 */
977 if (!subgrp->v_coalesce) {
978 if (bgp_debug_update(NULL, NULL, subgrp->update_group, 0))
979 zlog_debug("u%" PRIu64 ":s%" PRIu64" announcing all routes",
980 subgrp->update_group->id, subgrp->id);
981 subgroup_announce_route(subgrp);
982 return;
983 }
984
985 /*
986 * We should wait for the coalesce timer. Arm the timer if not done.
987 */
988 if (!subgrp->t_coalesce) {
989 thread_add_timer_msec(bm->master, subgroup_coalesce_timer,
990 subgrp, subgrp->v_coalesce,
991 &subgrp->t_coalesce);
992 }
993 }
994
995 /*
996 * Go through all update subgroups and set up the adv queue for the
997 * input route.
998 */
999 void group_announce_route(struct bgp *bgp, afi_t afi, safi_t safi,
1000 struct bgp_dest *dest, struct bgp_path_info *pi)
1001 {
1002 struct updwalk_context ctx;
1003 ctx.pi = pi;
1004 ctx.dest = dest;
1005
1006 /* If suppress fib is enabled, the route will be advertised when
1007 * FIB status is received
1008 */
1009 if (!bgp_check_advertise(bgp, dest))
1010 return;
1011
1012 update_group_af_walk(bgp, afi, safi, group_announce_route_walkcb, &ctx);
1013 }
1014
1015 void update_group_show_adj_queue(struct bgp *bgp, afi_t afi, safi_t safi,
1016 struct vty *vty, uint64_t id)
1017 {
1018 updgrp_show_adj(bgp, afi, safi, vty, id, UPDWALK_FLAGS_ADVQUEUE);
1019 }
1020
1021 void update_group_show_advertised(struct bgp *bgp, afi_t afi, safi_t safi,
1022 struct vty *vty, uint64_t id)
1023 {
1024 updgrp_show_adj(bgp, afi, safi, vty, id, UPDWALK_FLAGS_ADVERTISED);
1025 }
1026
1027 void update_group_announce(struct bgp *bgp)
1028 {
1029 update_group_walk(bgp, update_group_announce_walkcb, NULL);
1030 }
1031
1032 void update_group_announce_rrclients(struct bgp *bgp)
1033 {
1034 update_group_walk(bgp, update_group_announce_rrc_walkcb, NULL);
1035 }