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/scope_guard.h"
25 #include "include/stringify.h"
27 #include "messages/MMonGetMap.h"
28 #include "messages/MMonGetVersion.h"
29 #include "messages/MMonGetVersionReply.h"
30 #include "messages/MMonMap.h"
31 #include "messages/MConfig.h"
32 #include "messages/MGetConfig.h"
33 #include "messages/MAuth.h"
34 #include "messages/MLogAck.h"
35 #include "messages/MAuthReply.h"
36 #include "messages/MMonCommand.h"
37 #include "messages/MMonCommandAck.h"
38 #include "messages/MCommand.h"
39 #include "messages/MCommandReply.h"
40 #include "messages/MPing.h"
42 #include "messages/MMonSubscribe.h"
43 #include "messages/MMonSubscribeAck.h"
44 #include "common/errno.h"
45 #include "common/hostname.h"
46 #include "common/LogClient.h"
48 #include "MonClient.h"
51 #include "auth/Auth.h"
52 #include "auth/KeyRing.h"
53 #include "auth/AuthClientHandler.h"
54 #include "auth/AuthRegistry.h"
55 #include "auth/RotatingKeyRing.h"
57 #define dout_subsys ceph_subsys_monc
59 #define dout_prefix *_dout << "monclient" << (_hunting() ? "(hunting)":"") << ": "
63 MonClient::MonClient(CephContext
*cct_
) :
67 timer(cct_
, monc_lock
),
71 more_log_pending(false),
73 had_a_connection(false),
74 reopen_interval_multiplier(
75 cct_
->_conf
.get_val
<double>("mon_client_hunt_interval_min_multiple")),
76 last_mon_command_tid(0),
80 MonClient::~MonClient()
84 int MonClient::build_initial_monmap()
86 ldout(cct
, 10) << __func__
<< dendl
;
87 int r
= monmap
.build_initial(cct
, false, std::cerr
);
88 ldout(cct
,10) << "monmap:\n";
94 int MonClient::get_monmap()
96 ldout(cct
, 10) << __func__
<< dendl
;
97 std::unique_lock
l(monc_lock
);
99 sub
.want("monmap", 0, 0);
102 map_cond
.wait(l
, [this] { return !want_monmap
; });
103 ldout(cct
, 10) << __func__
<< " done" << dendl
;
107 int MonClient::get_monmap_and_config()
109 ldout(cct
, 10) << __func__
<< dendl
;
110 ceph_assert(!messenger
);
115 auto shutdown_crypto
= make_scope_guard([this] {
116 cct
->shutdown_crypto();
119 int r
= build_initial_monmap();
121 lderr(cct
) << __func__
<< " cannot identify monitors to contact" << dendl
;
125 messenger
= Messenger::create_client_messenger(
126 cct
, "temp_mon_client");
127 ceph_assert(messenger
);
128 messenger
->add_dispatcher_head(this);
130 auto shutdown_msgr
= make_scope_guard([this] {
131 messenger
->shutdown();
135 if (!monmap
.fsid
.is_zero()) {
136 cct
->_conf
.set_val("fsid", stringify(monmap
.fsid
));
140 while (tries
-- > 0) {
145 r
= authenticate(cct
->_conf
->client_mount_timeout
);
146 if (r
== -ETIMEDOUT
) {
154 std::unique_lock
l(monc_lock
);
155 if (monmap
.get_epoch() &&
156 !monmap
.persistent_features
.contains_all(
157 ceph::features::mon::FEATURE_MIMIC
)) {
158 ldout(cct
,10) << __func__
<< " pre-mimic monitor, no config to fetch"
163 while ((!got_config
|| monmap
.get_epoch() == 0) && r
== 0) {
164 ldout(cct
,20) << __func__
<< " waiting for monmap|config" << dendl
;
165 map_cond
.wait_for(l
, ceph::make_timespan(
166 cct
->_conf
->mon_client_hunt_interval
));
169 ldout(cct
,10) << __func__
<< " success" << dendl
;
174 lderr(cct
) << __func__
<< " failed to get config" << dendl
;
185 * Ping the monitor with id @p mon_id and set the resulting reply in
186 * the provided @p result_reply, if this last parameter is not NULL.
188 * So that we don't rely on the MonClient's default messenger, set up
189 * during connect(), we create our own messenger to comunicate with the
190 * specified monitor. This is advantageous in the following ways:
192 * - Isolate the ping procedure from the rest of the MonClient's operations,
193 * allowing us to not acquire or manage the big monc_lock, thus not
194 * having to block waiting for some other operation to finish before we
196 * * for instance, we can ping mon.FOO even if we are currently hunting
197 * or blocked waiting for auth to complete with mon.BAR.
199 * - Ping a monitor prior to establishing a connection (using connect())
200 * and properly establish the MonClient's messenger. This frees us
201 * from dealing with the complex foo that happens in connect().
203 * We also don't rely on MonClient as a dispatcher for this messenger,
204 * unlike what happens with the MonClient's default messenger. This allows
205 * us to sandbox the whole ping, having it much as a separate entity in
206 * the MonClient class, considerably simplifying the handling and dispatching
207 * of messages without needing to consider monc_lock.
209 * Current drawback is that we will establish a messenger for each ping
210 * we want to issue, instead of keeping a single messenger instance that
211 * would be used for all pings.
213 int MonClient::ping_monitor(const string
&mon_id
, string
*result_reply
)
215 ldout(cct
, 10) << __func__
<< dendl
;
218 if (monmap
.contains("noname-"+mon_id
)) {
219 new_mon_id
= "noname-"+mon_id
;
224 if (new_mon_id
.empty()) {
225 ldout(cct
, 10) << __func__
<< " specified mon id is empty!" << dendl
;
227 } else if (!monmap
.contains(new_mon_id
)) {
228 ldout(cct
, 10) << __func__
<< " no such monitor 'mon." << new_mon_id
<< "'"
233 // N.B. monc isn't initialized
235 auth_registry
.refresh_config();
238 keyring
.from_ceph_context(cct
);
239 RotatingKeyRing
rkeyring(cct
, cct
->get_module_type(), &keyring
);
241 MonClientPinger
*pinger
= new MonClientPinger(cct
,
245 Messenger
*smsgr
= Messenger::create_client_messenger(cct
, "temp_ping_client");
246 smsgr
->add_dispatcher_head(pinger
);
247 smsgr
->set_auth_client(pinger
);
250 ConnectionRef con
= smsgr
->connect_to_mon(monmap
.get_addrs(new_mon_id
));
251 ldout(cct
, 10) << __func__
<< " ping mon." << new_mon_id
252 << " " << con
->get_peer_addr() << dendl
;
254 pinger
->mc
.reset(new MonConnection(cct
, con
, 0, &auth_registry
));
255 pinger
->mc
->start(monmap
.get_epoch(), entity_name
);
256 con
->send_message(new MPing
);
258 int ret
= pinger
->wait_for_reply(cct
->_conf
->mon_client_ping_timeout
);
260 ldout(cct
,10) << __func__
<< " got ping reply" << dendl
;
274 bool MonClient::ms_dispatch(Message
*m
)
276 // we only care about these message types
277 switch (m
->get_type()) {
278 case CEPH_MSG_MON_MAP
:
279 case CEPH_MSG_AUTH_REPLY
:
280 case CEPH_MSG_MON_SUBSCRIBE_ACK
:
281 case CEPH_MSG_MON_GET_VERSION_REPLY
:
282 case MSG_MON_COMMAND_ACK
:
283 case MSG_COMMAND_REPLY
:
294 std::lock_guard
lock(monc_lock
);
296 if (!m
->get_connection()->is_anon() &&
297 m
->get_source().type() == CEPH_ENTITY_TYPE_MON
) {
299 auto p
= _find_pending_con(m
->get_connection());
300 if (p
== pending_cons
.end()) {
301 // ignore any messages outside hunting sessions
302 ldout(cct
, 10) << "discarding stray monitor message " << *m
<< dendl
;
306 } else if (!active_con
|| active_con
->get_con() != m
->get_connection()) {
307 // ignore any messages outside our session(s)
308 ldout(cct
, 10) << "discarding stray monitor message " << *m
<< dendl
;
314 switch (m
->get_type()) {
315 case CEPH_MSG_MON_MAP
:
316 handle_monmap(static_cast<MMonMap
*>(m
));
317 if (passthrough_monmap
) {
323 case CEPH_MSG_AUTH_REPLY
:
324 handle_auth(static_cast<MAuthReply
*>(m
));
326 case CEPH_MSG_MON_SUBSCRIBE_ACK
:
327 handle_subscribe_ack(static_cast<MMonSubscribeAck
*>(m
));
329 case CEPH_MSG_MON_GET_VERSION_REPLY
:
330 handle_get_version_reply(static_cast<MMonGetVersionReply
*>(m
));
332 case MSG_MON_COMMAND_ACK
:
333 handle_mon_command_ack(static_cast<MMonCommandAck
*>(m
));
335 case MSG_COMMAND_REPLY
:
336 if (m
->get_connection()->is_anon() &&
337 m
->get_source().type() == CEPH_ENTITY_TYPE_MON
) {
338 // this connection is from 'tell'... ignore everything except our command
339 // reply. (we'll get misc other message because we authenticated, but we
341 handle_command_reply(static_cast<MCommandReply
*>(m
));
344 // leave the message for another dispatch handler (e.g., Objecter)
348 log_client
->handle_log_ack(static_cast<MLogAck
*>(m
));
350 if (more_log_pending
) {
358 handle_config(static_cast<MConfig
*>(m
));
364 void MonClient::send_log(bool flush
)
367 auto lm
= log_client
->get_mon_log_message(flush
);
369 _send_mon_message(std::move(lm
));
370 more_log_pending
= log_client
->are_pending();
374 void MonClient::flush_log()
376 std::lock_guard
l(monc_lock
);
380 /* Unlike all the other message-handling functions, we don't put away a reference
381 * because we want to support MMonMap passthrough to other Dispatchers. */
382 void MonClient::handle_monmap(MMonMap
*m
)
384 ldout(cct
, 10) << __func__
<< " " << *m
<< dendl
;
385 auto con_addrs
= m
->get_source_addrs();
386 string old_name
= monmap
.get_name(con_addrs
);
387 const auto old_epoch
= monmap
.get_epoch();
389 auto p
= m
->monmapbl
.cbegin();
392 ldout(cct
, 10) << " got monmap " << monmap
.epoch
393 << " from mon." << old_name
394 << " (according to old e" << monmap
.get_epoch() << ")"
396 ldout(cct
, 10) << "dump:\n";
397 monmap
.print(*_dout
);
400 if (old_epoch
!= monmap
.get_epoch()) {
403 if (old_name
.size() == 0) {
404 ldout(cct
,10) << " can't identify which mon we were connected to" << dendl
;
407 auto new_name
= monmap
.get_name(con_addrs
);
408 if (new_name
.empty()) {
409 ldout(cct
, 10) << "mon." << old_name
<< " at " << con_addrs
410 << " went away" << dendl
;
411 // can't find the mon we were talking to (above)
413 } else if (messenger
->should_use_msgr2() &&
414 monmap
.get_addrs(new_name
).has_msgr2() &&
415 !con_addrs
.has_msgr2()) {
416 ldout(cct
,1) << " mon." << new_name
<< " has (v2) addrs "
417 << monmap
.get_addrs(new_name
) << " but i'm connected to "
418 << con_addrs
<< ", reconnecting" << dendl
;
423 sub
.got("monmap", monmap
.get_epoch());
424 map_cond
.notify_all();
427 if (authenticate_err
== 1) {
432 void MonClient::handle_config(MConfig
*m
)
434 ldout(cct
,10) << __func__
<< " " << *m
<< dendl
;
435 finisher
.queue(new LambdaContext([this, m
](int r
) {
436 cct
->_conf
.set_mon_vals(cct
, m
->config
, config_cb
);
437 if (config_notify_cb
) {
443 map_cond
.notify_all();
446 // ----------------------
448 int MonClient::init()
450 ldout(cct
, 10) << __func__
<< dendl
;
452 entity_name
= cct
->_conf
->name
;
454 auth_registry
.refresh_config();
456 std::lock_guard
l(monc_lock
);
457 keyring
.reset(new KeyRing
);
458 if (auth_registry
.is_supported_method(messenger
->get_mytype(),
460 // this should succeed, because auth_registry just checked!
461 int r
= keyring
->from_ceph_context(cct
);
463 // but be somewhat graceful in case there was a race condition
464 lderr(cct
) << "keyring not found" << dendl
;
468 if (!auth_registry
.any_supported_methods(messenger
->get_mytype())) {
472 rotating_secrets
.reset(
473 new RotatingKeyRing(cct
, cct
->get_module_type(), keyring
.get()));
477 messenger
->set_auth_client(this);
478 messenger
->add_dispatcher_head(this);
487 void MonClient::shutdown()
489 ldout(cct
, 10) << __func__
<< dendl
;
492 while (!version_requests
.empty()) {
493 version_requests
.begin()->second
->context
->complete(-ECANCELED
);
494 ldout(cct
, 20) << __func__
<< " canceling and discarding version request "
495 << version_requests
.begin()->second
<< dendl
;
496 delete version_requests
.begin()->second
;
497 version_requests
.erase(version_requests
.begin());
499 while (!mon_commands
.empty()) {
500 auto tid
= mon_commands
.begin()->first
;
501 _cancel_mon_command(tid
);
503 ldout(cct
, 20) << __func__
<< " discarding " << waiting_for_session
.size()
504 << " pending message(s)" << dendl
;
505 waiting_for_session
.clear();
508 pending_cons
.clear();
514 finisher
.wait_for_empty();
524 int MonClient::authenticate(double timeout
)
526 std::unique_lock lock
{monc_lock
};
529 ldout(cct
, 5) << "already authenticated" << dendl
;
532 sub
.want("monmap", monmap
.get_epoch() ? monmap
.get_epoch() + 1 : 0, 0);
533 sub
.want("config", 0, 0);
537 auto until
= ceph::real_clock::now();
538 until
+= ceph::make_timespan(timeout
);
540 ldout(cct
, 10) << "authenticate will time out at " << until
<< dendl
;
541 authenticate_err
= 1; // == in progress
542 while (!active_con
&& authenticate_err
>= 0) {
544 auto r
= auth_cond
.wait_until(lock
, until
);
545 if (r
== cv_status::timeout
&& !active_con
) {
546 ldout(cct
, 0) << "authenticate timed out after " << timeout
<< dendl
;
547 authenticate_err
= -ETIMEDOUT
;
550 auth_cond
.wait(lock
);
555 ldout(cct
, 5) << __func__
<< " success, global_id "
556 << active_con
->get_global_id() << dendl
;
557 // active_con should not have been set if there was an error
558 ceph_assert(authenticate_err
>= 0);
559 authenticated
= true;
562 if (authenticate_err
< 0 && auth_registry
.no_keyring_disabled_cephx()) {
563 lderr(cct
) << __func__
<< " NOTE: no keyring found; disabled cephx authentication" << dendl
;
566 return authenticate_err
;
569 void MonClient::handle_auth(MAuthReply
*m
)
571 ceph_assert(ceph_mutex_is_locked(monc_lock
));
573 if (m
->get_connection()->is_anon()) {
574 // anon connection, used for mon tell commands
575 for (auto& p
: mon_commands
) {
576 if (p
.second
->target_con
== m
->get_connection()) {
577 auto& mc
= p
.second
->target_session
;
578 int ret
= mc
->handle_auth(m
, entity_name
,
579 CEPH_ENTITY_TYPE_MON
,
580 rotating_secrets
.get());
581 (void)ret
; // we don't care
590 std::swap(active_con
->get_auth(), auth
);
591 int ret
= active_con
->authenticate(m
);
593 std::swap(auth
, active_con
->get_auth());
594 if (global_id
!= active_con
->get_global_id()) {
595 lderr(cct
) << __func__
<< " peer assigned me a different global_id: "
596 << active_con
->get_global_id() << dendl
;
598 if (ret
!= -EAGAIN
) {
605 auto found
= _find_pending_con(m
->get_connection());
606 ceph_assert(found
!= pending_cons
.end());
607 int auth_err
= found
->second
.handle_auth(m
, entity_name
, want_keys
,
608 rotating_secrets
.get());
610 if (auth_err
== -EAGAIN
) {
614 pending_cons
.erase(found
);
615 if (!pending_cons
.empty()) {
616 // keep trying with pending connections
619 // the last try just failed, give up.
621 auto& mc
= found
->second
;
622 ceph_assert(mc
.have_session());
623 active_con
.reset(new MonConnection(std::move(mc
)));
624 pending_cons
.clear();
627 _finish_hunting(auth_err
);
628 _finish_auth(auth_err
);
631 void MonClient::_finish_auth(int auth_err
)
633 ldout(cct
,10) << __func__
<< " " << auth_err
<< dendl
;
634 authenticate_err
= auth_err
;
635 // _resend_mon_commands() could _reopen_session() if the connected mon is not
636 // the one the MonCommand is targeting.
637 if (!auth_err
&& active_con
) {
639 _check_auth_tickets();
641 auth_cond
.notify_all();
644 Context
*cb
= nullptr;
645 if (session_established_context
) {
646 cb
= session_established_context
.release();
658 void MonClient::send_mon_message(MessageRef m
)
660 std::lock_guard l
{monc_lock
};
661 _send_mon_message(std::move(m
));
664 void MonClient::_send_mon_message(MessageRef m
)
666 ceph_assert(ceph_mutex_is_locked(monc_lock
));
668 auto cur_con
= active_con
->get_con();
669 ldout(cct
, 10) << "_send_mon_message to mon."
670 << monmap
.get_name(cur_con
->get_peer_addr())
671 << " at " << cur_con
->get_peer_addr() << dendl
;
672 cur_con
->send_message2(std::move(m
));
674 waiting_for_session
.push_back(std::move(m
));
678 void MonClient::_reopen_session(int rank
)
680 ceph_assert(ceph_mutex_is_locked(monc_lock
));
681 ldout(cct
, 10) << __func__
<< " rank " << rank
<< dendl
;
684 pending_cons
.clear();
689 _add_conn(rank
, global_id
);
691 _add_conns(global_id
);
694 // throw out old queued messages
695 waiting_for_session
.clear();
697 // throw out version check requests
698 while (!version_requests
.empty()) {
699 finisher
.queue(version_requests
.begin()->second
->context
, -EAGAIN
);
700 delete version_requests
.begin()->second
;
701 version_requests
.erase(version_requests
.begin());
704 for (auto& c
: pending_cons
) {
705 c
.second
.start(monmap
.get_epoch(), entity_name
);
713 MonConnection
& MonClient::_add_conn(unsigned rank
, uint64_t global_id
)
715 auto peer
= monmap
.get_addrs(rank
);
716 auto conn
= messenger
->connect_to_mon(peer
);
717 MonConnection
mc(cct
, conn
, global_id
, &auth_registry
);
718 auto inserted
= pending_cons
.insert(std::make_pair(peer
, std::move(mc
)));
719 ldout(cct
, 10) << "picked mon." << monmap
.get_name(rank
)
723 return inserted
.first
->second
;
726 void MonClient::_add_conns(uint64_t global_id
)
728 // collect the next batch of candidates who are listed right next to the ones
730 auto get_next_batch
= [this]() -> std::vector
<unsigned> {
731 std::multimap
<uint16_t, unsigned> ranks_by_priority
;
733 monmap
.mon_info
| boost::adaptors::filtered(
735 auto rank
= monmap
.get_rank(info
.first
);
736 return tried
.count(rank
) == 0;
737 }) | boost::adaptors::transformed(
739 auto rank
= monmap
.get_rank(info
.first
);
740 return std::make_pair(info
.second
.priority
, rank
);
741 }), std::inserter(ranks_by_priority
, end(ranks_by_priority
)));
742 if (ranks_by_priority
.empty()) {
745 // only choose the monitors with lowest priority
746 auto cands
= boost::make_iterator_range(
747 ranks_by_priority
.equal_range(ranks_by_priority
.begin()->first
));
748 std::vector
<unsigned> ranks
;
749 boost::range::copy(cands
| boost::adaptors::map_values
,
750 std::back_inserter(ranks
));
753 auto ranks
= get_next_batch();
755 tried
.clear(); // start over
756 ranks
= get_next_batch();
758 ceph_assert(!ranks
.empty());
759 if (ranks
.size() > 1) {
760 std::vector
<uint16_t> weights
;
761 for (auto i
: ranks
) {
762 auto rank_name
= monmap
.get_name(i
);
763 weights
.push_back(monmap
.get_weight(rank_name
));
765 std::random_device rd
;
766 if (std::accumulate(begin(weights
), end(weights
), 0u) == 0) {
767 std::shuffle(begin(ranks
), end(ranks
), std::mt19937
{rd()});
769 weighted_shuffle(begin(ranks
), end(ranks
), begin(weights
), end(weights
),
773 ldout(cct
, 10) << __func__
<< " ranks=" << ranks
<< dendl
;
774 unsigned n
= cct
->_conf
->mon_client_hunt_parallel
;
775 if (n
== 0 || n
> ranks
.size()) {
778 for (unsigned i
= 0; i
< n
; i
++) {
779 _add_conn(ranks
[i
], global_id
);
780 tried
.insert(ranks
[i
]);
784 bool MonClient::ms_handle_reset(Connection
*con
)
786 std::lock_guard
lock(monc_lock
);
788 if (con
->get_peer_type() != CEPH_ENTITY_TYPE_MON
)
791 if (con
->is_anon()) {
792 auto p
= mon_commands
.begin();
793 while (p
!= mon_commands
.end()) {
794 auto cmd
= p
->second
;
796 if (cmd
->target_con
== con
) {
797 _send_command(cmd
); // may retry or fail
805 if (pending_cons
.count(con
->get_peer_addrs())) {
806 ldout(cct
, 10) << __func__
<< " hunted mon " << con
->get_peer_addrs()
809 ldout(cct
, 10) << __func__
<< " stray mon " << con
->get_peer_addrs()
814 if (active_con
&& con
== active_con
->get_con()) {
815 ldout(cct
, 10) << __func__
<< " current mon " << con
->get_peer_addrs()
820 ldout(cct
, 10) << "ms_handle_reset stray mon " << con
->get_peer_addrs()
827 bool MonClient::_opened() const
829 ceph_assert(ceph_mutex_is_locked(monc_lock
));
830 return active_con
|| _hunting();
833 bool MonClient::_hunting() const
835 return !pending_cons
.empty();
838 void MonClient::_start_hunting()
840 ceph_assert(!_hunting());
841 // adjust timeouts if necessary
842 if (!had_a_connection
)
844 reopen_interval_multiplier
*= cct
->_conf
->mon_client_hunt_interval_backoff
;
845 if (reopen_interval_multiplier
>
846 cct
->_conf
->mon_client_hunt_interval_max_multiple
) {
847 reopen_interval_multiplier
=
848 cct
->_conf
->mon_client_hunt_interval_max_multiple
;
852 void MonClient::_finish_hunting(int auth_err
)
854 ldout(cct
,10) << __func__
<< " " << auth_err
<< dendl
;
855 ceph_assert(ceph_mutex_is_locked(monc_lock
));
856 // the pending conns have been cleaned.
857 ceph_assert(!_hunting());
859 auto con
= active_con
->get_con();
860 ldout(cct
, 1) << "found mon."
861 << monmap
.get_name(con
->get_peer_addr())
864 ldout(cct
, 1) << "no mon sessions established" << dendl
;
867 had_a_connection
= true;
871 last_rotating_renew_sent
= utime_t();
872 while (!waiting_for_session
.empty()) {
873 _send_mon_message(std::move(waiting_for_session
.front()));
874 waiting_for_session
.pop_front();
876 _resend_mon_commands();
879 std::swap(auth
, active_con
->get_auth());
880 if (global_id
&& global_id
!= active_con
->get_global_id()) {
881 lderr(cct
) << __func__
<< " global_id changed from " << global_id
882 << " to " << active_con
->get_global_id() << dendl
;
884 global_id
= active_con
->get_global_id();
889 void MonClient::tick()
891 ldout(cct
, 10) << __func__
<< dendl
;
893 utime_t now
= ceph_clock_now();
895 auto reschedule_tick
= make_scope_guard([this] {
899 _check_auth_tickets();
900 _check_tell_commands();
903 ldout(cct
, 1) << "continuing hunt" << dendl
;
904 return _reopen_session();
905 } else if (active_con
) {
906 // just renew as needed
907 auto cur_con
= active_con
->get_con();
908 if (!cur_con
->has_feature(CEPH_FEATURE_MON_STATEFUL_SUB
)) {
909 const bool maybe_renew
= sub
.need_renew();
910 ldout(cct
, 10) << "renew subs? -- " << (maybe_renew
? "yes" : "no")
917 if (now
> last_keepalive
+ cct
->_conf
->mon_client_ping_interval
) {
918 cur_con
->send_keepalive();
919 last_keepalive
= now
;
921 if (cct
->_conf
->mon_client_ping_timeout
> 0 &&
922 cur_con
->has_feature(CEPH_FEATURE_MSGR_KEEPALIVE2
)) {
923 utime_t lk
= cur_con
->get_last_keepalive_ack();
924 utime_t interval
= now
- lk
;
925 if (interval
> cct
->_conf
->mon_client_ping_timeout
) {
926 ldout(cct
, 1) << "no keepalive since " << lk
<< " (" << interval
927 << " seconds), reconnecting" << dendl
;
928 return _reopen_session();
935 if (now
> last_send_log
+ cct
->_conf
->mon_client_log_interval
) {
942 void MonClient::_un_backoff()
944 // un-backoff our reconnect interval
945 reopen_interval_multiplier
= std::max(
946 cct
->_conf
.get_val
<double>("mon_client_hunt_interval_min_multiple"),
947 reopen_interval_multiplier
/
948 cct
->_conf
.get_val
<double>("mon_client_hunt_interval_backoff"));
949 ldout(cct
, 20) << __func__
<< " reopen_interval_multipler now "
950 << reopen_interval_multiplier
<< dendl
;
953 void MonClient::schedule_tick()
955 auto do_tick
= make_lambda_context([this](int) { tick(); });
956 if (!is_connected()) {
957 // start another round of hunting
958 const auto hunt_interval
= (cct
->_conf
->mon_client_hunt_interval
*
959 reopen_interval_multiplier
);
960 timer
.add_event_after(hunt_interval
, do_tick
);
963 timer
.add_event_after(std::min(cct
->_conf
->mon_client_ping_interval
,
964 cct
->_conf
->mon_client_log_interval
),
971 void MonClient::_renew_subs()
973 ceph_assert(ceph_mutex_is_locked(monc_lock
));
974 if (!sub
.have_new()) {
975 ldout(cct
, 10) << __func__
<< " - empty" << dendl
;
979 ldout(cct
, 10) << __func__
<< dendl
;
983 auto m
= ceph::make_message
<MMonSubscribe
>();
984 m
->what
= sub
.get_subs();
985 m
->hostname
= ceph_get_short_hostname();
986 _send_mon_message(std::move(m
));
991 void MonClient::handle_subscribe_ack(MMonSubscribeAck
*m
)
993 sub
.acked(m
->interval
);
997 int MonClient::_check_auth_tickets()
999 ceph_assert(ceph_mutex_is_locked(monc_lock
));
1000 if (active_con
&& auth
) {
1001 if (auth
->need_tickets()) {
1002 ldout(cct
, 10) << __func__
<< " getting new tickets!" << dendl
;
1003 auto m
= ceph::make_message
<MAuth
>();
1004 m
->protocol
= auth
->get_protocol();
1005 auth
->prepare_build_request();
1006 auth
->build_request(m
->auth_payload
);
1007 _send_mon_message(m
);
1010 _check_auth_rotating();
1015 int MonClient::_check_auth_rotating()
1017 ceph_assert(ceph_mutex_is_locked(monc_lock
));
1018 if (!rotating_secrets
||
1019 !auth_principal_needs_rotating_keys(entity_name
)) {
1020 ldout(cct
, 20) << "_check_auth_rotating not needed by " << entity_name
<< dendl
;
1024 if (!active_con
|| !auth
) {
1025 ldout(cct
, 10) << "_check_auth_rotating waiting for auth session" << dendl
;
1029 utime_t now
= ceph_clock_now();
1030 utime_t cutoff
= now
;
1031 cutoff
-= std::min(30.0, cct
->_conf
->auth_service_ticket_ttl
/ 4.0);
1032 utime_t issued_at_lower_bound
= now
;
1033 issued_at_lower_bound
-= cct
->_conf
->auth_service_ticket_ttl
;
1034 if (!rotating_secrets
->need_new_secrets(cutoff
)) {
1035 ldout(cct
, 10) << "_check_auth_rotating have uptodate secrets (they expire after " << cutoff
<< ")" << dendl
;
1036 rotating_secrets
->dump_rotating();
1040 ldout(cct
, 10) << "_check_auth_rotating renewing rotating keys (they expired before " << cutoff
<< ")" << dendl
;
1041 if (!rotating_secrets
->need_new_secrets() &&
1042 rotating_secrets
->need_new_secrets(issued_at_lower_bound
)) {
1043 // the key has expired before it has been issued?
1044 lderr(cct
) << __func__
<< " possible clock skew, rotating keys expired way too early"
1045 << " (before " << issued_at_lower_bound
<< ")" << dendl
;
1047 if ((now
> last_rotating_renew_sent
) &&
1048 double(now
- last_rotating_renew_sent
) < 1) {
1049 ldout(cct
, 10) << __func__
<< " called too often (last: "
1050 << last_rotating_renew_sent
<< "), skipping refresh" << dendl
;
1053 auto m
= ceph::make_message
<MAuth
>();
1054 m
->protocol
= auth
->get_protocol();
1055 if (auth
->build_rotating_request(m
->auth_payload
)) {
1056 last_rotating_renew_sent
= now
;
1057 _send_mon_message(std::move(m
));
1062 int MonClient::wait_auth_rotating(double timeout
)
1064 std::unique_lock
l(monc_lock
);
1066 // Must be initialized
1067 ceph_assert(auth
!= nullptr);
1069 if (auth
->get_protocol() == CEPH_AUTH_NONE
)
1072 if (!rotating_secrets
)
1075 ldout(cct
, 10) << __func__
<< " waiting for " << timeout
<< dendl
;
1076 utime_t now
= ceph_clock_now();
1077 if (auth_cond
.wait_for(l
, ceph::make_timespan(timeout
), [now
, this] {
1078 return (!auth_principal_needs_rotating_keys(entity_name
) ||
1079 !rotating_secrets
->need_new_secrets(now
));
1081 ldout(cct
, 10) << __func__
<< " done" << dendl
;
1084 ldout(cct
, 0) << __func__
<< " timed out after " << timeout
<< dendl
;
1091 void MonClient::_send_command(MonCommand
*r
)
1095 if (r
->send_attempts
> cct
->_conf
->mon_client_directed_command_retry
) {
1096 _finish_command(r
, -ENXIO
, "mon unavailable");
1100 // tell-style command
1101 if (monmap
.min_mon_release
>= ceph_release_t::octopus
) {
1102 if (r
->target_con
) {
1103 r
->target_con
->mark_down();
1105 if (r
->target_rank
>= 0) {
1106 if (r
->target_rank
>= (int)monmap
.size()) {
1107 ldout(cct
, 10) << " target " << r
->target_rank
1108 << " >= max mon " << monmap
.size() << dendl
;
1109 _finish_command(r
, -ENOENT
, "mon rank dne");
1112 r
->target_con
= messenger
->connect_to_mon(
1113 monmap
.get_addrs(r
->target_rank
), true /* anon */);
1115 if (!monmap
.contains(r
->target_name
)) {
1116 ldout(cct
, 10) << " target " << r
->target_name
1117 << " not present in monmap" << dendl
;
1118 _finish_command(r
, -ENOENT
, "mon dne");
1121 r
->target_con
= messenger
->connect_to_mon(
1122 monmap
.get_addrs(r
->target_name
), true /* anon */);
1125 r
->target_session
.reset(new MonConnection(cct
, r
->target_con
, 0,
1127 r
->target_session
->start(monmap
.get_epoch(), entity_name
);
1128 r
->last_send_attempt
= ceph_clock_now();
1130 MCommand
*m
= new MCommand(monmap
.fsid
);
1133 m
->set_data(r
->inbl
);
1134 r
->target_session
->queue_command(m
);
1138 // ugly legacy handling of pre-octopus mons
1141 peer
= active_con
->get_con()->get_peer_addr();
1144 if (r
->target_rank
>= 0 &&
1145 r
->target_rank
!= monmap
.get_rank(peer
)) {
1146 ldout(cct
, 10) << __func__
<< " " << r
->tid
<< " " << r
->cmd
1147 << " wants rank " << r
->target_rank
1148 << ", reopening session"
1150 if (r
->target_rank
>= (int)monmap
.size()) {
1151 ldout(cct
, 10) << " target " << r
->target_rank
1152 << " >= max mon " << monmap
.size() << dendl
;
1153 _finish_command(r
, -ENOENT
, "mon rank dne");
1156 _reopen_session(r
->target_rank
);
1159 if (r
->target_name
.length() &&
1160 r
->target_name
!= monmap
.get_name(peer
)) {
1161 ldout(cct
, 10) << __func__
<< " " << r
->tid
<< " " << r
->cmd
1162 << " wants mon " << r
->target_name
1163 << ", reopening session"
1165 if (!monmap
.contains(r
->target_name
)) {
1166 ldout(cct
, 10) << " target " << r
->target_name
1167 << " not present in monmap" << dendl
;
1168 _finish_command(r
, -ENOENT
, "mon dne");
1171 _reopen_session(monmap
.get_rank(r
->target_name
));
1174 // fall-thru to send 'normal' CLI command
1177 // normal CLI command
1178 ldout(cct
, 10) << __func__
<< " " << r
->tid
<< " " << r
->cmd
<< dendl
;
1179 auto m
= ceph::make_message
<MMonCommand
>(monmap
.fsid
);
1182 m
->set_data(r
->inbl
);
1183 _send_mon_message(std::move(m
));
1187 void MonClient::_check_tell_commands()
1189 // resend any requests
1190 auto now
= ceph_clock_now();
1191 auto p
= mon_commands
.begin();
1192 while (p
!= mon_commands
.end()) {
1193 auto cmd
= p
->second
;
1195 if (cmd
->is_tell() &&
1196 cmd
->last_send_attempt
!= utime_t() &&
1197 now
- cmd
->last_send_attempt
> cct
->_conf
->mon_client_hunt_interval
) {
1198 ldout(cct
,5) << __func__
<< " timeout tell command " << cmd
->tid
<< dendl
;
1199 _send_command(cmd
); // might remove cmd from mon_commands
1204 void MonClient::_resend_mon_commands()
1206 // resend any requests
1207 auto p
= mon_commands
.begin();
1208 while (p
!= mon_commands
.end()) {
1209 auto cmd
= p
->second
;
1211 if (cmd
->is_tell() && monmap
.min_mon_release
>= ceph_release_t::octopus
) {
1212 // starting with octopus, tell commands use their own connetion and need no
1213 // special resend when we finish hunting.
1215 _send_command(cmd
); // might remove cmd from mon_commands
1220 void MonClient::handle_mon_command_ack(MMonCommandAck
*ack
)
1222 MonCommand
*r
= NULL
;
1223 uint64_t tid
= ack
->get_tid();
1225 if (tid
== 0 && !mon_commands
.empty()) {
1226 r
= mon_commands
.begin()->second
;
1227 ldout(cct
, 10) << __func__
<< " has tid 0, assuming it is " << r
->tid
<< dendl
;
1229 auto p
= mon_commands
.find(tid
);
1230 if (p
== mon_commands
.end()) {
1231 ldout(cct
, 10) << __func__
<< " " << ack
->get_tid() << " not found" << dendl
;
1238 ldout(cct
, 10) << __func__
<< " " << r
->tid
<< " " << r
->cmd
<< dendl
;
1240 r
->poutbl
->claim(ack
->get_data());
1241 _finish_command(r
, ack
->r
, ack
->rs
);
1245 void MonClient::handle_command_reply(MCommandReply
*reply
)
1247 MonCommand
*r
= NULL
;
1248 uint64_t tid
= reply
->get_tid();
1250 if (tid
== 0 && !mon_commands
.empty()) {
1251 r
= mon_commands
.begin()->second
;
1252 ldout(cct
, 10) << __func__
<< " has tid 0, assuming it is " << r
->tid
1255 auto p
= mon_commands
.find(tid
);
1256 if (p
== mon_commands
.end()) {
1257 ldout(cct
, 10) << __func__
<< " " << reply
->get_tid() << " not found"
1265 ldout(cct
, 10) << __func__
<< " " << r
->tid
<< " " << r
->cmd
<< dendl
;
1267 r
->poutbl
->claim(reply
->get_data());
1268 _finish_command(r
, reply
->r
, reply
->rs
);
1272 int MonClient::_cancel_mon_command(uint64_t tid
)
1274 ceph_assert(ceph_mutex_is_locked(monc_lock
));
1276 auto it
= mon_commands
.find(tid
);
1277 if (it
== mon_commands
.end()) {
1278 ldout(cct
, 10) << __func__
<< " tid " << tid
<< " dne" << dendl
;
1282 ldout(cct
, 10) << __func__
<< " tid " << tid
<< dendl
;
1284 MonCommand
*cmd
= it
->second
;
1285 _finish_command(cmd
, -ETIMEDOUT
, "");
1289 void MonClient::_finish_command(MonCommand
*r
, int ret
, string rs
)
1291 ldout(cct
, 10) << __func__
<< " " << r
->tid
<< " = " << ret
<< " " << rs
<< dendl
;
1297 finisher
.queue(r
->onfinish
, ret
);
1298 if (r
->target_con
) {
1299 r
->target_con
->mark_down();
1301 mon_commands
.erase(r
->tid
);
1305 void MonClient::start_mon_command(const std::vector
<string
>& cmd
,
1306 const ceph::buffer::list
& inbl
,
1307 ceph::buffer::list
*outbl
, string
*outs
,
1310 ldout(cct
,10) << __func__
<< " cmd=" << cmd
<< dendl
;
1311 std::lock_guard
l(monc_lock
);
1312 if (!initialized
|| stopping
) {
1314 onfinish
->complete(-ECANCELED
);
1318 MonCommand
*r
= new MonCommand(++last_mon_command_tid
);
1323 r
->onfinish
= onfinish
;
1324 if (cct
->_conf
->rados_mon_op_timeout
> 0) {
1325 class C_CancelMonCommand
: public Context
1330 C_CancelMonCommand(uint64_t tid
, MonClient
*monc
) : tid(tid
), monc(monc
) {}
1331 void finish(int r
) override
{
1332 monc
->_cancel_mon_command(tid
);
1335 r
->ontimeout
= new C_CancelMonCommand(r
->tid
, this);
1336 timer
.add_event_after(cct
->_conf
->rados_mon_op_timeout
, r
->ontimeout
);
1338 mon_commands
[r
->tid
] = r
;
1342 void MonClient::start_mon_command(const string
&mon_name
,
1343 const std::vector
<string
>& cmd
,
1344 const ceph::buffer::list
& inbl
,
1345 ceph::buffer::list
*outbl
, string
*outs
,
1348 ldout(cct
,10) << __func__
<< " mon." << mon_name
<< " cmd=" << cmd
<< dendl
;
1349 std::lock_guard
l(monc_lock
);
1350 if (!initialized
|| stopping
) {
1352 onfinish
->complete(-ECANCELED
);
1356 MonCommand
*r
= new MonCommand(++last_mon_command_tid
);
1358 // detect/tolerate mon *rank* passed as a string
1360 int rank
= strict_strtoll(mon_name
.c_str(), 10, &err
);
1361 if (err
.size() == 0 && rank
>= 0) {
1362 ldout(cct
,10) << __func__
<< " interpreting name '" << mon_name
1363 << "' as rank " << rank
<< dendl
;
1364 r
->target_rank
= rank
;
1366 r
->target_name
= mon_name
;
1372 r
->onfinish
= onfinish
;
1373 mon_commands
[r
->tid
] = r
;
1377 void MonClient::start_mon_command(int rank
,
1378 const std::vector
<string
>& cmd
,
1379 const ceph::buffer::list
& inbl
,
1380 ceph::buffer::list
*outbl
, string
*outs
,
1383 ldout(cct
,10) << __func__
<< " rank " << rank
<< " cmd=" << cmd
<< dendl
;
1384 std::lock_guard
l(monc_lock
);
1385 if (!initialized
|| stopping
) {
1387 onfinish
->complete(-ECANCELED
);
1391 MonCommand
*r
= new MonCommand(++last_mon_command_tid
);
1392 r
->target_rank
= rank
;
1397 r
->onfinish
= onfinish
;
1398 mon_commands
[r
->tid
] = r
;
1404 void MonClient::get_version(string map
, version_t
*newest
, version_t
*oldest
, Context
*onfinish
)
1406 version_req_d
*req
= new version_req_d(onfinish
, newest
, oldest
);
1407 ldout(cct
, 10) << "get_version " << map
<< " req " << req
<< dendl
;
1408 std::lock_guard
l(monc_lock
);
1409 auto m
= ceph::make_message
<MMonGetVersion
>();
1411 m
->handle
= ++version_req_id
;
1412 version_requests
[m
->handle
] = req
;
1413 _send_mon_message(std::move(m
));
1416 void MonClient::handle_get_version_reply(MMonGetVersionReply
* m
)
1418 ceph_assert(ceph_mutex_is_locked(monc_lock
));
1419 auto iter
= version_requests
.find(m
->handle
);
1420 if (iter
== version_requests
.end()) {
1421 ldout(cct
, 0) << __func__
<< " version request with handle " << m
->handle
1422 << " not found" << dendl
;
1424 version_req_d
*req
= iter
->second
;
1425 ldout(cct
, 10) << __func__
<< " finishing " << req
<< " version " << m
->version
<< dendl
;
1426 version_requests
.erase(iter
);
1428 *req
->newest
= m
->version
;
1430 *req
->oldest
= m
->oldest_version
;
1431 finisher
.queue(req
->context
, 0);
1437 int MonClient::get_auth_request(
1439 AuthConnectionMeta
*auth_meta
,
1440 uint32_t *auth_method
,
1441 std::vector
<uint32_t> *preferred_modes
,
1442 ceph::buffer::list
*bl
)
1444 std::lock_guard
l(monc_lock
);
1445 ldout(cct
,10) << __func__
<< " con " << con
<< " auth_method " << *auth_method
1448 // connection to mon?
1449 if (con
->get_peer_type() == CEPH_ENTITY_TYPE_MON
) {
1450 ceph_assert(!auth_meta
->authorizer
);
1451 if (con
->is_anon()) {
1452 for (auto& i
: mon_commands
) {
1453 if (i
.second
->target_con
== con
) {
1454 return i
.second
->target_session
->get_auth_request(
1455 auth_method
, preferred_modes
, bl
,
1456 entity_name
, want_keys
, rotating_secrets
.get());
1460 for (auto& i
: pending_cons
) {
1461 if (i
.second
.is_con(con
)) {
1462 return i
.second
.get_auth_request(
1463 auth_method
, preferred_modes
, bl
,
1464 entity_name
, want_keys
, rotating_secrets
.get());
1470 // generate authorizer
1472 lderr(cct
) << __func__
<< " but no auth handler is set up" << dendl
;
1475 auth_meta
->authorizer
.reset(auth
->build_authorizer(con
->get_peer_type()));
1476 if (!auth_meta
->authorizer
) {
1477 lderr(cct
) << __func__
<< " failed to build_authorizer for type "
1478 << ceph_entity_type_name(con
->get_peer_type()) << dendl
;
1481 auth_meta
->auth_method
= auth_meta
->authorizer
->protocol
;
1482 auth_registry
.get_supported_modes(con
->get_peer_type(),
1483 auth_meta
->auth_method
,
1485 *bl
= auth_meta
->authorizer
->bl
;
1489 int MonClient::handle_auth_reply_more(
1491 AuthConnectionMeta
*auth_meta
,
1492 const ceph::buffer::list
& bl
,
1493 ceph::buffer::list
*reply
)
1495 std::lock_guard
l(monc_lock
);
1497 if (con
->get_peer_type() == CEPH_ENTITY_TYPE_MON
) {
1498 if (con
->is_anon()) {
1499 for (auto& i
: mon_commands
) {
1500 if (i
.second
->target_con
== con
) {
1501 return i
.second
->target_session
->handle_auth_reply_more(
1502 auth_meta
, bl
, reply
);
1506 for (auto& i
: pending_cons
) {
1507 if (i
.second
.is_con(con
)) {
1508 return i
.second
.handle_auth_reply_more(auth_meta
, bl
, reply
);
1514 // authorizer challenges
1515 if (!auth
|| !auth_meta
->authorizer
) {
1516 lderr(cct
) << __func__
<< " no authorizer?" << dendl
;
1519 auth_meta
->authorizer
->add_challenge(cct
, bl
);
1520 *reply
= auth_meta
->authorizer
->bl
;
1524 int MonClient::handle_auth_done(
1526 AuthConnectionMeta
*auth_meta
,
1529 const ceph::buffer::list
& bl
,
1530 CryptoKey
*session_key
,
1531 std::string
*connection_secret
)
1533 if (con
->get_peer_type() == CEPH_ENTITY_TYPE_MON
) {
1534 std::lock_guard
l(monc_lock
);
1535 if (con
->is_anon()) {
1536 for (auto& i
: mon_commands
) {
1537 if (i
.second
->target_con
== con
) {
1538 return i
.second
->target_session
->handle_auth_done(
1539 auth_meta
, global_id
, bl
,
1540 session_key
, connection_secret
);
1544 for (auto& i
: pending_cons
) {
1545 if (i
.second
.is_con(con
)) {
1546 int r
= i
.second
.handle_auth_done(
1547 auth_meta
, global_id
, bl
,
1548 session_key
, connection_secret
);
1550 pending_cons
.erase(i
.first
);
1551 if (!pending_cons
.empty()) {
1555 active_con
.reset(new MonConnection(std::move(i
.second
)));
1556 pending_cons
.clear();
1557 ceph_assert(active_con
->have_session());
1561 if (r
|| monmap
.get_epoch() > 0) {
1569 // verify authorizer reply
1570 auto p
= bl
.begin();
1571 if (!auth_meta
->authorizer
->verify_reply(p
, &auth_meta
->connection_secret
)) {
1572 ldout(cct
, 0) << __func__
<< " failed verifying authorizer reply"
1576 auth_meta
->session_key
= auth_meta
->authorizer
->session_key
;
1581 int MonClient::handle_auth_bad_method(
1583 AuthConnectionMeta
*auth_meta
,
1584 uint32_t old_auth_method
,
1586 const std::vector
<uint32_t>& allowed_methods
,
1587 const std::vector
<uint32_t>& allowed_modes
)
1589 auth_meta
->allowed_methods
= allowed_methods
;
1591 std::lock_guard
l(monc_lock
);
1592 if (con
->get_peer_type() == CEPH_ENTITY_TYPE_MON
) {
1593 if (con
->is_anon()) {
1594 for (auto& i
: mon_commands
) {
1595 if (i
.second
->target_con
== con
) {
1596 int r
= i
.second
->target_session
->handle_auth_bad_method(
1602 _finish_command(i
.second
, r
, "auth failed");
1608 for (auto& i
: pending_cons
) {
1609 if (i
.second
.is_con(con
)) {
1610 int r
= i
.second
.handle_auth_bad_method(old_auth_method
,
1615 return r
; // try another method on this con
1617 pending_cons
.erase(i
.first
);
1618 if (!pending_cons
.empty()) {
1619 return r
; // fail this con, maybe another con will succeed
1630 ldout(cct
,10) << __func__
<< " hmm, they didn't like " << old_auth_method
1631 << " result " << cpp_strerror(result
)
1632 << " and auth is " << (auth
? auth
->get_protocol() : 0)
1638 int MonClient::handle_auth_request(
1640 AuthConnectionMeta
*auth_meta
,
1642 uint32_t auth_method
,
1643 const ceph::buffer::list
& payload
,
1644 ceph::buffer::list
*reply
)
1646 if (payload
.length() == 0) {
1647 // for some channels prior to nautilus (osd heartbeat), we
1648 // tolerate the lack of an authorizer.
1649 if (!con
->get_messenger()->require_authorizer
) {
1650 handle_authentication_dispatcher
->ms_handle_authentication(con
);
1655 auth_meta
->auth_mode
= payload
[0];
1656 if (auth_meta
->auth_mode
< AUTH_MODE_AUTHORIZER
||
1657 auth_meta
->auth_mode
> AUTH_MODE_AUTHORIZER_MAX
) {
1660 AuthAuthorizeHandler
*ah
= get_auth_authorize_handler(con
->get_peer_type(),
1663 lderr(cct
) << __func__
<< " no AuthAuthorizeHandler found for auth method "
1664 << auth_method
<< dendl
;
1668 auto ac
= &auth_meta
->authorizer_challenge
;
1669 if (auth_meta
->skip_authorizer_challenge
) {
1670 ldout(cct
, 10) << __func__
<< " skipping challenge on " << con
<< dendl
;
1674 bool was_challenge
= (bool)auth_meta
->authorizer_challenge
;
1675 bool isvalid
= ah
->verify_authorizer(
1679 auth_meta
->get_connection_secret_length(),
1682 &con
->peer_global_id
,
1683 &con
->peer_caps_info
,
1684 &auth_meta
->session_key
,
1685 &auth_meta
->connection_secret
,
1688 handle_authentication_dispatcher
->ms_handle_authentication(con
);
1691 if (!more
&& !was_challenge
&& auth_meta
->authorizer_challenge
) {
1692 ldout(cct
,10) << __func__
<< " added challenge on " << con
<< dendl
;
1695 ldout(cct
,10) << __func__
<< " bad authorizer on " << con
<< dendl
;
1696 // discard old challenge
1697 auth_meta
->authorizer_challenge
.reset();
1701 AuthAuthorizer
* MonClient::build_authorizer(int service_id
) const {
1702 std::lock_guard
l(monc_lock
);
1704 return auth
->build_authorizer(service_id
);
1706 ldout(cct
, 0) << __func__
<< " for " << ceph_entity_type_name(service_id
)
1707 << ", but no auth is available now" << dendl
;
1712 #define dout_subsys ceph_subsys_monc
1714 #define dout_prefix *_dout << "monclient" << (have_session() ? ": " : "(hunting): ")
1716 MonConnection::MonConnection(
1717 CephContext
*cct
, ConnectionRef con
, uint64_t global_id
,
1719 : cct(cct
), con(con
), global_id(global_id
), auth_registry(ar
)
1722 MonConnection::~MonConnection()
1730 bool MonConnection::have_session() const
1732 return state
== State::HAVE_SESSION
;
1735 void MonConnection::start(epoch_t epoch
,
1736 const EntityName
& entity_name
)
1739 auth_start
= ceph_clock_now();
1741 if (con
->get_peer_addr().is_msgr2()) {
1742 ldout(cct
, 10) << __func__
<< " opening mon connection" << dendl
;
1743 state
= State::AUTHENTICATING
;
1744 con
->send_message(new MMonGetMap());
1748 // restart authentication handshake
1749 state
= State::NEGOTIATING
;
1751 // send an initial keepalive to ensure our timestamp is valid by the
1752 // time we are in an OPENED state (by sequencing this before
1754 con
->send_keepalive();
1757 m
->protocol
= CEPH_AUTH_UNKNOWN
;
1758 m
->monmap_epoch
= epoch
;
1760 encode(struct_v
, m
->auth_payload
);
1761 std::vector
<uint32_t> auth_supported
;
1762 auth_registry
->get_supported_methods(con
->get_peer_type(), &auth_supported
);
1763 encode(auth_supported
, m
->auth_payload
);
1764 encode(entity_name
, m
->auth_payload
);
1765 encode(global_id
, m
->auth_payload
);
1766 con
->send_message(m
);
1769 int MonConnection::get_auth_request(
1771 std::vector
<uint32_t> *preferred_modes
,
1772 ceph::buffer::list
*bl
,
1773 const EntityName
& entity_name
,
1775 RotatingKeyRing
* keyring
)
1779 if (auth_method
< 0) {
1780 std::vector
<uint32_t> as
;
1781 auth_registry
->get_supported_methods(con
->get_peer_type(), &as
);
1785 auth_method
= as
.front();
1787 *method
= auth_method
;
1788 auth_registry
->get_supported_modes(con
->get_peer_type(), auth_method
,
1790 ldout(cct
,10) << __func__
<< " method " << *method
1791 << " preferred_modes " << *preferred_modes
<< dendl
;
1792 if (preferred_modes
->empty()) {
1799 int r
= _init_auth(*method
, entity_name
, want_keys
, keyring
, true);
1800 ceph_assert(r
== 0);
1802 // initial requset includes some boilerplate...
1803 encode((char)AUTH_MODE_MON
, *bl
);
1804 encode(entity_name
, *bl
);
1805 encode(global_id
, *bl
);
1807 // and (maybe) some method-specific initial payload
1808 auth
->build_initial_request(bl
);
1813 int MonConnection::handle_auth_reply_more(
1814 AuthConnectionMeta
*auth_meta
,
1815 const ceph::buffer::list
& bl
,
1816 ceph::buffer::list
*reply
)
1818 ldout(cct
, 10) << __func__
<< " payload " << bl
.length() << dendl
;
1819 ldout(cct
, 30) << __func__
<< " got\n";
1823 auto p
= bl
.cbegin();
1824 ldout(cct
, 10) << __func__
<< " payload_len " << bl
.length() << dendl
;
1825 int r
= auth
->handle_response(0, p
, &auth_meta
->session_key
,
1826 &auth_meta
->connection_secret
);
1828 auth
->prepare_build_request();
1829 auth
->build_request(*reply
);
1830 ldout(cct
, 10) << __func__
<< " responding with " << reply
->length()
1831 << " bytes" << dendl
;
1834 lderr(cct
) << __func__
<< " handle_response returned " << r
<< dendl
;
1836 ldout(cct
, 10) << __func__
<< " authenticated!" << dendl
;
1838 ceph_abort(cct
, "write me");
1843 int MonConnection::handle_auth_done(
1844 AuthConnectionMeta
*auth_meta
,
1845 uint64_t new_global_id
,
1846 const ceph::buffer::list
& bl
,
1847 CryptoKey
*session_key
,
1848 std::string
*connection_secret
)
1850 ldout(cct
,10) << __func__
<< " global_id " << new_global_id
1851 << " payload " << bl
.length()
1853 global_id
= new_global_id
;
1854 auth
->set_global_id(global_id
);
1855 auto p
= bl
.begin();
1856 int auth_err
= auth
->handle_response(0, p
, &auth_meta
->session_key
,
1857 &auth_meta
->connection_secret
);
1858 if (auth_err
>= 0) {
1859 state
= State::HAVE_SESSION
;
1861 con
->set_last_keepalive_ack(auth_start
);
1863 if (pending_tell_command
) {
1864 con
->send_message2(std::move(pending_tell_command
));
1869 int MonConnection::handle_auth_bad_method(
1870 uint32_t old_auth_method
,
1872 const std::vector
<uint32_t>& allowed_methods
,
1873 const std::vector
<uint32_t>& allowed_modes
)
1875 ldout(cct
,10) << __func__
<< " old_auth_method " << old_auth_method
1876 << " result " << cpp_strerror(result
)
1877 << " allowed_methods " << allowed_methods
<< dendl
;
1878 std::vector
<uint32_t> auth_supported
;
1879 auth_registry
->get_supported_methods(con
->get_peer_type(), &auth_supported
);
1880 auto p
= std::find(auth_supported
.begin(), auth_supported
.end(),
1882 assert(p
!= auth_supported
.end());
1883 p
= std::find_first_of(std::next(p
), auth_supported
.end(),
1884 allowed_methods
.begin(), allowed_methods
.end());
1885 if (p
== auth_supported
.end()) {
1886 lderr(cct
) << __func__
<< " server allowed_methods " << allowed_methods
1887 << " but i only support " << auth_supported
<< dendl
;
1891 ldout(cct
,10) << __func__
<< " will try " << auth_method
<< " next" << dendl
;
1895 int MonConnection::handle_auth(MAuthReply
* m
,
1896 const EntityName
& entity_name
,
1898 RotatingKeyRing
* keyring
)
1900 if (state
== State::NEGOTIATING
) {
1901 int r
= _negotiate(m
, entity_name
, want_keys
, keyring
);
1905 state
= State::AUTHENTICATING
;
1907 int r
= authenticate(m
);
1909 state
= State::HAVE_SESSION
;
1914 int MonConnection::_negotiate(MAuthReply
*m
,
1915 const EntityName
& entity_name
,
1917 RotatingKeyRing
* keyring
)
1919 if (auth
&& (int)m
->protocol
== auth
->get_protocol()) {
1920 // good, negotiation completed
1925 int r
= _init_auth(m
->protocol
, entity_name
, want_keys
, keyring
, false);
1926 if (r
== -ENOTSUP
) {
1927 if (m
->result
== -ENOTSUP
) {
1928 ldout(cct
, 10) << "none of our auth protocols are supported by the server"
1936 int MonConnection::_init_auth(
1938 const EntityName
& entity_name
,
1940 RotatingKeyRing
* keyring
,
1943 ldout(cct
,10) << __func__
<< " method " << method
<< dendl
;
1945 AuthClientHandler::create(cct
, method
, keyring
));
1947 ldout(cct
, 10) << " no handler for protocol " << method
<< dendl
;
1951 // do not request MGR key unless the mon has the SERVER_KRAKEN
1952 // feature. otherwise it will give us an auth error. note that
1953 // we have to use the FEATUREMASK because pre-jewel the kraken
1954 // feature bit was used for something else.
1956 (want_keys
& CEPH_ENTITY_TYPE_MGR
) &&
1957 !(con
->has_features(CEPH_FEATUREMASK_SERVER_KRAKEN
))) {
1958 ldout(cct
, 1) << __func__
1959 << " not requesting MGR keys from pre-kraken monitor"
1961 want_keys
&= ~CEPH_ENTITY_TYPE_MGR
;
1963 auth
->set_want_keys(want_keys
);
1964 auth
->init(entity_name
);
1965 auth
->set_global_id(global_id
);
1969 int MonConnection::authenticate(MAuthReply
*m
)
1972 if (!m
->global_id
) {
1973 ldout(cct
, 1) << "peer sent an invalid global_id" << dendl
;
1975 if (m
->global_id
!= global_id
) {
1976 // it's a new session
1978 global_id
= m
->global_id
;
1979 auth
->set_global_id(global_id
);
1980 ldout(cct
, 10) << "my global_id is " << m
->global_id
<< dendl
;
1982 auto p
= m
->result_bl
.cbegin();
1983 int ret
= auth
->handle_response(m
->result
, p
, nullptr, nullptr);
1984 if (ret
== -EAGAIN
) {
1985 auto ma
= new MAuth
;
1986 ma
->protocol
= auth
->get_protocol();
1987 auth
->prepare_build_request();
1988 auth
->build_request(ma
->auth_payload
);
1989 con
->send_message(ma
);
1991 if (ret
== 0 && pending_tell_command
) {
1992 con
->send_message2(std::move(pending_tell_command
));
1998 void MonClient::register_config_callback(md_config_t::config_callback fn
) {
1999 ceph_assert(!config_cb
);
2003 md_config_t::config_callback
MonClient::get_config_callback() {