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