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