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/MAuth.h"
35 #include "messages/MLogAck.h"
36 #include "messages/MAuthReply.h"
37 #include "messages/MMonCommand.h"
38 #include "messages/MMonCommandAck.h"
39 #include "messages/MCommand.h"
40 #include "messages/MCommandReply.h"
41 #include "messages/MPing.h"
43 #include "messages/MMonSubscribe.h"
44 #include "messages/MMonSubscribeAck.h"
45 #include "common/errno.h"
46 #include "common/hostname.h"
47 #include "common/LogClient.h"
49 #include "MonClient.h"
50 #include "error_code.h"
53 #include "auth/Auth.h"
54 #include "auth/KeyRing.h"
55 #include "auth/AuthClientHandler.h"
56 #include "auth/AuthRegistry.h"
57 #include "auth/RotatingKeyRing.h"
59 #define dout_subsys ceph_subsys_monc
61 #define dout_prefix *_dout << "monclient" << (_hunting() ? "(hunting)":"") << ": "
63 namespace bs
= boost::system
;
65 using namespace std::literals
;
67 MonClient::MonClient(CephContext
*cct_
, boost::asio::io_context
& service
) :
71 timer(cct_
, monc_lock
),
75 more_log_pending(false),
77 had_a_connection(false),
78 reopen_interval_multiplier(
79 cct_
->_conf
.get_val
<double>("mon_client_hunt_interval_min_multiple")),
80 last_mon_command_tid(0),
84 MonClient::~MonClient()
88 int MonClient::build_initial_monmap()
90 ldout(cct
, 10) << __func__
<< dendl
;
91 int r
= monmap
.build_initial(cct
, false, std::cerr
);
92 ldout(cct
,10) << "monmap:\n";
98 int MonClient::get_monmap()
100 ldout(cct
, 10) << __func__
<< dendl
;
101 std::unique_lock
l(monc_lock
);
103 sub
.want("monmap", 0, 0);
106 map_cond
.wait(l
, [this] { return !want_monmap
; });
107 ldout(cct
, 10) << __func__
<< " done" << dendl
;
111 int MonClient::get_monmap_and_config()
113 ldout(cct
, 10) << __func__
<< dendl
;
114 ceph_assert(!messenger
);
119 auto shutdown_crypto
= make_scope_guard([this] {
120 cct
->shutdown_crypto();
123 int r
= build_initial_monmap();
125 lderr(cct
) << __func__
<< " cannot identify monitors to contact" << dendl
;
129 messenger
= Messenger::create_client_messenger(
130 cct
, "temp_mon_client");
131 ceph_assert(messenger
);
132 messenger
->add_dispatcher_head(this);
134 auto shutdown_msgr
= make_scope_guard([this] {
135 messenger
->shutdown();
139 if (!monmap
.fsid
.is_zero()) {
140 cct
->_conf
.set_val("fsid", stringify(monmap
.fsid
));
144 want_bootstrap_config
= true;
145 auto shutdown_config
= make_scope_guard([this] {
146 std::unique_lock
l(monc_lock
);
147 want_bootstrap_config
= false;
148 bootstrap_config
.reset();
151 ceph::ref_t
<MConfig
> config
;
152 while (tries
-- > 0) {
157 r
= authenticate(std::chrono::duration
<double>(cct
->_conf
.get_val
<std::chrono::seconds
>("client_mount_timeout")).count());
158 if (r
== -ETIMEDOUT
) {
166 std::unique_lock
l(monc_lock
);
167 if (monmap
.get_epoch() &&
168 !monmap
.persistent_features
.contains_all(
169 ceph::features::mon::FEATURE_MIMIC
)) {
170 ldout(cct
,10) << __func__
<< " pre-mimic monitor, no config to fetch"
175 while ((!bootstrap_config
|| monmap
.get_epoch() == 0) && r
== 0) {
176 ldout(cct
,20) << __func__
<< " waiting for monmap|config" << dendl
;
177 auto status
= map_cond
.wait_for(l
, ceph::make_timespan(
178 cct
->_conf
->mon_client_hunt_interval
));
179 if (status
== std::cv_status::timeout
) {
184 if (bootstrap_config
) {
185 ldout(cct
,10) << __func__
<< " success" << dendl
;
186 config
= std::move(bootstrap_config
);
191 lderr(cct
) << __func__
<< " failed to get config" << dendl
;
197 // apply the bootstrap config to ensure its applied prior to completing
199 cct
->_conf
.set_mon_vals(cct
, config
->config
, config_cb
);
208 * Ping the monitor with id @p mon_id and set the resulting reply in
209 * the provided @p result_reply, if this last parameter is not NULL.
211 * So that we don't rely on the MonClient's default messenger, set up
212 * during connect(), we create our own messenger to comunicate with the
213 * specified monitor. This is advantageous in the following ways:
215 * - Isolate the ping procedure from the rest of the MonClient's operations,
216 * allowing us to not acquire or manage the big monc_lock, thus not
217 * having to block waiting for some other operation to finish before we
219 * * for instance, we can ping mon.FOO even if we are currently hunting
220 * or blocked waiting for auth to complete with mon.BAR.
222 * - Ping a monitor prior to establishing a connection (using connect())
223 * and properly establish the MonClient's messenger. This frees us
224 * from dealing with the complex foo that happens in connect().
226 * We also don't rely on MonClient as a dispatcher for this messenger,
227 * unlike what happens with the MonClient's default messenger. This allows
228 * us to sandbox the whole ping, having it much as a separate entity in
229 * the MonClient class, considerably simplifying the handling and dispatching
230 * of messages without needing to consider monc_lock.
232 * Current drawback is that we will establish a messenger for each ping
233 * we want to issue, instead of keeping a single messenger instance that
234 * would be used for all pings.
236 int MonClient::ping_monitor(const string
&mon_id
, string
*result_reply
)
238 ldout(cct
, 10) << __func__
<< dendl
;
241 if (monmap
.contains("noname-"+mon_id
)) {
242 new_mon_id
= "noname-"+mon_id
;
247 if (new_mon_id
.empty()) {
248 ldout(cct
, 10) << __func__
<< " specified mon id is empty!" << dendl
;
250 } else if (!monmap
.contains(new_mon_id
)) {
251 ldout(cct
, 10) << __func__
<< " no such monitor 'mon." << new_mon_id
<< "'"
256 // N.B. monc isn't initialized
258 auth_registry
.refresh_config();
261 keyring
.from_ceph_context(cct
);
262 RotatingKeyRing
rkeyring(cct
, cct
->get_module_type(), &keyring
);
264 MonClientPinger
*pinger
= new MonClientPinger(cct
,
268 Messenger
*smsgr
= Messenger::create_client_messenger(cct
, "temp_ping_client");
269 smsgr
->add_dispatcher_head(pinger
);
270 smsgr
->set_auth_client(pinger
);
273 ConnectionRef con
= smsgr
->connect_to_mon(monmap
.get_addrs(new_mon_id
));
274 ldout(cct
, 10) << __func__
<< " ping mon." << new_mon_id
275 << " " << con
->get_peer_addr() << dendl
;
277 pinger
->mc
.reset(new MonConnection(cct
, con
, 0, &auth_registry
));
278 pinger
->mc
->start(monmap
.get_epoch(), entity_name
);
279 con
->send_message(new MPing
);
281 int ret
= pinger
->wait_for_reply(cct
->_conf
->mon_client_ping_timeout
);
283 ldout(cct
,10) << __func__
<< " got ping reply" << dendl
;
297 bool MonClient::ms_dispatch(Message
*m
)
299 // we only care about these message types
300 switch (m
->get_type()) {
301 case CEPH_MSG_MON_MAP
:
302 case CEPH_MSG_AUTH_REPLY
:
303 case CEPH_MSG_MON_SUBSCRIBE_ACK
:
304 case CEPH_MSG_MON_GET_VERSION_REPLY
:
305 case MSG_MON_COMMAND_ACK
:
306 case MSG_COMMAND_REPLY
:
317 std::lock_guard
lock(monc_lock
);
319 if (!m
->get_connection()->is_anon() &&
320 m
->get_source().type() == CEPH_ENTITY_TYPE_MON
) {
322 auto p
= _find_pending_con(m
->get_connection());
323 if (p
== pending_cons
.end()) {
324 // ignore any messages outside hunting sessions
325 ldout(cct
, 10) << "discarding stray monitor message " << *m
<< dendl
;
329 } else if (!active_con
|| active_con
->get_con() != m
->get_connection()) {
330 // ignore any messages outside our session(s)
331 ldout(cct
, 10) << "discarding stray monitor message " << *m
<< dendl
;
337 switch (m
->get_type()) {
338 case CEPH_MSG_MON_MAP
:
339 handle_monmap(static_cast<MMonMap
*>(m
));
340 if (passthrough_monmap
) {
346 case CEPH_MSG_AUTH_REPLY
:
347 handle_auth(static_cast<MAuthReply
*>(m
));
349 case CEPH_MSG_MON_SUBSCRIBE_ACK
:
350 handle_subscribe_ack(static_cast<MMonSubscribeAck
*>(m
));
352 case CEPH_MSG_MON_GET_VERSION_REPLY
:
353 handle_get_version_reply(static_cast<MMonGetVersionReply
*>(m
));
355 case MSG_MON_COMMAND_ACK
:
356 handle_mon_command_ack(static_cast<MMonCommandAck
*>(m
));
358 case MSG_COMMAND_REPLY
:
359 if (m
->get_connection()->is_anon() &&
360 m
->get_source().type() == CEPH_ENTITY_TYPE_MON
) {
361 // this connection is from 'tell'... ignore everything except our command
362 // reply. (we'll get misc other message because we authenticated, but we
364 handle_command_reply(static_cast<MCommandReply
*>(m
));
367 // leave the message for another dispatch handler (e.g., Objecter)
371 log_client
->handle_log_ack(static_cast<MLogAck
*>(m
));
373 if (more_log_pending
) {
381 handle_config(static_cast<MConfig
*>(m
));
387 void MonClient::send_log(bool flush
)
390 auto lm
= log_client
->get_mon_log_message(flush
);
392 _send_mon_message(std::move(lm
));
393 more_log_pending
= log_client
->are_pending();
397 void MonClient::flush_log()
399 std::lock_guard
l(monc_lock
);
403 /* Unlike all the other message-handling functions, we don't put away a reference
404 * because we want to support MMonMap passthrough to other Dispatchers. */
405 void MonClient::handle_monmap(MMonMap
*m
)
407 ldout(cct
, 10) << __func__
<< " " << *m
<< dendl
;
408 auto con_addrs
= m
->get_source_addrs();
409 string old_name
= monmap
.get_name(con_addrs
);
410 const auto old_epoch
= monmap
.get_epoch();
412 auto p
= m
->monmapbl
.cbegin();
415 ldout(cct
, 10) << " got monmap " << monmap
.epoch
416 << " from mon." << old_name
417 << " (according to old e" << monmap
.get_epoch() << ")"
419 ldout(cct
, 10) << "dump:\n";
420 monmap
.print(*_dout
);
423 if (old_epoch
!= monmap
.get_epoch()) {
426 if (old_name
.size() == 0) {
427 ldout(cct
,10) << " can't identify which mon we were connected to" << dendl
;
430 auto new_name
= monmap
.get_name(con_addrs
);
431 if (new_name
.empty()) {
432 ldout(cct
, 10) << "mon." << old_name
<< " at " << con_addrs
433 << " went away" << dendl
;
434 // can't find the mon we were talking to (above)
436 } else if (messenger
->should_use_msgr2() &&
437 monmap
.get_addrs(new_name
).has_msgr2() &&
438 !con_addrs
.has_msgr2()) {
439 ldout(cct
,1) << " mon." << new_name
<< " has (v2) addrs "
440 << monmap
.get_addrs(new_name
) << " but i'm connected to "
441 << con_addrs
<< ", reconnecting" << dendl
;
446 cct
->set_mon_addrs(monmap
);
448 sub
.got("monmap", monmap
.get_epoch());
449 map_cond
.notify_all();
452 if (authenticate_err
== 1) {
457 void MonClient::handle_config(MConfig
*m
)
459 ldout(cct
,10) << __func__
<< " " << *m
<< dendl
;
461 if (want_bootstrap_config
) {
462 // get_monmap_and_config is waiting for config which it will apply
464 bootstrap_config
= ceph::ref_t
<MConfig
>(m
, false);
465 map_cond
.notify_all();
469 // Take the sledgehammer approach to ensuring we don't depend on
470 // anything in MonClient.
471 boost::asio::post(finish_strand
,
472 [m
, cct
= boost::intrusive_ptr
<CephContext
>(cct
),
473 config_notify_cb
= config_notify_cb
,
474 config_cb
= config_cb
]() {
475 cct
->_conf
.set_mon_vals(cct
.get(), m
->config
, config_cb
);
476 if (config_notify_cb
) {
483 // ----------------------
485 int MonClient::init()
487 ldout(cct
, 10) << __func__
<< dendl
;
489 entity_name
= cct
->_conf
->name
;
491 auth_registry
.refresh_config();
493 std::lock_guard
l(monc_lock
);
494 keyring
.reset(new KeyRing
);
495 if (auth_registry
.is_supported_method(messenger
->get_mytype(),
497 // this should succeed, because auth_registry just checked!
498 int r
= keyring
->from_ceph_context(cct
);
500 // but be somewhat graceful in case there was a race condition
501 lderr(cct
) << "keyring not found" << dendl
;
505 if (!auth_registry
.any_supported_methods(messenger
->get_mytype())) {
509 rotating_secrets
.reset(
510 new RotatingKeyRing(cct
, cct
->get_module_type(), keyring
.get()));
514 messenger
->set_auth_client(this);
515 messenger
->add_dispatcher_head(this);
520 cct
->get_admin_socket()->register_command(
523 "rotate live authentication key");
528 void MonClient::shutdown()
530 ldout(cct
, 10) << __func__
<< dendl
;
532 cct
->get_admin_socket()->unregister_commands(this);
536 while (!version_requests
.empty()) {
537 ceph::async::post(std::move(version_requests
.begin()->second
),
538 monc_errc::shutting_down
, 0, 0);
539 ldout(cct
, 20) << __func__
<< " canceling and discarding version request "
540 << version_requests
.begin()->first
<< dendl
;
541 version_requests
.erase(version_requests
.begin());
543 while (!mon_commands
.empty()) {
544 auto tid
= mon_commands
.begin()->first
;
545 _cancel_mon_command(tid
);
547 ldout(cct
, 20) << __func__
<< " discarding " << waiting_for_session
.size()
548 << " pending message(s)" << dendl
;
549 waiting_for_session
.clear();
552 pending_cons
.clear();
556 authenticate_err
= 0;
557 authenticated
= false;
570 int MonClient::authenticate(double timeout
)
572 std::unique_lock lock
{monc_lock
};
575 ldout(cct
, 5) << "already authenticated" << dendl
;
578 sub
.want("monmap", monmap
.get_epoch() ? monmap
.get_epoch() + 1 : 0, 0);
579 sub
.want("config", 0, 0);
583 auto until
= ceph::mono_clock::now();
584 until
+= ceph::make_timespan(timeout
);
586 ldout(cct
, 10) << "authenticate will time out at " << until
<< dendl
;
587 while (!active_con
&& authenticate_err
>= 0) {
589 auto r
= auth_cond
.wait_until(lock
, until
);
590 if (r
== std::cv_status::timeout
&& !active_con
) {
591 ldout(cct
, 0) << "authenticate timed out after " << timeout
<< dendl
;
592 authenticate_err
= -ETIMEDOUT
;
595 auth_cond
.wait(lock
);
600 ldout(cct
, 5) << __func__
<< " success, global_id "
601 << active_con
->get_global_id() << dendl
;
602 // active_con should not have been set if there was an error
603 ceph_assert(authenticate_err
>= 0);
604 authenticated
= true;
607 if (authenticate_err
< 0 && auth_registry
.no_keyring_disabled_cephx()) {
608 lderr(cct
) << __func__
<< " NOTE: no keyring found; disabled cephx authentication" << dendl
;
611 return authenticate_err
;
615 std::string_view command
,
616 const cmdmap_t
& cmdmap
,
617 const ceph::buffer::list
&inbl
,
620 ceph::buffer::list
& out
)
622 if (command
== "rotate-key") {
625 key
.decode_base64(inbl
.to_str());
626 } catch (buffer::error
& e
) {
627 errss
<< "error decoding key: " << e
.what();
631 ldout(cct
, 1) << "rotate live key for " << entity_name
<< dendl
;
632 keyring
->add(entity_name
, key
);
634 errss
<< "cephx not enabled; no key to rotate";
641 void MonClient::handle_auth(MAuthReply
*m
)
643 ceph_assert(ceph_mutex_is_locked(monc_lock
));
645 if (m
->get_connection()->is_anon()) {
646 // anon connection, used for mon tell commands
647 for (auto& p
: mon_commands
) {
648 if (p
.second
->target_con
== m
->get_connection()) {
649 auto& mc
= p
.second
->target_session
;
650 int ret
= mc
->handle_auth(m
, entity_name
,
651 CEPH_ENTITY_TYPE_MON
,
652 rotating_secrets
.get());
653 (void)ret
; // we don't care
662 std::swap(active_con
->get_auth(), auth
);
663 int ret
= active_con
->authenticate(m
);
665 std::swap(auth
, active_con
->get_auth());
666 if (global_id
!= active_con
->get_global_id()) {
667 lderr(cct
) << __func__
<< " peer assigned me a different global_id: "
668 << active_con
->get_global_id() << dendl
;
670 if (ret
!= -EAGAIN
) {
677 auto found
= _find_pending_con(m
->get_connection());
678 ceph_assert(found
!= pending_cons
.end());
679 int auth_err
= found
->second
.handle_auth(m
, entity_name
, want_keys
,
680 rotating_secrets
.get());
682 if (auth_err
== -EAGAIN
) {
686 pending_cons
.erase(found
);
687 if (!pending_cons
.empty()) {
688 // keep trying with pending connections
691 // the last try just failed, give up.
693 auto& mc
= found
->second
;
694 ceph_assert(mc
.have_session());
695 active_con
.reset(new MonConnection(std::move(mc
)));
696 pending_cons
.clear();
699 _finish_hunting(auth_err
);
700 _finish_auth(auth_err
);
703 void MonClient::_finish_auth(int auth_err
)
705 ldout(cct
,10) << __func__
<< " " << auth_err
<< dendl
;
706 authenticate_err
= auth_err
;
707 // _resend_mon_commands() could _reopen_session() if the connected mon is not
708 // the one the MonCommand is targeting.
709 if (!auth_err
&& active_con
) {
711 _check_auth_tickets();
713 auth_cond
.notify_all();
718 void MonClient::send_mon_message(MessageRef m
)
720 std::lock_guard l
{monc_lock
};
721 _send_mon_message(std::move(m
));
724 void MonClient::_send_mon_message(MessageRef m
)
726 ceph_assert(ceph_mutex_is_locked(monc_lock
));
728 auto cur_con
= active_con
->get_con();
729 ldout(cct
, 10) << "_send_mon_message to mon."
730 << monmap
.get_name(cur_con
->get_peer_addr())
731 << " at " << cur_con
->get_peer_addr() << dendl
;
732 cur_con
->send_message2(std::move(m
));
734 waiting_for_session
.push_back(std::move(m
));
738 void MonClient::_reopen_session(int rank
)
740 ceph_assert(ceph_mutex_is_locked(monc_lock
));
741 ldout(cct
, 10) << __func__
<< " rank " << rank
<< dendl
;
744 pending_cons
.clear();
746 authenticate_err
= 1; // == in progress
756 // throw out old queued messages
757 waiting_for_session
.clear();
759 // throw out version check requests
760 while (!version_requests
.empty()) {
761 ceph::async::post(std::move(version_requests
.begin()->second
),
762 monc_errc::session_reset
, 0, 0);
763 version_requests
.erase(version_requests
.begin());
766 for (auto& c
: pending_cons
) {
767 c
.second
.start(monmap
.get_epoch(), entity_name
);
775 void MonClient::_add_conn(unsigned rank
)
777 auto peer
= monmap
.get_addrs(rank
);
778 auto conn
= messenger
->connect_to_mon(peer
);
779 MonConnection
mc(cct
, conn
, global_id
, &auth_registry
);
781 mc
.get_auth().reset(auth
->clone());
783 pending_cons
.insert(std::make_pair(peer
, std::move(mc
)));
784 ldout(cct
, 10) << "picked mon." << monmap
.get_name(rank
)
790 void MonClient::_add_conns()
792 // collect the next batch of candidates who are listed right next to the ones
794 auto get_next_batch
= [this]() -> std::vector
<unsigned> {
795 std::multimap
<uint16_t, unsigned> ranks_by_priority
;
797 monmap
.mon_info
| boost::adaptors::filtered(
799 auto rank
= monmap
.get_rank(info
.first
);
800 return tried
.count(rank
) == 0;
801 }) | boost::adaptors::transformed(
803 auto rank
= monmap
.get_rank(info
.first
);
804 return std::make_pair(info
.second
.priority
, rank
);
805 }), std::inserter(ranks_by_priority
, end(ranks_by_priority
)));
806 if (ranks_by_priority
.empty()) {
809 // only choose the monitors with lowest priority
810 auto cands
= boost::make_iterator_range(
811 ranks_by_priority
.equal_range(ranks_by_priority
.begin()->first
));
812 std::vector
<unsigned> ranks
;
813 boost::range::copy(cands
| boost::adaptors::map_values
,
814 std::back_inserter(ranks
));
817 auto ranks
= get_next_batch();
819 tried
.clear(); // start over
820 ranks
= get_next_batch();
822 ceph_assert(!ranks
.empty());
823 if (ranks
.size() > 1) {
824 std::vector
<uint16_t> weights
;
825 for (auto i
: ranks
) {
826 auto rank_name
= monmap
.get_name(i
);
827 weights
.push_back(monmap
.get_weight(rank_name
));
830 if (std::accumulate(begin(weights
), end(weights
), 0u) == 0) {
831 std::shuffle(begin(ranks
), end(ranks
), std::mt19937
{rd()});
833 weighted_shuffle(begin(ranks
), end(ranks
), begin(weights
), end(weights
),
837 ldout(cct
, 10) << __func__
<< " ranks=" << ranks
<< dendl
;
838 unsigned n
= cct
->_conf
->mon_client_hunt_parallel
;
839 if (n
== 0 || n
> ranks
.size()) {
842 for (unsigned i
= 0; i
< n
; i
++) {
844 tried
.insert(ranks
[i
]);
848 bool MonClient::ms_handle_reset(Connection
*con
)
850 std::lock_guard
lock(monc_lock
);
852 if (con
->get_peer_type() != CEPH_ENTITY_TYPE_MON
)
855 if (con
->is_anon()) {
856 auto p
= mon_commands
.begin();
857 while (p
!= mon_commands
.end()) {
858 auto cmd
= p
->second
;
860 if (cmd
->target_con
== con
) {
861 _send_command(cmd
); // may retry or fail
869 if (pending_cons
.count(con
->get_peer_addrs())) {
870 ldout(cct
, 10) << __func__
<< " hunted mon " << con
->get_peer_addrs()
873 ldout(cct
, 10) << __func__
<< " stray mon " << con
->get_peer_addrs()
878 if (active_con
&& con
== active_con
->get_con()) {
879 ldout(cct
, 10) << __func__
<< " current mon " << con
->get_peer_addrs()
884 ldout(cct
, 10) << "ms_handle_reset stray mon " << con
->get_peer_addrs()
891 bool MonClient::_opened() const
893 ceph_assert(ceph_mutex_is_locked(monc_lock
));
894 return active_con
|| _hunting();
897 bool MonClient::_hunting() const
899 return !pending_cons
.empty();
902 void MonClient::_start_hunting()
904 ceph_assert(!_hunting());
905 // adjust timeouts if necessary
906 if (!had_a_connection
)
908 reopen_interval_multiplier
*= cct
->_conf
->mon_client_hunt_interval_backoff
;
909 if (reopen_interval_multiplier
>
910 cct
->_conf
->mon_client_hunt_interval_max_multiple
) {
911 reopen_interval_multiplier
=
912 cct
->_conf
->mon_client_hunt_interval_max_multiple
;
916 void MonClient::_finish_hunting(int auth_err
)
918 ldout(cct
,10) << __func__
<< " " << auth_err
<< dendl
;
919 ceph_assert(ceph_mutex_is_locked(monc_lock
));
920 // the pending conns have been cleaned.
921 ceph_assert(!_hunting());
923 auto con
= active_con
->get_con();
924 ldout(cct
, 1) << "found mon."
925 << monmap
.get_name(con
->get_peer_addr())
928 ldout(cct
, 1) << "no mon sessions established" << dendl
;
931 had_a_connection
= true;
935 last_rotating_renew_sent
= utime_t();
936 while (!waiting_for_session
.empty()) {
937 _send_mon_message(std::move(waiting_for_session
.front()));
938 waiting_for_session
.pop_front();
940 _resend_mon_commands();
943 auth
= std::move(active_con
->get_auth());
944 if (global_id
&& global_id
!= active_con
->get_global_id()) {
945 lderr(cct
) << __func__
<< " global_id changed from " << global_id
946 << " to " << active_con
->get_global_id() << dendl
;
948 global_id
= active_con
->get_global_id();
953 void MonClient::tick()
955 ldout(cct
, 10) << __func__
<< dendl
;
957 utime_t now
= ceph_clock_now();
959 auto reschedule_tick
= make_scope_guard([this] {
963 _check_auth_tickets();
964 _check_tell_commands();
967 ldout(cct
, 1) << "continuing hunt" << dendl
;
968 return _reopen_session();
969 } else if (active_con
) {
970 // just renew as needed
971 auto cur_con
= active_con
->get_con();
972 if (!cur_con
->has_feature(CEPH_FEATURE_MON_STATEFUL_SUB
)) {
973 const bool maybe_renew
= sub
.need_renew();
974 ldout(cct
, 10) << "renew subs? -- " << (maybe_renew
? "yes" : "no")
981 if (now
> last_keepalive
+ cct
->_conf
->mon_client_ping_interval
) {
982 cur_con
->send_keepalive();
983 last_keepalive
= now
;
985 if (cct
->_conf
->mon_client_ping_timeout
> 0 &&
986 cur_con
->has_feature(CEPH_FEATURE_MSGR_KEEPALIVE2
)) {
987 utime_t lk
= cur_con
->get_last_keepalive_ack();
988 utime_t interval
= now
- lk
;
989 if (interval
> cct
->_conf
->mon_client_ping_timeout
) {
990 ldout(cct
, 1) << "no keepalive since " << lk
<< " (" << interval
991 << " seconds), reconnecting" << dendl
;
992 return _reopen_session();
999 if (now
> last_send_log
+ cct
->_conf
->mon_client_log_interval
) {
1001 last_send_log
= now
;
1006 void MonClient::_un_backoff()
1008 // un-backoff our reconnect interval
1009 reopen_interval_multiplier
= std::max(
1010 cct
->_conf
.get_val
<double>("mon_client_hunt_interval_min_multiple"),
1011 reopen_interval_multiplier
/
1012 cct
->_conf
.get_val
<double>("mon_client_hunt_interval_backoff"));
1013 ldout(cct
, 20) << __func__
<< " reopen_interval_multipler now "
1014 << reopen_interval_multiplier
<< dendl
;
1017 void MonClient::schedule_tick()
1019 auto do_tick
= make_lambda_context([this](int) { tick(); });
1020 if (!is_connected()) {
1021 // start another round of hunting
1022 const auto hunt_interval
= (cct
->_conf
->mon_client_hunt_interval
*
1023 reopen_interval_multiplier
);
1024 timer
.add_event_after(hunt_interval
, do_tick
);
1027 timer
.add_event_after(std::min(cct
->_conf
->mon_client_ping_interval
,
1028 cct
->_conf
->mon_client_log_interval
),
1035 void MonClient::_renew_subs()
1037 ceph_assert(ceph_mutex_is_locked(monc_lock
));
1038 if (!sub
.have_new()) {
1039 ldout(cct
, 10) << __func__
<< " - empty" << dendl
;
1043 ldout(cct
, 10) << __func__
<< dendl
;
1047 auto m
= ceph::make_message
<MMonSubscribe
>();
1048 m
->what
= sub
.get_subs();
1049 m
->hostname
= ceph_get_short_hostname();
1050 _send_mon_message(std::move(m
));
1055 void MonClient::handle_subscribe_ack(MMonSubscribeAck
*m
)
1057 sub
.acked(m
->interval
);
1061 int MonClient::_check_auth_tickets()
1063 ldout(cct
, 10) << __func__
<< dendl
;
1064 ceph_assert(ceph_mutex_is_locked(monc_lock
));
1065 if (active_con
&& auth
) {
1066 if (auth
->need_tickets()) {
1067 ldout(cct
, 10) << __func__
<< " getting new tickets!" << dendl
;
1068 auto m
= ceph::make_message
<MAuth
>();
1069 m
->protocol
= auth
->get_protocol();
1070 auth
->prepare_build_request();
1071 auth
->build_request(m
->auth_payload
);
1072 _send_mon_message(m
);
1075 _check_auth_rotating();
1080 int MonClient::_check_auth_rotating()
1082 ceph_assert(ceph_mutex_is_locked(monc_lock
));
1083 if (!rotating_secrets
||
1084 !auth_principal_needs_rotating_keys(entity_name
)) {
1085 ldout(cct
, 20) << "_check_auth_rotating not needed by " << entity_name
<< dendl
;
1089 if (!active_con
|| !auth
) {
1090 ldout(cct
, 10) << "_check_auth_rotating waiting for auth session" << dendl
;
1094 utime_t now
= ceph_clock_now();
1095 utime_t cutoff
= now
;
1096 cutoff
-= std::min(30.0, cct
->_conf
->auth_service_ticket_ttl
/ 4.0);
1097 utime_t issued_at_lower_bound
= now
;
1098 issued_at_lower_bound
-= cct
->_conf
->auth_service_ticket_ttl
;
1099 if (!rotating_secrets
->need_new_secrets(cutoff
)) {
1100 ldout(cct
, 10) << "_check_auth_rotating have uptodate secrets (they expire after " << cutoff
<< ")" << dendl
;
1101 rotating_secrets
->dump_rotating();
1105 ldout(cct
, 10) << "_check_auth_rotating renewing rotating keys (they expired before " << cutoff
<< ")" << dendl
;
1106 if (!rotating_secrets
->need_new_secrets() &&
1107 rotating_secrets
->need_new_secrets(issued_at_lower_bound
)) {
1108 // the key has expired before it has been issued?
1109 lderr(cct
) << __func__
<< " possible clock skew, rotating keys expired way too early"
1110 << " (before " << issued_at_lower_bound
<< ")" << dendl
;
1112 if ((now
> last_rotating_renew_sent
) &&
1113 double(now
- last_rotating_renew_sent
) < 1) {
1114 ldout(cct
, 10) << __func__
<< " called too often (last: "
1115 << last_rotating_renew_sent
<< "), skipping refresh" << dendl
;
1118 auto m
= ceph::make_message
<MAuth
>();
1119 m
->protocol
= auth
->get_protocol();
1120 if (auth
->build_rotating_request(m
->auth_payload
)) {
1121 last_rotating_renew_sent
= now
;
1122 _send_mon_message(std::move(m
));
1127 int MonClient::wait_auth_rotating(double timeout
)
1129 std::unique_lock
l(monc_lock
);
1131 // Must be initialized
1132 ceph_assert(auth
!= nullptr);
1134 if (auth
->get_protocol() == CEPH_AUTH_NONE
)
1137 if (!rotating_secrets
)
1140 ldout(cct
, 10) << __func__
<< " waiting for " << timeout
<< dendl
;
1141 utime_t cutoff
= ceph_clock_now();
1142 cutoff
-= std::min(30.0, cct
->_conf
->auth_service_ticket_ttl
/ 4.0);
1143 if (auth_cond
.wait_for(l
, ceph::make_timespan(timeout
), [this, cutoff
] {
1144 return (!auth_principal_needs_rotating_keys(entity_name
) ||
1145 !rotating_secrets
->need_new_secrets(cutoff
));
1147 ldout(cct
, 10) << __func__
<< " done" << dendl
;
1150 ldout(cct
, 0) << __func__
<< " timed out after " << timeout
<< dendl
;
1157 void MonClient::_send_command(MonCommand
*r
)
1161 if (r
->send_attempts
> cct
->_conf
->mon_client_directed_command_retry
) {
1162 _finish_command(r
, monc_errc::mon_unavailable
, "mon unavailable", {});
1165 // tell-style command
1166 if (monmap
.min_mon_release
>= ceph_release_t::octopus
) {
1167 if (r
->target_con
) {
1168 r
->target_con
->mark_down();
1170 if (r
->target_rank
>= 0) {
1171 if (r
->target_rank
>= (int)monmap
.size()) {
1172 ldout(cct
, 10) << " target " << r
->target_rank
1173 << " >= max mon " << monmap
.size() << dendl
;
1174 _finish_command(r
, monc_errc::rank_dne
, "mon rank dne"sv
, {});
1177 r
->target_con
= messenger
->connect_to_mon(
1178 monmap
.get_addrs(r
->target_rank
), true /* anon */);
1180 if (!monmap
.contains(r
->target_name
)) {
1181 ldout(cct
, 10) << " target " << r
->target_name
1182 << " not present in monmap" << dendl
;
1183 _finish_command(r
, monc_errc::mon_dne
, "mon dne"sv
, {});
1186 r
->target_con
= messenger
->connect_to_mon(
1187 monmap
.get_addrs(r
->target_name
), true /* anon */);
1190 r
->target_session
.reset(new MonConnection(cct
, r
->target_con
, 0,
1192 r
->target_session
->start(monmap
.get_epoch(), entity_name
);
1193 r
->last_send_attempt
= ceph_clock_now();
1195 MCommand
*m
= new MCommand(monmap
.fsid
);
1198 m
->set_data(r
->inbl
);
1199 r
->target_session
->queue_command(m
);
1203 // ugly legacy handling of pre-octopus mons
1206 peer
= active_con
->get_con()->get_peer_addr();
1209 if (r
->target_rank
>= 0 &&
1210 r
->target_rank
!= monmap
.get_rank(peer
)) {
1211 ldout(cct
, 10) << __func__
<< " " << r
->tid
<< " " << r
->cmd
1212 << " wants rank " << r
->target_rank
1213 << ", reopening session"
1215 if (r
->target_rank
>= (int)monmap
.size()) {
1216 ldout(cct
, 10) << " target " << r
->target_rank
1217 << " >= max mon " << monmap
.size() << dendl
;
1218 _finish_command(r
, monc_errc::rank_dne
, "mon rank dne"sv
, {});
1221 _reopen_session(r
->target_rank
);
1224 if (r
->target_name
.length() &&
1225 r
->target_name
!= monmap
.get_name(peer
)) {
1226 ldout(cct
, 10) << __func__
<< " " << r
->tid
<< " " << r
->cmd
1227 << " wants mon " << r
->target_name
1228 << ", reopening session"
1230 if (!monmap
.contains(r
->target_name
)) {
1231 ldout(cct
, 10) << " target " << r
->target_name
1232 << " not present in monmap" << dendl
;
1233 _finish_command(r
, monc_errc::mon_dne
, "mon dne"sv
, {});
1236 _reopen_session(monmap
.get_rank(r
->target_name
));
1239 // fall-thru to send 'normal' CLI command
1242 // normal CLI command
1243 ldout(cct
, 10) << __func__
<< " " << r
->tid
<< " " << r
->cmd
<< dendl
;
1244 auto m
= ceph::make_message
<MMonCommand
>(monmap
.fsid
);
1247 m
->set_data(r
->inbl
);
1248 _send_mon_message(std::move(m
));
1252 void MonClient::_check_tell_commands()
1254 // resend any requests
1255 auto now
= ceph_clock_now();
1256 auto p
= mon_commands
.begin();
1257 while (p
!= mon_commands
.end()) {
1258 auto cmd
= p
->second
;
1260 if (cmd
->is_tell() &&
1261 cmd
->last_send_attempt
!= utime_t() &&
1262 now
- cmd
->last_send_attempt
> cct
->_conf
->mon_client_hunt_interval
) {
1263 ldout(cct
,5) << __func__
<< " timeout tell command " << cmd
->tid
<< dendl
;
1264 _send_command(cmd
); // might remove cmd from mon_commands
1269 void MonClient::_resend_mon_commands()
1271 // resend any requests
1272 auto p
= mon_commands
.begin();
1273 while (p
!= mon_commands
.end()) {
1274 auto cmd
= p
->second
;
1276 if (cmd
->is_tell() && monmap
.min_mon_release
>= ceph_release_t::octopus
) {
1277 // starting with octopus, tell commands use their own connetion and need no
1278 // special resend when we finish hunting.
1280 _send_command(cmd
); // might remove cmd from mon_commands
1285 void MonClient::handle_mon_command_ack(MMonCommandAck
*ack
)
1287 MonCommand
*r
= NULL
;
1288 uint64_t tid
= ack
->get_tid();
1290 if (tid
== 0 && !mon_commands
.empty()) {
1291 r
= mon_commands
.begin()->second
;
1292 ldout(cct
, 10) << __func__
<< " has tid 0, assuming it is " << r
->tid
<< dendl
;
1294 auto p
= mon_commands
.find(tid
);
1295 if (p
== mon_commands
.end()) {
1296 ldout(cct
, 10) << __func__
<< " " << ack
->get_tid() << " not found" << dendl
;
1303 ldout(cct
, 10) << __func__
<< " " << r
->tid
<< " " << r
->cmd
<< dendl
;
1304 auto ec
= ack
->r
< 0 ? bs::error_code(-ack
->r
, mon_category())
1306 _finish_command(r
, ec
, ack
->rs
,
1307 std::move(ack
->get_data()));
1311 void MonClient::handle_command_reply(MCommandReply
*reply
)
1313 MonCommand
*r
= NULL
;
1314 uint64_t tid
= reply
->get_tid();
1316 if (tid
== 0 && !mon_commands
.empty()) {
1317 r
= mon_commands
.begin()->second
;
1318 ldout(cct
, 10) << __func__
<< " has tid 0, assuming it is " << r
->tid
1321 auto p
= mon_commands
.find(tid
);
1322 if (p
== mon_commands
.end()) {
1323 ldout(cct
, 10) << __func__
<< " " << reply
->get_tid() << " not found"
1331 ldout(cct
, 10) << __func__
<< " " << r
->tid
<< " " << r
->cmd
<< dendl
;
1332 auto ec
= reply
->r
< 0 ? bs::error_code(-reply
->r
, mon_category())
1334 _finish_command(r
, ec
, reply
->rs
, std::move(reply
->get_data()));
1338 int MonClient::_cancel_mon_command(uint64_t tid
)
1340 ceph_assert(ceph_mutex_is_locked(monc_lock
));
1342 auto it
= mon_commands
.find(tid
);
1343 if (it
== mon_commands
.end()) {
1344 ldout(cct
, 10) << __func__
<< " tid " << tid
<< " dne" << dendl
;
1348 ldout(cct
, 10) << __func__
<< " tid " << tid
<< dendl
;
1350 MonCommand
*cmd
= it
->second
;
1351 _finish_command(cmd
, monc_errc::timed_out
, "timed out"sv
, {});
1355 void MonClient::_finish_command(MonCommand
*r
, bs::error_code ret
,
1356 std::string_view rs
, ceph::buffer::list
&& bl
)
1358 ldout(cct
, 10) << __func__
<< " " << r
->tid
<< " = " << ret
<< " " << rs
1360 ceph::async::post(std::move(r
->onfinish
), ret
, std::string(rs
),
1362 if (r
->target_con
) {
1363 r
->target_con
->mark_down();
1365 mon_commands
.erase(r
->tid
);
1371 void MonClient::handle_get_version_reply(MMonGetVersionReply
* m
)
1373 ceph_assert(ceph_mutex_is_locked(monc_lock
));
1374 auto iter
= version_requests
.find(m
->handle
);
1375 if (iter
== version_requests
.end()) {
1376 ldout(cct
, 0) << __func__
<< " version request with handle " << m
->handle
1377 << " not found" << dendl
;
1379 auto req
= std::move(iter
->second
);
1380 ldout(cct
, 10) << __func__
<< " finishing " << iter
->first
<< " version "
1381 << m
->version
<< dendl
;
1382 version_requests
.erase(iter
);
1383 ceph::async::post(std::move(req
), bs::error_code(),
1384 m
->version
, m
->oldest_version
);
1389 int MonClient::get_auth_request(
1391 AuthConnectionMeta
*auth_meta
,
1392 uint32_t *auth_method
,
1393 std::vector
<uint32_t> *preferred_modes
,
1394 ceph::buffer::list
*bl
)
1396 std::lock_guard
l(monc_lock
);
1397 ldout(cct
,10) << __func__
<< " con " << con
<< " auth_method " << *auth_method
1400 // connection to mon?
1401 if (con
->get_peer_type() == CEPH_ENTITY_TYPE_MON
) {
1402 ceph_assert(!auth_meta
->authorizer
);
1403 if (con
->is_anon()) {
1404 for (auto& i
: mon_commands
) {
1405 if (i
.second
->target_con
== con
) {
1406 return i
.second
->target_session
->get_auth_request(
1407 auth_method
, preferred_modes
, bl
,
1408 entity_name
, want_keys
, rotating_secrets
.get());
1412 for (auto& i
: pending_cons
) {
1413 if (i
.second
.is_con(con
)) {
1414 return i
.second
.get_auth_request(
1415 auth_method
, preferred_modes
, bl
,
1416 entity_name
, want_keys
, rotating_secrets
.get());
1422 // generate authorizer
1424 lderr(cct
) << __func__
<< " but no auth handler is set up" << dendl
;
1427 auth_meta
->authorizer
.reset(auth
->build_authorizer(con
->get_peer_type()));
1428 if (!auth_meta
->authorizer
) {
1429 lderr(cct
) << __func__
<< " failed to build_authorizer for type "
1430 << ceph_entity_type_name(con
->get_peer_type()) << dendl
;
1433 auth_meta
->auth_method
= auth_meta
->authorizer
->protocol
;
1434 auth_registry
.get_supported_modes(con
->get_peer_type(),
1435 auth_meta
->auth_method
,
1437 *bl
= auth_meta
->authorizer
->bl
;
1441 int MonClient::handle_auth_reply_more(
1443 AuthConnectionMeta
*auth_meta
,
1444 const ceph::buffer::list
& bl
,
1445 ceph::buffer::list
*reply
)
1447 std::lock_guard
l(monc_lock
);
1449 if (con
->get_peer_type() == CEPH_ENTITY_TYPE_MON
) {
1450 if (con
->is_anon()) {
1451 for (auto& i
: mon_commands
) {
1452 if (i
.second
->target_con
== con
) {
1453 return i
.second
->target_session
->handle_auth_reply_more(
1454 auth_meta
, bl
, reply
);
1458 for (auto& i
: pending_cons
) {
1459 if (i
.second
.is_con(con
)) {
1460 return i
.second
.handle_auth_reply_more(auth_meta
, bl
, reply
);
1466 // authorizer challenges
1467 if (!auth
|| !auth_meta
->authorizer
) {
1468 lderr(cct
) << __func__
<< " no authorizer?" << dendl
;
1471 auth_meta
->authorizer
->add_challenge(cct
, bl
);
1472 *reply
= auth_meta
->authorizer
->bl
;
1476 int MonClient::handle_auth_done(
1478 AuthConnectionMeta
*auth_meta
,
1481 const ceph::buffer::list
& bl
,
1482 CryptoKey
*session_key
,
1483 std::string
*connection_secret
)
1485 if (con
->get_peer_type() == CEPH_ENTITY_TYPE_MON
) {
1486 std::lock_guard
l(monc_lock
);
1487 if (con
->is_anon()) {
1488 for (auto& i
: mon_commands
) {
1489 if (i
.second
->target_con
== con
) {
1490 return i
.second
->target_session
->handle_auth_done(
1491 auth_meta
, global_id
, bl
,
1492 session_key
, connection_secret
);
1496 for (auto& i
: pending_cons
) {
1497 if (i
.second
.is_con(con
)) {
1498 int r
= i
.second
.handle_auth_done(
1499 auth_meta
, global_id
, bl
,
1500 session_key
, connection_secret
);
1502 pending_cons
.erase(i
.first
);
1503 if (!pending_cons
.empty()) {
1507 active_con
.reset(new MonConnection(std::move(i
.second
)));
1508 pending_cons
.clear();
1509 ceph_assert(active_con
->have_session());
1513 if (r
|| monmap
.get_epoch() > 0) {
1521 // verify authorizer reply
1522 auto p
= bl
.begin();
1523 if (!auth_meta
->authorizer
->verify_reply(p
, &auth_meta
->connection_secret
)) {
1524 ldout(cct
, 0) << __func__
<< " failed verifying authorizer reply"
1528 auth_meta
->session_key
= auth_meta
->authorizer
->session_key
;
1533 int MonClient::handle_auth_bad_method(
1535 AuthConnectionMeta
*auth_meta
,
1536 uint32_t old_auth_method
,
1538 const std::vector
<uint32_t>& allowed_methods
,
1539 const std::vector
<uint32_t>& allowed_modes
)
1541 auth_meta
->allowed_methods
= allowed_methods
;
1543 std::lock_guard
l(monc_lock
);
1544 if (con
->get_peer_type() == CEPH_ENTITY_TYPE_MON
) {
1545 if (con
->is_anon()) {
1546 for (auto& i
: mon_commands
) {
1547 if (i
.second
->target_con
== con
) {
1548 int r
= i
.second
->target_session
->handle_auth_bad_method(
1554 auto ec
= bs::error_code(-r
, mon_category());
1555 _finish_command(i
.second
, ec
, "auth failed"sv
, {});
1561 for (auto& i
: pending_cons
) {
1562 if (i
.second
.is_con(con
)) {
1563 int r
= i
.second
.handle_auth_bad_method(old_auth_method
,
1568 return r
; // try another method on this con
1570 pending_cons
.erase(i
.first
);
1571 if (!pending_cons
.empty()) {
1572 return r
; // fail this con, maybe another con will succeed
1583 ldout(cct
,10) << __func__
<< " hmm, they didn't like " << old_auth_method
1584 << " result " << cpp_strerror(result
)
1585 << " and auth is " << (auth
? auth
->get_protocol() : 0)
1591 int MonClient::handle_auth_request(
1593 AuthConnectionMeta
*auth_meta
,
1595 uint32_t auth_method
,
1596 const ceph::buffer::list
& payload
,
1597 ceph::buffer::list
*reply
)
1599 if (payload
.length() == 0) {
1600 // for some channels prior to nautilus (osd heartbeat), we
1601 // tolerate the lack of an authorizer.
1602 if (!con
->get_messenger()->require_authorizer
) {
1603 handle_authentication_dispatcher
->ms_handle_authentication(con
);
1608 auth_meta
->auth_mode
= payload
[0];
1609 if (auth_meta
->auth_mode
< AUTH_MODE_AUTHORIZER
||
1610 auth_meta
->auth_mode
> AUTH_MODE_AUTHORIZER_MAX
) {
1613 AuthAuthorizeHandler
*ah
= get_auth_authorize_handler(con
->get_peer_type(),
1616 lderr(cct
) << __func__
<< " no AuthAuthorizeHandler found for auth method "
1617 << auth_method
<< dendl
;
1621 auto ac
= &auth_meta
->authorizer_challenge
;
1622 if (auth_meta
->skip_authorizer_challenge
) {
1623 ldout(cct
, 10) << __func__
<< " skipping challenge on " << con
<< dendl
;
1627 bool was_challenge
= (bool)auth_meta
->authorizer_challenge
;
1628 bool isvalid
= ah
->verify_authorizer(
1632 auth_meta
->get_connection_secret_length(),
1635 &con
->peer_global_id
,
1636 &con
->peer_caps_info
,
1637 &auth_meta
->session_key
,
1638 &auth_meta
->connection_secret
,
1641 handle_authentication_dispatcher
->ms_handle_authentication(con
);
1644 if (!more
&& !was_challenge
&& auth_meta
->authorizer_challenge
) {
1645 ldout(cct
,10) << __func__
<< " added challenge on " << con
<< dendl
;
1648 ldout(cct
,10) << __func__
<< " bad authorizer on " << con
<< dendl
;
1649 // discard old challenge
1650 auth_meta
->authorizer_challenge
.reset();
1654 AuthAuthorizer
* MonClient::build_authorizer(int service_id
) const {
1655 std::lock_guard
l(monc_lock
);
1657 return auth
->build_authorizer(service_id
);
1659 ldout(cct
, 0) << __func__
<< " for " << ceph_entity_type_name(service_id
)
1660 << ", but no auth is available now" << dendl
;
1665 #define dout_subsys ceph_subsys_monc
1667 #define dout_prefix *_dout << "monclient" << (have_session() ? ": " : "(hunting): ")
1669 MonConnection::MonConnection(
1670 CephContext
*cct
, ConnectionRef con
, uint64_t global_id
,
1672 : cct(cct
), con(con
), global_id(global_id
), auth_registry(ar
)
1675 MonConnection::~MonConnection()
1683 bool MonConnection::have_session() const
1685 return state
== State::HAVE_SESSION
;
1688 void MonConnection::start(epoch_t epoch
,
1689 const EntityName
& entity_name
)
1692 auth_start
= ceph_clock_now();
1694 if (con
->get_peer_addr().is_msgr2()) {
1695 ldout(cct
, 10) << __func__
<< " opening mon connection" << dendl
;
1696 state
= State::AUTHENTICATING
;
1697 con
->send_message(new MMonGetMap());
1701 // restart authentication handshake
1702 state
= State::NEGOTIATING
;
1704 // send an initial keepalive to ensure our timestamp is valid by the
1705 // time we are in an OPENED state (by sequencing this before
1707 con
->send_keepalive();
1710 m
->protocol
= CEPH_AUTH_UNKNOWN
;
1711 m
->monmap_epoch
= epoch
;
1713 encode(struct_v
, m
->auth_payload
);
1714 std::vector
<uint32_t> auth_supported
;
1715 auth_registry
->get_supported_methods(con
->get_peer_type(), &auth_supported
);
1716 encode(auth_supported
, m
->auth_payload
);
1717 encode(entity_name
, m
->auth_payload
);
1718 encode(global_id
, m
->auth_payload
);
1719 con
->send_message(m
);
1722 int MonConnection::get_auth_request(
1724 std::vector
<uint32_t> *preferred_modes
,
1725 ceph::buffer::list
*bl
,
1726 const EntityName
& entity_name
,
1728 RotatingKeyRing
* keyring
)
1732 if (auth_method
< 0) {
1733 std::vector
<uint32_t> as
;
1734 auth_registry
->get_supported_methods(con
->get_peer_type(), &as
);
1738 auth_method
= as
.front();
1740 *method
= auth_method
;
1741 auth_registry
->get_supported_modes(con
->get_peer_type(), auth_method
,
1743 ldout(cct
,10) << __func__
<< " method " << *method
1744 << " preferred_modes " << *preferred_modes
<< dendl
;
1745 if (preferred_modes
->empty()) {
1749 int r
= _init_auth(*method
, entity_name
, want_keys
, keyring
, true);
1750 ceph_assert(r
== 0);
1752 // initial requset includes some boilerplate...
1753 encode((char)AUTH_MODE_MON
, *bl
);
1754 encode(entity_name
, *bl
);
1755 encode(global_id
, *bl
);
1757 // and (maybe) some method-specific initial payload
1758 auth
->build_initial_request(bl
);
1763 int MonConnection::handle_auth_reply_more(
1764 AuthConnectionMeta
*auth_meta
,
1765 const ceph::buffer::list
& bl
,
1766 ceph::buffer::list
*reply
)
1768 ldout(cct
, 10) << __func__
<< " payload " << bl
.length() << dendl
;
1769 ldout(cct
, 30) << __func__
<< " got\n";
1773 auto p
= bl
.cbegin();
1774 ldout(cct
, 10) << __func__
<< " payload_len " << bl
.length() << dendl
;
1775 int r
= auth
->handle_response(0, p
, &auth_meta
->session_key
,
1776 &auth_meta
->connection_secret
);
1778 auth
->prepare_build_request();
1779 auth
->build_request(*reply
);
1780 ldout(cct
, 10) << __func__
<< " responding with " << reply
->length()
1781 << " bytes" << dendl
;
1784 lderr(cct
) << __func__
<< " handle_response returned " << r
<< dendl
;
1786 ldout(cct
, 10) << __func__
<< " authenticated!" << dendl
;
1788 ceph_abort(cct
, "write me");
1793 int MonConnection::handle_auth_done(
1794 AuthConnectionMeta
*auth_meta
,
1795 uint64_t new_global_id
,
1796 const ceph::buffer::list
& bl
,
1797 CryptoKey
*session_key
,
1798 std::string
*connection_secret
)
1800 ldout(cct
,10) << __func__
<< " global_id " << new_global_id
1801 << " payload " << bl
.length()
1803 global_id
= new_global_id
;
1804 auth
->set_global_id(global_id
);
1805 auto p
= bl
.begin();
1806 int auth_err
= auth
->handle_response(0, p
, &auth_meta
->session_key
,
1807 &auth_meta
->connection_secret
);
1808 if (auth_err
>= 0) {
1809 state
= State::HAVE_SESSION
;
1811 con
->set_last_keepalive_ack(auth_start
);
1813 if (pending_tell_command
) {
1814 con
->send_message2(std::move(pending_tell_command
));
1819 int MonConnection::handle_auth_bad_method(
1820 uint32_t old_auth_method
,
1822 const std::vector
<uint32_t>& allowed_methods
,
1823 const std::vector
<uint32_t>& allowed_modes
)
1825 ldout(cct
,10) << __func__
<< " old_auth_method " << old_auth_method
1826 << " result " << cpp_strerror(result
)
1827 << " allowed_methods " << allowed_methods
<< dendl
;
1828 std::vector
<uint32_t> auth_supported
;
1829 auth_registry
->get_supported_methods(con
->get_peer_type(), &auth_supported
);
1830 auto p
= std::find(auth_supported
.begin(), auth_supported
.end(),
1832 assert(p
!= auth_supported
.end());
1833 p
= std::find_first_of(std::next(p
), auth_supported
.end(),
1834 allowed_methods
.begin(), allowed_methods
.end());
1835 if (p
== auth_supported
.end()) {
1836 lderr(cct
) << __func__
<< " server allowed_methods " << allowed_methods
1837 << " but i only support " << auth_supported
<< dendl
;
1841 ldout(cct
,10) << __func__
<< " will try " << auth_method
<< " next" << dendl
;
1845 int MonConnection::handle_auth(MAuthReply
* m
,
1846 const EntityName
& entity_name
,
1848 RotatingKeyRing
* keyring
)
1850 if (state
== State::NEGOTIATING
) {
1851 int r
= _negotiate(m
, entity_name
, want_keys
, keyring
);
1855 state
= State::AUTHENTICATING
;
1857 int r
= authenticate(m
);
1859 state
= State::HAVE_SESSION
;
1864 int MonConnection::_negotiate(MAuthReply
*m
,
1865 const EntityName
& entity_name
,
1867 RotatingKeyRing
* keyring
)
1869 ldout(cct
, 10) << __func__
<< dendl
;
1870 int r
= _init_auth(m
->protocol
, entity_name
, want_keys
, keyring
, false);
1871 if (r
== -ENOTSUP
) {
1872 if (m
->result
== -ENOTSUP
) {
1873 ldout(cct
, 10) << "none of our auth protocols are supported by the server"
1881 int MonConnection::_init_auth(
1883 const EntityName
& entity_name
,
1885 RotatingKeyRing
* keyring
,
1888 ldout(cct
, 10) << __func__
<< " method " << method
<< dendl
;
1889 if (auth
&& auth
->get_protocol() == (int)method
) {
1890 ldout(cct
, 10) << __func__
<< " already have auth, reseting" << dendl
;
1895 ldout(cct
, 10) << __func__
<< " creating new auth" << dendl
;
1896 auth
.reset(AuthClientHandler::create(cct
, method
, keyring
));
1898 ldout(cct
, 10) << " no handler for protocol " << method
<< dendl
;
1902 // do not request MGR key unless the mon has the SERVER_KRAKEN
1903 // feature. otherwise it will give us an auth error. note that
1904 // we have to use the FEATUREMASK because pre-jewel the kraken
1905 // feature bit was used for something else.
1907 (want_keys
& CEPH_ENTITY_TYPE_MGR
) &&
1908 !(con
->has_features(CEPH_FEATUREMASK_SERVER_KRAKEN
))) {
1909 ldout(cct
, 1) << __func__
1910 << " not requesting MGR keys from pre-kraken monitor"
1912 want_keys
&= ~CEPH_ENTITY_TYPE_MGR
;
1914 auth
->set_want_keys(want_keys
);
1915 auth
->init(entity_name
);
1916 auth
->set_global_id(global_id
);
1920 int MonConnection::authenticate(MAuthReply
*m
)
1923 if (!m
->global_id
) {
1924 ldout(cct
, 1) << "peer sent an invalid global_id" << dendl
;
1926 if (m
->global_id
!= global_id
) {
1927 // it's a new session
1929 global_id
= m
->global_id
;
1930 auth
->set_global_id(global_id
);
1931 ldout(cct
, 10) << "my global_id is " << m
->global_id
<< dendl
;
1933 auto p
= m
->result_bl
.cbegin();
1934 int ret
= auth
->handle_response(m
->result
, p
, nullptr, nullptr);
1935 if (ret
== -EAGAIN
) {
1936 auto ma
= new MAuth
;
1937 ma
->protocol
= auth
->get_protocol();
1938 auth
->prepare_build_request();
1939 auth
->build_request(ma
->auth_payload
);
1940 con
->send_message(ma
);
1942 if (ret
== 0 && pending_tell_command
) {
1943 con
->send_message2(std::move(pending_tell_command
));
1949 void MonClient::register_config_callback(md_config_t::config_callback fn
) {
1950 ceph_assert(!config_cb
);
1954 md_config_t::config_callback
MonClient::get_config_callback() {
1958 #pragma GCC diagnostic push
1959 #pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
1960 #pragma clang diagnostic push
1961 #pragma clang diagnostic ignored "-Wnon-virtual-dtor"
1962 class monc_error_category
: public ceph::converting_category
{
1964 monc_error_category(){}
1965 const char* name() const noexcept override
;
1966 const char* message(int ev
, char*, std::size_t) const noexcept override
;
1967 std::string
message(int ev
) const override
;
1968 bs::error_condition
default_error_condition(int ev
) const noexcept
1970 bool equivalent(int ev
, const bs::error_condition
& c
) const
1972 using ceph::converting_category::equivalent
;
1973 int from_code(int ev
) const noexcept override
;
1975 #pragma GCC diagnostic pop
1976 #pragma clang diagnostic pop
1978 const char* monc_error_category::name() const noexcept
{
1982 const char* monc_error_category::message(int ev
, char*, std::size_t) const noexcept
{
1986 switch (static_cast<monc_errc
>(ev
)) {
1987 case monc_errc::shutting_down
: // Command failed due to MonClient shutting down
1988 return "Command failed due to MonClient shutting down";
1989 case monc_errc::session_reset
:
1990 return "Monitor session was reset";
1991 case monc_errc::rank_dne
:
1992 return "Requested monitor rank does not exist";
1993 case monc_errc::mon_dne
:
1994 return "Requested monitor does not exist";
1995 case monc_errc::timed_out
:
1996 return "Monitor operation timed out";
1997 case monc_errc::mon_unavailable
:
1998 return "Monitor unavailable";
2001 return "Unknown error";
2004 std::string
monc_error_category::message(int ev
) const {
2005 return message(ev
, nullptr, 0);
2008 bs::error_condition
monc_error_category::default_error_condition(int ev
) const noexcept
{
2009 switch (static_cast<monc_errc
>(ev
)) {
2010 case monc_errc::shutting_down
:
2011 return bs::errc::operation_canceled
;
2012 case monc_errc::session_reset
:
2013 return bs::errc::resource_unavailable_try_again
;
2014 case monc_errc::rank_dne
:
2016 case monc_errc::mon_dne
:
2017 return ceph::errc::not_in_map
;
2018 case monc_errc::timed_out
:
2019 return bs::errc::timed_out
;
2020 case monc_errc::mon_unavailable
:
2021 return bs::errc::no_such_device
;
2023 return { ev
, *this };
2026 bool monc_error_category::equivalent(int ev
, const bs::error_condition
& c
) const noexcept
{
2027 switch (static_cast<monc_errc
>(ev
)) {
2028 case monc_errc::rank_dne
:
2030 case monc_errc::mon_dne
:
2031 return c
== bs::errc::no_such_file_or_directory
;
2033 return default_error_condition(ev
) == c
;
2037 int monc_error_category::from_code(int ev
) const noexcept
{
2041 switch (static_cast<monc_errc
>(ev
)) {
2042 case monc_errc::shutting_down
:
2044 case monc_errc::session_reset
:
2046 case monc_errc::rank_dne
:
2048 case monc_errc::mon_dne
:
2050 case monc_errc::timed_out
:
2052 case monc_errc::mon_unavailable
:
2058 const bs::error_category
& monc_category() noexcept
{
2059 static const monc_error_category c
;