LCOV - code coverage report
Current view: top level - bgp - bgp_path.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 64 199 32.2 %
Date: 2026-11-16 02:06:09 Functions: 7 15 46.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
       3             :  * Copyright Contributors to the OpenSDN project
       4             :  */
       5             : 
       6             : #include "bgp/bgp_path.h"
       7             : 
       8             : #include <boost/foreach.hpp>
       9             : 
      10             : #include "bgp/bgp_peer.h"
      11             : #include "bgp/bgp_route.h"
      12             : #include "bgp/bgp_server.h"
      13             : #include "bgp/extended-community/sub_cluster.h"
      14             : #include "net/community_type.h"
      15             : 
      16             : using std::string;
      17             : using std::vector;
      18             : 
      19       68501 : string BgpPath::PathIdString(uint32_t path_id) {
      20       68501 :     Ip4Address addr(path_id);
      21      137000 :     return addr.to_string();
      22             : }
      23             : 
      24       39653 : BgpPath::BgpPath(const IPeer *peer, uint32_t path_id, PathSource src,
      25             :                  const BgpAttrPtr ptr, uint32_t flags, uint32_t label,
      26       39653 :                  uint32_t l3_label)
      27       39646 :     : peer_(peer), path_id_(path_id), source_(src), attr_(ptr),
      28       39653 :       original_attr_(ptr), flags_(flags), label_(label), l3_label_(l3_label) {
      29       39654 : }
      30             : 
      31           0 : BgpPath::BgpPath(const IPeer *peer, PathSource src, const BgpAttrPtr ptr,
      32           0 :         uint32_t flags, uint32_t label, uint32_t l3_label)
      33           0 :     : peer_(peer), path_id_(0), source_(src), attr_(ptr), original_attr_(ptr),
      34           0 :       flags_(flags), label_(label), l3_label_(l3_label) {
      35           0 : }
      36             : 
      37          20 : BgpPath::BgpPath(uint32_t path_id, PathSource src, const BgpAttrPtr ptr,
      38          20 :         uint32_t flags, uint32_t label, uint32_t l3_label)
      39          20 :     : peer_(NULL), path_id_(path_id), source_(src), attr_(ptr),
      40          20 :       original_attr_(ptr), flags_(flags), label_(label), l3_label_(l3_label) {
      41          20 : }
      42             : 
      43           0 : BgpPath::BgpPath(PathSource src, const BgpAttrPtr ptr,
      44           0 :         uint32_t flags, uint32_t label, uint32_t l3_label)
      45           0 :     : peer_(NULL), path_id_(0), source_(src), attr_(ptr), original_attr_(ptr),
      46           0 :       flags_(flags), label_(label), l3_label_(l3_label) {
      47           0 : }
      48             : 
      49             : // True is better
      50             : #define BOOL_COMPARE(CondA, CondB)   \
      51             :     do {                                \
      52             :         if (CondA) {                    \
      53             :             if (!(CondB)) return -1;    \
      54             :         } else {                        \
      55             :             if (CondB) return 1;        \
      56             :         }                               \
      57             :     } while (0)
      58             : 
      59       14855 : int BgpPath::PathCompare(const BgpPath &rhs, bool allow_ecmp) const {
      60       14855 :     const BgpAttr *rattr = rhs.GetAttr();
      61             : 
      62             :     // Feasible Path first
      63       14854 :     KEY_COMPARE(rhs.IsFeasible(), IsFeasible());
      64             : 
      65             :     // Compare local_pref in reverse order as larger is better.
      66       14853 :     KEY_COMPARE(rattr->local_pref(), attr_->local_pref());
      67             : 
      68             :     // ETree Root path first [compare in reverse order]
      69       14852 :     BOOL_COMPARE(rattr->etree_leaf(), attr_->etree_leaf());
      70             : 
      71             :     // Sticky paths first
      72       14843 :     BOOL_COMPARE(rattr->evpn_sticky_mac(), attr_->evpn_sticky_mac());
      73             : 
      74             :     // Compare sequence_number in reverse order as larger is better.
      75       14844 :     KEY_COMPARE(rattr->sequence_number(), attr_->sequence_number());
      76             : 
      77             :     // Route without LLGR_STALE community is always preferred over one with.
      78       14846 :     bool llgr_stale = attr_->community() && attr_->community()->ContainsValue(
      79       14846 :                                                 CommunityType::LlgrStale);
      80       14846 :     llgr_stale |= IsLlgrStale();
      81             : 
      82       14846 :     bool rllgr_stale = rattr->community() && rattr->community()->ContainsValue(
      83       14846 :                                                  CommunityType::LlgrStale);
      84       14846 :     rllgr_stale |= rhs.IsLlgrStale();
      85             : 
      86       14846 :     KEY_COMPARE(llgr_stale, rllgr_stale);
      87             : 
      88             :     // Do not compare as path length for service chain paths at this point.
      89             :     // We want to treat service chain paths as ECMP irrespective of as path
      90             :     // length.
      91       14846 :     const BgpServer *server = attr_->attr_db()->server();
      92       29692 :     if (!server->ignore_aspath() &&
      93       14846 :             (!attr_->origin_vn_path() || !rattr->origin_vn_path())) {
      94       14846 :         KEY_COMPARE(attr_->max_as_path_count(), rattr->max_as_path_count());
      95             :     }
      96             : 
      97       14849 :     KEY_COMPARE(attr_->origin(), rattr->origin());
      98             : 
      99             :     // Compare med if always compare med knob is enabled or if both paths are
     100             :     // learnt from the same neighbor as.
     101       29692 :     if (server->global_config()->always_compare_med() ||
     102       14846 :         (attr_->neighbor_as() &&
     103        9356 :          attr_->neighbor_as() == rattr->neighbor_as())) {
     104        9357 :         KEY_COMPARE(attr_->med(), rattr->med());
     105             :     }
     106             : 
     107             :     // For ECMP paths, above checks should suffice.
     108       14845 :     if (allow_ecmp)
     109       12685 :         return 0;
     110             : 
     111             :     // Prefer non-aliased paths.
     112        2160 :     BOOL_COMPARE(rhs.IsAliased(), IsAliased());
     113             : 
     114             :     // Compare as path length for service chain paths since we bypassed the
     115             :     // check previously.
     116        2160 :     if (attr_->origin_vn_path() && rattr->origin_vn_path())
     117           0 :         KEY_COMPARE(attr_->max_as_path_count(), rattr->max_as_path_count());
     118             : 
     119             :     // Prefer locally generated routes over bgp and xmpp routes.
     120        2160 :     BOOL_COMPARE(peer_ == NULL, rhs.peer_ == NULL);
     121             : 
     122             :     // Compare the source and the path id.
     123        2160 :     KEY_COMPARE(rhs.GetSource(), GetSource());
     124             : 
     125             :     // Bail if both paths are local since all subsequent checks are
     126             :     // based on IPeer properties.
     127        2160 :     if (peer_ == NULL && rhs.peer_ == NULL) {
     128           0 :         KEY_COMPARE(path_id_, rhs.path_id_);
     129           0 :         return 0;
     130             :     }
     131             : 
     132             :     // Prefer xmpp routes over bgp routes.
     133        2160 :     BOOL_COMPARE(peer_->IsXmppPeer(), rhs.peer_->IsXmppPeer());
     134             : 
     135             :     // Path received from EBGP is better than the one received from IBGP
     136        2160 :     KEY_COMPARE(peer_->PeerType() == BgpProto::IBGP,
     137             :                 rhs.peer_->PeerType() == BgpProto::IBGP);
     138             : 
     139        2160 :     KEY_COMPARE(path_id_, rhs.path_id_);
     140             : 
     141             :     // Lower router id is better. Substitute originator id for router id
     142             :     // if the path has an originator id.
     143        2160 :     uint32_t orig_id = attr_->originator_id().to_ulong();
     144        2160 :     uint32_t rorig_id = rattr->originator_id().to_ulong();
     145        2160 :     uint32_t id = orig_id ? orig_id : peer_->bgp_identifier();
     146        2160 :     uint32_t rid = rorig_id ? rorig_id : rhs.peer_->bgp_identifier();
     147        2160 :     KEY_COMPARE(id, rid);
     148             : 
     149           0 :     KEY_COMPARE(attr_->cluster_list_length(), rattr->cluster_list_length());
     150             : 
     151           0 :     const BgpPeer *lpeer = dynamic_cast<const BgpPeer *>(peer_);
     152           0 :     const BgpPeer *rpeer = dynamic_cast<const BgpPeer *>(rhs.peer_);
     153           0 :     if (lpeer != NULL && rpeer != NULL) {
     154           0 :         KEY_COMPARE(lpeer->peer_key(), rpeer->peer_key());
     155             :     }
     156             : 
     157           0 :     return 0;
     158             : }
     159             : 
     160           0 : bool BgpPath::PathSameNeighborAs(const BgpPath &rhs) const {
     161           0 :     const BgpAttr *rattr = rhs.GetAttr();
     162           0 :     if (!peer_ || peer_->PeerType() != BgpProto::EBGP)
     163           0 :         return false;
     164           0 :     if (!rhs.peer_ || rhs.peer_->PeerType() != BgpProto::EBGP)
     165           0 :         return false;
     166           0 :     return (attr_->neighbor_as() == rattr->neighbor_as());
     167             : }
     168             : 
     169       79308 : void BgpPath::UpdatePeerRefCount(int count, Address::Family family) const {
     170       79308 :     if (!peer_)
     171        5154 :         return;
     172       74154 :     peer_->UpdateTotalPathCount(count);
     173       74170 :     if (source_ != BGP_XMPP || IsReplicated() || IsResolved() || IsAliased())
     174       52975 :         return;
     175       21191 :     peer_->UpdatePrimaryPathCount(count, family);
     176             : }
     177             : 
     178       42138 : string BgpPath::ToString() const {
     179       42138 :     return peer_ ? peer_->ToString() : "Nil";
     180             : }
     181             : 
     182           0 : RouteDistinguisher BgpPath::GetSourceRouteDistinguisher() const {
     183           0 :     if (!attr_->source_rd().IsZero())
     184           0 :         return attr_->source_rd();
     185           0 :     if (!IsReplicated())
     186           0 :         return RouteDistinguisher::kZeroRd;
     187             : 
     188           0 :     const BgpSecondaryPath *path = static_cast<const BgpSecondaryPath *>(this);
     189           0 :     return path->GetPrimaryRouteDistinguisher();
     190             : }
     191             : 
     192           0 : vector<string> BgpPath::GetFlagsStringList() const {
     193           0 :     vector<string> flag_names;
     194           0 :     if (flags_ == 0) {
     195           0 :         flag_names.push_back("None");
     196           0 :         return flag_names;
     197             :     }
     198             : 
     199             :     // First we form a list of enums and then iterate over it to get their
     200             :     // string forms using switch. This lets compiler tell us when ever we add a
     201             :     // new enumeration to PathFlag.
     202           0 :     vector<PathFlag> flags;
     203           0 :     if (flags_ & AsPathLooped)
     204           0 :         flags.push_back(AsPathLooped);
     205           0 :     if (flags_ & NoNeighborAs)
     206           0 :         flags.push_back(NoNeighborAs);
     207           0 :     if (flags_ & Stale)
     208           0 :         flags.push_back(Stale);
     209           0 :     if (flags_ & NoTunnelEncap)
     210           0 :         flags.push_back(NoTunnelEncap);
     211           0 :     if (flags_ & OriginatorIdLooped)
     212           0 :         flags.push_back(OriginatorIdLooped);
     213           0 :     if (flags_ & ResolveNexthop)
     214           0 :         flags.push_back(ResolveNexthop);
     215           0 :     if (flags_ & ResolvedPath)
     216           0 :         flags.push_back(ResolvedPath);
     217           0 :     if (flags_ & RoutingPolicyReject)
     218           0 :         flags.push_back(RoutingPolicyReject);
     219           0 :     if (flags_ & LlgrStale)
     220           0 :         flags.push_back(LlgrStale);
     221           0 :     if (flags_ & ClusterListLooped)
     222           0 :         flags.push_back(ClusterListLooped);
     223           0 :     if (flags_ & AliasedPath)
     224           0 :         flags.push_back(AliasedPath);
     225           0 :     if (flags_ & CheckGlobalErmVpnRoute)
     226           0 :         flags.push_back(CheckGlobalErmVpnRoute);
     227             : 
     228           0 :     for (auto flag : flags) {
     229           0 :         switch (flag) {
     230           0 :         case AsPathLooped:
     231           0 :             flag_names.push_back("AsPathLooped");
     232           0 :             break;
     233           0 :         case NoNeighborAs:
     234           0 :             flag_names.push_back("NoNeighborAs");
     235           0 :             break;
     236           0 :         case Stale:
     237           0 :             flag_names.push_back("Stale");
     238           0 :             break;
     239           0 :         case NoTunnelEncap:
     240           0 :             flag_names.push_back("NoTunnelEncap");
     241           0 :             break;
     242           0 :         case OriginatorIdLooped:
     243           0 :             flag_names.push_back("OriginatorIdLooped");
     244           0 :             break;
     245           0 :         case ResolveNexthop:
     246           0 :             flag_names.push_back("ResolveNexthop");
     247           0 :             break;
     248           0 :         case ResolvedPath:
     249           0 :             flag_names.push_back("ResolvedPath");
     250           0 :             break;
     251           0 :         case RoutingPolicyReject:
     252           0 :             flag_names.push_back("RoutingPolicyReject");
     253           0 :             break;
     254           0 :         case LlgrStale:
     255           0 :             flag_names.push_back("LlgrStale");
     256           0 :             break;
     257           0 :         case ClusterListLooped:
     258           0 :             flag_names.push_back("ClusterListLooped");
     259           0 :             break;
     260           0 :         case AliasedPath:
     261           0 :             flag_names.push_back("AliasedPath");
     262           0 :             break;
     263           0 :         case CheckGlobalErmVpnRoute:
     264           0 :             flag_names.push_back("CheckGlobalErmVpnRoute");
     265           0 :             break;
     266             :         }
     267             :     }
     268           0 :     return flag_names;
     269           0 : }
     270             : 
     271           0 : string BgpPath::GetSourceString(bool combine_bgp_and_xmpp) const {
     272           0 :     switch (source_) {
     273           0 :     case None:
     274           0 :         return "None";
     275           0 :     case BGP_XMPP:
     276           0 :         if (combine_bgp_and_xmpp) {
     277           0 :             return "BGP_XMPP";
     278           0 :         } else if (peer_) {
     279           0 :             return(peer_->IsXmppPeer() ? "XMPP" : "BGP");
     280             :         } else {
     281           0 :             return "None";
     282             :         }
     283           0 :     case ServiceChain:
     284           0 :         return "ServiceChain";
     285           0 :     case StaticRoute:
     286           0 :         return "StaticRoute";
     287           0 :     case Aggregate:
     288           0 :         return "Aggregate";
     289           0 :     case Local:
     290           0 :         return "Local";
     291             :     }
     292           0 :     return "None";
     293             : }
     294             : 
     295       28542 : BgpSecondaryPath::BgpSecondaryPath(const IPeer *peer, uint32_t path_id,
     296             :         PathSource src, const BgpAttrPtr ptr, uint32_t flags, uint32_t label,
     297       28542 :         uint32_t l3_label)
     298       28542 :     : BgpPath(peer, path_id, src, ptr, flags, label, l3_label) {
     299       28542 : }
     300             : 
     301           0 : RouteDistinguisher BgpSecondaryPath::GetPrimaryRouteDistinguisher() const {
     302           0 :     return src_entry_->GetRouteDistinguisher();
     303             : }
     304             : 
     305           0 : void BgpPath::AddExtCommunitySubCluster(uint32_t subcluster_id) {
     306           0 :     BgpAttr *attr = new BgpAttr(*(GetOriginalAttr()));
     307           0 :     BgpServer *server = attr->attr_db()->server();
     308           0 :     ExtCommunityPtr ext_community = attr->ext_community();
     309             : 
     310           0 :     SubCluster sc(server->autonomous_system(), subcluster_id);
     311             :     ext_community = server->extcomm_db()->
     312           0 :         ReplaceSubClusterAndLocate(ext_community.get(),
     313           0 :                 sc.GetExtCommunity());
     314             :     BgpAttrPtr modified_attr = server->attr_db()->
     315           0 :         ReplaceExtCommunityAndLocate(attr, ext_community);
     316             :     // Since routing policies are applied only to original attribute hence
     317             :     // we are updating original_attr with subcluster extended community.
     318             :     // Also we need to set modified_attr in attr as well as orignal_attr,
     319             :     // because there may or may not be a policy but we still need to add this
     320             :     // community.
     321             :     // Modifying original attr should be done judiciously and only if required.
     322           0 :     if (modified_attr) {
     323           0 :         SetAttr(modified_attr, modified_attr);
     324             :     }
     325           0 : }

Generated by: LCOV version 1.14