]>
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 | ||
17 | #include <map> | |
11fdf7f2 | 18 | #include <set> |
f67539c2 | 19 | #include <vector> |
11fdf7f2 TL |
20 | #include <seastar/core/gate.hh> |
21 | #include <seastar/core/reactor.hh> | |
22 | #include <seastar/core/sharded.hh> | |
9f95a23c | 23 | #include <seastar/core/shared_future.hh> |
11fdf7f2 | 24 | |
f67539c2 | 25 | #include "crimson/net/chained_dispatchers.h" |
11fdf7f2 | 26 | #include "Messenger.h" |
20effc67 | 27 | #include "Socket.h" |
11fdf7f2 TL |
28 | #include "SocketConnection.h" |
29 | ||
9f95a23c | 30 | namespace crimson::net { |
11fdf7f2 | 31 | |
aee94f69 | 32 | class ShardedServerSocket; |
9f95a23c TL |
33 | |
34 | class SocketMessenger final : public Messenger { | |
aee94f69 TL |
35 | // Messenger public interfaces |
36 | public: | |
11fdf7f2 TL |
37 | SocketMessenger(const entity_name_t& myname, |
38 | const std::string& logic_name, | |
aee94f69 TL |
39 | uint32_t nonce, |
40 | bool dispatch_only_on_this_shard); | |
41 | ||
20effc67 | 42 | ~SocketMessenger() override; |
11fdf7f2 | 43 | |
1e59de90 TL |
44 | const entity_name_t &get_myname() const override { |
45 | return my_name; | |
46 | } | |
47 | ||
48 | const entity_addrvec_t &get_myaddrs() const override { | |
49 | return my_addrs; | |
50 | } | |
51 | ||
52 | void set_myaddrs(const entity_addrvec_t& addr) override; | |
53 | ||
aee94f69 TL |
54 | bool set_addr_unknowns(const entity_addrvec_t &addr) override; |
55 | ||
1e59de90 | 56 | void set_auth_client(crimson::auth::AuthClient *ac) override { |
aee94f69 | 57 | assert(seastar::this_shard_id() == sid); |
1e59de90 TL |
58 | auth_client = ac; |
59 | } | |
60 | ||
61 | void set_auth_server(crimson::auth::AuthServer *as) override { | |
aee94f69 | 62 | assert(seastar::this_shard_id() == sid); |
1e59de90 TL |
63 | auth_server = as; |
64 | } | |
65 | ||
f67539c2 | 66 | bind_ertr::future<> bind(const entity_addrvec_t& addr) override; |
11fdf7f2 | 67 | |
f67539c2 | 68 | seastar::future<> start(const dispatchers_t& dispatchers) override; |
11fdf7f2 | 69 | |
9f95a23c | 70 | ConnectionRef connect(const entity_addr_t& peer_addr, |
f67539c2 | 71 | const entity_name_t& peer_name) override; |
1e59de90 TL |
72 | |
73 | bool owns_connection(Connection &conn) const override { | |
aee94f69 | 74 | assert(seastar::this_shard_id() == sid); |
1e59de90 TL |
75 | return this == &static_cast<SocketConnection&>(conn).get_messenger(); |
76 | } | |
77 | ||
11fdf7f2 TL |
78 | // can only wait once |
79 | seastar::future<> wait() override { | |
aee94f69 | 80 | assert(seastar::this_shard_id() == sid); |
11fdf7f2 TL |
81 | return shutdown_promise.get_future(); |
82 | } | |
83 | ||
f67539c2 | 84 | void stop() override { |
aee94f69 | 85 | assert(seastar::this_shard_id() == sid); |
f67539c2 TL |
86 | dispatchers.clear(); |
87 | } | |
88 | ||
89 | bool is_started() const override { | |
aee94f69 | 90 | assert(seastar::this_shard_id() == sid); |
f67539c2 TL |
91 | return !dispatchers.empty(); |
92 | } | |
93 | ||
11fdf7f2 TL |
94 | seastar::future<> shutdown() override; |
95 | ||
20effc67 | 96 | void print(std::ostream& out) const override { |
11fdf7f2 TL |
97 | out << get_myname() |
98 | << "(" << logic_name | |
99 | << ") " << get_myaddr(); | |
100 | } | |
101 | ||
9f95a23c TL |
102 | SocketPolicy get_policy(entity_type_t peer_type) const override; |
103 | ||
104 | SocketPolicy get_default_policy() const override; | |
105 | ||
11fdf7f2 TL |
106 | void set_default_policy(const SocketPolicy& p) override; |
107 | ||
108 | void set_policy(entity_type_t peer_type, const SocketPolicy& p) override; | |
109 | ||
110 | void set_policy_throttler(entity_type_t peer_type, Throttle* throttle) override; | |
111 | ||
aee94f69 TL |
112 | // SocketMessenger public interfaces |
113 | public: | |
114 | crimson::auth::AuthClient* get_auth_client() const { | |
115 | assert(seastar::this_shard_id() == sid); | |
116 | return auth_client; | |
117 | } | |
1e59de90 | 118 | |
aee94f69 TL |
119 | crimson::auth::AuthServer* get_auth_server() const { |
120 | assert(seastar::this_shard_id() == sid); | |
121 | return auth_server; | |
122 | } | |
1e59de90 TL |
123 | |
124 | uint32_t get_global_seq(uint32_t old=0); | |
125 | ||
126 | void learned_addr(const entity_addr_t &peer_addr_for_me, | |
127 | const SocketConnection& conn); | |
11fdf7f2 TL |
128 | |
129 | SocketConnectionRef lookup_conn(const entity_addr_t& addr); | |
aee94f69 | 130 | |
11fdf7f2 | 131 | void accept_conn(SocketConnectionRef); |
aee94f69 | 132 | |
11fdf7f2 | 133 | void unaccept_conn(SocketConnectionRef); |
aee94f69 | 134 | |
11fdf7f2 | 135 | void register_conn(SocketConnectionRef); |
aee94f69 | 136 | |
11fdf7f2 | 137 | void unregister_conn(SocketConnectionRef); |
aee94f69 | 138 | |
f67539c2 | 139 | void closing_conn(SocketConnectionRef); |
aee94f69 | 140 | |
f67539c2 | 141 | void closed_conn(SocketConnectionRef); |
1e59de90 | 142 | |
aee94f69 TL |
143 | seastar::shard_id get_shard_id() const { |
144 | return sid; | |
11fdf7f2 | 145 | } |
1e59de90 TL |
146 | |
147 | #ifdef UNIT_TESTS_BUILT | |
148 | void set_interceptor(Interceptor *i) override { | |
149 | interceptor = i; | |
150 | } | |
151 | ||
152 | Interceptor *interceptor = nullptr; | |
153 | #endif | |
aee94f69 TL |
154 | |
155 | private: | |
156 | seastar::future<> accept(SocketFRef &&, const entity_addr_t &); | |
157 | ||
158 | listen_ertr::future<> do_listen(const entity_addrvec_t& addr); | |
159 | ||
160 | /// try to bind to the first unused port of given address | |
161 | bind_ertr::future<> try_bind(const entity_addrvec_t& addr, | |
162 | uint32_t min_port, uint32_t max_port); | |
163 | ||
164 | const seastar::shard_id sid; | |
165 | // Distinguish messengers with meaningful names for debugging | |
166 | const std::string logic_name; | |
167 | const uint32_t nonce; | |
168 | const bool dispatch_only_on_sid; | |
169 | ||
170 | entity_name_t my_name; | |
171 | entity_addrvec_t my_addrs; | |
172 | crimson::auth::AuthClient* auth_client = nullptr; | |
173 | crimson::auth::AuthServer* auth_server = nullptr; | |
174 | ||
175 | ShardedServerSocket *listener = nullptr; | |
176 | ChainedDispatchers dispatchers; | |
177 | std::map<entity_addr_t, SocketConnectionRef> connections; | |
178 | std::set<SocketConnectionRef> accepting_conns; | |
179 | std::vector<SocketConnectionRef> closing_conns; | |
180 | ceph::net::PolicySet<Throttle> policy_set; | |
181 | // specifying we haven't learned our addr; set false when we find it. | |
182 | bool need_addr = true; | |
183 | uint32_t global_seq = 0; | |
184 | bool started = false; | |
185 | seastar::promise<> shutdown_promise; | |
11fdf7f2 TL |
186 | }; |
187 | ||
9f95a23c | 188 | } // namespace crimson::net |
1e59de90 TL |
189 | |
190 | #if FMT_VERSION >= 90000 | |
191 | template <> struct fmt::formatter<crimson::net::SocketMessenger> : fmt::ostream_formatter {}; | |
192 | #endif |