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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#pragma once
#include <seastar/core/shared_future.hh>
#include <seastar/core/sleep.hh>
#include "io_handler.h"
namespace crimson::net {
class ProtocolV2 final : public HandshakeListener {
using AuthConnectionMetaRef = seastar::lw_shared_ptr<AuthConnectionMeta>;
public:
ProtocolV2(SocketConnection &,
IOHandler &);
~ProtocolV2() final;
ProtocolV2(const ProtocolV2 &) = delete;
ProtocolV2(ProtocolV2 &&) = delete;
ProtocolV2 &operator=(const ProtocolV2 &) = delete;
ProtocolV2 &operator=(ProtocolV2 &&) = delete;
/**
* as HandshakeListener
*/
private:
seastar::future<> notify_out(
crosscore_t::seq_t cc_seq) final;
seastar::future<> notify_out_fault(
crosscore_t::seq_t cc_seq,
const char *where,
std::exception_ptr,
io_handler_state) final;
seastar::future<> notify_mark_down(
crosscore_t::seq_t cc_seq) final;
/*
* as ProtocolV2 to be called by SocketConnection
*/
public:
void start_connect(const entity_addr_t& peer_addr,
const entity_name_t& peer_name);
void start_accept(SocketFRef&& socket,
const entity_addr_t& peer_addr);
seastar::future<> close_clean_yielded();
#ifdef UNIT_TESTS_BUILT
bool is_ready() const {
return state == state_t::READY;
}
bool is_standby() const {
return state == state_t::STANDBY;
}
bool is_closed_clean() const {
return closed_clean;
}
bool is_closed() const {
return state == state_t::CLOSING;
}
#endif
private:
using io_state_t = IOHandler::io_state_t;
seastar::future<> wait_switch_io_shard() {
if (pr_switch_io_shard.has_value()) {
return pr_switch_io_shard->get_shared_future();
} else {
return seastar::now();
}
}
seastar::future<> wait_exit_io() {
if (pr_exit_io.has_value()) {
return pr_exit_io->get_shared_future();
} else {
assert(!need_exit_io);
return seastar::now();
}
}
enum class state_t {
NONE = 0,
ACCEPTING,
SERVER_WAIT,
ESTABLISHING,
CONNECTING,
READY,
STANDBY,
WAIT,
REPLACING,
CLOSING
};
static const char *get_state_name(state_t state) {
const char *const statenames[] = {"NONE",
"ACCEPTING",
"SERVER_WAIT",
"ESTABLISHING",
"CONNECTING",
"READY",
"STANDBY",
"WAIT",
"REPLACING",
"CLOSING"};
return statenames[static_cast<int>(state)];
}
void trigger_state_phase1(state_t new_state);
void trigger_state_phase2(state_t new_state, io_state_t new_io_state);
void trigger_state(state_t new_state, io_state_t new_io_state) {
ceph_assert_always(!pr_switch_io_shard.has_value());
trigger_state_phase1(new_state);
trigger_state_phase2(new_state, new_io_state);
}
template <typename Func, typename T>
void gated_execute(const char *what, T &who, Func &&func) {
gate.dispatch_in_background(what, who, [this, &who, &func] {
if (!execution_done.available()) {
// discard the unready future
gate.dispatch_in_background(
"gated_execute_abandon",
who,
[fut=std::move(execution_done)]() mutable {
return std::move(fut);
}
);
}
seastar::promise<> pr;
execution_done = pr.get_future();
return seastar::futurize_invoke(std::forward<Func>(func)
).finally([pr=std::move(pr)]() mutable {
pr.set_value();
});
});
}
void fault(state_t expected_state,
const char *where,
std::exception_ptr eptr);
void reset_session(bool is_full);
seastar::future<std::tuple<entity_type_t, entity_addr_t>>
banner_exchange(bool is_connect);
enum class next_step_t {
ready,
wait,
none, // protocol should have been aborted or failed
};
// CONNECTING (client)
seastar::future<> handle_auth_reply();
inline seastar::future<> client_auth() {
std::vector<uint32_t> empty;
return client_auth(empty);
}
seastar::future<> client_auth(std::vector<uint32_t> &allowed_methods);
seastar::future<next_step_t> process_wait();
seastar::future<next_step_t> client_connect();
seastar::future<next_step_t> client_reconnect();
void execute_connecting();
// ACCEPTING (server)
seastar::future<> _auth_bad_method(int r);
seastar::future<> _handle_auth_request(bufferlist& auth_payload, bool more);
seastar::future<> server_auth();
bool validate_peer_name(const entity_name_t& peer_name) const;
seastar::future<next_step_t> send_wait();
seastar::future<next_step_t> reuse_connection(ProtocolV2* existing_proto,
bool do_reset=false,
bool reconnect=false,
uint64_t conn_seq=0,
uint64_t msg_seq=0);
seastar::future<next_step_t> handle_existing_connection(SocketConnectionRef existing_conn);
seastar::future<next_step_t> server_connect();
seastar::future<next_step_t> read_reconnect();
seastar::future<next_step_t> send_retry(uint64_t connect_seq);
seastar::future<next_step_t> send_retry_global(uint64_t global_seq);
seastar::future<next_step_t> send_reset(bool full);
seastar::future<next_step_t> server_reconnect();
void execute_accepting();
// CONNECTING/ACCEPTING
seastar::future<> finish_auth();
// ESTABLISHING
void execute_establishing(SocketConnectionRef existing_conn);
// ESTABLISHING/REPLACING (server)
seastar::future<> send_server_ident();
// REPLACING (server)
void trigger_replacing(bool reconnect,
bool do_reset,
FrameAssemblerV2::mover_t &&mover,
AuthConnectionMetaRef&& new_auth_meta,
uint64_t new_peer_global_seq,
// !reconnect
uint64_t new_client_cookie,
entity_name_t new_peer_name,
uint64_t new_conn_features,
uint64_t new_peer_supported_features,
// reconnect
uint64_t new_connect_seq,
uint64_t new_msg_seq);
// READY
void execute_ready();
// STANDBY
void execute_standby();
// WAIT
void execute_wait(bool max_backoff);
// SERVER_WAIT
void execute_server_wait();
// CLOSING
// reentrant
void do_close(bool is_dispatch_reset,
std::optional<std::function<void()>> f_accept_new=std::nullopt);
private:
SocketConnection &conn;
SocketMessenger &messenger;
IOHandler &io_handler;
// asynchronously populated from io_handler
io_handler_state io_states;
crosscore_t crosscore;
bool has_socket = false;
// the socket exists and it is not shutdown
bool is_socket_valid = false;
FrameAssemblerV2Ref frame_assembler;
bool need_notify_out = false;
std::optional<seastar::shared_promise<>> pr_switch_io_shard;
bool need_exit_io = false;
std::optional<seastar::shared_promise<>> pr_exit_io;
AuthConnectionMetaRef auth_meta;
crimson::common::Gated gate;
seastar::shared_promise<> pr_closed_clean;
#ifdef UNIT_TESTS_BUILT
bool closed_clean = false;
#endif
state_t state = state_t::NONE;
uint64_t peer_supported_features = 0;
uint64_t client_cookie = 0;
uint64_t server_cookie = 0;
uint64_t global_seq = 0;
uint64_t peer_global_seq = 0;
uint64_t connect_seq = 0;
seastar::future<> execution_done = seastar::now();
class Timer {
double last_dur_ = 0.0;
const SocketConnection& conn;
std::optional<seastar::abort_source> as;
public:
Timer(SocketConnection& conn) : conn(conn) {}
double last_dur() const { return last_dur_; }
seastar::future<> backoff(double seconds);
void cancel() {
last_dur_ = 0.0;
if (as) {
as->request_abort();
as = std::nullopt;
}
}
};
Timer protocol_timer;
};
struct create_handlers_ret {
std::unique_ptr<ConnectionHandler> io_handler;
std::unique_ptr<ProtocolV2> protocol;
};
inline create_handlers_ret create_handlers(ChainedDispatchers &dispatchers, SocketConnection &conn) {
std::unique_ptr<ConnectionHandler> io_handler = std::make_unique<IOHandler>(dispatchers, conn);
IOHandler &io_handler_concrete = static_cast<IOHandler&>(*io_handler);
auto protocol = std::make_unique<ProtocolV2>(conn, io_handler_concrete);
io_handler_concrete.set_handshake_listener(*protocol);
return {std::move(io_handler), std::move(protocol)};
}
} // namespace crimson::net
#if FMT_VERSION >= 90000
template <> struct fmt::formatter<crimson::net::ProtocolV2> : fmt::ostream_formatter {};
#endif
|