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