1 /* $QuaggaId: Format:%an, %ai, %h$ $
4 * Copyright (C) 2010 Google Inc.
6 * This file is part of Quagga
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
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.
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
29 #include "sockunion.h"
34 #include "bgpd/bgpd.h"
35 #include "bgpd/bgp_table.h"
36 #include "bgpd/bgp_route.h"
37 #include "bgpd/bgp_attr.h"
38 #include "bgpd/bgp_debug.h"
39 #include "bgpd/bgp_aspath.h"
40 #include "bgpd/bgp_community.h"
41 #include "bgpd/bgp_ecommunity.h"
42 #include "bgpd/bgp_mpath.h"
45 * bgp_maximum_paths_set
47 * Record maximum-paths configuration for BGP instance
50 bgp_maximum_paths_set (struct bgp
*bgp
, afi_t afi
, safi_t safi
,
51 int peertype
, u_int16_t maxpaths
, u_int16_t options
)
53 if (!bgp
|| (afi
>= AFI_MAX
) || (safi
>= SAFI_MAX
))
59 bgp
->maxpaths
[afi
][safi
].maxpaths_ibgp
= maxpaths
;
60 bgp
->maxpaths
[afi
][safi
].ibgp_flags
|= options
;
63 bgp
->maxpaths
[afi
][safi
].maxpaths_ebgp
= maxpaths
;
73 * bgp_maximum_paths_unset
75 * Remove maximum-paths configuration from BGP instance
78 bgp_maximum_paths_unset (struct bgp
*bgp
, afi_t afi
, safi_t safi
,
81 if (!bgp
|| (afi
>= AFI_MAX
) || (safi
>= SAFI_MAX
))
87 bgp
->maxpaths
[afi
][safi
].maxpaths_ibgp
= MULTIPATH_NUM
;
88 bgp
->maxpaths
[afi
][safi
].ibgp_flags
= 0;
91 bgp
->maxpaths
[afi
][safi
].maxpaths_ebgp
= MULTIPATH_NUM
;
101 * bgp_info_nexthop_cmp
103 * Compare the nexthops of two paths. Return value is less than, equal to,
104 * or greater than zero if bi1 is respectively less than, equal to,
105 * or greater than bi2.
108 bgp_info_nexthop_cmp (struct bgp_info
*bi1
, struct bgp_info
*bi2
)
110 struct attr_extra
*ae1
, *ae2
;
113 ae1
= bi1
->attr
->extra
;
114 ae2
= bi2
->attr
->extra
;
116 compare
= IPV4_ADDR_CMP (&bi1
->attr
->nexthop
, &bi2
->attr
->nexthop
);
118 if (!compare
&& ae1
&& ae2
)
120 if (ae1
->mp_nexthop_len
== ae2
->mp_nexthop_len
)
122 switch (ae1
->mp_nexthop_len
)
124 case BGP_ATTR_NHLEN_IPV4
:
125 case BGP_ATTR_NHLEN_VPNV4
:
126 compare
= IPV4_ADDR_CMP (&ae1
->mp_nexthop_global_in
,
127 &ae2
->mp_nexthop_global_in
);
130 case BGP_ATTR_NHLEN_IPV6_GLOBAL
:
131 compare
= IPV6_ADDR_CMP (&ae1
->mp_nexthop_global
,
132 &ae2
->mp_nexthop_global
);
134 case BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
:
135 compare
= IPV6_ADDR_CMP (&ae1
->mp_nexthop_global
,
136 &ae2
->mp_nexthop_global
);
138 compare
= IPV6_ADDR_CMP (&ae1
->mp_nexthop_local
,
139 &ae2
->mp_nexthop_local
);
141 #endif /* HAVE_IPV6 */
146 /* This can happen if one IPv6 peer sends you global and link-local
147 * nexthops but another IPv6 peer only sends you global
149 else if (ae1
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL
||
150 ae1
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
152 compare
= IPV6_ADDR_CMP (&ae1
->mp_nexthop_global
,
153 &ae2
->mp_nexthop_global
);
156 if (ae1
->mp_nexthop_len
< ae2
->mp_nexthop_len
)
162 #endif /* HAVE_IPV6 */
171 * This function determines our multipath list ordering. By ordering
172 * the list we can deterministically select which paths are included
173 * in the multipath set. The ordering also helps in detecting changes
174 * in the multipath selection so we can detect whether to send an
177 * The order of paths is determined first by received nexthop, and then
178 * by peer address if the nexthops are the same.
181 bgp_info_mpath_cmp (void *val1
, void *val2
)
183 struct bgp_info
*bi1
, *bi2
;
189 compare
= bgp_info_nexthop_cmp (bi1
, bi2
);
193 if (!bi1
->peer
->su_remote
&& !bi2
->peer
->su_remote
)
195 else if (!bi1
->peer
->su_remote
)
197 else if (!bi2
->peer
->su_remote
)
200 compare
= sockunion_cmp (bi1
->peer
->su_remote
, bi2
->peer
->su_remote
);
209 * Initialize the mp_list, which holds the list of multipaths
210 * selected by bgp_best_selection
213 bgp_mp_list_init (struct list
*mp_list
)
216 memset (mp_list
, 0, sizeof (struct list
));
217 mp_list
->cmp
= bgp_info_mpath_cmp
;
223 * Clears all entries out of the mp_list
226 bgp_mp_list_clear (struct list
*mp_list
)
229 list_delete_all_node (mp_list
);
235 * Adds a multipath entry to the mp_list
238 bgp_mp_list_add (struct list
*mp_list
, struct bgp_info
*mpinfo
)
240 assert (mp_list
&& mpinfo
);
241 listnode_add_sort (mp_list
, mpinfo
);
247 * Allocate and zero memory for a new bgp_info_mpath element
249 static struct bgp_info_mpath
*
250 bgp_info_mpath_new (void)
252 struct bgp_info_mpath
*new_mpath
;
253 new_mpath
= XCALLOC (MTYPE_BGP_MPATH_INFO
, sizeof (struct bgp_info_mpath
));
258 * bgp_info_mpath_free
260 * Release resources for a bgp_info_mpath element and zero out pointer
263 bgp_info_mpath_free (struct bgp_info_mpath
**mpath
)
267 if ((*mpath
)->mp_attr
)
268 bgp_attr_unintern (&(*mpath
)->mp_attr
);
269 XFREE (MTYPE_BGP_MPATH_INFO
, *mpath
);
277 * Fetch the mpath element for the given bgp_info. Used for
278 * doing lazy allocation.
280 static struct bgp_info_mpath
*
281 bgp_info_mpath_get (struct bgp_info
*binfo
)
283 struct bgp_info_mpath
*mpath
;
286 mpath
= bgp_info_mpath_new();
289 binfo
->mpath
= mpath
;
290 mpath
->mp_info
= binfo
;
296 * bgp_info_mpath_enqueue
298 * Enqueue a path onto the multipath list given the previous multipath
302 bgp_info_mpath_enqueue (struct bgp_info
*prev_info
, struct bgp_info
*binfo
)
304 struct bgp_info_mpath
*prev
, *mpath
;
306 prev
= bgp_info_mpath_get (prev_info
);
307 mpath
= bgp_info_mpath_get (binfo
);
311 mpath
->mp_next
= prev
->mp_next
;
312 mpath
->mp_prev
= prev
;
314 prev
->mp_next
->mp_prev
= mpath
;
315 prev
->mp_next
= mpath
;
317 SET_FLAG (binfo
->flags
, BGP_INFO_MULTIPATH
);
321 * bgp_info_mpath_dequeue
323 * Remove a path from the multipath list
326 bgp_info_mpath_dequeue (struct bgp_info
*binfo
)
328 struct bgp_info_mpath
*mpath
= binfo
->mpath
;
332 mpath
->mp_prev
->mp_next
= mpath
->mp_next
;
334 mpath
->mp_next
->mp_prev
= mpath
->mp_prev
;
335 mpath
->mp_next
= mpath
->mp_prev
= NULL
;
336 UNSET_FLAG (binfo
->flags
, BGP_INFO_MULTIPATH
);
340 * bgp_info_mpath_next
342 * Given a bgp_info, return the next multipath entry
345 bgp_info_mpath_next (struct bgp_info
*binfo
)
347 if (!binfo
->mpath
|| !binfo
->mpath
->mp_next
)
349 return binfo
->mpath
->mp_next
->mp_info
;
353 * bgp_info_mpath_first
355 * Given bestpath bgp_info, return the first multipath entry.
358 bgp_info_mpath_first (struct bgp_info
*binfo
)
360 return bgp_info_mpath_next (binfo
);
364 * bgp_info_mpath_count
366 * Given the bestpath bgp_info, return the number of multipath entries
369 bgp_info_mpath_count (struct bgp_info
*binfo
)
373 return binfo
->mpath
->mp_count
;
377 * bgp_info_mpath_count_set
379 * Sets the count of multipaths into bestpath's mpath element
382 bgp_info_mpath_count_set (struct bgp_info
*binfo
, u_int32_t count
)
384 struct bgp_info_mpath
*mpath
;
385 if (!count
&& !binfo
->mpath
)
387 mpath
= bgp_info_mpath_get (binfo
);
390 mpath
->mp_count
= count
;
394 * bgp_info_mpath_attr
396 * Given bestpath bgp_info, return aggregated attribute set used
397 * for advertising the multipath route
400 bgp_info_mpath_attr (struct bgp_info
*binfo
)
404 return binfo
->mpath
->mp_attr
;
408 * bgp_info_mpath_attr_set
410 * Sets the aggregated attribute into bestpath's mpath element
413 bgp_info_mpath_attr_set (struct bgp_info
*binfo
, struct attr
*attr
)
415 struct bgp_info_mpath
*mpath
;
416 if (!attr
&& !binfo
->mpath
)
418 mpath
= bgp_info_mpath_get (binfo
);
421 mpath
->mp_attr
= attr
;
425 * bgp_info_mpath_update
427 * Compare and sync up the multipath list with the mp_list generated by
431 bgp_info_mpath_update (struct bgp_node
*rn
, struct bgp_info
*new_best
,
432 struct bgp_info
*old_best
, struct list
*mp_list
,
433 struct bgp_maxpaths_cfg
*mpath_cfg
)
435 u_int16_t maxpaths
, mpath_count
, old_mpath_count
;
436 struct listnode
*mp_node
, *mp_next_node
;
437 struct bgp_info
*cur_mpath
, *new_mpath
, *next_mpath
, *prev_mpath
;
438 int mpath_changed
, debug
;
439 char pfx_buf
[PREFIX2STR_BUFFER
], nh_buf
[2][INET6_ADDRSTRLEN
];
440 char path_buf
[PATH_ADDPATH_STR_BUFFER
];
443 maxpaths
= MULTIPATH_NUM
;
447 prev_mpath
= new_best
;
448 mp_node
= listhead (mp_list
);
449 debug
= bgp_debug_bestpath(&rn
->p
);
452 prefix2str (&rn
->p
, pfx_buf
, sizeof (pfx_buf
));
457 if (new_best
!= old_best
)
458 bgp_info_mpath_dequeue (new_best
);
459 maxpaths
= (new_best
->peer
->sort
== BGP_PEER_IBGP
) ?
460 mpath_cfg
->maxpaths_ibgp
: mpath_cfg
->maxpaths_ebgp
;
465 cur_mpath
= bgp_info_mpath_first (old_best
);
466 old_mpath_count
= bgp_info_mpath_count (old_best
);
467 bgp_info_mpath_count_set (old_best
, 0);
468 bgp_info_mpath_dequeue (old_best
);
472 zlog_debug("%s: starting mpath update, newbest %s num candidates %d old-mpath-count %d",
473 pfx_buf
, new_best
? new_best
->peer
->host
: "NONE",
474 listcount (mp_list
), old_mpath_count
);
477 * We perform an ordered walk through both lists in parallel.
478 * The reason for the ordered walk is that if there are paths
479 * that were previously multipaths and are still multipaths, the walk
480 * should encounter them in both lists at the same time. Otherwise
481 * there will be paths that are in one list or another, and we
482 * will deal with these separately.
484 * Note that new_best might be somewhere in the mp_list, so we need
487 while (mp_node
|| cur_mpath
)
489 struct bgp_info
*tmp_info
;
492 * We can bail out of this loop if all existing paths on the
493 * multipath list have been visited (for cleanup purposes) and
494 * the maxpath requirement is fulfulled
496 if (!cur_mpath
&& (mpath_count
>= maxpaths
))
499 mp_next_node
= mp_node
? listnextnode (mp_node
) : NULL
;
500 next_mpath
= cur_mpath
? bgp_info_mpath_next (cur_mpath
) : NULL
;
501 tmp_info
= mp_node
? listgetdata (mp_node
) : NULL
;
504 zlog_debug("%s: comparing candidate %s with existing mpath %s",
505 pfx_buf
, tmp_info
? tmp_info
->peer
->host
: "NONE",
506 cur_mpath
? cur_mpath
->peer
->host
: "NONE");
509 * If equal, the path was a multipath and is still a multipath.
510 * Insert onto new multipath list if maxpaths allows.
512 if (mp_node
&& (listgetdata (mp_node
) == cur_mpath
))
514 list_delete_node (mp_list
, mp_node
);
515 bgp_info_mpath_dequeue (cur_mpath
);
516 if ((mpath_count
< maxpaths
) &&
517 bgp_info_nexthop_cmp (prev_mpath
, cur_mpath
))
519 bgp_info_mpath_enqueue (prev_mpath
, cur_mpath
);
520 prev_mpath
= cur_mpath
;
524 bgp_info_path_with_addpath_rx_str(cur_mpath
, path_buf
);
525 zlog_debug("%s: %s is still multipath, cur count %d",
526 pfx_buf
, path_buf
, mpath_count
);
534 bgp_info_path_with_addpath_rx_str(cur_mpath
, path_buf
);
535 zlog_debug ("%s: remove mpath %s nexthop %s, cur count %d",
537 inet_ntop (AF_INET
, &cur_mpath
->attr
->nexthop
,
538 nh_buf
[0], sizeof (nh_buf
[0])),
542 mp_node
= mp_next_node
;
543 cur_mpath
= next_mpath
;
547 if (cur_mpath
&& (!mp_node
||
548 (bgp_info_mpath_cmp (cur_mpath
,
549 listgetdata (mp_node
)) < 0)))
552 * If here, we have an old multipath and either the mp_list
553 * is finished or the next mp_node points to a later
554 * multipath, so we need to purge this path from the
557 bgp_info_mpath_dequeue (cur_mpath
);
561 bgp_info_path_with_addpath_rx_str(cur_mpath
, path_buf
);
562 zlog_debug ("%s: remove mpath %s nexthop %s, cur count %d",
564 inet_ntop (AF_INET
, &cur_mpath
->attr
->nexthop
,
565 nh_buf
[0], sizeof (nh_buf
[0])),
568 cur_mpath
= next_mpath
;
573 * If here, we have a path on the mp_list that was not previously
574 * a multipath (due to non-equivalance or maxpaths exceeded),
575 * or the matching multipath is sorted later in the multipath
576 * list. Before we enqueue the path on the new multipath list,
577 * make sure its not on the old_best multipath list or referenced
579 * - If next_mpath points to this new path, update next_mpath to
580 * point to the multipath after this one
581 * - Dequeue the path from the multipath list just to make sure
583 new_mpath
= listgetdata (mp_node
);
584 list_delete_node (mp_list
, mp_node
);
585 if ((mpath_count
< maxpaths
) && (new_mpath
!= new_best
) &&
586 bgp_info_nexthop_cmp (prev_mpath
, new_mpath
))
588 if (new_mpath
== next_mpath
)
589 bgp_info_mpath_next (new_mpath
);
590 bgp_info_mpath_dequeue (new_mpath
);
592 bgp_info_mpath_enqueue (prev_mpath
, new_mpath
);
593 prev_mpath
= new_mpath
;
598 bgp_info_path_with_addpath_rx_str(new_mpath
, path_buf
);
599 zlog_debug ("%s: add mpath %s nexthop %s, cur count %d",
601 inet_ntop (AF_INET
, &new_mpath
->attr
->nexthop
,
602 nh_buf
[0], sizeof (nh_buf
[0])),
606 mp_node
= mp_next_node
;
613 zlog_debug("%s: New mpath count (incl newbest) %d mpath-change %s",
614 pfx_buf
, mpath_count
, mpath_changed
? "YES" : "NO");
616 bgp_info_mpath_count_set (new_best
, mpath_count
-1);
617 if (mpath_changed
|| (bgp_info_mpath_count (new_best
) != old_mpath_count
))
618 SET_FLAG (new_best
->flags
, BGP_INFO_MULTIPATH_CHG
);
623 * bgp_mp_dmed_deselect
625 * Clean up multipath information for BGP_INFO_DMED_SELECTED path that
626 * is not selected as best path
629 bgp_mp_dmed_deselect (struct bgp_info
*dmed_best
)
631 struct bgp_info
*mpinfo
, *mpnext
;
636 for (mpinfo
= bgp_info_mpath_first (dmed_best
); mpinfo
; mpinfo
= mpnext
)
638 mpnext
= bgp_info_mpath_next (mpinfo
);
639 bgp_info_mpath_dequeue (mpinfo
);
642 bgp_info_mpath_count_set (dmed_best
, 0);
643 UNSET_FLAG (dmed_best
->flags
, BGP_INFO_MULTIPATH_CHG
);
644 assert (bgp_info_mpath_first (dmed_best
) == 0);
648 * bgp_info_mpath_aggregate_update
650 * Set the multipath aggregate attribute. We need to see if the
651 * aggregate has changed and then set the ATTR_CHANGED flag on the
652 * bestpath info so that a peer update will be generated. The
653 * change is detected by generating the current attribute,
654 * interning it, and then comparing the interned pointer with the
655 * current value. We can skip this generate/compare step if there
656 * is no change in multipath selection and no attribute change in
660 bgp_info_mpath_aggregate_update (struct bgp_info
*new_best
,
661 struct bgp_info
*old_best
)
663 struct bgp_info
*mpinfo
;
664 struct aspath
*aspath
;
665 struct aspath
*asmerge
;
666 struct attr
*new_attr
, *old_attr
;
668 struct community
*community
, *commerge
;
669 struct ecommunity
*ecomm
, *ecommerge
;
670 struct attr_extra
*ae
;
671 struct attr attr
= { 0 };
673 if (old_best
&& (old_best
!= new_best
) &&
674 (old_attr
= bgp_info_mpath_attr (old_best
)))
676 bgp_attr_unintern (&old_attr
);
677 bgp_info_mpath_attr_set (old_best
, NULL
);
683 if (!bgp_info_mpath_count (new_best
))
685 if ((new_attr
= bgp_info_mpath_attr (new_best
)))
687 bgp_attr_unintern (&new_attr
);
688 bgp_info_mpath_attr_set (new_best
, NULL
);
689 SET_FLAG (new_best
->flags
, BGP_INFO_ATTR_CHANGED
);
694 bgp_attr_dup (&attr
, new_best
->attr
);
696 if (new_best
->peer
&&
697 bgp_flag_check (new_best
->peer
->bgp
, BGP_FLAG_MULTIPATH_RELAX_AS_SET
))
700 /* aggregate attribute from multipath constituents */
701 aspath
= aspath_dup (attr
.aspath
);
702 origin
= attr
.origin
;
703 community
= attr
.community
? community_dup (attr
.community
) : NULL
;
705 ecomm
= (ae
&& ae
->ecommunity
) ? ecommunity_dup (ae
->ecommunity
) : NULL
;
707 for (mpinfo
= bgp_info_mpath_first (new_best
); mpinfo
;
708 mpinfo
= bgp_info_mpath_next (mpinfo
))
710 asmerge
= aspath_aggregate (aspath
, mpinfo
->attr
->aspath
);
711 aspath_free (aspath
);
714 if (origin
< mpinfo
->attr
->origin
)
715 origin
= mpinfo
->attr
->origin
;
717 if (mpinfo
->attr
->community
)
721 commerge
= community_merge (community
, mpinfo
->attr
->community
);
722 community
= community_uniq_sort (commerge
);
723 community_free (commerge
);
726 community
= community_dup (mpinfo
->attr
->community
);
729 ae
= mpinfo
->attr
->extra
;
730 if (ae
&& ae
->ecommunity
)
734 ecommerge
= ecommunity_merge (ecomm
, ae
->ecommunity
);
735 ecomm
= ecommunity_uniq_sort (ecommerge
);
736 ecommunity_free (&ecommerge
);
739 ecomm
= ecommunity_dup (ae
->ecommunity
);
743 attr
.aspath
= aspath
;
744 attr
.origin
= origin
;
747 attr
.community
= community
;
748 attr
.flag
|= ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES
);
752 ae
= bgp_attr_extra_get (&attr
);
753 ae
->ecommunity
= ecomm
;
754 attr
.flag
|= ATTR_FLAG_BIT (BGP_ATTR_EXT_COMMUNITIES
);
757 /* Zap multipath attr nexthop so we set nexthop to self */
758 attr
.nexthop
.s_addr
= 0;
761 memset (&attr
.extra
->mp_nexthop_global
, 0, sizeof (struct in6_addr
));
762 #endif /* HAVE_IPV6 */
764 /* TODO: should we set ATOMIC_AGGREGATE and AGGREGATOR? */
767 new_attr
= bgp_attr_intern (&attr
);
768 bgp_attr_extra_free (&attr
);
770 if (new_attr
!= bgp_info_mpath_attr (new_best
))
772 if ((old_attr
= bgp_info_mpath_attr (new_best
)))
773 bgp_attr_unintern (&old_attr
);
774 bgp_info_mpath_attr_set (new_best
, new_attr
);
775 SET_FLAG (new_best
->flags
, BGP_INFO_ATTR_CHANGED
);
778 bgp_attr_unintern (&new_attr
);