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.
15 #ifndef CEPH_CONNECTION_H
16 #define CEPH_CONNECTION_H
21 #include "auth/Auth.h"
22 #include "common/RefCountedObj.h"
23 #include "common/config.h"
24 #include "common/debug.h"
25 #include "common/ref.h"
26 #include "common/ceph_mutex.h"
27 #include "include/ceph_assert.h" // Because intusive_ptr clobbers our assert...
28 #include "include/buffer.h"
29 #include "include/types.h"
30 #include "common/item_history.h"
31 #include "msg/MessageRef.h"
33 // ======================================================
35 // abstract Connection, for keeping per-connection state
39 #ifdef UNIT_TESTS_BUILT
43 struct Connection
: public RefCountedObjectSafe
{
44 mutable ceph::mutex lock
= ceph::make_mutex("Connection::lock");
48 int64_t peer_id
= -1; // [msgr2 only] the 0 of osd.0, 4567 or client.4567
49 safe_item_history
<entity_addrvec_t
> peer_addrs
;
50 utime_t last_keepalive
, last_keepalive_ack
;
51 bool anon
= false; ///< anonymous outgoing connection
53 uint64_t features
= 0;
55 bool is_loopback
= false;
56 bool failed
= false; // true if we are a lossy connection that has failed.
58 int rx_buffers_version
= 0;
59 std::map
<ceph_tid_t
,std::pair
<ceph::buffer::list
, int>> rx_buffers
;
61 // authentication state
62 // FIXME make these private after ms_handle_authorizer is removed
64 AuthCapsInfo peer_caps_info
;
66 uint64_t peer_global_id
= 0;
68 #ifdef UNIT_TESTS_BUILT
69 Interceptor
*interceptor
;
72 friend class PipeConnection
;
75 void set_priv(const RefCountedPtr
& o
) {
76 std::lock_guard l
{lock
};
80 RefCountedPtr
get_priv() {
81 std::lock_guard l
{lock
};
86 std::lock_guard l
{lock
};
91 * Used to judge whether this connection is ready to send. Usually, the
92 * implementation need to build a own shakehand or sesson then it can be
95 * @return true if ready to send, or false otherwise
97 virtual bool is_connected() = 0;
99 virtual bool is_msgr2() const {
103 bool is_anon() const {
107 Messenger
*get_messenger() {
112 * Queue the given Message to send out on the given Connection.
113 * Success in this function does not guarantee Message delivery, only
114 * success in queueing the Message. Other guarantees may be provided based
115 * on the Connection policy.
117 * @param m The Message to send. The Messenger consumes a single reference
118 * when you pass it in.
120 * @return 0 on success, or -errno on failure.
122 virtual int send_message(Message
*m
) = 0;
124 virtual int send_message2(MessageRef m
)
126 return send_message(m
.detach()); /* send_message(Message *m) consumes a reference */
130 * Send a "keepalive" ping along the given Connection, if it's working.
131 * If the underlying connection has broken, this function does nothing.
133 * @return 0, or implementation-defined error numbers.
135 virtual void send_keepalive() = 0;
137 * Mark down the given Connection.
139 * This will cause us to discard its outgoing queue, and if reset
140 * detection is enabled in the policy and the endpoint tries to
141 * reconnect they will discard their queue when we inform them of
144 * It does not generate any notifications to the Dispatcher.
146 virtual void mark_down() = 0;
149 * Mark a Connection as "disposable", setting it to lossy
150 * (regardless of initial Policy). This does not immediately close
151 * the Connection once Messages have been delivered, so as long as
152 * there are no errors you can continue to receive responses; but it
153 * will not attempt to reconnect for message delivery or preserve
154 * your old delivery semantics, either.
156 * TODO: There's some odd stuff going on in our SimpleMessenger
157 * implementation during connect that looks unused; is there
158 * more of a contract that that's enforcing?
160 virtual void mark_disposable() = 0;
162 // WARNING / FIXME: this is not populated for loopback connections
163 AuthCapsInfo
& get_peer_caps_info() {
164 return peer_caps_info
;
166 const EntityName
& get_peer_entity_name() {
169 uint64_t get_peer_global_id() {
170 return peer_global_id
;
173 int get_peer_type() const { return peer_type
; }
174 void set_peer_type(int t
) { peer_type
= t
; }
176 // peer_id is only defined for msgr2
177 int64_t get_peer_id() const { return peer_id
; }
178 void set_peer_id(int64_t t
) { peer_id
= t
; }
180 bool peer_is_mon() const { return peer_type
== CEPH_ENTITY_TYPE_MON
; }
181 bool peer_is_mgr() const { return peer_type
== CEPH_ENTITY_TYPE_MGR
; }
182 bool peer_is_mds() const { return peer_type
== CEPH_ENTITY_TYPE_MDS
; }
183 bool peer_is_osd() const { return peer_type
== CEPH_ENTITY_TYPE_OSD
; }
184 bool peer_is_client() const { return peer_type
== CEPH_ENTITY_TYPE_CLIENT
; }
186 /// which of the peer's addrs is actually in use for this connection
187 virtual entity_addr_t
get_peer_socket_addr() const = 0;
189 entity_addr_t
get_peer_addr() const {
190 return peer_addrs
->front();
192 const entity_addrvec_t
& get_peer_addrs() const {
195 void set_peer_addr(const entity_addr_t
& a
) {
196 peer_addrs
= entity_addrvec_t(a
);
198 void set_peer_addrs(const entity_addrvec_t
& av
) { peer_addrs
= av
; }
200 uint64_t get_features() const { return features
; }
201 bool has_feature(uint64_t f
) const { return features
& f
; }
202 bool has_features(uint64_t f
) const {
203 return (features
& f
) == f
;
205 void set_features(uint64_t f
) { features
= f
; }
206 void set_feature(uint64_t f
) { features
|= f
; }
208 virtual int get_con_mode() const {
209 return CEPH_CON_MODE_CRC
;
212 void post_rx_buffer(ceph_tid_t tid
, ceph::buffer::list
& bl
) {
214 std::lock_guard l
{lock
};
215 ++rx_buffers_version
;
216 rx_buffers
[tid
] = pair
<bufferlist
,int>(bl
, rx_buffers_version
);
220 void revoke_rx_buffer(ceph_tid_t tid
) {
222 std::lock_guard l
{lock
};
223 rx_buffers
.erase(tid
);
227 utime_t
get_last_keepalive() const {
228 std::lock_guard l
{lock
};
229 return last_keepalive
;
231 void set_last_keepalive(utime_t t
) {
232 std::lock_guard l
{lock
};
235 utime_t
get_last_keepalive_ack() const {
236 std::lock_guard l
{lock
};
237 return last_keepalive_ack
;
239 void set_last_keepalive_ack(utime_t t
) {
240 std::lock_guard l
{lock
};
241 last_keepalive_ack
= t
;
243 bool is_blackhole() const;
246 Connection(CephContext
*cct
, Messenger
*m
)
247 : RefCountedObjectSafe(cct
),
251 ~Connection() override
{
252 //generic_dout(0) << "~Connection " << this << dendl;
256 using ConnectionRef
= ceph::ref_t
<Connection
>;
258 #endif /* CEPH_CONNECTION_H */