1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
6 #include "ProtocolV2.h"
7 #include "AsyncMessenger.h"
9 #include "common/EventTrace.h"
10 #include "common/ceph_crypto.h"
11 #include "common/errno.h"
12 #include "include/random.h"
13 #include "auth/AuthClient.h"
14 #include "auth/AuthServer.h"
16 #define dout_subsys ceph_subsys_ms
18 #define dout_prefix _conn_prefix(_dout)
19 ostream
&ProtocolV2::_conn_prefix(std::ostream
*_dout
) {
20 return *_dout
<< "--2- " << messenger
->get_myaddrs() << " >> "
21 << *connection
->peer_addrs
<< " conn(" << connection
<< " "
23 << " " << ceph_con_mode_name(auth_meta
->con_mode
)
24 << " :" << connection
->port
25 << " s=" << get_state_name(state
) << " pgs=" << peer_global_seq
26 << " cs=" << connect_seq
<< " l=" << connection
->policy
.lossy
27 << " rx=" << session_stream_handlers
.rx
.get()
28 << " tx=" << session_stream_handlers
.tx
.get()
32 using namespace ceph::msgr::v2
;
34 using CtPtr
= Ct
<ProtocolV2
> *;
35 using CtRef
= Ct
<ProtocolV2
> &;
37 void ProtocolV2::run_continuation(CtPtr pcontinuation
) {
39 run_continuation(*pcontinuation
);
43 void ProtocolV2::run_continuation(CtRef continuation
) {
45 CONTINUATION_RUN(continuation
)
46 } catch (const buffer::error
&e
) {
47 lderr(cct
) << __func__
<< " failed decoding of frame header: " << e
50 } catch (const ceph::crypto::onwire::MsgAuthError
&e
) {
51 lderr(cct
) << __func__
<< " " << e
.what() << dendl
;
53 } catch (const DecryptionError
&) {
54 lderr(cct
) << __func__
<< " failed to decrypt frame payload" << dendl
;
58 #define WRITE(B, D, C) write(D, CONTINUATION(C), B)
60 #define READ(L, C) read(CONTINUATION(C), buffer::ptr_node::create(buffer::create(L)))
62 #define READ_RXBUF(B, C) read(CONTINUATION(C), B)
64 #ifdef UNIT_TESTS_BUILT
66 #define INTERCEPT(S) { \
67 if(connection->interceptor) { \
68 auto a = connection->interceptor->intercept(connection, (S)); \
69 if (a == Interceptor::ACTION::FAIL) { \
71 } else if (a == Interceptor::ACTION::STOP) { \
73 connection->dispatch_queue->queue_reset(connection); \
81 ProtocolV2::ProtocolV2(AsyncConnection
*connection
)
82 : Protocol(2, connection
),
84 peer_required_features(0),
94 bannerExchangeCallback(nullptr),
95 next_tag(static_cast<Tag
>(0)),
99 ProtocolV2::~ProtocolV2() {
102 void ProtocolV2::connect() {
103 ldout(cct
, 1) << __func__
<< dendl
;
104 state
= START_CONNECT
;
105 pre_auth
.enabled
= true;
108 void ProtocolV2::accept() {
109 ldout(cct
, 1) << __func__
<< dendl
;
110 state
= START_ACCEPT
;
113 bool ProtocolV2::is_connected() { return can_write
; }
116 * Tears down the message queues, and removes them from the
117 * DispatchQueue Must hold write_lock prior to calling.
119 void ProtocolV2::discard_out_queue() {
120 ldout(cct
, 10) << __func__
<< " started" << dendl
;
122 for (list
<Message
*>::iterator p
= sent
.begin(); p
!= sent
.end(); ++p
) {
123 ldout(cct
, 20) << __func__
<< " discard " << *p
<< dendl
;
127 for (auto& [ prio
, entries
] : out_queue
) {
128 static_cast<void>(prio
);
129 for (auto& entry
: entries
) {
130 ldout(cct
, 20) << __func__
<< " discard " << *entry
.m
<< dendl
;
135 write_in_progress
= false;
138 void ProtocolV2::reset_session() {
139 ldout(cct
, 1) << __func__
<< dendl
;
141 std::lock_guard
<std::mutex
> l(connection
->write_lock
);
142 if (connection
->delay_state
) {
143 connection
->delay_state
->discard();
146 connection
->dispatch_queue
->discard_queue(connection
->conn_id
);
148 connection
->outcoming_bl
.clear();
150 connection
->dispatch_queue
->queue_remote_reset(connection
);
163 void ProtocolV2::stop() {
164 ldout(cct
, 1) << __func__
<< dendl
;
165 if (state
== CLOSED
) {
169 if (connection
->delay_state
) connection
->delay_state
->flush();
171 std::lock_guard
<std::mutex
> l(connection
->write_lock
);
182 void ProtocolV2::fault() { _fault(); }
184 void ProtocolV2::requeue_sent() {
185 write_in_progress
= false;
190 auto& rq
= out_queue
[CEPH_MSG_PRIO_HIGHEST
];
191 out_seq
-= sent
.size();
192 while (!sent
.empty()) {
193 Message
*m
= sent
.back();
195 ldout(cct
, 5) << __func__
<< " requeueing message m=" << m
196 << " seq=" << m
->get_seq() << " type=" << m
->get_type() << " "
198 rq
.emplace_front(out_queue_entry_t
{false, m
});
202 uint64_t ProtocolV2::discard_requeued_up_to(uint64_t out_seq
, uint64_t seq
) {
203 ldout(cct
, 10) << __func__
<< " " << seq
<< dendl
;
204 std::lock_guard
<std::mutex
> l(connection
->write_lock
);
205 if (out_queue
.count(CEPH_MSG_PRIO_HIGHEST
) == 0) {
208 auto& rq
= out_queue
[CEPH_MSG_PRIO_HIGHEST
];
209 uint64_t count
= out_seq
;
210 while (!rq
.empty()) {
211 Message
* const m
= rq
.front().m
;
212 if (m
->get_seq() == 0 || m
->get_seq() > seq
) break;
213 ldout(cct
, 5) << __func__
<< " discarding message m=" << m
214 << " seq=" << m
->get_seq() << " ack_seq=" << seq
<< " "
220 if (rq
.empty()) out_queue
.erase(CEPH_MSG_PRIO_HIGHEST
);
224 void ProtocolV2::reset_recv_state() {
225 auth_meta
.reset(new AuthConnectionMeta
);
226 session_stream_handlers
.tx
.reset(nullptr);
227 session_stream_handlers
.rx
.reset(nullptr);
228 pre_auth
.txbuf
.clear();
229 pre_auth
.rxbuf
.clear();
231 // clean read and write callbacks
232 connection
->pendingReadLen
.reset();
233 connection
->writeCallback
.reset();
235 next_tag
= static_cast<Tag
>(0);
240 size_t ProtocolV2::get_current_msg_size() const {
241 ceph_assert(!rx_segments_desc
.empty());
243 // we don't include SegmentIndex::Msg::HEADER.
244 for (__u8 idx
= 1; idx
< rx_segments_desc
.size(); idx
++) {
245 sum
+= rx_segments_desc
[idx
].length
;
250 void ProtocolV2::reset_throttle() {
251 if (state
> THROTTLE_MESSAGE
&& state
<= THROTTLE_DONE
&&
252 connection
->policy
.throttler_messages
) {
253 ldout(cct
, 10) << __func__
<< " releasing " << 1
254 << " message to policy throttler "
255 << connection
->policy
.throttler_messages
->get_current()
256 << "/" << connection
->policy
.throttler_messages
->get_max()
258 connection
->policy
.throttler_messages
->put();
260 if (state
> THROTTLE_BYTES
&& state
<= THROTTLE_DONE
) {
261 if (connection
->policy
.throttler_bytes
) {
262 const size_t cur_msg_size
= get_current_msg_size();
263 ldout(cct
, 10) << __func__
<< " releasing " << cur_msg_size
264 << " bytes to policy throttler "
265 << connection
->policy
.throttler_bytes
->get_current() << "/"
266 << connection
->policy
.throttler_bytes
->get_max() << dendl
;
267 connection
->policy
.throttler_bytes
->put(cur_msg_size
);
270 if (state
> THROTTLE_DISPATCH_QUEUE
&& state
<= THROTTLE_DONE
) {
271 const size_t cur_msg_size
= get_current_msg_size();
273 << __func__
<< " releasing " << cur_msg_size
274 << " bytes to dispatch_queue throttler "
275 << connection
->dispatch_queue
->dispatch_throttler
.get_current() << "/"
276 << connection
->dispatch_queue
->dispatch_throttler
.get_max() << dendl
;
277 connection
->dispatch_queue
->dispatch_throttle_release(cur_msg_size
);
281 CtPtr
ProtocolV2::_fault() {
282 ldout(cct
, 10) << __func__
<< dendl
;
284 if (state
== CLOSED
|| state
== NONE
) {
285 ldout(cct
, 10) << __func__
<< " connection is already closed" << dendl
;
289 if (connection
->policy
.lossy
&&
290 !(state
>= START_CONNECT
&& state
<= SESSION_RECONNECTING
)) {
291 ldout(cct
, 2) << __func__
<< " on lossy channel, failing" << dendl
;
293 connection
->dispatch_queue
->queue_reset(connection
);
297 connection
->write_lock
.lock();
300 // requeue sent items
303 if (out_queue
.empty() && state
>= START_ACCEPT
&&
304 state
<= SESSION_ACCEPTING
&& !replacing
) {
305 ldout(cct
, 2) << __func__
<< " with nothing to send and in the half "
306 << " accept state just closed" << dendl
;
307 connection
->write_lock
.unlock();
309 connection
->dispatch_queue
->queue_reset(connection
);
317 reconnecting
= false;
319 if (connection
->policy
.standby
&& out_queue
.empty() && !keepalive
&&
321 ldout(cct
, 1) << __func__
<< " with nothing to send, going to standby"
324 connection
->write_lock
.unlock();
327 if (connection
->policy
.server
) {
328 ldout(cct
, 1) << __func__
<< " server, going to standby, even though i have stuff queued" << dendl
;
330 connection
->write_lock
.unlock();
334 connection
->write_lock
.unlock();
336 if (!(state
>= START_CONNECT
&& state
<= SESSION_RECONNECTING
) &&
338 state
!= SESSION_ACCEPTING
/* due to connection race */) {
339 // policy maybe empty when state is in accept
340 if (connection
->policy
.server
) {
341 ldout(cct
, 1) << __func__
<< " server, going to standby" << dendl
;
344 ldout(cct
, 1) << __func__
<< " initiating reconnect" << dendl
;
346 global_seq
= messenger
->get_global_seq();
347 state
= START_CONNECT
;
348 pre_auth
.enabled
= true;
349 connection
->state
= AsyncConnection::STATE_CONNECTING
;
352 connection
->center
->dispatch_event_external(connection
->read_handler
);
355 backoff
.set_from_double(cct
->_conf
->ms_max_backoff
);
356 } else if (backoff
== utime_t()) {
357 backoff
.set_from_double(cct
->_conf
->ms_initial_backoff
);
360 if (backoff
> cct
->_conf
->ms_max_backoff
)
361 backoff
.set_from_double(cct
->_conf
->ms_max_backoff
);
368 global_seq
= messenger
->get_global_seq();
369 state
= START_CONNECT
;
370 pre_auth
.enabled
= true;
371 connection
->state
= AsyncConnection::STATE_CONNECTING
;
372 ldout(cct
, 1) << __func__
<< " waiting " << backoff
<< dendl
;
374 connection
->register_time_events
.insert(
375 connection
->center
->create_time_event(backoff
.to_nsec() / 1000,
376 connection
->wakeup_handler
));
381 void ProtocolV2::prepare_send_message(uint64_t features
,
383 ldout(cct
, 20) << __func__
<< " m=" << *m
<< dendl
;
385 // associate message with Connection (for benefit of encode_payload)
386 if (m
->empty_payload()) {
387 ldout(cct
, 20) << __func__
<< " encoding features " << features
<< " " << m
388 << " " << *m
<< dendl
;
390 ldout(cct
, 20) << __func__
<< " half-reencoding features " << features
391 << " " << m
<< " " << *m
<< dendl
;
394 // encode and copy out of *m
395 m
->encode(features
, 0);
398 void ProtocolV2::send_message(Message
*m
) {
399 uint64_t f
= connection
->get_features();
401 // TODO: Currently not all messages supports reencode like MOSDMap, so here
402 // only let fast dispatch support messages prepare message
403 const bool can_fast_prepare
= messenger
->ms_can_fast_dispatch(m
);
404 if (can_fast_prepare
) {
405 prepare_send_message(f
, m
);
408 std::lock_guard
<std::mutex
> l(connection
->write_lock
);
409 bool is_prepared
= can_fast_prepare
;
410 // "features" changes will change the payload encoding
411 if (can_fast_prepare
&& (!can_write
|| connection
->get_features() != f
)) {
412 // ensure the correctness of message encoding
415 ldout(cct
, 10) << __func__
<< " clear encoded buffer previous " << f
416 << " != " << connection
->get_features() << dendl
;
418 if (state
== CLOSED
) {
419 ldout(cct
, 10) << __func__
<< " connection closed."
420 << " Drop message " << m
<< dendl
;
423 ldout(cct
, 5) << __func__
<< " enqueueing message m=" << m
424 << " type=" << m
->get_type() << " " << *m
<< dendl
;
425 m
->trace
.event("async enqueueing message");
426 out_queue
[m
->get_priority()].emplace_back(
427 out_queue_entry_t
{is_prepared
, m
});
428 ldout(cct
, 15) << __func__
<< " inline write is denied, reschedule m=" << m
430 if (((!replacing
&& can_write
) || state
== STANDBY
) && !write_in_progress
) {
431 write_in_progress
= true;
432 connection
->center
->dispatch_event_external(connection
->write_handler
);
437 void ProtocolV2::send_keepalive() {
438 ldout(cct
, 10) << __func__
<< dendl
;
439 std::lock_guard
<std::mutex
> l(connection
->write_lock
);
440 if (state
!= CLOSED
) {
442 connection
->center
->dispatch_event_external(connection
->write_handler
);
446 void ProtocolV2::read_event() {
447 ldout(cct
, 20) << __func__
<< dendl
;
451 run_continuation(CONTINUATION(start_client_banner_exchange
));
454 run_continuation(CONTINUATION(start_server_banner_exchange
));
457 run_continuation(CONTINUATION(read_frame
));
459 case THROTTLE_MESSAGE
:
460 run_continuation(CONTINUATION(throttle_message
));
463 run_continuation(CONTINUATION(throttle_bytes
));
465 case THROTTLE_DISPATCH_QUEUE
:
466 run_continuation(CONTINUATION(throttle_dispatch_queue
));
473 ProtocolV2::out_queue_entry_t
ProtocolV2::_get_next_outgoing() {
474 out_queue_entry_t out_entry
;
476 if (!out_queue
.empty()) {
477 auto it
= out_queue
.rbegin();
478 auto& entries
= it
->second
;
479 ceph_assert(!entries
.empty());
480 out_entry
= entries
.front();
482 if (entries
.empty()) {
483 out_queue
.erase(it
->first
);
489 ssize_t
ProtocolV2::write_message(Message
*m
, bool more
) {
491 ceph_assert(connection
->center
->in_thread());
492 m
->set_seq(++out_seq
);
494 connection
->lock
.lock();
495 uint64_t ack_seq
= in_seq
;
497 connection
->lock
.unlock();
499 ceph_msg_header
&header
= m
->get_header();
500 ceph_msg_footer
&footer
= m
->get_footer();
502 ceph_msg_header2 header2
{header
.seq
, header
.tid
,
503 header
.type
, header
.priority
,
507 footer
.flags
, header
.compat_version
,
510 auto message
= MessageFrame::Encode(
515 connection
->outcoming_bl
.append(message
.get_buffer(session_stream_handlers
));
517 ldout(cct
, 5) << __func__
<< " sending message m=" << m
518 << " seq=" << m
->get_seq() << " " << *m
<< dendl
;
520 m
->trace
.event("async writing message");
521 ldout(cct
, 20) << __func__
<< " sending m=" << m
<< " seq=" << m
->get_seq()
522 << " src=" << entity_name_t(messenger
->get_myname())
523 << " off=" << header2
.data_off
525 ssize_t total_send_size
= connection
->outcoming_bl
.length();
526 ssize_t rc
= connection
->_try_send(more
);
528 ldout(cct
, 1) << __func__
<< " error sending " << m
<< ", "
529 << cpp_strerror(rc
) << dendl
;
531 connection
->logger
->inc(
532 l_msgr_send_bytes
, total_send_size
- connection
->outcoming_bl
.length());
533 ldout(cct
, 10) << __func__
<< " sending " << m
534 << (rc
? " continuely." : " done.") << dendl
;
536 if (m
->get_type() == CEPH_MSG_OSD_OP
)
537 OID_EVENT_TRACE_WITH_MSG(m
, "SEND_MSG_OSD_OP_END", false);
538 else if (m
->get_type() == CEPH_MSG_OSD_OPREPLY
)
539 OID_EVENT_TRACE_WITH_MSG(m
, "SEND_MSG_OSD_OPREPLY_END", false);
545 void ProtocolV2::append_keepalive() {
546 ldout(cct
, 10) << __func__
<< dendl
;
547 auto keepalive_frame
= KeepAliveFrame::Encode();
548 connection
->outcoming_bl
.append(keepalive_frame
.get_buffer(session_stream_handlers
));
551 void ProtocolV2::append_keepalive_ack(utime_t
×tamp
) {
552 auto keepalive_ack_frame
= KeepAliveFrameAck::Encode(timestamp
);
553 connection
->outcoming_bl
.append(keepalive_ack_frame
.get_buffer(session_stream_handlers
));
556 void ProtocolV2::handle_message_ack(uint64_t seq
) {
557 if (connection
->policy
.lossy
) { // lossy connections don't keep sent messages
561 ldout(cct
, 15) << __func__
<< " seq=" << seq
<< dendl
;
564 static const int max_pending
= 128;
566 Message
*pending
[max_pending
];
567 connection
->write_lock
.lock();
568 while (!sent
.empty() && sent
.front()->get_seq() <= seq
&& i
< max_pending
) {
569 Message
*m
= sent
.front();
572 ldout(cct
, 10) << __func__
<< " got ack seq " << seq
573 << " >= " << m
->get_seq() << " on " << m
<< " " << *m
576 connection
->write_lock
.unlock();
577 for (int k
= 0; k
< i
; k
++) {
582 void ProtocolV2::write_event() {
583 ldout(cct
, 10) << __func__
<< dendl
;
586 connection
->write_lock
.lock();
593 auto start
= ceph::mono_clock::now();
596 const auto out_entry
= _get_next_outgoing();
601 if (!connection
->policy
.lossy
) {
603 sent
.push_back(out_entry
.m
);
606 more
= !out_queue
.empty();
607 connection
->write_lock
.unlock();
609 // send_message or requeue messages may not encode message
610 if (!out_entry
.is_prepared
) {
611 prepare_send_message(connection
->get_features(), out_entry
.m
);
614 r
= write_message(out_entry
.m
, more
);
616 connection
->write_lock
.lock();
620 ldout(cct
, 1) << __func__
<< " send msg failed" << dendl
;
625 write_in_progress
= false;
627 // if r > 0 mean data still lefted, so no need _try_send.
629 uint64_t left
= ack_left
;
633 auto ack
= AckFrame::Encode(in_seq
);
634 connection
->outcoming_bl
.append(ack
.get_buffer(session_stream_handlers
));
635 ldout(cct
, 10) << __func__
<< " try send msg ack, acked " << left
636 << " messages" << dendl
;
639 r
= connection
->_try_send(left
);
640 } else if (is_queued()) {
641 r
= connection
->_try_send();
644 connection
->write_lock
.unlock();
646 connection
->logger
->tinc(l_msgr_running_send_time
,
647 ceph::mono_clock::now() - start
);
649 ldout(cct
, 1) << __func__
<< " send msg failed" << dendl
;
650 connection
->lock
.lock();
652 connection
->lock
.unlock();
656 write_in_progress
= false;
657 connection
->write_lock
.unlock();
658 connection
->lock
.lock();
659 connection
->write_lock
.lock();
660 if (state
== STANDBY
&& !connection
->policy
.server
&& is_queued()) {
661 ldout(cct
, 10) << __func__
<< " policy.server is false" << dendl
;
662 if (server_cookie
) { // only increment connect_seq if there is a session
665 connection
->_connect();
666 } else if (connection
->cs
&& state
!= NONE
&& state
!= CLOSED
&&
667 state
!= START_CONNECT
) {
668 r
= connection
->_try_send();
670 ldout(cct
, 1) << __func__
<< " send outcoming bl failed" << dendl
;
671 connection
->write_lock
.unlock();
673 connection
->lock
.unlock();
677 connection
->write_lock
.unlock();
678 connection
->lock
.unlock();
682 bool ProtocolV2::is_queued() {
683 return !out_queue
.empty() || connection
->is_queued();
686 uint32_t ProtocolV2::get_onwire_size(const uint32_t logical_size
) const {
687 if (session_stream_handlers
.rx
) {
688 return segment_onwire_size(logical_size
);
694 uint32_t ProtocolV2::get_epilogue_size() const {
695 // In secure mode size of epilogue is flexible and depends on particular
696 // cipher implementation. See the comment for epilogue_secure_block_t or
697 // epilogue_plain_block_t.
698 if (session_stream_handlers
.rx
) {
699 return FRAME_SECURE_EPILOGUE_SIZE
+ \
700 session_stream_handlers
.rx
->get_extra_size_at_final();
702 return FRAME_PLAIN_EPILOGUE_SIZE
;
706 CtPtr
ProtocolV2::read(CONTINUATION_RXBPTR_TYPE
<ProtocolV2
> &next
,
707 rx_buffer_t
&&buffer
) {
708 const auto len
= buffer
->length();
709 const auto buf
= buffer
->c_str();
710 next
.node
= std::move(buffer
);
711 ssize_t r
= connection
->read(len
, buf
,
712 [&next
, this](char *buffer
, int r
) {
713 if (unlikely(pre_auth
.enabled
) && r
>= 0) {
714 pre_auth
.rxbuf
.append(*next
.node
);
715 ceph_assert(!cct
->_conf
->ms_die_on_bug
||
716 pre_auth
.rxbuf
.length() < 1000000);
719 run_continuation(next
);
722 // error or done synchronously
723 if (unlikely(pre_auth
.enabled
) && r
>= 0) {
724 pre_auth
.rxbuf
.append(*next
.node
);
725 ceph_assert(!cct
->_conf
->ms_die_on_bug
||
726 pre_auth
.rxbuf
.length() < 1000000);
736 CtPtr
ProtocolV2::write(const std::string
&desc
,
737 CONTINUATION_TYPE
<ProtocolV2
> &next
,
739 ceph::bufferlist bl
= frame
.get_buffer(session_stream_handlers
);
740 return write(desc
, next
, bl
);
743 CtPtr
ProtocolV2::write(const std::string
&desc
,
744 CONTINUATION_TYPE
<ProtocolV2
> &next
,
745 bufferlist
&buffer
) {
746 if (unlikely(pre_auth
.enabled
)) {
747 pre_auth
.txbuf
.append(buffer
);
748 ceph_assert(!cct
->_conf
->ms_die_on_bug
||
749 pre_auth
.txbuf
.length() < 1000000);
753 connection
->write(buffer
, [&next
, desc
, this](int r
) {
755 ldout(cct
, 1) << __func__
<< " " << desc
<< " write failed r=" << r
756 << " (" << cpp_strerror(r
) << ")" << dendl
;
757 connection
->inject_delay();
760 run_continuation(next
);
764 ldout(cct
, 1) << __func__
<< " " << desc
<< " write failed r=" << r
765 << " (" << cpp_strerror(r
) << ")" << dendl
;
775 CtPtr
ProtocolV2::_banner_exchange(CtRef callback
) {
776 ldout(cct
, 20) << __func__
<< dendl
;
777 bannerExchangeCallback
= &callback
;
779 bufferlist banner_payload
;
780 encode((uint64_t)CEPH_MSGR2_SUPPORTED_FEATURES
, banner_payload
, 0);
781 encode((uint64_t)CEPH_MSGR2_REQUIRED_FEATURES
, banner_payload
, 0);
784 bl
.append(CEPH_BANNER_V2_PREFIX
, strlen(CEPH_BANNER_V2_PREFIX
));
785 encode((uint16_t)banner_payload
.length(), bl
, 0);
786 bl
.claim_append(banner_payload
);
788 INTERCEPT(state
== BANNER_CONNECTING
? 3 : 4);
790 return WRITE(bl
, "banner", _wait_for_peer_banner
);
793 CtPtr
ProtocolV2::_wait_for_peer_banner() {
794 unsigned banner_len
= strlen(CEPH_BANNER_V2_PREFIX
) + sizeof(__le16
);
795 return READ(banner_len
, _handle_peer_banner
);
798 CtPtr
ProtocolV2::_handle_peer_banner(rx_buffer_t
&&buffer
, int r
) {
799 ldout(cct
, 20) << __func__
<< " r=" << r
<< dendl
;
802 ldout(cct
, 1) << __func__
<< " read peer banner failed r=" << r
<< " ("
803 << cpp_strerror(r
) << ")" << dendl
;
807 unsigned banner_prefix_len
= strlen(CEPH_BANNER_V2_PREFIX
);
809 if (memcmp(buffer
->c_str(), CEPH_BANNER_V2_PREFIX
, banner_prefix_len
)) {
810 if (memcmp(buffer
->c_str(), CEPH_BANNER
, strlen(CEPH_BANNER
)) == 0) {
811 lderr(cct
) << __func__
<< " peer " << *connection
->peer_addrs
812 << " is using msgr V1 protocol" << dendl
;
815 ldout(cct
, 1) << __func__
<< " accept peer sent bad banner" << dendl
;
819 uint16_t payload_len
;
821 buffer
->set_offset(banner_prefix_len
);
822 buffer
->set_length(sizeof(__le16
));
823 bl
.push_back(std::move(buffer
));
824 auto ti
= bl
.cbegin();
826 decode(payload_len
, ti
);
827 } catch (const buffer::error
&e
) {
828 lderr(cct
) << __func__
<< " decode banner payload len failed " << dendl
;
832 INTERCEPT(state
== BANNER_CONNECTING
? 5 : 6);
834 return READ(payload_len
, _handle_peer_banner_payload
);
837 CtPtr
ProtocolV2::_handle_peer_banner_payload(rx_buffer_t
&&buffer
, int r
) {
838 ldout(cct
, 20) << __func__
<< " r=" << r
<< dendl
;
841 ldout(cct
, 1) << __func__
<< " read peer banner payload failed r=" << r
842 << " (" << cpp_strerror(r
) << ")" << dendl
;
846 uint64_t peer_supported_features
;
847 uint64_t peer_required_features
;
850 bl
.push_back(std::move(buffer
));
851 auto ti
= bl
.cbegin();
853 decode(peer_supported_features
, ti
);
854 decode(peer_required_features
, ti
);
855 } catch (const buffer::error
&e
) {
856 lderr(cct
) << __func__
<< " decode banner payload failed " << dendl
;
860 ldout(cct
, 1) << __func__
<< " supported=" << std::hex
861 << peer_supported_features
<< " required=" << std::hex
862 << peer_required_features
<< std::dec
<< dendl
;
864 // Check feature bit compatibility
866 uint64_t supported_features
= CEPH_MSGR2_SUPPORTED_FEATURES
;
867 uint64_t required_features
= CEPH_MSGR2_REQUIRED_FEATURES
;
869 if ((required_features
& peer_supported_features
) != required_features
) {
870 ldout(cct
, 1) << __func__
<< " peer does not support all required features"
871 << " required=" << std::hex
<< required_features
872 << " supported=" << std::hex
<< peer_supported_features
873 << std::dec
<< dendl
;
875 connection
->dispatch_queue
->queue_reset(connection
);
878 if ((supported_features
& peer_required_features
) != peer_required_features
) {
879 ldout(cct
, 1) << __func__
<< " we do not support all peer required features"
880 << " required=" << std::hex
<< peer_required_features
881 << " supported=" << supported_features
<< std::dec
<< dendl
;
883 connection
->dispatch_queue
->queue_reset(connection
);
887 this->peer_required_features
= peer_required_features
;
888 if (this->peer_required_features
== 0) {
889 this->connection_features
= msgr2_required
;
892 // at this point we can change how the client protocol behaves based on
893 // this->peer_required_features
895 if (state
== BANNER_CONNECTING
) {
896 state
= HELLO_CONNECTING
;
899 ceph_assert(state
== BANNER_ACCEPTING
);
900 state
= HELLO_ACCEPTING
;
903 auto hello
= HelloFrame::Encode(messenger
->get_mytype(),
904 connection
->target_addr
);
906 INTERCEPT(state
== HELLO_CONNECTING
? 7 : 8);
908 return WRITE(hello
, "hello frame", read_frame
);
911 CtPtr
ProtocolV2::handle_hello(ceph::bufferlist
&payload
)
913 ldout(cct
, 20) << __func__
914 << " payload.length()=" << payload
.length() << dendl
;
916 if (state
!= HELLO_CONNECTING
&& state
!= HELLO_ACCEPTING
) {
917 lderr(cct
) << __func__
<< " not in hello exchange state!" << dendl
;
921 auto hello
= HelloFrame::Decode(payload
);
923 ldout(cct
, 5) << __func__
<< " received hello:"
924 << " peer_type=" << (int)hello
.entity_type()
925 << " peer_addr_for_me=" << hello
.peer_addr() << dendl
;
928 socklen_t len
= sizeof(ss
);
929 getsockname(connection
->cs
.fd(), (sockaddr
*)&ss
, &len
);
930 ldout(cct
, 5) << __func__
<< " getsockname says I am " << (sockaddr
*)&ss
931 << " when talking to " << connection
->target_addr
<< dendl
;
933 if (connection
->get_peer_type() == -1) {
934 connection
->set_peer_type(hello
.entity_type());
936 ceph_assert(state
== HELLO_ACCEPTING
);
937 connection
->policy
= messenger
->get_policy(hello
.entity_type());
938 ldout(cct
, 10) << __func__
<< " accept of host_type "
939 << (int)hello
.entity_type()
940 << ", policy.lossy=" << connection
->policy
.lossy
941 << " policy.server=" << connection
->policy
.server
942 << " policy.standby=" << connection
->policy
.standby
943 << " policy.resetcheck=" << connection
->policy
.resetcheck
946 ceph_assert(state
== HELLO_CONNECTING
);
947 if (connection
->get_peer_type() != hello
.entity_type()) {
948 ldout(cct
, 1) << __func__
<< " connection peer type does not match what"
949 << " peer advertises " << connection
->get_peer_type()
950 << " != " << (int)hello
.entity_type() << dendl
;
952 connection
->dispatch_queue
->queue_reset(connection
);
957 if (messenger
->get_myaddrs().empty() ||
958 messenger
->get_myaddrs().front().is_blank_ip()) {
960 if (cct
->_conf
->ms_learn_addr_from_peer
) {
961 ldout(cct
, 1) << __func__
<< " peer " << connection
->target_addr
962 << " says I am " << hello
.peer_addr() << " (socket says "
963 << (sockaddr
*)&ss
<< ")" << dendl
;
964 a
= hello
.peer_addr();
966 ldout(cct
, 1) << __func__
<< " socket to " << connection
->target_addr
967 << " says I am " << (sockaddr
*)&ss
968 << " (peer says " << hello
.peer_addr() << ")" << dendl
;
969 a
.set_sockaddr((sockaddr
*)&ss
);
971 a
.set_type(entity_addr_t::TYPE_MSGR2
); // anything but NONE; learned_addr ignores this
973 connection
->lock
.unlock();
974 messenger
->learned_addr(a
);
975 if (cct
->_conf
->ms_inject_internal_delays
&&
976 cct
->_conf
->ms_inject_socket_failures
) {
977 if (rand() % cct
->_conf
->ms_inject_socket_failures
== 0) {
978 ldout(cct
, 10) << __func__
<< " sleep for "
979 << cct
->_conf
->ms_inject_internal_delays
<< dendl
;
981 t
.set_from_double(cct
->_conf
->ms_inject_internal_delays
);
985 connection
->lock
.lock();
986 if (state
!= HELLO_CONNECTING
) {
987 ldout(cct
, 1) << __func__
988 << " state changed while learned_addr, mark_down or "
989 << " replacing must be happened just now" << dendl
;
997 callback
= bannerExchangeCallback
;
998 bannerExchangeCallback
= nullptr;
999 ceph_assert(callback
);
1003 CtPtr
ProtocolV2::read_frame() {
1004 if (state
== CLOSED
) {
1008 ldout(cct
, 20) << __func__
<< dendl
;
1009 return READ(FRAME_PREAMBLE_SIZE
, handle_read_frame_preamble_main
);
1012 CtPtr
ProtocolV2::handle_read_frame_preamble_main(rx_buffer_t
&&buffer
, int r
) {
1013 ldout(cct
, 20) << __func__
<< " r=" << r
<< dendl
;
1016 ldout(cct
, 1) << __func__
<< " read frame length and tag failed r=" << r
1017 << " (" << cpp_strerror(r
) << ")" << dendl
;
1021 ceph::bufferlist preamble
;
1022 preamble
.push_back(std::move(buffer
));
1024 ldout(cct
, 30) << __func__
<< " preamble\n";
1025 preamble
.hexdump(*_dout
);
1028 if (session_stream_handlers
.rx
) {
1029 ceph_assert(session_stream_handlers
.rx
);
1031 session_stream_handlers
.rx
->reset_rx_handler();
1032 preamble
= session_stream_handlers
.rx
->authenticated_decrypt_update(
1033 std::move(preamble
), segment_t::DEFAULT_ALIGNMENT
);
1035 ldout(cct
, 10) << __func__
<< " got encrypted preamble."
1036 << " after decrypt premable.length()=" << preamble
.length()
1039 ldout(cct
, 30) << __func__
<< " preamble after decrypt\n";
1040 preamble
.hexdump(*_dout
);
1045 // I expect ceph_le32 will make the endian conversion for me. Passing
1046 // everything through ::Decode is unnecessary.
1047 const auto& main_preamble
= \
1048 reinterpret_cast<preamble_block_t
&>(*preamble
.c_str());
1050 // verify preamble's CRC before any further processing
1051 const auto rx_crc
= ceph_crc32c(0,
1052 reinterpret_cast<const unsigned char*>(&main_preamble
),
1053 sizeof(main_preamble
) - sizeof(main_preamble
.crc
));
1054 if (rx_crc
!= main_preamble
.crc
) {
1055 ldout(cct
, 10) << __func__
<< " crc mismatch for main preamble"
1056 << " rx_crc=" << rx_crc
1057 << " tx_crc=" << main_preamble
.crc
<< dendl
;
1061 // currently we do support between 1 and MAX_NUM_SEGMENTS segments
1062 if (main_preamble
.num_segments
< 1 ||
1063 main_preamble
.num_segments
> MAX_NUM_SEGMENTS
) {
1064 ldout(cct
, 10) << __func__
<< " unsupported num_segments="
1065 << " tx_crc=" << main_preamble
.num_segments
<< dendl
;
1069 next_tag
= static_cast<Tag
>(main_preamble
.tag
);
1071 rx_segments_desc
.clear();
1072 rx_segments_data
.clear();
1074 if (main_preamble
.num_segments
> MAX_NUM_SEGMENTS
) {
1075 ldout(cct
, 30) << __func__
1076 << " num_segments=" << main_preamble
.num_segments
1077 << " is too much" << dendl
;
1080 for (std::uint8_t idx
= 0; idx
< main_preamble
.num_segments
; idx
++) {
1081 ldout(cct
, 10) << __func__
<< " got new segment:"
1082 << " len=" << main_preamble
.segments
[idx
].length
1083 << " align=" << main_preamble
.segments
[idx
].alignment
1085 rx_segments_desc
.emplace_back(main_preamble
.segments
[idx
]);
1089 // does it need throttle?
1090 if (next_tag
== Tag::MESSAGE
) {
1091 if (state
!= READY
) {
1092 lderr(cct
) << __func__
<< " not in ready state!" << dendl
;
1095 state
= THROTTLE_MESSAGE
;
1096 return CONTINUE(throttle_message
);
1098 return read_frame_segment();
1102 CtPtr
ProtocolV2::handle_read_frame_dispatch() {
1103 ldout(cct
, 10) << __func__
1104 << " tag=" << static_cast<uint32_t>(next_tag
) << dendl
;
1108 case Tag::AUTH_REQUEST
:
1109 case Tag::AUTH_BAD_METHOD
:
1110 case Tag::AUTH_REPLY_MORE
:
1111 case Tag::AUTH_REQUEST_MORE
:
1112 case Tag::AUTH_DONE
:
1113 case Tag::AUTH_SIGNATURE
:
1114 case Tag::CLIENT_IDENT
:
1115 case Tag::SERVER_IDENT
:
1116 case Tag::IDENT_MISSING_FEATURES
:
1117 case Tag::SESSION_RECONNECT
:
1118 case Tag::SESSION_RESET
:
1119 case Tag::SESSION_RETRY
:
1120 case Tag::SESSION_RETRY_GLOBAL
:
1121 case Tag::SESSION_RECONNECT_OK
:
1122 case Tag::KEEPALIVE2
:
1123 case Tag::KEEPALIVE2_ACK
:
1126 return handle_frame_payload();
1128 return handle_message();
1130 lderr(cct
) << __func__
1131 << " received unknown tag=" << static_cast<uint32_t>(next_tag
)
1140 CtPtr
ProtocolV2::read_frame_segment() {
1141 ldout(cct
, 20) << __func__
<< dendl
;
1142 ceph_assert(!rx_segments_desc
.empty());
1144 // description of current segment to read
1145 const auto& cur_rx_desc
= rx_segments_desc
.at(rx_segments_data
.size());
1146 rx_buffer_t rx_buffer
;
1148 rx_buffer
= buffer::ptr_node::create(buffer::create_aligned(
1149 get_onwire_size(cur_rx_desc
.length
), cur_rx_desc
.alignment
));
1150 } catch (std::bad_alloc
&) {
1151 // Catching because of potential issues with satisfying alignment.
1152 ldout(cct
, 20) << __func__
<< " can't allocate aligned rx_buffer "
1153 << " len=" << get_onwire_size(cur_rx_desc
.length
)
1154 << " align=" << cur_rx_desc
.alignment
1159 return READ_RXBUF(std::move(rx_buffer
), handle_read_frame_segment
);
1162 CtPtr
ProtocolV2::handle_read_frame_segment(rx_buffer_t
&&rx_buffer
, int r
) {
1163 ldout(cct
, 20) << __func__
<< " r=" << r
<< dendl
;
1166 ldout(cct
, 1) << __func__
<< " read frame segment failed r=" << r
<< " ("
1167 << cpp_strerror(r
) << ")" << dendl
;
1171 rx_segments_data
.emplace_back();
1172 rx_segments_data
.back().push_back(std::move(rx_buffer
));
1174 // decrypt incoming data
1175 // FIXME: if (auth_meta->is_mode_secure()) {
1176 if (session_stream_handlers
.rx
) {
1177 ceph_assert(session_stream_handlers
.rx
);
1179 auto& new_seg
= rx_segments_data
.back();
1180 if (new_seg
.length()) {
1181 auto padded
= session_stream_handlers
.rx
->authenticated_decrypt_update(
1182 std::move(new_seg
), segment_t::DEFAULT_ALIGNMENT
);
1183 const auto idx
= rx_segments_data
.size() - 1;
1185 padded
.splice(0, rx_segments_desc
[idx
].length
, &new_seg
);
1187 ldout(cct
, 20) << __func__
1188 << " unpadded new_seg.length()=" << new_seg
.length()
1193 if (rx_segments_desc
.size() == rx_segments_data
.size()) {
1194 // OK, all segments planned to read are read. Can go with epilogue.
1195 return READ(get_epilogue_size(), handle_read_frame_epilogue_main
);
1197 // TODO: for makeshift only. This will be more generic and throttled
1198 return read_frame_segment();
1202 CtPtr
ProtocolV2::handle_frame_payload() {
1203 ceph_assert(!rx_segments_data
.empty());
1204 auto& payload
= rx_segments_data
.back();
1206 ldout(cct
, 30) << __func__
<< "\n";
1207 payload
.hexdump(*_dout
);
1212 return handle_hello(payload
);
1213 case Tag::AUTH_REQUEST
:
1214 return handle_auth_request(payload
);
1215 case Tag::AUTH_BAD_METHOD
:
1216 return handle_auth_bad_method(payload
);
1217 case Tag::AUTH_REPLY_MORE
:
1218 return handle_auth_reply_more(payload
);
1219 case Tag::AUTH_REQUEST_MORE
:
1220 return handle_auth_request_more(payload
);
1221 case Tag::AUTH_DONE
:
1222 return handle_auth_done(payload
);
1223 case Tag::AUTH_SIGNATURE
:
1224 return handle_auth_signature(payload
);
1225 case Tag::CLIENT_IDENT
:
1226 return handle_client_ident(payload
);
1227 case Tag::SERVER_IDENT
:
1228 return handle_server_ident(payload
);
1229 case Tag::IDENT_MISSING_FEATURES
:
1230 return handle_ident_missing_features(payload
);
1231 case Tag::SESSION_RECONNECT
:
1232 return handle_reconnect(payload
);
1233 case Tag::SESSION_RESET
:
1234 return handle_session_reset(payload
);
1235 case Tag::SESSION_RETRY
:
1236 return handle_session_retry(payload
);
1237 case Tag::SESSION_RETRY_GLOBAL
:
1238 return handle_session_retry_global(payload
);
1239 case Tag::SESSION_RECONNECT_OK
:
1240 return handle_reconnect_ok(payload
);
1241 case Tag::KEEPALIVE2
:
1242 return handle_keepalive2(payload
);
1243 case Tag::KEEPALIVE2_ACK
:
1244 return handle_keepalive2_ack(payload
);
1246 return handle_message_ack(payload
);
1248 return handle_wait(payload
);
1255 CtPtr
ProtocolV2::ready() {
1256 ldout(cct
, 25) << __func__
<< dendl
;
1258 reconnecting
= false;
1261 // make sure no pending tick timer
1262 if (connection
->last_tick_id
) {
1263 connection
->center
->delete_time_event(connection
->last_tick_id
);
1265 connection
->last_tick_id
= connection
->center
->create_time_event(
1266 connection
->inactive_timeout_us
, connection
->tick_handler
);
1269 std::lock_guard
<std::mutex
> l(connection
->write_lock
);
1271 if (!out_queue
.empty()) {
1272 connection
->center
->dispatch_event_external(connection
->write_handler
);
1276 connection
->maybe_start_delay_thread();
1279 ldout(cct
, 1) << __func__
<< " entity=" << peer_name
<< " client_cookie="
1280 << std::hex
<< client_cookie
<< " server_cookie="
1281 << server_cookie
<< std::dec
<< " in_seq=" << in_seq
1282 << " out_seq=" << out_seq
<< dendl
;
1286 return CONTINUE(read_frame
);
1289 CtPtr
ProtocolV2::handle_read_frame_epilogue_main(rx_buffer_t
&&buffer
, int r
)
1291 ldout(cct
, 20) << __func__
<< " r=" << r
<< dendl
;
1294 ldout(cct
, 1) << __func__
<< " read data error " << dendl
;
1300 // FIXME: if (auth_meta->is_mode_secure()) {
1301 if (session_stream_handlers
.rx
) {
1302 ldout(cct
, 1) << __func__
<< " read frame epilogue bytes="
1303 << get_epilogue_size() << dendl
;
1305 // decrypt epilogue and authenticate entire frame.
1306 ceph::bufferlist epilogue_bl
;
1308 epilogue_bl
.push_back(std::move(buffer
));
1311 session_stream_handlers
.rx
->authenticated_decrypt_update_final(
1312 std::move(epilogue_bl
), segment_t::DEFAULT_ALIGNMENT
);
1313 } catch (ceph::crypto::onwire::MsgAuthError
&e
) {
1314 ldout(cct
, 5) << __func__
<< " message authentication failed: "
1315 << e
.what() << dendl
;
1320 reinterpret_cast<epilogue_plain_block_t
&>(*epilogue_bl
.c_str());
1321 late_flags
= epilogue
.late_flags
;
1323 auto& epilogue
= reinterpret_cast<epilogue_plain_block_t
&>(*buffer
->c_str());
1325 for (std::uint8_t idx
= 0; idx
< rx_segments_data
.size(); idx
++) {
1326 const __u32 expected_crc
= epilogue
.crc_values
[idx
];
1327 const __u32 calculated_crc
= rx_segments_data
[idx
].crc32c(-1);
1328 if (expected_crc
!= calculated_crc
) {
1329 ldout(cct
, 5) << __func__
<< " message integrity check failed: "
1330 << " expected_crc=" << expected_crc
1331 << " calculated_crc=" << calculated_crc
1335 ldout(cct
, 20) << __func__
<< " message integrity check success: "
1336 << " expected_crc=" << expected_crc
1337 << " calculated_crc=" << calculated_crc
1341 late_flags
= epilogue
.late_flags
;
1344 // we do have a mechanism that allows transmitter to start sending message
1345 // and abort after putting entire data field on wire. This will be used by
1346 // the kernel client to avoid unnecessary buffering.
1347 if (late_flags
& FRAME_FLAGS_LATEABRT
) {
1350 return CONTINUE(read_frame
);
1352 return handle_read_frame_dispatch();
1356 CtPtr
ProtocolV2::handle_message() {
1357 ldout(cct
, 20) << __func__
<< dendl
;
1358 ceph_assert(state
== THROTTLE_DONE
);
1360 #if defined(WITH_LTTNG) && defined(WITH_EVENTTRACE)
1361 ltt_recv_stamp
= ceph_clock_now();
1363 recv_stamp
= ceph_clock_now();
1365 // we need to get the size before std::moving segments data
1366 const size_t cur_msg_size
= get_current_msg_size();
1367 auto msg_frame
= MessageFrame::Decode(std::move(rx_segments_data
));
1369 // XXX: paranoid copy just to avoid oops
1370 ceph_msg_header2 current_header
= msg_frame
.header();
1372 ldout(cct
, 5) << __func__
1373 << " got " << msg_frame
.front_len()
1374 << " + " << msg_frame
.middle_len()
1375 << " + " << msg_frame
.data_len()
1377 << " envelope type=" << current_header
.type
1378 << " src " << peer_name
1379 << " off " << current_header
.data_off
1383 ceph_msg_header header
{current_header
.seq
,
1385 current_header
.type
,
1386 current_header
.priority
,
1387 current_header
.version
,
1388 msg_frame
.front_len(),
1389 msg_frame
.middle_len(),
1390 msg_frame
.data_len(),
1391 current_header
.data_off
,
1393 current_header
.compat_version
,
1394 current_header
.reserved
,
1396 ceph_msg_footer footer
{0, 0, 0, 0, current_header
.flags
};
1398 Message
*message
= decode_message(cct
, 0, header
, footer
,
1404 ldout(cct
, 1) << __func__
<< " decode message failed " << dendl
;
1407 state
= READ_MESSAGE_COMPLETE
;
1412 message
->set_byte_throttler(connection
->policy
.throttler_bytes
);
1413 message
->set_message_throttler(connection
->policy
.throttler_messages
);
1415 // store reservation size in message, so we don't get confused
1416 // by messages entering the dispatch queue through other paths.
1417 message
->set_dispatch_throttle_size(cur_msg_size
);
1419 message
->set_recv_stamp(recv_stamp
);
1420 message
->set_throttle_stamp(throttle_stamp
);
1421 message
->set_recv_complete_stamp(ceph_clock_now());
1423 // check received seq#. if it is old, drop the message.
1424 // note that incoming messages may skip ahead. this is convenient for the
1425 // client side queueing because messages can't be renumbered, but the (kernel)
1426 // client will occasionally pull a message out of the sent queue to send
1427 // elsewhere. in that case it doesn't matter if we "got" it or not.
1428 uint64_t cur_seq
= in_seq
;
1429 if (message
->get_seq() <= cur_seq
) {
1430 ldout(cct
, 0) << __func__
<< " got old message " << message
->get_seq()
1431 << " <= " << cur_seq
<< " " << message
<< " " << *message
1432 << ", discarding" << dendl
;
1434 if (connection
->has_feature(CEPH_FEATURE_RECONNECT_SEQ
) &&
1435 cct
->_conf
->ms_die_on_old_message
) {
1436 ceph_assert(0 == "old msgs despite reconnect_seq feature");
1440 if (message
->get_seq() > cur_seq
+ 1) {
1441 ldout(cct
, 0) << __func__
<< " missed message? skipped from seq "
1442 << cur_seq
<< " to " << message
->get_seq() << dendl
;
1443 if (cct
->_conf
->ms_die_on_skipped_message
) {
1444 ceph_assert(0 == "skipped incoming seq");
1448 #if defined(WITH_LTTNG) && defined(WITH_EVENTTRACE)
1449 if (message
->get_type() == CEPH_MSG_OSD_OP
||
1450 message
->get_type() == CEPH_MSG_OSD_OPREPLY
) {
1451 utime_t ltt_processed_stamp
= ceph_clock_now();
1452 double usecs_elapsed
=
1453 (ltt_processed_stamp
.to_nsec() - ltt_recv_stamp
.to_nsec()) / 1000;
1455 if (message
->get_type() == CEPH_MSG_OSD_OP
)
1456 OID_ELAPSED_WITH_MSG(message
, usecs_elapsed
, "TIME_TO_DECODE_OSD_OP",
1459 OID_ELAPSED_WITH_MSG(message
, usecs_elapsed
, "TIME_TO_DECODE_OSD_OPREPLY",
1464 // note last received message.
1465 in_seq
= message
->get_seq();
1466 ldout(cct
, 5) << __func__
<< " received message m=" << message
1467 << " seq=" << message
->get_seq()
1468 << " from=" << message
->get_source() << " type=" << header
.type
1469 << " " << *message
<< dendl
;
1471 bool need_dispatch_writer
= false;
1472 if (!connection
->policy
.lossy
) {
1474 need_dispatch_writer
= true;
1479 connection
->logger
->inc(l_msgr_recv_messages
);
1480 connection
->logger
->inc(
1482 cur_msg_size
+ sizeof(ceph_msg_header
) + sizeof(ceph_msg_footer
));
1484 messenger
->ms_fast_preprocess(message
);
1485 auto fast_dispatch_time
= ceph::mono_clock::now();
1486 connection
->logger
->tinc(l_msgr_running_recv_time
,
1487 fast_dispatch_time
- connection
->recv_start_time
);
1488 if (connection
->delay_state
) {
1489 double delay_period
= 0;
1490 if (rand() % 10000 < cct
->_conf
->ms_inject_delay_probability
* 10000.0) {
1492 cct
->_conf
->ms_inject_delay_max
* (double)(rand() % 10000) / 10000.0;
1493 ldout(cct
, 1) << "queue_received will delay after "
1494 << (ceph_clock_now() + delay_period
) << " on " << message
1495 << " " << *message
<< dendl
;
1497 connection
->delay_state
->queue(delay_period
, message
);
1498 } else if (messenger
->ms_can_fast_dispatch(message
)) {
1499 connection
->lock
.unlock();
1500 connection
->dispatch_queue
->fast_dispatch(message
);
1501 connection
->recv_start_time
= ceph::mono_clock::now();
1502 connection
->logger
->tinc(l_msgr_running_fast_dispatch_time
,
1503 connection
->recv_start_time
- fast_dispatch_time
);
1504 connection
->lock
.lock();
1506 connection
->dispatch_queue
->enqueue(message
, message
->get_priority(),
1507 connection
->conn_id
);
1510 handle_message_ack(current_header
.ack_seq
);
1512 // we might have been reused by another connection
1513 // let's check if that is the case
1514 if (state
!= READY
) {
1515 // yes, that was the case, let's do nothing
1519 if (need_dispatch_writer
&& connection
->is_connected()) {
1520 connection
->center
->dispatch_event_external(connection
->write_handler
);
1523 return CONTINUE(read_frame
);
1527 CtPtr
ProtocolV2::throttle_message() {
1528 ldout(cct
, 20) << __func__
<< dendl
;
1530 if (connection
->policy
.throttler_messages
) {
1531 ldout(cct
, 10) << __func__
<< " wants " << 1
1532 << " message from policy throttler "
1533 << connection
->policy
.throttler_messages
->get_current()
1534 << "/" << connection
->policy
.throttler_messages
->get_max()
1536 if (!connection
->policy
.throttler_messages
->get_or_fail()) {
1537 ldout(cct
, 10) << __func__
<< " wants 1 message from policy throttle "
1538 << connection
->policy
.throttler_messages
->get_current()
1539 << "/" << connection
->policy
.throttler_messages
->get_max()
1540 << " failed, just wait." << dendl
;
1541 // following thread pool deal with th full message queue isn't a
1542 // short time, so we can wait a ms.
1543 if (connection
->register_time_events
.empty()) {
1544 connection
->register_time_events
.insert(
1545 connection
->center
->create_time_event(1000,
1546 connection
->wakeup_handler
));
1552 state
= THROTTLE_BYTES
;
1553 return CONTINUE(throttle_bytes
);
1556 CtPtr
ProtocolV2::throttle_bytes() {
1557 ldout(cct
, 20) << __func__
<< dendl
;
1559 const size_t cur_msg_size
= get_current_msg_size();
1561 if (connection
->policy
.throttler_bytes
) {
1562 ldout(cct
, 10) << __func__
<< " wants " << cur_msg_size
1563 << " bytes from policy throttler "
1564 << connection
->policy
.throttler_bytes
->get_current() << "/"
1565 << connection
->policy
.throttler_bytes
->get_max() << dendl
;
1566 if (!connection
->policy
.throttler_bytes
->get_or_fail(cur_msg_size
)) {
1567 ldout(cct
, 10) << __func__
<< " wants " << cur_msg_size
1568 << " bytes from policy throttler "
1569 << connection
->policy
.throttler_bytes
->get_current()
1570 << "/" << connection
->policy
.throttler_bytes
->get_max()
1571 << " failed, just wait." << dendl
;
1572 // following thread pool deal with th full message queue isn't a
1573 // short time, so we can wait a ms.
1574 if (connection
->register_time_events
.empty()) {
1575 connection
->register_time_events
.insert(
1576 connection
->center
->create_time_event(
1577 1000, connection
->wakeup_handler
));
1584 state
= THROTTLE_DISPATCH_QUEUE
;
1585 return CONTINUE(throttle_dispatch_queue
);
1588 CtPtr
ProtocolV2::throttle_dispatch_queue() {
1589 ldout(cct
, 20) << __func__
<< dendl
;
1591 const size_t cur_msg_size
= get_current_msg_size();
1593 if (!connection
->dispatch_queue
->dispatch_throttler
.get_or_fail(
1596 << __func__
<< " wants " << cur_msg_size
1597 << " bytes from dispatch throttle "
1598 << connection
->dispatch_queue
->dispatch_throttler
.get_current() << "/"
1599 << connection
->dispatch_queue
->dispatch_throttler
.get_max()
1600 << " failed, just wait." << dendl
;
1601 // following thread pool deal with th full message queue isn't a
1602 // short time, so we can wait a ms.
1603 if (connection
->register_time_events
.empty()) {
1604 connection
->register_time_events
.insert(
1605 connection
->center
->create_time_event(1000,
1606 connection
->wakeup_handler
));
1612 throttle_stamp
= ceph_clock_now();
1613 state
= THROTTLE_DONE
;
1615 return read_frame_segment();
1618 CtPtr
ProtocolV2::handle_keepalive2(ceph::bufferlist
&payload
)
1620 ldout(cct
, 20) << __func__
1621 << " payload.length()=" << payload
.length() << dendl
;
1623 if (state
!= READY
) {
1624 lderr(cct
) << __func__
<< " not in ready state!" << dendl
;
1628 auto keepalive_frame
= KeepAliveFrame::Decode(payload
);
1630 ldout(cct
, 30) << __func__
<< " got KEEPALIVE2 tag ..." << dendl
;
1632 connection
->write_lock
.lock();
1633 append_keepalive_ack(keepalive_frame
.timestamp());
1634 connection
->write_lock
.unlock();
1636 ldout(cct
, 20) << __func__
<< " got KEEPALIVE2 "
1637 << keepalive_frame
.timestamp() << dendl
;
1638 connection
->set_last_keepalive(ceph_clock_now());
1640 if (is_connected()) {
1641 connection
->center
->dispatch_event_external(connection
->write_handler
);
1644 return CONTINUE(read_frame
);
1647 CtPtr
ProtocolV2::handle_keepalive2_ack(ceph::bufferlist
&payload
)
1649 ldout(cct
, 20) << __func__
1650 << " payload.length()=" << payload
.length() << dendl
;
1652 if (state
!= READY
) {
1653 lderr(cct
) << __func__
<< " not in ready state!" << dendl
;
1657 auto keepalive_ack_frame
= KeepAliveFrameAck::Decode(payload
);
1658 connection
->set_last_keepalive_ack(keepalive_ack_frame
.timestamp());
1659 ldout(cct
, 20) << __func__
<< " got KEEPALIVE_ACK" << dendl
;
1661 return CONTINUE(read_frame
);
1664 CtPtr
ProtocolV2::handle_message_ack(ceph::bufferlist
&payload
)
1666 ldout(cct
, 20) << __func__
1667 << " payload.length()=" << payload
.length() << dendl
;
1669 if (state
!= READY
) {
1670 lderr(cct
) << __func__
<< " not in ready state!" << dendl
;
1674 auto ack
= AckFrame::Decode(payload
);
1675 handle_message_ack(ack
.seq());
1676 return CONTINUE(read_frame
);
1679 /* Client Protocol Methods */
1681 CtPtr
ProtocolV2::start_client_banner_exchange() {
1682 ldout(cct
, 20) << __func__
<< dendl
;
1686 state
= BANNER_CONNECTING
;
1688 global_seq
= messenger
->get_global_seq();
1690 return _banner_exchange(CONTINUATION(post_client_banner_exchange
));
1693 CtPtr
ProtocolV2::post_client_banner_exchange() {
1694 ldout(cct
, 20) << __func__
<< dendl
;
1696 state
= AUTH_CONNECTING
;
1698 return send_auth_request();
1701 CtPtr
ProtocolV2::send_auth_request(std::vector
<uint32_t> &allowed_methods
) {
1702 ldout(cct
, 20) << __func__
<< " peer_type " << (int)connection
->peer_type
1703 << " auth_client " << messenger
->auth_client
<< dendl
;
1704 ceph_assert(messenger
->auth_client
);
1707 vector
<uint32_t> preferred_modes
;
1708 auto am
= auth_meta
;
1709 connection
->lock
.unlock();
1710 int r
= messenger
->auth_client
->get_auth_request(
1711 connection
, am
.get(),
1712 &am
->auth_method
, &preferred_modes
, &bl
);
1713 connection
->lock
.lock();
1714 if (state
!= AUTH_CONNECTING
) {
1715 ldout(cct
, 1) << __func__
<< " state changed!" << dendl
;
1719 ldout(cct
, 0) << __func__
<< " get_initial_auth_request returned " << r
1722 connection
->dispatch_queue
->queue_reset(connection
);
1728 auto frame
= AuthRequestFrame::Encode(auth_meta
->auth_method
, preferred_modes
,
1730 return WRITE(frame
, "auth request", read_frame
);
1733 CtPtr
ProtocolV2::handle_auth_bad_method(ceph::bufferlist
&payload
) {
1734 ldout(cct
, 20) << __func__
1735 << " payload.length()=" << payload
.length() << dendl
;
1737 if (state
!= AUTH_CONNECTING
) {
1738 lderr(cct
) << __func__
<< " not in auth connect state!" << dendl
;
1742 auto bad_method
= AuthBadMethodFrame::Decode(payload
);
1743 ldout(cct
, 1) << __func__
<< " method=" << bad_method
.method()
1744 << " result " << cpp_strerror(bad_method
.result())
1745 << ", allowed methods=" << bad_method
.allowed_methods()
1746 << ", allowed modes=" << bad_method
.allowed_modes()
1748 ceph_assert(messenger
->auth_client
);
1749 auto am
= auth_meta
;
1750 connection
->lock
.unlock();
1751 int r
= messenger
->auth_client
->handle_auth_bad_method(
1754 bad_method
.method(), bad_method
.result(),
1755 bad_method
.allowed_methods(),
1756 bad_method
.allowed_modes());
1757 connection
->lock
.lock();
1758 if (state
!= AUTH_CONNECTING
|| r
< 0) {
1761 return send_auth_request(bad_method
.allowed_methods());
1764 CtPtr
ProtocolV2::handle_auth_reply_more(ceph::bufferlist
&payload
)
1766 ldout(cct
, 20) << __func__
1767 << " payload.length()=" << payload
.length() << dendl
;
1769 if (state
!= AUTH_CONNECTING
) {
1770 lderr(cct
) << __func__
<< " not in auth connect state!" << dendl
;
1774 auto auth_more
= AuthReplyMoreFrame::Decode(payload
);
1775 ldout(cct
, 5) << __func__
1776 << " auth reply more len=" << auth_more
.auth_payload().length()
1778 ceph_assert(messenger
->auth_client
);
1779 ceph::bufferlist reply
;
1780 auto am
= auth_meta
;
1781 connection
->lock
.unlock();
1782 int r
= messenger
->auth_client
->handle_auth_reply_more(
1783 connection
, am
.get(), auth_more
.auth_payload(), &reply
);
1784 connection
->lock
.lock();
1785 if (state
!= AUTH_CONNECTING
) {
1786 ldout(cct
, 1) << __func__
<< " state changed!" << dendl
;
1790 lderr(cct
) << __func__
<< " auth_client handle_auth_reply_more returned "
1794 auto more_reply
= AuthRequestMoreFrame::Encode(reply
);
1795 return WRITE(more_reply
, "auth request more", read_frame
);
1798 CtPtr
ProtocolV2::handle_auth_done(ceph::bufferlist
&payload
)
1800 ldout(cct
, 20) << __func__
1801 << " payload.length()=" << payload
.length() << dendl
;
1803 if (state
!= AUTH_CONNECTING
) {
1804 lderr(cct
) << __func__
<< " not in auth connect state!" << dendl
;
1808 auto auth_done
= AuthDoneFrame::Decode(payload
);
1810 ceph_assert(messenger
->auth_client
);
1811 auto am
= auth_meta
;
1812 connection
->lock
.unlock();
1813 int r
= messenger
->auth_client
->handle_auth_done(
1816 auth_done
.global_id(),
1817 auth_done
.con_mode(),
1818 auth_done
.auth_payload(),
1820 &am
->connection_secret
);
1821 connection
->lock
.lock();
1822 if (state
!= AUTH_CONNECTING
) {
1823 ldout(cct
, 1) << __func__
<< " state changed!" << dendl
;
1829 auth_meta
->con_mode
= auth_done
.con_mode();
1830 session_stream_handlers
= \
1831 ceph::crypto::onwire::rxtx_t::create_handler_pair(cct
, *auth_meta
, false);
1833 state
= AUTH_CONNECTING_SIGN
;
1835 const auto sig
= auth_meta
->session_key
.empty() ? sha256_digest_t() :
1836 auth_meta
->session_key
.hmac_sha256(cct
, pre_auth
.rxbuf
);
1837 auto sig_frame
= AuthSignatureFrame::Encode(sig
);
1838 pre_auth
.enabled
= false;
1839 pre_auth
.rxbuf
.clear();
1840 return WRITE(sig_frame
, "auth signature", read_frame
);
1843 CtPtr
ProtocolV2::finish_client_auth() {
1844 if (!server_cookie
) {
1845 ceph_assert(connect_seq
== 0);
1846 state
= SESSION_CONNECTING
;
1847 return send_client_ident();
1848 } else { // reconnecting to previous session
1849 state
= SESSION_RECONNECTING
;
1850 ceph_assert(connect_seq
> 0);
1851 return send_reconnect();
1855 CtPtr
ProtocolV2::send_client_ident() {
1856 ldout(cct
, 20) << __func__
<< dendl
;
1858 if (!connection
->policy
.lossy
&& !client_cookie
) {
1859 client_cookie
= ceph::util::generate_random_number
<uint64_t>(1, -1ll);
1863 if (connection
->policy
.lossy
) {
1864 flags
|= CEPH_MSG_CONNECT_LOSSY
;
1867 auto client_ident
= ClientIdentFrame::Encode(
1868 messenger
->get_myaddrs(),
1869 connection
->target_addr
,
1870 messenger
->get_myname().num(),
1872 connection
->policy
.features_supported
,
1873 connection
->policy
.features_required
| msgr2_required
,
1877 ldout(cct
, 5) << __func__
<< " sending identification: "
1878 << "addrs=" << messenger
->get_myaddrs()
1879 << " target=" << connection
->target_addr
1880 << " gid=" << messenger
->get_myname().num()
1881 << " global_seq=" << global_seq
1882 << " features_supported=" << std::hex
1883 << connection
->policy
.features_supported
1884 << " features_required="
1885 << (connection
->policy
.features_required
| msgr2_required
)
1886 << " flags=" << flags
1887 << " cookie=" << client_cookie
<< std::dec
<< dendl
;
1891 return WRITE(client_ident
, "client ident", read_frame
);
1894 CtPtr
ProtocolV2::send_reconnect() {
1895 ldout(cct
, 20) << __func__
<< dendl
;
1897 auto reconnect
= ReconnectFrame::Encode(messenger
->get_myaddrs(),
1904 ldout(cct
, 5) << __func__
<< " reconnect to session: client_cookie="
1905 << std::hex
<< client_cookie
<< " server_cookie="
1906 << server_cookie
<< std::dec
1907 << " gs=" << global_seq
<< " cs=" << connect_seq
1908 << " ms=" << in_seq
<< dendl
;
1912 return WRITE(reconnect
, "reconnect", read_frame
);
1915 CtPtr
ProtocolV2::handle_ident_missing_features(ceph::bufferlist
&payload
)
1917 ldout(cct
, 20) << __func__
1918 << " payload.length()=" << payload
.length() << dendl
;
1920 if (state
!= SESSION_CONNECTING
) {
1921 lderr(cct
) << __func__
<< " not in session connect state!" << dendl
;
1925 auto ident_missing
=
1926 IdentMissingFeaturesFrame::Decode(payload
);
1927 lderr(cct
) << __func__
1928 << " client does not support all server features: " << std::hex
1929 << ident_missing
.features() << std::dec
<< dendl
;
1934 CtPtr
ProtocolV2::handle_session_reset(ceph::bufferlist
&payload
)
1936 ldout(cct
, 20) << __func__
1937 << " payload.length()=" << payload
.length() << dendl
;
1939 if (state
!= SESSION_RECONNECTING
) {
1940 lderr(cct
) << __func__
<< " not in session reconnect state!" << dendl
;
1944 auto reset
= ResetFrame::Decode(payload
);
1946 ldout(cct
, 1) << __func__
<< " received session reset full=" << reset
.full()
1956 state
= SESSION_CONNECTING
;
1957 return send_client_ident();
1960 CtPtr
ProtocolV2::handle_session_retry(ceph::bufferlist
&payload
)
1962 ldout(cct
, 20) << __func__
1963 << " payload.length()=" << payload
.length() << dendl
;
1965 if (state
!= SESSION_RECONNECTING
) {
1966 lderr(cct
) << __func__
<< " not in session reconnect state!" << dendl
;
1970 auto retry
= RetryFrame::Decode(payload
);
1971 connect_seq
= retry
.connect_seq() + 1;
1973 ldout(cct
, 1) << __func__
1974 << " received session retry connect_seq=" << retry
.connect_seq()
1975 << ", inc to cs=" << connect_seq
<< dendl
;
1977 return send_reconnect();
1980 CtPtr
ProtocolV2::handle_session_retry_global(ceph::bufferlist
&payload
)
1982 ldout(cct
, 20) << __func__
1983 << " payload.length()=" << payload
.length() << dendl
;
1985 if (state
!= SESSION_RECONNECTING
) {
1986 lderr(cct
) << __func__
<< " not in session reconnect state!" << dendl
;
1990 auto retry
= RetryGlobalFrame::Decode(payload
);
1991 global_seq
= messenger
->get_global_seq(retry
.global_seq());
1993 ldout(cct
, 1) << __func__
<< " received session retry global global_seq="
1994 << retry
.global_seq() << ", choose new gs=" << global_seq
1997 return send_reconnect();
2000 CtPtr
ProtocolV2::handle_wait(ceph::bufferlist
&payload
) {
2001 ldout(cct
, 20) << __func__
2002 << " received WAIT (connection race)"
2003 << " payload.length()=" << payload
.length()
2006 if (state
!= SESSION_CONNECTING
&& state
!= SESSION_RECONNECTING
) {
2007 lderr(cct
) << __func__
<< " not in session (re)connect state!" << dendl
;
2012 WaitFrame::Decode(payload
);
2016 CtPtr
ProtocolV2::handle_reconnect_ok(ceph::bufferlist
&payload
)
2018 ldout(cct
, 20) << __func__
2019 << " payload.length()=" << payload
.length() << dendl
;
2021 if (state
!= SESSION_RECONNECTING
) {
2022 lderr(cct
) << __func__
<< " not in session reconnect state!" << dendl
;
2026 auto reconnect_ok
= ReconnectOkFrame::Decode(payload
);
2027 ldout(cct
, 5) << __func__
2028 << " reconnect accepted: sms=" << reconnect_ok
.msg_seq()
2031 out_seq
= discard_requeued_up_to(out_seq
, reconnect_ok
.msg_seq());
2033 backoff
= utime_t();
2034 ldout(cct
, 10) << __func__
<< " reconnect success " << connect_seq
2035 << ", lossy = " << connection
->policy
.lossy
<< ", features "
2036 << connection
->get_features() << dendl
;
2038 if (connection
->delay_state
) {
2039 ceph_assert(connection
->delay_state
->ready());
2042 connection
->dispatch_queue
->queue_connect(connection
);
2043 messenger
->ms_deliver_handle_fast_connect(connection
);
2048 CtPtr
ProtocolV2::handle_server_ident(ceph::bufferlist
&payload
)
2050 ldout(cct
, 20) << __func__
2051 << " payload.length()=" << payload
.length() << dendl
;
2053 if (state
!= SESSION_CONNECTING
) {
2054 lderr(cct
) << __func__
<< " not in session connect state!" << dendl
;
2058 auto server_ident
= ServerIdentFrame::Decode(payload
);
2059 ldout(cct
, 5) << __func__
<< " received server identification:"
2060 << " addrs=" << server_ident
.addrs()
2061 << " gid=" << server_ident
.gid()
2062 << " global_seq=" << server_ident
.global_seq()
2063 << " features_supported=" << std::hex
2064 << server_ident
.supported_features()
2065 << " features_required=" << server_ident
.required_features()
2066 << " flags=" << server_ident
.flags() << " cookie=" << std::dec
2067 << server_ident
.cookie() << dendl
;
2069 // is this who we intended to talk to?
2070 // be a bit forgiving here, since we may be connecting based on addresses parsed out
2071 // of mon_host or something.
2072 if (!server_ident
.addrs().contains(connection
->target_addr
)) {
2073 ldout(cct
,1) << __func__
<< " peer identifies as " << server_ident
.addrs()
2074 << ", does not include " << connection
->target_addr
<< dendl
;
2078 server_cookie
= server_ident
.cookie();
2080 connection
->set_peer_addrs(server_ident
.addrs());
2081 peer_name
= entity_name_t(connection
->get_peer_type(), server_ident
.gid());
2082 connection
->set_features(server_ident
.supported_features() &
2083 connection
->policy
.features_supported
);
2084 peer_global_seq
= server_ident
.global_seq();
2086 connection
->policy
.lossy
= server_ident
.flags() & CEPH_MSG_CONNECT_LOSSY
;
2088 backoff
= utime_t();
2089 ldout(cct
, 10) << __func__
<< " connect success " << connect_seq
2090 << ", lossy = " << connection
->policy
.lossy
<< ", features "
2091 << connection
->get_features() << dendl
;
2093 if (connection
->delay_state
) {
2094 ceph_assert(connection
->delay_state
->ready());
2097 connection
->dispatch_queue
->queue_connect(connection
);
2098 messenger
->ms_deliver_handle_fast_connect(connection
);
2103 /* Server Protocol Methods */
2105 CtPtr
ProtocolV2::start_server_banner_exchange() {
2106 ldout(cct
, 20) << __func__
<< dendl
;
2110 state
= BANNER_ACCEPTING
;
2112 return _banner_exchange(CONTINUATION(post_server_banner_exchange
));
2115 CtPtr
ProtocolV2::post_server_banner_exchange() {
2116 ldout(cct
, 20) << __func__
<< dendl
;
2118 state
= AUTH_ACCEPTING
;
2120 return CONTINUE(read_frame
);
2123 CtPtr
ProtocolV2::handle_auth_request(ceph::bufferlist
&payload
) {
2124 ldout(cct
, 20) << __func__
<< " payload.length()=" << payload
.length()
2127 if (state
!= AUTH_ACCEPTING
) {
2128 lderr(cct
) << __func__
<< " not in auth accept state!" << dendl
;
2132 auto request
= AuthRequestFrame::Decode(payload
);
2133 ldout(cct
, 10) << __func__
<< " AuthRequest(method=" << request
.method()
2134 << ", preferred_modes=" << request
.preferred_modes()
2135 << ", payload_len=" << request
.auth_payload().length() << ")"
2137 auth_meta
->auth_method
= request
.method();
2138 auth_meta
->con_mode
= messenger
->auth_server
->pick_con_mode(
2139 connection
->get_peer_type(), auth_meta
->auth_method
,
2140 request
.preferred_modes());
2141 if (auth_meta
->con_mode
== CEPH_CON_MODE_UNKNOWN
) {
2142 return _auth_bad_method(-EOPNOTSUPP
);
2144 return _handle_auth_request(request
.auth_payload(), false);
2147 CtPtr
ProtocolV2::_auth_bad_method(int r
)
2150 std::vector
<uint32_t> allowed_methods
;
2151 std::vector
<uint32_t> allowed_modes
;
2152 messenger
->auth_server
->get_supported_auth_methods(
2153 connection
->get_peer_type(), &allowed_methods
, &allowed_modes
);
2154 ldout(cct
, 1) << __func__
<< " auth_method " << auth_meta
->auth_method
2155 << " r " << cpp_strerror(r
)
2156 << ", allowed_methods " << allowed_methods
2157 << ", allowed_modes " << allowed_modes
2159 auto bad_method
= AuthBadMethodFrame::Encode(auth_meta
->auth_method
, r
,
2160 allowed_methods
, allowed_modes
);
2161 return WRITE(bad_method
, "bad auth method", read_frame
);
2164 CtPtr
ProtocolV2::_handle_auth_request(bufferlist
& auth_payload
, bool more
)
2166 if (!messenger
->auth_server
) {
2170 auto am
= auth_meta
;
2171 connection
->lock
.unlock();
2172 int r
= messenger
->auth_server
->handle_auth_request(
2173 connection
, am
.get(),
2174 more
, am
->auth_method
, auth_payload
,
2176 connection
->lock
.lock();
2177 if (state
!= AUTH_ACCEPTING
&& state
!= AUTH_ACCEPTING_MORE
) {
2178 ldout(cct
, 1) << __func__
2179 << " state changed while accept, it must be mark_down"
2181 ceph_assert(state
== CLOSED
);
2186 state
= AUTH_ACCEPTING_SIGN
;
2188 auto auth_done
= AuthDoneFrame::Encode(connection
->peer_global_id
,
2189 auth_meta
->con_mode
,
2191 return WRITE(auth_done
, "auth done", finish_auth
);
2192 } else if (r
== 0) {
2193 state
= AUTH_ACCEPTING_MORE
;
2195 auto more
= AuthReplyMoreFrame::Encode(reply
);
2196 return WRITE(more
, "auth reply more", read_frame
);
2197 } else if (r
== -EBUSY
) {
2198 // kick the client and maybe they'll come back later
2201 return _auth_bad_method(r
);
2205 CtPtr
ProtocolV2::finish_auth()
2207 ceph_assert(auth_meta
);
2208 // TODO: having a possibility to check whether we're server or client could
2209 // allow reusing finish_auth().
2210 session_stream_handlers
= \
2211 ceph::crypto::onwire::rxtx_t::create_handler_pair(cct
, *auth_meta
, true);
2213 const auto sig
= auth_meta
->session_key
.empty() ? sha256_digest_t() :
2214 auth_meta
->session_key
.hmac_sha256(cct
, pre_auth
.rxbuf
);
2215 auto sig_frame
= AuthSignatureFrame::Encode(sig
);
2216 pre_auth
.enabled
= false;
2217 pre_auth
.rxbuf
.clear();
2218 return WRITE(sig_frame
, "auth signature", read_frame
);
2221 CtPtr
ProtocolV2::handle_auth_request_more(ceph::bufferlist
&payload
)
2223 ldout(cct
, 20) << __func__
2224 << " payload.length()=" << payload
.length() << dendl
;
2226 if (state
!= AUTH_ACCEPTING_MORE
) {
2227 lderr(cct
) << __func__
<< " not in auth accept more state!" << dendl
;
2231 auto auth_more
= AuthRequestMoreFrame::Decode(payload
);
2232 return _handle_auth_request(auth_more
.auth_payload(), true);
2235 CtPtr
ProtocolV2::handle_auth_signature(ceph::bufferlist
&payload
)
2237 ldout(cct
, 20) << __func__
2238 << " payload.length()=" << payload
.length() << dendl
;
2240 if (state
!= AUTH_ACCEPTING_SIGN
&& state
!= AUTH_CONNECTING_SIGN
) {
2241 lderr(cct
) << __func__
2242 << " pre-auth verification signature seen in wrong state!"
2247 auto sig_frame
= AuthSignatureFrame::Decode(payload
);
2249 const auto actual_tx_sig
= auth_meta
->session_key
.empty() ?
2250 sha256_digest_t() : auth_meta
->session_key
.hmac_sha256(cct
, pre_auth
.txbuf
);
2251 if (sig_frame
.signature() != actual_tx_sig
) {
2252 ldout(cct
, 2) << __func__
<< " pre-auth signature mismatch"
2253 << " actual_tx_sig=" << actual_tx_sig
2254 << " sig_frame.signature()=" << sig_frame
.signature()
2258 ldout(cct
, 20) << __func__
<< " pre-auth signature success"
2259 << " sig_frame.signature()=" << sig_frame
.signature()
2261 pre_auth
.txbuf
.clear();
2264 if (state
== AUTH_ACCEPTING_SIGN
) {
2265 // server had sent AuthDone and client responded with correct pre-auth
2266 // signature. we can start accepting new sessions/reconnects.
2267 state
= SESSION_ACCEPTING
;
2268 return CONTINUE(read_frame
);
2269 } else if (state
== AUTH_CONNECTING_SIGN
) {
2270 // this happened at client side
2271 return finish_client_auth();
2273 ceph_assert_always("state corruption" == nullptr);
2277 CtPtr
ProtocolV2::handle_client_ident(ceph::bufferlist
&payload
)
2279 ldout(cct
, 20) << __func__
2280 << " payload.length()=" << payload
.length() << dendl
;
2282 if (state
!= SESSION_ACCEPTING
) {
2283 lderr(cct
) << __func__
<< " not in session accept state!" << dendl
;
2287 auto client_ident
= ClientIdentFrame::Decode(payload
);
2289 ldout(cct
, 5) << __func__
<< " received client identification:"
2290 << " addrs=" << client_ident
.addrs()
2291 << " target=" << client_ident
.target_addr()
2292 << " gid=" << client_ident
.gid()
2293 << " global_seq=" << client_ident
.global_seq()
2294 << " features_supported=" << std::hex
2295 << client_ident
.supported_features()
2296 << " features_required=" << client_ident
.required_features()
2297 << " flags=" << client_ident
.flags()
2298 << " cookie=" << client_ident
.cookie() << std::dec
<< dendl
;
2300 if (client_ident
.addrs().empty() ||
2301 client_ident
.addrs().front() == entity_addr_t()) {
2302 ldout(cct
,5) << __func__
<< " oops, client_ident.addrs() is empty" << dendl
;
2303 return _fault(); // a v2 peer should never do this
2305 if (!messenger
->get_myaddrs().contains(client_ident
.target_addr())) {
2306 ldout(cct
,5) << __func__
<< " peer is trying to reach "
2307 << client_ident
.target_addr()
2308 << " which is not us (" << messenger
->get_myaddrs() << ")"
2313 connection
->set_peer_addrs(client_ident
.addrs());
2314 connection
->target_addr
= connection
->_infer_target_addr(client_ident
.addrs());
2316 peer_name
= entity_name_t(connection
->get_peer_type(), client_ident
.gid());
2317 connection
->set_peer_id(client_ident
.gid());
2319 client_cookie
= client_ident
.cookie();
2321 uint64_t feat_missing
=
2322 (connection
->policy
.features_required
| msgr2_required
) &
2323 ~(uint64_t)client_ident
.supported_features();
2325 ldout(cct
, 1) << __func__
<< " peer missing required features " << std::hex
2326 << feat_missing
<< std::dec
<< dendl
;
2327 auto ident_missing_features
=
2328 IdentMissingFeaturesFrame::Encode(feat_missing
);
2330 return WRITE(ident_missing_features
, "ident missing features", read_frame
);
2333 connection_features
=
2334 client_ident
.supported_features() & connection
->policy
.features_supported
;
2336 peer_global_seq
= client_ident
.global_seq();
2338 // Looks good so far, let's check if there is already an existing connection
2341 connection
->lock
.unlock();
2342 AsyncConnectionRef existing
= messenger
->lookup_conn(*connection
->peer_addrs
);
2345 existing
->protocol
->proto_type
!= 2) {
2346 ldout(cct
,1) << __func__
<< " existing " << existing
<< " proto "
2347 << existing
->protocol
.get() << " version is "
2348 << existing
->protocol
->proto_type
<< ", marking down" << dendl
;
2349 existing
->mark_down();
2353 connection
->inject_delay();
2355 connection
->lock
.lock();
2356 if (state
!= SESSION_ACCEPTING
) {
2357 ldout(cct
, 1) << __func__
2358 << " state changed while accept, it must be mark_down"
2360 ceph_assert(state
== CLOSED
);
2365 return handle_existing_connection(existing
);
2368 // if everything is OK reply with server identification
2369 return send_server_ident();
2372 CtPtr
ProtocolV2::handle_reconnect(ceph::bufferlist
&payload
)
2374 ldout(cct
, 20) << __func__
2375 << " payload.length()=" << payload
.length() << dendl
;
2377 if (state
!= SESSION_ACCEPTING
) {
2378 lderr(cct
) << __func__
<< " not in session accept state!" << dendl
;
2382 auto reconnect
= ReconnectFrame::Decode(payload
);
2384 ldout(cct
, 5) << __func__
2385 << " received reconnect:"
2386 << " client_cookie=" << std::hex
<< reconnect
.client_cookie()
2387 << " server_cookie=" << reconnect
.server_cookie() << std::dec
2388 << " gs=" << reconnect
.global_seq()
2389 << " cs=" << reconnect
.connect_seq()
2390 << " ms=" << reconnect
.msg_seq()
2393 // Should we check if one of the ident.addrs match connection->target_addr
2394 // as we do in ProtocolV1?
2395 connection
->set_peer_addrs(reconnect
.addrs());
2396 connection
->target_addr
= connection
->_infer_target_addr(reconnect
.addrs());
2397 peer_global_seq
= reconnect
.global_seq();
2399 connection
->lock
.unlock();
2400 AsyncConnectionRef existing
= messenger
->lookup_conn(*connection
->peer_addrs
);
2403 existing
->protocol
->proto_type
!= 2) {
2404 ldout(cct
,1) << __func__
<< " existing " << existing
<< " proto "
2405 << existing
->protocol
.get() << " version is "
2406 << existing
->protocol
->proto_type
<< ", marking down" << dendl
;
2407 existing
->mark_down();
2411 connection
->inject_delay();
2413 connection
->lock
.lock();
2414 if (state
!= SESSION_ACCEPTING
) {
2415 ldout(cct
, 1) << __func__
2416 << " state changed while accept, it must be mark_down"
2418 ceph_assert(state
== CLOSED
);
2423 // there is no existing connection therefore cannot reconnect to previous
2425 ldout(cct
, 0) << __func__
2426 << " no existing connection exists, reseting client" << dendl
;
2427 auto reset
= ResetFrame::Encode(true);
2428 return WRITE(reset
, "session reset", read_frame
);
2431 std::lock_guard
<std::mutex
> l(existing
->lock
);
2433 ProtocolV2
*exproto
= dynamic_cast<ProtocolV2
*>(existing
->protocol
.get());
2435 ldout(cct
, 1) << __func__
<< " existing=" << existing
<< dendl
;
2439 if (exproto
->state
== CLOSED
) {
2440 ldout(cct
, 5) << __func__
<< " existing " << existing
2441 << " already closed. Reseting client" << dendl
;
2442 auto reset
= ResetFrame::Encode(true);
2443 return WRITE(reset
, "session reset", read_frame
);
2446 if (exproto
->replacing
) {
2447 ldout(cct
, 1) << __func__
2448 << " existing racing replace happened while replacing."
2449 << " existing=" << existing
<< dendl
;
2450 auto retry
= RetryGlobalFrame::Encode(exproto
->peer_global_seq
);
2451 return WRITE(retry
, "session retry", read_frame
);
2454 if (exproto
->client_cookie
!= reconnect
.client_cookie()) {
2455 ldout(cct
, 1) << __func__
<< " existing=" << existing
2456 << " client cookie mismatch, I must have reseted:"
2457 << " cc=" << std::hex
<< exproto
->client_cookie
2458 << " rcc=" << reconnect
.client_cookie()
2459 << ", reseting client." << std::dec
2461 auto reset
= ResetFrame::Encode(connection
->policy
.resetcheck
);
2462 return WRITE(reset
, "session reset", read_frame
);
2463 } else if (exproto
->server_cookie
== 0) {
2464 // this happens when:
2465 // - a connects to b
2466 // - a sends client_ident
2467 // - b gets client_ident, sends server_ident and sets cookie X
2468 // - connection fault
2469 // - b reconnects to a with cookie X, connect_seq=1
2470 // - a has cookie==0
2471 ldout(cct
, 1) << __func__
<< " I was a client and didn't received the"
2472 << " server_ident. Asking peer to resume session"
2473 << " establishment" << dendl
;
2474 auto reset
= ResetFrame::Encode(false);
2475 return WRITE(reset
, "session reset", read_frame
);
2478 if (exproto
->peer_global_seq
> reconnect
.global_seq()) {
2479 ldout(cct
, 5) << __func__
2480 << " stale global_seq: sgs=" << exproto
->peer_global_seq
2481 << " cgs=" << reconnect
.global_seq()
2482 << ", ask client to retry global" << dendl
;
2483 auto retry
= RetryGlobalFrame::Encode(exproto
->peer_global_seq
);
2487 return WRITE(retry
, "session retry", read_frame
);
2490 if (exproto
->connect_seq
> reconnect
.connect_seq()) {
2491 ldout(cct
, 5) << __func__
2492 << " stale connect_seq scs=" << exproto
->connect_seq
2493 << " ccs=" << reconnect
.connect_seq()
2494 << " , ask client to retry" << dendl
;
2495 auto retry
= RetryFrame::Encode(exproto
->connect_seq
);
2496 return WRITE(retry
, "session retry", read_frame
);
2499 if (exproto
->connect_seq
== reconnect
.connect_seq()) {
2500 // reconnect race: both peers are sending reconnect messages
2501 if (existing
->peer_addrs
->msgr2_addr() >
2502 messenger
->get_myaddrs().msgr2_addr() &&
2503 !existing
->policy
.server
) {
2504 // the existing connection wins
2507 << " reconnect race detected, this connection loses to existing="
2508 << existing
<< dendl
;
2510 auto wait
= WaitFrame::Encode();
2511 return WRITE(wait
, "wait", read_frame
);
2513 // this connection wins
2514 ldout(cct
, 1) << __func__
2515 << " reconnect race detected, replacing existing="
2516 << existing
<< " socket by this connection's socket"
2521 ldout(cct
, 1) << __func__
<< " reconnect to existing=" << existing
<< dendl
;
2523 reconnecting
= true;
2525 // everything looks good
2526 exproto
->connect_seq
= reconnect
.connect_seq();
2527 exproto
->message_seq
= reconnect
.msg_seq();
2529 return reuse_connection(existing
, exproto
);
2532 CtPtr
ProtocolV2::handle_existing_connection(AsyncConnectionRef existing
) {
2533 ldout(cct
, 20) << __func__
<< " existing=" << existing
<< dendl
;
2535 std::lock_guard
<std::mutex
> l(existing
->lock
);
2537 ProtocolV2
*exproto
= dynamic_cast<ProtocolV2
*>(existing
->protocol
.get());
2539 ldout(cct
, 1) << __func__
<< " existing=" << existing
<< dendl
;
2543 if (exproto
->state
== CLOSED
) {
2544 ldout(cct
, 1) << __func__
<< " existing " << existing
<< " already closed."
2546 return send_server_ident();
2549 if (exproto
->replacing
) {
2550 ldout(cct
, 1) << __func__
2551 << " existing racing replace happened while replacing."
2552 << " existing=" << existing
<< dendl
;
2553 auto wait
= WaitFrame::Encode();
2554 return WRITE(wait
, "wait", read_frame
);
2557 if (exproto
->peer_global_seq
> peer_global_seq
) {
2558 ldout(cct
, 1) << __func__
<< " this is a stale connection, peer_global_seq="
2560 << " existing->peer_global_seq=" << exproto
->peer_global_seq
2561 << ", stopping this connection." << dendl
;
2563 connection
->dispatch_queue
->queue_reset(connection
);
2567 if (existing
->policy
.lossy
) {
2568 // existing connection can be thrown out in favor of this one
2570 << __func__
<< " existing=" << existing
2571 << " is a lossy channel. Stopping existing in favor of this connection"
2573 existing
->protocol
->stop();
2574 existing
->dispatch_queue
->queue_reset(existing
.get());
2575 return send_server_ident();
2578 if (exproto
->server_cookie
&& exproto
->client_cookie
&&
2579 exproto
->client_cookie
!= client_cookie
) {
2580 // Found previous session
2581 // peer has reseted and we're going to reuse the existing connection
2582 // by replacing the communication socket
2583 ldout(cct
, 1) << __func__
<< " found previous session existing=" << existing
2584 << ", peer must have reseted." << dendl
;
2585 if (connection
->policy
.resetcheck
) {
2586 exproto
->reset_session();
2588 return reuse_connection(existing
, exproto
);
2591 if (exproto
->client_cookie
== client_cookie
) {
2592 // session establishment interrupted between client_ident and server_ident,
2594 ldout(cct
, 1) << __func__
<< " found previous session existing=" << existing
2595 << ", continuing session establishment." << dendl
;
2596 return reuse_connection(existing
, exproto
);
2599 if (exproto
->state
== READY
|| exproto
->state
== STANDBY
) {
2600 ldout(cct
, 1) << __func__
<< " existing=" << existing
2601 << " is READY/STANDBY, lets reuse it" << dendl
;
2602 return reuse_connection(existing
, exproto
);
2605 // Looks like a connection race: server and client are both connecting to
2606 // each other at the same time.
2607 if (connection
->peer_addrs
->msgr2_addr() <
2608 messenger
->get_myaddrs().msgr2_addr() ||
2609 existing
->policy
.server
) {
2610 // this connection wins
2611 ldout(cct
, 1) << __func__
2612 << " connection race detected, replacing existing="
2613 << existing
<< " socket by this connection's socket" << dendl
;
2614 return reuse_connection(existing
, exproto
);
2616 // the existing connection wins
2619 << " connection race detected, this connection loses to existing="
2620 << existing
<< dendl
;
2621 ceph_assert(connection
->peer_addrs
->msgr2_addr() >
2622 messenger
->get_myaddrs().msgr2_addr());
2624 // make sure we follow through with opening the existing
2625 // connection (if it isn't yet open) since we know the peer
2626 // has something to send to us.
2627 existing
->send_keepalive();
2628 auto wait
= WaitFrame::Encode();
2629 return WRITE(wait
, "wait", read_frame
);
2633 CtPtr
ProtocolV2::reuse_connection(AsyncConnectionRef existing
,
2634 ProtocolV2
*exproto
) {
2635 ldout(cct
, 20) << __func__
<< " existing=" << existing
2636 << " reconnect=" << reconnecting
<< dendl
;
2638 connection
->inject_delay();
2640 std::lock_guard
<std::mutex
> l(existing
->write_lock
);
2642 connection
->center
->delete_file_event(connection
->cs
.fd(),
2643 EVENT_READABLE
| EVENT_WRITABLE
);
2645 if (existing
->delay_state
) {
2646 existing
->delay_state
->flush();
2647 ceph_assert(!connection
->delay_state
);
2649 exproto
->reset_recv_state();
2650 exproto
->pre_auth
.enabled
= false;
2652 if (!reconnecting
) {
2653 exproto
->client_cookie
= client_cookie
;
2654 exproto
->peer_name
= peer_name
;
2655 exproto
->connection_features
= connection_features
;
2656 existing
->set_features(connection_features
);
2658 exproto
->peer_global_seq
= peer_global_seq
;
2660 auto temp_cs
= std::move(connection
->cs
);
2661 EventCenter
*new_center
= connection
->center
;
2662 Worker
*new_worker
= connection
->worker
;
2664 ldout(messenger
->cct
, 5) << __func__
<< " stop myself to swap existing"
2667 std::swap(exproto
->session_stream_handlers
, session_stream_handlers
);
2668 exproto
->auth_meta
= auth_meta
;
2670 // avoid _stop shutdown replacing socket
2671 // queue a reset on the new connection, which we're dumping for the old
2674 connection
->dispatch_queue
->queue_reset(connection
);
2676 exproto
->can_write
= false;
2677 exproto
->write_in_progress
= false;
2678 exproto
->reconnecting
= reconnecting
;
2679 exproto
->replacing
= true;
2680 existing
->state_offset
= 0;
2681 // avoid previous thread modify event
2682 exproto
->state
= NONE
;
2683 existing
->state
= AsyncConnection::STATE_NONE
;
2684 // Discard existing prefetch buffer in `recv_buf`
2685 existing
->recv_start
= existing
->recv_end
= 0;
2686 // there shouldn't exist any buffer
2687 ceph_assert(connection
->recv_start
== connection
->recv_end
);
2689 auto deactivate_existing
= std::bind(
2690 [existing
, new_worker
, new_center
, exproto
](ConnectedSocket
&cs
) mutable {
2691 // we need to delete time event in original thread
2693 std::lock_guard
<std::mutex
> l(existing
->lock
);
2694 existing
->write_lock
.lock();
2695 exproto
->requeue_sent();
2696 existing
->outcoming_bl
.clear();
2697 existing
->open_write
= false;
2698 existing
->write_lock
.unlock();
2699 if (exproto
->state
== NONE
) {
2700 existing
->shutdown_socket();
2701 existing
->cs
= std::move(cs
);
2702 existing
->worker
->references
--;
2703 new_worker
->references
++;
2704 existing
->logger
= new_worker
->get_perf_counter();
2705 existing
->worker
= new_worker
;
2706 existing
->center
= new_center
;
2707 if (existing
->delay_state
)
2708 existing
->delay_state
->set_center(new_center
);
2709 } else if (exproto
->state
== CLOSED
) {
2710 auto back_to_close
= std::bind(
2711 [](ConnectedSocket
&cs
) mutable { cs
.close(); }, std::move(cs
));
2712 new_center
->submit_to(new_center
->get_id(),
2713 std::move(back_to_close
), true);
2720 // Before changing existing->center, it may already exists some
2721 // events in existing->center's queue. Then if we mark down
2722 // `existing`, it will execute in another thread and clean up
2723 // connection. Previous event will result in segment fault
2724 auto transfer_existing
= [existing
, exproto
]() mutable {
2725 std::lock_guard
<std::mutex
> l(existing
->lock
);
2726 if (exproto
->state
== CLOSED
) return;
2727 ceph_assert(exproto
->state
== NONE
);
2729 exproto
->state
= SESSION_ACCEPTING
;
2730 // we have called shutdown_socket above
2731 ceph_assert(existing
->last_tick_id
== 0);
2732 // restart timer since we are going to re-build connection
2733 existing
->last_connect_started
= ceph::coarse_mono_clock::now();
2734 existing
->last_tick_id
= existing
->center
->create_time_event(
2735 existing
->connect_timeout_us
, existing
->tick_handler
);
2736 existing
->state
= AsyncConnection::STATE_CONNECTION_ESTABLISHED
;
2737 existing
->center
->create_file_event(existing
->cs
.fd(), EVENT_READABLE
,
2738 existing
->read_handler
);
2739 if (!exproto
->reconnecting
) {
2740 exproto
->run_continuation(exproto
->send_server_ident());
2742 exproto
->run_continuation(exproto
->send_reconnect_ok());
2745 if (existing
->center
->in_thread())
2746 transfer_existing();
2748 existing
->center
->submit_to(existing
->center
->get_id(),
2749 std::move(transfer_existing
), true);
2751 std::move(temp_cs
));
2753 existing
->center
->submit_to(existing
->center
->get_id(),
2754 std::move(deactivate_existing
), true);
2758 CtPtr
ProtocolV2::send_server_ident() {
2759 ldout(cct
, 20) << __func__
<< dendl
;
2761 // this is required for the case when this connection is being replaced
2762 out_seq
= discard_requeued_up_to(out_seq
, 0);
2765 if (!connection
->policy
.lossy
) {
2766 server_cookie
= ceph::util::generate_random_number
<uint64_t>(1, -1ll);
2770 if (connection
->policy
.lossy
) {
2771 flags
= flags
| CEPH_MSG_CONNECT_LOSSY
;
2774 uint64_t gs
= messenger
->get_global_seq();
2775 auto server_ident
= ServerIdentFrame::Encode(
2776 messenger
->get_myaddrs(),
2777 messenger
->get_myname().num(),
2779 connection
->policy
.features_supported
,
2780 connection
->policy
.features_required
| msgr2_required
,
2784 ldout(cct
, 5) << __func__
<< " sending identification:"
2785 << " addrs=" << messenger
->get_myaddrs()
2786 << " gid=" << messenger
->get_myname().num()
2787 << " global_seq=" << gs
<< " features_supported=" << std::hex
2788 << connection
->policy
.features_supported
2789 << " features_required="
2790 << (connection
->policy
.features_required
| msgr2_required
)
2791 << " flags=" << flags
<< " cookie=" << std::dec
<< server_cookie
2794 connection
->lock
.unlock();
2795 // Because "replacing" will prevent other connections preempt this addr,
2796 // it's safe that here we don't acquire Connection's lock
2797 ssize_t r
= messenger
->accept_conn(connection
);
2799 connection
->inject_delay();
2801 connection
->lock
.lock();
2804 ldout(cct
, 1) << __func__
<< " existing race replacing process for addr = "
2805 << connection
->peer_addrs
->msgr2_addr()
2806 << " just fail later one(this)" << dendl
;
2807 connection
->inject_delay();
2810 if (state
!= SESSION_ACCEPTING
) {
2811 ldout(cct
, 1) << __func__
2812 << " state changed while accept_conn, it must be mark_down"
2814 ceph_assert(state
== CLOSED
|| state
== NONE
);
2815 messenger
->unregister_conn(connection
);
2816 connection
->inject_delay();
2820 connection
->set_features(connection_features
);
2823 connection
->dispatch_queue
->queue_accept(connection
);
2824 messenger
->ms_deliver_handle_fast_accept(connection
);
2828 return WRITE(server_ident
, "server ident", server_ready
);
2831 CtPtr
ProtocolV2::server_ready() {
2832 ldout(cct
, 20) << __func__
<< dendl
;
2834 if (connection
->delay_state
) {
2835 ceph_assert(connection
->delay_state
->ready());
2841 CtPtr
ProtocolV2::send_reconnect_ok() {
2842 ldout(cct
, 20) << __func__
<< dendl
;
2844 out_seq
= discard_requeued_up_to(out_seq
, message_seq
);
2846 uint64_t ms
= in_seq
;
2847 auto reconnect_ok
= ReconnectOkFrame::Encode(ms
);
2849 ldout(cct
, 5) << __func__
<< " sending reconnect_ok: msg_seq=" << ms
<< dendl
;
2851 connection
->lock
.unlock();
2852 // Because "replacing" will prevent other connections preempt this addr,
2853 // it's safe that here we don't acquire Connection's lock
2854 ssize_t r
= messenger
->accept_conn(connection
);
2856 connection
->inject_delay();
2858 connection
->lock
.lock();
2861 ldout(cct
, 1) << __func__
<< " existing race replacing process for addr = "
2862 << connection
->peer_addrs
->msgr2_addr()
2863 << " just fail later one(this)" << dendl
;
2864 connection
->inject_delay();
2867 if (state
!= SESSION_ACCEPTING
) {
2868 ldout(cct
, 1) << __func__
2869 << " state changed while accept_conn, it must be mark_down"
2871 ceph_assert(state
== CLOSED
|| state
== NONE
);
2872 messenger
->unregister_conn(connection
);
2873 connection
->inject_delay();
2878 connection
->dispatch_queue
->queue_accept(connection
);
2879 messenger
->ms_deliver_handle_fast_accept(connection
);
2883 return WRITE(reconnect_ok
, "reconnect ok", server_ready
);