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
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#pragma once
#include <seastar/core/gate.hh>
#include <seastar/core/reactor.hh>
#include <seastar/core/sharded.hh>
#include <seastar/net/packet.hh>
#include "include/buffer.h"
#include "crimson/common/log.h"
#include "Errors.h"
#include "Fwd.h"
#ifdef UNIT_TESTS_BUILT
#include "Interceptor.h"
#endif
namespace crimson::net {
class Socket;
using SocketRef = std::unique_ptr<Socket>;
class Socket
{
struct construct_tag {};
public:
// if acceptor side, peer is using a different port (ephemeral_port)
// if connector side, I'm using a different port (ephemeral_port)
enum class side_t {
acceptor,
connector
};
Socket(seastar::connected_socket&& _socket, side_t _side, uint16_t e_port, construct_tag)
: sid{seastar::this_shard_id()},
socket(std::move(_socket)),
in(socket.input()),
// the default buffer size 8192 is too small that may impact our write
// performance. see seastar::net::connected_socket::output()
out(socket.output(65536)),
side(_side),
ephemeral_port(e_port) {}
~Socket() {
#ifndef NDEBUG
assert(closed);
#endif
}
Socket(Socket&& o) = delete;
static seastar::future<SocketRef>
connect(const entity_addr_t& peer_addr) {
return seastar::connect(peer_addr.in4_addr()
).then([] (seastar::connected_socket socket) {
return std::make_unique<Socket>(
std::move(socket), side_t::connector, 0, construct_tag{});
});
}
/// read the requested number of bytes into a bufferlist
seastar::future<bufferlist> read(size_t bytes);
using tmp_buf = seastar::temporary_buffer<char>;
using packet = seastar::net::packet;
seastar::future<tmp_buf> read_exactly(size_t bytes);
seastar::future<> write(packet&& buf) {
#ifdef UNIT_TESTS_BUILT
return try_trap_pre(next_trap_write).then([buf = std::move(buf), this] () mutable {
#endif
return out.write(std::move(buf));
#ifdef UNIT_TESTS_BUILT
}).then([this] {
return try_trap_post(next_trap_write);
});
#endif
}
seastar::future<> flush() {
return out.flush();
}
seastar::future<> write_flush(packet&& buf) {
#ifdef UNIT_TESTS_BUILT
return try_trap_pre(next_trap_write).then([buf = std::move(buf), this] () mutable {
#endif
return out.write(std::move(buf)).then([this] { return out.flush(); });
#ifdef UNIT_TESTS_BUILT
}).then([this] {
return try_trap_post(next_trap_write);
});
#endif
}
// preemptively disable further reads or writes, can only be shutdown once.
void shutdown();
/// Socket can only be closed once.
seastar::future<> close();
// shutdown input_stream only, for tests
void force_shutdown_in() {
socket.shutdown_input();
}
// shutdown output_stream only, for tests
void force_shutdown_out() {
socket.shutdown_output();
}
side_t get_side() const {
return side;
}
uint16_t get_ephemeral_port() const {
return ephemeral_port;
}
// learn my ephemeral_port as connector.
// unfortunately, there's no way to identify which port I'm using as
// connector with current seastar interface.
void learn_ephemeral_port_as_connector(uint16_t port) {
assert(side == side_t::connector &&
(ephemeral_port == 0 || ephemeral_port == port));
ephemeral_port = port;
}
private:
const seastar::shard_id sid;
seastar::connected_socket socket;
seastar::input_stream<char> in;
seastar::output_stream<char> out;
side_t side;
uint16_t ephemeral_port;
#ifndef NDEBUG
bool closed = false;
#endif
/// buffer state for read()
struct {
bufferlist buffer;
size_t remaining;
} r;
#ifdef UNIT_TESTS_BUILT
public:
void set_trap(bp_type_t type, bp_action_t action, socket_blocker* blocker_);
private:
bp_action_t next_trap_read = bp_action_t::CONTINUE;
bp_action_t next_trap_write = bp_action_t::CONTINUE;
socket_blocker* blocker = nullptr;
seastar::future<> try_trap_pre(bp_action_t& trap);
seastar::future<> try_trap_post(bp_action_t& trap);
#endif
friend class FixedCPUServerSocket;
};
class FixedCPUServerSocket
: public seastar::peering_sharded_service<FixedCPUServerSocket> {
const seastar::shard_id cpu;
entity_addr_t addr;
std::optional<seastar::server_socket> listener;
seastar::gate shutdown_gate;
using sharded_service_t = seastar::sharded<FixedCPUServerSocket>;
std::unique_ptr<sharded_service_t> service;
struct construct_tag {};
static seastar::logger& logger() {
return crimson::get_logger(ceph_subsys_ms);
}
seastar::future<> reset() {
return container().invoke_on_all([] (auto& ss) {
assert(ss.shutdown_gate.is_closed());
ss.shutdown_gate = seastar::gate();
ss.addr = entity_addr_t();
ss.listener.reset();
});
}
public:
FixedCPUServerSocket(seastar::shard_id cpu, construct_tag) : cpu{cpu} {}
~FixedCPUServerSocket() {
assert(!listener);
// detect whether user have called destroy() properly
ceph_assert(!service);
}
FixedCPUServerSocket(FixedCPUServerSocket&&) = delete;
FixedCPUServerSocket(const FixedCPUServerSocket&) = delete;
FixedCPUServerSocket& operator=(const FixedCPUServerSocket&) = delete;
using listen_ertr = crimson::errorator<
crimson::ct_error::address_in_use // The address is already bound
>;
listen_ertr::future<> listen(entity_addr_t addr);
// fn_accept should be a nothrow function of type
// seastar::future<>(SocketRef, entity_addr_t)
template <typename Func>
seastar::future<> accept(Func&& fn_accept) {
assert(seastar::this_shard_id() == cpu);
logger().trace("FixedCPUServerSocket({})::accept()...", addr);
return container().invoke_on_all(
[fn_accept = std::move(fn_accept)] (auto& ss) mutable {
assert(ss.listener);
// gate accepting
// FixedCPUServerSocket::shutdown() will drain the continuations in the gate
// so ignore the returned future
std::ignore = seastar::with_gate(ss.shutdown_gate,
[&ss, fn_accept = std::move(fn_accept)] () mutable {
return seastar::keep_doing([&ss, fn_accept = std::move(fn_accept)] () mutable {
return ss.listener->accept().then(
[&ss, fn_accept = std::move(fn_accept)]
(seastar::accept_result accept_result) mutable {
// assert seastar::listen_options::set_fixed_cpu() works
assert(seastar::this_shard_id() == ss.cpu);
auto [socket, paddr] = std::move(accept_result);
entity_addr_t peer_addr;
peer_addr.set_sockaddr(&paddr.as_posix_sockaddr());
peer_addr.set_type(entity_addr_t::TYPE_ANY);
SocketRef _socket = std::make_unique<Socket>(
std::move(socket), Socket::side_t::acceptor,
peer_addr.get_port(), Socket::construct_tag{});
std::ignore = seastar::with_gate(ss.shutdown_gate,
[socket = std::move(_socket), peer_addr,
&ss, fn_accept = std::move(fn_accept)] () mutable {
logger().trace("FixedCPUServerSocket({})::accept(): "
"accepted peer {}", ss.addr, peer_addr);
return fn_accept(std::move(socket), peer_addr
).handle_exception([&ss, peer_addr] (auto eptr) {
logger().error("FixedCPUServerSocket({})::accept(): "
"fn_accept(s, {}) got unexpected exception {}",
ss.addr, peer_addr, eptr);
ceph_abort();
});
});
});
}).handle_exception_type([&ss] (const std::system_error& e) {
if (e.code() == std::errc::connection_aborted ||
e.code() == std::errc::invalid_argument) {
logger().trace("FixedCPUServerSocket({})::accept(): stopped ({})",
ss.addr, e);
} else {
throw;
}
}).handle_exception([&ss] (auto eptr) {
logger().error("FixedCPUServerSocket({})::accept(): "
"got unexpected exception {}", ss.addr, eptr);
ceph_abort();
});
});
});
}
seastar::future<> shutdown();
seastar::future<> destroy();
static seastar::future<FixedCPUServerSocket*> create();
};
} // namespace crimson::net
|