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