/
ProtocolV1.cc
2495 lines (2110 loc) · 82.1 KB
/
ProtocolV1.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 "ProtocolV1.h"
#include "common/errno.h"
#include "AsyncConnection.h"
#include "AsyncMessenger.h"
#include "common/EventTrace.h"
#include "include/random.h"
#define dout_subsys ceph_subsys_ms
#undef dout_prefix
#define dout_prefix _conn_prefix(_dout)
ostream &ProtocolV1::_conn_prefix(std::ostream *_dout) {
return *_dout << "--1- " << messenger->get_myaddrs() << " >> "
<< *connection->peer_addrs
<< " conn("
<< connection << " " << this
<< " :" << connection->port << " s=" << get_state_name(state)
<< " pgs=" << peer_global_seq << " cs=" << connect_seq
<< " l=" << connection->policy.lossy << ").";
}
#define WRITE(B, C) write(CONTINUATION(C), B)
#define READ(L, C) read(CONTINUATION(C), L)
#define READB(L, B, C) read(CONTINUATION(C), L, B)
// Constant to limit starting sequence number to 2^31. Nothing special about
// it, just a big number. PLR
#define SEQ_MASK 0x7fffffff
const int ASYNC_COALESCE_THRESHOLD = 256;
using namespace std;
static void alloc_aligned_buffer(bufferlist &data, unsigned len, unsigned off) {
// create a buffer to read into that matches the data alignment
unsigned alloc_len = 0;
unsigned left = len;
unsigned head = 0;
if (off & ~CEPH_PAGE_MASK) {
// head
alloc_len += CEPH_PAGE_SIZE;
head = std::min<uint64_t>(CEPH_PAGE_SIZE - (off & ~CEPH_PAGE_MASK), left);
left -= head;
}
alloc_len += left;
bufferptr ptr(buffer::create_small_page_aligned(alloc_len));
if (head) ptr.set_offset(CEPH_PAGE_SIZE - head);
data.push_back(std::move(ptr));
}
/**
* Protocol V1
**/
ProtocolV1::ProtocolV1(AsyncConnection *connection)
: Protocol(1, connection),
temp_buffer(nullptr),
can_write(WriteStatus::NOWRITE),
keepalive(false),
connect_seq(0),
peer_global_seq(0),
msg_left(0),
cur_msg_size(0),
replacing(false),
is_reset_from_peer(false),
once_ready(false),
state(NONE),
global_seq(0),
authorizer(nullptr),
wait_for_seq(false) {
temp_buffer = new char[4096];
}
ProtocolV1::~ProtocolV1() {
ceph_assert(out_q.empty());
ceph_assert(sent.empty());
delete[] temp_buffer;
if (authorizer) {
delete authorizer;
}
}
void ProtocolV1::connect() {
this->state = START_CONNECT;
// reset connect state variables
if (authorizer) {
delete authorizer;
authorizer = nullptr;
}
authorizer_buf.clear();
memset(&connect_msg, 0, sizeof(connect_msg));
memset(&connect_reply, 0, sizeof(connect_reply));
global_seq = messenger->get_global_seq();
}
void ProtocolV1::accept() { this->state = START_ACCEPT; }
bool ProtocolV1::is_connected() {
return can_write.load() == WriteStatus::CANWRITE;
}
void ProtocolV1::stop() {
ldout(cct, 20) << __func__ << dendl;
if (state == CLOSED) {
return;
}
if (connection->delay_state) connection->delay_state->flush();
ldout(cct, 2) << __func__ << dendl;
std::lock_guard<std::mutex> l(connection->write_lock);
reset_recv_state();
discard_out_queue();
connection->_stop();
can_write = WriteStatus::CLOSED;
state = CLOSED;
}
void ProtocolV1::fault() {
ldout(cct, 20) << __func__ << dendl;
if (state == CLOSED || state == NONE) {
ldout(cct, 10) << __func__ << " connection is already closed" << dendl;
return;
}
if (connection->policy.lossy && state != START_CONNECT &&
state != CONNECTING) {
ldout(cct, 1) << __func__ << " on lossy channel, failing" << dendl;
stop();
connection->dispatch_queue->queue_reset(connection);
return;
}
connection->write_lock.lock();
can_write = WriteStatus::NOWRITE;
is_reset_from_peer = false;
// requeue sent items
requeue_sent();
if (!once_ready && out_q.empty() && state >= START_ACCEPT &&
state <= ACCEPTING_WAIT_CONNECT_MSG_AUTH && !replacing) {
ldout(cct, 10) << __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;
}
replacing = false;
connection->fault();
reset_recv_state();
if (connection->policy.standby && out_q.empty() && !keepalive &&
state != WAIT) {
ldout(cct, 10) << __func__ << " with nothing to send, going to standby"
<< dendl;
state = STANDBY;
connection->write_lock.unlock();
return;
}
connection->write_lock.unlock();
if ((state >= START_CONNECT && state <= CONNECTING_SEND_CONNECT_MSG) ||
state == WAIT) {
// backoff!
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);
}
global_seq = messenger->get_global_seq();
state = START_CONNECT;
connection->state = AsyncConnection::STATE_CONNECTING;
ldout(cct, 10) << __func__ << " waiting " << backoff << dendl;
// woke up again;
connection->register_time_events.insert(
connection->center->create_time_event(backoff.to_nsec() / 1000,
connection->wakeup_handler));
} else {
// policy maybe empty when state is in accept
if (connection->policy.server) {
ldout(cct, 0) << __func__ << " server, going to standby" << dendl;
state = STANDBY;
} else {
ldout(cct, 0) << __func__ << " initiating reconnect" << dendl;
connect_seq++;
global_seq = messenger->get_global_seq();
state = START_CONNECT;
connection->state = AsyncConnection::STATE_CONNECTING;
}
backoff = utime_t();
connection->center->dispatch_event_external(connection->read_handler);
}
}
void ProtocolV1::send_message(Message *m) {
bufferlist bl;
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
bool can_fast_prepare = messenger->ms_can_fast_dispatch(m);
if (can_fast_prepare) {
prepare_send_message(f, m, bl);
}
std::lock_guard<std::mutex> l(connection->write_lock);
// "features" changes will change the payload encoding
if (can_fast_prepare &&
(can_write == WriteStatus::NOWRITE || connection->get_features() != f)) {
// ensure the correctness of message encoding
bl.clear();
m->clear_payload();
ldout(cct, 5) << __func__ << " clear encoded buffer previous " << f
<< " != " << connection->get_features() << dendl;
}
if (can_write == WriteStatus::CLOSED) {
ldout(cct, 10) << __func__ << " connection closed."
<< " Drop message " << m << dendl;
m->put();
} else {
m->trace.event("async enqueueing message");
out_q[m->get_priority()].emplace_back(std::move(bl), m);
ldout(cct, 15) << __func__ << " inline write is denied, reschedule m=" << m
<< dendl;
if (can_write != WriteStatus::REPLACING) {
connection->center->dispatch_event_external(connection->write_handler);
}
}
}
void ProtocolV1::prepare_send_message(uint64_t features, Message *m,
bufferlist &bl) {
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, messenger->crcflags);
bl.append(m->get_payload());
bl.append(m->get_middle());
bl.append(m->get_data());
}
void ProtocolV1::send_keepalive() {
ldout(cct, 10) << __func__ << dendl;
std::lock_guard<std::mutex> l(connection->write_lock);
if (can_write != WriteStatus::CLOSED) {
keepalive = true;
connection->center->dispatch_event_external(connection->write_handler);
}
}
void ProtocolV1::read_event() {
ldout(cct, 20) << __func__ << dendl;
switch (state) {
case START_CONNECT:
CONTINUATION_RUN(CONTINUATION(send_client_banner));
break;
case START_ACCEPT:
CONTINUATION_RUN(CONTINUATION(send_server_banner));
break;
case OPENED:
CONTINUATION_RUN(CONTINUATION(wait_message));
break;
case THROTTLE_MESSAGE:
CONTINUATION_RUN(CONTINUATION(throttle_message));
break;
case THROTTLE_BYTES:
CONTINUATION_RUN(CONTINUATION(throttle_bytes));
break;
case THROTTLE_DISPATCH_QUEUE:
CONTINUATION_RUN(CONTINUATION(throttle_dispatch_queue));
break;
default:
break;
}
}
void ProtocolV1::write_event() {
ldout(cct, 10) << __func__ << dendl;
ssize_t r = 0;
connection->write_lock.lock();
if (can_write == WriteStatus::CANWRITE) {
if (keepalive) {
append_keepalive_or_ack();
keepalive = false;
}
auto start = ceph::mono_clock::now();
bool more;
do {
bufferlist data;
Message *m = _get_next_outgoing(&data);
if (!m) {
break;
}
if (!connection->policy.lossy) {
// put on sent list
sent.push_back(m);
m->get();
}
more = !out_q.empty();
connection->write_lock.unlock();
// send_message or requeue messages may not encode message
if (!data.length()) {
prepare_send_message(connection->get_features(), m, data);
}
r = write_message(m, data, 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 == WriteStatus::CANWRITE);
connection->write_lock.unlock();
// 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;
connection->outcoming_bl.append(CEPH_MSGR_TAG_ACK);
connection->outcoming_bl.append((char *)&s, sizeof(s));
ldout(cct, 10) << __func__ << " try send msg ack, acked " << left
<< " messages" << dendl;
ack_left -= left;
left = ack_left;
r = connection->_try_send(left);
} else if (is_queued()) {
r = connection->_try_send();
}
}
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 {
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;
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 ProtocolV1::is_queued() {
return !out_q.empty() || connection->is_queued();
}
void ProtocolV1::run_continuation(CtPtr pcontinuation) {
if (pcontinuation) {
CONTINUATION_RUN(*pcontinuation);
}
}
CtPtr ProtocolV1::read(CONTINUATION_RX_TYPE<ProtocolV1> &next,
int len, char *buffer) {
if (!buffer) {
buffer = temp_buffer;
}
ssize_t r = connection->read(len, buffer,
[&next, this](char *buffer, int r) {
next.setParams(buffer, r);
CONTINUATION_RUN(next);
});
if (r <= 0) {
next.setParams(buffer, r);
return &next;
}
return nullptr;
}
CtPtr ProtocolV1::write(CONTINUATION_TX_TYPE<ProtocolV1> &next,
bufferlist &buffer) {
ssize_t r = connection->write(buffer, [&next, this](int r) {
next.setParams(r);
CONTINUATION_RUN(next);
});
if (r <= 0) {
next.setParams(r);
return &next;
}
return nullptr;
}
CtPtr ProtocolV1::ready() {
ldout(cct, 25) << __func__ << dendl;
// make sure no pending tick timer
if (connection->last_tick_id) {
connection->center->delete_time_event(connection->last_tick_id);
}
connection->last_tick_id = connection->center->create_time_event(
connection->inactive_timeout_us, connection->tick_handler);
connection->write_lock.lock();
can_write = WriteStatus::CANWRITE;
if (is_queued()) {
connection->center->dispatch_event_external(connection->write_handler);
}
connection->write_lock.unlock();
connection->maybe_start_delay_thread();
state = OPENED;
return wait_message();
}
CtPtr ProtocolV1::wait_message() {
if (state != OPENED) { // must have changed due to a replace
return nullptr;
}
ldout(cct, 20) << __func__ << dendl;
return READ(sizeof(char), handle_message);
}
CtPtr ProtocolV1::handle_message(char *buffer, int r) {
ldout(cct, 20) << __func__ << " r=" << r << dendl;
if (r < 0) {
ldout(cct, 1) << __func__ << " read tag failed" << dendl;
return _fault();
}
char tag = buffer[0];
ldout(cct, 20) << __func__ << " process tag " << (int)tag << dendl;
if (tag == CEPH_MSGR_TAG_KEEPALIVE) {
ldout(cct, 20) << __func__ << " got KEEPALIVE" << dendl;
connection->set_last_keepalive(ceph_clock_now());
} else if (tag == CEPH_MSGR_TAG_KEEPALIVE2) {
return READ(sizeof(ceph_timespec), handle_keepalive2);
} else if (tag == CEPH_MSGR_TAG_KEEPALIVE2_ACK) {
return READ(sizeof(ceph_timespec), handle_keepalive2_ack);
} else if (tag == CEPH_MSGR_TAG_ACK) {
return READ(sizeof(ceph_le64), handle_tag_ack);
} else if (tag == CEPH_MSGR_TAG_MSG) {
#if defined(WITH_LTTNG) && defined(WITH_EVENTTRACE)
ltt_recv_stamp = ceph_clock_now();
#endif
recv_stamp = ceph_clock_now();
ldout(cct, 20) << __func__ << " begin MSG" << dendl;
return READ(sizeof(ceph_msg_header), handle_message_header);
} else if (tag == CEPH_MSGR_TAG_CLOSE) {
ldout(cct, 20) << __func__ << " got CLOSE" << dendl;
stop();
} else {
ldout(cct, 0) << __func__ << " bad tag " << (int)tag << dendl;
return _fault();
}
return nullptr;
}
CtPtr ProtocolV1::handle_keepalive2(char *buffer, int r) {
ldout(cct, 20) << __func__ << " r=" << r << dendl;
if (r < 0) {
ldout(cct, 1) << __func__ << " read keeplive timespec failed" << dendl;
return _fault();
}
ldout(cct, 30) << __func__ << " got KEEPALIVE2 tag ..." << dendl;
ceph_timespec *t;
t = (ceph_timespec *)buffer;
utime_t kp_t = utime_t(*t);
connection->write_lock.lock();
append_keepalive_or_ack(true, &kp_t);
connection->write_lock.unlock();
ldout(cct, 20) << __func__ << " got KEEPALIVE2 " << kp_t << dendl;
connection->set_last_keepalive(ceph_clock_now());
if (is_connected()) {
connection->center->dispatch_event_external(connection->write_handler);
}
return CONTINUE(wait_message);
}
void ProtocolV1::append_keepalive_or_ack(bool ack, utime_t *tp) {
ldout(cct, 10) << __func__ << dendl;
if (ack) {
ceph_assert(tp);
struct ceph_timespec ts;
tp->encode_timeval(&ts);
connection->outcoming_bl.append(CEPH_MSGR_TAG_KEEPALIVE2_ACK);
connection->outcoming_bl.append((char *)&ts, sizeof(ts));
} else if (connection->has_feature(CEPH_FEATURE_MSGR_KEEPALIVE2)) {
struct ceph_timespec ts;
utime_t t = ceph_clock_now();
t.encode_timeval(&ts);
connection->outcoming_bl.append(CEPH_MSGR_TAG_KEEPALIVE2);
connection->outcoming_bl.append((char *)&ts, sizeof(ts));
} else {
connection->outcoming_bl.append(CEPH_MSGR_TAG_KEEPALIVE);
}
}
CtPtr ProtocolV1::handle_keepalive2_ack(char *buffer, int r) {
ldout(cct, 20) << __func__ << " r=" << r << dendl;
if (r < 0) {
ldout(cct, 1) << __func__ << " read keeplive timespec failed" << dendl;
return _fault();
}
ceph_timespec *t;
t = (ceph_timespec *)buffer;
connection->set_last_keepalive_ack(utime_t(*t));
ldout(cct, 20) << __func__ << " got KEEPALIVE_ACK" << dendl;
return CONTINUE(wait_message);
}
CtPtr ProtocolV1::handle_tag_ack(char *buffer, int r) {
ldout(cct, 20) << __func__ << " r=" << r << dendl;
if (r < 0) {
ldout(cct, 1) << __func__ << " read ack seq failed" << dendl;
return _fault();
}
ceph_le64 seq;
seq = *(ceph_le64 *)buffer;
ldout(cct, 20) << __func__ << " got ACK" << dendl;
ldout(cct, 15) << __func__ << " got ack 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();
}
return CONTINUE(wait_message);
}
CtPtr ProtocolV1::handle_message_header(char *buffer, int r) {
ldout(cct, 20) << __func__ << " r=" << r << dendl;
if (r < 0) {
ldout(cct, 1) << __func__ << " read message header failed" << dendl;
return _fault();
}
ldout(cct, 20) << __func__ << " got MSG header" << dendl;
current_header = *((ceph_msg_header *)buffer);
ldout(cct, 20) << __func__ << " got envelope type=" << current_header.type << " src "
<< entity_name_t(current_header.src) << " front=" << current_header.front_len
<< " data=" << current_header.data_len << " off " << current_header.data_off
<< dendl;
if (messenger->crcflags & MSG_CRC_HEADER) {
__u32 header_crc = 0;
header_crc = ceph_crc32c(0, (unsigned char *)¤t_header,
sizeof(current_header) - sizeof(current_header.crc));
// verify header crc
if (header_crc != current_header.crc) {
ldout(cct, 0) << __func__ << " got bad header crc " << header_crc
<< " != " << current_header.crc << dendl;
return _fault();
}
}
// Reset state
data_buf.clear();
front.clear();
middle.clear();
data.clear();
state = THROTTLE_MESSAGE;
return CONTINUE(throttle_message);
}
CtPtr ProtocolV1::throttle_message() {
ldout(cct, 20) << __func__ << dendl;
if (connection->policy.throttler_messages) {
ldout(cct, 10) << __func__ << " wants " << 1
<< " message from policy throttler "
<< connection->policy.throttler_messages->get_current()
<< "/" << connection->policy.throttler_messages->get_max()
<< dendl;
if (!connection->policy.throttler_messages->get_or_fail()) {
ldout(cct, 10) << __func__ << " wants 1 message from policy throttle "
<< connection->policy.throttler_messages->get_current()
<< "/" << connection->policy.throttler_messages->get_max()
<< " failed, just wait." << dendl;
// following thread pool deal with th full message queue isn't a
// short time, so we can wait a ms.
if (connection->register_time_events.empty()) {
connection->register_time_events.insert(
connection->center->create_time_event(1000,
connection->wakeup_handler));
}
return nullptr;
}
}
state = THROTTLE_BYTES;
return CONTINUE(throttle_bytes);
}
CtPtr ProtocolV1::throttle_bytes() {
ldout(cct, 20) << __func__ << dendl;
cur_msg_size = current_header.front_len + current_header.middle_len +
current_header.data_len;
if (cur_msg_size) {
if (connection->policy.throttler_bytes) {
ldout(cct, 10) << __func__ << " wants " << cur_msg_size
<< " bytes from policy throttler "
<< connection->policy.throttler_bytes->get_current() << "/"
<< connection->policy.throttler_bytes->get_max() << dendl;
if (!connection->policy.throttler_bytes->get_or_fail(cur_msg_size)) {
ldout(cct, 10) << __func__ << " wants " << cur_msg_size
<< " bytes from policy throttler "
<< connection->policy.throttler_bytes->get_current()
<< "/" << connection->policy.throttler_bytes->get_max()
<< " failed, just wait." << dendl;
// following thread pool deal with th full message queue isn't a
// short time, so we can wait a ms.
if (connection->register_time_events.empty()) {
connection->register_time_events.insert(
connection->center->create_time_event(
1000, connection->wakeup_handler));
}
return nullptr;
}
}
}
state = THROTTLE_DISPATCH_QUEUE;
return CONTINUE(throttle_dispatch_queue);
}
CtPtr ProtocolV1::throttle_dispatch_queue() {
ldout(cct, 20) << __func__ << dendl;
if (cur_msg_size) {
if (!connection->dispatch_queue->dispatch_throttler.get_or_fail(
cur_msg_size)) {
ldout(cct, 10)
<< __func__ << " wants " << cur_msg_size
<< " bytes from dispatch throttle "
<< connection->dispatch_queue->dispatch_throttler.get_current() << "/"
<< connection->dispatch_queue->dispatch_throttler.get_max()
<< " failed, just wait." << dendl;
// following thread pool deal with th full message queue isn't a
// short time, so we can wait a ms.
if (connection->register_time_events.empty()) {
connection->register_time_events.insert(
connection->center->create_time_event(1000,
connection->wakeup_handler));
}
return nullptr;
}
}
throttle_stamp = ceph_clock_now();
state = READ_MESSAGE_FRONT;
return read_message_front();
}
CtPtr ProtocolV1::read_message_front() {
ldout(cct, 20) << __func__ << dendl;
unsigned front_len = current_header.front_len;
if (front_len) {
if (!front.length()) {
front.push_back(buffer::create(front_len));
}
return READB(front_len, front.c_str(), handle_message_front);
}
return read_message_middle();
}
CtPtr ProtocolV1::handle_message_front(char *buffer, int r) {
ldout(cct, 20) << __func__ << " r=" << r << dendl;
if (r < 0) {
ldout(cct, 1) << __func__ << " read message front failed" << dendl;
return _fault();
}
ldout(cct, 20) << __func__ << " got front " << front.length() << dendl;
return read_message_middle();
}
CtPtr ProtocolV1::read_message_middle() {
ldout(cct, 20) << __func__ << dendl;
if (current_header.middle_len) {
if (!middle.length()) {
middle.push_back(buffer::create(current_header.middle_len));
}
return READB(current_header.middle_len, middle.c_str(),
handle_message_middle);
}
return read_message_data_prepare();
}
CtPtr ProtocolV1::handle_message_middle(char *buffer, int r) {
ldout(cct, 20) << __func__ << " r" << r << dendl;
if (r < 0) {
ldout(cct, 1) << __func__ << " read message middle failed" << dendl;
return _fault();
}
ldout(cct, 20) << __func__ << " got middle " << middle.length() << dendl;
return read_message_data_prepare();
}
CtPtr ProtocolV1::read_message_data_prepare() {
ldout(cct, 20) << __func__ << dendl;
unsigned data_len = le32_to_cpu(current_header.data_len);
unsigned data_off = le32_to_cpu(current_header.data_off);
if (data_len) {
// get a buffer
#if 0
// rx_buffers is broken by design... see
// http://tracker.ceph.com/issues/22480
map<ceph_tid_t, pair<bufferlist, int> >::iterator p =
connection->rx_buffers.find(current_header.tid);
if (p != connection->rx_buffers.end()) {
ldout(cct, 10) << __func__ << " seleting rx buffer v " << p->second.second
<< " at offset " << data_off << " len "
<< p->second.first.length() << dendl;
data_buf = p->second.first;
// make sure it's big enough
if (data_buf.length() < data_len)
data_buf.push_back(buffer::create(data_len - data_buf.length()));
data_blp = data_buf.begin();
} else {
ldout(cct, 20) << __func__ << " allocating new rx buffer at offset "
<< data_off << dendl;
alloc_aligned_buffer(data_buf, data_len, data_off);
data_blp = data_buf.begin();
}
#else
ldout(cct, 20) << __func__ << " allocating new rx buffer at offset "
<< data_off << dendl;
alloc_aligned_buffer(data_buf, data_len, data_off);
data_blp = data_buf.begin();
#endif
}
msg_left = data_len;
return CONTINUE(read_message_data);
}
CtPtr ProtocolV1::read_message_data() {
ldout(cct, 20) << __func__ << " msg_left=" << msg_left << dendl;
if (msg_left > 0) {
bufferptr bp = data_blp.get_current_ptr();
unsigned read_len = std::min(bp.length(), msg_left);
return READB(read_len, bp.c_str(), handle_message_data);
}
return read_message_footer();
}
CtPtr ProtocolV1::handle_message_data(char *buffer, int r) {
ldout(cct, 20) << __func__ << " r=" << r << dendl;
if (r < 0) {
ldout(cct, 1) << __func__ << " read data error " << dendl;
return _fault();
}
bufferptr bp = data_blp.get_current_ptr();
unsigned read_len = std::min(bp.length(), msg_left);
ceph_assert(read_len < std::numeric_limits<int>::max());
data_blp.advance(read_len);
data.append(bp, 0, read_len);
msg_left -= read_len;
return CONTINUE(read_message_data);
}
CtPtr ProtocolV1::read_message_footer() {
ldout(cct, 20) << __func__ << dendl;
state = READ_FOOTER_AND_DISPATCH;
unsigned len;
if (connection->has_feature(CEPH_FEATURE_MSG_AUTH)) {
len = sizeof(ceph_msg_footer);
} else {
len = sizeof(ceph_msg_footer_old);
}
return READ(len, handle_message_footer);
}
CtPtr ProtocolV1::handle_message_footer(char *buffer, int r) {
ldout(cct, 20) << __func__ << " r=" << r << dendl;
if (r < 0) {
ldout(cct, 1) << __func__ << " read footer data error " << dendl;
return _fault();
}
ceph_msg_footer footer;
ceph_msg_footer_old old_footer;
if (connection->has_feature(CEPH_FEATURE_MSG_AUTH)) {
footer = *((ceph_msg_footer *)buffer);
} else {
old_footer = *((ceph_msg_footer_old *)buffer);
footer.front_crc = old_footer.front_crc;
footer.middle_crc = old_footer.middle_crc;
footer.data_crc = old_footer.data_crc;
footer.sig = 0;
footer.flags = old_footer.flags;
}
int aborted = (footer.flags & CEPH_MSG_FOOTER_COMPLETE) == 0;
ldout(cct, 10) << __func__ << " aborted = " << aborted << dendl;
if (aborted) {
ldout(cct, 0) << __func__ << " got " << front.length() << " + "
<< middle.length() << " + " << data.length()
<< " byte message.. ABORTED" << dendl;
return _fault();
}
ldout(cct, 20) << __func__ << " got " << front.length() << " + "
<< middle.length() << " + " << data.length() << " byte message"
<< dendl;
Message *message = decode_message(cct, messenger->crcflags, current_header,
footer, front, middle, data, connection);
if (!message) {
ldout(cct, 1) << __func__ << " decode message failed " << dendl;
return _fault();
}
//
// Check the signature if one should be present. A zero return indicates
// success. PLR
//
if (session_security.get() == NULL) {
ldout(cct, 10) << __func__ << " no session security set" << dendl;
} else {
if (session_security->check_message_signature(message)) {
ldout(cct, 0) << __func__ << " Signature check failed" << dendl;
message->put();
return _fault();
}
}
message->set_byte_throttler(connection->policy.throttler_bytes);
message->set_message_throttler(connection->policy.throttler_messages);
// store reservation size in message, so we don't get confused
// by messages entering the dispatch queue through other paths.
message->set_dispatch_throttle_size(cur_msg_size);
message->set_recv_stamp(recv_stamp);
message->set_throttle_stamp(throttle_stamp);
message->set_recv_complete_stamp(ceph_clock_now());
// check received seq#. if it is old, drop the message.
// note that incoming messages may skip ahead. this is convenient for the
// client side queueing because messages can't be renumbered, but the (kernel)
// client will occasionally pull a message out of the sent queue to send
// elsewhere. in that case it doesn't matter if we "got" it or not.
uint64_t cur_seq = in_seq;
if (message->get_seq() <= cur_seq) {
ldout(cct, 0) << __func__ << " got old message " << message->get_seq()
<< " <= " << cur_seq << " " << message << " " << *message
<< ", discarding" << dendl;
message->put();
if (connection->has_feature(CEPH_FEATURE_RECONNECT_SEQ) &&
cct->_conf->ms_die_on_old_message) {
ceph_assert(0 == "old msgs despite reconnect_seq feature");
}
return nullptr;
}
if (message->get_seq() > cur_seq + 1) {
ldout(cct, 0) << __func__ << " missed message? skipped from seq "
<< cur_seq << " to " << message->get_seq() << dendl;
if (cct->_conf->ms_die_on_skipped_message) {
ceph_assert(0 == "skipped incoming seq");
}
}
#if defined(WITH_LTTNG) && defined(WITH_EVENTTRACE)
if (message->get_type() == CEPH_MSG_OSD_OP ||
message->get_type() == CEPH_MSG_OSD_OPREPLY) {
utime_t ltt_processed_stamp = ceph_clock_now();
double usecs_elapsed =
(ltt_processed_stamp.to_nsec() - ltt_recv_stamp.to_nsec()) / 1000;
ostringstream buf;
if (message->get_type() == CEPH_MSG_OSD_OP)
OID_ELAPSED_WITH_MSG(message, usecs_elapsed, "TIME_TO_DECODE_OSD_OP",
false);
else
OID_ELAPSED_WITH_MSG(message, usecs_elapsed, "TIME_TO_DECODE_OSD_OPREPLY",
false);
}
#endif
// note last received message.
in_seq = message->get_seq();
ldout(cct, 5) << " rx " << message->get_source() << " seq "
<< message->get_seq() << " " << message << " " << *message
<< dendl;
bool need_dispatch_writer = false;
if (!connection->policy.lossy) {
ack_left++;
need_dispatch_writer = true;