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