LCOV - code coverage report
Current view: top level - bgp - bgp_sandesh.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 11 408 2.7 %
Date: 2026-11-16 02:06:09 Functions: 2 39 5.1 %
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_sandesh.h"
       7             : 
       8             : #include <boost/assign/list_of.hpp>
       9             : #include <boost/foreach.hpp>
      10             : #include <sandesh/request_pipeline.h>
      11             : 
      12             : #include "bgp/bgp_multicast.h"
      13             : #include "bgp/bgp_mvpn.h"
      14             : #include "bgp/bgp_peer.h"
      15             : #include "bgp/bgp_peer_internal_types.h"
      16             : #include "bgp/bgp_server.h"
      17             : #include "bgp/bgp_session_manager.h"
      18             : #include "bgp/ermvpn/ermvpn_table.h"
      19             : #include "bgp/inet/inet_table.h"
      20             : #include "bgp/mvpn/mvpn_table.h"
      21             : #include "bgp/routing-instance/peer_manager.h"
      22             : #include "bgp/routing-instance/routing_instance.h"
      23             : 
      24             : using boost::assign::list_of;
      25             : using std::string;
      26             : using std::vector;
      27             : 
      28             : class ShowNeighborStatisticsHandler {
      29             : public:
      30             :     static bool CallbackS1(const Sandesh *sr,
      31             :         const RequestPipeline::PipeSpec ps, int stage, int instNum,
      32             :         RequestPipeline::InstData * data);
      33             :     static size_t FillBgpNeighborStatistics(
      34             :         const ShowNeighborStatisticsReq *req, BgpServer *bgp_server);
      35             : };
      36             : 
      37           0 : size_t ShowNeighborStatisticsHandler::FillBgpNeighborStatistics(
      38             :     const ShowNeighborStatisticsReq *req, BgpServer *bgp_server) {
      39           0 :     size_t count = 0;
      40             : 
      41           0 :     if (req->get_bgp_or_xmpp().empty() ||
      42           0 :         boost::iequals(req->get_bgp_or_xmpp(), "bgp")) {
      43           0 :         RoutingInstanceMgr *rim = bgp_server->routing_instance_mgr();
      44           0 :         if (!req->get_domain().empty()) {
      45           0 :             RoutingInstance *ri = rim->GetRoutingInstance(req->get_domain());
      46           0 :             PeerManager *pmgr = ri ? ri->peer_manager() : NULL;
      47           0 :             if (pmgr) {
      48           0 :                 count += pmgr->GetNeighborCount(req->get_up_or_down());
      49             :             }
      50             :         } else {
      51           0 :             RoutingInstanceMgr::RoutingInstanceIterator it = rim->begin();
      52           0 :             for (; it != rim->end(); it++) {
      53           0 :                 PeerManager *pmgr = it->second->peer_manager();
      54           0 :                 if (pmgr) {
      55           0 :                     count += pmgr->GetNeighborCount(req->get_up_or_down());
      56             :                 }
      57             :             }
      58             :         }
      59             :     }
      60             : 
      61           0 :     return count;
      62             : }
      63             : 
      64           0 : bool ShowNeighborStatisticsHandler::CallbackS1(
      65             :         const Sandesh *sr, const RequestPipeline::PipeSpec ps,
      66             :         int stage, int instNum, RequestPipeline::InstData *data) {
      67             :     const ShowNeighborStatisticsReq *req;
      68             :     BgpSandeshContext *bsc;
      69             : 
      70           0 :     req = static_cast<const ShowNeighborStatisticsReq *>(ps.snhRequest_.get());
      71           0 :     bsc = static_cast<BgpSandeshContext *>(req->client_context());
      72             : 
      73             :     // Retrieve number of BGP peers.
      74           0 :     size_t count = FillBgpNeighborStatistics(req, bsc->bgp_server);
      75             : 
      76             :     // Retrieve numner of XMPP agents.
      77           0 :     if (req->get_bgp_or_xmpp().empty() ||
      78           0 :             boost::iequals(req->get_bgp_or_xmpp(), "xmpp")) {
      79           0 :         bsc->ShowNeighborStatisticsExtension(&count, req);
      80             :     }
      81             : 
      82           0 :     ShowNeighborStatisticsResp *resp = new ShowNeighborStatisticsResp;
      83           0 :     resp->set_bgp_or_xmpp(req->get_bgp_or_xmpp());
      84           0 :     resp->set_up_or_down(req->get_up_or_down());
      85           0 :     resp->set_domain(req->get_domain());
      86           0 :     resp->set_count(count);
      87           0 :     resp->set_context(req->context());
      88           0 :     resp->Response();
      89             : 
      90           0 :     return true;
      91             : }
      92             : 
      93           0 : void ShowNeighborStatisticsReq::HandleRequest() const {
      94             :     // Use config task as we need to examine both bgp and xmpp data bases
      95             :     // to compute the number of neighbors. Both BGP and XMPP peers are
      96             :     // inserted/deleted under config task.
      97           0 :     RequestPipeline::StageSpec s1;
      98           0 :     s1.taskId_ = TaskScheduler::GetInstance()->GetTaskId("bgp::Config");
      99           0 :     s1.instances_.push_back(0);
     100           0 :     s1.cbFn_ = ShowNeighborStatisticsHandler::CallbackS1;
     101             : 
     102           0 :     RequestPipeline::PipeSpec ps(this);
     103           0 :     ps.stages_= list_of(s1)
     104           0 :         .convert_to_container<vector<RequestPipeline::StageSpec> >();
     105           0 :     RequestPipeline rp(ps);
     106           0 : }
     107             : 
     108             : class ClearBgpNeighborHandler {
     109             : public:
     110             :     static bool CallbackS1(const Sandesh *sr,
     111             :                            const RequestPipeline::PipeSpec ps, int stage,
     112             :                            int instNum, RequestPipeline::InstData * data);
     113             : };
     114             : 
     115           0 : bool ClearBgpNeighborHandler::CallbackS1(
     116             :         const Sandesh *sr, const RequestPipeline::PipeSpec ps,
     117             :         int stage, int instNum, RequestPipeline::InstData *data) {
     118             :     const ClearBgpNeighborReq *req;
     119             :     BgpSandeshContext *bsc;
     120             : 
     121           0 :     req = static_cast<const ClearBgpNeighborReq *>(ps.snhRequest_.get());
     122           0 :     bsc = static_cast<BgpSandeshContext *>(req->client_context());
     123           0 :     BgpPeer *peer = bsc->bgp_server->FindPeer(req->get_name());
     124             : 
     125           0 :     ClearBgpNeighborResp *resp = new ClearBgpNeighborResp;
     126           0 :     if (!bsc->test_mode()) {
     127           0 :         resp->set_success(false);
     128           0 :     } else if (peer) {
     129           0 :         peer->Clear(BgpProto::Notification::AdminReset);
     130           0 :         resp->set_success(true);
     131             :     } else {
     132           0 :         resp->set_success(false);
     133             :     }
     134           0 :     resp->set_context(req->context());
     135           0 :     resp->Response();
     136             : 
     137           0 :     return true;
     138             : }
     139             : 
     140             : // handler for 'clear bgp neighbor'
     141           0 : void ClearBgpNeighborReq::HandleRequest() const {
     142             :     // Use config task since neighbors are added/deleted under that task.
     143             :     // to compute the number of neighbors.
     144           0 :     RequestPipeline::StageSpec s1;
     145           0 :     s1.taskId_ = TaskScheduler::GetInstance()->GetTaskId("bgp::Config");
     146           0 :     s1.instances_.push_back(0);
     147           0 :     s1.cbFn_ = ClearBgpNeighborHandler::CallbackS1;
     148             : 
     149           0 :     RequestPipeline::PipeSpec ps(this);
     150           0 :     ps.stages_= list_of(s1)
     151           0 :         .convert_to_container<vector<RequestPipeline::StageSpec> >();
     152           0 :     RequestPipeline rp(ps);
     153           0 : }
     154             : 
     155             : class ShowMulticastManagerDetailHandler {
     156             : public:
     157             :     struct MulticastManagerDetailData : public RequestPipeline::InstData {
     158             :         vector<ShowMulticastTree> tree_list;
     159             :     };
     160             : 
     161           0 :     static RequestPipeline::InstData *CreateData(int stage) {
     162           0 :         return (new MulticastManagerDetailData);
     163             :     }
     164             : 
     165           0 :     static void FillMulticastLinkInfo(const McastForwarder *forwarder,
     166             :         ShowMulticastTreeLink *smtl) {
     167           0 :         smtl->set_address(forwarder->address().to_string());
     168           0 :         smtl->set_label(forwarder->label());
     169           0 :     }
     170             : 
     171           0 :     static void FillMulticastForwarderInfo(const McastForwarder *forwarder,
     172             :         ShowMulticastForwarder *smf) {
     173           0 :         smf->set_address(forwarder->address().to_string());
     174           0 :         smf->set_label(forwarder->label());
     175           0 :         smf->set_label_block(forwarder->label_block()->ToString());
     176           0 :         smf->set_router_id(forwarder->router_id().to_string());
     177           0 :         for (McastForwarderList::const_iterator it =
     178           0 :              forwarder->tree_links_.begin();
     179           0 :              it != forwarder->tree_links_.end(); ++it) {
     180           0 :             ShowMulticastTreeLink smtl;
     181           0 :             FillMulticastLinkInfo(*it, &smtl);
     182           0 :             smf->links.push_back(smtl);
     183           0 :         }
     184           0 :     }
     185             : 
     186           0 :     static void FillMulticastTreeInfo(const McastSGEntry *sg,
     187             :         ShowMulticastTree *smt) {
     188           0 :         smt->set_group(sg->group().to_string());
     189           0 :         smt->set_source(sg->source().to_string());
     190           0 :         for (uint8_t level = McastTreeManager::LevelFirst;
     191           0 :              level < McastTreeManager::LevelCount; ++level) {
     192           0 :             if (!sg->IsTreeBuilder(level))
     193           0 :                 continue;
     194           0 :             for (McastSGEntry::ForwarderSet::const_iterator it =
     195           0 :                  sg->forwarder_sets_[level]->begin();
     196           0 :                  it != sg->forwarder_sets_[level]->end(); ++it) {
     197           0 :                 ShowMulticastForwarder smf;
     198           0 :                 FillMulticastForwarderInfo(*it, &smf);
     199           0 :                 if (level == McastTreeManager::LevelNative) {
     200           0 :                     smt->level0_forwarders.push_back(smf);
     201             :                 } else {
     202           0 :                     smt->level1_forwarders.push_back(smf);
     203             :                 }
     204           0 :             }
     205             :         }
     206           0 :     }
     207             : 
     208           0 :     static void FillMulticastPartitionInfo(MulticastManagerDetailData *data,
     209             :             ErmVpnTable *table, int inst_id) {
     210           0 :         McastTreeManager *tm = table->GetTreeManager();
     211           0 :         McastManagerPartition *partition = tm->GetPartition(inst_id);
     212           0 :         for (McastManagerPartition::SGList::const_iterator it =
     213           0 :              partition->sg_list_.begin();
     214           0 :              it != partition->sg_list_.end(); it++) {
     215           0 :             ShowMulticastTree smt;
     216           0 :             FillMulticastTreeInfo(*it, &smt);
     217           0 :             data->tree_list.push_back(smt);
     218           0 :         }
     219           0 :     }
     220             : 
     221           0 :     static bool CallbackS1(const Sandesh *sr,
     222             :             const RequestPipeline::PipeSpec ps,
     223             :             int stage, int instNum,
     224             :             RequestPipeline::InstData *data) {
     225           0 :         int inst_id = ps.stages_[stage].instances_[instNum];
     226             : 
     227           0 :         MulticastManagerDetailData *mydata =
     228             :             static_cast<MulticastManagerDetailData *>(data);
     229             :         const ShowMulticastManagerDetailReq *req =
     230             :             static_cast<const ShowMulticastManagerDetailReq *>(
     231           0 :                 ps.snhRequest_.get());
     232             :         BgpSandeshContext *bsc =
     233           0 :             static_cast<BgpSandeshContext *>(req->client_context());
     234             :         DBTableBase *table =
     235           0 :             bsc->bgp_server->database()->FindTable(req->get_name());
     236           0 :         ErmVpnTable *mcast_table = dynamic_cast<ErmVpnTable *>(table);
     237           0 :         if (mcast_table && !mcast_table->IsVpnTable())
     238           0 :             FillMulticastPartitionInfo(mydata, mcast_table, inst_id);
     239             : 
     240           0 :         return true;
     241             :     }
     242             : 
     243           0 :     static void CombineMulticastPartitionInfo(
     244             :             const RequestPipeline::StageData *sd,
     245             :             vector<ShowMulticastTree> *tree_list) {
     246           0 :         for (size_t idx = 0; idx < sd->size(); idx++) {
     247             :             const MulticastManagerDetailData &data =
     248           0 :                 static_cast<const MulticastManagerDetailData &>(sd->at(idx));
     249           0 :             tree_list->insert(tree_list->end(),
     250             :                              data.tree_list.begin(), data.tree_list.end());
     251             :         }
     252           0 :     }
     253             : 
     254           0 :     static bool CallbackS2(const Sandesh *sr,
     255             :             const RequestPipeline::PipeSpec ps,
     256             :             int stage, int instNum,
     257             :             RequestPipeline::InstData *data) {
     258             :         const ShowMulticastManagerReq *req =
     259           0 :             static_cast<const ShowMulticastManagerReq *>(ps.snhRequest_.get());
     260           0 :         const RequestPipeline::StageData *sd = ps.GetStageData(0);
     261           0 :         vector<ShowMulticastTree> tree_list;
     262           0 :         CombineMulticastPartitionInfo(sd, &tree_list);
     263             : 
     264             :         ShowMulticastManagerDetailResp *resp =
     265           0 :             new ShowMulticastManagerDetailResp;
     266           0 :         resp->set_trees(tree_list);
     267           0 :         resp->set_context(req->context());
     268           0 :         resp->Response();
     269           0 :         return true;
     270           0 :     }
     271             : };
     272             : 
     273             : 
     274           0 : void ShowMulticastManagerDetailReq::HandleRequest() const {
     275           0 :     RequestPipeline::PipeSpec ps(this);
     276             : 
     277             :     // Request pipeline has 2 stages.
     278             :     // First stage to collect multicast manager stats.
     279             :     // Second stage to fill stats from stage 1 and respond to the request.
     280           0 :     RequestPipeline::StageSpec s1, s2;
     281           0 :     TaskScheduler *scheduler = TaskScheduler::GetInstance();
     282             : 
     283           0 :     s1.taskId_ = scheduler->GetTaskId("db::DBTable");
     284           0 :     s1.allocFn_ = ShowMulticastManagerDetailHandler::CreateData;
     285           0 :     s1.cbFn_ = ShowMulticastManagerDetailHandler::CallbackS1;
     286           0 :     for (int i = 0; i < ErmVpnTable::kPartitionCount; i++) {
     287           0 :         s1.instances_.push_back(i);
     288             :     }
     289             : 
     290           0 :     s2.taskId_ = scheduler->GetTaskId("bgp::ShowCommand");
     291           0 :     s2.cbFn_ = ShowMulticastManagerDetailHandler::CallbackS2;
     292           0 :     s2.instances_.push_back(0);
     293             : 
     294           0 :     ps.stages_ = list_of(s1)(s2)
     295           0 :         .convert_to_container<vector<RequestPipeline::StageSpec> >();
     296           0 :     RequestPipeline rp(ps);
     297           0 : }
     298             : 
     299             : class ShowMvpnManagerDetailHandler {
     300             : public:
     301             :     struct MvpnManagerDetailData : public RequestPipeline::InstData {
     302             :         vector<ShowMvpnNeighbor> neighbors;
     303             :     };
     304             : 
     305           0 :     static RequestPipeline::InstData *CreateData(int stage) {
     306           0 :         return (new MvpnManagerDetailData);
     307             :     }
     308             : 
     309           0 :     static void FillMvpnNeighborInfo(const MvpnNeighbor *nbr,
     310             :         ShowMvpnNeighbor *snbr) {
     311           0 :         snbr->set_rd(nbr->rd().ToString());
     312           0 :         snbr->set_originator(nbr->originator().to_string());
     313           0 :         snbr->set_source_as(nbr->source_as());
     314           0 :     }
     315             : 
     316           0 :     static void FillMvpnNeighborsInfo(MvpnManagerDetailData *data,
     317             :             MvpnTable *table, int inst_id) {
     318           0 :         MvpnManager *tm = table->manager();
     319           0 :         if (!tm)
     320           0 :             return;
     321           0 :         std::shared_lock<std::shared_mutex> lock(tm->neighbors_mutex());
     322             : 
     323           0 :         BOOST_FOREACH(const MvpnManager::NeighborMap::value_type &val,
     324             :                       tm->neighbors()) {
     325           0 :             ShowMvpnNeighbor snbr;
     326           0 :             FillMvpnNeighborInfo(&val.second, &snbr);
     327           0 :             data->neighbors.push_back(snbr);
     328           0 :         }
     329           0 :     }
     330             : 
     331           0 :     static bool CallbackS1(const Sandesh *sr,
     332             :             const RequestPipeline::PipeSpec ps,
     333             :             int stage, int instNum,
     334             :             RequestPipeline::InstData *data) {
     335           0 :         int inst_id = ps.stages_[stage].instances_[instNum];
     336             : 
     337           0 :         MvpnManagerDetailData *mydata =
     338             :             static_cast<MvpnManagerDetailData *>(data);
     339             :         const ShowMvpnManagerDetailReq *req =
     340             :             static_cast<const ShowMvpnManagerDetailReq *>(
     341           0 :                 ps.snhRequest_.get());
     342             :         BgpSandeshContext *bsc =
     343           0 :             static_cast<BgpSandeshContext *>(req->client_context());
     344             :         DBTableBase *table =
     345           0 :             bsc->bgp_server->database()->FindTable(req->get_name());
     346           0 :         MvpnTable *mvpn_table = dynamic_cast<MvpnTable *>(table);
     347           0 :         if (mvpn_table && !mvpn_table->IsVpnTable())
     348           0 :             FillMvpnNeighborsInfo(mydata, mvpn_table, inst_id);
     349             : 
     350           0 :         ShowMvpnManagerDetailResp *resp = new ShowMvpnManagerDetailResp;
     351           0 :         resp->set_neighbors(mydata->neighbors);
     352           0 :         resp->set_context(req->context());
     353           0 :         resp->Response();
     354             : 
     355           0 :         return true;
     356             :     }
     357             : };
     358             : 
     359           0 : void ShowMvpnManagerDetailReq::HandleRequest() const {
     360           0 :     RequestPipeline::PipeSpec ps(this);
     361           0 :     RequestPipeline::StageSpec s1, s2;
     362           0 :     TaskScheduler *scheduler = TaskScheduler::GetInstance();
     363             : 
     364           0 :     s1.taskId_ = scheduler->GetTaskId("db::DBTable");
     365           0 :     s1.allocFn_ = ShowMvpnManagerDetailHandler::CreateData;
     366           0 :     s1.cbFn_ = ShowMvpnManagerDetailHandler::CallbackS1;
     367           0 :     s1.instances_.push_back(0);
     368           0 :     ps.stages_ = list_of(s1)
     369           0 :         .convert_to_container<vector<RequestPipeline::StageSpec> >();
     370             : 
     371           0 :     RequestPipeline rp(ps);
     372           0 : }
     373             : 
     374             : class ShowMvpnProjectManagerDetailHandler {
     375             : public:
     376             :     struct MvpnProjectManagerDetailData : public RequestPipeline::InstData {
     377             :         vector<ShowMvpnState> states;
     378             :     };
     379             : 
     380           0 :     static RequestPipeline::InstData *CreateData(int stage) {
     381           0 :         return (new MvpnProjectManagerDetailData);
     382             :     }
     383             : 
     384           0 :     static void FillMvpnProjectStateInfo(const MvpnState *state,
     385             :                                          ShowMvpnState *st) {
     386           0 :         st->set_source(state->sg().source.to_string());
     387           0 :         st->set_group(state->sg().group.to_string());
     388           0 :         if (state->global_ermvpn_tree_rt()) {
     389           0 :             st->set_global_ermvpn_tree_rt(
     390           0 :                 state->global_ermvpn_tree_rt()->ToString());
     391             :         }
     392           0 :         if (state->spmsi_rt())
     393           0 :             st->set_spmsi_rt(state->spmsi_rt()->ToString());
     394           0 :         if (state->source_active_rt())
     395           0 :             st->set_source_active_rt(state->source_active_rt()->ToString());
     396             : 
     397           0 :         vector<string> spmsi_routes_received;
     398           0 :         BOOST_FOREACH(MvpnRoute *rt, state->spmsi_routes_received()) {
     399           0 :             spmsi_routes_received.push_back(rt->ToString());
     400             :         }
     401           0 :         st->set_spmsi_routes_received(spmsi_routes_received);
     402             : 
     403           0 :         vector<string> leafad_routes_attr_received;
     404           0 :         BOOST_FOREACH(const MvpnState::RoutesMap::value_type &val,
     405             :                       state->leafad_routes_attr_received()) {
     406           0 :             const PmsiTunnel *pmsi = val.second->pmsi_tunnel();
     407           0 :             if (pmsi) {
     408           0 :                 leafad_routes_attr_received.push_back(
     409           0 :                     pmsi->pmsi_tunnel().ToString());
     410             :             }
     411             :         }
     412           0 :         st->set_leafad_routes_attr_received(leafad_routes_attr_received);
     413           0 :         st->set_total_states(state->states()->size());
     414           0 :         st->set_project_manager(state->project_manager()->table()->name());
     415           0 :         st->set_refcount(state->refcount());
     416           0 :     }
     417             : 
     418           0 :     static void FillMvpnProjectPartitionInfo(MvpnProjectManagerDetailData *data,
     419             :             ErmVpnTable *table, int inst_id) {
     420           0 :         MvpnProjectManager *tm = table->mvpn_project_manager();
     421           0 :         if (!tm) return;
     422           0 :         MvpnProjectManagerPartition *partition = tm->GetPartition(inst_id);
     423           0 :         BOOST_FOREACH(MvpnState::StatesMap::value_type &val,
     424             :                       partition->states()) {
     425           0 :             ShowMvpnState st;
     426           0 :             FillMvpnProjectStateInfo(val.second, &st);
     427           0 :             data->states.push_back(st);
     428           0 :         }
     429             :     }
     430             : 
     431           0 :     static bool CallbackS1(const Sandesh *sr,
     432             :             const RequestPipeline::PipeSpec ps,
     433             :             int stage, int instNum,
     434             :             RequestPipeline::InstData *data) {
     435           0 :         int inst_id = ps.stages_[stage].instances_[instNum];
     436             : 
     437           0 :         MvpnProjectManagerDetailData *mydata =
     438             :             static_cast<MvpnProjectManagerDetailData *>(data);
     439             :         const ShowMvpnProjectManagerDetailReq *req =
     440             :             static_cast<const ShowMvpnProjectManagerDetailReq *>(
     441           0 :                 ps.snhRequest_.get());
     442             :         BgpSandeshContext *bsc =
     443           0 :             static_cast<BgpSandeshContext *>(req->client_context());
     444             :         DBTableBase *table =
     445           0 :             bsc->bgp_server->database()->FindTable(req->get_name());
     446           0 :         ErmVpnTable *ermvpn_table = dynamic_cast<ErmVpnTable *>(table);
     447           0 :         if (ermvpn_table && !ermvpn_table->IsVpnTable())
     448           0 :             FillMvpnProjectPartitionInfo(mydata, ermvpn_table, inst_id);
     449             : 
     450           0 :         return true;
     451             :     }
     452             : 
     453           0 :     static void CombineMvpnProjectPartitionInfo(
     454             :             const RequestPipeline::StageData *sd,
     455             :             vector<ShowMvpnState> *states) {
     456           0 :         for (size_t idx = 0; idx < sd->size(); idx++) {
     457             :             const MvpnProjectManagerDetailData &data =
     458           0 :                 static_cast<const MvpnProjectManagerDetailData &>(sd->at(idx));
     459           0 :             states->insert(states->end(),
     460             :                            data.states.begin(), data.states.end());
     461             :         }
     462           0 :     }
     463             : 
     464           0 :     static bool CallbackS2(const Sandesh *sr,
     465             :             const RequestPipeline::PipeSpec ps,
     466             :             int stage, int instNum,
     467             :             RequestPipeline::InstData *data) {
     468             :         const ShowMvpnProjectManagerReq *req =
     469             :             static_cast<const ShowMvpnProjectManagerReq *>(
     470           0 :                 ps.snhRequest_.get());
     471           0 :         const RequestPipeline::StageData *sd = ps.GetStageData(0);
     472           0 :         vector<ShowMvpnState> states;
     473           0 :         CombineMvpnProjectPartitionInfo(sd, &states);
     474             : 
     475             :         ShowMvpnProjectManagerDetailResp *resp =
     476           0 :             new ShowMvpnProjectManagerDetailResp;
     477           0 :         resp->set_states(states);
     478           0 :         resp->set_context(req->context());
     479           0 :         resp->Response();
     480           0 :         return true;
     481           0 :     }
     482             : };
     483             : 
     484             : 
     485           0 : void ShowMvpnProjectManagerDetailReq::HandleRequest() const {
     486           0 :     RequestPipeline::PipeSpec ps(this);
     487             : 
     488             :     // Request pipeline has 2 stages.
     489             :     // First stage to collect multicast manager stats.
     490             :     // Second stage to fill stats from stage 1 and respond to the request.
     491           0 :     RequestPipeline::StageSpec s1, s2;
     492           0 :     TaskScheduler *scheduler = TaskScheduler::GetInstance();
     493             : 
     494           0 :     s1.taskId_ = scheduler->GetTaskId("db::DBTable");
     495           0 :     s1.allocFn_ = ShowMvpnProjectManagerDetailHandler::CreateData;
     496           0 :     s1.cbFn_ = ShowMvpnProjectManagerDetailHandler::CallbackS1;
     497           0 :     for (int i = 0; i < ErmVpnTable::kPartitionCount; i++) {
     498           0 :         s1.instances_.push_back(i);
     499             :     }
     500             : 
     501           0 :     s2.taskId_ = scheduler->GetTaskId("bgp::ShowCommand");
     502           0 :     s2.cbFn_ = ShowMvpnProjectManagerDetailHandler::CallbackS2;
     503           0 :     s2.instances_.push_back(0);
     504             : 
     505           0 :     ps.stages_ = list_of(s1)(s2)
     506           0 :         .convert_to_container<vector<RequestPipeline::StageSpec> >();
     507           0 :     RequestPipeline rp(ps);
     508           0 : }
     509             : 
     510             : class ShowRouteVrfHandler {
     511             : public:
     512             :     struct SearchRouteInVrfData : public RequestPipeline::InstData {
     513             :         vector<ShowRoute> routes;
     514             :     };
     515             : 
     516           0 :     static RequestPipeline::InstData *CreateData(int stage) {
     517           0 :         return (new SearchRouteInVrfData);
     518             :     }
     519             : 
     520           0 :     static bool CallbackS1(const Sandesh *sr,
     521             :             const RequestPipeline::PipeSpec ps,
     522             :             int stage, int instNum,
     523             :             RequestPipeline::InstData *data) {
     524           0 :         SearchRouteInVrfData *sdata = static_cast<SearchRouteInVrfData *>(data);
     525             :         const ShowRouteVrfReq *req =
     526           0 :             static_cast<const ShowRouteVrfReq *>(ps.snhRequest_.get());
     527             :         BgpSandeshContext *bsc =
     528           0 :             static_cast<BgpSandeshContext *>(req->client_context());
     529           0 :         RoutingInstanceMgr *rim = bsc->bgp_server->routing_instance_mgr();
     530           0 :         RoutingInstance *ri = rim->GetRoutingInstance(req->get_vrf());
     531           0 :         if (!ri) return true;
     532           0 :         BgpTable *table = ri->GetTable(Address::INET);
     533           0 :         if (!table) return true;
     534           0 :         Ip4Prefix prefix(Ip4Prefix::FromString(req->get_prefix()));
     535           0 :         InetTable::RequestKey key(prefix, NULL);
     536             : 
     537             :         // Find route from the calling task instance specific db partition.
     538           0 :         InetRoute *route = static_cast<InetRoute *>(table->Find(&key, instNum));
     539           0 :         if (!route) return true;
     540           0 :         ShowRoute show_route;
     541           0 :         show_route.set_prefix(route->ToString());
     542           0 :         show_route.set_last_modified(duration_usecs_to_string(
     543           0 :                        UTCTimestampUsec() - route->last_change_at()));
     544           0 :         vector<ShowRoutePath> show_route_paths;
     545           0 :         for (Route::PathList::const_iterator it = route->GetPathList().begin();
     546           0 :              it != route->GetPathList().end(); it++) {
     547           0 :             const BgpPath *path = static_cast<const BgpPath *>(it.operator->());
     548           0 :             ShowRoutePath srp;
     549           0 :             srp.set_protocol("BGP");
     550           0 :             const BgpAttr *attr = path->GetAttr();
     551           0 :             if (attr->as_path() != NULL)
     552           0 :                 srp.set_as_path(attr->as_path()->path().ToString());
     553           0 :             srp.set_local_preference(attr->local_pref());
     554           0 :             if (path->GetPeer()) srp.set_source(path->GetPeer()->ToString());
     555           0 :             srp.set_last_modified(duration_usecs_to_string(
     556           0 :                     UTCTimestampUsec() - path->time_stamp_usecs()));
     557           0 :             srp.set_next_hop(attr->nexthop().to_string());
     558           0 :             srp.set_label(path->GetLabel());
     559           0 :             show_route_paths.push_back(srp);
     560           0 :         }
     561           0 :         show_route.set_paths(show_route_paths);
     562           0 :         sdata->routes.push_back(show_route);
     563           0 :         return true;
     564           0 :     }
     565             : 
     566           0 :     static bool CallbackS2(const Sandesh *sr,
     567             :             const RequestPipeline::PipeSpec ps,
     568             :             int stage, int instNum,
     569             :             RequestPipeline::InstData *data) {
     570           0 :         const RequestPipeline::StageData *sd = ps.GetStageData(0);
     571           0 :         ShowRoute route;
     572           0 :         for (size_t i = 0; i < sd->size(); i++) {
     573             :             const SearchRouteInVrfData &data =
     574           0 :                 static_cast<const SearchRouteInVrfData &>(sd->at(i));
     575           0 :             if (data.routes.size()) {
     576           0 :                 route = data.routes.front();
     577           0 :                 break;
     578             :             }
     579             :         }
     580             : 
     581           0 :         ShowRouteVrfResp *resp = new ShowRouteVrfResp;
     582           0 :         resp->set_route(route);
     583             :         const ShowRouteVrfReq *req =
     584           0 :             static_cast<const ShowRouteVrfReq *>(ps.snhRequest_.get());
     585           0 :         resp->set_context(req->context());
     586           0 :         resp->Response();
     587           0 :         return true;
     588           0 :     }
     589             : };
     590             : 
     591           0 : void ShowRouteVrfReq::HandleRequest() const {
     592           0 :     RequestPipeline::PipeSpec ps(this);
     593           0 :     BgpSandeshContext *bsc = static_cast<BgpSandeshContext *>(client_context());
     594             : 
     595             :     // Request pipeline has 2 stages.
     596             :     // First stage to search in different partition
     597             :     // Second stage to fill stats from stage 1 and respond to the request.
     598           0 :     RequestPipeline::StageSpec s1, s2;
     599           0 :     TaskScheduler *scheduler = TaskScheduler::GetInstance();
     600             : 
     601           0 :     s1.taskId_ = scheduler->GetTaskId("db::DBTable");
     602           0 :     s1.allocFn_ = ShowRouteVrfHandler::CreateData;
     603           0 :     s1.cbFn_ = ShowRouteVrfHandler::CallbackS1;
     604           0 :     for (int i = 0; i < bsc->bgp_server->database()->PartitionCount(); i++) {
     605           0 :         s1.instances_.push_back(i);
     606             :     }
     607             : 
     608           0 :     s2.taskId_ = scheduler->GetTaskId("bgp::ShowCommand");
     609           0 :     s2.cbFn_ = ShowRouteVrfHandler::CallbackS2;
     610           0 :     s2.instances_.push_back(0);
     611             : 
     612           0 :     ps.stages_ = list_of(s1)(s2)
     613           0 :         .convert_to_container<vector<RequestPipeline::StageSpec> >();
     614           0 :     RequestPipeline rp(ps);
     615           0 : }
     616             : 
     617             : class ShowBgpServerHandler {
     618             : public:
     619           0 :     static bool CallbackS1(const Sandesh *sr,
     620             :             const RequestPipeline::PipeSpec ps, int stage, int instNum,
     621             :             RequestPipeline::InstData *data) {
     622             :         const ShowBgpServerReq *req =
     623           0 :             static_cast<const ShowBgpServerReq *>(ps.snhRequest_.get());
     624             :         BgpSandeshContext *bsc =
     625           0 :             static_cast<BgpSandeshContext *>(req->client_context());
     626             : 
     627           0 :         ShowBgpServerResp *resp = new ShowBgpServerResp;
     628           0 :         SocketIOStats peer_socket_stats;
     629           0 :         bsc->bgp_server->session_manager()->GetRxSocketStats(&peer_socket_stats);
     630           0 :         resp->set_rx_socket_stats(peer_socket_stats);
     631             : 
     632           0 :         bsc->bgp_server->session_manager()->GetTxSocketStats(&peer_socket_stats);
     633           0 :         resp->set_tx_socket_stats(peer_socket_stats);
     634             : 
     635           0 :         resp->set_context(req->context());
     636           0 :         resp->Response();
     637           0 :         return true;
     638           0 :     }
     639             : };
     640             : 
     641           0 : void ShowBgpServerReq::HandleRequest() const {
     642           0 :     RequestPipeline::PipeSpec ps(this);
     643             : 
     644             :     // Request pipeline has single stage to collect neighbor config info
     645             :     // and respond to the request
     646           0 :     RequestPipeline::StageSpec s1;
     647           0 :     TaskScheduler *scheduler = TaskScheduler::GetInstance();
     648           0 :     s1.taskId_ = scheduler->GetTaskId("bgp::ShowCommand");
     649           0 :     s1.cbFn_ = ShowBgpServerHandler::CallbackS1;
     650           0 :     s1.instances_.push_back(0);
     651           0 :     ps.stages_ = list_of(s1)
     652           0 :        .convert_to_container<vector<RequestPipeline::StageSpec> >();
     653           0 :     RequestPipeline rp(ps);
     654           0 : }
     655             : 
     656           4 : BgpSandeshContext::BgpSandeshContext()
     657           4 :     : bgp_server(NULL),
     658           4 :       xmpp_peer_manager(NULL),
     659           4 :       test_mode_(false),
     660           4 :       page_limit_(0),
     661           4 :       iter_limit_(0) {
     662           4 : }
     663             : 
     664           4 : void BgpSandeshContext::SetNeighborShowExtensions(
     665             :     const NeighborListExtension &show_neighbor,
     666             :     const NeighborStatisticsExtension &show_neighbor_statistics) {
     667           4 :     show_neighbor_ext_ = show_neighbor;
     668           4 :     show_neighbor_statistics_ext_ = show_neighbor_statistics;
     669           4 : }
     670             : 
     671           0 : bool BgpSandeshContext::ShowNeighborExtension(const BgpSandeshContext *bsc,
     672             :     bool summary, uint32_t page_limit, uint32_t iter_limit,
     673             :     const string &start_neighbor, const string &search_string,
     674             :     vector<BgpNeighborResp> *list, string *next_neighbor) const {
     675           0 :     if (!show_neighbor_ext_)
     676           0 :         return true;
     677           0 :     bool done = show_neighbor_ext_(bsc, summary, page_limit, iter_limit,
     678             :         start_neighbor, search_string, list, next_neighbor);
     679           0 :     return done;
     680             : }
     681             : 
     682           0 : void BgpSandeshContext::ShowNeighborStatisticsExtension(
     683             :     size_t *count, const ShowNeighborStatisticsReq *req) const {
     684           0 :     if (!show_neighbor_statistics_ext_)
     685           0 :         return;
     686           0 :     show_neighbor_statistics_ext_(count, this, req);
     687             : }
     688             : 
     689           0 : void BgpSandeshContext::SetPeeringShowHandlers(
     690             :     const PeeringReqHandler &show_peering_req_handler,
     691             :     const PeeringReqIterateHandler &show_peering_req_iterate_handler) {
     692           0 :     show_peering_req_handler_ = show_peering_req_handler;
     693           0 :     show_peering_req_iterate_handler_ = show_peering_req_iterate_handler;
     694           0 : }
     695             : 
     696           0 : void BgpSandeshContext::PeeringShowReqHandler(
     697             :     const ShowBgpPeeringConfigReq *req) {
     698           0 :     if (show_peering_req_handler_) {
     699           0 :        show_peering_req_handler_(this, req);
     700             :     } else {
     701           0 :         ShowBgpPeeringConfigResp *resp = new ShowBgpPeeringConfigResp;
     702           0 :         resp->Response();
     703             :     }
     704           0 : }
     705             : 
     706           0 : void BgpSandeshContext::PeeringShowReqIterateHandler(
     707             :     const ShowBgpPeeringConfigReqIterate *req_iterate) {
     708           0 :     if (show_peering_req_iterate_handler_) {
     709           0 :        show_peering_req_iterate_handler_(this, req_iterate);
     710             :     } else {
     711           0 :         ShowBgpPeeringConfigResp *resp = new ShowBgpPeeringConfigResp;
     712           0 :         resp->Response();
     713             :     }
     714           0 : }

Generated by: LCOV version 1.14