/
xmpp_server.cc
640 lines (552 loc) · 20.8 KB
/
xmpp_server.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include "xmpp/xmpp_server.h"
#include <boost/foreach.hpp>
#include <boost/tuple/tuple.hpp>
#include "base/task_annotations.h"
#include "xmpp/xmpp_connection.h"
#include "xmpp/xmpp_factory.h"
#include "xmpp/xmpp_lifetime.h"
#include "xmpp/xmpp_log.h"
#include "xmpp/xmpp_sandesh.h"
#include "xmpp/xmpp_session.h"
#include "sandesh/request_pipeline.h"
#include "sandesh/sandesh.h"
#include "sandesh/sandesh_types.h"
#include "sandesh/common/vns_types.h"
#include "sandesh/common/vns_constants.h"
#include "sandesh/xmpp_server_types.h"
#include "sandesh/xmpp_trace_sandesh_types.h"
#include "sandesh/xmpp_client_server_sandesh_types.h"
using namespace std;
using namespace boost::asio;
using boost::tie;
class XmppServer::DeleteActor : public LifetimeActor {
public:
DeleteActor(XmppServer *server)
: LifetimeActor(server->lifetime_manager()), server_(server) {
}
virtual bool MayDelete() const {
CHECK_CONCURRENCY("bgp::Config");
return server_->MayDelete();
}
virtual void Shutdown() {
CHECK_CONCURRENCY("bgp::Config");
server_->SessionShutdown();
}
virtual void Destroy() {
CHECK_CONCURRENCY("bgp::Config");
server_->Terminate();
}
private:
XmppServer *server_;
};
XmppServer::XmppServer(EventManager *evm, const string &server_addr,
const XmppChannelConfig *config)
: XmppConnectionManager(
evm, ssl::context::tlsv1_server, config->auth_enabled, true),
max_connections_(0),
lifetime_manager_(XmppObjectFactory::Create<XmppLifetimeManager>(
TaskScheduler::GetInstance()->GetTaskId("bgp::Config"))),
deleter_(new DeleteActor(this)),
server_addr_(server_addr),
log_uve_(false),
auth_enabled_(config->auth_enabled),
tcp_hold_time_(config->tcp_hold_time),
connection_queue_(TaskScheduler::GetInstance()->GetTaskId("bgp::Config"),
0, boost::bind(&XmppServer::DequeueConnection, this, _1)) {
if (config->auth_enabled) {
// Get SSL context from base class and update
boost::asio::ssl::context *ctx = context();
boost::system::error_code ec;
// set mode
ctx->set_options(ssl::context::default_workarounds |
ssl::context::no_sslv3 | ssl::context::no_sslv2, ec);
assert(ec.value() == 0);
ctx->use_certificate_file(config->path_to_server_cert,
boost::asio::ssl::context::pem, ec);
assert(ec.value() == 0);
ctx->use_private_key_file(config->path_to_pvt_key,
boost::asio::ssl::context::pem, ec);
assert(ec.value() == 0);
ctx->set_verify_mode(boost::asio::ssl::verify_none, ec);
assert(ec.value() == 0);
}
}
XmppServer::XmppServer(EventManager *evm, const string &server_addr)
: XmppConnectionManager(evm, ssl::context::tlsv1_server, false, false),
max_connections_(0),
lifetime_manager_(XmppObjectFactory::Create<XmppLifetimeManager>(
TaskScheduler::GetInstance()->GetTaskId("bgp::Config"))),
deleter_(new DeleteActor(this)),
server_addr_(server_addr),
log_uve_(false),
auth_enabled_(false),
tcp_hold_time_(XmppChannelConfig::kTcpHoldTime),
connection_queue_(TaskScheduler::GetInstance()->GetTaskId("bgp::Config"),
0, boost::bind(&XmppServer::DequeueConnection, this, _1)) {
}
XmppServer::XmppServer(EventManager *evm)
: XmppConnectionManager(evm, ssl::context::tlsv1_server, false, false),
max_connections_(0),
lifetime_manager_(new LifetimeManager(
TaskScheduler::GetInstance()->GetTaskId("bgp::Config"))),
deleter_(new DeleteActor(this)),
log_uve_(false),
auth_enabled_(false),
tcp_hold_time_(XmppChannelConfig::kTcpHoldTime),
connection_queue_(TaskScheduler::GetInstance()->GetTaskId("bgp::Config"),
0, boost::bind(&XmppServer::DequeueConnection, this, _1)) {
}
bool XmppServer::IsPeerCloseGraceful() {
// If the server is deleted, do not do graceful restart
if (deleter()->IsDeleted()) return false;
static bool init = false;
static bool enabled = false;
if (!init) {
init = true;
char *p = getenv("XMPP_GRACEFUL_RESTART_ENABLE");
if (p && !strcasecmp(p, "true")) enabled = true;
}
return enabled;
}
XmppServer::~XmppServer() {
STLDeleteElements(&connection_endpoint_map_);
TcpServer::ClearSessions();
}
bool XmppServer::Initialize(short port) {
log_uve_ = false;
return TcpServer::Initialize(port);
}
bool XmppServer::Initialize(short port, bool logUVE) {
log_uve_ = logUVE;
return TcpServer::Initialize(port);
}
//
// Can be removed after Shutdown is renamed to ManagedDelete.
//
void XmppServer::SessionShutdown() {
XmppConnectionManager::Shutdown();
}
//
// Return true if it's possible to delete the XmppServer.
//
// No need to check the connection WorkQueue since XmppServerConnections on it
// are dependents of the XmppServer.
//
bool XmppServer::MayDelete() const {
return (GetSessionQueueSize() == 0);
}
//
// Trigger deletion of the XmppServer.
//
// A mutex is used to ensure that we do not create new XmppServerConnections
// after this point. Note that this routine and AcceptSession may be called
// concurrently from 2 different threads in tests.
//
void XmppServer::Shutdown() {
tbb::mutex::scoped_lock lock(deletion_mutex_);
deleter_->Delete();
}
//
// Called when the XmppServer delete actor is being destroyed.
//
void XmppServer::Terminate() {
ClearSessions();
connection_queue_.Shutdown();
}
LifetimeActor *XmppServer::deleter() {
return deleter_.get();
}
LifetimeManager *XmppServer::lifetime_manager() {
return lifetime_manager_.get();
}
TcpSession *XmppServer::CreateSession() {
typedef boost::asio::detail::socket_option::boolean<
#ifdef __APPLE__
SOL_SOCKET, SO_REUSEPORT> reuse_port_t;
#else
SOL_SOCKET, SO_REUSEADDR> reuse_addr_t;
#endif
TcpSession *session = TcpServer::CreateSession();
Socket *socket = session->socket();
boost::system::error_code err;
socket->open(ip::tcp::v4(), err);
if (err) {
XMPP_WARNING(ServerOpenFail, err.message());
}
#ifdef __APPLE__
socket->set_option(reuse_port_t(true), err);
#else
socket->set_option(reuse_addr_t(true), err);
#endif
if (err) {
XMPP_WARNING(SetSockOptFail, err.message());
return session;
}
socket->bind(LocalEndpoint(), err);
if (err) {
XMPP_WARNING(ServerBindFailure, err.message());
}
XmppSession *xmpps = static_cast<XmppSession *>(session);
err = xmpps->EnableTcpKeepalive(tcp_hold_time_);
if (err) {
XMPP_WARNING(ServerKeepAliveFailure, err.message());
}
return session;
}
size_t XmppServer::ConnectionEventCount() const {
return connection_event_map_.size();
}
size_t XmppServer::ConnectionCount() const {
return connection_map_.size() + deleted_connection_set_.size();
}
XmppServerConnection *XmppServer::FindConnection(Endpoint remote_endpoint) {
ConnectionMap::iterator loc = connection_map_.find(remote_endpoint);
if (loc != connection_map_.end()) {
return loc->second;
}
return NULL;
}
XmppServerConnection *XmppServer::FindConnection(const string &address) {
BOOST_FOREACH(ConnectionMap::value_type &value, connection_map_) {
if (value.second->ToString() == address)
return value.second;
}
return NULL;
}
bool XmppServer::ClearConnection(const string &hostname) {
BOOST_FOREACH(ConnectionMap::value_type &value, connection_map_) {
if (value.second->GetComputeHostName() == hostname) {
value.second->Clear();
return true;
}
}
return false;
}
void XmppServer::ClearAllConnections() {
BOOST_FOREACH(ConnectionMap::value_type &value, connection_map_) {
value.second->Clear();
}
}
void XmppServer::RegisterConnectionEvent(xmps::PeerId id,
ConnectionEventCb cb) {
connection_event_map_.insert(make_pair(id, cb));
}
void XmppServer::UnRegisterConnectionEvent(xmps::PeerId id) {
ConnectionEventCbMap::iterator it = connection_event_map_.find(id);
if (it != connection_event_map_.end())
connection_event_map_.erase(it);
}
void XmppServer::NotifyConnectionEvent(XmppChannelMux *mux,
xmps::PeerState state) {
ConnectionEventCbMap::iterator iter = connection_event_map_.begin();
for (; iter != connection_event_map_.end(); ++iter) {
ConnectionEventCb cb = iter->second;
cb(mux, state);
}
}
SslSession *XmppServer::AllocSession(SslSocket *socket) {
SslSession *session = new XmppSession(this, socket);
return session;
}
//
// Accept newly formed passive tcp session by creating necessary xmpp data
// structures. We do so to make sure that if there is any error reported
// over this tcp session, it can still be correctly handled, even though
// the allocated xmpp data structures are not fully processed yet.
//
bool XmppServer::AcceptSession(TcpSession *tcp_session) {
tbb::mutex::scoped_lock lock(deletion_mutex_);
if (deleter_->IsDeleted())
return false;
XmppSession *session = dynamic_cast<XmppSession *>(tcp_session);
XmppServerConnection *connection = CreateConnection(session);
// Register event handler.
tcp_session->set_observer(boost::bind(&XmppStateMachine::OnSessionEvent,
connection->state_machine(), _1, _2));
// set async_read_ready as false
session->set_read_on_connect(false);
connection->set_session(session);
connection->set_on_work_queue();
connection_queue_.Enqueue(connection);
return true;
}
//
// Remove the given XmppServerConnection from the ConnectionMap.
//
void XmppServer::RemoveConnection(XmppServerConnection *connection) {
CHECK_CONCURRENCY("bgp::Config");
assert(connection->IsDeleted());
Endpoint endpoint = connection->endpoint();
ConnectionMap::iterator loc = connection_map_.find(endpoint);
assert(loc != connection_map_.end() && loc->second == connection);
connection_map_.erase(loc);
}
//
// Insert the given XmppServerConnection into the ConnectionMap.
//
void XmppServer::InsertConnection(XmppServerConnection *connection) {
CHECK_CONCURRENCY("bgp::Config");
assert(!connection->IsDeleted());
connection->Initialize();
Endpoint endpoint = connection->endpoint();
ConnectionMap::iterator loc;
bool result;
tie(loc, result) = connection_map_.insert(make_pair(endpoint, connection));
assert(result);
max_connections_ = max(max_connections_, connection_map_.size());
}
//
// Create XmppConnnection and its associated data structures. This API is
// only used to allocate data structures and initialize necessary fields.
// The data structures are not populated to any maps in the XmppServer at
// this point. However, the newly created XmppServerConnection does add
// itself as a dependent of the XmppServer via LifetimeManager linkage.
//
XmppServerConnection *XmppServer::CreateConnection(XmppSession *session) {
XmppServerConnection *connection;
XmppChannelConfig cfg(false);
cfg.endpoint = session->remote_endpoint();
cfg.local_endpoint = session->local_endpoint();
cfg.FromAddr = server_addr_;
cfg.logUVE = log_uve_;
cfg.auth_enabled = auth_enabled_;
XMPP_DEBUG(XmppCreateConnection, session->ToString());
connection = XmppObjectFactory::Create<XmppServerConnection>(this, &cfg);
return connection;
}
//
// Handler for XmppServerConnections that are dequeued from the WorkQueue.
//
// Since the XmppServerConnections on the WorkQueue are dependents of the
// XmppServer, we are guaranteed that the XmppServer won't get destroyed
// before the connection WorkQueue is drained.
//
bool XmppServer::DequeueConnection(XmppServerConnection *connection) {
CHECK_CONCURRENCY("bgp::Config");
connection->clear_on_work_queue();
// This happens if the XmppServer got deleted while the XmppConnnection
// was on the WorkQueue.
if (connection->IsDeleted()) {
connection->RetryDelete();
return true;
}
XmppSession *session = connection->session();
connection->clear_session();
Endpoint remote_endpoint = session->remote_endpoint();
XmppServerConnection *old_connection = FindConnection(remote_endpoint);
// Close as duplicate if we have a connection from the same Endpoint.
// Otherwise go ahead and insert into the ConnectionMap. We may find
// it has a conflicting Endpoint name and decide to terminate it when
// we process the Open message.
if (old_connection) {
XMPP_DEBUG(XmppCreateConnection, "Close duplicate connection " +
session->ToString());
DeleteSession(session);
connection->set_duplicate();
connection->ManagedDelete();
InsertDeletedConnection(connection);
} else {
InsertConnection(connection);
connection->AcceptSession(session);
}
return true;
}
size_t XmppServer::GetConnectionQueueSize() const {
return connection_queue_.Length();
}
void XmppServer::SetConnectionQueueDisable(bool disabled) {
connection_queue_.set_disable(disabled);
}
//
// Connection is marked deleted, add it to the ConnectionSet.
//
void XmppServer::InsertDeletedConnection(XmppServerConnection *connection) {
CHECK_CONCURRENCY("bgp::Config");
assert(connection->IsDeleted());
ConnectionSet::iterator it;
bool result;
tie(it, result) = deleted_connection_set_.insert(connection);
assert(result);
}
//
// Connection is being destroyed, remove it from the ConnectionSet.
//
void XmppServer::RemoveDeletedConnection(XmppServerConnection *connection) {
CHECK_CONCURRENCY("bgp::Config");
assert(connection->IsDeleted());
ConnectionSet::iterator it = deleted_connection_set_.find(connection);
assert(it != deleted_connection_set_.end());
deleted_connection_set_.erase(it);
}
const XmppConnectionEndpoint *XmppServer::FindConnectionEndpoint(
const string &endpoint_name) const {
ConnectionEndpointMap::const_iterator loc =
connection_endpoint_map_.find(endpoint_name);
return (loc != connection_endpoint_map_.end() ? loc->second : NULL);
}
//
// Find or create an XmppConnectionEndpoint for the Endpoint of the given
// XmppConnnection. If XmppConnectionEndpoint is already associated with a
// XmppConnection, return NULL to indicate that the XmppConnection should
// be terminated. Otherwise, associate it with the given XmppConnection.
//
XmppConnectionEndpoint *XmppServer::LocateConnectionEndpoint(
XmppServerConnection *connection) {
tbb::mutex::scoped_lock lock(endpoint_map_mutex_);
const string &endpoint_name = connection->ToString();
XmppConnectionEndpoint *conn_endpoint;
ConnectionEndpointMap::iterator loc =
connection_endpoint_map_.find(endpoint_name);
if (loc == connection_endpoint_map_.end()) {
conn_endpoint = new XmppConnectionEndpoint(endpoint_name);
bool result;
tie(loc, result) = connection_endpoint_map_.insert(
make_pair(endpoint_name, conn_endpoint));
assert(result);
} else {
conn_endpoint = loc->second;
}
if (conn_endpoint->connection())
return NULL;
conn_endpoint->set_connection(connection);
return conn_endpoint;
}
//
// Remove association of the given XmppConnectionEndpoint with XmppConnection.
// This method is provided just to make things symmetrical - the caller could
// simply have called XmppConnectionEndpoint::reset_connection directly.
//
void XmppServer::ReleaseConnectionEndpoint(XmppServerConnection *connection) {
tbb::mutex::scoped_lock lock(endpoint_map_mutex_);
if (!connection->conn_endpoint())
return;
assert(connection->conn_endpoint()->connection() == connection);
connection->conn_endpoint()->reset_connection();
}
void XmppServer::FillShowConnections(
vector<ShowXmppConnection> *show_connection_list) const {
BOOST_FOREACH(const ConnectionMap::value_type &value, connection_map_) {
const XmppServerConnection *connection = value.second;
ShowXmppConnection show_connection;
connection->FillShowInfo(&show_connection);
show_connection_list->push_back(show_connection);
}
BOOST_FOREACH(const XmppServerConnection *connection,
deleted_connection_set_) {
ShowXmppConnection show_connection;
connection->FillShowInfo(&show_connection);
show_connection_list->push_back(show_connection);
}
}
void XmppServer::FillShowServer(ShowXmppServerResp *resp) const {
SocketIOStats peer_socket_stats;
GetRxSocketStats(peer_socket_stats);
resp->set_rx_socket_stats(peer_socket_stats);
GetTxSocketStats(peer_socket_stats);
resp->set_tx_socket_stats(peer_socket_stats);
resp->set_current_connections(connection_map_.size());
resp->set_max_connections(max_connections_);
}
class ShowXmppConnectionHandler {
public:
static bool CallbackS1(const Sandesh *sr,
const RequestPipeline::PipeSpec ps, int stage, int instNum,
RequestPipeline::InstData *data) {
const ShowXmppConnectionReq *req =
static_cast<const ShowXmppConnectionReq *>(ps.snhRequest_.get());
XmppSandeshContext *xsc =
dynamic_cast<XmppSandeshContext *>(req->module_context("XMPP"));
ShowXmppConnectionResp *resp = new ShowXmppConnectionResp;
vector<ShowXmppConnection> connections;
if (xsc)
xsc->xmpp_server->FillShowConnections(&connections);
resp->set_connections(connections);
resp->set_context(req->context());
resp->Response();
return true;
}
};
void ShowXmppConnectionReq::HandleRequest() const {
RequestPipeline::PipeSpec ps(this);
// Request pipeline has single stage to collect connection info and
// respond to the request.
RequestPipeline::StageSpec s1;
TaskScheduler *scheduler = TaskScheduler::GetInstance();
s1.taskId_ = scheduler->GetTaskId("bgp::ShowCommand");
s1.cbFn_ = ShowXmppConnectionHandler::CallbackS1;
s1.instances_.push_back(0);
ps.stages_.push_back(s1);
RequestPipeline rp(ps);
}
class ClearXmppConnectionHandler {
public:
static bool CallbackS1(const Sandesh *sr,
const RequestPipeline::PipeSpec ps, int stage, int instNum,
RequestPipeline::InstData *data) {
const ClearXmppConnectionReq *req =
static_cast<const ClearXmppConnectionReq *>(ps.snhRequest_.get());
XmppSandeshContext *xsc =
dynamic_cast<XmppSandeshContext *>(req->module_context("XMPP"));
ClearXmppConnectionResp *resp = new ClearXmppConnectionResp;
if (!xsc || !xsc->test_mode) {
resp->set_success(false);
} else if (req->get_hostname_or_all() != "all") {
if (xsc->xmpp_server->ClearConnection(req->get_hostname_or_all())) {
resp->set_success(true);
} else {
resp->set_success(false);
}
} else {
if (xsc->xmpp_server->ConnectionCount()) {
xsc->xmpp_server->ClearAllConnections();
resp->set_success(true);
} else {
resp->set_success(false);
}
}
resp->set_context(req->context());
resp->Response();
return true;
}
};
void ClearXmppConnectionReq::HandleRequest() const {
// config task is used to create and delete connection objects.
// hence use the same task to find the connection
RequestPipeline::StageSpec s1;
s1.taskId_ = TaskScheduler::GetInstance()->GetTaskId("bgp::Config");
s1.instances_.push_back(0);
s1.cbFn_ = ClearXmppConnectionHandler::CallbackS1;
RequestPipeline::PipeSpec ps(this);
ps.stages_.push_back(s1);
RequestPipeline rp(ps);
}
class ShowXmppServerHandler {
public:
static bool CallbackS1(const Sandesh *sr,
const RequestPipeline::PipeSpec ps, int stage, int instNum,
RequestPipeline::InstData *data) {
const ShowXmppServerReq *req =
static_cast<const ShowXmppServerReq *>(ps.snhRequest_.get());
XmppSandeshContext *xsc =
dynamic_cast<XmppSandeshContext *>(req->module_context("XMPP"));
ShowXmppServerResp *resp = new ShowXmppServerResp;
if (xsc)
xsc->xmpp_server->FillShowServer(resp);
resp->set_context(req->context());
resp->Response();
return true;
}
};
void ShowXmppServerReq::HandleRequest() const {
RequestPipeline::PipeSpec ps(this);
RequestPipeline::StageSpec s1;
TaskScheduler *scheduler = TaskScheduler::GetInstance();
s1.taskId_ = scheduler->GetTaskId("bgp::ShowCommand");
s1.cbFn_ = ShowXmppServerHandler::CallbackS1;
s1.instances_.push_back(0);
ps.stages_.push_back(s1);
RequestPipeline rp(ps);
}