blob: 9eaf3c26aee66302d2f7c5a68551b5de0bacfe9e [file] [log] [blame]
Josh Bailey165b5ff2011-07-20 20:43:22 -07001/* $QuaggaId: Format:%an, %ai, %h$ $
2 *
3 * BGP Multipath
4 * Copyright (C) 2010 Google Inc.
5 *
6 * This file is part of Quagga
7 *
8 * Quagga is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
12 *
13 * Quagga is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with Quagga; see the file COPYING. If not, write to the Free
20 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 * 02111-1307, USA.
22 */
23
24#include <zebra.h>
25
26#include "command.h"
Josh Bailey96450fa2011-07-20 20:45:12 -070027#include "prefix.h"
28#include "linklist.h"
29#include "sockunion.h"
Josh Baileyde8d5df2011-07-20 20:46:01 -070030#include "memory.h"
Donald Sharp04907292016-01-07 10:03:01 -050031#include "filter.h"
Josh Bailey165b5ff2011-07-20 20:43:22 -070032
33#include "bgpd/bgpd.h"
Josh Bailey96450fa2011-07-20 20:45:12 -070034#include "bgpd/bgp_table.h"
35#include "bgpd/bgp_route.h"
36#include "bgpd/bgp_attr.h"
Josh Baileyde8d5df2011-07-20 20:46:01 -070037#include "bgpd/bgp_debug.h"
Josh Bailey0b597ef2011-07-20 20:49:11 -070038#include "bgpd/bgp_aspath.h"
39#include "bgpd/bgp_community.h"
40#include "bgpd/bgp_ecommunity.h"
Josh Bailey165b5ff2011-07-20 20:43:22 -070041#include "bgpd/bgp_mpath.h"
42
Paul Jakma6d4742b2015-11-25 17:14:37 +000043bool
44bgp_mpath_is_configured_sort (struct bgp *bgp, bgp_peer_sort_t sort,
45 afi_t afi, safi_t safi)
46{
47 struct bgp_maxpaths_cfg *cfg = &bgp->maxpaths[afi][safi];
48
49 /* XXX: BGP_DEFAULT_MAXPATHS is 1, and this test only seems to make sense
50 * if if it stays 1, so not sure the DEFAULT define is that useful.
51 */
52 switch (sort)
53 {
54 case BGP_PEER_IBGP:
55 return cfg->maxpaths_ibgp != BGP_DEFAULT_MAXPATHS;
56 case BGP_PEER_EBGP:
57 return cfg->maxpaths_ebgp != BGP_DEFAULT_MAXPATHS;
58 default:
59 return false;
60 }
61}
62
63bool
64bgp_mpath_is_configured (struct bgp *bgp, afi_t afi, safi_t safi)
65{
66 return bgp_mpath_is_configured_sort (bgp, BGP_PEER_IBGP, afi, safi)
67 || bgp_mpath_is_configured_sort (bgp, BGP_PEER_EBGP, afi, safi);
68}
69
Josh Bailey165b5ff2011-07-20 20:43:22 -070070/*
71 * bgp_maximum_paths_set
72 *
73 * Record maximum-paths configuration for BGP instance
74 */
75int
76bgp_maximum_paths_set (struct bgp *bgp, afi_t afi, safi_t safi,
77 int peertype, u_int16_t maxpaths)
78{
79 if (!bgp || (afi >= AFI_MAX) || (safi >= SAFI_MAX))
80 return -1;
81
82 switch (peertype)
83 {
84 case BGP_PEER_IBGP:
85 bgp->maxpaths[afi][safi].maxpaths_ibgp = maxpaths;
86 break;
87 case BGP_PEER_EBGP:
88 bgp->maxpaths[afi][safi].maxpaths_ebgp = maxpaths;
89 break;
90 default:
91 return -1;
92 }
93
94 return 0;
95}
96
97/*
98 * bgp_maximum_paths_unset
99 *
100 * Remove maximum-paths configuration from BGP instance
101 */
102int
103bgp_maximum_paths_unset (struct bgp *bgp, afi_t afi, safi_t safi,
104 int peertype)
105{
106 if (!bgp || (afi >= AFI_MAX) || (safi >= SAFI_MAX))
107 return -1;
108
109 switch (peertype)
110 {
111 case BGP_PEER_IBGP:
112 bgp->maxpaths[afi][safi].maxpaths_ibgp = BGP_DEFAULT_MAXPATHS;
113 break;
114 case BGP_PEER_EBGP:
115 bgp->maxpaths[afi][safi].maxpaths_ebgp = BGP_DEFAULT_MAXPATHS;
116 break;
117 default:
118 return -1;
119 }
120
121 return 0;
122}
Josh Bailey96450fa2011-07-20 20:45:12 -0700123
124/*
125 * bgp_info_nexthop_cmp
126 *
127 * Compare the nexthops of two paths. Return value is less than, equal to,
128 * or greater than zero if bi1 is respectively less than, equal to,
129 * or greater than bi2.
130 */
131static int
132bgp_info_nexthop_cmp (struct bgp_info *bi1, struct bgp_info *bi2)
133{
134 struct attr_extra *ae1, *ae2;
135 int compare;
136
Josh Bailey0b597ef2011-07-20 20:49:11 -0700137 ae1 = bi1->attr->extra;
138 ae2 = bi2->attr->extra;
Josh Bailey96450fa2011-07-20 20:45:12 -0700139
140 compare = IPV4_ADDR_CMP (&bi1->attr->nexthop, &bi2->attr->nexthop);
141
142 if (!compare && ae1 && ae2 && (ae1->mp_nexthop_len == ae2->mp_nexthop_len))
143 {
144 switch (ae1->mp_nexthop_len)
145 {
146 case 4:
147 case 12:
148 compare = IPV4_ADDR_CMP (&ae1->mp_nexthop_global_in,
149 &ae2->mp_nexthop_global_in);
150 break;
151#ifdef HAVE_IPV6
152 case 16:
153 compare = IPV6_ADDR_CMP (&ae1->mp_nexthop_global,
154 &ae2->mp_nexthop_global);
155 break;
156 case 32:
157 compare = IPV6_ADDR_CMP (&ae1->mp_nexthop_global,
158 &ae2->mp_nexthop_global);
159 if (!compare)
160 compare = IPV6_ADDR_CMP (&ae1->mp_nexthop_local,
161 &ae2->mp_nexthop_local);
162 break;
163#endif /* HAVE_IPV6 */
164 }
165 }
166
167 return compare;
168}
169
170/*
171 * bgp_info_mpath_cmp
172 *
173 * This function determines our multipath list ordering. By ordering
174 * the list we can deterministically select which paths are included
175 * in the multipath set. The ordering also helps in detecting changes
176 * in the multipath selection so we can detect whether to send an
177 * update to zebra.
178 *
179 * The order of paths is determined first by received nexthop, and then
180 * by peer address if the nexthops are the same.
181 */
182static int
183bgp_info_mpath_cmp (void *val1, void *val2)
184{
185 struct bgp_info *bi1, *bi2;
186 int compare;
187
188 bi1 = val1;
189 bi2 = val2;
190
191 compare = bgp_info_nexthop_cmp (bi1, bi2);
192
193 if (!compare)
194 compare = sockunion_cmp (bi1->peer->su_remote, bi2->peer->su_remote);
195
196 return compare;
197}
198
199/*
200 * bgp_mp_list_init
201 *
202 * Initialize the mp_list, which holds the list of multipaths
203 * selected by bgp_best_selection
204 */
205void
206bgp_mp_list_init (struct list *mp_list)
207{
208 assert (mp_list);
209 memset (mp_list, 0, sizeof (struct list));
210 mp_list->cmp = bgp_info_mpath_cmp;
211}
212
213/*
214 * bgp_mp_list_clear
215 *
216 * Clears all entries out of the mp_list
217 */
218void
219bgp_mp_list_clear (struct list *mp_list)
220{
221 assert (mp_list);
222 list_delete_all_node (mp_list);
223}
224
225/*
226 * bgp_mp_list_add
227 *
228 * Adds a multipath entry to the mp_list
229 */
230void
231bgp_mp_list_add (struct list *mp_list, struct bgp_info *mpinfo)
232{
233 assert (mp_list && mpinfo);
234 listnode_add_sort (mp_list, mpinfo);
235}
Josh Baileyde8d5df2011-07-20 20:46:01 -0700236
237/*
238 * bgp_info_mpath_new
239 *
240 * Allocate and zero memory for a new bgp_info_mpath element
241 */
242static struct bgp_info_mpath *
243bgp_info_mpath_new (void)
244{
245 struct bgp_info_mpath *new_mpath;
246 new_mpath = XCALLOC (MTYPE_BGP_MPATH_INFO, sizeof (struct bgp_info_mpath));
247 return new_mpath;
248}
249
250/*
251 * bgp_info_mpath_free
252 *
253 * Release resources for a bgp_info_mpath element and zero out pointer
254 */
255void
256bgp_info_mpath_free (struct bgp_info_mpath **mpath)
257{
258 if (mpath && *mpath)
259 {
Josh Bailey0b597ef2011-07-20 20:49:11 -0700260 if ((*mpath)->mp_attr)
David Lamparterfac3c242012-04-28 22:37:20 +0200261 bgp_attr_unintern (&(*mpath)->mp_attr);
Josh Baileyde8d5df2011-07-20 20:46:01 -0700262 XFREE (MTYPE_BGP_MPATH_INFO, *mpath);
263 *mpath = NULL;
264 }
265}
266
267/*
268 * bgp_info_mpath_get
269 *
270 * Fetch the mpath element for the given bgp_info. Used for
271 * doing lazy allocation.
272 */
273static struct bgp_info_mpath *
274bgp_info_mpath_get (struct bgp_info *binfo)
275{
276 struct bgp_info_mpath *mpath;
277 if (!binfo->mpath)
278 {
279 mpath = bgp_info_mpath_new();
280 if (!mpath)
281 return NULL;
282 binfo->mpath = mpath;
283 mpath->mp_info = binfo;
284 }
285 return binfo->mpath;
286}
287
288/*
289 * bgp_info_mpath_enqueue
290 *
291 * Enqueue a path onto the multipath list given the previous multipath
292 * list entry
293 */
294static void
295bgp_info_mpath_enqueue (struct bgp_info *prev_info, struct bgp_info *binfo)
296{
297 struct bgp_info_mpath *prev, *mpath;
298
299 prev = bgp_info_mpath_get (prev_info);
300 mpath = bgp_info_mpath_get (binfo);
301 if (!prev || !mpath)
302 return;
303
304 mpath->mp_next = prev->mp_next;
305 mpath->mp_prev = prev;
306 if (prev->mp_next)
307 prev->mp_next->mp_prev = mpath;
308 prev->mp_next = mpath;
309
310 SET_FLAG (binfo->flags, BGP_INFO_MULTIPATH);
311}
312
313/*
314 * bgp_info_mpath_dequeue
315 *
316 * Remove a path from the multipath list
317 */
318void
319bgp_info_mpath_dequeue (struct bgp_info *binfo)
320{
321 struct bgp_info_mpath *mpath = binfo->mpath;
322 if (!mpath)
323 return;
324 if (mpath->mp_prev)
325 mpath->mp_prev->mp_next = mpath->mp_next;
326 if (mpath->mp_next)
327 mpath->mp_next->mp_prev = mpath->mp_prev;
328 mpath->mp_next = mpath->mp_prev = NULL;
329 UNSET_FLAG (binfo->flags, BGP_INFO_MULTIPATH);
330}
331
332/*
333 * bgp_info_mpath_next
334 *
335 * Given a bgp_info, return the next multipath entry
336 */
337struct bgp_info *
338bgp_info_mpath_next (struct bgp_info *binfo)
339{
340 if (!binfo->mpath || !binfo->mpath->mp_next)
341 return NULL;
342 return binfo->mpath->mp_next->mp_info;
343}
344
345/*
346 * bgp_info_mpath_first
347 *
348 * Given bestpath bgp_info, return the first multipath entry.
349 */
350struct bgp_info *
351bgp_info_mpath_first (struct bgp_info *binfo)
352{
353 return bgp_info_mpath_next (binfo);
354}
355
356/*
357 * bgp_info_mpath_count
358 *
359 * Given the bestpath bgp_info, return the number of multipath entries
360 */
361u_int32_t
362bgp_info_mpath_count (struct bgp_info *binfo)
363{
364 if (!binfo->mpath)
365 return 0;
366 return binfo->mpath->mp_count;
367}
368
369/*
370 * bgp_info_mpath_count_set
371 *
372 * Sets the count of multipaths into bestpath's mpath element
373 */
374static void
375bgp_info_mpath_count_set (struct bgp_info *binfo, u_int32_t count)
376{
377 struct bgp_info_mpath *mpath;
378 if (!count && !binfo->mpath)
379 return;
380 mpath = bgp_info_mpath_get (binfo);
381 if (!mpath)
382 return;
383 mpath->mp_count = count;
384}
385
386/*
Josh Bailey0b597ef2011-07-20 20:49:11 -0700387 * bgp_info_mpath_attr
388 *
389 * Given bestpath bgp_info, return aggregated attribute set used
390 * for advertising the multipath route
391 */
392struct attr *
393bgp_info_mpath_attr (struct bgp_info *binfo)
394{
395 if (!binfo->mpath)
396 return NULL;
397 return binfo->mpath->mp_attr;
398}
399
400/*
401 * bgp_info_mpath_attr_set
402 *
403 * Sets the aggregated attribute into bestpath's mpath element
404 */
405static void
406bgp_info_mpath_attr_set (struct bgp_info *binfo, struct attr *attr)
407{
408 struct bgp_info_mpath *mpath;
409 if (!attr && !binfo->mpath)
410 return;
411 mpath = bgp_info_mpath_get (binfo);
412 if (!mpath)
413 return;
414 mpath->mp_attr = attr;
415}
416
417/*
Josh Baileyde8d5df2011-07-20 20:46:01 -0700418 * bgp_info_mpath_update
419 *
420 * Compare and sync up the multipath list with the mp_list generated by
421 * bgp_best_selection
422 */
423void
424bgp_info_mpath_update (struct bgp_node *rn, struct bgp_info *new_best,
425 struct bgp_info *old_best, struct list *mp_list,
Paul Jakma6d4742b2015-11-25 17:14:37 +0000426 afi_t afi, safi_t safi)
Josh Baileyde8d5df2011-07-20 20:46:01 -0700427{
428 u_int16_t maxpaths, mpath_count, old_mpath_count;
429 struct listnode *mp_node, *mp_next_node;
430 struct bgp_info *cur_mpath, *new_mpath, *next_mpath, *prev_mpath;
431 int mpath_changed, debug;
432 char pfx_buf[INET_ADDRSTRLEN], nh_buf[2][INET_ADDRSTRLEN];
433
434 mpath_changed = 0;
435 maxpaths = BGP_DEFAULT_MAXPATHS;
436 mpath_count = 0;
437 cur_mpath = NULL;
438 old_mpath_count = 0;
439 prev_mpath = new_best;
440 mp_node = listhead (mp_list);
Paul Jakma6d4742b2015-11-25 17:14:37 +0000441 struct bgp_maxpaths_cfg *mpath_cfg;
Josh Baileyde8d5df2011-07-20 20:46:01 -0700442 debug = BGP_DEBUG (events, EVENTS);
443
Paul Jakma6d4742b2015-11-25 17:14:37 +0000444 mpath_cfg = &new_best->peer->bgp->maxpaths[afi][safi];
445
Josh Baileyde8d5df2011-07-20 20:46:01 -0700446 if (debug)
447 prefix2str (&rn->p, pfx_buf, sizeof (pfx_buf));
448
449 if (new_best)
450 {
451 mpath_count++;
452 if (new_best != old_best)
453 bgp_info_mpath_dequeue (new_best);
Jorge Boncompte [DTI2]6d85b152012-05-07 16:52:54 +0000454 maxpaths = (new_best->peer->sort == BGP_PEER_IBGP) ?
Josh Baileyde8d5df2011-07-20 20:46:01 -0700455 mpath_cfg->maxpaths_ibgp : mpath_cfg->maxpaths_ebgp;
456 }
457
458 if (old_best)
459 {
460 cur_mpath = bgp_info_mpath_first (old_best);
461 old_mpath_count = bgp_info_mpath_count (old_best);
462 bgp_info_mpath_count_set (old_best, 0);
463 bgp_info_mpath_dequeue (old_best);
464 }
465
466 /*
467 * We perform an ordered walk through both lists in parallel.
468 * The reason for the ordered walk is that if there are paths
469 * that were previously multipaths and are still multipaths, the walk
470 * should encounter them in both lists at the same time. Otherwise
471 * there will be paths that are in one list or another, and we
472 * will deal with these separately.
473 *
474 * Note that new_best might be somewhere in the mp_list, so we need
475 * to skip over it
476 */
477 while (mp_node || cur_mpath)
478 {
479 /*
480 * We can bail out of this loop if all existing paths on the
481 * multipath list have been visited (for cleanup purposes) and
482 * the maxpath requirement is fulfulled
483 */
484 if (!cur_mpath && (mpath_count >= maxpaths))
485 break;
486
487 mp_next_node = mp_node ? listnextnode (mp_node) : NULL;
488 next_mpath = cur_mpath ? bgp_info_mpath_next (cur_mpath) : NULL;
489
490 /*
491 * If equal, the path was a multipath and is still a multipath.
492 * Insert onto new multipath list if maxpaths allows.
493 */
494 if (mp_node && (listgetdata (mp_node) == cur_mpath))
495 {
496 list_delete_node (mp_list, mp_node);
497 bgp_info_mpath_dequeue (cur_mpath);
498 if ((mpath_count < maxpaths) &&
499 bgp_info_nexthop_cmp (prev_mpath, cur_mpath))
500 {
501 bgp_info_mpath_enqueue (prev_mpath, cur_mpath);
502 prev_mpath = cur_mpath;
503 mpath_count++;
504 }
505 else
506 {
507 mpath_changed = 1;
508 if (debug)
509 zlog_debug ("%s remove mpath nexthop %s peer %s", pfx_buf,
510 inet_ntop (AF_INET, &cur_mpath->attr->nexthop,
511 nh_buf[0], sizeof (nh_buf[0])),
512 sockunion2str (cur_mpath->peer->su_remote,
513 nh_buf[1], sizeof (nh_buf[1])));
514 }
515 mp_node = mp_next_node;
516 cur_mpath = next_mpath;
517 continue;
518 }
519
520 if (cur_mpath && (!mp_node ||
521 (bgp_info_mpath_cmp (cur_mpath,
522 listgetdata (mp_node)) < 0)))
523 {
524 /*
525 * If here, we have an old multipath and either the mp_list
526 * is finished or the next mp_node points to a later
527 * multipath, so we need to purge this path from the
528 * multipath list
529 */
530 bgp_info_mpath_dequeue (cur_mpath);
531 mpath_changed = 1;
532 if (debug)
533 zlog_debug ("%s remove mpath nexthop %s peer %s", pfx_buf,
534 inet_ntop (AF_INET, &cur_mpath->attr->nexthop,
535 nh_buf[0], sizeof (nh_buf[0])),
536 sockunion2str (cur_mpath->peer->su_remote,
537 nh_buf[1], sizeof (nh_buf[1])));
538 cur_mpath = next_mpath;
539 }
540 else
541 {
542 /*
543 * If here, we have a path on the mp_list that was not previously
544 * a multipath (due to non-equivalance or maxpaths exceeded),
545 * or the matching multipath is sorted later in the multipath
546 * list. Before we enqueue the path on the new multipath list,
547 * make sure its not on the old_best multipath list or referenced
548 * via next_mpath:
549 * - If next_mpath points to this new path, update next_mpath to
550 * point to the multipath after this one
551 * - Dequeue the path from the multipath list just to make sure
552 */
553 new_mpath = listgetdata (mp_node);
554 list_delete_node (mp_list, mp_node);
Josh Baileyde8d5df2011-07-20 20:46:01 -0700555 if ((mpath_count < maxpaths) && (new_mpath != new_best) &&
556 bgp_info_nexthop_cmp (prev_mpath, new_mpath))
557 {
Josh Bailey78d92e12011-07-20 20:51:07 -0700558 if (new_mpath == next_mpath)
559 next_mpath = bgp_info_mpath_next (new_mpath);
560 bgp_info_mpath_dequeue (new_mpath);
561
Josh Baileyde8d5df2011-07-20 20:46:01 -0700562 bgp_info_mpath_enqueue (prev_mpath, new_mpath);
563 prev_mpath = new_mpath;
564 mpath_changed = 1;
565 mpath_count++;
566 if (debug)
567 zlog_debug ("%s add mpath nexthop %s peer %s", pfx_buf,
568 inet_ntop (AF_INET, &new_mpath->attr->nexthop,
569 nh_buf[0], sizeof (nh_buf[0])),
570 sockunion2str (new_mpath->peer->su_remote,
571 nh_buf[1], sizeof (nh_buf[1])));
572 }
573 mp_node = mp_next_node;
574 }
575 }
576
577 if (new_best)
578 {
579 bgp_info_mpath_count_set (new_best, mpath_count-1);
580 if (mpath_changed || (bgp_info_mpath_count (new_best) != old_mpath_count))
581 SET_FLAG (new_best->flags, BGP_INFO_MULTIPATH_CHG);
582 }
583}
Josh Bailey6918e742011-07-20 20:48:20 -0700584
585/*
586 * bgp_mp_dmed_deselect
587 *
588 * Clean up multipath information for BGP_INFO_DMED_SELECTED path that
589 * is not selected as best path
590 */
591void
592bgp_mp_dmed_deselect (struct bgp_info *dmed_best)
593{
594 struct bgp_info *mpinfo, *mpnext;
595
596 if (!dmed_best)
597 return;
598
599 for (mpinfo = bgp_info_mpath_first (dmed_best); mpinfo; mpinfo = mpnext)
600 {
601 mpnext = bgp_info_mpath_next (mpinfo);
602 bgp_info_mpath_dequeue (mpinfo);
603 }
604
605 bgp_info_mpath_count_set (dmed_best, 0);
606 UNSET_FLAG (dmed_best->flags, BGP_INFO_MULTIPATH_CHG);
607 assert (bgp_info_mpath_first (dmed_best) == 0);
608}
Josh Bailey0b597ef2011-07-20 20:49:11 -0700609
610/*
611 * bgp_info_mpath_aggregate_update
612 *
613 * Set the multipath aggregate attribute. We need to see if the
614 * aggregate has changed and then set the ATTR_CHANGED flag on the
615 * bestpath info so that a peer update will be generated. The
616 * change is detected by generating the current attribute,
617 * interning it, and then comparing the interned pointer with the
618 * current value. We can skip this generate/compare step if there
619 * is no change in multipath selection and no attribute change in
620 * any multipath.
621 */
622void
623bgp_info_mpath_aggregate_update (struct bgp_info *new_best,
624 struct bgp_info *old_best)
625{
626 struct bgp_info *mpinfo;
627 struct aspath *aspath;
628 struct aspath *asmerge;
629 struct attr *new_attr, *old_attr;
630 u_char origin, attr_chg;
631 struct community *community, *commerge;
632 struct ecommunity *ecomm, *ecommerge;
633 struct attr_extra *ae;
634 struct attr attr = { 0 };
635
636 if (old_best && (old_best != new_best) &&
637 (old_attr = bgp_info_mpath_attr (old_best)))
638 {
David Lamparterfac3c242012-04-28 22:37:20 +0200639 bgp_attr_unintern (&old_attr);
Josh Bailey0b597ef2011-07-20 20:49:11 -0700640 bgp_info_mpath_attr_set (old_best, NULL);
641 }
642
643 if (!new_best)
644 return;
645
646 if (!bgp_info_mpath_count (new_best))
647 {
648 if ((new_attr = bgp_info_mpath_attr (new_best)))
649 {
David Lamparterfac3c242012-04-28 22:37:20 +0200650 bgp_attr_unintern (&new_attr);
Josh Bailey0b597ef2011-07-20 20:49:11 -0700651 bgp_info_mpath_attr_set (new_best, NULL);
652 SET_FLAG (new_best->flags, BGP_INFO_ATTR_CHANGED);
653 }
654 return;
655 }
656
657 /*
658 * Bail out here if the following is true:
659 * - MULTIPATH_CHG bit is not set on new_best, and
Josh Bailey01d7ff02011-07-20 20:52:06 -0700660 * - No change in bestpath, and
Josh Bailey0b597ef2011-07-20 20:49:11 -0700661 * - ATTR_CHANGED bit is not set on new_best or any of the multipaths
662 */
Josh Bailey01d7ff02011-07-20 20:52:06 -0700663 if (!CHECK_FLAG (new_best->flags, BGP_INFO_MULTIPATH_CHG) &&
664 (old_best == new_best))
Josh Bailey0b597ef2011-07-20 20:49:11 -0700665 {
Josh Bailey01d7ff02011-07-20 20:52:06 -0700666 attr_chg = 0;
667
668 if (CHECK_FLAG (new_best->flags, BGP_INFO_ATTR_CHANGED))
669 attr_chg = 1;
670 else
671 for (mpinfo = bgp_info_mpath_first (new_best); mpinfo;
672 mpinfo = bgp_info_mpath_next (mpinfo))
673 {
674 if (CHECK_FLAG (mpinfo->flags, BGP_INFO_ATTR_CHANGED))
675 {
676 attr_chg = 1;
677 break;
678 }
679 }
680
681 if (!attr_chg)
682 {
683 assert (bgp_info_mpath_attr (new_best));
684 return;
685 }
Josh Bailey0b597ef2011-07-20 20:49:11 -0700686 }
687
688 bgp_attr_dup (&attr, new_best->attr);
689
690 /* aggregate attribute from multipath constituents */
691 aspath = aspath_dup (attr.aspath);
692 origin = attr.origin;
693 community = attr.community ? community_dup (attr.community) : NULL;
694 ae = attr.extra;
695 ecomm = (ae && ae->ecommunity) ? ecommunity_dup (ae->ecommunity) : NULL;
696
697 for (mpinfo = bgp_info_mpath_first (new_best); mpinfo;
698 mpinfo = bgp_info_mpath_next (mpinfo))
699 {
700 asmerge = aspath_aggregate (aspath, mpinfo->attr->aspath);
701 aspath_free (aspath);
702 aspath = asmerge;
703
704 if (origin < mpinfo->attr->origin)
705 origin = mpinfo->attr->origin;
706
707 if (mpinfo->attr->community)
708 {
709 if (community)
710 {
711 commerge = community_merge (community, mpinfo->attr->community);
712 community = community_uniq_sort (commerge);
713 community_free (commerge);
714 }
715 else
716 community = community_dup (mpinfo->attr->community);
717 }
718
719 ae = mpinfo->attr->extra;
720 if (ae && ae->ecommunity)
721 {
722 if (ecomm)
723 {
724 ecommerge = ecommunity_merge (ecomm, ae->ecommunity);
725 ecomm = ecommunity_uniq_sort (ecommerge);
David Lamparterfac3c242012-04-28 22:37:20 +0200726 ecommunity_free (&ecommerge);
Josh Bailey0b597ef2011-07-20 20:49:11 -0700727 }
728 else
729 ecomm = ecommunity_dup (ae->ecommunity);
730 }
731 }
732
733 attr.aspath = aspath;
734 attr.origin = origin;
735 if (community)
736 {
737 attr.community = community;
738 attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES);
739 }
740 if (ecomm)
741 {
742 ae = bgp_attr_extra_get (&attr);
743 ae->ecommunity = ecomm;
744 attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_EXT_COMMUNITIES);
745 }
746
747 /* Zap multipath attr nexthop so we set nexthop to self */
748 attr.nexthop.s_addr = 0;
749#ifdef HAVE_IPV6
750 if (attr.extra)
751 memset (&attr.extra->mp_nexthop_global, 0, sizeof (struct in6_addr));
752#endif /* HAVE_IPV6 */
753
754 /* TODO: should we set ATOMIC_AGGREGATE and AGGREGATOR? */
755
756 new_attr = bgp_attr_intern (&attr);
757 bgp_attr_extra_free (&attr);
758
759 if (new_attr != bgp_info_mpath_attr (new_best))
760 {
761 if ((old_attr = bgp_info_mpath_attr (new_best)))
David Lamparterfac3c242012-04-28 22:37:20 +0200762 bgp_attr_unintern (&old_attr);
Josh Bailey0b597ef2011-07-20 20:49:11 -0700763 bgp_info_mpath_attr_set (new_best, new_attr);
764 SET_FLAG (new_best->flags, BGP_INFO_ATTR_CHANGED);
765 }
766 else
David Lamparterfac3c242012-04-28 22:37:20 +0200767 bgp_attr_unintern (&new_attr);
Josh Bailey0b597ef2011-07-20 20:49:11 -0700768}