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