LCOV - code coverage report
Current view: top level - bgp - bgp_table.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 262 678 38.6 %
Date: 2026-11-16 02:06:09 Functions: 35 52 67.3 %
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_table.h"
       7             : 
       8             : #include <boost/foreach.hpp>
       9             : 
      10             : #include "sandesh/sandesh_trace.h"
      11             : #include "base/task_annotations.h"
      12             : #include "bgp/bgp_log.h"
      13             : #include "bgp/bgp_membership.h"
      14             : #include "bgp/bgp_peer.h"
      15             : #include "bgp/bgp_peer_types.h"
      16             : #include "bgp/bgp_ribout.h"
      17             : #include "bgp/bgp_ribout_updates.h"
      18             : #include "bgp/bgp_route.h"
      19             : #include "bgp/bgp_server.h"
      20             : #include "bgp/bgp_update.h"
      21             : #include "bgp/inet/inet_table.h"
      22             : #include "bgp/routing-instance/iroute_aggregator.h"
      23             : #include "bgp/routing-instance/path_resolver.h"
      24             : #include "bgp/routing-instance/routing_instance.h"
      25             : #include "bgp/routing-instance/rtarget_group_mgr.h"
      26             : #include "bgp/tunnel_encap/tunnel_encap.h"
      27             : #include "net/community_type.h"
      28             : 
      29             : using std::make_pair;
      30             : using std::string;
      31             : 
      32             : class BgpTable::DeleteActor : public LifetimeActor {
      33             :   public:
      34         580 :     explicit DeleteActor(BgpTable *table)
      35         580 :         : LifetimeActor(table->rtinstance_->server()->lifetime_manager()),
      36         580 :           table_(table) {
      37         580 :     }
      38        1160 :     virtual ~DeleteActor() {
      39        1160 :     }
      40         580 :     virtual bool MayDelete() const {
      41         580 :         return table_->MayDelete();
      42             :     }
      43             : 
      44         580 :     virtual void Shutdown() {
      45         580 :         table_->Shutdown();
      46         580 :     }
      47             : 
      48             :     // Make sure that all notifications have been processed and all db
      49             :     // state have been cleared for all partitions, before we inform the
      50             :     // parent instance that this table deletion process is complete.
      51         580 :     virtual void Destroy() {
      52         580 :         table_->rtinstance_->DestroyDBTable(table_);
      53         580 :     }
      54             : 
      55             :   private:
      56             :     BgpTable *table_;
      57             : };
      58             : 
      59         580 : BgpTable::BgpTable(DB *db, const string &name)
      60             :     : RouteTable(db, name),
      61         580 :       rtinstance_(NULL),
      62         580 :       path_resolver_(NULL),
      63         580 :       instance_delete_ref_(this, NULL) {
      64         579 :     primary_path_count_ = 0;
      65         580 :     secondary_path_count_ = 0;
      66         580 :     infeasible_path_count_ = 0;
      67         580 :     stale_path_count_ = 0;
      68         580 :     llgr_stale_path_count_ = 0;
      69         580 : }
      70             : 
      71             : //
      72             : // Remove the table from the instance dependents before attempting to
      73             : // destroy the DeleteActor which can have its Delete() method be called
      74             : // via the reference.
      75             : //
      76         580 : BgpTable::~BgpTable() {
      77         580 :     assert(path_resolver_ == NULL),
      78         580 :     instance_delete_ref_.Reset(NULL);
      79         580 : }
      80             : 
      81         580 : void BgpTable::set_routing_instance(RoutingInstance *rtinstance) {
      82         580 :     rtinstance_ = rtinstance;
      83         580 :     assert(rtinstance);
      84         580 :     deleter_.reset(new DeleteActor(this));
      85         580 :     instance_delete_ref_.Reset(rtinstance->deleter());
      86         580 : }
      87             : 
      88       90160 : BgpServer *BgpTable::server() {
      89       90160 :     return rtinstance_->server();
      90             : }
      91             : 
      92       98202 : const BgpServer *BgpTable::server() const {
      93       98202 :     return rtinstance_->server();
      94             : }
      95             : 
      96             : //
      97             : // Find the RibOut for the given RibExportPolicy.
      98             : //
      99           0 : RibOut *BgpTable::RibOutFind(const RibExportPolicy &policy) {
     100           0 :     RibOutMap::iterator loc = ribout_map_.find(policy);
     101           0 :     return (loc != ribout_map_.end()) ? loc->second : NULL;
     102             : }
     103             : 
     104             : //
     105             : // Find or create the RibOut associated with the given RibExportPolicy.
     106             : // If a new RibOut is created, an entry for the pair gets added to the
     107             : // RibOutMap.
     108             : //
     109        2184 : RibOut *BgpTable::RibOutLocate(BgpUpdateSender *sender,
     110             :                                const RibExportPolicy &policy) {
     111        2184 :     RibOutMap::iterator loc = ribout_map_.find(policy);
     112        2184 :     if (loc == ribout_map_.end()) {
     113         804 :         RibOut *ribout = new RibOut(this, sender, policy);
     114         804 :         ribout_map_.insert(make_pair(policy, ribout));
     115         804 :         return ribout;
     116             :     }
     117        1380 :     return loc->second;
     118             : }
     119             : 
     120             : //
     121             : // Delete the entry corresponding to the given RibExportPolicy from the
     122             : // RibOutMap.  Also deletes the RibOut itself.
     123             : //
     124         804 : void BgpTable::RibOutDelete(const RibExportPolicy &policy) {
     125         804 :     RibOutMap::iterator loc = ribout_map_.find(policy);
     126         804 :     assert(loc != ribout_map_.end());
     127         804 :     delete loc->second;
     128         804 :     ribout_map_.erase(loc);
     129         804 : }
     130             : 
     131             : // If both as2 and as4 aggregators are present then we need to choose one
     132       12428 : void BgpTable::CheckAggregatorAttr(BgpAttr *attr) const {
     133       12428 :     if (attr->aggregator_as_num() && attr->aggregator_as4_num()) {
     134           0 :         if (attr->aggregator_as_num() != AS_TRANS) {
     135             :             // If as2 aggregator is not as_trans then we ignore as4 aggregator
     136             :             // and ignore as4_path also
     137           0 :             attr->set_as4_path(NULL);
     138           0 :             attr->set_as4_aggregator(0, attr->aggregator_adderess());
     139             :         } else {
     140             :             // If as2 aggregator is as_trans then we use as4 aggregator
     141           0 :             attr->set_aggregator(attr->aggregator_as4_num(),
     142           0 :                                  attr->aggregator_adderess());
     143           0 :             attr->set_as4_aggregator(0, attr->aggregator_adderess());
     144             :         }
     145             :     }
     146       12428 : }
     147             : 
     148       12428 : void BgpTable::PrependLocalAs(const RibOut *ribout, BgpAttr *clone,
     149             :         const IPeer* peer) const {
     150       12428 :     CheckAggregatorAttr(clone);
     151       12428 :     as_t local_as = ribout->local_as() ?:
     152           0 :                 clone->attr_db()->server()->local_autonomous_system();
     153       12428 :     if (!server()->enable_4byte_as())
     154       12428 :        return PrependAsToAsPath2Byte(clone, (as2_t)local_as);
     155           0 :     if (ribout->as4_supported()) {
     156           0 :         if (clone->aspath_4byte())
     157           0 :             PrependAsToAsPath4Byte(clone, local_as);
     158             :         else
     159           0 :             CreateAsPath4Byte(clone, local_as);
     160             :     } else {
     161           0 :         if (!clone->as_path())
     162           0 :             CreateAsPath2Byte(clone);
     163           0 :         PrependAsToAsPath2Byte(clone, local_as);
     164             :     }
     165             : }
     166             : 
     167           0 : void BgpTable::RemovePrivateAs(const RibOut *ribout, BgpAttr *attr) const {
     168           0 :     bool all = ribout->remove_private_all();
     169           0 :     bool replace = ribout->remove_private_replace();
     170           0 :     bool peer_loop_check = ribout->remove_private_peer_loop_check();
     171             : 
     172           0 :     const AsPathSpec &spec = attr->as_path()->path();
     173           0 :     as_t peer_asn = peer_loop_check ? ribout->peer_as() : 0;
     174           0 :     as_t replace_asn = 0;
     175           0 :     if (replace) {
     176           0 :         if (ribout->peer_type() == BgpProto::EBGP) {
     177           0 :             replace_asn = server()->local_autonomous_system();
     178             :         } else {
     179           0 :             replace_asn = spec.AsLeftMostPublic();
     180             :         }
     181             :     }
     182           0 :     if (replace_asn > AS2_MAX) {
     183           0 :         if (!attr->as4_path())
     184           0 :             CreateAs4Path(attr);
     185           0 :         const As4PathSpec &spec = attr->as4_path()->path();
     186           0 :         As4PathSpec *new_spec = spec.RemovePrivate(all, replace_asn, peer_asn);
     187           0 :         attr->set_as4_path(new_spec);
     188           0 :         delete new_spec;
     189           0 :         replace_asn = AS_TRANS;
     190             :     }
     191           0 :     if (peer_asn > AS2_MAX)
     192           0 :         peer_asn = AS_TRANS;
     193             : 
     194           0 :     AsPathSpec *new_spec = spec.RemovePrivate(all, replace_asn, peer_asn);
     195           0 :     attr->set_as_path(new_spec);
     196           0 :     delete new_spec;
     197           0 : }
     198             : 
     199           0 : void BgpTable::RemovePrivate4ByteAs(const RibOut *ribout, BgpAttr *attr) const {
     200           0 :     bool all = ribout->remove_private_all();
     201           0 :     bool replace = ribout->remove_private_replace();
     202           0 :     bool peer_loop_check = ribout->remove_private_peer_loop_check();
     203             : 
     204           0 :     const AsPath4ByteSpec &spec = attr->aspath_4byte()->path();
     205           0 :     as_t peer_asn = peer_loop_check ? ribout->peer_as() : 0;
     206           0 :     as_t replace_asn = 0;
     207           0 :     if (replace) {
     208           0 :         if (ribout->peer_type() == BgpProto::EBGP) {
     209           0 :             replace_asn = server()->local_autonomous_system();
     210             :         } else {
     211           0 :             replace_asn = spec.AsLeftMostPublic();
     212             :         }
     213             :     }
     214             : 
     215           0 :     AsPath4ByteSpec *new_spec = spec.RemovePrivate(all, replace_asn, peer_asn);
     216           0 :     attr->set_aspath_4byte(new_spec);
     217           0 :     delete new_spec;
     218           0 : }
     219             : 
     220           0 : void BgpTable::RemovePrivateAs4(const RibOut *ribout, BgpAttr *attr) const {
     221           0 :     bool all = ribout->remove_private_all();
     222           0 :     bool replace = ribout->remove_private_replace();
     223           0 :     bool peer_loop_check = ribout->remove_private_peer_loop_check();
     224             : 
     225           0 :     const As4PathSpec &spec = attr->as4_path()->path();
     226           0 :     as_t peer_asn = peer_loop_check ? ribout->peer_as() : 0;
     227           0 :     as_t replace_asn = 0;
     228           0 :     if (replace) {
     229           0 :         if (ribout->peer_type() == BgpProto::EBGP) {
     230           0 :             replace_asn = server()->local_autonomous_system();
     231             :         } else {
     232           0 :             replace_asn = spec.AsLeftMostPublic();
     233             :         }
     234             :     }
     235             : 
     236           0 :     As4PathSpec *new_spec = spec.RemovePrivate(all, replace_asn, peer_asn);
     237           0 :     attr->set_as4_path(new_spec);
     238           0 :     delete new_spec;
     239           0 : }
     240             : 
     241             : //
     242             : // Process Remove Private information.
     243             : //
     244       12427 : void BgpTable::ProcessRemovePrivate(const RibOut *ribout, BgpAttr *attr) const {
     245       12427 :     if (!ribout->IsEncodingBgp())
     246           0 :         return;
     247       12428 :     if (!ribout->remove_private_enabled())
     248       12428 :         return;
     249             : 
     250           0 :     if (attr->as_path())
     251           0 :         RemovePrivateAs(ribout, attr);
     252           0 :     if (attr->aspath_4byte())
     253           0 :         RemovePrivate4ByteAs(ribout, attr);
     254           0 :     if (attr->as4_path())
     255           0 :         RemovePrivateAs4(ribout, attr);
     256             : }
     257             : 
     258             : //
     259             : // Process Remove Private information.
     260             : //
     261       12427 : void BgpTable::ProcessAsOverride(const RibOut *ribout, BgpAttr *attr) const {
     262       12427 :     if (ribout->as_override() && !attr->IsAsPathEmpty()) {
     263           0 :         as_t replace_as = ribout->local_as() ?:
     264           0 :                 attr->attr_db()->server()->local_autonomous_system();
     265           0 :         if (attr->as_path()) {
     266           0 :             const AsPathSpec &as_path = attr->as_path()->path();
     267             :             // If peer_as is > 0xffff, it can't be in as_path
     268           0 :             if (ribout->peer_as() <= AS2_MAX) {
     269           0 :                 if (replace_as > AS2_MAX) {
     270             :                     // if replace_as > 0xffff, as4_path should be created if
     271             :                     // not already there by copying data from as_path
     272           0 :                     if (!attr->as4_path()) {
     273           0 :                         CreateAs4Path(attr);
     274             :                     }
     275           0 :                     replace_as = AS_TRANS;
     276             :                 }
     277           0 :                 AsPathSpec *as_path_ptr = as_path.Replace(
     278           0 :                                                  ribout->peer_as(), replace_as);
     279           0 :                 attr->set_as_path(as_path_ptr);
     280           0 :                 delete as_path_ptr;
     281             :             }
     282           0 :             if (attr->as4_path()) {
     283           0 :                 As4PathSpec *as_path_ptr = attr->as4_path()->path().Replace(
     284             :                                                  ribout->peer_as(), replace_as);
     285           0 :                 attr->set_as4_path(as_path_ptr);
     286           0 :                 delete as_path_ptr;
     287             :             }
     288             :         }
     289           0 :         if (attr->aspath_4byte()) {
     290           0 :             const AsPath4ByteSpec &as_path = attr->aspath_4byte()->path();
     291             :             AsPath4ByteSpec *as_path_ptr =
     292           0 :                           as_path.Replace(ribout->peer_as(), replace_as);
     293           0 :             attr->set_aspath_4byte(as_path_ptr);
     294           0 :             delete as_path_ptr;
     295             :         }
     296             :     }
     297       12427 : }
     298             : 
     299             : //
     300             : // Process Long Lived Graceful Restart state information.
     301             : //
     302             : // For LLGR_STALE paths, if the peer supports LLGR then attach LLGR_STALE
     303             : // community. Otherwise, strip LLGR_STALE community, reduce LOCAL_PREF and
     304             : // attach NO_EXPORT community instead.
     305             : //
     306       12428 : void BgpTable::ProcessLlgrState(const RibOut *ribout, const BgpPath *path,
     307             :                                 BgpAttr *attr, bool llgr_stale_comm) {
     308       12428 :     if (!server() || !server()->comm_db())
     309           0 :         return;
     310             : 
     311             :     // Skip LLGR specific attributes manipulation for rtarget routes.
     312       12428 :     if (family() == Address::RTARGET)
     313        3540 :         return;
     314             : 
     315             :     // If the path is not marked as llgr_stale or if it does not have the
     316             :     // LLGR_STALE community, then no action is necessary.
     317        8888 :     if (!path->IsLlgrStale() && !llgr_stale_comm)
     318        8888 :         return;
     319             : 
     320             :     // If peers support LLGR, then attach LLGR_STALE community and return.
     321           0 :     if (ribout->llgr()) {
     322           0 :         if (!llgr_stale_comm) {
     323             :             CommunityPtr comm = server()->comm_db()->AppendAndLocate(
     324           0 :                     attr->community(), CommunityType::LlgrStale);
     325           0 :             attr->set_community(comm);
     326           0 :         }
     327           0 :         return;
     328             :     }
     329             : 
     330             :     // Peers do not understand LLGR. Bring down local preference instead to
     331             :     // make the advertised path less preferred.
     332           0 :     attr->set_local_pref(0);
     333             : 
     334             :     // Remove LLGR_STALE community as the peers do not support LLGR.
     335           0 :     if (llgr_stale_comm) {
     336             :         CommunityPtr comm = server()->comm_db()->RemoveAndLocate(
     337           0 :                                 attr->community(), CommunityType::LlgrStale);
     338           0 :         attr->set_community(comm);
     339           0 :     }
     340             : 
     341             :     // Attach NO_EXPORT community as well to make sure that this path does not
     342             :     // exits local AS, unless it is already present.
     343           0 :     if (!attr->community() ||
     344           0 :         !attr->community()->ContainsValue(CommunityType::NoExport)) {
     345             :         CommunityPtr comm = server()->comm_db()->AppendAndLocate(
     346           0 :                                 attr->community(), CommunityType::NoExport);
     347           0 :         attr->set_community(comm);
     348           0 :     }
     349             : }
     350             : 
     351       12428 : void BgpTable::ProcessDefaultTunnelEncapsulation(const RibOut *ribout,
     352             :     ExtCommunityDB *extcomm_db, BgpAttr *attr) const {
     353       12428 :     if (!ribout->ExportPolicy().default_tunnel_encap_list.empty()) {
     354           0 :         ExtCommunity::ExtCommunityList encap_list;
     355           0 :         BOOST_FOREACH(const string &encap_string,
     356             :           ribout->ExportPolicy().default_tunnel_encap_list) {
     357           0 :             TunnelEncap tunnel_encap(encap_string);
     358           0 :             encap_list.push_back(tunnel_encap.GetExtCommunity());
     359             :         }
     360           0 :         ExtCommunityPtr ext_community = attr->ext_community();
     361             :         ext_community =
     362           0 :             extcomm_db->ReplaceTunnelEncapsulationAndLocate(
     363           0 :             ext_community.get(), encap_list);
     364           0 :         attr->set_ext_community(ext_community);
     365           0 :     }
     366       12428 : }
     367             : 
     368       12428 : void BgpTable::PrependAsToAsPath2Byte(BgpAttr *attr, as2_t asn) const {
     369       12428 :     if (attr->as_path()) {
     370        4460 :         const AsPathSpec &as_path = attr->as_path()->path();
     371        4460 :         AsPathSpec *as_path_ptr = as_path.Add(asn);
     372        4457 :         attr->set_as_path(as_path_ptr);
     373        4460 :         delete as_path_ptr;
     374             :     } else {
     375        7968 :         AsPathSpec as_path;
     376        7967 :         AsPathSpec *as_path_ptr = as_path.Add(asn);
     377        7956 :         attr->set_as_path(as_path_ptr);
     378        7968 :         delete as_path_ptr;
     379        7968 :     }
     380       12428 : }
     381             : 
     382           0 : void BgpTable::PrependAsToAsPath2Byte(BgpAttr *attr, as_t asn) const {
     383           0 :     if (asn <= AS2_MAX) {
     384           0 :         if (attr->as_path() && attr->as4_path()) {
     385           0 :             PrependAsToAs4Path(attr, asn);
     386             :         }
     387           0 :         return PrependAsToAsPath2Byte(attr, static_cast<as2_t>(asn & AS2_MAX));
     388             :     }
     389           0 :     if (attr->as_path() && !attr->as4_path()) {
     390           0 :         CreateAs4Path(attr);
     391           0 :         assert(attr->as_path()->path().path_segments.size() ==
     392             :                     attr->as4_path()->path().path_segments.size());
     393             :     }
     394           0 :     as2_t as_trans = AS_TRANS;
     395           0 :     PrependAsToAsPath2Byte(attr, as_trans);
     396           0 :     PrependAsToAs4Path(attr, asn);
     397             : }
     398             : 
     399           0 : void BgpTable::PrependAsToAsPath4Byte(BgpAttr *clone, as_t asn) const {
     400           0 :     if (clone->aspath_4byte()) {
     401           0 :         const AsPath4ByteSpec &as4_path = clone->aspath_4byte()->path();
     402           0 :         AsPath4ByteSpec *as4_path_ptr = as4_path.Add(asn);
     403           0 :         clone->set_aspath_4byte(as4_path_ptr);
     404           0 :         delete as4_path_ptr;
     405             :     } else {
     406           0 :         AsPath4ByteSpec as_path;
     407           0 :         AsPath4ByteSpec *as_path_ptr = as_path.Add(asn);
     408           0 :         clone->set_aspath_4byte(as_path_ptr);
     409           0 :         delete as_path_ptr;
     410           0 :     }
     411           0 : }
     412             : 
     413           0 : void BgpTable::PrependAsToAs4Path(BgpAttr* attr, as_t asn) const {
     414           0 :     if (attr->as4_path()) {
     415           0 :         const As4PathSpec &as4_path = attr->as4_path()->path();
     416           0 :         As4PathSpec *as4_path_ptr = as4_path.Add(asn);
     417           0 :         attr->set_as4_path(as4_path_ptr);
     418           0 :         delete as4_path_ptr;
     419             :     } else {
     420           0 :         As4PathSpec as_path;
     421           0 :         As4PathSpec *as_path_ptr = as_path.Add(asn);
     422           0 :         attr->set_as4_path(as_path_ptr);
     423           0 :         delete as_path_ptr;
     424           0 :     }
     425           0 : }
     426             : 
     427             : // Create as4_path by copying data from as_path
     428           0 : void BgpTable::CreateAs4Path(BgpAttr *attr) const {
     429           0 :     if (!attr->as_path()) {
     430           0 :         return;
     431             :     }
     432             : 
     433           0 :     std::unique_ptr<As4PathSpec> new_as_path(new As4PathSpec);
     434           0 :     const AsPathSpec &as_path = attr->as_path()->path();
     435           0 :     for (size_t i = 0; i < as_path.path_segments.size(); i++) {
     436           0 :         As4PathSpec::PathSegment *ps4 = new As4PathSpec::PathSegment;
     437           0 :         AsPathSpec::PathSegment *ps = as_path.path_segments[i];
     438           0 :         ps4->path_segment_type = ps->path_segment_type;
     439           0 :         for (size_t j = 0; j < ps->path_segment.size(); j++) {
     440           0 :             as_t as = ps->path_segment[j];
     441           0 :             ps4->path_segment.push_back(as);
     442             :         }
     443           0 :         new_as_path->path_segments.push_back(ps4);
     444             :     }
     445           0 :     attr->set_as4_path(new_as_path.get());
     446           0 : }
     447             : 
     448             : // Check if aspath_4byte has any asn > 0xFFFF
     449           0 : bool BgpTable::Has4ByteAsn(BgpAttr *attr) const {
     450           0 :     if (!attr->aspath_4byte())
     451           0 :         return false;
     452           0 :     const AsPath4ByteSpec &as_path4 = attr->aspath_4byte()->path();
     453           0 :     for (size_t i = 0; i < as_path4.path_segments.size(); ++i) {
     454           0 :         AsPath4ByteSpec::PathSegment *ps4 = as_path4.path_segments[i];
     455           0 :         for (size_t j = 0; j < ps4->path_segment.size(); ++j) {
     456           0 :             if (ps4->path_segment[j] > AS2_MAX)
     457           0 :                 return true;
     458             :         }
     459             :     }
     460           0 :     return false;
     461             : }
     462             : 
     463             : // Create as_path (and as4_path) from as_path4byte
     464           0 : void BgpTable::CreateAsPath2Byte(BgpAttr *attr) const {
     465           0 :     std::unique_ptr<AsPathSpec> new_as_path(new AsPathSpec);
     466           0 :     As4PathSpec *new_as4_path = NULL;
     467           0 :     bool has_4byte_asn = Has4ByteAsn(attr);
     468           0 :     if (has_4byte_asn)
     469           0 :         new_as4_path = new As4PathSpec;
     470           0 :     if (attr->aspath_4byte()) {
     471           0 :         const AsPath4ByteSpec &as_path4 = attr->aspath_4byte()->path();
     472           0 :         for (size_t i = 0; i < as_path4.path_segments.size(); i++) {
     473           0 :             AsPathSpec::PathSegment *ps = new AsPathSpec::PathSegment;
     474           0 :             As4PathSpec::PathSegment *as4_ps = NULL;
     475           0 :             AsPath4ByteSpec::PathSegment *ps4 = as_path4.path_segments[i];
     476           0 :             ps->path_segment_type = ps4->path_segment_type;
     477           0 :             if (has_4byte_asn) {
     478           0 :                 as4_ps = new As4PathSpec::PathSegment;
     479           0 :                 as4_ps->path_segment_type = ps4->path_segment_type;
     480             :             }
     481           0 :             for (size_t j = 0; j < ps4->path_segment.size(); ++j) {
     482           0 :                 as_t as4 = ps4->path_segment[j];
     483           0 :                 if (as4 > AS2_MAX) {
     484           0 :                     as2_t as_trans = AS_TRANS;
     485           0 :                     ps->path_segment.push_back(as_trans);
     486             :                 } else {
     487           0 :                     ps->path_segment.push_back(static_cast<as2_t>(
     488             :                                                as4 & AS2_MAX));
     489             :                 }
     490           0 :                 if (has_4byte_asn)
     491           0 :                     as4_ps->path_segment.push_back(as4);
     492             :             }
     493           0 :             new_as_path->path_segments.push_back(ps);
     494           0 :             if (has_4byte_asn)
     495           0 :                 new_as4_path->path_segments.push_back(as4_ps);
     496             :         }
     497           0 :         attr->set_aspath_4byte(NULL);
     498             :     }
     499           0 :     attr->set_as_path(new_as_path.get());
     500           0 :     if (has_4byte_asn) {
     501           0 :         attr->set_as4_path(new_as4_path);
     502           0 :         delete new_as4_path;
     503             :     }
     504           0 : }
     505             : 
     506             : // Create aspath_4byte by merging as_path and as4_path
     507           0 : void BgpTable::CreateAsPath4Byte(BgpAttr *attr, as_t local_as) const {
     508           0 :     int as2_count = attr->as_path_count();
     509           0 :     int as4_count = attr->as4_path_count();
     510           0 :     if (as2_count < as4_count) {
     511           0 :         as4_count = 0;
     512           0 :         attr->set_as4_path(NULL);
     513             :     }
     514           0 :     AsPath4ByteSpec::PathSegment *ps4 = NULL;
     515           0 :     bool part_segment = false;
     516           0 :     std::unique_ptr <AsPath4ByteSpec> aspath_4byte(new AsPath4ByteSpec);
     517           0 :     if (attr->as_path()) {
     518           0 :         const AsPathSpec &as_path = attr->as_path()->path();
     519           0 :         int new_as_count = 0;
     520           0 :         for (size_t i = 0; i < as_path.path_segments.size() &&
     521           0 :                     new_as_count < (as2_count - as4_count) ; ++i) {
     522           0 :             ps4 = new AsPath4ByteSpec::PathSegment;
     523           0 :             AsPathSpec::PathSegment *ps = as_path.path_segments[i];
     524           0 :             ps4->path_segment_type = ps->path_segment_type;
     525           0 :             if (ps->path_segment_type == AsPathSpec::PathSegment::AS_SET) {
     526           0 :                 new_as_count++;
     527           0 :                 for (size_t j = 0; j < ps->path_segment.size(); ++j) {
     528           0 :                     as2_t as = ps->path_segment[j];
     529           0 :                     ps4->path_segment.push_back(as);
     530             :                 }
     531             :             } else {
     532           0 :                 for (size_t j = 0; j < ps->path_segment.size() &&
     533           0 :                         new_as_count < (as2_count - as4_count); ++j) {
     534           0 :                     new_as_count++;
     535           0 :                     as2_t as = ps->path_segment[j];
     536           0 :                     ps4->path_segment.push_back(as);
     537             :                 }
     538           0 :                 if (new_as_count == (as2_count - as4_count)) {
     539           0 :                     if (attr->as4_path()) {
     540           0 :                         part_segment = true;
     541           0 :                         break;
     542             :                     }
     543             :                 }
     544             :             }
     545           0 :             aspath_4byte->path_segments.push_back(ps4);
     546             :         }
     547           0 :         if (attr->as4_path()) {
     548           0 :             const As4PathSpec &as4_path = attr->as4_path()->path();
     549           0 :             for (size_t i = 0; i < as4_path.path_segments.size(); ++i) {
     550           0 :                 if (!part_segment) {
     551           0 :                     ps4 = new AsPath4ByteSpec::PathSegment;
     552             :                 }
     553           0 :                 part_segment = false;
     554           0 :                 As4PathSpec::PathSegment *ps = as4_path.path_segments[i];
     555           0 :                 ps4->path_segment_type = ps->path_segment_type;
     556           0 :                 if (ps->path_segment_type == As4PathSpec::PathSegment::AS_SET) {
     557           0 :                     new_as_count++;
     558           0 :                     for (size_t j = 0; j < ps->path_segment.size(); ++j) {
     559           0 :                         as2_t as = ps->path_segment[j];
     560           0 :                         ps4->path_segment.push_back(as);
     561             :                     }
     562             :                 } else {
     563           0 :                     for (size_t j = 0; j < ps->path_segment.size(); ++j) {
     564           0 :                         as_t as = ps->path_segment[j];
     565           0 :                         ps4->path_segment.push_back(as);
     566             :                     }
     567             :                 }
     568           0 :                 aspath_4byte->path_segments.push_back(ps4);
     569             :             }
     570           0 :             attr->set_as4_path(NULL);
     571             :         }
     572           0 :         attr->set_as_path(NULL);
     573             :     }
     574           0 :     if (local_as) {
     575           0 :         std::unique_ptr<AsPath4ByteSpec> as_path_spec(aspath_4byte->Add(local_as));
     576           0 :         attr->set_aspath_4byte(as_path_spec.get());
     577           0 :     } else {
     578           0 :         attr->set_aspath_4byte(aspath_4byte.get());
     579             :     }
     580           0 : }
     581             : 
     582       52218 : UpdateInfo *BgpTable::GetUpdateInfo(RibOut *ribout, BgpRoute *route,
     583             :         const RibPeerSet &peerset) {
     584       52218 :     const BgpPath *path = route->BestPath();
     585             : 
     586             :     // Ignore if there is no best-path.
     587       52246 :     if (!path)
     588           0 :         return NULL;
     589             : 
     590             :     // Don't advertise infeasible paths.
     591       52246 :     if (!path->IsFeasible())
     592           0 :         return NULL;
     593             : 
     594             :     // Check whether the route is contributing route
     595       52240 :     if (IsRouteAggregationSupported() && IsContributingRoute(route))
     596           0 :         return NULL;
     597             : 
     598             :     // Needs to be outside the if block so it's not destroyed prematurely.
     599       52228 :     BgpAttrPtr attr_ptr;
     600       52228 :     const BgpAttr *attr = path->GetAttr();
     601             : 
     602       52220 :     RibPeerSet new_peerset = peerset;
     603             : 
     604             :     // LocalPref, Med and AsPath manipulation is needed only if the RibOut
     605             :     // has BGP encoding. Similarly, well-known communities do not apply if
     606             :     // the encoding is not BGP.
     607       52209 :     if (ribout->IsEncodingBgp()) {
     608             :         // Handle well-known communities.
     609       23598 :         if (attr->community() != NULL &&
     610           0 :             attr->community()->communities().size()) {
     611           0 :             BOOST_FOREACH(uint32_t value, attr->community()->communities()) {
     612           0 :                 if (value == CommunityType::NoAdvertise)
     613           0 :                     return NULL;
     614             : 
     615           0 :                 if ((ribout->peer_type() == BgpProto::EBGP) &&
     616           0 :                     ((value == CommunityType::NoExport) ||
     617             :                      (value == CommunityType::NoExportSubconfed))) {
     618           0 :                     return NULL;
     619             :                 }
     620             :             }
     621             :         }
     622             : 
     623       23597 :         const IPeer *peer = path->GetPeer();
     624       23595 :         BgpAttr *clone = NULL;
     625       23595 :         bool llgr_stale_comm = attr->community() &&
     626           0 :             attr->community()->ContainsValue(CommunityType::LlgrStale);
     627       23594 :         if (ribout->peer_type() == BgpProto::IBGP) {
     628             :             // Split horizon check.
     629           0 :             if (peer && peer->CheckSplitHorizon(server()->cluster_id(),
     630             :                         ribout->cluster_id()))
     631           0 :                 return NULL;
     632             : 
     633             :             // Handle route-target filtering.
     634           0 :             if (IsVpnTable() && attr->ext_community() != NULL) {
     635           0 :                 server()->rtarget_group_mgr()->GetRibOutInterestedPeers(
     636             :                     ribout, attr->ext_community(), peerset, &new_peerset);
     637           0 :                 if (new_peerset.empty())
     638           0 :                     return NULL;
     639             :             }
     640             : 
     641           0 :             if (server()->cluster_id()) {
     642             :                 // Check if there is a loop in cluster_list
     643           0 :                 if (attr->cluster_list() && attr->cluster_list()->cluster_list()
     644           0 :                         .ClusterListLoop(server()->cluster_id())) {
     645           0 :                     return NULL;
     646             :                 }
     647             :             }
     648             : 
     649           0 :             if (server()->cluster_id() && (family() != Address::RTARGET)) {
     650             :                 // route reflector should not reflect the route back to the peer
     651             :                 // from which it received that route, for non rtarget routes
     652             :                 // This is done by checking peer_router_id of all the feasible
     653             :                 // paths of this route
     654           0 :                 RibPeerSet route_peerset;
     655           0 :                 for (Route::PathList::iterator it= route->GetPathList().begin();
     656           0 :                         it != route->GetPathList().end(); it++) {
     657           0 :                     BgpPath *ipath = static_cast<BgpPath *>(it.operator->());
     658           0 :                     if (ipath->IsFeasible() && ipath->GetPeer()) {
     659           0 :                         const IPeer *ipeer = ipath->GetPeer();
     660           0 :                         const BgpPeer *bgp_peer = dynamic_cast<
     661           0 :                                                   const BgpPeer *>(ipeer);
     662           0 :                         if (bgp_peer)
     663           0 :                             route_peerset.set(bgp_peer->GetIndex());
     664             :                     }
     665             :                 }
     666           0 :                 RibOut::PeerIterator iter(ribout, new_peerset);
     667           0 :                 while (iter.HasNext()) {
     668           0 :                     int current_index = iter.index();
     669           0 :                     IPeerUpdate *peer = iter.Next();
     670           0 :                     const BgpPeer *bgp_peer = dynamic_cast<
     671           0 :                                               const BgpPeer *>(peer);
     672           0 :                     if (bgp_peer && route_peerset.test(bgp_peer->GetIndex()))
     673           0 :                         new_peerset.reset(current_index);
     674             :                 }
     675           0 :                 if (new_peerset.empty())
     676           0 :                     return NULL;
     677           0 :             }
     678             : 
     679           0 :             clone = new BgpAttr(*attr);
     680             : 
     681             :             // Retain LocalPref value if set, else set default to 100.
     682           0 :             if (clone->local_pref() == 0)
     683           0 :                 clone->set_local_pref(100);
     684             : 
     685             :             // Check aggregator attributes to identify which ones to be used
     686           0 :             CheckAggregatorAttr(clone);
     687             : 
     688             :             // Should not normally be needed for iBGP, but there could be
     689             :             // complex configurations where this is useful.
     690           0 :             ProcessRemovePrivate(ribout, clone);
     691             : 
     692             :             // Add Originator_Id if acting as route reflector and cluster_id
     693             :             // is not present
     694           0 :             if (server()->cluster_id()) {
     695           0 :                 if (clone->originator_id().is_unspecified()) {
     696           0 :                     if (peer && (peer->bgp_identifier() != 0)) {
     697           0 :                         clone->set_originator_id(Ip4Address(
     698           0 :                                 peer->bgp_identifier()));
     699             :                     } else {
     700           0 :                         clone->set_originator_id(Ip4Address(
     701             :                                 server()->bgp_identifier()));
     702             :                     }
     703             :                 }
     704           0 :                 if (attr->cluster_list()) {
     705             :                     const ClusterListSpec &cluster =
     706           0 :                         clone->cluster_list()->cluster_list();
     707             :                     ClusterListSpec *cl_ptr = new ClusterListSpec(
     708           0 :                             server()->cluster_id(), &cluster);
     709           0 :                     clone->set_cluster_list(cl_ptr);
     710           0 :                     delete cl_ptr;
     711             :                 } else {
     712             :                     ClusterListSpec *cl_ptr = new ClusterListSpec(
     713           0 :                             server()->cluster_id(), NULL);
     714           0 :                     clone->set_cluster_list(cl_ptr);
     715           0 :                     delete cl_ptr;
     716             :                 }
     717             :             }
     718             :             // If the route is locally originated i.e. there's no AsPath,
     719             :             // then generate a Nil AsPath i.e. one with 0 length. No need
     720             :             // to modify the AsPath if it already exists since this is an
     721             :             // iBGP RibOut.
     722           0 :             if (ribout->as4_supported() && !clone->aspath_4byte()) {
     723           0 :                 if (attr->as_path()) {
     724           0 :                     CreateAsPath4Byte(clone, 0);
     725             :                 } else {
     726           0 :                     AsPath4ByteSpec as_path;
     727           0 :                     clone->set_aspath_4byte(&as_path);
     728           0 :                 }
     729             :             }
     730           0 :             if (!ribout->as4_supported() && !clone->as_path()) {
     731           0 :                 if (attr->aspath_4byte()) {
     732           0 :                     CreateAsPath2Byte(clone);
     733             :                 } else {
     734           0 :                     AsPathSpec as_path;
     735           0 :                     clone->set_as_path(&as_path);
     736           0 :                 }
     737             :             }
     738       23594 :         } else if (ribout->peer_type() == BgpProto::EBGP) {
     739             :             // Don't advertise routes from non-master instances if there's
     740             :             // no nexthop. The ribout has to be for bgpaas-clients because
     741             :             // that's the only case with bgp peers in non-master instance.
     742       23592 :             if (!rtinstance_->IsMasterRoutingInstance() &&
     743           0 :                 ribout->nexthop().is_unspecified()) {
     744           0 :                 return NULL;
     745             :             }
     746             : 
     747             :             // Handle route-target filtering.
     748       23592 :             if (IsVpnTable() && attr->ext_community() != NULL) {
     749       18625 :                 server()->rtarget_group_mgr()->GetRibOutInterestedPeers(
     750             :                     ribout, attr->ext_community(), peerset, &new_peerset);
     751       18668 :                 if (new_peerset.empty())
     752        1557 :                     return NULL;
     753             :             }
     754             : 
     755             :             // Sender side AS path loop check and split horizon within RibOut.
     756       22076 :             if (!ribout->as_override()) {
     757       22076 :                 if (attr->IsAsPathLoop(ribout->peer_as()))
     758        9647 :                     return NULL;
     759             :             } else {
     760           0 :                 if (peer && peer->PeerType() == BgpProto::EBGP) {
     761           0 :                     ribout->GetSubsetPeerSet(&new_peerset, peer);
     762           0 :                     if (new_peerset.empty())
     763           0 :                         return NULL;
     764             :                 }
     765             :             }
     766             : 
     767       12426 :             clone = new BgpAttr(*attr);
     768             : 
     769             :             // Remove non-transitive attributes.
     770             :             // Note that med is handled further down.
     771       12428 :             clone->set_originator_id(Ip4Address());
     772       12428 :             clone->set_cluster_list(NULL);
     773             : 
     774             :             // Update nexthop.
     775       12428 :             if (!ribout->nexthop().is_unspecified())
     776           0 :                 clone->set_nexthop(ribout->nexthop());
     777             : 
     778             :             // Reset LocalPref.
     779       12427 :             if (clone->local_pref())
     780       12427 :                 clone->set_local_pref(0);
     781             : 
     782             :             // Reset Med if the path did not originate from an xmpp peer.
     783             :             // The AS path is NULL if the originating xmpp peer is locally
     784             :             // connected. It's non-NULL but empty if the originating xmpp
     785             :             // peer is connected to another bgp speaker in the iBGP mesh.
     786       12426 :             if (clone->med() && !clone->IsAsPathEmpty())
     787           0 :                 clone->set_med(0);
     788             : 
     789             :             // Override the peer AS with local AS in AsPath.
     790       12427 :             ProcessAsOverride(ribout, clone);
     791             : 
     792             :             // Remove private processing must happen before local AS prepend.
     793       12427 :             ProcessRemovePrivate(ribout, clone);
     794             : 
     795             :             // Prepend the local AS to AsPath.
     796       12428 :             PrependLocalAs(ribout, clone, peer);
     797             :         }
     798             : 
     799       12428 :         assert(clone);
     800             : 
     801             :         // Update with the Default tunnel Encapsulation ordered List if
     802             :         // configured on the peer.
     803             :         // Note that all peers with the same list share the same Ribout, this is
     804             :         // ensured by making the Default Encapsulation List part of the Rib
     805             :         // Export policy.Note that, if there is Default Tunnel Encapsulation
     806             :         // configuration any tunnel encapsulation present is removed.
     807       12428 :         ProcessDefaultTunnelEncapsulation(ribout, server()->extcomm_db(),
     808             :                                           clone);
     809             : 
     810       12428 :         ProcessLlgrState(ribout, path, clone, llgr_stale_comm);
     811             : 
     812             :         // Locate the new BgpAttrPtr.
     813       12428 :         attr_ptr = clone->attr_db()->Locate(clone);
     814       12428 :         attr = attr_ptr.get();
     815             :     }
     816             : 
     817       41040 :     UpdateInfo *uinfo = new UpdateInfo;
     818       41095 :     uinfo->target = new_peerset;
     819       41084 :     uinfo->roattr = RibOutAttr(route, attr, ribout->IsEncodingXmpp());
     820       41159 :     return uinfo;
     821       52363 : }
     822             : 
     823             : // Bgp Path selection..
     824             : // Based Attribute weight
     825        2160 : bool BgpTable::PathSelection(const Path &path1, const Path &path2) {
     826        2160 :     const BgpPath &l_path = dynamic_cast<const BgpPath &> (path1);
     827        2160 :     const BgpPath &r_path = dynamic_cast<const BgpPath &> (path2);
     828             : 
     829             :     // Check the weight of Path
     830        2160 :     bool res = l_path.PathCompare(r_path, false) < 0;
     831             : 
     832        2160 :     return res;
     833             : }
     834             : 
     835           0 : bool BgpTable::DeletePath(DBTablePartBase *root, BgpRoute *rt, BgpPath *path) {
     836           0 :     return InputCommon(root, rt, path, path->GetPeer(), NULL,
     837           0 :         DBRequest::DB_ENTRY_DELETE, NULL, path->GetPathId(), 0, 0, 0);
     838             : }
     839             : 
     840       21304 : bool BgpTable::InputCommon(DBTablePartBase *root, BgpRoute *rt, BgpPath *path,
     841             :                            const IPeer *peer, DBRequest *req,
     842             :                            DBRequest::DBOperation oper, BgpAttrPtr attrs,
     843             :                            uint32_t path_id, uint32_t flags, uint32_t label,
     844             :                            uint32_t l3_label) {
     845       21304 :     bool notify_rt = false;
     846             : 
     847       21304 :     switch (oper) {
     848       11109 :     case DBRequest::DB_ENTRY_ADD_CHANGE: {
     849       11109 :         assert(rt);
     850             : 
     851             :         // The entry may currently be marked as deleted.
     852       11109 :         rt->ClearDelete();
     853       11108 :         if (peer)
     854       10596 :             peer->UpdateCloseRouteStats(family(), path, flags);
     855             : 
     856             :         // Check whether peer already has a path.
     857       11107 :         if (path != NULL) {
     858        1810 :             if ((path->GetAttr() != attrs.get()) ||
     859         905 :                 (path->GetFlags() != flags) ||
     860        1810 :                 (path->GetLabel() != label) ||
     861           0 :                 (path->GetL3Label() != l3_label)) {
     862             :                 // Update Attributes and notify (if needed)
     863         905 :                 if (path->NeedsResolution())
     864           0 :                     path_resolver_->StopPathResolution(root->index(), path);
     865         905 :                 rt->DeletePath(path);
     866             :             } else {
     867             :                 // Ignore duplicate update.
     868           0 :                 break;
     869             :             }
     870             :         }
     871             : 
     872             :         BgpPath *new_path;
     873       22217 :         new_path = new BgpPath(
     874       11110 :             peer, path_id, BgpPath::BGP_XMPP, attrs, flags, label, l3_label);
     875             : 
     876             :         // Start path resolution for the new path if
     877             :         // (1) Path flag has ResolveNextHop set in which case the path will
     878             :         //     be resolved in the overlay routing table (or)
     879             :         // (2) Fast convergence knob is enabled in which case the path will
     880             :         //     be marked for resolution in the underlay (default) routing table
     881             :         // (3) Path resolution in this case is needed only for routes
     882             :         //     in the VRF routing tables and for INET, INET6 and EVPN
     883             :         //     address families.
     884       11110 :         bool fc_family = false;
     885       21258 :         if (family() == Address::INET || family() == Address::INET6 ||
     886       10149 :             family() == Address::EVPN) {
     887        2752 :             fc_family = true;
     888             :         }
     889       11108 :         bool fc_enabled = (server()->IsNextHopCheckEnabled() &&
     890       11108 :                        !rtinstance_->IsMasterRoutingInstance() && fc_family);
     891       11108 :         if (new_path->NeedsResolution() || fc_enabled) {
     892           0 :             if (peer && !(peer->IsXmppPeer()) ) {
     893           0 :                 rt->add_peer_sources(peer->ToString());
     894             :             }
     895           0 :             Address::Family family = new_path->GetAttr()->nexthop_family();
     896             :             BgpTable *table;
     897           0 :             if (new_path->NeedsResolution()) {
     898           0 :                 table = rtinstance_->GetTable(family);
     899             :             } else {
     900             :                 // Fast convergence knob is enabled.
     901             :                 // Update path flag to indicate need for resolution.
     902           0 :                 new_path->SetResolveNextHop();
     903             : 
     904             :                 // Get the default table.
     905           0 :                 RoutingInstanceMgr *mgr = server()->routing_instance_mgr();
     906           0 :                 assert(mgr);
     907           0 :                 RoutingInstance *master_ri = mgr->GetDefaultRoutingInstance();
     908           0 :                 assert(master_ri);
     909           0 :                 table = master_ri->GetTable(family);
     910             :             }
     911           0 :             path_resolver_->StartPathResolution(rt, new_path, table);
     912             :         }
     913             : 
     914       11107 :         rt->InsertPath(new_path);
     915       11110 :         notify_rt = true;
     916       11110 :         break;
     917             :     }
     918             : 
     919       10196 :     case DBRequest::DB_ENTRY_DELETE: {
     920       10196 :         if (rt && !rt->IsDeleted()) {
     921       10196 :             if (peer && !(peer->IsXmppPeer()) ) {
     922        8070 :                 rt->del_peer_sources(peer->ToString());
     923             :             }
     924       11236 :             BGP_LOG_ROUTE(this, const_cast<IPeer *>(peer), rt,
     925             :                           "Delete BGP path");
     926             : 
     927             :             // Remove the Path from the route
     928       10197 :             if (path->NeedsResolution())
     929           0 :                 path_resolver_->StopPathResolution(root->index(), path);
     930       10197 :             rt->DeletePath(path);
     931       10200 :             notify_rt = true;
     932             :         }
     933       10200 :         break;
     934             :     }
     935             : 
     936           0 :     default: {
     937           0 :         assert(false);
     938             :         break;
     939             :     }
     940             :     }
     941       21310 :     return notify_rt;
     942             : }
     943             : 
     944       15869 : void BgpTable::Input(DBTablePartition *root, DBClient *client,
     945             :                      DBRequest *req) {
     946       15869 :     const IPeer *peer = (static_cast<RequestKey *>(req->key.get()))->GetPeer();
     947       15869 :     RequestData *data = static_cast<RequestData *>(req->data.get());
     948             : 
     949       15869 :     if (req->oper == DBRequest::DB_ENTRY_ADD_CHANGE && peer) {
     950             :         // Skip if this peer is down.
     951       10178 :         if (!peer->IsReady())
     952         898 :             return;
     953             : 
     954             :         // For xmpp peers, verify that agent is subscribed to the table and
     955             :         // the route add is from the same incarnation of table subscription.
     956        9792 :         if (peer->IsXmppPeer() && peer->IsRegistrationRequired()) {
     957        1616 :             BgpMembershipManager *mgr = rtinstance_->server()->membership_mgr();
     958        1616 :             int instance_id = -1;
     959        1616 :             uint64_t subscription_gen_id = 0;
     960             :             bool is_registered =
     961        1616 :                 mgr->GetRegistrationInfo(const_cast<IPeer *>(peer), this,
     962             :                                          &instance_id, &subscription_gen_id);
     963        2512 :             if ((!is_registered && (family() != Address::RTARGET)) ||
     964         896 :                 (is_registered &&
     965         896 :                  (subscription_gen_id != data->subscription_gen_id()))) {
     966           0 :                 return;
     967             :             }
     968             :         }
     969             :     }
     970             : 
     971             :     // Create route if it's not already present in case of add/change.
     972       15483 :     BgpRoute *rt = TableFind(root, req->key.get());
     973       15476 :     if (!rt) {
     974       10170 :         if ((req->oper == DBRequest::DB_ENTRY_DELETE) ||
     975        9659 :             (req->oper == DBRequest::DB_ENTRY_NOTIFY))
     976         511 :             return;
     977             : 
     978        9659 :         rt = static_cast<BgpRoute *>(Add(req));
     979        9652 :         static_cast<DBTablePartition *>(root)->Add(rt);
     980       10593 :         BGP_LOG_ROUTE(this, const_cast<IPeer *>(peer), rt,
     981             :                       "Insert new BGP path");
     982             :     }
     983             : 
     984       14967 :     if (req->oper == DBRequest::DB_ENTRY_NOTIFY) {
     985           0 :         root->Notify(rt);
     986           0 :         return;
     987             :     }
     988             : 
     989       14967 :     uint32_t path_id = 0;
     990       14967 :     uint32_t flags = 0;
     991       14967 :     uint32_t label = 0;
     992       14967 :     uint32_t l3_label = 0;
     993       14967 :     BgpPath *path = rt->FindPath(peer);
     994       14968 :     BgpAttrPtr attr = data ? data->attrs() : NULL;
     995       14970 :     if (req->oper == DBRequest::DB_ENTRY_ADD_CHANGE) {
     996       10304 :         assert(data);
     997       10304 :         const RequestData::NextHop &nexthop = data->nexthop();
     998       10304 :         if (nexthop.address_.is_v4())
     999       10304 :             path_id = nexthop.address_.to_v4().to_ulong();
    1000       10304 :         flags = nexthop.flags_;
    1001       10304 :         label = nexthop.label_;
    1002       10304 :         l3_label = nexthop.l3_label_;
    1003             : 
    1004       10304 :         attr = GetAttributes(rt, attr, peer);
    1005             :     } else {
    1006        4666 :         if (!path)
    1007           0 :             return;
    1008        4666 :         path_id = path->GetPathId();
    1009             :     }
    1010             : 
    1011       14969 :     bool notify_rt = InputCommon(root, rt, path, peer, req, req->oper,
    1012             :         attr, path_id, flags, label, l3_label);
    1013       14972 :     InputCommonPostProcess(root, rt, notify_rt);
    1014       14970 : }
    1015             : 
    1016       63410 : void BgpTable::InputCommonPostProcess(DBTablePartBase *root,
    1017             :                                       BgpRoute *rt, bool notify_rt) {
    1018       63410 :     if (!notify_rt)
    1019       42113 :         return;
    1020             : 
    1021       21297 :     if (rt->front() == NULL)
    1022        9655 :         root->Delete(rt);
    1023             :     else
    1024       11648 :         root->Notify(rt);
    1025             : }
    1026             : 
    1027         580 : bool BgpTable::MayDelete() const {
    1028         580 :     CHECK_CONCURRENCY("bgp::Config");
    1029             : 
    1030             :     // Bail if the table has listeners.
    1031         580 :     if (HasListeners()) {
    1032           0 :         BGP_LOG_TABLE(this, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL,
    1033             :                       "Paused table deletion due to pending listeners");
    1034           0 :         return false;
    1035             :     }
    1036             : 
    1037             :     // Bail if the table has walkers.
    1038         580 :     if (HasWalkers()) {
    1039           0 :         BGP_LOG_TABLE(this, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL,
    1040             :                       "Paused table deletion due to pending walkers");
    1041           0 :         return false;
    1042             :     }
    1043             : 
    1044             :     // Bail if the table is not empty.
    1045         580 :     size_t size = Size();
    1046         580 :     if (size > 0) {
    1047           0 :         BGP_LOG_TABLE(this, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL,
    1048             :                       "Paused table deletion due to " << size <<
    1049             :                       " pending routes");
    1050           0 :         return false;
    1051             :     }
    1052             : 
    1053             :     // Check the base class at the end so that we add custom checks
    1054             :     // before this if needed and to get more informative log message.
    1055         580 :     if (!DBTableBase::MayDelete())
    1056           0 :         return false;
    1057             : 
    1058         580 :     return true;
    1059             : }
    1060             : 
    1061         580 : void BgpTable::Shutdown() {
    1062         580 :     CHECK_CONCURRENCY("bgp::PeerMembership", "bgp::Config");
    1063         580 : }
    1064             : 
    1065         580 : void BgpTable::ManagedDelete() {
    1066         580 :     BGP_LOG_TABLE(this, SandeshLevel::SYS_INFO, BGP_LOG_FLAG_ALL,
    1067             :                   "Received request for table deletion");
    1068         580 :     deleter_->Delete();
    1069         580 : }
    1070             : 
    1071             : //
    1072             : // Retry deletion of the table if it is pending.
    1073             : //
    1074        5435 : void BgpTable::RetryDelete() {
    1075        5435 :     if (!deleter()->IsDeleted())
    1076        4867 :         return;
    1077         566 :     deleter()->RetryDelete();
    1078             : }
    1079             : 
    1080           0 : PathResolver *BgpTable::CreatePathResolver() {
    1081           0 :     return NULL;
    1082             : }
    1083             : 
    1084         320 : void BgpTable::LocatePathResolver() {
    1085         320 :     if (path_resolver_)
    1086           0 :         return;
    1087         320 :     assert(!deleter()->IsDeleted());
    1088         320 :     path_resolver_ = CreatePathResolver();
    1089             : }
    1090             : 
    1091         320 : void BgpTable::DestroyPathResolver() {
    1092         320 :     if (!path_resolver_)
    1093           0 :         return;
    1094         320 :     delete path_resolver_;
    1095         320 :     path_resolver_ = NULL;
    1096             : }
    1097             : 
    1098           0 : size_t BgpTable::GetPendingRiboutsCount(size_t *markers) const {
    1099           0 :     CHECK_CONCURRENCY("bgp::ShowCommand", "bgp::Config");
    1100           0 :     size_t count = 0;
    1101           0 :     *markers = 0;
    1102             : 
    1103           0 :     BOOST_FOREACH(const RibOutMap::value_type &value, ribout_map_) {
    1104           0 :         const RibOut *ribout = value.second;
    1105           0 :         for (int idx = 0; idx < DB::PartitionCount(); ++idx) {
    1106           0 :             const RibOutUpdates *updates = ribout->updates(idx);
    1107           0 :             for (int qid = RibOutUpdates::QFIRST; qid < RibOutUpdates::QCOUNT;
    1108             :                  ++qid) {
    1109           0 :                 count += updates->queue_size(qid);
    1110           0 :                 *markers += updates->queue_marker_count(qid);
    1111             :             }
    1112             :         }
    1113             :     }
    1114             : 
    1115           0 :     return count;
    1116             : }
    1117             : 
    1118        8728 : LifetimeActor *BgpTable::deleter() {
    1119        8728 :     return deleter_.get();
    1120             : }
    1121             : 
    1122        2184 : const LifetimeActor *BgpTable::deleter() const {
    1123        2184 :     return deleter_.get();
    1124             : }
    1125             : 
    1126       79229 : void BgpTable::UpdatePathCount(const BgpPath *path, int count) {
    1127       79229 :     if (dynamic_cast<const BgpSecondaryPath *>(path)) {
    1128       56998 :         secondary_path_count_ += count;
    1129             :     } else {
    1130       22231 :         primary_path_count_ += count;
    1131             :     }
    1132             : 
    1133       79339 :     if (!path->IsFeasible()) {
    1134           0 :         infeasible_path_count_ += count;
    1135             :     }
    1136             : 
    1137       79325 :     if (path->IsStale()) {
    1138        5258 :         stale_path_count_ += count;
    1139             :     }
    1140             : 
    1141       79323 :     if (path->IsLlgrStale()) {
    1142           0 :         llgr_stale_path_count_ += count;
    1143             :     }
    1144       79308 : }
    1145             : 
    1146             : // Check whether the route is aggregate route
    1147           0 : bool BgpTable::IsAggregateRoute(const BgpRoute *route) const {
    1148           0 :     return routing_instance()->IsAggregateRoute(this, route);
    1149             : }
    1150             : 
    1151             : // Check whether the route is contributing route to aggregate route
    1152       56345 : bool BgpTable::IsContributingRoute(const BgpRoute *route) const {
    1153       56345 :     return routing_instance()->IsContributingRoute(this, route);
    1154             : }
    1155             : 
    1156           0 : void BgpTable::FillRibOutStatisticsInfo(
    1157             :     vector<ShowRibOutStatistics> *sros_list) const {
    1158           0 :     BOOST_FOREACH(const RibOutMap::value_type &value, ribout_map_) {
    1159           0 :         const RibOut *ribout = value.second;
    1160           0 :         ribout->FillStatisticsInfo(sros_list);
    1161             :     }
    1162           0 : }

Generated by: LCOV version 1.14