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