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