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 <boost/intrusive_ptr.hpp>
22 // Because intusive_ptr clobbers our assert...
23 #include "include/assert.h"
25 #include "include/types.h"
26 #include "include/buffer.h"
28 #include "common/RefCountedObj.h"
30 #include "common/debug.h"
31 #include "common/config.h"
34 // ======================================================
36 // abstract Connection, for keeping per-connection state
41 struct Connection : public RefCountedObject {
44 RefCountedObject *priv;
46 entity_addr_t peer_addr;
47 utime_t last_keepalive, last_keepalive_ack;
51 bool failed; // true if we are a lossy connection that has failed.
53 int rx_buffers_version;
54 map<ceph_tid_t,pair<bufferlist,int> > rx_buffers;
56 friend class boost::intrusive_ptr<Connection>;
57 friend class PipeConnection;
60 Connection(CephContext *cct, Messenger *m)
61 // we are managed exlusively by ConnectionRef; make it so you can
62 // ConnectionRef foo = new Connection;
63 : RefCountedObject(cct, 0),
64 lock("Connection::lock"),
70 rx_buffers_version(0) {
73 ~Connection() override {
74 //generic_dout(0) << "~Connection " << this << dendl;
76 //generic_dout(0) << "~Connection " << this << " dropping priv " << priv << dendl;
81 void set_priv(RefCountedObject *o) {
82 Mutex::Locker l(lock);
88 RefCountedObject *get_priv() {
89 Mutex::Locker l(lock);
96 * Used to judge whether this connection is ready to send. Usually, the
97 * implementation need to build a own shakehand or sesson then it can be
100 * @return true if ready to send, or false otherwise
102 virtual bool is_connected() = 0;
104 Messenger *get_messenger() {
109 * Queue the given Message to send out on the given Connection.
110 * Success in this function does not guarantee Message delivery, only
111 * success in queueing the Message. Other guarantees may be provided based
112 * on the Connection policy.
114 * @param m The Message to send. The Messenger consumes a single reference
115 * when you pass it in.
117 * @return 0 on success, or -errno on failure.
119 virtual int send_message(Message *m) = 0;
121 * Send a "keepalive" ping along the given Connection, if it's working.
122 * If the underlying connection has broken, this function does nothing.
124 * @return 0, or implementation-defined error numbers.
126 virtual void send_keepalive() = 0;
128 * Mark down the given Connection.
130 * This will cause us to discard its outgoing queue, and if reset
131 * detection is enabled in the policy and the endpoint tries to
132 * reconnect they will discard their queue when we inform them of
135 * It does not generate any notifications to the Dispatcher.
137 virtual void mark_down() = 0;
140 * Mark a Connection as "disposable", setting it to lossy
141 * (regardless of initial Policy). This does not immediately close
142 * the Connection once Messages have been delivered, so as long as
143 * there are no errors you can continue to receive responses; but it
144 * will not attempt to reconnect for message delivery or preserve
145 * your old delivery semantics, either.
147 * TODO: There's some odd stuff going on in our SimpleMessenger
148 * implementation during connect that looks unused; is there
149 * more of a contract that that's enforcing?
151 virtual void mark_disposable() = 0;
154 int get_peer_type() const { return peer_type; }
155 void set_peer_type(int t) { peer_type = t; }
157 bool peer_is_mon() const { return peer_type == CEPH_ENTITY_TYPE_MON; }
158 bool peer_is_mgr() const { return peer_type == CEPH_ENTITY_TYPE_MGR; }
159 bool peer_is_mds() const { return peer_type == CEPH_ENTITY_TYPE_MDS; }
160 bool peer_is_osd() const { return peer_type == CEPH_ENTITY_TYPE_OSD; }
161 bool peer_is_client() const { return peer_type == CEPH_ENTITY_TYPE_CLIENT; }
163 const entity_addr_t& get_peer_addr() const { return peer_addr; }
164 void set_peer_addr(const entity_addr_t& a) { peer_addr = a; }
166 uint64_t get_features() const { return features; }
167 bool has_feature(uint64_t f) const { return features & f; }
168 bool has_features(uint64_t f) const {
169 return (features & f) == f;
171 void set_features(uint64_t f) { features = f; }
172 void set_feature(uint64_t f) { features |= f; }
174 void post_rx_buffer(ceph_tid_t tid, bufferlist& bl) {
175 Mutex::Locker l(lock);
176 ++rx_buffers_version;
177 rx_buffers[tid] = pair<bufferlist,int>(bl, rx_buffers_version);
180 void revoke_rx_buffer(ceph_tid_t tid) {
181 Mutex::Locker l(lock);
182 rx_buffers.erase(tid);
185 utime_t get_last_keepalive() const {
186 Mutex::Locker l(lock);
187 return last_keepalive;
189 void set_last_keepalive(utime_t t) {
190 Mutex::Locker l(lock);
193 utime_t get_last_keepalive_ack() const {
194 Mutex::Locker l(lock);
195 return last_keepalive_ack;
197 void set_last_keepalive_ack(utime_t t) {
198 Mutex::Locker l(lock);
199 last_keepalive_ack = t;
204 typedef boost::intrusive_ptr<Connection> ConnectionRef;
207 #endif /* CEPH_CONNECTION_H */