]>
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 | * | |
24 | * You should have received a copy of the GNU General Public License | |
25 | * along with GNU Zebra; see the file COPYING. If not, write to the Free | |
26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
27 | * 02111-1307, USA. | |
28 | */ | |
29 | ||
30 | #include <zebra.h> | |
31 | ||
32 | #include "command.h" | |
33 | #include "memory.h" | |
34 | #include "prefix.h" | |
35 | #include "hash.h" | |
36 | #include "thread.h" | |
37 | #include "queue.h" | |
38 | #include "routemap.h" | |
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 | ||
06370dac DW |
169 | /* Process the bestpath last so the "show ip bgp neighbor x.x.x.x advertised" |
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 | { | |
229 | vty_out (vty, | |
ffd0c037 | 230 | "BGP table version is %" PRIu64 ", local router ID is %s%s", |
3f9c7369 DS |
231 | table->version, inet_ntoa (bgp->router_id), |
232 | VTY_NEWLINE); | |
233 | vty_out (vty, BGP_SHOW_SCODE_HEADER, VTY_NEWLINE, VTY_NEWLINE); | |
234 | vty_out (vty, BGP_SHOW_OCODE_HEADER, VTY_NEWLINE, VTY_NEWLINE); | |
235 | header1 = 0; | |
236 | } | |
237 | if (header2) | |
238 | { | |
239 | vty_out (vty, BGP_SHOW_HEADER, VTY_NEWLINE); | |
240 | header2 = 0; | |
241 | } | |
242 | if ((flags & UPDWALK_FLAGS_ADVQUEUE) && adj->adv && adj->adv->baa) | |
243 | { | |
856ca177 | 244 | route_vty_out_tmp (vty, &rn->p, adj->adv->baa->attr, SUBGRP_SAFI (subgrp), 0, NULL); |
3f9c7369 DS |
245 | output_count++; |
246 | } | |
247 | if ((flags & UPDWALK_FLAGS_ADVERTISED) && adj->attr) | |
248 | { | |
856ca177 | 249 | route_vty_out_tmp (vty, &rn->p, adj->attr, SUBGRP_SAFI (subgrp), 0, NULL); |
3f9c7369 DS |
250 | output_count++; |
251 | } | |
252 | } | |
253 | if (output_count != 0) | |
254 | vty_out (vty, "%sTotal number of prefixes %ld%s", | |
255 | VTY_NEWLINE, output_count, VTY_NEWLINE); | |
256 | } | |
257 | ||
258 | static int | |
259 | updgrp_show_adj_walkcb (struct update_group *updgrp, void *arg) | |
260 | { | |
261 | struct updwalk_context *ctx = arg; | |
262 | struct update_subgroup *subgrp; | |
263 | struct vty *vty; | |
264 | ||
265 | vty = ctx->vty; | |
266 | UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) | |
267 | { | |
268 | if (ctx->subgrp_id && (ctx->subgrp_id != subgrp->id)) | |
269 | continue; | |
ffd0c037 | 270 | vty_out (vty, "update group %" PRIu64 ", subgroup %" PRIu64 "%s", updgrp->id, |
3f9c7369 DS |
271 | subgrp->id, VTY_NEWLINE); |
272 | subgrp_show_adjq_vty (subgrp, vty, ctx->flags); | |
273 | } | |
274 | return UPDWALK_CONTINUE; | |
275 | } | |
276 | ||
277 | static void | |
278 | updgrp_show_adj (struct bgp *bgp, afi_t afi, safi_t safi, | |
279 | struct vty *vty, u_int64_t id, u_int8_t flags) | |
280 | { | |
281 | struct updwalk_context ctx; | |
282 | memset (&ctx, 0, sizeof (ctx)); | |
283 | ctx.vty = vty; | |
284 | ctx.subgrp_id = id; | |
285 | ctx.flags = flags; | |
286 | ||
287 | update_group_af_walk (bgp, afi, safi, updgrp_show_adj_walkcb, &ctx); | |
288 | } | |
289 | ||
290 | static int | |
291 | subgroup_coalesce_timer (struct thread *thread) | |
292 | { | |
293 | struct update_subgroup *subgrp; | |
294 | ||
295 | subgrp = THREAD_ARG (thread); | |
296 | if (bgp_debug_update(NULL, NULL, subgrp->update_group, 0)) | |
ffd0c037 | 297 | zlog_debug ("u%" PRIu64 ":s%" PRIu64 " announcing routes upon coalesce timer expiry", |
3f9c7369 DS |
298 | (SUBGRP_UPDGRP (subgrp))->id, subgrp->id); |
299 | subgrp->t_coalesce = NULL; | |
300 | subgrp->v_coalesce = 0; | |
301 | subgroup_announce_route (subgrp); | |
302 | ||
303 | ||
304 | /* While the announce_route() may kick off the route advertisement timer for | |
305 | * the members of the subgroup, we'd like to send the initial updates much | |
306 | * faster (i.e., without enforcing MRAI). Also, if there were no routes to | |
307 | * announce, this is the method currently employed to trigger the EOR. | |
308 | */ | |
309 | if (!bgp_update_delay_active(SUBGRP_INST(subgrp))) | |
310 | { | |
311 | struct peer_af *paf; | |
312 | struct peer *peer; | |
313 | ||
314 | SUBGRP_FOREACH_PEER (subgrp, paf) | |
315 | { | |
316 | peer = PAF_PEER(paf); | |
317 | BGP_TIMER_OFF(peer->t_routeadv); | |
318 | BGP_TIMER_ON (peer->t_routeadv, bgp_routeadv_timer, 0); | |
319 | } | |
320 | } | |
321 | ||
322 | return 0; | |
323 | } | |
324 | ||
325 | static int | |
326 | update_group_announce_walkcb (struct update_group *updgrp, void *arg) | |
327 | { | |
328 | struct update_subgroup *subgrp; | |
329 | ||
330 | UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) | |
331 | { | |
332 | subgroup_announce_all (subgrp); | |
333 | } | |
334 | ||
335 | return UPDWALK_CONTINUE; | |
336 | } | |
337 | ||
338 | static int | |
339 | update_group_announce_rrc_walkcb (struct update_group *updgrp, void *arg) | |
340 | { | |
341 | struct update_subgroup *subgrp; | |
342 | afi_t afi; | |
343 | safi_t safi; | |
344 | struct peer *peer; | |
345 | ||
346 | afi = UPDGRP_AFI (updgrp); | |
347 | safi = UPDGRP_SAFI (updgrp); | |
348 | peer = UPDGRP_PEER (updgrp); | |
349 | ||
350 | /* Only announce if this is a group of route-reflector-clients */ | |
351 | if (CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_REFLECTOR_CLIENT)) | |
352 | { | |
353 | UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) | |
354 | { | |
355 | subgroup_announce_all (subgrp); | |
356 | } | |
357 | } | |
358 | ||
359 | return UPDWALK_CONTINUE; | |
360 | } | |
361 | ||
362 | /******************** | |
363 | * PUBLIC FUNCTIONS | |
364 | ********************/ | |
365 | ||
366 | /** | |
367 | * Allocate an adj-out object. Do proper initialization of its fields, | |
368 | * primarily its association with the subgroup and the prefix. | |
369 | */ | |
370 | struct bgp_adj_out * | |
adbac85e DW |
371 | bgp_adj_out_alloc (struct update_subgroup *subgrp, struct bgp_node *rn, |
372 | u_int32_t addpath_tx_id) | |
3f9c7369 DS |
373 | { |
374 | struct bgp_adj_out *adj; | |
375 | ||
376 | adj = XCALLOC (MTYPE_BGP_ADJ_OUT, sizeof (struct bgp_adj_out)); | |
377 | adj->subgroup = subgrp; | |
378 | if (rn) | |
379 | { | |
380 | BGP_ADJ_OUT_ADD (rn, adj); | |
381 | bgp_lock_node (rn); | |
382 | adj->rn = rn; | |
383 | } | |
adbac85e DW |
384 | |
385 | adj->addpath_tx_id = addpath_tx_id; | |
3f9c7369 DS |
386 | TAILQ_INSERT_TAIL (&(subgrp->adjq), adj, subgrp_adj_train); |
387 | SUBGRP_INCR_STAT (subgrp, adj_count); | |
388 | return adj; | |
389 | } | |
390 | ||
391 | ||
392 | struct bgp_advertise * | |
393 | bgp_advertise_clean_subgroup (struct update_subgroup *subgrp, | |
394 | struct bgp_adj_out *adj) | |
395 | { | |
396 | struct bgp_advertise *adv; | |
397 | struct bgp_advertise_attr *baa; | |
398 | struct bgp_advertise *next; | |
399 | struct bgp_advertise_fifo *fhead; | |
400 | ||
401 | adv = adj->adv; | |
402 | baa = adv->baa; | |
403 | next = NULL; | |
404 | ||
405 | if (baa) | |
406 | { | |
407 | fhead = &subgrp->sync->update; | |
408 | ||
409 | /* Unlink myself from advertise attribute FIFO. */ | |
410 | bgp_advertise_delete (baa, adv); | |
411 | ||
412 | /* Fetch next advertise candidate. */ | |
413 | next = baa->adv; | |
414 | ||
415 | /* Unintern BGP advertise attribute. */ | |
416 | bgp_advertise_unintern (subgrp->hash, baa); | |
417 | } | |
418 | else | |
419 | fhead = &subgrp->sync->withdraw; | |
420 | ||
421 | ||
422 | /* Unlink myself from advertisement FIFO. */ | |
423 | BGP_ADV_FIFO_DEL (fhead, adv); | |
424 | ||
425 | /* Free memory. */ | |
426 | bgp_advertise_free (adj->adv); | |
427 | adj->adv = NULL; | |
428 | ||
429 | return next; | |
430 | } | |
431 | ||
432 | void | |
433 | bgp_adj_out_set_subgroup (struct bgp_node *rn, | |
434 | struct update_subgroup *subgrp, | |
435 | struct attr *attr, struct bgp_info *binfo) | |
436 | { | |
437 | struct bgp_adj_out *adj = NULL; | |
438 | struct bgp_advertise *adv; | |
439 | ||
440 | if (DISABLE_BGP_ANNOUNCE) | |
441 | return; | |
442 | ||
443 | /* Look for adjacency information. */ | |
adbac85e | 444 | adj = adj_lookup (rn, subgrp, binfo->addpath_tx_id); |
3f9c7369 DS |
445 | |
446 | if (!adj) | |
447 | { | |
adbac85e | 448 | adj = bgp_adj_out_alloc (subgrp, rn, binfo->addpath_tx_id); |
3f9c7369 DS |
449 | if (!adj) |
450 | return; | |
451 | } | |
452 | ||
453 | if (adj->adv) | |
454 | bgp_advertise_clean_subgroup (subgrp, adj); | |
455 | adj->adv = bgp_advertise_new (); | |
456 | ||
457 | adv = adj->adv; | |
458 | adv->rn = rn; | |
459 | assert (adv->binfo == NULL); | |
460 | adv->binfo = bgp_info_lock (binfo); /* bgp_info adj_out reference */ | |
461 | ||
462 | if (attr) | |
463 | adv->baa = bgp_advertise_intern (subgrp->hash, attr); | |
464 | else | |
465 | adv->baa = baa_new (); | |
466 | adv->adj = adj; | |
467 | ||
468 | /* Add new advertisement to advertisement attribute list. */ | |
469 | bgp_advertise_add (adv->baa, adv); | |
470 | ||
471 | /* | |
472 | * If the update adv list is empty, trigger the member peers' | |
473 | * mrai timers so the socket writes can happen. | |
474 | */ | |
475 | if (BGP_ADV_FIFO_EMPTY (&subgrp->sync->update)) | |
476 | { | |
477 | struct peer_af *paf; | |
478 | ||
479 | SUBGRP_FOREACH_PEER (subgrp, paf) | |
480 | { | |
481 | bgp_adjust_routeadv (PAF_PEER (paf)); | |
482 | } | |
483 | } | |
484 | ||
485 | BGP_ADV_FIFO_ADD (&subgrp->sync->update, &adv->fifo); | |
486 | ||
487 | subgrp->version = max (subgrp->version, rn->version); | |
488 | } | |
489 | ||
4125bb67 DS |
490 | /* The only time 'withdraw' will be false is if we are sending |
491 | * the "neighbor x.x.x.x default-originate" default and need to clear | |
492 | * bgp_adj_out for the 0.0.0.0/0 route in the BGP table. | |
493 | */ | |
3f9c7369 DS |
494 | void |
495 | bgp_adj_out_unset_subgroup (struct bgp_node *rn, | |
4125bb67 | 496 | struct update_subgroup *subgrp, |
adbac85e DW |
497 | char withdraw, |
498 | u_int32_t addpath_tx_id) | |
3f9c7369 DS |
499 | { |
500 | struct bgp_adj_out *adj; | |
501 | struct bgp_advertise *adv; | |
4125bb67 | 502 | char trigger_write; |
3f9c7369 DS |
503 | |
504 | if (DISABLE_BGP_ANNOUNCE) | |
505 | return; | |
506 | ||
adbac85e DW |
507 | /* Lookup existing adjacency */ |
508 | if ((adj = adj_lookup (rn, subgrp, addpath_tx_id)) != NULL) | |
509 | { | |
510 | /* Clean up previous advertisement. */ | |
511 | if (adj->adv) | |
512 | bgp_advertise_clean_subgroup (subgrp, adj); | |
3f9c7369 | 513 | |
adbac85e DW |
514 | if (adj->attr && withdraw) |
515 | { | |
516 | /* We need advertisement structure. */ | |
517 | adj->adv = bgp_advertise_new (); | |
518 | adv = adj->adv; | |
519 | adv->rn = rn; | |
520 | adv->adj = adj; | |
521 | ||
522 | /* Note if we need to trigger a packet write */ | |
523 | if (BGP_ADV_FIFO_EMPTY (&subgrp->sync->withdraw)) | |
524 | trigger_write = 1; | |
525 | else | |
526 | trigger_write = 0; | |
3f9c7369 | 527 | |
adbac85e DW |
528 | /* Add to synchronization entry for withdraw announcement. */ |
529 | BGP_ADV_FIFO_ADD (&subgrp->sync->withdraw, &adv->fifo); | |
3f9c7369 | 530 | |
adbac85e DW |
531 | /* Schedule packet write, if FIFO is getting its first entry. */ |
532 | if (trigger_write) | |
533 | subgroup_trigger_write(subgrp); | |
534 | } | |
4125bb67 | 535 | else |
adbac85e DW |
536 | { |
537 | /* Remove myself from adjacency. */ | |
538 | BGP_ADJ_OUT_DEL (rn, adj); | |
3f9c7369 | 539 | |
adbac85e DW |
540 | /* Free allocated information. */ |
541 | adj_free (adj); | |
3f9c7369 | 542 | |
adbac85e DW |
543 | bgp_unlock_node (rn); |
544 | } | |
3f9c7369 DS |
545 | } |
546 | ||
3f9c7369 DS |
547 | subgrp->version = max (subgrp->version, rn->version); |
548 | } | |
549 | ||
550 | void | |
551 | bgp_adj_out_remove_subgroup (struct bgp_node *rn, struct bgp_adj_out *adj, | |
552 | struct update_subgroup *subgrp) | |
553 | { | |
554 | if (adj->attr) | |
555 | bgp_attr_unintern (&adj->attr); | |
556 | ||
557 | if (adj->adv) | |
558 | bgp_advertise_clean_subgroup (subgrp, adj); | |
559 | ||
560 | BGP_ADJ_OUT_DEL (rn, adj); | |
561 | adj_free (adj); | |
562 | } | |
563 | ||
564 | /* | |
565 | * Go through all the routes and clean up the adj/adv structures corresponding | |
566 | * to the subgroup. | |
567 | */ | |
568 | void | |
569 | subgroup_clear_table (struct update_subgroup *subgrp) | |
570 | { | |
571 | struct bgp_adj_out *aout, *taout; | |
572 | ||
573 | SUBGRP_FOREACH_ADJ_SAFE (subgrp, aout, taout) | |
574 | { | |
575 | bgp_unlock_node (aout->rn); | |
576 | bgp_adj_out_remove_subgroup (aout->rn, aout, subgrp); | |
577 | } | |
578 | } | |
579 | ||
580 | /* | |
581 | * subgroup_announce_table | |
582 | */ | |
583 | void | |
584 | subgroup_announce_table (struct update_subgroup *subgrp, | |
2a3d5731 | 585 | struct bgp_table *table) |
3f9c7369 DS |
586 | { |
587 | struct bgp_node *rn; | |
588 | struct bgp_info *ri; | |
589 | struct attr attr; | |
590 | struct attr_extra extra; | |
591 | struct peer *peer; | |
3f9c7369 DS |
592 | afi_t afi; |
593 | safi_t safi; | |
adbac85e | 594 | int addpath_capable; |
3f9c7369 DS |
595 | |
596 | peer = SUBGRP_PEER (subgrp); | |
597 | afi = SUBGRP_AFI (subgrp); | |
598 | safi = SUBGRP_SAFI (subgrp); | |
adbac85e | 599 | addpath_capable = bgp_addpath_encode_tx (peer, afi, safi); |
3f9c7369 | 600 | |
3f9c7369 | 601 | if (!table) |
2a3d5731 | 602 | table = peer->bgp->rib[afi][safi]; |
3f9c7369 DS |
603 | |
604 | if (safi != SAFI_MPLS_VPN | |
587ff0fd | 605 | && safi != SAFI_ENCAP |
3f9c7369 DS |
606 | && CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_DEFAULT_ORIGINATE)) |
607 | subgroup_default_originate (subgrp, 0); | |
608 | ||
2a3d5731 | 609 | /* It's initialized in bgp_announce_check() */ |
3f9c7369 DS |
610 | attr.extra = &extra; |
611 | ||
612 | for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn)) | |
613 | for (ri = rn->info; ri; ri = ri->next) | |
614 | ||
adbac85e | 615 | if (CHECK_FLAG (ri->flags, BGP_INFO_SELECTED) || |
06370dac | 616 | (addpath_capable && bgp_addpath_tx_path(peer, afi, safi, ri))) |
3f9c7369 | 617 | { |
2a3d5731 | 618 | if (subgroup_announce_check (ri, subgrp, &rn->p, &attr)) |
3f9c7369 DS |
619 | bgp_adj_out_set_subgroup (rn, subgrp, &attr, ri); |
620 | else | |
adbac85e | 621 | bgp_adj_out_unset_subgroup (rn, subgrp, 1, ri->addpath_tx_id); |
3f9c7369 DS |
622 | } |
623 | ||
624 | /* | |
625 | * We walked through the whole table -- make sure our version number | |
626 | * is consistent with the one on the table. This should allow | |
627 | * subgroups to merge sooner if a peer comes up when the route node | |
628 | * with the largest version is no longer in the table. This also | |
629 | * covers the pathological case where all routes in the table have | |
630 | * now been deleted. | |
631 | */ | |
632 | subgrp->version = max (subgrp->version, table->version); | |
633 | ||
634 | /* | |
635 | * Start a task to merge the subgroup if necessary. | |
636 | */ | |
637 | update_subgroup_trigger_merge_check (subgrp, 0); | |
638 | } | |
639 | ||
640 | /* | |
641 | * subgroup_announce_route | |
642 | * | |
643 | * Refresh all routes out to a subgroup. | |
644 | */ | |
645 | void | |
646 | subgroup_announce_route (struct update_subgroup *subgrp) | |
647 | { | |
648 | struct bgp_node *rn; | |
649 | struct bgp_table *table; | |
650 | struct peer *onlypeer; | |
3f9c7369 DS |
651 | |
652 | if (update_subgroup_needs_refresh (subgrp)) | |
653 | { | |
654 | update_subgroup_set_needs_refresh (subgrp, 0); | |
655 | } | |
656 | ||
657 | /* | |
658 | * First update is deferred until ORF or ROUTE-REFRESH is received | |
659 | */ | |
660 | onlypeer = ((SUBGRP_PCOUNT (subgrp) == 1) ? | |
661 | (SUBGRP_PFIRST (subgrp))->peer : NULL); | |
662 | if (onlypeer && | |
663 | CHECK_FLAG (onlypeer-> | |
664 | af_sflags[SUBGRP_AFI (subgrp)][SUBGRP_SAFI (subgrp)], | |
665 | PEER_STATUS_ORF_WAIT_REFRESH)) | |
666 | return; | |
667 | ||
587ff0fd LB |
668 | if (SUBGRP_SAFI (subgrp) != SAFI_MPLS_VPN && |
669 | SUBGRP_SAFI (subgrp) != SAFI_ENCAP) | |
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); | |
712 | #ifdef HAVE_IPV6 | |
713 | else if (afi == AFI_IP6) | |
714 | { | |
715 | struct attr_extra *ae = attr.extra; | |
716 | ||
717 | str2prefix ("::/0", &p); | |
718 | ||
719 | /* IPv6 global nexthop must be included. */ | |
801a9bcc | 720 | ae->mp_nexthop_len = BGP_ATTR_NHLEN_IPV6_GLOBAL; |
3f9c7369 DS |
721 | |
722 | /* If the peer is on shared nextwork and we have link-local | |
723 | nexthop set it. */ | |
724 | if (peer->shared_network | |
725 | && !IN6_IS_ADDR_UNSPECIFIED (&peer->nexthop.v6_local)) | |
3fb387f5 | 726 | ae->mp_nexthop_len = BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL; |
3f9c7369 DS |
727 | } |
728 | #endif /* HAVE_IPV6 */ | |
729 | ||
730 | if (peer->default_rmap[afi][safi].name) | |
731 | { | |
732 | SET_FLAG (bgp->peer_self->rmap_type, PEER_RMAP_TYPE_DEFAULT); | |
733 | for (rn = bgp_table_top (bgp->rib[afi][safi]); rn; | |
734 | rn = bgp_route_next (rn)) | |
735 | { | |
736 | for (ri = rn->info; ri; ri = ri->next) | |
737 | { | |
738 | struct attr dummy_attr; | |
739 | struct attr_extra dummy_extra; | |
740 | struct bgp_info info; | |
741 | ||
742 | /* Provide dummy so the route-map can't modify the attributes */ | |
743 | dummy_attr.extra = &dummy_extra; | |
744 | bgp_attr_dup (&dummy_attr, ri->attr); | |
745 | info.peer = ri->peer; | |
746 | info.attr = &dummy_attr; | |
747 | ||
748 | ret = | |
749 | route_map_apply (peer->default_rmap[afi][safi].map, &rn->p, | |
750 | RMAP_BGP, &info); | |
751 | ||
752 | /* The route map might have set attributes. If we don't flush them | |
753 | * here, they will be leaked. */ | |
754 | bgp_attr_flush (&dummy_attr); | |
755 | if (ret != RMAP_DENYMATCH) | |
756 | break; | |
757 | } | |
758 | if (ret != RMAP_DENYMATCH) | |
759 | break; | |
760 | } | |
761 | bgp->peer_self->rmap_type = 0; | |
762 | ||
763 | if (ret == RMAP_DENYMATCH) | |
764 | withdraw = 1; | |
765 | } | |
766 | ||
767 | if (withdraw) | |
768 | { | |
769 | if (CHECK_FLAG (subgrp->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE)) | |
770 | subgroup_default_withdraw_packet (subgrp); | |
771 | UNSET_FLAG (subgrp->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE); | |
772 | } | |
773 | else | |
774 | { | |
775 | if (!CHECK_FLAG (subgrp->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE)) | |
776 | { | |
777 | SET_FLAG (subgrp->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE); | |
778 | subgroup_default_update_packet (subgrp, &attr, from); | |
4125bb67 DS |
779 | |
780 | /* The 'neighbor x.x.x.x default-originate' default will act as an | |
781 | * implicit withdraw for any previous UPDATEs sent for 0.0.0.0/0 so | |
782 | * clear adj_out for the 0.0.0.0/0 prefix in the BGP table. | |
783 | */ | |
784 | if (afi == AFI_IP) | |
785 | str2prefix ("0.0.0.0/0", &p); | |
786 | #ifdef HAVE_IPV6 | |
787 | else | |
788 | str2prefix ("::/0", &p); | |
789 | #endif /* HAVE_IPV6 */ | |
790 | ||
791 | rn = bgp_afi_node_get (bgp->rib[afi][safi], afi, safi, &p, NULL); | |
adbac85e | 792 | bgp_adj_out_unset_subgroup (rn, subgrp, 0, BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE); |
3f9c7369 DS |
793 | } |
794 | } | |
795 | ||
796 | bgp_attr_extra_free (&attr); | |
797 | aspath_unintern (&aspath); | |
798 | } | |
799 | ||
800 | /* | |
801 | * Announce the BGP table to a subgroup. | |
802 | * | |
803 | * At startup, we try to optimize route announcement by coalescing the | |
804 | * peer-up events. This is done only the first time - from then on, | |
805 | * subgrp->v_coalesce will be set to zero and the normal logic | |
806 | * prevails. | |
807 | */ | |
808 | void | |
809 | subgroup_announce_all (struct update_subgroup *subgrp) | |
810 | { | |
811 | if (!subgrp) | |
812 | return; | |
813 | ||
814 | /* | |
815 | * If coalesce timer value is not set, announce routes immediately. | |
816 | */ | |
817 | if (!subgrp->v_coalesce) | |
818 | { | |
819 | if (bgp_debug_update(NULL, NULL, subgrp->update_group, 0)) | |
ffd0c037 | 820 | zlog_debug ("u%" PRIu64 ":s%" PRIu64 " announcing all routes", |
3f9c7369 DS |
821 | subgrp->update_group->id, subgrp->id); |
822 | subgroup_announce_route (subgrp); | |
823 | return; | |
824 | } | |
825 | ||
826 | /* | |
827 | * We should wait for the coalesce timer. Arm the timer if not done. | |
828 | */ | |
829 | if (!subgrp->t_coalesce) | |
830 | { | |
9229d914 | 831 | THREAD_TIMER_MSEC_ON (bm->master, subgrp->t_coalesce, subgroup_coalesce_timer, |
3f9c7369 DS |
832 | subgrp, subgrp->v_coalesce); |
833 | } | |
834 | } | |
835 | ||
836 | /* | |
837 | * Go through all update subgroups and set up the adv queue for the | |
838 | * input route. | |
839 | */ | |
840 | void | |
841 | group_announce_route (struct bgp *bgp, afi_t afi, safi_t safi, | |
842 | struct bgp_node *rn, struct bgp_info *ri) | |
843 | { | |
844 | struct updwalk_context ctx; | |
845 | ctx.ri = ri; | |
846 | ctx.rn = rn; | |
847 | update_group_af_walk (bgp, afi, safi, group_announce_route_walkcb, &ctx); | |
848 | } | |
849 | ||
850 | void | |
851 | update_group_show_adj_queue (struct bgp *bgp, afi_t afi, safi_t safi, | |
852 | struct vty *vty, u_int64_t id) | |
853 | { | |
854 | updgrp_show_adj (bgp, afi, safi, vty, id, UPDWALK_FLAGS_ADVQUEUE); | |
855 | } | |
856 | ||
857 | void | |
858 | update_group_show_advertised (struct bgp *bgp, afi_t afi, safi_t safi, | |
859 | struct vty *vty, u_int64_t id) | |
860 | { | |
861 | updgrp_show_adj (bgp, afi, safi, vty, id, UPDWALK_FLAGS_ADVERTISED); | |
862 | } | |
863 | ||
864 | void | |
865 | update_group_announce (struct bgp *bgp) | |
866 | { | |
867 | update_group_walk (bgp, update_group_announce_walkcb, NULL); | |
868 | } | |
869 | ||
870 | void | |
871 | update_group_announce_rrclients (struct bgp *bgp) | |
872 | { | |
873 | update_group_walk (bgp, update_group_announce_rrc_walkcb, NULL); | |
874 | } |