/
AsyncConnection.cc
2558 lines (2261 loc) · 89.6 KB
/
AsyncConnection.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
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2014 UnitedStack <haomai@unitedstack.com>
*
* Author: Haomai Wang <haomaiwang@gmail.com>
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include "include/Context.h"
#include "common/errno.h"
#include "AsyncMessenger.h"
#include "AsyncConnection.h"
#include "include/sock_compat.h"
// Constant to limit starting sequence number to 2^31. Nothing special about it, just a big number. PLR
#define SEQ_MASK 0x7fffffff
#define dout_subsys ceph_subsys_ms
#undef dout_prefix
#define dout_prefix _conn_prefix(_dout)
ostream& AsyncConnection::_conn_prefix(std::ostream *_dout) {
return *_dout << "-- " << async_msgr->get_myinst().addr << " >> " << peer_addr << " conn(" << this
<< " sd=" << sd << " :" << port
<< " s=" << get_state_name(state)
<< " pgs=" << peer_global_seq
<< " cs=" << connect_seq
<< " l=" << policy.lossy
<< ").";
}
// Notes:
// 1. Don't dispatch any event when closed! It may cause AsyncConnection alive even if AsyncMessenger dead
const int AsyncConnection::TCP_PREFETCH_MIN_SIZE = 512;
const int ASYNC_COALESCE_THRESHOLD = 256;
class C_time_wakeup : public EventCallback {
AsyncConnectionRef conn;
public:
explicit C_time_wakeup(AsyncConnectionRef c): conn(c) {}
void do_request(int fd_or_id) {
conn->wakeup_from(fd_or_id);
}
};
class C_handle_read : public EventCallback {
AsyncConnectionRef conn;
public:
explicit C_handle_read(AsyncConnectionRef c): conn(c) {}
void do_request(int fd_or_id) {
conn->process();
}
};
class C_handle_write : public EventCallback {
AsyncConnectionRef conn;
public:
explicit C_handle_write(AsyncConnectionRef c): conn(c) {}
void do_request(int fd) {
conn->handle_write();
}
};
class C_handle_reset : public EventCallback {
AsyncMessenger *msgr;
AsyncConnectionRef conn;
public:
C_handle_reset(AsyncMessenger *m, AsyncConnectionRef c): msgr(m), conn(c) {}
void do_request(int id) {
msgr->ms_deliver_handle_reset(conn.get());
}
};
class C_handle_remote_reset : public EventCallback {
AsyncMessenger *msgr;
AsyncConnectionRef conn;
public:
C_handle_remote_reset(AsyncMessenger *m, AsyncConnectionRef c): msgr(m), conn(c) {}
void do_request(int id) {
msgr->ms_deliver_handle_remote_reset(conn.get());
}
};
class C_handle_dispatch : public EventCallback {
AsyncMessenger *msgr;
Message *m;
public:
C_handle_dispatch(AsyncMessenger *msgr, Message *m): msgr(msgr), m(m) {}
void do_request(int id) {
msgr->ms_deliver_dispatch(m);
delete this;
}
};
class C_deliver_connect : public EventCallback {
AsyncMessenger *msgr;
AsyncConnectionRef conn;
public:
C_deliver_connect(AsyncMessenger *msgr, AsyncConnectionRef c): msgr(msgr), conn(c) {}
void do_request(int id) {
msgr->ms_deliver_handle_connect(conn.get());
}
};
class C_deliver_accept : public EventCallback {
AsyncMessenger *msgr;
AsyncConnectionRef conn;
public:
C_deliver_accept(AsyncMessenger *msgr, AsyncConnectionRef c): msgr(msgr), conn(c) {}
void do_request(int id) {
msgr->ms_deliver_handle_accept(conn.get());
delete this;
}
};
class C_local_deliver : public EventCallback {
AsyncConnectionRef conn;
public:
explicit C_local_deliver(AsyncConnectionRef c): conn(c) {}
void do_request(int id) {
conn->local_deliver();
}
};
class C_clean_handler : public EventCallback {
AsyncConnectionRef conn;
public:
explicit C_clean_handler(AsyncConnectionRef c): conn(c) {}
void do_request(int id) {
conn->cleanup_handler();
delete this;
}
};
static void alloc_aligned_buffer(bufferlist& data, unsigned len, unsigned off)
{
// create a buffer to read into that matches the data alignment
unsigned left = len;
if (off & ~CEPH_PAGE_MASK) {
// head
unsigned head = 0;
head = MIN(CEPH_PAGE_SIZE - (off & ~CEPH_PAGE_MASK), left);
data.push_back(buffer::create(head));
left -= head;
}
unsigned middle = left & CEPH_PAGE_MASK;
if (middle > 0) {
data.push_back(buffer::create_page_aligned(middle));
left -= middle;
}
if (left) {
data.push_back(buffer::create(left));
}
}
AsyncConnection::AsyncConnection(CephContext *cct, AsyncMessenger *m, EventCenter *c, PerfCounters *p)
: Connection(cct, m), async_msgr(m), logger(p), global_seq(0), connect_seq(0), peer_global_seq(0),
out_seq(0), ack_left(0), in_seq(0), state(STATE_NONE), state_after_send(0), sd(-1), port(-1),
write_lock("AsyncConnection::write_lock"), can_write(NOWRITE),
open_write(false), keepalive(false), lock("AsyncConnection::lock"), recv_buf(NULL),
recv_max_prefetch(MIN(msgr->cct->_conf->ms_tcp_prefetch_max_size, TCP_PREFETCH_MIN_SIZE)),
recv_start(0), recv_end(0), got_bad_auth(false), authorizer(NULL), replacing(false),
is_reset_from_peer(false), once_ready(false), state_buffer(NULL), state_offset(0), net(cct), center(c)
{
read_handler = new C_handle_read(this);
write_handler = new C_handle_write(this);
reset_handler = new C_handle_reset(async_msgr, this);
remote_reset_handler = new C_handle_remote_reset(async_msgr, this);
connect_handler = new C_deliver_connect(async_msgr, this);
local_deliver_handler = new C_local_deliver(this);
wakeup_handler = new C_time_wakeup(this);
memset(msgvec, 0, sizeof(msgvec));
// double recv_max_prefetch see "read_until"
recv_buf = new char[2*recv_max_prefetch];
state_buffer = new char[4096];
logger->inc(l_msgr_created_connections);
}
AsyncConnection::~AsyncConnection()
{
assert(out_q.empty());
assert(sent.empty());
delete authorizer;
if (recv_buf)
delete[] recv_buf;
if (state_buffer)
delete[] state_buffer;
}
/* return -1 means `fd` occurs error or closed, it should be closed
* return 0 means EAGAIN or EINTR */
ssize_t AsyncConnection::read_bulk(int fd, char *buf, unsigned len)
{
ssize_t nread = ::read(fd, buf, len);
if (nread == -1) {
if (errno == EAGAIN || errno == EINTR) {
nread = 0;
} else {
ldout(async_msgr->cct, 1) << __func__ << " reading from fd=" << fd
<< " : "<< strerror(errno) << dendl;
return -1;
}
} else if (nread == 0) {
ldout(async_msgr->cct, 1) << __func__ << " peer close file descriptor "
<< fd << dendl;
return -1;
}
return nread;
}
/*
SIGPIPE suppression - for platforms without SO_NOSIGPIPE or MSG_NOSIGNAL
http://krokisplace.blogspot.in/2010/02/suppressing-sigpipe-in-library.html
http://www.microhowto.info/howto/ignore_sigpipe_without_affecting_other_threads_in_a_process.html
*/
void AsyncConnection::suppress_sigpipe()
{
#if !defined(MSG_NOSIGNAL) && !defined(SO_NOSIGPIPE)
/*
We want to ignore possible SIGPIPE that we can generate on write.
SIGPIPE is delivered *synchronously* and *only* to the thread
doing the write. So if it is reported as already pending (which
means the thread blocks it), then we do nothing: if we generate
SIGPIPE, it will be merged with the pending one (there's no
queuing), and that suits us well. If it is not pending, we block
it in this thread (and we avoid changing signal action, because it
is per-process).
*/
sigset_t pending;
sigemptyset(&pending);
sigpending(&pending);
sigpipe_pending = sigismember(&pending, SIGPIPE);
if (!sigpipe_pending) {
sigset_t blocked;
sigemptyset(&blocked);
pthread_sigmask(SIG_BLOCK, &sigpipe_mask, &blocked);
/* Maybe is was blocked already? */
sigpipe_unblock = ! sigismember(&blocked, SIGPIPE);
}
#endif /* !defined(MSG_NOSIGNAL) && !defined(SO_NOSIGPIPE) */
}
void AsyncConnection::restore_sigpipe()
{
#if !defined(MSG_NOSIGNAL) && !defined(SO_NOSIGPIPE)
/*
If SIGPIPE was pending already we do nothing. Otherwise, if it
become pending (i.e., we generated it), then we sigwait() it (thus
clearing pending status). Then we unblock SIGPIPE, but only if it
were us who blocked it.
*/
if (!sigpipe_pending) {
sigset_t pending;
sigemptyset(&pending);
sigpending(&pending);
if (sigismember(&pending, SIGPIPE)) {
/*
Protect ourselves from a situation when SIGPIPE was sent
by the user to the whole process, and was delivered to
other thread before we had a chance to wait for it.
*/
static const struct timespec nowait = { 0, 0 };
TEMP_FAILURE_RETRY(sigtimedwait(&sigpipe_mask, NULL, &nowait));
}
if (sigpipe_unblock)
pthread_sigmask(SIG_UNBLOCK, &sigpipe_mask, NULL);
}
#endif /* !defined(MSG_NOSIGNAL) && !defined(SO_NOSIGPIPE) */
}
// return the length of msg needed to be sent,
// < 0 means error occured
ssize_t AsyncConnection::do_sendmsg(struct msghdr &msg, unsigned len, bool more)
{
suppress_sigpipe();
while (len > 0) {
ssize_t r;
#if defined(MSG_NOSIGNAL)
r = ::sendmsg(sd, &msg, MSG_NOSIGNAL | (more ? MSG_MORE : 0));
#else
r = ::sendmsg(sd, &msg, (more ? MSG_MORE : 0));
#endif /* defined(MSG_NOSIGNAL) */
if (r == 0) {
ldout(async_msgr->cct, 10) << __func__ << " sendmsg got r==0!" << dendl;
} else if (r < 0) {
if (errno == EINTR) {
continue;
} else if (errno == EAGAIN) {
break;
} else {
ldout(async_msgr->cct, 1) << __func__ << " sendmsg error: " << cpp_strerror(errno) << dendl;
restore_sigpipe();
return r;
}
}
len -= r;
if (len == 0) break;
// hrmph. drain r bytes from the front of our message.
ldout(async_msgr->cct, 20) << __func__ << " short write did " << r << ", still have " << len << dendl;
while (r > 0) {
if (msg.msg_iov[0].iov_len <= (size_t)r) {
// drain this whole item
r -= msg.msg_iov[0].iov_len;
msg.msg_iov++;
msg.msg_iovlen--;
} else {
msg.msg_iov[0].iov_base = (char *)msg.msg_iov[0].iov_base + r;
msg.msg_iov[0].iov_len -= r;
break;
}
}
}
restore_sigpipe();
return (ssize_t)len;
}
// return the remaining bytes, it may larger than the length of ptr
// else return < 0 means error
ssize_t AsyncConnection::_try_send(bool send, bool more)
{
if (!send)
return 0;
if (async_msgr->cct->_conf->ms_inject_socket_failures && sd >= 0) {
if (rand() % async_msgr->cct->_conf->ms_inject_socket_failures == 0) {
ldout(async_msgr->cct, 0) << __func__ << " injecting socket failure" << dendl;
::shutdown(sd, SHUT_RDWR);
}
}
uint64_t sent_bytes = 0;
list<bufferptr>::const_iterator pb = outcoming_bl.buffers().begin();
uint64_t left_pbrs = outcoming_bl.buffers().size();
while (left_pbrs) {
struct msghdr msg;
uint64_t size = MIN(left_pbrs, ASYNC_IOV_MAX);
left_pbrs -= size;
memset(&msg, 0, sizeof(msg));
msg.msg_iovlen = 0;
msg.msg_iov = msgvec;
unsigned msglen = 0;
while (size > 0) {
msgvec[msg.msg_iovlen].iov_base = (void*)(pb->c_str());
msgvec[msg.msg_iovlen].iov_len = pb->length();
msg.msg_iovlen++;
msglen += pb->length();
++pb;
size--;
}
ssize_t r = do_sendmsg(msg, msglen, left_pbrs || more);
if (r < 0)
return r;
// "r" is the remaining length
sent_bytes += msglen - r;
if (r > 0) {
ldout(async_msgr->cct, 5) << __func__ << " remaining " << r
<< " needed to be sent, creating event for writing"
<< dendl;
break;
}
// only "r" == 0 continue
}
// trim already sent for outcoming_bl
if (sent_bytes) {
if (sent_bytes < outcoming_bl.length()) {
outcoming_bl.splice(0, sent_bytes);
} else {
outcoming_bl.clear();
}
}
ldout(async_msgr->cct, 20) << __func__ << " sent bytes " << sent_bytes
<< " remaining bytes " << outcoming_bl.length() << dendl;
if (!open_write && is_queued()) {
center->create_file_event(sd, EVENT_WRITABLE, write_handler);
open_write = true;
}
if (open_write && !is_queued()) {
center->delete_file_event(sd, EVENT_WRITABLE);
open_write = false;
}
return outcoming_bl.length();
}
// Because this func will be called multi times to populate
// the needed buffer, so the passed in bufferptr must be the same.
// Normally, only "read_message" will pass existing bufferptr in
//
// And it will uses readahead method to reduce small read overhead,
// "recv_buf" is used to store read buffer
//
// return the remaining bytes, 0 means this buffer is finished
// else return < 0 means error
ssize_t AsyncConnection::read_until(unsigned len, char *p)
{
ldout(async_msgr->cct, 25) << __func__ << " len is " << len << " state_offset is "
<< state_offset << dendl;
if (async_msgr->cct->_conf->ms_inject_socket_failures && sd >= 0) {
if (rand() % async_msgr->cct->_conf->ms_inject_socket_failures == 0) {
ldout(async_msgr->cct, 0) << __func__ << " injecting socket failure" << dendl;
::shutdown(sd, SHUT_RDWR);
}
}
ssize_t r = 0;
uint64_t left = len - state_offset;
if (recv_end > recv_start) {
uint64_t to_read = MIN(recv_end - recv_start, left);
memcpy(p, recv_buf+recv_start, to_read);
recv_start += to_read;
left -= to_read;
ldout(async_msgr->cct, 25) << __func__ << " got " << to_read << " in buffer "
<< " left is " << left << " buffer still has "
<< recv_end - recv_start << dendl;
if (left == 0) {
return 0;
}
state_offset += to_read;
}
recv_end = recv_start = 0;
/* nothing left in the prefetch buffer */
if (len > recv_max_prefetch) {
/* this was a large read, we don't prefetch for these */
do {
r = read_bulk(sd, p+state_offset, left);
ldout(async_msgr->cct, 25) << __func__ << " read_bulk left is " << left << " got " << r << dendl;
if (r < 0) {
ldout(async_msgr->cct, 1) << __func__ << " read failed" << dendl;
return -1;
} else if (r == static_cast<int>(left)) {
state_offset = 0;
return 0;
}
state_offset += r;
left -= r;
} while (r > 0);
} else {
do {
r = read_bulk(sd, recv_buf+recv_end, recv_max_prefetch);
ldout(async_msgr->cct, 25) << __func__ << " read_bulk recv_end is " << recv_end
<< " left is " << left << " got " << r << dendl;
if (r < 0) {
ldout(async_msgr->cct, 1) << __func__ << " read failed" << dendl;
return -1;
}
recv_end += r;
if (r >= static_cast<int>(left)) {
recv_start = len - state_offset;
memcpy(p+state_offset, recv_buf, recv_start);
state_offset = 0;
return 0;
}
left -= r;
} while (r > 0);
memcpy(p+state_offset, recv_buf, recv_end-recv_start);
state_offset += (recv_end - recv_start);
recv_end = recv_start = 0;
}
ldout(async_msgr->cct, 25) << __func__ << " need len " << len << " remaining "
<< len - state_offset << " bytes" << dendl;
return len - state_offset;
}
void AsyncConnection::process()
{
ssize_t r = 0;
int prev_state = state;
bool already_dispatch_writer = false;
Mutex::Locker l(lock);
do {
ldout(async_msgr->cct, 20) << __func__ << " prev state is " << get_state_name(prev_state) << dendl;
prev_state = state;
switch (state) {
case STATE_OPEN:
{
char tag = -1;
r = read_until(sizeof(tag), &tag);
if (r < 0) {
ldout(async_msgr->cct, 1) << __func__ << " read tag failed" << dendl;
goto fail;
} else if (r > 0) {
break;
}
if (tag == CEPH_MSGR_TAG_KEEPALIVE) {
ldout(async_msgr->cct, 20) << __func__ << " got KEEPALIVE" << dendl;
set_last_keepalive(ceph_clock_now(NULL));
} else if (tag == CEPH_MSGR_TAG_KEEPALIVE2) {
state = STATE_OPEN_KEEPALIVE2;
} else if (tag == CEPH_MSGR_TAG_KEEPALIVE2_ACK) {
state = STATE_OPEN_KEEPALIVE2_ACK;
} else if (tag == CEPH_MSGR_TAG_ACK) {
state = STATE_OPEN_TAG_ACK;
} else if (tag == CEPH_MSGR_TAG_MSG) {
state = STATE_OPEN_MESSAGE_HEADER;
} else if (tag == CEPH_MSGR_TAG_CLOSE) {
state = STATE_OPEN_TAG_CLOSE;
} else {
ldout(async_msgr->cct, 0) << __func__ << " bad tag " << (int)tag << dendl;
goto fail;
}
break;
}
case STATE_OPEN_KEEPALIVE2:
{
ceph_timespec *t;
r = read_until(sizeof(*t), state_buffer);
if (r < 0) {
ldout(async_msgr->cct, 1) << __func__ << " read keeplive timespec failed" << dendl;
goto fail;
} else if (r > 0) {
break;
}
ldout(async_msgr->cct, 30) << __func__ << " got KEEPALIVE2 tag ..." << dendl;
t = (ceph_timespec*)state_buffer;
utime_t kp_t = utime_t(*t);
write_lock.Lock();
_send_keepalive_or_ack(true, &kp_t);
write_lock.Unlock();
ldout(async_msgr->cct, 20) << __func__ << " got KEEPALIVE2 " << kp_t << dendl;
set_last_keepalive(ceph_clock_now(NULL));
state = STATE_OPEN;
break;
}
case STATE_OPEN_KEEPALIVE2_ACK:
{
ceph_timespec *t;
r = read_until(sizeof(*t), state_buffer);
if (r < 0) {
ldout(async_msgr->cct, 1) << __func__ << " read keeplive timespec failed" << dendl;
goto fail;
} else if (r > 0) {
break;
}
t = (ceph_timespec*)state_buffer;
set_last_keepalive_ack(utime_t(*t));
ldout(async_msgr->cct, 20) << __func__ << " got KEEPALIVE_ACK" << dendl;
state = STATE_OPEN;
break;
}
case STATE_OPEN_TAG_ACK:
{
ceph_le64 *seq;
r = read_until(sizeof(seq), state_buffer);
if (r < 0) {
ldout(async_msgr->cct, 1) << __func__ << " read ack seq failed" << dendl;
goto fail;
} else if (r > 0) {
break;
}
seq = (ceph_le64*)state_buffer;
ldout(async_msgr->cct, 20) << __func__ << " got ACK" << dendl;
handle_ack(*seq);
state = STATE_OPEN;
break;
}
case STATE_OPEN_MESSAGE_HEADER:
{
ldout(async_msgr->cct, 20) << __func__ << " begin MSG" << dendl;
ceph_msg_header header;
ceph_msg_header_old oldheader;
__u32 header_crc = 0;
unsigned len;
if (has_feature(CEPH_FEATURE_NOSRCADDR))
len = sizeof(header);
else
len = sizeof(oldheader);
r = read_until(len, state_buffer);
if (r < 0) {
ldout(async_msgr->cct, 1) << __func__ << " read message header failed" << dendl;
goto fail;
} else if (r > 0) {
break;
}
ldout(async_msgr->cct, 20) << __func__ << " got MSG header" << dendl;
if (has_feature(CEPH_FEATURE_NOSRCADDR)) {
header = *((ceph_msg_header*)state_buffer);
if (msgr->crcflags & MSG_CRC_HEADER)
header_crc = ceph_crc32c(0, (unsigned char *)&header,
sizeof(header) - sizeof(header.crc));
} else {
oldheader = *((ceph_msg_header_old*)state_buffer);
// this is fugly
memcpy(&header, &oldheader, sizeof(header));
header.src = oldheader.src.name;
header.reserved = oldheader.reserved;
if (msgr->crcflags & MSG_CRC_HEADER) {
header.crc = oldheader.crc;
header_crc = ceph_crc32c(0, (unsigned char *)&oldheader, sizeof(oldheader) - sizeof(oldheader.crc));
}
}
ldout(async_msgr->cct, 20) << __func__ << " got envelope type=" << header.type
<< " src " << entity_name_t(header.src)
<< " front=" << header.front_len
<< " data=" << header.data_len
<< " off " << header.data_off << dendl;
// verify header crc
if (msgr->crcflags & MSG_CRC_HEADER && header_crc != header.crc) {
ldout(async_msgr->cct,0) << __func__ << " reader got bad header crc "
<< header_crc << " != " << header.crc << dendl;
goto fail;
}
// Reset state
data_buf.clear();
front.clear();
middle.clear();
data.clear();
recv_stamp = ceph_clock_now(async_msgr->cct);
current_header = header;
state = STATE_OPEN_MESSAGE_THROTTLE_MESSAGE;
break;
}
case STATE_OPEN_MESSAGE_THROTTLE_MESSAGE:
{
if (policy.throttler_messages) {
ldout(async_msgr->cct, 10) << __func__ << " wants " << 1 << " message from policy throttler "
<< policy.throttler_messages->get_current() << "/"
<< policy.throttler_messages->get_max() << dendl;
if (!policy.throttler_messages->get_or_fail()) {
ldout(async_msgr->cct, 1) << __func__ << " wants 1 message from policy throttle "
<< policy.throttler_messages->get_current() << "/"
<< 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 (register_time_events.empty())
register_time_events.insert(center->create_time_event(1000, wakeup_handler));
break;
}
}
state = STATE_OPEN_MESSAGE_THROTTLE_BYTES;
break;
}
case STATE_OPEN_MESSAGE_THROTTLE_BYTES:
{
uint64_t message_size = current_header.front_len + current_header.middle_len + current_header.data_len;
if (message_size) {
if (policy.throttler_bytes) {
ldout(async_msgr->cct, 10) << __func__ << " wants " << message_size << " bytes from policy throttler "
<< policy.throttler_bytes->get_current() << "/"
<< policy.throttler_bytes->get_max() << dendl;
if (!policy.throttler_bytes->get_or_fail(message_size)) {
ldout(async_msgr->cct, 10) << __func__ << " wants " << message_size << " bytes from policy throttler "
<< policy.throttler_bytes->get_current() << "/"
<< 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 (register_time_events.empty())
register_time_events.insert(center->create_time_event(1000, wakeup_handler));
break;
}
}
}
throttle_stamp = ceph_clock_now(msgr->cct);
state = STATE_OPEN_MESSAGE_READ_FRONT;
break;
}
case STATE_OPEN_MESSAGE_READ_FRONT:
{
// read front
unsigned front_len = current_header.front_len;
if (front_len) {
if (!front.length())
front.push_back(buffer::create(front_len));
r = read_until(front_len, front.c_str());
if (r < 0) {
ldout(async_msgr->cct, 1) << __func__ << " read message front failed" << dendl;
goto fail;
} else if (r > 0) {
break;
}
ldout(async_msgr->cct, 20) << __func__ << " got front " << front.length() << dendl;
}
state = STATE_OPEN_MESSAGE_READ_MIDDLE;
break;
}
case STATE_OPEN_MESSAGE_READ_MIDDLE:
{
// read middle
unsigned middle_len = current_header.middle_len;
if (middle_len) {
if (!middle.length())
middle.push_back(buffer::create(middle_len));
r = read_until(middle_len, middle.c_str());
if (r < 0) {
ldout(async_msgr->cct, 1) << __func__ << " read message middle failed" << dendl;
goto fail;
} else if (r > 0) {
break;
}
ldout(async_msgr->cct, 20) << __func__ << " got middle " << middle.length() << dendl;
}
state = STATE_OPEN_MESSAGE_READ_DATA_PREPARE;
break;
}
case STATE_OPEN_MESSAGE_READ_DATA_PREPARE:
{
// read data
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
map<ceph_tid_t,pair<bufferlist,int> >::iterator p = rx_buffers.find(current_header.tid);
if (p != rx_buffers.end()) {
ldout(async_msgr->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(async_msgr->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();
}
}
msg_left = data_len;
state = STATE_OPEN_MESSAGE_READ_DATA;
break;
}
case STATE_OPEN_MESSAGE_READ_DATA:
{
while (msg_left > 0) {
bufferptr bp = data_blp.get_current_ptr();
unsigned read = MIN(bp.length(), msg_left);
r = read_until(read, bp.c_str());
if (r < 0) {
ldout(async_msgr->cct, 1) << __func__ << " read data error " << dendl;
goto fail;
} else if (r > 0) {
break;
}
data_blp.advance(read);
data.append(bp, 0, read);
msg_left -= read;
}
if (msg_left == 0)
state = STATE_OPEN_MESSAGE_READ_FOOTER_AND_DISPATCH;
break;
}
case STATE_OPEN_MESSAGE_READ_FOOTER_AND_DISPATCH:
{
ceph_msg_footer footer;
ceph_msg_footer_old old_footer;
unsigned len;
// footer
if (has_feature(CEPH_FEATURE_MSG_AUTH))
len = sizeof(footer);
else
len = sizeof(old_footer);
r = read_until(len, state_buffer);
if (r < 0) {
ldout(async_msgr->cct, 1) << __func__ << " read footer data error " << dendl;
goto fail;
} else if (r > 0) {
break;
}
if (has_feature(CEPH_FEATURE_MSG_AUTH)) {
footer = *((ceph_msg_footer*)state_buffer);
} else {
old_footer = *((ceph_msg_footer_old*)state_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(async_msgr->cct, 10) << __func__ << " aborted = " << aborted << dendl;
if (aborted) {
ldout(async_msgr->cct, 0) << __func__ << " got " << front.length() << " + " << middle.length() << " + " << data.length()
<< " byte message.. ABORTED" << dendl;
goto fail;
}
ldout(async_msgr->cct, 20) << __func__ << " got " << front.length() << " + " << middle.length()
<< " + " << data.length() << " byte message" << dendl;
Message *message = decode_message(async_msgr->cct, async_msgr->crcflags, current_header, footer, front, middle, data);
if (!message) {
ldout(async_msgr->cct, 1) << __func__ << " decode message failed " << dendl;
goto fail;
}
//
// Check the signature if one should be present. A zero return indicates success. PLR
//
if (session_security.get() == NULL) {
ldout(async_msgr->cct, 10) << __func__ << " no session security set" << dendl;
} else {
if (session_security->check_message_signature(message)) {
ldout(async_msgr->cct, 0) << __func__ << " Signature check failed" << dendl;
message->put();
goto fail;
}
}
message->set_byte_throttler(policy.throttler_bytes);
message->set_message_throttler(policy.throttler_messages);
// store reservation size in message, so we don't get confused
// by messages entering the dispatch queue through other paths.
uint64_t message_size = current_header.front_len + current_header.middle_len + current_header.data_len;
message->set_dispatch_throttle_size(message_size);
message->set_recv_stamp(recv_stamp);
message->set_throttle_stamp(throttle_stamp);
message->set_recv_complete_stamp(ceph_clock_now(async_msgr->cct));
// 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.read();
if (message->get_seq() <= cur_seq) {
ldout(async_msgr->cct,0) << __func__ << " got old message "
<< message->get_seq() << " <= " << cur_seq << " " << message << " " << *message
<< ", discarding" << dendl;
message->put();
if (has_feature(CEPH_FEATURE_RECONNECT_SEQ) && async_msgr->cct->_conf->ms_die_on_old_message)
assert(0 == "old msgs despite reconnect_seq feature");
break;
}
if (message->get_seq() > cur_seq + 1) {
ldout(async_msgr->cct, 0) << __func__ << " missed message? skipped from seq "
<< cur_seq << " to " << message->get_seq() << dendl;
if (async_msgr->cct->_conf->ms_die_on_skipped_message)
assert(0 == "skipped incoming seq");
}
message->set_connection(this);
// note last received message.
in_seq.set(message->get_seq());
ldout(async_msgr->cct, 1) << " == rx == " << message->get_source() << " seq "
<< message->get_seq() << " " << message << " " << *message << dendl;
ack_left.inc();
// if send_message always send inline, it may have no
// opportunity to send seq ack.
if (!already_dispatch_writer) {
center->dispatch_event_external(write_handler);
already_dispatch_writer = true;
}
state = STATE_OPEN;
async_msgr->ms_fast_preprocess(message);
if (async_msgr->ms_can_fast_dispatch(message)) {
lock.Unlock();
async_msgr->ms_fast_dispatch(message);
lock.Lock();
} else {
center->dispatch_event_external(EventCallbackRef(new C_handle_dispatch(async_msgr, message)));
}
logger->inc(l_msgr_recv_messages);
logger->inc(l_msgr_recv_bytes, message_size + sizeof(ceph_msg_header) + sizeof(ceph_msg_footer));
break;
}
case STATE_OPEN_TAG_CLOSE:
{
ldout(async_msgr->cct, 20) << __func__ << " got CLOSE" << dendl;
_stop();
return ;
}
case STATE_STANDBY:
{
ldout(async_msgr->cct, 20) << __func__ << " enter STANDY" << dendl;
break;
}
case STATE_CLOSED:
{
if (sd >= 0)
center->delete_file_event(sd, EVENT_READABLE);
ldout(async_msgr->cct, 20) << __func__ << " socket closed" << dendl;
break;
}
case STATE_WAIT:
{
ldout(async_msgr->cct, 20) << __func__ << " enter wait state" << dendl;
break;
}
default:
{
if (_process_connection() < 0)
goto fail;
break;
}
}
} while (prev_state != state);
return;
fail:
// clean up state internal variables and states
if (state >= STATE_CONNECTING_SEND_CONNECT_MSG &&
state <= STATE_CONNECTING_READY) {
delete authorizer;
authorizer = NULL;
got_bad_auth = false;
}
if (state > STATE_OPEN_MESSAGE_THROTTLE_MESSAGE &&
state <= STATE_OPEN_MESSAGE_READ_FOOTER_AND_DISPATCH
&& policy.throttler_messages) {
ldout(async_msgr->cct,10) << __func__ << " releasing " << 1
<< " message to policy throttler "
<< policy.throttler_messages->get_current() << "/"
<< policy.throttler_messages->get_max() << dendl;
policy.throttler_messages->put();
}
if (state > STATE_OPEN_MESSAGE_THROTTLE_BYTES &&
state <= STATE_OPEN_MESSAGE_READ_FOOTER_AND_DISPATCH) {
uint64_t message_size = current_header.front_len + current_header.middle_len + current_header.data_len;
if (policy.throttler_bytes) {
ldout(async_msgr->cct,10) << __func__ << " releasing " << message_size
<< " bytes to policy throttler "
<< policy.throttler_bytes->get_current() << "/"
<< policy.throttler_bytes->get_max() << dendl;
policy.throttler_bytes->put(message_size);
}
}