]>
Commit | Line | Data |
---|---|---|
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 | ********************/ | |
58 | ||
d62a17ae | 59 | static inline struct bgp_adj_out *adj_lookup(struct bgp_node *rn, |
60 | struct update_subgroup *subgrp, | |
d7c0a89a | 61 | uint32_t addpath_tx_id) |
3f9c7369 | 62 | { |
d62a17ae | 63 | struct bgp_adj_out *adj; |
64 | struct peer *peer; | |
65 | afi_t afi; | |
66 | safi_t safi; | |
67 | int addpath_capable; | |
68 | ||
69 | if (!rn || !subgrp) | |
70 | return NULL; | |
71 | ||
72 | peer = SUBGRP_PEER(subgrp); | |
73 | afi = SUBGRP_AFI(subgrp); | |
74 | safi = SUBGRP_SAFI(subgrp); | |
75 | addpath_capable = bgp_addpath_encode_tx(peer, afi, safi); | |
76 | ||
77 | /* update-groups that do not support addpath will pass 0 for | |
78 | * addpath_tx_id so do not both matching against it */ | |
79 | for (adj = rn->adj_out; adj; adj = adj->next) { | |
80 | if (adj->subgroup == subgrp) { | |
81 | if (addpath_capable) { | |
82 | if (adj->addpath_tx_id == addpath_tx_id) { | |
83 | break; | |
84 | } | |
85 | } else { | |
86 | break; | |
87 | } | |
88 | } | |
89 | } | |
3f9c7369 | 90 | |
d62a17ae | 91 | return adj; |
3f9c7369 DS |
92 | } |
93 | ||
d62a17ae | 94 | static 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 |
101 | static 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 */ | |
113 | for (adj = ctx->rn->adj_out; adj; adj = adj_next) { | |
114 | adj_next = adj->next; | |
115 | ||
116 | if (adj->subgroup == subgrp) { | |
117 | for (pi = ctx->rn->info; pi; pi = pi->next) { | |
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( | |
128 | subgrp, NULL, ctx->rn, | |
129 | adj->addpath_tx_id); | |
130 | } | |
131 | } | |
132 | } | |
133 | } | |
134 | ||
d62a17ae | 135 | static 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 | ||
e0207895 PZ |
151 | if (BGP_DEBUG(update, UPDATE_OUT)) { |
152 | char buf_prefix[PREFIX_STRLEN]; | |
153 | prefix2str(&ctx->rn->p, buf_prefix, sizeof(buf_prefix)); | |
154 | zlog_debug("%s: afi=%s, safi=%s, p=%s", __func__, afi2str(afi), | |
155 | safi2str(safi), buf_prefix); | |
156 | } | |
157 | ||
158 | ||
a2addae8 | 159 | UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) { |
3f9c7369 | 160 | |
d62a17ae | 161 | /* |
162 | * Skip the subgroups that have coalesce timer running. We will | |
163 | * walk the entire prefix table for those subgroups when the | |
164 | * coalesce timer fires. | |
165 | */ | |
166 | if (!subgrp->t_coalesce) { | |
167 | /* An update-group that uses addpath */ | |
168 | if (addpath_capable) { | |
dcc68b5e | 169 | subgrp_withdraw_stale_addpath(ctx, subgrp); |
d62a17ae | 170 | |
40381db7 | 171 | for (pi = ctx->rn->info; pi; pi = pi->next) { |
d62a17ae | 172 | /* Skip the bestpath for now */ |
40381db7 | 173 | if (pi == ctx->pi) |
d62a17ae | 174 | continue; |
175 | ||
176 | subgroup_process_announce_selected( | |
40381db7 | 177 | subgrp, pi, ctx->rn, |
dcc68b5e MS |
178 | bgp_addpath_id_for_peer( |
179 | peer, afi, safi, | |
180 | &pi->tx_addpath)); | |
d62a17ae | 181 | } |
182 | ||
183 | /* Process the bestpath last so the "show [ip] | |
184 | * bgp neighbor x.x.x.x advertised" | |
185 | * output shows the attributes from the bestpath | |
186 | */ | |
40381db7 | 187 | if (ctx->pi) |
d62a17ae | 188 | subgroup_process_announce_selected( |
40381db7 | 189 | subgrp, ctx->pi, ctx->rn, |
dcc68b5e MS |
190 | bgp_addpath_id_for_peer( |
191 | peer, afi, safi, | |
192 | &ctx->pi->tx_addpath)); | |
d62a17ae | 193 | } |
194 | ||
195 | /* An update-group that does not use addpath */ | |
196 | else { | |
40381db7 | 197 | if (ctx->pi) { |
d62a17ae | 198 | subgroup_process_announce_selected( |
40381db7 | 199 | subgrp, ctx->pi, ctx->rn, |
dcc68b5e MS |
200 | bgp_addpath_id_for_peer( |
201 | peer, afi, safi, | |
202 | &ctx->pi->tx_addpath)); | |
d62a17ae | 203 | } else { |
204 | /* Find the addpath_tx_id of the path we | |
205 | * had advertised and | |
206 | * send a withdraw */ | |
207 | for (adj = ctx->rn->adj_out; adj; | |
208 | adj = adj_next) { | |
209 | adj_next = adj->next; | |
210 | ||
211 | if (adj->subgroup == subgrp) { | |
212 | subgroup_process_announce_selected( | |
213 | subgrp, NULL, | |
214 | ctx->rn, | |
215 | adj->addpath_tx_id); | |
216 | } | |
217 | } | |
218 | } | |
219 | } | |
220 | } | |
221 | } | |
3f9c7369 | 222 | |
d62a17ae | 223 | return UPDWALK_CONTINUE; |
224 | } | |
3f9c7369 | 225 | |
d62a17ae | 226 | static 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; | |
232 | struct bgp_node *rn; | |
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 | ||
245 | for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) | |
246 | for (adj = rn->adj_out; adj; adj = adj->next) | |
247 | if (adj->subgroup == subgrp) { | |
248 | if (header1) { | |
249 | vty_out(vty, | |
250 | "BGP table version is %" PRIu64 | |
251 | ", local router ID is %s\n", | |
252 | table->version, | |
253 | inet_ntoa(bgp->router_id)); | |
254 | vty_out(vty, BGP_SHOW_SCODE_HEADER); | |
255 | vty_out(vty, BGP_SHOW_OCODE_HEADER); | |
256 | header1 = 0; | |
257 | } | |
258 | if (header2) { | |
259 | vty_out(vty, BGP_SHOW_HEADER); | |
260 | header2 = 0; | |
261 | } | |
262 | if ((flags & UPDWALK_FLAGS_ADVQUEUE) && adj->adv | |
263 | && adj->adv->baa) { | |
264 | route_vty_out_tmp(vty, &rn->p, | |
265 | adj->adv->baa->attr, | |
266 | SUBGRP_SAFI(subgrp), | |
267 | 0, NULL); | |
268 | output_count++; | |
269 | } | |
270 | if ((flags & UPDWALK_FLAGS_ADVERTISED) | |
271 | && adj->attr) { | |
272 | route_vty_out_tmp( | |
273 | vty, &rn->p, adj->attr, | |
274 | SUBGRP_SAFI(subgrp), 0, NULL); | |
275 | output_count++; | |
276 | } | |
277 | } | |
278 | if (output_count != 0) | |
279 | vty_out(vty, "\nTotal number of prefixes %ld\n", output_count); | |
3f9c7369 DS |
280 | } |
281 | ||
d62a17ae | 282 | static int updgrp_show_adj_walkcb(struct update_group *updgrp, void *arg) |
3f9c7369 | 283 | { |
d62a17ae | 284 | struct updwalk_context *ctx = arg; |
285 | struct update_subgroup *subgrp; | |
286 | struct vty *vty; | |
287 | ||
288 | vty = ctx->vty; | |
a2addae8 | 289 | UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) { |
d62a17ae | 290 | if (ctx->subgrp_id && (ctx->subgrp_id != subgrp->id)) |
291 | continue; | |
292 | vty_out(vty, "update group %" PRIu64 ", subgroup %" PRIu64 "\n", | |
293 | updgrp->id, subgrp->id); | |
294 | subgrp_show_adjq_vty(subgrp, vty, ctx->flags); | |
295 | } | |
296 | return UPDWALK_CONTINUE; | |
3f9c7369 DS |
297 | } |
298 | ||
d62a17ae | 299 | static void updgrp_show_adj(struct bgp *bgp, afi_t afi, safi_t safi, |
d7c0a89a | 300 | struct vty *vty, uint64_t id, uint8_t flags) |
3f9c7369 | 301 | { |
d62a17ae | 302 | struct updwalk_context ctx; |
303 | memset(&ctx, 0, sizeof(ctx)); | |
304 | ctx.vty = vty; | |
305 | ctx.subgrp_id = id; | |
306 | ctx.flags = flags; | |
3f9c7369 | 307 | |
d62a17ae | 308 | update_group_af_walk(bgp, afi, safi, updgrp_show_adj_walkcb, &ctx); |
3f9c7369 DS |
309 | } |
310 | ||
d62a17ae | 311 | static int subgroup_coalesce_timer(struct thread *thread) |
3f9c7369 | 312 | { |
d62a17ae | 313 | struct update_subgroup *subgrp; |
314 | ||
315 | subgrp = THREAD_ARG(thread); | |
316 | if (bgp_debug_update(NULL, NULL, subgrp->update_group, 0)) | |
317 | zlog_debug("u%" PRIu64 ":s%" PRIu64 | |
318 | " announcing routes upon coalesce timer expiry", | |
319 | (SUBGRP_UPDGRP(subgrp))->id, subgrp->id); | |
320 | subgrp->t_coalesce = NULL; | |
321 | subgrp->v_coalesce = 0; | |
322 | subgroup_announce_route(subgrp); | |
323 | ||
324 | ||
325 | /* While the announce_route() may kick off the route advertisement timer | |
326 | * for | |
327 | * the members of the subgroup, we'd like to send the initial updates | |
328 | * much | |
329 | * faster (i.e., without enforcing MRAI). Also, if there were no routes | |
330 | * to | |
331 | * announce, this is the method currently employed to trigger the EOR. | |
332 | */ | |
333 | if (!bgp_update_delay_active(SUBGRP_INST(subgrp))) { | |
334 | struct peer_af *paf; | |
335 | struct peer *peer; | |
336 | ||
a2addae8 | 337 | SUBGRP_FOREACH_PEER (subgrp, paf) { |
d62a17ae | 338 | peer = PAF_PEER(paf); |
339 | BGP_TIMER_OFF(peer->t_routeadv); | |
340 | BGP_TIMER_ON(peer->t_routeadv, bgp_routeadv_timer, 0); | |
341 | } | |
342 | } | |
343 | ||
344 | return 0; | |
3f9c7369 DS |
345 | } |
346 | ||
d62a17ae | 347 | static int update_group_announce_walkcb(struct update_group *updgrp, void *arg) |
3f9c7369 | 348 | { |
d62a17ae | 349 | struct update_subgroup *subgrp; |
3f9c7369 | 350 | |
a2addae8 | 351 | UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) { |
d62a17ae | 352 | subgroup_announce_all(subgrp); |
353 | } | |
3f9c7369 | 354 | |
d62a17ae | 355 | return UPDWALK_CONTINUE; |
3f9c7369 DS |
356 | } |
357 | ||
d62a17ae | 358 | static int update_group_announce_rrc_walkcb(struct update_group *updgrp, |
359 | void *arg) | |
3f9c7369 | 360 | { |
d62a17ae | 361 | struct update_subgroup *subgrp; |
362 | afi_t afi; | |
363 | safi_t safi; | |
364 | struct peer *peer; | |
365 | ||
366 | afi = UPDGRP_AFI(updgrp); | |
367 | safi = UPDGRP_SAFI(updgrp); | |
368 | peer = UPDGRP_PEER(updgrp); | |
369 | ||
370 | /* Only announce if this is a group of route-reflector-clients */ | |
371 | if (CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_REFLECTOR_CLIENT)) { | |
a2addae8 | 372 | UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) { |
d62a17ae | 373 | subgroup_announce_all(subgrp); |
374 | } | |
375 | } | |
376 | ||
377 | return UPDWALK_CONTINUE; | |
3f9c7369 DS |
378 | } |
379 | ||
380 | /******************** | |
381 | * PUBLIC FUNCTIONS | |
382 | ********************/ | |
383 | ||
384 | /** | |
385 | * Allocate an adj-out object. Do proper initialization of its fields, | |
386 | * primarily its association with the subgroup and the prefix. | |
387 | */ | |
d62a17ae | 388 | struct bgp_adj_out *bgp_adj_out_alloc(struct update_subgroup *subgrp, |
389 | struct bgp_node *rn, | |
d7c0a89a | 390 | uint32_t addpath_tx_id) |
3f9c7369 | 391 | { |
d62a17ae | 392 | struct bgp_adj_out *adj; |
393 | ||
394 | adj = XCALLOC(MTYPE_BGP_ADJ_OUT, sizeof(struct bgp_adj_out)); | |
395 | adj->subgroup = subgrp; | |
396 | if (rn) { | |
397 | BGP_ADJ_OUT_ADD(rn, adj); | |
398 | bgp_lock_node(rn); | |
399 | adj->rn = rn; | |
400 | } | |
401 | ||
402 | adj->addpath_tx_id = addpath_tx_id; | |
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 | ||
409 | struct bgp_advertise * | |
d62a17ae | 410 | bgp_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; | |
416 | struct bgp_advertise_fifo *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. */ |
432 | bgp_advertise_unintern(subgrp->hash, baa); | |
433 | } else | |
434 | fhead = &subgrp->sync->withdraw; | |
3f9c7369 DS |
435 | |
436 | ||
d62a17ae | 437 | /* Unlink myself from advertisement FIFO. */ |
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 | ||
d62a17ae | 447 | void bgp_adj_out_set_subgroup(struct bgp_node *rn, |
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; | |
456 | ||
457 | peer = SUBGRP_PEER(subgrp); | |
458 | afi = SUBGRP_AFI(subgrp); | |
459 | safi = SUBGRP_SAFI(subgrp); | |
d62a17ae | 460 | |
461 | if (DISABLE_BGP_ANNOUNCE) | |
462 | return; | |
463 | ||
464 | /* Look for adjacency information. */ | |
dcc68b5e MS |
465 | adj = adj_lookup( |
466 | rn, subgrp, | |
467 | bgp_addpath_id_for_peer(peer, afi, safi, &path->tx_addpath)); | |
d62a17ae | 468 | |
469 | if (!adj) { | |
dcc68b5e MS |
470 | adj = bgp_adj_out_alloc( |
471 | subgrp, rn, | |
472 | bgp_addpath_id_for_peer(peer, afi, safi, | |
473 | &path->tx_addpath)); | |
d62a17ae | 474 | if (!adj) |
475 | return; | |
476 | } | |
477 | ||
478 | if (adj->adv) | |
479 | bgp_advertise_clean_subgroup(subgrp, adj); | |
480 | adj->adv = bgp_advertise_new(); | |
481 | ||
482 | adv = adj->adv; | |
483 | adv->rn = rn; | |
9b6d8fcf | 484 | assert(adv->pathi == NULL); |
4b7e6066 | 485 | /* bgp_path_info adj_out reference */ |
9b6d8fcf | 486 | adv->pathi = bgp_path_info_lock(path); |
d62a17ae | 487 | |
488 | if (attr) | |
489 | adv->baa = bgp_advertise_intern(subgrp->hash, attr); | |
490 | else | |
491 | adv->baa = baa_new(); | |
492 | adv->adj = adj; | |
493 | ||
494 | /* Add new advertisement to advertisement attribute list. */ | |
495 | bgp_advertise_add(adv->baa, adv); | |
496 | ||
497 | /* | |
498 | * If the update adv list is empty, trigger the member peers' | |
499 | * mrai timers so the socket writes can happen. | |
500 | */ | |
501 | if (BGP_ADV_FIFO_EMPTY(&subgrp->sync->update)) { | |
502 | struct peer_af *paf; | |
503 | ||
a2addae8 | 504 | SUBGRP_FOREACH_PEER (subgrp, paf) { |
d62a17ae | 505 | bgp_adjust_routeadv(PAF_PEER(paf)); |
506 | } | |
3f9c7369 | 507 | } |
3f9c7369 | 508 | |
d62a17ae | 509 | BGP_ADV_FIFO_ADD(&subgrp->sync->update, &adv->fifo); |
3f9c7369 | 510 | |
d62a17ae | 511 | subgrp->version = max(subgrp->version, rn->version); |
3f9c7369 DS |
512 | } |
513 | ||
4125bb67 DS |
514 | /* The only time 'withdraw' will be false is if we are sending |
515 | * the "neighbor x.x.x.x default-originate" default and need to clear | |
516 | * bgp_adj_out for the 0.0.0.0/0 route in the BGP table. | |
517 | */ | |
d62a17ae | 518 | void bgp_adj_out_unset_subgroup(struct bgp_node *rn, |
519 | struct update_subgroup *subgrp, char withdraw, | |
d7c0a89a | 520 | uint32_t addpath_tx_id) |
3f9c7369 | 521 | { |
d62a17ae | 522 | struct bgp_adj_out *adj; |
523 | struct bgp_advertise *adv; | |
2fc102e1 | 524 | bool trigger_write; |
d62a17ae | 525 | |
526 | if (DISABLE_BGP_ANNOUNCE) | |
527 | return; | |
528 | ||
529 | /* Lookup existing adjacency */ | |
530 | if ((adj = adj_lookup(rn, subgrp, addpath_tx_id)) != NULL) { | |
531 | /* Clean up previous advertisement. */ | |
532 | if (adj->adv) | |
533 | bgp_advertise_clean_subgroup(subgrp, adj); | |
534 | ||
535 | if (adj->attr && withdraw) { | |
536 | /* We need advertisement structure. */ | |
537 | adj->adv = bgp_advertise_new(); | |
538 | adv = adj->adv; | |
539 | adv->rn = rn; | |
540 | adv->adj = adj; | |
541 | ||
2fc102e1 QY |
542 | /* Note if we need to trigger a packet write */ |
543 | trigger_write = | |
544 | BGP_ADV_FIFO_EMPTY(&subgrp->sync->withdraw); | |
545 | ||
d62a17ae | 546 | /* Add to synchronization entry for withdraw |
547 | * announcement. */ | |
548 | BGP_ADV_FIFO_ADD(&subgrp->sync->withdraw, &adv->fifo); | |
2fc102e1 QY |
549 | |
550 | if (trigger_write) | |
551 | subgroup_trigger_write(subgrp); | |
d62a17ae | 552 | } else { |
553 | /* Remove myself from adjacency. */ | |
554 | BGP_ADJ_OUT_DEL(rn, adj); | |
555 | ||
556 | /* Free allocated information. */ | |
557 | adj_free(adj); | |
558 | ||
559 | bgp_unlock_node(rn); | |
560 | } | |
561 | } | |
562 | ||
563 | subgrp->version = max(subgrp->version, rn->version); | |
3f9c7369 DS |
564 | } |
565 | ||
d62a17ae | 566 | void bgp_adj_out_remove_subgroup(struct bgp_node *rn, struct bgp_adj_out *adj, |
567 | struct update_subgroup *subgrp) | |
3f9c7369 | 568 | { |
d62a17ae | 569 | if (adj->attr) |
570 | bgp_attr_unintern(&adj->attr); | |
3f9c7369 | 571 | |
d62a17ae | 572 | if (adj->adv) |
573 | bgp_advertise_clean_subgroup(subgrp, adj); | |
3f9c7369 | 574 | |
d62a17ae | 575 | BGP_ADJ_OUT_DEL(rn, adj); |
576 | adj_free(adj); | |
3f9c7369 DS |
577 | } |
578 | ||
579 | /* | |
580 | * Go through all the routes and clean up the adj/adv structures corresponding | |
581 | * to the subgroup. | |
582 | */ | |
d62a17ae | 583 | void subgroup_clear_table(struct update_subgroup *subgrp) |
3f9c7369 | 584 | { |
d62a17ae | 585 | struct bgp_adj_out *aout, *taout; |
586 | ||
a2addae8 | 587 | SUBGRP_FOREACH_ADJ_SAFE (subgrp, aout, taout) { |
d62a17ae | 588 | struct bgp_node *rn = aout->rn; |
589 | bgp_adj_out_remove_subgroup(rn, aout, subgrp); | |
590 | bgp_unlock_node(rn); | |
591 | } | |
3f9c7369 DS |
592 | } |
593 | ||
594 | /* | |
595 | * subgroup_announce_table | |
596 | */ | |
d62a17ae | 597 | void subgroup_announce_table(struct update_subgroup *subgrp, |
598 | struct bgp_table *table) | |
3f9c7369 | 599 | { |
d62a17ae | 600 | struct bgp_node *rn; |
4b7e6066 | 601 | struct bgp_path_info *ri; |
d62a17ae | 602 | struct attr attr; |
603 | struct peer *peer; | |
604 | afi_t afi; | |
605 | safi_t safi; | |
606 | int addpath_capable; | |
607 | ||
608 | peer = SUBGRP_PEER(subgrp); | |
609 | afi = SUBGRP_AFI(subgrp); | |
610 | safi = SUBGRP_SAFI(subgrp); | |
611 | addpath_capable = bgp_addpath_encode_tx(peer, afi, safi); | |
612 | ||
613 | if (safi == SAFI_LABELED_UNICAST) | |
614 | safi = SAFI_UNICAST; | |
615 | ||
616 | if (!table) | |
617 | table = peer->bgp->rib[afi][safi]; | |
618 | ||
619 | if (safi != SAFI_MPLS_VPN && safi != SAFI_ENCAP && safi != SAFI_EVPN | |
620 | && CHECK_FLAG(peer->af_flags[afi][safi], | |
621 | PEER_FLAG_DEFAULT_ORIGINATE)) | |
622 | subgroup_default_originate(subgrp, 0); | |
623 | ||
624 | for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) | |
625 | for (ri = rn->info; ri; ri = ri->next) | |
626 | ||
1defdda8 | 627 | if (CHECK_FLAG(ri->flags, BGP_PATH_SELECTED) |
d62a17ae | 628 | || (addpath_capable |
dcc68b5e MS |
629 | && bgp_addpath_tx_path( |
630 | peer->addpath_type[afi][safi], | |
631 | ri))) { | |
d62a17ae | 632 | if (subgroup_announce_check(rn, ri, subgrp, |
633 | &rn->p, &attr)) | |
634 | bgp_adj_out_set_subgroup(rn, subgrp, | |
635 | &attr, ri); | |
636 | else | |
637 | bgp_adj_out_unset_subgroup( | |
638 | rn, subgrp, 1, | |
dcc68b5e MS |
639 | bgp_addpath_id_for_peer( |
640 | peer, afi, safi, | |
641 | &ri->tx_addpath)); | |
d62a17ae | 642 | } |
643 | ||
644 | /* | |
645 | * We walked through the whole table -- make sure our version number | |
646 | * is consistent with the one on the table. This should allow | |
647 | * subgroups to merge sooner if a peer comes up when the route node | |
648 | * with the largest version is no longer in the table. This also | |
649 | * covers the pathological case where all routes in the table have | |
650 | * now been deleted. | |
651 | */ | |
652 | subgrp->version = max(subgrp->version, table->version); | |
653 | ||
654 | /* | |
655 | * Start a task to merge the subgroup if necessary. | |
656 | */ | |
657 | update_subgroup_trigger_merge_check(subgrp, 0); | |
3f9c7369 DS |
658 | } |
659 | ||
660 | /* | |
661 | * subgroup_announce_route | |
662 | * | |
663 | * Refresh all routes out to a subgroup. | |
664 | */ | |
d62a17ae | 665 | void subgroup_announce_route(struct update_subgroup *subgrp) |
3f9c7369 | 666 | { |
d62a17ae | 667 | struct bgp_node *rn; |
668 | struct bgp_table *table; | |
669 | struct peer *onlypeer; | |
670 | ||
671 | if (update_subgroup_needs_refresh(subgrp)) { | |
672 | update_subgroup_set_needs_refresh(subgrp, 0); | |
673 | } | |
674 | ||
675 | /* | |
676 | * First update is deferred until ORF or ROUTE-REFRESH is received | |
677 | */ | |
678 | onlypeer = ((SUBGRP_PCOUNT(subgrp) == 1) ? (SUBGRP_PFIRST(subgrp))->peer | |
679 | : NULL); | |
9d303b37 DL |
680 | if (onlypeer && CHECK_FLAG(onlypeer->af_sflags[SUBGRP_AFI(subgrp)] |
681 | [SUBGRP_SAFI(subgrp)], | |
682 | PEER_STATUS_ORF_WAIT_REFRESH)) | |
d62a17ae | 683 | return; |
684 | ||
685 | if (SUBGRP_SAFI(subgrp) != SAFI_MPLS_VPN | |
686 | && SUBGRP_SAFI(subgrp) != SAFI_ENCAP | |
687 | && SUBGRP_SAFI(subgrp) != SAFI_EVPN) | |
688 | subgroup_announce_table(subgrp, NULL); | |
689 | else | |
690 | for (rn = bgp_table_top(update_subgroup_rib(subgrp)); rn; | |
691 | rn = bgp_route_next(rn)) | |
692 | if ((table = (rn->info)) != NULL) | |
693 | subgroup_announce_table(subgrp, table); | |
3f9c7369 DS |
694 | } |
695 | ||
d62a17ae | 696 | void subgroup_default_originate(struct update_subgroup *subgrp, int withdraw) |
3f9c7369 | 697 | { |
d62a17ae | 698 | struct bgp *bgp; |
e50c68b2 DS |
699 | struct attr attr; |
700 | struct aspath *aspath; | |
701 | struct bgp_path_info tmp_info; | |
d62a17ae | 702 | struct prefix p; |
703 | struct peer *from; | |
704 | struct bgp_node *rn; | |
4b7e6066 | 705 | struct bgp_path_info *ri; |
d62a17ae | 706 | struct peer *peer; |
707 | int ret = RMAP_DENYMATCH; | |
708 | afi_t afi; | |
709 | safi_t safi; | |
710 | ||
711 | if (!subgrp) | |
712 | return; | |
713 | ||
714 | peer = SUBGRP_PEER(subgrp); | |
715 | afi = SUBGRP_AFI(subgrp); | |
716 | safi = SUBGRP_SAFI(subgrp); | |
717 | ||
718 | if (!(afi == AFI_IP || afi == AFI_IP6)) | |
719 | return; | |
720 | ||
721 | bgp = peer->bgp; | |
722 | from = bgp->peer_self; | |
723 | ||
e50c68b2 DS |
724 | bgp_attr_default_set(&attr, BGP_ORIGIN_IGP); |
725 | aspath = attr.aspath; | |
726 | ||
727 | attr.local_pref = bgp->default_local_pref; | |
d62a17ae | 728 | |
cbb65f5e RW |
729 | memset(&p, 0, sizeof(p)); |
730 | p.family = afi2family(afi); | |
731 | p.prefixlen = 0; | |
d62a17ae | 732 | |
e50c68b2 DS |
733 | if ((afi == AFI_IP6) || peer_cap_enhe(peer, afi, safi)) { |
734 | /* IPv6 global nexthop must be included. */ | |
735 | attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV6_GLOBAL; | |
736 | ||
737 | /* If the peer is on shared nextwork and we have link-local | |
738 | nexthop set it. */ | |
739 | if (peer->shared_network | |
740 | && !IN6_IS_ADDR_UNSPECIFIED(&peer->nexthop.v6_local)) | |
741 | attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL; | |
742 | } | |
743 | ||
d62a17ae | 744 | if (peer->default_rmap[afi][safi].name) { |
745 | SET_FLAG(bgp->peer_self->rmap_type, PEER_RMAP_TYPE_DEFAULT); | |
746 | for (rn = bgp_table_top(bgp->rib[afi][safi]); rn; | |
747 | rn = bgp_route_next(rn)) { | |
748 | for (ri = rn->info; ri; ri = ri->next) { | |
e50c68b2 DS |
749 | struct attr dummy_attr; |
750 | ||
751 | /* Provide dummy so the route-map can't modify | |
752 | * the attributes */ | |
753 | bgp_attr_dup(&dummy_attr, ri->attr); | |
74401e62 | 754 | tmp_info.peer = ri->peer; |
e50c68b2 | 755 | tmp_info.attr = &dummy_attr; |
d62a17ae | 756 | |
757 | ret = route_map_apply( | |
758 | peer->default_rmap[afi][safi].map, | |
74401e62 DA |
759 | &rn->p, RMAP_BGP, &tmp_info); |
760 | ||
e50c68b2 DS |
761 | /* The route map might have set attributes. If |
762 | * we don't flush them | |
763 | * here, they will be leaked. */ | |
764 | bgp_attr_flush(&dummy_attr); | |
d62a17ae | 765 | if (ret != RMAP_DENYMATCH) |
766 | break; | |
767 | } | |
768 | if (ret != RMAP_DENYMATCH) | |
769 | break; | |
770 | } | |
771 | bgp->peer_self->rmap_type = 0; | |
772 | ||
773 | if (ret == RMAP_DENYMATCH) | |
774 | withdraw = 1; | |
3f9c7369 | 775 | } |
3f9c7369 | 776 | |
d62a17ae | 777 | if (withdraw) { |
778 | if (CHECK_FLAG(subgrp->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE)) | |
779 | subgroup_default_withdraw_packet(subgrp); | |
780 | UNSET_FLAG(subgrp->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE); | |
781 | } else { | |
782 | if (!CHECK_FLAG(subgrp->sflags, | |
783 | SUBGRP_STATUS_DEFAULT_ORIGINATE)) { | |
7f323236 DW |
784 | |
785 | if (bgp_flag_check(bgp, BGP_FLAG_GRACEFUL_SHUTDOWN)) { | |
e50c68b2 | 786 | bgp_attr_add_gshut_community(&attr); |
7f323236 DW |
787 | } |
788 | ||
d62a17ae | 789 | SET_FLAG(subgrp->sflags, |
790 | SUBGRP_STATUS_DEFAULT_ORIGINATE); | |
e50c68b2 | 791 | subgroup_default_update_packet(subgrp, &attr, from); |
d62a17ae | 792 | |
793 | /* The 'neighbor x.x.x.x default-originate' default will | |
794 | * act as an | |
795 | * implicit withdraw for any previous UPDATEs sent for | |
796 | * 0.0.0.0/0 so | |
797 | * clear adj_out for the 0.0.0.0/0 prefix in the BGP | |
798 | * table. | |
799 | */ | |
cbb65f5e RW |
800 | memset(&p, 0, sizeof(p)); |
801 | p.family = afi2family(afi); | |
802 | p.prefixlen = 0; | |
d62a17ae | 803 | |
804 | rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi, | |
805 | &p, NULL); | |
806 | bgp_adj_out_unset_subgroup( | |
807 | rn, subgrp, 0, | |
808 | BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE); | |
809 | } | |
810 | } | |
e50c68b2 DS |
811 | |
812 | aspath_unintern(&aspath); | |
3f9c7369 DS |
813 | } |
814 | ||
815 | /* | |
816 | * Announce the BGP table to a subgroup. | |
817 | * | |
818 | * At startup, we try to optimize route announcement by coalescing the | |
819 | * peer-up events. This is done only the first time - from then on, | |
820 | * subgrp->v_coalesce will be set to zero and the normal logic | |
821 | * prevails. | |
822 | */ | |
d62a17ae | 823 | void subgroup_announce_all(struct update_subgroup *subgrp) |
3f9c7369 | 824 | { |
d62a17ae | 825 | if (!subgrp) |
826 | return; | |
827 | ||
828 | /* | |
829 | * If coalesce timer value is not set, announce routes immediately. | |
830 | */ | |
831 | if (!subgrp->v_coalesce) { | |
832 | if (bgp_debug_update(NULL, NULL, subgrp->update_group, 0)) | |
833 | zlog_debug("u%" PRIu64 ":s%" PRIu64 | |
834 | " announcing all routes", | |
835 | subgrp->update_group->id, subgrp->id); | |
836 | subgroup_announce_route(subgrp); | |
837 | return; | |
838 | } | |
839 | ||
840 | /* | |
841 | * We should wait for the coalesce timer. Arm the timer if not done. | |
842 | */ | |
843 | if (!subgrp->t_coalesce) { | |
844 | thread_add_timer_msec(bm->master, subgroup_coalesce_timer, | |
845 | subgrp, subgrp->v_coalesce, | |
846 | &subgrp->t_coalesce); | |
847 | } | |
3f9c7369 DS |
848 | } |
849 | ||
850 | /* | |
851 | * Go through all update subgroups and set up the adv queue for the | |
852 | * input route. | |
853 | */ | |
d62a17ae | 854 | void group_announce_route(struct bgp *bgp, afi_t afi, safi_t safi, |
40381db7 | 855 | struct bgp_node *rn, struct bgp_path_info *pi) |
3f9c7369 | 856 | { |
d62a17ae | 857 | struct updwalk_context ctx; |
40381db7 | 858 | ctx.pi = pi; |
d62a17ae | 859 | ctx.rn = rn; |
860 | update_group_af_walk(bgp, afi, safi, group_announce_route_walkcb, &ctx); | |
3f9c7369 DS |
861 | } |
862 | ||
d62a17ae | 863 | void update_group_show_adj_queue(struct bgp *bgp, afi_t afi, safi_t safi, |
864 | struct vty *vty, uint64_t id) | |
3f9c7369 | 865 | { |
d62a17ae | 866 | updgrp_show_adj(bgp, afi, safi, vty, id, UPDWALK_FLAGS_ADVQUEUE); |
3f9c7369 DS |
867 | } |
868 | ||
d62a17ae | 869 | void update_group_show_advertised(struct bgp *bgp, afi_t afi, safi_t safi, |
870 | struct vty *vty, uint64_t id) | |
3f9c7369 | 871 | { |
d62a17ae | 872 | updgrp_show_adj(bgp, afi, safi, vty, id, UPDWALK_FLAGS_ADVERTISED); |
3f9c7369 DS |
873 | } |
874 | ||
d62a17ae | 875 | void update_group_announce(struct bgp *bgp) |
3f9c7369 | 876 | { |
d62a17ae | 877 | update_group_walk(bgp, update_group_announce_walkcb, NULL); |
3f9c7369 DS |
878 | } |
879 | ||
d62a17ae | 880 | void update_group_announce_rrclients(struct bgp *bgp) |
3f9c7369 | 881 | { |
d62a17ae | 882 | update_group_walk(bgp, update_group_announce_rrc_walkcb, NULL); |
3f9c7369 | 883 | } |