]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_updgrp_adv.c
Merge pull request #12460 from opensourcerouting/msdp-fixes
[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"
dcc68b5e 52#include "bgpd/bgp_addpath.h"
3f9c7369
DS
53
54
55/********************
56 * PRIVATE FUNCTIONS
57 ********************/
a79c04e7
DS
58static int bgp_adj_out_compare(const struct bgp_adj_out *o1,
59 const struct bgp_adj_out *o2)
60{
61 if (o1->subgroup < o2->subgroup)
62 return -1;
63
64 if (o1->subgroup > o2->subgroup)
65 return 1;
66
3373d7e7
MS
67 if (o1->addpath_tx_id < o2->addpath_tx_id)
68 return -1;
69
70 if (o1->addpath_tx_id > o2->addpath_tx_id)
71 return 1;
72
a79c04e7
DS
73 return 0;
74}
75RB_GENERATE(bgp_adj_out_rb, bgp_adj_out, adj_entry, bgp_adj_out_compare);
3f9c7369 76
9bcb3eef 77static inline struct bgp_adj_out *adj_lookup(struct bgp_dest *dest,
d62a17ae 78 struct update_subgroup *subgrp,
d7c0a89a 79 uint32_t addpath_tx_id)
3f9c7369 80{
3373d7e7 81 struct bgp_adj_out lookup;
d62a17ae 82
9bcb3eef 83 if (!dest || !subgrp)
d62a17ae 84 return NULL;
85
d62a17ae 86 /* update-groups that do not support addpath will pass 0 for
3373d7e7 87 * addpath_tx_id. */
a79c04e7 88 lookup.subgroup = subgrp;
3373d7e7
MS
89 lookup.addpath_tx_id = addpath_tx_id;
90
9bcb3eef 91 return RB_FIND(bgp_adj_out_rb, &dest->adj_out, &lookup);
3f9c7369
DS
92}
93
d62a17ae 94static void adj_free(struct bgp_adj_out *adj)
3f9c7369 95{
d62a17ae 96 TAILQ_REMOVE(&(adj->subgroup->adjq), adj, subgrp_adj_train);
97 SUBGRP_DECR_STAT(adj->subgroup, adj_count);
9669fbde
DS
98
99 RB_REMOVE(bgp_adj_out_rb, &adj->dest->adj_out, adj);
100 bgp_dest_unlock_node(adj->dest);
101
d62a17ae 102 XFREE(MTYPE_BGP_ADJ_OUT, adj);
3f9c7369
DS
103}
104
dcc68b5e
MS
105static void subgrp_withdraw_stale_addpath(struct updwalk_context *ctx,
106 struct update_subgroup *subgrp)
107{
108 struct bgp_adj_out *adj, *adj_next;
109 uint32_t id;
110 struct bgp_path_info *pi;
111 afi_t afi = SUBGRP_AFI(subgrp);
112 safi_t safi = SUBGRP_SAFI(subgrp);
113 struct peer *peer = SUBGRP_PEER(subgrp);
114
115 /* Look through all of the paths we have advertised for this rn and send
116 * a withdraw for the ones that are no longer present */
9bcb3eef 117 RB_FOREACH_SAFE (adj, bgp_adj_out_rb, &ctx->dest->adj_out, adj_next) {
7622259e
DS
118 if (adj->subgroup != subgrp)
119 continue;
dcc68b5e 120
7622259e
DS
121 for (pi = bgp_dest_get_bgp_path_info(ctx->dest); pi;
122 pi = pi->next) {
123 id = bgp_addpath_id_for_peer(peer, afi, safi,
124 &pi->tx_addpath);
dcc68b5e 125
7622259e
DS
126 if (id == adj->addpath_tx_id) {
127 break;
dcc68b5e 128 }
7622259e 129 }
dcc68b5e 130
7622259e
DS
131 if (!pi) {
132 subgroup_process_announce_selected(
133 subgrp, NULL, ctx->dest, adj->addpath_tx_id);
dcc68b5e
MS
134 }
135 }
136}
137
d62a17ae 138static int group_announce_route_walkcb(struct update_group *updgrp, void *arg)
3f9c7369 139{
d62a17ae 140 struct updwalk_context *ctx = arg;
141 struct update_subgroup *subgrp;
40381db7 142 struct bgp_path_info *pi;
d62a17ae 143 afi_t afi;
144 safi_t safi;
145 struct peer *peer;
146 struct bgp_adj_out *adj, *adj_next;
be92fc9f 147 bool addpath_capable;
d62a17ae 148
149 afi = UPDGRP_AFI(updgrp);
150 safi = UPDGRP_SAFI(updgrp);
151 peer = UPDGRP_PEER(updgrp);
152 addpath_capable = bgp_addpath_encode_tx(peer, afi, safi);
153
b54892e0
DS
154 if (BGP_DEBUG(update, UPDATE_OUT))
155 zlog_debug("%s: afi=%s, safi=%s, p=%pRN", __func__,
9bcb3eef
DS
156 afi2str(afi), safi2str(safi),
157 bgp_dest_to_rnode(ctx->dest));
e0207895 158
a2addae8 159 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
3f9c7369 160
d62a17ae 161 /*
162 * Skip the subgroups that have coalesce timer running. We will
163 * walk the entire prefix table for those subgroups when the
164 * coalesce timer fires.
165 */
166 if (!subgrp->t_coalesce) {
7622259e 167
d62a17ae 168 /* An update-group that uses addpath */
169 if (addpath_capable) {
dcc68b5e 170 subgrp_withdraw_stale_addpath(ctx, subgrp);
d62a17ae 171
9bcb3eef 172 for (pi = bgp_dest_get_bgp_path_info(ctx->dest);
6f94b685 173 pi; pi = pi->next) {
d62a17ae 174 /* Skip the bestpath for now */
40381db7 175 if (pi == ctx->pi)
d62a17ae 176 continue;
177
178 subgroup_process_announce_selected(
9bcb3eef 179 subgrp, pi, ctx->dest,
dcc68b5e
MS
180 bgp_addpath_id_for_peer(
181 peer, afi, safi,
182 &pi->tx_addpath));
d62a17ae 183 }
184
185 /* Process the bestpath last so the "show [ip]
186 * bgp neighbor x.x.x.x advertised"
187 * output shows the attributes from the bestpath
188 */
40381db7 189 if (ctx->pi)
d62a17ae 190 subgroup_process_announce_selected(
9bcb3eef 191 subgrp, ctx->pi, ctx->dest,
dcc68b5e
MS
192 bgp_addpath_id_for_peer(
193 peer, afi, safi,
194 &ctx->pi->tx_addpath));
d62a17ae 195 }
d62a17ae 196 /* An update-group that does not use addpath */
197 else {
40381db7 198 if (ctx->pi) {
d62a17ae 199 subgroup_process_announce_selected(
9bcb3eef 200 subgrp, ctx->pi, ctx->dest,
dcc68b5e
MS
201 bgp_addpath_id_for_peer(
202 peer, afi, safi,
203 &ctx->pi->tx_addpath));
d62a17ae 204 } else {
205 /* Find the addpath_tx_id of the path we
206 * had advertised and
207 * send a withdraw */
a79c04e7 208 RB_FOREACH_SAFE (adj, bgp_adj_out_rb,
9bcb3eef 209 &ctx->dest->adj_out,
a79c04e7 210 adj_next) {
d62a17ae 211 if (adj->subgroup == subgrp) {
212 subgroup_process_announce_selected(
213 subgrp, NULL,
9bcb3eef 214 ctx->dest,
d62a17ae 215 adj->addpath_tx_id);
216 }
217 }
218 }
219 }
220 }
e73c112e
MK
221
222 /* Notify BGP Conditional advertisement */
223 bgp_notify_conditional_adv_scanner(subgrp);
d62a17ae 224 }
3f9c7369 225
d62a17ae 226 return UPDWALK_CONTINUE;
227}
3f9c7369 228
d62a17ae 229static void subgrp_show_adjq_vty(struct update_subgroup *subgrp,
d7c0a89a 230 struct vty *vty, uint8_t flags)
d62a17ae 231{
232 struct bgp_table *table;
233 struct bgp_adj_out *adj;
234 unsigned long output_count;
9bcb3eef 235 struct bgp_dest *dest;
d62a17ae 236 int header1 = 1;
237 struct bgp *bgp;
238 int header2 = 1;
239
240 bgp = SUBGRP_INST(subgrp);
241 if (!bgp)
242 return;
243
244 table = bgp->rib[SUBGRP_AFI(subgrp)][SUBGRP_SAFI(subgrp)];
245
246 output_count = 0;
247
9bcb3eef
DS
248 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
249 const struct prefix *dest_p = bgp_dest_get_prefix(dest);
b54892e0 250
7622259e
DS
251 RB_FOREACH (adj, bgp_adj_out_rb, &dest->adj_out) {
252 if (adj->subgroup != subgrp)
253 continue;
254
255 if (header1) {
256 vty_out(vty,
257 "BGP table version is %" PRIu64
258 ", local router ID is %pI4\n",
259 table->version, &bgp->router_id);
260 vty_out(vty, BGP_SHOW_SCODE_HEADER);
261 vty_out(vty, BGP_SHOW_OCODE_HEADER);
262 header1 = 0;
263 }
264 if (header2) {
265 vty_out(vty, BGP_SHOW_HEADER);
266 header2 = 0;
d62a17ae 267 }
7622259e
DS
268 if ((flags & UPDWALK_FLAGS_ADVQUEUE) && adj->adv &&
269 adj->adv->baa) {
270 route_vty_out_tmp(
271 vty, dest, dest_p, adj->adv->baa->attr,
272 SUBGRP_SAFI(subgrp), 0, NULL, false);
273 output_count++;
274 }
275 if ((flags & UPDWALK_FLAGS_ADVERTISED) && adj->attr) {
276 route_vty_out_tmp(vty, dest, dest_p, adj->attr,
277 SUBGRP_SAFI(subgrp), 0, NULL,
278 false);
279 output_count++;
280 }
281 }
b54892e0 282 }
d62a17ae 283 if (output_count != 0)
284 vty_out(vty, "\nTotal number of prefixes %ld\n", output_count);
3f9c7369
DS
285}
286
d62a17ae 287static int updgrp_show_adj_walkcb(struct update_group *updgrp, void *arg)
3f9c7369 288{
d62a17ae 289 struct updwalk_context *ctx = arg;
290 struct update_subgroup *subgrp;
291 struct vty *vty;
292
293 vty = ctx->vty;
a2addae8 294 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
d62a17ae 295 if (ctx->subgrp_id && (ctx->subgrp_id != subgrp->id))
296 continue;
297 vty_out(vty, "update group %" PRIu64 ", subgroup %" PRIu64 "\n",
298 updgrp->id, subgrp->id);
299 subgrp_show_adjq_vty(subgrp, vty, ctx->flags);
300 }
301 return UPDWALK_CONTINUE;
3f9c7369
DS
302}
303
d62a17ae 304static void updgrp_show_adj(struct bgp *bgp, afi_t afi, safi_t safi,
d7c0a89a 305 struct vty *vty, uint64_t id, uint8_t flags)
3f9c7369 306{
d62a17ae 307 struct updwalk_context ctx;
308 memset(&ctx, 0, sizeof(ctx));
309 ctx.vty = vty;
310 ctx.subgrp_id = id;
311 ctx.flags = flags;
3f9c7369 312
d62a17ae 313 update_group_af_walk(bgp, afi, safi, updgrp_show_adj_walkcb, &ctx);
3f9c7369
DS
314}
315
cc9f21da 316static void subgroup_coalesce_timer(struct thread *thread)
3f9c7369 317{
d62a17ae 318 struct update_subgroup *subgrp;
a77e2f4b 319 struct bgp *bgp;
d62a17ae 320
321 subgrp = THREAD_ARG(thread);
322 if (bgp_debug_update(NULL, NULL, subgrp->update_group, 0))
6cde4b45 323 zlog_debug("u%" PRIu64 ":s%" PRIu64" announcing routes upon coalesce timer expiry(%u ms)",
5b18ef82 324 (SUBGRP_UPDGRP(subgrp))->id, subgrp->id,
80833111 325 subgrp->v_coalesce);
d62a17ae 326 subgrp->t_coalesce = NULL;
327 subgrp->v_coalesce = 0;
a77e2f4b 328 bgp = SUBGRP_INST(subgrp);
d62a17ae 329 subgroup_announce_route(subgrp);
330
331
332 /* While the announce_route() may kick off the route advertisement timer
333 * for
334 * the members of the subgroup, we'd like to send the initial updates
335 * much
336 * faster (i.e., without enforcing MRAI). Also, if there were no routes
337 * to
338 * announce, this is the method currently employed to trigger the EOR.
339 */
a77e2f4b
S
340 if (!bgp_update_delay_active(SUBGRP_INST(subgrp)) &&
341 !(BGP_SUPPRESS_FIB_ENABLED(bgp))) {
d62a17ae 342 struct peer_af *paf;
343 struct peer *peer;
344
a2addae8 345 SUBGRP_FOREACH_PEER (subgrp, paf) {
d62a17ae 346 peer = PAF_PEER(paf);
fa5806c3 347 THREAD_OFF(peer->t_routeadv);
d62a17ae 348 BGP_TIMER_ON(peer->t_routeadv, bgp_routeadv_timer, 0);
349 }
350 }
3f9c7369
DS
351}
352
d62a17ae 353static int update_group_announce_walkcb(struct update_group *updgrp, void *arg)
3f9c7369 354{
d62a17ae 355 struct update_subgroup *subgrp;
3f9c7369 356
a2addae8 357 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
05ab8ced
AC
358 /* Avoid supressing duplicate routes later
359 * when processing in subgroup_announce_table().
360 */
361 SET_FLAG(subgrp->sflags, SUBGRP_STATUS_FORCE_UPDATES);
362
d62a17ae 363 subgroup_announce_all(subgrp);
364 }
3f9c7369 365
d62a17ae 366 return UPDWALK_CONTINUE;
3f9c7369
DS
367}
368
d62a17ae 369static int update_group_announce_rrc_walkcb(struct update_group *updgrp,
370 void *arg)
3f9c7369 371{
d62a17ae 372 struct update_subgroup *subgrp;
373 afi_t afi;
374 safi_t safi;
375 struct peer *peer;
376
377 afi = UPDGRP_AFI(updgrp);
378 safi = UPDGRP_SAFI(updgrp);
379 peer = UPDGRP_PEER(updgrp);
380
381 /* Only announce if this is a group of route-reflector-clients */
382 if (CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_REFLECTOR_CLIENT)) {
a2addae8 383 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
d62a17ae 384 subgroup_announce_all(subgrp);
385 }
386 }
387
388 return UPDWALK_CONTINUE;
3f9c7369
DS
389}
390
391/********************
392 * PUBLIC FUNCTIONS
393 ********************/
394
395/**
396 * Allocate an adj-out object. Do proper initialization of its fields,
397 * primarily its association with the subgroup and the prefix.
398 */
d62a17ae 399struct bgp_adj_out *bgp_adj_out_alloc(struct update_subgroup *subgrp,
9bcb3eef 400 struct bgp_dest *dest,
d7c0a89a 401 uint32_t addpath_tx_id)
3f9c7369 402{
d62a17ae 403 struct bgp_adj_out *adj;
404
405 adj = XCALLOC(MTYPE_BGP_ADJ_OUT, sizeof(struct bgp_adj_out));
406 adj->subgroup = subgrp;
3373d7e7
MS
407 adj->addpath_tx_id = addpath_tx_id;
408
9669fbde
DS
409 RB_INSERT(bgp_adj_out_rb, &dest->adj_out, adj);
410 bgp_dest_lock_node(dest);
411 adj->dest = dest;
d62a17ae 412
d62a17ae 413 TAILQ_INSERT_TAIL(&(subgrp->adjq), adj, subgrp_adj_train);
414 SUBGRP_INCR_STAT(subgrp, adj_count);
415 return adj;
3f9c7369
DS
416}
417
418
419struct bgp_advertise *
d62a17ae 420bgp_advertise_clean_subgroup(struct update_subgroup *subgrp,
421 struct bgp_adj_out *adj)
3f9c7369 422{
d62a17ae 423 struct bgp_advertise *adv;
424 struct bgp_advertise_attr *baa;
425 struct bgp_advertise *next;
a274fef8 426 struct bgp_adv_fifo_head *fhead;
3f9c7369 427
d62a17ae 428 adv = adj->adv;
429 baa = adv->baa;
430 next = NULL;
3f9c7369 431
d62a17ae 432 if (baa) {
433 fhead = &subgrp->sync->update;
3f9c7369 434
d62a17ae 435 /* Unlink myself from advertise attribute FIFO. */
436 bgp_advertise_delete(baa, adv);
3f9c7369 437
d62a17ae 438 /* Fetch next advertise candidate. */
439 next = baa->adv;
3f9c7369 440
d62a17ae 441 /* Unintern BGP advertise attribute. */
4d28080c 442 bgp_advertise_attr_unintern(subgrp->hash, baa);
d62a17ae 443 } else
444 fhead = &subgrp->sync->withdraw;
3f9c7369
DS
445
446
d62a17ae 447 /* Unlink myself from advertisement FIFO. */
a274fef8 448 bgp_adv_fifo_del(fhead, adv);
3f9c7369 449
d62a17ae 450 /* Free memory. */
451 bgp_advertise_free(adj->adv);
452 adj->adv = NULL;
3f9c7369 453
d62a17ae 454 return next;
3f9c7369
DS
455}
456
9bcb3eef 457void bgp_adj_out_set_subgroup(struct bgp_dest *dest,
d62a17ae 458 struct update_subgroup *subgrp, struct attr *attr,
9b6d8fcf 459 struct bgp_path_info *path)
3f9c7369 460{
d62a17ae 461 struct bgp_adj_out *adj = NULL;
462 struct bgp_advertise *adv;
dcc68b5e
MS
463 struct peer *peer;
464 afi_t afi;
465 safi_t safi;
a77e2f4b
S
466 struct peer *adv_peer;
467 struct peer_af *paf;
468 struct bgp *bgp;
2adac256 469 uint32_t attr_hash = attrhash_key_make(attr);
dcc68b5e
MS
470
471 peer = SUBGRP_PEER(subgrp);
472 afi = SUBGRP_AFI(subgrp);
473 safi = SUBGRP_SAFI(subgrp);
a77e2f4b 474 bgp = SUBGRP_INST(subgrp);
d62a17ae 475
476 if (DISABLE_BGP_ANNOUNCE)
477 return;
478
479 /* Look for adjacency information. */
dcc68b5e 480 adj = adj_lookup(
9bcb3eef 481 dest, subgrp,
dcc68b5e 482 bgp_addpath_id_for_peer(peer, afi, safi, &path->tx_addpath));
d62a17ae 483
d0bf49ec
LS
484 if (adj) {
485 if (CHECK_FLAG(subgrp->sflags, SUBGRP_STATUS_TABLE_REPARSING))
486 subgrp->pscount++;
487 } else {
dcc68b5e 488 adj = bgp_adj_out_alloc(
9bcb3eef 489 subgrp, dest,
dcc68b5e 490 bgp_addpath_id_for_peer(peer, afi, safi,
9bcb3eef 491 &path->tx_addpath));
d62a17ae 492 if (!adj)
493 return;
d0bf49ec
LS
494
495 subgrp->pscount++;
d62a17ae 496 }
497
2adac256
DA
498 /* Check if we are sending the same route. This is needed to
499 * avoid duplicate UPDATES. For instance, filtering communities
500 * at egress, neighbors will see duplicate UPDATES despite
501 * the route wasn't changed actually.
502 * Do not suppress BGP UPDATES for route-refresh.
503 */
504 if (CHECK_FLAG(bgp->flags, BGP_FLAG_SUPPRESS_DUPLICATES)
505 && !CHECK_FLAG(subgrp->sflags, SUBGRP_STATUS_FORCE_UPDATES)
506 && adj->attr_hash == attr_hash) {
507 if (BGP_DEBUG(update, UPDATE_OUT)) {
508 char attr_str[BUFSIZ] = {0};
509
510 bgp_dump_attr(attr, attr_str, sizeof(attr_str));
511
512 zlog_debug("%s suppress UPDATE w/ attr: %s", peer->host,
513 attr_str);
514 }
515 return;
516 }
517
d62a17ae 518 if (adj->adv)
519 bgp_advertise_clean_subgroup(subgrp, adj);
520 adj->adv = bgp_advertise_new();
521
522 adv = adj->adv;
9bcb3eef 523 adv->dest = dest;
9b6d8fcf 524 assert(adv->pathi == NULL);
4b7e6066 525 /* bgp_path_info adj_out reference */
9b6d8fcf 526 adv->pathi = bgp_path_info_lock(path);
d62a17ae 527
4d28080c 528 adv->baa = bgp_advertise_attr_intern(subgrp->hash, attr);
d62a17ae 529 adv->adj = adj;
2adac256 530 adj->attr_hash = attr_hash;
d62a17ae 531
532 /* Add new advertisement to advertisement attribute list. */
533 bgp_advertise_add(adv->baa, adv);
534
535 /*
536 * If the update adv list is empty, trigger the member peers'
537 * mrai timers so the socket writes can happen.
538 */
a274fef8 539 if (!bgp_adv_fifo_count(&subgrp->sync->update)) {
a2addae8 540 SUBGRP_FOREACH_PEER (subgrp, paf) {
a77e2f4b
S
541 /* If there are no routes in the withdraw list, set
542 * the flag PEER_STATUS_ADV_DELAY which will allow
543 * more routes to be sent in the update message
544 */
545 if (BGP_SUPPRESS_FIB_ENABLED(bgp)) {
546 adv_peer = PAF_PEER(paf);
547 if (!bgp_adv_fifo_count(
548 &subgrp->sync->withdraw))
549 SET_FLAG(adv_peer->thread_flags,
550 PEER_THREAD_SUBGRP_ADV_DELAY);
551 else
552 UNSET_FLAG(adv_peer->thread_flags,
553 PEER_THREAD_SUBGRP_ADV_DELAY);
554 }
d62a17ae 555 bgp_adjust_routeadv(PAF_PEER(paf));
556 }
3f9c7369 557 }
3f9c7369 558
a274fef8 559 bgp_adv_fifo_add_tail(&subgrp->sync->update, adv);
3f9c7369 560
7a8ce9d5 561 subgrp->version = MAX(subgrp->version, dest->version);
3f9c7369
DS
562}
563
4125bb67
DS
564/* The only time 'withdraw' will be false is if we are sending
565 * the "neighbor x.x.x.x default-originate" default and need to clear
566 * bgp_adj_out for the 0.0.0.0/0 route in the BGP table.
567 */
9bcb3eef 568void bgp_adj_out_unset_subgroup(struct bgp_dest *dest,
d62a17ae 569 struct update_subgroup *subgrp, char withdraw,
d7c0a89a 570 uint32_t addpath_tx_id)
3f9c7369 571{
d62a17ae 572 struct bgp_adj_out *adj;
573 struct bgp_advertise *adv;
2fc102e1 574 bool trigger_write;
d62a17ae 575
576 if (DISABLE_BGP_ANNOUNCE)
577 return;
578
579 /* Lookup existing adjacency */
4953391b
DA
580 adj = adj_lookup(dest, subgrp, addpath_tx_id);
581 if (adj != NULL) {
d62a17ae 582 /* Clean up previous advertisement. */
583 if (adj->adv)
584 bgp_advertise_clean_subgroup(subgrp, adj);
585
f55c9a46
S
586 /* If default originate is enabled and the route is default
587 * route, do not send withdraw. This will prevent deletion of
588 * the default route at the peer.
589 */
590 if (CHECK_FLAG(subgrp->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE)
9bcb3eef 591 && is_default_prefix(bgp_dest_get_prefix(dest)))
f55c9a46
S
592 return;
593
d62a17ae 594 if (adj->attr && withdraw) {
595 /* We need advertisement structure. */
596 adj->adv = bgp_advertise_new();
597 adv = adj->adv;
9bcb3eef 598 adv->dest = dest;
d62a17ae 599 adv->adj = adj;
600
2fc102e1
QY
601 /* Note if we need to trigger a packet write */
602 trigger_write =
a274fef8 603 !bgp_adv_fifo_count(&subgrp->sync->withdraw);
2fc102e1 604
d62a17ae 605 /* Add to synchronization entry for withdraw
606 * announcement. */
a274fef8 607 bgp_adv_fifo_add_tail(&subgrp->sync->withdraw, adv);
2fc102e1
QY
608
609 if (trigger_write)
610 subgroup_trigger_write(subgrp);
d62a17ae 611 } else {
d62a17ae 612 /* Free allocated information. */
613 adj_free(adj);
d62a17ae 614 }
d0bf49ec
LS
615 if (!CHECK_FLAG(subgrp->sflags, SUBGRP_STATUS_TABLE_REPARSING))
616 subgrp->pscount--;
d62a17ae 617 }
618
7a8ce9d5 619 subgrp->version = MAX(subgrp->version, dest->version);
3f9c7369
DS
620}
621
9bcb3eef 622void bgp_adj_out_remove_subgroup(struct bgp_dest *dest, struct bgp_adj_out *adj,
d62a17ae 623 struct update_subgroup *subgrp)
3f9c7369 624{
d62a17ae 625 if (adj->attr)
626 bgp_attr_unintern(&adj->attr);
3f9c7369 627
d62a17ae 628 if (adj->adv)
629 bgp_advertise_clean_subgroup(subgrp, adj);
3f9c7369 630
d62a17ae 631 adj_free(adj);
3f9c7369
DS
632}
633
634/*
635 * Go through all the routes and clean up the adj/adv structures corresponding
636 * to the subgroup.
637 */
d62a17ae 638void subgroup_clear_table(struct update_subgroup *subgrp)
3f9c7369 639{
d62a17ae 640 struct bgp_adj_out *aout, *taout;
641
9669fbde
DS
642 SUBGRP_FOREACH_ADJ_SAFE (subgrp, aout, taout)
643 bgp_adj_out_remove_subgroup(aout->dest, aout, subgrp);
3f9c7369
DS
644}
645
646/*
647 * subgroup_announce_table
648 */
d62a17ae 649void subgroup_announce_table(struct update_subgroup *subgrp,
650 struct bgp_table *table)
3f9c7369 651{
9bcb3eef 652 struct bgp_dest *dest;
4b7e6066 653 struct bgp_path_info *ri;
d62a17ae 654 struct attr attr;
655 struct peer *peer;
656 afi_t afi;
657 safi_t safi;
be92fc9f 658 bool addpath_capable;
a77e2f4b
S
659 struct bgp *bgp;
660 bool advertise;
d62a17ae 661
662 peer = SUBGRP_PEER(subgrp);
663 afi = SUBGRP_AFI(subgrp);
664 safi = SUBGRP_SAFI(subgrp);
a77e2f4b 665 bgp = SUBGRP_INST(subgrp);
d62a17ae 666 addpath_capable = bgp_addpath_encode_tx(peer, afi, safi);
667
668 if (safi == SAFI_LABELED_UNICAST)
669 safi = SAFI_UNICAST;
670
671 if (!table)
672 table = peer->bgp->rib[afi][safi];
673
674 if (safi != SAFI_MPLS_VPN && safi != SAFI_ENCAP && safi != SAFI_EVPN
675 && CHECK_FLAG(peer->af_flags[afi][safi],
676 PEER_FLAG_DEFAULT_ORIGINATE))
677 subgroup_default_originate(subgrp, 0);
678
d0bf49ec
LS
679 subgrp->pscount = 0;
680 SET_FLAG(subgrp->sflags, SUBGRP_STATUS_TABLE_REPARSING);
681
9bcb3eef
DS
682 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
683 const struct prefix *dest_p = bgp_dest_get_prefix(dest);
b54892e0 684
a77e2f4b
S
685 /* Check if the route can be advertised */
686 advertise = bgp_check_advertise(bgp, dest);
687
7622259e
DS
688 for (ri = bgp_dest_get_bgp_path_info(dest); ri; ri = ri->next) {
689
690 if (!bgp_check_selected(ri, peer, addpath_capable, afi,
691 safi))
692 continue;
693
694 if (subgroup_announce_check(dest, ri, subgrp, dest_p,
695 &attr, NULL)) {
696 /* Check if route can be advertised */
697 if (advertise) {
698 if (!bgp_check_withdrawal(bgp, dest))
699 bgp_adj_out_set_subgroup(
700 dest, subgrp, &attr,
701 ri);
702 else
703 bgp_adj_out_unset_subgroup(
704 dest, subgrp, 1,
705 bgp_addpath_id_for_peer(
706 peer, afi, safi,
707 &ri->tx_addpath));
f55c9a46 708 }
7622259e
DS
709 } else {
710 /* If default originate is enabled for
711 * the peer, do not send explicit
712 * withdraw. This will prevent deletion
713 * of default route advertised through
714 * default originate
715 */
716 if (CHECK_FLAG(peer->af_flags[afi][safi],
717 PEER_FLAG_DEFAULT_ORIGINATE) &&
718 is_default_prefix(
719 bgp_dest_get_prefix(dest)))
720 break;
721
722 bgp_adj_out_unset_subgroup(
723 dest, subgrp, 1,
724 bgp_addpath_id_for_peer(
725 peer, afi, safi,
726 &ri->tx_addpath));
d62a17ae 727 }
7622259e 728 }
b54892e0 729 }
d0bf49ec 730 UNSET_FLAG(subgrp->sflags, SUBGRP_STATUS_TABLE_REPARSING);
d62a17ae 731
732 /*
733 * We walked through the whole table -- make sure our version number
734 * is consistent with the one on the table. This should allow
735 * subgroups to merge sooner if a peer comes up when the route node
736 * with the largest version is no longer in the table. This also
737 * covers the pathological case where all routes in the table have
738 * now been deleted.
739 */
7a8ce9d5 740 subgrp->version = MAX(subgrp->version, table->version);
d62a17ae 741
742 /*
743 * Start a task to merge the subgroup if necessary.
744 */
745 update_subgroup_trigger_merge_check(subgrp, 0);
3f9c7369
DS
746}
747
748/*
749 * subgroup_announce_route
750 *
751 * Refresh all routes out to a subgroup.
752 */
d62a17ae 753void subgroup_announce_route(struct update_subgroup *subgrp)
3f9c7369 754{
9bcb3eef 755 struct bgp_dest *dest;
d62a17ae 756 struct bgp_table *table;
757 struct peer *onlypeer;
758
759 if (update_subgroup_needs_refresh(subgrp)) {
760 update_subgroup_set_needs_refresh(subgrp, 0);
761 }
762
763 /*
764 * First update is deferred until ORF or ROUTE-REFRESH is received
765 */
766 onlypeer = ((SUBGRP_PCOUNT(subgrp) == 1) ? (SUBGRP_PFIRST(subgrp))->peer
767 : NULL);
9d303b37
DL
768 if (onlypeer && CHECK_FLAG(onlypeer->af_sflags[SUBGRP_AFI(subgrp)]
769 [SUBGRP_SAFI(subgrp)],
770 PEER_STATUS_ORF_WAIT_REFRESH))
d62a17ae 771 return;
772
773 if (SUBGRP_SAFI(subgrp) != SAFI_MPLS_VPN
774 && SUBGRP_SAFI(subgrp) != SAFI_ENCAP
775 && SUBGRP_SAFI(subgrp) != SAFI_EVPN)
776 subgroup_announce_table(subgrp, NULL);
777 else
9bcb3eef
DS
778 for (dest = bgp_table_top(update_subgroup_rib(subgrp)); dest;
779 dest = bgp_route_next(dest)) {
780 table = bgp_dest_get_bgp_table_info(dest);
67009e22
DS
781 if (!table)
782 continue;
783 subgroup_announce_table(subgrp, table);
784 }
3f9c7369
DS
785}
786
d62a17ae 787void subgroup_default_originate(struct update_subgroup *subgrp, int withdraw)
3f9c7369 788{
d62a17ae 789 struct bgp *bgp;
e50c68b2 790 struct attr attr;
7208c282 791 struct attr *new_attr = &attr;
d62a17ae 792 struct prefix p;
793 struct peer *from;
9bcb3eef 794 struct bgp_dest *dest;
f55c9a46 795 struct bgp_path_info *pi;
d62a17ae 796 struct peer *peer;
f55c9a46 797 struct bgp_adj_out *adj;
b68885f9 798 route_map_result_t ret = RMAP_DENYMATCH;
a633fb57 799 route_map_result_t new_ret = RMAP_DENYMATCH;
d62a17ae 800 afi_t afi;
801 safi_t safi;
a633fb57
IS
802 int pref = 65536;
803 int new_pref = 0;
d62a17ae 804
805 if (!subgrp)
806 return;
807
808 peer = SUBGRP_PEER(subgrp);
809 afi = SUBGRP_AFI(subgrp);
810 safi = SUBGRP_SAFI(subgrp);
811
812 if (!(afi == AFI_IP || afi == AFI_IP6))
813 return;
814
815 bgp = peer->bgp;
816 from = bgp->peer_self;
817
0f05ea43 818 bgp_attr_default_set(&attr, bgp, BGP_ORIGIN_IGP);
e50c68b2 819
91204984
IR
820 /* make coverity happy */
821 assert(attr.aspath);
822
f78cfba9
AR
823 attr.med = 0;
824 attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC);
d62a17ae 825
e50c68b2
DS
826 if ((afi == AFI_IP6) || peer_cap_enhe(peer, afi, safi)) {
827 /* IPv6 global nexthop must be included. */
828 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV6_GLOBAL;
829
830 /* If the peer is on shared nextwork and we have link-local
831 nexthop set it. */
832 if (peer->shared_network
833 && !IN6_IS_ADDR_UNSPECIFIED(&peer->nexthop.v6_local))
834 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL;
835 }
836
d62a17ae 837 if (peer->default_rmap[afi][safi].name) {
bf844bac
IR
838 struct bgp_path_info tmp_pi = {0};
839
840 tmp_pi.peer = bgp->peer_self;
841
d62a17ae 842 SET_FLAG(bgp->peer_self->rmap_type, PEER_RMAP_TYPE_DEFAULT);
7208c282
DA
843
844 /* Iterate over the RIB to see if we can announce
845 * the default route. We announce the default
846 * route only if route-map has a match.
847 */
9bcb3eef
DS
848 for (dest = bgp_table_top(bgp->rib[afi][safi]); dest;
849 dest = bgp_route_next(dest)) {
f52a961a
AC
850 if (!bgp_dest_has_bgp_path_info_data(dest))
851 continue;
852
f2ee6d5c
DA
853 for (pi = bgp_dest_get_bgp_path_info(dest); pi;
854 pi = pi->next) {
bf844bac 855 struct attr tmp_attr = attr;
f2ee6d5c 856
bf844bac 857 tmp_pi.attr = &tmp_attr;
7208c282 858
a633fb57 859 new_ret = route_map_apply_ext(
f2ee6d5c 860 peer->default_rmap[afi][safi].map,
a633fb57
IS
861 bgp_dest_get_prefix(dest), pi, &tmp_pi,
862 &new_pref);
863
864 if (new_ret == RMAP_PERMITMATCH) {
865 if (new_pref < pref) {
866 pref = new_pref;
867 bgp_attr_flush(new_attr);
868 new_attr = bgp_attr_intern(
869 tmp_pi.attr);
870 bgp_attr_flush(tmp_pi.attr);
871 }
f2ee6d5c
DA
872 subgroup_announce_reset_nhop(
873 (peer_cap_enhe(peer, afi, safi)
874 ? AF_INET6
875 : AF_INET),
876 new_attr);
a633fb57
IS
877 ret = new_ret;
878 } else
879 bgp_attr_flush(&tmp_attr);
dc52bece 880 }
d62a17ae 881 }
882 bgp->peer_self->rmap_type = 0;
883
a9ae9fb5
AR
884 if (ret == RMAP_DENYMATCH) {
885 /*
886 * If its a implicit withdraw due to routemap
887 * deny operation need to set the flag back.
888 * This is a convertion of update flow to
889 * withdraw flow.
890 */
891 if (!withdraw &&
892 (!CHECK_FLAG(subgrp->sflags,
893 SUBGRP_STATUS_DEFAULT_ORIGINATE)))
894 SET_FLAG(subgrp->sflags,
895 SUBGRP_STATUS_DEFAULT_ORIGINATE);
d62a17ae 896 withdraw = 1;
a9ae9fb5 897 }
3f9c7369 898 }
3f9c7369 899
f55c9a46
S
900 /* Check if the default route is in local BGP RIB which is
901 * installed through redistribute or network command
902 */
903 memset(&p, 0, sizeof(p));
904 p.family = afi2family(afi);
905 p.prefixlen = 0;
9bcb3eef 906 dest = bgp_afi_node_lookup(bgp->rib[afi][safi], afi, safi, &p, NULL);
f55c9a46 907
d62a17ae 908 if (withdraw) {
f55c9a46
S
909 /* Withdraw the default route advertised using default
910 * originate
911 */
d62a17ae 912 if (CHECK_FLAG(subgrp->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE))
913 subgroup_default_withdraw_packet(subgrp);
914 UNSET_FLAG(subgrp->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE);
f55c9a46
S
915
916 /* If default route is present in the local RIB, advertise the
917 * route
918 */
e71ad4b6 919 if (dest) {
9bcb3eef 920 for (pi = bgp_dest_get_bgp_path_info(dest); pi;
f55c9a46
S
921 pi = pi->next) {
922 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED))
923 if (subgroup_announce_check(
9bcb3eef
DS
924 dest, pi, subgrp,
925 bgp_dest_get_prefix(dest),
51c3a7de 926 &attr, NULL))
f55c9a46 927 bgp_adj_out_set_subgroup(
9bcb3eef
DS
928 dest, subgrp, &attr,
929 pi);
f55c9a46 930 }
e71ad4b6 931 bgp_dest_unlock_node(dest);
f55c9a46 932 }
d62a17ae 933 } else {
934 if (!CHECK_FLAG(subgrp->sflags,
935 SUBGRP_STATUS_DEFAULT_ORIGINATE)) {
7f323236 936
d62a17ae 937 /* The 'neighbor x.x.x.x default-originate' default will
938 * act as an
939 * implicit withdraw for any previous UPDATEs sent for
940 * 0.0.0.0/0 so
941 * clear adj_out for the 0.0.0.0/0 prefix in the BGP
942 * table.
943 */
e71ad4b6 944 if (dest) {
f55c9a46
S
945 /* Remove the adjacency for the previously
946 * advertised default route
947 */
948 adj = adj_lookup(
9bcb3eef 949 dest, subgrp,
f55c9a46
S
950 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE);
951 if (adj != NULL) {
952 /* Clean up previous advertisement. */
953 if (adj->adv)
954 bgp_advertise_clean_subgroup(
955 subgrp, adj);
956
f55c9a46
S
957 /* Free allocated information. */
958 adj_free(adj);
f55c9a46 959 }
e71ad4b6 960 bgp_dest_unlock_node(dest);
f55c9a46
S
961 }
962
963 /* Advertise the default route */
637e5ba4 964 if (bgp_in_graceful_shutdown(bgp))
f55c9a46
S
965 bgp_attr_add_gshut_community(new_attr);
966
967 SET_FLAG(subgrp->sflags,
968 SUBGRP_STATUS_DEFAULT_ORIGINATE);
969 subgroup_default_update_packet(subgrp, new_attr, from);
d62a17ae 970 }
971 }
ab798674
IR
972
973 aspath_unintern(&attr.aspath);
3f9c7369
DS
974}
975
976/*
977 * Announce the BGP table to a subgroup.
978 *
979 * At startup, we try to optimize route announcement by coalescing the
980 * peer-up events. This is done only the first time - from then on,
981 * subgrp->v_coalesce will be set to zero and the normal logic
982 * prevails.
983 */
d62a17ae 984void subgroup_announce_all(struct update_subgroup *subgrp)
3f9c7369 985{
d62a17ae 986 if (!subgrp)
987 return;
988
989 /*
990 * If coalesce timer value is not set, announce routes immediately.
991 */
992 if (!subgrp->v_coalesce) {
993 if (bgp_debug_update(NULL, NULL, subgrp->update_group, 0))
6cde4b45 994 zlog_debug("u%" PRIu64 ":s%" PRIu64" announcing all routes",
d62a17ae 995 subgrp->update_group->id, subgrp->id);
996 subgroup_announce_route(subgrp);
997 return;
998 }
999
1000 /*
1001 * We should wait for the coalesce timer. Arm the timer if not done.
1002 */
1003 if (!subgrp->t_coalesce) {
1004 thread_add_timer_msec(bm->master, subgroup_coalesce_timer,
1005 subgrp, subgrp->v_coalesce,
1006 &subgrp->t_coalesce);
1007 }
3f9c7369
DS
1008}
1009
1010/*
1011 * Go through all update subgroups and set up the adv queue for the
1012 * input route.
1013 */
d62a17ae 1014void group_announce_route(struct bgp *bgp, afi_t afi, safi_t safi,
9bcb3eef 1015 struct bgp_dest *dest, struct bgp_path_info *pi)
3f9c7369 1016{
d62a17ae 1017 struct updwalk_context ctx;
40381db7 1018 ctx.pi = pi;
9bcb3eef 1019 ctx.dest = dest;
a77e2f4b
S
1020
1021 /* If suppress fib is enabled, the route will be advertised when
1022 * FIB status is received
1023 */
1024 if (!bgp_check_advertise(bgp, dest))
1025 return;
1026
d62a17ae 1027 update_group_af_walk(bgp, afi, safi, group_announce_route_walkcb, &ctx);
3f9c7369
DS
1028}
1029
d62a17ae 1030void update_group_show_adj_queue(struct bgp *bgp, afi_t afi, safi_t safi,
1031 struct vty *vty, uint64_t id)
3f9c7369 1032{
d62a17ae 1033 updgrp_show_adj(bgp, afi, safi, vty, id, UPDWALK_FLAGS_ADVQUEUE);
3f9c7369
DS
1034}
1035
d62a17ae 1036void update_group_show_advertised(struct bgp *bgp, afi_t afi, safi_t safi,
1037 struct vty *vty, uint64_t id)
3f9c7369 1038{
d62a17ae 1039 updgrp_show_adj(bgp, afi, safi, vty, id, UPDWALK_FLAGS_ADVERTISED);
3f9c7369
DS
1040}
1041
d62a17ae 1042void update_group_announce(struct bgp *bgp)
3f9c7369 1043{
d62a17ae 1044 update_group_walk(bgp, update_group_announce_walkcb, NULL);
3f9c7369
DS
1045}
1046
d62a17ae 1047void update_group_announce_rrclients(struct bgp *bgp)
3f9c7369 1048{
d62a17ae 1049 update_group_walk(bgp, update_group_announce_rrc_walkcb, NULL);
3f9c7369 1050}