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