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