]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_mpath.c
Merge pull request #5717 from pguibert6WIND/flowspec_issue_redistribute
[mirror_frr.git] / bgpd / bgp_mpath.c
CommitLineData
8ef0791c 1/*
165b5fff
JB
2 * BGP Multipath
3 * Copyright (C) 2010 Google Inc.
4 *
5 * This file is part of Quagga
6 *
7 * Quagga is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * Quagga is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
896014f4
DL
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
165b5fff
JB
20 */
21
22#include <zebra.h>
23
24#include "command.h"
96450faf
JB
25#include "prefix.h"
26#include "linklist.h"
27#include "sockunion.h"
de8d5dff 28#include "memory.h"
3f9c7369 29#include "queue.h"
039f3a34 30#include "filter.h"
165b5fff
JB
31
32#include "bgpd/bgpd.h"
96450faf
JB
33#include "bgpd/bgp_table.h"
34#include "bgpd/bgp_route.h"
35#include "bgpd/bgp_attr.h"
de8d5dff 36#include "bgpd/bgp_debug.h"
0b597ef0
JB
37#include "bgpd/bgp_aspath.h"
38#include "bgpd/bgp_community.h"
39#include "bgpd/bgp_ecommunity.h"
57d187bc 40#include "bgpd/bgp_lcommunity.h"
165b5fff
JB
41#include "bgpd/bgp_mpath.h"
42
43/*
44 * bgp_maximum_paths_set
45 *
46 * Record maximum-paths configuration for BGP instance
47 */
d62a17ae 48int bgp_maximum_paths_set(struct bgp *bgp, afi_t afi, safi_t safi, int peertype,
d7c0a89a 49 uint16_t maxpaths, uint16_t options)
165b5fff 50{
d62a17ae 51 if (!bgp || (afi >= AFI_MAX) || (safi >= SAFI_MAX))
52 return -1;
53
54 switch (peertype) {
55 case BGP_PEER_IBGP:
56 bgp->maxpaths[afi][safi].maxpaths_ibgp = maxpaths;
57 bgp->maxpaths[afi][safi].ibgp_flags |= options;
58 break;
59 case BGP_PEER_EBGP:
60 bgp->maxpaths[afi][safi].maxpaths_ebgp = maxpaths;
61 break;
62 default:
63 return -1;
64 }
65
66 return 0;
165b5fff
JB
67}
68
69/*
70 * bgp_maximum_paths_unset
71 *
72 * Remove maximum-paths configuration from BGP instance
73 */
d62a17ae 74int bgp_maximum_paths_unset(struct bgp *bgp, afi_t afi, safi_t safi,
75 int peertype)
165b5fff 76{
d62a17ae 77 if (!bgp || (afi >= AFI_MAX) || (safi >= SAFI_MAX))
78 return -1;
79
80 switch (peertype) {
81 case BGP_PEER_IBGP:
82 bgp->maxpaths[afi][safi].maxpaths_ibgp = multipath_num;
83 bgp->maxpaths[afi][safi].ibgp_flags = 0;
84 break;
85 case BGP_PEER_EBGP:
86 bgp->maxpaths[afi][safi].maxpaths_ebgp = multipath_num;
87 break;
88 default:
89 return -1;
90 }
91
92 return 0;
165b5fff 93}
96450faf 94
194a4f2c
DW
95/*
96 * bgp_interface_same
97 *
98 * Return true if ifindex for ifp1 and ifp2 are the same, else return false.
99 */
100static int bgp_interface_same(struct interface *ifp1, struct interface *ifp2)
101{
102 if (!ifp1 && !ifp2)
103 return 1;
104
105 if (!ifp1 && ifp2)
106 return 0;
107
108 if (ifp1 && !ifp2)
109 return 0;
110
111 return (ifp1->ifindex == ifp2->ifindex);
112}
113
114
96450faf 115/*
18ee8310 116 * bgp_path_info_nexthop_cmp
96450faf
JB
117 *
118 * Compare the nexthops of two paths. Return value is less than, equal to,
40381db7
DS
119 * or greater than zero if bpi1 is respectively less than, equal to,
120 * or greater than bpi2.
96450faf 121 */
40381db7
DS
122int bgp_path_info_nexthop_cmp(struct bgp_path_info *bpi1,
123 struct bgp_path_info *bpi2)
96450faf 124{
d62a17ae 125 int compare;
05a38c0c 126 struct in6_addr addr1, addr2;
d62a17ae 127
40381db7 128 compare = IPV4_ADDR_CMP(&bpi1->attr->nexthop, &bpi2->attr->nexthop);
d62a17ae 129 if (!compare) {
40381db7
DS
130 if (bpi1->attr->mp_nexthop_len == bpi2->attr->mp_nexthop_len) {
131 switch (bpi1->attr->mp_nexthop_len) {
d62a17ae 132 case BGP_ATTR_NHLEN_IPV4:
133 case BGP_ATTR_NHLEN_VPNV4:
134 compare = IPV4_ADDR_CMP(
40381db7
DS
135 &bpi1->attr->mp_nexthop_global_in,
136 &bpi2->attr->mp_nexthop_global_in);
d62a17ae 137 break;
138 case BGP_ATTR_NHLEN_IPV6_GLOBAL:
139 case BGP_ATTR_NHLEN_VPNV6_GLOBAL:
140 compare = IPV6_ADDR_CMP(
40381db7
DS
141 &bpi1->attr->mp_nexthop_global,
142 &bpi2->attr->mp_nexthop_global);
d62a17ae 143 break;
144 case BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL:
40381db7
DS
145 addr1 = (bpi1->attr->mp_nexthop_prefer_global)
146 ? bpi1->attr->mp_nexthop_global
147 : bpi1->attr->mp_nexthop_local;
148 addr2 = (bpi2->attr->mp_nexthop_prefer_global)
149 ? bpi2->attr->mp_nexthop_global
150 : bpi2->attr->mp_nexthop_local;
151
152 if (!bpi1->attr->mp_nexthop_prefer_global
153 && !bpi2->attr->mp_nexthop_prefer_global)
996c9314 154 compare = !bgp_interface_same(
40381db7
DS
155 bpi1->peer->ifp,
156 bpi2->peer->ifp);
194a4f2c 157
d62a17ae 158 if (!compare)
05a38c0c 159 compare = IPV6_ADDR_CMP(&addr1, &addr2);
d62a17ae 160 break;
161 }
162 }
163
164 /* This can happen if one IPv6 peer sends you global and
165 * link-local
166 * nexthops but another IPv6 peer only sends you global
167 */
40381db7
DS
168 else if (bpi1->attr->mp_nexthop_len
169 == BGP_ATTR_NHLEN_IPV6_GLOBAL
170 || bpi1->attr->mp_nexthop_len
d62a17ae 171 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL) {
40381db7
DS
172 compare = IPV6_ADDR_CMP(&bpi1->attr->mp_nexthop_global,
173 &bpi2->attr->mp_nexthop_global);
d62a17ae 174 if (!compare) {
40381db7
DS
175 if (bpi1->attr->mp_nexthop_len
176 < bpi2->attr->mp_nexthop_len)
d62a17ae 177 compare = -1;
178 else
179 compare = 1;
180 }
181 }
182 }
183
184 return compare;
96450faf
JB
185}
186
187/*
18ee8310 188 * bgp_path_info_mpath_cmp
96450faf
JB
189 *
190 * This function determines our multipath list ordering. By ordering
191 * the list we can deterministically select which paths are included
192 * in the multipath set. The ordering also helps in detecting changes
193 * in the multipath selection so we can detect whether to send an
194 * update to zebra.
195 *
196 * The order of paths is determined first by received nexthop, and then
197 * by peer address if the nexthops are the same.
198 */
18ee8310 199static int bgp_path_info_mpath_cmp(void *val1, void *val2)
96450faf 200{
40381db7 201 struct bgp_path_info *bpi1, *bpi2;
d62a17ae 202 int compare;
203
40381db7
DS
204 bpi1 = val1;
205 bpi2 = val2;
d62a17ae 206
40381db7 207 compare = bgp_path_info_nexthop_cmp(bpi1, bpi2);
d62a17ae 208
209 if (!compare) {
40381db7 210 if (!bpi1->peer->su_remote && !bpi2->peer->su_remote)
d62a17ae 211 compare = 0;
40381db7 212 else if (!bpi1->peer->su_remote)
d62a17ae 213 compare = 1;
40381db7 214 else if (!bpi2->peer->su_remote)
d62a17ae 215 compare = -1;
216 else
40381db7
DS
217 compare = sockunion_cmp(bpi1->peer->su_remote,
218 bpi2->peer->su_remote);
d62a17ae 219 }
220
221 return compare;
96450faf
JB
222}
223
224/*
225 * bgp_mp_list_init
226 *
227 * Initialize the mp_list, which holds the list of multipaths
228 * selected by bgp_best_selection
229 */
d62a17ae 230void bgp_mp_list_init(struct list *mp_list)
96450faf 231{
d62a17ae 232 assert(mp_list);
233 memset(mp_list, 0, sizeof(struct list));
18ee8310 234 mp_list->cmp = bgp_path_info_mpath_cmp;
96450faf
JB
235}
236
237/*
238 * bgp_mp_list_clear
239 *
240 * Clears all entries out of the mp_list
241 */
d62a17ae 242void bgp_mp_list_clear(struct list *mp_list)
96450faf 243{
d62a17ae 244 assert(mp_list);
245 list_delete_all_node(mp_list);
96450faf
JB
246}
247
248/*
249 * bgp_mp_list_add
250 *
251 * Adds a multipath entry to the mp_list
252 */
4b7e6066 253void bgp_mp_list_add(struct list *mp_list, struct bgp_path_info *mpinfo)
96450faf 254{
d62a17ae 255 assert(mp_list && mpinfo);
256 listnode_add_sort(mp_list, mpinfo);
96450faf 257}
de8d5dff
JB
258
259/*
18ee8310 260 * bgp_path_info_mpath_new
de8d5dff 261 *
18ee8310 262 * Allocate and zero memory for a new bgp_path_info_mpath element
de8d5dff 263 */
18ee8310 264static struct bgp_path_info_mpath *bgp_path_info_mpath_new(void)
de8d5dff 265{
4b7e6066
DS
266 struct bgp_path_info_mpath *new_mpath;
267 new_mpath = XCALLOC(MTYPE_BGP_MPATH_INFO,
268 sizeof(struct bgp_path_info_mpath));
d62a17ae 269 return new_mpath;
de8d5dff
JB
270}
271
272/*
18ee8310 273 * bgp_path_info_mpath_free
de8d5dff 274 *
18ee8310 275 * Release resources for a bgp_path_info_mpath element and zero out pointer
de8d5dff 276 */
18ee8310 277void bgp_path_info_mpath_free(struct bgp_path_info_mpath **mpath)
de8d5dff 278{
d62a17ae 279 if (mpath && *mpath) {
280 if ((*mpath)->mp_attr)
281 bgp_attr_unintern(&(*mpath)->mp_attr);
282 XFREE(MTYPE_BGP_MPATH_INFO, *mpath);
283 *mpath = NULL;
284 }
de8d5dff
JB
285}
286
287/*
18ee8310 288 * bgp_path_info_mpath_get
de8d5dff 289 *
18ee8310 290 * Fetch the mpath element for the given bgp_path_info. Used for
de8d5dff
JB
291 * doing lazy allocation.
292 */
4b7e6066 293static struct bgp_path_info_mpath *
9b6d8fcf 294bgp_path_info_mpath_get(struct bgp_path_info *path)
de8d5dff 295{
4b7e6066 296 struct bgp_path_info_mpath *mpath;
fa3bf3a2
MS
297
298 if (!path)
299 return NULL;
300
9b6d8fcf 301 if (!path->mpath) {
18ee8310 302 mpath = bgp_path_info_mpath_new();
d62a17ae 303 if (!mpath)
304 return NULL;
9b6d8fcf
DS
305 path->mpath = mpath;
306 mpath->mp_info = path;
d62a17ae 307 }
9b6d8fcf 308 return path->mpath;
de8d5dff
JB
309}
310
311/*
18ee8310 312 * bgp_path_info_mpath_enqueue
de8d5dff
JB
313 *
314 * Enqueue a path onto the multipath list given the previous multipath
315 * list entry
316 */
18ee8310 317static void bgp_path_info_mpath_enqueue(struct bgp_path_info *prev_info,
9b6d8fcf 318 struct bgp_path_info *path)
de8d5dff 319{
4b7e6066 320 struct bgp_path_info_mpath *prev, *mpath;
de8d5dff 321
18ee8310 322 prev = bgp_path_info_mpath_get(prev_info);
9b6d8fcf 323 mpath = bgp_path_info_mpath_get(path);
d62a17ae 324 if (!prev || !mpath)
325 return;
de8d5dff 326
d62a17ae 327 mpath->mp_next = prev->mp_next;
328 mpath->mp_prev = prev;
329 if (prev->mp_next)
330 prev->mp_next->mp_prev = mpath;
331 prev->mp_next = mpath;
de8d5dff 332
9b6d8fcf 333 SET_FLAG(path->flags, BGP_PATH_MULTIPATH);
de8d5dff
JB
334}
335
336/*
18ee8310 337 * bgp_path_info_mpath_dequeue
de8d5dff
JB
338 *
339 * Remove a path from the multipath list
340 */
9b6d8fcf 341void bgp_path_info_mpath_dequeue(struct bgp_path_info *path)
de8d5dff 342{
9b6d8fcf 343 struct bgp_path_info_mpath *mpath = path->mpath;
d62a17ae 344 if (!mpath)
345 return;
346 if (mpath->mp_prev)
347 mpath->mp_prev->mp_next = mpath->mp_next;
348 if (mpath->mp_next)
349 mpath->mp_next->mp_prev = mpath->mp_prev;
350 mpath->mp_next = mpath->mp_prev = NULL;
9b6d8fcf 351 UNSET_FLAG(path->flags, BGP_PATH_MULTIPATH);
de8d5dff
JB
352}
353
354/*
18ee8310 355 * bgp_path_info_mpath_next
de8d5dff 356 *
18ee8310 357 * Given a bgp_path_info, return the next multipath entry
de8d5dff 358 */
9b6d8fcf 359struct bgp_path_info *bgp_path_info_mpath_next(struct bgp_path_info *path)
de8d5dff 360{
9b6d8fcf 361 if (!path->mpath || !path->mpath->mp_next)
d62a17ae 362 return NULL;
9b6d8fcf 363 return path->mpath->mp_next->mp_info;
de8d5dff
JB
364}
365
366/*
18ee8310 367 * bgp_path_info_mpath_first
de8d5dff 368 *
18ee8310 369 * Given bestpath bgp_path_info, return the first multipath entry.
de8d5dff 370 */
9b6d8fcf 371struct bgp_path_info *bgp_path_info_mpath_first(struct bgp_path_info *path)
de8d5dff 372{
9b6d8fcf 373 return bgp_path_info_mpath_next(path);
de8d5dff
JB
374}
375
376/*
18ee8310 377 * bgp_path_info_mpath_count
de8d5dff 378 *
18ee8310 379 * Given the bestpath bgp_path_info, return the number of multipath entries
de8d5dff 380 */
9b6d8fcf 381uint32_t bgp_path_info_mpath_count(struct bgp_path_info *path)
de8d5dff 382{
9b6d8fcf 383 if (!path->mpath)
d62a17ae 384 return 0;
9b6d8fcf 385 return path->mpath->mp_count;
de8d5dff
JB
386}
387
388/*
18ee8310 389 * bgp_path_info_mpath_count_set
de8d5dff
JB
390 *
391 * Sets the count of multipaths into bestpath's mpath element
392 */
9b6d8fcf 393static void bgp_path_info_mpath_count_set(struct bgp_path_info *path,
18ee8310 394 uint32_t count)
de8d5dff 395{
4b7e6066 396 struct bgp_path_info_mpath *mpath;
9b6d8fcf 397 if (!count && !path->mpath)
d62a17ae 398 return;
9b6d8fcf 399 mpath = bgp_path_info_mpath_get(path);
d62a17ae 400 if (!mpath)
401 return;
402 mpath->mp_count = count;
de8d5dff
JB
403}
404
0b597ef0 405/*
18ee8310 406 * bgp_path_info_mpath_attr
0b597ef0 407 *
18ee8310 408 * Given bestpath bgp_path_info, return aggregated attribute set used
0b597ef0
JB
409 * for advertising the multipath route
410 */
9b6d8fcf 411struct attr *bgp_path_info_mpath_attr(struct bgp_path_info *path)
0b597ef0 412{
9b6d8fcf 413 if (!path->mpath)
d62a17ae 414 return NULL;
9b6d8fcf 415 return path->mpath->mp_attr;
0b597ef0
JB
416}
417
418/*
18ee8310 419 * bgp_path_info_mpath_attr_set
0b597ef0
JB
420 *
421 * Sets the aggregated attribute into bestpath's mpath element
422 */
9b6d8fcf 423static void bgp_path_info_mpath_attr_set(struct bgp_path_info *path,
18ee8310 424 struct attr *attr)
0b597ef0 425{
4b7e6066 426 struct bgp_path_info_mpath *mpath;
9b6d8fcf 427 if (!attr && !path->mpath)
d62a17ae 428 return;
9b6d8fcf 429 mpath = bgp_path_info_mpath_get(path);
d62a17ae 430 if (!mpath)
431 return;
432 mpath->mp_attr = attr;
0b597ef0
JB
433}
434
de8d5dff 435/*
18ee8310 436 * bgp_path_info_mpath_update
de8d5dff
JB
437 *
438 * Compare and sync up the multipath list with the mp_list generated by
439 * bgp_best_selection
440 */
18ee8310
DS
441void bgp_path_info_mpath_update(struct bgp_node *rn,
442 struct bgp_path_info *new_best,
443 struct bgp_path_info *old_best,
444 struct list *mp_list,
445 struct bgp_maxpaths_cfg *mpath_cfg)
de8d5dff 446{
d7c0a89a 447 uint16_t maxpaths, mpath_count, old_mpath_count;
d62a17ae 448 struct listnode *mp_node, *mp_next_node;
4b7e6066 449 struct bgp_path_info *cur_mpath, *new_mpath, *next_mpath, *prev_mpath;
d62a17ae 450 int mpath_changed, debug;
451 char pfx_buf[PREFIX2STR_BUFFER], nh_buf[2][INET6_ADDRSTRLEN];
452 char path_buf[PATH_ADDPATH_STR_BUFFER];
453
454 mpath_changed = 0;
455 maxpaths = multipath_num;
456 mpath_count = 0;
457 cur_mpath = NULL;
458 old_mpath_count = 0;
459 prev_mpath = new_best;
460 mp_node = listhead(mp_list);
461 debug = bgp_debug_bestpath(&rn->p);
462
463 if (debug)
464 prefix2str(&rn->p, pfx_buf, sizeof(pfx_buf));
465
466 if (new_best) {
467 mpath_count++;
468 if (new_best != old_best)
18ee8310 469 bgp_path_info_mpath_dequeue(new_best);
d62a17ae 470 maxpaths = (new_best->peer->sort == BGP_PEER_IBGP)
471 ? mpath_cfg->maxpaths_ibgp
472 : mpath_cfg->maxpaths_ebgp;
473 }
474
475 if (old_best) {
18ee8310
DS
476 cur_mpath = bgp_path_info_mpath_first(old_best);
477 old_mpath_count = bgp_path_info_mpath_count(old_best);
478 bgp_path_info_mpath_count_set(old_best, 0);
479 bgp_path_info_mpath_dequeue(old_best);
d62a17ae 480 }
481
482 if (debug)
483 zlog_debug(
484 "%s: starting mpath update, newbest %s num candidates %d old-mpath-count %d",
485 pfx_buf, new_best ? new_best->peer->host : "NONE",
cc35bc7c 486 mp_list ? listcount(mp_list) : 0, old_mpath_count);
d62a17ae 487
488 /*
489 * We perform an ordered walk through both lists in parallel.
490 * The reason for the ordered walk is that if there are paths
491 * that were previously multipaths and are still multipaths, the walk
492 * should encounter them in both lists at the same time. Otherwise
493 * there will be paths that are in one list or another, and we
494 * will deal with these separately.
495 *
496 * Note that new_best might be somewhere in the mp_list, so we need
497 * to skip over it
498 */
499 while (mp_node || cur_mpath) {
4b7e6066 500 struct bgp_path_info *tmp_info;
d62a17ae 501
502 /*
503 * We can bail out of this loop if all existing paths on the
504 * multipath list have been visited (for cleanup purposes) and
505 * the maxpath requirement is fulfulled
506 */
507 if (!cur_mpath && (mpath_count >= maxpaths))
508 break;
509
510 mp_next_node = mp_node ? listnextnode(mp_node) : NULL;
18ee8310
DS
511 next_mpath =
512 cur_mpath ? bgp_path_info_mpath_next(cur_mpath) : NULL;
d62a17ae 513 tmp_info = mp_node ? listgetdata(mp_node) : NULL;
514
515 if (debug)
516 zlog_debug(
517 "%s: comparing candidate %s with existing mpath %s",
518 pfx_buf,
519 tmp_info ? tmp_info->peer->host : "NONE",
520 cur_mpath ? cur_mpath->peer->host : "NONE");
521
522 /*
523 * If equal, the path was a multipath and is still a multipath.
524 * Insert onto new multipath list if maxpaths allows.
525 */
526 if (mp_node && (listgetdata(mp_node) == cur_mpath)) {
527 list_delete_node(mp_list, mp_node);
18ee8310 528 bgp_path_info_mpath_dequeue(cur_mpath);
d62a17ae 529 if ((mpath_count < maxpaths)
fa3bf3a2 530 && prev_mpath
18ee8310
DS
531 && bgp_path_info_nexthop_cmp(prev_mpath,
532 cur_mpath)) {
533 bgp_path_info_mpath_enqueue(prev_mpath,
534 cur_mpath);
d62a17ae 535 prev_mpath = cur_mpath;
536 mpath_count++;
537 if (debug) {
18ee8310 538 bgp_path_info_path_with_addpath_rx_str(
d62a17ae 539 cur_mpath, path_buf);
540 zlog_debug(
541 "%s: %s is still multipath, cur count %d",
542 pfx_buf, path_buf, mpath_count);
543 }
544 } else {
545 mpath_changed = 1;
546 if (debug) {
18ee8310 547 bgp_path_info_path_with_addpath_rx_str(
d62a17ae 548 cur_mpath, path_buf);
549 zlog_debug(
550 "%s: remove mpath %s nexthop %s, cur count %d",
551 pfx_buf, path_buf,
552 inet_ntop(AF_INET,
553 &cur_mpath->attr
554 ->nexthop,
555 nh_buf[0],
556 sizeof(nh_buf[0])),
557 mpath_count);
558 }
559 }
560 mp_node = mp_next_node;
561 cur_mpath = next_mpath;
562 continue;
563 }
564
565 if (cur_mpath
566 && (!mp_node
18ee8310
DS
567 || (bgp_path_info_mpath_cmp(cur_mpath,
568 listgetdata(mp_node))
d62a17ae 569 < 0))) {
570 /*
571 * If here, we have an old multipath and either the
572 * mp_list
573 * is finished or the next mp_node points to a later
574 * multipath, so we need to purge this path from the
575 * multipath list
576 */
18ee8310 577 bgp_path_info_mpath_dequeue(cur_mpath);
d62a17ae 578 mpath_changed = 1;
579 if (debug) {
18ee8310
DS
580 bgp_path_info_path_with_addpath_rx_str(
581 cur_mpath, path_buf);
d62a17ae 582 zlog_debug(
583 "%s: remove mpath %s nexthop %s, cur count %d",
584 pfx_buf, path_buf,
585 inet_ntop(AF_INET,
586 &cur_mpath->attr->nexthop,
587 nh_buf[0], sizeof(nh_buf[0])),
588 mpath_count);
589 }
590 cur_mpath = next_mpath;
591 } else {
592 /*
593 * If here, we have a path on the mp_list that was not
594 * previously
595 * a multipath (due to non-equivalance or maxpaths
596 * exceeded),
597 * or the matching multipath is sorted later in the
598 * multipath
599 * list. Before we enqueue the path on the new multipath
600 * list,
601 * make sure its not on the old_best multipath list or
602 * referenced
603 * via next_mpath:
604 * - If next_mpath points to this new path, update
605 * next_mpath to
606 * point to the multipath after this one
607 * - Dequeue the path from the multipath list just to
608 * make sure
609 */
610 new_mpath = listgetdata(mp_node);
611 list_delete_node(mp_list, mp_node);
2165e2ea
QY
612 assert(new_mpath);
613 assert(prev_mpath);
d62a17ae 614 if ((mpath_count < maxpaths) && (new_mpath != new_best)
18ee8310
DS
615 && bgp_path_info_nexthop_cmp(prev_mpath,
616 new_mpath)) {
18ee8310 617 bgp_path_info_mpath_dequeue(new_mpath);
d62a17ae 618
18ee8310
DS
619 bgp_path_info_mpath_enqueue(prev_mpath,
620 new_mpath);
d62a17ae 621 prev_mpath = new_mpath;
622 mpath_changed = 1;
623 mpath_count++;
624 if (debug) {
18ee8310 625 bgp_path_info_path_with_addpath_rx_str(
d62a17ae 626 new_mpath, path_buf);
627 zlog_debug(
628 "%s: add mpath %s nexthop %s, cur count %d",
629 pfx_buf, path_buf,
630 inet_ntop(AF_INET,
631 &new_mpath->attr
632 ->nexthop,
633 nh_buf[0],
634 sizeof(nh_buf[0])),
635 mpath_count);
636 }
637 }
638 mp_node = mp_next_node;
639 }
640 }
641
642 if (new_best) {
643 if (debug)
644 zlog_debug(
645 "%s: New mpath count (incl newbest) %d mpath-change %s",
646 pfx_buf, mpath_count,
647 mpath_changed ? "YES" : "NO");
648
18ee8310 649 bgp_path_info_mpath_count_set(new_best, mpath_count - 1);
d62a17ae 650 if (mpath_changed
18ee8310 651 || (bgp_path_info_mpath_count(new_best) != old_mpath_count))
1defdda8 652 SET_FLAG(new_best->flags, BGP_PATH_MULTIPATH_CHG);
d62a17ae 653 }
de8d5dff 654}
6918e74b
JB
655
656/*
657 * bgp_mp_dmed_deselect
658 *
1defdda8 659 * Clean up multipath information for BGP_PATH_DMED_SELECTED path that
6918e74b
JB
660 * is not selected as best path
661 */
4b7e6066 662void bgp_mp_dmed_deselect(struct bgp_path_info *dmed_best)
6918e74b 663{
4b7e6066 664 struct bgp_path_info *mpinfo, *mpnext;
6918e74b 665
d62a17ae 666 if (!dmed_best)
667 return;
6918e74b 668
18ee8310 669 for (mpinfo = bgp_path_info_mpath_first(dmed_best); mpinfo;
d62a17ae 670 mpinfo = mpnext) {
18ee8310
DS
671 mpnext = bgp_path_info_mpath_next(mpinfo);
672 bgp_path_info_mpath_dequeue(mpinfo);
d62a17ae 673 }
6918e74b 674
18ee8310 675 bgp_path_info_mpath_count_set(dmed_best, 0);
1defdda8 676 UNSET_FLAG(dmed_best->flags, BGP_PATH_MULTIPATH_CHG);
831600c3 677 assert(bgp_path_info_mpath_first(dmed_best) == NULL);
6918e74b 678}
0b597ef0
JB
679
680/*
18ee8310 681 * bgp_path_info_mpath_aggregate_update
0b597ef0
JB
682 *
683 * Set the multipath aggregate attribute. We need to see if the
684 * aggregate has changed and then set the ATTR_CHANGED flag on the
685 * bestpath info so that a peer update will be generated. The
686 * change is detected by generating the current attribute,
687 * interning it, and then comparing the interned pointer with the
688 * current value. We can skip this generate/compare step if there
689 * is no change in multipath selection and no attribute change in
690 * any multipath.
691 */
18ee8310
DS
692void bgp_path_info_mpath_aggregate_update(struct bgp_path_info *new_best,
693 struct bgp_path_info *old_best)
0b597ef0 694{
4b7e6066 695 struct bgp_path_info *mpinfo;
d62a17ae 696 struct aspath *aspath;
697 struct aspath *asmerge;
698 struct attr *new_attr, *old_attr;
d7c0a89a 699 uint8_t origin;
d62a17ae 700 struct community *community, *commerge;
701 struct ecommunity *ecomm, *ecommerge;
702 struct lcommunity *lcomm, *lcommerge;
703 struct attr attr = {0};
704
705 if (old_best && (old_best != new_best)
18ee8310 706 && (old_attr = bgp_path_info_mpath_attr(old_best))) {
d62a17ae 707 bgp_attr_unintern(&old_attr);
18ee8310 708 bgp_path_info_mpath_attr_set(old_best, NULL);
d62a17ae 709 }
710
711 if (!new_best)
712 return;
713
18ee8310
DS
714 if (!bgp_path_info_mpath_count(new_best)) {
715 if ((new_attr = bgp_path_info_mpath_attr(new_best))) {
d62a17ae 716 bgp_attr_unintern(&new_attr);
18ee8310 717 bgp_path_info_mpath_attr_set(new_best, NULL);
1defdda8 718 SET_FLAG(new_best->flags, BGP_PATH_ATTR_CHANGED);
d62a17ae 719 }
720 return;
721 }
722
6f4f49b2 723 attr = *new_best->attr;
d62a17ae 724
9d303b37
DL
725 if (new_best->peer && bgp_flag_check(new_best->peer->bgp,
726 BGP_FLAG_MULTIPATH_RELAX_AS_SET)) {
d62a17ae 727
728 /* aggregate attribute from multipath constituents */
729 aspath = aspath_dup(attr.aspath);
730 origin = attr.origin;
731 community =
732 attr.community ? community_dup(attr.community) : NULL;
733 ecomm = (attr.ecommunity) ? ecommunity_dup(attr.ecommunity)
734 : NULL;
735 lcomm = (attr.lcommunity) ? lcommunity_dup(attr.lcommunity)
736 : NULL;
737
18ee8310
DS
738 for (mpinfo = bgp_path_info_mpath_first(new_best); mpinfo;
739 mpinfo = bgp_path_info_mpath_next(mpinfo)) {
d62a17ae 740 asmerge =
741 aspath_aggregate(aspath, mpinfo->attr->aspath);
742 aspath_free(aspath);
743 aspath = asmerge;
744
745 if (origin < mpinfo->attr->origin)
746 origin = mpinfo->attr->origin;
747
748 if (mpinfo->attr->community) {
749 if (community) {
750 commerge = community_merge(
751 community,
752 mpinfo->attr->community);
753 community =
754 community_uniq_sort(commerge);
3c1f53de 755 community_free(&commerge);
d62a17ae 756 } else
757 community = community_dup(
758 mpinfo->attr->community);
759 }
760
761 if (mpinfo->attr->ecommunity) {
762 if (ecomm) {
763 ecommerge = ecommunity_merge(
764 ecomm,
765 mpinfo->attr->ecommunity);
766 ecomm = ecommunity_uniq_sort(ecommerge);
767 ecommunity_free(&ecommerge);
768 } else
769 ecomm = ecommunity_dup(
770 mpinfo->attr->ecommunity);
771 }
772 if (mpinfo->attr->lcommunity) {
773 if (lcomm) {
774 lcommerge = lcommunity_merge(
775 lcomm,
776 mpinfo->attr->lcommunity);
777 lcomm = lcommunity_uniq_sort(lcommerge);
778 lcommunity_free(&lcommerge);
779 } else
780 lcomm = lcommunity_dup(
781 mpinfo->attr->lcommunity);
782 }
783 }
784
785 attr.aspath = aspath;
786 attr.origin = origin;
787 if (community) {
788 attr.community = community;
789 attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES);
790 }
791 if (ecomm) {
792 attr.ecommunity = ecomm;
793 attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES);
794 }
44f12f20
RW
795 if (lcomm) {
796 attr.lcommunity = lcomm;
797 attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES);
798 }
d62a17ae 799
800 /* Zap multipath attr nexthop so we set nexthop to self */
801 attr.nexthop.s_addr = 0;
802 memset(&attr.mp_nexthop_global, 0, sizeof(struct in6_addr));
803
804 /* TODO: should we set ATOMIC_AGGREGATE and AGGREGATOR? */
805 }
806
807 new_attr = bgp_attr_intern(&attr);
808
18ee8310
DS
809 if (new_attr != bgp_path_info_mpath_attr(new_best)) {
810 if ((old_attr = bgp_path_info_mpath_attr(new_best)))
d62a17ae 811 bgp_attr_unintern(&old_attr);
18ee8310 812 bgp_path_info_mpath_attr_set(new_best, new_attr);
1defdda8 813 SET_FLAG(new_best->flags, BGP_PATH_ATTR_CHANGED);
d62a17ae 814 } else
815 bgp_attr_unintern(&new_attr);
0b597ef0 816}