OpenSDN source code
bgp_xmpp_sandesh.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 #include "bgp/bgp_xmpp_sandesh.h"
6 
7 #include <string>
8 #include <vector>
9 
10 #include "base/regex.h"
11 #include "bgp/bgp_membership.h"
12 #include "bgp/bgp_peer.h"
13 #include "bgp/bgp_peer_internal_types.h"
14 #include "bgp/bgp_sandesh.h"
15 #include "bgp/bgp_server.h"
16 #include "bgp/bgp_xmpp_channel.h"
17 #include "xmpp/xmpp_connection.h"
18 
19 using contrail::regex;
22 using std::string;
23 using std::vector;
24 
25 static bool ShowXmppNeighborMatch(const BgpXmppChannel *bx_channel,
26  const string &search_string, const regex &search_expr) {
27  if (regex_search(bx_channel->ToString(), search_expr))
28  return true;
29  string address_string = bx_channel->remote_endpoint().address().to_string();
30  if (regex_search(address_string, search_expr))
31  return true;
32  if (search_string == "deleted" && bx_channel->peer_deleted())
33  return true;
34  return false;
35 }
36 
37 static void FillXmppNeighborInfo(BgpNeighborResp *bnr,
38  const BgpSandeshContext *bsc, const BgpXmppChannel *bx_channel,
39  bool summary) {
40  bnr->set_peer(bx_channel->ToString());
41  bnr->set_peer_address(bx_channel->remote_endpoint().address().to_string());
42  bnr->set_transport_address(bx_channel->transport_address_string());
43  bnr->set_deleted(bx_channel->peer_deleted());
44  bnr->set_closed_at(UTCUsecToString(bx_channel->peer_closed_at()));
45  bnr->set_local_address(bx_channel->local_endpoint().address().to_string());
46  bnr->set_peer_type("internal");
47  bnr->set_encoding("XMPP");
48  bnr->set_state(bx_channel->StateName());
49 
50  const XmppConnection *connection = bx_channel->channel()->connection();
51  bnr->set_negotiated_hold_time(connection->GetNegotiatedHoldTime());
52  bnr->set_primary_path_count(bx_channel->Peer()->GetPrimaryPathCount());
53  bnr->set_task_instance(connection->GetTaskInstance());
54  bnr->set_auth_type(connection->GetXmppAuthenticationType());
55  bnr->set_send_ready(bx_channel->Peer()->send_ready());
56  bnr->set_flap_count(bx_channel->Peer()->peer_stats()->num_flaps());
57  bnr->set_flap_time(bx_channel->Peer()->peer_stats()->last_flap());
58 
59  const XmppSession *sess = bx_channel->GetSession();
60  if (sess) {
61  short int dscp = sess->GetDscpValue();
62  bnr->set_dscp_value(dscp);
63  }
64  if (summary)
65  return;
66 
67  bnr->set_configured_hold_time(connection->GetConfiguredHoldTime());
68  bnr->set_configured_address_families(vector<string>());
69  bnr->set_negotiated_address_families(vector<string>());
70  const BgpMembershipManager *mgr = bsc->bgp_server->membership_mgr();
71  mgr->FillPeerMembershipInfo(bx_channel->Peer(), bnr);
72  bx_channel->FillTableMembershipInfo(bnr);
73  bx_channel->FillInstanceMembershipInfo(bnr);
74  bx_channel->FillCloseInfo(bnr);
75 
76  BgpPeer::FillBgpNeighborDebugState(bnr, bx_channel->Peer()->peer_stats());
77 }
78 
79 static bool ShowXmppNeighbor(const BgpSandeshContext *bsc, bool summary,
80  uint32_t page_limit, uint32_t iter_limit, const string &start_neighbor,
81  const string &search_string, vector<BgpNeighborResp> *list,
82  string *next_neighbor) {
83  regex search_expr(search_string);
84  const BgpXmppChannelManager *bxcm = bsc->xmpp_peer_manager;
86  bxcm->name_clower_bound(start_neighbor);
87  for (uint32_t iter_count = 0; it != bxcm->name_cend(); ++it, ++iter_count) {
88  const BgpXmppChannel *bx_channel = it->second;
89  if (!ShowXmppNeighborMatch(bx_channel, search_string, search_expr))
90  continue;
91  BgpNeighborResp bnr;
92  FillXmppNeighborInfo(&bnr, bsc, bx_channel, summary);
93  list->push_back(bnr);
94  if (list->size() >= page_limit)
95  break;
96  if (iter_count >= iter_limit)
97  break;
98  }
99 
100  // All done if we've looked at all channels.
101  if (it == bxcm->name_cend() || ++it == bxcm->name_cend())
102  return true;
103 
104  // Return true if we've reached the page limit, false if we've reached the
105  // iteration limit.
106  bool done = list->size() >= page_limit;
107  *next_neighbor = it->second->ToString();
108  return done;
109 }
110 
112  size_t *count, const BgpServer *bgp_server, string domain,
113  string up_or_down, const BgpXmppChannel *channel) {
114 
115  if (boost::iequals(up_or_down, "up") && !channel->Peer()->IsReady()) {
116  return;
117  }
118  if (boost::iequals(up_or_down, "down") && channel->Peer()->IsReady()) {
119  return;
120  }
121 
122  if (!domain.empty()) {
123  const RoutingInstanceMgr *rim = bgp_server->routing_instance_mgr();
124  const RoutingInstance *ri = rim->GetRoutingInstance(domain);
125  if (!ri)
126  return;
127  const BgpTable *table = ri->GetTable(Address::INET);
128  if (!table)
129  return;
130 
131  const BgpMembershipManager *mgr = bgp_server->membership_mgr();
132  if (!mgr->GetRegistrationInfo(channel->Peer(), table)) {
133  return;
134  }
135  }
136 
137  ++*count;
138 }
139 
141  size_t *count, const BgpSandeshContext *bsc,
142  const ShowNeighborStatisticsReq *req) {
144  boost::bind(ShowXmppNeighborStatisticsVisitor, count,
145  bsc->bgp_server, req->get_domain(),
146  req->get_up_or_down(), _1));
147 }
148 
153 }
static bool ShowXmppNeighborMatch(const BgpXmppChannel *bx_channel, const string &search_string, const regex &search_expr)
void RegisterSandeshShowXmppExtensions(BgpSandeshContext *bsc)
static void FillXmppNeighborInfo(BgpNeighborResp *bnr, const BgpSandeshContext *bsc, const BgpXmppChannel *bx_channel, bool summary)
static bool ShowXmppNeighbor(const BgpSandeshContext *bsc, bool summary, uint32_t page_limit, uint32_t iter_limit, const string &start_neighbor, const string &search_string, vector< BgpNeighborResp > *list, string *next_neighbor)
static void ShowXmppNeighborStatisticsVisitor(size_t *count, const BgpServer *bgp_server, string domain, string up_or_down, const BgpXmppChannel *channel)
static void ShowXmppNeighborStatistics(size_t *count, const BgpSandeshContext *bsc, const ShowNeighborStatisticsReq *req)
@ INET
Definition: address.h:27
bool GetRegistrationInfo(const IPeer *peer, const BgpTable *table, int *instance_id=NULL, uint64_t *subscription_gen_id=NULL) const
void FillPeerMembershipInfo(const IPeer *peer, BgpNeighborResp *resp) const
static void FillBgpNeighborDebugState(BgpNeighborResp *bnr, const IPeerDebugStats *peer)
Definition: bgp_peer.cc:2534
RoutingInstanceMgr * routing_instance_mgr()
Definition: bgp_server.h:107
BgpMembershipManager * membership_mgr()
Definition: bgp_server.h:180
const_name_iterator name_clower_bound(const std::string &name) const
XmppChannelNameMap::const_iterator const_name_iterator
void VisitChannels(BgpXmppChannelManager::VisitorFn)
const_name_iterator name_cend() const
void FillCloseInfo(BgpNeighborResp *resp) const
TcpSession::Endpoint local_endpoint() const
std::string transport_address_string() const
const XmppSession * GetSession() const
uint64_t peer_closed_at() const
const std::string & ToString() const
TcpSession::Endpoint remote_endpoint() const
void FillTableMembershipInfo(BgpNeighborResp *resp) const
void FillInstanceMembershipInfo(BgpNeighborResp *resp) const
bool peer_deleted() const
const XmppChannel * channel() const
std::string StateName() const
virtual uint64_t num_flaps() const =0
virtual std::string last_flap() const =0
virtual bool send_ready() const
Definition: ipeer.h:24
virtual bool IsReady() const =0
virtual int GetPrimaryPathCount() const =0
virtual IPeerDebugStats * peer_stats()=0
RoutingInstance * GetRoutingInstance(const std::string &name)
BgpTable * GetTable(Address::Family fmly)
uint8_t GetDscpValue() const
Definition: tcp_session.cc:575
virtual const XmppConnection * connection() const =0
int GetTaskInstance() const
std::string GetXmppAuthenticationType() const
int GetConfiguredHoldTime() const
int GetNegotiatedHoldTime() const
static bool regex_search(const std::string &input, const regex &regex)
Definition: regex.h:26
static bool regex_match(const std::string &input, const regex &regex)
Definition: regex.h:35
void SetNeighborShowExtensions(const NeighborListExtension &show_neighbor, const NeighborStatisticsExtension &show_neighbor_statistics)
Definition: bgp_sandesh.cc:664
BgpXmppChannelManager * xmpp_peer_manager
Definition: bgp_sandesh.h:48
BgpServer * bgp_server
Definition: bgp_sandesh.h:47
static std::string UTCUsecToString(uint64_t tstamp)
Definition: time_util.h:55