]>
Commit | Line | Data |
---|---|---|
3f9c7369 DS |
1 | /** |
2 | * bgp_updgrp_adv.c: BGP update group advertisement and adjacency | |
3 | * maintenance | |
4 | * | |
5 | * | |
6 | * @copyright Copyright (C) 2014 Cumulus Networks, Inc. | |
7 | * | |
8 | * @author Avneesh Sachdev <avneesh@sproute.net> | |
9 | * @author Rajesh Varadarajan <rajesh@sproute.net> | |
10 | * @author Pradosh Mohapatra <pradosh@sproute.net> | |
11 | * | |
12 | * This file is part of GNU Zebra. | |
13 | * | |
14 | * GNU Zebra is free software; you can redistribute it and/or modify it | |
15 | * under the terms of the GNU General Public License as published by the | |
16 | * Free Software Foundation; either version 2, or (at your option) any | |
17 | * later version. | |
18 | * | |
19 | * GNU Zebra is distributed in the hope that it will be useful, but | |
20 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
22 | * General Public License for more details. | |
23 | * | |
896014f4 DL |
24 | * You should have received a copy of the GNU General Public License along |
25 | * with this program; see the file COPYING; if not, write to the Free Software | |
26 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
3f9c7369 DS |
27 | */ |
28 | ||
29 | #include <zebra.h> | |
30 | ||
31 | #include "command.h" | |
32 | #include "memory.h" | |
33 | #include "prefix.h" | |
34 | #include "hash.h" | |
35 | #include "thread.h" | |
36 | #include "queue.h" | |
37 | #include "routemap.h" | |
039f3a34 | 38 | #include "filter.h" |
3f9c7369 DS |
39 | |
40 | #include "bgpd/bgpd.h" | |
41 | #include "bgpd/bgp_table.h" | |
42 | #include "bgpd/bgp_debug.h" | |
43 | #include "bgpd/bgp_route.h" | |
44 | #include "bgpd/bgp_advertise.h" | |
45 | #include "bgpd/bgp_attr.h" | |
46 | #include "bgpd/bgp_aspath.h" | |
47 | #include "bgpd/bgp_packet.h" | |
48 | #include "bgpd/bgp_fsm.h" | |
49 | #include "bgpd/bgp_mplsvpn.h" | |
50 | #include "bgpd/bgp_updgrp.h" | |
51 | #include "bgpd/bgp_advertise.h" | |
52 | ||
53 | ||
54 | /******************** | |
55 | * PRIVATE FUNCTIONS | |
56 | ********************/ | |
57 | ||
58 | static inline struct bgp_adj_out * | |
adbac85e DW |
59 | adj_lookup (struct bgp_node *rn, struct update_subgroup *subgrp, |
60 | u_int32_t addpath_tx_id) | |
3f9c7369 DS |
61 | { |
62 | struct bgp_adj_out *adj; | |
adbac85e DW |
63 | struct peer *peer; |
64 | afi_t afi; | |
65 | safi_t safi; | |
66 | int addpath_capable; | |
3f9c7369 DS |
67 | |
68 | if (!rn || !subgrp) | |
69 | return NULL; | |
adbac85e DW |
70 | |
71 | peer = SUBGRP_PEER (subgrp); | |
72 | afi = SUBGRP_AFI (subgrp); | |
73 | safi = SUBGRP_SAFI (subgrp); | |
74 | addpath_capable = bgp_addpath_encode_tx (peer, afi, safi); | |
75 | ||
76 | /* update-groups that do not support addpath will pass 0 for | |
77 | * addpath_tx_id so do not both matching against it */ | |
3f9c7369 | 78 | for (adj = rn->adj_out; adj; adj = adj->next) |
adbac85e DW |
79 | { |
80 | if (adj->subgroup == subgrp) | |
81 | { | |
82 | if (addpath_capable) | |
83 | { | |
84 | if (adj->addpath_tx_id == addpath_tx_id) | |
85 | { | |
86 | break; | |
87 | } | |
88 | } | |
89 | else | |
90 | { | |
91 | break; | |
92 | } | |
93 | } | |
94 | } | |
95 | ||
3f9c7369 DS |
96 | return adj; |
97 | } | |
98 | ||
99 | static void | |
100 | adj_free (struct bgp_adj_out *adj) | |
101 | { | |
102 | TAILQ_REMOVE (&(adj->subgroup->adjq), adj, subgrp_adj_train); | |
103 | SUBGRP_DECR_STAT (adj->subgroup, adj_count); | |
104 | XFREE (MTYPE_BGP_ADJ_OUT, adj); | |
105 | } | |
106 | ||
107 | static int | |
108 | group_announce_route_walkcb (struct update_group *updgrp, void *arg) | |
109 | { | |
110 | struct updwalk_context *ctx = arg; | |
111 | struct update_subgroup *subgrp; | |
adbac85e DW |
112 | struct bgp_info *ri; |
113 | afi_t afi; | |
114 | safi_t safi; | |
115 | struct peer *peer; | |
bd0c620c | 116 | struct bgp_adj_out *adj, *adj_next; |
adbac85e DW |
117 | int addpath_capable; |
118 | ||
119 | afi = UPDGRP_AFI (updgrp); | |
120 | safi = UPDGRP_SAFI (updgrp); | |
121 | peer = UPDGRP_PEER (updgrp); | |
122 | addpath_capable = bgp_addpath_encode_tx (peer, afi, safi); | |
3f9c7369 DS |
123 | |
124 | UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) | |
125 | { | |
126 | ||
127 | /* | |
128 | * Skip the subgroups that have coalesce timer running. We will | |
129 | * walk the entire prefix table for those subgroups when the | |
130 | * coalesce timer fires. | |
131 | */ | |
132 | if (!subgrp->t_coalesce) | |
adbac85e DW |
133 | { |
134 | /* An update-group that uses addpath */ | |
135 | if (addpath_capable) | |
136 | { | |
137 | /* Look through all of the paths we have advertised for this rn and | |
138 | * send a withdraw for the ones that are no longer present */ | |
bd0c620c | 139 | for (adj = ctx->rn->adj_out; adj; adj = adj_next) |
adbac85e | 140 | { |
bd0c620c DW |
141 | adj_next = adj->next; |
142 | ||
adbac85e DW |
143 | if (adj->subgroup == subgrp) |
144 | { | |
145 | for (ri = ctx->rn->info; ri; ri = ri->next) | |
146 | { | |
147 | if (ri->addpath_tx_id == adj->addpath_tx_id) | |
148 | { | |
149 | break; | |
150 | } | |
151 | } | |
152 | ||
153 | if (!ri) | |
154 | { | |
155 | subgroup_process_announce_selected (subgrp, NULL, ctx->rn, adj->addpath_tx_id); | |
156 | } | |
157 | } | |
158 | } | |
159 | ||
06370dac | 160 | for (ri = ctx->rn->info; ri; ri = ri->next) |
adbac85e | 161 | { |
06370dac DW |
162 | /* Skip the bestpath for now */ |
163 | if (ri == ctx->ri) | |
164 | continue; | |
165 | ||
166 | subgroup_process_announce_selected (subgrp, ri, ctx->rn, ri->addpath_tx_id); | |
adbac85e DW |
167 | } |
168 | ||
716b2d8a | 169 | /* Process the bestpath last so the "show [ip] bgp neighbor x.x.x.x advertised" |
06370dac | 170 | * output shows the attributes from the bestpath */ |
adbac85e DW |
171 | if (ctx->ri) |
172 | subgroup_process_announce_selected (subgrp, ctx->ri, ctx->rn, ctx->ri->addpath_tx_id); | |
173 | } | |
174 | ||
175 | /* An update-group that does not use addpath */ | |
176 | else | |
177 | { | |
178 | if (ctx->ri) | |
179 | { | |
180 | subgroup_process_announce_selected (subgrp, ctx->ri, ctx->rn, ctx->ri->addpath_tx_id); | |
181 | } | |
182 | else | |
183 | { | |
184 | /* Find the addpath_tx_id of the path we had advertised and | |
185 | * send a withdraw */ | |
bd0c620c | 186 | for (adj = ctx->rn->adj_out; adj; adj = adj_next) |
adbac85e | 187 | { |
bd0c620c DW |
188 | adj_next = adj->next; |
189 | ||
adbac85e DW |
190 | if (adj->subgroup == subgrp) |
191 | { | |
192 | subgroup_process_announce_selected (subgrp, NULL, ctx->rn, adj->addpath_tx_id); | |
193 | } | |
194 | } | |
195 | } | |
196 | } | |
197 | } | |
3f9c7369 DS |
198 | } |
199 | ||
200 | return UPDWALK_CONTINUE; | |
201 | } | |
202 | ||
203 | static void | |
204 | subgrp_show_adjq_vty (struct update_subgroup *subgrp, struct vty *vty, | |
205 | u_int8_t flags) | |
206 | { | |
207 | struct bgp_table *table; | |
208 | struct bgp_adj_out *adj; | |
209 | unsigned long output_count; | |
210 | struct bgp_node *rn; | |
211 | int header1 = 1; | |
212 | struct bgp *bgp; | |
213 | int header2 = 1; | |
214 | ||
215 | bgp = SUBGRP_INST (subgrp); | |
216 | if (!bgp) | |
217 | return; | |
218 | ||
219 | table = bgp->rib[SUBGRP_AFI (subgrp)][SUBGRP_SAFI (subgrp)]; | |
220 | ||
221 | output_count = 0; | |
222 | ||
223 | for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn)) | |
224 | for (adj = rn->adj_out; adj; adj = adj->next) | |
225 | if (adj->subgroup == subgrp) | |
226 | { | |
227 | if (header1) | |
228 | { | |
181039f3 DL |
229 | vty_out (vty, |
230 | "BGP table version is %" PRIu64 ", local router ID is %s\n", | |
96ade3ed | 231 | table->version,inet_ntoa(bgp->router_id)); |
181039f3 DL |
232 | vty_out (vty, BGP_SHOW_SCODE_HEADER); |
233 | vty_out (vty, BGP_SHOW_OCODE_HEADER); | |
3f9c7369 DS |
234 | header1 = 0; |
235 | } | |
236 | if (header2) | |
237 | { | |
181039f3 | 238 | vty_out (vty, BGP_SHOW_HEADER); |
3f9c7369 DS |
239 | header2 = 0; |
240 | } | |
241 | if ((flags & UPDWALK_FLAGS_ADVQUEUE) && adj->adv && adj->adv->baa) | |
242 | { | |
856ca177 | 243 | route_vty_out_tmp (vty, &rn->p, adj->adv->baa->attr, SUBGRP_SAFI (subgrp), 0, NULL); |
3f9c7369 DS |
244 | output_count++; |
245 | } | |
246 | if ((flags & UPDWALK_FLAGS_ADVERTISED) && adj->attr) | |
247 | { | |
856ca177 | 248 | route_vty_out_tmp (vty, &rn->p, adj->attr, SUBGRP_SAFI (subgrp), 0, NULL); |
3f9c7369 DS |
249 | output_count++; |
250 | } | |
251 | } | |
252 | if (output_count != 0) | |
5c7571d4 | 253 | vty_out (vty, "%sTotal number of prefixes %ld\n", |
1318e7c8 | 254 | VTYNL, output_count); |
3f9c7369 DS |
255 | } |
256 | ||
257 | static int | |
258 | 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; | |
181039f3 | 269 | vty_out (vty, "update group %" PRIu64 ", subgroup %" PRIu64 "\n", updgrp->id, |
96ade3ed | 270 | subgrp->id); |
3f9c7369 DS |
271 | subgrp_show_adjq_vty (subgrp, vty, ctx->flags); |
272 | } | |
273 | return UPDWALK_CONTINUE; | |
274 | } | |
275 | ||
276 | static void | |
277 | updgrp_show_adj (struct bgp *bgp, afi_t afi, safi_t safi, | |
f43e655e | 278 | struct vty *vty, uint64_t id, u_int8_t flags) |
3f9c7369 DS |
279 | { |
280 | struct updwalk_context ctx; | |
281 | memset (&ctx, 0, sizeof (ctx)); | |
282 | ctx.vty = vty; | |
283 | ctx.subgrp_id = id; | |
284 | ctx.flags = flags; | |
285 | ||
286 | update_group_af_walk (bgp, afi, safi, updgrp_show_adj_walkcb, &ctx); | |
287 | } | |
288 | ||
289 | static int | |
290 | subgroup_coalesce_timer (struct thread *thread) | |
291 | { | |
292 | struct update_subgroup *subgrp; | |
293 | ||
294 | subgrp = THREAD_ARG (thread); | |
295 | if (bgp_debug_update(NULL, NULL, subgrp->update_group, 0)) | |
ffd0c037 | 296 | zlog_debug ("u%" PRIu64 ":s%" PRIu64 " announcing routes upon coalesce timer expiry", |
3f9c7369 DS |
297 | (SUBGRP_UPDGRP (subgrp))->id, subgrp->id); |
298 | subgrp->t_coalesce = NULL; | |
299 | subgrp->v_coalesce = 0; | |
300 | subgroup_announce_route (subgrp); | |
301 | ||
302 | ||
303 | /* While the announce_route() may kick off the route advertisement timer for | |
304 | * the members of the subgroup, we'd like to send the initial updates much | |
305 | * faster (i.e., without enforcing MRAI). Also, if there were no routes to | |
306 | * announce, this is the method currently employed to trigger the EOR. | |
307 | */ | |
308 | if (!bgp_update_delay_active(SUBGRP_INST(subgrp))) | |
309 | { | |
310 | struct peer_af *paf; | |
311 | struct peer *peer; | |
312 | ||
313 | SUBGRP_FOREACH_PEER (subgrp, paf) | |
314 | { | |
315 | peer = PAF_PEER(paf); | |
316 | BGP_TIMER_OFF(peer->t_routeadv); | |
317 | BGP_TIMER_ON (peer->t_routeadv, bgp_routeadv_timer, 0); | |
318 | } | |
319 | } | |
320 | ||
321 | return 0; | |
322 | } | |
323 | ||
324 | static int | |
325 | 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 | |
338 | update_group_announce_rrc_walkcb (struct update_group *updgrp, 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 | { | |
352 | UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) | |
353 | { | |
354 | subgroup_announce_all (subgrp); | |
355 | } | |
356 | } | |
357 | ||
358 | return UPDWALK_CONTINUE; | |
359 | } | |
360 | ||
361 | /******************** | |
362 | * PUBLIC FUNCTIONS | |
363 | ********************/ | |
364 | ||
365 | /** | |
366 | * Allocate an adj-out object. Do proper initialization of its fields, | |
367 | * primarily its association with the subgroup and the prefix. | |
368 | */ | |
369 | struct bgp_adj_out * | |
adbac85e DW |
370 | bgp_adj_out_alloc (struct update_subgroup *subgrp, struct bgp_node *rn, |
371 | u_int32_t addpath_tx_id) | |
3f9c7369 DS |
372 | { |
373 | struct bgp_adj_out *adj; | |
374 | ||
375 | adj = XCALLOC (MTYPE_BGP_ADJ_OUT, sizeof (struct bgp_adj_out)); | |
376 | adj->subgroup = subgrp; | |
377 | if (rn) | |
378 | { | |
379 | BGP_ADJ_OUT_ADD (rn, adj); | |
380 | bgp_lock_node (rn); | |
381 | adj->rn = rn; | |
382 | } | |
adbac85e DW |
383 | |
384 | adj->addpath_tx_id = addpath_tx_id; | |
3f9c7369 DS |
385 | TAILQ_INSERT_TAIL (&(subgrp->adjq), adj, subgrp_adj_train); |
386 | SUBGRP_INCR_STAT (subgrp, adj_count); | |
387 | return adj; | |
388 | } | |
389 | ||
390 | ||
391 | struct bgp_advertise * | |
392 | bgp_advertise_clean_subgroup (struct update_subgroup *subgrp, | |
393 | struct bgp_adj_out *adj) | |
394 | { | |
395 | struct bgp_advertise *adv; | |
396 | struct bgp_advertise_attr *baa; | |
397 | struct bgp_advertise *next; | |
398 | struct bgp_advertise_fifo *fhead; | |
399 | ||
400 | adv = adj->adv; | |
401 | baa = adv->baa; | |
402 | next = NULL; | |
403 | ||
404 | if (baa) | |
405 | { | |
406 | fhead = &subgrp->sync->update; | |
407 | ||
408 | /* Unlink myself from advertise attribute FIFO. */ | |
409 | bgp_advertise_delete (baa, adv); | |
410 | ||
411 | /* Fetch next advertise candidate. */ | |
412 | next = baa->adv; | |
413 | ||
414 | /* Unintern BGP advertise attribute. */ | |
415 | bgp_advertise_unintern (subgrp->hash, baa); | |
416 | } | |
417 | else | |
418 | fhead = &subgrp->sync->withdraw; | |
419 | ||
420 | ||
421 | /* Unlink myself from advertisement FIFO. */ | |
422 | BGP_ADV_FIFO_DEL (fhead, adv); | |
423 | ||
424 | /* Free memory. */ | |
425 | bgp_advertise_free (adj->adv); | |
426 | adj->adv = NULL; | |
427 | ||
428 | return next; | |
429 | } | |
430 | ||
431 | void | |
432 | bgp_adj_out_set_subgroup (struct bgp_node *rn, | |
433 | struct update_subgroup *subgrp, | |
434 | struct attr *attr, struct bgp_info *binfo) | |
435 | { | |
436 | struct bgp_adj_out *adj = NULL; | |
437 | struct bgp_advertise *adv; | |
438 | ||
439 | if (DISABLE_BGP_ANNOUNCE) | |
440 | return; | |
441 | ||
442 | /* Look for adjacency information. */ | |
adbac85e | 443 | adj = adj_lookup (rn, subgrp, binfo->addpath_tx_id); |
3f9c7369 DS |
444 | |
445 | if (!adj) | |
446 | { | |
adbac85e | 447 | adj = bgp_adj_out_alloc (subgrp, rn, binfo->addpath_tx_id); |
3f9c7369 DS |
448 | if (!adj) |
449 | return; | |
450 | } | |
451 | ||
452 | if (adj->adv) | |
453 | bgp_advertise_clean_subgroup (subgrp, adj); | |
454 | adj->adv = bgp_advertise_new (); | |
455 | ||
456 | adv = adj->adv; | |
457 | adv->rn = rn; | |
458 | assert (adv->binfo == NULL); | |
459 | adv->binfo = bgp_info_lock (binfo); /* bgp_info adj_out reference */ | |
460 | ||
461 | if (attr) | |
462 | adv->baa = bgp_advertise_intern (subgrp->hash, attr); | |
463 | else | |
464 | adv->baa = baa_new (); | |
465 | adv->adj = adj; | |
466 | ||
467 | /* Add new advertisement to advertisement attribute list. */ | |
468 | bgp_advertise_add (adv->baa, adv); | |
469 | ||
470 | /* | |
471 | * If the update adv list is empty, trigger the member peers' | |
472 | * mrai timers so the socket writes can happen. | |
473 | */ | |
474 | if (BGP_ADV_FIFO_EMPTY (&subgrp->sync->update)) | |
475 | { | |
476 | struct peer_af *paf; | |
477 | ||
478 | SUBGRP_FOREACH_PEER (subgrp, paf) | |
479 | { | |
480 | bgp_adjust_routeadv (PAF_PEER (paf)); | |
481 | } | |
482 | } | |
483 | ||
484 | BGP_ADV_FIFO_ADD (&subgrp->sync->update, &adv->fifo); | |
485 | ||
486 | subgrp->version = max (subgrp->version, rn->version); | |
487 | } | |
488 | ||
4125bb67 DS |
489 | /* The only time 'withdraw' will be false is if we are sending |
490 | * the "neighbor x.x.x.x default-originate" default and need to clear | |
491 | * bgp_adj_out for the 0.0.0.0/0 route in the BGP table. | |
492 | */ | |
3f9c7369 DS |
493 | void |
494 | bgp_adj_out_unset_subgroup (struct bgp_node *rn, | |
4125bb67 | 495 | struct update_subgroup *subgrp, |
adbac85e DW |
496 | char withdraw, |
497 | u_int32_t addpath_tx_id) | |
3f9c7369 DS |
498 | { |
499 | struct bgp_adj_out *adj; | |
500 | struct bgp_advertise *adv; | |
4125bb67 | 501 | char trigger_write; |
3f9c7369 DS |
502 | |
503 | if (DISABLE_BGP_ANNOUNCE) | |
504 | return; | |
505 | ||
adbac85e DW |
506 | /* Lookup existing adjacency */ |
507 | if ((adj = adj_lookup (rn, subgrp, addpath_tx_id)) != NULL) | |
508 | { | |
509 | /* Clean up previous advertisement. */ | |
510 | if (adj->adv) | |
511 | bgp_advertise_clean_subgroup (subgrp, adj); | |
3f9c7369 | 512 | |
adbac85e DW |
513 | if (adj->attr && withdraw) |
514 | { | |
515 | /* We need advertisement structure. */ | |
516 | adj->adv = bgp_advertise_new (); | |
517 | adv = adj->adv; | |
518 | adv->rn = rn; | |
519 | adv->adj = adj; | |
520 | ||
521 | /* Note if we need to trigger a packet write */ | |
522 | if (BGP_ADV_FIFO_EMPTY (&subgrp->sync->withdraw)) | |
523 | trigger_write = 1; | |
524 | else | |
525 | trigger_write = 0; | |
3f9c7369 | 526 | |
adbac85e DW |
527 | /* Add to synchronization entry for withdraw announcement. */ |
528 | BGP_ADV_FIFO_ADD (&subgrp->sync->withdraw, &adv->fifo); | |
3f9c7369 | 529 | |
adbac85e DW |
530 | /* Schedule packet write, if FIFO is getting its first entry. */ |
531 | if (trigger_write) | |
532 | subgroup_trigger_write(subgrp); | |
533 | } | |
4125bb67 | 534 | else |
adbac85e DW |
535 | { |
536 | /* Remove myself from adjacency. */ | |
537 | BGP_ADJ_OUT_DEL (rn, adj); | |
3f9c7369 | 538 | |
adbac85e DW |
539 | /* Free allocated information. */ |
540 | adj_free (adj); | |
3f9c7369 | 541 | |
adbac85e DW |
542 | bgp_unlock_node (rn); |
543 | } | |
3f9c7369 DS |
544 | } |
545 | ||
3f9c7369 DS |
546 | subgrp->version = max (subgrp->version, rn->version); |
547 | } | |
548 | ||
549 | void | |
550 | bgp_adj_out_remove_subgroup (struct bgp_node *rn, struct bgp_adj_out *adj, | |
551 | struct update_subgroup *subgrp) | |
552 | { | |
553 | if (adj->attr) | |
554 | bgp_attr_unintern (&adj->attr); | |
555 | ||
556 | if (adj->adv) | |
557 | bgp_advertise_clean_subgroup (subgrp, adj); | |
558 | ||
559 | BGP_ADJ_OUT_DEL (rn, adj); | |
560 | adj_free (adj); | |
561 | } | |
562 | ||
563 | /* | |
564 | * Go through all the routes and clean up the adj/adv structures corresponding | |
565 | * to the subgroup. | |
566 | */ | |
567 | void | |
568 | subgroup_clear_table (struct update_subgroup *subgrp) | |
569 | { | |
570 | struct bgp_adj_out *aout, *taout; | |
571 | ||
572 | SUBGRP_FOREACH_ADJ_SAFE (subgrp, aout, taout) | |
573 | { | |
26acb92b DS |
574 | struct bgp_node *rn = aout->rn; |
575 | bgp_adj_out_remove_subgroup (rn, aout, subgrp); | |
576 | bgp_unlock_node (rn); | |
3f9c7369 DS |
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 | |
9bedbb1e DW |
601 | if (safi == SAFI_LABELED_UNICAST) |
602 | safi = SAFI_UNICAST; | |
603 | ||
3f9c7369 | 604 | if (!table) |
2a3d5731 | 605 | table = peer->bgp->rib[afi][safi]; |
3f9c7369 DS |
606 | |
607 | if (safi != SAFI_MPLS_VPN | |
587ff0fd | 608 | && safi != SAFI_ENCAP |
3d6c0dfa | 609 | && safi != SAFI_EVPN |
3f9c7369 DS |
610 | && CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_DEFAULT_ORIGINATE)) |
611 | subgroup_default_originate (subgrp, 0); | |
612 | ||
2a3d5731 | 613 | /* It's initialized in bgp_announce_check() */ |
3f9c7369 DS |
614 | attr.extra = &extra; |
615 | ||
616 | for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn)) | |
617 | for (ri = rn->info; ri; ri = ri->next) | |
618 | ||
adbac85e | 619 | if (CHECK_FLAG (ri->flags, BGP_INFO_SELECTED) || |
06370dac | 620 | (addpath_capable && bgp_addpath_tx_path(peer, afi, safi, ri))) |
3f9c7369 | 621 | { |
cd1964ff | 622 | if (subgroup_announce_check (rn, ri, subgrp, &rn->p, &attr)) |
3f9c7369 DS |
623 | bgp_adj_out_set_subgroup (rn, subgrp, &attr, ri); |
624 | else | |
adbac85e | 625 | bgp_adj_out_unset_subgroup (rn, subgrp, 1, ri->addpath_tx_id); |
3f9c7369 DS |
626 | } |
627 | ||
628 | /* | |
629 | * We walked through the whole table -- make sure our version number | |
630 | * is consistent with the one on the table. This should allow | |
631 | * subgroups to merge sooner if a peer comes up when the route node | |
632 | * with the largest version is no longer in the table. This also | |
633 | * covers the pathological case where all routes in the table have | |
634 | * now been deleted. | |
635 | */ | |
636 | subgrp->version = max (subgrp->version, table->version); | |
637 | ||
638 | /* | |
639 | * Start a task to merge the subgroup if necessary. | |
640 | */ | |
641 | update_subgroup_trigger_merge_check (subgrp, 0); | |
642 | } | |
643 | ||
644 | /* | |
645 | * subgroup_announce_route | |
646 | * | |
647 | * Refresh all routes out to a subgroup. | |
648 | */ | |
649 | void | |
650 | subgroup_announce_route (struct update_subgroup *subgrp) | |
651 | { | |
652 | struct bgp_node *rn; | |
653 | struct bgp_table *table; | |
654 | struct peer *onlypeer; | |
3f9c7369 DS |
655 | |
656 | if (update_subgroup_needs_refresh (subgrp)) | |
657 | { | |
658 | update_subgroup_set_needs_refresh (subgrp, 0); | |
659 | } | |
660 | ||
661 | /* | |
662 | * First update is deferred until ORF or ROUTE-REFRESH is received | |
663 | */ | |
664 | onlypeer = ((SUBGRP_PCOUNT (subgrp) == 1) ? | |
665 | (SUBGRP_PFIRST (subgrp))->peer : NULL); | |
666 | if (onlypeer && | |
667 | CHECK_FLAG (onlypeer-> | |
668 | af_sflags[SUBGRP_AFI (subgrp)][SUBGRP_SAFI (subgrp)], | |
669 | PEER_STATUS_ORF_WAIT_REFRESH)) | |
670 | return; | |
671 | ||
587ff0fd | 672 | if (SUBGRP_SAFI (subgrp) != SAFI_MPLS_VPN && |
3d6c0dfa PG |
673 | SUBGRP_SAFI (subgrp) != SAFI_ENCAP && |
674 | SUBGRP_SAFI (subgrp) != SAFI_EVPN) | |
2a3d5731 | 675 | subgroup_announce_table (subgrp, NULL); |
3f9c7369 DS |
676 | else |
677 | for (rn = bgp_table_top (update_subgroup_rib (subgrp)); rn; | |
678 | rn = bgp_route_next (rn)) | |
679 | if ((table = (rn->info)) != NULL) | |
2a3d5731 | 680 | subgroup_announce_table (subgrp, table); |
3f9c7369 DS |
681 | } |
682 | ||
683 | void | |
684 | subgroup_default_originate (struct update_subgroup *subgrp, int withdraw) | |
685 | { | |
686 | struct bgp *bgp; | |
687 | struct attr attr; | |
688 | struct aspath *aspath; | |
689 | struct prefix p; | |
690 | struct peer *from; | |
691 | struct bgp_node *rn; | |
692 | struct bgp_info *ri; | |
693 | struct peer *peer; | |
694 | int ret = RMAP_DENYMATCH; | |
695 | afi_t afi; | |
696 | safi_t safi; | |
697 | ||
698 | if (!subgrp) | |
699 | return; | |
700 | ||
701 | peer = SUBGRP_PEER (subgrp); | |
702 | afi = SUBGRP_AFI (subgrp); | |
703 | safi = SUBGRP_SAFI (subgrp); | |
704 | ||
705 | if (!(afi == AFI_IP || afi == AFI_IP6)) | |
706 | return; | |
707 | ||
708 | bgp = peer->bgp; | |
709 | from = bgp->peer_self; | |
710 | ||
711 | bgp_attr_default_set (&attr, BGP_ORIGIN_IGP); | |
712 | aspath = attr.aspath; | |
713 | attr.local_pref = bgp->default_local_pref; | |
3f9c7369 DS |
714 | |
715 | if (afi == AFI_IP) | |
716 | str2prefix ("0.0.0.0/0", &p); | |
3f9c7369 DS |
717 | else if (afi == AFI_IP6) |
718 | { | |
719 | struct attr_extra *ae = attr.extra; | |
720 | ||
721 | str2prefix ("::/0", &p); | |
722 | ||
723 | /* IPv6 global nexthop must be included. */ | |
801a9bcc | 724 | ae->mp_nexthop_len = BGP_ATTR_NHLEN_IPV6_GLOBAL; |
3f9c7369 DS |
725 | |
726 | /* If the peer is on shared nextwork and we have link-local | |
727 | nexthop set it. */ | |
728 | if (peer->shared_network | |
729 | && !IN6_IS_ADDR_UNSPECIFIED (&peer->nexthop.v6_local)) | |
3fb387f5 | 730 | ae->mp_nexthop_len = BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL; |
3f9c7369 | 731 | } |
3f9c7369 DS |
732 | |
733 | if (peer->default_rmap[afi][safi].name) | |
734 | { | |
735 | SET_FLAG (bgp->peer_self->rmap_type, PEER_RMAP_TYPE_DEFAULT); | |
736 | for (rn = bgp_table_top (bgp->rib[afi][safi]); rn; | |
737 | rn = bgp_route_next (rn)) | |
738 | { | |
739 | for (ri = rn->info; ri; ri = ri->next) | |
740 | { | |
741 | struct attr dummy_attr; | |
742 | struct attr_extra dummy_extra; | |
743 | struct bgp_info info; | |
744 | ||
745 | /* Provide dummy so the route-map can't modify the attributes */ | |
746 | dummy_attr.extra = &dummy_extra; | |
747 | bgp_attr_dup (&dummy_attr, ri->attr); | |
748 | info.peer = ri->peer; | |
749 | info.attr = &dummy_attr; | |
750 | ||
751 | ret = | |
752 | route_map_apply (peer->default_rmap[afi][safi].map, &rn->p, | |
753 | RMAP_BGP, &info); | |
754 | ||
755 | /* The route map might have set attributes. If we don't flush them | |
756 | * here, they will be leaked. */ | |
757 | bgp_attr_flush (&dummy_attr); | |
758 | if (ret != RMAP_DENYMATCH) | |
759 | break; | |
760 | } | |
761 | if (ret != RMAP_DENYMATCH) | |
762 | break; | |
763 | } | |
764 | bgp->peer_self->rmap_type = 0; | |
765 | ||
766 | if (ret == RMAP_DENYMATCH) | |
767 | withdraw = 1; | |
768 | } | |
769 | ||
770 | if (withdraw) | |
771 | { | |
772 | if (CHECK_FLAG (subgrp->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE)) | |
773 | subgroup_default_withdraw_packet (subgrp); | |
774 | UNSET_FLAG (subgrp->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE); | |
775 | } | |
776 | else | |
777 | { | |
778 | if (!CHECK_FLAG (subgrp->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE)) | |
779 | { | |
780 | SET_FLAG (subgrp->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE); | |
781 | subgroup_default_update_packet (subgrp, &attr, from); | |
4125bb67 DS |
782 | |
783 | /* The 'neighbor x.x.x.x default-originate' default will act as an | |
784 | * implicit withdraw for any previous UPDATEs sent for 0.0.0.0/0 so | |
785 | * clear adj_out for the 0.0.0.0/0 prefix in the BGP table. | |
786 | */ | |
787 | if (afi == AFI_IP) | |
788 | str2prefix ("0.0.0.0/0", &p); | |
4125bb67 DS |
789 | else |
790 | str2prefix ("::/0", &p); | |
4125bb67 DS |
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 | { | |
ffa2c898 QY |
832 | thread_add_timer_msec(bm->master, subgroup_coalesce_timer, subgrp, |
833 | subgrp->v_coalesce, &subgrp->t_coalesce); | |
3f9c7369 DS |
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 | } |