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