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