blob: 5fb398c50bd611dcace86c54d5501282018e1644 [file] [log] [blame]
murgatroid999030c812016-09-16 13:25:08 -07001/*
2 *
3 * Copyright 2016, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include "src/core/lib/iomgr/port.h"
35
36#ifdef GRPC_UV
37
38#include <limits.h>
39#include <string.h>
40
murgatroid99e2672c92016-11-09 15:12:22 -080041#include <grpc/slice_buffer.h>
42
murgatroid999030c812016-09-16 13:25:08 -070043#include <grpc/support/alloc.h>
44#include <grpc/support/log.h>
murgatroid999030c812016-09-16 13:25:08 -070045#include <grpc/support/string_util.h>
46
47#include "src/core/lib/iomgr/error.h"
48#include "src/core/lib/iomgr/network_status_tracker.h"
murgatroid99e2672c92016-11-09 15:12:22 -080049#include "src/core/lib/iomgr/resource_quota.h"
murgatroid999030c812016-09-16 13:25:08 -070050#include "src/core/lib/iomgr/tcp_uv.h"
Craig Tiller7c70b6c2017-01-23 07:48:42 -080051#include "src/core/lib/slice/slice_internal.h"
murgatroid99e2672c92016-11-09 15:12:22 -080052#include "src/core/lib/slice/slice_string_helpers.h"
murgatroid999030c812016-09-16 13:25:08 -070053#include "src/core/lib/support/string.h"
54
55int grpc_tcp_trace = 0;
56
57typedef struct {
58 grpc_endpoint base;
59 gpr_refcount refcount;
60
murgatroid9969259d42016-10-31 14:34:10 -070061 uv_write_t write_req;
62 uv_shutdown_t shutdown_req;
63
murgatroid999030c812016-09-16 13:25:08 -070064 uv_tcp_t *handle;
65
66 grpc_closure *read_cb;
67 grpc_closure *write_cb;
68
murgatroid99e2672c92016-11-09 15:12:22 -080069 grpc_slice read_slice;
70 grpc_slice_buffer *read_slices;
71 grpc_slice_buffer *write_slices;
murgatroid999030c812016-09-16 13:25:08 -070072 uv_buf_t *write_buffers;
73
murgatroid99e2672c92016-11-09 15:12:22 -080074 grpc_resource_user *resource_user;
murgatroid9969259d42016-10-31 14:34:10 -070075
murgatroid992c287ca2016-10-07 09:55:35 -070076 bool shutting_down;
murgatroid9969259d42016-10-31 14:34:10 -070077
murgatroid999030c812016-09-16 13:25:08 -070078 char *peer_string;
79 grpc_pollset *pollset;
80} grpc_tcp;
81
murgatroid99aa9c5782016-10-10 12:16:13 -070082static void uv_close_callback(uv_handle_t *handle) { gpr_free(handle); }
murgatroid999030c812016-09-16 13:25:08 -070083
murgatroid99e2672c92016-11-09 15:12:22 -080084static void tcp_free(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp) {
85 grpc_resource_user_unref(exec_ctx, tcp->resource_user);
murgatroid9969259d42016-10-31 14:34:10 -070086 gpr_free(tcp);
murgatroid9969259d42016-10-31 14:34:10 -070087}
murgatroid999030c812016-09-16 13:25:08 -070088
89/*#define GRPC_TCP_REFCOUNT_DEBUG*/
90#ifdef GRPC_TCP_REFCOUNT_DEBUG
murgatroid992e012342016-11-10 18:24:08 -080091#define TCP_UNREF(exec_ctx, tcp, reason) \
92 tcp_unref((exec_ctx), (tcp), (reason), __FILE__, __LINE__)
93#define TCP_REF(tcp, reason) \
94 tcp_ref((exec_ctx), (tcp), (reason), __FILE__, __LINE__)
95static void tcp_unref(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp,
96 const char *reason, const char *file, int line) {
murgatroid999030c812016-09-16 13:25:08 -070097 gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "TCP unref %p : %s %d -> %d", tcp,
98 reason, tcp->refcount.count, tcp->refcount.count - 1);
99 if (gpr_unref(&tcp->refcount)) {
murgatroid99e2672c92016-11-09 15:12:22 -0800100 tcp_free(exec_ctx, tcp);
murgatroid999030c812016-09-16 13:25:08 -0700101 }
102}
103
104static void tcp_ref(grpc_tcp *tcp, const char *reason, const char *file,
105 int line) {
106 gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "TCP ref %p : %s %d -> %d", tcp,
107 reason, tcp->refcount.count, tcp->refcount.count + 1);
108 gpr_ref(&tcp->refcount);
109}
110#else
murgatroid99e2672c92016-11-09 15:12:22 -0800111#define TCP_UNREF(exec_ctx, tcp, reason) tcp_unref((exec_ctx), (tcp))
murgatroid999030c812016-09-16 13:25:08 -0700112#define TCP_REF(tcp, reason) tcp_ref((tcp))
murgatroid99e2672c92016-11-09 15:12:22 -0800113static void tcp_unref(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp) {
murgatroid999030c812016-09-16 13:25:08 -0700114 if (gpr_unref(&tcp->refcount)) {
murgatroid99e2672c92016-11-09 15:12:22 -0800115 tcp_free(exec_ctx, tcp);
murgatroid999030c812016-09-16 13:25:08 -0700116 }
117}
118
119static void tcp_ref(grpc_tcp *tcp) { gpr_ref(&tcp->refcount); }
120#endif
121
murgatroid99dedb9232016-09-26 13:54:04 -0700122static void alloc_uv_buf(uv_handle_t *handle, size_t suggested_size,
123 uv_buf_t *buf) {
murgatroid9969259d42016-10-31 14:34:10 -0700124 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
murgatroid999030c812016-09-16 13:25:08 -0700125 grpc_tcp *tcp = handle->data;
126 (void)suggested_size;
murgatroid9969259d42016-10-31 14:34:10 -0700127 tcp->read_slice = grpc_resource_user_slice_malloc(
murgatroid99e2672c92016-11-09 15:12:22 -0800128 &exec_ctx, tcp->resource_user, GRPC_TCP_DEFAULT_READ_SLICE_SIZE);
Craig Tiller618e67d2016-10-26 21:08:10 -0700129 buf->base = (char *)GRPC_SLICE_START_PTR(tcp->read_slice);
130 buf->len = GRPC_SLICE_LENGTH(tcp->read_slice);
murgatroid9969259d42016-10-31 14:34:10 -0700131 grpc_exec_ctx_finish(&exec_ctx);
murgatroid999030c812016-09-16 13:25:08 -0700132}
133
murgatroid99dedb9232016-09-26 13:54:04 -0700134static void read_callback(uv_stream_t *stream, ssize_t nread,
135 const uv_buf_t *buf) {
murgatroid99e2672c92016-11-09 15:12:22 -0800136 grpc_slice sub;
murgatroid999030c812016-09-16 13:25:08 -0700137 grpc_error *error;
138 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
139 grpc_tcp *tcp = stream->data;
140 grpc_closure *cb = tcp->read_cb;
141 if (nread == 0) {
142 // Nothing happened. Wait for the next callback
143 return;
144 }
murgatroid99e2672c92016-11-09 15:12:22 -0800145 TCP_UNREF(&exec_ctx, tcp, "read");
murgatroid999030c812016-09-16 13:25:08 -0700146 tcp->read_cb = NULL;
147 // TODO(murgatroid99): figure out what the return value here means
148 uv_read_stop(stream);
149 if (nread == UV_EOF) {
150 error = GRPC_ERROR_CREATE("EOF");
151 } else if (nread > 0) {
152 // Successful read
murgatroid99e2672c92016-11-09 15:12:22 -0800153 sub = grpc_slice_sub_no_ref(tcp->read_slice, 0, (size_t)nread);
154 grpc_slice_buffer_add(tcp->read_slices, sub);
murgatroid999030c812016-09-16 13:25:08 -0700155 error = GRPC_ERROR_NONE;
156 if (grpc_tcp_trace) {
157 size_t i;
158 const char *str = grpc_error_string(error);
159 gpr_log(GPR_DEBUG, "read: error=%s", str);
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800160
murgatroid999030c812016-09-16 13:25:08 -0700161 for (i = 0; i < tcp->read_slices->count; i++) {
murgatroid99e2672c92016-11-09 15:12:22 -0800162 char *dump = grpc_dump_slice(tcp->read_slices->slices[i],
murgatroid992e012342016-11-10 18:24:08 -0800163 GPR_DUMP_HEX | GPR_DUMP_ASCII);
murgatroid99dedb9232016-09-26 13:54:04 -0700164 gpr_log(GPR_DEBUG, "READ %p (peer=%s): %s", tcp, tcp->peer_string,
165 dump);
murgatroid999030c812016-09-16 13:25:08 -0700166 gpr_free(dump);
167 }
168 }
169 } else {
170 // nread < 0: Error
171 error = GRPC_ERROR_CREATE("TCP Read failed");
172 }
Craig Tiller91031da2016-12-28 15:44:25 -0800173 grpc_closure_sched(&exec_ctx, cb, error);
murgatroid999030c812016-09-16 13:25:08 -0700174 grpc_exec_ctx_finish(&exec_ctx);
175}
176
177static void uv_endpoint_read(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
murgatroid99e2672c92016-11-09 15:12:22 -0800178 grpc_slice_buffer *read_slices, grpc_closure *cb) {
murgatroid999030c812016-09-16 13:25:08 -0700179 grpc_tcp *tcp = (grpc_tcp *)ep;
180 int status;
181 grpc_error *error = GRPC_ERROR_NONE;
182 GPR_ASSERT(tcp->read_cb == NULL);
183 tcp->read_cb = cb;
184 tcp->read_slices = read_slices;
Craig Tillerab7b2d82016-12-07 07:26:34 -0800185 grpc_slice_buffer_reset_and_unref_internal(exec_ctx, read_slices);
murgatroid999030c812016-09-16 13:25:08 -0700186 TCP_REF(tcp, "read");
187 // TODO(murgatroid99): figure out what the return value here means
murgatroid99dedb9232016-09-26 13:54:04 -0700188 status =
189 uv_read_start((uv_stream_t *)tcp->handle, alloc_uv_buf, read_callback);
murgatroid999030c812016-09-16 13:25:08 -0700190 if (status != 0) {
191 error = GRPC_ERROR_CREATE("TCP Read failed at start");
murgatroid99dedb9232016-09-26 13:54:04 -0700192 error =
193 grpc_error_set_str(error, GRPC_ERROR_STR_OS_ERROR, uv_strerror(status));
Craig Tiller91031da2016-12-28 15:44:25 -0800194 grpc_closure_sched(exec_ctx, cb, error);
murgatroid999030c812016-09-16 13:25:08 -0700195 }
196 if (grpc_tcp_trace) {
197 const char *str = grpc_error_string(error);
198 gpr_log(GPR_DEBUG, "Initiating read on %p: error=%s", tcp, str);
199 }
200}
201
202static void write_callback(uv_write_t *req, int status) {
203 grpc_tcp *tcp = req->data;
204 grpc_error *error;
205 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
206 grpc_closure *cb = tcp->write_cb;
207 tcp->write_cb = NULL;
murgatroid99e2672c92016-11-09 15:12:22 -0800208 TCP_UNREF(&exec_ctx, tcp, "write");
murgatroid999030c812016-09-16 13:25:08 -0700209 if (status == 0) {
210 error = GRPC_ERROR_NONE;
211 } else {
212 error = GRPC_ERROR_CREATE("TCP Write failed");
213 }
214 if (grpc_tcp_trace) {
215 const char *str = grpc_error_string(error);
216 gpr_log(GPR_DEBUG, "write complete on %p: error=%s", tcp, str);
217 }
218 gpr_free(tcp->write_buffers);
murgatroid99e2672c92016-11-09 15:12:22 -0800219 grpc_resource_user_free(&exec_ctx, tcp->resource_user,
murgatroid9969259d42016-10-31 14:34:10 -0700220 sizeof(uv_buf_t) * tcp->write_slices->count);
Craig Tiller91031da2016-12-28 15:44:25 -0800221 grpc_closure_sched(&exec_ctx, cb, error);
murgatroid999030c812016-09-16 13:25:08 -0700222 grpc_exec_ctx_finish(&exec_ctx);
223}
224
225static void uv_endpoint_write(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
murgatroid99e2672c92016-11-09 15:12:22 -0800226 grpc_slice_buffer *write_slices,
murgatroid999030c812016-09-16 13:25:08 -0700227 grpc_closure *cb) {
228 grpc_tcp *tcp = (grpc_tcp *)ep;
229 uv_buf_t *buffers;
230 unsigned int buffer_count;
231 unsigned int i;
murgatroid99e2672c92016-11-09 15:12:22 -0800232 grpc_slice *slice;
murgatroid999030c812016-09-16 13:25:08 -0700233 uv_write_t *write_req;
234
235 if (grpc_tcp_trace) {
murgatroid99c36f6ea2016-10-03 09:24:09 -0700236 size_t j;
murgatroid999030c812016-09-16 13:25:08 -0700237
murgatroid99c36f6ea2016-10-03 09:24:09 -0700238 for (j = 0; j < write_slices->count; j++) {
murgatroid99e2672c92016-11-09 15:12:22 -0800239 char *data = grpc_dump_slice(write_slices->slices[j],
murgatroid992e012342016-11-10 18:24:08 -0800240 GPR_DUMP_HEX | GPR_DUMP_ASCII);
murgatroid999030c812016-09-16 13:25:08 -0700241 gpr_log(GPR_DEBUG, "WRITE %p (peer=%s): %s", tcp, tcp->peer_string, data);
242 gpr_free(data);
243 }
244 }
245
246 if (tcp->shutting_down) {
Craig Tiller91031da2016-12-28 15:44:25 -0800247 grpc_closure_sched(exec_ctx, cb,
Craig Tillerfb27cac2017-01-03 09:54:51 -0800248 GRPC_ERROR_CREATE("TCP socket is shutting down"));
murgatroid999030c812016-09-16 13:25:08 -0700249 return;
250 }
251
252 GPR_ASSERT(tcp->write_cb == NULL);
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 // and libuv doesn't like empty writes
Craig Tiller91031da2016-12-28 15:44:25 -0800258 grpc_closure_sched(exec_ctx, cb, GRPC_ERROR_NONE);
murgatroid999030c812016-09-16 13:25:08 -0700259 return;
260 }
261
262 tcp->write_cb = cb;
263 buffer_count = (unsigned int)tcp->write_slices->count;
264 buffers = gpr_malloc(sizeof(uv_buf_t) * buffer_count);
murgatroid99e2672c92016-11-09 15:12:22 -0800265 grpc_resource_user_alloc(exec_ctx, tcp->resource_user,
murgatroid9969259d42016-10-31 14:34:10 -0700266 sizeof(uv_buf_t) * buffer_count, NULL);
murgatroid999030c812016-09-16 13:25:08 -0700267 for (i = 0; i < buffer_count; i++) {
268 slice = &tcp->write_slices->slices[i];
Craig Tiller618e67d2016-10-26 21:08:10 -0700269 buffers[i].base = (char *)GRPC_SLICE_START_PTR(*slice);
270 buffers[i].len = GRPC_SLICE_LENGTH(*slice);
murgatroid999030c812016-09-16 13:25:08 -0700271 }
murgatroid9969259d42016-10-31 14:34:10 -0700272 tcp->write_buffers = buffers;
273 write_req = &tcp->write_req;
murgatroid999030c812016-09-16 13:25:08 -0700274 write_req->data = tcp;
275 TCP_REF(tcp, "write");
276 // TODO(murgatroid99): figure out what the return value here means
277 uv_write(write_req, (uv_stream_t *)tcp->handle, buffers, buffer_count,
278 write_callback);
279}
280
281static void uv_add_to_pollset(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
282 grpc_pollset *pollset) {
283 // No-op. We're ignoring pollsets currently
murgatroid99dedb9232016-09-26 13:54:04 -0700284 (void)exec_ctx;
285 (void)ep;
286 (void)pollset;
287 grpc_tcp *tcp = (grpc_tcp *)ep;
murgatroid999030c812016-09-16 13:25:08 -0700288 tcp->pollset = pollset;
289}
290
291static void uv_add_to_pollset_set(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
292 grpc_pollset_set *pollset) {
293 // No-op. We're ignoring pollsets currently
murgatroid99dedb9232016-09-26 13:54:04 -0700294 (void)exec_ctx;
295 (void)ep;
296 (void)pollset;
murgatroid999030c812016-09-16 13:25:08 -0700297}
298
murgatroid9969259d42016-10-31 14:34:10 -0700299static void shutdown_callback(uv_shutdown_t *req, int status) {}
300
Craig Tiller22f13fb2017-01-27 11:43:25 -0800301static void uv_endpoint_shutdown(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
302 grpc_error *why) {
murgatroid999030c812016-09-16 13:25:08 -0700303 grpc_tcp *tcp = (grpc_tcp *)ep;
murgatroid992c287ca2016-10-07 09:55:35 -0700304 if (!tcp->shutting_down) {
305 tcp->shutting_down = true;
murgatroid9969259d42016-10-31 14:34:10 -0700306 uv_shutdown_t *req = &tcp->shutdown_req;
murgatroid992c287ca2016-10-07 09:55:35 -0700307 uv_shutdown(req, (uv_stream_t *)tcp->handle, shutdown_callback);
308 }
Craig Tiller22f13fb2017-01-27 11:43:25 -0800309 GRPC_ERROR_UNREF(why);
murgatroid999030c812016-09-16 13:25:08 -0700310}
311
312static void uv_destroy(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep) {
313 grpc_network_status_unregister_endpoint(ep);
314 grpc_tcp *tcp = (grpc_tcp *)ep;
murgatroid999030c812016-09-16 13:25:08 -0700315 uv_close((uv_handle_t *)tcp->handle, uv_close_callback);
murgatroid99e2672c92016-11-09 15:12:22 -0800316 TCP_UNREF(exec_ctx, tcp, "destroy");
murgatroid999030c812016-09-16 13:25:08 -0700317}
318
319static char *uv_get_peer(grpc_endpoint *ep) {
320 grpc_tcp *tcp = (grpc_tcp *)ep;
321 return gpr_strdup(tcp->peer_string);
322}
323
murgatroid9969259d42016-10-31 14:34:10 -0700324static grpc_resource_user *uv_get_resource_user(grpc_endpoint *ep) {
325 grpc_tcp *tcp = (grpc_tcp *)ep;
murgatroid99e2672c92016-11-09 15:12:22 -0800326 return tcp->resource_user;
murgatroid9969259d42016-10-31 14:34:10 -0700327}
328
murgatroid99dedb9232016-09-26 13:54:04 -0700329static grpc_workqueue *uv_get_workqueue(grpc_endpoint *ep) { return NULL; }
murgatroid999030c812016-09-16 13:25:08 -0700330
murgatroid992e012342016-11-10 18:24:08 -0800331static int uv_get_fd(grpc_endpoint *ep) { return -1; }
332
murgatroid9969259d42016-10-31 14:34:10 -0700333static grpc_endpoint_vtable vtable = {
334 uv_endpoint_read, uv_endpoint_write, uv_get_workqueue,
335 uv_add_to_pollset, uv_add_to_pollset_set, uv_endpoint_shutdown,
murgatroid992e012342016-11-10 18:24:08 -0800336 uv_destroy, uv_get_resource_user, uv_get_peer,
337 uv_get_fd};
murgatroid999030c812016-09-16 13:25:08 -0700338
murgatroid9969259d42016-10-31 14:34:10 -0700339grpc_endpoint *grpc_tcp_create(uv_tcp_t *handle,
340 grpc_resource_quota *resource_quota,
341 char *peer_string) {
murgatroid999030c812016-09-16 13:25:08 -0700342 grpc_tcp *tcp = (grpc_tcp *)gpr_malloc(sizeof(grpc_tcp));
343
344 if (grpc_tcp_trace) {
345 gpr_log(GPR_DEBUG, "Creating TCP endpoint %p", tcp);
346 }
347
murgatroid9904d28292016-10-27 14:36:57 -0700348 /* Disable Nagle's Algorithm */
murgatroid9912e57752016-10-27 14:30:41 -0700349 uv_tcp_nodelay(handle, 1);
350
murgatroid999030c812016-09-16 13:25:08 -0700351 memset(tcp, 0, sizeof(grpc_tcp));
352 tcp->base.vtable = &vtable;
353 tcp->handle = handle;
354 handle->data = tcp;
355 gpr_ref_init(&tcp->refcount, 1);
356 tcp->peer_string = gpr_strdup(peer_string);
murgatroid992c287ca2016-10-07 09:55:35 -0700357 tcp->shutting_down = false;
murgatroid99e2672c92016-11-09 15:12:22 -0800358 tcp->resource_user = grpc_resource_user_create(resource_quota, peer_string);
murgatroid999030c812016-09-16 13:25:08 -0700359 /* Tell network status tracking code about the new endpoint */
360 grpc_network_status_register_endpoint(&tcp->base);
361
362#ifndef GRPC_UV_TCP_HOLD_LOOP
363 uv_unref((uv_handle_t *)handle);
364#endif
365
366 return &tcp->base;
367}
368
369#endif /* GRPC_UV */