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