summaryrefslogtreecommitdiffstats
path: root/src/msg/Connection.h
blob: 4eea5ff0d6c1824f7c6323075d6237bcbd4a66a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation.  See file COPYING.
 *
 */

#ifndef CEPH_CONNECTION_H
#define CEPH_CONNECTION_H

#include <stdlib.h>
#include <ostream>

#include <boost/intrusive_ptr.hpp>

#include "auth/Auth.h"
#include "common/RefCountedObj.h"
#include "common/config.h"
#include "common/debug.h"
#include "common/Mutex.h"
#include "include/ceph_assert.h" // Because intusive_ptr clobbers our assert...
#include "include/buffer.h"
#include "include/types.h"
#include "common/item_history.h"
#include "msg/MessageRef.h"


// ======================================================

// abstract Connection, for keeping per-connection state

class Messenger;

#ifdef UNIT_TESTS_BUILT
class Interceptor;
#endif

struct Connection : public RefCountedObject {
  mutable Mutex lock;
  Messenger *msgr;
  RefCountedPtr priv;
  int peer_type;
  int64_t peer_id = -1;  // [msgr2 only] the 0 of osd.0, 4567 or client.4567
  safe_item_history<entity_addrvec_t> peer_addrs;
  utime_t last_keepalive, last_keepalive_ack;
private:
  uint64_t features;
public:
  bool failed; // true if we are a lossy connection that has failed.

  int rx_buffers_version;
  map<ceph_tid_t,pair<bufferlist,int> > rx_buffers;

  // authentication state
  // FIXME make these private after ms_handle_authorizer is removed
public:
  AuthCapsInfo peer_caps_info;
  EntityName peer_name;
  uint64_t peer_global_id = 0;

#ifdef UNIT_TESTS_BUILT
  Interceptor *interceptor;
#endif

  friend class boost::intrusive_ptr<Connection>;
  friend class PipeConnection;

public:
  Connection(CephContext *cct, Messenger *m)
    // we are managed exclusively by ConnectionRef; make it so you can
    //   ConnectionRef foo = new Connection;
    : RefCountedObject(cct, 0),
      lock("Connection::lock"),
      msgr(m),
      peer_type(-1),
      features(0),
      failed(false),
      rx_buffers_version(0) {
  }

  ~Connection() override {
    //generic_dout(0) << "~Connection " << this << dendl;
  }

  void set_priv(const RefCountedPtr& o) {
    Mutex::Locker l(lock);
    priv = o;
  }

  RefCountedPtr get_priv() {
    Mutex::Locker l(lock);
    return priv;
  }

  /**
   * Used to judge whether this connection is ready to send. Usually, the
   * implementation need to build a own shakehand or sesson then it can be
   * ready to send.
   *
   * @return true if ready to send, or false otherwise
   */
  virtual bool is_connected() = 0;

  Messenger *get_messenger() {
    return msgr;
  }

  /**
   * Queue the given Message to send out on the given Connection.
   * Success in this function does not guarantee Message delivery, only
   * success in queueing the Message. Other guarantees may be provided based
   * on the Connection policy.
   *
   * @param m The Message to send. The Messenger consumes a single reference
   * when you pass it in.
   *
   * @return 0 on success, or -errno on failure.
   */
  virtual int send_message(Message *m) = 0;

  virtual int send_message2(MessageRef m)
  {
    return send_message(m.detach()); /* send_message(Message *m) consumes a reference */
  }

  /**
   * Send a "keepalive" ping along the given Connection, if it's working.
   * If the underlying connection has broken, this function does nothing.
   *
   * @return 0, or implementation-defined error numbers.
   */
  virtual void send_keepalive() = 0;
  /**
   * Mark down the given Connection.
   *
   * This will cause us to discard its outgoing queue, and if reset
   * detection is enabled in the policy and the endpoint tries to
   * reconnect they will discard their queue when we inform them of
   * the session reset.
   *
   * It does not generate any notifications to the Dispatcher.
   */
  virtual void mark_down() = 0;

  /**
   * Mark a Connection as "disposable", setting it to lossy
   * (regardless of initial Policy).  This does not immediately close
   * the Connection once Messages have been delivered, so as long as
   * there are no errors you can continue to receive responses; but it
   * will not attempt to reconnect for message delivery or preserve
   * your old delivery semantics, either.
   *
   * TODO: There's some odd stuff going on in our SimpleMessenger
   * implementation during connect that looks unused; is there
   * more of a contract that that's enforcing?
   */
  virtual void mark_disposable() = 0;

  // WARNING / FIXME: this is not populated for loopback connections
  AuthCapsInfo& get_peer_caps_info() {
    return peer_caps_info;
  }
  const EntityName& get_peer_entity_name() {
    return peer_name;
  }
  uint64_t get_peer_global_id() {
    return peer_global_id;
  }

  int get_peer_type() const { return peer_type; }
  void set_peer_type(int t) { peer_type = t; }

  // peer_id is only defined for msgr2
  int64_t get_peer_id() const { return peer_id; }
  void set_peer_id(int64_t t) { peer_id = t; }

  bool peer_is_mon() const { return peer_type == CEPH_ENTITY_TYPE_MON; }
  bool peer_is_mgr() const { return peer_type == CEPH_ENTITY_TYPE_MGR; }
  bool peer_is_mds() const { return peer_type == CEPH_ENTITY_TYPE_MDS; }
  bool peer_is_osd() const { return peer_type == CEPH_ENTITY_TYPE_OSD; }
  bool peer_is_client() const { return peer_type == CEPH_ENTITY_TYPE_CLIENT; }

  /// which of the peer's addrs is actually in use for this connection
  virtual entity_addr_t get_peer_socket_addr() const = 0;

  entity_addr_t get_peer_addr() const {
    return peer_addrs->front();
  }
  const entity_addrvec_t& get_peer_addrs() const {
    return *peer_addrs;
  }
  void set_peer_addr(const entity_addr_t& a) {
    peer_addrs = entity_addrvec_t(a);
  }
  void set_peer_addrs(const entity_addrvec_t& av) { peer_addrs = av; }

  uint64_t get_features() const { return features; }
  bool has_feature(uint64_t f) const { return features & f; }
  bool has_features(uint64_t f) const {
    return (features & f) == f;
  }
  void set_features(uint64_t f) { features = f; }
  void set_feature(uint64_t f) { features |= f; }

  virtual int get_con_mode() const {
    return CEPH_CON_MODE_CRC;
  }

  void post_rx_buffer(ceph_tid_t tid, bufferlist& bl) {
#if 0
    Mutex::Locker l(lock);
    ++rx_buffers_version;
    rx_buffers[tid] = pair<bufferlist,int>(bl, rx_buffers_version);
#endif
  }

  void revoke_rx_buffer(ceph_tid_t tid) {
#if 0
    Mutex::Locker l(lock);
    rx_buffers.erase(tid);
#endif
  }

  utime_t get_last_keepalive() const {
    Mutex::Locker l(lock);
    return last_keepalive;
  }
  void set_last_keepalive(utime_t t) {
    Mutex::Locker l(lock);
    last_keepalive = t;
  }
  utime_t get_last_keepalive_ack() const {
    Mutex::Locker l(lock);
    return last_keepalive_ack;
  }
  void set_last_keepalive_ack(utime_t t) {
    Mutex::Locker l(lock);
    last_keepalive_ack = t;
  }

};

typedef boost::intrusive_ptr<Connection> ConnectionRef;


#endif /* CEPH_CONNECTION_H */