1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
18 #include <boost/range/adaptor/map.hpp>
19 #include <boost/range/adaptor/filtered.hpp>
20 #include <boost/range/algorithm/copy.hpp>
21 #include <boost/range/algorithm_ext/copy_n.hpp>
22 #include "common/weighted_shuffle.h"
24 #include "include/random.h"
25 #include "include/scope_guard.h"
26 #include "include/stringify.h"
28 #include "messages/MMonGetMap.h"
29 #include "messages/MMonGetVersion.h"
30 #include "messages/MMonGetMap.h"
31 #include "messages/MMonGetVersionReply.h"
32 #include "messages/MMonMap.h"
33 #include "messages/MConfig.h"
34 #include "messages/MGetConfig.h"
35 #include "messages/MAuth.h"
36 #include "messages/MLogAck.h"
37 #include "messages/MAuthReply.h"
38 #include "messages/MMonCommand.h"
39 #include "messages/MMonCommandAck.h"
40 #include "messages/MCommand.h"
41 #include "messages/MCommandReply.h"
42 #include "messages/MPing.h"
44 #include "messages/MMonSubscribe.h"
45 #include "messages/MMonSubscribeAck.h"
46 #include "common/errno.h"
47 #include "common/hostname.h"
48 #include "common/LogClient.h"
50 #include "MonClient.h"
51 #include "error_code.h"
54 #include "auth/Auth.h"
55 #include "auth/KeyRing.h"
56 #include "auth/AuthClientHandler.h"
57 #include "auth/AuthRegistry.h"
58 #include "auth/RotatingKeyRing.h"
60 #define dout_subsys ceph_subsys_monc
62 #define dout_prefix *_dout << "monclient" << (_hunting() ? "(hunting)":"") << ": "
64 namespace bs
= boost::system
;
66 using namespace std::literals
;
68 MonClient::MonClient(CephContext
*cct_
, boost::asio::io_context
& service
) :
72 timer(cct_
, monc_lock
),
76 more_log_pending(false),
78 had_a_connection(false),
79 reopen_interval_multiplier(
80 cct_
->_conf
.get_val
<double>("mon_client_hunt_interval_min_multiple")),
81 last_mon_command_tid(0),
85 MonClient::~MonClient()
89 int MonClient::build_initial_monmap()
91 ldout(cct
, 10) << __func__
<< dendl
;
92 int r
= monmap
.build_initial(cct
, false, std::cerr
);
93 ldout(cct
,10) << "monmap:\n";
99 int MonClient::get_monmap()
101 ldout(cct
, 10) << __func__
<< dendl
;
102 std::unique_lock
l(monc_lock
);
104 sub
.want("monmap", 0, 0);
107 map_cond
.wait(l
, [this] { return !want_monmap
; });
108 ldout(cct
, 10) << __func__
<< " done" << dendl
;
112 int MonClient::get_monmap_and_config()
114 ldout(cct
, 10) << __func__
<< dendl
;
115 ceph_assert(!messenger
);
120 auto shutdown_crypto
= make_scope_guard([this] {
121 cct
->shutdown_crypto();
124 int r
= build_initial_monmap();
126 lderr(cct
) << __func__
<< " cannot identify monitors to contact" << dendl
;
130 messenger
= Messenger::create_client_messenger(
131 cct
, "temp_mon_client");
132 ceph_assert(messenger
);
133 messenger
->add_dispatcher_head(this);
135 auto shutdown_msgr
= make_scope_guard([this] {
136 messenger
->shutdown();
140 if (!monmap
.fsid
.is_zero()) {
141 cct
->_conf
.set_val("fsid", stringify(monmap
.fsid
));
145 want_bootstrap_config
= true;
146 auto shutdown_config
= make_scope_guard([this] {
147 std::unique_lock
l(monc_lock
);
148 want_bootstrap_config
= false;
149 bootstrap_config
.reset();
152 ceph::ref_t
<MConfig
> config
;
153 while (tries
-- > 0) {
158 r
= authenticate(cct
->_conf
->client_mount_timeout
);
159 if (r
== -ETIMEDOUT
) {
167 std::unique_lock
l(monc_lock
);
168 if (monmap
.get_epoch() &&
169 !monmap
.persistent_features
.contains_all(
170 ceph::features::mon::FEATURE_MIMIC
)) {
171 ldout(cct
,10) << __func__
<< " pre-mimic monitor, no config to fetch"
176 while ((!bootstrap_config
|| monmap
.get_epoch() == 0) && r
== 0) {
177 ldout(cct
,20) << __func__
<< " waiting for monmap|config" << dendl
;
178 auto status
= map_cond
.wait_for(l
, ceph::make_timespan(
179 cct
->_conf
->mon_client_hunt_interval
));
180 if (status
== std::cv_status::timeout
) {
185 if (bootstrap_config
) {
186 ldout(cct
,10) << __func__
<< " success" << dendl
;
187 config
= std::move(bootstrap_config
);
192 lderr(cct
) << __func__
<< " failed to get config" << dendl
;
198 // apply the bootstrap config to ensure its applied prior to completing
200 cct
->_conf
.set_mon_vals(cct
, config
->config
, config_cb
);
209 * Ping the monitor with id @p mon_id and set the resulting reply in
210 * the provided @p result_reply, if this last parameter is not NULL.
212 * So that we don't rely on the MonClient's default messenger, set up
213 * during connect(), we create our own messenger to comunicate with the
214 * specified monitor. This is advantageous in the following ways:
216 * - Isolate the ping procedure from the rest of the MonClient's operations,
217 * allowing us to not acquire or manage the big monc_lock, thus not
218 * having to block waiting for some other operation to finish before we
220 * * for instance, we can ping mon.FOO even if we are currently hunting
221 * or blocked waiting for auth to complete with mon.BAR.
223 * - Ping a monitor prior to establishing a connection (using connect())
224 * and properly establish the MonClient's messenger. This frees us
225 * from dealing with the complex foo that happens in connect().
227 * We also don't rely on MonClient as a dispatcher for this messenger,
228 * unlike what happens with the MonClient's default messenger. This allows
229 * us to sandbox the whole ping, having it much as a separate entity in
230 * the MonClient class, considerably simplifying the handling and dispatching
231 * of messages without needing to consider monc_lock.
233 * Current drawback is that we will establish a messenger for each ping
234 * we want to issue, instead of keeping a single messenger instance that
235 * would be used for all pings.
237 int MonClient::ping_monitor(const string
&mon_id
, string
*result_reply
)
239 ldout(cct
, 10) << __func__
<< dendl
;
242 if (monmap
.contains("noname-"+mon_id
)) {
243 new_mon_id
= "noname-"+mon_id
;
248 if (new_mon_id
.empty()) {
249 ldout(cct
, 10) << __func__
<< " specified mon id is empty!" << dendl
;
251 } else if (!monmap
.contains(new_mon_id
)) {
252 ldout(cct
, 10) << __func__
<< " no such monitor 'mon." << new_mon_id
<< "'"
257 // N.B. monc isn't initialized
259 auth_registry
.refresh_config();
262 keyring
.from_ceph_context(cct
);
263 RotatingKeyRing
rkeyring(cct
, cct
->get_module_type(), &keyring
);
265 MonClientPinger
*pinger
= new MonClientPinger(cct
,
269 Messenger
*smsgr
= Messenger::create_client_messenger(cct
, "temp_ping_client");
270 smsgr
->add_dispatcher_head(pinger
);
271 smsgr
->set_auth_client(pinger
);
274 ConnectionRef con
= smsgr
->connect_to_mon(monmap
.get_addrs(new_mon_id
));
275 ldout(cct
, 10) << __func__
<< " ping mon." << new_mon_id
276 << " " << con
->get_peer_addr() << dendl
;
278 pinger
->mc
.reset(new MonConnection(cct
, con
, 0, &auth_registry
));
279 pinger
->mc
->start(monmap
.get_epoch(), entity_name
);
280 con
->send_message(new MPing
);
282 int ret
= pinger
->wait_for_reply(cct
->_conf
->mon_client_ping_timeout
);
284 ldout(cct
,10) << __func__
<< " got ping reply" << dendl
;
298 bool MonClient::ms_dispatch(Message
*m
)
300 // we only care about these message types
301 switch (m
->get_type()) {
302 case CEPH_MSG_MON_MAP
:
303 case CEPH_MSG_AUTH_REPLY
:
304 case CEPH_MSG_MON_SUBSCRIBE_ACK
:
305 case CEPH_MSG_MON_GET_VERSION_REPLY
:
306 case MSG_MON_COMMAND_ACK
:
307 case MSG_COMMAND_REPLY
:
318 std::lock_guard
lock(monc_lock
);
320 if (!m
->get_connection()->is_anon() &&
321 m
->get_source().type() == CEPH_ENTITY_TYPE_MON
) {
323 auto p
= _find_pending_con(m
->get_connection());
324 if (p
== pending_cons
.end()) {
325 // ignore any messages outside hunting sessions
326 ldout(cct
, 10) << "discarding stray monitor message " << *m
<< dendl
;
330 } else if (!active_con
|| active_con
->get_con() != m
->get_connection()) {
331 // ignore any messages outside our session(s)
332 ldout(cct
, 10) << "discarding stray monitor message " << *m
<< dendl
;
338 switch (m
->get_type()) {
339 case CEPH_MSG_MON_MAP
:
340 handle_monmap(static_cast<MMonMap
*>(m
));
341 if (passthrough_monmap
) {
347 case CEPH_MSG_AUTH_REPLY
:
348 handle_auth(static_cast<MAuthReply
*>(m
));
350 case CEPH_MSG_MON_SUBSCRIBE_ACK
:
351 handle_subscribe_ack(static_cast<MMonSubscribeAck
*>(m
));
353 case CEPH_MSG_MON_GET_VERSION_REPLY
:
354 handle_get_version_reply(static_cast<MMonGetVersionReply
*>(m
));
356 case MSG_MON_COMMAND_ACK
:
357 handle_mon_command_ack(static_cast<MMonCommandAck
*>(m
));
359 case MSG_COMMAND_REPLY
:
360 if (m
->get_connection()->is_anon() &&
361 m
->get_source().type() == CEPH_ENTITY_TYPE_MON
) {
362 // this connection is from 'tell'... ignore everything except our command
363 // reply. (we'll get misc other message because we authenticated, but we
365 handle_command_reply(static_cast<MCommandReply
*>(m
));
368 // leave the message for another dispatch handler (e.g., Objecter)
372 log_client
->handle_log_ack(static_cast<MLogAck
*>(m
));
374 if (more_log_pending
) {
382 handle_config(static_cast<MConfig
*>(m
));
388 void MonClient::send_log(bool flush
)
391 auto lm
= log_client
->get_mon_log_message(flush
);
393 _send_mon_message(std::move(lm
));
394 more_log_pending
= log_client
->are_pending();
398 void MonClient::flush_log()
400 std::lock_guard
l(monc_lock
);
404 /* Unlike all the other message-handling functions, we don't put away a reference
405 * because we want to support MMonMap passthrough to other Dispatchers. */
406 void MonClient::handle_monmap(MMonMap
*m
)
408 ldout(cct
, 10) << __func__
<< " " << *m
<< dendl
;
409 auto con_addrs
= m
->get_source_addrs();
410 string old_name
= monmap
.get_name(con_addrs
);
411 const auto old_epoch
= monmap
.get_epoch();
413 auto p
= m
->monmapbl
.cbegin();
416 ldout(cct
, 10) << " got monmap " << monmap
.epoch
417 << " from mon." << old_name
418 << " (according to old e" << monmap
.get_epoch() << ")"
420 ldout(cct
, 10) << "dump:\n";
421 monmap
.print(*_dout
);
424 if (old_epoch
!= monmap
.get_epoch()) {
427 if (old_name
.size() == 0) {
428 ldout(cct
,10) << " can't identify which mon we were connected to" << dendl
;
431 auto new_name
= monmap
.get_name(con_addrs
);
432 if (new_name
.empty()) {
433 ldout(cct
, 10) << "mon." << old_name
<< " at " << con_addrs
434 << " went away" << dendl
;
435 // can't find the mon we were talking to (above)
437 } else if (messenger
->should_use_msgr2() &&
438 monmap
.get_addrs(new_name
).has_msgr2() &&
439 !con_addrs
.has_msgr2()) {
440 ldout(cct
,1) << " mon." << new_name
<< " has (v2) addrs "
441 << monmap
.get_addrs(new_name
) << " but i'm connected to "
442 << con_addrs
<< ", reconnecting" << dendl
;
447 cct
->set_mon_addrs(monmap
);
449 sub
.got("monmap", monmap
.get_epoch());
450 map_cond
.notify_all();
453 if (authenticate_err
== 1) {
458 void MonClient::handle_config(MConfig
*m
)
460 ldout(cct
,10) << __func__
<< " " << *m
<< dendl
;
462 if (want_bootstrap_config
) {
463 // get_monmap_and_config is waiting for config which it will apply
465 bootstrap_config
= ceph::ref_t
<MConfig
>(m
, false);
466 map_cond
.notify_all();
470 // Take the sledgehammer approach to ensuring we don't depend on
471 // anything in MonClient.
472 boost::asio::post(finish_strand
,
473 [m
, cct
= boost::intrusive_ptr
<CephContext
>(cct
),
474 config_notify_cb
= config_notify_cb
,
475 config_cb
= config_cb
]() {
476 cct
->_conf
.set_mon_vals(cct
.get(), m
->config
, config_cb
);
477 if (config_notify_cb
) {
484 // ----------------------
486 int MonClient::init()
488 ldout(cct
, 10) << __func__
<< dendl
;
490 entity_name
= cct
->_conf
->name
;
492 auth_registry
.refresh_config();
494 std::lock_guard
l(monc_lock
);
495 keyring
.reset(new KeyRing
);
496 if (auth_registry
.is_supported_method(messenger
->get_mytype(),
498 // this should succeed, because auth_registry just checked!
499 int r
= keyring
->from_ceph_context(cct
);
501 // but be somewhat graceful in case there was a race condition
502 lderr(cct
) << "keyring not found" << dendl
;
506 if (!auth_registry
.any_supported_methods(messenger
->get_mytype())) {
510 rotating_secrets
.reset(
511 new RotatingKeyRing(cct
, cct
->get_module_type(), keyring
.get()));
515 messenger
->set_auth_client(this);
516 messenger
->add_dispatcher_head(this);
524 void MonClient::shutdown()
526 ldout(cct
, 10) << __func__
<< dendl
;
529 while (!version_requests
.empty()) {
530 ceph::async::post(std::move(version_requests
.begin()->second
),
531 monc_errc::shutting_down
, 0, 0);
532 ldout(cct
, 20) << __func__
<< " canceling and discarding version request "
533 << version_requests
.begin()->first
<< dendl
;
534 version_requests
.erase(version_requests
.begin());
536 while (!mon_commands
.empty()) {
537 auto tid
= mon_commands
.begin()->first
;
538 _cancel_mon_command(tid
);
540 ldout(cct
, 20) << __func__
<< " discarding " << waiting_for_session
.size()
541 << " pending message(s)" << dendl
;
542 waiting_for_session
.clear();
545 pending_cons
.clear();
549 authenticate_err
= 0;
550 authenticated
= false;
563 int MonClient::authenticate(double timeout
)
565 std::unique_lock lock
{monc_lock
};
568 ldout(cct
, 5) << "already authenticated" << dendl
;
571 sub
.want("monmap", monmap
.get_epoch() ? monmap
.get_epoch() + 1 : 0, 0);
572 sub
.want("config", 0, 0);
576 auto until
= ceph::real_clock::now();
577 until
+= ceph::make_timespan(timeout
);
579 ldout(cct
, 10) << "authenticate will time out at " << until
<< dendl
;
580 authenticate_err
= 1; // == in progress
581 while (!active_con
&& authenticate_err
>= 0) {
583 auto r
= auth_cond
.wait_until(lock
, until
);
584 if (r
== std::cv_status::timeout
&& !active_con
) {
585 ldout(cct
, 0) << "authenticate timed out after " << timeout
<< dendl
;
586 authenticate_err
= -ETIMEDOUT
;
589 auth_cond
.wait(lock
);
594 ldout(cct
, 5) << __func__
<< " success, global_id "
595 << active_con
->get_global_id() << dendl
;
596 // active_con should not have been set if there was an error
597 ceph_assert(authenticate_err
>= 0);
598 authenticated
= true;
601 if (authenticate_err
< 0 && auth_registry
.no_keyring_disabled_cephx()) {
602 lderr(cct
) << __func__
<< " NOTE: no keyring found; disabled cephx authentication" << dendl
;
605 return authenticate_err
;
608 void MonClient::handle_auth(MAuthReply
*m
)
610 ceph_assert(ceph_mutex_is_locked(monc_lock
));
612 if (m
->get_connection()->is_anon()) {
613 // anon connection, used for mon tell commands
614 for (auto& p
: mon_commands
) {
615 if (p
.second
->target_con
== m
->get_connection()) {
616 auto& mc
= p
.second
->target_session
;
617 int ret
= mc
->handle_auth(m
, entity_name
,
618 CEPH_ENTITY_TYPE_MON
,
619 rotating_secrets
.get());
620 (void)ret
; // we don't care
629 std::swap(active_con
->get_auth(), auth
);
630 int ret
= active_con
->authenticate(m
);
632 std::swap(auth
, active_con
->get_auth());
633 if (global_id
!= active_con
->get_global_id()) {
634 lderr(cct
) << __func__
<< " peer assigned me a different global_id: "
635 << active_con
->get_global_id() << dendl
;
637 if (ret
!= -EAGAIN
) {
644 auto found
= _find_pending_con(m
->get_connection());
645 ceph_assert(found
!= pending_cons
.end());
646 int auth_err
= found
->second
.handle_auth(m
, entity_name
, want_keys
,
647 rotating_secrets
.get());
649 if (auth_err
== -EAGAIN
) {
653 pending_cons
.erase(found
);
654 if (!pending_cons
.empty()) {
655 // keep trying with pending connections
658 // the last try just failed, give up.
660 auto& mc
= found
->second
;
661 ceph_assert(mc
.have_session());
662 active_con
.reset(new MonConnection(std::move(mc
)));
663 pending_cons
.clear();
666 _finish_hunting(auth_err
);
667 _finish_auth(auth_err
);
670 void MonClient::_finish_auth(int auth_err
)
672 ldout(cct
,10) << __func__
<< " " << auth_err
<< dendl
;
673 authenticate_err
= auth_err
;
674 // _resend_mon_commands() could _reopen_session() if the connected mon is not
675 // the one the MonCommand is targeting.
676 if (!auth_err
&& active_con
) {
678 _check_auth_tickets();
680 auth_cond
.notify_all();
683 Context
*cb
= nullptr;
684 if (session_established_context
) {
685 cb
= session_established_context
.release();
697 void MonClient::send_mon_message(MessageRef m
)
699 std::lock_guard l
{monc_lock
};
700 _send_mon_message(std::move(m
));
703 void MonClient::_send_mon_message(MessageRef m
)
705 ceph_assert(ceph_mutex_is_locked(monc_lock
));
707 auto cur_con
= active_con
->get_con();
708 ldout(cct
, 10) << "_send_mon_message to mon."
709 << monmap
.get_name(cur_con
->get_peer_addr())
710 << " at " << cur_con
->get_peer_addr() << dendl
;
711 cur_con
->send_message2(std::move(m
));
713 waiting_for_session
.push_back(std::move(m
));
717 void MonClient::_reopen_session(int rank
)
719 ceph_assert(ceph_mutex_is_locked(monc_lock
));
720 ldout(cct
, 10) << __func__
<< " rank " << rank
<< dendl
;
723 pending_cons
.clear();
733 // throw out old queued messages
734 waiting_for_session
.clear();
736 // throw out version check requests
737 while (!version_requests
.empty()) {
738 ceph::async::post(std::move(version_requests
.begin()->second
),
739 monc_errc::session_reset
, 0, 0);
740 version_requests
.erase(version_requests
.begin());
743 for (auto& c
: pending_cons
) {
744 c
.second
.start(monmap
.get_epoch(), entity_name
);
752 void MonClient::_add_conn(unsigned rank
)
754 auto peer
= monmap
.get_addrs(rank
);
755 auto conn
= messenger
->connect_to_mon(peer
);
756 MonConnection
mc(cct
, conn
, global_id
, &auth_registry
);
758 mc
.get_auth().reset(auth
->clone());
760 pending_cons
.insert(std::make_pair(peer
, std::move(mc
)));
761 ldout(cct
, 10) << "picked mon." << monmap
.get_name(rank
)
767 void MonClient::_add_conns()
769 // collect the next batch of candidates who are listed right next to the ones
771 auto get_next_batch
= [this]() -> std::vector
<unsigned> {
772 std::multimap
<uint16_t, unsigned> ranks_by_priority
;
774 monmap
.mon_info
| boost::adaptors::filtered(
776 auto rank
= monmap
.get_rank(info
.first
);
777 return tried
.count(rank
) == 0;
778 }) | boost::adaptors::transformed(
780 auto rank
= monmap
.get_rank(info
.first
);
781 return std::make_pair(info
.second
.priority
, rank
);
782 }), std::inserter(ranks_by_priority
, end(ranks_by_priority
)));
783 if (ranks_by_priority
.empty()) {
786 // only choose the monitors with lowest priority
787 auto cands
= boost::make_iterator_range(
788 ranks_by_priority
.equal_range(ranks_by_priority
.begin()->first
));
789 std::vector
<unsigned> ranks
;
790 boost::range::copy(cands
| boost::adaptors::map_values
,
791 std::back_inserter(ranks
));
794 auto ranks
= get_next_batch();
796 tried
.clear(); // start over
797 ranks
= get_next_batch();
799 ceph_assert(!ranks
.empty());
800 if (ranks
.size() > 1) {
801 std::vector
<uint16_t> weights
;
802 for (auto i
: ranks
) {
803 auto rank_name
= monmap
.get_name(i
);
804 weights
.push_back(monmap
.get_weight(rank_name
));
807 if (std::accumulate(begin(weights
), end(weights
), 0u) == 0) {
808 std::shuffle(begin(ranks
), end(ranks
), std::mt19937
{rd()});
810 weighted_shuffle(begin(ranks
), end(ranks
), begin(weights
), end(weights
),
814 ldout(cct
, 10) << __func__
<< " ranks=" << ranks
<< dendl
;
815 unsigned n
= cct
->_conf
->mon_client_hunt_parallel
;
816 if (n
== 0 || n
> ranks
.size()) {
819 for (unsigned i
= 0; i
< n
; i
++) {
821 tried
.insert(ranks
[i
]);
825 bool MonClient::ms_handle_reset(Connection
*con
)
827 std::lock_guard
lock(monc_lock
);
829 if (con
->get_peer_type() != CEPH_ENTITY_TYPE_MON
)
832 if (con
->is_anon()) {
833 auto p
= mon_commands
.begin();
834 while (p
!= mon_commands
.end()) {
835 auto cmd
= p
->second
;
837 if (cmd
->target_con
== con
) {
838 _send_command(cmd
); // may retry or fail
846 if (pending_cons
.count(con
->get_peer_addrs())) {
847 ldout(cct
, 10) << __func__
<< " hunted mon " << con
->get_peer_addrs()
850 ldout(cct
, 10) << __func__
<< " stray mon " << con
->get_peer_addrs()
855 if (active_con
&& con
== active_con
->get_con()) {
856 ldout(cct
, 10) << __func__
<< " current mon " << con
->get_peer_addrs()
861 ldout(cct
, 10) << "ms_handle_reset stray mon " << con
->get_peer_addrs()
868 bool MonClient::_opened() const
870 ceph_assert(ceph_mutex_is_locked(monc_lock
));
871 return active_con
|| _hunting();
874 bool MonClient::_hunting() const
876 return !pending_cons
.empty();
879 void MonClient::_start_hunting()
881 ceph_assert(!_hunting());
882 // adjust timeouts if necessary
883 if (!had_a_connection
)
885 reopen_interval_multiplier
*= cct
->_conf
->mon_client_hunt_interval_backoff
;
886 if (reopen_interval_multiplier
>
887 cct
->_conf
->mon_client_hunt_interval_max_multiple
) {
888 reopen_interval_multiplier
=
889 cct
->_conf
->mon_client_hunt_interval_max_multiple
;
893 void MonClient::_finish_hunting(int auth_err
)
895 ldout(cct
,10) << __func__
<< " " << auth_err
<< dendl
;
896 ceph_assert(ceph_mutex_is_locked(monc_lock
));
897 // the pending conns have been cleaned.
898 ceph_assert(!_hunting());
900 auto con
= active_con
->get_con();
901 ldout(cct
, 1) << "found mon."
902 << monmap
.get_name(con
->get_peer_addr())
905 ldout(cct
, 1) << "no mon sessions established" << dendl
;
908 had_a_connection
= true;
912 last_rotating_renew_sent
= utime_t();
913 while (!waiting_for_session
.empty()) {
914 _send_mon_message(std::move(waiting_for_session
.front()));
915 waiting_for_session
.pop_front();
917 _resend_mon_commands();
920 auth
= std::move(active_con
->get_auth());
921 if (global_id
&& global_id
!= active_con
->get_global_id()) {
922 lderr(cct
) << __func__
<< " global_id changed from " << global_id
923 << " to " << active_con
->get_global_id() << dendl
;
925 global_id
= active_con
->get_global_id();
930 void MonClient::tick()
932 ldout(cct
, 10) << __func__
<< dendl
;
934 utime_t now
= ceph_clock_now();
936 auto reschedule_tick
= make_scope_guard([this] {
940 _check_auth_tickets();
941 _check_tell_commands();
944 ldout(cct
, 1) << "continuing hunt" << dendl
;
945 return _reopen_session();
946 } else if (active_con
) {
947 // just renew as needed
948 auto cur_con
= active_con
->get_con();
949 if (!cur_con
->has_feature(CEPH_FEATURE_MON_STATEFUL_SUB
)) {
950 const bool maybe_renew
= sub
.need_renew();
951 ldout(cct
, 10) << "renew subs? -- " << (maybe_renew
? "yes" : "no")
958 if (now
> last_keepalive
+ cct
->_conf
->mon_client_ping_interval
) {
959 cur_con
->send_keepalive();
960 last_keepalive
= now
;
962 if (cct
->_conf
->mon_client_ping_timeout
> 0 &&
963 cur_con
->has_feature(CEPH_FEATURE_MSGR_KEEPALIVE2
)) {
964 utime_t lk
= cur_con
->get_last_keepalive_ack();
965 utime_t interval
= now
- lk
;
966 if (interval
> cct
->_conf
->mon_client_ping_timeout
) {
967 ldout(cct
, 1) << "no keepalive since " << lk
<< " (" << interval
968 << " seconds), reconnecting" << dendl
;
969 return _reopen_session();
976 if (now
> last_send_log
+ cct
->_conf
->mon_client_log_interval
) {
983 void MonClient::_un_backoff()
985 // un-backoff our reconnect interval
986 reopen_interval_multiplier
= std::max(
987 cct
->_conf
.get_val
<double>("mon_client_hunt_interval_min_multiple"),
988 reopen_interval_multiplier
/
989 cct
->_conf
.get_val
<double>("mon_client_hunt_interval_backoff"));
990 ldout(cct
, 20) << __func__
<< " reopen_interval_multipler now "
991 << reopen_interval_multiplier
<< dendl
;
994 void MonClient::schedule_tick()
996 auto do_tick
= make_lambda_context([this](int) { tick(); });
997 if (!is_connected()) {
998 // start another round of hunting
999 const auto hunt_interval
= (cct
->_conf
->mon_client_hunt_interval
*
1000 reopen_interval_multiplier
);
1001 timer
.add_event_after(hunt_interval
, do_tick
);
1004 timer
.add_event_after(std::min(cct
->_conf
->mon_client_ping_interval
,
1005 cct
->_conf
->mon_client_log_interval
),
1012 void MonClient::_renew_subs()
1014 ceph_assert(ceph_mutex_is_locked(monc_lock
));
1015 if (!sub
.have_new()) {
1016 ldout(cct
, 10) << __func__
<< " - empty" << dendl
;
1020 ldout(cct
, 10) << __func__
<< dendl
;
1024 auto m
= ceph::make_message
<MMonSubscribe
>();
1025 m
->what
= sub
.get_subs();
1026 m
->hostname
= ceph_get_short_hostname();
1027 _send_mon_message(std::move(m
));
1032 void MonClient::handle_subscribe_ack(MMonSubscribeAck
*m
)
1034 sub
.acked(m
->interval
);
1038 int MonClient::_check_auth_tickets()
1040 ceph_assert(ceph_mutex_is_locked(monc_lock
));
1041 if (active_con
&& auth
) {
1042 if (auth
->need_tickets()) {
1043 ldout(cct
, 10) << __func__
<< " getting new tickets!" << dendl
;
1044 auto m
= ceph::make_message
<MAuth
>();
1045 m
->protocol
= auth
->get_protocol();
1046 auth
->prepare_build_request();
1047 auth
->build_request(m
->auth_payload
);
1048 _send_mon_message(m
);
1051 _check_auth_rotating();
1056 int MonClient::_check_auth_rotating()
1058 ceph_assert(ceph_mutex_is_locked(monc_lock
));
1059 if (!rotating_secrets
||
1060 !auth_principal_needs_rotating_keys(entity_name
)) {
1061 ldout(cct
, 20) << "_check_auth_rotating not needed by " << entity_name
<< dendl
;
1065 if (!active_con
|| !auth
) {
1066 ldout(cct
, 10) << "_check_auth_rotating waiting for auth session" << dendl
;
1070 utime_t now
= ceph_clock_now();
1071 utime_t cutoff
= now
;
1072 cutoff
-= std::min(30.0, cct
->_conf
->auth_service_ticket_ttl
/ 4.0);
1073 utime_t issued_at_lower_bound
= now
;
1074 issued_at_lower_bound
-= cct
->_conf
->auth_service_ticket_ttl
;
1075 if (!rotating_secrets
->need_new_secrets(cutoff
)) {
1076 ldout(cct
, 10) << "_check_auth_rotating have uptodate secrets (they expire after " << cutoff
<< ")" << dendl
;
1077 rotating_secrets
->dump_rotating();
1081 ldout(cct
, 10) << "_check_auth_rotating renewing rotating keys (they expired before " << cutoff
<< ")" << dendl
;
1082 if (!rotating_secrets
->need_new_secrets() &&
1083 rotating_secrets
->need_new_secrets(issued_at_lower_bound
)) {
1084 // the key has expired before it has been issued?
1085 lderr(cct
) << __func__
<< " possible clock skew, rotating keys expired way too early"
1086 << " (before " << issued_at_lower_bound
<< ")" << dendl
;
1088 if ((now
> last_rotating_renew_sent
) &&
1089 double(now
- last_rotating_renew_sent
) < 1) {
1090 ldout(cct
, 10) << __func__
<< " called too often (last: "
1091 << last_rotating_renew_sent
<< "), skipping refresh" << dendl
;
1094 auto m
= ceph::make_message
<MAuth
>();
1095 m
->protocol
= auth
->get_protocol();
1096 if (auth
->build_rotating_request(m
->auth_payload
)) {
1097 last_rotating_renew_sent
= now
;
1098 _send_mon_message(std::move(m
));
1103 int MonClient::wait_auth_rotating(double timeout
)
1105 std::unique_lock
l(monc_lock
);
1107 // Must be initialized
1108 ceph_assert(auth
!= nullptr);
1110 if (auth
->get_protocol() == CEPH_AUTH_NONE
)
1113 if (!rotating_secrets
)
1116 ldout(cct
, 10) << __func__
<< " waiting for " << timeout
<< dendl
;
1117 utime_t now
= ceph_clock_now();
1118 if (auth_cond
.wait_for(l
, ceph::make_timespan(timeout
), [now
, this] {
1119 return (!auth_principal_needs_rotating_keys(entity_name
) ||
1120 !rotating_secrets
->need_new_secrets(now
));
1122 ldout(cct
, 10) << __func__
<< " done" << dendl
;
1125 ldout(cct
, 0) << __func__
<< " timed out after " << timeout
<< dendl
;
1132 void MonClient::_send_command(MonCommand
*r
)
1136 if (r
->send_attempts
> cct
->_conf
->mon_client_directed_command_retry
) {
1137 _finish_command(r
, monc_errc::mon_unavailable
, "mon unavailable", {});
1140 // tell-style command
1141 if (monmap
.min_mon_release
>= ceph_release_t::octopus
) {
1142 if (r
->target_con
) {
1143 r
->target_con
->mark_down();
1145 if (r
->target_rank
>= 0) {
1146 if (r
->target_rank
>= (int)monmap
.size()) {
1147 ldout(cct
, 10) << " target " << r
->target_rank
1148 << " >= max mon " << monmap
.size() << dendl
;
1149 _finish_command(r
, monc_errc::rank_dne
, "mon rank dne"sv
, {});
1152 r
->target_con
= messenger
->connect_to_mon(
1153 monmap
.get_addrs(r
->target_rank
), true /* anon */);
1155 if (!monmap
.contains(r
->target_name
)) {
1156 ldout(cct
, 10) << " target " << r
->target_name
1157 << " not present in monmap" << dendl
;
1158 _finish_command(r
, monc_errc::mon_dne
, "mon dne"sv
, {});
1161 r
->target_con
= messenger
->connect_to_mon(
1162 monmap
.get_addrs(r
->target_name
), true /* anon */);
1165 r
->target_session
.reset(new MonConnection(cct
, r
->target_con
, 0,
1167 r
->target_session
->start(monmap
.get_epoch(), entity_name
);
1168 r
->last_send_attempt
= ceph_clock_now();
1170 MCommand
*m
= new MCommand(monmap
.fsid
);
1173 m
->set_data(r
->inbl
);
1174 r
->target_session
->queue_command(m
);
1178 // ugly legacy handling of pre-octopus mons
1181 peer
= active_con
->get_con()->get_peer_addr();
1184 if (r
->target_rank
>= 0 &&
1185 r
->target_rank
!= monmap
.get_rank(peer
)) {
1186 ldout(cct
, 10) << __func__
<< " " << r
->tid
<< " " << r
->cmd
1187 << " wants rank " << r
->target_rank
1188 << ", reopening session"
1190 if (r
->target_rank
>= (int)monmap
.size()) {
1191 ldout(cct
, 10) << " target " << r
->target_rank
1192 << " >= max mon " << monmap
.size() << dendl
;
1193 _finish_command(r
, monc_errc::rank_dne
, "mon rank dne"sv
, {});
1196 _reopen_session(r
->target_rank
);
1199 if (r
->target_name
.length() &&
1200 r
->target_name
!= monmap
.get_name(peer
)) {
1201 ldout(cct
, 10) << __func__
<< " " << r
->tid
<< " " << r
->cmd
1202 << " wants mon " << r
->target_name
1203 << ", reopening session"
1205 if (!monmap
.contains(r
->target_name
)) {
1206 ldout(cct
, 10) << " target " << r
->target_name
1207 << " not present in monmap" << dendl
;
1208 _finish_command(r
, monc_errc::mon_dne
, "mon dne"sv
, {});
1211 _reopen_session(monmap
.get_rank(r
->target_name
));
1214 // fall-thru to send 'normal' CLI command
1217 // normal CLI command
1218 ldout(cct
, 10) << __func__
<< " " << r
->tid
<< " " << r
->cmd
<< dendl
;
1219 auto m
= ceph::make_message
<MMonCommand
>(monmap
.fsid
);
1222 m
->set_data(r
->inbl
);
1223 _send_mon_message(std::move(m
));
1227 void MonClient::_check_tell_commands()
1229 // resend any requests
1230 auto now
= ceph_clock_now();
1231 auto p
= mon_commands
.begin();
1232 while (p
!= mon_commands
.end()) {
1233 auto cmd
= p
->second
;
1235 if (cmd
->is_tell() &&
1236 cmd
->last_send_attempt
!= utime_t() &&
1237 now
- cmd
->last_send_attempt
> cct
->_conf
->mon_client_hunt_interval
) {
1238 ldout(cct
,5) << __func__
<< " timeout tell command " << cmd
->tid
<< dendl
;
1239 _send_command(cmd
); // might remove cmd from mon_commands
1244 void MonClient::_resend_mon_commands()
1246 // resend any requests
1247 auto p
= mon_commands
.begin();
1248 while (p
!= mon_commands
.end()) {
1249 auto cmd
= p
->second
;
1251 if (cmd
->is_tell() && monmap
.min_mon_release
>= ceph_release_t::octopus
) {
1252 // starting with octopus, tell commands use their own connetion and need no
1253 // special resend when we finish hunting.
1255 _send_command(cmd
); // might remove cmd from mon_commands
1260 void MonClient::handle_mon_command_ack(MMonCommandAck
*ack
)
1262 MonCommand
*r
= NULL
;
1263 uint64_t tid
= ack
->get_tid();
1265 if (tid
== 0 && !mon_commands
.empty()) {
1266 r
= mon_commands
.begin()->second
;
1267 ldout(cct
, 10) << __func__
<< " has tid 0, assuming it is " << r
->tid
<< dendl
;
1269 auto p
= mon_commands
.find(tid
);
1270 if (p
== mon_commands
.end()) {
1271 ldout(cct
, 10) << __func__
<< " " << ack
->get_tid() << " not found" << dendl
;
1278 ldout(cct
, 10) << __func__
<< " " << r
->tid
<< " " << r
->cmd
<< dendl
;
1279 auto ec
= ack
->r
< 0 ? bs::error_code(-ack
->r
, mon_category())
1281 _finish_command(r
, ec
, ack
->rs
,
1282 std::move(ack
->get_data()));
1286 void MonClient::handle_command_reply(MCommandReply
*reply
)
1288 MonCommand
*r
= NULL
;
1289 uint64_t tid
= reply
->get_tid();
1291 if (tid
== 0 && !mon_commands
.empty()) {
1292 r
= mon_commands
.begin()->second
;
1293 ldout(cct
, 10) << __func__
<< " has tid 0, assuming it is " << r
->tid
1296 auto p
= mon_commands
.find(tid
);
1297 if (p
== mon_commands
.end()) {
1298 ldout(cct
, 10) << __func__
<< " " << reply
->get_tid() << " not found"
1306 ldout(cct
, 10) << __func__
<< " " << r
->tid
<< " " << r
->cmd
<< dendl
;
1307 auto ec
= reply
->r
< 0 ? bs::error_code(-reply
->r
, mon_category())
1309 _finish_command(r
, ec
, reply
->rs
, std::move(reply
->get_data()));
1313 int MonClient::_cancel_mon_command(uint64_t tid
)
1315 ceph_assert(ceph_mutex_is_locked(monc_lock
));
1317 auto it
= mon_commands
.find(tid
);
1318 if (it
== mon_commands
.end()) {
1319 ldout(cct
, 10) << __func__
<< " tid " << tid
<< " dne" << dendl
;
1323 ldout(cct
, 10) << __func__
<< " tid " << tid
<< dendl
;
1325 MonCommand
*cmd
= it
->second
;
1326 _finish_command(cmd
, monc_errc::timed_out
, "timed out"sv
, {});
1330 void MonClient::_finish_command(MonCommand
*r
, bs::error_code ret
,
1331 std::string_view rs
, ceph::buffer::list
&& bl
)
1333 ldout(cct
, 10) << __func__
<< " " << r
->tid
<< " = " << ret
<< " " << rs
1335 ceph::async::post(std::move(r
->onfinish
), ret
, std::string(rs
),
1337 if (r
->target_con
) {
1338 r
->target_con
->mark_down();
1340 mon_commands
.erase(r
->tid
);
1346 void MonClient::handle_get_version_reply(MMonGetVersionReply
* m
)
1348 ceph_assert(ceph_mutex_is_locked(monc_lock
));
1349 auto iter
= version_requests
.find(m
->handle
);
1350 if (iter
== version_requests
.end()) {
1351 ldout(cct
, 0) << __func__
<< " version request with handle " << m
->handle
1352 << " not found" << dendl
;
1354 auto req
= std::move(iter
->second
);
1355 ldout(cct
, 10) << __func__
<< " finishing " << iter
->first
<< " version "
1356 << m
->version
<< dendl
;
1357 version_requests
.erase(iter
);
1358 ceph::async::post(std::move(req
), bs::error_code(),
1359 m
->version
, m
->oldest_version
);
1364 int MonClient::get_auth_request(
1366 AuthConnectionMeta
*auth_meta
,
1367 uint32_t *auth_method
,
1368 std::vector
<uint32_t> *preferred_modes
,
1369 ceph::buffer::list
*bl
)
1371 std::lock_guard
l(monc_lock
);
1372 ldout(cct
,10) << __func__
<< " con " << con
<< " auth_method " << *auth_method
1375 // connection to mon?
1376 if (con
->get_peer_type() == CEPH_ENTITY_TYPE_MON
) {
1377 ceph_assert(!auth_meta
->authorizer
);
1378 if (con
->is_anon()) {
1379 for (auto& i
: mon_commands
) {
1380 if (i
.second
->target_con
== con
) {
1381 return i
.second
->target_session
->get_auth_request(
1382 auth_method
, preferred_modes
, bl
,
1383 entity_name
, want_keys
, rotating_secrets
.get());
1387 for (auto& i
: pending_cons
) {
1388 if (i
.second
.is_con(con
)) {
1389 return i
.second
.get_auth_request(
1390 auth_method
, preferred_modes
, bl
,
1391 entity_name
, want_keys
, rotating_secrets
.get());
1397 // generate authorizer
1399 lderr(cct
) << __func__
<< " but no auth handler is set up" << dendl
;
1402 auth_meta
->authorizer
.reset(auth
->build_authorizer(con
->get_peer_type()));
1403 if (!auth_meta
->authorizer
) {
1404 lderr(cct
) << __func__
<< " failed to build_authorizer for type "
1405 << ceph_entity_type_name(con
->get_peer_type()) << dendl
;
1408 auth_meta
->auth_method
= auth_meta
->authorizer
->protocol
;
1409 auth_registry
.get_supported_modes(con
->get_peer_type(),
1410 auth_meta
->auth_method
,
1412 *bl
= auth_meta
->authorizer
->bl
;
1416 int MonClient::handle_auth_reply_more(
1418 AuthConnectionMeta
*auth_meta
,
1419 const ceph::buffer::list
& bl
,
1420 ceph::buffer::list
*reply
)
1422 std::lock_guard
l(monc_lock
);
1424 if (con
->get_peer_type() == CEPH_ENTITY_TYPE_MON
) {
1425 if (con
->is_anon()) {
1426 for (auto& i
: mon_commands
) {
1427 if (i
.second
->target_con
== con
) {
1428 return i
.second
->target_session
->handle_auth_reply_more(
1429 auth_meta
, bl
, reply
);
1433 for (auto& i
: pending_cons
) {
1434 if (i
.second
.is_con(con
)) {
1435 return i
.second
.handle_auth_reply_more(auth_meta
, bl
, reply
);
1441 // authorizer challenges
1442 if (!auth
|| !auth_meta
->authorizer
) {
1443 lderr(cct
) << __func__
<< " no authorizer?" << dendl
;
1446 auth_meta
->authorizer
->add_challenge(cct
, bl
);
1447 *reply
= auth_meta
->authorizer
->bl
;
1451 int MonClient::handle_auth_done(
1453 AuthConnectionMeta
*auth_meta
,
1456 const ceph::buffer::list
& bl
,
1457 CryptoKey
*session_key
,
1458 std::string
*connection_secret
)
1460 if (con
->get_peer_type() == CEPH_ENTITY_TYPE_MON
) {
1461 std::lock_guard
l(monc_lock
);
1462 if (con
->is_anon()) {
1463 for (auto& i
: mon_commands
) {
1464 if (i
.second
->target_con
== con
) {
1465 return i
.second
->target_session
->handle_auth_done(
1466 auth_meta
, global_id
, bl
,
1467 session_key
, connection_secret
);
1471 for (auto& i
: pending_cons
) {
1472 if (i
.second
.is_con(con
)) {
1473 int r
= i
.second
.handle_auth_done(
1474 auth_meta
, global_id
, bl
,
1475 session_key
, connection_secret
);
1477 pending_cons
.erase(i
.first
);
1478 if (!pending_cons
.empty()) {
1482 active_con
.reset(new MonConnection(std::move(i
.second
)));
1483 pending_cons
.clear();
1484 ceph_assert(active_con
->have_session());
1488 if (r
|| monmap
.get_epoch() > 0) {
1496 // verify authorizer reply
1497 auto p
= bl
.begin();
1498 if (!auth_meta
->authorizer
->verify_reply(p
, &auth_meta
->connection_secret
)) {
1499 ldout(cct
, 0) << __func__
<< " failed verifying authorizer reply"
1503 auth_meta
->session_key
= auth_meta
->authorizer
->session_key
;
1508 int MonClient::handle_auth_bad_method(
1510 AuthConnectionMeta
*auth_meta
,
1511 uint32_t old_auth_method
,
1513 const std::vector
<uint32_t>& allowed_methods
,
1514 const std::vector
<uint32_t>& allowed_modes
)
1516 auth_meta
->allowed_methods
= allowed_methods
;
1518 std::lock_guard
l(monc_lock
);
1519 if (con
->get_peer_type() == CEPH_ENTITY_TYPE_MON
) {
1520 if (con
->is_anon()) {
1521 for (auto& i
: mon_commands
) {
1522 if (i
.second
->target_con
== con
) {
1523 int r
= i
.second
->target_session
->handle_auth_bad_method(
1529 auto ec
= bs::error_code(-r
, mon_category());
1530 _finish_command(i
.second
, ec
, "auth failed"sv
, {});
1536 for (auto& i
: pending_cons
) {
1537 if (i
.second
.is_con(con
)) {
1538 int r
= i
.second
.handle_auth_bad_method(old_auth_method
,
1543 return r
; // try another method on this con
1545 pending_cons
.erase(i
.first
);
1546 if (!pending_cons
.empty()) {
1547 return r
; // fail this con, maybe another con will succeed
1558 ldout(cct
,10) << __func__
<< " hmm, they didn't like " << old_auth_method
1559 << " result " << cpp_strerror(result
)
1560 << " and auth is " << (auth
? auth
->get_protocol() : 0)
1566 int MonClient::handle_auth_request(
1568 AuthConnectionMeta
*auth_meta
,
1570 uint32_t auth_method
,
1571 const ceph::buffer::list
& payload
,
1572 ceph::buffer::list
*reply
)
1574 if (payload
.length() == 0) {
1575 // for some channels prior to nautilus (osd heartbeat), we
1576 // tolerate the lack of an authorizer.
1577 if (!con
->get_messenger()->require_authorizer
) {
1578 handle_authentication_dispatcher
->ms_handle_authentication(con
);
1583 auth_meta
->auth_mode
= payload
[0];
1584 if (auth_meta
->auth_mode
< AUTH_MODE_AUTHORIZER
||
1585 auth_meta
->auth_mode
> AUTH_MODE_AUTHORIZER_MAX
) {
1588 AuthAuthorizeHandler
*ah
= get_auth_authorize_handler(con
->get_peer_type(),
1591 lderr(cct
) << __func__
<< " no AuthAuthorizeHandler found for auth method "
1592 << auth_method
<< dendl
;
1596 auto ac
= &auth_meta
->authorizer_challenge
;
1597 if (auth_meta
->skip_authorizer_challenge
) {
1598 ldout(cct
, 10) << __func__
<< " skipping challenge on " << con
<< dendl
;
1602 bool was_challenge
= (bool)auth_meta
->authorizer_challenge
;
1603 bool isvalid
= ah
->verify_authorizer(
1607 auth_meta
->get_connection_secret_length(),
1610 &con
->peer_global_id
,
1611 &con
->peer_caps_info
,
1612 &auth_meta
->session_key
,
1613 &auth_meta
->connection_secret
,
1616 handle_authentication_dispatcher
->ms_handle_authentication(con
);
1619 if (!more
&& !was_challenge
&& auth_meta
->authorizer_challenge
) {
1620 ldout(cct
,10) << __func__
<< " added challenge on " << con
<< dendl
;
1623 ldout(cct
,10) << __func__
<< " bad authorizer on " << con
<< dendl
;
1624 // discard old challenge
1625 auth_meta
->authorizer_challenge
.reset();
1629 AuthAuthorizer
* MonClient::build_authorizer(int service_id
) const {
1630 std::lock_guard
l(monc_lock
);
1632 return auth
->build_authorizer(service_id
);
1634 ldout(cct
, 0) << __func__
<< " for " << ceph_entity_type_name(service_id
)
1635 << ", but no auth is available now" << dendl
;
1640 #define dout_subsys ceph_subsys_monc
1642 #define dout_prefix *_dout << "monclient" << (have_session() ? ": " : "(hunting): ")
1644 MonConnection::MonConnection(
1645 CephContext
*cct
, ConnectionRef con
, uint64_t global_id
,
1647 : cct(cct
), con(con
), global_id(global_id
), auth_registry(ar
)
1650 MonConnection::~MonConnection()
1658 bool MonConnection::have_session() const
1660 return state
== State::HAVE_SESSION
;
1663 void MonConnection::start(epoch_t epoch
,
1664 const EntityName
& entity_name
)
1667 auth_start
= ceph_clock_now();
1669 if (con
->get_peer_addr().is_msgr2()) {
1670 ldout(cct
, 10) << __func__
<< " opening mon connection" << dendl
;
1671 state
= State::AUTHENTICATING
;
1672 con
->send_message(new MMonGetMap());
1676 // restart authentication handshake
1677 state
= State::NEGOTIATING
;
1679 // send an initial keepalive to ensure our timestamp is valid by the
1680 // time we are in an OPENED state (by sequencing this before
1682 con
->send_keepalive();
1685 m
->protocol
= CEPH_AUTH_UNKNOWN
;
1686 m
->monmap_epoch
= epoch
;
1688 encode(struct_v
, m
->auth_payload
);
1689 std::vector
<uint32_t> auth_supported
;
1690 auth_registry
->get_supported_methods(con
->get_peer_type(), &auth_supported
);
1691 encode(auth_supported
, m
->auth_payload
);
1692 encode(entity_name
, m
->auth_payload
);
1693 encode(global_id
, m
->auth_payload
);
1694 con
->send_message(m
);
1697 int MonConnection::get_auth_request(
1699 std::vector
<uint32_t> *preferred_modes
,
1700 ceph::buffer::list
*bl
,
1701 const EntityName
& entity_name
,
1703 RotatingKeyRing
* keyring
)
1707 if (auth_method
< 0) {
1708 std::vector
<uint32_t> as
;
1709 auth_registry
->get_supported_methods(con
->get_peer_type(), &as
);
1713 auth_method
= as
.front();
1715 *method
= auth_method
;
1716 auth_registry
->get_supported_modes(con
->get_peer_type(), auth_method
,
1718 ldout(cct
,10) << __func__
<< " method " << *method
1719 << " preferred_modes " << *preferred_modes
<< dendl
;
1720 if (preferred_modes
->empty()) {
1724 int r
= _init_auth(*method
, entity_name
, want_keys
, keyring
, true);
1725 ceph_assert(r
== 0);
1727 // initial requset includes some boilerplate...
1728 encode((char)AUTH_MODE_MON
, *bl
);
1729 encode(entity_name
, *bl
);
1730 encode(global_id
, *bl
);
1732 // and (maybe) some method-specific initial payload
1733 auth
->build_initial_request(bl
);
1738 int MonConnection::handle_auth_reply_more(
1739 AuthConnectionMeta
*auth_meta
,
1740 const ceph::buffer::list
& bl
,
1741 ceph::buffer::list
*reply
)
1743 ldout(cct
, 10) << __func__
<< " payload " << bl
.length() << dendl
;
1744 ldout(cct
, 30) << __func__
<< " got\n";
1748 auto p
= bl
.cbegin();
1749 ldout(cct
, 10) << __func__
<< " payload_len " << bl
.length() << dendl
;
1750 int r
= auth
->handle_response(0, p
, &auth_meta
->session_key
,
1751 &auth_meta
->connection_secret
);
1753 auth
->prepare_build_request();
1754 auth
->build_request(*reply
);
1755 ldout(cct
, 10) << __func__
<< " responding with " << reply
->length()
1756 << " bytes" << dendl
;
1759 lderr(cct
) << __func__
<< " handle_response returned " << r
<< dendl
;
1761 ldout(cct
, 10) << __func__
<< " authenticated!" << dendl
;
1763 ceph_abort(cct
, "write me");
1768 int MonConnection::handle_auth_done(
1769 AuthConnectionMeta
*auth_meta
,
1770 uint64_t new_global_id
,
1771 const ceph::buffer::list
& bl
,
1772 CryptoKey
*session_key
,
1773 std::string
*connection_secret
)
1775 ldout(cct
,10) << __func__
<< " global_id " << new_global_id
1776 << " payload " << bl
.length()
1778 global_id
= new_global_id
;
1779 auth
->set_global_id(global_id
);
1780 auto p
= bl
.begin();
1781 int auth_err
= auth
->handle_response(0, p
, &auth_meta
->session_key
,
1782 &auth_meta
->connection_secret
);
1783 if (auth_err
>= 0) {
1784 state
= State::HAVE_SESSION
;
1786 con
->set_last_keepalive_ack(auth_start
);
1788 if (pending_tell_command
) {
1789 con
->send_message2(std::move(pending_tell_command
));
1794 int MonConnection::handle_auth_bad_method(
1795 uint32_t old_auth_method
,
1797 const std::vector
<uint32_t>& allowed_methods
,
1798 const std::vector
<uint32_t>& allowed_modes
)
1800 ldout(cct
,10) << __func__
<< " old_auth_method " << old_auth_method
1801 << " result " << cpp_strerror(result
)
1802 << " allowed_methods " << allowed_methods
<< dendl
;
1803 std::vector
<uint32_t> auth_supported
;
1804 auth_registry
->get_supported_methods(con
->get_peer_type(), &auth_supported
);
1805 auto p
= std::find(auth_supported
.begin(), auth_supported
.end(),
1807 assert(p
!= auth_supported
.end());
1808 p
= std::find_first_of(std::next(p
), auth_supported
.end(),
1809 allowed_methods
.begin(), allowed_methods
.end());
1810 if (p
== auth_supported
.end()) {
1811 lderr(cct
) << __func__
<< " server allowed_methods " << allowed_methods
1812 << " but i only support " << auth_supported
<< dendl
;
1816 ldout(cct
,10) << __func__
<< " will try " << auth_method
<< " next" << dendl
;
1820 int MonConnection::handle_auth(MAuthReply
* m
,
1821 const EntityName
& entity_name
,
1823 RotatingKeyRing
* keyring
)
1825 if (state
== State::NEGOTIATING
) {
1826 int r
= _negotiate(m
, entity_name
, want_keys
, keyring
);
1830 state
= State::AUTHENTICATING
;
1832 int r
= authenticate(m
);
1834 state
= State::HAVE_SESSION
;
1839 int MonConnection::_negotiate(MAuthReply
*m
,
1840 const EntityName
& entity_name
,
1842 RotatingKeyRing
* keyring
)
1844 int r
= _init_auth(m
->protocol
, entity_name
, want_keys
, keyring
, false);
1845 if (r
== -ENOTSUP
) {
1846 if (m
->result
== -ENOTSUP
) {
1847 ldout(cct
, 10) << "none of our auth protocols are supported by the server"
1855 int MonConnection::_init_auth(
1857 const EntityName
& entity_name
,
1859 RotatingKeyRing
* keyring
,
1862 ldout(cct
, 10) << __func__
<< " method " << method
<< dendl
;
1863 if (auth
&& auth
->get_protocol() == (int)method
) {
1864 ldout(cct
, 10) << __func__
<< " already have auth, reseting" << dendl
;
1869 ldout(cct
, 10) << __func__
<< " creating new auth" << dendl
;
1870 auth
.reset(AuthClientHandler::create(cct
, method
, keyring
));
1872 ldout(cct
, 10) << " no handler for protocol " << method
<< dendl
;
1876 // do not request MGR key unless the mon has the SERVER_KRAKEN
1877 // feature. otherwise it will give us an auth error. note that
1878 // we have to use the FEATUREMASK because pre-jewel the kraken
1879 // feature bit was used for something else.
1881 (want_keys
& CEPH_ENTITY_TYPE_MGR
) &&
1882 !(con
->has_features(CEPH_FEATUREMASK_SERVER_KRAKEN
))) {
1883 ldout(cct
, 1) << __func__
1884 << " not requesting MGR keys from pre-kraken monitor"
1886 want_keys
&= ~CEPH_ENTITY_TYPE_MGR
;
1888 auth
->set_want_keys(want_keys
);
1889 auth
->init(entity_name
);
1890 auth
->set_global_id(global_id
);
1894 int MonConnection::authenticate(MAuthReply
*m
)
1897 if (!m
->global_id
) {
1898 ldout(cct
, 1) << "peer sent an invalid global_id" << dendl
;
1900 if (m
->global_id
!= global_id
) {
1901 // it's a new session
1903 global_id
= m
->global_id
;
1904 auth
->set_global_id(global_id
);
1905 ldout(cct
, 10) << "my global_id is " << m
->global_id
<< dendl
;
1907 auto p
= m
->result_bl
.cbegin();
1908 int ret
= auth
->handle_response(m
->result
, p
, nullptr, nullptr);
1909 if (ret
== -EAGAIN
) {
1910 auto ma
= new MAuth
;
1911 ma
->protocol
= auth
->get_protocol();
1912 auth
->prepare_build_request();
1913 auth
->build_request(ma
->auth_payload
);
1914 con
->send_message(ma
);
1916 if (ret
== 0 && pending_tell_command
) {
1917 con
->send_message2(std::move(pending_tell_command
));
1923 void MonClient::register_config_callback(md_config_t::config_callback fn
) {
1924 ceph_assert(!config_cb
);
1928 md_config_t::config_callback
MonClient::get_config_callback() {
1932 #pragma GCC diagnostic push
1933 #pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
1934 #pragma clang diagnostic push
1935 #pragma clang diagnostic ignored "-Wnon-virtual-dtor"
1936 class monc_error_category
: public ceph::converting_category
{
1938 monc_error_category(){}
1939 const char* name() const noexcept override
;
1940 const char* message(int ev
, char*, std::size_t) const noexcept override
;
1941 std::string
message(int ev
) const override
;
1942 bs::error_condition
default_error_condition(int ev
) const noexcept
1944 bool equivalent(int ev
, const bs::error_condition
& c
) const
1946 using ceph::converting_category::equivalent
;
1947 int from_code(int ev
) const noexcept override
;
1949 #pragma GCC diagnostic pop
1950 #pragma clang diagnostic pop
1952 const char* monc_error_category::name() const noexcept
{
1956 const char* monc_error_category::message(int ev
, char*, std::size_t) const noexcept
{
1960 switch (static_cast<monc_errc
>(ev
)) {
1961 case monc_errc::shutting_down
: // Command failed due to MonClient shutting down
1962 return "Command failed due to MonClient shutting down";
1963 case monc_errc::session_reset
:
1964 return "Monitor session was reset";
1965 case monc_errc::rank_dne
:
1966 return "Requested monitor rank does not exist";
1967 case monc_errc::mon_dne
:
1968 return "Requested monitor does not exist";
1969 case monc_errc::timed_out
:
1970 return "Monitor operation timed out";
1971 case monc_errc::mon_unavailable
:
1972 return "Monitor unavailable";
1975 return "Unknown error";
1978 std::string
monc_error_category::message(int ev
) const {
1979 return message(ev
, nullptr, 0);
1982 bs::error_condition
monc_error_category::default_error_condition(int ev
) const noexcept
{
1983 switch (static_cast<monc_errc
>(ev
)) {
1984 case monc_errc::shutting_down
:
1985 return bs::errc::operation_canceled
;
1986 case monc_errc::session_reset
:
1987 return bs::errc::resource_unavailable_try_again
;
1988 case monc_errc::rank_dne
:
1990 case monc_errc::mon_dne
:
1991 return ceph::errc::not_in_map
;
1992 case monc_errc::timed_out
:
1993 return bs::errc::timed_out
;
1994 case monc_errc::mon_unavailable
:
1995 return bs::errc::no_such_device
;
1997 return { ev
, *this };
2000 bool monc_error_category::equivalent(int ev
, const bs::error_condition
& c
) const noexcept
{
2001 switch (static_cast<monc_errc
>(ev
)) {
2002 case monc_errc::rank_dne
:
2004 case monc_errc::mon_dne
:
2005 return c
== bs::errc::no_such_file_or_directory
;
2007 return default_error_condition(ev
) == c
;
2011 int monc_error_category::from_code(int ev
) const noexcept
{
2015 switch (static_cast<monc_errc
>(ev
)) {
2016 case monc_errc::shutting_down
:
2018 case monc_errc::session_reset
:
2020 case monc_errc::rank_dne
:
2022 case monc_errc::mon_dne
:
2024 case monc_errc::timed_out
:
2026 case monc_errc::mon_unavailable
:
2032 const bs::error_category
& monc_category() noexcept
{
2033 static const monc_error_category c
;