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