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: 302 678 44.5 %
Date: 2026-11-23 02:04:52 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         782 :     explicit DeleteActor(BgpTable *table)
      35         782 :         : LifetimeActor(table->rtinstance_->server()->lifetime_manager()),
      36         782 :           table_(table) {
      37         782 :     }
      38        1564 :     virtual ~DeleteActor() {
      39        1564 :     }
      40         782 :     virtual bool MayDelete() const {
      41         782 :         return table_->MayDelete();
      42             :     }
      43             : 
      44         782 :     virtual void Shutdown() {
      45         782 :         table_->Shutdown();
      46         782 :     }
      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         782 :     virtual void Destroy() {
      52         782 :         table_->rtinstance_->DestroyDBTable(table_);
      53         782 :     }
      54             : 
      55             :   private:
      56             :     BgpTable *table_;
      57             : };
      58             : 
      59         782 : BgpTable::BgpTable(DB *db, const string &name)
      60             :     : RouteTable(db, name),
      61         782 :       rtinstance_(NULL),
      62         782 :       path_resolver_(NULL),
      63         782 :       instance_delete_ref_(this, NULL) {
      64         782 :     primary_path_count_ = 0;
      65         782 :     secondary_path_count_ = 0;
      66         782 :     infeasible_path_count_ = 0;
      67         782 :     stale_path_count_ = 0;
      68         782 :     llgr_stale_path_count_ = 0;
      69         782 : }
      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         782 : BgpTable::~BgpTable() {
      77         782 :     assert(path_resolver_ == NULL),
      78         782 :     instance_delete_ref_.Reset(NULL);
      79         782 : }
      80             : 
      81         782 : void BgpTable::set_routing_instance(RoutingInstance *rtinstance) {
      82         782 :     rtinstance_ = rtinstance;
      83         782 :     assert(rtinstance);
      84         782 :     deleter_.reset(new DeleteActor(this));
      85         782 :     instance_delete_ref_.Reset(rtinstance->deleter());
      86         782 : }
      87             : 
      88       61183 : BgpServer *BgpTable::server() {
      89       61183 :     return rtinstance_->server();
      90             : }
      91             : 
      92        9354 : const BgpServer *BgpTable::server() const {
      93        9354 :     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         862 : RibOut *BgpTable::RibOutLocate(BgpUpdateSender *sender,
     110             :                                const RibExportPolicy &policy) {
     111         862 :     RibOutMap::iterator loc = ribout_map_.find(policy);
     112         862 :     if (loc == ribout_map_.end()) {
     113         862 :         RibOut *ribout = new RibOut(this, sender, policy);
     114         862 :         ribout_map_.insert(make_pair(policy, ribout));
     115         862 :         return ribout;
     116             :     }
     117           0 :     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         862 : void BgpTable::RibOutDelete(const RibExportPolicy &policy) {
     125         862 :     RibOutMap::iterator loc = ribout_map_.find(policy);
     126         862 :     assert(loc != ribout_map_.end());
     127         862 :     delete loc->second;
     128         862 :     ribout_map_.erase(loc);
     129         862 : }
     130             : 
     131             : // If both as2 and as4 aggregators are present then we need to choose one
     132        4397 : void BgpTable::CheckAggregatorAttr(BgpAttr *attr) const {
     133        4397 :     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        4397 : }
     147             : 
     148        1061 : void BgpTable::PrependLocalAs(const RibOut *ribout, BgpAttr *clone,
     149             :         const IPeer* peer) const {
     150        1061 :     CheckAggregatorAttr(clone);
     151        1061 :     as_t local_as = ribout->local_as() ?:
     152           0 :                 clone->attr_db()->server()->local_autonomous_system();
     153        1061 :     if (!server()->enable_4byte_as())
     154        1060 :        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        4397 : void BgpTable::ProcessRemovePrivate(const RibOut *ribout, BgpAttr *attr) const {
     245        4397 :     if (!ribout->IsEncodingBgp())
     246           0 :         return;
     247        4397 :     if (!ribout->remove_private_enabled())
     248        4397 :         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        1062 : void BgpTable::ProcessAsOverride(const RibOut *ribout, BgpAttr *attr) const {
     262        1062 :     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        1061 : }
     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        4403 : void BgpTable::ProcessLlgrState(const RibOut *ribout, const BgpPath *path,
     307             :                                 BgpAttr *attr, bool llgr_stale_comm) {
     308        4403 :     if (!server() || !server()->comm_db())
     309           0 :         return;
     310             : 
     311             :     // Skip LLGR specific attributes manipulation for rtarget routes.
     312        4403 :     if (family() == Address::RTARGET)
     313           0 :         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        4403 :     if (!path->IsLlgrStale() && !llgr_stale_comm)
     318        3869 :         return;
     319             : 
     320             :     // If peers support LLGR, then attach LLGR_STALE community and return.
     321         534 :     if (ribout->llgr()) {
     322         534 :         if (!llgr_stale_comm) {
     323             :             CommunityPtr comm = server()->comm_db()->AppendAndLocate(
     324         534 :                     attr->community(), CommunityType::LlgrStale);
     325         534 :             attr->set_community(comm);
     326         534 :         }
     327         534 :         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        4404 : void BgpTable::ProcessDefaultTunnelEncapsulation(const RibOut *ribout,
     352             :     ExtCommunityDB *extcomm_db, BgpAttr *attr) const {
     353        4404 :     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        4403 : }
     367             : 
     368        1061 : void BgpTable::PrependAsToAsPath2Byte(BgpAttr *attr, as2_t asn) const {
     369        1061 :     if (attr->as_path()) {
     370         572 :         const AsPathSpec &as_path = attr->as_path()->path();
     371         572 :         AsPathSpec *as_path_ptr = as_path.Add(asn);
     372         572 :         attr->set_as_path(as_path_ptr);
     373         572 :         delete as_path_ptr;
     374             :     } else {
     375         489 :         AsPathSpec as_path;
     376         488 :         AsPathSpec *as_path_ptr = as_path.Add(asn);
     377         492 :         attr->set_as_path(as_path_ptr);
     378         494 :         delete as_path_ptr;
     379         494 :     }
     380        1066 : }
     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       12436 : UpdateInfo *BgpTable::GetUpdateInfo(RibOut *ribout, BgpRoute *route,
     583             :         const RibPeerSet &peerset) {
     584       12436 :     const BgpPath *path = route->BestPath();
     585             : 
     586             :     // Ignore if there is no best-path.
     587       12430 :     if (!path)
     588           0 :         return NULL;
     589             : 
     590             :     // Don't advertise infeasible paths.
     591       12430 :     if (!path->IsFeasible())
     592        1636 :         return NULL;
     593             : 
     594             :     // Check whether the route is contributing route
     595       10790 :     if (IsRouteAggregationSupported() && IsContributingRoute(route))
     596           0 :         return NULL;
     597             : 
     598             :     // Needs to be outside the if block so it's not destroyed prematurely.
     599       10787 :     BgpAttrPtr attr_ptr;
     600       10787 :     const BgpAttr *attr = path->GetAttr();
     601             : 
     602       10780 :     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       10786 :     if (ribout->IsEncodingBgp()) {
     608             :         // Handle well-known communities.
     609        8536 :         if (attr->community() != NULL &&
     610         348 :             attr->community()->communities().size()) {
     611        1044 :             BOOST_FOREACH(uint32_t value, attr->community()->communities()) {
     612         348 :                 if (value == CommunityType::NoAdvertise)
     613           0 :                     return NULL;
     614             : 
     615         412 :                 if ((ribout->peer_type() == BgpProto::EBGP) &&
     616          64 :                     ((value == CommunityType::NoExport) ||
     617             :                      (value == CommunityType::NoExportSubconfed))) {
     618           0 :                     return NULL;
     619             :                 }
     620             :             }
     621             :         }
     622             : 
     623        8195 :         const IPeer *peer = path->GetPeer();
     624        8193 :         BgpAttr *clone = NULL;
     625        8539 :         bool llgr_stale_comm = attr->community() &&
     626         348 :             attr->community()->ContainsValue(CommunityType::LlgrStale);
     627        8187 :         if (ribout->peer_type() == BgpProto::IBGP) {
     628             :             // Split horizon check.
     629        6150 :             if (peer && peer->CheckSplitHorizon(server()->cluster_id(),
     630             :                         ribout->cluster_id()))
     631        2885 :                 return NULL;
     632             : 
     633             :             // Handle route-target filtering.
     634        3331 :             if (IsVpnTable() && attr->ext_community() != NULL) {
     635        1535 :                 server()->rtarget_group_mgr()->GetRibOutInterestedPeers(
     636             :                     ribout, attr->ext_community(), peerset, &new_peerset);
     637        1545 :                 if (new_peerset.empty())
     638           0 :                     return NULL;
     639             :             }
     640             : 
     641        3337 :             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        3336 :             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        3336 :             clone = new BgpAttr(*attr);
     680             : 
     681             :             // Retain LocalPref value if set, else set default to 100.
     682        3337 :             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        3336 :             CheckAggregatorAttr(clone);
     687             : 
     688             :             // Should not normally be needed for iBGP, but there could be
     689             :             // complex configurations where this is useful.
     690        3336 :             ProcessRemovePrivate(ribout, clone);
     691             : 
     692             :             // Add Originator_Id if acting as route reflector and cluster_id
     693             :             // is not present
     694        3336 :             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        3336 :             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        3336 :             if (!ribout->as4_supported() && !clone->as_path()) {
     731        1229 :                 if (attr->aspath_4byte()) {
     732           0 :                     CreateAsPath2Byte(clone);
     733             :                 } else {
     734        1229 :                     AsPathSpec as_path;
     735        1229 :                     clone->set_as_path(&as_path);
     736        1230 :                 }
     737             :             }
     738        2034 :         } 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        3258 :             if (!rtinstance_->IsMasterRoutingInstance() &&
     743        1225 :                 ribout->nexthop().is_unspecified()) {
     744           0 :                 return NULL;
     745             :             }
     746             : 
     747             :             // Handle route-target filtering.
     748        2033 :             if (IsVpnTable() && attr->ext_community() != NULL) {
     749           0 :                 server()->rtarget_group_mgr()->GetRibOutInterestedPeers(
     750             :                     ribout, attr->ext_community(), peerset, &new_peerset);
     751           0 :                 if (new_peerset.empty())
     752           0 :                     return NULL;
     753             :             }
     754             : 
     755             :             // Sender side AS path loop check and split horizon within RibOut.
     756        2033 :             if (!ribout->as_override()) {
     757        2033 :                 if (attr->IsAsPathLoop(ribout->peer_as()))
     758         967 :                     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        1063 :             clone = new BgpAttr(*attr);
     768             : 
     769             :             // Remove non-transitive attributes.
     770             :             // Note that med is handled further down.
     771        1066 :             clone->set_originator_id(Ip4Address());
     772        1064 :             clone->set_cluster_list(NULL);
     773             : 
     774             :             // Update nexthop.
     775        1063 :             if (!ribout->nexthop().is_unspecified())
     776         776 :                 clone->set_nexthop(ribout->nexthop());
     777             : 
     778             :             // Reset LocalPref.
     779        1062 :             if (clone->local_pref())
     780        1062 :                 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        1063 :             if (clone->med() && !clone->IsAsPathEmpty())
     787           0 :                 clone->set_med(0);
     788             : 
     789             :             // Override the peer AS with local AS in AsPath.
     790        1062 :             ProcessAsOverride(ribout, clone);
     791             : 
     792             :             // Remove private processing must happen before local AS prepend.
     793        1061 :             ProcessRemovePrivate(ribout, clone);
     794             : 
     795             :             // Prepend the local AS to AsPath.
     796        1061 :             PrependLocalAs(ribout, clone, peer);
     797             :         }
     798             : 
     799        4404 :         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        4404 :         ProcessDefaultTunnelEncapsulation(ribout, server()->extcomm_db(),
     808             :                                           clone);
     809             : 
     810        4403 :         ProcessLlgrState(ribout, path, clone, llgr_stale_comm);
     811             : 
     812             :         // Locate the new BgpAttrPtr.
     813        4403 :         attr_ptr = clone->attr_db()->Locate(clone);
     814        4403 :         attr = attr_ptr.get();
     815             :     }
     816             : 
     817        6998 :     UpdateInfo *uinfo = new UpdateInfo;
     818        6996 :     uinfo->target = new_peerset;
     819        6998 :     uinfo->roattr = RibOutAttr(route, attr, ribout->IsEncodingXmpp());
     820        7000 :     return uinfo;
     821       10852 : }
     822             : 
     823             : // Bgp Path selection..
     824             : // Based Attribute weight
     825        5348 : bool BgpTable::PathSelection(const Path &path1, const Path &path2) {
     826        5348 :     const BgpPath &l_path = dynamic_cast<const BgpPath &> (path1);
     827        5348 :     const BgpPath &r_path = dynamic_cast<const BgpPath &> (path2);
     828             : 
     829             :     // Check the weight of Path
     830        5349 :     bool res = l_path.PathCompare(r_path, false) < 0;
     831             : 
     832        5348 :     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        8777 : 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        8777 :     bool notify_rt = false;
     846             : 
     847        8777 :     switch (oper) {
     848        6241 :     case DBRequest::DB_ENTRY_ADD_CHANGE: {
     849        6241 :         assert(rt);
     850             : 
     851             :         // The entry may currently be marked as deleted.
     852        6241 :         rt->ClearDelete();
     853        6239 :         if (peer)
     854        5952 :             peer->UpdateCloseRouteStats(family(), path, flags);
     855             : 
     856             :         // Check whether peer already has a path.
     857        6227 :         if (path != NULL) {
     858        6644 :             if ((path->GetAttr() != attrs.get()) ||
     859        3146 :                 (path->GetFlags() != flags) ||
     860        6833 :                 (path->GetLabel() != label) ||
     861         194 :                 (path->GetL3Label() != l3_label)) {
     862             :                 // Update Attributes and notify (if needed)
     863        3495 :                 if (path->NeedsResolution())
     864         768 :                     path_resolver_->StopPathResolution(root->index(), path);
     865        3494 :                 rt->DeletePath(path);
     866             :             } else {
     867             :                 // Ignore duplicate update.
     868         192 :                 break;
     869             :             }
     870             :         }
     871             : 
     872             :         BgpPath *new_path;
     873       12095 :         new_path = new BgpPath(
     874        6051 :             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        6052 :         bool fc_family = false;
     885        9316 :         if (family() == Address::INET || family() == Address::INET6 ||
     886        3266 :             family() == Address::EVPN) {
     887        5876 :             fc_family = true;
     888             :         }
     889        6043 :         bool fc_enabled = (server()->IsNextHopCheckEnabled() &&
     890        6027 :                        !rtinstance_->IsMasterRoutingInstance() && fc_family);
     891        6027 :         if (new_path->NeedsResolution() || fc_enabled) {
     892        1344 :             if (peer && !(peer->IsXmppPeer()) ) {
     893        1344 :                 rt->add_peer_sources(peer->ToString());
     894             :             }
     895        1344 :             Address::Family family = new_path->GetAttr()->nexthop_family();
     896             :             BgpTable *table;
     897        1344 :             if (new_path->NeedsResolution()) {
     898        1344 :                 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        1342 :             path_resolver_->StartPathResolution(rt, new_path, table);
     912             :         }
     913             : 
     914        6023 :         rt->InsertPath(new_path);
     915        6056 :         notify_rt = true;
     916        6056 :         break;
     917             :     }
     918             : 
     919        2536 :     case DBRequest::DB_ENTRY_DELETE: {
     920        2536 :         if (rt && !rt->IsDeleted()) {
     921        2536 :             if (peer && !(peer->IsXmppPeer()) ) {
     922        1860 :                 rt->del_peer_sources(peer->ToString());
     923             :             }
     924        2924 :             BGP_LOG_ROUTE(this, const_cast<IPeer *>(peer), rt,
     925             :                           "Delete BGP path");
     926             : 
     927             :             // Remove the Path from the route
     928        2536 :             if (path->NeedsResolution())
     929         576 :                 path_resolver_->StopPathResolution(root->index(), path);
     930        2536 :             rt->DeletePath(path);
     931        2536 :             notify_rt = true;
     932             :         }
     933        2536 :         break;
     934             :     }
     935             : 
     936           0 :     default: {
     937           0 :         assert(false);
     938             :         break;
     939             :     }
     940             :     }
     941        8784 :     return notify_rt;
     942             : }
     943             : 
     944        7172 : void BgpTable::Input(DBTablePartition *root, DBClient *client,
     945             :                      DBRequest *req) {
     946        7172 :     const IPeer *peer = (static_cast<RequestKey *>(req->key.get()))->GetPeer();
     947        7171 :     RequestData *data = static_cast<RequestData *>(req->data.get());
     948             : 
     949        7170 :     if (req->oper == DBRequest::DB_ENTRY_ADD_CHANGE && peer) {
     950             :         // Skip if this peer is down.
     951        4356 :         if (!peer->IsReady())
     952          68 :             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        4290 :         if (peer->IsXmppPeer() && peer->IsRegistrationRequired()) {
     957         388 :             BgpMembershipManager *mgr = rtinstance_->server()->membership_mgr();
     958         388 :             int instance_id = -1;
     959         388 :             uint64_t subscription_gen_id = 0;
     960             :             bool is_registered =
     961         388 :                 mgr->GetRegistrationInfo(const_cast<IPeer *>(peer), this,
     962             :                                          &instance_id, &subscription_gen_id);
     963         708 :             if ((!is_registered && (family() != Address::RTARGET)) ||
     964         320 :                 (is_registered &&
     965         320 :                  (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        7104 :     BgpRoute *rt = TableFind(root, req->key.get());
     973        7103 :     if (!rt) {
     974        2108 :         if ((req->oper == DBRequest::DB_ENTRY_DELETE) ||
     975        2108 :             (req->oper == DBRequest::DB_ENTRY_NOTIFY))
     976           0 :             return;
     977             : 
     978        2108 :         rt = static_cast<BgpRoute *>(Add(req));
     979        2108 :         static_cast<DBTablePartition *>(root)->Add(rt);
     980        2472 :         BGP_LOG_ROUTE(this, const_cast<IPeer *>(peer), rt,
     981             :                       "Insert new BGP path");
     982             :     }
     983             : 
     984        7103 :     if (req->oper == DBRequest::DB_ENTRY_NOTIFY) {
     985           0 :         root->Notify(rt);
     986           0 :         return;
     987             :     }
     988             : 
     989        7103 :     uint32_t path_id = 0;
     990        7103 :     uint32_t flags = 0;
     991        7103 :     uint32_t label = 0;
     992        7103 :     uint32_t l3_label = 0;
     993        7103 :     BgpPath *path = rt->FindPath(peer);
     994        7104 :     BgpAttrPtr attr = data ? data->attrs() : NULL;
     995        7104 :     if (req->oper == DBRequest::DB_ENTRY_ADD_CHANGE) {
     996        4578 :         assert(data);
     997        4578 :         const RequestData::NextHop &nexthop = data->nexthop();
     998        4578 :         if (nexthop.address_.is_v4())
     999        4306 :             path_id = nexthop.address_.to_v4().to_ulong();
    1000        4578 :         flags = nexthop.flags_;
    1001        4578 :         label = nexthop.label_;
    1002        4578 :         l3_label = nexthop.l3_label_;
    1003             : 
    1004        4578 :         attr = GetAttributes(rt, attr, peer);
    1005             :     } else {
    1006        2526 :         if (!path)
    1007           0 :             return;
    1008        2526 :         path_id = path->GetPathId();
    1009             :     }
    1010             : 
    1011        7104 :     bool notify_rt = InputCommon(root, rt, path, peer, req, req->oper,
    1012             :         attr, path_id, flags, label, l3_label);
    1013        7104 :     InputCommonPostProcess(root, rt, notify_rt);
    1014        7103 : }
    1015             : 
    1016       12474 : void BgpTable::InputCommonPostProcess(DBTablePartBase *root,
    1017             :                                       BgpRoute *rt, bool notify_rt) {
    1018       12474 :     if (!notify_rt)
    1019        3880 :         return;
    1020             : 
    1021        8594 :     if (rt->front() == NULL)
    1022        1927 :         root->Delete(rt);
    1023             :     else
    1024        6666 :         root->Notify(rt);
    1025             : }
    1026             : 
    1027         782 : bool BgpTable::MayDelete() const {
    1028         782 :     CHECK_CONCURRENCY("bgp::Config");
    1029             : 
    1030             :     // Bail if the table has listeners.
    1031         782 :     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         782 :     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         782 :     size_t size = Size();
    1046         782 :     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         782 :     if (!DBTableBase::MayDelete())
    1056           0 :         return false;
    1057             : 
    1058         782 :     return true;
    1059             : }
    1060             : 
    1061         782 : void BgpTable::Shutdown() {
    1062         782 :     CHECK_CONCURRENCY("bgp::PeerMembership", "bgp::Config");
    1063         782 : }
    1064             : 
    1065         782 : void BgpTable::ManagedDelete() {
    1066         782 :     BGP_LOG_TABLE(this, SandeshLevel::SYS_INFO, BGP_LOG_FLAG_ALL,
    1067             :                   "Received request for table deletion");
    1068         782 :     deleter_->Delete();
    1069         782 : }
    1070             : 
    1071             : //
    1072             : // Retry deletion of the table if it is pending.
    1073             : //
    1074        4834 : void BgpTable::RetryDelete() {
    1075        4834 :     if (!deleter()->IsDeleted())
    1076        4039 :         return;
    1077         795 :     deleter()->RetryDelete();
    1078             : }
    1079             : 
    1080           0 : PathResolver *BgpTable::CreatePathResolver() {
    1081           0 :     return NULL;
    1082             : }
    1083             : 
    1084         204 : void BgpTable::LocatePathResolver() {
    1085         204 :     if (path_resolver_)
    1086          68 :         return;
    1087         136 :     assert(!deleter()->IsDeleted());
    1088         136 :     path_resolver_ = CreatePathResolver();
    1089             : }
    1090             : 
    1091         136 : void BgpTable::DestroyPathResolver() {
    1092         136 :     if (!path_resolver_)
    1093           0 :         return;
    1094         136 :     delete path_resolver_;
    1095         136 :     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        8619 : LifetimeActor *BgpTable::deleter() {
    1119        8619 :     return deleter_.get();
    1120             : }
    1121             : 
    1122        3979 : const LifetimeActor *BgpTable::deleter() const {
    1123        3979 :     return deleter_.get();
    1124             : }
    1125             : 
    1126       18272 : void BgpTable::UpdatePathCount(const BgpPath *path, int count) {
    1127       18272 :     if (dynamic_cast<const BgpSecondaryPath *>(path)) {
    1128        3327 :         secondary_path_count_ += count;
    1129             :     } else {
    1130       14945 :         primary_path_count_ += count;
    1131             :     }
    1132             : 
    1133       18298 :     if (!path->IsFeasible()) {
    1134        2688 :         infeasible_path_count_ += count;
    1135             :     }
    1136             : 
    1137       18294 :     if (path->IsStale()) {
    1138        5380 :         stale_path_count_ += count;
    1139             :     }
    1140             : 
    1141       18293 :     if (path->IsLlgrStale()) {
    1142        1920 :         llgr_stale_path_count_ += count;
    1143             :     }
    1144       18291 : }
    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        6974 : bool BgpTable::IsContributingRoute(const BgpRoute *route) const {
    1153        6974 :     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