OpenSDN source code
xmpp_server.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #include "xmpp/xmpp_server.h"
7 
8 #include <boost/foreach.hpp>
9 #include <boost/tuple/tuple.hpp>
10 
11 #include "base/task_annotations.h"
12 #include "xmpp/xmpp_connection.h"
13 #include "xmpp/xmpp_factory.h"
14 #include "xmpp/xmpp_lifetime.h"
15 #include "xmpp/xmpp_log.h"
16 #include "xmpp/xmpp_sandesh.h"
17 #include "xmpp/xmpp_session.h"
18 
19 #include "sandesh/request_pipeline.h"
20 #include "sandesh/common/vns_types.h"
21 #include "sandesh/common/vns_constants.h"
22 #include "sandesh/xmpp_server_types.h"
23 #include "sandesh/xmpp_trace_sandesh_types.h"
24 #include "sandesh/xmpp_client_server_sandesh_types.h"
25 
26 using namespace std;
27 using namespace boost::asio;
28 using boost::tie;
29 
30 #define DEFAULT_XMPP_HOLD_TIME 90
32 public:
34  : LifetimeActor(server->lifetime_manager()), server_(server) {
35  }
36  virtual bool MayDelete() const {
37  CHECK_CONCURRENCY("bgp::Config");
38  return server_->MayDelete();
39  }
40  virtual void Shutdown() {
41  CHECK_CONCURRENCY("bgp::Config");
42  server_->SessionShutdown();
43  }
44  virtual void Destroy() {
45  CHECK_CONCURRENCY("bgp::Config");
46  server_->Terminate();
47  }
48 
49 private:
51 };
52 
53 
54 XmppServer::XmppServer(EventManager *evm, const string &server_addr,
55  const XmppChannelConfig *config)
57  evm, ssl::context::sslv23_server, config->auth_enabled, true),
58  max_connections_(0),
59  lifetime_manager_(XmppStaticObjectFactory::Create<XmppLifetimeManager>(
60  TaskScheduler::GetInstance()->GetTaskId("bgp::Config"))),
61  deleter_(new DeleteActor(this)),
62  server_addr_(server_addr),
63  log_uve_(false),
64  auth_enabled_(config->auth_enabled),
65  tcp_hold_time_(config->tcp_hold_time),
66  gr_helper_disable_(config->gr_helper_disable),
67  dscp_value_(0),
68  connection_queue_(TaskScheduler::GetInstance()->GetTaskId("bgp::Config"),
69  0, boost::bind(&XmppServer::DequeueConnection, this, _1)) {
70 
71  if (config->auth_enabled) {
72 
73  // Get SSL context from base class and update
74  boost::asio::ssl::context *ctx = context();
75  boost::system::error_code ec;
76 
77  // set mode
78  ctx->set_options(ssl::context::default_workarounds |
79  ssl::context::no_sslv3 | ssl::context::no_sslv2, ec);
80  if (ec.value() != 0) {
81  LOG(ERROR, "Error : " << ec.message() << ", setting ssl options");
82  exit(EINVAL);
83  }
84 
85  // CA certificate, used to verify if the peer certificate
86  // is signed by a trusted CA
87  std::string ca_cert_filename = config->path_to_ca_cert;
88  if (!ca_cert_filename.empty()) {
89 
90  // Verify peer has CA signed certificate
91  ctx->set_verify_mode(boost::asio::ssl::verify_peer, ec);
92  if (ec.value() != 0) {
93  LOG(ERROR, "Error : " << ec.message()
94  << ", while setting ssl verification mode");
95  exit(EINVAL);
96  }
97 
98  ctx->load_verify_file(config->path_to_ca_cert, ec);
99  if (ec.value() != 0) {
100  LOG(ERROR, "Error : " << ec.message()
101  << ", while using cacert file : "
102  << config->path_to_ca_cert);
103  exit(EINVAL);
104  }
105  }
106 
107  // server certificate
108  ctx->use_certificate_file(config->path_to_server_cert,
109  boost::asio::ssl::context::pem, ec);
110  if (ec.value() != 0) {
111  LOG(ERROR, "Error : " << ec.message()
112  << ", while using server cert file : "
113  << config->path_to_server_cert);
114  exit(EINVAL);
115  }
116 
117  // server private key
118  ctx->use_private_key_file(config->path_to_server_priv_key,
119  boost::asio::ssl::context::pem, ec);
120  if (ec.value() != 0) {
121  LOG(ERROR, "Error : " << ec.message()
122  << ", while using privkey file : "
123  << config->path_to_server_priv_key);
124  exit(EINVAL);
125  }
126  }
127 }
128 
130 public:
131  explicit XmppConfigUpdater(XmppServer *server,
132  BgpConfigManager *config_manager) :
133  server_(server) {
136  this, _1, _2);
138  this, _1, _2);
139  config_manager->RegisterObservers(obs);
140  }
141 
142  const BgpGlobalSystemConfig &config() const { return config_; }
143 
144  void ProcessProtocolConfig(const BgpProtocolConfig *protocol_config,
146 
147  if (server_->subcluster_name() != protocol_config->subcluster_name()) {
148  server_->set_subcluster_name(protocol_config->subcluster_name());
149  server_->ClearAllConnections();
150  }
151  }
152 
155  // Clear peers only if GR is or was enabled.
156  bool clear_peers = config_.gr_enable() || system->gr_enable();
157  bool update_peers = false;
158  config_.set_gr_enable(system->gr_enable());
159  config_.set_gr_time(system->gr_time());
160  config_.set_llgr_time(system->llgr_time());
161  config_.set_end_of_rib_timeout(system->end_of_rib_timeout());
162  config_.set_gr_xmpp_helper(system->gr_xmpp_helper());
163 
164  // Process any change in xmpp-hold-time
165  if (config_.fc_enabled() != system->fc_enabled() ||
166  config_.xmpp_hold_time() != system->xmpp_hold_time()) {
167  config_.set_xmpp_hold_time(system->xmpp_hold_time());
168  config_.set_fc_enabled(system->fc_enabled());
169  // if fc_enabled is not set, use default hold time
170  if (!system->fc_enabled())
171  config_.set_xmpp_hold_time(DEFAULT_XMPP_HOLD_TIME);
172  update_peers = true;
173  }
174 
175  // Process any change in rd-cluster-seed knob
176  if (config_.rd_cluster_seed() != system->rd_cluster_seed()) {
177  config_.set_rd_cluster_seed(system->rd_cluster_seed());
178  clear_peers = true;
179  }
180 
181  if (clear_peers)
182  server_->ClearAllConnections();
183  else if (update_peers)
184  server_->UpdateAllConnections(config_.xmpp_hold_time());
185  }
186 
187  const string subcluster_name() const { return server_->subcluster_name(); }
188  void set_subcluster_name(const string& name) {
189  server_->set_subcluster_name(name);
190  }
191 
192  uint8_t xmpp_hold_time() const { return config_.xmpp_hold_time(); }
193  void set_xmpp_hold_time(int hold_time) {
194  config_.set_xmpp_hold_time(hold_time);
195  }
196 
197 private:
200 };
201 
202 XmppServer::XmppServer(EventManager *evm, const string &server_addr)
203  : XmppConnectionManager(evm, ssl::context::sslv23_server, false, false),
204  max_connections_(0),
206  TaskScheduler::GetInstance()->GetTaskId("bgp::Config"))),
207  deleter_(new DeleteActor(this)),
208  server_addr_(server_addr),
209  log_uve_(false),
210  auth_enabled_(false),
211  tcp_hold_time_(XmppChannelConfig::kTcpHoldTime),
212  gr_helper_disable_(false),
213  xmpp_config_updater_(NULL),
214  dscp_value_(0),
215  connection_queue_(TaskScheduler::GetInstance()->GetTaskId("bgp::Config"),
216  0, boost::bind(&XmppServer::DequeueConnection, this, _1)) {
217 }
218 
219 
221  : XmppConnectionManager(evm, ssl::context::sslv23_server, false, false),
222  max_connections_(0),
223  lifetime_manager_(new LifetimeManager(
224  TaskScheduler::GetInstance()->GetTaskId("bgp::Config"))),
225  deleter_(new DeleteActor(this)),
226  log_uve_(false),
227  auth_enabled_(false),
228  tcp_hold_time_(XmppChannelConfig::kTcpHoldTime),
229  gr_helper_disable_(false),
230  dscp_value_(0),
231  connection_queue_(TaskScheduler::GetInstance()->GetTaskId("bgp::Config"),
232  0, boost::bind(&XmppServer::DequeueConnection, this, _1)) {
233 }
234 
236  xmpp_config_updater_.reset(new XmppConfigUpdater(this, config_manager));
237 }
238 
240  // Check if GR is disabled..
241  if (!xmpp_config_updater_ || !xmpp_config_updater_->config().gr_enable())
242  return 0;
243  return xmpp_config_updater_->config().gr_time();
244 }
245 
247  // Check if GR is disabled..
248  if (!xmpp_config_updater_ || !xmpp_config_updater_->config().gr_enable())
249  return 0;
250  return xmpp_config_updater_->config().llgr_time();
251 }
252 
255  return xmpp_config_updater_->config().end_of_rib_timeout();
257 }
258 
261  xmpp_config_updater_->config().end_of_rib_timeout();
263 }
264 
266  // Check if disabled in .conf file.
267  if (gr_helper_disable_)
268  return false;
269 
270  // Check from configuration.
272  return false;
273 
274  // Check if GR is disabled..
275  if (!xmpp_config_updater_->config().gr_enable())
276  return false;
277 
278  return xmpp_config_updater_->config().gr_xmpp_helper();
279 }
280 
282 
283  // If the server is deleted, do not do graceful restart
284  if (deleter()->IsDeleted())
285  return false;
286 
287  // Check if GR helper mode is disabled.
288  if (!IsGRHelperModeEnabled())
289  return false;
290 
291  // Enable GR if either gr-time or llgr-time is configured.
292  return (xmpp_config_updater_->config().gr_time() ||
293  xmpp_config_updater_->config().llgr_time());
294 }
295 
299 }
300 
301 bool XmppServer::Initialize(short port) {
302  log_uve_ = false;
303  return TcpServer::Initialize(port);
304 }
305 
306 bool XmppServer::Initialize(short port, bool logUVE) {
307  log_uve_ = logUVE;
308  return TcpServer::Initialize(port);
309 }
310 
311 bool XmppServer::Initialize(short port, bool logUVE, const IpAddress& ip) {
312  log_uve_ = logUVE;
313  return TcpServer::Initialize(port, ip);
314 }
315 
316 //
317 // Can be removed after Shutdown is renamed to ManagedDelete.
318 //
321 }
322 
323 //
324 // Return true if it's possible to delete the XmppServer.
325 //
326 // No need to check the connection WorkQueue since XmppServerConnections on it
327 // are dependents of the XmppServer.
328 //
329 bool XmppServer::MayDelete() const {
330  return (GetSessionQueueSize() == 0);
331 }
332 
333 //
334 // Trigger deletion of the XmppServer.
335 //
336 // A mutex is used to ensure that we do not create new XmppServerConnections
337 // after this point. Note that this routine and AcceptSession may be called
338 // concurrently from 2 different threads in tests.
339 //
341  std::scoped_lock lock(deletion_mutex_);
342  deleter_->Delete();
343 }
344 
345 //
346 // Called when the XmppServer delete actor is being destroyed.
347 //
349  ClearSessions();
351 }
352 
354  return deleter_.get();
355 }
356 
358  return deleter_.get();
359 }
360 
362  return lifetime_manager_.get();
363 }
364 
367  SOL_SOCKET, SO_REUSEADDR> reuse_addr_t;
369  Socket *socket = session->socket();
370 
371  boost::system::error_code err;
372  socket->open(ip::tcp::v4(), err);
373  if (err) {
374  XMPP_WARNING(ServerOpenFail, err.message());
375  }
376 
377  socket->set_option(reuse_addr_t(true), err);
378  if (err) {
379  XMPP_WARNING(SetSockOptFail, "", XMPP_PEER_DIR_OUT, err.message());
380  }
381 
382  socket->bind(LocalEndpoint(), err);
383  if (err) {
384  XMPP_WARNING(ServerBindFailure, err.message());
385  }
386 
387  XmppSession *xmpps = static_cast<XmppSession *>(session);
388  err = xmpps->EnableTcpKeepalive(tcp_hold_time_);
389  if (err) {
390  XMPP_WARNING(ServerKeepAliveFailure, session->ToUVEKey(),
391  XMPP_PEER_DIR_OUT, err.message());
392  }
393 
394  return session;
395 }
396 
398  return connection_event_map_.size();
399 }
400 
403  return connection_map_.size();
404 }
405 
407  return ConnectionMapSize() + deleted_connection_set_.size();
408 }
409 
412  ConnectionMap::iterator loc = connection_map_.find(remote_endpoint);
413  if (loc != connection_map_.end()) {
414  return loc->second;
415  }
416  return NULL;
417 }
418 
419 XmppServerConnection *XmppServer::FindConnection(const string &address) {
421  for (auto& value : connection_map_) {
422  if (value.second->ToString() == address)
423  return value.second;
424  }
425  return NULL;
426 }
427 
428 bool XmppServer::ClearConnection(const string &hostname) {
430  for (auto& value : connection_map_) {
431  if (value.second->GetComputeHostName() == hostname) {
432  value.second->Clear();
433  return true;
434  }
435  }
436  return false;
437 }
438 
439 void XmppServer::UpdateAllConnections(uint8_t time_out) {
441  for (auto& value : connection_map_) {
442  value.second->UpdateKeepAliveTimer(time_out);
443  }
444 }
445 
448  for (auto& value : connection_map_) {
449  value.second->Clear();
450  }
451 }
452 
454  ConnectionEventCb cb) {
455  connection_event_map_.insert(make_pair(id, cb));
456 }
457 
459  ConnectionEventCbMap::iterator it = connection_event_map_.find(id);
460  if (it != connection_event_map_.end())
461  connection_event_map_.erase(it);
462 }
463 
465  xmps::PeerState state) {
466  ConnectionEventCbMap::iterator iter = connection_event_map_.begin();
467  for (; iter != connection_event_map_.end(); ++iter) {
468  ConnectionEventCb cb = iter->second;
469  cb(mux, state);
470  }
471 }
472 
474  SslSession *session = new XmppSession(this, socket);
475  boost::system::error_code err;
476  XmppSession *xmpp_session = static_cast<XmppSession *>(session);
477  err = xmpp_session->EnableTcpKeepalive(tcp_hold_time_);
478  if (err) {
479  XMPP_WARNING(ServerKeepAliveFailure, xmpp_session->ToUVEKey(),
480  XMPP_PEER_DIR_OUT, err.message());
481  }
482  return session;
483 }
484 
485 //
486 // Accept newly formed passive tcp session by creating necessary xmpp data
487 // structures. We do so to make sure that if there is any error reported
488 // over this tcp session, it can still be correctly handled, even though
489 // the allocated xmpp data structures are not fully processed yet.
490 //
492  std::scoped_lock lock(deletion_mutex_);
493  if (deleter_->IsDeleted())
494  return false;
495 
496  XmppSession *session = dynamic_cast<XmppSession *>(tcp_session);
497  XmppServerConnection *connection = CreateConnection(session);
498 
499  if (xmpp_config_updater_) {
500  connection->state_machine()->set_hold_time(
501  xmpp_config_updater_->xmpp_hold_time());
502  }
503  // Register event handler.
504  tcp_session->set_observer(boost::bind(&XmppStateMachine::OnSessionEvent,
505  connection->state_machine(), _1, _2));
506 
507  // set async_read_ready as false
508  session->set_read_on_connect(false);
509  connection->set_session(session);
510  connection->state_machine()->set_session(session);
511  connection->set_on_work_queue();
512  connection_queue_.Enqueue(connection);
513  return true;
514 }
515 
516 //
517 // Remove the given XmppServerConnection from the ConnectionMap.
518 //
520  CHECK_CONCURRENCY("bgp::Config");
521 
522  assert(connection->IsDeleted());
523  Endpoint endpoint = connection->endpoint();
525  ConnectionMap::iterator loc = connection_map_.find(endpoint);
526  assert(loc != connection_map_.end() && loc->second == connection);
527  connection_map_.erase(loc);
528 }
529 
531  XmppConnection *connection1, XmppConnection *connection2) {
533  ConnectionMap::iterator loc1 =
534  connection_map_.find(connection1->endpoint());
535  assert(loc1 != connection_map_.end());
536  ConnectionMap::iterator loc2 =
537  connection_map_.find(connection2->endpoint());
538  assert(loc2 != connection_map_.end());
539  swap(loc1->second, loc2->second);
540  swap(loc1->second->endpoint(), loc2->second->endpoint());
541 }
542 
543 //
544 // Insert the given XmppServerConnection into the ConnectionMap.
545 //
547  CHECK_CONCURRENCY("bgp::Config");
548 
549  assert(!connection->IsDeleted());
550  connection->Initialize();
551  Endpoint endpoint = connection->endpoint();
552  ConnectionMap::iterator loc;
553  bool result;
555  tie(loc, result) = connection_map_.insert(make_pair(endpoint, connection));
556  assert(result);
558 }
559 
560 //
561 // Create XmppConnnection and its associated data structures. This API is
562 // only used to allocate data structures and initialize necessary fields.
563 // The data structures are not populated to any maps in the XmppServer at
564 // this point. However, the newly created XmppServerConnection does add
565 // itself as a dependent of the XmppServer via LifetimeManager linkage.
566 //
568  XmppServerConnection *connection;
569 
570  XmppChannelConfig cfg(false);
571  cfg.endpoint = session->remote_endpoint();
572  cfg.local_endpoint = session->local_endpoint();
573  cfg.FromAddr = server_addr_;
574  cfg.logUVE = log_uve_;
576  cfg.dscp_value = dscp_value_;
577 
578  XMPP_DEBUG(XmppCreateConnection, session->ToUVEKey(), XMPP_PEER_DIR_OUT,
579  session->ToString());
580  connection = XmppStaticObjectFactory::Create<XmppServerConnection>
581  (this, static_cast<const XmppChannelConfig*>(&cfg));
582 
583  return connection;
584 }
585 
586 void XmppServer::SetDscpValue(uint8_t value) {
588  dscp_value_ = value;
589  for (auto& value : connection_map_) {
590  XmppServerConnection *connection = value.second;
591  connection->SetDscpValue(dscp_value_);
592  }
593 }
594 //
595 // Handler for XmppServerConnections that are dequeued from the WorkQueue.
596 //
597 // Since the XmppServerConnections on the WorkQueue are dependents of the
598 // XmppServer, we are guaranteed that the XmppServer won't get destroyed
599 // before the connection WorkQueue is drained.
600 //
602  CHECK_CONCURRENCY("bgp::Config");
603  connection->clear_on_work_queue();
604 
605  // This happens if the XmppServer got deleted while the XmppConnnection
606  // was on the WorkQueue.
607  if (connection->IsDeleted()) {
608  connection->RetryDelete();
609  return true;
610  }
611 
612  XmppSession *session = connection->session();
613  connection->clear_session();
614  Endpoint remote_endpoint = session->remote_endpoint();
615  XmppServerConnection *old_connection = FindConnection(remote_endpoint);
616 
617  // Close as duplicate if we have a connection from the same Endpoint.
618  // Otherwise go ahead and insert into the ConnectionMap. We may find
619  // it has a conflicting Endpoint name and decide to terminate it when
620  // we process the Open message.
621  if (old_connection) {
622  XMPP_DEBUG(XmppCreateConnection, session->ToUVEKey(), XMPP_PEER_DIR_IN,
623  "Close duplicate connection " + session->ToString());
624  // Remove reference to the session from StateMachine directly. We don't
625  // go through the entire normal cleanup pipeline for cleaning these
626  // duplicate connections.
627  assert(connection->state_machine()->session());
628  assert(connection->state_machine()->session() == session);
629  connection->state_machine()->RemoveSession();
630  DeleteSession(session);
631  connection->set_duplicate();
632  connection->ManagedDelete();
633  InsertDeletedConnection(connection);
634  } else {
635  InsertConnection(connection);
636  connection->AcceptSession(session);
637  }
638 
639  return true;
640 }
641 
643  return connection_queue_.Length();
644 }
645 
647  connection_queue_.set_disable(disabled);
648 }
649 
650 //
651 // Connection is marked deleted, add it to the ConnectionSet.
652 //
654  CHECK_CONCURRENCY("bgp::Config");
655 
656  assert(connection->IsDeleted());
657  ConnectionSet::iterator it;
658  bool result;
659  tie(it, result) = deleted_connection_set_.insert(connection);
660  assert(result);
661 }
662 
663 //
664 // Connection is being destroyed, remove it from the ConnectionSet.
665 //
667  CHECK_CONCURRENCY("bgp::Config");
668 
669  assert(connection->IsDeleted());
670  ConnectionSet::iterator it = deleted_connection_set_.find(connection);
671  assert(it != deleted_connection_set_.end());
672  deleted_connection_set_.erase(it);
673  ReleaseConnectionEndpoint(connection);
674 }
675 
677  const string &endpoint_name) {
678  std::scoped_lock lock(endpoint_map_mutex_);
679  ConnectionEndpointMap::const_iterator loc =
680  connection_endpoint_map_.find(endpoint_name);
681  return (loc != connection_endpoint_map_.end() ? loc->second : NULL);
682 }
683 
685  XmppServerConnection *connection, bool &created) {
686  created = false;
687  if (!connection)
688  return NULL;
689 
690  std::scoped_lock lock(endpoint_map_mutex_);
691 
692  ConnectionEndpointMap::const_iterator loc =
693  connection_endpoint_map_.find(connection->ToString());
694  XmppConnectionEndpoint *conn_endpoint;
695 
696  if (loc != connection_endpoint_map_.end()) {
697  conn_endpoint = loc->second;
698  if (!conn_endpoint->connection()) {
699  created = true;
700  conn_endpoint->set_connection(connection);
701  connection->set_conn_endpoint(conn_endpoint);
702  }
703  return conn_endpoint;
704  }
705 
706  created = true;
707  conn_endpoint = new XmppConnectionEndpoint(connection->ToString());
708  bool result;
709  tie(loc, result) = connection_endpoint_map_.insert(
710  make_pair(connection->ToString(), conn_endpoint));
711  assert(result);
712  conn_endpoint->set_connection(connection);
713  connection->set_conn_endpoint(conn_endpoint);
714  return conn_endpoint;
715 }
716 
717 //
718 // Remove association of the given XmppConnectionEndpoint with XmppConnection.
719 // This method is provided just to make things symmetrical - the caller could
720 // simply have called XmppConnectionEndpoint::reset_connection directly.
721 //
723  std::scoped_lock lock(endpoint_map_mutex_);
724 
725  if (!connection->conn_endpoint())
726  return;
727  assert(connection->conn_endpoint()->connection() == connection);
728  connection->conn_endpoint()->reset_connection();
729  connection->set_conn_endpoint(NULL);
730 }
731 
733  vector<ShowXmppConnection> *show_connection_list) const {
735  for (const auto& value : connection_map_) {
736  const XmppServerConnection *connection = value.second;
737  ShowXmppConnection show_connection;
738  connection->FillShowInfo(&show_connection);
739  show_connection_list->push_back(show_connection);
740  }
741  for (const auto& connection : deleted_connection_set_) {
742  ShowXmppConnection show_connection;
743  connection->FillShowInfo(&show_connection);
744  show_connection_list->push_back(show_connection);
745  }
746 }
747 
748 void XmppServer::FillShowServer(ShowXmppServerResp *resp) const {
749  SocketIOStats peer_socket_stats;
750  GetRxSocketStats(&peer_socket_stats);
751  resp->set_rx_socket_stats(peer_socket_stats);
752  GetTxSocketStats(&peer_socket_stats);
753  resp->set_tx_socket_stats(peer_socket_stats);
754  resp->set_current_connections(ConnectionMapSize());
755  resp->set_max_connections(max_connections_);
756 }
757 
759 public:
760  static bool CallbackS1(const Sandesh *sr,
761  const RequestPipeline::PipeSpec ps, int stage, int instNum,
763  const ShowXmppConnectionReq *req =
764  static_cast<const ShowXmppConnectionReq *>(ps.snhRequest_.get());
765  XmppSandeshContext *xsc =
766  dynamic_cast<XmppSandeshContext *>(req->module_context("XMPP"));
767 
768  ShowXmppConnectionResp *resp = new ShowXmppConnectionResp;
769  vector<ShowXmppConnection> connections;
770  if (xsc)
771  xsc->xmpp_server->FillShowConnections(&connections);
772  resp->set_connections(connections);
773  resp->set_context(req->context());
774  resp->Response();
775  return true;
776  }
777 };
778 
779 void ShowXmppConnectionReq::HandleRequest() const {
780  RequestPipeline::PipeSpec ps(this);
781 
782  // Request pipeline has single stage to collect connection info and
783  // respond to the request.
786  s1.taskId_ = scheduler->GetTaskId("bgp::ShowCommand");
788  s1.instances_.push_back(0);
789  ps.stages_.push_back(s1);
790  RequestPipeline rp(ps);
791 }
792 
794 public:
795  static bool CallbackS1(const Sandesh *sr,
796  const RequestPipeline::PipeSpec ps, int stage, int instNum,
798 
799  const ClearXmppConnectionReq *req =
800  static_cast<const ClearXmppConnectionReq *>(ps.snhRequest_.get());
801  XmppSandeshContext *xsc =
802  dynamic_cast<XmppSandeshContext *>(req->module_context("XMPP"));
803 
804  ClearXmppConnectionResp *resp = new ClearXmppConnectionResp;
805  if (!xsc || !xsc->test_mode) {
806  resp->set_success(false);
807  } else if (req->get_hostname_or_all() != "all") {
808  if (xsc->xmpp_server->ClearConnection(req->get_hostname_or_all())) {
809  resp->set_success(true);
810  } else {
811  resp->set_success(false);
812  }
813  } else {
814  if (xsc->xmpp_server->ConnectionCount()) {
816  resp->set_success(true);
817  } else {
818  resp->set_success(false);
819  }
820  }
821 
822  resp->set_context(req->context());
823  resp->Response();
824  return true;
825  }
826 };
827 
828 void ClearXmppConnectionReq::HandleRequest() const {
829 
830  // config task is used to create and delete connection objects.
831  // hence use the same task to find the connection
833  s1.taskId_ = TaskScheduler::GetInstance()->GetTaskId("bgp::Config");
834  s1.instances_.push_back(0);
836 
837  RequestPipeline::PipeSpec ps(this);
838  ps.stages_.push_back(s1);
839  RequestPipeline rp(ps);
840 }
841 
843 public:
844  static bool CallbackS1(const Sandesh *sr,
845  const RequestPipeline::PipeSpec ps, int stage, int instNum,
847  const ShowXmppServerReq *req =
848  static_cast<const ShowXmppServerReq *>(ps.snhRequest_.get());
849  XmppSandeshContext *xsc =
850  dynamic_cast<XmppSandeshContext *>(req->module_context("XMPP"));
851 
852  ShowXmppServerResp *resp = new ShowXmppServerResp;
853  if (xsc)
854  xsc->xmpp_server->FillShowServer(resp);
855  resp->set_context(req->context());
856  resp->Response();
857  return true;
858  }
859 };
860 
861 void ShowXmppServerReq::HandleRequest() const {
862  RequestPipeline::PipeSpec ps(this);
865  s1.taskId_ = scheduler->GetTaskId("bgp::ShowCommand");
867  s1.instances_.push_back(0);
868  ps.stages_.push_back(s1);
869  RequestPipeline rp(ps);
870 }
boost::asio::ip::address IpAddress
Definition: address.h:14
void RegisterObservers(const Observers &obs)
Definition: bgp_config.h:787
uint16_t end_of_rib_timeout() const
Definition: bgp_config.h:639
uint16_t rd_cluster_seed() const
Definition: bgp_config.h:671
uint32_t llgr_time() const
Definition: bgp_config.h:635
bool gr_enable() const
Definition: bgp_config.h:645
static const int kEndOfRibTime
Definition: bgp_config.h:616
bool fc_enabled() const
Definition: bgp_config.h:649
uint8_t xmpp_hold_time() const
Definition: bgp_config.h:665
bool gr_xmpp_helper() const
Definition: bgp_config.h:643
uint16_t gr_time() const
Definition: bgp_config.h:633
const std::string & subcluster_name() const
Definition: bgp_config.h:564
static bool CallbackS1(const Sandesh *sr, const RequestPipeline::PipeSpec ps, int stage, int instNum, RequestPipeline::InstData *data)
Definition: xmpp_server.cc:795
static bool CallbackS1(const Sandesh *sr, const RequestPipeline::PipeSpec ps, int stage, int instNum, RequestPipeline::InstData *data)
Definition: xmpp_server.cc:760
static bool CallbackS1(const Sandesh *sr, const RequestPipeline::PipeSpec ps, int stage, int instNum, RequestPipeline::InstData *data)
Definition: xmpp_server.cc:844
boost::asio::ssl::stream< boost::asio::ip::tcp::socket > SslSocket
Definition: ssl_server.h:17
boost::asio::ssl::context * context()
Definition: ssl_server.cc:44
The TaskScheduler keeps track of what tasks are currently schedulable. When a task is enqueued it is ...
Definition: task.h:307
int GetTaskId(const std::string &name)
Definition: task.cc:884
static TaskScheduler * GetInstance()
Definition: task.cc:574
Endpoint LocalEndpoint() const
Definition: tcp_server.cc:309
void GetRxSocketStats(SocketIOStats *socket_stats) const
Definition: tcp_server.cc:642
virtual bool Initialize(unsigned short port)
Definition: tcp_server.cc:61
virtual TcpSession * CreateSession()
Definition: tcp_server.cc:191
virtual void DeleteSession(TcpSession *session)
Definition: tcp_server.cc:200
void ClearSessions()
Definition: tcp_server.cc:161
boost::asio::ip::tcp::socket Socket
Definition: tcp_server.h:32
void GetTxSocketStats(SocketIOStats *socket_stats) const
Definition: tcp_server.cc:646
const std::string & ToUVEKey() const
Definition: tcp_session.h:175
virtual Socket * socket() const
Definition: tcp_session.h:83
virtual std::string ToString() const
Definition: tcp_session.h:80
void set_read_on_connect(bool read)
Definition: tcp_session.h:148
void set_observer(EventObserver observer)
Definition: tcp_session.cc:220
Endpoint remote_endpoint() const
Definition: tcp_session.h:132
Endpoint local_endpoint() const
Definition: tcp_session.cc:207
bool Enqueue(QueueEntryT entry)
Definition: queue_task.h:249
void set_disable(bool disabled)
Definition: queue_task.h:320
size_t Length() const
Definition: queue_task.h:357
void Shutdown(bool delete_entries=true)
Definition: queue_task.h:153
std::string FromAddr
Definition: xmpp_config.h:22
boost::asio::ip::tcp::endpoint local_endpoint
Definition: xmpp_config.h:26
boost::asio::ip::tcp::endpoint endpoint
Definition: xmpp_config.h:25
std::string path_to_ca_cert
Definition: xmpp_config.h:31
uint8_t dscp_value
Definition: xmpp_config.h:35
std::string path_to_server_cert
Definition: xmpp_config.h:29
std::string path_to_server_priv_key
Definition: xmpp_config.h:30
void set_xmpp_hold_time(int hold_time)
Definition: xmpp_server.cc:193
const string subcluster_name() const
Definition: xmpp_server.cc:187
uint8_t xmpp_hold_time() const
Definition: xmpp_server.cc:192
BgpGlobalSystemConfig config_
Definition: xmpp_server.cc:199
XmppServer * server_
Definition: xmpp_server.cc:198
void ProcessGlobalSystemConfig(const BgpGlobalSystemConfig *system, BgpConfigManager::EventType event)
Definition: xmpp_server.cc:153
XmppConfigUpdater(XmppServer *server, BgpConfigManager *config_manager)
Definition: xmpp_server.cc:131
void ProcessProtocolConfig(const BgpProtocolConfig *protocol_config, BgpConfigManager::EventType event)
Definition: xmpp_server.cc:144
const BgpGlobalSystemConfig & config() const
Definition: xmpp_server.cc:142
void set_subcluster_name(const string &name)
Definition: xmpp_server.cc:188
void set_connection(XmppConnection *connection)
XmppConnection * connection()
const XmppSession * session() const
bool IsDeleted() const
virtual boost::asio::ip::tcp::endpoint endpoint() const
virtual bool AcceptSession(XmppSession *session)
int SetDscpValue(uint8_t value)
XmppStateMachine * state_machine()
const std::string & ToString() const
void set_session(XmppSession *session)
virtual void ManagedDelete()
XmppConnectionEndpoint * conn_endpoint()
void set_conn_endpoint(XmppConnectionEndpoint *conn_endpoint)
virtual void RetryDelete()
void FillShowInfo(ShowXmppConnection *show_connection) const
DeleteActor(XmppServer *server)
Definition: xmpp_server.cc:33
virtual void Shutdown()
Definition: xmpp_server.cc:40
virtual void Destroy()
Definition: xmpp_server.cc:44
virtual bool MayDelete() const
Definition: xmpp_server.cc:36
uint32_t GetLongLivedGracefulRestartTime() const
Definition: xmpp_server.cc:246
virtual bool IsPeerCloseGraceful() const
Definition: xmpp_server.cc:281
void ClearAllConnections()
Definition: xmpp_server.cc:446
XmppServer(EventManager *evm, const std::string &server_addr, const XmppChannelConfig *config)
void SetDscpValue(uint8_t value)
Definition: xmpp_server.cc:586
LifetimeManager * lifetime_manager()
Definition: xmpp_server.cc:361
std::mutex deletion_mutex_
Definition: xmpp_server.h:143
XmppConnectionEndpoint * FindConnectionEndpoint(const std::string &endpoint_name)
Definition: xmpp_server.cc:676
virtual ~XmppServer()
Definition: xmpp_server.cc:296
size_t ConnectionEventCount() const
Definition: xmpp_server.cc:397
bool MayDelete() const
Definition: xmpp_server.cc:329
virtual XmppServerConnection * CreateConnection(XmppSession *session)
Definition: xmpp_server.cc:567
virtual void RemoveDeletedConnection(XmppServerConnection *connection)
Definition: xmpp_server.cc:666
std::mutex endpoint_map_mutex_
Definition: xmpp_server.h:140
virtual SslSession * AllocSession(SslSocket *socket)
Definition: xmpp_server.cc:473
size_t ConnectionCount() const
Definition: xmpp_server.cc:406
void FillShowConnections(std::vector< ShowXmppConnection > *show_connection_list) const
Definition: xmpp_server.cc:732
std::shared_mutex connection_map_mutex_
Definition: xmpp_server.h:117
std::string server_addr_
Definition: xmpp_server.h:148
boost::function< void(XmppChannelMux *, xmps::PeerState)> ConnectionEventCb
Definition: xmpp_server.h:47
virtual void RemoveConnection(XmppServerConnection *connection)
Definition: xmpp_server.cc:519
ConnectionEventCbMap connection_event_map_
Definition: xmpp_server.h:147
std::shared_lock< std::shared_mutex > ReadLock
Definition: xmpp_server.h:39
ConnectionSet deleted_connection_set_
Definition: xmpp_server.h:137
virtual void InsertConnection(XmppServerConnection *connection)
Definition: xmpp_server.cc:546
virtual bool AcceptSession(TcpSession *session)
Definition: xmpp_server.cc:491
bool ClearConnection(const std::string &hostname)
Definition: xmpp_server.cc:428
void UnRegisterConnectionEvent(xmps::PeerId)
Definition: xmpp_server.cc:458
bool log_uve_
Definition: xmpp_server.h:149
void Terminate()
Definition: xmpp_server.cc:348
void SwapXmppConnectionMapEntries(XmppConnection *connection1, XmppConnection *connection2)
Definition: xmpp_server.cc:530
bool gr_helper_disable_
Definition: xmpp_server.h:152
bool auth_enabled_
Definition: xmpp_server.h:150
void CreateConfigUpdater(BgpConfigManager *config_manager)
Definition: xmpp_server.cc:235
ConnectionMap connection_map_
Definition: xmpp_server.h:119
virtual TcpSession * CreateSession()
Definition: xmpp_server.cc:365
boost::scoped_ptr< DeleteActor > deleter_
Definition: xmpp_server.h:145
void SetConnectionQueueDisable(bool disabled)
Definition: xmpp_server.cc:646
void Shutdown()
Definition: xmpp_server.cc:340
int tcp_hold_time_
Definition: xmpp_server.h:151
virtual bool Initialize(short port)
Definition: xmpp_server.cc:301
void ReleaseConnectionEndpoint(XmppServerConnection *connection)
Definition: xmpp_server.cc:722
bool IsGRHelperModeEnabled() const
Definition: xmpp_server.cc:265
WorkQueue< XmppServerConnection * > connection_queue_
Definition: xmpp_server.h:156
bool DequeueConnection(XmppServerConnection *connection)
Definition: xmpp_server.cc:601
uint16_t GetGracefulRestartTime() const
Definition: xmpp_server.cc:239
XmppConnectionEndpoint * LocateConnectionEndpoint(XmppServerConnection *connection, bool &created)
Definition: xmpp_server.cc:684
size_t ConnectionMapSize() const
Definition: xmpp_server.cc:401
void RegisterConnectionEvent(xmps::PeerId, ConnectionEventCb)
Definition: xmpp_server.cc:453
virtual XmppServerConnection * FindConnection(Endpoint remote_endpoint)
Definition: xmpp_server.cc:410
boost::asio::ip::tcp::endpoint Endpoint
Definition: xmpp_server.h:37
uint8_t dscp_value_
Definition: xmpp_server.h:154
size_t GetConnectionQueueSize() const
Definition: xmpp_server.cc:642
friend class DeleteActor
Definition: xmpp_server.h:124
void SessionShutdown()
Definition: xmpp_server.cc:319
boost::scoped_ptr< XmppConfigUpdater > xmpp_config_updater_
Definition: xmpp_server.h:153
boost::scoped_ptr< LifetimeManager > lifetime_manager_
Definition: xmpp_server.h:144
virtual LifetimeActor * deleter()
Definition: xmpp_server.cc:353
virtual void InsertDeletedConnection(XmppServerConnection *connection)
Definition: xmpp_server.cc:653
void NotifyConnectionEvent(XmppChannelMux *, xmps::PeerState)
Definition: xmpp_server.cc:464
uint32_t GetEndOfRibSendTime() const
Definition: xmpp_server.cc:259
void UpdateAllConnections(uint8_t time_out)
Definition: xmpp_server.cc:439
ConnectionEndpointMap connection_endpoint_map_
Definition: xmpp_server.h:141
size_t max_connections_
Definition: xmpp_server.h:138
void FillShowServer(ShowXmppServerResp *resp) const
Definition: xmpp_server.cc:748
uint32_t GetEndOfRibReceiveTime() const
Definition: xmpp_server.cc:253
std::unique_lock< std::shared_mutex > WriteLock
Definition: xmpp_server.h:38
boost::system::error_code EnableTcpKeepalive(int tcp_hold_time)
void set_hold_time(int hold_time)
void set_session(TcpSession *session)
XmppSession * session()
virtual void OnSessionEvent(TcpSession *session, TcpSession::Event event)
static EventManager evm
#define LOG(_Level, _Msg)
Definition: logging.h:35
unsigned int boolean
Definition: mcast_common.h:12
BgpGlobalSystemConfigObserver system
Definition: bgp_config.h:764
BgpProtocolObserver protocol
Definition: bgp_config.h:760
boost::shared_ptr< const SandeshRequest > snhRequest_
std::vector< int > instances_
XmppServer * xmpp_server
Definition: xmpp_sandesh.h:17
#define CHECK_CONCURRENCY(...)
void STLDeleteElements(Container *container)
Definition: util.h:115
#define XMPP_PEER_DIR_OUT
Definition: xmpp_log.h:15
#define XMPP_WARNING(obj,...)
Definition: xmpp_log.h:36
#define XMPP_DEBUG(obj,...)
Definition: xmpp_log.h:60
#define XMPP_PEER_DIR_IN
Definition: xmpp_log.h:16
#define DEFAULT_XMPP_HOLD_TIME
Definition: xmpp_server.cc:30