]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_addpath.c
Merge pull request #12447 from karlquan/master
[mirror_frr.git] / bgpd / bgp_addpath.c
1 /*
2 * Addpath TX ID selection, and related utilities
3 * Copyright (C) 2018 Amazon.com, Inc. or its affiliates
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "bgp_addpath.h"
25 #include "bgp_route.h"
26
27 static const struct bgp_addpath_strategy_names strat_names[BGP_ADDPATH_MAX] = {
28 {
29 .config_name = "addpath-tx-all-paths",
30 .human_name = "All",
31 .human_description = "Advertise all paths via addpath",
32 .type_json_name = "addpathTxAllPaths",
33 .id_json_name = "addpathTxIdAll"
34 },
35 {
36 .config_name = "addpath-tx-bestpath-per-AS",
37 .human_name = "Best-Per-AS",
38 .human_description = "Advertise bestpath per AS via addpath",
39 .type_json_name = "addpathTxBestpathPerAS",
40 .id_json_name = "addpathTxIdBestPerAS"
41 }
42 };
43
44 static const struct bgp_addpath_strategy_names unknown_names = {
45 .config_name = "addpath-tx-unknown",
46 .human_name = "Unknown-Addpath-Strategy",
47 .human_description = "Unknown Addpath Strategy",
48 .type_json_name = "addpathTxUnknown",
49 .id_json_name = "addpathTxIdUnknown"
50 };
51
52 /*
53 * Returns a structure full of strings associated with an addpath type. Will
54 * never return null.
55 */
56 const struct bgp_addpath_strategy_names *
57 bgp_addpath_names(enum bgp_addpath_strat strat)
58 {
59 if (strat < BGP_ADDPATH_MAX)
60 return &(strat_names[strat]);
61 else
62 return &unknown_names;
63 };
64
65 /*
66 * Returns if any peer is transmitting addpaths for a given afi/safi.
67 */
68 bool bgp_addpath_is_addpath_used(struct bgp_addpath_bgp_data *d, afi_t afi,
69 safi_t safi)
70 {
71 return d->total_peercount[afi][safi] > 0;
72 }
73
74 /*
75 * Initialize the BGP instance level data for addpath.
76 */
77 void bgp_addpath_init_bgp_data(struct bgp_addpath_bgp_data *d)
78 {
79 safi_t safi;
80 afi_t afi;
81 int i;
82
83 FOREACH_AFI_SAFI (afi, safi) {
84 for (i = 0; i < BGP_ADDPATH_MAX; i++) {
85 d->id_allocators[afi][safi][i] = NULL;
86 d->peercount[afi][safi][i] = 0;
87 }
88 d->total_peercount[afi][safi] = 0;
89 }
90 }
91
92 /*
93 * Free up resources associated with BGP route info structures.
94 */
95 void bgp_addpath_free_info_data(struct bgp_addpath_info_data *d,
96 struct bgp_addpath_node_data *nd)
97 {
98 int i;
99
100 for (i = 0; i < BGP_ADDPATH_MAX; i++) {
101 if (d->addpath_tx_id[i] != IDALLOC_INVALID)
102 idalloc_free_to_pool(&nd->free_ids[i],
103 d->addpath_tx_id[i]);
104 }
105 }
106
107 /*
108 * Return the addpath ID used to send a particular route, to a particular peer,
109 * in a particular AFI/SAFI.
110 */
111 uint32_t bgp_addpath_id_for_peer(struct peer *peer, afi_t afi, safi_t safi,
112 struct bgp_addpath_info_data *d)
113 {
114 if (safi == SAFI_LABELED_UNICAST)
115 safi = SAFI_UNICAST;
116
117 if (peer->addpath_type[afi][safi] < BGP_ADDPATH_MAX)
118 return d->addpath_tx_id[peer->addpath_type[afi][safi]];
119 else
120 return IDALLOC_INVALID;
121 }
122
123 /*
124 * Returns true if the path has an assigned addpath ID for any of the addpath
125 * strategies.
126 */
127 bool bgp_addpath_info_has_ids(struct bgp_addpath_info_data *d)
128 {
129 int i;
130
131 for (i = 0; i < BGP_ADDPATH_MAX; i++)
132 if (d->addpath_tx_id[i] != 0)
133 return true;
134
135 return false;
136 }
137
138 /*
139 * Releases any ID's associated with the BGP prefix.
140 */
141 void bgp_addpath_free_node_data(struct bgp_addpath_bgp_data *bd,
142 struct bgp_addpath_node_data *nd, afi_t afi,
143 safi_t safi)
144 {
145 int i;
146
147 for (i = 0; i < BGP_ADDPATH_MAX; i++) {
148 idalloc_drain_pool(bd->id_allocators[afi][safi][i],
149 &(nd->free_ids[i]));
150 }
151 }
152
153 /*
154 * Check to see if the addpath strategy requires DMED to be configured to work.
155 */
156 bool bgp_addpath_dmed_required(int strategy)
157 {
158 return strategy == BGP_ADDPATH_BEST_PER_AS;
159 }
160
161 /*
162 * Return true if this is a path we should advertise due to a
163 * configured addpath-tx knob
164 */
165 bool bgp_addpath_tx_path(enum bgp_addpath_strat strat, struct bgp_path_info *pi)
166 {
167 switch (strat) {
168 case BGP_ADDPATH_NONE:
169 return false;
170 case BGP_ADDPATH_ALL:
171 return true;
172 case BGP_ADDPATH_BEST_PER_AS:
173 if (CHECK_FLAG(pi->flags, BGP_PATH_DMED_SELECTED))
174 return true;
175 else
176 return false;
177 default:
178 return false;
179 }
180 }
181
182 static void bgp_addpath_flush_type_rn(struct bgp *bgp, afi_t afi, safi_t safi,
183 enum bgp_addpath_strat addpath_type,
184 struct bgp_dest *dest)
185 {
186 struct bgp_path_info *pi;
187
188 if (safi == SAFI_LABELED_UNICAST)
189 safi = SAFI_UNICAST;
190
191 idalloc_drain_pool(
192 bgp->tx_addpath.id_allocators[afi][safi][addpath_type],
193 &(dest->tx_addpath.free_ids[addpath_type]));
194 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
195 if (pi->tx_addpath.addpath_tx_id[addpath_type]
196 != IDALLOC_INVALID) {
197 idalloc_free(
198 bgp->tx_addpath
199 .id_allocators[afi][safi][addpath_type],
200 pi->tx_addpath.addpath_tx_id[addpath_type]);
201 pi->tx_addpath.addpath_tx_id[addpath_type] =
202 IDALLOC_INVALID;
203 }
204 }
205 }
206
207 /*
208 * Purge all addpath ID's on a BGP instance associated with the addpath
209 * strategy, and afi/safi combination. This lets us let go of all memory held to
210 * track ID numbers associated with an addpath type not in use. Since
211 * post-bestpath ID processing is skipped for types not used, this is the only
212 * chance to free this data.
213 */
214 static void bgp_addpath_flush_type(struct bgp *bgp, afi_t afi, safi_t safi,
215 enum bgp_addpath_strat addpath_type)
216 {
217 struct bgp_dest *dest, *ndest;
218
219 if (safi == SAFI_LABELED_UNICAST)
220 safi = SAFI_UNICAST;
221
222 for (dest = bgp_table_top(bgp->rib[afi][safi]); dest;
223 dest = bgp_route_next(dest)) {
224 if (safi == SAFI_MPLS_VPN) {
225 struct bgp_table *table;
226
227 table = bgp_dest_get_bgp_table_info(dest);
228 if (!table)
229 continue;
230
231 for (ndest = bgp_table_top(table); ndest;
232 ndest = bgp_route_next(ndest))
233 bgp_addpath_flush_type_rn(bgp, afi, safi,
234 addpath_type, ndest);
235 } else {
236 bgp_addpath_flush_type_rn(bgp, afi, safi, addpath_type,
237 dest);
238 }
239 }
240
241 idalloc_destroy(bgp->tx_addpath.id_allocators[afi][safi][addpath_type]);
242 bgp->tx_addpath.id_allocators[afi][safi][addpath_type] = NULL;
243 }
244
245 /*
246 * Allocate an Addpath ID for the given type on a path, if necessary.
247 */
248 static void bgp_addpath_populate_path(struct id_alloc *allocator,
249 struct bgp_path_info *path,
250 enum bgp_addpath_strat addpath_type)
251 {
252 if (bgp_addpath_tx_path(addpath_type, path)) {
253 path->tx_addpath.addpath_tx_id[addpath_type] =
254 idalloc_allocate(allocator);
255 }
256 }
257
258 /*
259 * Compute addpath ID's on a BGP instance associated with the addpath strategy,
260 * and afi/safi combination. Since we won't waste the time computing addpath IDs
261 * for unused strategies, the first time a peer is configured to use a strategy,
262 * we have to backfill the data.
263 * In labeled-unicast, addpath allocations SHOULD be done in unicast SAFI.
264 */
265 static void bgp_addpath_populate_type(struct bgp *bgp, afi_t afi, safi_t safi,
266 enum bgp_addpath_strat addpath_type)
267 {
268 struct bgp_dest *dest, *ndest;
269 char buf[200];
270 struct id_alloc *allocator;
271
272 if (safi == SAFI_LABELED_UNICAST)
273 safi = SAFI_UNICAST;
274
275 snprintf(buf, sizeof(buf), "Addpath ID Allocator %s:%d/%d",
276 bgp_addpath_names(addpath_type)->config_name, (int)afi,
277 (int)safi);
278 buf[sizeof(buf) - 1] = '\0';
279 zlog_info("Computing addpath IDs for addpath type %s",
280 bgp_addpath_names(addpath_type)->human_name);
281
282 bgp->tx_addpath.id_allocators[afi][safi][addpath_type] =
283 idalloc_new(buf);
284
285 idalloc_reserve(bgp->tx_addpath.id_allocators[afi][safi][addpath_type],
286 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE);
287
288 allocator = bgp->tx_addpath.id_allocators[afi][safi][addpath_type];
289
290 for (dest = bgp_table_top(bgp->rib[afi][safi]); dest;
291 dest = bgp_route_next(dest)) {
292 struct bgp_path_info *bi;
293
294 if (safi == SAFI_MPLS_VPN) {
295 struct bgp_table *table;
296
297 table = bgp_dest_get_bgp_table_info(dest);
298 if (!table)
299 continue;
300
301 for (ndest = bgp_table_top(table); ndest;
302 ndest = bgp_route_next(ndest))
303 for (bi = bgp_dest_get_bgp_path_info(ndest); bi;
304 bi = bi->next)
305 bgp_addpath_populate_path(allocator, bi,
306 addpath_type);
307 } else {
308 for (bi = bgp_dest_get_bgp_path_info(dest); bi;
309 bi = bi->next)
310 bgp_addpath_populate_path(allocator, bi,
311 addpath_type);
312 }
313 }
314 }
315
316 /*
317 * Handle updates to a peer or group's addpath strategy. If after adjusting
318 * counts a addpath strategy is in use for the first time, or no longer in use,
319 * the IDs for that strategy will be populated or flushed.
320 */
321 void bgp_addpath_type_changed(struct bgp *bgp)
322 {
323 afi_t afi;
324 safi_t safi;
325 struct listnode *node, *nnode;
326 struct peer *peer;
327 int peer_count[AFI_MAX][SAFI_MAX][BGP_ADDPATH_MAX];
328 enum bgp_addpath_strat type;
329
330 FOREACH_AFI_SAFI(afi, safi) {
331 for (type=0; type<BGP_ADDPATH_MAX; type++) {
332 peer_count[afi][safi][type] = 0;
333 }
334 bgp->tx_addpath.total_peercount[afi][safi] = 0;
335 }
336
337 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
338 FOREACH_AFI_SAFI(afi, safi) {
339 type = peer->addpath_type[afi][safi];
340 if (type != BGP_ADDPATH_NONE) {
341 peer_count[afi][safi][type] += 1;
342 bgp->tx_addpath.total_peercount[afi][safi] += 1;
343 }
344 }
345 }
346
347 FOREACH_AFI_SAFI(afi, safi) {
348 for (type=0; type<BGP_ADDPATH_MAX; type++) {
349 int old = bgp->tx_addpath.peercount[afi][safi][type];
350 int new = peer_count[afi][safi][type];
351
352 bgp->tx_addpath.peercount[afi][safi][type] = new;
353
354 if (old == 0 && new != 0) {
355 bgp_addpath_populate_type(bgp, afi, safi,
356 type);
357 } else if (old != 0 && new == 0) {
358 bgp_addpath_flush_type(bgp, afi, safi, type);
359 }
360 }
361 }
362 }
363
364 /*
365 * Change the addpath type assigned to a peer, or peer group. In addition to
366 * adjusting the counts, peer sessions will be reset as needed to make the
367 * change take effect.
368 */
369 void bgp_addpath_set_peer_type(struct peer *peer, afi_t afi, safi_t safi,
370 enum bgp_addpath_strat addpath_type)
371 {
372 struct bgp *bgp = peer->bgp;
373 enum bgp_addpath_strat old_type;
374 struct listnode *node, *nnode;
375 struct peer *tmp_peer;
376 struct peer_group *group;
377
378 if (safi == SAFI_LABELED_UNICAST)
379 safi = SAFI_UNICAST;
380
381 old_type = peer->addpath_type[afi][safi];
382 if (addpath_type == old_type)
383 return;
384
385 if (addpath_type == BGP_ADDPATH_NONE && peer->group &&
386 !CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
387 /* A "no" config on a group member inherits group */
388 addpath_type = peer->group->conf->addpath_type[afi][safi];
389 }
390
391 peer->addpath_type[afi][safi] = addpath_type;
392
393 bgp_addpath_type_changed(bgp);
394
395 if (addpath_type != BGP_ADDPATH_NONE) {
396 if (bgp_addpath_dmed_required(addpath_type)) {
397 if (!CHECK_FLAG(bgp->flags,
398 BGP_FLAG_DETERMINISTIC_MED)) {
399 zlog_warn(
400 "%s: enabling bgp deterministic-med, this is required for addpath-tx-bestpath-per-AS",
401 peer->host);
402 SET_FLAG(bgp->flags,
403 BGP_FLAG_DETERMINISTIC_MED);
404 bgp_recalculate_all_bestpaths(bgp);
405 }
406 }
407 }
408
409 zlog_info("Resetting peer %s%s due to change in addpath config",
410 CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP) ? "group " : "",
411 peer->host);
412
413 if (CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
414 group = peer->group;
415
416 /* group will be null as peer_group_delete calls peer_delete on
417 * group->conf. That peer_delete will eventuallly end up here
418 * if the group was configured to tx addpaths.
419 */
420 if (group != NULL) {
421 for (ALL_LIST_ELEMENTS(group->peer, node, nnode,
422 tmp_peer)) {
423 if (tmp_peer->addpath_type[afi][safi] ==
424 old_type) {
425 bgp_addpath_set_peer_type(tmp_peer,
426 afi,
427 safi,
428 addpath_type);
429 }
430 }
431 }
432 } else {
433 peer_change_action(peer, afi, safi, peer_change_reset);
434 }
435
436 }
437
438 /*
439 * Intended to run after bestpath. This function will take TX IDs from paths
440 * that no longer need them, and give them to paths that do. This prevents
441 * best-per-as updates from needing to do a separate withdraw and update just to
442 * swap out which path is sent.
443 */
444 void bgp_addpath_update_ids(struct bgp *bgp, struct bgp_dest *bn, afi_t afi,
445 safi_t safi)
446 {
447 int i;
448 struct bgp_path_info *pi;
449 struct id_alloc_pool **pool_ptr;
450
451 if (safi == SAFI_LABELED_UNICAST)
452 safi = SAFI_UNICAST;
453
454 for (i = 0; i < BGP_ADDPATH_MAX; i++) {
455 struct id_alloc *alloc =
456 bgp->tx_addpath.id_allocators[afi][safi][i];
457 pool_ptr = &(bn->tx_addpath.free_ids[i]);
458
459 if (bgp->tx_addpath.peercount[afi][safi][i] == 0)
460 continue;
461
462 /* Free Unused IDs back to the pool.*/
463 for (pi = bgp_dest_get_bgp_path_info(bn); pi; pi = pi->next) {
464 if (pi->tx_addpath.addpath_tx_id[i] != IDALLOC_INVALID
465 && !bgp_addpath_tx_path(i, pi)) {
466 idalloc_free_to_pool(pool_ptr,
467 pi->tx_addpath.addpath_tx_id[i]);
468 pi->tx_addpath.addpath_tx_id[i] =
469 IDALLOC_INVALID;
470 }
471 }
472
473 /* Give IDs to paths that need them (pulling from the pool) */
474 for (pi = bgp_dest_get_bgp_path_info(bn); pi; pi = pi->next) {
475 if (pi->tx_addpath.addpath_tx_id[i] == IDALLOC_INVALID
476 && bgp_addpath_tx_path(i, pi)) {
477 pi->tx_addpath.addpath_tx_id[i] =
478 idalloc_allocate_prefer_pool(
479 alloc, pool_ptr);
480 }
481 }
482
483 /* Free any IDs left in the pool to the main allocator */
484 idalloc_drain_pool(alloc, pool_ptr);
485 }
486 }