murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
murgatroid99 | e2672c9 | 2016-11-09 15:12:22 -0800 | [diff] [blame] | 41 | #include <grpc/slice_buffer.h> |
| 42 | |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 43 | #include <grpc/support/alloc.h> |
| 44 | #include <grpc/support/log.h> |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 45 | #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" |
murgatroid99 | e2672c9 | 2016-11-09 15:12:22 -0800 | [diff] [blame] | 49 | #include "src/core/lib/iomgr/resource_quota.h" |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 50 | #include "src/core/lib/iomgr/tcp_uv.h" |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 51 | #include "src/core/lib/slice/slice_internal.h" |
murgatroid99 | e2672c9 | 2016-11-09 15:12:22 -0800 | [diff] [blame] | 52 | #include "src/core/lib/slice/slice_string_helpers.h" |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 53 | #include "src/core/lib/support/string.h" |
| 54 | |
Craig Tiller | 020176d | 2017-05-09 08:36:44 -0700 | [diff] [blame] | 55 | grpc_tracer_flag grpc_tcp_trace = GRPC_TRACER_INITIALIZER(false); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 56 | |
| 57 | typedef struct { |
| 58 | grpc_endpoint base; |
| 59 | gpr_refcount refcount; |
| 60 | |
murgatroid99 | 69259d4 | 2016-10-31 14:34:10 -0700 | [diff] [blame] | 61 | uv_write_t write_req; |
| 62 | uv_shutdown_t shutdown_req; |
| 63 | |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 64 | uv_tcp_t *handle; |
| 65 | |
| 66 | grpc_closure *read_cb; |
| 67 | grpc_closure *write_cb; |
| 68 | |
murgatroid99 | e2672c9 | 2016-11-09 15:12:22 -0800 | [diff] [blame] | 69 | grpc_slice read_slice; |
| 70 | grpc_slice_buffer *read_slices; |
| 71 | grpc_slice_buffer *write_slices; |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 72 | uv_buf_t *write_buffers; |
| 73 | |
murgatroid99 | e2672c9 | 2016-11-09 15:12:22 -0800 | [diff] [blame] | 74 | grpc_resource_user *resource_user; |
murgatroid99 | 69259d4 | 2016-10-31 14:34:10 -0700 | [diff] [blame] | 75 | |
murgatroid99 | 2c287ca | 2016-10-07 09:55:35 -0700 | [diff] [blame] | 76 | bool shutting_down; |
murgatroid99 | 69259d4 | 2016-10-31 14:34:10 -0700 | [diff] [blame] | 77 | |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 78 | char *peer_string; |
| 79 | grpc_pollset *pollset; |
| 80 | } grpc_tcp; |
| 81 | |
murgatroid99 | e2672c9 | 2016-11-09 15:12:22 -0800 | [diff] [blame] | 82 | static void tcp_free(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp) { |
| 83 | grpc_resource_user_unref(exec_ctx, tcp->resource_user); |
murgatroid99 | 69259d4 | 2016-10-31 14:34:10 -0700 | [diff] [blame] | 84 | gpr_free(tcp); |
murgatroid99 | 69259d4 | 2016-10-31 14:34:10 -0700 | [diff] [blame] | 85 | } |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 86 | |
| 87 | /*#define GRPC_TCP_REFCOUNT_DEBUG*/ |
| 88 | #ifdef GRPC_TCP_REFCOUNT_DEBUG |
murgatroid99 | 2e01234 | 2016-11-10 18:24:08 -0800 | [diff] [blame] | 89 | #define TCP_UNREF(exec_ctx, tcp, reason) \ |
| 90 | tcp_unref((exec_ctx), (tcp), (reason), __FILE__, __LINE__) |
murgatroid99 | a1137ce | 2017-05-09 14:21:12 -0700 | [diff] [blame] | 91 | #define TCP_REF(tcp, reason) tcp_ref((tcp), (reason), __FILE__, __LINE__) |
murgatroid99 | 2e01234 | 2016-11-10 18:24:08 -0800 | [diff] [blame] | 92 | static void tcp_unref(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp, |
| 93 | const char *reason, const char *file, int line) { |
murgatroid99 | a1137ce | 2017-05-09 14:21:12 -0700 | [diff] [blame] | 94 | gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, |
| 95 | "TCP unref %p : %s %" PRIiPTR " -> %" PRIiPTR, tcp, reason, |
| 96 | tcp->refcount.count, tcp->refcount.count - 1); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 97 | if (gpr_unref(&tcp->refcount)) { |
murgatroid99 | e2672c9 | 2016-11-09 15:12:22 -0800 | [diff] [blame] | 98 | tcp_free(exec_ctx, tcp); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
| 102 | static void tcp_ref(grpc_tcp *tcp, const char *reason, const char *file, |
| 103 | int line) { |
murgatroid99 | a1137ce | 2017-05-09 14:21:12 -0700 | [diff] [blame] | 104 | gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, |
| 105 | "TCP ref %p : %s %" PRIiPTR " -> %" PRIiPTR, tcp, reason, |
| 106 | tcp->refcount.count, tcp->refcount.count + 1); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 107 | gpr_ref(&tcp->refcount); |
| 108 | } |
| 109 | #else |
murgatroid99 | e2672c9 | 2016-11-09 15:12:22 -0800 | [diff] [blame] | 110 | #define TCP_UNREF(exec_ctx, tcp, reason) tcp_unref((exec_ctx), (tcp)) |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 111 | #define TCP_REF(tcp, reason) tcp_ref((tcp)) |
murgatroid99 | e2672c9 | 2016-11-09 15:12:22 -0800 | [diff] [blame] | 112 | static void tcp_unref(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp) { |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 113 | if (gpr_unref(&tcp->refcount)) { |
murgatroid99 | e2672c9 | 2016-11-09 15:12:22 -0800 | [diff] [blame] | 114 | tcp_free(exec_ctx, tcp); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
| 118 | static void tcp_ref(grpc_tcp *tcp) { gpr_ref(&tcp->refcount); } |
| 119 | #endif |
| 120 | |
murgatroid99 | 1191b72 | 2017-02-08 11:56:52 -0800 | [diff] [blame] | 121 | static void uv_close_callback(uv_handle_t *handle) { |
| 122 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 123 | grpc_tcp *tcp = handle->data; |
| 124 | TCP_UNREF(&exec_ctx, tcp, "destroy"); |
| 125 | grpc_exec_ctx_finish(&exec_ctx); |
| 126 | } |
| 127 | |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 128 | static void alloc_uv_buf(uv_handle_t *handle, size_t suggested_size, |
| 129 | uv_buf_t *buf) { |
murgatroid99 | 69259d4 | 2016-10-31 14:34:10 -0700 | [diff] [blame] | 130 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 131 | grpc_tcp *tcp = handle->data; |
| 132 | (void)suggested_size; |
murgatroid99 | 69259d4 | 2016-10-31 14:34:10 -0700 | [diff] [blame] | 133 | tcp->read_slice = grpc_resource_user_slice_malloc( |
murgatroid99 | e2672c9 | 2016-11-09 15:12:22 -0800 | [diff] [blame] | 134 | &exec_ctx, tcp->resource_user, GRPC_TCP_DEFAULT_READ_SLICE_SIZE); |
Craig Tiller | 618e67d | 2016-10-26 21:08:10 -0700 | [diff] [blame] | 135 | buf->base = (char *)GRPC_SLICE_START_PTR(tcp->read_slice); |
| 136 | buf->len = GRPC_SLICE_LENGTH(tcp->read_slice); |
murgatroid99 | 69259d4 | 2016-10-31 14:34:10 -0700 | [diff] [blame] | 137 | grpc_exec_ctx_finish(&exec_ctx); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 138 | } |
| 139 | |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 140 | static void read_callback(uv_stream_t *stream, ssize_t nread, |
| 141 | const uv_buf_t *buf) { |
murgatroid99 | e2672c9 | 2016-11-09 15:12:22 -0800 | [diff] [blame] | 142 | grpc_slice sub; |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 143 | grpc_error *error; |
| 144 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 145 | grpc_tcp *tcp = stream->data; |
| 146 | grpc_closure *cb = tcp->read_cb; |
| 147 | if (nread == 0) { |
| 148 | // Nothing happened. Wait for the next callback |
| 149 | return; |
| 150 | } |
murgatroid99 | e2672c9 | 2016-11-09 15:12:22 -0800 | [diff] [blame] | 151 | TCP_UNREF(&exec_ctx, tcp, "read"); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 152 | tcp->read_cb = NULL; |
| 153 | // TODO(murgatroid99): figure out what the return value here means |
| 154 | uv_read_stop(stream); |
| 155 | if (nread == UV_EOF) { |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 156 | error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("EOF"); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 157 | } else if (nread > 0) { |
| 158 | // Successful read |
murgatroid99 | e2672c9 | 2016-11-09 15:12:22 -0800 | [diff] [blame] | 159 | sub = grpc_slice_sub_no_ref(tcp->read_slice, 0, (size_t)nread); |
| 160 | grpc_slice_buffer_add(tcp->read_slices, sub); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 161 | error = GRPC_ERROR_NONE; |
Craig Tiller | 341bcc5 | 2017-05-09 08:37:44 -0700 | [diff] [blame] | 162 | if (GRPC_TRACER_ON(grpc_tcp_trace)) { |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 163 | size_t i; |
| 164 | const char *str = grpc_error_string(error); |
| 165 | gpr_log(GPR_DEBUG, "read: error=%s", str); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 166 | |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 167 | for (i = 0; i < tcp->read_slices->count; i++) { |
murgatroid99 | e2672c9 | 2016-11-09 15:12:22 -0800 | [diff] [blame] | 168 | char *dump = grpc_dump_slice(tcp->read_slices->slices[i], |
murgatroid99 | 2e01234 | 2016-11-10 18:24:08 -0800 | [diff] [blame] | 169 | GPR_DUMP_HEX | GPR_DUMP_ASCII); |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 170 | gpr_log(GPR_DEBUG, "READ %p (peer=%s): %s", tcp, tcp->peer_string, |
| 171 | dump); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 172 | gpr_free(dump); |
| 173 | } |
| 174 | } |
| 175 | } else { |
| 176 | // nread < 0: Error |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 177 | error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("TCP Read failed"); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 178 | } |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 179 | grpc_closure_sched(&exec_ctx, cb, error); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 180 | grpc_exec_ctx_finish(&exec_ctx); |
| 181 | } |
| 182 | |
| 183 | static void uv_endpoint_read(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
murgatroid99 | e2672c9 | 2016-11-09 15:12:22 -0800 | [diff] [blame] | 184 | grpc_slice_buffer *read_slices, grpc_closure *cb) { |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 185 | grpc_tcp *tcp = (grpc_tcp *)ep; |
| 186 | int status; |
| 187 | grpc_error *error = GRPC_ERROR_NONE; |
| 188 | GPR_ASSERT(tcp->read_cb == NULL); |
| 189 | tcp->read_cb = cb; |
| 190 | tcp->read_slices = read_slices; |
Craig Tiller | ab7b2d8 | 2016-12-07 07:26:34 -0800 | [diff] [blame] | 191 | grpc_slice_buffer_reset_and_unref_internal(exec_ctx, read_slices); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 192 | TCP_REF(tcp, "read"); |
| 193 | // TODO(murgatroid99): figure out what the return value here means |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 194 | status = |
| 195 | uv_read_start((uv_stream_t *)tcp->handle, alloc_uv_buf, read_callback); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 196 | if (status != 0) { |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 197 | error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("TCP Read failed at start"); |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 198 | error = |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 199 | grpc_error_set_str(error, GRPC_ERROR_STR_OS_ERROR, |
| 200 | grpc_slice_from_static_string(uv_strerror(status))); |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 201 | grpc_closure_sched(exec_ctx, cb, error); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 202 | } |
Craig Tiller | 341bcc5 | 2017-05-09 08:37:44 -0700 | [diff] [blame] | 203 | if (GRPC_TRACER_ON(grpc_tcp_trace)) { |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 204 | const char *str = grpc_error_string(error); |
| 205 | gpr_log(GPR_DEBUG, "Initiating read on %p: error=%s", tcp, str); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | static void write_callback(uv_write_t *req, int status) { |
| 210 | grpc_tcp *tcp = req->data; |
| 211 | grpc_error *error; |
| 212 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 213 | grpc_closure *cb = tcp->write_cb; |
| 214 | tcp->write_cb = NULL; |
murgatroid99 | e2672c9 | 2016-11-09 15:12:22 -0800 | [diff] [blame] | 215 | TCP_UNREF(&exec_ctx, tcp, "write"); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 216 | if (status == 0) { |
| 217 | error = GRPC_ERROR_NONE; |
| 218 | } else { |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 219 | error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("TCP Write failed"); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 220 | } |
Craig Tiller | 341bcc5 | 2017-05-09 08:37:44 -0700 | [diff] [blame] | 221 | if (GRPC_TRACER_ON(grpc_tcp_trace)) { |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 222 | const char *str = grpc_error_string(error); |
| 223 | gpr_log(GPR_DEBUG, "write complete on %p: error=%s", tcp, str); |
| 224 | } |
| 225 | gpr_free(tcp->write_buffers); |
murgatroid99 | e2672c9 | 2016-11-09 15:12:22 -0800 | [diff] [blame] | 226 | grpc_resource_user_free(&exec_ctx, tcp->resource_user, |
murgatroid99 | 69259d4 | 2016-10-31 14:34:10 -0700 | [diff] [blame] | 227 | sizeof(uv_buf_t) * tcp->write_slices->count); |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 228 | grpc_closure_sched(&exec_ctx, cb, error); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 229 | grpc_exec_ctx_finish(&exec_ctx); |
| 230 | } |
| 231 | |
| 232 | static void uv_endpoint_write(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
murgatroid99 | e2672c9 | 2016-11-09 15:12:22 -0800 | [diff] [blame] | 233 | grpc_slice_buffer *write_slices, |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 234 | grpc_closure *cb) { |
| 235 | grpc_tcp *tcp = (grpc_tcp *)ep; |
| 236 | uv_buf_t *buffers; |
| 237 | unsigned int buffer_count; |
| 238 | unsigned int i; |
murgatroid99 | e2672c9 | 2016-11-09 15:12:22 -0800 | [diff] [blame] | 239 | grpc_slice *slice; |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 240 | uv_write_t *write_req; |
| 241 | |
Craig Tiller | 341bcc5 | 2017-05-09 08:37:44 -0700 | [diff] [blame] | 242 | if (GRPC_TRACER_ON(grpc_tcp_trace)) { |
murgatroid99 | c36f6ea | 2016-10-03 09:24:09 -0700 | [diff] [blame] | 243 | size_t j; |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 244 | |
murgatroid99 | c36f6ea | 2016-10-03 09:24:09 -0700 | [diff] [blame] | 245 | for (j = 0; j < write_slices->count; j++) { |
murgatroid99 | e2672c9 | 2016-11-09 15:12:22 -0800 | [diff] [blame] | 246 | char *data = grpc_dump_slice(write_slices->slices[j], |
murgatroid99 | 2e01234 | 2016-11-10 18:24:08 -0800 | [diff] [blame] | 247 | GPR_DUMP_HEX | GPR_DUMP_ASCII); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 248 | gpr_log(GPR_DEBUG, "WRITE %p (peer=%s): %s", tcp, tcp->peer_string, data); |
| 249 | gpr_free(data); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | if (tcp->shutting_down) { |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 254 | grpc_closure_sched(exec_ctx, cb, GRPC_ERROR_CREATE_FROM_STATIC_STRING( |
| 255 | "TCP socket is shutting down")); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 256 | return; |
| 257 | } |
| 258 | |
| 259 | GPR_ASSERT(tcp->write_cb == NULL); |
| 260 | tcp->write_slices = write_slices; |
| 261 | GPR_ASSERT(tcp->write_slices->count <= UINT_MAX); |
| 262 | if (tcp->write_slices->count == 0) { |
| 263 | // No slices means we don't have to do anything, |
| 264 | // and libuv doesn't like empty writes |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 265 | grpc_closure_sched(exec_ctx, cb, GRPC_ERROR_NONE); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 266 | return; |
| 267 | } |
| 268 | |
| 269 | tcp->write_cb = cb; |
| 270 | buffer_count = (unsigned int)tcp->write_slices->count; |
| 271 | buffers = gpr_malloc(sizeof(uv_buf_t) * buffer_count); |
murgatroid99 | e2672c9 | 2016-11-09 15:12:22 -0800 | [diff] [blame] | 272 | grpc_resource_user_alloc(exec_ctx, tcp->resource_user, |
murgatroid99 | 69259d4 | 2016-10-31 14:34:10 -0700 | [diff] [blame] | 273 | sizeof(uv_buf_t) * buffer_count, NULL); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 274 | for (i = 0; i < buffer_count; i++) { |
| 275 | slice = &tcp->write_slices->slices[i]; |
Craig Tiller | 618e67d | 2016-10-26 21:08:10 -0700 | [diff] [blame] | 276 | buffers[i].base = (char *)GRPC_SLICE_START_PTR(*slice); |
| 277 | buffers[i].len = GRPC_SLICE_LENGTH(*slice); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 278 | } |
murgatroid99 | 69259d4 | 2016-10-31 14:34:10 -0700 | [diff] [blame] | 279 | tcp->write_buffers = buffers; |
| 280 | write_req = &tcp->write_req; |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 281 | write_req->data = tcp; |
| 282 | TCP_REF(tcp, "write"); |
| 283 | // TODO(murgatroid99): figure out what the return value here means |
| 284 | uv_write(write_req, (uv_stream_t *)tcp->handle, buffers, buffer_count, |
| 285 | write_callback); |
| 286 | } |
| 287 | |
| 288 | static void uv_add_to_pollset(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
| 289 | grpc_pollset *pollset) { |
| 290 | // No-op. We're ignoring pollsets currently |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 291 | (void)exec_ctx; |
| 292 | (void)ep; |
| 293 | (void)pollset; |
| 294 | grpc_tcp *tcp = (grpc_tcp *)ep; |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 295 | tcp->pollset = pollset; |
| 296 | } |
| 297 | |
| 298 | static void uv_add_to_pollset_set(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
| 299 | grpc_pollset_set *pollset) { |
| 300 | // No-op. We're ignoring pollsets currently |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 301 | (void)exec_ctx; |
| 302 | (void)ep; |
| 303 | (void)pollset; |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 304 | } |
| 305 | |
murgatroid99 | 69259d4 | 2016-10-31 14:34:10 -0700 | [diff] [blame] | 306 | static void shutdown_callback(uv_shutdown_t *req, int status) {} |
| 307 | |
Craig Tiller | 22f13fb | 2017-01-27 11:43:25 -0800 | [diff] [blame] | 308 | static void uv_endpoint_shutdown(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
| 309 | grpc_error *why) { |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 310 | grpc_tcp *tcp = (grpc_tcp *)ep; |
murgatroid99 | 2c287ca | 2016-10-07 09:55:35 -0700 | [diff] [blame] | 311 | if (!tcp->shutting_down) { |
| 312 | tcp->shutting_down = true; |
murgatroid99 | 69259d4 | 2016-10-31 14:34:10 -0700 | [diff] [blame] | 313 | uv_shutdown_t *req = &tcp->shutdown_req; |
murgatroid99 | 2c287ca | 2016-10-07 09:55:35 -0700 | [diff] [blame] | 314 | uv_shutdown(req, (uv_stream_t *)tcp->handle, shutdown_callback); |
murgatroid99 | a1137ce | 2017-05-09 14:21:12 -0700 | [diff] [blame] | 315 | grpc_resource_user_shutdown(exec_ctx, tcp->resource_user); |
murgatroid99 | 2c287ca | 2016-10-07 09:55:35 -0700 | [diff] [blame] | 316 | } |
Craig Tiller | 22f13fb | 2017-01-27 11:43:25 -0800 | [diff] [blame] | 317 | GRPC_ERROR_UNREF(why); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | static void uv_destroy(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep) { |
| 321 | grpc_network_status_unregister_endpoint(ep); |
| 322 | grpc_tcp *tcp = (grpc_tcp *)ep; |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 323 | uv_close((uv_handle_t *)tcp->handle, uv_close_callback); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | static char *uv_get_peer(grpc_endpoint *ep) { |
| 327 | grpc_tcp *tcp = (grpc_tcp *)ep; |
| 328 | return gpr_strdup(tcp->peer_string); |
| 329 | } |
| 330 | |
murgatroid99 | 69259d4 | 2016-10-31 14:34:10 -0700 | [diff] [blame] | 331 | static grpc_resource_user *uv_get_resource_user(grpc_endpoint *ep) { |
| 332 | grpc_tcp *tcp = (grpc_tcp *)ep; |
murgatroid99 | e2672c9 | 2016-11-09 15:12:22 -0800 | [diff] [blame] | 333 | return tcp->resource_user; |
murgatroid99 | 69259d4 | 2016-10-31 14:34:10 -0700 | [diff] [blame] | 334 | } |
| 335 | |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 336 | static grpc_workqueue *uv_get_workqueue(grpc_endpoint *ep) { return NULL; } |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 337 | |
murgatroid99 | 2e01234 | 2016-11-10 18:24:08 -0800 | [diff] [blame] | 338 | static int uv_get_fd(grpc_endpoint *ep) { return -1; } |
| 339 | |
murgatroid99 | 69259d4 | 2016-10-31 14:34:10 -0700 | [diff] [blame] | 340 | static grpc_endpoint_vtable vtable = { |
| 341 | uv_endpoint_read, uv_endpoint_write, uv_get_workqueue, |
| 342 | uv_add_to_pollset, uv_add_to_pollset_set, uv_endpoint_shutdown, |
murgatroid99 | 2e01234 | 2016-11-10 18:24:08 -0800 | [diff] [blame] | 343 | uv_destroy, uv_get_resource_user, uv_get_peer, |
| 344 | uv_get_fd}; |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 345 | |
murgatroid99 | 69259d4 | 2016-10-31 14:34:10 -0700 | [diff] [blame] | 346 | grpc_endpoint *grpc_tcp_create(uv_tcp_t *handle, |
| 347 | grpc_resource_quota *resource_quota, |
| 348 | char *peer_string) { |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 349 | grpc_tcp *tcp = (grpc_tcp *)gpr_malloc(sizeof(grpc_tcp)); |
| 350 | |
Craig Tiller | 341bcc5 | 2017-05-09 08:37:44 -0700 | [diff] [blame] | 351 | if (GRPC_TRACER_ON(grpc_tcp_trace)) { |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 352 | gpr_log(GPR_DEBUG, "Creating TCP endpoint %p", tcp); |
| 353 | } |
| 354 | |
murgatroid99 | 04d2829 | 2016-10-27 14:36:57 -0700 | [diff] [blame] | 355 | /* Disable Nagle's Algorithm */ |
murgatroid99 | 12e5775 | 2016-10-27 14:30:41 -0700 | [diff] [blame] | 356 | uv_tcp_nodelay(handle, 1); |
| 357 | |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 358 | memset(tcp, 0, sizeof(grpc_tcp)); |
| 359 | tcp->base.vtable = &vtable; |
| 360 | tcp->handle = handle; |
| 361 | handle->data = tcp; |
| 362 | gpr_ref_init(&tcp->refcount, 1); |
| 363 | tcp->peer_string = gpr_strdup(peer_string); |
murgatroid99 | 2c287ca | 2016-10-07 09:55:35 -0700 | [diff] [blame] | 364 | tcp->shutting_down = false; |
murgatroid99 | e2672c9 | 2016-11-09 15:12:22 -0800 | [diff] [blame] | 365 | tcp->resource_user = grpc_resource_user_create(resource_quota, peer_string); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 366 | /* Tell network status tracking code about the new endpoint */ |
| 367 | grpc_network_status_register_endpoint(&tcp->base); |
| 368 | |
| 369 | #ifndef GRPC_UV_TCP_HOLD_LOOP |
| 370 | uv_unref((uv_handle_t *)handle); |
| 371 | #endif |
| 372 | |
| 373 | return &tcp->base; |
| 374 | } |
| 375 | |
| 376 | #endif /* GRPC_UV */ |