Debian Codegraph @

src/core/lib/transport/tcp_connect_handshaker.cc

raw
1 //
2 //
3 // Copyright 2022 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18
19 #include <grpc/support/port_platform.h>
20
21 #include "src/core/lib/transport/tcp_connect_handshaker.h"
22
23 #include <memory>
24
25 #include "absl/base/thread_annotations.h"
26 #include "absl/status/status.h"
27 #include "absl/status/statusor.h"
28 #include "absl/strings/string_view.h"
29 #include "absl/types/optional.h"
30
31 #include <grpc/slice.h>
32 #include <grpc/slice_buffer.h>
33 #include <grpc/support/alloc.h>
34 #include <grpc/support/log.h>
35
36 #include "src/core/lib/address_utils/parse_address.h"
37 #include "src/core/lib/channel/channel_args.h"
38 #include "src/core/lib/config/core_configuration.h"
39 #include "src/core/lib/event_engine/channel_args_endpoint_config.h"
40 #include "src/core/lib/gprpp/debug_location.h"
41 #include "src/core/lib/gprpp/ref_counted_ptr.h"
42 #include "src/core/lib/gprpp/sync.h"
43 #include "src/core/lib/iomgr/closure.h"
44 #include "src/core/lib/iomgr/endpoint.h"
45 #include "src/core/lib/iomgr/error.h"
46 #include "src/core/lib/iomgr/exec_ctx.h"
47 #include "src/core/lib/iomgr/iomgr_fwd.h"
48 #include "src/core/lib/iomgr/polling_entity.h"
49 #include "src/core/lib/iomgr/pollset_set.h"
50 #include "src/core/lib/iomgr/resolved_address.h"
51 #include "src/core/lib/iomgr/tcp_client.h"
52 #include "src/core/lib/iomgr/tcp_server.h"
53 #include "src/core/lib/transport/handshaker.h"
54 #include "src/core/lib/transport/handshaker_factory.h"
55 #include "src/core/lib/transport/handshaker_registry.h"
56 #include "src/core/lib/uri/uri_parser.h"
57
58 namespace grpc_core {
59
60 namespace {
61
62 class TCPConnectHandshaker : public Handshaker {
63 public:
64 explicit TCPConnectHandshaker(grpc_pollset_set* pollset_set);
65 void Shutdown(grpc_error_handle why) override;
66 void DoHandshake(grpc_tcp_server_acceptor* /*acceptor*/,
67 grpc_closure* on_handshake_done,
68 HandshakerArgs* args) override;
69 const char* name() const override { return "tcp_connect"; }
70
71 private:
72 ~TCPConnectHandshaker() override;
73 void CleanupArgsForFailureLocked() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_);
74 void FinishLocked(grpc_error_handle error) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_);
75 static void Connected(void* arg, grpc_error_handle error);
76
77 Mutex mu_;
78 bool shutdown_ ABSL_GUARDED_BY(mu_) = false;
79 // Endpoint and read buffer to destroy after a shutdown.
80 grpc_endpoint* endpoint_to_destroy_ ABSL_GUARDED_BY(mu_) = nullptr;
81 grpc_slice_buffer* read_buffer_to_destroy_ ABSL_GUARDED_BY(mu_) = nullptr;
82 grpc_closure* on_handshake_done_ ABSL_GUARDED_BY(mu_) = nullptr;
83 grpc_pollset_set* interested_parties_ = nullptr;
84 grpc_polling_entity pollent_;
85 HandshakerArgs* args_ = nullptr;
86 bool bind_endpoint_to_pollset_ = false;
87 grpc_resolved_address addr_;
88 grpc_closure connected_;
89 };
90
91 TCPConnectHandshaker::TCPConnectHandshaker(grpc_pollset_set* pollset_set)
92 : interested_parties_(grpc_pollset_set_create()),
93 pollent_(grpc_polling_entity_create_from_pollset_set(pollset_set)) {
94 // Interested parties might be null for platforms like Apple.
95 // Explicitly check before adding/deleting from pollset_set to handle this
96 // use case.
97 if (interested_parties_ != nullptr) {
98 grpc_polling_entity_add_to_pollset_set(&pollent_, interested_parties_);
99 }
100 GRPC_CLOSURE_INIT(&connected_, Connected, this, grpc_schedule_on_exec_ctx);
101 }
102
103 void TCPConnectHandshaker::Shutdown(grpc_error_handle /*why*/) {
104 // TODO(anramach): After migration to EventEngine, cancel the in-progress
105 // TCP connection attempt.
106 {
107 MutexLock lock(&mu_);
108 if (!shutdown_) {
109 shutdown_ = true;
110 // If we are shutting down while connecting, respond back with
111 // handshake done.
112 // The callback from grpc_tcp_client_connect will perform
113 // the necessary clean up.
114 if (on_handshake_done_ != nullptr) {
115 CleanupArgsForFailureLocked();
116 FinishLocked(GRPC_ERROR_CREATE("tcp handshaker shutdown"));
117 }
118 }
119 }
120 }
121
122 void TCPConnectHandshaker::DoHandshake(grpc_tcp_server_acceptor* /*acceptor*/,
123 grpc_closure* on_handshake_done,
124 HandshakerArgs* args) {
125 {
126 MutexLock lock(&mu_);
127 on_handshake_done_ = on_handshake_done;
128 }
129 GPR_ASSERT(args->endpoint == nullptr);
130 args_ = args;
131 absl::StatusOr<URI> uri = URI::Parse(
132 args->args.GetString(GRPC_ARG_TCP_HANDSHAKER_RESOLVED_ADDRESS).value());
133 if (!uri.ok() || !grpc_parse_uri(*uri, &addr_)) {
134 MutexLock lock(&mu_);
135 FinishLocked(GRPC_ERROR_CREATE("Resolved address in invalid format"));
136 return;
137 }
138 bind_endpoint_to_pollset_ =
139 args->args.GetBool(GRPC_ARG_TCP_HANDSHAKER_BIND_ENDPOINT_TO_POLLSET)
140 .value_or(false);
141 // Update args to not contain the args relevant to TCP connect handshaker.
142 args->args = args->args.Remove(GRPC_ARG_TCP_HANDSHAKER_RESOLVED_ADDRESS)
143 .Remove(GRPC_ARG_TCP_HANDSHAKER_BIND_ENDPOINT_TO_POLLSET);
144 // In some implementations, the closure can be flushed before
145 // grpc_tcp_client_connect() returns, and since the closure requires access
146 // to mu_, this can result in a deadlock (see
147 // https://github.com/grpc/grpc/issues/16427 for details).
148 // grpc_tcp_client_connect() will fill endpoint_ with proper contents, and we
149 // make sure that we still exist at that point by taking a ref.
150 Ref().release(); // Ref held by callback.
151 // As we fake the TCP client connection failure when shutdown is called
152 // we don't want to pass args->endpoint directly.
153 // Instead pass endpoint_ and swap this endpoint to
154 // args endpoint on success.
155 // TODO(hork): use EventEngine::Connect if(IsEventEngineClientEnabled())
156 grpc_tcp_client_connect(
157 &connected_, &endpoint_to_destroy_, interested_parties_,
158 grpc_event_engine::experimental::ChannelArgsEndpointConfig(args->args),
159 &addr_, args->deadline);
160 }
161
162 void TCPConnectHandshaker::Connected(void* arg, grpc_error_handle error) {
163 RefCountedPtr<TCPConnectHandshaker> self(
164 static_cast<TCPConnectHandshaker*>(arg));
165 {
166 MutexLock lock(&self->mu_);
167 if (!error.ok() || self->shutdown_) {
168 if (error.ok()) {
169 error = GRPC_ERROR_CREATE("tcp handshaker shutdown");
170 }
171 if (self->endpoint_to_destroy_ != nullptr) {
172 grpc_endpoint_shutdown(self->endpoint_to_destroy_, error);
173 }
174 if (!self->shutdown_) {
175 self->CleanupArgsForFailureLocked();
176 self->shutdown_ = true;
177 self->FinishLocked(error);
178 } else {
179 // The on_handshake_done_ is already as part of shutdown when
180 // connecting So nothing to be done here other than unrefing the
181 // error.
182 }
183 return;
184 }
185 GPR_ASSERT(self->endpoint_to_destroy_ != nullptr);
186 self->args_->endpoint = self->endpoint_to_destroy_;
187 self->endpoint_to_destroy_ = nullptr;
188 if (self->bind_endpoint_to_pollset_) {
189 grpc_endpoint_add_to_pollset_set(self->args_->endpoint,
190 self->interested_parties_);
191 }
192 self->FinishLocked(absl::OkStatus());
193 }
194 }
195
196 TCPConnectHandshaker::~TCPConnectHandshaker() {
197 if (endpoint_to_destroy_ != nullptr) {
198 grpc_endpoint_destroy(endpoint_to_destroy_);
199 }
200 if (read_buffer_to_destroy_ != nullptr) {
201 grpc_slice_buffer_destroy(read_buffer_to_destroy_);
202 gpr_free(read_buffer_to_destroy_);
203 }
204 grpc_pollset_set_destroy(interested_parties_);
205 }
206
207 void TCPConnectHandshaker::CleanupArgsForFailureLocked() {
208 read_buffer_to_destroy_ = args_->read_buffer;
209 args_->read_buffer = nullptr;
210 args_->args = ChannelArgs();
211 }
212
213 void TCPConnectHandshaker::FinishLocked(grpc_error_handle error) {
214 if (interested_parties_ != nullptr) {
215 grpc_polling_entity_del_from_pollset_set(&pollent_, interested_parties_);
216 }
217 ExecCtx::Run(DEBUG_LOCATION, on_handshake_done_, error);
218 on_handshake_done_ = nullptr;
219 }
220
221 //
222 // TCPConnectHandshakerFactory
223 //
224
225 class TCPConnectHandshakerFactory : public HandshakerFactory {
226 public:
227 void AddHandshakers(const ChannelArgs& /*args*/,
228 grpc_pollset_set* interested_parties,
229 HandshakeManager* handshake_mgr) override {
230 handshake_mgr->Add(
231 MakeRefCounted<TCPConnectHandshaker>(interested_parties));
232 }
233 HandshakerPriority Priority() override {
234 return HandshakerPriority::kTCPConnectHandshakers;
235 }
236 ~TCPConnectHandshakerFactory() override = default;
237 };
238
239 } // namespace
240
241 void RegisterTCPConnectHandshaker(CoreConfiguration::Builder* builder) {
242 builder->handshaker_registry()->RegisterHandshakerFactory(
243 HANDSHAKER_CLIENT, std::make_unique<TCPConnectHandshakerFactory>());
244 }
245
246 } // namespace grpc_core