blob: 05d6eeb2408df4ff4014e985ecb060299a0484d8 [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
41#include <grpc/support/alloc.h>
42#include <grpc/support/log.h>
43#include <grpc/support/slice_buffer.h>
44#include <grpc/support/string_util.h>
45
46#include "src/core/lib/iomgr/error.h"
47#include "src/core/lib/iomgr/network_status_tracker.h"
48#include "src/core/lib/iomgr/tcp_uv.h"
49#include "src/core/lib/support/string.h"
50
51int grpc_tcp_trace = 0;
52
53typedef struct {
54 grpc_endpoint base;
55 gpr_refcount refcount;
56
57 uv_tcp_t *handle;
58
59 grpc_closure *read_cb;
60 grpc_closure *write_cb;
61
62 gpr_slice read_slice;
63 gpr_slice_buffer *read_slices;
64 gpr_slice_buffer *write_slices;
65 uv_buf_t *write_buffers;
66
murgatroid992c287ca2016-10-07 09:55:35 -070067 bool shutting_down;
murgatroid999030c812016-09-16 13:25:08 -070068 char *peer_string;
69 grpc_pollset *pollset;
70} grpc_tcp;
71
murgatroid99aa9c5782016-10-10 12:16:13 -070072static void uv_close_callback(uv_handle_t *handle) { gpr_free(handle); }
murgatroid999030c812016-09-16 13:25:08 -070073
murgatroid99dedb9232016-09-26 13:54:04 -070074static void tcp_free(grpc_tcp *tcp) { gpr_free(tcp); }
murgatroid999030c812016-09-16 13:25:08 -070075
76/*#define GRPC_TCP_REFCOUNT_DEBUG*/
77#ifdef GRPC_TCP_REFCOUNT_DEBUG
murgatroid99dedb9232016-09-26 13:54:04 -070078#define TCP_UNREF(tcp, reason) tcp_unref((tcp), (reason), __FILE__, __LINE__)
murgatroid999030c812016-09-16 13:25:08 -070079#define TCP_REF(tcp, reason) tcp_ref((tcp), (reason), __FILE__, __LINE__)
80static void tcp_unref(grpc_tcp *tcp, const char *reason, const char *file,
81 int line) {
82 gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "TCP unref %p : %s %d -> %d", tcp,
83 reason, tcp->refcount.count, tcp->refcount.count - 1);
84 if (gpr_unref(&tcp->refcount)) {
85 tcp_free(tcp);
86 }
87}
88
89static void tcp_ref(grpc_tcp *tcp, const char *reason, const char *file,
90 int line) {
91 gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "TCP ref %p : %s %d -> %d", tcp,
92 reason, tcp->refcount.count, tcp->refcount.count + 1);
93 gpr_ref(&tcp->refcount);
94}
95#else
96#define TCP_UNREF(tcp, reason) tcp_unref((tcp))
97#define TCP_REF(tcp, reason) tcp_ref((tcp))
98static void tcp_unref(grpc_tcp *tcp) {
99 if (gpr_unref(&tcp->refcount)) {
100 tcp_free(tcp);
101 }
102}
103
104static void tcp_ref(grpc_tcp *tcp) { gpr_ref(&tcp->refcount); }
105#endif
106
murgatroid99dedb9232016-09-26 13:54:04 -0700107static void alloc_uv_buf(uv_handle_t *handle, size_t suggested_size,
108 uv_buf_t *buf) {
murgatroid999030c812016-09-16 13:25:08 -0700109 grpc_tcp *tcp = handle->data;
110 (void)suggested_size;
111 tcp->read_slice = gpr_slice_malloc(GRPC_TCP_DEFAULT_READ_SLICE_SIZE);
112 buf->base = (char *)GPR_SLICE_START_PTR(tcp->read_slice);
113 buf->len = GPR_SLICE_LENGTH(tcp->read_slice);
114}
115
murgatroid99dedb9232016-09-26 13:54:04 -0700116static void read_callback(uv_stream_t *stream, ssize_t nread,
117 const uv_buf_t *buf) {
murgatroid999030c812016-09-16 13:25:08 -0700118 gpr_slice sub;
119 grpc_error *error;
120 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
121 grpc_tcp *tcp = stream->data;
122 grpc_closure *cb = tcp->read_cb;
123 if (nread == 0) {
124 // Nothing happened. Wait for the next callback
125 return;
126 }
127 TCP_UNREF(tcp, "read");
128 tcp->read_cb = NULL;
129 // TODO(murgatroid99): figure out what the return value here means
130 uv_read_stop(stream);
131 if (nread == UV_EOF) {
132 error = GRPC_ERROR_CREATE("EOF");
133 } else if (nread > 0) {
134 // Successful read
murgatroid99c36f6ea2016-10-03 09:24:09 -0700135 sub = gpr_slice_sub_no_ref(tcp->read_slice, 0, (size_t)nread);
murgatroid999030c812016-09-16 13:25:08 -0700136 gpr_slice_buffer_add(tcp->read_slices, sub);
137 error = GRPC_ERROR_NONE;
138 if (grpc_tcp_trace) {
139 size_t i;
140 const char *str = grpc_error_string(error);
141 gpr_log(GPR_DEBUG, "read: error=%s", str);
142 grpc_error_free_string(str);
143 for (i = 0; i < tcp->read_slices->count; i++) {
144 char *dump = gpr_dump_slice(tcp->read_slices->slices[i],
145 GPR_DUMP_HEX | GPR_DUMP_ASCII);
murgatroid99dedb9232016-09-26 13:54:04 -0700146 gpr_log(GPR_DEBUG, "READ %p (peer=%s): %s", tcp, tcp->peer_string,
147 dump);
murgatroid999030c812016-09-16 13:25:08 -0700148 gpr_free(dump);
149 }
150 }
151 } else {
152 // nread < 0: Error
153 error = GRPC_ERROR_CREATE("TCP Read failed");
154 }
155 grpc_exec_ctx_sched(&exec_ctx, cb, error, NULL);
156 grpc_exec_ctx_finish(&exec_ctx);
157}
158
159static void uv_endpoint_read(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
160 gpr_slice_buffer *read_slices, grpc_closure *cb) {
161 grpc_tcp *tcp = (grpc_tcp *)ep;
162 int status;
163 grpc_error *error = GRPC_ERROR_NONE;
164 GPR_ASSERT(tcp->read_cb == NULL);
165 tcp->read_cb = cb;
166 tcp->read_slices = read_slices;
167 gpr_slice_buffer_reset_and_unref(read_slices);
168 TCP_REF(tcp, "read");
169 // TODO(murgatroid99): figure out what the return value here means
murgatroid99dedb9232016-09-26 13:54:04 -0700170 status =
171 uv_read_start((uv_stream_t *)tcp->handle, alloc_uv_buf, read_callback);
murgatroid999030c812016-09-16 13:25:08 -0700172 if (status != 0) {
173 error = GRPC_ERROR_CREATE("TCP Read failed at start");
murgatroid99dedb9232016-09-26 13:54:04 -0700174 error =
175 grpc_error_set_str(error, GRPC_ERROR_STR_OS_ERROR, uv_strerror(status));
murgatroid999030c812016-09-16 13:25:08 -0700176 grpc_exec_ctx_sched(exec_ctx, cb, error, NULL);
177 }
178 if (grpc_tcp_trace) {
179 const char *str = grpc_error_string(error);
180 gpr_log(GPR_DEBUG, "Initiating read on %p: error=%s", tcp, str);
181 }
182}
183
184static void write_callback(uv_write_t *req, int status) {
185 grpc_tcp *tcp = req->data;
186 grpc_error *error;
187 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
188 grpc_closure *cb = tcp->write_cb;
189 tcp->write_cb = NULL;
190 TCP_UNREF(tcp, "write");
191 if (status == 0) {
192 error = GRPC_ERROR_NONE;
193 } else {
194 error = GRPC_ERROR_CREATE("TCP Write failed");
195 }
196 if (grpc_tcp_trace) {
197 const char *str = grpc_error_string(error);
198 gpr_log(GPR_DEBUG, "write complete on %p: error=%s", tcp, str);
199 }
200 gpr_free(tcp->write_buffers);
201 gpr_free(req);
202 grpc_exec_ctx_sched(&exec_ctx, cb, error, NULL);
203 grpc_exec_ctx_finish(&exec_ctx);
204}
205
206static void uv_endpoint_write(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
207 gpr_slice_buffer *write_slices,
208 grpc_closure *cb) {
209 grpc_tcp *tcp = (grpc_tcp *)ep;
210 uv_buf_t *buffers;
211 unsigned int buffer_count;
212 unsigned int i;
213 gpr_slice *slice;
214 uv_write_t *write_req;
215
216 if (grpc_tcp_trace) {
murgatroid99c36f6ea2016-10-03 09:24:09 -0700217 size_t j;
murgatroid999030c812016-09-16 13:25:08 -0700218
murgatroid99c36f6ea2016-10-03 09:24:09 -0700219 for (j = 0; j < write_slices->count; j++) {
220 char *data = gpr_dump_slice(write_slices->slices[j],
murgatroid99dedb9232016-09-26 13:54:04 -0700221 GPR_DUMP_HEX | GPR_DUMP_ASCII);
murgatroid999030c812016-09-16 13:25:08 -0700222 gpr_log(GPR_DEBUG, "WRITE %p (peer=%s): %s", tcp, tcp->peer_string, data);
223 gpr_free(data);
224 }
225 }
226
227 if (tcp->shutting_down) {
228 grpc_exec_ctx_sched(exec_ctx, cb,
229 GRPC_ERROR_CREATE("TCP socket is shutting down"), NULL);
230 return;
231 }
232
233 GPR_ASSERT(tcp->write_cb == NULL);
234 tcp->write_slices = write_slices;
235 GPR_ASSERT(tcp->write_slices->count <= UINT_MAX);
236 if (tcp->write_slices->count == 0) {
237 // No slices means we don't have to do anything,
238 // and libuv doesn't like empty writes
239 grpc_exec_ctx_sched(exec_ctx, cb, GRPC_ERROR_NONE, NULL);
240 return;
241 }
242
243 tcp->write_cb = cb;
244 buffer_count = (unsigned int)tcp->write_slices->count;
245 buffers = gpr_malloc(sizeof(uv_buf_t) * buffer_count);
246 for (i = 0; i < buffer_count; i++) {
247 slice = &tcp->write_slices->slices[i];
248 buffers[i].base = (char *)GPR_SLICE_START_PTR(*slice);
249 buffers[i].len = GPR_SLICE_LENGTH(*slice);
250 }
251 write_req = gpr_malloc(sizeof(uv_write_t));
252 write_req->data = tcp;
253 TCP_REF(tcp, "write");
254 // TODO(murgatroid99): figure out what the return value here means
255 uv_write(write_req, (uv_stream_t *)tcp->handle, buffers, buffer_count,
256 write_callback);
257}
258
259static void uv_add_to_pollset(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
260 grpc_pollset *pollset) {
261 // No-op. We're ignoring pollsets currently
murgatroid99dedb9232016-09-26 13:54:04 -0700262 (void)exec_ctx;
263 (void)ep;
264 (void)pollset;
265 grpc_tcp *tcp = (grpc_tcp *)ep;
murgatroid999030c812016-09-16 13:25:08 -0700266 tcp->pollset = pollset;
267}
268
269static void uv_add_to_pollset_set(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
270 grpc_pollset_set *pollset) {
271 // No-op. We're ignoring pollsets currently
murgatroid99dedb9232016-09-26 13:54:04 -0700272 (void)exec_ctx;
273 (void)ep;
274 (void)pollset;
murgatroid999030c812016-09-16 13:25:08 -0700275}
276
murgatroid99dedb9232016-09-26 13:54:04 -0700277static void shutdown_callback(uv_shutdown_t *req, int status) { gpr_free(req); }
murgatroid999030c812016-09-16 13:25:08 -0700278
279static void uv_endpoint_shutdown(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep) {
280 grpc_tcp *tcp = (grpc_tcp *)ep;
murgatroid992c287ca2016-10-07 09:55:35 -0700281 if (!tcp->shutting_down) {
282 tcp->shutting_down = true;
283 uv_shutdown_t *req = gpr_malloc(sizeof(uv_shutdown_t));
284 uv_shutdown(req, (uv_stream_t *)tcp->handle, shutdown_callback);
285 }
murgatroid999030c812016-09-16 13:25:08 -0700286}
287
288static void uv_destroy(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep) {
289 grpc_network_status_unregister_endpoint(ep);
290 grpc_tcp *tcp = (grpc_tcp *)ep;
murgatroid999030c812016-09-16 13:25:08 -0700291 uv_close((uv_handle_t *)tcp->handle, uv_close_callback);
292 TCP_UNREF(tcp, "destroy");
293}
294
295static char *uv_get_peer(grpc_endpoint *ep) {
296 grpc_tcp *tcp = (grpc_tcp *)ep;
297 return gpr_strdup(tcp->peer_string);
298}
299
murgatroid99dedb9232016-09-26 13:54:04 -0700300static grpc_workqueue *uv_get_workqueue(grpc_endpoint *ep) { return NULL; }
murgatroid999030c812016-09-16 13:25:08 -0700301
302static grpc_endpoint_vtable vtable = {uv_endpoint_read,
303 uv_endpoint_write,
304 uv_get_workqueue,
305 uv_add_to_pollset,
306 uv_add_to_pollset_set,
307 uv_endpoint_shutdown,
308 uv_destroy,
309 uv_get_peer};
310
311grpc_endpoint *grpc_tcp_create(uv_tcp_t *handle, char *peer_string) {
312 grpc_tcp *tcp = (grpc_tcp *)gpr_malloc(sizeof(grpc_tcp));
313
314 if (grpc_tcp_trace) {
315 gpr_log(GPR_DEBUG, "Creating TCP endpoint %p", tcp);
316 }
317
318 memset(tcp, 0, sizeof(grpc_tcp));
319 tcp->base.vtable = &vtable;
320 tcp->handle = handle;
321 handle->data = tcp;
322 gpr_ref_init(&tcp->refcount, 1);
323 tcp->peer_string = gpr_strdup(peer_string);
murgatroid992c287ca2016-10-07 09:55:35 -0700324 tcp->shutting_down = false;
murgatroid999030c812016-09-16 13:25:08 -0700325 /* Tell network status tracking code about the new endpoint */
326 grpc_network_status_register_endpoint(&tcp->base);
327
328#ifndef GRPC_UV_TCP_HOLD_LOOP
329 uv_unref((uv_handle_t *)handle);
330#endif
331
332 return &tcp->base;
333}
334
335#endif /* GRPC_UV */