]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | /* | |
4 | * Ceph - scalable distributed file system | |
5 | * | |
6 | * Copyright (C) 2017 Red Hat, Inc | |
7 | * | |
8 | * This is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License version 2.1, as published by the Free Software | |
11 | * Foundation. See file COPYING. | |
12 | * | |
13 | */ | |
14 | ||
15 | #pragma once | |
16 | ||
11fdf7f2 TL |
17 | #include <seastar/core/sharded.hh> |
18 | ||
19 | #include "msg/Policy.h" | |
f67539c2 | 20 | #include "crimson/common/throttle.h" |
9f95a23c TL |
21 | #include "crimson/net/Connection.h" |
22 | #include "crimson/net/Socket.h" | |
11fdf7f2 | 23 | |
9f95a23c | 24 | namespace crimson::net { |
11fdf7f2 | 25 | |
1e59de90 | 26 | class ProtocolV2; |
11fdf7f2 | 27 | class SocketMessenger; |
aee94f69 | 28 | class SocketConnection; |
11fdf7f2 TL |
29 | using SocketConnectionRef = seastar::shared_ptr<SocketConnection>; |
30 | ||
1e59de90 TL |
31 | #ifdef UNIT_TESTS_BUILT |
32 | class Interceptor; | |
33 | #endif | |
34 | ||
35 | /** | |
36 | * ConnectionHandler | |
37 | * | |
38 | * The interface class to implement Connection, called by SocketConnection. | |
aee94f69 TL |
39 | * |
40 | * The operations must be done in get_shard_id(). | |
1e59de90 TL |
41 | */ |
42 | class ConnectionHandler { | |
43 | public: | |
44 | using clock_t = seastar::lowres_system_clock; | |
45 | ||
46 | virtual ~ConnectionHandler() = default; | |
47 | ||
48 | ConnectionHandler(const ConnectionHandler &) = delete; | |
49 | ConnectionHandler(ConnectionHandler &&) = delete; | |
50 | ConnectionHandler &operator=(const ConnectionHandler &) = delete; | |
51 | ConnectionHandler &operator=(ConnectionHandler &&) = delete; | |
52 | ||
aee94f69 TL |
53 | virtual seastar::shard_id get_shard_id() const = 0; |
54 | ||
1e59de90 TL |
55 | virtual bool is_connected() const = 0; |
56 | ||
aee94f69 | 57 | virtual seastar::future<> send(MessageFRef) = 0; |
1e59de90 TL |
58 | |
59 | virtual seastar::future<> send_keepalive() = 0; | |
60 | ||
61 | virtual clock_t::time_point get_last_keepalive() const = 0; | |
62 | ||
63 | virtual clock_t::time_point get_last_keepalive_ack() const = 0; | |
64 | ||
65 | virtual void set_last_keepalive_ack(clock_t::time_point) = 0; | |
66 | ||
67 | virtual void mark_down() = 0; | |
68 | ||
69 | protected: | |
70 | ConnectionHandler() = default; | |
71 | }; | |
72 | ||
11fdf7f2 | 73 | class SocketConnection : public Connection { |
aee94f69 TL |
74 | /* |
75 | * Connection interfaces, public to users | |
76 | * Working in ConnectionHandler::get_shard_id() | |
77 | */ | |
11fdf7f2 TL |
78 | public: |
79 | SocketConnection(SocketMessenger& messenger, | |
20effc67 | 80 | ChainedDispatchers& dispatchers); |
1e59de90 | 81 | |
9f95a23c | 82 | ~SocketConnection() override; |
11fdf7f2 | 83 | |
aee94f69 TL |
84 | const seastar::shard_id get_shard_id() const override { |
85 | return io_handler->get_shard_id(); | |
86 | } | |
87 | ||
1e59de90 TL |
88 | const entity_name_t &get_peer_name() const override { |
89 | return peer_name; | |
90 | } | |
11fdf7f2 | 91 | |
1e59de90 TL |
92 | const entity_addr_t &get_peer_addr() const override { |
93 | return peer_addr; | |
94 | } | |
11fdf7f2 | 95 | |
1e59de90 TL |
96 | const entity_addr_t &get_peer_socket_addr() const override { |
97 | return target_addr; | |
98 | } | |
f67539c2 | 99 | |
1e59de90 TL |
100 | uint64_t get_features() const override { |
101 | return features; | |
102 | } | |
9f95a23c | 103 | |
1e59de90 | 104 | bool is_connected() const override; |
11fdf7f2 | 105 | |
20effc67 | 106 | seastar::future<> send(MessageURef msg) override; |
11fdf7f2 | 107 | |
1e59de90 TL |
108 | seastar::future<> send_keepalive() override; |
109 | ||
110 | clock_t::time_point get_last_keepalive() const override; | |
111 | ||
112 | clock_t::time_point get_last_keepalive_ack() const override; | |
113 | ||
114 | void set_last_keepalive_ack(clock_t::time_point when) override; | |
11fdf7f2 | 115 | |
f67539c2 | 116 | void mark_down() override; |
11fdf7f2 | 117 | |
1e59de90 TL |
118 | bool has_user_private() const override { |
119 | return user_private != nullptr; | |
120 | } | |
121 | ||
122 | user_private_t &get_user_private() override { | |
123 | assert(has_user_private()); | |
124 | return *user_private; | |
125 | } | |
126 | ||
127 | void set_user_private(std::unique_ptr<user_private_t> new_user_private) override { | |
128 | assert(!has_user_private()); | |
129 | user_private = std::move(new_user_private); | |
130 | } | |
131 | ||
20effc67 | 132 | void print(std::ostream& out) const override; |
11fdf7f2 | 133 | |
aee94f69 TL |
134 | /* |
135 | * Public to SocketMessenger | |
136 | * Working in SocketMessenger::get_shard_id(); | |
137 | */ | |
1e59de90 | 138 | public: |
11fdf7f2 TL |
139 | /// start a handshake from the client's perspective, |
140 | /// only call when SocketConnection first construct | |
141 | void start_connect(const entity_addr_t& peer_addr, | |
f67539c2 | 142 | const entity_name_t& peer_name); |
1e59de90 | 143 | |
11fdf7f2 TL |
144 | /// start a handshake from the server's perspective, |
145 | /// only call when SocketConnection first construct | |
aee94f69 | 146 | void start_accept(SocketFRef&& socket, |
11fdf7f2 TL |
147 | const entity_addr_t& peer_addr); |
148 | ||
1e59de90 TL |
149 | seastar::future<> close_clean_yielded(); |
150 | ||
151 | seastar::socket_address get_local_address() const; | |
152 | ||
aee94f69 TL |
153 | seastar::shard_id get_messenger_shard_id() const; |
154 | ||
155 | SocketMessenger &get_messenger() const; | |
1e59de90 TL |
156 | |
157 | ConnectionRef get_local_shared_foreign_from_this(); | |
f67539c2 | 158 | |
1e59de90 | 159 | private: |
aee94f69 | 160 | void set_peer_type(entity_type_t peer_type); |
9f95a23c | 161 | |
aee94f69 | 162 | void set_peer_id(int64_t peer_id); |
11fdf7f2 | 163 | |
1e59de90 TL |
164 | void set_peer_name(entity_name_t name) { |
165 | set_peer_type(name.type()); | |
166 | set_peer_id(name.num()); | |
167 | } | |
168 | ||
aee94f69 TL |
169 | void set_features(uint64_t f); |
170 | ||
171 | void set_socket(Socket *s); | |
20effc67 | 172 | |
1e59de90 | 173 | #ifdef UNIT_TESTS_BUILT |
aee94f69 TL |
174 | bool is_protocol_ready() const override; |
175 | ||
176 | bool is_protocol_standby() const override; | |
1e59de90 | 177 | |
aee94f69 TL |
178 | bool is_protocol_closed_clean() const override; |
179 | ||
180 | bool is_protocol_closed() const override; | |
1e59de90 TL |
181 | |
182 | // peer wins if myaddr > peeraddr | |
183 | bool peer_wins() const override; | |
184 | ||
185 | Interceptor *interceptor = nullptr; | |
186 | #else | |
187 | // peer wins if myaddr > peeraddr | |
188 | bool peer_wins() const; | |
189 | #endif | |
190 | ||
aee94f69 TL |
191 | private: |
192 | const seastar::shard_id msgr_sid; | |
193 | ||
194 | /* | |
195 | * Core owner is messenger core, may allow to access from the I/O core. | |
196 | */ | |
197 | SocketMessenger& messenger; | |
198 | ||
199 | std::unique_ptr<ProtocolV2> protocol; | |
200 | ||
201 | Socket *socket = nullptr; | |
202 | ||
203 | entity_name_t peer_name = {0, entity_name_t::NEW}; | |
204 | ||
205 | entity_addr_t peer_addr; | |
206 | ||
207 | // which of the peer_addrs we're connecting to (as client) | |
208 | // or should reconnect to (as peer) | |
209 | entity_addr_t target_addr; | |
210 | ||
211 | uint64_t features = 0; | |
212 | ||
213 | ceph::net::Policy<crimson::common::Throttle> policy; | |
214 | ||
215 | uint64_t peer_global_id = 0; | |
216 | ||
217 | /* | |
218 | * Core owner is I/O core (mutable). | |
219 | */ | |
220 | std::unique_ptr<ConnectionHandler> io_handler; | |
221 | ||
222 | /* | |
223 | * Core owner is up to the connection user. | |
224 | */ | |
225 | std::unique_ptr<user_private_t> user_private; | |
226 | ||
1e59de90 | 227 | friend class IOHandler; |
9f95a23c | 228 | friend class ProtocolV2; |
1e59de90 | 229 | friend class FrameAssemblerV2; |
11fdf7f2 TL |
230 | }; |
231 | ||
9f95a23c | 232 | } // namespace crimson::net |
1e59de90 TL |
233 | |
234 | #if FMT_VERSION >= 90000 | |
235 | template <> struct fmt::formatter<crimson::net::SocketConnection> : fmt::ostream_formatter {}; | |
236 | #endif |