blob: 4be95457d5acf19255fe11672af5057a5bf7176e [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"
murgatroid99e2672c92016-11-09 15:12:22 -080051#include "src/core/lib/slice/slice_string_helpers.h"
murgatroid999030c812016-09-16 13:25:08 -070052#include "src/core/lib/support/string.h"
53
54int grpc_tcp_trace = 0;
55
56typedef struct {
57 grpc_endpoint base;
58 gpr_refcount refcount;
59
murgatroid9969259d42016-10-31 14:34:10 -070060 uv_write_t write_req;
61 uv_shutdown_t shutdown_req;
62
murgatroid999030c812016-09-16 13:25:08 -070063 uv_tcp_t *handle;
64
65 grpc_closure *read_cb;
66 grpc_closure *write_cb;
67
murgatroid99e2672c92016-11-09 15:12:22 -080068 grpc_slice read_slice;
69 grpc_slice_buffer *read_slices;
70 grpc_slice_buffer *write_slices;
murgatroid999030c812016-09-16 13:25:08 -070071 uv_buf_t *write_buffers;
72
murgatroid99e2672c92016-11-09 15:12:22 -080073 grpc_resource_user *resource_user;
murgatroid9969259d42016-10-31 14:34:10 -070074
murgatroid992c287ca2016-10-07 09:55:35 -070075 bool shutting_down;
murgatroid9969259d42016-10-31 14:34:10 -070076
murgatroid999030c812016-09-16 13:25:08 -070077 char *peer_string;
78 grpc_pollset *pollset;
79} grpc_tcp;
80
murgatroid99aa9c5782016-10-10 12:16:13 -070081static void uv_close_callback(uv_handle_t *handle) { gpr_free(handle); }
murgatroid999030c812016-09-16 13:25:08 -070082
murgatroid99e2672c92016-11-09 15:12:22 -080083static void tcp_free(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp) {
84 grpc_resource_user_unref(exec_ctx, tcp->resource_user);
murgatroid9969259d42016-10-31 14:34:10 -070085 gpr_free(tcp);
murgatroid9969259d42016-10-31 14:34:10 -070086}
murgatroid999030c812016-09-16 13:25:08 -070087
88/*#define GRPC_TCP_REFCOUNT_DEBUG*/
89#ifdef GRPC_TCP_REFCOUNT_DEBUG
murgatroid992e012342016-11-10 18:24:08 -080090#define TCP_UNREF(exec_ctx, tcp, reason) \
91 tcp_unref((exec_ctx), (tcp), (reason), __FILE__, __LINE__)
92#define TCP_REF(tcp, reason) \
93 tcp_ref((exec_ctx), (tcp), (reason), __FILE__, __LINE__)
94static void tcp_unref(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp,
95 const char *reason, const char *file, int line) {
murgatroid999030c812016-09-16 13:25:08 -070096 gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "TCP unref %p : %s %d -> %d", tcp,
97 reason, tcp->refcount.count, tcp->refcount.count - 1);
98 if (gpr_unref(&tcp->refcount)) {
murgatroid99e2672c92016-11-09 15:12:22 -080099 tcp_free(exec_ctx, tcp);
murgatroid999030c812016-09-16 13:25:08 -0700100 }
101}
102
103static void tcp_ref(grpc_tcp *tcp, const char *reason, const char *file,
104 int line) {
105 gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "TCP ref %p : %s %d -> %d", tcp,
106 reason, tcp->refcount.count, tcp->refcount.count + 1);
107 gpr_ref(&tcp->refcount);
108}
109#else
murgatroid99e2672c92016-11-09 15:12:22 -0800110#define TCP_UNREF(exec_ctx, tcp, reason) tcp_unref((exec_ctx), (tcp))
murgatroid999030c812016-09-16 13:25:08 -0700111#define TCP_REF(tcp, reason) tcp_ref((tcp))
murgatroid99e2672c92016-11-09 15:12:22 -0800112static void tcp_unref(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp) {
murgatroid999030c812016-09-16 13:25:08 -0700113 if (gpr_unref(&tcp->refcount)) {
murgatroid99e2672c92016-11-09 15:12:22 -0800114 tcp_free(exec_ctx, tcp);
murgatroid999030c812016-09-16 13:25:08 -0700115 }
116}
117
118static void tcp_ref(grpc_tcp *tcp) { gpr_ref(&tcp->refcount); }
119#endif
120
murgatroid99dedb9232016-09-26 13:54:04 -0700121static void alloc_uv_buf(uv_handle_t *handle, size_t suggested_size,
122 uv_buf_t *buf) {
murgatroid9969259d42016-10-31 14:34:10 -0700123 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
murgatroid999030c812016-09-16 13:25:08 -0700124 grpc_tcp *tcp = handle->data;
125 (void)suggested_size;
murgatroid9969259d42016-10-31 14:34:10 -0700126 tcp->read_slice = grpc_resource_user_slice_malloc(
murgatroid99e2672c92016-11-09 15:12:22 -0800127 &exec_ctx, tcp->resource_user, GRPC_TCP_DEFAULT_READ_SLICE_SIZE);
Craig Tiller618e67d2016-10-26 21:08:10 -0700128 buf->base = (char *)GRPC_SLICE_START_PTR(tcp->read_slice);
129 buf->len = GRPC_SLICE_LENGTH(tcp->read_slice);
murgatroid9969259d42016-10-31 14:34:10 -0700130 grpc_exec_ctx_finish(&exec_ctx);
murgatroid999030c812016-09-16 13:25:08 -0700131}
132
murgatroid99dedb9232016-09-26 13:54:04 -0700133static void read_callback(uv_stream_t *stream, ssize_t nread,
134 const uv_buf_t *buf) {
murgatroid99e2672c92016-11-09 15:12:22 -0800135 grpc_slice sub;
murgatroid999030c812016-09-16 13:25:08 -0700136 grpc_error *error;
137 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
138 grpc_tcp *tcp = stream->data;
139 grpc_closure *cb = tcp->read_cb;
140 if (nread == 0) {
141 // Nothing happened. Wait for the next callback
142 return;
143 }
murgatroid99e2672c92016-11-09 15:12:22 -0800144 TCP_UNREF(&exec_ctx, tcp, "read");
murgatroid999030c812016-09-16 13:25:08 -0700145 tcp->read_cb = NULL;
146 // TODO(murgatroid99): figure out what the return value here means
147 uv_read_stop(stream);
148 if (nread == UV_EOF) {
149 error = GRPC_ERROR_CREATE("EOF");
150 } else if (nread > 0) {
151 // Successful read
murgatroid99e2672c92016-11-09 15:12:22 -0800152 sub = grpc_slice_sub_no_ref(tcp->read_slice, 0, (size_t)nread);
153 grpc_slice_buffer_add(tcp->read_slices, sub);
murgatroid999030c812016-09-16 13:25:08 -0700154 error = GRPC_ERROR_NONE;
155 if (grpc_tcp_trace) {
156 size_t i;
157 const char *str = grpc_error_string(error);
158 gpr_log(GPR_DEBUG, "read: error=%s", str);
159 grpc_error_free_string(str);
160 for (i = 0; i < tcp->read_slices->count; i++) {
murgatroid99e2672c92016-11-09 15:12:22 -0800161 char *dump = grpc_dump_slice(tcp->read_slices->slices[i],
murgatroid992e012342016-11-10 18:24:08 -0800162 GPR_DUMP_HEX | GPR_DUMP_ASCII);
murgatroid99dedb9232016-09-26 13:54:04 -0700163 gpr_log(GPR_DEBUG, "READ %p (peer=%s): %s", tcp, tcp->peer_string,
164 dump);
murgatroid999030c812016-09-16 13:25:08 -0700165 gpr_free(dump);
166 }
167 }
168 } else {
169 // nread < 0: Error
170 error = GRPC_ERROR_CREATE("TCP Read failed");
171 }
Craig Tiller91031da2016-12-28 15:44:25 -0800172 grpc_closure_sched(&exec_ctx, cb, error);
murgatroid999030c812016-09-16 13:25:08 -0700173 grpc_exec_ctx_finish(&exec_ctx);
174}
175
176static void uv_endpoint_read(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
murgatroid99e2672c92016-11-09 15:12:22 -0800177 grpc_slice_buffer *read_slices, grpc_closure *cb) {
murgatroid999030c812016-09-16 13:25:08 -0700178 grpc_tcp *tcp = (grpc_tcp *)ep;
179 int status;
180 grpc_error *error = GRPC_ERROR_NONE;
181 GPR_ASSERT(tcp->read_cb == NULL);
182 tcp->read_cb = cb;
183 tcp->read_slices = read_slices;
murgatroid99e2672c92016-11-09 15:12:22 -0800184 grpc_slice_buffer_reset_and_unref(read_slices);
murgatroid999030c812016-09-16 13:25:08 -0700185 TCP_REF(tcp, "read");
186 // TODO(murgatroid99): figure out what the return value here means
murgatroid99dedb9232016-09-26 13:54:04 -0700187 status =
188 uv_read_start((uv_stream_t *)tcp->handle, alloc_uv_buf, read_callback);
murgatroid999030c812016-09-16 13:25:08 -0700189 if (status != 0) {
190 error = GRPC_ERROR_CREATE("TCP Read failed at start");
murgatroid99dedb9232016-09-26 13:54:04 -0700191 error =
192 grpc_error_set_str(error, GRPC_ERROR_STR_OS_ERROR, uv_strerror(status));
Craig Tiller91031da2016-12-28 15:44:25 -0800193 grpc_closure_sched(exec_ctx, cb, error);
murgatroid999030c812016-09-16 13:25:08 -0700194 }
195 if (grpc_tcp_trace) {
196 const char *str = grpc_error_string(error);
197 gpr_log(GPR_DEBUG, "Initiating read on %p: error=%s", tcp, str);
198 }
199}
200
201static void write_callback(uv_write_t *req, int status) {
202 grpc_tcp *tcp = req->data;
203 grpc_error *error;
204 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
205 grpc_closure *cb = tcp->write_cb;
206 tcp->write_cb = NULL;
murgatroid99e2672c92016-11-09 15:12:22 -0800207 TCP_UNREF(&exec_ctx, tcp, "write");
murgatroid999030c812016-09-16 13:25:08 -0700208 if (status == 0) {
209 error = GRPC_ERROR_NONE;
210 } else {
211 error = GRPC_ERROR_CREATE("TCP Write failed");
212 }
213 if (grpc_tcp_trace) {
214 const char *str = grpc_error_string(error);
215 gpr_log(GPR_DEBUG, "write complete on %p: error=%s", tcp, str);
216 }
217 gpr_free(tcp->write_buffers);
murgatroid99e2672c92016-11-09 15:12:22 -0800218 grpc_resource_user_free(&exec_ctx, tcp->resource_user,
murgatroid9969259d42016-10-31 14:34:10 -0700219 sizeof(uv_buf_t) * tcp->write_slices->count);
Craig Tiller91031da2016-12-28 15:44:25 -0800220 grpc_closure_sched(&exec_ctx, cb, error);
murgatroid999030c812016-09-16 13:25:08 -0700221 grpc_exec_ctx_finish(&exec_ctx);
222}
223
224static void uv_endpoint_write(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
murgatroid99e2672c92016-11-09 15:12:22 -0800225 grpc_slice_buffer *write_slices,
murgatroid999030c812016-09-16 13:25:08 -0700226 grpc_closure *cb) {
227 grpc_tcp *tcp = (grpc_tcp *)ep;
228 uv_buf_t *buffers;
229 unsigned int buffer_count;
230 unsigned int i;
murgatroid99e2672c92016-11-09 15:12:22 -0800231 grpc_slice *slice;
murgatroid999030c812016-09-16 13:25:08 -0700232 uv_write_t *write_req;
233
234 if (grpc_tcp_trace) {
murgatroid99c36f6ea2016-10-03 09:24:09 -0700235 size_t j;
murgatroid999030c812016-09-16 13:25:08 -0700236
murgatroid99c36f6ea2016-10-03 09:24:09 -0700237 for (j = 0; j < write_slices->count; j++) {
murgatroid99e2672c92016-11-09 15:12:22 -0800238 char *data = grpc_dump_slice(write_slices->slices[j],
murgatroid992e012342016-11-10 18:24:08 -0800239 GPR_DUMP_HEX | GPR_DUMP_ASCII);
murgatroid999030c812016-09-16 13:25:08 -0700240 gpr_log(GPR_DEBUG, "WRITE %p (peer=%s): %s", tcp, tcp->peer_string, data);
241 gpr_free(data);
242 }
243 }
244
245 if (tcp->shutting_down) {
Craig Tiller91031da2016-12-28 15:44:25 -0800246 grpc_closure_sched(exec_ctx, cb,
Craig Tillerfb27cac2017-01-03 09:54:51 -0800247 GRPC_ERROR_CREATE("TCP socket is shutting down"));
murgatroid999030c812016-09-16 13:25:08 -0700248 return;
249 }
250
251 GPR_ASSERT(tcp->write_cb == NULL);
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
Craig Tiller91031da2016-12-28 15:44:25 -0800257 grpc_closure_sched(exec_ctx, cb, GRPC_ERROR_NONE);
murgatroid999030c812016-09-16 13:25:08 -0700258 return;
259 }
260
261 tcp->write_cb = cb;
262 buffer_count = (unsigned int)tcp->write_slices->count;
263 buffers = gpr_malloc(sizeof(uv_buf_t) * buffer_count);
murgatroid99e2672c92016-11-09 15:12:22 -0800264 grpc_resource_user_alloc(exec_ctx, tcp->resource_user,
murgatroid9969259d42016-10-31 14:34:10 -0700265 sizeof(uv_buf_t) * buffer_count, NULL);
murgatroid999030c812016-09-16 13:25:08 -0700266 for (i = 0; i < buffer_count; i++) {
267 slice = &tcp->write_slices->slices[i];
Craig Tiller618e67d2016-10-26 21:08:10 -0700268 buffers[i].base = (char *)GRPC_SLICE_START_PTR(*slice);
269 buffers[i].len = GRPC_SLICE_LENGTH(*slice);
murgatroid999030c812016-09-16 13:25:08 -0700270 }
murgatroid9969259d42016-10-31 14:34:10 -0700271 tcp->write_buffers = buffers;
272 write_req = &tcp->write_req;
murgatroid999030c812016-09-16 13:25:08 -0700273 write_req->data = tcp;
274 TCP_REF(tcp, "write");
275 // TODO(murgatroid99): figure out what the return value here means
276 uv_write(write_req, (uv_stream_t *)tcp->handle, buffers, buffer_count,
277 write_callback);
278}
279
280static void uv_add_to_pollset(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
281 grpc_pollset *pollset) {
282 // No-op. We're ignoring pollsets currently
murgatroid99dedb9232016-09-26 13:54:04 -0700283 (void)exec_ctx;
284 (void)ep;
285 (void)pollset;
286 grpc_tcp *tcp = (grpc_tcp *)ep;
murgatroid999030c812016-09-16 13:25:08 -0700287 tcp->pollset = pollset;
288}
289
290static void uv_add_to_pollset_set(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
291 grpc_pollset_set *pollset) {
292 // No-op. We're ignoring pollsets currently
murgatroid99dedb9232016-09-26 13:54:04 -0700293 (void)exec_ctx;
294 (void)ep;
295 (void)pollset;
murgatroid999030c812016-09-16 13:25:08 -0700296}
297
murgatroid9969259d42016-10-31 14:34:10 -0700298static void shutdown_callback(uv_shutdown_t *req, int status) {}
299
murgatroid999030c812016-09-16 13:25:08 -0700300static void uv_endpoint_shutdown(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep) {
301 grpc_tcp *tcp = (grpc_tcp *)ep;
murgatroid992c287ca2016-10-07 09:55:35 -0700302 if (!tcp->shutting_down) {
303 tcp->shutting_down = true;
murgatroid9969259d42016-10-31 14:34:10 -0700304 uv_shutdown_t *req = &tcp->shutdown_req;
murgatroid992c287ca2016-10-07 09:55:35 -0700305 uv_shutdown(req, (uv_stream_t *)tcp->handle, shutdown_callback);
306 }
murgatroid999030c812016-09-16 13:25:08 -0700307}
308
309static void uv_destroy(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep) {
310 grpc_network_status_unregister_endpoint(ep);
311 grpc_tcp *tcp = (grpc_tcp *)ep;
murgatroid999030c812016-09-16 13:25:08 -0700312 uv_close((uv_handle_t *)tcp->handle, uv_close_callback);
murgatroid99e2672c92016-11-09 15:12:22 -0800313 TCP_UNREF(exec_ctx, tcp, "destroy");
murgatroid999030c812016-09-16 13:25:08 -0700314}
315
316static char *uv_get_peer(grpc_endpoint *ep) {
317 grpc_tcp *tcp = (grpc_tcp *)ep;
318 return gpr_strdup(tcp->peer_string);
319}
320
murgatroid9969259d42016-10-31 14:34:10 -0700321static grpc_resource_user *uv_get_resource_user(grpc_endpoint *ep) {
322 grpc_tcp *tcp = (grpc_tcp *)ep;
murgatroid99e2672c92016-11-09 15:12:22 -0800323 return tcp->resource_user;
murgatroid9969259d42016-10-31 14:34:10 -0700324}
325
murgatroid99dedb9232016-09-26 13:54:04 -0700326static grpc_workqueue *uv_get_workqueue(grpc_endpoint *ep) { return NULL; }
murgatroid999030c812016-09-16 13:25:08 -0700327
murgatroid992e012342016-11-10 18:24:08 -0800328static int uv_get_fd(grpc_endpoint *ep) { return -1; }
329
murgatroid9969259d42016-10-31 14:34:10 -0700330static grpc_endpoint_vtable vtable = {
331 uv_endpoint_read, uv_endpoint_write, uv_get_workqueue,
332 uv_add_to_pollset, uv_add_to_pollset_set, uv_endpoint_shutdown,
murgatroid992e012342016-11-10 18:24:08 -0800333 uv_destroy, uv_get_resource_user, uv_get_peer,
334 uv_get_fd};
murgatroid999030c812016-09-16 13:25:08 -0700335
murgatroid9969259d42016-10-31 14:34:10 -0700336grpc_endpoint *grpc_tcp_create(uv_tcp_t *handle,
337 grpc_resource_quota *resource_quota,
338 char *peer_string) {
murgatroid999030c812016-09-16 13:25:08 -0700339 grpc_tcp *tcp = (grpc_tcp *)gpr_malloc(sizeof(grpc_tcp));
340
341 if (grpc_tcp_trace) {
342 gpr_log(GPR_DEBUG, "Creating TCP endpoint %p", tcp);
343 }
344
murgatroid9904d28292016-10-27 14:36:57 -0700345 /* Disable Nagle's Algorithm */
murgatroid9912e57752016-10-27 14:30:41 -0700346 uv_tcp_nodelay(handle, 1);
347
murgatroid999030c812016-09-16 13:25:08 -0700348 memset(tcp, 0, sizeof(grpc_tcp));
349 tcp->base.vtable = &vtable;
350 tcp->handle = handle;
351 handle->data = tcp;
352 gpr_ref_init(&tcp->refcount, 1);
353 tcp->peer_string = gpr_strdup(peer_string);
murgatroid992c287ca2016-10-07 09:55:35 -0700354 tcp->shutting_down = false;
murgatroid99e2672c92016-11-09 15:12:22 -0800355 tcp->resource_user = grpc_resource_user_create(resource_quota, peer_string);
murgatroid999030c812016-09-16 13:25:08 -0700356 /* Tell network status tracking code about the new endpoint */
357 grpc_network_status_register_endpoint(&tcp->base);
358
359#ifndef GRPC_UV_TCP_HOLD_LOOP
360 uv_unref((uv_handle_t *)handle);
361#endif
362
363 return &tcp->base;
364}
365
366#endif /* GRPC_UV */