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