]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_updgrp_adv.c
*: Rename thread.[ch] to event.[ch]
[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"
cb37cb33 20#include "event.h"
3f9c7369
DS
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 }
a8038460
DS
500
501 /*
502 * If BGP is skipping sending this value to it's peers
503 * the version number should be updated just like it
504 * would if it sent the data. Why? Because update
505 * groups will not be coalesced until such time that
506 * the version numbers are the same.
507 *
508 * Imagine a scenario with say 2 peers and they come
509 * up and are placed in the same update group. Then
510 * a new peer comes up a bit later. Then a prefix is
511 * flapped that we decide for the first 2 peers are
512 * mapped to and we decide not to send the data to
513 * it. Then unless more network changes happen we
514 * will never be able to coalesce the 3rd peer down
515 */
516 subgrp->version = MAX(subgrp->version, dest->version);
2adac256
DA
517 return;
518 }
519
d62a17ae 520 if (adj->adv)
521 bgp_advertise_clean_subgroup(subgrp, adj);
522 adj->adv = bgp_advertise_new();
523
524 adv = adj->adv;
9bcb3eef 525 adv->dest = dest;
9b6d8fcf 526 assert(adv->pathi == NULL);
4b7e6066 527 /* bgp_path_info adj_out reference */
9b6d8fcf 528 adv->pathi = bgp_path_info_lock(path);
d62a17ae 529
4d28080c 530 adv->baa = bgp_advertise_attr_intern(subgrp->hash, attr);
d62a17ae 531 adv->adj = adj;
2adac256 532 adj->attr_hash = attr_hash;
d62a17ae 533
534 /* Add new advertisement to advertisement attribute list. */
535 bgp_advertise_add(adv->baa, adv);
536
537 /*
538 * If the update adv list is empty, trigger the member peers'
539 * mrai timers so the socket writes can happen.
540 */
a274fef8 541 if (!bgp_adv_fifo_count(&subgrp->sync->update)) {
a2addae8 542 SUBGRP_FOREACH_PEER (subgrp, paf) {
a77e2f4b
S
543 /* If there are no routes in the withdraw list, set
544 * the flag PEER_STATUS_ADV_DELAY which will allow
545 * more routes to be sent in the update message
546 */
547 if (BGP_SUPPRESS_FIB_ENABLED(bgp)) {
548 adv_peer = PAF_PEER(paf);
549 if (!bgp_adv_fifo_count(
550 &subgrp->sync->withdraw))
551 SET_FLAG(adv_peer->thread_flags,
552 PEER_THREAD_SUBGRP_ADV_DELAY);
553 else
554 UNSET_FLAG(adv_peer->thread_flags,
555 PEER_THREAD_SUBGRP_ADV_DELAY);
556 }
d62a17ae 557 bgp_adjust_routeadv(PAF_PEER(paf));
558 }
3f9c7369 559 }
3f9c7369 560
a274fef8 561 bgp_adv_fifo_add_tail(&subgrp->sync->update, adv);
3f9c7369 562
7a8ce9d5 563 subgrp->version = MAX(subgrp->version, dest->version);
3f9c7369
DS
564}
565
4125bb67
DS
566/* The only time 'withdraw' will be false is if we are sending
567 * the "neighbor x.x.x.x default-originate" default and need to clear
568 * bgp_adj_out for the 0.0.0.0/0 route in the BGP table.
569 */
9bcb3eef 570void bgp_adj_out_unset_subgroup(struct bgp_dest *dest,
d62a17ae 571 struct update_subgroup *subgrp, char withdraw,
d7c0a89a 572 uint32_t addpath_tx_id)
3f9c7369 573{
d62a17ae 574 struct bgp_adj_out *adj;
575 struct bgp_advertise *adv;
2fc102e1 576 bool trigger_write;
d62a17ae 577
578 if (DISABLE_BGP_ANNOUNCE)
579 return;
580
581 /* Lookup existing adjacency */
4953391b
DA
582 adj = adj_lookup(dest, subgrp, addpath_tx_id);
583 if (adj != NULL) {
d62a17ae 584 /* Clean up previous advertisement. */
585 if (adj->adv)
586 bgp_advertise_clean_subgroup(subgrp, adj);
587
f55c9a46
S
588 /* If default originate is enabled and the route is default
589 * route, do not send withdraw. This will prevent deletion of
590 * the default route at the peer.
591 */
592 if (CHECK_FLAG(subgrp->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE)
9bcb3eef 593 && is_default_prefix(bgp_dest_get_prefix(dest)))
f55c9a46
S
594 return;
595
d62a17ae 596 if (adj->attr && withdraw) {
597 /* We need advertisement structure. */
598 adj->adv = bgp_advertise_new();
599 adv = adj->adv;
9bcb3eef 600 adv->dest = dest;
d62a17ae 601 adv->adj = adj;
602
2fc102e1
QY
603 /* Note if we need to trigger a packet write */
604 trigger_write =
a274fef8 605 !bgp_adv_fifo_count(&subgrp->sync->withdraw);
2fc102e1 606
d62a17ae 607 /* Add to synchronization entry for withdraw
608 * announcement. */
a274fef8 609 bgp_adv_fifo_add_tail(&subgrp->sync->withdraw, adv);
2fc102e1
QY
610
611 if (trigger_write)
612 subgroup_trigger_write(subgrp);
d62a17ae 613 } else {
d62a17ae 614 /* Free allocated information. */
615 adj_free(adj);
d62a17ae 616 }
d0bf49ec
LS
617 if (!CHECK_FLAG(subgrp->sflags, SUBGRP_STATUS_TABLE_REPARSING))
618 subgrp->pscount--;
d62a17ae 619 }
620
7a8ce9d5 621 subgrp->version = MAX(subgrp->version, dest->version);
3f9c7369
DS
622}
623
9bcb3eef 624void bgp_adj_out_remove_subgroup(struct bgp_dest *dest, struct bgp_adj_out *adj,
d62a17ae 625 struct update_subgroup *subgrp)
3f9c7369 626{
d62a17ae 627 if (adj->attr)
628 bgp_attr_unintern(&adj->attr);
3f9c7369 629
d62a17ae 630 if (adj->adv)
631 bgp_advertise_clean_subgroup(subgrp, adj);
3f9c7369 632
d62a17ae 633 adj_free(adj);
3f9c7369
DS
634}
635
636/*
637 * Go through all the routes and clean up the adj/adv structures corresponding
638 * to the subgroup.
639 */
d62a17ae 640void subgroup_clear_table(struct update_subgroup *subgrp)
3f9c7369 641{
d62a17ae 642 struct bgp_adj_out *aout, *taout;
643
9669fbde
DS
644 SUBGRP_FOREACH_ADJ_SAFE (subgrp, aout, taout)
645 bgp_adj_out_remove_subgroup(aout->dest, aout, subgrp);
3f9c7369
DS
646}
647
648/*
649 * subgroup_announce_table
650 */
d62a17ae 651void subgroup_announce_table(struct update_subgroup *subgrp,
652 struct bgp_table *table)
3f9c7369 653{
9bcb3eef 654 struct bgp_dest *dest;
4b7e6066 655 struct bgp_path_info *ri;
d62a17ae 656 struct attr attr;
657 struct peer *peer;
658 afi_t afi;
659 safi_t safi;
2de5f5b5 660 safi_t safi_rib;
be92fc9f 661 bool addpath_capable;
a77e2f4b
S
662 struct bgp *bgp;
663 bool advertise;
d62a17ae 664
665 peer = SUBGRP_PEER(subgrp);
666 afi = SUBGRP_AFI(subgrp);
667 safi = SUBGRP_SAFI(subgrp);
a77e2f4b 668 bgp = SUBGRP_INST(subgrp);
d62a17ae 669 addpath_capable = bgp_addpath_encode_tx(peer, afi, safi);
670
671 if (safi == SAFI_LABELED_UNICAST)
2de5f5b5
DA
672 safi_rib = SAFI_UNICAST;
673 else
674 safi_rib = safi;
d62a17ae 675
676 if (!table)
2de5f5b5 677 table = peer->bgp->rib[afi][safi_rib];
d62a17ae 678
679 if (safi != SAFI_MPLS_VPN && safi != SAFI_ENCAP && safi != SAFI_EVPN
680 && CHECK_FLAG(peer->af_flags[afi][safi],
681 PEER_FLAG_DEFAULT_ORIGINATE))
682 subgroup_default_originate(subgrp, 0);
683
d0bf49ec
LS
684 subgrp->pscount = 0;
685 SET_FLAG(subgrp->sflags, SUBGRP_STATUS_TABLE_REPARSING);
686
9bcb3eef
DS
687 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
688 const struct prefix *dest_p = bgp_dest_get_prefix(dest);
b54892e0 689
a77e2f4b
S
690 /* Check if the route can be advertised */
691 advertise = bgp_check_advertise(bgp, dest);
692
7622259e
DS
693 for (ri = bgp_dest_get_bgp_path_info(dest); ri; ri = ri->next) {
694
695 if (!bgp_check_selected(ri, peer, addpath_capable, afi,
2de5f5b5 696 safi_rib))
7622259e
DS
697 continue;
698
699 if (subgroup_announce_check(dest, ri, subgrp, dest_p,
700 &attr, NULL)) {
701 /* Check if route can be advertised */
702 if (advertise) {
29a9aae5
DA
703 if (!bgp_check_withdrawal(bgp, dest)) {
704 struct attr *adv_attr =
705 bgp_attr_intern(&attr);
706
7622259e 707 bgp_adj_out_set_subgroup(
29a9aae5 708 dest, subgrp, adv_attr,
7622259e 709 ri);
29a9aae5 710 } else
7622259e
DS
711 bgp_adj_out_unset_subgroup(
712 dest, subgrp, 1,
713 bgp_addpath_id_for_peer(
2de5f5b5
DA
714 peer, afi,
715 safi_rib,
7622259e 716 &ri->tx_addpath));
f55c9a46 717 }
7622259e
DS
718 } else {
719 /* If default originate is enabled for
720 * the peer, do not send explicit
721 * withdraw. This will prevent deletion
722 * of default route advertised through
723 * default originate
724 */
725 if (CHECK_FLAG(peer->af_flags[afi][safi],
726 PEER_FLAG_DEFAULT_ORIGINATE) &&
727 is_default_prefix(
728 bgp_dest_get_prefix(dest)))
729 break;
730
731 bgp_adj_out_unset_subgroup(
732 dest, subgrp, 1,
733 bgp_addpath_id_for_peer(
2de5f5b5 734 peer, afi, safi_rib,
7622259e 735 &ri->tx_addpath));
d62a17ae 736 }
7622259e 737 }
b54892e0 738 }
d0bf49ec 739 UNSET_FLAG(subgrp->sflags, SUBGRP_STATUS_TABLE_REPARSING);
d62a17ae 740
741 /*
742 * We walked through the whole table -- make sure our version number
743 * is consistent with the one on the table. This should allow
744 * subgroups to merge sooner if a peer comes up when the route node
745 * with the largest version is no longer in the table. This also
746 * covers the pathological case where all routes in the table have
747 * now been deleted.
748 */
7a8ce9d5 749 subgrp->version = MAX(subgrp->version, table->version);
d62a17ae 750
751 /*
752 * Start a task to merge the subgroup if necessary.
753 */
754 update_subgroup_trigger_merge_check(subgrp, 0);
3f9c7369
DS
755}
756
757/*
758 * subgroup_announce_route
759 *
760 * Refresh all routes out to a subgroup.
761 */
d62a17ae 762void subgroup_announce_route(struct update_subgroup *subgrp)
3f9c7369 763{
9bcb3eef 764 struct bgp_dest *dest;
d62a17ae 765 struct bgp_table *table;
766 struct peer *onlypeer;
767
768 if (update_subgroup_needs_refresh(subgrp)) {
769 update_subgroup_set_needs_refresh(subgrp, 0);
770 }
771
772 /*
773 * First update is deferred until ORF or ROUTE-REFRESH is received
774 */
775 onlypeer = ((SUBGRP_PCOUNT(subgrp) == 1) ? (SUBGRP_PFIRST(subgrp))->peer
776 : NULL);
9d303b37
DL
777 if (onlypeer && CHECK_FLAG(onlypeer->af_sflags[SUBGRP_AFI(subgrp)]
778 [SUBGRP_SAFI(subgrp)],
779 PEER_STATUS_ORF_WAIT_REFRESH))
d62a17ae 780 return;
781
782 if (SUBGRP_SAFI(subgrp) != SAFI_MPLS_VPN
783 && SUBGRP_SAFI(subgrp) != SAFI_ENCAP
784 && SUBGRP_SAFI(subgrp) != SAFI_EVPN)
785 subgroup_announce_table(subgrp, NULL);
786 else
9bcb3eef
DS
787 for (dest = bgp_table_top(update_subgroup_rib(subgrp)); dest;
788 dest = bgp_route_next(dest)) {
789 table = bgp_dest_get_bgp_table_info(dest);
67009e22
DS
790 if (!table)
791 continue;
792 subgroup_announce_table(subgrp, table);
793 }
3f9c7369
DS
794}
795
d62a17ae 796void subgroup_default_originate(struct update_subgroup *subgrp, int withdraw)
3f9c7369 797{
d62a17ae 798 struct bgp *bgp;
e50c68b2 799 struct attr attr;
7208c282 800 struct attr *new_attr = &attr;
d62a17ae 801 struct prefix p;
802 struct peer *from;
9bcb3eef 803 struct bgp_dest *dest;
f55c9a46 804 struct bgp_path_info *pi;
d62a17ae 805 struct peer *peer;
f55c9a46 806 struct bgp_adj_out *adj;
b68885f9 807 route_map_result_t ret = RMAP_DENYMATCH;
a633fb57 808 route_map_result_t new_ret = RMAP_DENYMATCH;
d62a17ae 809 afi_t afi;
810 safi_t safi;
2de5f5b5 811 safi_t safi_rib;
a633fb57
IS
812 int pref = 65536;
813 int new_pref = 0;
d62a17ae 814
815 if (!subgrp)
816 return;
817
818 peer = SUBGRP_PEER(subgrp);
819 afi = SUBGRP_AFI(subgrp);
820 safi = SUBGRP_SAFI(subgrp);
821
822 if (!(afi == AFI_IP || afi == AFI_IP6))
823 return;
824
2de5f5b5
DA
825 if (safi == SAFI_LABELED_UNICAST)
826 safi_rib = SAFI_UNICAST;
827 else
828 safi_rib = safi;
829
d62a17ae 830 bgp = peer->bgp;
831 from = bgp->peer_self;
832
0f05ea43 833 bgp_attr_default_set(&attr, bgp, BGP_ORIGIN_IGP);
e50c68b2 834
91204984
IR
835 /* make coverity happy */
836 assert(attr.aspath);
837
f78cfba9
AR
838 attr.med = 0;
839 attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC);
d62a17ae 840
e50c68b2
DS
841 if ((afi == AFI_IP6) || peer_cap_enhe(peer, afi, safi)) {
842 /* IPv6 global nexthop must be included. */
843 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV6_GLOBAL;
844
845 /* If the peer is on shared nextwork and we have link-local
846 nexthop set it. */
847 if (peer->shared_network
848 && !IN6_IS_ADDR_UNSPECIFIED(&peer->nexthop.v6_local))
849 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL;
850 }
851
d62a17ae 852 if (peer->default_rmap[afi][safi].name) {
bf844bac
IR
853 struct bgp_path_info tmp_pi = {0};
854
855 tmp_pi.peer = bgp->peer_self;
856
d62a17ae 857 SET_FLAG(bgp->peer_self->rmap_type, PEER_RMAP_TYPE_DEFAULT);
7208c282
DA
858
859 /* Iterate over the RIB to see if we can announce
860 * the default route. We announce the default
861 * route only if route-map has a match.
862 */
2de5f5b5 863 for (dest = bgp_table_top(bgp->rib[afi][safi_rib]); dest;
9bcb3eef 864 dest = bgp_route_next(dest)) {
f52a961a
AC
865 if (!bgp_dest_has_bgp_path_info_data(dest))
866 continue;
867
f2ee6d5c
DA
868 for (pi = bgp_dest_get_bgp_path_info(dest); pi;
869 pi = pi->next) {
bf844bac 870 struct attr tmp_attr = attr;
f2ee6d5c 871
bf844bac 872 tmp_pi.attr = &tmp_attr;
7208c282 873
a633fb57 874 new_ret = route_map_apply_ext(
f2ee6d5c 875 peer->default_rmap[afi][safi].map,
a633fb57
IS
876 bgp_dest_get_prefix(dest), pi, &tmp_pi,
877 &new_pref);
878
879 if (new_ret == RMAP_PERMITMATCH) {
880 if (new_pref < pref) {
881 pref = new_pref;
882 bgp_attr_flush(new_attr);
883 new_attr = bgp_attr_intern(
884 tmp_pi.attr);
885 bgp_attr_flush(tmp_pi.attr);
886 }
f2ee6d5c
DA
887 subgroup_announce_reset_nhop(
888 (peer_cap_enhe(peer, afi, safi)
889 ? AF_INET6
890 : AF_INET),
891 new_attr);
a633fb57
IS
892 ret = new_ret;
893 } else
894 bgp_attr_flush(&tmp_attr);
dc52bece 895 }
d62a17ae 896 }
897 bgp->peer_self->rmap_type = 0;
898
a9ae9fb5
AR
899 if (ret == RMAP_DENYMATCH) {
900 /*
901 * If its a implicit withdraw due to routemap
902 * deny operation need to set the flag back.
903 * This is a convertion of update flow to
904 * withdraw flow.
905 */
906 if (!withdraw &&
907 (!CHECK_FLAG(subgrp->sflags,
908 SUBGRP_STATUS_DEFAULT_ORIGINATE)))
909 SET_FLAG(subgrp->sflags,
910 SUBGRP_STATUS_DEFAULT_ORIGINATE);
d62a17ae 911 withdraw = 1;
a9ae9fb5 912 }
3f9c7369 913 }
3f9c7369 914
f55c9a46
S
915 /* Check if the default route is in local BGP RIB which is
916 * installed through redistribute or network command
917 */
918 memset(&p, 0, sizeof(p));
919 p.family = afi2family(afi);
920 p.prefixlen = 0;
59d6b4d6
DA
921 dest = bgp_safi_node_lookup(bgp->rib[afi][safi_rib], safi_rib, &p,
922 NULL);
f55c9a46 923
d62a17ae 924 if (withdraw) {
f55c9a46
S
925 /* Withdraw the default route advertised using default
926 * originate
927 */
d62a17ae 928 if (CHECK_FLAG(subgrp->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE))
929 subgroup_default_withdraw_packet(subgrp);
930 UNSET_FLAG(subgrp->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE);
f55c9a46
S
931
932 /* If default route is present in the local RIB, advertise the
933 * route
934 */
e71ad4b6 935 if (dest) {
9bcb3eef 936 for (pi = bgp_dest_get_bgp_path_info(dest); pi;
f55c9a46
S
937 pi = pi->next) {
938 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED))
939 if (subgroup_announce_check(
9bcb3eef
DS
940 dest, pi, subgrp,
941 bgp_dest_get_prefix(dest),
e9340ff4
DA
942 &attr, NULL)) {
943 struct attr *default_attr =
944 bgp_attr_intern(&attr);
945
f55c9a46 946 bgp_adj_out_set_subgroup(
e9340ff4
DA
947 dest, subgrp,
948 default_attr, pi);
949 }
f55c9a46 950 }
e71ad4b6 951 bgp_dest_unlock_node(dest);
f55c9a46 952 }
d62a17ae 953 } else {
954 if (!CHECK_FLAG(subgrp->sflags,
955 SUBGRP_STATUS_DEFAULT_ORIGINATE)) {
7f323236 956
d62a17ae 957 /* The 'neighbor x.x.x.x default-originate' default will
958 * act as an
959 * implicit withdraw for any previous UPDATEs sent for
960 * 0.0.0.0/0 so
961 * clear adj_out for the 0.0.0.0/0 prefix in the BGP
962 * table.
963 */
e71ad4b6 964 if (dest) {
f55c9a46
S
965 /* Remove the adjacency for the previously
966 * advertised default route
967 */
968 adj = adj_lookup(
9bcb3eef 969 dest, subgrp,
f55c9a46
S
970 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE);
971 if (adj != NULL) {
972 /* Clean up previous advertisement. */
973 if (adj->adv)
974 bgp_advertise_clean_subgroup(
975 subgrp, adj);
976
f55c9a46
S
977 /* Free allocated information. */
978 adj_free(adj);
f55c9a46 979 }
e71ad4b6 980 bgp_dest_unlock_node(dest);
f55c9a46
S
981 }
982
983 /* Advertise the default route */
637e5ba4 984 if (bgp_in_graceful_shutdown(bgp))
f55c9a46
S
985 bgp_attr_add_gshut_community(new_attr);
986
987 SET_FLAG(subgrp->sflags,
988 SUBGRP_STATUS_DEFAULT_ORIGINATE);
989 subgroup_default_update_packet(subgrp, new_attr, from);
d62a17ae 990 }
991 }
ab798674
IR
992
993 aspath_unintern(&attr.aspath);
3f9c7369
DS
994}
995
996/*
997 * Announce the BGP table to a subgroup.
998 *
999 * At startup, we try to optimize route announcement by coalescing the
1000 * peer-up events. This is done only the first time - from then on,
1001 * subgrp->v_coalesce will be set to zero and the normal logic
1002 * prevails.
1003 */
d62a17ae 1004void subgroup_announce_all(struct update_subgroup *subgrp)
3f9c7369 1005{
d62a17ae 1006 if (!subgrp)
1007 return;
1008
1009 /*
1010 * If coalesce timer value is not set, announce routes immediately.
1011 */
1012 if (!subgrp->v_coalesce) {
1013 if (bgp_debug_update(NULL, NULL, subgrp->update_group, 0))
6cde4b45 1014 zlog_debug("u%" PRIu64 ":s%" PRIu64" announcing all routes",
d62a17ae 1015 subgrp->update_group->id, subgrp->id);
1016 subgroup_announce_route(subgrp);
1017 return;
1018 }
1019
1020 /*
1021 * We should wait for the coalesce timer. Arm the timer if not done.
1022 */
1023 if (!subgrp->t_coalesce) {
1024 thread_add_timer_msec(bm->master, subgroup_coalesce_timer,
1025 subgrp, subgrp->v_coalesce,
1026 &subgrp->t_coalesce);
1027 }
3f9c7369
DS
1028}
1029
1030/*
1031 * Go through all update subgroups and set up the adv queue for the
1032 * input route.
1033 */
d62a17ae 1034void group_announce_route(struct bgp *bgp, afi_t afi, safi_t safi,
9bcb3eef 1035 struct bgp_dest *dest, struct bgp_path_info *pi)
3f9c7369 1036{
d62a17ae 1037 struct updwalk_context ctx;
40381db7 1038 ctx.pi = pi;
9bcb3eef 1039 ctx.dest = dest;
a77e2f4b
S
1040
1041 /* If suppress fib is enabled, the route will be advertised when
1042 * FIB status is received
1043 */
1044 if (!bgp_check_advertise(bgp, dest))
1045 return;
1046
d62a17ae 1047 update_group_af_walk(bgp, afi, safi, group_announce_route_walkcb, &ctx);
3f9c7369
DS
1048}
1049
d62a17ae 1050void update_group_show_adj_queue(struct bgp *bgp, afi_t afi, safi_t safi,
1051 struct vty *vty, uint64_t id)
3f9c7369 1052{
d62a17ae 1053 updgrp_show_adj(bgp, afi, safi, vty, id, UPDWALK_FLAGS_ADVQUEUE);
3f9c7369
DS
1054}
1055
d62a17ae 1056void update_group_show_advertised(struct bgp *bgp, afi_t afi, safi_t safi,
1057 struct vty *vty, uint64_t id)
3f9c7369 1058{
d62a17ae 1059 updgrp_show_adj(bgp, afi, safi, vty, id, UPDWALK_FLAGS_ADVERTISED);
3f9c7369
DS
1060}
1061
d62a17ae 1062void update_group_announce(struct bgp *bgp)
3f9c7369 1063{
d62a17ae 1064 update_group_walk(bgp, update_group_announce_walkcb, NULL);
3f9c7369
DS
1065}
1066
d62a17ae 1067void update_group_announce_rrclients(struct bgp *bgp)
3f9c7369 1068{
d62a17ae 1069 update_group_walk(bgp, update_group_announce_rrc_walkcb, NULL);
3f9c7369 1070}