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