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 = (custom_tcp_endpoint*)s->endpoint;
77 grpc_resource_user_unref(tcp->resource_user);
78 delete tcp;
79 s->refs--;
80 if (s->refs == 0) {
81 grpc_custom_socket_vtable->destroy(s);
82 gpr_free(s);
83 }
84 }
85
86 #ifndef NDEBUG
87 #define TCP_UNREF(tcp, reason) tcp_unref((tcp), (reason), __FILE__, __LINE__)
88 #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)89 static void tcp_unref(custom_tcp_endpoint* tcp, const char* reason,
90 const char* file, int line) {
91 if (GRPC_TRACE_FLAG_ENABLED(grpc_tcp_trace)) {
92 gpr_atm val = gpr_atm_no_barrier_load(&tcp->refcount.count);
93 gpr_log(file, line, GPR_LOG_SEVERITY_ERROR,
94 "TCP unref %p : %s %" PRIdPTR " -> %" PRIdPTR, tcp->socket, reason,
95 val, val - 1);
96 }
97 if (gpr_unref(&tcp->refcount)) {
98 tcp_free(tcp->socket);
99 }
100 }
101
tcp_ref(custom_tcp_endpoint * tcp,const char * reason,const char * file,int line)102 static void tcp_ref(custom_tcp_endpoint* tcp, const char* reason,
103 const char* file, int line) {
104 if (GRPC_TRACE_FLAG_ENABLED(grpc_tcp_trace)) {
105 gpr_atm val = gpr_atm_no_barrier_load(&tcp->refcount.count);
106 gpr_log(file, line, GPR_LOG_SEVERITY_ERROR,
107 "TCP ref %p : %s %" PRIdPTR " -> %" PRIdPTR, tcp->socket, reason,
108 val, val + 1);
109 }
110 gpr_ref(&tcp->refcount);
111 }
112 #else
113 #define TCP_UNREF(tcp, reason) tcp_unref((tcp))
114 #define TCP_REF(tcp, reason) tcp_ref((tcp))
tcp_unref(custom_tcp_endpoint * tcp)115 static void tcp_unref(custom_tcp_endpoint* tcp) {
116 if (gpr_unref(&tcp->refcount)) {
117 tcp_free(tcp->socket);
118 }
119 }
120
tcp_ref(custom_tcp_endpoint * tcp)121 static void tcp_ref(custom_tcp_endpoint* tcp) { gpr_ref(&tcp->refcount); }
122 #endif
123
call_read_cb(custom_tcp_endpoint * tcp,grpc_error * error)124 static void call_read_cb(custom_tcp_endpoint* tcp, grpc_error* error) {
125 grpc_closure* cb = tcp->read_cb;
126 if (GRPC_TRACE_FLAG_ENABLED(grpc_tcp_trace)) {
127 gpr_log(GPR_INFO, "TCP:%p call_cb %p %p:%p", tcp->socket, cb, cb->cb,
128 cb->cb_arg);
129 size_t i;
130 const char* str = grpc_error_string(error);
131 gpr_log(GPR_INFO, "read: error=%s", str);
132
133 for (i = 0; i < tcp->read_slices->count; i++) {
134 char* dump = grpc_dump_slice(tcp->read_slices->slices[i],
135 GPR_DUMP_HEX | GPR_DUMP_ASCII);
136 gpr_log(GPR_INFO, "READ %p (peer=%s): %s", tcp, tcp->peer_string.c_str(),
137 dump);
138 gpr_free(dump);
139 }
140 }
141 TCP_UNREF(tcp, "read");
142 tcp->read_slices = nullptr;
143 tcp->read_cb = nullptr;
144 grpc_core::ExecCtx::Run(DEBUG_LOCATION, cb, error);
145 }
146
custom_read_callback(grpc_custom_socket * socket,size_t nread,grpc_error * error)147 static void custom_read_callback(grpc_custom_socket* socket, size_t nread,
148 grpc_error* error) {
149 grpc_core::ApplicationCallbackExecCtx callback_exec_ctx;
150 grpc_core::ExecCtx exec_ctx;
151 grpc_slice_buffer garbage;
152 custom_tcp_endpoint* tcp = (custom_tcp_endpoint*)socket->endpoint;
153 if (error == GRPC_ERROR_NONE && nread == 0) {
154 error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("EOF");
155 }
156 if (error == GRPC_ERROR_NONE) {
157 // Successful read
158 if ((size_t)nread < tcp->read_slices->length) {
159 /* TODO(murgatroid99): Instead of discarding the unused part of the read
160 * buffer, reuse it as the next read buffer. */
161 grpc_slice_buffer_init(&garbage);
162 grpc_slice_buffer_trim_end(
163 tcp->read_slices, tcp->read_slices->length - (size_t)nread, &garbage);
164 grpc_slice_buffer_reset_and_unref_internal(&garbage);
165 }
166 } else {
167 grpc_slice_buffer_reset_and_unref_internal(tcp->read_slices);
168 }
169 call_read_cb(tcp, error);
170 }
171
tcp_read_allocation_done(void * tcpp,grpc_error * error)172 static void tcp_read_allocation_done(void* tcpp, grpc_error* error) {
173 custom_tcp_endpoint* tcp = (custom_tcp_endpoint*)tcpp;
174 if (GRPC_TRACE_FLAG_ENABLED(grpc_tcp_trace)) {
175 gpr_log(GPR_INFO, "TCP:%p read_allocation_done: %s", tcp->socket,
176 grpc_error_string(error));
177 }
178 if (error == GRPC_ERROR_NONE) {
179 /* Before calling read, we allocate a buffer with exactly one slice
180 * to tcp->read_slices and wait for the callback indicating that the
181 * allocation was successful. So slices[0] should always exist here */
182 char* buffer = (char*)GRPC_SLICE_START_PTR(tcp->read_slices->slices[0]);
183 size_t len = GRPC_SLICE_LENGTH(tcp->read_slices->slices[0]);
184 grpc_custom_socket_vtable->read(tcp->socket, buffer, len,
185 custom_read_callback);
186 } else {
187 grpc_slice_buffer_reset_and_unref_internal(tcp->read_slices);
188 call_read_cb(tcp, GRPC_ERROR_REF(error));
189 }
190 if (GRPC_TRACE_FLAG_ENABLED(grpc_tcp_trace)) {
191 const char* str = grpc_error_string(error);
192 gpr_log(GPR_INFO, "Initiating read on %p: error=%s", tcp->socket, str);
193 }
194 }
195
endpoint_read(grpc_endpoint * ep,grpc_slice_buffer * read_slices,grpc_closure * cb,bool)196 static void endpoint_read(grpc_endpoint* ep, grpc_slice_buffer* read_slices,
197 grpc_closure* cb, bool /*urgent*/) {
198 custom_tcp_endpoint* tcp = (custom_tcp_endpoint*)ep;
199 GRPC_CUSTOM_IOMGR_ASSERT_SAME_THREAD();
200 GPR_ASSERT(tcp->read_cb == nullptr);
201 tcp->read_cb = cb;
202 tcp->read_slices = read_slices;
203 grpc_slice_buffer_reset_and_unref_internal(read_slices);
204 TCP_REF(tcp, "read");
205 if (grpc_resource_user_alloc_slices(&tcp->slice_allocator,
206 GRPC_TCP_DEFAULT_READ_SLICE_SIZE, 1,
207 tcp->read_slices)) {
208 tcp_read_allocation_done(tcp, GRPC_ERROR_NONE);
209 }
210 }
211
custom_write_callback(grpc_custom_socket * socket,grpc_error * error)212 static void custom_write_callback(grpc_custom_socket* socket,
213 grpc_error* error) {
214 grpc_core::ApplicationCallbackExecCtx callback_exec_ctx;
215 grpc_core::ExecCtx exec_ctx;
216 custom_tcp_endpoint* tcp = (custom_tcp_endpoint*)socket->endpoint;
217 grpc_closure* cb = tcp->write_cb;
218 tcp->write_cb = nullptr;
219 if (GRPC_TRACE_FLAG_ENABLED(grpc_tcp_trace)) {
220 const char* str = grpc_error_string(error);
221 gpr_log(GPR_INFO, "write complete on %p: error=%s", tcp->socket, str);
222 }
223 TCP_UNREF(tcp, "write");
224 grpc_core::ExecCtx::Run(DEBUG_LOCATION, cb, error);
225 }
226
endpoint_write(grpc_endpoint * ep,grpc_slice_buffer * write_slices,grpc_closure * cb,void *)227 static void endpoint_write(grpc_endpoint* ep, grpc_slice_buffer* write_slices,
228 grpc_closure* cb, void* /*arg*/) {
229 custom_tcp_endpoint* tcp = (custom_tcp_endpoint*)ep;
230 GRPC_CUSTOM_IOMGR_ASSERT_SAME_THREAD();
231
232 if (GRPC_TRACE_FLAG_ENABLED(grpc_tcp_trace)) {
233 size_t j;
234
235 for (j = 0; j < write_slices->count; j++) {
236 char* data = grpc_dump_slice(write_slices->slices[j],
237 GPR_DUMP_HEX | GPR_DUMP_ASCII);
238 gpr_log(GPR_INFO, "WRITE %p (peer=%s): %s", tcp->socket,
239 tcp->peer_string.c_str(), data);
240 gpr_free(data);
241 }
242 }
243
244 if (tcp->shutting_down) {
245 grpc_core::ExecCtx::Run(
246 DEBUG_LOCATION, cb,
247 GRPC_ERROR_CREATE_FROM_STATIC_STRING("TCP socket is shutting down"));
248 return;
249 }
250
251 GPR_ASSERT(tcp->write_cb == nullptr);
252 tcp->write_slices = write_slices;
253 GPR_ASSERT(tcp->write_slices->count <= UINT_MAX);
254 if (tcp->write_slices->count == 0) {
255 // No slices means we don't have to do anything,
256 // and libuv doesn't like empty writes
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 * why)286 static void endpoint_shutdown(grpc_endpoint* ep, grpc_error* why) {
287 custom_tcp_endpoint* tcp = (custom_tcp_endpoint*)ep;
288 if (!tcp->shutting_down) {
289 if (GRPC_TRACE_FLAG_ENABLED(grpc_tcp_trace)) {
290 const char* str = grpc_error_string(why);
291 gpr_log(GPR_INFO, "TCP %p shutdown why=%s", tcp->socket, 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_resource_user_shutdown(tcp->resource_user);
299 grpc_custom_socket_vtable->shutdown(tcp->socket);
300 }
301 GRPC_ERROR_UNREF(why);
302 }
303
custom_close_callback(grpc_custom_socket * socket)304 static void custom_close_callback(grpc_custom_socket* socket) {
305 socket->refs--;
306 if (socket->refs == 0) {
307 grpc_custom_socket_vtable->destroy(socket);
308 gpr_free(socket);
309 } else if (socket->endpoint) {
310 grpc_core::ApplicationCallbackExecCtx callback_exec_ctx;
311 grpc_core::ExecCtx exec_ctx;
312 custom_tcp_endpoint* tcp = (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 = (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 = (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 = (custom_tcp_endpoint*)ep;
329 return tcp->local_address;
330 }
331
endpoint_get_resource_user(grpc_endpoint * ep)332 static grpc_resource_user* endpoint_get_resource_user(grpc_endpoint* ep) {
333 custom_tcp_endpoint* tcp = (custom_tcp_endpoint*)ep;
334 return tcp->resource_user;
335 }
336
endpoint_get_fd(grpc_endpoint *)337 static int endpoint_get_fd(grpc_endpoint* /*ep*/) { return -1; }
338
endpoint_can_track_err(grpc_endpoint *)339 static bool endpoint_can_track_err(grpc_endpoint* /*ep*/) { return false; }
340
341 static grpc_endpoint_vtable vtable = {endpoint_read,
342 endpoint_write,
343 endpoint_add_to_pollset,
344 endpoint_add_to_pollset_set,
345 endpoint_delete_from_pollset_set,
346 endpoint_shutdown,
347 endpoint_destroy,
348 endpoint_get_resource_user,
349 endpoint_get_peer,
350 endpoint_get_local_address,
351 endpoint_get_fd,
352 endpoint_can_track_err};
353
custom_tcp_endpoint_create(grpc_custom_socket * socket,grpc_resource_quota * resource_quota,const char * peer_string)354 grpc_endpoint* custom_tcp_endpoint_create(grpc_custom_socket* socket,
355 grpc_resource_quota* resource_quota,
356 const char* peer_string) {
357 custom_tcp_endpoint* tcp = new custom_tcp_endpoint;
358 grpc_core::ApplicationCallbackExecCtx callback_exec_ctx;
359 grpc_core::ExecCtx exec_ctx;
360
361 if (GRPC_TRACE_FLAG_ENABLED(grpc_tcp_trace)) {
362 gpr_log(GPR_INFO, "Creating TCP endpoint %p", socket);
363 }
364 socket->refs++;
365 socket->endpoint = (grpc_endpoint*)tcp;
366 tcp->socket = socket;
367 tcp->base.vtable = &vtable;
368 gpr_ref_init(&tcp->refcount, 1);
369 tcp->peer_string = peer_string;
370 grpc_resolved_address resolved_local_addr;
371 resolved_local_addr.len = sizeof(resolved_local_addr.addr);
372 if (grpc_custom_socket_vtable->getsockname(
373 socket, reinterpret_cast<sockaddr*>(resolved_local_addr.addr),
374 reinterpret_cast<int*>(&resolved_local_addr.len)) !=
375 GRPC_ERROR_NONE) {
376 tcp->local_address = "";
377 } else {
378 tcp->local_address = grpc_sockaddr_to_uri(&resolved_local_addr);
379 }
380 tcp->shutting_down = false;
381 tcp->resource_user = grpc_resource_user_create(resource_quota, peer_string);
382 grpc_resource_user_slice_allocator_init(
383 &tcp->slice_allocator, tcp->resource_user, tcp_read_allocation_done, tcp);
384
385 return &tcp->base;
386 }
387