summaryrefslogtreecommitdiffstats
path: root/src/crimson/net/FrameAssemblerV2.h
blob: 9c89c144e80a1bf69964741be13a894065896fc5 (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
254
255
256
257
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:nil -*-
// vim: ts=8 sw=2 smarttab

#pragma once

#include "msg/async/frames_v2.h"
#include "msg/async/crypto_onwire.h"
#include "msg/async/compression_onwire.h"

#include "crimson/common/gated.h"
#include "crimson/net/Socket.h"

#ifdef UNIT_TESTS_BUILT
#include "Interceptor.h"
#endif

namespace crimson::net {

class SocketConnection;
class FrameAssemblerV2;
using FrameAssemblerV2Ref = std::unique_ptr<FrameAssemblerV2>;

class FrameAssemblerV2 {
public:
  FrameAssemblerV2(SocketConnection &conn);

  ~FrameAssemblerV2();

  FrameAssemblerV2(const FrameAssemblerV2 &) = delete;

  FrameAssemblerV2(FrameAssemblerV2 &&) = delete;

  void set_shard_id(seastar::shard_id _sid) {
    assert(seastar::this_shard_id() == sid);
    clear();
    sid = _sid;
  }

  seastar::shard_id get_shard_id() const {
    return sid;
  }

  void set_is_rev1(bool is_rev1);

  void create_session_stream_handlers(
      const AuthConnectionMeta &auth_meta,
      bool crossed);

  void reset_handlers();

  /*
   * replacing
   */

  struct mover_t {
    SocketFRef socket;
    ceph::crypto::onwire::rxtx_t session_stream_handlers;
    ceph::compression::onwire::rxtx_t session_comp_handlers;
  };

  mover_t to_replace();

  seastar::future<> replace_by(mover_t &&);

  /*
   * auth signature interfaces
   */

  void start_recording();

  struct record_bufs_t {
    ceph::bufferlist rxbuf;
    ceph::bufferlist txbuf;
  };
  record_bufs_t stop_recording();

  /*
   * socket maintainence interfaces
   */

  // the socket exists and not shutdown
  bool is_socket_valid() const;

  seastar::shard_id get_socket_shard_id() const;

  void set_socket(SocketFRef &&);

  void learn_socket_ephemeral_port_as_connector(uint16_t port);

  // if may_cross_core == true, gate is required for cross-core shutdown
  template <bool may_cross_core>
  void shutdown_socket(crimson::common::Gated *gate);

  seastar::future<> replace_shutdown_socket(SocketFRef &&);

  seastar::future<> close_shutdown_socket();

  /*
   * socket read and write interfaces
   */

  template <bool may_cross_core = true>
  seastar::future<ceph::bufferptr> read_exactly(std::size_t bytes);

  template <bool may_cross_core = true>
  seastar::future<ceph::bufferlist> read(std::size_t bytes);

  template <bool may_cross_core = true>
  seastar::future<> write(ceph::bufferlist);

  template <bool may_cross_core = true>
  seastar::future<> flush();

  template <bool may_cross_core = true>
  seastar::future<> write_flush(ceph::bufferlist);

  /*
   * frame read and write interfaces
   */

  /// may throw negotiation_failure as fault
  struct read_main_t {
    ceph::msgr::v2::Tag tag;
    const ceph::msgr::v2::FrameAssembler *rx_frame_asm;
  };
  template <bool may_cross_core = true>
  seastar::future<read_main_t> read_main_preamble();

  /// may throw negotiation_failure as fault
  using read_payload_t = ceph::msgr::v2::segment_bls_t;
  // FIXME: read_payload_t cannot be no-throw move constructible
  template <bool may_cross_core = true>
  seastar::future<read_payload_t*> read_frame_payload();

  template <class F>
  ceph::bufferlist get_buffer(F &tx_frame) {
    assert(seastar::this_shard_id() == sid);
    auto bl = tx_frame.get_buffer(tx_frame_asm);
    log_main_preamble(bl);
    return bl;
  }

  template <class F, bool may_cross_core = true>
  seastar::future<> write_flush_frame(F &tx_frame) {
    assert(seastar::this_shard_id() == sid);
    auto bl = get_buffer(tx_frame);
#ifdef UNIT_TESTS_BUILT
    return intercept_frame(F::tag, true
    ).then([this, bl=std::move(bl)]() mutable {
      return write_flush<may_cross_core>(std::move(bl));
    });
#else
    return write_flush<may_cross_core>(std::move(bl));
#endif
  }

  static FrameAssemblerV2Ref create(SocketConnection &conn);

#ifdef UNIT_TESTS_BUILT
  seastar::future<> intercept_frames(
      std::vector<ceph::msgr::v2::Tag> tags,
      bool is_write) {
    auto type = is_write ? bp_type_t::WRITE : bp_type_t::READ;
    std::vector<Breakpoint> bps;
    for (auto &tag : tags) {
      bps.emplace_back(Breakpoint{tag, type});
    }
    return intercept_frames(bps, type);
  }

  seastar::future<> intercept_frame(
      ceph::msgr::v2::Tag tag,
      bool is_write) {
    auto type = is_write ? bp_type_t::WRITE : bp_type_t::READ;
    std::vector<Breakpoint> bps;
    bps.emplace_back(Breakpoint{tag, type});
    return intercept_frames(bps, type);
  }

  seastar::future<> intercept_frame(
      custom_bp_t bp,
      bool is_write) {
    auto type = is_write ? bp_type_t::WRITE : bp_type_t::READ;
    std::vector<Breakpoint> bps;
    bps.emplace_back(Breakpoint{bp});
    return intercept_frames(bps, type);
  }
#endif

private:
#ifdef UNIT_TESTS_BUILT
  seastar::future<> intercept_frames(
      std::vector<Breakpoint> bps,
      bp_type_t type);
#endif

  bool has_socket() const;

  SocketFRef move_socket();

  void clear();

  void log_main_preamble(const ceph::bufferlist &bl);

  SocketConnection &conn;

  SocketFRef socket;

  // checking Socket::is_shutdown() synchronously is impossible when sid is
  // different from the socket sid.
  bool is_socket_shutdown = false;

  // the current working shard, can be messenger or socket shard.
  // if is messenger shard, should call interfaces with may_cross_core = true.
  seastar::shard_id sid;

  /*
   * auth signature
   *
   * only in the messenger core
   */

  bool record_io = false;

  ceph::bufferlist rxbuf;

  ceph::bufferlist txbuf;

  /*
   * frame data and handlers
   */

  ceph::crypto::onwire::rxtx_t session_stream_handlers = { nullptr, nullptr };

  // TODO
  ceph::compression::onwire::rxtx_t session_comp_handlers = { nullptr, nullptr };

  bool is_rev1 = false;

  ceph::msgr::v2::FrameAssembler tx_frame_asm{
    &session_stream_handlers, is_rev1, common::local_conf()->ms_crc_data,
    &session_comp_handlers};

  ceph::msgr::v2::FrameAssembler rx_frame_asm{
    &session_stream_handlers, is_rev1, common::local_conf()->ms_crc_data,
    &session_comp_handlers};

  // in the messenger core during handshake,
  // and in the socket core during open,
  // must be cleaned before switching cores.

  ceph::bufferlist rx_preamble;

  read_payload_t rx_segments_data;
};

} // namespace crimson::net