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
|
// -*- 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 "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>;
using SocketFRef = seastar::foreign_ptr<SocketRef>;
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 &&, side_t, uint16_t e_port, construct_tag);
~Socket();
Socket(Socket&& o) = delete;
seastar::shard_id get_shard_id() const {
return sid;
}
side_t get_side() const {
return side;
}
uint16_t get_ephemeral_port() const {
return ephemeral_port;
}
seastar::socket_address get_local_address() const {
return socket.local_address();
}
bool is_shutdown() const {
assert(seastar::this_shard_id() == sid);
return socket_is_shutdown;
}
// 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;
}
/// read the requested number of bytes into a bufferlist
seastar::future<bufferlist> read(size_t bytes);
seastar::future<bufferptr> read_exactly(size_t bytes);
seastar::future<> write(bufferlist);
seastar::future<> flush();
seastar::future<> write_flush(bufferlist);
// preemptively disable further reads or writes, can only be shutdown once.
void shutdown();
/// Socket can only be closed once.
seastar::future<> close();
static seastar::future<SocketRef>
connect(const entity_addr_t& peer_addr);
/*
* test interfaces
*/
// shutdown for tests
void force_shutdown() {
assert(seastar::this_shard_id() == sid);
socket.shutdown_input();
socket.shutdown_output();
}
// shutdown input_stream only, for tests
void force_shutdown_in() {
assert(seastar::this_shard_id() == sid);
socket.shutdown_input();
}
// shutdown output_stream only, for tests
void force_shutdown_out() {
assert(seastar::this_shard_id() == sid);
socket.shutdown_output();
}
private:
const seastar::shard_id sid;
seastar::connected_socket socket;
seastar::input_stream<char> in;
seastar::output_stream<char> out;
bool socket_is_shutdown;
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:
seastar::future<> try_trap_pre(bp_action_t& trap);
seastar::future<> try_trap_post(bp_action_t& trap);
bp_action_t next_trap_read = bp_action_t::CONTINUE;
bp_action_t next_trap_write = bp_action_t::CONTINUE;
socket_blocker* blocker = nullptr;
#endif
friend class ShardedServerSocket;
};
using listen_ertr = crimson::errorator<
crimson::ct_error::address_in_use, // The address is already bound
crimson::ct_error::address_not_available // https://techoverflow.net/2021/08/06/how-i-fixed-python-oserror-errno-99-cannot-assign-requested-address/
>;
class ShardedServerSocket
: public seastar::peering_sharded_service<ShardedServerSocket> {
struct construct_tag {};
public:
ShardedServerSocket(
seastar::shard_id sid,
bool dispatch_only_on_primary_sid,
construct_tag);
~ShardedServerSocket();
ShardedServerSocket(ShardedServerSocket&&) = delete;
ShardedServerSocket(const ShardedServerSocket&) = delete;
ShardedServerSocket& operator=(ShardedServerSocket&&) = delete;
ShardedServerSocket& operator=(const ShardedServerSocket&) = delete;
bool is_fixed_shard_dispatching() const {
return dispatch_only_on_primary_sid;
}
listen_ertr::future<> listen(entity_addr_t addr);
using accept_func_t =
std::function<seastar::future<>(SocketRef, entity_addr_t)>;
seastar::future<> accept(accept_func_t &&_fn_accept);
seastar::future<> shutdown_destroy();
static seastar::future<ShardedServerSocket*> create(
bool dispatch_only_on_this_shard);
private:
const seastar::shard_id primary_sid;
/// XXX: Remove once all infrastructure uses multi-core messenger
const bool dispatch_only_on_primary_sid;
entity_addr_t listen_addr;
std::optional<seastar::server_socket> listener;
seastar::gate shutdown_gate;
accept_func_t fn_accept;
using sharded_service_t = seastar::sharded<ShardedServerSocket>;
std::unique_ptr<sharded_service_t> service;
};
} // namespace crimson::net
|