1 /*
2 *
3 * Copyright 2018 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/iomgr/port.h"
22
23 #include <limits.h>
24 #include <string.h>
25
26 #include <grpc/slice_buffer.h>
27
28 #include <grpc/support/alloc.h>
29 #include <grpc/support/log.h>
30 #include <grpc/support/string_util.h>
31
32 #include "src/core/lib/iomgr/error.h"
33 #include "src/core/lib/iomgr/iomgr_custom.h"
34 #include "src/core/lib/iomgr/resource_quota.h"
35 #include "src/core/lib/iomgr/sockaddr_utils.h"
36 #include "src/core/lib/iomgr/tcp_client.h"
37 #include "src/core/lib/iomgr/tcp_custom.h"
38 #include "src/core/lib/iomgr/tcp_server.h"
39 #include "src/core/lib/slice/slice_internal.h"
40 #include "src/core/lib/slice/slice_string_helpers.h"
41
42 #define GRPC_TCP_DEFAULT_READ_SLICE_SIZE 8192
43
44 extern grpc_core::TraceFlag grpc_tcp_trace;
45
46 grpc_socket_vtable* grpc_custom_socket_vtable = nullptr;
47 extern grpc_tcp_server_vtable custom_tcp_server_vtable;
48 extern grpc_tcp_client_vtable custom_tcp_client_vtable;
49
grpc_custom_endpoint_init(grpc_socket_vtable * impl)50 void grpc_custom_endpoint_init(grpc_socket_vtable* impl) {
51 grpc_custom_socket_vtable = impl;
52 grpc_set_tcp_client_impl(&custom_tcp_client_vtable);
53 grpc_set_tcp_server_impl(&custom_tcp_server_vtable);
54 }
55
56 struct custom_tcp_endpoint {
57 grpc_endpoint base;
58 gpr_refcount refcount;
59 grpc_custom_socket* socket;
60
61 grpc_closure* read_cb = nullptr;
62 grpc_closure* write_cb = nullptr;
63
64 grpc_slice_buffer* read_slices = nullptr;
65 grpc_slice_buffer* write_slices = nullptr;
66
67 grpc_resource_user* resource_user;
68 grpc_resource_user_slice_allocator slice_allocator;
69
70 bool shutting_down;
71
72 std::string peer_string;
73 std::string local_address;
74 };
tcp_free(grpc_custom_socket * s)75 static void tcp_free(grpc_custom_socket* s) {
76 custom_tcp_endpoint* tcp =
77 reinterpret_cast<custom_tcp_endpoint*>(s->endpoint);
78 grpc_resource_user_unref(tcp->resource_user);
79 delete tcp;
80 s->refs--;
81 if (s->refs == 0) {
82 grpc_custom_socket_vtable->destroy(s);
83 gpr_free(s);
84 }
85 }
86
87 #ifndef NDEBUG
88 #define TCP_UNREF(tcp, reason) tcp_unref((tcp), (reason), __FILE__, __LINE__)
89 #define TCP_REF(tcp, reason) tcp_ref((tcp), (reason), __FILE__, __LINE__)
tcp_unref(custom_tcp_endpoint * tcp,const char * reason,const char * file,int line)90 static void tcp_unref(custom_tcp_endpoint* tcp, const char* reason,
91 const char* file, int line) {
92 if (GRPC_TRACE_FLAG_ENABLED(grpc_tcp_trace)) {
93 gpr_atm val = gpr_atm_no_barrier_load(&tcp->refcount.count);
94 gpr_log(file, line, GPR_LOG_SEVERITY_ERROR,
95 "TCP unref %p : %s %" PRIdPTR " -> %" PRIdPTR, tcp->socket, reason,
96 val, val - 1);
97 }
98 if (gpr_unref(&tcp->refcount)) {
99 tcp_free(tcp->socket);
100 }
101 }
102
tcp_ref(custom_tcp_endpoint * tcp,const char * reason,const char * file,int line)103 static void tcp_ref(custom_tcp_endpoint* tcp, const char* reason,
104 const char* file, int line) {
105 if (GRPC_TRACE_FLAG_ENABLED(grpc_tcp_trace)) {
106 gpr_atm val = gpr_atm_no_barrier_load(&tcp->refcount.count);
107 gpr_log(file, line, GPR_LOG_SEVERITY_ERROR,
108 "TCP ref %p : %s %" PRIdPTR " -> %" PRIdPTR, tcp->socket, reason,
109 val, val + 1);
110 }
111 gpr_ref(&tcp->refcount);
112 }
113 #else
114 #define TCP_UNREF(tcp, reason) tcp_unref((tcp))
115 #define TCP_REF(tcp, reason) tcp_ref((tcp))
tcp_unref(custom_tcp_endpoint * tcp)116 static void tcp_unref(custom_tcp_endpoint* tcp) {
117 if (gpr_unref(&tcp->refcount)) {
118 tcp_free(tcp->socket);
119 }
120 }
121
tcp_ref(custom_tcp_endpoint * tcp)122 static void tcp_ref(custom_tcp_endpoint* tcp) { gpr_ref(&tcp->refcount); }
123 #endif
124
call_read_cb(custom_tcp_endpoint * tcp,grpc_error * error)125 static void call_read_cb(custom_tcp_endpoint* tcp, grpc_error* error) {
126 grpc_closure* cb = tcp->read_cb;
127 if (GRPC_TRACE_FLAG_ENABLED(grpc_tcp_trace)) {
128 gpr_log(GPR_INFO, "TCP:%p call_cb %p %p:%p", tcp->socket, cb, cb->cb,
129 cb->cb_arg);
130 size_t i;
131 const char* str = grpc_error_string(error);
132 gpr_log(GPR_INFO, "read: error=%s", str);
133
134 for (i = 0; i < tcp->read_slices->count; i++) {
135 char* dump = grpc_dump_slice(tcp->read_slices->slices[i],
136 GPR_DUMP_HEX | GPR_DUMP_ASCII);
137 gpr_log(GPR_INFO, "READ %p (peer=%s): %s", tcp, tcp->peer_string.c_str(),
138 dump);
139 gpr_free(dump);
140 }
141 }
142 TCP_UNREF(tcp, "read");
143 tcp->read_slices = nullptr;
144 tcp->read_cb = nullptr;
145 grpc_core::ExecCtx::Run(DEBUG_LOCATION, cb, error);
146 }
147
custom_read_callback(grpc_custom_socket * socket,size_t nread,grpc_error * error)148 static void custom_read_callback(grpc_custom_socket* socket, size_t nread,
149 grpc_error* error) {
150 grpc_core::ApplicationCallbackExecCtx callback_exec_ctx;
151 grpc_core::ExecCtx exec_ctx;
152 grpc_slice_buffer garbage;
153 custom_tcp_endpoint* tcp =
154 reinterpret_cast<custom_tcp_endpoint*>(socket->endpoint);
155 if (error == GRPC_ERROR_NONE && nread == 0) {
156 error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("EOF");
157 }
158 if (error == GRPC_ERROR_NONE) {
159 // Successful read
160 if (nread < tcp->read_slices->length) {
161 /* TODO(murgatroid99): Instead of discarding the unused part of the read
162 * buffer, reuse it as the next read buffer. */
163 grpc_slice_buffer_init(&garbage);
164 grpc_slice_buffer_trim_end(tcp->read_slices,
165 tcp->read_slices->length - nread, &garbage);
166 grpc_slice_buffer_reset_and_unref_internal(&garbage);
167 }
168 } else {
169 grpc_slice_buffer_reset_and_unref_internal(tcp->read_slices);
170 }
171 call_read_cb(tcp, error);
172 }
173
tcp_read_allocation_done(void * tcpp,grpc_error * error)174 static void tcp_read_allocation_done(void* tcpp, grpc_error* error) {
175 custom_tcp_endpoint* tcp = static_cast<custom_tcp_endpoint*>(tcpp);
176 if (GRPC_TRACE_FLAG_ENABLED(grpc_tcp_trace)) {
177 gpr_log(GPR_INFO, "TCP:%p read_allocation_done: %s", tcp->socket,
178 grpc_error_string(error));
179 }
180 if (error == GRPC_ERROR_NONE) {
181 /* Before calling read, we allocate a buffer with exactly one slice
182 * to tcp->read_slices and wait for the callback indicating that the
183 * allocation was successful. So slices[0] should always exist here */
184 char* buffer = reinterpret_cast<char*>(
185 GRPC_SLICE_START_PTR(tcp->read_slices->slices[0]));
186 size_t len = GRPC_SLICE_LENGTH(tcp->read_slices->slices[0]);
187 grpc_custom_socket_vtable->read(tcp->socket, buffer, len,
188 custom_read_callback);
189 } else {
190 grpc_slice_buffer_reset_and_unref_internal(tcp->read_slices);
191 call_read_cb(tcp, GRPC_ERROR_REF(error));
192 }
193 if (GRPC_TRACE_FLAG_ENABLED(grpc_tcp_trace)) {
194 const char* str = grpc_error_string(error);
195 gpr_log(GPR_INFO, "Initiating read on %p: error=%s", tcp->socket, str);
196 }
197 }
198
endpoint_read(grpc_endpoint * ep,grpc_slice_buffer * read_slices,grpc_closure * cb,bool)199 static void endpoint_read(grpc_endpoint* ep, grpc_slice_buffer* read_slices,
200 grpc_closure* cb, bool /*urgent*/) {
201 custom_tcp_endpoint* tcp = reinterpret_cast<custom_tcp_endpoint*>(ep);
202 GRPC_CUSTOM_IOMGR_ASSERT_SAME_THREAD();
203 GPR_ASSERT(tcp->read_cb == nullptr);
204 tcp->read_cb = cb;
205 tcp->read_slices = read_slices;
206 grpc_slice_buffer_reset_and_unref_internal(read_slices);
207 TCP_REF(tcp, "read");
208 if (grpc_resource_user_alloc_slices(&tcp->slice_allocator,
209 GRPC_TCP_DEFAULT_READ_SLICE_SIZE, 1,
210 tcp->read_slices)) {
211 tcp_read_allocation_done(tcp, GRPC_ERROR_NONE);
212 }
213 }
214
custom_write_callback(grpc_custom_socket * socket,grpc_error * error)215 static void custom_write_callback(grpc_custom_socket* socket,
216 grpc_error* error) {
217 grpc_core::ApplicationCallbackExecCtx callback_exec_ctx;
218 grpc_core::ExecCtx exec_ctx;
219 custom_tcp_endpoint* tcp =
220 reinterpret_cast<custom_tcp_endpoint*>(socket->endpoint);
221 grpc_closure* cb = tcp->write_cb;
222 tcp->write_cb = nullptr;
223 if (GRPC_TRACE_FLAG_ENABLED(grpc_tcp_trace)) {
224 const char* str = grpc_error_string(error);
225 gpr_log(GPR_INFO, "write complete on %p: error=%s", tcp->socket, str);
226 }
227 TCP_UNREF(tcp, "write");
228 grpc_core::ExecCtx::Run(DEBUG_LOCATION, cb, error);
229 }
230
endpoint_write(grpc_endpoint * ep,grpc_slice_buffer * write_slices,grpc_closure * cb,void *)231 static void endpoint_write(grpc_endpoint* ep, grpc_slice_buffer* write_slices,
232 grpc_closure* cb, void* /*arg*/) {
233 custom_tcp_endpoint* tcp = reinterpret_cast<custom_tcp_endpoint*>(ep);
234 GRPC_CUSTOM_IOMGR_ASSERT_SAME_THREAD();
235
236 if (GRPC_TRACE_FLAG_ENABLED(grpc_tcp_trace)) {
237 size_t j;
238
239 for (j = 0; j < write_slices->count; j++) {
240 char* data = grpc_dump_slice(write_slices->slices[j],
241 GPR_DUMP_HEX | GPR_DUMP_ASCII);
242 gpr_log(GPR_INFO, "WRITE %p (peer=%s): %s", tcp->socket,
243 tcp->peer_string.c_str(), data);
244 gpr_free(data);
245 }
246 }
247
248 if (tcp->shutting_down) {
249 grpc_core::ExecCtx::Run(
250 DEBUG_LOCATION, cb,
251 GRPC_ERROR_CREATE_FROM_STATIC_STRING("TCP socket is shutting down"));
252 return;
253 }
254
255 GPR_ASSERT(tcp->write_cb == nullptr);
256 tcp->write_slices = write_slices;
257 GPR_ASSERT(tcp->write_slices->count <= UINT_MAX);
258 if (tcp->write_slices->count == 0) {
259 // No slices means we don't have to do anything,
260 // and libuv doesn't like empty writes
261 grpc_core::ExecCtx::Run(DEBUG_LOCATION, cb, GRPC_ERROR_NONE);
262 return;
263 }
264 tcp->write_cb = cb;
265 TCP_REF(tcp, "write");
266 grpc_custom_socket_vtable->write(tcp->socket, tcp->write_slices,
267 custom_write_callback);
268 }
269
endpoint_add_to_pollset(grpc_endpoint * ep,grpc_pollset * pollset)270 static void endpoint_add_to_pollset(grpc_endpoint* ep, grpc_pollset* pollset) {
271 // No-op. We're ignoring pollsets currently
272 (void)ep;
273 (void)pollset;
274 }
275
endpoint_add_to_pollset_set(grpc_endpoint * ep,grpc_pollset_set * pollset)276 static void endpoint_add_to_pollset_set(grpc_endpoint* ep,
277 grpc_pollset_set* pollset) {
278 // No-op. We're ignoring pollsets currently
279 (void)ep;
280 (void)pollset;
281 }
282
endpoint_delete_from_pollset_set(grpc_endpoint * ep,grpc_pollset_set * pollset)283 static void endpoint_delete_from_pollset_set(grpc_endpoint* ep,
284 grpc_pollset_set* pollset) {
285 // No-op. We're ignoring pollsets currently
286 (void)ep;
287 (void)pollset;
288 }
289
endpoint_shutdown(grpc_endpoint * ep,grpc_error * why)290 static void endpoint_shutdown(grpc_endpoint* ep, grpc_error* why) {
291 custom_tcp_endpoint* tcp = reinterpret_cast<custom_tcp_endpoint*>(ep);
292 if (!tcp->shutting_down) {
293 if (GRPC_TRACE_FLAG_ENABLED(grpc_tcp_trace)) {
294 const char* str = grpc_error_string(why);
295 gpr_log(GPR_INFO, "TCP %p shutdown why=%s", tcp->socket, str);
296 }
297 tcp->shutting_down = true;
298 // grpc_core::ExecCtx::Run(DEBUG_LOCATION,tcp->read_cb,
299 // GRPC_ERROR_REF(why));
300 // grpc_core::ExecCtx::Run(DEBUG_LOCATION,tcp->write_cb,
301 // GRPC_ERROR_REF(why)); tcp->read_cb = nullptr; tcp->write_cb = nullptr;
302 grpc_resource_user_shutdown(tcp->resource_user);
303 grpc_custom_socket_vtable->shutdown(tcp->socket);
304 }
305 GRPC_ERROR_UNREF(why);
306 }
307
custom_close_callback(grpc_custom_socket * socket)308 static void custom_close_callback(grpc_custom_socket* socket) {
309 socket->refs--;
310 if (socket->refs == 0) {
311 grpc_custom_socket_vtable->destroy(socket);
312 gpr_free(socket);
313 } else if (socket->endpoint) {
314 grpc_core::ApplicationCallbackExecCtx callback_exec_ctx;
315 grpc_core::ExecCtx exec_ctx;
316 custom_tcp_endpoint* tcp =
317 reinterpret_cast<custom_tcp_endpoint*>(socket->endpoint);
318 TCP_UNREF(tcp, "destroy");
319 }
320 }
321
endpoint_destroy(grpc_endpoint * ep)322 static void endpoint_destroy(grpc_endpoint* ep) {
323 custom_tcp_endpoint* tcp = reinterpret_cast<custom_tcp_endpoint*>(ep);
324 grpc_custom_socket_vtable->close(tcp->socket, custom_close_callback);
325 }
326
endpoint_get_peer(grpc_endpoint * ep)327 static absl::string_view endpoint_get_peer(grpc_endpoint* ep) {
328 custom_tcp_endpoint* tcp = reinterpret_cast<custom_tcp_endpoint*>(ep);
329 return tcp->peer_string;
330 }
331
endpoint_get_local_address(grpc_endpoint * ep)332 static absl::string_view endpoint_get_local_address(grpc_endpoint* ep) {
333 custom_tcp_endpoint* tcp = reinterpret_cast<custom_tcp_endpoint*>(ep);
334 return tcp->local_address;
335 }
336
endpoint_get_resource_user(grpc_endpoint * ep)337 static grpc_resource_user* endpoint_get_resource_user(grpc_endpoint* ep) {
338 custom_tcp_endpoint* tcp = reinterpret_cast<custom_tcp_endpoint*>(ep);
339 return tcp->resource_user;
340 }
341
endpoint_get_fd(grpc_endpoint *)342 static int endpoint_get_fd(grpc_endpoint* /*ep*/) { return -1; }
343
endpoint_can_track_err(grpc_endpoint *)344 static bool endpoint_can_track_err(grpc_endpoint* /*ep*/) { return false; }
345
346 static grpc_endpoint_vtable vtable = {endpoint_read,
347 endpoint_write,
348 endpoint_add_to_pollset,
349 endpoint_add_to_pollset_set,
350 endpoint_delete_from_pollset_set,
351 endpoint_shutdown,
352 endpoint_destroy,
353 endpoint_get_resource_user,
354 endpoint_get_peer,
355 endpoint_get_local_address,
356 endpoint_get_fd,
357 endpoint_can_track_err};
358
custom_tcp_endpoint_create(grpc_custom_socket * socket,grpc_resource_quota * resource_quota,const char * peer_string)359 grpc_endpoint* custom_tcp_endpoint_create(grpc_custom_socket* socket,
360 grpc_resource_quota* resource_quota,
361 const char* peer_string) {
362 custom_tcp_endpoint* tcp = new custom_tcp_endpoint;
363 grpc_core::ApplicationCallbackExecCtx callback_exec_ctx;
364 grpc_core::ExecCtx exec_ctx;
365
366 if (GRPC_TRACE_FLAG_ENABLED(grpc_tcp_trace)) {
367 gpr_log(GPR_INFO, "Creating TCP endpoint %p", socket);
368 }
369 socket->refs++;
370 socket->endpoint = reinterpret_cast<grpc_endpoint*>(tcp);
371 tcp->socket = socket;
372 tcp->base.vtable = &vtable;
373 gpr_ref_init(&tcp->refcount, 1);
374 tcp->peer_string = peer_string;
375 grpc_resolved_address resolved_local_addr;
376 resolved_local_addr.len = sizeof(resolved_local_addr.addr);
377 if (grpc_custom_socket_vtable->getsockname(
378 socket, reinterpret_cast<sockaddr*>(resolved_local_addr.addr),
379 reinterpret_cast<int*>(&resolved_local_addr.len)) !=
380 GRPC_ERROR_NONE) {
381 tcp->local_address = "";
382 } else {
383 tcp->local_address = grpc_sockaddr_to_uri(&resolved_local_addr);
384 }
385 tcp->shutting_down = false;
386 tcp->resource_user = grpc_resource_user_create(resource_quota, peer_string);
387 grpc_resource_user_slice_allocator_init(
388 &tcp->slice_allocator, tcp->resource_user, tcp_read_allocation_done, tcp);
389
390 return &tcp->base;
391 }
392