/
ProtocolV2.cc
2911 lines (2476 loc) · 94.2 KB
/
ProtocolV2.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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include <type_traits>
#include "ProtocolV2.h"
#include "AsyncMessenger.h"
#include "common/EventTrace.h"
#include "common/ceph_crypto.h"
#include "common/errno.h"
#include "include/random.h"
#include "auth/AuthClient.h"
#include "auth/AuthServer.h"
#define dout_subsys ceph_subsys_ms
#undef dout_prefix
#define dout_prefix _conn_prefix(_dout)
ostream &ProtocolV2::_conn_prefix(std::ostream *_dout) {
return *_dout << "--2- " << messenger->get_myaddrs() << " >> "
<< *connection->peer_addrs << " conn(" << connection << " "
<< this
<< " " << ceph_con_mode_name(auth_meta->con_mode)
<< " :" << connection->port
<< " s=" << get_state_name(state) << " pgs=" << peer_global_seq
<< " cs=" << connect_seq << " l=" << connection->policy.lossy
<< " rx=" << session_stream_handlers.rx.get()
<< " tx=" << session_stream_handlers.tx.get()
<< ").";
}
using namespace ceph::msgr::v2;
using CtPtr = Ct<ProtocolV2> *;
using CtRef = Ct<ProtocolV2> &;
void ProtocolV2::run_continuation(CtPtr pcontinuation) {
if (pcontinuation) {
run_continuation(*pcontinuation);
}
}
void ProtocolV2::run_continuation(CtRef continuation) {
try {
CONTINUATION_RUN(continuation)
} catch (const buffer::error &e) {
lderr(cct) << __func__ << " failed decoding of frame header: " << e
<< dendl;
_fault();
} catch (const ceph::crypto::onwire::MsgAuthError &e) {
lderr(cct) << __func__ << " " << e.what() << dendl;
_fault();
} catch (const DecryptionError &) {
lderr(cct) << __func__ << " failed to decrypt frame payload" << dendl;
}
}
#define WRITE(B, D, C) write(D, CONTINUATION(C), B)
#define READ(L, C) read(CONTINUATION(C), buffer::ptr_node::create(buffer::create(L)))
#define READ_RXBUF(B, C) read(CONTINUATION(C), B)
#ifdef UNIT_TESTS_BUILT
#define INTERCEPT(S) { \
if(connection->interceptor) { \
auto a = connection->interceptor->intercept(connection, (S)); \
if (a == Interceptor::ACTION::FAIL) { \
return _fault(); \
} else if (a == Interceptor::ACTION::STOP) { \
stop(); \
connection->dispatch_queue->queue_reset(connection); \
return nullptr; \
}}}
#else
#define INTERCEPT(S)
#endif
ProtocolV2::ProtocolV2(AsyncConnection *connection)
: Protocol(2, connection),
state(NONE),
peer_required_features(0),
client_cookie(0),
server_cookie(0),
global_seq(0),
connect_seq(0),
peer_global_seq(0),
message_seq(0),
reconnecting(false),
replacing(false),
can_write(false),
bannerExchangeCallback(nullptr),
next_tag(static_cast<Tag>(0)),
keepalive(false) {
}
ProtocolV2::~ProtocolV2() {
}
void ProtocolV2::connect() {
ldout(cct, 1) << __func__ << dendl;
state = START_CONNECT;
pre_auth.enabled = true;
}
void ProtocolV2::accept() {
ldout(cct, 1) << __func__ << dendl;
state = START_ACCEPT;
}
bool ProtocolV2::is_connected() { return can_write; }
/*
* Tears down the message queues, and removes them from the
* DispatchQueue Must hold write_lock prior to calling.
*/
void ProtocolV2::discard_out_queue() {
ldout(cct, 10) << __func__ << " started" << dendl;
for (list<Message *>::iterator p = sent.begin(); p != sent.end(); ++p) {
ldout(cct, 20) << __func__ << " discard " << *p << dendl;
(*p)->put();
}
sent.clear();
for (auto& [ prio, entries ] : out_queue) {
static_cast<void>(prio);
for (auto& entry : entries) {
ldout(cct, 20) << __func__ << " discard " << *entry.m << dendl;
entry.m->put();
}
}
out_queue.clear();
write_in_progress = false;
}
void ProtocolV2::reset_session() {
ldout(cct, 1) << __func__ << dendl;
std::lock_guard<std::mutex> l(connection->write_lock);
if (connection->delay_state) {
connection->delay_state->discard();
}
connection->dispatch_queue->discard_queue(connection->conn_id);
discard_out_queue();
connection->outgoing_bl.clear();
connection->dispatch_queue->queue_remote_reset(connection);
out_seq = 0;
in_seq = 0;
client_cookie = 0;
server_cookie = 0;
connect_seq = 0;
peer_global_seq = 0;
message_seq = 0;
ack_left = 0;
can_write = false;
}
void ProtocolV2::stop() {
ldout(cct, 1) << __func__ << dendl;
if (state == CLOSED) {
return;
}
if (connection->delay_state) connection->delay_state->flush();
std::lock_guard<std::mutex> l(connection->write_lock);
reset_recv_state();
discard_out_queue();
connection->_stop();
can_write = false;
state = CLOSED;
}
void ProtocolV2::fault() { _fault(); }
void ProtocolV2::requeue_sent() {
write_in_progress = false;
if (sent.empty()) {
return;
}
auto& rq = out_queue[CEPH_MSG_PRIO_HIGHEST];
out_seq -= sent.size();
while (!sent.empty()) {
Message *m = sent.back();
sent.pop_back();
ldout(cct, 5) << __func__ << " requeueing message m=" << m
<< " seq=" << m->get_seq() << " type=" << m->get_type() << " "
<< *m << dendl;
rq.emplace_front(out_queue_entry_t{false, m});
}
}
uint64_t ProtocolV2::discard_requeued_up_to(uint64_t out_seq, uint64_t seq) {
ldout(cct, 10) << __func__ << " " << seq << dendl;
std::lock_guard<std::mutex> l(connection->write_lock);
if (out_queue.count(CEPH_MSG_PRIO_HIGHEST) == 0) {
return seq;
}
auto& rq = out_queue[CEPH_MSG_PRIO_HIGHEST];
uint64_t count = out_seq;
while (!rq.empty()) {
Message* const m = rq.front().m;
if (m->get_seq() == 0 || m->get_seq() > seq) break;
ldout(cct, 5) << __func__ << " discarding message m=" << m
<< " seq=" << m->get_seq() << " ack_seq=" << seq << " "
<< *m << dendl;
m->put();
rq.pop_front();
count++;
}
if (rq.empty()) out_queue.erase(CEPH_MSG_PRIO_HIGHEST);
return count;
}
void ProtocolV2::reset_recv_state() {
auth_meta.reset(new AuthConnectionMeta);
session_stream_handlers.tx.reset(nullptr);
session_stream_handlers.rx.reset(nullptr);
pre_auth.txbuf.clear();
pre_auth.rxbuf.clear();
// clean read and write callbacks
connection->pendingReadLen.reset();
connection->writeCallback.reset();
next_tag = static_cast<Tag>(0);
reset_throttle();
}
size_t ProtocolV2::get_current_msg_size() const {
ceph_assert(!rx_segments_desc.empty());
size_t sum = 0;
// we don't include SegmentIndex::Msg::HEADER.
for (__u8 idx = 1; idx < rx_segments_desc.size(); idx++) {
sum += rx_segments_desc[idx].length;
}
return sum;
}
void ProtocolV2::reset_throttle() {
if (state > THROTTLE_MESSAGE && state <= THROTTLE_DONE &&
connection->policy.throttler_messages) {
ldout(cct, 10) << __func__ << " releasing " << 1
<< " message to policy throttler "
<< connection->policy.throttler_messages->get_current()
<< "/" << connection->policy.throttler_messages->get_max()
<< dendl;
connection->policy.throttler_messages->put();
}
if (state > THROTTLE_BYTES && state <= THROTTLE_DONE) {
if (connection->policy.throttler_bytes) {
const size_t cur_msg_size = get_current_msg_size();
ldout(cct, 10) << __func__ << " releasing " << cur_msg_size
<< " bytes to policy throttler "
<< connection->policy.throttler_bytes->get_current() << "/"
<< connection->policy.throttler_bytes->get_max() << dendl;
connection->policy.throttler_bytes->put(cur_msg_size);
}
}
if (state > THROTTLE_DISPATCH_QUEUE && state <= THROTTLE_DONE) {
const size_t cur_msg_size = get_current_msg_size();
ldout(cct, 10)
<< __func__ << " releasing " << cur_msg_size
<< " bytes to dispatch_queue throttler "
<< connection->dispatch_queue->dispatch_throttler.get_current() << "/"
<< connection->dispatch_queue->dispatch_throttler.get_max() << dendl;
connection->dispatch_queue->dispatch_throttle_release(cur_msg_size);
}
}
CtPtr ProtocolV2::_fault() {
ldout(cct, 10) << __func__ << dendl;
if (state == CLOSED || state == NONE) {
ldout(cct, 10) << __func__ << " connection is already closed" << dendl;
return nullptr;
}
if (connection->policy.lossy &&
!(state >= START_CONNECT && state <= SESSION_RECONNECTING)) {
ldout(cct, 2) << __func__ << " on lossy channel, failing" << dendl;
stop();
connection->dispatch_queue->queue_reset(connection);
return nullptr;
}
connection->write_lock.lock();
can_write = false;
// requeue sent items
requeue_sent();
if (out_queue.empty() && state >= START_ACCEPT &&
state <= SESSION_ACCEPTING && !replacing) {
ldout(cct, 2) << __func__ << " with nothing to send and in the half "
<< " accept state just closed" << dendl;
connection->write_lock.unlock();
stop();
connection->dispatch_queue->queue_reset(connection);
return nullptr;
}
replacing = false;
connection->fault();
reset_recv_state();
reconnecting = false;
if (connection->policy.standby && out_queue.empty() && !keepalive &&
state != WAIT) {
ldout(cct, 1) << __func__ << " with nothing to send, going to standby"
<< dendl;
state = STANDBY;
connection->write_lock.unlock();
return nullptr;
}
if (connection->policy.server) {
ldout(cct, 1) << __func__ << " server, going to standby, even though i have stuff queued" << dendl;
state = STANDBY;
connection->write_lock.unlock();
return nullptr;
}
connection->write_lock.unlock();
if (!(state >= START_CONNECT && state <= SESSION_RECONNECTING) &&
state != WAIT &&
state != SESSION_ACCEPTING /* due to connection race */) {
// policy maybe empty when state is in accept
if (connection->policy.server) {
ldout(cct, 1) << __func__ << " server, going to standby" << dendl;
state = STANDBY;
} else {
ldout(cct, 1) << __func__ << " initiating reconnect" << dendl;
connect_seq++;
global_seq = messenger->get_global_seq();
state = START_CONNECT;
pre_auth.enabled = true;
connection->state = AsyncConnection::STATE_CONNECTING;
}
backoff = utime_t();
connection->center->dispatch_event_external(connection->read_handler);
} else {
if (state == WAIT) {
backoff.set_from_double(cct->_conf->ms_max_backoff);
} else if (backoff == utime_t()) {
backoff.set_from_double(cct->_conf->ms_initial_backoff);
} else {
backoff += backoff;
if (backoff > cct->_conf->ms_max_backoff)
backoff.set_from_double(cct->_conf->ms_max_backoff);
}
if (server_cookie) {
connect_seq++;
}
global_seq = messenger->get_global_seq();
state = START_CONNECT;
pre_auth.enabled = true;
connection->state = AsyncConnection::STATE_CONNECTING;
ldout(cct, 1) << __func__ << " waiting " << backoff << dendl;
// woke up again;
connection->register_time_events.insert(
connection->center->create_time_event(backoff.to_nsec() / 1000,
connection->wakeup_handler));
}
return nullptr;
}
void ProtocolV2::prepare_send_message(uint64_t features,
Message *m) {
ldout(cct, 20) << __func__ << " m=" << *m << dendl;
// associate message with Connection (for benefit of encode_payload)
if (m->empty_payload()) {
ldout(cct, 20) << __func__ << " encoding features " << features << " " << m
<< " " << *m << dendl;
} else {
ldout(cct, 20) << __func__ << " half-reencoding features " << features
<< " " << m << " " << *m << dendl;
}
// encode and copy out of *m
m->encode(features, 0);
}
void ProtocolV2::send_message(Message *m) {
uint64_t f = connection->get_features();
// TODO: Currently not all messages supports reencode like MOSDMap, so here
// only let fast dispatch support messages prepare message
const bool can_fast_prepare = messenger->ms_can_fast_dispatch(m);
if (can_fast_prepare) {
prepare_send_message(f, m);
}
std::lock_guard<std::mutex> l(connection->write_lock);
bool is_prepared = can_fast_prepare;
// "features" changes will change the payload encoding
if (can_fast_prepare && (!can_write || connection->get_features() != f)) {
// ensure the correctness of message encoding
m->clear_payload();
is_prepared = false;
ldout(cct, 10) << __func__ << " clear encoded buffer previous " << f
<< " != " << connection->get_features() << dendl;
}
if (state == CLOSED) {
ldout(cct, 10) << __func__ << " connection closed."
<< " Drop message " << m << dendl;
m->put();
} else {
ldout(cct, 5) << __func__ << " enqueueing message m=" << m
<< " type=" << m->get_type() << " " << *m << dendl;
m->trace.event("async enqueueing message");
out_queue[m->get_priority()].emplace_back(
out_queue_entry_t{is_prepared, m});
ldout(cct, 15) << __func__ << " inline write is denied, reschedule m=" << m
<< dendl;
if (((!replacing && can_write) || state == STANDBY) && !write_in_progress) {
write_in_progress = true;
connection->center->dispatch_event_external(connection->write_handler);
}
}
}
void ProtocolV2::send_keepalive() {
ldout(cct, 10) << __func__ << dendl;
std::lock_guard<std::mutex> l(connection->write_lock);
if (state != CLOSED) {
keepalive = true;
connection->center->dispatch_event_external(connection->write_handler);
}
}
void ProtocolV2::read_event() {
ldout(cct, 20) << __func__ << dendl;
switch (state) {
case START_CONNECT:
run_continuation(CONTINUATION(start_client_banner_exchange));
break;
case START_ACCEPT:
run_continuation(CONTINUATION(start_server_banner_exchange));
break;
case READY:
run_continuation(CONTINUATION(read_frame));
break;
case THROTTLE_MESSAGE:
run_continuation(CONTINUATION(throttle_message));
break;
case THROTTLE_BYTES:
run_continuation(CONTINUATION(throttle_bytes));
break;
case THROTTLE_DISPATCH_QUEUE:
run_continuation(CONTINUATION(throttle_dispatch_queue));
break;
default:
break;
}
}
ProtocolV2::out_queue_entry_t ProtocolV2::_get_next_outgoing() {
out_queue_entry_t out_entry;
if (!out_queue.empty()) {
auto it = out_queue.rbegin();
auto& entries = it->second;
ceph_assert(!entries.empty());
out_entry = entries.front();
entries.pop_front();
if (entries.empty()) {
out_queue.erase(it->first);
}
}
return out_entry;
}
ssize_t ProtocolV2::write_message(Message *m, bool more) {
FUNCTRACE(cct);
ceph_assert(connection->center->in_thread());
m->set_seq(++out_seq);
connection->lock.lock();
uint64_t ack_seq = in_seq;
ack_left = 0;
connection->lock.unlock();
ceph_msg_header &header = m->get_header();
ceph_msg_footer &footer = m->get_footer();
ceph_msg_header2 header2{header.seq, header.tid,
header.type, header.priority,
header.version,
init_le32(0), header.data_off,
init_le64(ack_seq),
footer.flags, header.compat_version,
header.reserved};
auto message = MessageFrame::Encode(
header2,
m->get_payload(),
m->get_middle(),
m->get_data());
if (!append_frame(message)) {
m->put();
return -EILSEQ;
}
ldout(cct, 5) << __func__ << " sending message m=" << m
<< " seq=" << m->get_seq() << " " << *m << dendl;
m->trace.event("async writing message");
ldout(cct, 20) << __func__ << " sending m=" << m << " seq=" << m->get_seq()
<< " src=" << entity_name_t(messenger->get_myname())
<< " off=" << header2.data_off
<< dendl;
ssize_t total_send_size = connection->outgoing_bl.length();
ssize_t rc = connection->_try_send(more);
if (rc < 0) {
ldout(cct, 1) << __func__ << " error sending " << m << ", "
<< cpp_strerror(rc) << dendl;
} else {
connection->logger->inc(
l_msgr_send_bytes, total_send_size - connection->outgoing_bl.length());
ldout(cct, 10) << __func__ << " sending " << m
<< (rc ? " continuely." : " done.") << dendl;
}
if (m->get_type() == CEPH_MSG_OSD_OP)
OID_EVENT_TRACE_WITH_MSG(m, "SEND_MSG_OSD_OP_END", false);
else if (m->get_type() == CEPH_MSG_OSD_OPREPLY)
OID_EVENT_TRACE_WITH_MSG(m, "SEND_MSG_OSD_OPREPLY_END", false);
m->put();
return rc;
}
template <class F>
bool ProtocolV2::append_frame(F& frame) {
ceph::bufferlist bl;
try {
bl = frame.get_buffer(session_stream_handlers);
} catch (ceph::crypto::onwire::TxHandlerError &e) {
ldout(cct, 1) << __func__ << " " << e.what() << dendl;
return false;
}
connection->outgoing_bl.append(bl);
return true;
}
void ProtocolV2::handle_message_ack(uint64_t seq) {
if (connection->policy.lossy) { // lossy connections don't keep sent messages
return;
}
ldout(cct, 15) << __func__ << " seq=" << seq << dendl;
// trim sent list
static const int max_pending = 128;
int i = 0;
Message *pending[max_pending];
connection->write_lock.lock();
while (!sent.empty() && sent.front()->get_seq() <= seq && i < max_pending) {
Message *m = sent.front();
sent.pop_front();
pending[i++] = m;
ldout(cct, 10) << __func__ << " got ack seq " << seq
<< " >= " << m->get_seq() << " on " << m << " " << *m
<< dendl;
}
connection->write_lock.unlock();
for (int k = 0; k < i; k++) {
pending[k]->put();
}
}
void ProtocolV2::write_event() {
ldout(cct, 10) << __func__ << dendl;
ssize_t r = 0;
connection->write_lock.lock();
if (can_write) {
if (keepalive) {
ldout(cct, 10) << __func__ << " appending keepalive" << dendl;
auto keepalive_frame = KeepAliveFrame::Encode();
if (!append_frame(keepalive_frame)) {
connection->write_lock.unlock();
connection->lock.lock();
fault();
connection->lock.unlock();
return;
}
keepalive = false;
}
auto start = ceph::mono_clock::now();
bool more;
do {
const auto out_entry = _get_next_outgoing();
if (!out_entry.m) {
break;
}
if (!connection->policy.lossy) {
// put on sent list
sent.push_back(out_entry.m);
out_entry.m->get();
}
more = !out_queue.empty();
connection->write_lock.unlock();
// send_message or requeue messages may not encode message
if (!out_entry.is_prepared) {
prepare_send_message(connection->get_features(), out_entry.m);
}
r = write_message(out_entry.m, more);
connection->write_lock.lock();
if (r == 0) {
;
} else if (r < 0) {
ldout(cct, 1) << __func__ << " send msg failed" << dendl;
break;
} else if (r > 0)
break;
} while (can_write);
write_in_progress = false;
// if r > 0 mean data still lefted, so no need _try_send.
if (r == 0) {
uint64_t left = ack_left;
if (left) {
ceph_le64 s;
s = in_seq;
ldout(cct, 10) << __func__ << " try send msg ack, acked " << left
<< " messages" << dendl;
auto ack_frame = AckFrame::Encode(in_seq);
if (append_frame(ack_frame)) {
ack_left -= left;
left = ack_left;
r = connection->_try_send(left);
} else {
r = -EILSEQ;
}
} else if (is_queued()) {
r = connection->_try_send();
}
}
connection->write_lock.unlock();
connection->logger->tinc(l_msgr_running_send_time,
ceph::mono_clock::now() - start);
if (r < 0) {
ldout(cct, 1) << __func__ << " send msg failed" << dendl;
connection->lock.lock();
fault();
connection->lock.unlock();
return;
}
} else {
write_in_progress = false;
connection->write_lock.unlock();
connection->lock.lock();
connection->write_lock.lock();
if (state == STANDBY && !connection->policy.server && is_queued()) {
ldout(cct, 10) << __func__ << " policy.server is false" << dendl;
if (server_cookie) { // only increment connect_seq if there is a session
connect_seq++;
}
connection->_connect();
} else if (connection->cs && state != NONE && state != CLOSED &&
state != START_CONNECT) {
r = connection->_try_send();
if (r < 0) {
ldout(cct, 1) << __func__ << " send outcoming bl failed" << dendl;
connection->write_lock.unlock();
fault();
connection->lock.unlock();
return;
}
}
connection->write_lock.unlock();
connection->lock.unlock();
}
}
bool ProtocolV2::is_queued() {
return !out_queue.empty() || connection->is_queued();
}
uint32_t ProtocolV2::get_onwire_size(const uint32_t logical_size) const {
if (session_stream_handlers.rx) {
return segment_onwire_size(logical_size);
} else {
return logical_size;
}
}
uint32_t ProtocolV2::get_epilogue_size() const {
// In secure mode size of epilogue is flexible and depends on particular
// cipher implementation. See the comment for epilogue_secure_block_t or
// epilogue_plain_block_t.
if (session_stream_handlers.rx) {
return FRAME_SECURE_EPILOGUE_SIZE + \
session_stream_handlers.rx->get_extra_size_at_final();
} else {
return FRAME_PLAIN_EPILOGUE_SIZE;
}
}
CtPtr ProtocolV2::read(CONTINUATION_RXBPTR_TYPE<ProtocolV2> &next,
rx_buffer_t &&buffer) {
const auto len = buffer->length();
const auto buf = buffer->c_str();
next.node = std::move(buffer);
ssize_t r = connection->read(len, buf,
[&next, this](char *buffer, int r) {
if (unlikely(pre_auth.enabled) && r >= 0) {
pre_auth.rxbuf.append(*next.node);
ceph_assert(!cct->_conf->ms_die_on_bug ||
pre_auth.rxbuf.length() < 1000000);
}
next.r = r;
run_continuation(next);
});
if (r <= 0) {
// error or done synchronously
if (unlikely(pre_auth.enabled) && r >= 0) {
pre_auth.rxbuf.append(*next.node);
ceph_assert(!cct->_conf->ms_die_on_bug ||
pre_auth.rxbuf.length() < 1000000);
}
next.r = r;
return &next;
}
return nullptr;
}
template <class F>
CtPtr ProtocolV2::write(const std::string &desc,
CONTINUATION_TYPE<ProtocolV2> &next,
F &frame) {
ceph::bufferlist bl;
try {
bl = frame.get_buffer(session_stream_handlers);
} catch (ceph::crypto::onwire::TxHandlerError &e) {
ldout(cct, 1) << __func__ << " " << e.what() << dendl;
return _fault();
}
return write(desc, next, bl);
}
CtPtr ProtocolV2::write(const std::string &desc,
CONTINUATION_TYPE<ProtocolV2> &next,
bufferlist &buffer) {
if (unlikely(pre_auth.enabled)) {
pre_auth.txbuf.append(buffer);
ceph_assert(!cct->_conf->ms_die_on_bug ||
pre_auth.txbuf.length() < 1000000);
}
ssize_t r =
connection->write(buffer, [&next, desc, this](int r) {
if (r < 0) {
ldout(cct, 1) << __func__ << " " << desc << " write failed r=" << r
<< " (" << cpp_strerror(r) << ")" << dendl;
connection->inject_delay();
_fault();
}
run_continuation(next);
});
if (r < 0) {
ldout(cct, 1) << __func__ << " " << desc << " write failed r=" << r
<< " (" << cpp_strerror(r) << ")" << dendl;
return _fault();
} else if (r == 0) {
next.setParams();
return &next;
}
return nullptr;
}
CtPtr ProtocolV2::_banner_exchange(CtRef callback) {
ldout(cct, 20) << __func__ << dendl;
bannerExchangeCallback = &callback;
bufferlist banner_payload;
encode((uint64_t)CEPH_MSGR2_SUPPORTED_FEATURES, banner_payload, 0);
encode((uint64_t)CEPH_MSGR2_REQUIRED_FEATURES, banner_payload, 0);
bufferlist bl;
bl.append(CEPH_BANNER_V2_PREFIX, strlen(CEPH_BANNER_V2_PREFIX));
encode((uint16_t)banner_payload.length(), bl, 0);
bl.claim_append(banner_payload);
INTERCEPT(state == BANNER_CONNECTING ? 3 : 4);
return WRITE(bl, "banner", _wait_for_peer_banner);
}
CtPtr ProtocolV2::_wait_for_peer_banner() {
unsigned banner_len = strlen(CEPH_BANNER_V2_PREFIX) + sizeof(__le16);
return READ(banner_len, _handle_peer_banner);
}
CtPtr ProtocolV2::_handle_peer_banner(rx_buffer_t &&buffer, int r) {
ldout(cct, 20) << __func__ << " r=" << r << dendl;
if (r < 0) {
ldout(cct, 1) << __func__ << " read peer banner failed r=" << r << " ("
<< cpp_strerror(r) << ")" << dendl;
return _fault();
}
unsigned banner_prefix_len = strlen(CEPH_BANNER_V2_PREFIX);
if (memcmp(buffer->c_str(), CEPH_BANNER_V2_PREFIX, banner_prefix_len)) {
if (memcmp(buffer->c_str(), CEPH_BANNER, strlen(CEPH_BANNER)) == 0) {
lderr(cct) << __func__ << " peer " << *connection->peer_addrs
<< " is using msgr V1 protocol" << dendl;
return _fault();
}
ldout(cct, 1) << __func__ << " accept peer sent bad banner" << dendl;
return _fault();
}
uint16_t payload_len;
bufferlist bl;
buffer->set_offset(banner_prefix_len);
buffer->set_length(sizeof(__le16));
bl.push_back(std::move(buffer));
auto ti = bl.cbegin();
try {
decode(payload_len, ti);
} catch (const buffer::error &e) {
lderr(cct) << __func__ << " decode banner payload len failed " << dendl;
return _fault();
}
INTERCEPT(state == BANNER_CONNECTING ? 5 : 6);
return READ(payload_len, _handle_peer_banner_payload);
}
CtPtr ProtocolV2::_handle_peer_banner_payload(rx_buffer_t &&buffer, int r) {
ldout(cct, 20) << __func__ << " r=" << r << dendl;
if (r < 0) {
ldout(cct, 1) << __func__ << " read peer banner payload failed r=" << r
<< " (" << cpp_strerror(r) << ")" << dendl;
return _fault();
}
uint64_t peer_supported_features;
uint64_t peer_required_features;
bufferlist bl;
bl.push_back(std::move(buffer));
auto ti = bl.cbegin();
try {
decode(peer_supported_features, ti);
decode(peer_required_features, ti);
} catch (const buffer::error &e) {
lderr(cct) << __func__ << " decode banner payload failed " << dendl;
return _fault();
}
ldout(cct, 1) << __func__ << " supported=" << std::hex
<< peer_supported_features << " required=" << std::hex
<< peer_required_features << std::dec << dendl;
// Check feature bit compatibility
uint64_t supported_features = CEPH_MSGR2_SUPPORTED_FEATURES;
uint64_t required_features = CEPH_MSGR2_REQUIRED_FEATURES;
if ((required_features & peer_supported_features) != required_features) {
ldout(cct, 1) << __func__ << " peer does not support all required features"
<< " required=" << std::hex << required_features
<< " supported=" << std::hex << peer_supported_features
<< std::dec << dendl;
stop();
connection->dispatch_queue->queue_reset(connection);
return nullptr;
}
if ((supported_features & peer_required_features) != peer_required_features) {
ldout(cct, 1) << __func__ << " we do not support all peer required features"
<< " required=" << std::hex << peer_required_features
<< " supported=" << supported_features << std::dec << dendl;
stop();
connection->dispatch_queue->queue_reset(connection);
return nullptr;
}
this->peer_required_features = peer_required_features;
if (this->peer_required_features == 0) {
this->connection_features = msgr2_required;
}
// at this point we can change how the client protocol behaves based on
// this->peer_required_features
if (state == BANNER_CONNECTING) {
state = HELLO_CONNECTING;
}
else {
ceph_assert(state == BANNER_ACCEPTING);
state = HELLO_ACCEPTING;
}
auto hello = HelloFrame::Encode(messenger->get_mytype(),
connection->target_addr);
INTERCEPT(state == HELLO_CONNECTING ? 7 : 8);
return WRITE(hello, "hello frame", read_frame);
}
CtPtr ProtocolV2::handle_hello(ceph::bufferlist &payload)
{
ldout(cct, 20) << __func__
<< " payload.length()=" << payload.length() << dendl;
if (state != HELLO_CONNECTING && state != HELLO_ACCEPTING) {
lderr(cct) << __func__ << " not in hello exchange state!" << dendl;
return _fault();
}
auto hello = HelloFrame::Decode(payload);
ldout(cct, 5) << __func__ << " received hello:"
<< " peer_type=" << (int)hello.entity_type()
<< " peer_addr_for_me=" << hello.peer_addr() << dendl;
sockaddr_storage ss;
socklen_t len = sizeof(ss);
getsockname(connection->cs.fd(), (sockaddr *)&ss, &len);
ldout(cct, 5) << __func__ << " getsockname says I am " << (sockaddr *)&ss
<< " when talking to " << connection->target_addr << dendl;
if (connection->get_peer_type() == -1) {
connection->set_peer_type(hello.entity_type());
ceph_assert(state == HELLO_ACCEPTING);
connection->policy = messenger->get_policy(hello.entity_type());
ldout(cct, 10) << __func__ << " accept of host_type "
<< (int)hello.entity_type()
<< ", policy.lossy=" << connection->policy.lossy
<< " policy.server=" << connection->policy.server
<< " policy.standby=" << connection->policy.standby
<< " policy.resetcheck=" << connection->policy.resetcheck
<< dendl;
} else {
ceph_assert(state == HELLO_CONNECTING);
if (connection->get_peer_type() != hello.entity_type()) {
ldout(cct, 1) << __func__ << " connection peer type does not match what"
<< " peer advertises " << connection->get_peer_type()
<< " != " << (int)hello.entity_type() << dendl;
stop();
connection->dispatch_queue->queue_reset(connection);
return nullptr;
}
}
if (messenger->get_myaddrs().empty() ||
messenger->get_myaddrs().front().is_blank_ip()) {
entity_addr_t a;
if (cct->_conf->ms_learn_addr_from_peer) {
ldout(cct, 1) << __func__ << " peer " << connection->target_addr
<< " says I am " << hello.peer_addr() << " (socket says "
<< (sockaddr*)&ss << ")" << dendl;
a = hello.peer_addr();
} else {
ldout(cct, 1) << __func__ << " socket to " << connection->target_addr
<< " says I am " << (sockaddr*)&ss
<< " (peer says " << hello.peer_addr() << ")" << dendl;
a.set_sockaddr((sockaddr *)&ss);
}
a.set_type(entity_addr_t::TYPE_MSGR2); // anything but NONE; learned_addr ignores this
a.set_port(0);
connection->lock.unlock();
messenger->learned_addr(a);
if (cct->_conf->ms_inject_internal_delays &&
cct->_conf->ms_inject_socket_failures) {
if (rand() % cct->_conf->ms_inject_socket_failures == 0) {
ldout(cct, 10) << __func__ << " sleep for "